From 6bd442f543972b072ef2cbbcf2f7c91202de1045 Mon Sep 17 00:00:00 2001 From: Myles Watson Date: Wed, 19 Oct 2016 09:50:22 -0700 Subject: [PATCH] btif: Apply clang-format clang-format doesn't understand block quotes of this form: /* This is not handled well ** because there are two asterisks **/ cd btif/ # Replace '**' at the beginning of the line with ' *' sed 's/^[*][*]/ \*/' -i include/* src/* test/* co/* clang-format --style=file -i src/* include/* test/* co/* Test: mma -j32 Change-Id: I2477eae5480602d5b2fee5ec89c9ed7888022341 --- btif/co/bta_ag_co.cc | 179 +- btif/co/bta_av_co.cc | 1472 +++-- btif/co/bta_dm_co.cc | 674 ++- btif/co/bta_gatts_co.cc | 202 +- btif/co/bta_hh_co.cc | 996 ++-- btif/co/bta_hl_co.cc | 698 ++- btif/co/bta_pan_co.cc | 539 +- btif/include/btif_api.h | 694 +-- btif/include/btif_av.h | 215 +- btif/include/btif_av_co.h | 17 +- btif/include/btif_avrcp_audio_track.h | 19 +- btif/include/btif_common.h | 210 +- btif/include/btif_config.h | 44 +- btif/include/btif_config_transcode.h | 2 +- btif/include/btif_debug_conn.h | 5 +- btif/include/btif_dm.h | 80 +- btif/include/btif_gatt.h | 14 +- btif/include/btif_gatt_util.h | 21 +- btif/include/btif_hh.h | 104 +- btif/include/btif_hl.h | 467 +- btif/include/btif_mce.h | 2 +- btif/include/btif_pan.h | 2 +- btif/include/btif_pan_internal.h | 109 +- btif/include/btif_profile_queue.h | 5 +- btif/include/btif_sdp.h | 2 +- btif/include/btif_sm.h | 125 +- btif/include/btif_sock.h | 2 +- btif/include/btif_sock_l2cap.h | 12 +- btif/include/btif_sock_rfc.h | 10 +- btif/include/btif_sock_sco.h | 7 +- btif/include/btif_sock_sdp.h | 36 +- btif/include/btif_sock_thread.h | 33 +- btif/include/btif_storage.h | 323 +- btif/include/btif_util.h | 23 +- btif/include/stack_manager.h | 7 +- btif/include/uinput.h | 1005 ++-- btif/src/bluetooth.cc | 463 +- btif/src/btif_av.cc | 2792 +++++---- btif/src/btif_avrcp_audio_track.cc | 186 +- btif/src/btif_config.cc | 280 +- btif/src/btif_config_transcode.cc | 28 +- btif/src/btif_core.cc | 1960 +++--- btif/src/btif_debug.cc | 2 +- btif/src/btif_debug_btsnoop.cc | 61 +- btif/src/btif_debug_conn.cc | 39 +- btif/src/btif_dm.cc | 5976 +++++++++---------- btif/src/btif_gatt.cc | 72 +- btif/src/btif_gatt_client.cc | 214 +- btif/src/btif_gatt_server.cc | 467 +- btif/src/btif_gatt_test.cc | 438 +- btif/src/btif_gatt_util.cc | 401 +- btif/src/btif_hf.cc | 2616 ++++---- btif/src/btif_hf_client.cc | 1526 +++-- btif/src/btif_hh.cc | 2842 +++++---- btif/src/btif_hl.cc | 8430 +++++++++++++------------- btif/src/btif_mce.cc | 186 +- btif/src/btif_pan.cc | 1230 ++-- btif/src/btif_profile_queue.cc | 181 +- btif/src/btif_rc.cc | 10213 +++++++++++++++----------------- btif/src/btif_sdp.cc | 198 +- btif/src/btif_sdp_server.cc | 1187 ++-- btif/src/btif_sm.cc | 251 +- btif/src/btif_sock.cc | 66 +- btif/src/btif_sock_l2cap.cc | 1736 +++--- btif/src/btif_sock_rfc.cc | 507 +- btif/src/btif_sock_sco.cc | 156 +- btif/src/btif_sock_sdp.cc | 195 +- btif/src/btif_sock_thread.cc | 919 ++- btif/src/btif_sock_util.cc | 327 +- btif/src/btif_storage.cc | 2347 ++++---- btif/src/btif_uid.cc | 137 +- btif/src/btif_util.cc | 834 ++- btif/src/stack_manager.cc | 100 +- btif/test/btif_storage_test.cc | 10 +- 74 files changed, 28182 insertions(+), 29746 deletions(-) diff --git a/btif/co/bta_ag_co.cc b/btif/co/bta_ag_co.cc index 94a055540..640402cd0 100644 --- a/btif/co/bta_ag_co.cc +++ b/btif/co/bta_ag_co.cc @@ -18,137 +18,138 @@ #define LOG_TAG "bt_btif_bta_ag" -#include "bta/include/bta_ag_api.h" #include "bta/include/bta_ag_co.h" +#include "bta/include/bta_ag_api.h" #include "hci/include/hci_audio.h" #include "osi/include/osi.h" /******************************************************************************* -** -** Function bta_ag_co_init -** -** Description This callout function is executed by AG when it is -** started by calling BTA_AgEnable(). This function can be -** used by the phone to initialize audio paths or for other -** initialization purposes. -** -** -** Returns Void. -** -*******************************************************************************/ -void bta_ag_co_init(void) -{ - BTM_WriteVoiceSettings(AG_VOICE_SETTINGS); -} - + * + * Function bta_ag_co_init + * + * Description This callout function is executed by AG when it is + * started by calling BTA_AgEnable(). This function can be + * used by the phone to initialize audio paths or for other + * initialization purposes. + * + * + * Returns Void. + * + ******************************************************************************/ +void bta_ag_co_init(void) { BTM_WriteVoiceSettings(AG_VOICE_SETTINGS); } /******************************************************************************* -** -** Function bta_ag_co_audio_state -** -** Description This function is called by the AG before the audio connection -** is brought up, after it comes up, and after it goes down. -** -** Parameters handle - handle of the AG instance -** state - Audio state -** codec - if WBS support is compiled in, codec to going to be used is provided -** and when in SCO_STATE_SETUP, BTM_I2SPCMConfig() must be called with -** the correct platform parameters. -** in the other states codec type should not be ignored -** -** Returns void -** -*******************************************************************************/ + * + * Function bta_ag_co_audio_state + * + * Description This function is called by the AG before the audio + *connection + * is brought up, after it comes up, and after it goes down. + * + * Parameters handle - handle of the AG instance + * state - Audio state + * codec - if WBS support is compiled in, codec to going to be + *used is provided + * and when in SCO_STATE_SETUP, BTM_I2SPCMConfig() must be + *called with + * the correct platform parameters. + * in the other states codec type should not be ignored + * + * Returns void + * + ******************************************************************************/ #if (BTM_WBS_INCLUDED == TRUE) -void bta_ag_co_audio_state(uint16_t handle, uint8_t app_id, uint8_t state, tBTA_AG_PEER_CODEC codec) +void bta_ag_co_audio_state(uint16_t handle, uint8_t app_id, uint8_t state, + tBTA_AG_PEER_CODEC codec) #else void bta_ag_co_audio_state(uint16_t handle, uint8_t app_id, uint8_t state) #endif { - BTIF_TRACE_DEBUG("bta_ag_co_audio_state: handle %d, state %d", handle, state); - switch (state) - { + BTIF_TRACE_DEBUG("bta_ag_co_audio_state: handle %d, state %d", handle, state); + switch (state) { case SCO_STATE_OFF: #if (BTM_WBS_INCLUDED == TRUE) - BTIF_TRACE_DEBUG("bta_ag_co_audio_state(handle %d)::Closed (OFF), codec: 0x%x", - handle, codec); - set_audio_state(handle, (sco_codec_t) codec, (sco_state_t) state); + BTIF_TRACE_DEBUG( + "bta_ag_co_audio_state(handle %d)::Closed (OFF), codec: 0x%x", handle, + codec); + set_audio_state(handle, (sco_codec_t)codec, (sco_state_t)state); #else - BTIF_TRACE_DEBUG("bta_ag_co_audio_state(handle %d)::Closed (OFF)", - handle); + BTIF_TRACE_DEBUG("bta_ag_co_audio_state(handle %d)::Closed (OFF)", + handle); #endif - break; + break; case SCO_STATE_OFF_TRANSFER: - BTIF_TRACE_DEBUG("bta_ag_co_audio_state(handle %d)::Closed (XFERRING)", handle); - break; + BTIF_TRACE_DEBUG("bta_ag_co_audio_state(handle %d)::Closed (XFERRING)", + handle); + break; case SCO_STATE_SETUP: #if (BTM_WBS_INCLUDED == TRUE) - set_audio_state(handle, (sco_codec_t) codec, (sco_state_t) state); + set_audio_state(handle, (sco_codec_t)codec, (sco_state_t)state); #else - set_audio_state(handle, (sco_codec_t) BTA_AG_CODEC_CVSD, (sco_state_t) state); + set_audio_state(handle, (sco_codec_t)BTA_AG_CODEC_CVSD, + (sco_state_t)state); #endif - break; + break; default: - break; - } + break; + } #if (BTM_WBS_INCLUDED == TRUE) - APPL_TRACE_DEBUG("bta_ag_co_audio_state(handle %d, app_id: %d, state %d, codec: 0x%x)", - handle, app_id, state, codec); + APPL_TRACE_DEBUG( + "bta_ag_co_audio_state(handle %d, app_id: %d, state %d, codec: 0x%x)", + handle, app_id, state, codec); #else - APPL_TRACE_DEBUG("bta_ag_co_audio_state(handle %d, app_id: %d, state %d)", \ - handle, app_id, state); + APPL_TRACE_DEBUG("bta_ag_co_audio_state(handle %d, app_id: %d, state %d)", + handle, app_id, state); #endif } - /******************************************************************************* -** -** Function bta_ag_co_data_open -** -** Description This function is executed by AG when a service level connection -** is opened. The phone can use this function to set -** up data paths or perform any required initialization or -** set up particular to the connected service. -** -** -** Returns void -** -*******************************************************************************/ -void bta_ag_co_data_open(uint16_t handle, tBTA_SERVICE_ID service) -{ - BTIF_TRACE_DEBUG("bta_ag_co_data_open handle:%d service:%d", handle, service); + * + * Function bta_ag_co_data_open + * + * Description This function is executed by AG when a service level + *connection + * is opened. The phone can use this function to set + * up data paths or perform any required initialization or + * set up particular to the connected service. + * + * + * Returns void + * + ******************************************************************************/ +void bta_ag_co_data_open(uint16_t handle, tBTA_SERVICE_ID service) { + BTIF_TRACE_DEBUG("bta_ag_co_data_open handle:%d service:%d", handle, service); } /******************************************************************************* -** -** Function bta_ag_co_data_close -** -** Description This function is called by AG when a service level -** connection is closed -** -** -** Returns void -** -*******************************************************************************/ -void bta_ag_co_data_close(uint16_t handle) -{ - BTIF_TRACE_DEBUG("bta_ag_co_data_close handle:%d", handle); + * + * Function bta_ag_co_data_close + * + * Description This function is called by AG when a service level + * connection is closed + * + * + * Returns void + * + ******************************************************************************/ +void bta_ag_co_data_close(uint16_t handle) { + BTIF_TRACE_DEBUG("bta_ag_co_data_close handle:%d", handle); } - /******************************************************************************* ** ** Function bta_ag_co_tx_write ** ** Description This function is called by the AG to send data to the - ** phone when the AG is configured for AT command pass-through. + ** phone when the AG is configured for AT command + *pass-through. ** The implementation of this function must copy the data to ** the phones memory. ** ** Returns void ** *******************************************************************************/ -void bta_ag_co_tx_write(uint16_t handle, UNUSED_ATTR uint8_t * p_data, uint16_t len) -{ - BTIF_TRACE_DEBUG( "bta_ag_co_tx_write: handle: %d, len: %d", handle, len ); +void bta_ag_co_tx_write(uint16_t handle, UNUSED_ATTR uint8_t* p_data, + uint16_t len) { + BTIF_TRACE_DEBUG("bta_ag_co_tx_write: handle: %d, len: %d", handle, len); } diff --git a/btif/co/bta_av_co.cc b/btif/co/bta_av_co.cc index 19de55ec0..3cd30ab2f 100644 --- a/btif/co/bta_av_co.cc +++ b/btif/co/bta_av_co.cc @@ -23,31 +23,30 @@ * ******************************************************************************/ +#include "bta_av_co.h" #include #include #include "a2dp_api.h" #include "bt_target.h" -#include "bta_sys.h" #include "bta_av_api.h" -#include "bta_av_co.h" #include "bta_av_ci.h" +#include "bta_sys.h" #include "btif_av_co.h" #include "btif_util.h" #include "osi/include/mutex.h" #include "osi/include/osi.h" - /***************************************************************************** ** Constants *****************************************************************************/ /* Macro to retrieve the number of elements in a statically allocated array */ -#define BTA_AV_CO_NUM_ELEMENTS(__a) (sizeof(__a)/sizeof((__a)[0])) +#define BTA_AV_CO_NUM_ELEMENTS(__a) (sizeof(__a) / sizeof((__a)[0])) /* MIN and MAX macros */ -#define BTA_AV_CO_MIN(X,Y) ((X) < (Y) ? (X) : (Y)) -#define BTA_AV_CO_MAX(X,Y) ((X) > (Y) ? (X) : (Y)) +#define BTA_AV_CO_MIN(X, Y) ((X) < (Y) ? (X) : (Y)) +#define BTA_AV_CO_MAX(X, Y) ((X) > (Y) ? (X) : (Y)) /* Macro to convert audio handle to index and vice versa */ #define BTA_AV_CO_AUDIO_HNDL_TO_INDX(hndl) (((hndl) & (~BTA_AV_CHNL_MSK)) - 1) @@ -56,68 +55,66 @@ /* SCMS-T protect info */ const uint8_t bta_av_co_cp_scmst[BTA_AV_CP_INFO_LEN] = {0x02, 0x02, 0x00}; - /***************************************************************************** -** Local data -*****************************************************************************/ -typedef struct -{ - uint8_t sep_info_idx; /* local SEP index (in BTA tables) */ - uint8_t seid; /* peer SEP index (in peer tables) */ - uint8_t codec_caps[AVDT_CODEC_SIZE]; /* peer SEP codec capabilities */ - uint8_t num_protect; /* peer SEP number of CP elements */ - uint8_t protect_info[BTA_AV_CP_INFO_LEN]; /* peer SEP content protection info */ + * Local data + ****************************************************************************/ +typedef struct { + uint8_t sep_info_idx; /* local SEP index (in BTA tables) */ + uint8_t seid; /* peer SEP index (in peer tables) */ + uint8_t codec_caps[AVDT_CODEC_SIZE]; /* peer SEP codec capabilities */ + uint8_t num_protect; /* peer SEP number of CP elements */ + uint8_t + protect_info[BTA_AV_CP_INFO_LEN]; /* peer SEP content protection info */ } tBTA_AV_CO_SINK; -typedef struct -{ - BD_ADDR addr; /* address of audio/video peer */ - tBTA_AV_CO_SINK sinks[A2DP_CODEC_SEP_INDEX_MAX]; /* array of supported sinks */ - tBTA_AV_CO_SINK srcs[A2DP_CODEC_SEP_INDEX_MAX]; /* array of supported srcs */ - uint8_t num_sinks; /* total number of sinks at peer */ - uint8_t num_srcs; /* total number of srcs at peer */ - uint8_t num_seps; /* total number of seids at peer */ - uint8_t num_rx_sinks; /* number of received sinks */ - uint8_t num_rx_srcs; /* number of received srcs */ - uint8_t num_sup_sinks; /* number of supported sinks in the sinks array */ - uint8_t num_sup_srcs; /* number of supported srcs in the srcs array */ - const tBTA_AV_CO_SINK *p_sink; /* currently selected sink */ - const tBTA_AV_CO_SINK *p_src; /* currently selected src */ - uint8_t codec_cfg[AVDT_CODEC_SIZE]; /* current codec configuration */ - bool cp_active; /* current CP configuration */ - bool acp; /* acceptor */ - bool recfg_needed; /* reconfiguration is needed */ - bool opened; /* opened */ - uint16_t mtu; /* maximum transmit unit size */ - uint16_t uuid_to_connect; /* uuid of peer device */ +typedef struct { + BD_ADDR addr; /* address of audio/video peer */ + tBTA_AV_CO_SINK + sinks[A2DP_CODEC_SEP_INDEX_MAX]; /* array of supported sinks */ + tBTA_AV_CO_SINK srcs[A2DP_CODEC_SEP_INDEX_MAX]; /* array of supported srcs */ + uint8_t num_sinks; /* total number of sinks at peer */ + uint8_t num_srcs; /* total number of srcs at peer */ + uint8_t num_seps; /* total number of seids at peer */ + uint8_t num_rx_sinks; /* number of received sinks */ + uint8_t num_rx_srcs; /* number of received srcs */ + uint8_t num_sup_sinks; /* number of supported sinks in the sinks array */ + uint8_t num_sup_srcs; /* number of supported srcs in the srcs array */ + const tBTA_AV_CO_SINK* p_sink; /* currently selected sink */ + const tBTA_AV_CO_SINK* p_src; /* currently selected src */ + uint8_t codec_cfg[AVDT_CODEC_SIZE]; /* current codec configuration */ + bool cp_active; /* current CP configuration */ + bool acp; /* acceptor */ + bool recfg_needed; /* reconfiguration is needed */ + bool opened; /* opened */ + uint16_t mtu; /* maximum transmit unit size */ + uint16_t uuid_to_connect; /* uuid of peer device */ } tBTA_AV_CO_PEER; -typedef struct -{ - bool active; - uint8_t flag; +typedef struct { + bool active; + uint8_t flag; } tBTA_AV_CO_CP; -typedef struct -{ - /* Connected peer information */ - tBTA_AV_CO_PEER peers[BTA_AV_NUM_STRS]; - /* Current codec configuration - access to this variable must be protected */ - uint8_t codec_cfg[AVDT_CODEC_SIZE]; - uint8_t codec_cfg_setconfig[AVDT_CODEC_SIZE]; /* remote peer setconfig preference */ +typedef struct { + /* Connected peer information */ + tBTA_AV_CO_PEER peers[BTA_AV_NUM_STRS]; + /* Current codec configuration - access to this variable must be protected */ + uint8_t codec_cfg[AVDT_CODEC_SIZE]; + uint8_t codec_cfg_setconfig[AVDT_CODEC_SIZE]; /* remote peer setconfig + preference */ - tBTA_AV_CO_CP cp; + tBTA_AV_CO_CP cp; } tBTA_AV_CO_CB; /* Control block instance */ static tBTA_AV_CO_CB bta_av_co_cb; -static bool bta_av_co_cp_is_scmst(const uint8_t *p_protectinfo); -static bool bta_av_co_audio_sink_has_scmst(const tBTA_AV_CO_SINK *p_sink); +static bool bta_av_co_cp_is_scmst(const uint8_t* p_protectinfo); +static bool bta_av_co_audio_sink_has_scmst(const tBTA_AV_CO_SINK* p_sink); static const tBTA_AV_CO_SINK* bta_av_co_find_peer_sink_supports_codec( - const tBTA_AV_CO_PEER *p_peer); + const tBTA_AV_CO_PEER* p_peer); static const tBTA_AV_CO_SINK* bta_av_co_find_peer_src_supports_codec( - const tBTA_AV_CO_PEER *p_peer); + const tBTA_AV_CO_PEER* p_peer); /******************************************************************************* ** @@ -131,10 +128,7 @@ static const tBTA_AV_CO_SINK* bta_av_co_find_peer_src_supports_codec( ** Returns The current flag value ** *******************************************************************************/ -static uint8_t bta_av_co_cp_get_flag(void) -{ - return bta_av_co_cb.cp.flag; -} +static uint8_t bta_av_co_cp_get_flag(void) { return bta_av_co_cb.cp.flag; } /******************************************************************************* ** @@ -148,19 +142,17 @@ static uint8_t bta_av_co_cp_get_flag(void) ** Returns true if setting the SCMS flag is supported else false ** *******************************************************************************/ -static bool bta_av_co_cp_set_flag(uint8_t cp_flag) -{ - APPL_TRACE_DEBUG("%s: cp_flag = %d", __func__, cp_flag); +static bool bta_av_co_cp_set_flag(uint8_t cp_flag) { + APPL_TRACE_DEBUG("%s: cp_flag = %d", __func__, cp_flag); #if (BTA_AV_CO_CP_SCMS_T == TRUE) #else - if (cp_flag != BTA_AV_CP_SCMS_COPY_FREE) - { - return false; - } + if (cp_flag != BTA_AV_CP_SCMS_COPY_FREE) { + return false; + } #endif - bta_av_co_cb.cp.flag = cp_flag; - return true; + bta_av_co_cb.cp.flag = cp_flag; + return true; } /******************************************************************************* @@ -172,22 +164,20 @@ static bool bta_av_co_cp_set_flag(uint8_t cp_flag) ** Returns the control block ** *******************************************************************************/ -static tBTA_AV_CO_PEER *bta_av_co_get_peer(tBTA_AV_HNDL hndl) -{ - uint8_t index; +static tBTA_AV_CO_PEER* bta_av_co_get_peer(tBTA_AV_HNDL hndl) { + uint8_t index; - index = BTA_AV_CO_AUDIO_HNDL_TO_INDX(hndl); + index = BTA_AV_CO_AUDIO_HNDL_TO_INDX(hndl); - APPL_TRACE_DEBUG("%s: handle = %d index = %d", __func__, hndl, index); + APPL_TRACE_DEBUG("%s: handle = %d index = %d", __func__, hndl, index); - /* Sanity check */ - if (index >= BTA_AV_CO_NUM_ELEMENTS(bta_av_co_cb.peers)) - { - APPL_TRACE_ERROR("%s: peer index out of bounds: %d", __func__, index); - return NULL; - } + /* Sanity check */ + if (index >= BTA_AV_CO_NUM_ELEMENTS(bta_av_co_cb.peers)) { + APPL_TRACE_ERROR("%s: peer index out of bounds: %d", __func__, index); + return NULL; + } - return &bta_av_co_cb.peers[index]; + return &bta_av_co_cb.peers[index]; } /******************************************************************************* @@ -204,13 +194,12 @@ static tBTA_AV_CO_PEER *bta_av_co_get_peer(tBTA_AV_HNDL hndl) ** *******************************************************************************/ bool bta_av_co_audio_init(tA2DP_CODEC_SEP_INDEX codec_sep_index, - tAVDT_CFG *p_cfg) -{ - /* reset remote preference through setconfig */ - memset(bta_av_co_cb.codec_cfg_setconfig, 0, - sizeof(bta_av_co_cb.codec_cfg_setconfig)); + tAVDT_CFG* p_cfg) { + /* reset remote preference through setconfig */ + memset(bta_av_co_cb.codec_cfg_setconfig, 0, + sizeof(bta_av_co_cb.codec_cfg_setconfig)); - return A2DP_InitCodecConfig(codec_sep_index, p_cfg); + return A2DP_InitCodecConfig(codec_sep_index, p_cfg); } /******************************************************************************* @@ -227,39 +216,36 @@ bool bta_av_co_audio_init(tA2DP_CODEC_SEP_INDEX codec_sep_index, *******************************************************************************/ void bta_av_co_audio_disc_res(tBTA_AV_HNDL hndl, uint8_t num_seps, uint8_t num_sink, uint8_t num_src, BD_ADDR addr, - uint16_t uuid_local) -{ - tBTA_AV_CO_PEER *p_peer; - - APPL_TRACE_DEBUG("%s: h:x%x num_seps:%d num_sink:%d num_src:%d", - __func__, hndl, num_seps, num_sink, num_src); - - /* Find the peer info */ - p_peer = bta_av_co_get_peer(hndl); - if (p_peer == NULL) - { - APPL_TRACE_ERROR("%s: could not find peer entry", __func__); - return; - } - - /* Sanity check : this should never happen */ - if (p_peer->opened) - { - APPL_TRACE_ERROR("%s: peer already opened", __func__); - } - - /* Copy the discovery results */ - bdcpy(p_peer->addr, addr); - p_peer->num_sinks = num_sink; - p_peer->num_srcs = num_src; - p_peer->num_seps = num_seps; - p_peer->num_rx_sinks = 0; - p_peer->num_rx_srcs = 0; - p_peer->num_sup_sinks = 0; - if (uuid_local == UUID_SERVCLASS_AUDIO_SINK) - p_peer->uuid_to_connect = UUID_SERVCLASS_AUDIO_SOURCE; - else if (uuid_local == UUID_SERVCLASS_AUDIO_SOURCE) - p_peer->uuid_to_connect = UUID_SERVCLASS_AUDIO_SINK; + uint16_t uuid_local) { + tBTA_AV_CO_PEER* p_peer; + + APPL_TRACE_DEBUG("%s: h:x%x num_seps:%d num_sink:%d num_src:%d", __func__, + hndl, num_seps, num_sink, num_src); + + /* Find the peer info */ + p_peer = bta_av_co_get_peer(hndl); + if (p_peer == NULL) { + APPL_TRACE_ERROR("%s: could not find peer entry", __func__); + return; + } + + /* Sanity check : this should never happen */ + if (p_peer->opened) { + APPL_TRACE_ERROR("%s: peer already opened", __func__); + } + + /* Copy the discovery results */ + bdcpy(p_peer->addr, addr); + p_peer->num_sinks = num_sink; + p_peer->num_srcs = num_src; + p_peer->num_seps = num_seps; + p_peer->num_rx_sinks = 0; + p_peer->num_rx_srcs = 0; + p_peer->num_sup_sinks = 0; + if (uuid_local == UUID_SERVCLASS_AUDIO_SINK) + p_peer->uuid_to_connect = UUID_SERVCLASS_AUDIO_SOURCE; + else if (uuid_local == UUID_SERVCLASS_AUDIO_SOURCE) + p_peer->uuid_to_connect = UUID_SERVCLASS_AUDIO_SINK; } /******************************************************************************* @@ -274,110 +260,99 @@ void bta_av_co_audio_disc_res(tBTA_AV_HNDL hndl, uint8_t num_seps, ** Returns Pass or Fail for current getconfig. ** *******************************************************************************/ -static tA2DP_STATUS bta_av_audio_sink_getconfig(tBTA_AV_HNDL hndl, - uint8_t *p_codec_info, - uint8_t *p_sep_info_idx, - uint8_t seid, - uint8_t *p_num_protect, - uint8_t *p_protect_info) -{ - tA2DP_STATUS result = A2DP_FAIL; - tBTA_AV_CO_PEER *p_peer; - uint8_t pref_cfg[AVDT_CODEC_SIZE]; - - APPL_TRACE_DEBUG("%s: handle:0x%x codec:%s seid:%d", - __func__, hndl, A2DP_CodecName(p_codec_info), seid); - APPL_TRACE_DEBUG("%s: num_protect:0x%02x protect_info:0x%02x%02x%02x", - __func__, *p_num_protect, p_protect_info[0], - p_protect_info[1], p_protect_info[2]); - - /* Retrieve the peer info */ - p_peer = bta_av_co_get_peer(hndl); - if (p_peer == NULL) - { - APPL_TRACE_ERROR("%s: could not find peer entry", __func__); - return A2DP_FAIL; +static tA2DP_STATUS bta_av_audio_sink_getconfig( + tBTA_AV_HNDL hndl, uint8_t* p_codec_info, uint8_t* p_sep_info_idx, + uint8_t seid, uint8_t* p_num_protect, uint8_t* p_protect_info) { + tA2DP_STATUS result = A2DP_FAIL; + tBTA_AV_CO_PEER* p_peer; + uint8_t pref_cfg[AVDT_CODEC_SIZE]; + + APPL_TRACE_DEBUG("%s: handle:0x%x codec:%s seid:%d", __func__, hndl, + A2DP_CodecName(p_codec_info), seid); + APPL_TRACE_DEBUG("%s: num_protect:0x%02x protect_info:0x%02x%02x%02x", + __func__, *p_num_protect, p_protect_info[0], + p_protect_info[1], p_protect_info[2]); + + /* Retrieve the peer info */ + p_peer = bta_av_co_get_peer(hndl); + if (p_peer == NULL) { + APPL_TRACE_ERROR("%s: could not find peer entry", __func__); + return A2DP_FAIL; + } + + APPL_TRACE_DEBUG("%s: peer(o=%d,n_sinks=%d,n_rx_sinks=%d,n_sup_sinks=%d)", + __func__, p_peer->opened, p_peer->num_srcs, + p_peer->num_rx_srcs, p_peer->num_sup_srcs); + + p_peer->num_rx_srcs++; + + /* Check the peer's SOURCE codec */ + if (A2DP_IsPeerSourceCodecValid(p_codec_info)) { + /* If there is room for a new one */ + if (p_peer->num_sup_srcs < BTA_AV_CO_NUM_ELEMENTS(p_peer->srcs)) { + tBTA_AV_CO_SINK* p_src = &p_peer->srcs[p_peer->num_sup_srcs++]; + + APPL_TRACE_DEBUG("%s: saved caps[%x:%x:%x:%x:%x:%x]", __func__, + p_codec_info[1], p_codec_info[2], p_codec_info[3], + p_codec_info[4], p_codec_info[5], p_codec_info[6]); + + memcpy(p_src->codec_caps, p_codec_info, AVDT_CODEC_SIZE); + p_src->sep_info_idx = *p_sep_info_idx; + p_src->seid = seid; + p_src->num_protect = *p_num_protect; + memcpy(p_src->protect_info, p_protect_info, BTA_AV_CP_INFO_LEN); + } else { + APPL_TRACE_ERROR("%s: no more room for SRC info", __func__); } + } - APPL_TRACE_DEBUG("%s: peer(o=%d,n_sinks=%d,n_rx_sinks=%d,n_sup_sinks=%d)", - __func__, p_peer->opened, p_peer->num_srcs, - p_peer->num_rx_srcs, p_peer->num_sup_srcs); - - p_peer->num_rx_srcs++; - - /* Check the peer's SOURCE codec */ - if (A2DP_IsPeerSourceCodecValid(p_codec_info)) { - /* If there is room for a new one */ - if (p_peer->num_sup_srcs < BTA_AV_CO_NUM_ELEMENTS(p_peer->srcs)) - { - tBTA_AV_CO_SINK *p_src = &p_peer->srcs[p_peer->num_sup_srcs++]; - - APPL_TRACE_DEBUG("%s: saved caps[%x:%x:%x:%x:%x:%x]", - __func__, p_codec_info[1], p_codec_info[2], - p_codec_info[3], p_codec_info[4], p_codec_info[5], - p_codec_info[6]); - - memcpy(p_src->codec_caps, p_codec_info, AVDT_CODEC_SIZE); - p_src->sep_info_idx = *p_sep_info_idx; - p_src->seid = seid; - p_src->num_protect = *p_num_protect; - memcpy(p_src->protect_info, p_protect_info, BTA_AV_CP_INFO_LEN); - } - else - { - APPL_TRACE_ERROR("%s: no more room for SRC info", __func__); + /* If last SINK get capabilities or all supported codec caps retrieved */ + if ((p_peer->num_rx_srcs == p_peer->num_srcs) || + (p_peer->num_sup_srcs == BTA_AV_CO_NUM_ELEMENTS(p_peer->srcs))) { + APPL_TRACE_DEBUG("%s: last SRC reached", __func__); + + /* Protect access to bta_av_co_cb.codec_cfg */ + mutex_global_lock(); + + /* Find a src that matches the codec config */ + const tBTA_AV_CO_SINK* p_src = + bta_av_co_find_peer_src_supports_codec(p_peer); + if (p_src != NULL) { + APPL_TRACE_DEBUG("%s: codec supported", __func__); + + /* Build the codec configuration for this sink */ + { + /* Save the new configuration */ + p_peer->p_src = p_src; + /* get preferred config from src_caps */ + if (A2DP_BuildSrc2SinkConfig(p_src->codec_caps, pref_cfg) != + A2DP_SUCCESS) { + mutex_global_unlock(); + return A2DP_FAIL; } - } + memcpy(p_peer->codec_cfg, pref_cfg, AVDT_CODEC_SIZE); - /* If last SINK get capabilities or all supported codec caps retrieved */ - if ((p_peer->num_rx_srcs == p_peer->num_srcs) || - (p_peer->num_sup_srcs == BTA_AV_CO_NUM_ELEMENTS(p_peer->srcs))) - { - APPL_TRACE_DEBUG("%s: last SRC reached", __func__); - - /* Protect access to bta_av_co_cb.codec_cfg */ - mutex_global_lock(); - - /* Find a src that matches the codec config */ - const tBTA_AV_CO_SINK *p_src = - bta_av_co_find_peer_src_supports_codec(p_peer); - if (p_src != NULL) { - APPL_TRACE_DEBUG("%s: codec supported", __func__); - - /* Build the codec configuration for this sink */ - { - /* Save the new configuration */ - p_peer->p_src = p_src; - /* get preferred config from src_caps */ - if (A2DP_BuildSrc2SinkConfig(p_src->codec_caps, pref_cfg) != - A2DP_SUCCESS) { - mutex_global_unlock(); - return A2DP_FAIL; - } - memcpy(p_peer->codec_cfg, pref_cfg, AVDT_CODEC_SIZE); - - APPL_TRACE_DEBUG("%s: p_codec_info[%x:%x:%x:%x:%x:%x]", - __func__, p_peer->codec_cfg[1], - p_peer->codec_cfg[2], p_peer->codec_cfg[3], - p_peer->codec_cfg[4], p_peer->codec_cfg[5], - p_peer->codec_cfg[6]); - /* By default, no content protection */ - *p_num_protect = 0; + APPL_TRACE_DEBUG("%s: p_codec_info[%x:%x:%x:%x:%x:%x]", __func__, + p_peer->codec_cfg[1], p_peer->codec_cfg[2], + p_peer->codec_cfg[3], p_peer->codec_cfg[4], + p_peer->codec_cfg[5], p_peer->codec_cfg[6]); + /* By default, no content protection */ + *p_num_protect = 0; #if (BTA_AV_CO_CP_SCMS_T == TRUE) - p_peer->cp_active = false; - bta_av_co_cb.cp.active = false; + p_peer->cp_active = false; + bta_av_co_cb.cp.active = false; #endif - *p_sep_info_idx = p_src->sep_info_idx; - memcpy(p_codec_info, p_peer->codec_cfg, AVDT_CODEC_SIZE); - result = A2DP_SUCCESS; - } - } - /* Protect access to bta_av_co_cb.codec_cfg */ - mutex_global_unlock(); + *p_sep_info_idx = p_src->sep_info_idx; + memcpy(p_codec_info, p_peer->codec_cfg, AVDT_CODEC_SIZE); + result = A2DP_SUCCESS; + } } - return result; + /* Protect access to bta_av_co_cb.codec_cfg */ + mutex_global_unlock(); + } + return result; } /******************************************************************************* ** @@ -391,300 +366,284 @@ static tA2DP_STATUS bta_av_audio_sink_getconfig(tBTA_AV_HNDL hndl, ** Returns Stream codec and content protection configuration info. ** *******************************************************************************/ -tA2DP_STATUS bta_av_co_audio_getconfig(tBTA_AV_HNDL hndl, - uint8_t *p_codec_info, - uint8_t *p_sep_info_idx, uint8_t seid, - uint8_t *p_num_protect, - uint8_t *p_protect_info) -{ - tA2DP_STATUS result = A2DP_FAIL; - tBTA_AV_CO_PEER *p_peer; - uint8_t codec_cfg[AVDT_CODEC_SIZE]; +tA2DP_STATUS bta_av_co_audio_getconfig(tBTA_AV_HNDL hndl, uint8_t* p_codec_info, + uint8_t* p_sep_info_idx, uint8_t seid, + uint8_t* p_num_protect, + uint8_t* p_protect_info) { + tA2DP_STATUS result = A2DP_FAIL; + tBTA_AV_CO_PEER* p_peer; + uint8_t codec_cfg[AVDT_CODEC_SIZE]; + + APPL_TRACE_DEBUG("%s", __func__); + + /* Retrieve the peer info */ + p_peer = bta_av_co_get_peer(hndl); + if (p_peer == NULL) { + APPL_TRACE_ERROR("%s: could not find peer entry", __func__); + return A2DP_FAIL; + } + + if (p_peer->uuid_to_connect == UUID_SERVCLASS_AUDIO_SOURCE) { + result = bta_av_audio_sink_getconfig(hndl, p_codec_info, p_sep_info_idx, + seid, p_num_protect, p_protect_info); + return result; + } + APPL_TRACE_DEBUG("%s: handle:0x%x codec:%s seid:%d", __func__, hndl, + A2DP_CodecName(p_codec_info), seid); + APPL_TRACE_DEBUG("%s: num_protect:0x%02x protect_info:0x%02x%02x%02x", + __func__, *p_num_protect, p_protect_info[0], + p_protect_info[1], p_protect_info[2]); + APPL_TRACE_DEBUG("%s: peer(o=%d, n_sinks=%d, n_rx_sinks=%d, n_sup_sinks=%d)", + __func__, p_peer->opened, p_peer->num_sinks, + p_peer->num_rx_sinks, p_peer->num_sup_sinks); + + p_peer->num_rx_sinks++; + + /* Check the peer's SINK codec */ + if (A2DP_IsPeerSinkCodecValid(p_codec_info)) { + /* If there is room for a new one */ + if (p_peer->num_sup_sinks < BTA_AV_CO_NUM_ELEMENTS(p_peer->sinks)) { + tBTA_AV_CO_SINK* p_sink = &p_peer->sinks[p_peer->num_sup_sinks++]; + + APPL_TRACE_DEBUG("%s: saved caps[%x:%x:%x:%x:%x:%x]", __func__, + p_codec_info[1], p_codec_info[2], p_codec_info[3], + p_codec_info[4], p_codec_info[5], p_codec_info[6]); + + memcpy(p_sink->codec_caps, p_codec_info, AVDT_CODEC_SIZE); + p_sink->sep_info_idx = *p_sep_info_idx; + p_sink->seid = seid; + p_sink->num_protect = *p_num_protect; + memcpy(p_sink->protect_info, p_protect_info, BTA_AV_CP_INFO_LEN); + } else { + APPL_TRACE_ERROR("%s: no more room for SINK info", __func__); + } + } - APPL_TRACE_DEBUG("%s", __func__); + /* If last SINK get capabilities or all supported codec capa retrieved */ + if ((p_peer->num_rx_sinks == p_peer->num_sinks) || + (p_peer->num_sup_sinks == BTA_AV_CO_NUM_ELEMENTS(p_peer->sinks))) { + APPL_TRACE_DEBUG("%s: last sink reached", __func__); - /* Retrieve the peer info */ - p_peer = bta_av_co_get_peer(hndl); - if (p_peer == NULL) - { - APPL_TRACE_ERROR("%s: could not find peer entry", __func__); - return A2DP_FAIL; - } + /* Protect access to bta_av_co_cb.codec_cfg */ + mutex_global_lock(); - if (p_peer->uuid_to_connect == UUID_SERVCLASS_AUDIO_SOURCE) - { - result = bta_av_audio_sink_getconfig(hndl, p_codec_info, - p_sep_info_idx, seid, - p_num_protect, p_protect_info); - return result; - } - APPL_TRACE_DEBUG("%s: handle:0x%x codec:%s seid:%d", - __func__, hndl, A2DP_CodecName(p_codec_info), seid); - APPL_TRACE_DEBUG("%s: num_protect:0x%02x protect_info:0x%02x%02x%02x", - __func__, *p_num_protect, p_protect_info[0], - p_protect_info[1], p_protect_info[2]); - APPL_TRACE_DEBUG("%s: peer(o=%d, n_sinks=%d, n_rx_sinks=%d, n_sup_sinks=%d)", - __func__, p_peer->opened, p_peer->num_sinks, - p_peer->num_rx_sinks, p_peer->num_sup_sinks); - - p_peer->num_rx_sinks++; - - /* Check the peer's SINK codec */ - if (A2DP_IsPeerSinkCodecValid(p_codec_info)) { - /* If there is room for a new one */ - if (p_peer->num_sup_sinks < BTA_AV_CO_NUM_ELEMENTS(p_peer->sinks)) { - tBTA_AV_CO_SINK *p_sink = &p_peer->sinks[p_peer->num_sup_sinks++]; - - APPL_TRACE_DEBUG("%s: saved caps[%x:%x:%x:%x:%x:%x]", __func__, - p_codec_info[1], p_codec_info[2], p_codec_info[3], - p_codec_info[4], p_codec_info[5], p_codec_info[6]); - - memcpy(p_sink->codec_caps, p_codec_info, AVDT_CODEC_SIZE); - p_sink->sep_info_idx = *p_sep_info_idx; - p_sink->seid = seid; - p_sink->num_protect = *p_num_protect; - memcpy(p_sink->protect_info, p_protect_info, BTA_AV_CP_INFO_LEN); - } else { - APPL_TRACE_ERROR("%s: no more room for SINK info", __func__); + /* Find a sink that matches the codec config */ + const tBTA_AV_CO_SINK* p_sink = + bta_av_co_find_peer_sink_supports_codec(p_peer); + if (p_sink == NULL) { + APPL_TRACE_ERROR("%s: cannot find peer SINK for this codec config", + __func__); + } else { + /* stop fetching caps once we retrieved a supported codec */ + if (p_peer->acp) { + APPL_TRACE_EVENT("%s: no need to fetch more SEPs", __func__); + *p_sep_info_idx = p_peer->num_seps; + } + + /* Build the codec configuration for this sink */ + memset(codec_cfg, 0, AVDT_CODEC_SIZE); + if (A2DP_BuildSinkConfig(bta_av_co_cb.codec_cfg, p_sink->codec_caps, + codec_cfg) == A2DP_SUCCESS) { + APPL_TRACE_DEBUG("%s: reconfig codec_cfg[%x:%x:%x:%x:%x:%x]", __func__, + codec_cfg[1], codec_cfg[2], codec_cfg[3], codec_cfg[4], + codec_cfg[5], codec_cfg[6]); + for (int i = 0; i < AVDT_CODEC_SIZE; i++) { + APPL_TRACE_DEBUG("%s: p_codec_info[%d]: %x", __func__, i, + p_codec_info[i]); } - } - /* If last SINK get capabilities or all supported codec capa retrieved */ - if ((p_peer->num_rx_sinks == p_peer->num_sinks) || - (p_peer->num_sup_sinks == BTA_AV_CO_NUM_ELEMENTS(p_peer->sinks))) { - APPL_TRACE_DEBUG("%s: last sink reached", __func__); + /* Save the new configuration */ + p_peer->p_sink = p_sink; + memcpy(p_peer->codec_cfg, codec_cfg, AVDT_CODEC_SIZE); - /* Protect access to bta_av_co_cb.codec_cfg */ - mutex_global_lock(); - - /* Find a sink that matches the codec config */ - const tBTA_AV_CO_SINK *p_sink = - bta_av_co_find_peer_sink_supports_codec(p_peer); - if (p_sink == NULL) { - APPL_TRACE_ERROR("%s: cannot find peer SINK for this codec config", - __func__); - } else { - /* stop fetching caps once we retrieved a supported codec */ - if (p_peer->acp) { - APPL_TRACE_EVENT("%s: no need to fetch more SEPs", __func__); - *p_sep_info_idx = p_peer->num_seps; - } - - /* Build the codec configuration for this sink */ - memset(codec_cfg, 0, AVDT_CODEC_SIZE); - if (A2DP_BuildSinkConfig(bta_av_co_cb.codec_cfg, - p_sink->codec_caps, codec_cfg) == - A2DP_SUCCESS) { - APPL_TRACE_DEBUG("%s: reconfig codec_cfg[%x:%x:%x:%x:%x:%x]", - __func__, - codec_cfg[1], codec_cfg[2], codec_cfg[3], - codec_cfg[4], codec_cfg[5], codec_cfg[6]); - for (int i = 0; i < AVDT_CODEC_SIZE; i++) { - APPL_TRACE_DEBUG("%s: p_codec_info[%d]: %x", - __func__, i, p_codec_info[i]); - } - - /* Save the new configuration */ - p_peer->p_sink = p_sink; - memcpy(p_peer->codec_cfg, codec_cfg, AVDT_CODEC_SIZE); - - /* By default, no content protection */ - *p_num_protect = 0; + /* By default, no content protection */ + *p_num_protect = 0; #if (BTA_AV_CO_CP_SCMS_T == TRUE) - /* Check if this sink supports SCMS */ - p_peer->cp_active = bta_av_co_audio_sink_has_scmst(p_sink); - bta_av_co_cb.cp.active = p_peer->cp_active; - if (p_peer->cp_active) { - *p_num_protect = BTA_AV_CP_INFO_LEN; - memcpy(p_protect_info, bta_av_co_cp_scmst, - BTA_AV_CP_INFO_LEN); - } + /* Check if this sink supports SCMS */ + p_peer->cp_active = bta_av_co_audio_sink_has_scmst(p_sink); + bta_av_co_cb.cp.active = p_peer->cp_active; + if (p_peer->cp_active) { + *p_num_protect = BTA_AV_CP_INFO_LEN; + memcpy(p_protect_info, bta_av_co_cp_scmst, BTA_AV_CP_INFO_LEN); + } #endif - /* If acceptor -> reconfig otherwise reply for configuration */ - if (p_peer->acp) { - if (p_peer->recfg_needed) { - APPL_TRACE_DEBUG("%s: call BTA_AvReconfig(x%x)", - __func__, hndl); - BTA_AvReconfig(hndl, true, p_sink->sep_info_idx, - p_peer->codec_cfg, *p_num_protect, - bta_av_co_cp_scmst); - } - } else { - *p_sep_info_idx = p_sink->sep_info_idx; - memcpy(p_codec_info, p_peer->codec_cfg, AVDT_CODEC_SIZE); - } - result = A2DP_SUCCESS; - } + /* If acceptor -> reconfig otherwise reply for configuration */ + if (p_peer->acp) { + if (p_peer->recfg_needed) { + APPL_TRACE_DEBUG("%s: call BTA_AvReconfig(x%x)", __func__, hndl); + BTA_AvReconfig(hndl, true, p_sink->sep_info_idx, p_peer->codec_cfg, + *p_num_protect, bta_av_co_cp_scmst); + } + } else { + *p_sep_info_idx = p_sink->sep_info_idx; + memcpy(p_codec_info, p_peer->codec_cfg, AVDT_CODEC_SIZE); } - /* Protect access to bta_av_co_cb.codec_cfg */ - mutex_global_unlock(); + result = A2DP_SUCCESS; + } } - return result; + /* Protect access to bta_av_co_cb.codec_cfg */ + mutex_global_unlock(); + } + return result; } /******************************************************************************* ** ** Function bta_av_co_audio_setconfig ** - ** Description This callout function is executed by AV to set the codec and + ** Description This callout function is executed by AV to set the codec + *and ** content protection configuration of the audio stream. ** ** ** Returns void ** *******************************************************************************/ -void bta_av_co_audio_setconfig(tBTA_AV_HNDL hndl, const uint8_t *p_codec_info, +void bta_av_co_audio_setconfig(tBTA_AV_HNDL hndl, const uint8_t* p_codec_info, UNUSED_ATTR uint8_t seid, UNUSED_ATTR BD_ADDR addr, uint8_t num_protect, - uint8_t *p_protect_info, uint8_t t_local_sep, - uint8_t avdt_handle) -{ - tBTA_AV_CO_PEER *p_peer; - tA2DP_STATUS status = A2DP_SUCCESS; - uint8_t category = A2DP_SUCCESS; - bool recfg_needed = false; - - APPL_TRACE_DEBUG("%s: p_codec_info[%x:%x:%x:%x:%x:%x]", - __func__, - p_codec_info[1], p_codec_info[2], p_codec_info[3], - p_codec_info[4], p_codec_info[5], p_codec_info[6]); - APPL_TRACE_DEBUG("num_protect:0x%02x protect_info:0x%02x%02x%02x", - num_protect, p_protect_info[0], p_protect_info[1], p_protect_info[2]); - - /* Retrieve the peer info */ - p_peer = bta_av_co_get_peer(hndl); - if (p_peer == NULL) { - APPL_TRACE_ERROR("%s: could not find peer entry", __func__); - /* Call call-in rejecting the configuration */ - bta_av_ci_setconfig(hndl, A2DP_BUSY, AVDT_ASC_CODEC, 0, NULL, false, - avdt_handle); - return; - } + uint8_t* p_protect_info, uint8_t t_local_sep, + uint8_t avdt_handle) { + tBTA_AV_CO_PEER* p_peer; + tA2DP_STATUS status = A2DP_SUCCESS; + uint8_t category = A2DP_SUCCESS; + bool recfg_needed = false; + + APPL_TRACE_DEBUG("%s: p_codec_info[%x:%x:%x:%x:%x:%x]", __func__, + p_codec_info[1], p_codec_info[2], p_codec_info[3], + p_codec_info[4], p_codec_info[5], p_codec_info[6]); + APPL_TRACE_DEBUG("num_protect:0x%02x protect_info:0x%02x%02x%02x", + num_protect, p_protect_info[0], p_protect_info[1], + p_protect_info[2]); + + /* Retrieve the peer info */ + p_peer = bta_av_co_get_peer(hndl); + if (p_peer == NULL) { + APPL_TRACE_ERROR("%s: could not find peer entry", __func__); + /* Call call-in rejecting the configuration */ + bta_av_ci_setconfig(hndl, A2DP_BUSY, AVDT_ASC_CODEC, 0, NULL, false, + avdt_handle); + return; + } - APPL_TRACE_DEBUG("%s: peer(o=%d, n_sinks=%d, n_rx_sinks=%d, n_sup_sinks=%d)", - __func__, p_peer->opened, p_peer->num_sinks, - p_peer->num_rx_sinks, p_peer->num_sup_sinks); + APPL_TRACE_DEBUG("%s: peer(o=%d, n_sinks=%d, n_rx_sinks=%d, n_sup_sinks=%d)", + __func__, p_peer->opened, p_peer->num_sinks, + p_peer->num_rx_sinks, p_peer->num_sup_sinks); - /* Sanity check: should not be opened at this point */ - if (p_peer->opened) { - APPL_TRACE_ERROR("%s: peer already in use", __func__); - } + /* Sanity check: should not be opened at this point */ + if (p_peer->opened) { + APPL_TRACE_ERROR("%s: peer already in use", __func__); + } - if (num_protect != 0) { + if (num_protect != 0) { #if (BTA_AV_CO_CP_SCMS_T == TRUE) - /* If CP is supported */ - if ((num_protect != 1) || - (bta_av_co_cp_is_scmst(p_protect_info) == false)) { - APPL_TRACE_ERROR("%s: wrong CP configuration", __func__); - status = A2DP_BAD_CP_TYPE; - category = AVDT_ASC_PROTECT; - } + /* If CP is supported */ + if ((num_protect != 1) || + (bta_av_co_cp_is_scmst(p_protect_info) == false)) { + APPL_TRACE_ERROR("%s: wrong CP configuration", __func__); + status = A2DP_BAD_CP_TYPE; + category = AVDT_ASC_PROTECT; + } #else - /* Do not support content protection for the time being */ - APPL_TRACE_ERROR("%s: wrong CP configuration", __func__); - status = A2DP_BAD_CP_TYPE; - category = AVDT_ASC_PROTECT; + /* Do not support content protection for the time being */ + APPL_TRACE_ERROR("%s: wrong CP configuration", __func__); + status = A2DP_BAD_CP_TYPE; + category = AVDT_ASC_PROTECT; #endif - } + } - if (status == A2DP_SUCCESS) { - bool codec_cfg_supported = false; - if (t_local_sep == AVDT_TSEP_SNK) { - APPL_TRACE_DEBUG("%s: peer is A2DP SRC", __func__); - codec_cfg_supported = A2DP_IsSinkCodecSupported(p_codec_info); - } - if (t_local_sep == AVDT_TSEP_SRC) { - APPL_TRACE_DEBUG("%s: peer is A2DP SINK", __func__); - codec_cfg_supported = A2DP_IsSourceCodecSupported(p_codec_info); - } - - /* Check if codec configuration is supported */ - if (codec_cfg_supported) { - - /* Protect access to bta_av_co_cb.codec_cfg */ - mutex_global_lock(); - - /* Check if the configuration matches the current codec config */ - if (A2DP_CodecRequiresReconfig(p_codec_info, - bta_av_co_cb.codec_cfg)) { - recfg_needed = true; - } else if ((num_protect == 1) && (!bta_av_co_cb.cp.active)) { - recfg_needed = true; - } - memcpy(bta_av_co_cb.codec_cfg_setconfig, p_codec_info, - AVDT_CODEC_SIZE); - if (t_local_sep == AVDT_TSEP_SNK) { - /* - * If Peer is SRC, and our cfg subset matches with what is - * requested by peer, then just accept what peer wants. - */ - memcpy(bta_av_co_cb.codec_cfg, p_codec_info, AVDT_CODEC_SIZE); - recfg_needed = false; - } - /* Protect access to bta_av_co_cb.codec_cfg */ - mutex_global_unlock(); - } else { - category = AVDT_ASC_CODEC; - status = A2DP_WRONG_CODEC; - } + if (status == A2DP_SUCCESS) { + bool codec_cfg_supported = false; + if (t_local_sep == AVDT_TSEP_SNK) { + APPL_TRACE_DEBUG("%s: peer is A2DP SRC", __func__); + codec_cfg_supported = A2DP_IsSinkCodecSupported(p_codec_info); } - - if (status != A2DP_SUCCESS) { - APPL_TRACE_DEBUG("%s: reject s=%d c=%d", __func__, status, category); - /* Call call-in rejecting the configuration */ - bta_av_ci_setconfig(hndl, status, category, 0, NULL, false, - avdt_handle); - return; + if (t_local_sep == AVDT_TSEP_SRC) { + APPL_TRACE_DEBUG("%s: peer is A2DP SINK", __func__); + codec_cfg_supported = A2DP_IsSourceCodecSupported(p_codec_info); } - /* Mark that this is an acceptor peer */ - p_peer->acp = true; - p_peer->recfg_needed = recfg_needed; - APPL_TRACE_DEBUG("%s: accept reconf=%d", __func__, recfg_needed); - /* Call call-in accepting the configuration */ - bta_av_ci_setconfig(hndl, A2DP_SUCCESS, A2DP_SUCCESS, 0, NULL, recfg_needed, - avdt_handle); + /* Check if codec configuration is supported */ + if (codec_cfg_supported) { + /* Protect access to bta_av_co_cb.codec_cfg */ + mutex_global_lock(); + + /* Check if the configuration matches the current codec config */ + if (A2DP_CodecRequiresReconfig(p_codec_info, bta_av_co_cb.codec_cfg)) { + recfg_needed = true; + } else if ((num_protect == 1) && (!bta_av_co_cb.cp.active)) { + recfg_needed = true; + } + memcpy(bta_av_co_cb.codec_cfg_setconfig, p_codec_info, AVDT_CODEC_SIZE); + if (t_local_sep == AVDT_TSEP_SNK) { + /* + * If Peer is SRC, and our cfg subset matches with what is + * requested by peer, then just accept what peer wants. + */ + memcpy(bta_av_co_cb.codec_cfg, p_codec_info, AVDT_CODEC_SIZE); + recfg_needed = false; + } + /* Protect access to bta_av_co_cb.codec_cfg */ + mutex_global_unlock(); + } else { + category = AVDT_ASC_CODEC; + status = A2DP_WRONG_CODEC; + } + } + + if (status != A2DP_SUCCESS) { + APPL_TRACE_DEBUG("%s: reject s=%d c=%d", __func__, status, category); + /* Call call-in rejecting the configuration */ + bta_av_ci_setconfig(hndl, status, category, 0, NULL, false, avdt_handle); + return; + } + + /* Mark that this is an acceptor peer */ + p_peer->acp = true; + p_peer->recfg_needed = recfg_needed; + APPL_TRACE_DEBUG("%s: accept reconf=%d", __func__, recfg_needed); + /* Call call-in accepting the configuration */ + bta_av_ci_setconfig(hndl, A2DP_SUCCESS, A2DP_SUCCESS, 0, NULL, recfg_needed, + avdt_handle); } /******************************************************************************* ** ** Function bta_av_co_audio_open ** - ** Description This function is called by AV when the audio stream connection + ** Description This function is called by AV when the audio stream + *connection ** is opened. ** ** ** Returns void ** *******************************************************************************/ -void bta_av_co_audio_open(tBTA_AV_HNDL hndl, uint8_t *p_codec_info, - uint16_t mtu) -{ - tBTA_AV_CO_PEER *p_peer; - - APPL_TRACE_DEBUG("%s: mtu:%d codec:%s", __func__, mtu, - A2DP_CodecName(p_codec_info)); - - /* Retrieve the peer info */ - p_peer = bta_av_co_get_peer(hndl); - if (p_peer == NULL) - { - APPL_TRACE_ERROR("%s: could not find peer entry", __func__); - } - else - { - p_peer->opened = true; - p_peer->mtu = mtu; - } +void bta_av_co_audio_open(tBTA_AV_HNDL hndl, uint8_t* p_codec_info, + uint16_t mtu) { + tBTA_AV_CO_PEER* p_peer; + + APPL_TRACE_DEBUG("%s: mtu:%d codec:%s", __func__, mtu, + A2DP_CodecName(p_codec_info)); + + /* Retrieve the peer info */ + p_peer = bta_av_co_get_peer(hndl); + if (p_peer == NULL) { + APPL_TRACE_ERROR("%s: could not find peer entry", __func__); + } else { + p_peer->opened = true; + p_peer->mtu = mtu; + } } /******************************************************************************* ** ** Function bta_av_co_audio_close ** - ** Description This function is called by AV when the audio stream connection + ** Description This function is called by AV when the audio stream + *connection ** is closed. ** ** @@ -694,25 +653,22 @@ void bta_av_co_audio_open(tBTA_AV_HNDL hndl, uint8_t *p_codec_info, void bta_av_co_audio_close(tBTA_AV_HNDL hndl, UNUSED_ATTR uint16_t mtu) { - tBTA_AV_CO_PEER *p_peer; - - APPL_TRACE_DEBUG("%s", __func__); - - /* Retrieve the peer info */ - p_peer = bta_av_co_get_peer(hndl); - if (p_peer) - { - /* Mark the peer closed and clean the peer info */ - memset(p_peer, 0, sizeof(*p_peer)); - } - else - { - APPL_TRACE_ERROR("%s: could not find peer entry", __func__); - } - - /* reset remote preference through setconfig */ - memset(bta_av_co_cb.codec_cfg_setconfig, 0, - sizeof(bta_av_co_cb.codec_cfg_setconfig)); + tBTA_AV_CO_PEER* p_peer; + + APPL_TRACE_DEBUG("%s", __func__); + + /* Retrieve the peer info */ + p_peer = bta_av_co_get_peer(hndl); + if (p_peer) { + /* Mark the peer closed and clean the peer info */ + memset(p_peer, 0, sizeof(*p_peer)); + } else { + APPL_TRACE_ERROR("%s: could not find peer entry", __func__); + } + + /* reset remote preference through setconfig */ + memset(bta_av_co_cb.codec_cfg_setconfig, 0, + sizeof(bta_av_co_cb.codec_cfg_setconfig)); } /******************************************************************************* @@ -727,10 +683,9 @@ void bta_av_co_audio_close(tBTA_AV_HNDL hndl, UNUSED_ATTR uint16_t mtu) ** *******************************************************************************/ void bta_av_co_audio_start(UNUSED_ATTR tBTA_AV_HNDL hndl, - UNUSED_ATTR uint8_t *p_codec_info, - UNUSED_ATTR bool *p_no_rtp_hdr) -{ - APPL_TRACE_DEBUG("%s", __func__); + UNUSED_ATTR uint8_t* p_codec_info, + UNUSED_ATTR bool* p_no_rtp_hdr) { + APPL_TRACE_DEBUG("%s", __func__); } /******************************************************************************* @@ -744,9 +699,8 @@ void bta_av_co_audio_start(UNUSED_ATTR tBTA_AV_HNDL hndl, ** Returns void ** *******************************************************************************/ -void bta_av_co_audio_stop(UNUSED_ATTR tBTA_AV_HNDL hndl) -{ - APPL_TRACE_DEBUG("%s", __func__); +void bta_av_co_audio_stop(UNUSED_ATTR tBTA_AV_HNDL hndl) { + APPL_TRACE_DEBUG("%s", __func__); } /******************************************************************************* @@ -756,45 +710,44 @@ void bta_av_co_audio_stop(UNUSED_ATTR tBTA_AV_HNDL hndl) ** Description This function is called to manage data transfer from ** the audio codec to AVDTP. ** - ** Returns Pointer to the GKI buffer to send, NULL if no buffer to send + ** Returns Pointer to the GKI buffer to send, NULL if no buffer to + *send ** *******************************************************************************/ -void* bta_av_co_audio_src_data_path(const uint8_t *p_codec_info, - uint32_t *p_timestamp) -{ - BT_HDR *p_buf; - - APPL_TRACE_DEBUG("%s: codec: %s", __func__, A2DP_CodecName(p_codec_info)); - - p_buf = btif_a2dp_source_audio_readbuf(); - if (p_buf == NULL) - return NULL; - - /* - * Retrieve the timestamp information from the media packet, - * and set up the packet header. - * - * In media packet, the following information is available: - * p_buf->layer_specific : number of audio frames in the packet - * p_buf->word[0] : timestamp - */ - if (!A2DP_GetPacketTimestamp(p_codec_info, (const uint8_t *)(p_buf + 1), - p_timestamp) || - !A2DP_BuildCodecHeader(p_codec_info, p_buf, p_buf->layer_specific)) { - APPL_TRACE_ERROR("%s: unsupported codec type (%d)", __func__, - A2DP_GetCodecType(p_codec_info)); - } +void* bta_av_co_audio_src_data_path(const uint8_t* p_codec_info, + uint32_t* p_timestamp) { + BT_HDR* p_buf; + + APPL_TRACE_DEBUG("%s: codec: %s", __func__, A2DP_CodecName(p_codec_info)); + + p_buf = btif_a2dp_source_audio_readbuf(); + if (p_buf == NULL) return NULL; + + /* + * Retrieve the timestamp information from the media packet, + * and set up the packet header. + * + * In media packet, the following information is available: + * p_buf->layer_specific : number of audio frames in the packet + * p_buf->word[0] : timestamp + */ + if (!A2DP_GetPacketTimestamp(p_codec_info, (const uint8_t*)(p_buf + 1), + p_timestamp) || + !A2DP_BuildCodecHeader(p_codec_info, p_buf, p_buf->layer_specific)) { + APPL_TRACE_ERROR("%s: unsupported codec type (%d)", __func__, + A2DP_GetCodecType(p_codec_info)); + } #if (BTA_AV_CO_CP_SCMS_T == TRUE) - if (bta_av_co_cb.cp.active) { - p_buf->len++; - p_buf->offset--; - uint8_t *p = (uint8_t *)(p_buf + 1) + p_buf->offset; - *p = bta_av_co_cp_get_flag(); - } + if (bta_av_co_cb.cp.active) { + p_buf->len++; + p_buf->offset--; + uint8_t* p = (uint8_t*)(p_buf + 1) + p_buf->offset; + *p = bta_av_co_cp_get_flag(); + } #endif - return p_buf; + return p_buf; } /******************************************************************************* @@ -802,33 +755,33 @@ void* bta_av_co_audio_src_data_path(const uint8_t *p_codec_info, ** Function bta_av_co_audio_drop ** ** Description An Audio packet is dropped. . - ** It's very likely that the connected headset with this handle + ** It's very likely that the connected headset with this + *handle ** is moved far away. The implementation may want to reduce ** the encoder bit rate setting to reduce the packet size. ** ** Returns void ** *******************************************************************************/ -void bta_av_co_audio_drop(tBTA_AV_HNDL hndl) -{ - APPL_TRACE_ERROR("%s: dropped audio packet on handle 0x%x", - __func__, hndl); +void bta_av_co_audio_drop(tBTA_AV_HNDL hndl) { + APPL_TRACE_ERROR("%s: dropped audio packet on handle 0x%x", __func__, hndl); } /******************************************************************************* ** ** Function bta_av_co_audio_delay ** - ** Description This function is called by AV when the audio stream connection - ** needs to send the initial delay report to the connected SRC. + ** Description This function is called by AV when the audio stream + *connection + ** needs to send the initial delay report to the connected + *SRC. ** ** ** Returns void ** *******************************************************************************/ -void bta_av_co_audio_delay(tBTA_AV_HNDL hndl, uint16_t delay) -{ - APPL_TRACE_ERROR("%s: handle: x%x, delay:0x%x", __func__, hndl, delay); +void bta_av_co_audio_delay(tBTA_AV_HNDL hndl, uint16_t delay) { + APPL_TRACE_ERROR("%s: handle: x%x, delay:0x%x", __func__, hndl, delay); } /******************************************************************************* @@ -840,23 +793,21 @@ void bta_av_co_audio_delay(tBTA_AV_HNDL hndl, uint16_t delay) ** Returns true if this CP is SCMS-T, false otherwise ** *******************************************************************************/ -static bool bta_av_co_cp_is_scmst(const uint8_t *p_protectinfo) -{ - APPL_TRACE_DEBUG("%s", __func__); +static bool bta_av_co_cp_is_scmst(const uint8_t* p_protectinfo) { + APPL_TRACE_DEBUG("%s", __func__); - if (*p_protectinfo >= BTA_AV_CP_LOSC) { - uint16_t cp_id; + if (*p_protectinfo >= BTA_AV_CP_LOSC) { + uint16_t cp_id; - p_protectinfo++; - STREAM_TO_UINT16(cp_id, p_protectinfo); - if (cp_id == BTA_AV_CP_SCMS_T_ID) - { - APPL_TRACE_DEBUG("%s: SCMS-T found", __func__); - return true; - } + p_protectinfo++; + STREAM_TO_UINT16(cp_id, p_protectinfo); + if (cp_id == BTA_AV_CP_SCMS_T_ID) { + APPL_TRACE_DEBUG("%s: SCMS-T found", __func__); + return true; } + } - return false; + return false; } /******************************************************************************* @@ -868,27 +819,25 @@ static bool bta_av_co_cp_is_scmst(const uint8_t *p_protectinfo) ** Returns true if the sink supports this CP, false otherwise ** *******************************************************************************/ -static bool bta_av_co_audio_sink_has_scmst(const tBTA_AV_CO_SINK *p_sink) -{ - uint8_t index; - const uint8_t *p; - - APPL_TRACE_DEBUG("%s", __func__); - - /* Check if sink supports SCMS-T */ - index = p_sink->num_protect; - p = &p_sink->protect_info[0]; - - while (index) { - if (bta_av_co_cp_is_scmst(p)) - return true; - /* Move to the next SC */ - p += *p + 1; - /* Decrement the SC counter */ - index--; - } - APPL_TRACE_DEBUG("%s: SCMS-T not found", __func__); - return false; +static bool bta_av_co_audio_sink_has_scmst(const tBTA_AV_CO_SINK* p_sink) { + uint8_t index; + const uint8_t* p; + + APPL_TRACE_DEBUG("%s", __func__); + + /* Check if sink supports SCMS-T */ + index = p_sink->num_protect; + p = &p_sink->protect_info[0]; + + while (index) { + if (bta_av_co_cp_is_scmst(p)) return true; + /* Move to the next SC */ + p += *p + 1; + /* Decrement the SC counter */ + index--; + } + APPL_TRACE_DEBUG("%s: SCMS-T not found", __func__); + return false; } /******************************************************************************* @@ -900,16 +849,15 @@ static bool bta_av_co_audio_sink_has_scmst(const tBTA_AV_CO_SINK *p_sink) ** Returns true if the sink supports this CP, false otherwise ** *******************************************************************************/ -static bool bta_av_co_audio_sink_supports_cp(const tBTA_AV_CO_SINK *p_sink) -{ - APPL_TRACE_DEBUG("%s", __func__); +static bool bta_av_co_audio_sink_supports_cp(const tBTA_AV_CO_SINK* p_sink) { + APPL_TRACE_DEBUG("%s", __func__); - /* Check if content protection is enabled for this stream */ - if (bta_av_co_cp_get_flag() != BTA_AV_CP_SCMS_COPY_FREE) - return bta_av_co_audio_sink_has_scmst(p_sink); + /* Check if content protection is enabled for this stream */ + if (bta_av_co_cp_get_flag() != BTA_AV_CP_SCMS_COPY_FREE) + return bta_av_co_audio_sink_has_scmst(p_sink); - APPL_TRACE_DEBUG("%s: not required", __func__); - return true; + APPL_TRACE_DEBUG("%s: not required", __func__); + return true; } /******************************************************************************* @@ -922,18 +870,17 @@ static bool bta_av_co_audio_sink_supports_cp(const tBTA_AV_CO_SINK *p_sink) ** *******************************************************************************/ static const tBTA_AV_CO_SINK* bta_av_co_find_peer_sink_supports_codec( - const tBTA_AV_CO_PEER *p_peer) -{ - APPL_TRACE_DEBUG("%s: peer num_sup_sinks = %d", - __func__, p_peer->num_sup_sinks); - - for (size_t index = 0; index < p_peer->num_sup_sinks; index++) { - if (A2DP_CodecConfigMatchesCapabilities(bta_av_co_cb.codec_cfg, - p_peer->sinks[index].codec_caps)) { - return &p_peer->sinks[index]; - } + const tBTA_AV_CO_PEER* p_peer) { + APPL_TRACE_DEBUG("%s: peer num_sup_sinks = %d", __func__, + p_peer->num_sup_sinks); + + for (size_t index = 0; index < p_peer->num_sup_sinks; index++) { + if (A2DP_CodecConfigMatchesCapabilities(bta_av_co_cb.codec_cfg, + p_peer->sinks[index].codec_caps)) { + return &p_peer->sinks[index]; } - return NULL; + } + return NULL; } /******************************************************************************* @@ -946,19 +893,18 @@ static const tBTA_AV_CO_SINK* bta_av_co_find_peer_sink_supports_codec( ** *******************************************************************************/ static const tBTA_AV_CO_SINK* bta_av_co_find_peer_src_supports_codec( - const tBTA_AV_CO_PEER *p_peer) -{ - APPL_TRACE_DEBUG("%s: peer num_sup_srcs = %d", - __func__, p_peer->num_sup_srcs); - - for (size_t index = 0; index < p_peer->num_sup_srcs; index++) { - const uint8_t *p_codec_caps = p_peer->srcs[index].codec_caps; - if (A2DP_CodecTypeEquals(bta_av_co_cb.codec_cfg, p_codec_caps) && - A2DP_IsPeerSourceCodecSupported(p_codec_caps)) { - return &p_peer->srcs[index]; - } + const tBTA_AV_CO_PEER* p_peer) { + APPL_TRACE_DEBUG("%s: peer num_sup_srcs = %d", __func__, + p_peer->num_sup_srcs); + + for (size_t index = 0; index < p_peer->num_sup_srcs; index++) { + const uint8_t* p_codec_caps = p_peer->srcs[index].codec_caps; + if (A2DP_CodecTypeEquals(bta_av_co_cb.codec_cfg, p_codec_caps) && + A2DP_IsPeerSourceCodecSupported(p_codec_caps)) { + return &p_peer->srcs[index]; } - return NULL; + } + return NULL; } /******************************************************************************* @@ -968,74 +914,71 @@ static const tBTA_AV_CO_SINK* bta_av_co_find_peer_src_supports_codec( ** Description Check if all opened connections are compatible with a codec ** configuration and content protection ** - ** Returns true if all opened devices support this codec, false otherwise + ** Returns true if all opened devices support this codec, false + *otherwise ** *******************************************************************************/ -static bool bta_av_co_audio_codec_supported(void) -{ - APPL_TRACE_DEBUG("%s", __func__); - - /* Check AV feeding is supported */ - for (uint8_t index = 0; index < BTA_AV_CO_NUM_ELEMENTS(bta_av_co_cb.peers); - index++) { - uint8_t codec_cfg[AVDT_CODEC_SIZE]; - - tBTA_AV_CO_PEER *p_peer = &bta_av_co_cb.peers[index]; - if (!p_peer->opened) - continue; - - const tBTA_AV_CO_SINK *p_sink = - bta_av_co_find_peer_sink_supports_codec(p_peer); - if (p_sink == NULL) { - APPL_TRACE_DEBUG("%s: index %d doesn't support codec", - __func__, index); - return false; - } +static bool bta_av_co_audio_codec_supported(void) { + APPL_TRACE_DEBUG("%s", __func__); - /* Check that this sink is compatible with the CP */ - if (!bta_av_co_audio_sink_supports_cp(p_sink)) { - APPL_TRACE_DEBUG("%s: sink of peer %d doesn't support cp", - __func__, index); - return false; - } + /* Check AV feeding is supported */ + for (uint8_t index = 0; index < BTA_AV_CO_NUM_ELEMENTS(bta_av_co_cb.peers); + index++) { + uint8_t codec_cfg[AVDT_CODEC_SIZE]; - /* Build the codec configuration for this sink */ - memset(codec_cfg, 0, AVDT_CODEC_SIZE); - if (A2DP_BuildSinkConfig(bta_av_co_cb.codec_cfg, p_sink->codec_caps, - codec_cfg) != A2DP_SUCCESS) { - continue; - } + tBTA_AV_CO_PEER* p_peer = &bta_av_co_cb.peers[index]; + if (!p_peer->opened) continue; + + const tBTA_AV_CO_SINK* p_sink = + bta_av_co_find_peer_sink_supports_codec(p_peer); + if (p_sink == NULL) { + APPL_TRACE_DEBUG("%s: index %d doesn't support codec", __func__, index); + return false; + } + + /* Check that this sink is compatible with the CP */ + if (!bta_av_co_audio_sink_supports_cp(p_sink)) { + APPL_TRACE_DEBUG("%s: sink of peer %d doesn't support cp", __func__, + index); + return false; + } + + /* Build the codec configuration for this sink */ + memset(codec_cfg, 0, AVDT_CODEC_SIZE); + if (A2DP_BuildSinkConfig(bta_av_co_cb.codec_cfg, p_sink->codec_caps, + codec_cfg) != A2DP_SUCCESS) { + continue; + } #if (BTA_AV_CO_CP_SCMS_T == TRUE) - /* Check if this sink supports SCMS */ - bool cp_active = bta_av_co_audio_sink_has_scmst(p_sink); + /* Check if this sink supports SCMS */ + bool cp_active = bta_av_co_audio_sink_has_scmst(p_sink); #endif - /* Check if this is a new configuration (new sink or new config) */ - if ((p_sink != p_peer->p_sink) || - (!A2DP_CodecEquals(codec_cfg, p_peer->codec_cfg)) + /* Check if this is a new configuration (new sink or new config) */ + if ((p_sink != p_peer->p_sink) || + (!A2DP_CodecEquals(codec_cfg, p_peer->codec_cfg)) #if (BTA_AV_CO_CP_SCMS_T == TRUE) - || (p_peer->cp_active != cp_active) + || (p_peer->cp_active != cp_active) #endif - ) { - uint8_t num_protect = 0; + ) { + uint8_t num_protect = 0; - /* Save the new configuration */ - p_peer->p_sink = p_sink; - memcpy(p_peer->codec_cfg, codec_cfg, AVDT_CODEC_SIZE); + /* Save the new configuration */ + p_peer->p_sink = p_sink; + memcpy(p_peer->codec_cfg, codec_cfg, AVDT_CODEC_SIZE); #if (BTA_AV_CO_CP_SCMS_T == TRUE) - bta_av_co_cb.cp.active = cp_active; - p_peer->cp_active = cp_active; - if (p_peer->cp_active) - num_protect = BTA_AV_CP_INFO_LEN; + bta_av_co_cb.cp.active = cp_active; + p_peer->cp_active = cp_active; + if (p_peer->cp_active) num_protect = BTA_AV_CP_INFO_LEN; #endif - APPL_TRACE_DEBUG("%s: call BTA_AvReconfig(0x%x)", __func__, - BTA_AV_CO_AUDIO_INDX_TO_HNDL(index)); - BTA_AvReconfig(BTA_AV_CO_AUDIO_INDX_TO_HNDL(index), true, - p_sink->sep_info_idx, p_peer->codec_cfg, - num_protect, bta_av_co_cp_scmst); - } + APPL_TRACE_DEBUG("%s: call BTA_AvReconfig(0x%x)", __func__, + BTA_AV_CO_AUDIO_INDX_TO_HNDL(index)); + BTA_AvReconfig(BTA_AV_CO_AUDIO_INDX_TO_HNDL(index), true, + p_sink->sep_info_idx, p_peer->codec_cfg, num_protect, + bta_av_co_cp_scmst); } + } - return true; + return true; } /******************************************************************************* @@ -1047,16 +990,15 @@ static bool bta_av_co_audio_codec_supported(void) ** Returns void ** *******************************************************************************/ -static void bta_av_co_audio_codec_reset(void) -{ - APPL_TRACE_DEBUG("%s", __func__); +static void bta_av_co_audio_codec_reset(void) { + APPL_TRACE_DEBUG("%s", __func__); - mutex_global_lock(); + mutex_global_lock(); - /* Reset the current configuration to the default codec */ - A2DP_InitDefaultCodec(bta_av_co_cb.codec_cfg); + /* Reset the current configuration to the default codec */ + A2DP_InitDefaultCodec(bta_av_co_cb.codec_cfg); - mutex_global_unlock(); + mutex_global_unlock(); } /******************************************************************************* @@ -1070,142 +1012,133 @@ static void bta_av_co_audio_codec_reset(void) ** Returns true if successful, false otherwise ** *******************************************************************************/ -bool bta_av_co_audio_set_codec(const tA2DP_FEEDING_PARAMS *p_feeding_params) -{ - uint8_t new_cfg[AVDT_CODEC_SIZE]; +bool bta_av_co_audio_set_codec(const tA2DP_FEEDING_PARAMS* p_feeding_params) { + uint8_t new_cfg[AVDT_CODEC_SIZE]; - if (!A2DP_SetCodec(p_feeding_params, new_cfg)) - return false; + if (!A2DP_SetCodec(p_feeding_params, new_cfg)) return false; - /* The new config was correctly built */ - memcpy(bta_av_co_cb.codec_cfg, new_cfg, sizeof(new_cfg)); + /* The new config was correctly built */ + memcpy(bta_av_co_cb.codec_cfg, new_cfg, sizeof(new_cfg)); - /* Check all devices support it */ - return bta_av_co_audio_codec_supported(); + /* Check all devices support it */ + return bta_av_co_audio_codec_supported(); } -void bta_av_co_audio_encoder_init(tA2DP_ENCODER_INIT_PARAMS *p_init_params) -{ - uint16_t min_mtu = 0xFFFF; +void bta_av_co_audio_encoder_init(tA2DP_ENCODER_INIT_PARAMS* p_init_params) { + uint16_t min_mtu = 0xFFFF; - APPL_TRACE_DEBUG("%s", __func__); - assert(p_init_params != nullptr); + APPL_TRACE_DEBUG("%s", __func__); + assert(p_init_params != nullptr); - /* Protect access to bta_av_co_cb.codec_cfg */ - mutex_global_lock(); + /* Protect access to bta_av_co_cb.codec_cfg */ + mutex_global_lock(); - /* Compute the MTU */ - for (size_t i = 0; i < BTA_AV_CO_NUM_ELEMENTS(bta_av_co_cb.peers); i++) { - const tBTA_AV_CO_PEER *p_peer = &bta_av_co_cb.peers[i]; - if (!p_peer->opened) - continue; - if (p_peer->mtu < min_mtu) - min_mtu = p_peer->mtu; - } + /* Compute the MTU */ + for (size_t i = 0; i < BTA_AV_CO_NUM_ELEMENTS(bta_av_co_cb.peers); i++) { + const tBTA_AV_CO_PEER* p_peer = &bta_av_co_cb.peers[i]; + if (!p_peer->opened) continue; + if (p_peer->mtu < min_mtu) min_mtu = p_peer->mtu; + } - const uint8_t *p_codec_info = bta_av_co_cb.codec_cfg; - p_init_params->NumOfSubBands = A2DP_GetNumberOfSubbands(p_codec_info); - p_init_params->NumOfBlocks = A2DP_GetNumberOfBlocks(p_codec_info); - p_init_params->AllocationMethod = A2DP_GetAllocationMethodCode(p_codec_info); - p_init_params->ChannelMode = A2DP_GetChannelModeCode(p_codec_info); - p_init_params->SamplingFreq = A2DP_GetSamplingFrequencyCode(p_codec_info); - p_init_params->MtuSize = min_mtu; + const uint8_t* p_codec_info = bta_av_co_cb.codec_cfg; + p_init_params->NumOfSubBands = A2DP_GetNumberOfSubbands(p_codec_info); + p_init_params->NumOfBlocks = A2DP_GetNumberOfBlocks(p_codec_info); + p_init_params->AllocationMethod = A2DP_GetAllocationMethodCode(p_codec_info); + p_init_params->ChannelMode = A2DP_GetChannelModeCode(p_codec_info); + p_init_params->SamplingFreq = A2DP_GetSamplingFrequencyCode(p_codec_info); + p_init_params->MtuSize = min_mtu; - /* Protect access to bta_av_co_cb.codec_cfg */ - mutex_global_unlock(); + /* Protect access to bta_av_co_cb.codec_cfg */ + mutex_global_unlock(); } -void bta_av_co_audio_encoder_update(tA2DP_ENCODER_UPDATE_PARAMS *p_update_params) -{ - uint16_t min_mtu = 0xFFFF; - - APPL_TRACE_DEBUG("%s", __func__); - assert(p_update_params != nullptr); +void bta_av_co_audio_encoder_update( + tA2DP_ENCODER_UPDATE_PARAMS* p_update_params) { + uint16_t min_mtu = 0xFFFF; - /* Protect access to bta_av_co_cb.codec_cfg */ - mutex_global_lock(); + APPL_TRACE_DEBUG("%s", __func__); + assert(p_update_params != nullptr); - const uint8_t *p_codec_info = bta_av_co_cb.codec_cfg; - int min_bitpool = A2DP_GetMinBitpool(p_codec_info); - int max_bitpool = A2DP_GetMaxBitpool(p_codec_info); - - if ((min_bitpool < 0) || (max_bitpool < 0)) { - APPL_TRACE_ERROR("%s: Invalid min/max bitpool: [%d, %d]", - __func__, min_bitpool, max_bitpool); - mutex_global_unlock(); - return; - } + /* Protect access to bta_av_co_cb.codec_cfg */ + mutex_global_lock(); - for (size_t i = 0; i < BTA_AV_CO_NUM_ELEMENTS(bta_av_co_cb.peers); i++) { - const tBTA_AV_CO_PEER *p_peer = &bta_av_co_cb.peers[i]; - if (!p_peer->opened) - continue; - - if (p_peer->mtu < min_mtu) - min_mtu = p_peer->mtu; - - for (int j = 0; j < p_peer->num_sup_sinks; j++) { - const tBTA_AV_CO_SINK *p_sink = &p_peer->sinks[j]; - if (!A2DP_CodecTypeEquals(p_codec_info, p_sink->codec_caps)) - continue; - /* Update the bitpool boundaries of the current config */ - int peer_min_bitpool = A2DP_GetMinBitpool(p_sink->codec_caps); - int peer_max_bitpool = A2DP_GetMaxBitpool(p_sink->codec_caps); - if (peer_min_bitpool >= 0) - min_bitpool = BTA_AV_CO_MAX(min_bitpool, peer_min_bitpool); - if (peer_max_bitpool >= 0) - max_bitpool = BTA_AV_CO_MIN(max_bitpool, peer_max_bitpool); - APPL_TRACE_EVENT("%s: sink bitpool min %d, max %d", - __func__, min_bitpool, max_bitpool); - break; - } - } - - /* - * Check if the remote Sink has a preferred bitpool range. - * Adjust our preferred bitpool with the remote preference if within - * our capable range. - */ - if (A2DP_IsSourceCodecValid(bta_av_co_cb.codec_cfg_setconfig) && - A2DP_CodecTypeEquals(p_codec_info, bta_av_co_cb.codec_cfg_setconfig)) { - int setconfig_min_bitpool = - A2DP_GetMinBitpool(bta_av_co_cb.codec_cfg_setconfig); - int setconfig_max_bitpool = - A2DP_GetMaxBitpool(bta_av_co_cb.codec_cfg_setconfig); - if (setconfig_min_bitpool >= 0) - min_bitpool = BTA_AV_CO_MAX(min_bitpool, setconfig_min_bitpool); - if (setconfig_max_bitpool >= 0) - max_bitpool = BTA_AV_CO_MIN(max_bitpool, setconfig_max_bitpool); - APPL_TRACE_EVENT("%s: sink adjusted bitpool min %d, max %d", - __func__, min_bitpool, max_bitpool); - } + const uint8_t* p_codec_info = bta_av_co_cb.codec_cfg; + int min_bitpool = A2DP_GetMinBitpool(p_codec_info); + int max_bitpool = A2DP_GetMaxBitpool(p_codec_info); - /* Protect access to bta_av_co_cb.codec_cfg */ + if ((min_bitpool < 0) || (max_bitpool < 0)) { + APPL_TRACE_ERROR("%s: Invalid min/max bitpool: [%d, %d]", __func__, + min_bitpool, max_bitpool); mutex_global_unlock(); - - if (min_bitpool > max_bitpool) { - APPL_TRACE_ERROR("%s: Irrational min/max bitpool: [%d, %d]", - __func__, min_bitpool, max_bitpool); - return; + return; + } + + for (size_t i = 0; i < BTA_AV_CO_NUM_ELEMENTS(bta_av_co_cb.peers); i++) { + const tBTA_AV_CO_PEER* p_peer = &bta_av_co_cb.peers[i]; + if (!p_peer->opened) continue; + + if (p_peer->mtu < min_mtu) min_mtu = p_peer->mtu; + + for (int j = 0; j < p_peer->num_sup_sinks; j++) { + const tBTA_AV_CO_SINK* p_sink = &p_peer->sinks[j]; + if (!A2DP_CodecTypeEquals(p_codec_info, p_sink->codec_caps)) continue; + /* Update the bitpool boundaries of the current config */ + int peer_min_bitpool = A2DP_GetMinBitpool(p_sink->codec_caps); + int peer_max_bitpool = A2DP_GetMaxBitpool(p_sink->codec_caps); + if (peer_min_bitpool >= 0) + min_bitpool = BTA_AV_CO_MAX(min_bitpool, peer_min_bitpool); + if (peer_max_bitpool >= 0) + max_bitpool = BTA_AV_CO_MIN(max_bitpool, peer_max_bitpool); + APPL_TRACE_EVENT("%s: sink bitpool min %d, max %d", __func__, min_bitpool, + max_bitpool); + break; } - - p_update_params->MinMtuSize = min_mtu; - p_update_params->MinBitPool = min_bitpool; - p_update_params->MaxBitPool = max_bitpool; + } + + /* + * Check if the remote Sink has a preferred bitpool range. + * Adjust our preferred bitpool with the remote preference if within + * our capable range. + */ + if (A2DP_IsSourceCodecValid(bta_av_co_cb.codec_cfg_setconfig) && + A2DP_CodecTypeEquals(p_codec_info, bta_av_co_cb.codec_cfg_setconfig)) { + int setconfig_min_bitpool = + A2DP_GetMinBitpool(bta_av_co_cb.codec_cfg_setconfig); + int setconfig_max_bitpool = + A2DP_GetMaxBitpool(bta_av_co_cb.codec_cfg_setconfig); + if (setconfig_min_bitpool >= 0) + min_bitpool = BTA_AV_CO_MAX(min_bitpool, setconfig_min_bitpool); + if (setconfig_max_bitpool >= 0) + max_bitpool = BTA_AV_CO_MIN(max_bitpool, setconfig_max_bitpool); + APPL_TRACE_EVENT("%s: sink adjusted bitpool min %d, max %d", __func__, + min_bitpool, max_bitpool); + } + + /* Protect access to bta_av_co_cb.codec_cfg */ + mutex_global_unlock(); + + if (min_bitpool > max_bitpool) { + APPL_TRACE_ERROR("%s: Irrational min/max bitpool: [%d, %d]", __func__, + min_bitpool, max_bitpool); + return; + } + + p_update_params->MinMtuSize = min_mtu; + p_update_params->MinBitPool = min_bitpool; + p_update_params->MaxBitPool = max_bitpool; } -const tA2DP_ENCODER_INTERFACE *bta_av_co_get_encoder_interface(void) -{ - /* Protect access to bta_av_co_cb.codec_cfg */ - mutex_global_lock(); +const tA2DP_ENCODER_INTERFACE* bta_av_co_get_encoder_interface(void) { + /* Protect access to bta_av_co_cb.codec_cfg */ + mutex_global_lock(); - const tA2DP_ENCODER_INTERFACE *encoder_interface = - A2DP_GetEncoderInterface(bta_av_co_cb.codec_cfg); + const tA2DP_ENCODER_INTERFACE* encoder_interface = + A2DP_GetEncoderInterface(bta_av_co_cb.codec_cfg); - /* Protect access to bta_av_co_cb.codec_cfg */ - mutex_global_unlock(); + /* Protect access to bta_av_co_cb.codec_cfg */ + mutex_global_unlock(); - return encoder_interface; + return encoder_interface; } /******************************************************************************* @@ -1217,19 +1150,18 @@ const tA2DP_ENCODER_INTERFACE *bta_av_co_get_encoder_interface(void) ** Returns Nothing ** *******************************************************************************/ -void bta_av_co_init(void) -{ - APPL_TRACE_DEBUG("%s", __func__); +void bta_av_co_init(void) { + APPL_TRACE_DEBUG("%s", __func__); - /* Reset the control block */ - memset(&bta_av_co_cb, 0, sizeof(bta_av_co_cb)); + /* Reset the control block */ + memset(&bta_av_co_cb, 0, sizeof(bta_av_co_cb)); #if (BTA_AV_CO_CP_SCMS_T == TRUE) - bta_av_co_cp_set_flag(BTA_AV_CP_SCMS_COPY_NEVER); + bta_av_co_cp_set_flag(BTA_AV_CP_SCMS_COPY_NEVER); #else - bta_av_co_cp_set_flag(BTA_AV_CP_SCMS_COPY_FREE); + bta_av_co_cp_set_flag(BTA_AV_CP_SCMS_COPY_FREE); #endif - /* Reset the current config */ - bta_av_co_audio_codec_reset(); + /* Reset the current config */ + bta_av_co_audio_codec_reset(); } diff --git a/btif/co/bta_dm_co.cc b/btif/co/bta_dm_co.cc index 3e8bda738..4ea6a964e 100644 --- a/btif/co/bta_dm_co.cc +++ b/btif/co/bta_dm_co.cc @@ -18,421 +18,411 @@ #include #include +#include "bt_utils.h" #include "bta_api.h" -#include "bta_sys.h" -#include "bta_dm_co.h" #include "bta_dm_ci.h" -#include "bt_utils.h" +#include "bta_dm_co.h" +#include "bta_sys.h" #include "btif_dm.h" #if (BLE_INCLUDED == TRUE) #include "bte_appl.h" -tBTE_APPL_CFG bte_appl_cfg = -{ +tBTE_APPL_CFG bte_appl_cfg = { #if (SMP_INCLUDED == TRUE) - BTA_LE_AUTH_REQ_SC_MITM_BOND, // Authentication requirements + BTA_LE_AUTH_REQ_SC_MITM_BOND, // Authentication requirements #else - BTM_AUTH_SPGB_YES, // Authentication requirements + BTM_AUTH_SPGB_YES, // Authentication requirements #endif - BTM_LOCAL_IO_CAPS_BLE, - BTM_BLE_INITIATOR_KEY_SIZE, - BTM_BLE_RESPONDER_KEY_SIZE, - BTM_BLE_MAX_KEY_SIZE -}; + BTM_LOCAL_IO_CAPS_BLE, BTM_BLE_INITIATOR_KEY_SIZE, + BTM_BLE_RESPONDER_KEY_SIZE, BTM_BLE_MAX_KEY_SIZE}; #endif /******************************************************************************* -** -** Function bta_dm_co_get_compress_memory -** -** Description This callout function is executed by DM to get memory for compression - -** Parameters id - BTA SYS ID -** memory_p - memory return by callout -** memory_size - memory size -** -** Returns true for success, false for fail. -** -*******************************************************************************/ -bool bta_dm_co_get_compress_memory(tBTA_SYS_ID id, uint8_t **memory_p, uint32_t *memory_size) -{ - UNUSED(id); - UNUSED(memory_p); - UNUSED(memory_size); - return true; + * + * Function bta_dm_co_get_compress_memory + * + * Description This callout function is executed by DM to get memory for + compression + + * Parameters id - BTA SYS ID + * memory_p - memory return by callout + * memory_size - memory size + * + * Returns true for success, false for fail. + * + ******************************************************************************/ +bool bta_dm_co_get_compress_memory(tBTA_SYS_ID id, uint8_t** memory_p, + uint32_t* memory_size) { + UNUSED(id); + UNUSED(memory_p); + UNUSED(memory_size); + return true; } /******************************************************************************* -** -** Function bta_dm_co_io_req -** -** Description This callout function is executed by DM to get IO capabilities -** of the local device for the Simple Pairing process -** -** Parameters bd_addr - The peer device -** *p_io_cap - The local Input/Output capabilities -** *p_oob_data - true, if OOB data is available for the peer device. -** *p_auth_req - true, if MITM protection is required. -** -** Returns void. -** -*******************************************************************************/ -void bta_dm_co_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data, - tBTA_AUTH_REQ *p_auth_req, bool is_orig) -{ - UNUSED(bd_addr); - btif_dm_set_oob_for_io_req(p_oob_data); - btif_dm_proc_io_req(bd_addr, p_io_cap, p_oob_data, p_auth_req, is_orig); - BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_oob_data = %d", *p_oob_data); - BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_io_cap = %d", *p_io_cap); - BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_auth_req = %d", *p_auth_req); - BTIF_TRACE_DEBUG("bta_dm_co_io_req is_orig = %d", is_orig); + * + * Function bta_dm_co_io_req + * + * Description This callout function is executed by DM to get IO + *capabilities + * of the local device for the Simple Pairing process + * + * Parameters bd_addr - The peer device + * *p_io_cap - The local Input/Output capabilities + * *p_oob_data - true, if OOB data is available for the peer + *device. + * *p_auth_req - true, if MITM protection is required. + * + * Returns void. + * + ******************************************************************************/ +void bta_dm_co_io_req(BD_ADDR bd_addr, tBTA_IO_CAP* p_io_cap, + tBTA_OOB_DATA* p_oob_data, tBTA_AUTH_REQ* p_auth_req, + bool is_orig) { + UNUSED(bd_addr); + btif_dm_set_oob_for_io_req(p_oob_data); + btif_dm_proc_io_req(bd_addr, p_io_cap, p_oob_data, p_auth_req, is_orig); + BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_oob_data = %d", *p_oob_data); + BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_io_cap = %d", *p_io_cap); + BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_auth_req = %d", *p_auth_req); + BTIF_TRACE_DEBUG("bta_dm_co_io_req is_orig = %d", is_orig); } /******************************************************************************* -** -** Function bta_dm_co_io_rsp -** -** Description This callout function is executed by DM to report IO capabilities -** of the peer device for the Simple Pairing process -** -** Parameters bd_addr - The peer device -** io_cap - The remote Input/Output capabilities -** oob_data - true, if OOB data is available for the peer device. -** auth_req - true, if MITM protection is required. -** -** Returns void. -** -*******************************************************************************/ + * + * Function bta_dm_co_io_rsp + * + * Description This callout function is executed by DM to report IO + *capabilities + * of the peer device for the Simple Pairing process + * + * Parameters bd_addr - The peer device + * io_cap - The remote Input/Output capabilities + * oob_data - true, if OOB data is available for the peer + *device. + * auth_req - true, if MITM protection is required. + * + * Returns void. + * + ******************************************************************************/ void bta_dm_co_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap, - tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req) -{ - btif_dm_proc_io_rsp(bd_addr, io_cap, oob_data, auth_req); + tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req) { + btif_dm_proc_io_rsp(bd_addr, io_cap, oob_data, auth_req); } /******************************************************************************* -** -** Function bta_dm_co_lk_upgrade -** -** Description This callout function is executed by DM to check if the -** platform wants allow link key upgrade -** -** Parameters bd_addr - The peer device -** *p_upgrade - true, if link key upgrade is desired. -** -** Returns void. -** -*******************************************************************************/ -void bta_dm_co_lk_upgrade(BD_ADDR bd_addr, bool *p_upgrade ) -{ - UNUSED(bd_addr); - UNUSED(p_upgrade); + * + * Function bta_dm_co_lk_upgrade + * + * Description This callout function is executed by DM to check if the + * platform wants allow link key upgrade + * + * Parameters bd_addr - The peer device + * *p_upgrade - true, if link key upgrade is desired. + * + * Returns void. + * + ******************************************************************************/ +void bta_dm_co_lk_upgrade(BD_ADDR bd_addr, bool* p_upgrade) { + UNUSED(bd_addr); + UNUSED(p_upgrade); } /******************************************************************************* -** -** Function bta_dm_co_loc_oob -** -** Description This callout function is executed by DM to report the OOB -** data of the local device for the Simple Pairing process -** -** Parameters valid - true, if the local OOB data is retrieved from LM -** c - Simple Pairing Hash C -** r - Simple Pairing Randomnizer R -** -** Returns void. -** -*******************************************************************************/ -void bta_dm_co_loc_oob(bool valid, BT_OCTET16 c, BT_OCTET16 r) -{ - BTIF_TRACE_DEBUG("bta_dm_co_loc_oob, valid = %d", valid); + * + * Function bta_dm_co_loc_oob + * + * Description This callout function is executed by DM to report the OOB + * data of the local device for the Simple Pairing process + * + * Parameters valid - true, if the local OOB data is retrieved from LM + * c - Simple Pairing Hash C + * r - Simple Pairing Randomnizer R + * + * Returns void. + * + ******************************************************************************/ +void bta_dm_co_loc_oob(bool valid, BT_OCTET16 c, BT_OCTET16 r) { + BTIF_TRACE_DEBUG("bta_dm_co_loc_oob, valid = %d", valid); #ifdef BTIF_DM_OOB_TEST - btif_dm_proc_loc_oob(valid, c, r); + btif_dm_proc_loc_oob(valid, c, r); #endif } /******************************************************************************* -** -** Function bta_dm_co_rmt_oob -** -** Description This callout function is executed by DM to request the OOB -** data for the remote device for the Simple Pairing process -** Need to call bta_dm_ci_rmt_oob() in response -** -** Parameters bd_addr - The peer device -** -** Returns void. -** -*******************************************************************************/ -void bta_dm_co_rmt_oob(BD_ADDR bd_addr) -{ - BT_OCTET16 p_c; - BT_OCTET16 p_r; - bool result = false; + * + * Function bta_dm_co_rmt_oob + * + * Description This callout function is executed by DM to request the OOB + * data for the remote device for the Simple Pairing process + * Need to call bta_dm_ci_rmt_oob() in response + * + * Parameters bd_addr - The peer device + * + * Returns void. + * + ******************************************************************************/ +void bta_dm_co_rmt_oob(BD_ADDR bd_addr) { + BT_OCTET16 p_c; + BT_OCTET16 p_r; + bool result = false; #ifdef BTIF_DM_OOB_TEST - result = btif_dm_proc_rmt_oob(bd_addr, p_c, p_r); + result = btif_dm_proc_rmt_oob(bd_addr, p_c, p_r); #endif - BTIF_TRACE_DEBUG("bta_dm_co_rmt_oob: result=%d",result); - bta_dm_ci_rmt_oob(result, bd_addr, p_c, p_r); + BTIF_TRACE_DEBUG("bta_dm_co_rmt_oob: result=%d", result); + bta_dm_ci_rmt_oob(result, bd_addr, p_c, p_r); } - // REMOVE FOR BLUEDROID ? #if (BTM_SCO_HCI_INCLUDED == TRUE) && (BTM_SCO_INCLUDED == TRUE) /******************************************************************************* -** -** Function btui_sco_codec_callback -** -** Description Callback for btui codec. -** -** -** Returns void -** -*******************************************************************************/ -static void btui_sco_codec_callback(uint16_t event, uint16_t sco_handle) -{ - bta_dm_sco_ci_data_ready(event, sco_handle); + * + * Function btui_sco_codec_callback + * + * Description Callback for btui codec. + * + * + * Returns void + * + ******************************************************************************/ +static void btui_sco_codec_callback(uint16_t event, uint16_t sco_handle) { + bta_dm_sco_ci_data_ready(event, sco_handle); } /******************************************************************************* -** -** Function bta_dm_sco_co_init -** -** Description This function can be used by the phone to initialize audio -** codec or for other initialization purposes before SCO connection -** is opened. -** -** -** Returns tBTA_DM_SCO_ROUTE_TYPE: SCO routing configuration type. -** -*******************************************************************************/ + * + * Function bta_dm_sco_co_init + * + * Description This function can be used by the phone to initialize audio + * codec or for other initialization purposes before SCO + *connection + * is opened. + * + * + * Returns tBTA_DM_SCO_ROUTE_TYPE: SCO routing configuration type. + * + ******************************************************************************/ tBTA_DM_SCO_ROUTE_TYPE bta_dm_sco_co_init(uint32_t rx_bw, uint32_t tx_bw, - tBTA_CODEC_INFO * p_codec_type, uint8_t app_id) -{ - tBTM_SCO_ROUTE_TYPE route = BTA_DM_SCO_ROUTE_PCM; + tBTA_CODEC_INFO* p_codec_type, + uint8_t app_id) { + tBTM_SCO_ROUTE_TYPE route = BTA_DM_SCO_ROUTE_PCM; - BTIF_TRACE_DEBUG("bta_dm_sco_co_init"); + BTIF_TRACE_DEBUG("bta_dm_sco_co_init"); - /* set up SCO routing configuration if SCO over HCI app ID is used and run time - configuration is set to SCO over HCI */ - /* HS invoke this call-out */ - if ( + /* set up SCO routing configuration if SCO over HCI app ID is used and run + time + configuration is set to SCO over HCI */ + /* HS invoke this call-out */ + if ( #if (BTA_HS_INCLUDED == TRUE) - (app_id == BTUI_DM_SCO_4_HS_APP_ID && btui_cfg.hs_sco_over_hci) || + (app_id == BTUI_DM_SCO_4_HS_APP_ID && btui_cfg.hs_sco_over_hci) || #endif - /* AG invoke this call-out */ - (app_id != BTUI_DM_SCO_4_HS_APP_ID && btui_cfg.ag_sco_over_hci )) - { - route = btui_cb.sco_hci = BTA_DM_SCO_ROUTE_HCI; - } - /* no codec is is used for the SCO data */ - if (p_codec_type->codec_type == BTA_SCO_CODEC_PCM && route == BTA_DM_SCO_ROUTE_HCI) - { - /* initialize SCO codec */ - if (!btui_sco_codec_init(rx_bw, tx_bw)) - { - BTIF_TRACE_ERROR("codec initialization exception!"); - } + /* AG invoke this call-out */ + (app_id != BTUI_DM_SCO_4_HS_APP_ID && btui_cfg.ag_sco_over_hci)) { + route = btui_cb.sco_hci = BTA_DM_SCO_ROUTE_HCI; + } + /* no codec is is used for the SCO data */ + if (p_codec_type->codec_type == BTA_SCO_CODEC_PCM && + route == BTA_DM_SCO_ROUTE_HCI) { + /* initialize SCO codec */ + if (!btui_sco_codec_init(rx_bw, tx_bw)) { + BTIF_TRACE_ERROR("codec initialization exception!"); } + } - return route; + return route; } - - /******************************************************************************* -** -** Function bta_dm_sco_co_open -** -** Description This function is executed when a SCO connection is open. -** -** -** Returns void -** -*******************************************************************************/ -void bta_dm_sco_co_open(uint16_t handle, uint8_t pkt_size, uint16_t event) -{ - tBTUI_SCO_CODEC_CFG cfg; - - if (btui_cb.sco_hci) - { - BTIF_TRACE_DEBUG("bta_dm_sco_co_open handle:%d pkt_size:%d", handle, pkt_size); - cfg.p_cback = btui_sco_codec_callback; - cfg.pkt_size = pkt_size; - cfg.cb_event = event; - /* open and start the codec */ - btui_sco_codec_open(&cfg); - btui_sco_codec_start(handle); - } + * + * Function bta_dm_sco_co_open + * + * Description This function is executed when a SCO connection is open. + * + * + * Returns void + * + ******************************************************************************/ +void bta_dm_sco_co_open(uint16_t handle, uint8_t pkt_size, uint16_t event) { + tBTUI_SCO_CODEC_CFG cfg; + + if (btui_cb.sco_hci) { + BTIF_TRACE_DEBUG("bta_dm_sco_co_open handle:%d pkt_size:%d", handle, + pkt_size); + cfg.p_cback = btui_sco_codec_callback; + cfg.pkt_size = pkt_size; + cfg.cb_event = event; + /* open and start the codec */ + btui_sco_codec_open(&cfg); + btui_sco_codec_start(handle); + } } /******************************************************************************* -** -** Function bta_dm_sco_co_close -** -** Description This function is called when a SCO connection is closed -** -** -** Returns void -** -*******************************************************************************/ -void bta_dm_sco_co_close(void) -{ - if (btui_cb.sco_hci) - { - BTIF_TRACE_DEBUG("bta_dm_sco_co_close close codec"); - /* close sco codec */ - btui_sco_codec_close(); - - btui_cb.sco_hci = false; - } + * + * Function bta_dm_sco_co_close + * + * Description This function is called when a SCO connection is closed + * + * + * Returns void + * + ******************************************************************************/ +void bta_dm_sco_co_close(void) { + if (btui_cb.sco_hci) { + BTIF_TRACE_DEBUG("bta_dm_sco_co_close close codec"); + /* close sco codec */ + btui_sco_codec_close(); + + btui_cb.sco_hci = false; + } } /******************************************************************************* -** -** Function bta_dm_sco_co_in_data -** -** Description This function is called to send incoming SCO data to application. -** -** Returns void -** -*******************************************************************************/ -void bta_dm_sco_co_in_data(BT_HDR *p_buf) -{ - if (btui_cfg.sco_use_mic) - btui_sco_codec_inqdata(p_buf); - else - osi_free(p_buf); + * + * Function bta_dm_sco_co_in_data + * + * Description This function is called to send incoming SCO data to + *application. + * + * Returns void + * + ******************************************************************************/ +void bta_dm_sco_co_in_data(BT_HDR* p_buf) { + if (btui_cfg.sco_use_mic) + btui_sco_codec_inqdata(p_buf); + else + osi_free(p_buf); } /******************************************************************************* -** -** Function bta_dm_sco_co_out_data -** -** Description This function is called to send SCO data over HCI. -** -** Returns void -** -*******************************************************************************/ -void bta_dm_sco_co_out_data(BT_HDR **p_buf) -{ - btui_sco_codec_readbuf(p_buf); -} + * + * Function bta_dm_sco_co_out_data + * + * Description This function is called to send SCO data over HCI. + * + * Returns void + * + ******************************************************************************/ +void bta_dm_sco_co_out_data(BT_HDR** p_buf) { btui_sco_codec_readbuf(p_buf); } #endif /* (BTM_SCO_HCI_INCLUDED == TRUE) && (BTM_SCO_INCLUDED == TRUE)*/ - #if (BLE_INCLUDED == TRUE) /******************************************************************************* -** -** Function bta_dm_co_le_io_key_req -** -** Description This callout function is executed by DM to get BLE key information -** before SMP pairing gets going. -** -** Parameters bd_addr - The peer device -** *p_max_key_size - max key size local device supported. -** *p_init_key - initiator keys. -** *p_resp_key - responder keys. -** -** Returns void. -** -*******************************************************************************/ -void bta_dm_co_le_io_key_req(BD_ADDR bd_addr, uint8_t *p_max_key_size, - tBTA_LE_KEY_TYPE *p_init_key, - tBTA_LE_KEY_TYPE *p_resp_key ) -{ - UNUSED(bd_addr); - BTIF_TRACE_ERROR("##################################"); - BTIF_TRACE_ERROR("bta_dm_co_le_io_key_req: only setting max size to 16"); - BTIF_TRACE_ERROR("##################################"); - *p_max_key_size = 16; - *p_init_key = *p_resp_key = - (BTA_LE_KEY_PENC|BTA_LE_KEY_PID|BTA_LE_KEY_PCSRK|BTA_LE_KEY_LENC|BTA_LE_KEY_LID|BTA_LE_KEY_LCSRK); + * + * Function bta_dm_co_le_io_key_req + * + * Description This callout function is executed by DM to get BLE key + *information + * before SMP pairing gets going. + * + * Parameters bd_addr - The peer device + * *p_max_key_size - max key size local device supported. + * *p_init_key - initiator keys. + * *p_resp_key - responder keys. + * + * Returns void. + * + ******************************************************************************/ +void bta_dm_co_le_io_key_req(BD_ADDR bd_addr, uint8_t* p_max_key_size, + tBTA_LE_KEY_TYPE* p_init_key, + tBTA_LE_KEY_TYPE* p_resp_key) { + UNUSED(bd_addr); + BTIF_TRACE_ERROR("##################################"); + BTIF_TRACE_ERROR("bta_dm_co_le_io_key_req: only setting max size to 16"); + BTIF_TRACE_ERROR("##################################"); + *p_max_key_size = 16; + *p_init_key = *p_resp_key = + (BTA_LE_KEY_PENC | BTA_LE_KEY_PID | BTA_LE_KEY_PCSRK | BTA_LE_KEY_LENC | + BTA_LE_KEY_LID | BTA_LE_KEY_LCSRK); } - /******************************************************************************* -** -** Function bta_dm_co_ble_local_key_reload -** -** Description This callout function is to load the local BLE keys if available -** on the device. -** -** Parameters none -** -** Returns void. -** -*******************************************************************************/ -void bta_dm_co_ble_load_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er, - tBTA_BLE_LOCAL_ID_KEYS *p_id_keys) -{ - BTIF_TRACE_DEBUG("##################################"); - BTIF_TRACE_DEBUG("bta_dm_co_ble_load_local_keys: Load local keys if any are persisted"); - BTIF_TRACE_DEBUG("##################################"); - btif_dm_get_ble_local_keys( p_key_mask, er, p_id_keys); + * + * Function bta_dm_co_ble_local_key_reload + * + * Description This callout function is to load the local BLE keys if + *available + * on the device. + * + * Parameters none + * + * Returns void. + * + ******************************************************************************/ +void bta_dm_co_ble_load_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK* p_key_mask, + BT_OCTET16 er, + tBTA_BLE_LOCAL_ID_KEYS* p_id_keys) { + BTIF_TRACE_DEBUG("##################################"); + BTIF_TRACE_DEBUG( + "bta_dm_co_ble_load_local_keys: Load local keys if any are persisted"); + BTIF_TRACE_DEBUG("##################################"); + btif_dm_get_ble_local_keys(p_key_mask, er, p_id_keys); } /******************************************************************************* -** -** Function bta_dm_co_ble_io_req -** -** Description This callout function is executed by DM to get BLE IO capabilities -** before SMP pairing gets going. -** -** Parameters bd_addr - The peer device -** *p_io_cap - The local Input/Output capabilities -** *p_oob_data - true, if OOB data is available for the peer device. -** *p_auth_req - Auth request setting (Bonding and MITM required or not) -** *p_max_key_size - max key size local device supported. -** *p_init_key - initiator keys. -** *p_resp_key - responder keys. -** -** Returns void. -** -*******************************************************************************/ -void bta_dm_co_ble_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, - tBTA_OOB_DATA *p_oob_data, - tBTA_LE_AUTH_REQ *p_auth_req, - uint8_t *p_max_key_size, - tBTA_LE_KEY_TYPE *p_init_key, - tBTA_LE_KEY_TYPE *p_resp_key ) -{ - UNUSED(bd_addr); - /* Retrieve the properties from file system if possible */ - tBTE_APPL_CFG nv_config; - if(btif_dm_get_smp_config(&nv_config)) - bte_appl_cfg = nv_config; - - /* *p_auth_req by default is false for devices with NoInputNoOutput; true for other devices. */ - - if (bte_appl_cfg.ble_auth_req) - *p_auth_req = bte_appl_cfg.ble_auth_req | (bte_appl_cfg.ble_auth_req & 0x04) | ((*p_auth_req) & 0x04); - - /* if OOB is not supported, this call-out function does not need to do anything - * otherwise, look for the OOB data associated with the address and set *p_oob_data accordingly. - * If the answer can not be obtained right away, - * set *p_oob_data to BTA_OOB_UNKNOWN and call bta_dm_ci_io_req() when the answer is available. - */ - - btif_dm_set_oob_for_le_io_req(bd_addr, p_oob_data, p_auth_req); - - if (bte_appl_cfg.ble_io_cap <=4) - *p_io_cap = bte_appl_cfg.ble_io_cap; - - if (bte_appl_cfg.ble_init_key <= BTM_BLE_INITIATOR_KEY_SIZE) - *p_init_key = bte_appl_cfg.ble_init_key; - - if (bte_appl_cfg.ble_resp_key <= BTM_BLE_RESPONDER_KEY_SIZE) - *p_resp_key = bte_appl_cfg.ble_resp_key; - - if (bte_appl_cfg.ble_max_key_size > 7 && bte_appl_cfg.ble_max_key_size <= 16) - *p_max_key_size = bte_appl_cfg.ble_max_key_size; + * + * Function bta_dm_co_ble_io_req + * + * Description This callout function is executed by DM to get BLE IO + *capabilities + * before SMP pairing gets going. + * + * Parameters bd_addr - The peer device + * *p_io_cap - The local Input/Output capabilities + * *p_oob_data - true, if OOB data is available for the peer + *device. + * *p_auth_req - Auth request setting (Bonding and MITM + *required or not) + * *p_max_key_size - max key size local device supported. + * *p_init_key - initiator keys. + * *p_resp_key - responder keys. + * + * Returns void. + * + ******************************************************************************/ +void bta_dm_co_ble_io_req(BD_ADDR bd_addr, tBTA_IO_CAP* p_io_cap, + tBTA_OOB_DATA* p_oob_data, + tBTA_LE_AUTH_REQ* p_auth_req, uint8_t* p_max_key_size, + tBTA_LE_KEY_TYPE* p_init_key, + tBTA_LE_KEY_TYPE* p_resp_key) { + UNUSED(bd_addr); + /* Retrieve the properties from file system if possible */ + tBTE_APPL_CFG nv_config; + if (btif_dm_get_smp_config(&nv_config)) bte_appl_cfg = nv_config; + + /* *p_auth_req by default is false for devices with NoInputNoOutput; true for + * other devices. */ + + if (bte_appl_cfg.ble_auth_req) + *p_auth_req = bte_appl_cfg.ble_auth_req | + (bte_appl_cfg.ble_auth_req & 0x04) | ((*p_auth_req) & 0x04); + + /* if OOB is not supported, this call-out function does not need to do + * anything + * otherwise, look for the OOB data associated with the address and set + * *p_oob_data accordingly. + * If the answer can not be obtained right away, + * set *p_oob_data to BTA_OOB_UNKNOWN and call bta_dm_ci_io_req() when the + * answer is available. + */ + + btif_dm_set_oob_for_le_io_req(bd_addr, p_oob_data, p_auth_req); + + if (bte_appl_cfg.ble_io_cap <= 4) *p_io_cap = bte_appl_cfg.ble_io_cap; + + if (bte_appl_cfg.ble_init_key <= BTM_BLE_INITIATOR_KEY_SIZE) + *p_init_key = bte_appl_cfg.ble_init_key; + + if (bte_appl_cfg.ble_resp_key <= BTM_BLE_RESPONDER_KEY_SIZE) + *p_resp_key = bte_appl_cfg.ble_resp_key; + + if (bte_appl_cfg.ble_max_key_size > 7 && bte_appl_cfg.ble_max_key_size <= 16) + *p_max_key_size = bte_appl_cfg.ble_max_key_size; } - #endif - diff --git a/btif/co/bta_gatts_co.cc b/btif/co/bta_gatts_co.cc index b5d9dd5b1..77569caf2 100644 --- a/btif/co/bta_gatts_co.cc +++ b/btif/co/bta_gatts_co.cc @@ -27,141 +27,135 @@ #include "btif_util.h" /***************************************************************************** -** Local type definitions -*****************************************************************************/ + * Local type definitions + ****************************************************************************/ #define BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE 50 -typedef struct -{ - bool enable; - uint8_t num_clients; - tBTA_GATTS_SRV_CHG srv_chg[BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE]; +typedef struct { + bool enable; + uint8_t num_clients; + tBTA_GATTS_SRV_CHG srv_chg[BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE]; } __attribute__((packed)) btif_gatts_srv_chg_cb_t; /***************************************************************************** -** Static variables -*****************************************************************************/ + * Static variables + ****************************************************************************/ static btif_gatts_srv_chg_cb_t btif_gatts_srv_chg_cb; /***************************************************************************** -** Static functions -*****************************************************************************/ + * Static functions + ****************************************************************************/ -static void btif_gatts_check_init(void) -{ - btif_gatts_srv_chg_cb_t *p_cb= &btif_gatts_srv_chg_cb; +static void btif_gatts_check_init(void) { + btif_gatts_srv_chg_cb_t* p_cb = &btif_gatts_srv_chg_cb; - if (!p_cb->enable) - { - memset(p_cb, 0, sizeof(btif_gatts_srv_chg_cb_t)); - p_cb->enable = true; - } + if (!p_cb->enable) { + memset(p_cb, 0, sizeof(btif_gatts_srv_chg_cb_t)); + p_cb->enable = true; + } } /***************************************************************************** -** Externally called functions -*****************************************************************************/ - -void btif_gatts_add_bonded_dev_from_nv(BD_ADDR bda) -{ - btif_gatts_srv_chg_cb_t *p_cb= &btif_gatts_srv_chg_cb; - bool found = false; - uint8_t i; - - btif_gatts_check_init(); - - for (i=0; i != p_cb->num_clients; ++i) - { - if (!memcmp(p_cb->srv_chg[i].bda, bda, sizeof(BD_ADDR))) - { - found = true; - break; - } + * Externally called functions + ****************************************************************************/ + +void btif_gatts_add_bonded_dev_from_nv(BD_ADDR bda) { + btif_gatts_srv_chg_cb_t* p_cb = &btif_gatts_srv_chg_cb; + bool found = false; + uint8_t i; + + btif_gatts_check_init(); + + for (i = 0; i != p_cb->num_clients; ++i) { + if (!memcmp(p_cb->srv_chg[i].bda, bda, sizeof(BD_ADDR))) { + found = true; + break; } + } - if (!found) - { - if (p_cb->num_clients < BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE) - { - bdcpy(p_cb->srv_chg[p_cb->num_clients].bda, bda); - p_cb->srv_chg[p_cb->num_clients].srv_changed = false; - p_cb->num_clients++; - } + if (!found) { + if (p_cb->num_clients < BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE) { + bdcpy(p_cb->srv_chg[p_cb->num_clients].bda, bda); + p_cb->srv_chg[p_cb->num_clients].srv_changed = false; + p_cb->num_clients++; } + } } /***************************************************************************** -** Call-out functions -*****************************************************************************/ + * Call-out functions + ****************************************************************************/ /******************************************************************************* -** -** Function bta_gatts_co_update_handle_range -** -** Description This callout function is executed by GATTS when a GATT server -** handle range ios to be added or removed. -** -** Parameter is_add: true is to add a handle range; otherwise is to delete. -** p_hndl_range: handle range. -** -** Returns void. -** -*******************************************************************************/ -void bta_gatts_co_update_handle_range(bool is_add, tBTA_GATTS_HNDL_RANGE *p_hndl_range) -{ - UNUSED(is_add); - UNUSED(p_hndl_range); + * + * Function bta_gatts_co_update_handle_range + * + * Description This callout function is executed by GATTS when a GATT + *server + * handle range ios to be added or removed. + * + * Parameter is_add: true is to add a handle range; otherwise is to + *delete. + * p_hndl_range: handle range. + * + * Returns void. + * + ******************************************************************************/ +void bta_gatts_co_update_handle_range(bool is_add, + tBTA_GATTS_HNDL_RANGE* p_hndl_range) { + UNUSED(is_add); + UNUSED(p_hndl_range); } /******************************************************************************* -** -** Function bta_gatts_co_srv_chg -** -** Description This call-out is to read/write/remove service change related -** informaiton. The request consists of the cmd and p_req and the -** response is returned in p_rsp -** -** Parameter cmd - request command -** p_req - request paramters -** p_rsp - response data for the request -** -** Returns true - if the request is processed successfully and -** the response is returned in p_rsp. -** false - if the request can not be processed -** -*******************************************************************************/ + * + * Function bta_gatts_co_srv_chg + * + * Description This call-out is to read/write/remove service change related + * informaiton. The request consists of the cmd and p_req and + *the + * response is returned in p_rsp + * + * Parameter cmd - request command + * p_req - request paramters + * p_rsp - response data for the request + * + * Returns true - if the request is processed successfully and + * the response is returned in p_rsp. + * false - if the request can not be processed + * + ******************************************************************************/ bool bta_gatts_co_srv_chg(tBTA_GATTS_SRV_CHG_CMD cmd, - tBTA_GATTS_SRV_CHG_REQ *p_req, - tBTA_GATTS_SRV_CHG_RSP *p_rsp) -{ - UNUSED(cmd); - UNUSED(p_req); - UNUSED(p_rsp); - - return false; + tBTA_GATTS_SRV_CHG_REQ* p_req, + tBTA_GATTS_SRV_CHG_RSP* p_rsp) { + UNUSED(cmd); + UNUSED(p_req); + UNUSED(p_rsp); + + return false; } /******************************************************************************* -** -** Function bta_gatts_co_load_handle_range -** -** Description This callout function is executed by GATTS when a GATT server -** handle range is requested to be loaded from NV. -** -** Parameter -** -** Returns void. -** -*******************************************************************************/ + * + * Function bta_gatts_co_load_handle_range + * + * Description This callout function is executed by GATTS when a GATT + *server + * handle range is requested to be loaded from NV. + * + * Parameter + * + * Returns void. + * + ******************************************************************************/ bool bta_gatts_co_load_handle_range(uint8_t index, - tBTA_GATTS_HNDL_RANGE *p_handle_range) -{ - UNUSED(index); - UNUSED(p_handle_range); + tBTA_GATTS_HNDL_RANGE* p_handle_range) { + UNUSED(index); + UNUSED(p_handle_range); - return false; + return false; } -#endif // BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE +#endif // BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE diff --git a/btif/co/bta_hh_co.cc b/btif/co/bta_hh_co.cc index a9dc4b146..74cf1b320 100644 --- a/btif/co/bta_hh_co.cc +++ b/btif/co/bta_hh_co.cc @@ -22,594 +22,592 @@ #include #include #include +#include #include #include -#include #include #include -#include "btcore/include/bdaddr.h" -#include "osi/include/osi.h" #include "bta_api.h" #include "bta_hh_api.h" #include "bta_hh_co.h" +#include "btcore/include/bdaddr.h" #include "btif_hh.h" #include "btif_util.h" +#include "osi/include/osi.h" -const char *dev_path = "/dev/uhid"; +const char* dev_path = "/dev/uhid"; #if (BLE_INCLUDED == TRUE && BTA_HH_LE_INCLUDED == TRUE) #include "btif_config.h" -#define BTA_HH_NV_LOAD_MAX 16 +#define BTA_HH_NV_LOAD_MAX 16 static tBTA_HH_RPT_CACHE_ENTRY sReportCache[BTA_HH_NV_LOAD_MAX]; #endif -void uhid_set_non_blocking(int fd) -{ - int opts = fcntl(fd, F_GETFL); - if (opts < 0) - APPL_TRACE_ERROR("%s() Getting flags failed (%s)", __func__, strerror(errno)); +void uhid_set_non_blocking(int fd) { + int opts = fcntl(fd, F_GETFL); + if (opts < 0) + APPL_TRACE_ERROR("%s() Getting flags failed (%s)", __func__, + strerror(errno)); - opts |= O_NONBLOCK; + opts |= O_NONBLOCK; - if (fcntl(fd, F_SETFL, opts) < 0) - APPL_TRACE_EVENT("%s() Setting non-blocking flag failed (%s)", __func__, strerror(errno)); + if (fcntl(fd, F_SETFL, opts) < 0) + APPL_TRACE_EVENT("%s() Setting non-blocking flag failed (%s)", __func__, + strerror(errno)); } /*Internal function to perform UHID write and error checking*/ -static int uhid_write(int fd, const struct uhid_event *ev) -{ - ssize_t ret; - OSI_NO_INTR(ret = write(fd, ev, sizeof(*ev))); - - if (ret < 0){ - int rtn = -errno; - APPL_TRACE_ERROR("%s: Cannot write to uhid:%s", - __func__, strerror(errno)); - return rtn; - } else if (ret != (ssize_t)sizeof(*ev)) { - APPL_TRACE_ERROR("%s: Wrong size written to uhid: %zd != %zu", - __func__, ret, sizeof(*ev)); - return -EFAULT; - } - - return 0; +static int uhid_write(int fd, const struct uhid_event* ev) { + ssize_t ret; + OSI_NO_INTR(ret = write(fd, ev, sizeof(*ev))); + + if (ret < 0) { + int rtn = -errno; + APPL_TRACE_ERROR("%s: Cannot write to uhid:%s", __func__, strerror(errno)); + return rtn; + } else if (ret != (ssize_t)sizeof(*ev)) { + APPL_TRACE_ERROR("%s: Wrong size written to uhid: %zd != %zu", __func__, + ret, sizeof(*ev)); + return -EFAULT; + } + + return 0; } /* Internal function to parse the events received from UHID driver*/ -static int uhid_read_event(btif_hh_device_t *p_dev) -{ - assert(p_dev); +static int uhid_read_event(btif_hh_device_t* p_dev) { + assert(p_dev); - struct uhid_event ev; - memset(&ev, 0, sizeof(ev)); + struct uhid_event ev; + memset(&ev, 0, sizeof(ev)); - ssize_t ret; - OSI_NO_INTR(ret = read(p_dev->fd, &ev, sizeof(ev))); + ssize_t ret; + OSI_NO_INTR(ret = read(p_dev->fd, &ev, sizeof(ev))); - if (ret == 0) { - APPL_TRACE_ERROR("%s: Read HUP on uhid-cdev %s", __func__, - strerror(errno)); - return -EFAULT; - } else if (ret < 0) { - APPL_TRACE_ERROR("%s: Cannot read uhid-cdev: %s", __func__, - strerror(errno)); - return -errno; - } + if (ret == 0) { + APPL_TRACE_ERROR("%s: Read HUP on uhid-cdev %s", __func__, strerror(errno)); + return -EFAULT; + } else if (ret < 0) { + APPL_TRACE_ERROR("%s: Cannot read uhid-cdev: %s", __func__, + strerror(errno)); + return -errno; + } - switch (ev.type) { + switch (ev.type) { case UHID_START: - APPL_TRACE_DEBUG("UHID_START from uhid-dev\n"); - p_dev->ready_for_data = true; - break; + APPL_TRACE_DEBUG("UHID_START from uhid-dev\n"); + p_dev->ready_for_data = true; + break; case UHID_STOP: - APPL_TRACE_DEBUG("UHID_STOP from uhid-dev\n"); - p_dev->ready_for_data = false; - break; + APPL_TRACE_DEBUG("UHID_STOP from uhid-dev\n"); + p_dev->ready_for_data = false; + break; case UHID_OPEN: - APPL_TRACE_DEBUG("UHID_OPEN from uhid-dev\n"); - p_dev->ready_for_data = true; - break; + APPL_TRACE_DEBUG("UHID_OPEN from uhid-dev\n"); + p_dev->ready_for_data = true; + break; case UHID_CLOSE: - APPL_TRACE_DEBUG("UHID_CLOSE from uhid-dev\n"); - p_dev->ready_for_data = false; - break; + APPL_TRACE_DEBUG("UHID_CLOSE from uhid-dev\n"); + p_dev->ready_for_data = false; + break; case UHID_OUTPUT: - if (ret < (ssize_t)(sizeof(ev.type) + sizeof(ev.u.output))) { - APPL_TRACE_ERROR("%s: Invalid size read from uhid-dev: %zd < %zu", - __func__, ret, - sizeof(ev.type) + sizeof(ev.u.output)); - return -EFAULT; - } - - APPL_TRACE_DEBUG("UHID_OUTPUT: Report type = %d, report_size = %d" - ,ev.u.output.rtype, ev.u.output.size); - //Send SET_REPORT with feature report if the report type in output event is FEATURE - if(ev.u.output.rtype == UHID_FEATURE_REPORT) - btif_hh_setreport(p_dev, BTHH_FEATURE_REPORT, - ev.u.output.size, ev.u.output.data); - else if(ev.u.output.rtype == UHID_OUTPUT_REPORT) - btif_hh_setreport(p_dev, BTHH_OUTPUT_REPORT, - ev.u.output.size, ev.u.output.data); - else - btif_hh_setreport(p_dev, BTHH_INPUT_REPORT, - ev.u.output.size, ev.u.output.data); - break; + if (ret < (ssize_t)(sizeof(ev.type) + sizeof(ev.u.output))) { + APPL_TRACE_ERROR("%s: Invalid size read from uhid-dev: %zd < %zu", + __func__, ret, sizeof(ev.type) + sizeof(ev.u.output)); + return -EFAULT; + } + + APPL_TRACE_DEBUG("UHID_OUTPUT: Report type = %d, report_size = %d", + ev.u.output.rtype, ev.u.output.size); + // Send SET_REPORT with feature report if the report type in output event + // is FEATURE + if (ev.u.output.rtype == UHID_FEATURE_REPORT) + btif_hh_setreport(p_dev, BTHH_FEATURE_REPORT, ev.u.output.size, + ev.u.output.data); + else if (ev.u.output.rtype == UHID_OUTPUT_REPORT) + btif_hh_setreport(p_dev, BTHH_OUTPUT_REPORT, ev.u.output.size, + ev.u.output.data); + else + btif_hh_setreport(p_dev, BTHH_INPUT_REPORT, ev.u.output.size, + ev.u.output.data); + break; case UHID_OUTPUT_EV: - if (ret < (ssize_t)(sizeof(ev.type) + sizeof(ev.u.output_ev))) { - APPL_TRACE_ERROR("%s: Invalid size read from uhid-dev: %zd < %zu", - __func__, ret, sizeof(ev.type) + sizeof(ev.u.output_ev)); - return -EFAULT; - } - APPL_TRACE_DEBUG("UHID_OUTPUT_EV from uhid-dev\n"); - break; + if (ret < (ssize_t)(sizeof(ev.type) + sizeof(ev.u.output_ev))) { + APPL_TRACE_ERROR("%s: Invalid size read from uhid-dev: %zd < %zu", + __func__, ret, + sizeof(ev.type) + sizeof(ev.u.output_ev)); + return -EFAULT; + } + APPL_TRACE_DEBUG("UHID_OUTPUT_EV from uhid-dev\n"); + break; case UHID_FEATURE: - APPL_TRACE_DEBUG("UHID_FEATURE from uhid-dev\n"); - break; + APPL_TRACE_DEBUG("UHID_FEATURE from uhid-dev\n"); + break; case UHID_FEATURE_ANSWER: - APPL_TRACE_DEBUG("UHID_FEATURE_ANSWER from uhid-dev\n"); - break; + APPL_TRACE_DEBUG("UHID_FEATURE_ANSWER from uhid-dev\n"); + break; default: - APPL_TRACE_DEBUG("Invalid event from uhid-dev: %u\n", ev.type); - } + APPL_TRACE_DEBUG("Invalid event from uhid-dev: %u\n", ev.type); + } - return 0; + return 0; } /******************************************************************************* -** -** Function create_thread -** -** Description creat a select loop -** -** Returns pthread_t -** -*******************************************************************************/ -static inline pthread_t create_thread(void *(*start_routine)(void *), void * arg){ - APPL_TRACE_DEBUG("create_thread: entered"); - pthread_attr_t thread_attr; - - pthread_attr_init(&thread_attr); - pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE); - pthread_t thread_id = -1; - if ( pthread_create(&thread_id, &thread_attr, start_routine, arg)!=0 ) - { - APPL_TRACE_ERROR("pthread_create : %s", strerror(errno)); - return -1; - } - APPL_TRACE_DEBUG("create_thread: thread created successfully"); - return thread_id; + * + * Function create_thread + * + * Description creat a select loop + * + * Returns pthread_t + * + ******************************************************************************/ +static inline pthread_t create_thread(void* (*start_routine)(void*), + void* arg) { + APPL_TRACE_DEBUG("create_thread: entered"); + pthread_attr_t thread_attr; + + pthread_attr_init(&thread_attr); + pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE); + pthread_t thread_id = -1; + if (pthread_create(&thread_id, &thread_attr, start_routine, arg) != 0) { + APPL_TRACE_ERROR("pthread_create : %s", strerror(errno)); + return -1; + } + APPL_TRACE_DEBUG("create_thread: thread created successfully"); + return thread_id; } /******************************************************************************* -** -** Function btif_hh_poll_event_thread -** -** Description the polling thread which polls for event from UHID driver -** -** Returns void -** -*******************************************************************************/ -static void *btif_hh_poll_event_thread(void *arg) -{ - btif_hh_device_t *p_dev = (btif_hh_device_t*)arg; - APPL_TRACE_DEBUG("%s: Thread created fd = %d", __func__, p_dev->fd); - struct pollfd pfds[1]; - - pfds[0].fd = p_dev->fd; - pfds[0].events = POLLIN; - - // Set the uhid fd as non-blocking to ensure we never block the BTU thread - uhid_set_non_blocking(p_dev->fd); - - while(p_dev->hh_keep_polling){ - int ret; - OSI_NO_INTR(ret = poll(pfds, 1, 50)); - if (ret < 0) { - APPL_TRACE_ERROR("%s: Cannot poll for fds: %s\n", __func__, strerror(errno)); - break; - } - if (pfds[0].revents & POLLIN) { - APPL_TRACE_DEBUG("%s: POLLIN", __func__); - ret = uhid_read_event(p_dev); - if (ret != 0) - break; - } + * + * Function btif_hh_poll_event_thread + * + * Description the polling thread which polls for event from UHID driver + * + * Returns void + * + ******************************************************************************/ +static void* btif_hh_poll_event_thread(void* arg) { + btif_hh_device_t* p_dev = (btif_hh_device_t*)arg; + APPL_TRACE_DEBUG("%s: Thread created fd = %d", __func__, p_dev->fd); + struct pollfd pfds[1]; + + pfds[0].fd = p_dev->fd; + pfds[0].events = POLLIN; + + // Set the uhid fd as non-blocking to ensure we never block the BTU thread + uhid_set_non_blocking(p_dev->fd); + + while (p_dev->hh_keep_polling) { + int ret; + OSI_NO_INTR(ret = poll(pfds, 1, 50)); + if (ret < 0) { + APPL_TRACE_ERROR("%s: Cannot poll for fds: %s\n", __func__, + strerror(errno)); + break; + } + if (pfds[0].revents & POLLIN) { + APPL_TRACE_DEBUG("%s: POLLIN", __func__); + ret = uhid_read_event(p_dev); + if (ret != 0) break; } + } - p_dev->hh_poll_thread_id = -1; - return 0; + p_dev->hh_poll_thread_id = -1; + return 0; } -static inline void btif_hh_close_poll_thread(btif_hh_device_t *p_dev) -{ - APPL_TRACE_DEBUG("%s", __func__); - p_dev->hh_keep_polling = 0; - if(p_dev->hh_poll_thread_id > 0) - pthread_join(p_dev->hh_poll_thread_id,NULL); +static inline void btif_hh_close_poll_thread(btif_hh_device_t* p_dev) { + APPL_TRACE_DEBUG("%s", __func__); + p_dev->hh_keep_polling = 0; + if (p_dev->hh_poll_thread_id > 0) + pthread_join(p_dev->hh_poll_thread_id, NULL); - return; + return; } -void bta_hh_co_destroy(int fd) -{ - struct uhid_event ev; - memset(&ev, 0, sizeof(ev)); - ev.type = UHID_DESTROY; - uhid_write(fd, &ev); - APPL_TRACE_DEBUG("%s: Closing fd=%d", __func__, fd); - close(fd); +void bta_hh_co_destroy(int fd) { + struct uhid_event ev; + memset(&ev, 0, sizeof(ev)); + ev.type = UHID_DESTROY; + uhid_write(fd, &ev); + APPL_TRACE_DEBUG("%s: Closing fd=%d", __func__, fd); + close(fd); } -int bta_hh_co_write(int fd, uint8_t* rpt, uint16_t len) -{ - APPL_TRACE_DEBUG("%s: UHID write %d", __func__, len); - - struct uhid_event ev; - memset(&ev, 0, sizeof(ev)); - ev.type = UHID_INPUT; - ev.u.input.size = len; - if(len > sizeof(ev.u.input.data)){ - APPL_TRACE_WARNING("%s: Report size greater than allowed size", - __func__); - return -1; - } - memcpy(ev.u.input.data, rpt, len); +int bta_hh_co_write(int fd, uint8_t* rpt, uint16_t len) { + APPL_TRACE_DEBUG("%s: UHID write %d", __func__, len); - return uhid_write(fd, &ev); + struct uhid_event ev; + memset(&ev, 0, sizeof(ev)); + ev.type = UHID_INPUT; + ev.u.input.size = len; + if (len > sizeof(ev.u.input.data)) { + APPL_TRACE_WARNING("%s: Report size greater than allowed size", __func__); + return -1; + } + memcpy(ev.u.input.data, rpt, len); + return uhid_write(fd, &ev); } - /******************************************************************************* -** -** Function bta_hh_co_open -** -** Description When connection is opened, this call-out function is executed -** by HH to do platform specific initialization. -** -** Returns void. -*******************************************************************************/ -void bta_hh_co_open(uint8_t dev_handle, uint8_t sub_class, tBTA_HH_ATTR_MASK attr_mask, - uint8_t app_id) -{ - uint32_t i; - btif_hh_device_t *p_dev = NULL; - - if (dev_handle == BTA_HH_INVALID_HANDLE) { - APPL_TRACE_WARNING("%s: Oops, dev_handle (%d) is invalid...", - __func__, dev_handle); - return; + * + * Function bta_hh_co_open + * + * Description When connection is opened, this call-out function is executed + * by HH to do platform specific initialization. + * + * Returns void. + ******************************************************************************/ +void bta_hh_co_open(uint8_t dev_handle, uint8_t sub_class, + tBTA_HH_ATTR_MASK attr_mask, uint8_t app_id) { + uint32_t i; + btif_hh_device_t* p_dev = NULL; + + if (dev_handle == BTA_HH_INVALID_HANDLE) { + APPL_TRACE_WARNING("%s: Oops, dev_handle (%d) is invalid...", __func__, + dev_handle); + return; + } + + for (i = 0; i < BTIF_HH_MAX_HID; i++) { + p_dev = &btif_hh_cb.devices[i]; + if (p_dev->dev_status != BTHH_CONN_STATE_UNKNOWN && + p_dev->dev_handle == dev_handle) { + // We found a device with the same handle. Must be a device reconnected. + APPL_TRACE_WARNING( + "%s: Found an existing device with the same handle " + "dev_status = %d", + __func__, p_dev->dev_status); + APPL_TRACE_WARNING("%s: bd_addr = [%02X:%02X:%02X:%02X:%02X:]", + __func__, p_dev->bd_addr.address[0], + p_dev->bd_addr.address[1], p_dev->bd_addr.address[2], + p_dev->bd_addr.address[3], p_dev->bd_addr.address[4]); + APPL_TRACE_WARNING( + "%s: attr_mask = 0x%04x, sub_class = 0x%02x, app_id = %d", + __func__, p_dev->attr_mask, p_dev->sub_class, p_dev->app_id); + + if (p_dev->fd < 0) { + p_dev->fd = open(dev_path, O_RDWR | O_CLOEXEC); + if (p_dev->fd < 0) { + APPL_TRACE_ERROR("%s: Error: failed to open uhid, err:%s", __func__, + strerror(errno)); + return; + } else + APPL_TRACE_DEBUG("%s: uhid fd = %d", __func__, p_dev->fd); + } + + p_dev->hh_keep_polling = 1; + p_dev->hh_poll_thread_id = + create_thread(btif_hh_poll_event_thread, p_dev); + break; } + p_dev = NULL; + } + if (p_dev == NULL) { + // Did not find a device reconnection case. Find an empty slot now. for (i = 0; i < BTIF_HH_MAX_HID; i++) { + if (btif_hh_cb.devices[i].dev_status == BTHH_CONN_STATE_UNKNOWN) { p_dev = &btif_hh_cb.devices[i]; - if (p_dev->dev_status != BTHH_CONN_STATE_UNKNOWN && - p_dev->dev_handle == dev_handle) { - // We found a device with the same handle. Must be a device reconnected. - APPL_TRACE_WARNING("%s: Found an existing device with the same handle " - "dev_status = %d",__func__, - p_dev->dev_status); - APPL_TRACE_WARNING("%s: bd_addr = [%02X:%02X:%02X:%02X:%02X:]", __func__, - p_dev->bd_addr.address[0], p_dev->bd_addr.address[1], p_dev->bd_addr.address[2], - p_dev->bd_addr.address[3], p_dev->bd_addr.address[4]); - APPL_TRACE_WARNING("%s: attr_mask = 0x%04x, sub_class = 0x%02x, app_id = %d", - __func__, p_dev->attr_mask, p_dev->sub_class, p_dev->app_id); - - if(p_dev->fd<0) { - p_dev->fd = open(dev_path, O_RDWR | O_CLOEXEC); - if (p_dev->fd < 0){ - APPL_TRACE_ERROR("%s: Error: failed to open uhid, err:%s", - __func__,strerror(errno)); - return; - }else - APPL_TRACE_DEBUG("%s: uhid fd = %d", __func__, p_dev->fd); - } - - p_dev->hh_keep_polling = 1; - p_dev->hh_poll_thread_id = create_thread(btif_hh_poll_event_thread, p_dev); - break; + p_dev->dev_handle = dev_handle; + p_dev->attr_mask = attr_mask; + p_dev->sub_class = sub_class; + p_dev->app_id = app_id; + p_dev->local_vup = false; + + btif_hh_cb.device_num++; + // This is a new device,open the uhid driver now. + p_dev->fd = open(dev_path, O_RDWR | O_CLOEXEC); + if (p_dev->fd < 0) { + APPL_TRACE_ERROR("%s: Error: failed to open uhid, err:%s", __func__, + strerror(errno)); + return; + } else { + APPL_TRACE_DEBUG("%s: uhid fd = %d", __func__, p_dev->fd); + p_dev->hh_keep_polling = 1; + p_dev->hh_poll_thread_id = + create_thread(btif_hh_poll_event_thread, p_dev); } - p_dev = NULL; - } - if (p_dev == NULL) { - // Did not find a device reconnection case. Find an empty slot now. - for (i = 0; i < BTIF_HH_MAX_HID; i++) { - if (btif_hh_cb.devices[i].dev_status == BTHH_CONN_STATE_UNKNOWN) { - p_dev = &btif_hh_cb.devices[i]; - p_dev->dev_handle = dev_handle; - p_dev->attr_mask = attr_mask; - p_dev->sub_class = sub_class; - p_dev->app_id = app_id; - p_dev->local_vup = false; - - btif_hh_cb.device_num++; - // This is a new device,open the uhid driver now. - p_dev->fd = open(dev_path, O_RDWR | O_CLOEXEC); - if (p_dev->fd < 0){ - APPL_TRACE_ERROR("%s: Error: failed to open uhid, err:%s", - __func__,strerror(errno)); - return; - }else{ - APPL_TRACE_DEBUG("%s: uhid fd = %d", __func__, p_dev->fd); - p_dev->hh_keep_polling = 1; - p_dev->hh_poll_thread_id = create_thread(btif_hh_poll_event_thread, p_dev); - } - - - break; - } - } + break; + } } + } - if (p_dev == NULL) { - APPL_TRACE_ERROR("%s: Error: too many HID devices are connected", __func__); - return; - } + if (p_dev == NULL) { + APPL_TRACE_ERROR("%s: Error: too many HID devices are connected", __func__); + return; + } - p_dev->dev_status = BTHH_CONN_STATE_CONNECTED; - APPL_TRACE_DEBUG("%s: Return device status %d", __func__, p_dev->dev_status); + p_dev->dev_status = BTHH_CONN_STATE_CONNECTED; + APPL_TRACE_DEBUG("%s: Return device status %d", __func__, p_dev->dev_status); } - /******************************************************************************* -** -** Function bta_hh_co_close -** -** Description When connection is closed, this call-out function is executed -** by HH to do platform specific finalization. -** -** Parameters dev_handle - device handle -** app_id - application id -** -** Returns void. -*******************************************************************************/ -void bta_hh_co_close(uint8_t dev_handle, uint8_t app_id) -{ - uint32_t i; - btif_hh_device_t *p_dev = NULL; - - APPL_TRACE_WARNING("%s: dev_handle = %d, app_id = %d", __func__, dev_handle, app_id); - if (dev_handle == BTA_HH_INVALID_HANDLE) { - APPL_TRACE_WARNING("%s: Oops, dev_handle (%d) is invalid...", __func__, dev_handle); - return; + * + * Function bta_hh_co_close + * + * Description When connection is closed, this call-out function is executed + * by HH to do platform specific finalization. + * + * Parameters dev_handle - device handle + * app_id - application id + * + * Returns void. + ******************************************************************************/ +void bta_hh_co_close(uint8_t dev_handle, uint8_t app_id) { + uint32_t i; + btif_hh_device_t* p_dev = NULL; + + APPL_TRACE_WARNING("%s: dev_handle = %d, app_id = %d", __func__, dev_handle, + app_id); + if (dev_handle == BTA_HH_INVALID_HANDLE) { + APPL_TRACE_WARNING("%s: Oops, dev_handle (%d) is invalid...", __func__, + dev_handle); + return; + } + + for (i = 0; i < BTIF_HH_MAX_HID; i++) { + p_dev = &btif_hh_cb.devices[i]; + if (p_dev->dev_status != BTHH_CONN_STATE_UNKNOWN && + p_dev->dev_handle == dev_handle) { + APPL_TRACE_WARNING( + "%s: Found an existing device with the same handle " + "dev_status = %d, dev_handle =%d", + __func__, p_dev->dev_status, p_dev->dev_handle); + btif_hh_close_poll_thread(p_dev); + break; } - - for (i = 0; i < BTIF_HH_MAX_HID; i++) { - p_dev = &btif_hh_cb.devices[i]; - if (p_dev->dev_status != BTHH_CONN_STATE_UNKNOWN && p_dev->dev_handle == dev_handle) { - APPL_TRACE_WARNING("%s: Found an existing device with the same handle " - "dev_status = %d, dev_handle =%d" - ,__func__,p_dev->dev_status - ,p_dev->dev_handle); - btif_hh_close_poll_thread(p_dev); - break; - } - } + } } - /******************************************************************************* -** -** Function bta_hh_co_data -** -** Description This function is executed by BTA when HID host receive a data -** report. -** -** Parameters dev_handle - device handle -** *p_rpt - pointer to the report data -** len - length of report data -** mode - Hid host Protocol Mode -** sub_clas - Device Subclass -** app_id - application id -** -** Returns void -*******************************************************************************/ -void bta_hh_co_data(uint8_t dev_handle, uint8_t *p_rpt, uint16_t len, tBTA_HH_PROTO_MODE mode, - uint8_t sub_class, uint8_t ctry_code, BD_ADDR peer_addr, uint8_t app_id) -{ - btif_hh_device_t *p_dev; - UNUSED(peer_addr); - - APPL_TRACE_DEBUG("%s: dev_handle = %d, subclass = 0x%02X, mode = %d, " - "ctry_code = %d, app_id = %d", - __func__, dev_handle, sub_class, mode, ctry_code, app_id); - - p_dev = btif_hh_find_connected_dev_by_handle(dev_handle); - if (p_dev == NULL) { - APPL_TRACE_WARNING("%s: Error: unknown HID device handle %d", __func__, dev_handle); - return; - } - - // Wait a maximum of MAX_POLLING_ATTEMPTS x POLLING_SLEEP_DURATION in case - // device creation is pending. - if (p_dev->fd >= 0) { - uint32_t polling_attempts = 0; - while (!p_dev->ready_for_data && polling_attempts++ < BTIF_HH_MAX_POLLING_ATTEMPTS) { - usleep(BTIF_HH_POLLING_SLEEP_DURATION_US); - } - } - - // Send the HID data to the kernel. - if ((p_dev->fd >= 0) && p_dev->ready_for_data) { - bta_hh_co_write(p_dev->fd, p_rpt, len); - }else { - APPL_TRACE_WARNING("%s: Error: fd = %d, ready %d, len = %d", __func__, p_dev->fd, - p_dev->ready_for_data, len); + * + * Function bta_hh_co_data + * + * Description This function is executed by BTA when HID host receive a + *data + * report. + * + * Parameters dev_handle - device handle + * *p_rpt - pointer to the report data + * len - length of report data + * mode - Hid host Protocol Mode + * sub_clas - Device Subclass + * app_id - application id + * + * Returns void + ******************************************************************************/ +void bta_hh_co_data(uint8_t dev_handle, uint8_t* p_rpt, uint16_t len, + tBTA_HH_PROTO_MODE mode, uint8_t sub_class, + uint8_t ctry_code, BD_ADDR peer_addr, uint8_t app_id) { + btif_hh_device_t* p_dev; + UNUSED(peer_addr); + + APPL_TRACE_DEBUG( + "%s: dev_handle = %d, subclass = 0x%02X, mode = %d, " + "ctry_code = %d, app_id = %d", + __func__, dev_handle, sub_class, mode, ctry_code, app_id); + + p_dev = btif_hh_find_connected_dev_by_handle(dev_handle); + if (p_dev == NULL) { + APPL_TRACE_WARNING("%s: Error: unknown HID device handle %d", __func__, + dev_handle); + return; + } + + // Wait a maximum of MAX_POLLING_ATTEMPTS x POLLING_SLEEP_DURATION in case + // device creation is pending. + if (p_dev->fd >= 0) { + uint32_t polling_attempts = 0; + while (!p_dev->ready_for_data && + polling_attempts++ < BTIF_HH_MAX_POLLING_ATTEMPTS) { + usleep(BTIF_HH_POLLING_SLEEP_DURATION_US); } + } + + // Send the HID data to the kernel. + if ((p_dev->fd >= 0) && p_dev->ready_for_data) { + bta_hh_co_write(p_dev->fd, p_rpt, len); + } else { + APPL_TRACE_WARNING("%s: Error: fd = %d, ready %d, len = %d", __func__, + p_dev->fd, p_dev->ready_for_data, len); + } } - /******************************************************************************* -** -** Function bta_hh_co_send_hid_info -** -** Description This function is called in btif_hh.c to process DSCP received. -** -** Parameters dev_handle - device handle -** dscp_len - report descriptor length -** *p_dscp - report descriptor -** -** Returns void -*******************************************************************************/ -void bta_hh_co_send_hid_info(btif_hh_device_t *p_dev, const char *dev_name, uint16_t vendor_id, - uint16_t product_id, uint16_t version, uint8_t ctry_code, - int dscp_len, uint8_t *p_dscp) -{ - int result; - struct uhid_event ev; - - if (p_dev->fd < 0) { - APPL_TRACE_WARNING("%s: Error: fd = %d, dscp_len = %d", __func__, p_dev->fd, dscp_len); - return; - } - - APPL_TRACE_WARNING("%s: fd = %d, name = [%s], dscp_len = %d", __func__, - p_dev->fd, dev_name, dscp_len); - APPL_TRACE_WARNING("%s: vendor_id = 0x%04x, product_id = 0x%04x, version= 0x%04x," - "ctry_code=0x%02x",__func__, - vendor_id, product_id, - version, ctry_code); - - //Create and send hid descriptor to kernel - memset(&ev, 0, sizeof(ev)); - ev.type = UHID_CREATE; - strncpy((char*)ev.u.create.name, dev_name, sizeof(ev.u.create.name) - 1); - snprintf((char*)ev.u.create.uniq, sizeof(ev.u.create.uniq), - "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X", - p_dev->bd_addr.address[5], p_dev->bd_addr.address[4], - p_dev->bd_addr.address[3], p_dev->bd_addr.address[2], - p_dev->bd_addr.address[1], p_dev->bd_addr.address[0]); - ev.u.create.rd_size = dscp_len; - ev.u.create.rd_data = p_dscp; - ev.u.create.bus = BUS_BLUETOOTH; - ev.u.create.vendor = vendor_id; - ev.u.create.product = product_id; - ev.u.create.version = version; - ev.u.create.country = ctry_code; - result = uhid_write(p_dev->fd, &ev); - - APPL_TRACE_WARNING("%s: wrote descriptor to fd = %d, dscp_len = %d, result = %d", __func__, - p_dev->fd, dscp_len, result); - - if (result) { - APPL_TRACE_WARNING("%s: Error: failed to send DSCP, result = %d", __func__, result); - - /* The HID report descriptor is corrupted. Close the driver. */ - close(p_dev->fd); - p_dev->fd = -1; - } + * + * Function bta_hh_co_send_hid_info + * + * Description This function is called in btif_hh.c to process DSCP + *received. + * + * Parameters dev_handle - device handle + * dscp_len - report descriptor length + * *p_dscp - report descriptor + * + * Returns void + ******************************************************************************/ +void bta_hh_co_send_hid_info(btif_hh_device_t* p_dev, const char* dev_name, + uint16_t vendor_id, uint16_t product_id, + uint16_t version, uint8_t ctry_code, int dscp_len, + uint8_t* p_dscp) { + int result; + struct uhid_event ev; + + if (p_dev->fd < 0) { + APPL_TRACE_WARNING("%s: Error: fd = %d, dscp_len = %d", __func__, p_dev->fd, + dscp_len); + return; + } + + APPL_TRACE_WARNING("%s: fd = %d, name = [%s], dscp_len = %d", __func__, + p_dev->fd, dev_name, dscp_len); + APPL_TRACE_WARNING( + "%s: vendor_id = 0x%04x, product_id = 0x%04x, version= 0x%04x," + "ctry_code=0x%02x", + __func__, vendor_id, product_id, version, ctry_code); + + // Create and send hid descriptor to kernel + memset(&ev, 0, sizeof(ev)); + ev.type = UHID_CREATE; + strncpy((char*)ev.u.create.name, dev_name, sizeof(ev.u.create.name) - 1); + snprintf((char*)ev.u.create.uniq, sizeof(ev.u.create.uniq), + "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X", p_dev->bd_addr.address[5], + p_dev->bd_addr.address[4], p_dev->bd_addr.address[3], + p_dev->bd_addr.address[2], p_dev->bd_addr.address[1], + p_dev->bd_addr.address[0]); + ev.u.create.rd_size = dscp_len; + ev.u.create.rd_data = p_dscp; + ev.u.create.bus = BUS_BLUETOOTH; + ev.u.create.vendor = vendor_id; + ev.u.create.product = product_id; + ev.u.create.version = version; + ev.u.create.country = ctry_code; + result = uhid_write(p_dev->fd, &ev); + + APPL_TRACE_WARNING( + "%s: wrote descriptor to fd = %d, dscp_len = %d, result = %d", __func__, + p_dev->fd, dscp_len, result); + + if (result) { + APPL_TRACE_WARNING("%s: Error: failed to send DSCP, result = %d", __func__, + result); + + /* The HID report descriptor is corrupted. Close the driver. */ + close(p_dev->fd); + p_dev->fd = -1; + } } #if (BLE_INCLUDED == TRUE && BTA_HH_LE_INCLUDED == TRUE) /******************************************************************************* -** -** Function bta_hh_le_co_rpt_info -** -** Description This callout function is to convey the report information on -** a HOGP device to the application. Application can save this -** information in NV if device is bonded and load it back when -** stack reboot. -** -** Parameters remote_bda - remote device address -** p_entry - report entry pointer -** app_id - application id -** -** Returns void. -** -*******************************************************************************/ -void bta_hh_le_co_rpt_info(BD_ADDR remote_bda, tBTA_HH_RPT_CACHE_ENTRY *p_entry, uint8_t app_id) -{ - UNUSED(app_id); - - unsigned idx = 0; - - bdstr_t bdstr; - snprintf(bdstr, sizeof(bdstr), "%02x:%02x:%02x:%02x:%02x:%02x", - remote_bda[0], remote_bda[1], remote_bda[2], - remote_bda[3], remote_bda[4], remote_bda[5]); - - size_t len = btif_config_get_bin_length(bdstr, "HidReport"); - if (len >= sizeof(tBTA_HH_RPT_CACHE_ENTRY) && len <= sizeof(sReportCache)) - { - btif_config_get_bin(bdstr, "HidReport", (uint8_t *)sReportCache, &len); - idx = len / sizeof(tBTA_HH_RPT_CACHE_ENTRY); - } - - if (idx < BTA_HH_NV_LOAD_MAX) - { - memcpy(&sReportCache[idx++], p_entry, sizeof(tBTA_HH_RPT_CACHE_ENTRY)); - btif_config_set_bin(bdstr, "HidReport", (const uint8_t *)sReportCache, - idx * sizeof(tBTA_HH_RPT_CACHE_ENTRY)); - BTIF_TRACE_DEBUG("%s() - Saving report; dev=%s, idx=%d", __func__, bdstr, idx); - } + * + * Function bta_hh_le_co_rpt_info + * + * Description This callout function is to convey the report information on + * a HOGP device to the application. Application can save this + * information in NV if device is bonded and load it back when + * stack reboot. + * + * Parameters remote_bda - remote device address + * p_entry - report entry pointer + * app_id - application id + * + * Returns void. + * + ******************************************************************************/ +void bta_hh_le_co_rpt_info(BD_ADDR remote_bda, tBTA_HH_RPT_CACHE_ENTRY* p_entry, + uint8_t app_id) { + UNUSED(app_id); + + unsigned idx = 0; + + bdstr_t bdstr; + snprintf(bdstr, sizeof(bdstr), "%02x:%02x:%02x:%02x:%02x:%02x", remote_bda[0], + remote_bda[1], remote_bda[2], remote_bda[3], remote_bda[4], + remote_bda[5]); + + size_t len = btif_config_get_bin_length(bdstr, "HidReport"); + if (len >= sizeof(tBTA_HH_RPT_CACHE_ENTRY) && len <= sizeof(sReportCache)) { + btif_config_get_bin(bdstr, "HidReport", (uint8_t*)sReportCache, &len); + idx = len / sizeof(tBTA_HH_RPT_CACHE_ENTRY); + } + + if (idx < BTA_HH_NV_LOAD_MAX) { + memcpy(&sReportCache[idx++], p_entry, sizeof(tBTA_HH_RPT_CACHE_ENTRY)); + btif_config_set_bin(bdstr, "HidReport", (const uint8_t*)sReportCache, + idx * sizeof(tBTA_HH_RPT_CACHE_ENTRY)); + BTIF_TRACE_DEBUG("%s() - Saving report; dev=%s, idx=%d", __func__, bdstr, + idx); + } } - /******************************************************************************* -** -** Function bta_hh_le_co_cache_load -** -** Description This callout function is to request the application to load the -** cached HOGP report if there is any. When cache reading is completed, -** bta_hh_le_ci_cache_load() is called by the application. -** -** Parameters remote_bda - remote device address -** p_num_rpt: number of cached report -** app_id - application id -** -** Returns the acched report array -** -*******************************************************************************/ -tBTA_HH_RPT_CACHE_ENTRY * bta_hh_le_co_cache_load (BD_ADDR remote_bda, - uint8_t *p_num_rpt, uint8_t app_id) -{ - UNUSED(app_id); - - bdstr_t bdstr; - snprintf(bdstr, sizeof(bdstr), "%02x:%02x:%02x:%02x:%02x:%02x", - remote_bda[0], remote_bda[1], remote_bda[2], - remote_bda[3], remote_bda[4], remote_bda[5]); - - size_t len = btif_config_get_bin_length(bdstr, "HidReport"); - if (!p_num_rpt && len < sizeof(tBTA_HH_RPT_CACHE_ENTRY)) - return NULL; - - if (len > sizeof(sReportCache)) - len = sizeof(sReportCache); - btif_config_get_bin(bdstr, "HidReport", (uint8_t *)sReportCache, &len); - *p_num_rpt = len / sizeof(tBTA_HH_RPT_CACHE_ENTRY); - - BTIF_TRACE_DEBUG("%s() - Loaded %d reports; dev=%s", __func__, *p_num_rpt, bdstr); - - return sReportCache; + * + * Function bta_hh_le_co_cache_load + * + * Description This callout function is to request the application to load + *the + * cached HOGP report if there is any. When cache reading is + *completed, + * bta_hh_le_ci_cache_load() is called by the application. + * + * Parameters remote_bda - remote device address + * p_num_rpt: number of cached report + * app_id - application id + * + * Returns the acched report array + * + ******************************************************************************/ +tBTA_HH_RPT_CACHE_ENTRY* bta_hh_le_co_cache_load(BD_ADDR remote_bda, + uint8_t* p_num_rpt, + uint8_t app_id) { + UNUSED(app_id); + + bdstr_t bdstr; + snprintf(bdstr, sizeof(bdstr), "%02x:%02x:%02x:%02x:%02x:%02x", remote_bda[0], + remote_bda[1], remote_bda[2], remote_bda[3], remote_bda[4], + remote_bda[5]); + + size_t len = btif_config_get_bin_length(bdstr, "HidReport"); + if (!p_num_rpt && len < sizeof(tBTA_HH_RPT_CACHE_ENTRY)) return NULL; + + if (len > sizeof(sReportCache)) len = sizeof(sReportCache); + btif_config_get_bin(bdstr, "HidReport", (uint8_t*)sReportCache, &len); + *p_num_rpt = len / sizeof(tBTA_HH_RPT_CACHE_ENTRY); + + BTIF_TRACE_DEBUG("%s() - Loaded %d reports; dev=%s", __func__, *p_num_rpt, + bdstr); + + return sReportCache; } /******************************************************************************* -** -** Function bta_hh_le_co_reset_rpt_cache -** -** Description This callout function is to reset the HOGP device cache. -** -** Parameters remote_bda - remote device address -** -** Returns none -** -*******************************************************************************/ -void bta_hh_le_co_reset_rpt_cache (BD_ADDR remote_bda, uint8_t app_id) -{ - UNUSED(app_id); - - bdstr_t bdstr; - snprintf(bdstr, sizeof(bdstr), "%02x:%02x:%02x:%02x:%02x:%02x", - remote_bda[0], remote_bda[1], remote_bda[2], - remote_bda[3], remote_bda[4], remote_bda[5]); - btif_config_remove(bdstr, "HidReport"); - - BTIF_TRACE_DEBUG("%s() - Reset cache for bda %s", __func__, bdstr); -} + * + * Function bta_hh_le_co_reset_rpt_cache + * + * Description This callout function is to reset the HOGP device cache. + * + * Parameters remote_bda - remote device address + * + * Returns none + * + ******************************************************************************/ +void bta_hh_le_co_reset_rpt_cache(BD_ADDR remote_bda, uint8_t app_id) { + UNUSED(app_id); + + bdstr_t bdstr; + snprintf(bdstr, sizeof(bdstr), "%02x:%02x:%02x:%02x:%02x:%02x", remote_bda[0], + remote_bda[1], remote_bda[2], remote_bda[3], remote_bda[4], + remote_bda[5]); + btif_config_remove(bdstr, "HidReport"); -#endif // (BLE_INCLUDED == TRUE && BTA_HH_LE_INCLUDED == TRUE) + BTIF_TRACE_DEBUG("%s() - Reset cache for bda %s", __func__, bdstr); +} +#endif // (BLE_INCLUDED == TRUE && BTA_HH_LE_INCLUDED == TRUE) diff --git a/btif/co/bta_hl_co.cc b/btif/co/bta_hl_co.cc index 30bdd54b8..1bcd0ead2 100644 --- a/btif/co/bta_hl_co.cc +++ b/btif/co/bta_hl_co.cc @@ -34,7 +34,6 @@ #include #include -#include "osi/include/osi.h" #include "bta_api.h" #include "bta_hl_api.h" #include "bta_hl_ci.h" @@ -43,416 +42,383 @@ #include "btif_hl.h" #include "btif_util.h" #include "btm_api.h" +#include "osi/include/osi.h" /***************************************************************************** -** Constants and Data Types -*****************************************************************************/ + * Constants and Data Types + ****************************************************************************/ /************************** -** Common Definitions -***************************/ - - - + * Common Definitions + **************************/ /******************************************************************************* -** -** Function bta_hl_co_get_num_of_mdep -** -** Description This function is called to get the number of MDEPs for this -** application ID -** -** Parameters app_id - application ID -** p_num_of_mdep (output) - number of MDEP configurations supported -** by the application -** -** Returns true on success -** -*******************************************************************************/ -bool bta_hl_co_get_num_of_mdep(uint8_t app_id, uint8_t *p_num_of_mdep) -{ - uint8_t app_idx; - bool success = false; - - if (btif_hl_find_app_idx(app_id, &app_idx)) - { - *p_num_of_mdep = p_btif_hl_cb->acb[app_idx].sup_feature.num_of_mdeps; - success = true; - } - - - BTIF_TRACE_DEBUG("%s success=%d num_mdeps=%d", - __func__, success, *p_num_of_mdep ); - return success; + * + * Function bta_hl_co_get_num_of_mdep + * + * Description This function is called to get the number of MDEPs for this + * application ID + * + * Parameters app_id - application ID + * p_num_of_mdep (output) - number of MDEP configurations + *supported + * by the application + * + * Returns true on success + * + ******************************************************************************/ +bool bta_hl_co_get_num_of_mdep(uint8_t app_id, uint8_t* p_num_of_mdep) { + uint8_t app_idx; + bool success = false; + + if (btif_hl_find_app_idx(app_id, &app_idx)) { + *p_num_of_mdep = p_btif_hl_cb->acb[app_idx].sup_feature.num_of_mdeps; + success = true; + } + + BTIF_TRACE_DEBUG("%s success=%d num_mdeps=%d", __func__, success, + *p_num_of_mdep); + return success; } /******************************************************************************* -** -** Function bta_hl_co_advrtise_source_sdp -** -** Description This function is called to find out whether the SOURCE MDEP -** configuration information should be advertize in the SDP or nopt -** -** Parameters app_id - application ID -** -** Returns true when advertise the SOURCE MDEP configuration -** information -** -*******************************************************************************/ -bool bta_hl_co_advrtise_source_sdp(uint8_t app_id) -{ - bool advertize_source_sdp=false; - uint8_t app_idx; - - if (btif_hl_find_app_idx(app_id, &app_idx)) - { - advertize_source_sdp = p_btif_hl_cb->acb[app_idx].sup_feature.advertize_source_sdp; - } + * + * Function bta_hl_co_advrtise_source_sdp + * + * Description This function is called to find out whether the SOURCE MDEP + * configuration information should be advertize in the SDP or + *nopt + * + * Parameters app_id - application ID + * + * Returns true when advertise the SOURCE MDEP configuration + * information + * + ******************************************************************************/ +bool bta_hl_co_advrtise_source_sdp(uint8_t app_id) { + bool advertize_source_sdp = false; + uint8_t app_idx; + if (btif_hl_find_app_idx(app_id, &app_idx)) { + advertize_source_sdp = + p_btif_hl_cb->acb[app_idx].sup_feature.advertize_source_sdp; + } - BTIF_TRACE_DEBUG("%s advertize_flag=%d", __func__, advertize_source_sdp ); + BTIF_TRACE_DEBUG("%s advertize_flag=%d", __func__, advertize_source_sdp); - return advertize_source_sdp; + return advertize_source_sdp; } /******************************************************************************* -** -** Function bta_hl_co_get_mdep_config -** -** Description This function is called to get the supported feature -** configuration for the specified mdep index and it also assigns -** the MDEP ID for the specified mdep index -** -** Parameters app_id - HDP application ID -** mdep_idx - the mdep index -** mdep_counter - number of mdeps -** mdep_id - the assigned MDEP ID for the specified medp_idx -** p_mdl_cfg (output) - pointer to the MDEP configuration -** -** -** Returns Bloolean - true success -*******************************************************************************/ -bool bta_hl_co_get_mdep_config(uint8_t app_id, - uint8_t mdep_idx, - uint8_t mdep_counter, - tBTA_HL_MDEP_ID mdep_id, - tBTA_HL_MDEP_CFG *p_mdep_cfg) -{ - uint8_t idx ; - uint8_t app_idx; - bool success = false; - - BTIF_TRACE_DEBUG("%s app_id=%d mdep_idx=%d mdep_id=%d mdep_counter=%d", - __func__, app_id,mdep_idx,mdep_id,mdep_counter); - - if (btif_hl_find_app_idx(app_id, &app_idx)) - { - idx = mdep_idx -mdep_counter-1; - p_btif_hl_cb->acb[app_idx].sup_feature.mdep[idx].mdep_id = mdep_id; - memcpy(p_mdep_cfg, - &p_btif_hl_cb->acb[app_idx].sup_feature.mdep[idx].mdep_cfg, - sizeof(tBTA_HL_MDEP_CFG)); - - success = true; - } - - BTIF_TRACE_DEBUG("%s success=%d mdep_idx=%d mdep_id=%d", - __func__, success, mdep_idx, mdep_id ); - - return success; + * + * Function bta_hl_co_get_mdep_config + * + * Description This function is called to get the supported feature + * configuration for the specified mdep index and it also + *assigns + * the MDEP ID for the specified mdep index + * + * Parameters app_id - HDP application ID + * mdep_idx - the mdep index + * mdep_counter - number of mdeps + * mdep_id - the assigned MDEP ID for the specified medp_idx + * p_mdl_cfg (output) - pointer to the MDEP configuration + * + * + * Returns Bloolean - true success + ******************************************************************************/ +bool bta_hl_co_get_mdep_config(uint8_t app_id, uint8_t mdep_idx, + uint8_t mdep_counter, tBTA_HL_MDEP_ID mdep_id, + tBTA_HL_MDEP_CFG* p_mdep_cfg) { + uint8_t idx; + uint8_t app_idx; + bool success = false; + + BTIF_TRACE_DEBUG("%s app_id=%d mdep_idx=%d mdep_id=%d mdep_counter=%d", + __func__, app_id, mdep_idx, mdep_id, mdep_counter); + + if (btif_hl_find_app_idx(app_id, &app_idx)) { + idx = mdep_idx - mdep_counter - 1; + p_btif_hl_cb->acb[app_idx].sup_feature.mdep[idx].mdep_id = mdep_id; + memcpy(p_mdep_cfg, + &p_btif_hl_cb->acb[app_idx].sup_feature.mdep[idx].mdep_cfg, + sizeof(tBTA_HL_MDEP_CFG)); + + success = true; + } + + BTIF_TRACE_DEBUG("%s success=%d mdep_idx=%d mdep_id=%d", __func__, success, + mdep_idx, mdep_id); + + return success; } - /******************************************************************************* -** -** Function bta_hl_co_get_echo_config -** -** Description This function is called to get the echo test -** maximum APDU size configurations -** -** Parameters app_id - HDP application ID -** p_echo_cfg (output) - pointer to the Echo test maximum APDU size -** configuration -** -** Returns Bloolean - true success -*******************************************************************************/ -bool bta_hl_co_get_echo_config(uint8_t app_id, - tBTA_HL_ECHO_CFG *p_echo_cfg) -{ - uint8_t app_idx; - bool success = false; - btif_hl_app_cb_t *p_acb; - tBTA_HL_SUP_FEATURE *p_sup; - - BTIF_TRACE_DEBUG("%s app_id=%d",__func__, app_id ); - - if (btif_hl_find_app_idx(app_id, &app_idx)) - { - p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - p_sup = &p_acb->sup_feature; - p_echo_cfg->max_rx_apdu_size = p_sup->echo_cfg.max_rx_apdu_size; - p_echo_cfg->max_tx_apdu_size = p_sup->echo_cfg.max_tx_apdu_size; - success = true; - } - - BTIF_TRACE_DEBUG("%s success=%d max tx_size=%d rx_size=%d", - __func__, success, p_echo_cfg->max_tx_apdu_size, - p_echo_cfg->max_rx_apdu_size ); - - return success; + * + * Function bta_hl_co_get_echo_config + * + * Description This function is called to get the echo test + * maximum APDU size configurations + * + * Parameters app_id - HDP application ID + * p_echo_cfg (output) - pointer to the Echo test maximum APDU + *size + * configuration + * + * Returns Bloolean - true success + ******************************************************************************/ +bool bta_hl_co_get_echo_config(uint8_t app_id, tBTA_HL_ECHO_CFG* p_echo_cfg) { + uint8_t app_idx; + bool success = false; + btif_hl_app_cb_t* p_acb; + tBTA_HL_SUP_FEATURE* p_sup; + + BTIF_TRACE_DEBUG("%s app_id=%d", __func__, app_id); + + if (btif_hl_find_app_idx(app_id, &app_idx)) { + p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + p_sup = &p_acb->sup_feature; + p_echo_cfg->max_rx_apdu_size = p_sup->echo_cfg.max_rx_apdu_size; + p_echo_cfg->max_tx_apdu_size = p_sup->echo_cfg.max_tx_apdu_size; + success = true; + } + + BTIF_TRACE_DEBUG("%s success=%d max tx_size=%d rx_size=%d", __func__, success, + p_echo_cfg->max_tx_apdu_size, p_echo_cfg->max_rx_apdu_size); + + return success; } - /******************************************************************************* -** -** Function bta_hl_co_save_mdl -** -** Description This function is called to save a MDL configuration item in persistent -** storage -** -** Parameters app_id - HDP application ID -** item_idx - the MDL configuration storage index -** p_mdl_cfg - pointer to the MDL configuration data -** -** Returns void -** -*******************************************************************************/ -void bta_hl_co_save_mdl(uint8_t mdep_id, uint8_t item_idx, tBTA_HL_MDL_CFG *p_mdl_cfg ) -{ - - BTIF_TRACE_DEBUG("%s mdep_id =%d, item_idx=%d active=%d mdl_id=%d time=%d", - __func__, mdep_id, item_idx, - p_mdl_cfg->active, - p_mdl_cfg->mdl_id, - p_mdl_cfg->time); - - btif_hl_save_mdl_cfg(mdep_id, item_idx, p_mdl_cfg); + * + * Function bta_hl_co_save_mdl + * + * Description This function is called to save a MDL configuration item in + *persistent + * storage + * + * Parameters app_id - HDP application ID + * item_idx - the MDL configuration storage index + * p_mdl_cfg - pointer to the MDL configuration data + * + * Returns void + * + ******************************************************************************/ +void bta_hl_co_save_mdl(uint8_t mdep_id, uint8_t item_idx, + tBTA_HL_MDL_CFG* p_mdl_cfg) { + BTIF_TRACE_DEBUG("%s mdep_id =%d, item_idx=%d active=%d mdl_id=%d time=%d", + __func__, mdep_id, item_idx, p_mdl_cfg->active, + p_mdl_cfg->mdl_id, p_mdl_cfg->time); + btif_hl_save_mdl_cfg(mdep_id, item_idx, p_mdl_cfg); } /******************************************************************************* -** -** Function bta_hl_co_delete_mdl -** -** Description This function is called to delete a MDL configuration item in persistent -** storage -** -** Parameters app_id - HDP application ID -** item_idx - the MDL configuration storage index -** -** Returns void -** -*******************************************************************************/ -void bta_hl_co_delete_mdl(uint8_t mdep_id, uint8_t item_idx) -{ - - - BTIF_TRACE_DEBUG("%s mdep_id=%d, item_idx=%d", __func__, mdep_id, item_idx); - - btif_hl_delete_mdl_cfg(mdep_id, item_idx); - + * + * Function bta_hl_co_delete_mdl + * + * Description This function is called to delete a MDL configuration item in + *persistent + * storage + * + * Parameters app_id - HDP application ID + * item_idx - the MDL configuration storage index + * + * Returns void + * + ******************************************************************************/ +void bta_hl_co_delete_mdl(uint8_t mdep_id, uint8_t item_idx) { + BTIF_TRACE_DEBUG("%s mdep_id=%d, item_idx=%d", __func__, mdep_id, item_idx); + btif_hl_delete_mdl_cfg(mdep_id, item_idx); } /******************************************************************************* -** -** Function bta_hl_co_get_mdl_config -** -** Description This function is called to get the MDL configuration -** from the persistent memory. This function shall only be called + * + * Function bta_hl_co_get_mdl_config + * + * Description This function is called to get the MDL configuration + * from the persistent memory. This function shall only be +*called *8 once after the device is powered up -** -** Parameters app_id - HDP application ID -** buffer_size - the unit of the buffer size is sizeof(tBTA_HL_MDL_CFG) -** p_mdl_buf - Point to the starting location of the buffer -** -** Returns bool -** -** -*******************************************************************************/ -bool bta_hl_co_load_mdl_config (uint8_t app_id, uint8_t buffer_size, - tBTA_HL_MDL_CFG *p_mdl_buf ) -{ - bool result = true; - uint8_t i; - tBTA_HL_MDL_CFG *p; - - BTIF_TRACE_DEBUG("%s app_id=%d, num_items=%d", - __func__, app_id, buffer_size); - - if (buffer_size > BTA_HL_NUM_MDL_CFGS) - { - result = false; - return result; - } - result = btif_hl_load_mdl_config(app_id, buffer_size, p_mdl_buf); - - if (result) - { - for (i=0, p=p_mdl_buf; iactive) - { - BTIF_TRACE_DEBUG("i=%d mdl_id=0x%x dch_mode=%d local mdep_role=%d mdep_id=%d mtu=%d", - i, p->mdl_id, p->dch_mode, p->local_mdep_role, p->local_mdep_role, p->mtu); - } - } - } + * + * Parameters app_id - HDP application ID + * buffer_size - the unit of the buffer size is +*sizeof(tBTA_HL_MDL_CFG) + * p_mdl_buf - Point to the starting location of the buffer + * + * Returns bool + * + * + ******************************************************************************/ +bool bta_hl_co_load_mdl_config(uint8_t app_id, uint8_t buffer_size, + tBTA_HL_MDL_CFG* p_mdl_buf) { + bool result = true; + uint8_t i; + tBTA_HL_MDL_CFG* p; - BTIF_TRACE_DEBUG("%s success=%d num_items=%d", __func__, result, buffer_size); + BTIF_TRACE_DEBUG("%s app_id=%d, num_items=%d", __func__, app_id, buffer_size); + if (buffer_size > BTA_HL_NUM_MDL_CFGS) { + result = false; return result; + } + result = btif_hl_load_mdl_config(app_id, buffer_size, p_mdl_buf); + + if (result) { + for (i = 0, p = p_mdl_buf; i < buffer_size; i++, p++) { + if (p->active) { + BTIF_TRACE_DEBUG( + "i=%d mdl_id=0x%x dch_mode=%d local mdep_role=%d mdep_id=%d mtu=%d", + i, p->mdl_id, p->dch_mode, p->local_mdep_role, p->local_mdep_role, + p->mtu); + } + } + } + + BTIF_TRACE_DEBUG("%s success=%d num_items=%d", __func__, result, buffer_size); + + return result; } /******************************************************************************* -** -** Function bta_hl_co_get_tx_data -** -** Description Get the data to be sent -** -** Parameters app_id - HDP application ID -** mdl_handle - MDL handle -** buf_size - the size of the buffer -** p_buf - the buffer pointer -** evt - the evt to be passed back to the HL in the -** bta_hl_ci_get_tx_data call-in function -** -** Returns Void -** -*******************************************************************************/ -void bta_hl_co_get_tx_data (uint8_t app_id, tBTA_HL_MDL_HANDLE mdl_handle, - uint16_t buf_size, uint8_t *p_buf, uint16_t evt) -{ - uint8_t app_idx, mcl_idx, mdl_idx; - btif_hl_mdl_cb_t *p_dcb; - tBTA_HL_STATUS status = BTA_HL_STATUS_FAIL; - - BTIF_TRACE_DEBUG("%s app_id=%d mdl_handle=0x%x buf_size=%d", - __func__, app_id, mdl_handle, buf_size); - - if (btif_hl_find_mdl_idx_using_handle(mdl_handle, &app_idx, &mcl_idx, &mdl_idx)) - { - p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); - - if (p_dcb->tx_size <= buf_size ) - { - memcpy(p_buf, p_dcb->p_tx_pkt, p_dcb->tx_size); - osi_free_and_reset((void **)&p_dcb->p_tx_pkt); - p_dcb->tx_size = 0; - status = BTA_HL_STATUS_OK; - } + * + * Function bta_hl_co_get_tx_data + * + * Description Get the data to be sent + * + * Parameters app_id - HDP application ID + * mdl_handle - MDL handle + * buf_size - the size of the buffer + * p_buf - the buffer pointer + * evt - the evt to be passed back to the HL in the + * bta_hl_ci_get_tx_data call-in function + * + * Returns Void + * + ******************************************************************************/ +void bta_hl_co_get_tx_data(uint8_t app_id, tBTA_HL_MDL_HANDLE mdl_handle, + uint16_t buf_size, uint8_t* p_buf, uint16_t evt) { + uint8_t app_idx, mcl_idx, mdl_idx; + btif_hl_mdl_cb_t* p_dcb; + tBTA_HL_STATUS status = BTA_HL_STATUS_FAIL; + + BTIF_TRACE_DEBUG("%s app_id=%d mdl_handle=0x%x buf_size=%d", __func__, app_id, + mdl_handle, buf_size); + + if (btif_hl_find_mdl_idx_using_handle(mdl_handle, &app_idx, &mcl_idx, + &mdl_idx)) { + p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); + + if (p_dcb->tx_size <= buf_size) { + memcpy(p_buf, p_dcb->p_tx_pkt, p_dcb->tx_size); + osi_free_and_reset((void**)&p_dcb->p_tx_pkt); + p_dcb->tx_size = 0; + status = BTA_HL_STATUS_OK; } + } - - bta_hl_ci_get_tx_data(mdl_handle, status, evt); - + bta_hl_ci_get_tx_data(mdl_handle, status, evt); } - /******************************************************************************* -** -** Function bta_hl_co_put_rx_data -** -** Description Put the received data -** -** Parameters app_id - HDP application ID -** mdl_handle - MDL handle -** data_size - the size of the data -** p_data - the data pointer -** evt - the evt to be passed back to the HL in the -** bta_hl_ci_put_rx_data call-in function -** -** Returns Void -** -*******************************************************************************/ -void bta_hl_co_put_rx_data (uint8_t app_id, tBTA_HL_MDL_HANDLE mdl_handle, - uint16_t data_size, uint8_t *p_data, uint16_t evt) -{ - uint8_t app_idx, mcl_idx, mdl_idx; - btif_hl_mdl_cb_t *p_dcb; - tBTA_HL_STATUS status = BTA_HL_STATUS_FAIL; - BTIF_TRACE_DEBUG("%s app_id=%d mdl_handle=0x%x data_size=%d", - __func__,app_id, mdl_handle, data_size); - - if (btif_hl_find_mdl_idx_using_handle(mdl_handle, &app_idx, &mcl_idx, &mdl_idx)) - { - p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); - - p_dcb->p_rx_pkt = (uint8_t *)osi_malloc(data_size); - memcpy(p_dcb->p_rx_pkt, p_data, data_size); - if (p_dcb->p_scb) { - BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=0x%x mdl_idx=0x%x data_size=%d", - app_idx, mcl_idx, mdl_idx, data_size); - ssize_t r; - OSI_NO_INTR(r = send(p_dcb->p_scb->socket_id[1], p_dcb->p_rx_pkt, - data_size, 0)); - if (r == data_size) { - BTIF_TRACE_DEBUG("socket send success data_size=%d", data_size); - status = BTA_HL_STATUS_OK; - } else { - BTIF_TRACE_ERROR("socket send failed r=%d data_size=%d", r, - data_size); - } - } - osi_free_and_reset((void **)&p_dcb->p_rx_pkt); + * + * Function bta_hl_co_put_rx_data + * + * Description Put the received data + * + * Parameters app_id - HDP application ID + * mdl_handle - MDL handle + * data_size - the size of the data + * p_data - the data pointer + * evt - the evt to be passed back to the HL in the + * bta_hl_ci_put_rx_data call-in function + * + * Returns Void + * + ******************************************************************************/ +void bta_hl_co_put_rx_data(uint8_t app_id, tBTA_HL_MDL_HANDLE mdl_handle, + uint16_t data_size, uint8_t* p_data, uint16_t evt) { + uint8_t app_idx, mcl_idx, mdl_idx; + btif_hl_mdl_cb_t* p_dcb; + tBTA_HL_STATUS status = BTA_HL_STATUS_FAIL; + BTIF_TRACE_DEBUG("%s app_id=%d mdl_handle=0x%x data_size=%d", __func__, + app_id, mdl_handle, data_size); + + if (btif_hl_find_mdl_idx_using_handle(mdl_handle, &app_idx, &mcl_idx, + &mdl_idx)) { + p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); + + p_dcb->p_rx_pkt = (uint8_t*)osi_malloc(data_size); + memcpy(p_dcb->p_rx_pkt, p_data, data_size); + if (p_dcb->p_scb) { + BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=0x%x mdl_idx=0x%x data_size=%d", + app_idx, mcl_idx, mdl_idx, data_size); + ssize_t r; + OSI_NO_INTR( + r = send(p_dcb->p_scb->socket_id[1], p_dcb->p_rx_pkt, data_size, 0)); + if (r == data_size) { + BTIF_TRACE_DEBUG("socket send success data_size=%d", data_size); + status = BTA_HL_STATUS_OK; + } else { + BTIF_TRACE_ERROR("socket send failed r=%d data_size=%d", r, data_size); + } } + osi_free_and_reset((void**)&p_dcb->p_rx_pkt); + } - bta_hl_ci_put_rx_data(mdl_handle, status, evt); + bta_hl_ci_put_rx_data(mdl_handle, status, evt); } - /******************************************************************************* -** -** Function bta_hl_co_get_tx_data -** -** Description Get the Echo data to be sent -** -** Parameters app_id - HDP application ID -** mcl_handle - MCL handle -** buf_size - the size of the buffer -** p_buf - the buffer pointer -** evt - the evt to be passed back to the HL in the -** bta_hl_ci_get_tx_data call-in function -** -** Returns Void -** -*******************************************************************************/ -void bta_hl_co_get_echo_data (uint8_t app_id, tBTA_HL_MCL_HANDLE mcl_handle, - uint16_t buf_size, uint8_t *p_buf, uint16_t evt) -{ - tBTA_HL_STATUS status = BTA_HL_STATUS_FAIL; - UNUSED(app_id); - UNUSED(buf_size); - UNUSED(p_buf); - - BTIF_TRACE_ERROR("%s not supported",__func__); - bta_hl_ci_get_echo_data(mcl_handle, status, evt); + * + * Function bta_hl_co_get_tx_data + * + * Description Get the Echo data to be sent + * + * Parameters app_id - HDP application ID + * mcl_handle - MCL handle + * buf_size - the size of the buffer + * p_buf - the buffer pointer + * evt - the evt to be passed back to the HL in the + * bta_hl_ci_get_tx_data call-in function + * + * Returns Void + * + ******************************************************************************/ +void bta_hl_co_get_echo_data(uint8_t app_id, tBTA_HL_MCL_HANDLE mcl_handle, + uint16_t buf_size, uint8_t* p_buf, uint16_t evt) { + tBTA_HL_STATUS status = BTA_HL_STATUS_FAIL; + UNUSED(app_id); + UNUSED(buf_size); + UNUSED(p_buf); + + BTIF_TRACE_ERROR("%s not supported", __func__); + bta_hl_ci_get_echo_data(mcl_handle, status, evt); } - /******************************************************************************* -** -** Function bta_hl_co_put_echo_data -** -** Description Put the received loopback echo data -** -** Parameters app_id - HDP application ID -** mcl_handle - MCL handle -** data_size - the size of the data -** p_data - the data pointer -** evt - the evt to be passed back to the HL in the -** bta_hl_ci_put_echo_data call-in function -** -** Returns Void -** -*******************************************************************************/ -void bta_hl_co_put_echo_data (uint8_t app_id, tBTA_HL_MCL_HANDLE mcl_handle, - uint16_t data_size, uint8_t *p_data, uint16_t evt) -{ - tBTA_HL_STATUS status = BTA_HL_STATUS_FAIL; - UNUSED(app_id); - UNUSED(data_size); - UNUSED(p_data); - - BTIF_TRACE_ERROR("%s not supported",__func__); - bta_hl_ci_put_echo_data(mcl_handle, status, evt); + * + * Function bta_hl_co_put_echo_data + * + * Description Put the received loopback echo data + * + * Parameters app_id - HDP application ID + * mcl_handle - MCL handle + * data_size - the size of the data + * p_data - the data pointer + * evt - the evt to be passed back to the HL in the + * bta_hl_ci_put_echo_data call-in function + * + * Returns Void + * + ******************************************************************************/ +void bta_hl_co_put_echo_data(uint8_t app_id, tBTA_HL_MCL_HANDLE mcl_handle, + uint16_t data_size, uint8_t* p_data, + uint16_t evt) { + tBTA_HL_STATUS status = BTA_HL_STATUS_FAIL; + UNUSED(app_id); + UNUSED(data_size); + UNUSED(p_data); + + BTIF_TRACE_ERROR("%s not supported", __func__); + bta_hl_ci_put_echo_data(mcl_handle, status, evt); } - diff --git a/btif/co/bta_pan_co.cc b/btif/co/bta_pan_co.cc index c389c2a70..25329d57b 100644 --- a/btif/co/bta_pan_co.cc +++ b/btif/co/bta_pan_co.cc @@ -24,334 +24,319 @@ * * ******************************************************************************/ -#include "bta_api.h" -#include "bta_pan_api.h" -#include "bta_pan_ci.h" #include "bta_pan_co.h" -#include "pan_api.h" -#include "bt_common.h" #include #include +#include +#include "bt_common.h" +#include "bta_api.h" +#include "bta_pan_api.h" +#include "bta_pan_ci.h" +#include "btcore/include/bdaddr.h" #include "btif_pan_internal.h" #include "btif_sock_thread.h" -#include #include "btif_util.h" -#include "btcore/include/bdaddr.h" +#include "pan_api.h" /******************************************************************************* -** -** Function bta_pan_co_init -** -** Description -** -** -** Returns Data flow mask. -** -*******************************************************************************/ -uint8_t bta_pan_co_init(uint8_t *q_level) -{ - BTIF_TRACE_API("bta_pan_co_init"); + * + * Function bta_pan_co_init + * + * Description + * + * + * Returns Data flow mask. + * + ******************************************************************************/ +uint8_t bta_pan_co_init(uint8_t* q_level) { + BTIF_TRACE_API("bta_pan_co_init"); - /* set the q_level to 30 buffers */ - *q_level = 30; + /* set the q_level to 30 buffers */ + *q_level = 30; - //return (BTA_PAN_RX_PULL | BTA_PAN_TX_PULL); - return (BTA_PAN_RX_PUSH_BUF | BTA_PAN_RX_PUSH | BTA_PAN_TX_PULL); + // return (BTA_PAN_RX_PULL | BTA_PAN_TX_PULL); + return (BTA_PAN_RX_PUSH_BUF | BTA_PAN_RX_PUSH | BTA_PAN_TX_PULL); } /****************************************************************************** -** -** Function bta_pan_co_open -** -** Description -** -** -** -** -** -** Returns void -** -*******************************************************************************/ + * + * Function bta_pan_co_open + * + * Description + * + * + * + * + * + * Returns void + * + ******************************************************************************/ void bta_pan_co_open(uint16_t handle, uint8_t app_id, tBTA_PAN_ROLE local_role, - tBTA_PAN_ROLE peer_role, BD_ADDR peer_addr) -{ - BTIF_TRACE_API("bta_pan_co_open:app_id:%d, local_role:%d, peer_role:%d, " - "handle:%d", app_id, local_role, peer_role, handle); - btpan_conn_t* conn = btpan_find_conn_addr(peer_addr); - if(conn == NULL) - conn = btpan_new_conn(handle, peer_addr, local_role, peer_role); - if(conn) - { - BTIF_TRACE_DEBUG("bta_pan_co_open:tap_fd:%d, open_count:%d, " - "conn->handle:%d should = handle:%d, local_role:%d, remote_role:%d", - btpan_cb.tap_fd, btpan_cb.open_count, conn->handle, handle, - conn->local_role, conn->remote_role); - //refresh the role & bt address + tBTA_PAN_ROLE peer_role, BD_ADDR peer_addr) { + BTIF_TRACE_API( + "bta_pan_co_open:app_id:%d, local_role:%d, peer_role:%d, " + "handle:%d", + app_id, local_role, peer_role, handle); + btpan_conn_t* conn = btpan_find_conn_addr(peer_addr); + if (conn == NULL) + conn = btpan_new_conn(handle, peer_addr, local_role, peer_role); + if (conn) { + BTIF_TRACE_DEBUG( + "bta_pan_co_open:tap_fd:%d, open_count:%d, " + "conn->handle:%d should = handle:%d, local_role:%d, remote_role:%d", + btpan_cb.tap_fd, btpan_cb.open_count, conn->handle, handle, + conn->local_role, conn->remote_role); + // refresh the role & bt address - btpan_cb.open_count++; - conn->handle = handle; - //bdcpy(conn->peer, peer_addr); - if(btpan_cb.tap_fd < 0) - { - btpan_cb.tap_fd = btpan_tap_open(); - if(btpan_cb.tap_fd >= 0) - create_tap_read_thread(btpan_cb.tap_fd); - } - if(btpan_cb.tap_fd >= 0) - { - btpan_cb.flow = 1; - conn->state = PAN_STATE_OPEN; - bta_pan_ci_rx_ready(handle); - } + btpan_cb.open_count++; + conn->handle = handle; + // bdcpy(conn->peer, peer_addr); + if (btpan_cb.tap_fd < 0) { + btpan_cb.tap_fd = btpan_tap_open(); + if (btpan_cb.tap_fd >= 0) create_tap_read_thread(btpan_cb.tap_fd); + } + if (btpan_cb.tap_fd >= 0) { + btpan_cb.flow = 1; + conn->state = PAN_STATE_OPEN; + bta_pan_ci_rx_ready(handle); } + } } /******************************************************************************* -** -** Function bta_pan_co_close -** -** Description This function is called by PAN when a connection to a -** peer is closed. -** -** -** Returns void -** -*******************************************************************************/ -void bta_pan_co_close(uint16_t handle, uint8_t app_id) -{ - BTIF_TRACE_API("bta_pan_co_close:app_id:%d, handle:%d", app_id, handle); - btpan_conn_t* conn = btpan_find_conn_handle(handle); - if(conn && conn->state == PAN_STATE_OPEN) - { - BTIF_TRACE_DEBUG("bta_pan_co_close"); + * + * Function bta_pan_co_close + * + * Description This function is called by PAN when a connection to a + * peer is closed. + * + * + * Returns void + * + ******************************************************************************/ +void bta_pan_co_close(uint16_t handle, uint8_t app_id) { + BTIF_TRACE_API("bta_pan_co_close:app_id:%d, handle:%d", app_id, handle); + btpan_conn_t* conn = btpan_find_conn_handle(handle); + if (conn && conn->state == PAN_STATE_OPEN) { + BTIF_TRACE_DEBUG("bta_pan_co_close"); - // let bta close event reset this handle as it needs - // the handle to find the connection upon CLOSE - //conn->handle = -1; - conn->state = PAN_STATE_CLOSE; - btpan_cb.open_count--; + // let bta close event reset this handle as it needs + // the handle to find the connection upon CLOSE + // conn->handle = -1; + conn->state = PAN_STATE_CLOSE; + btpan_cb.open_count--; - if(btpan_cb.open_count == 0 && btpan_cb.tap_fd != -1) - { - btpan_tap_close(btpan_cb.tap_fd); - btpan_cb.tap_fd = -1; - } + if (btpan_cb.open_count == 0 && btpan_cb.tap_fd != -1) { + btpan_tap_close(btpan_cb.tap_fd); + btpan_cb.tap_fd = -1; } + } } /******************************************************************************* -** -** Function bta_pan_co_tx_path -** -** Description This function is called by PAN to transfer data on the -** TX path; that is, data being sent from BTA to the phone. -** This function is used when the TX data path is configured -** to use the pull interface. The implementation of this -** function will typically call Bluetooth stack functions -** PORT_Read() or PORT_ReadData() to read data from RFCOMM -** and then a platform-specific function to send data that -** data to the phone. -** -** -** Returns void -** -*******************************************************************************/ -void bta_pan_co_tx_path(uint16_t handle, uint8_t app_id) -{ - BT_HDR *p_buf; - BD_ADDR src; - BD_ADDR dst; - uint16_t protocol; - bool ext; - bool forward; - - BTIF_TRACE_API("%s, handle:%d, app_id:%d", __func__, handle, app_id); + * + * Function bta_pan_co_tx_path + * + * Description This function is called by PAN to transfer data on the + * TX path; that is, data being sent from BTA to the phone. + * This function is used when the TX data path is configured + * to use the pull interface. The implementation of this + * function will typically call Bluetooth stack functions + * PORT_Read() or PORT_ReadData() to read data from RFCOMM + * and then a platform-specific function to send data that + * data to the phone. + * + * + * Returns void + * + ******************************************************************************/ +void bta_pan_co_tx_path(uint16_t handle, uint8_t app_id) { + BT_HDR* p_buf; + BD_ADDR src; + BD_ADDR dst; + uint16_t protocol; + bool ext; + bool forward; - btpan_conn_t* conn = btpan_find_conn_handle(handle); - if (!conn) - { - BTIF_TRACE_ERROR("%s: cannot find pan connection", __func__); - return; - } - else if(conn->state != PAN_STATE_OPEN) - { - BTIF_TRACE_ERROR("%s: conn is not opened, conn:%p, conn->state:%d", - __func__, conn, conn->state); - return; - } + BTIF_TRACE_API("%s, handle:%d, app_id:%d", __func__, handle, app_id); - do - { - /* read next data buffer from pan */ - if ((p_buf = bta_pan_ci_readbuf(handle, src, dst, &protocol, - &ext, &forward))) - { - bdstr_t bdstr; - BTIF_TRACE_DEBUG("%s, calling btapp_tap_send, " - "p_buf->len:%d, offset:%d", __func__, p_buf->len, p_buf->offset); - if(is_empty_eth_addr(conn->eth_addr) && is_valid_bt_eth_addr(src)) - { - BTIF_TRACE_DEBUG("%s pan bt peer addr: %s", __func__, - bdaddr_to_string((bt_bdaddr_t *)conn->peer, bdstr, sizeof(bdstr))); - bdaddr_to_string((bt_bdaddr_t *)src, bdstr, sizeof(bdstr)); - BTIF_TRACE_DEBUG("%s: update its ethernet addr: %s", __func__, - bdaddr_to_string((bt_bdaddr_t *)src, bdstr, sizeof(bdstr))); - memcpy(conn->eth_addr, src, sizeof(conn->eth_addr)); + btpan_conn_t* conn = btpan_find_conn_handle(handle); + if (!conn) { + BTIF_TRACE_ERROR("%s: cannot find pan connection", __func__); + return; + } else if (conn->state != PAN_STATE_OPEN) { + BTIF_TRACE_ERROR("%s: conn is not opened, conn:%p, conn->state:%d", + __func__, conn, conn->state); + return; + } - } - btpan_tap_send(btpan_cb.tap_fd, src, dst, protocol, - (char*)(p_buf + 1) + p_buf->offset, p_buf->len, ext, forward); - osi_free(p_buf); - } + do { + /* read next data buffer from pan */ + if ((p_buf = + bta_pan_ci_readbuf(handle, src, dst, &protocol, &ext, &forward))) { + bdstr_t bdstr; + BTIF_TRACE_DEBUG( + "%s, calling btapp_tap_send, " + "p_buf->len:%d, offset:%d", + __func__, p_buf->len, p_buf->offset); + if (is_empty_eth_addr(conn->eth_addr) && is_valid_bt_eth_addr(src)) { + BTIF_TRACE_DEBUG( + "%s pan bt peer addr: %s", __func__, + bdaddr_to_string((bt_bdaddr_t*)conn->peer, bdstr, sizeof(bdstr))); + bdaddr_to_string((bt_bdaddr_t*)src, bdstr, sizeof(bdstr)); + BTIF_TRACE_DEBUG( + "%s: update its ethernet addr: %s", __func__, + bdaddr_to_string((bt_bdaddr_t*)src, bdstr, sizeof(bdstr))); + memcpy(conn->eth_addr, src, sizeof(conn->eth_addr)); + } + btpan_tap_send(btpan_cb.tap_fd, src, dst, protocol, + (char*)(p_buf + 1) + p_buf->offset, p_buf->len, ext, + forward); + osi_free(p_buf); + } - } while (p_buf != NULL); + } while (p_buf != NULL); } /******************************************************************************* -** -** Function bta_pan_co_rx_path -** -** Description -** -** -** -** -** Returns void -** -*******************************************************************************/ -void bta_pan_co_rx_path(uint16_t handle, uint8_t app_id) -{ - UNUSED(handle); - UNUSED(app_id); + * + * Function bta_pan_co_rx_path + * + * Description + * + * + * + * + * Returns void + * + ******************************************************************************/ +void bta_pan_co_rx_path(uint16_t handle, uint8_t app_id) { + UNUSED(handle); + UNUSED(app_id); - BTIF_TRACE_API("bta_pan_co_rx_path not used"); + BTIF_TRACE_API("bta_pan_co_rx_path not used"); } /******************************************************************************* -** -** Function bta_pan_co_tx_write -** -** Description This function is called by PAN to send data to the phone -** when the TX path is configured to use a push interface. -** The implementation of this function must copy the data to -** the phone's memory. -** -** -** Returns void -** -*******************************************************************************/ -void bta_pan_co_tx_write(uint16_t handle, uint8_t app_id, BD_ADDR src, BD_ADDR dst, - uint16_t protocol, uint8_t *p_data, - uint16_t len, bool ext, bool forward) -{ - UNUSED(handle); - UNUSED(app_id); - UNUSED(src); - UNUSED(dst); - UNUSED(protocol); - UNUSED(p_data); - UNUSED(len); - UNUSED(ext); - UNUSED(forward); + * + * Function bta_pan_co_tx_write + * + * Description This function is called by PAN to send data to the phone + * when the TX path is configured to use a push interface. + * The implementation of this function must copy the data to + * the phone's memory. + * + * + * Returns void + * + ******************************************************************************/ +void bta_pan_co_tx_write(uint16_t handle, uint8_t app_id, BD_ADDR src, + BD_ADDR dst, uint16_t protocol, uint8_t* p_data, + uint16_t len, bool ext, bool forward) { + UNUSED(handle); + UNUSED(app_id); + UNUSED(src); + UNUSED(dst); + UNUSED(protocol); + UNUSED(p_data); + UNUSED(len); + UNUSED(ext); + UNUSED(forward); - BTIF_TRACE_API("bta_pan_co_tx_write not used"); + BTIF_TRACE_API("bta_pan_co_tx_write not used"); } /******************************************************************************* -** -** Function bta_pan_co_tx_writebuf -** -** Description This function is called by PAN to send data to the phone -** when the TX path is configured to use a push interface with -** zero copy. The phone must free the buffer using function -** osi_free() when it is through processing the buffer. -** -** -** Returns true if flow enabled -** -*******************************************************************************/ -void bta_pan_co_tx_writebuf(uint16_t handle, uint8_t app_id, BD_ADDR src, - BD_ADDR dst, uint16_t protocol, BT_HDR *p_buf, - bool ext, bool forward) -{ - UNUSED(handle); - UNUSED(app_id); - UNUSED(src); - UNUSED(dst); - UNUSED(protocol); - UNUSED(p_buf); - UNUSED(ext); - UNUSED(forward); + * + * Function bta_pan_co_tx_writebuf + * + * Description This function is called by PAN to send data to the phone + * when the TX path is configured to use a push interface with + * zero copy. The phone must free the buffer using function + * osi_free() when it is through processing the buffer. + * + * + * Returns true if flow enabled + * + ******************************************************************************/ +void bta_pan_co_tx_writebuf(uint16_t handle, uint8_t app_id, BD_ADDR src, + BD_ADDR dst, uint16_t protocol, BT_HDR* p_buf, + bool ext, bool forward) { + UNUSED(handle); + UNUSED(app_id); + UNUSED(src); + UNUSED(dst); + UNUSED(protocol); + UNUSED(p_buf); + UNUSED(ext); + UNUSED(forward); - BTIF_TRACE_API("bta_pan_co_tx_writebuf not used"); + BTIF_TRACE_API("bta_pan_co_tx_writebuf not used"); } /******************************************************************************* -** -** Function bta_pan_co_rx_flow -** -** Description This function is called by PAN to enable or disable -** data flow on the RX path when it is configured to use -** a push interface. If data flow is disabled the phone must -** not call bta_pan_ci_rx_write() or bta_pan_ci_rx_writebuf() -** until data flow is enabled again. -** -** -** Returns void -** -*******************************************************************************/ -void bta_pan_co_rx_flow(uint16_t handle, uint8_t app_id, bool enable) -{ - UNUSED(handle); - UNUSED(app_id); - UNUSED(enable); + * + * Function bta_pan_co_rx_flow + * + * Description This function is called by PAN to enable or disable + * data flow on the RX path when it is configured to use + * a push interface. If data flow is disabled the phone must + * not call bta_pan_ci_rx_write() or bta_pan_ci_rx_writebuf() + * until data flow is enabled again. + * + * + * Returns void + * + ******************************************************************************/ +void bta_pan_co_rx_flow(uint16_t handle, uint8_t app_id, bool enable) { + UNUSED(handle); + UNUSED(app_id); + UNUSED(enable); - BTIF_TRACE_API("bta_pan_co_rx_flow, enabled:%d, not used", enable); - btpan_conn_t* conn = btpan_find_conn_handle(handle); - if(!conn || conn->state != PAN_STATE_OPEN) - return; - btpan_set_flow_control(enable); + BTIF_TRACE_API("bta_pan_co_rx_flow, enabled:%d, not used", enable); + btpan_conn_t* conn = btpan_find_conn_handle(handle); + if (!conn || conn->state != PAN_STATE_OPEN) return; + btpan_set_flow_control(enable); } /******************************************************************************* -** -** Function bta_pan_co_filt_ind -** -** Description protocol filter indication from peer device -** -** Returns void -** -*******************************************************************************/ -void bta_pan_co_pfilt_ind(uint16_t handle, bool indication, tBTA_PAN_STATUS result, - uint16_t len, uint8_t *p_filters) -{ - UNUSED(handle); - UNUSED(indication); - UNUSED(result); - UNUSED(len); - UNUSED(p_filters); + * + * Function bta_pan_co_filt_ind + * + * Description protocol filter indication from peer device + * + * Returns void + * + ******************************************************************************/ +void bta_pan_co_pfilt_ind(uint16_t handle, bool indication, + tBTA_PAN_STATUS result, uint16_t len, + uint8_t* p_filters) { + UNUSED(handle); + UNUSED(indication); + UNUSED(result); + UNUSED(len); + UNUSED(p_filters); - BTIF_TRACE_API("bta_pan_co_pfilt_ind"); + BTIF_TRACE_API("bta_pan_co_pfilt_ind"); } /******************************************************************************* -** -** Function bta_pan_co_mfilt_ind -** -** Description multicast filter indication from peer device -** -** Returns void -** -*******************************************************************************/ -void bta_pan_co_mfilt_ind(uint16_t handle, bool indication, tBTA_PAN_STATUS result, - uint16_t len, uint8_t *p_filters) -{ - UNUSED(handle); - UNUSED(indication); - UNUSED(result); - UNUSED(len); - UNUSED(p_filters); + * + * Function bta_pan_co_mfilt_ind + * + * Description multicast filter indication from peer device + * + * Returns void + * + ******************************************************************************/ +void bta_pan_co_mfilt_ind(uint16_t handle, bool indication, + tBTA_PAN_STATUS result, uint16_t len, + uint8_t* p_filters) { + UNUSED(handle); + UNUSED(indication); + UNUSED(result); + UNUSED(len); + UNUSED(p_filters); - BTIF_TRACE_API("bta_pan_co_mfilt_ind"); + BTIF_TRACE_API("bta_pan_co_mfilt_ind"); } - diff --git a/btif/include/btif_api.h b/btif/include/btif_api.h index 455b94822..392740843 100644 --- a/btif/include/btif_api.h +++ b/btif/include/btif_api.h @@ -36,389 +36,391 @@ #include "btif_dm.h" /******************************************************************************* -** BTIF CORE API -********************************************************************************/ - -/******************************************************************************* -** -** Function btif_init_bluetooth -** -** Description Creates BTIF task and prepares BT scheduler for startup -** -** Returns bt_status_t -** -*******************************************************************************/ + * BTIF CORE API + *******************************************************************************/ + +/******************************************************************************* + * + * Function btif_init_bluetooth + * + * Description Creates BTIF task and prepares BT scheduler for startup + * + * Returns bt_status_t + * + ******************************************************************************/ bt_status_t btif_init_bluetooth(void); /******************************************************************************* -** -** Function btif_enable_bluetooth -** -** Description Performs chip power on and kickstarts OS scheduler -** -** Returns bt_status_t -** -*******************************************************************************/ + * + * Function btif_enable_bluetooth + * + * Description Performs chip power on and kickstarts OS scheduler + * + * Returns bt_status_t + * + ******************************************************************************/ bt_status_t btif_enable_bluetooth(void); /******************************************************************************* -** -** Function btif_disable_bluetooth -** -** Description Inititates shutdown of Bluetooth system. -** Any active links will be dropped and device entering -** non connectable/discoverable mode -** -** Returns void -** -*******************************************************************************/ + * + * Function btif_disable_bluetooth + * + * Description Inititates shutdown of Bluetooth system. + * Any active links will be dropped and device entering + * non connectable/discoverable mode + * + * Returns void + * + ******************************************************************************/ bt_status_t btif_disable_bluetooth(void); /******************************************************************************* -** -** Function btif_cleanup_bluetooth -** -** Description Cleanup BTIF state. -** -** -** Returns void -** -*******************************************************************************/ + * + * Function btif_cleanup_bluetooth + * + * Description Cleanup BTIF state. + * + * + * Returns void + * + ******************************************************************************/ bt_status_t btif_cleanup_bluetooth(void); /******************************************************************************* -** -** Function is_restricted_mode -** -** Description Checks if BT was enabled in restriced mode. In restricted -** mode, bonds that are created are marked as temporary. -** These bonds persist until we leave restricted mode, at -** which point they will be deleted from the config. Also -** while in restricted mode, the user can access devices -** that are already paired before entering restricted mode, -** but they cannot remove any of these devices. -** -** Returns bool -** -*******************************************************************************/ + * + * Function is_restricted_mode + * + * Description Checks if BT was enabled in restriced mode. In restricted + * mode, bonds that are created are marked as temporary. + * These bonds persist until we leave restricted mode, at + * which point they will be deleted from the config. Also + * while in restricted mode, the user can access devices + * that are already paired before entering restricted mode, + * but they cannot remove any of these devices. + * + * Returns bool + * + ******************************************************************************/ bool is_restricted_mode(void); /******************************************************************************* -** -** Function btif_get_adapter_properties -** -** Description Fetches all local adapter properties -** -** Returns bt_status_t -** -*******************************************************************************/ + * + * Function btif_get_adapter_properties + * + * Description Fetches all local adapter properties + * + * Returns bt_status_t + * + ******************************************************************************/ bt_status_t btif_get_adapter_properties(void); /******************************************************************************* -** -** Function btif_get_adapter_property -** -** Description Fetches property value from local cache -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_get_adapter_property( bt_property_type_t type); - -/******************************************************************************* -** -** Function btif_set_adapter_property -** -** Description Updates core stack with property value and stores it in -** local cache -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_set_adapter_property( const bt_property_t *property); - -/******************************************************************************* -** -** Function btif_get_remote_device_property -** -** Description Fetches the remote device property from the NVRAM -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_get_remote_device_property( bt_bdaddr_t *remote_addr, - bt_property_type_t type); - -/******************************************************************************* -** -** Function btif_get_remote_device_properties -** -** Description Fetches all the remote device properties from NVRAM -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_get_remote_device_properties( bt_bdaddr_t *remote_addr); - -/******************************************************************************* -** -** Function btif_set_remote_device_property -** -** Description Writes the remote device property to NVRAM. -** Currently, BT_PROPERTY_REMOTE_FRIENDLY_NAME is the only -** remote device property that can be set -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_set_remote_device_property( bt_bdaddr_t *remote_addr, - const bt_property_t *property); - -/******************************************************************************* -** -** Function btif_get_remote_service_record -** -** Description Looks up the service matching uuid on the remote device -** and fetches the SCN and service_name if the UUID is found -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_get_remote_service_record( bt_bdaddr_t *remote_addr, - bt_uuid_t *uuid); - - -/******************************************************************************* -** BTIF DM API -********************************************************************************/ - -/******************************************************************************* -** -** Function btif_dm_start_discovery -** -** Description Start device discovery/inquiry -** -** -** Returns bt_status_t -** -*******************************************************************************/ + * + * Function btif_get_adapter_property + * + * Description Fetches property value from local cache + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_get_adapter_property(bt_property_type_t type); + +/******************************************************************************* + * + * Function btif_set_adapter_property + * + * Description Updates core stack with property value and stores it in + * local cache + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_set_adapter_property(const bt_property_t* property); + +/******************************************************************************* + * + * Function btif_get_remote_device_property + * + * Description Fetches the remote device property from the NVRAM + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_get_remote_device_property(bt_bdaddr_t* remote_addr, + bt_property_type_t type); + +/******************************************************************************* + * + * Function btif_get_remote_device_properties + * + * Description Fetches all the remote device properties from NVRAM + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_get_remote_device_properties(bt_bdaddr_t* remote_addr); + +/******************************************************************************* + * + * Function btif_set_remote_device_property + * + * Description Writes the remote device property to NVRAM. + * Currently, BT_PROPERTY_REMOTE_FRIENDLY_NAME is the only + * remote device property that can be set + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_set_remote_device_property(bt_bdaddr_t* remote_addr, + const bt_property_t* property); + +/******************************************************************************* + * + * Function btif_get_remote_service_record + * + * Description Looks up the service matching uuid on the remote device + * and fetches the SCN and service_name if the UUID is found + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_get_remote_service_record(bt_bdaddr_t* remote_addr, + bt_uuid_t* uuid); + +/******************************************************************************* + * BTIF DM API + *******************************************************************************/ + +/******************************************************************************* + * + * Function btif_dm_start_discovery + * + * Description Start device discovery/inquiry + * + * + * Returns bt_status_t + * + ******************************************************************************/ bt_status_t btif_dm_start_discovery(void); /******************************************************************************* -** -** Function btif_dm_cancel_discovery -** -** Description Cancels search -** -** Returns bt_status_t -** -*******************************************************************************/ + * + * Function btif_dm_cancel_discovery + * + * Description Cancels search + * + * Returns bt_status_t + * + ******************************************************************************/ bt_status_t btif_dm_cancel_discovery(void); /******************************************************************************* -** -** Function btif_dm_create_bond -** -** Description Initiate bonding with the specified device -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport); - -/******************************************************************************* -** -** Function btif_dm_create_bond_out_of_band -** -** Description Initiate bonding with the specified device using OOB data. -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_create_bond_out_of_band(const bt_bdaddr_t *bd_addr, int transport, - const bt_out_of_band_data_t *oob_data); - -/******************************************************************************* -** -** Function btif_dm_cancel_bond -** -** Description Initiate bonding with the specified device -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr); - -/******************************************************************************* -** -** Function btif_dm_remove_bond -** -** Description Removes bonding with the specified device -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr); - -/******************************************************************************* -** -** Function btif_dm_get_connection_state -** -** Description Returns whether the remote device is currently connected -** -** Returns 0 if not connected -** -*******************************************************************************/ -uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr); - -/******************************************************************************* -** -** Function btif_dm_pin_reply -** -** Description BT legacy pairing - PIN code reply -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept, - uint8_t pin_len, bt_pin_code_t *pin_code); - -/******************************************************************************* -** -** Function btif_dm_passkey_reply -** -** Description BT SSP passkey reply -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_passkey_reply( const bt_bdaddr_t *bd_addr, - uint8_t accept, uint32_t passkey); - -/******************************************************************************* -** -** Function btif_dm_ssp_reply -** -** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_ssp_reply( const bt_bdaddr_t *bd_addr, + * + * Function btif_dm_create_bond + * + * Description Initiate bonding with the specified device + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_create_bond(const bt_bdaddr_t* bd_addr, int transport); + +/******************************************************************************* + * + * Function btif_dm_create_bond_out_of_band + * + * Description Initiate bonding with the specified device using OOB data. + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_create_bond_out_of_band( + const bt_bdaddr_t* bd_addr, int transport, + const bt_out_of_band_data_t* oob_data); + +/******************************************************************************* + * + * Function btif_dm_cancel_bond + * + * Description Initiate bonding with the specified device + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t* bd_addr); + +/******************************************************************************* + * + * Function btif_dm_remove_bond + * + * Description Removes bonding with the specified device + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_remove_bond(const bt_bdaddr_t* bd_addr); + +/******************************************************************************* + * + * Function btif_dm_get_connection_state + * + * Description Returns whether the remote device is currently connected + * + * Returns 0 if not connected + * + ******************************************************************************/ +uint16_t btif_dm_get_connection_state(const bt_bdaddr_t* bd_addr); + +/******************************************************************************* + * + * Function btif_dm_pin_reply + * + * Description BT legacy pairing - PIN code reply + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_pin_reply(const bt_bdaddr_t* bd_addr, uint8_t accept, + uint8_t pin_len, bt_pin_code_t* pin_code); + +/******************************************************************************* + * + * Function btif_dm_passkey_reply + * + * Description BT SSP passkey reply + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_passkey_reply(const bt_bdaddr_t* bd_addr, uint8_t accept, + uint32_t passkey); + +/******************************************************************************* + * + * Function btif_dm_ssp_reply + * + * Description BT SSP Reply - Just Works, Numeric Comparison & Passkey + *Entry + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t* bd_addr, bt_ssp_variant_t variant, uint8_t accept, uint32_t passkey); /******************************************************************************* -** -** Function btif_dm_get_adapter_property -** -** Description Queries the BTA for the adapter property -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_get_adapter_property(bt_property_t *prop); - -/******************************************************************************* -** -** Function btif_dm_get_remote_services -** -** Description Start SDP to get remote services -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr, - bt_uuid_t *uuid); - - -/******************************************************************************* -** -** Function btif_dm_get_remote_services -** -** Description Start SDP to get remote services -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr); - -/******************************************************************************* -** -** Function btif_dm_get_remote_services_by_transport -** -** Description Start SDP to get remote services by transport -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_get_remote_services_by_transport(bt_bdaddr_t *remote_addr, int transport); - -/******************************************************************************* -** -** Function btif_dut_mode_configure -** -** Description Configure Test Mode - 'enable' to 1 puts the device in test mode and 0 exits -** test mode -** -** Returns BT_STATUS_SUCCESS on success -** -*******************************************************************************/ + * + * Function btif_dm_get_adapter_property + * + * Description Queries the BTA for the adapter property + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_get_adapter_property(bt_property_t* prop); + +/******************************************************************************* + * + * Function btif_dm_get_remote_services + * + * Description Start SDP to get remote services + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t* remote_addr, + bt_uuid_t* uuid); + +/******************************************************************************* + * + * Function btif_dm_get_remote_services + * + * Description Start SDP to get remote services + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_get_remote_services(bt_bdaddr_t* remote_addr); + +/******************************************************************************* + * + * Function btif_dm_get_remote_services_by_transport + * + * Description Start SDP to get remote services by transport + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_get_remote_services_by_transport(bt_bdaddr_t* remote_addr, + int transport); + +/******************************************************************************* + * + * Function btif_dut_mode_configure + * + * Description Configure Test Mode - 'enable' to 1 puts the device in test + *mode and 0 exits + * test mode + * + * Returns BT_STATUS_SUCCESS on success + * + ******************************************************************************/ bt_status_t btif_dut_mode_configure(uint8_t enable); /******************************************************************************* -** -** Function btif_dut_mode_send -** -** Description Sends a HCI Vendor specific command to the controller -** -** Returns BT_STATUS_SUCCESS on success -** -*******************************************************************************/ -bt_status_t btif_dut_mode_send(uint16_t opcode, uint8_t *buf, uint8_t len); - -/******************************************************************************* -** -** Function btif_le_test_mode -** -** Description Sends a HCI BLE Test command to the Controller -** -** Returns BT_STATUS_SUCCESS on success -** -*******************************************************************************/ -bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len); - -/******************************************************************************* -** -** Function btif_dm_read_energy_info -** -** Description Reads the energy info from controller -** -** Returns void -** -*******************************************************************************/ + * + * Function btif_dut_mode_send + * + * Description Sends a HCI Vendor specific command to the controller + * + * Returns BT_STATUS_SUCCESS on success + * + ******************************************************************************/ +bt_status_t btif_dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len); + +/******************************************************************************* + * + * Function btif_le_test_mode + * + * Description Sends a HCI BLE Test command to the Controller + * + * Returns BT_STATUS_SUCCESS on success + * + ******************************************************************************/ +bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len); + +/******************************************************************************* + * + * Function btif_dm_read_energy_info + * + * Description Reads the energy info from controller + * + * Returns void + * + ******************************************************************************/ void btif_dm_read_energy_info(); /******************************************************************************* -** -** Function btif_config_hci_snoop_log -** -** Description enable or disable HCI snoop log -** -** Returns BT_STATUS_SUCCESS on success -** -*******************************************************************************/ + * + * Function btif_config_hci_snoop_log + * + * Description enable or disable HCI snoop log + * + * Returns BT_STATUS_SUCCESS on success + * + ******************************************************************************/ bt_status_t btif_config_hci_snoop_log(uint8_t enable); /******************************************************************************* -** -** Function btif_debug_bond_event_dump -** -** Description Dump bond event information -** -** Returns void -** -*******************************************************************************/ + * + * Function btif_debug_bond_event_dump + * + * Description Dump bond event information + * + * Returns void + * + ******************************************************************************/ void btif_debug_bond_event_dump(int fd); #endif /* BTIF_API_H */ diff --git a/btif/include/btif_av.h b/btif/include/btif_av.h index ebb2daa9b..02f8521c4 100644 --- a/btif/include/btif_av.h +++ b/btif/include/btif_av.h @@ -28,164 +28,163 @@ #ifndef BTIF_AV_H #define BTIF_AV_H +#include "bta_av_api.h" #include "btif_common.h" #include "btif_sm.h" -#include "bta_av_api.h" /******************************************************************************* -** Type definitions for callback functions -********************************************************************************/ + * Type definitions for callback functions + *******************************************************************************/ typedef enum { - /* Reuse BTA_AV_XXX_EVT - No need to redefine them here */ - BTIF_AV_CONNECT_REQ_EVT = BTA_AV_MAX_EVT, - BTIF_AV_DISCONNECT_REQ_EVT, - BTIF_AV_START_STREAM_REQ_EVT, - BTIF_AV_STOP_STREAM_REQ_EVT, - BTIF_AV_SUSPEND_STREAM_REQ_EVT, - BTIF_AV_SINK_CONFIG_REQ_EVT, - BTIF_AV_OFFLOAD_START_REQ_EVT, - BTIF_AV_CLEANUP_REQ_EVT, + /* Reuse BTA_AV_XXX_EVT - No need to redefine them here */ + BTIF_AV_CONNECT_REQ_EVT = BTA_AV_MAX_EVT, + BTIF_AV_DISCONNECT_REQ_EVT, + BTIF_AV_START_STREAM_REQ_EVT, + BTIF_AV_STOP_STREAM_REQ_EVT, + BTIF_AV_SUSPEND_STREAM_REQ_EVT, + BTIF_AV_SINK_CONFIG_REQ_EVT, + BTIF_AV_OFFLOAD_START_REQ_EVT, + BTIF_AV_CLEANUP_REQ_EVT, } btif_av_sm_event_t; - /******************************************************************************* -** BTIF AV API -********************************************************************************/ + * BTIF AV API + *******************************************************************************/ /******************************************************************************* -** -** Function btif_av_get_addr -** -** Description Fetches current AV BD address -** -** Returns BD address -** -*******************************************************************************/ + * + * Function btif_av_get_addr + * + * Description Fetches current AV BD address + * + * Returns BD address + * + ******************************************************************************/ bt_bdaddr_t btif_av_get_addr(void); /******************************************************************************* -** Function btif_av_is_sink_enabled -** -** Description Checks if A2DP Sink is enabled or not -** -** Returns true if A2DP Sink is enabled, false otherwise -** -*******************************************************************************/ + * Function btif_av_is_sink_enabled + * + * Description Checks if A2DP Sink is enabled or not + * + * Returns true if A2DP Sink is enabled, false otherwise + * + ******************************************************************************/ bool btif_av_is_sink_enabled(void); /******************************************************************************* -** -** Function btif_av_stream_ready -** -** Description Checks whether AV is ready for starting a stream -** -** Returns None -** -*******************************************************************************/ + * + * Function btif_av_stream_ready + * + * Description Checks whether AV is ready for starting a stream + * + * Returns None + * + ******************************************************************************/ bool btif_av_stream_ready(void); /******************************************************************************* -** -** Function btif_av_stream_started_ready -** -** Description Checks whether AV ready for media start in streaming state -** -** Returns None -** -*******************************************************************************/ + * + * Function btif_av_stream_started_ready + * + * Description Checks whether AV ready for media start in streaming state + * + * Returns None + * + ******************************************************************************/ bool btif_av_stream_started_ready(void); /******************************************************************************* -** -** Function btif_dispatch_sm_event -** -** Description Send event to AV statemachine -** -** Returns None -** -*******************************************************************************/ + * + * Function btif_dispatch_sm_event + * + * Description Send event to AV statemachine + * + * Returns None + * + ******************************************************************************/ /* used to pass events to AV statemachine from other tasks */ -void btif_dispatch_sm_event(btif_av_sm_event_t event, void *p_data, int len); +void btif_dispatch_sm_event(btif_av_sm_event_t event, void* p_data, int len); /******************************************************************************* -** -** Function btif_av_init -** -** Description Initializes btif AV if not already done -** -** Returns bt_status_t -** -*******************************************************************************/ + * + * Function btif_av_init + * + * Description Initializes btif AV if not already done + * + * Returns bt_status_t + * + ******************************************************************************/ bt_status_t btif_av_init(int service_id); /******************************************************************************* -** -** Function btif_av_is_connected -** -** Description Checks if av has a connected sink -** -** Returns bool -** -*******************************************************************************/ + * + * Function btif_av_is_connected + * + * Description Checks if av has a connected sink + * + * Returns bool + * + ******************************************************************************/ bool btif_av_is_connected(void); /******************************************************************************* -** -** Function btif_av_get_peer_sep -** -** Description Get the stream endpoint type. -** -** Returns The stream endpoint type: either AVDT_TSEP_SRC or -** AVDT_TSEP_SNK. -** -*******************************************************************************/ + * + * Function btif_av_get_peer_sep + * + * Description Get the stream endpoint type. + * + * Returns The stream endpoint type: either AVDT_TSEP_SRC or + * AVDT_TSEP_SNK. + * + ******************************************************************************/ uint8_t btif_av_get_peer_sep(void); /******************************************************************************* -** -** Function btif_av_is_peer_edr -** -** Description Check if the connected a2dp device supports -** EDR or not. Only when connected this function -** will accurately provide a true capability of -** remote peer. If not connected it will always be false. -** -** Returns true if remote device is capable of EDR -** -*******************************************************************************/ + * + * Function btif_av_is_peer_edr + * + * Description Check if the connected a2dp device supports + * EDR or not. Only when connected this function + * will accurately provide a true capability of + * remote peer. If not connected it will always be false. + * + * Returns true if remote device is capable of EDR + * + ******************************************************************************/ bool btif_av_is_peer_edr(void); /****************************************************************************** -** -** Function btif_av_clear_remote_suspend_flag -** -** Description Clears remote suspended flag -** -** Returns Void -********************************************************************************/ + * + * Function btif_av_clear_remote_suspend_flag + * + * Description Clears remote suspended flag + * + * Returns Void + *******************************************************************************/ void btif_av_clear_remote_suspend_flag(void); /******************************************************************************* -** -** Function btif_av_peer_supports_3mbps -** -** Description Check if the connected A2DP device supports -** 3 Mbps EDR. This function will only work while connected. -** If not connected it will always return false. -** -** Returns true if remote device is EDR and supports 3 Mbps -** -*******************************************************************************/ + * + * Function btif_av_peer_supports_3mbps + * + * Description Check if the connected A2DP device supports + * 3 Mbps EDR. This function will only work while connected. + * If not connected it will always return false. + * + * Returns true if remote device is EDR and supports 3 Mbps + * + ******************************************************************************/ bool btif_av_peer_supports_3mbps(void); #endif /* BTIF_AV_H */ diff --git a/btif/include/btif_av_co.h b/btif/include/btif_av_co.h index 3f02af2e8..f6e3a2daf 100644 --- a/btif/include/btif_av_co.h +++ b/btif/include/btif_av_co.h @@ -23,16 +23,16 @@ #include "stack/include/a2dp_api.h" /******************************************************************************* -** Constants & Macros -********************************************************************************/ + * Constants & Macros + *******************************************************************************/ #ifdef __cplusplus extern "C" { #endif /******************************************************************************* -** Functions -********************************************************************************/ + * Functions + *******************************************************************************/ /******************************************************************************* ** @@ -46,23 +46,24 @@ extern "C" { ** Returns true if successful, false otherwise ** *******************************************************************************/ -bool bta_av_co_audio_set_codec(const tA2DP_FEEDING_PARAMS *p_feeding_params); +bool bta_av_co_audio_set_codec(const tA2DP_FEEDING_PARAMS* p_feeding_params); // Prepares a message to initialize the encoder. The prepared message is // stored in |p_init_params|. // |p_init_params| cannot be null. -void bta_av_co_audio_encoder_init(tA2DP_ENCODER_INIT_PARAMS *p_init_params); +void bta_av_co_audio_encoder_init(tA2DP_ENCODER_INIT_PARAMS* p_init_params); // Prepares a message to update the encoder. The prepared message is // stored in |p_update_params|. // |p_update_params| cannot be null. -void bta_av_co_audio_encoder_update(tA2DP_ENCODER_UPDATE_PARAMS *p_update_params); +void bta_av_co_audio_encoder_update( + tA2DP_ENCODER_UPDATE_PARAMS* p_update_params); // Gets the current A2DP encoder interface that can be used to encode and // prepare A2DP packets for transmission - see |tA2DP_ENCODER_INTERFACE|. // Returns the A2DP encoder interface if the current codec is setup, // otherwise NULL. -const tA2DP_ENCODER_INTERFACE *bta_av_co_get_encoder_interface(void); +const tA2DP_ENCODER_INTERFACE* bta_av_co_get_encoder_interface(void); /******************************************************************************* ** diff --git a/btif/include/btif_avrcp_audio_track.h b/btif/include/btif_avrcp_audio_track.h index b6b539ab2..6986e4344 100644 --- a/btif/include/btif_avrcp_audio_track.h +++ b/btif/include/btif_avrcp_audio_track.h @@ -27,10 +27,12 @@ */ /** - * Creates an audio track object and returns a void handle. Use this handle to the + * Creates an audio track object and returns a void handle. Use this handle to + * the * following functions. * - * The ownership of the handle is maintained by the caller of this API and it should eventually be + * The ownership of the handle is maintained by the caller of this API and it + * should eventually be * deleted using BtifAvrcpAudioTrackDelete (see below). */ void* BtifAvrcpAudioTrackCreate(int trackFreq, int channelType); @@ -38,28 +40,29 @@ void* BtifAvrcpAudioTrackCreate(int trackFreq, int channelType); /** * Starts the audio track. */ -void BtifAvrcpAudioTrackStart(void *handle); +void BtifAvrcpAudioTrackStart(void* handle); /** * Pauses the audio track. */ -void BtifAvrcpAudioTrackPause(void *handle); +void BtifAvrcpAudioTrackPause(void* handle); /** * Sets audio track gain. */ -void BtifAvrcpSetAudioTrackGain(void *handle, float gain); +void BtifAvrcpSetAudioTrackGain(void* handle, float gain); /** * Stop / Delete the audio track. * Delete should usually be called stop. */ -void BtifAvrcpAudioTrackStop(void *handle); -void BtifAvrcpAudioTrackDelete(void *handle); +void BtifAvrcpAudioTrackStop(void* handle); +void BtifAvrcpAudioTrackDelete(void* handle); /** * Writes the audio track data to file. * * Used only for debugging. */ -int BtifAvrcpAudioTrackWriteData(void *handle, void *audioBuffer, int bufferlen); +int BtifAvrcpAudioTrackWriteData(void* handle, void* audioBuffer, + int bufferlen); diff --git a/btif/include/btif_common.h b/btif/include/btif_common.h index c53c8e576..3de3420f8 100644 --- a/btif/include/btif_common.h +++ b/btif/include/btif_common.h @@ -24,24 +24,27 @@ #include -#include "osi/include/osi.h" -#include "osi/include/log.h" #include "bt_types.h" #include "bta_api.h" +#include "osi/include/log.h" +#include "osi/include/osi.h" /******************************************************************************* -** Constants & Macros -********************************************************************************/ + * Constants & Macros + *******************************************************************************/ -#define ASSERTC(cond, msg, val) if (!(cond)) { LOG_ERROR(LOG_TAG, \ - "### ASSERT : %s %s line %d %s (%d) ###", __FILE__, __func__, __LINE__, msg, val);} +#define ASSERTC(cond, msg, val) \ + if (!(cond)) { \ + LOG_ERROR(LOG_TAG, "### ASSERT : %s %s line %d %s (%d) ###", __FILE__, \ + __func__, __LINE__, msg, val); \ + } /* Calculate start of event enumeration; id is top 8 bits of event */ -#define BTIF_SIG_START(id) ((id) << 8) +#define BTIF_SIG_START(id) ((id) << 8) /* For upstream the MSB bit is always SET */ -#define BTIF_SIG_CB_BIT (0x8000) -#define BTIF_SIG_CB_START(id) (((id) << 8) | BTIF_SIG_CB_BIT) +#define BTIF_SIG_CB_BIT (0x8000) +#define BTIF_SIG_CB_START(id) (((id) << 8) | BTIF_SIG_CB_BIT) /* * A memcpy(3) wrapper when copying memory that might not be aligned. @@ -57,55 +60,53 @@ * problematic "char *" to struct pointer casting, and this macro itself should * be removed. */ -#define maybe_non_aligned_memcpy(_a, _b, _c) memcpy((void *)(_a), (_b), (_c)) +#define maybe_non_aligned_memcpy(_a, _b, _c) memcpy((void*)(_a), (_b), (_c)) /* BTIF sub-systems */ -#define BTIF_CORE 0 -#define BTIF_DM 1 -#define BTIF_HFP 2 -#define BTIF_AV 3 -#define BTIF_PAN 4 -#define BTIF_HF_CLIENT 5 - -extern bt_callbacks_t *bt_hal_cbacks; - -#define HAL_CBACK(P_CB, P_CBACK, ...)\ - if ((P_CB) && (P_CB)->P_CBACK) { \ - BTIF_TRACE_API("HAL %s->%s", #P_CB, #P_CBACK); \ - (P_CB)->P_CBACK(__VA_ARGS__); \ - } \ - else { \ - ASSERTC(0, "Callback is NULL", 0); \ - } +#define BTIF_CORE 0 +#define BTIF_DM 1 +#define BTIF_HFP 2 +#define BTIF_AV 3 +#define BTIF_PAN 4 +#define BTIF_HF_CLIENT 5 + +extern bt_callbacks_t* bt_hal_cbacks; + +#define HAL_CBACK(P_CB, P_CBACK, ...) \ + if ((P_CB) && (P_CB)->P_CBACK) { \ + BTIF_TRACE_API("HAL %s->%s", #P_CB, #P_CBACK); \ + (P_CB)->P_CBACK(__VA_ARGS__); \ + } else { \ + ASSERTC(0, "Callback is NULL", 0); \ + } /** * BTIF events for requests that require context switch to btif task * on downstreams path */ -enum -{ - BTIF_CORE_API_START = BTIF_SIG_START(BTIF_CORE), - BTIF_CORE_STORAGE_NO_ACTION, - BTIF_CORE_STORAGE_ADAPTER_WRITE, - BTIF_CORE_STORAGE_ADAPTER_READ, - BTIF_CORE_STORAGE_ADAPTER_READ_ALL, - BTIF_CORE_STORAGE_REMOTE_WRITE, - BTIF_CORE_STORAGE_REMOTE_READ, - BTIF_CORE_STORAGE_REMOTE_READ_ALL, - BTIF_CORE_STORAGE_READ_ALL, - BTIF_CORE_STORAGE_NOTIFY_STATUS, - /* add here */ - - BTIF_DM_API_START = BTIF_SIG_START(BTIF_DM), - BTIF_DM_ENABLE_SERVICE, - BTIF_DM_DISABLE_SERVICE, - /* add here */ - - BTIF_HFP_API_START = BTIF_SIG_START(BTIF_HFP), - /* add here */ - - BTIF_AV_API_START = BTIF_SIG_START(BTIF_AV), - /* add here */ +enum { + BTIF_CORE_API_START = BTIF_SIG_START(BTIF_CORE), + BTIF_CORE_STORAGE_NO_ACTION, + BTIF_CORE_STORAGE_ADAPTER_WRITE, + BTIF_CORE_STORAGE_ADAPTER_READ, + BTIF_CORE_STORAGE_ADAPTER_READ_ALL, + BTIF_CORE_STORAGE_REMOTE_WRITE, + BTIF_CORE_STORAGE_REMOTE_READ, + BTIF_CORE_STORAGE_REMOTE_READ_ALL, + BTIF_CORE_STORAGE_READ_ALL, + BTIF_CORE_STORAGE_NOTIFY_STATUS, + /* add here */ + + BTIF_DM_API_START = BTIF_SIG_START(BTIF_DM), + BTIF_DM_ENABLE_SERVICE, + BTIF_DM_DISABLE_SERVICE, + /* add here */ + + BTIF_HFP_API_START = BTIF_SIG_START(BTIF_HFP), + /* add here */ + + BTIF_AV_API_START = BTIF_SIG_START(BTIF_AV), + /* add here */ }; /** @@ -113,29 +114,31 @@ enum * on upstream path - Typically these would be non-BTA events * that are generated by the BTIF layer. */ -enum -{ - BTIF_CORE_CB_START = BTIF_SIG_CB_START(BTIF_CORE), - /* add here */ - - BTIF_DM_CB_START = BTIF_SIG_CB_START(BTIF_DM), - BTIF_DM_CB_DISCOVERY_STARTED, /* Discovery has started */ - BTIF_DM_CB_CREATE_BOND, /* Create bond */ - BTIF_DM_CB_REMOVE_BOND, /*Remove bond */ - BTIF_DM_CB_HID_REMOTE_NAME, /* Remote name callback for HID device */ - BTIF_DM_CB_BOND_STATE_BONDING, - BTIF_DM_CB_LE_TX_TEST, /* BLE Tx Test command complete callback */ - BTIF_DM_CB_LE_RX_TEST, /* BLE Rx Test command complete callback */ - BTIF_DM_CB_LE_TEST_END, /* BLE Test mode end callback */ - - BTIF_HFP_CB_START = BTIF_SIG_CB_START(BTIF_HFP), - BTIF_HFP_CB_AUDIO_CONNECTING, /* HF AUDIO connect has been sent to BTA successfully */ - - BTIF_PAN_CB_START = BTIF_SIG_CB_START(BTIF_PAN), - BTIF_PAN_CB_DISCONNECTING, /* PAN Disconnect has been sent to BTA successfully */ - - BTIF_HF_CLIENT_CLIENT_CB_START = BTIF_SIG_CB_START(BTIF_HF_CLIENT), - BTIF_HF_CLIENT_CB_AUDIO_CONNECTING, /* AUDIO connect has been sent to BTA successfully */ +enum { + BTIF_CORE_CB_START = BTIF_SIG_CB_START(BTIF_CORE), + /* add here */ + + BTIF_DM_CB_START = BTIF_SIG_CB_START(BTIF_DM), + BTIF_DM_CB_DISCOVERY_STARTED, /* Discovery has started */ + BTIF_DM_CB_CREATE_BOND, /* Create bond */ + BTIF_DM_CB_REMOVE_BOND, /*Remove bond */ + BTIF_DM_CB_HID_REMOTE_NAME, /* Remote name callback for HID device */ + BTIF_DM_CB_BOND_STATE_BONDING, + BTIF_DM_CB_LE_TX_TEST, /* BLE Tx Test command complete callback */ + BTIF_DM_CB_LE_RX_TEST, /* BLE Rx Test command complete callback */ + BTIF_DM_CB_LE_TEST_END, /* BLE Test mode end callback */ + + BTIF_HFP_CB_START = BTIF_SIG_CB_START(BTIF_HFP), + BTIF_HFP_CB_AUDIO_CONNECTING, /* HF AUDIO connect has been sent to BTA + successfully */ + + BTIF_PAN_CB_START = BTIF_SIG_CB_START(BTIF_PAN), + BTIF_PAN_CB_DISCONNECTING, /* PAN Disconnect has been sent to BTA successfully + */ + + BTIF_HF_CLIENT_CLIENT_CB_START = BTIF_SIG_CB_START(BTIF_HF_CLIENT), + BTIF_HF_CLIENT_CB_AUDIO_CONNECTING, /* AUDIO connect has been sent to BTA + successfully */ }; /* Macro definitions for BD ADDR persistence */ @@ -145,7 +148,7 @@ enum * The property key stores the storage location of Bluetooth Device Address */ #ifndef PROPERTY_BT_BDADDR_PATH -#define PROPERTY_BT_BDADDR_PATH "ro.bt.bdaddr_path" +#define PROPERTY_BT_BDADDR_PATH "ro.bt.bdaddr_path" #endif /** @@ -154,7 +157,7 @@ enum * generating a random BDADDR and keeping it in the PERSIST_BDADDR_DROP. */ #ifndef PERSIST_BDADDR_PROPERTY -#define PERSIST_BDADDR_PROPERTY "persist.service.bdroid.bdaddr" +#define PERSIST_BDADDR_PROPERTY "persist.service.bdroid.bdaddr" #endif /** @@ -164,39 +167,35 @@ enum * PERSIST_BDADDR_PROPERTY use a factory set address */ #ifndef FACTORY_BT_ADDR_PROPERTY -#define FACTORY_BT_ADDR_PROPERTY "ro.boot.btmacaddr" +#define FACTORY_BT_ADDR_PROPERTY "ro.boot.btmacaddr" #endif -#define FACTORY_BT_BDADDR_STORAGE_LEN 17 - +#define FACTORY_BT_BDADDR_STORAGE_LEN 17 /******************************************************************************* -** Type definitions for callback functions -********************************************************************************/ - -typedef void (tBTIF_CBACK) (uint16_t event, char *p_param); -typedef void (tBTIF_COPY_CBACK) (uint16_t event, char *p_dest, char *p_src); + * Type definitions for callback functions + *******************************************************************************/ +typedef void(tBTIF_CBACK)(uint16_t event, char* p_param); +typedef void(tBTIF_COPY_CBACK)(uint16_t event, char* p_dest, char* p_src); /******************************************************************************* -** Type definitions and return values -********************************************************************************/ + * Type definitions and return values + *******************************************************************************/ /* this type handles all btif context switches between BTU and HAL */ -typedef struct -{ - BT_HDR hdr; - tBTIF_CBACK* p_cb; /* context switch callback */ - - /* parameters passed to callback */ - uint16_t event; /* message event id */ - char __attribute__ ((aligned)) p_param[]; /* parameter area needs to be last */ -} tBTIF_CONTEXT_SWITCH_CBACK; +typedef struct { + BT_HDR hdr; + tBTIF_CBACK* p_cb; /* context switch callback */ + /* parameters passed to callback */ + uint16_t event; /* message event id */ + char __attribute__((aligned)) p_param[]; /* parameter area needs to be last */ +} tBTIF_CONTEXT_SWITCH_CBACK; /******************************************************************************* -** Functions -********************************************************************************/ + * Functions + *******************************************************************************/ tBTA_SERVICE_MASK btif_get_enabled_services_mask(void); bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id); @@ -208,16 +207,16 @@ int btif_is_enabled(void); */ void btif_enable_bluetooth_evt(tBTA_STATUS status); void btif_disable_bluetooth_evt(void); -void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props, bt_property_t *p_props); -void btif_remote_properties_evt(bt_status_t status, bt_bdaddr_t *remote_addr, - uint32_t num_props, bt_property_t *p_props); - +void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props, + bt_property_t* p_props); +void btif_remote_properties_evt(bt_status_t status, bt_bdaddr_t* remote_addr, + uint32_t num_props, bt_property_t* p_props); #ifdef __cplusplus extern "C" { #endif -void bte_load_did_conf(const char *p_path); +void bte_load_did_conf(const char* p_path); void bte_main_boot_entry(void); void bte_main_enable(void); void bte_main_disable(void); @@ -227,10 +226,11 @@ void bte_main_enable_lpm(bool enable); #endif void bte_main_postload_cfg(void); -bt_status_t btif_transfer_context (tBTIF_CBACK *p_cback, uint16_t event, char* p_params, - int param_len, tBTIF_COPY_CBACK *p_copy_cback); +bt_status_t btif_transfer_context(tBTIF_CBACK* p_cback, uint16_t event, + char* p_params, int param_len, + tBTIF_COPY_CBACK* p_copy_cback); -void btif_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param); +void btif_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char* p_param); #ifdef __cplusplus } diff --git a/btif/include/btif_config.h b/btif/include/btif_config.h index 495e9386e..5d9ea3386 100644 --- a/btif/include/btif_config.h +++ b/btif/include/btif_config.h @@ -27,29 +27,35 @@ static const char BTIF_CONFIG_MODULE[] = "btif_config_module"; typedef struct btif_config_section_iter_t btif_config_section_iter_t; -bool btif_config_has_section(const char *section); -bool btif_config_exist(const char *section, const char *key); -bool btif_config_get_int(const char *section, const char *key, int *value); -bool btif_config_set_int(const char *section, const char *key, int value); -bool btif_config_get_str(const char *section, const char *key, char *value, int *size_bytes); -bool btif_config_set_str(const char *section, const char *key, const char *value); -bool btif_config_get_bin(const char *section, const char *key, uint8_t *value, size_t *length); -bool btif_config_set_bin(const char *section, const char *key, const uint8_t *value, size_t length); -bool btif_config_remove(const char *section, const char *key); - -size_t btif_config_get_bin_length(const char *section, const char *key); - -const btif_config_section_iter_t *btif_config_section_begin(void); -const btif_config_section_iter_t *btif_config_section_end(void); -const btif_config_section_iter_t *btif_config_section_next(const btif_config_section_iter_t *section); -const char *btif_config_section_name(const btif_config_section_iter_t *section); +bool btif_config_has_section(const char* section); +bool btif_config_exist(const char* section, const char* key); +bool btif_config_get_int(const char* section, const char* key, int* value); +bool btif_config_set_int(const char* section, const char* key, int value); +bool btif_config_get_str(const char* section, const char* key, char* value, + int* size_bytes); +bool btif_config_set_str(const char* section, const char* key, + const char* value); +bool btif_config_get_bin(const char* section, const char* key, uint8_t* value, + size_t* length); +bool btif_config_set_bin(const char* section, const char* key, + const uint8_t* value, size_t length); +bool btif_config_remove(const char* section, const char* key); + +size_t btif_config_get_bin_length(const char* section, const char* key); + +const btif_config_section_iter_t* btif_config_section_begin(void); +const btif_config_section_iter_t* btif_config_section_end(void); +const btif_config_section_iter_t* btif_config_section_next( + const btif_config_section_iter_t* section); +const char* btif_config_section_name(const btif_config_section_iter_t* section); void btif_config_save(void); void btif_config_flush(void); bool btif_config_clear(void); -// TODO(zachoverflow): Eww...we need to move these out. These are peer specific, not config general. -bool btif_get_address_type(const BD_ADDR bd_addr, int *p_addr_type); -bool btif_get_device_type(const BD_ADDR bd_addr, int *p_device_type); +// TODO(zachoverflow): Eww...we need to move these out. These are peer specific, +// not config general. +bool btif_get_address_type(const BD_ADDR bd_addr, int* p_addr_type); +bool btif_get_device_type(const BD_ADDR bd_addr, int* p_device_type); void btif_debug_config_dump(int fd); diff --git a/btif/include/btif_config_transcode.h b/btif/include/btif_config_transcode.h index e020ba631..66aac8abc 100644 --- a/btif/include/btif_config_transcode.h +++ b/btif/include/btif_config_transcode.h @@ -20,4 +20,4 @@ typedef struct config_t config_t; -config_t *btif_config_transcode(const char *xml_filename); +config_t* btif_config_transcode(const char* xml_filename); diff --git a/btif/include/btif_debug_conn.h b/btif/include/btif_debug_conn.h index bf1bd6aa5..1823b8ac0 100644 --- a/btif/include/btif_debug_conn.h +++ b/btif/include/btif_debug_conn.h @@ -28,7 +28,8 @@ typedef enum { } btif_debug_conn_state_t; // Report a connection state change -void btif_debug_conn_state(const bt_bdaddr_t bda, const btif_debug_conn_state_t state, - const tGATT_DISCONN_REASON disconnect_reason); +void btif_debug_conn_state(const bt_bdaddr_t bda, + const btif_debug_conn_state_t state, + const tGATT_DISCONN_REASON disconnect_reason); void btif_debug_conn_dump(int fd); diff --git a/btif/include/btif_dm.h b/btif/include/btif_dm.h index ed6c77ae6..b517e86b6 100644 --- a/btif/include/btif_dm.h +++ b/btif/include/btif_dm.h @@ -20,19 +20,19 @@ #define BTIF_DM_H #include "bta_api.h" -#include "btif_uid.h" #include "bte_appl.h" +#include "btif_uid.h" /************************************************************************************ -** Functions -********************************************************************************/ + * Functions + *******************************************************************************/ void btif_dm_init(uid_set_t* set); void btif_dm_cleanup(void); /** * BTIF callback to switch context from bte to btif */ -void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data); +void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC* p_data); /** * Notify BT disable being initiated. DM may chose to abort @@ -43,66 +43,68 @@ void btif_dm_on_disable(void); /** * Callout for handling io_capabilities request */ -void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data, - tBTA_AUTH_REQ *p_auth_req, bool is_orig); +void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP* p_io_cap, + tBTA_OOB_DATA* p_oob_data, tBTA_AUTH_REQ* p_auth_req, + bool is_orig); /** * Callout for handling io_capabilities response */ void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap, - tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req); + tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req); /** * Out-of-band functions */ -void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data); -void btif_dm_set_oob_for_le_io_req(BD_ADDR bd_addr, tBTA_OOB_DATA *p_oob_data, - tBTA_LE_AUTH_REQ *p_auth_req); +void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA* p_oob_data); +void btif_dm_set_oob_for_le_io_req(BD_ADDR bd_addr, tBTA_OOB_DATA* p_oob_data, + tBTA_LE_AUTH_REQ* p_auth_req); #ifdef BTIF_DM_OOB_TEST void btif_dm_load_local_oob(void); void btif_dm_proc_loc_oob(bool valid, BT_OCTET16 c, BT_OCTET16 r); -bool btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r); +bool btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r); #endif /* BTIF_DM_OOB_TEST */ #if (BLE_INCLUDED == TRUE) /*callout for reading SMP properties from Text file*/ bool btif_dm_get_smp_config(tBTE_APPL_CFG* p_cfg); -typedef struct -{ - bool is_penc_key_rcvd; - tBTM_LE_PENC_KEYS penc_key; /* received peer encryption key */ - bool is_pcsrk_key_rcvd; - tBTM_LE_PCSRK_KEYS pcsrk_key; /* received peer device SRK */ - bool is_pid_key_rcvd; - tBTM_LE_PID_KEYS pid_key; /* peer device ID key */ - bool is_lenc_key_rcvd; - tBTM_LE_LENC_KEYS lenc_key; /* local encryption reproduction keys LTK = = d1(ER,DIV,0)*/ - bool is_lcsrk_key_rcvd; - tBTM_LE_LCSRK_KEYS lcsrk_key; /* local device CSRK = d1(ER,DIV,1)*/ - bool is_lidk_key_rcvd; /* local identity key received */ +typedef struct { + bool is_penc_key_rcvd; + tBTM_LE_PENC_KEYS penc_key; /* received peer encryption key */ + bool is_pcsrk_key_rcvd; + tBTM_LE_PCSRK_KEYS pcsrk_key; /* received peer device SRK */ + bool is_pid_key_rcvd; + tBTM_LE_PID_KEYS pid_key; /* peer device ID key */ + bool is_lenc_key_rcvd; + tBTM_LE_LENC_KEYS + lenc_key; /* local encryption reproduction keys LTK = = d1(ER,DIV,0)*/ + bool is_lcsrk_key_rcvd; + tBTM_LE_LCSRK_KEYS lcsrk_key; /* local device CSRK = d1(ER,DIV,1)*/ + bool is_lidk_key_rcvd; /* local identity key received */ } btif_dm_ble_cb_t; -#define BTIF_DM_LE_KEY_PENC BTA_LE_KEY_PENC -#define BTIF_DM_LE_KEY_PID BTA_LE_KEY_PID -#define BTIF_DM_LE_KEY_PCSRK BTA_LE_KEY_PCSRK -#define BTIF_DM_LE_KEY_LENC BTA_LE_KEY_LENC -#define BTIF_DM_LE_KEY_LID BTA_LE_KEY_LID -#define BTIF_DM_LE_KEY_LCSRK BTA_LE_KEY_LCSRK +#define BTIF_DM_LE_KEY_PENC BTA_LE_KEY_PENC +#define BTIF_DM_LE_KEY_PID BTA_LE_KEY_PID +#define BTIF_DM_LE_KEY_PCSRK BTA_LE_KEY_PCSRK +#define BTIF_DM_LE_KEY_LENC BTA_LE_KEY_LENC +#define BTIF_DM_LE_KEY_LID BTA_LE_KEY_LID +#define BTIF_DM_LE_KEY_LCSRK BTA_LE_KEY_LCSRK -#define BTIF_DM_LE_LOCAL_KEY_IR (1<<0) -#define BTIF_DM_LE_LOCAL_KEY_IRK (1<<1) -#define BTIF_DM_LE_LOCAL_KEY_DHK (1<<2) -#define BTIF_DM_LE_LOCAL_KEY_ER (1<<3) +#define BTIF_DM_LE_LOCAL_KEY_IR (1 << 0) +#define BTIF_DM_LE_LOCAL_KEY_IRK (1 << 1) +#define BTIF_DM_LE_LOCAL_KEY_DHK (1 << 2) +#define BTIF_DM_LE_LOCAL_KEY_ER (1 << 3) void btif_dm_load_ble_local_keys(void); -void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er, - tBTA_BLE_LOCAL_ID_KEYS *p_id_keys); +void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK* p_key_mask, + BT_OCTET16 er, + tBTA_BLE_LOCAL_ID_KEYS* p_id_keys); void btif_dm_save_ble_bonding_keys(void); void btif_dm_remove_ble_bonding_keys(void); -void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req); +void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ* p_ble_req); -void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name, - tBT_DEVICE_TYPE dev_type); +void btif_dm_update_ble_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name, + tBT_DEVICE_TYPE dev_type); #endif /* BLE_INCLUDED */ diff --git a/btif/include/btif_gatt.h b/btif/include/btif_gatt.h index 3596df22c..3ddff4305 100644 --- a/btif/include/btif_gatt.h +++ b/btif/include/btif_gatt.h @@ -16,14 +16,13 @@ * ******************************************************************************/ - /***************************************************************************** -** -** Name: btif_gatt.h -** -** Description: -** -******************************************************************************/ + * + * Name: btif_gatt.h + * + * Description: + * + *****************************************************************************/ #ifndef BTIF_GATT_H #define BTIF_GATT_H @@ -33,4 +32,3 @@ extern const btgatt_server_interface_t btgattServerInterface; BleAdvertiserInterface* get_ble_advertiser_instance(); #endif - diff --git a/btif/include/btif_gatt_util.h b/btif/include/btif_gatt_util.h index 0f2cf645a..4a8d41231 100644 --- a/btif/include/btif_gatt_util.h +++ b/btif/include/btif_gatt_util.h @@ -24,18 +24,19 @@ #include "bta/include/bta_gatt_api.h" -void btif_to_bta_uuid(tBT_UUID *p_dest, const bt_uuid_t *p_src); -void btif_to_bta_response(tBTA_GATTS_RSP *p_dest, btgatt_response_t* p_src); -void btif_to_bta_uuid_mask(tBTA_DM_BLE_PF_COND_MASK *p_mask, const bt_uuid_t *p_src, const bt_uuid_t *svc_uuid); +void btif_to_bta_uuid(tBT_UUID* p_dest, const bt_uuid_t* p_src); +void btif_to_bta_response(tBTA_GATTS_RSP* p_dest, btgatt_response_t* p_src); +void btif_to_bta_uuid_mask(tBTA_DM_BLE_PF_COND_MASK* p_mask, + const bt_uuid_t* p_src, const bt_uuid_t* svc_uuid); -void bta_to_btif_uuid(bt_uuid_t *p_dest, tBT_UUID *p_src); +void bta_to_btif_uuid(bt_uuid_t* p_dest, tBT_UUID* p_src); -uint16_t set_read_value(btgatt_read_params_t *p_dest, tBTA_GATTC_READ *p_src); -uint16_t get_uuid16(tBT_UUID *p_uuid); +uint16_t set_read_value(btgatt_read_params_t* p_dest, tBTA_GATTC_READ* p_src); +uint16_t get_uuid16(tBT_UUID* p_uuid); -void btif_gatt_check_encrypted_link(BD_ADDR bd_addr, tBTA_GATT_TRANSPORT transport); -extern void btif_gatt_move_track_adv_data(btgatt_track_adv_info_t *p_dest, - btgatt_track_adv_info_t *p_src); +void btif_gatt_check_encrypted_link(BD_ADDR bd_addr, + tBTA_GATT_TRANSPORT transport); +extern void btif_gatt_move_track_adv_data(btgatt_track_adv_info_t* p_dest, + btgatt_track_adv_info_t* p_src); #endif - diff --git a/btif/include/btif_hh.h b/btif/include/btif_hh.h index 646a80476..39d1a118a 100644 --- a/btif/include/btif_hh.h +++ b/btif/include/btif_hh.h @@ -27,86 +27,82 @@ #include "btu.h" /******************************************************************************* -** Constants & Macros -********************************************************************************/ + * Constants & Macros + *******************************************************************************/ -#define BTIF_HH_MAX_HID 8 -#define BTIF_HH_MAX_ADDED_DEV 32 +#define BTIF_HH_MAX_HID 8 +#define BTIF_HH_MAX_ADDED_DEV 32 -#define BTIF_HH_MAX_KEYSTATES 3 -#define BTIF_HH_KEYSTATE_MASK_NUMLOCK 0x01 -#define BTIF_HH_KEYSTATE_MASK_CAPSLOCK 0x02 +#define BTIF_HH_MAX_KEYSTATES 3 +#define BTIF_HH_KEYSTATE_MASK_NUMLOCK 0x01 +#define BTIF_HH_KEYSTATE_MASK_CAPSLOCK 0x02 #define BTIF_HH_KEYSTATE_MASK_SCROLLLOCK 0x04 -#define BTIF_HH_MAX_POLLING_ATTEMPTS 10 +#define BTIF_HH_MAX_POLLING_ATTEMPTS 10 #define BTIF_HH_POLLING_SLEEP_DURATION_US 5000 /******************************************************************************* -** Type definitions and return values -********************************************************************************/ - -typedef enum -{ - BTIF_HH_DISABLED = 0, - BTIF_HH_ENABLED, - BTIF_HH_DISABLING, - BTIF_HH_DEV_UNKNOWN, - BTIF_HH_DEV_CONNECTING, - BTIF_HH_DEV_CONNECTED, - BTIF_HH_DEV_DISCONNECTED + * Type definitions and return values + *******************************************************************************/ + +typedef enum { + BTIF_HH_DISABLED = 0, + BTIF_HH_ENABLED, + BTIF_HH_DISABLING, + BTIF_HH_DEV_UNKNOWN, + BTIF_HH_DEV_CONNECTING, + BTIF_HH_DEV_CONNECTED, + BTIF_HH_DEV_DISCONNECTED } BTIF_HH_STATUS; -typedef struct -{ - bthh_connection_state_t dev_status; - uint8_t dev_handle; - bt_bdaddr_t bd_addr; - tBTA_HH_ATTR_MASK attr_mask; - uint8_t sub_class; - uint8_t app_id; - int fd; - bool ready_for_data; - pthread_t hh_poll_thread_id; - uint8_t hh_keep_polling; - alarm_t *vup_timer; - bool local_vup; // Indicated locally initiated VUP +typedef struct { + bthh_connection_state_t dev_status; + uint8_t dev_handle; + bt_bdaddr_t bd_addr; + tBTA_HH_ATTR_MASK attr_mask; + uint8_t sub_class; + uint8_t app_id; + int fd; + bool ready_for_data; + pthread_t hh_poll_thread_id; + uint8_t hh_keep_polling; + alarm_t* vup_timer; + bool local_vup; // Indicated locally initiated VUP } btif_hh_device_t; /* Control block to maintain properties of devices */ -typedef struct -{ - uint8_t dev_handle; - bt_bdaddr_t bd_addr; - tBTA_HH_ATTR_MASK attr_mask; +typedef struct { + uint8_t dev_handle; + bt_bdaddr_t bd_addr; + tBTA_HH_ATTR_MASK attr_mask; } btif_hh_added_device_t; /** * BTIF-HH control block to maintain added devices and currently * connected hid devices */ -typedef struct -{ - BTIF_HH_STATUS status; - btif_hh_device_t devices[BTIF_HH_MAX_HID]; - uint32_t device_num; - btif_hh_added_device_t added_devices[BTIF_HH_MAX_ADDED_DEV]; - btif_hh_device_t *p_curr_dev; +typedef struct { + BTIF_HH_STATUS status; + btif_hh_device_t devices[BTIF_HH_MAX_HID]; + uint32_t device_num; + btif_hh_added_device_t added_devices[BTIF_HH_MAX_ADDED_DEV]; + btif_hh_device_t* p_curr_dev; } btif_hh_cb_t; - /******************************************************************************* -** Functions -********************************************************************************/ + * Functions + *******************************************************************************/ extern btif_hh_cb_t btif_hh_cb; -extern btif_hh_device_t *btif_hh_find_connected_dev_by_handle(uint8_t handle); +extern btif_hh_device_t* btif_hh_find_connected_dev_by_handle(uint8_t handle); extern void btif_hh_remove_device(bt_bdaddr_t bd_addr); bool btif_hh_add_added_dev(bt_bdaddr_t bda, tBTA_HH_ATTR_MASK attr_mask); -extern bt_status_t btif_hh_virtual_unplug(bt_bdaddr_t *bd_addr); -extern void btif_hh_disconnect(bt_bdaddr_t *bd_addr); -extern void btif_hh_setreport(btif_hh_device_t *p_dev, bthh_report_type_t r_type, - uint16_t size, uint8_t* report); +extern bt_status_t btif_hh_virtual_unplug(bt_bdaddr_t* bd_addr); +extern void btif_hh_disconnect(bt_bdaddr_t* bd_addr); +extern void btif_hh_setreport(btif_hh_device_t* p_dev, + bthh_report_type_t r_type, uint16_t size, + uint8_t* report); bool btif_hh_add_added_dev(bt_bdaddr_t bd_addr, tBTA_HH_ATTR_MASK attr_mask); diff --git a/btif/include/btif_hl.h b/btif/include/btif_hl.h index 99870de34..a99f2805a 100644 --- a/btif/include/btif_hl.h +++ b/btif/include/btif_hl.h @@ -21,311 +21,296 @@ #include -#include "bta_hl_api.h" #include "bt_common.h" +#include "bta_hl_api.h" #include "osi/include/alarm.h" /******************************************************************************* -** Constants & Macros -********************************************************************************/ + * Constants & Macros + *******************************************************************************/ -#define BTIF_HL_DATA_TYPE_NONE 0x0000 -#define BTIF_HL_DATA_TYPE_PULSE_OXIMETER 0x1004 /* from BT assigned number */ +#define BTIF_HL_DATA_TYPE_NONE 0x0000 +#define BTIF_HL_DATA_TYPE_PULSE_OXIMETER 0x1004 /* from BT assigned number */ #define BTIF_HL_DATA_TYPE_BLOOD_PRESSURE_MON 0x1007 -#define BTIF_HL_DATA_TYPE_BODY_THERMOMETER 0x1008 -#define BTIF_HL_DATA_TYPE_BODY_WEIGHT_SCALE 0x100F -#define BTIF_HL_DATA_TYPE_GLUCOSE_METER 0x1011 -#define BTIF_HL_DATA_TYPE_STEP_COUNTER 0x1068 -#define BTIF_HL_DATA_TYPE_BCA 0x1014 -#define BTIF_HL_DATA_TYPE_PEAK_FLOW 0x1015 -#define BTIF_HL_DATA_TYPE_CARDIO 0x1029 -#define BTIF_HL_DATA_TYPE_ACTIVITY_HUB 0x1047 -#define BTIF_HL_DATA_TYPE_AMM 0x1048 - -#define BTIF_HL_CCH_NUM_FILTER_ELEMS 3 -#define BTIF_HL_APPLICATION_NAME_LEN 512 +#define BTIF_HL_DATA_TYPE_BODY_THERMOMETER 0x1008 +#define BTIF_HL_DATA_TYPE_BODY_WEIGHT_SCALE 0x100F +#define BTIF_HL_DATA_TYPE_GLUCOSE_METER 0x1011 +#define BTIF_HL_DATA_TYPE_STEP_COUNTER 0x1068 +#define BTIF_HL_DATA_TYPE_BCA 0x1014 +#define BTIF_HL_DATA_TYPE_PEAK_FLOW 0x1015 +#define BTIF_HL_DATA_TYPE_CARDIO 0x1029 +#define BTIF_HL_DATA_TYPE_ACTIVITY_HUB 0x1047 +#define BTIF_HL_DATA_TYPE_AMM 0x1048 + +#define BTIF_HL_CCH_NUM_FILTER_ELEMS 3 +#define BTIF_HL_APPLICATION_NAME_LEN 512 /******************************************************************************* -** Type definitions and return values -********************************************************************************/ - -typedef enum -{ - BTIF_HL_SOC_STATE_IDLE, - BTIF_HL_SOC_STATE_W4_ADD, - BTIF_HL_SOC_STATE_W4_CONN, - BTIF_HL_SOC_STATE_W4_READ, - BTIF_HL_SOC_STATE_W4_REL + * Type definitions and return values + *******************************************************************************/ + +typedef enum { + BTIF_HL_SOC_STATE_IDLE, + BTIF_HL_SOC_STATE_W4_ADD, + BTIF_HL_SOC_STATE_W4_CONN, + BTIF_HL_SOC_STATE_W4_READ, + BTIF_HL_SOC_STATE_W4_REL } btif_hl_soc_state_t; -typedef enum -{ - BTIF_HL_STATE_DISABLED, - BTIF_HL_STATE_DISABLING, - BTIF_HL_STATE_ENABLED, - BTIF_HL_STATE_ENABLING, +typedef enum { + BTIF_HL_STATE_DISABLED, + BTIF_HL_STATE_DISABLING, + BTIF_HL_STATE_ENABLED, + BTIF_HL_STATE_ENABLING, } btif_hl_state_t; -typedef enum -{ - BTIF_HL_CCH_OP_NONE, - BTIF_HL_CCH_OP_MDEP_FILTERING, - BTIF_HL_CCH_OP_MATCHED_CTRL_PSM, - BTIF_HL_CCH_OP_DCH_OPEN, - BTIF_HL_CCH_OP_DCH_RECONNECT, - BTIF_HL_CCH_OP_DCH_ECHO_TEST +typedef enum { + BTIF_HL_CCH_OP_NONE, + BTIF_HL_CCH_OP_MDEP_FILTERING, + BTIF_HL_CCH_OP_MATCHED_CTRL_PSM, + BTIF_HL_CCH_OP_DCH_OPEN, + BTIF_HL_CCH_OP_DCH_RECONNECT, + BTIF_HL_CCH_OP_DCH_ECHO_TEST } btif_hl_cch_op_t; -typedef enum -{ - BTIF_HL_PEND_DCH_OP_NONE, - BTIF_HL_PEND_DCH_OP_DELETE_MDL, - BTIF_HL_PEND_DCH_OP_OPEN, - BTIF_HL_PEND_DCH_OP_RECONNECT +typedef enum { + BTIF_HL_PEND_DCH_OP_NONE, + BTIF_HL_PEND_DCH_OP_DELETE_MDL, + BTIF_HL_PEND_DCH_OP_OPEN, + BTIF_HL_PEND_DCH_OP_RECONNECT } btif_hl_pend_dch_op_t; -typedef enum -{ - BTIF_HL_DCH_OP_NONE, - BTIF_HL_DCH_OP_DISC -} btif_hl_dch_op_t; - -typedef enum -{ - BTIF_HL_CHAN_CB_STATE_NONE, - BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING, - BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING, - - BTIF_HL_CHAN_CB_STATE_DISCONNECTING_PENDING, - BTIF_HL_CHAN_CB_STATE_DISCONNECTED_PENDING, - BTIF_HL_CHAN_CB_STATE_DESTROYED_PENDING, +typedef enum { BTIF_HL_DCH_OP_NONE, BTIF_HL_DCH_OP_DISC } btif_hl_dch_op_t; + +typedef enum { + BTIF_HL_CHAN_CB_STATE_NONE, + BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING, + BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING, + + BTIF_HL_CHAN_CB_STATE_DISCONNECTING_PENDING, + BTIF_HL_CHAN_CB_STATE_DISCONNECTED_PENDING, + BTIF_HL_CHAN_CB_STATE_DESTROYED_PENDING, } btif_hl_chan_cb_state_t; -enum -{ - BTIF_HL_SEND_CONNECTED_CB, - BTIF_HL_SEND_DISCONNECTED_CB, - BTIF_HL_REG_APP, - BTIF_HL_UNREG_APP, - BTIF_HL_UPDATE_MDL, +enum { + BTIF_HL_SEND_CONNECTED_CB, + BTIF_HL_SEND_DISCONNECTED_CB, + BTIF_HL_REG_APP, + BTIF_HL_UNREG_APP, + BTIF_HL_UPDATE_MDL, }; -typedef struct -{ - uint8_t mdep_cfg_idx; - int data_type; - tBTA_HL_MDEP_ID peer_mdep_id; +typedef struct { + uint8_t mdep_cfg_idx; + int data_type; + tBTA_HL_MDEP_ID peer_mdep_id; } btif_hl_extra_mdl_cfg_t; -typedef struct -{ - tBTA_HL_MDL_CFG base; - btif_hl_extra_mdl_cfg_t extra; +typedef struct { + tBTA_HL_MDL_CFG base; + btif_hl_extra_mdl_cfg_t extra; } btif_hl_mdl_cfg_t; -typedef struct -{ - bool active; - uint8_t app_idx; +typedef struct { + bool active; + uint8_t app_idx; } btif_hl_app_data_t; -typedef struct -{ - int channel_id; - BD_ADDR bd_addr; - uint8_t mdep_cfg_idx; - int max_s; - int socket_id[2]; - uint8_t app_idx; - uint8_t mcl_idx; - uint8_t mdl_idx; - btif_hl_soc_state_t state; -}btif_hl_soc_cb_t; - -typedef struct -{ - uint16_t data_type; - uint16_t max_tx_apdu_size; - uint16_t max_rx_apdu_size; +typedef struct { + int channel_id; + BD_ADDR bd_addr; + uint8_t mdep_cfg_idx; + int max_s; + int socket_id[2]; + uint8_t app_idx; + uint8_t mcl_idx; + uint8_t mdl_idx; + btif_hl_soc_state_t state; +} btif_hl_soc_cb_t; + +typedef struct { + uint16_t data_type; + uint16_t max_tx_apdu_size; + uint16_t max_rx_apdu_size; } btif_hl_data_type_cfg_t; -typedef struct -{ - uint16_t data_type; - tBTA_HL_MDEP_ROLE peer_mdep_role; +typedef struct { + uint16_t data_type; + tBTA_HL_MDEP_ROLE peer_mdep_role; } btif_hl_filter_elem_t; -typedef struct -{ - uint8_t num_elems; - btif_hl_filter_elem_t elem[BTIF_HL_CCH_NUM_FILTER_ELEMS]; +typedef struct { + uint8_t num_elems; + btif_hl_filter_elem_t elem[BTIF_HL_CCH_NUM_FILTER_ELEMS]; } btif_hl_cch_filter_t; -typedef struct -{ - bool in_use; - uint16_t mdl_id; - tBTA_HL_MDL_HANDLE mdl_handle; - btif_hl_dch_op_t dch_oper; - tBTA_HL_MDEP_ID local_mdep_id; - uint8_t local_mdep_cfg_idx; - tBTA_HL_DCH_CFG local_cfg; - tBTA_HL_MDEP_ID peer_mdep_id; - uint16_t peer_data_type; - tBTA_HL_MDEP_ROLE peer_mdep_role; - tBTA_HL_DCH_MODE dch_mode; - tBTA_SEC sec_mask; - bool is_the_first_reliable; - bool delete_mdl; - uint16_t mtu; - tMCA_CHNL_CFG chnl_cfg; - uint16_t tx_size; - uint8_t *p_tx_pkt; - uint8_t *p_rx_pkt; - bool cong; - btif_hl_soc_cb_t *p_scb; - int channel_id; +typedef struct { + bool in_use; + uint16_t mdl_id; + tBTA_HL_MDL_HANDLE mdl_handle; + btif_hl_dch_op_t dch_oper; + tBTA_HL_MDEP_ID local_mdep_id; + uint8_t local_mdep_cfg_idx; + tBTA_HL_DCH_CFG local_cfg; + tBTA_HL_MDEP_ID peer_mdep_id; + uint16_t peer_data_type; + tBTA_HL_MDEP_ROLE peer_mdep_role; + tBTA_HL_DCH_MODE dch_mode; + tBTA_SEC sec_mask; + bool is_the_first_reliable; + bool delete_mdl; + uint16_t mtu; + tMCA_CHNL_CFG chnl_cfg; + uint16_t tx_size; + uint8_t* p_tx_pkt; + uint8_t* p_rx_pkt; + bool cong; + btif_hl_soc_cb_t* p_scb; + int channel_id; } btif_hl_mdl_cb_t; -typedef struct -{ - int channel_id; - int mdep_cfg_idx; - bool in_use; - btif_hl_chan_cb_state_t cb_state; - btif_hl_pend_dch_op_t op; - BD_ADDR bd_addr; - bool abort_pending; +typedef struct { + int channel_id; + int mdep_cfg_idx; + bool in_use; + btif_hl_chan_cb_state_t cb_state; + btif_hl_pend_dch_op_t op; + BD_ADDR bd_addr; + bool abort_pending; } btif_hl_pending_chan_cb_t; -typedef struct -{ - btif_hl_mdl_cb_t mdl[BTA_HL_NUM_MDLS_PER_MCL]; - bool in_use; - bool is_connected; - uint16_t req_ctrl_psm; - uint16_t ctrl_psm; - uint16_t data_psm; - BD_ADDR bd_addr; - uint16_t cch_mtu; - tBTA_SEC sec_mask; - tBTA_HL_MCL_HANDLE mcl_handle; - btif_hl_pending_chan_cb_t pcb; - bool valid_sdp_idx; - uint8_t sdp_idx; - tBTA_HL_SDP sdp; - btif_hl_cch_op_t cch_oper; - alarm_t *cch_timer; +typedef struct { + btif_hl_mdl_cb_t mdl[BTA_HL_NUM_MDLS_PER_MCL]; + bool in_use; + bool is_connected; + uint16_t req_ctrl_psm; + uint16_t ctrl_psm; + uint16_t data_psm; + BD_ADDR bd_addr; + uint16_t cch_mtu; + tBTA_SEC sec_mask; + tBTA_HL_MCL_HANDLE mcl_handle; + btif_hl_pending_chan_cb_t pcb; + bool valid_sdp_idx; + uint8_t sdp_idx; + tBTA_HL_SDP sdp; + btif_hl_cch_op_t cch_oper; + alarm_t* cch_timer; } btif_hl_mcl_cb_t; -typedef struct -{ - bool active; - uint16_t mdl_id; - uint8_t mdep_cfg_idx; - BD_ADDR bd_addr; - int channel_id; +typedef struct { + bool active; + uint16_t mdl_id; + uint8_t mdep_cfg_idx; + BD_ADDR bd_addr; + int channel_id; } btif_hl_delete_mdl_t; -typedef struct -{ - btif_hl_mcl_cb_t mcb[BTA_HL_NUM_MCLS]; /* application Control Blocks */ - bool in_use; /* this CB is in use*/ - bool reg_pending; - uint8_t app_id; - - tBTA_HL_SUP_FEATURE sup_feature; - tBTA_HL_DCH_CFG channel_type[BTA_HL_NUM_MDEPS]; - tBTA_HL_SDP_INFO_IND sdp_info_ind; - btif_hl_cch_filter_t filter; - - btif_hl_mdl_cfg_t mdl_cfg[BTA_HL_NUM_MDL_CFGS]; - int mdl_cfg_channel_id[BTA_HL_NUM_MDL_CFGS]; - - btif_hl_delete_mdl_t delete_mdl; - tBTA_HL_DEVICE_TYPE dev_type; - tBTA_HL_APP_HANDLE app_handle; - uint16_t sec_mask; /* Security mask for BTM_SetSecurityLevel() */ - char srv_name[BTA_SERVICE_NAME_LEN +1]; /* service name to be used in the SDP; null terminated*/ - char srv_desp[BTA_SERVICE_DESP_LEN +1]; /* service description to be used in the SDP; null terminated */ - char provider_name[BTA_PROVIDER_NAME_LEN +1]; /* provide name to be used in the SDP; null terminated */ - char application_name[BTIF_HL_APPLICATION_NAME_LEN +1]; /* applicaiton name */ +typedef struct { + btif_hl_mcl_cb_t mcb[BTA_HL_NUM_MCLS]; /* application Control Blocks */ + bool in_use; /* this CB is in use*/ + bool reg_pending; + uint8_t app_id; + + tBTA_HL_SUP_FEATURE sup_feature; + tBTA_HL_DCH_CFG channel_type[BTA_HL_NUM_MDEPS]; + tBTA_HL_SDP_INFO_IND sdp_info_ind; + btif_hl_cch_filter_t filter; + + btif_hl_mdl_cfg_t mdl_cfg[BTA_HL_NUM_MDL_CFGS]; + int mdl_cfg_channel_id[BTA_HL_NUM_MDL_CFGS]; + + btif_hl_delete_mdl_t delete_mdl; + tBTA_HL_DEVICE_TYPE dev_type; + tBTA_HL_APP_HANDLE app_handle; + uint16_t sec_mask; /* Security mask for BTM_SetSecurityLevel() */ + char srv_name[BTA_SERVICE_NAME_LEN + + 1]; /* service name to be used in the SDP; null terminated*/ + char + srv_desp[BTA_SERVICE_DESP_LEN + 1]; /* service description to be used in + the SDP; null terminated */ + char provider_name[BTA_PROVIDER_NAME_LEN + 1]; /* provide name to be used in + the SDP; null terminated */ + char + application_name[BTIF_HL_APPLICATION_NAME_LEN + 1]; /* applicaiton name */ } btif_hl_app_cb_t; -typedef struct -{ - bool in_use; - uint8_t app_idx; +typedef struct { + bool in_use; + uint8_t app_idx; } btif_hl_pending_reg_cb_t; /* BTIF-HL control block */ -typedef struct -{ - btif_hl_app_cb_t acb[BTA_HL_NUM_APPS]; /* HL Control Blocks */ - tBTA_HL_CTRL_CBACK *p_ctrl_cback; /* pointer to control callback function */ - uint8_t next_app_id; - uint16_t next_channel_id; - btif_hl_state_t state; +typedef struct { + btif_hl_app_cb_t acb[BTA_HL_NUM_APPS]; /* HL Control Blocks */ + tBTA_HL_CTRL_CBACK* p_ctrl_cback; /* pointer to control callback function */ + uint8_t next_app_id; + uint16_t next_channel_id; + btif_hl_state_t state; } btif_hl_cb_t; typedef uint8_t btif_hl_evt_t; -typedef struct -{ - int app_id; - BD_ADDR bd_addr; - int mdep_cfg_index; - int channel_id; - btif_hl_chan_cb_state_t cb_state; - int fd; +typedef struct { + int app_id; + BD_ADDR bd_addr; + int mdep_cfg_index; + int channel_id; + btif_hl_chan_cb_state_t cb_state; + int fd; } btif_hl_send_chan_state_cb_t; -typedef struct -{ - uint8_t app_idx; -} btif_hl_reg_t; +typedef struct { uint8_t app_idx; } btif_hl_reg_t; typedef btif_hl_reg_t btif_hl_unreg_t; typedef btif_hl_reg_t btif_hl_update_mdl_t; -typedef union -{ - btif_hl_send_chan_state_cb_t chan_cb; - btif_hl_reg_t reg; - btif_hl_unreg_t unreg; - btif_hl_update_mdl_t update_mdl; +typedef union { + btif_hl_send_chan_state_cb_t chan_cb; + btif_hl_reg_t reg; + btif_hl_unreg_t unreg; + btif_hl_update_mdl_t update_mdl; } btif_hl_evt_cb_t; /******************************************************************************* -** Functions -********************************************************************************/ + * Functions + *******************************************************************************/ #define BTIF_HL_GET_CB_PTR() &(btif_hl_cb) #define BTIF_HL_GET_APP_CB_PTR(app_idx) &(btif_hl_cb.acb[(app_idx)]) -#define BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx) &(btif_hl_cb.acb[(app_idx)].mcb[(mcl_idx)]) -#define BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx) &(btif_hl_cb.acb[(app_idx)].mcb[(mcl_idx)].mdl[mdl_idx]) -#define BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx) &(btif_hl_cb.acb[app_idx].mcb[mcl_idx].pcb) -#define BTIF_HL_GET_MDL_CFG_PTR(app_idx, item_idx) &(btif_hl_cb.acb[(app_idx)].mdl_cfg[(item_idx)]) -#define BTIF_HL_GET_MDL_CFG_CHANNEL_ID_PTR(app_idx, item_idx) &(btif_hl_cb.acb[(app_idx)].mdl_cfg_channel_id[(item_idx)]) - -extern btif_hl_cb_t btif_hl_cb; -extern btif_hl_cb_t *p_btif_hl_cb; - -extern bool btif_hl_find_mcl_idx(uint8_t app_idx, BD_ADDR p_bd_addr, uint8_t *p_mcl_idx); -extern bool btif_hl_find_app_idx(uint8_t app_id, uint8_t *p_app_idx); -extern bool btif_hl_find_avail_mcl_idx(uint8_t app_idx, uint8_t *p_mcl_idx); +#define BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx) \ + &(btif_hl_cb.acb[(app_idx)].mcb[(mcl_idx)]) +#define BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx) \ + &(btif_hl_cb.acb[(app_idx)].mcb[(mcl_idx)].mdl[mdl_idx]) +#define BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx) \ + &(btif_hl_cb.acb[app_idx].mcb[mcl_idx].pcb) +#define BTIF_HL_GET_MDL_CFG_PTR(app_idx, item_idx) \ + &(btif_hl_cb.acb[(app_idx)].mdl_cfg[(item_idx)]) +#define BTIF_HL_GET_MDL_CFG_CHANNEL_ID_PTR(app_idx, item_idx) \ + &(btif_hl_cb.acb[(app_idx)].mdl_cfg_channel_id[(item_idx)]) + +extern btif_hl_cb_t btif_hl_cb; +extern btif_hl_cb_t* p_btif_hl_cb; + +extern bool btif_hl_find_mcl_idx(uint8_t app_idx, BD_ADDR p_bd_addr, + uint8_t* p_mcl_idx); +extern bool btif_hl_find_app_idx(uint8_t app_id, uint8_t* p_app_idx); +extern bool btif_hl_find_avail_mcl_idx(uint8_t app_idx, uint8_t* p_mcl_idx); extern bool btif_hl_find_avail_mdl_idx(uint8_t app_idx, uint8_t mcl_idx, - uint8_t *p_mdl_idx); -extern bool btif_hl_find_mcl_idx_using_handle( tBTA_HL_MCL_HANDLE mcl_handle, - uint8_t *p_app_idx, uint8_t *p_mcl_idx); -extern bool btif_hl_save_mdl_cfg(uint8_t app_id, uint8_t item_idx, tBTA_HL_MDL_CFG *p_mdl_cfg); -extern bool btif_hl_delete_mdl_cfg(uint8_t app_id, uint8_t item_idx); + uint8_t* p_mdl_idx); +extern bool btif_hl_find_mcl_idx_using_handle(tBTA_HL_MCL_HANDLE mcl_handle, + uint8_t* p_app_idx, + uint8_t* p_mcl_idx); +extern bool btif_hl_save_mdl_cfg(uint8_t app_id, uint8_t item_idx, + tBTA_HL_MDL_CFG* p_mdl_cfg); +extern bool btif_hl_delete_mdl_cfg(uint8_t app_id, uint8_t item_idx); extern bool btif_hl_find_mdl_idx_using_handle(tBTA_HL_MDL_HANDLE mdl_handle, - uint8_t *p_app_idx,uint8_t *p_mcl_idx, - uint8_t *p_mdl_idx); + uint8_t* p_app_idx, + uint8_t* p_mcl_idx, + uint8_t* p_mdl_idx); extern void btif_hl_abort_pending_chan_setup(uint8_t app_idx, uint8_t mcl_idx); extern bool btif_hl_proc_pending_op(uint8_t app_idx, uint8_t mcl_idx); -extern bool btif_hl_load_mdl_config (uint8_t app_id, uint8_t buffer_size, - tBTA_HL_MDL_CFG *p_mdl_buf ); +extern bool btif_hl_load_mdl_config(uint8_t app_id, uint8_t buffer_size, + tBTA_HL_MDL_CFG* p_mdl_buf); #endif diff --git a/btif/include/btif_mce.h b/btif/include/btif_mce.h index aa61cfb55..beca0a283 100644 --- a/btif/include/btif_mce.h +++ b/btif/include/btif_mce.h @@ -30,6 +30,6 @@ #include -btmce_interface_t *btif_mce_get_interface(); +btmce_interface_t* btif_mce_get_interface(); #endif diff --git a/btif/include/btif_pan.h b/btif/include/btif_pan.h index d9ecb2e77..ecb480a77 100644 --- a/btif/include/btif_pan.h +++ b/btif/include/btif_pan.h @@ -29,7 +29,7 @@ #include -btpan_interface_t *btif_pan_interface(); +btpan_interface_t* btif_pan_interface(); void btif_pan_init(); void btif_pan_cleanup(); diff --git a/btif/include/btif_pan_internal.h b/btif/include/btif_pan_internal.h index 485a905f6..462f78719 100644 --- a/btif/include/btif_pan_internal.h +++ b/btif/include/btif_pan_internal.h @@ -27,97 +27,92 @@ #ifndef BTIF_PAN_INTERNAL_H #define BTIF_PAN_INTERNAL_H -#include "btif_pan.h" #include "bt_types.h" +#include "btif_pan.h" /******************************************************************************* -** Constants & Macros -********************************************************************************/ + * Constants & Macros + *******************************************************************************/ #define PAN_NAP_SERVICE_NAME "Android Network Access Point" #define PANU_SERVICE_NAME "Android Network User" #define TAP_IF_NAME "bt-pan" -#define ETH_ADDR_LEN 6 +#define ETH_ADDR_LEN 6 #define TAP_MAX_PKT_WRITE_LEN 2000 #ifndef PAN_SECURITY -#define PAN_SECURITY (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_IN_ENCRYPT | BTM_SEC_OUT_ENCRYPT) +#define PAN_SECURITY \ + (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_IN_ENCRYPT | \ + BTM_SEC_OUT_ENCRYPT) #endif -#define PAN_STATE_UNKNOWN 0 -#define PAN_STATE_OPEN 1 -#define PAN_STATE_CLOSE 2 +#define PAN_STATE_UNKNOWN 0 +#define PAN_STATE_OPEN 1 +#define PAN_STATE_CLOSE 2 #ifndef PAN_ROLE_INACTIVE #define PAN_ROLE_INACTIVE 0 #endif - /******************************************************************************* -** Type definitions and return values -********************************************************************************/ + * Type definitions and return values + *******************************************************************************/ -typedef struct eth_hdr -{ - unsigned char h_dest[ETH_ADDR_LEN]; - unsigned char h_src[ETH_ADDR_LEN]; - short h_proto; +typedef struct eth_hdr { + unsigned char h_dest[ETH_ADDR_LEN]; + unsigned char h_src[ETH_ADDR_LEN]; + short h_proto; } tETH_HDR; -typedef struct -{ - int handle; - int state; - uint16_t protocol; - BD_ADDR peer; - int local_role; - int remote_role; - unsigned char eth_addr[ETH_ADDR_LEN]; +typedef struct { + int handle; + int state; + uint16_t protocol; + BD_ADDR peer; + int local_role; + int remote_role; + unsigned char eth_addr[ETH_ADDR_LEN]; } btpan_conn_t; -typedef struct -{ - int btl_if_handle; - int btl_if_handle_panu; - int tap_fd; - int enabled; - int open_count; - int flow; // 1: outbound data flow on; 0: outbound data flow off - btpan_conn_t conns[MAX_PAN_CONNS]; - int congest_packet_size; - unsigned char congest_packet[1600]; //max ethernet packet size +typedef struct { + int btl_if_handle; + int btl_if_handle_panu; + int tap_fd; + int enabled; + int open_count; + int flow; // 1: outbound data flow on; 0: outbound data flow off + btpan_conn_t conns[MAX_PAN_CONNS]; + int congest_packet_size; + unsigned char congest_packet[1600]; // max ethernet packet size } btpan_cb_t; - /******************************************************************************* -** Functions -********************************************************************************/ + * Functions + *******************************************************************************/ extern btpan_cb_t btpan_cb; -btpan_conn_t *btpan_new_conn(int handle, const BD_ADDR addr, int local_role, int peer_role); -btpan_conn_t *btpan_find_conn_addr(const BD_ADDR addr); -btpan_conn_t *btpan_find_conn_handle(uint16_t handle); +btpan_conn_t* btpan_new_conn(int handle, const BD_ADDR addr, int local_role, + int peer_role); +btpan_conn_t* btpan_find_conn_addr(const BD_ADDR addr); +btpan_conn_t* btpan_find_conn_handle(uint16_t handle); void btpan_set_flow_control(bool enable); int btpan_get_connected_count(void); int btpan_tap_open(void); void create_tap_read_thread(int tap_fd); void destroy_tap_read_thread(void); int btpan_tap_close(int tap_fd); -int btpan_tap_send(int tap_fd, const BD_ADDR src, const BD_ADDR dst, uint16_t protocol, - const char* buff, uint16_t size, bool ext, bool forward); - -static inline int is_empty_eth_addr(const BD_ADDR addr) -{ - int i; - for(i = 0; i < BD_ADDR_LEN; i++) - if(addr[i] != 0) - return 0; - return 1; +int btpan_tap_send(int tap_fd, const BD_ADDR src, const BD_ADDR dst, + uint16_t protocol, const char* buff, uint16_t size, bool ext, + bool forward); + +static inline int is_empty_eth_addr(const BD_ADDR addr) { + int i; + for (i = 0; i < BD_ADDR_LEN; i++) + if (addr[i] != 0) return 0; + return 1; } -static inline int is_valid_bt_eth_addr(const BD_ADDR addr) -{ - if(is_empty_eth_addr(addr)) - return 0; - return addr[0] & 1 ? 0 : 1; /* Cannot be multicasting address */ +static inline int is_valid_bt_eth_addr(const BD_ADDR addr) { + if (is_empty_eth_addr(addr)) return 0; + return addr[0] & 1 ? 0 : 1; /* Cannot be multicasting address */ } #endif diff --git a/btif/include/btif_profile_queue.h b/btif/include/btif_profile_queue.h index 5cc3d0a65..f6e01a008 100644 --- a/btif/include/btif_profile_queue.h +++ b/btif/include/btif_profile_queue.h @@ -29,9 +29,10 @@ #include -typedef bt_status_t (*btif_connect_cb_t) (bt_bdaddr_t *bda, uint16_t uuid); +typedef bt_status_t (*btif_connect_cb_t)(bt_bdaddr_t* bda, uint16_t uuid); -bt_status_t btif_queue_connect(uint16_t uuid, const bt_bdaddr_t *bda, btif_connect_cb_t connect_cb); +bt_status_t btif_queue_connect(uint16_t uuid, const bt_bdaddr_t* bda, + btif_connect_cb_t connect_cb); void btif_queue_advance(); bt_status_t btif_queue_connect_next(void); void btif_queue_release(); diff --git a/btif/include/btif_sdp.h b/btif/include/btif_sdp.h index 2aac1c31d..2fe8b63e2 100644 --- a/btif/include/btif_sdp.h +++ b/btif/include/btif_sdp.h @@ -29,6 +29,6 @@ #include -btsdp_interface_t *btif_sdp_get_interface(); +btsdp_interface_t* btif_sdp_get_interface(); #endif diff --git a/btif/include/btif_sm.h b/btif/include/btif_sm.h index 13702d47b..3f2f91a02 100644 --- a/btif/include/btif_sm.h +++ b/btif/include/btif_sm.h @@ -32,89 +32,92 @@ #include "stack/include/bt_types.h" /***************************************************************************** -** Constants & Macros -******************************************************************************/ + * Constants & Macros + *****************************************************************************/ /* Generic Enter/Exit state machine events */ #define BTIF_SM_ENTER_EVT 0xFFFF -#define BTIF_SM_EXIT_EVT 0xFFFE +#define BTIF_SM_EXIT_EVT 0xFFFE /***************************************************************************** -** Type definitions and return values -******************************************************************************/ + * Type definitions and return values + *****************************************************************************/ typedef uint32_t btif_sm_state_t; typedef uint32_t btif_sm_event_t; typedef void* btif_sm_handle_t; -typedef bool(*btif_sm_handler_t)(btif_sm_event_t event, void *data); +typedef bool (*btif_sm_handler_t)(btif_sm_event_t event, void* data); /***************************************************************************** -** Functions -** -** NOTE: THESE APIs SHOULD BE INVOKED ONLY IN THE BTIF CONTEXT -** -******************************************************************************/ + * Functions + * + * NOTE: THESE APIs SHOULD BE INVOKED ONLY IN THE BTIF CONTEXT + * + *****************************************************************************/ /***************************************************************************** -** -** Function btif_sm_init -** -** Description Initializes the state machine with the state handlers -** The caller should ensure that the table and the corresponding -** states match. The location that 'p_handlers' points to shall -** be available until the btif_sm_shutdown API is invoked. -** -** Returns Returns a pointer to the initialized state machine handle. -** -******************************************************************************/ -btif_sm_handle_t btif_sm_init(const btif_sm_handler_t *p_handlers, - btif_sm_state_t initial_state); + * + * Function btif_sm_init + * + * Description Initializes the state machine with the state handlers + * The caller should ensure that the table and the corresponding + * states match. The location that 'p_handlers' points to shall + * be available until the btif_sm_shutdown API is invoked. + * + * Returns Returns a pointer to the initialized state machine handle. + * + *****************************************************************************/ +btif_sm_handle_t btif_sm_init(const btif_sm_handler_t* p_handlers, + btif_sm_state_t initial_state); /***************************************************************************** -** -** Function btif_sm_shutdown -** -** Description Tears down the state machine -** -** Returns None -** -******************************************************************************/ + * + * Function btif_sm_shutdown + * + * Description Tears down the state machine + * + * Returns None + * + *****************************************************************************/ void btif_sm_shutdown(btif_sm_handle_t handle); /***************************************************************************** -** -** Function btif_sm_get_state -** -** Description Fetches the current state of the state machine -** -** Returns Current state -** -******************************************************************************/ + * + * Function btif_sm_get_state + * + * Description Fetches the current state of the state machine + * + * Returns Current state + * + *****************************************************************************/ btif_sm_state_t btif_sm_get_state(btif_sm_handle_t handle); /***************************************************************************** -** -** Function btif_sm_dispatch -** -** Description Dispatches the 'event' along with 'data' to the current state handler -** -** Returns Returns BT_STATUS_OK on success, BT_STATUS_FAIL otherwise -** -******************************************************************************/ + * + * Function btif_sm_dispatch + * + * Description Dispatches the 'event' along with 'data' to the current state + *handler + * + * Returns Returns BT_STATUS_OK on success, BT_STATUS_FAIL otherwise + * + *****************************************************************************/ bt_status_t btif_sm_dispatch(btif_sm_handle_t handle, btif_sm_event_t event, - void *data); + void* data); /***************************************************************************** -** -** Function btif_sm_change_state -** -** Description Make a transition to the new 'state'. The 'BTIF_SM_EXIT_EVT' -** shall be invoked before exiting the current state. The -** 'BTIF_SM_ENTER_EVT' shall be invoked before entering the new state -** -** -** Returns Returns BT_STATUS_OK on success, BT_STATUS_FAIL otherwise -** -******************************************************************************/ -bt_status_t btif_sm_change_state(btif_sm_handle_t handle, btif_sm_state_t state); + * + * Function btif_sm_change_state + * + * Description Make a transition to the new 'state'. The 'BTIF_SM_EXIT_EVT' + * shall be invoked before exiting the current state. The + * 'BTIF_SM_ENTER_EVT' shall be invoked before entering the new + *state + * + * + * Returns Returns BT_STATUS_OK on success, BT_STATUS_FAIL otherwise + * + *****************************************************************************/ +bt_status_t btif_sm_change_state(btif_sm_handle_t handle, + btif_sm_state_t state); #endif /* BTIF_SM_H */ diff --git a/btif/include/btif_sock.h b/btif/include/btif_sock.h index 4ad4ab659..e65def6a3 100644 --- a/btif/include/btif_sock.h +++ b/btif/include/btif_sock.h @@ -22,7 +22,7 @@ #include -btsock_interface_t *btif_sock_get_interface(void); +btsock_interface_t* btif_sock_get_interface(void); bt_status_t btif_sock_init(uid_set_t* uid_set); void btif_sock_cleanup(void); diff --git a/btif/include/btif_sock_l2cap.h b/btif/include/btif_sock_l2cap.h index 6bf68edee..480ee6d45 100644 --- a/btif/include/btif_sock_l2cap.h +++ b/btif/include/btif_sock_l2cap.h @@ -9,15 +9,15 @@ #include -#define L2CAP_MASK_FIXED_CHANNEL 0x10000 -#define L2CAP_MASK_LE_COC_CHANNEL 0x20000 +#define L2CAP_MASK_FIXED_CHANNEL 0x10000 +#define L2CAP_MASK_LE_COC_CHANNEL 0x20000 bt_status_t btsock_l2cap_init(int handle, uid_set_t* set); bt_status_t btsock_l2cap_cleanup(); -bt_status_t btsock_l2cap_listen(const char* name, int channel, - int* sock_fd, int flags, int app_uid); -bt_status_t btsock_l2cap_connect(const bt_bdaddr_t *bd_addr, - int channel, int* sock_fd, int flags, int app_uid); +bt_status_t btsock_l2cap_listen(const char* name, int channel, int* sock_fd, + int flags, int app_uid); +bt_status_t btsock_l2cap_connect(const bt_bdaddr_t* bd_addr, int channel, + int* sock_fd, int flags, int app_uid); void btsock_l2cap_signaled(int fd, int flags, uint32_t user_id); void on_l2cap_psm_assigned(int id, int psm); diff --git a/btif/include/btif_sock_rfc.h b/btif/include/btif_sock_rfc.h index 7d9385675..93129e2c4 100644 --- a/btif/include/btif_sock_rfc.h +++ b/btif/include/btif_sock_rfc.h @@ -31,10 +31,12 @@ bt_status_t btsock_rfc_init(int handle, uid_set_t* set); bt_status_t btsock_rfc_cleanup(); -bt_status_t btsock_rfc_listen(const char* name, const uint8_t* uuid, int channel, - int* sock_fd, int flags, int app_uid); -bt_status_t btsock_rfc_connect(const bt_bdaddr_t *bd_addr, const uint8_t* uuid, - int channel, int* sock_fd, int flags, int app_uid); +bt_status_t btsock_rfc_listen(const char* name, const uint8_t* uuid, + int channel, int* sock_fd, int flags, + int app_uid); +bt_status_t btsock_rfc_connect(const bt_bdaddr_t* bd_addr, const uint8_t* uuid, + int channel, int* sock_fd, int flags, + int app_uid); void btsock_rfc_signaled(int fd, int flags, uint32_t user_id); #endif diff --git a/btif/include/btif_sock_sco.h b/btif/include/btif_sock_sco.h index 972aeebb1..66859d7d0 100644 --- a/btif/include/btif_sock_sco.h +++ b/btif/include/btif_sock_sco.h @@ -22,7 +22,8 @@ typedef struct thread_t thread_t; -bt_status_t btsock_sco_init(thread_t *thread); +bt_status_t btsock_sco_init(thread_t* thread); bt_status_t btsock_sco_cleanup(void); -bt_status_t btsock_sco_listen(int *sock_fd, int flags); -bt_status_t btsock_sco_connect(const bt_bdaddr_t *bd_addr, int *sock_fd, int flags); +bt_status_t btsock_sco_listen(int* sock_fd, int flags); +bt_status_t btsock_sco_connect(const bt_bdaddr_t* bd_addr, int* sock_fd, + int flags); diff --git a/btif/include/btif_sock_sdp.h b/btif/include/btif_sock_sdp.h index 7353cc971..7672c7cdf 100644 --- a/btif/include/btif_sock_sdp.h +++ b/btif/include/btif_sock_sdp.h @@ -19,25 +19,29 @@ #ifndef BTIF_SOCK_SDP_H #define BTIF_SOCK_SDP_H -#include -#include #include +#include +#include -static const uint8_t UUID_OBEX_OBJECT_PUSH[] = {0x00, 0x00, 0x11, 0x05, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; -static const uint8_t UUID_PBAP_PSE[] = {0x00, 0x00, 0x11, 0x2F, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; -static const uint8_t UUID_MAP_MAS[] = {0x00, 0x00, 0x11, 0x32, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; -static const uint8_t UUID_SAP[] = {0x00, 0x00, 0x11, 0x2D, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; -static const uint8_t UUID_SPP[] = {0x00, 0x00, 0x11, 0x01, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; +static const uint8_t UUID_OBEX_OBJECT_PUSH[] = { + 0x00, 0x00, 0x11, 0x05, 0x00, 0x00, 0x10, 0x00, + 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; +static const uint8_t UUID_PBAP_PSE[] = {0x00, 0x00, 0x11, 0x2F, 0x00, 0x00, + 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, + 0x5F, 0x9B, 0x34, 0xFB}; +static const uint8_t UUID_MAP_MAS[] = {0x00, 0x00, 0x11, 0x32, 0x00, 0x00, + 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, + 0x5F, 0x9B, 0x34, 0xFB}; +static const uint8_t UUID_SAP[] = {0x00, 0x00, 0x11, 0x2D, 0x00, 0x00, + 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, + 0x5F, 0x9B, 0x34, 0xFB}; +static const uint8_t UUID_SPP[] = {0x00, 0x00, 0x11, 0x01, 0x00, 0x00, + 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, + 0x5F, 0x9B, 0x34, 0xFB}; -static inline bool is_uuid_empty(const uint8_t* uuid) -{ - static uint8_t empty_uuid[16]; - return uuid == NULL || memcmp(uuid, empty_uuid, sizeof(empty_uuid)) == 0; +static inline bool is_uuid_empty(const uint8_t* uuid) { + static uint8_t empty_uuid[16]; + return uuid == NULL || memcmp(uuid, empty_uuid, sizeof(empty_uuid)) == 0; } int add_rfc_sdp_rec(const char* name, const uint8_t* uuid, int scn); diff --git a/btif/include/btif_sock_thread.h b/btif/include/btif_sock_thread.h index b81c494bb..6349b688e 100644 --- a/btif/include/btif_sock_thread.h +++ b/btif/include/btif_sock_thread.h @@ -25,29 +25,34 @@ #include /******************************************************************************* -** Constants & Macros -********************************************************************************/ + * Constants & Macros + *******************************************************************************/ -#define SOCK_THREAD_FD_RD 1 /* BT socket read signal */ -#define SOCK_THREAD_FD_WR (1 << 1) /* BT socket write signal */ -#define SOCK_THREAD_FD_EXCEPTION (1 << 2) /* BT socket exception singal */ -#define SOCK_THREAD_ADD_FD_SYNC (1 << 3) /* Add BT socket fd in current socket - poll thread context immediately */ +#define SOCK_THREAD_FD_RD 1 /* BT socket read signal */ +#define SOCK_THREAD_FD_WR (1 << 1) /* BT socket write signal */ +#define SOCK_THREAD_FD_EXCEPTION (1 << 2) /* BT socket exception singal */ +#define SOCK_THREAD_ADD_FD_SYNC \ + (1 << 3) /* Add BT socket fd in current socket \ + poll thread context immediately */ /******************************************************************************* -** Functions -********************************************************************************/ + * Functions + *******************************************************************************/ -typedef void (*btsock_signaled_cb)(int fd, int type, int flags, uint32_t user_id); +typedef void (*btsock_signaled_cb)(int fd, int type, int flags, + uint32_t user_id); typedef void (*btsock_cmd_cb)(int cmd_fd, int type, int size, uint32_t user_id); int btsock_thread_init(); -int btsock_thread_add_fd(int handle, int fd, int type, int flags, uint32_t user_id); +int btsock_thread_add_fd(int handle, int fd, int type, int flags, + uint32_t user_id); bool btsock_thread_remove_fd_and_close(int thread_handle, int fd); int btsock_thread_wakeup(int handle); -int btsock_thread_post_cmd(int handle, int cmd_type, const unsigned char* cmd_data, - int data_size, uint32_t user_id); -int btsock_thread_create(btsock_signaled_cb callback, btsock_cmd_cb cmd_callback); +int btsock_thread_post_cmd(int handle, int cmd_type, + const unsigned char* cmd_data, int data_size, + uint32_t user_id); +int btsock_thread_create(btsock_signaled_cb callback, + btsock_cmd_cb cmd_callback); int btsock_thread_exit(int handle); #endif diff --git a/btif/include/btif_storage.h b/btif/include/btif_storage.h index 9e5d75289..a182e47f0 100644 --- a/btif/include/btif_storage.h +++ b/btif/include/btif_storage.h @@ -25,212 +25,213 @@ #include "bt_types.h" /******************************************************************************* -** Constants & Macros -********************************************************************************/ + * Constants & Macros + *******************************************************************************/ #define BTIF_STORAGE_FILL_PROPERTY(p_prop, t, l, p_v) \ - (p_prop)->type = t;(p_prop)->len = l; (p_prop)->val = (p_v); + (p_prop)->type = t; \ + (p_prop)->len = l; \ + (p_prop)->val = (p_v); /******************************************************************************* -** Functions -********************************************************************************/ + * Functions + *******************************************************************************/ /******************************************************************************* -** -** Function btif_storage_get_adapter_property -** -** Description BTIF storage API - Fetches the adapter property->type -** from NVRAM and fills property->val. -** Caller should provide memory for property->val and -** set the property->val -** -** Returns BT_STATUS_SUCCESS if the fetch was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_get_adapter_property(bt_property_t *property); + * + * Function btif_storage_get_adapter_property + * + * Description BTIF storage API - Fetches the adapter property->type + * from NVRAM and fills property->val. + * Caller should provide memory for property->val and + * set the property->val + * + * Returns BT_STATUS_SUCCESS if the fetch was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_get_adapter_property(bt_property_t* property); /******************************************************************************* -** -** Function btif_storage_set_adapter_property -** -** Description BTIF storage API - Stores the adapter property -** to NVRAM -** -** Returns BT_STATUS_SUCCESS if the store was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_set_adapter_property(bt_property_t *property); + * + * Function btif_storage_set_adapter_property + * + * Description BTIF storage API - Stores the adapter property + * to NVRAM + * + * Returns BT_STATUS_SUCCESS if the store was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_set_adapter_property(bt_property_t* property); /******************************************************************************* -** -** Function btif_storage_get_remote_device_property -** -** Description BTIF storage API - Fetches the remote device property->type -** from NVRAM and fills property->val. -** Caller should provide memory for property->val and -** set the property->val -** -** Returns BT_STATUS_SUCCESS if the fetch was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_get_remote_device_property(bt_bdaddr_t *remote_bd_addr, - bt_property_t *property); + * + * Function btif_storage_get_remote_device_property + * + * Description BTIF storage API - Fetches the remote device property->type + * from NVRAM and fills property->val. + * Caller should provide memory for property->val and + * set the property->val + * + * Returns BT_STATUS_SUCCESS if the fetch was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_get_remote_device_property(bt_bdaddr_t* remote_bd_addr, + bt_property_t* property); /******************************************************************************* -** -** Function btif_storage_set_remote_device_property -** -** Description BTIF storage API - Stores the remote device property -** to NVRAM -** -** Returns BT_STATUS_SUCCESS if the store was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_set_remote_device_property(bt_bdaddr_t *remote_bd_addr, - bt_property_t *property); + * + * Function btif_storage_set_remote_device_property + * + * Description BTIF storage API - Stores the remote device property + * to NVRAM + * + * Returns BT_STATUS_SUCCESS if the store was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_set_remote_device_property(bt_bdaddr_t* remote_bd_addr, + bt_property_t* property); /******************************************************************************* -** -** Function btif_storage_add_remote_device -** -** Description BTIF storage API - Adds a newly discovered device to -** track along with the timestamp. Also, stores the various -** properties - RSSI, BDADDR, NAME (if found in EIR) -** -** Returns BT_STATUS_SUCCESS if successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_add_remote_device(bt_bdaddr_t *remote_bd_addr, + * + * Function btif_storage_add_remote_device + * + * Description BTIF storage API - Adds a newly discovered device to + * track along with the timestamp. Also, stores the various + * properties - RSSI, BDADDR, NAME (if found in EIR) + * + * Returns BT_STATUS_SUCCESS if successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_add_remote_device(bt_bdaddr_t* remote_bd_addr, uint32_t num_properties, - bt_property_t *properties); + bt_property_t* properties); /******************************************************************************* -** -** Function btif_storage_add_bonded_device -** -** Description BTIF storage API - Adds the newly bonded device to NVRAM -** along with the link-key, Key type and Pin key length -** -** Returns BT_STATUS_SUCCESS if the store was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr, - LINK_KEY link_key, - uint8_t key_type, + * + * Function btif_storage_add_bonded_device + * + * Description BTIF storage API - Adds the newly bonded device to NVRAM + * along with the link-key, Key type and Pin key length + * + * Returns BT_STATUS_SUCCESS if the store was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t* remote_bd_addr, + LINK_KEY link_key, uint8_t key_type, uint8_t pin_length); /******************************************************************************* -** -** Function btif_storage_remove_bonded_device -** -** Description BTIF storage API - Deletes the bonded device from NVRAM -** -** Returns BT_STATUS_SUCCESS if the deletion was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr); + * + * Function btif_storage_remove_bonded_device + * + * Description BTIF storage API - Deletes the bonded device from NVRAM + * + * Returns BT_STATUS_SUCCESS if the deletion was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t* remote_bd_addr); /******************************************************************************* -** -** Function btif_storage_remove_bonded_device -** -** Description BTIF storage API - Deletes the bonded device from NVRAM -** -** Returns BT_STATUS_SUCCESS if the deletion was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ + * + * Function btif_storage_remove_bonded_device + * + * Description BTIF storage API - Deletes the bonded device from NVRAM + * + * Returns BT_STATUS_SUCCESS if the deletion was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ bt_status_t btif_storage_load_bonded_devices(void); /******************************************************************************* -** -** Function btif_storage_add_hid_device_info -** -** Description BTIF storage API - Adds the hid information of bonded hid devices-to NVRAM -** -** Returns BT_STATUS_SUCCESS if the store was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ - -bt_status_t btif_storage_add_hid_device_info(bt_bdaddr_t *remote_bd_addr, - uint16_t attr_mask, uint8_t sub_class, - uint8_t app_id, uint16_t vendor_id, - uint16_t product_id, uint16_t version, - uint8_t ctry_code, uint16_t ssr_max_latency, - uint16_t ssr_min_tout, uint16_t dl_len, uint8_t *dsc_list); + * + * Function btif_storage_add_hid_device_info + * + * Description BTIF storage API - Adds the hid information of bonded hid + *devices-to NVRAM + * + * Returns BT_STATUS_SUCCESS if the store was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ + +bt_status_t btif_storage_add_hid_device_info( + bt_bdaddr_t* remote_bd_addr, uint16_t attr_mask, uint8_t sub_class, + uint8_t app_id, uint16_t vendor_id, uint16_t product_id, uint16_t version, + uint8_t ctry_code, uint16_t ssr_max_latency, uint16_t ssr_min_tout, + uint16_t dl_len, uint8_t* dsc_list); /******************************************************************************* -** -** Function btif_storage_load_bonded_hid_info -** -** Description BTIF storage API - Loads hid info for all the bonded devices from NVRAM -** and adds those devices to the BTA_HH. -** -** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise -** -*******************************************************************************/ + * + * Function btif_storage_load_bonded_hid_info + * + * Description BTIF storage API - Loads hid info for all the bonded devices + *from NVRAM + * and adds those devices to the BTA_HH. + * + * Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise + * + ******************************************************************************/ bt_status_t btif_storage_load_bonded_hid_info(void); /******************************************************************************* -** -** Function btif_storage_remove_hid_info -** -** Description BTIF storage API - Deletes the bonded hid device info from NVRAM -** -** Returns BT_STATUS_SUCCESS if the deletion was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t *remote_bd_addr); + * + * Function btif_storage_remove_hid_info + * + * Description BTIF storage API - Deletes the bonded hid device info from + *NVRAM + * + * Returns BT_STATUS_SUCCESS if the deletion was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t* remote_bd_addr); /******************************************************************************* -** -** Function btif_storage_is_retricted_device -** -** Description BTIF storage API - checks if this device is a restricted device -** -** Returns true if the device is labled as restricted -** false otherwise -** -*******************************************************************************/ -bool btif_storage_is_restricted_device(const bt_bdaddr_t *remote_bd_addr); + * + * Function btif_storage_is_retricted_device + * + * Description BTIF storage API - checks if this device is a restricted + *device + * + * Returns true if the device is labled as restricted + * false otherwise + * + ******************************************************************************/ +bool btif_storage_is_restricted_device(const bt_bdaddr_t* remote_bd_addr); #if (BLE_INCLUDED == TRUE) -bt_status_t btif_storage_add_ble_bonding_key( bt_bdaddr_t *remote_bd_addr, - char *key, - uint8_t key_type, - uint8_t key_length); -bt_status_t btif_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr, - uint8_t key_type, - char *key_value, +bt_status_t btif_storage_add_ble_bonding_key(bt_bdaddr_t* remote_bd_addr, + char* key, uint8_t key_type, + uint8_t key_length); +bt_status_t btif_storage_get_ble_bonding_key(bt_bdaddr_t* remote_bd_addr, + uint8_t key_type, char* key_value, int key_length); -bt_status_t btif_storage_add_ble_local_key(char *key, - uint8_t key_type, +bt_status_t btif_storage_add_ble_local_key(char* key, uint8_t key_type, uint8_t key_length); -bt_status_t btif_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr); +bt_status_t btif_storage_remove_ble_bonding_keys(bt_bdaddr_t* remote_bd_addr); bt_status_t btif_storage_remove_ble_local_keys(void); -bt_status_t btif_storage_get_ble_local_key(uint8_t key_type, - char *key_value, +bt_status_t btif_storage_get_ble_local_key(uint8_t key_type, char* key_value, int key_len); -bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr, - int *addr_type); +bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t* remote_bd_addr, + int* addr_type); -bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr, +bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t* remote_bd_addr, uint8_t addr_type); #endif /****************************************************************************** * Exported for unit tests *****************************************************************************/ -size_t btif_split_uuids_string(const char *str, bt_uuid_t *p_uuid, size_t max_uuids); +size_t btif_split_uuids_string(const char* str, bt_uuid_t* p_uuid, + size_t max_uuids); #endif /* BTIF_STORAGE_H */ diff --git a/btif/include/btif_util.h b/btif/include/btif_util.h index 3e260b333..58ff18f13 100644 --- a/btif/include/btif_util.h +++ b/btif/include/btif_util.h @@ -29,19 +29,20 @@ #include "bt_utils.h" /******************************************************************************* -** Constants & Macros -********************************************************************************/ - -#define CASE_RETURN_STR(const) case const: return #const; + * Constants & Macros + *******************************************************************************/ +#define CASE_RETURN_STR(const) \ + case const: \ + return #const; /******************************************************************************* -** Type definitions for callback functions -********************************************************************************/ + * Type definitions for callback functions + *******************************************************************************/ /******************************************************************************* -** Functions -********************************************************************************/ + * Functions + *******************************************************************************/ const char* dump_bt_status(bt_status_t status); const char* dump_dm_search_event(uint16_t event); @@ -70,10 +71,10 @@ void uuid16_to_uuid128(uint16_t uuid16, bt_uuid_t* uuid128); // "12345678-1234-1234-1234-123456789012" // |p_uuid| cannot be null. Returns true if parsing was successful, false // otherwise. Returns false if |str| is null. -bool string_to_uuid(const char *str, bt_uuid_t *p_uuid); +bool string_to_uuid(const char* str, bt_uuid_t* p_uuid); -int ascii_2_hex (const char *p_ascii, int len, uint8_t *p_hex); +int ascii_2_hex(const char* p_ascii, int len, uint8_t* p_hex); -void uuid_to_string_legacy(bt_uuid_t *p_uuid, char *str, size_t str_len); +void uuid_to_string_legacy(bt_uuid_t* p_uuid, char* str, size_t str_len); #endif /* BTIF_UTIL_H */ diff --git a/btif/include/stack_manager.h b/btif/include/stack_manager.h index 97fd02f6c..90d81c563 100644 --- a/btif/include/stack_manager.h +++ b/btif/include/stack_manager.h @@ -31,7 +31,8 @@ typedef struct { bool (*get_stack_is_running)(void); } stack_manager_t; -const stack_manager_t *stack_manager_get_interface(); +const stack_manager_t* stack_manager_get_interface(); -// TODO(zachoverflow): remove this terrible hack once the startup sequence is more sane -future_t *stack_manager_get_hack_future(); +// TODO(zachoverflow): remove this terrible hack once the startup sequence is +// more sane +future_t* stack_manager_get_hack_future(); diff --git a/btif/include/uinput.h b/btif/include/uinput.h index e2db6925a..99a03ce5a 100644 --- a/btif/include/uinput.h +++ b/btif/include/uinput.h @@ -20,564 +20,563 @@ #define __UINPUT_H #include -#include #include +#include /******************************************************************************* -** Constants & Macros -********************************************************************************/ + * Constants & Macros + *******************************************************************************/ /* Events */ -#define EV_SYN 0x00 -#define EV_KEY 0x01 -#define EV_REL 0x02 -#define EV_ABS 0x03 -#define EV_MSC 0x04 -#define EV_LED 0x11 -#define EV_SND 0x12 -#define EV_REP 0x14 -#define EV_FF 0x15 -#define EV_PWR 0x16 -#define EV_FF_STATUS 0x17 -#define EV_MAX 0x1f +#define EV_SYN 0x00 +#define EV_KEY 0x01 +#define EV_REL 0x02 +#define EV_ABS 0x03 +#define EV_MSC 0x04 +#define EV_LED 0x11 +#define EV_SND 0x12 +#define EV_REP 0x14 +#define EV_FF 0x15 +#define EV_PWR 0x16 +#define EV_FF_STATUS 0x17 +#define EV_MAX 0x1f /* Synchronization events */ -#define SYN_REPORT 0 -#define SYN_CONFIG 1 +#define SYN_REPORT 0 +#define SYN_CONFIG 1 /* Keys and buttons */ -#define KEY_RESERVED 0 -#define KEY_ESC 1 -#define KEY_1 2 -#define KEY_2 3 -#define KEY_3 4 -#define KEY_4 5 -#define KEY_5 6 -#define KEY_6 7 -#define KEY_7 8 -#define KEY_8 9 -#define KEY_9 10 -#define KEY_0 11 -#define KEY_MINUS 12 -#define KEY_EQUAL 13 -#define KEY_BACKSPACE 14 -#define KEY_TAB 15 -#define KEY_Q 16 -#define KEY_W 17 -#define KEY_E 18 -#define KEY_R 19 -#define KEY_T 20 -#define KEY_Y 21 -#define KEY_U 22 -#define KEY_I 23 -#define KEY_O 24 -#define KEY_P 25 -#define KEY_LEFTBRACE 26 -#define KEY_RIGHTBRACE 27 -#define KEY_ENTER 28 -#define KEY_LEFTCTRL 29 -#define KEY_A 30 -#define KEY_S 31 -#define KEY_D 32 -#define KEY_F 33 -#define KEY_G 34 -#define KEY_H 35 -#define KEY_J 36 -#define KEY_K 37 -#define KEY_L 38 -#define KEY_SEMICOLON 39 -#define KEY_APOSTROPHE 40 -#define KEY_GRAVE 41 -#define KEY_LEFTSHIFT 42 -#define KEY_BACKSLASH 43 -#define KEY_Z 44 -#define KEY_X 45 -#define KEY_C 46 -#define KEY_V 47 -#define KEY_B 48 -#define KEY_N 49 -#define KEY_M 50 -#define KEY_COMMA 51 -#define KEY_DOT 52 -#define KEY_SLASH 53 -#define KEY_RIGHTSHIFT 54 -#define KEY_KPASTERISK 55 -#define KEY_LEFTALT 56 -#define KEY_SPACE 57 -#define KEY_CAPSLOCK 58 -#define KEY_F1 59 -#define KEY_F2 60 -#define KEY_F3 61 -#define KEY_F4 62 -#define KEY_F5 63 -#define KEY_F6 64 -#define KEY_F7 65 -#define KEY_F8 66 -#define KEY_F9 67 -#define KEY_F10 68 -#define KEY_NUMLOCK 69 -#define KEY_SCROLLLOCK 70 -#define KEY_KP7 71 -#define KEY_KP8 72 -#define KEY_KP9 73 -#define KEY_KPMINUS 74 -#define KEY_KP4 75 -#define KEY_KP5 76 -#define KEY_KP6 77 -#define KEY_KPPLUS 78 -#define KEY_KP1 79 -#define KEY_KP2 80 -#define KEY_KP3 81 -#define KEY_KP0 82 -#define KEY_KPDOT 83 -#define KEY_103RD 84 -#define KEY_F13 85 -#define KEY_102ND 86 -#define KEY_F11 87 -#define KEY_F12 88 -#define KEY_F14 89 -#define KEY_F15 90 -#define KEY_F16 91 -#define KEY_F17 92 -#define KEY_F18 93 -#define KEY_F19 94 -#define KEY_F20 95 -#define KEY_KPENTER 96 -#define KEY_RIGHTCTRL 97 -#define KEY_KPSLASH 98 -#define KEY_SYSRQ 99 -#define KEY_RIGHTALT 100 -#define KEY_LINEFEED 101 -#define KEY_HOME 102 -#define KEY_UP 103 -#define KEY_PAGEUP 104 -#define KEY_LEFT 105 -#define KEY_RIGHT 106 -#define KEY_END 107 -#define KEY_DOWN 108 -#define KEY_PAGEDOWN 109 -#define KEY_INSERT 110 -#define KEY_DELETE 111 -#define KEY_MACRO 112 -#define KEY_MUTE 113 -#define KEY_VOLUMEDOWN 114 -#define KEY_VOLUMEUP 115 -#define KEY_POWER 116 -#define KEY_KPEQUAL 117 -#define KEY_KPPLUSMINUS 118 -#define KEY_PAUSE 119 -#define KEY_F21 120 -#define KEY_F22 121 -#define KEY_F23 122 -#define KEY_F24 123 -#define KEY_KPCOMMA 124 -#define KEY_LEFTMETA 125 -#define KEY_RIGHTMETA 126 -#define KEY_COMPOSE 127 - -#define KEY_STOP 128 -#define KEY_AGAIN 129 -#define KEY_PROPS 130 -#define KEY_UNDO 131 -#define KEY_FRONT 132 -#define KEY_COPY 133 -#define KEY_OPEN 134 -#define KEY_PASTE 135 -#define KEY_FIND 136 -#define KEY_CUT 137 -#define KEY_HELP 138 -#define KEY_MENU 139 -#define KEY_CALC 140 -#define KEY_SETUP 141 -#define KEY_SLEEP 142 -#define KEY_WAKEUP 143 -#define KEY_FILE 144 -#define KEY_SENDFILE 145 -#define KEY_DELETEFILE 146 -#define KEY_XFER 147 -#define KEY_PROG1 148 -#define KEY_PROG2 149 -#define KEY_WWW 150 -#define KEY_MSDOS 151 -#define KEY_COFFEE 152 -#define KEY_DIRECTION 153 -#define KEY_CYCLEWINDOWS 154 -#define KEY_MAIL 155 -#define KEY_BOOKMARKS 156 -#define KEY_COMPUTER 157 -#define KEY_BACK 158 -#define KEY_FORWARD 159 -#define KEY_CLOSECD 160 -#define KEY_EJECTCD 161 -#define KEY_EJECTCLOSECD 162 -#define KEY_NEXTSONG 163 -#define KEY_PLAYPAUSE 164 -#define KEY_PREVIOUSSONG 165 -#define KEY_STOPCD 166 -#define KEY_RECORD 167 -#define KEY_REWIND 168 -#define KEY_PHONE 169 -#define KEY_ISO 170 -#define KEY_CONFIG 171 -#define KEY_HOMEPAGE 172 -#define KEY_REFRESH 173 -#define KEY_EXIT 174 -#define KEY_MOVE 175 -#define KEY_EDIT 176 -#define KEY_SCROLLUP 177 -#define KEY_SCROLLDOWN 178 -#define KEY_KPLEFTPAREN 179 -#define KEY_KPRIGHTPAREN 180 - -#define KEY_INTL1 181 -#define KEY_INTL2 182 -#define KEY_INTL3 183 -#define KEY_INTL4 184 -#define KEY_INTL5 185 -#define KEY_INTL6 186 -#define KEY_INTL7 187 -#define KEY_INTL8 188 -#define KEY_INTL9 189 -#define KEY_LANG1 190 -#define KEY_LANG2 191 -#define KEY_LANG3 192 -#define KEY_LANG4 193 -#define KEY_LANG5 194 -#define KEY_LANG6 195 -#define KEY_LANG7 196 -#define KEY_LANG8 197 -#define KEY_LANG9 198 - -#define KEY_PLAYCD 200 -#define KEY_PAUSECD 201 -#define KEY_PROG3 202 -#define KEY_PROG4 203 -#define KEY_SUSPEND 205 -#define KEY_CLOSE 206 -#define KEY_PLAY 207 -#define KEY_FAST_FORWARD 208 - -#define KEY_UNKNOWN 220 - -#define KEY_BRIGHTNESSDOWN 224 -#define KEY_BRIGHTNESSUP 225 - -#define BTN_MISC 0x100 -#define BTN_0 0x100 -#define BTN_1 0x101 -#define BTN_2 0x102 -#define BTN_3 0x103 -#define BTN_4 0x104 -#define BTN_5 0x105 -#define BTN_6 0x106 -#define BTN_7 0x107 -#define BTN_8 0x108 -#define BTN_9 0x109 - -#define BTN_MOUSE 0x110 -#define BTN_LEFT 0x110 -#define BTN_RIGHT 0x111 -#define BTN_MIDDLE 0x112 -#define BTN_SIDE 0x113 -#define BTN_EXTRA 0x114 -#define BTN_FORWARD 0x115 -#define BTN_BACK 0x116 -#define BTN_TASK 0x117 - -#define BTN_JOYSTICK 0x120 -#define BTN_TRIGGER 0x120 -#define BTN_THUMB 0x121 -#define BTN_THUMB2 0x122 -#define BTN_TOP 0x123 -#define BTN_TOP2 0x124 -#define BTN_PINKIE 0x125 -#define BTN_BASE 0x126 -#define BTN_BASE2 0x127 -#define BTN_BASE3 0x128 -#define BTN_BASE4 0x129 -#define BTN_BASE5 0x12a -#define BTN_BASE6 0x12b -#define BTN_DEAD 0x12f - -#define BTN_GAMEPAD 0x130 -#define BTN_A 0x130 -#define BTN_B 0x131 -#define BTN_C 0x132 -#define BTN_X 0x133 -#define BTN_Y 0x134 -#define BTN_Z 0x135 -#define BTN_TL 0x136 -#define BTN_TR 0x137 -#define BTN_TL2 0x138 -#define BTN_TR2 0x139 -#define BTN_SELECT 0x13a -#define BTN_START 0x13b -#define BTN_MODE 0x13c -#define BTN_THUMBL 0x13d -#define BTN_THUMBR 0x13e - -#define BTN_DIGI 0x140 -#define BTN_TOOL_PEN 0x140 -#define BTN_TOOL_RUBBER 0x141 -#define BTN_TOOL_BRUSH 0x142 -#define BTN_TOOL_PENCIL 0x143 -#define BTN_TOOL_AIRBRUSH 0x144 -#define BTN_TOOL_FINGER 0x145 -#define BTN_TOOL_MOUSE 0x146 -#define BTN_TOOL_LENS 0x147 -#define BTN_TOUCH 0x14a -#define BTN_STYLUS 0x14b -#define BTN_STYLUS2 0x14c -#define BTN_TOOL_DOUBLETAP 0x14d -#define BTN_TOOL_TRIPLETAP 0x14e - -#define BTN_WHEEL 0x150 -#define BTN_GEAR_DOWN 0x150 -#define BTN_GEAR_UP 0x151 - -#define KEY_OK 0x160 -#define KEY_SELECT 0x161 -#define KEY_GOTO 0x162 -#define KEY_CLEAR 0x163 -#define KEY_POWER2 0x164 -#define KEY_OPTION 0x165 -#define KEY_INFO 0x166 -#define KEY_TIME 0x167 -#define KEY_VENDOR 0x168 -#define KEY_ARCHIVE 0x169 -#define KEY_PROGRAM 0x16a -#define KEY_CHANNEL 0x16b -#define KEY_FAVORITES 0x16c -#define KEY_EPG 0x16d -#define KEY_PVR 0x16e -#define KEY_MHP 0x16f -#define KEY_LANGUAGE 0x170 -#define KEY_TITLE 0x171 -#define KEY_SUBTITLE 0x172 -#define KEY_ANGLE 0x173 -#define KEY_ZOOM 0x174 -#define KEY_MODE 0x175 -#define KEY_KEYBOARD 0x176 -#define KEY_SCREEN 0x177 -#define KEY_PC 0x178 -#define KEY_TV 0x179 -#define KEY_TV2 0x17a -#define KEY_VCR 0x17b -#define KEY_VCR2 0x17c -#define KEY_SAT 0x17d -#define KEY_SAT2 0x17e -#define KEY_CD 0x17f -#define KEY_TAPE 0x180 -#define KEY_RADIO 0x181 -#define KEY_TUNER 0x182 -#define KEY_PLAYER 0x183 -#define KEY_TEXT 0x184 -#define KEY_DVD 0x185 -#define KEY_AUX 0x186 -#define KEY_MP3 0x187 -#define KEY_AUDIO 0x188 -#define KEY_VIDEO 0x189 -#define KEY_DIRECTORY 0x18a -#define KEY_LIST 0x18b -#define KEY_MEMO 0x18c -#define KEY_CALENDAR 0x18d -#define KEY_RED 0x18e -#define KEY_GREEN 0x18f -#define KEY_YELLOW 0x190 -#define KEY_BLUE 0x191 -#define KEY_CHANNELUP 0x192 -#define KEY_CHANNELDOWN 0x193 -#define KEY_FIRST 0x194 -#define KEY_LAST 0x195 -#define KEY_AB 0x196 -#define KEY_NEXT 0x197 -#define KEY_RESTART 0x198 -#define KEY_SLOW 0x199 -#define KEY_SHUFFLE 0x19a -#define KEY_BREAK 0x19b -#define KEY_PREVIOUS 0x19c -#define KEY_DIGITS 0x19d -#define KEY_TEEN 0x19e -#define KEY_TWEN 0x19f - -#define KEY_FRAMEBACK 0x1b2 -#define KEY_FRAMEFORWARD 0x1b3 -#define KEY_CONTEXT_MENU 0x1fb - -#define KEY_MAX 0x1ff +#define KEY_RESERVED 0 +#define KEY_ESC 1 +#define KEY_1 2 +#define KEY_2 3 +#define KEY_3 4 +#define KEY_4 5 +#define KEY_5 6 +#define KEY_6 7 +#define KEY_7 8 +#define KEY_8 9 +#define KEY_9 10 +#define KEY_0 11 +#define KEY_MINUS 12 +#define KEY_EQUAL 13 +#define KEY_BACKSPACE 14 +#define KEY_TAB 15 +#define KEY_Q 16 +#define KEY_W 17 +#define KEY_E 18 +#define KEY_R 19 +#define KEY_T 20 +#define KEY_Y 21 +#define KEY_U 22 +#define KEY_I 23 +#define KEY_O 24 +#define KEY_P 25 +#define KEY_LEFTBRACE 26 +#define KEY_RIGHTBRACE 27 +#define KEY_ENTER 28 +#define KEY_LEFTCTRL 29 +#define KEY_A 30 +#define KEY_S 31 +#define KEY_D 32 +#define KEY_F 33 +#define KEY_G 34 +#define KEY_H 35 +#define KEY_J 36 +#define KEY_K 37 +#define KEY_L 38 +#define KEY_SEMICOLON 39 +#define KEY_APOSTROPHE 40 +#define KEY_GRAVE 41 +#define KEY_LEFTSHIFT 42 +#define KEY_BACKSLASH 43 +#define KEY_Z 44 +#define KEY_X 45 +#define KEY_C 46 +#define KEY_V 47 +#define KEY_B 48 +#define KEY_N 49 +#define KEY_M 50 +#define KEY_COMMA 51 +#define KEY_DOT 52 +#define KEY_SLASH 53 +#define KEY_RIGHTSHIFT 54 +#define KEY_KPASTERISK 55 +#define KEY_LEFTALT 56 +#define KEY_SPACE 57 +#define KEY_CAPSLOCK 58 +#define KEY_F1 59 +#define KEY_F2 60 +#define KEY_F3 61 +#define KEY_F4 62 +#define KEY_F5 63 +#define KEY_F6 64 +#define KEY_F7 65 +#define KEY_F8 66 +#define KEY_F9 67 +#define KEY_F10 68 +#define KEY_NUMLOCK 69 +#define KEY_SCROLLLOCK 70 +#define KEY_KP7 71 +#define KEY_KP8 72 +#define KEY_KP9 73 +#define KEY_KPMINUS 74 +#define KEY_KP4 75 +#define KEY_KP5 76 +#define KEY_KP6 77 +#define KEY_KPPLUS 78 +#define KEY_KP1 79 +#define KEY_KP2 80 +#define KEY_KP3 81 +#define KEY_KP0 82 +#define KEY_KPDOT 83 +#define KEY_103RD 84 +#define KEY_F13 85 +#define KEY_102ND 86 +#define KEY_F11 87 +#define KEY_F12 88 +#define KEY_F14 89 +#define KEY_F15 90 +#define KEY_F16 91 +#define KEY_F17 92 +#define KEY_F18 93 +#define KEY_F19 94 +#define KEY_F20 95 +#define KEY_KPENTER 96 +#define KEY_RIGHTCTRL 97 +#define KEY_KPSLASH 98 +#define KEY_SYSRQ 99 +#define KEY_RIGHTALT 100 +#define KEY_LINEFEED 101 +#define KEY_HOME 102 +#define KEY_UP 103 +#define KEY_PAGEUP 104 +#define KEY_LEFT 105 +#define KEY_RIGHT 106 +#define KEY_END 107 +#define KEY_DOWN 108 +#define KEY_PAGEDOWN 109 +#define KEY_INSERT 110 +#define KEY_DELETE 111 +#define KEY_MACRO 112 +#define KEY_MUTE 113 +#define KEY_VOLUMEDOWN 114 +#define KEY_VOLUMEUP 115 +#define KEY_POWER 116 +#define KEY_KPEQUAL 117 +#define KEY_KPPLUSMINUS 118 +#define KEY_PAUSE 119 +#define KEY_F21 120 +#define KEY_F22 121 +#define KEY_F23 122 +#define KEY_F24 123 +#define KEY_KPCOMMA 124 +#define KEY_LEFTMETA 125 +#define KEY_RIGHTMETA 126 +#define KEY_COMPOSE 127 + +#define KEY_STOP 128 +#define KEY_AGAIN 129 +#define KEY_PROPS 130 +#define KEY_UNDO 131 +#define KEY_FRONT 132 +#define KEY_COPY 133 +#define KEY_OPEN 134 +#define KEY_PASTE 135 +#define KEY_FIND 136 +#define KEY_CUT 137 +#define KEY_HELP 138 +#define KEY_MENU 139 +#define KEY_CALC 140 +#define KEY_SETUP 141 +#define KEY_SLEEP 142 +#define KEY_WAKEUP 143 +#define KEY_FILE 144 +#define KEY_SENDFILE 145 +#define KEY_DELETEFILE 146 +#define KEY_XFER 147 +#define KEY_PROG1 148 +#define KEY_PROG2 149 +#define KEY_WWW 150 +#define KEY_MSDOS 151 +#define KEY_COFFEE 152 +#define KEY_DIRECTION 153 +#define KEY_CYCLEWINDOWS 154 +#define KEY_MAIL 155 +#define KEY_BOOKMARKS 156 +#define KEY_COMPUTER 157 +#define KEY_BACK 158 +#define KEY_FORWARD 159 +#define KEY_CLOSECD 160 +#define KEY_EJECTCD 161 +#define KEY_EJECTCLOSECD 162 +#define KEY_NEXTSONG 163 +#define KEY_PLAYPAUSE 164 +#define KEY_PREVIOUSSONG 165 +#define KEY_STOPCD 166 +#define KEY_RECORD 167 +#define KEY_REWIND 168 +#define KEY_PHONE 169 +#define KEY_ISO 170 +#define KEY_CONFIG 171 +#define KEY_HOMEPAGE 172 +#define KEY_REFRESH 173 +#define KEY_EXIT 174 +#define KEY_MOVE 175 +#define KEY_EDIT 176 +#define KEY_SCROLLUP 177 +#define KEY_SCROLLDOWN 178 +#define KEY_KPLEFTPAREN 179 +#define KEY_KPRIGHTPAREN 180 + +#define KEY_INTL1 181 +#define KEY_INTL2 182 +#define KEY_INTL3 183 +#define KEY_INTL4 184 +#define KEY_INTL5 185 +#define KEY_INTL6 186 +#define KEY_INTL7 187 +#define KEY_INTL8 188 +#define KEY_INTL9 189 +#define KEY_LANG1 190 +#define KEY_LANG2 191 +#define KEY_LANG3 192 +#define KEY_LANG4 193 +#define KEY_LANG5 194 +#define KEY_LANG6 195 +#define KEY_LANG7 196 +#define KEY_LANG8 197 +#define KEY_LANG9 198 + +#define KEY_PLAYCD 200 +#define KEY_PAUSECD 201 +#define KEY_PROG3 202 +#define KEY_PROG4 203 +#define KEY_SUSPEND 205 +#define KEY_CLOSE 206 +#define KEY_PLAY 207 +#define KEY_FAST_FORWARD 208 + +#define KEY_UNKNOWN 220 + +#define KEY_BRIGHTNESSDOWN 224 +#define KEY_BRIGHTNESSUP 225 + +#define BTN_MISC 0x100 +#define BTN_0 0x100 +#define BTN_1 0x101 +#define BTN_2 0x102 +#define BTN_3 0x103 +#define BTN_4 0x104 +#define BTN_5 0x105 +#define BTN_6 0x106 +#define BTN_7 0x107 +#define BTN_8 0x108 +#define BTN_9 0x109 + +#define BTN_MOUSE 0x110 +#define BTN_LEFT 0x110 +#define BTN_RIGHT 0x111 +#define BTN_MIDDLE 0x112 +#define BTN_SIDE 0x113 +#define BTN_EXTRA 0x114 +#define BTN_FORWARD 0x115 +#define BTN_BACK 0x116 +#define BTN_TASK 0x117 + +#define BTN_JOYSTICK 0x120 +#define BTN_TRIGGER 0x120 +#define BTN_THUMB 0x121 +#define BTN_THUMB2 0x122 +#define BTN_TOP 0x123 +#define BTN_TOP2 0x124 +#define BTN_PINKIE 0x125 +#define BTN_BASE 0x126 +#define BTN_BASE2 0x127 +#define BTN_BASE3 0x128 +#define BTN_BASE4 0x129 +#define BTN_BASE5 0x12a +#define BTN_BASE6 0x12b +#define BTN_DEAD 0x12f + +#define BTN_GAMEPAD 0x130 +#define BTN_A 0x130 +#define BTN_B 0x131 +#define BTN_C 0x132 +#define BTN_X 0x133 +#define BTN_Y 0x134 +#define BTN_Z 0x135 +#define BTN_TL 0x136 +#define BTN_TR 0x137 +#define BTN_TL2 0x138 +#define BTN_TR2 0x139 +#define BTN_SELECT 0x13a +#define BTN_START 0x13b +#define BTN_MODE 0x13c +#define BTN_THUMBL 0x13d +#define BTN_THUMBR 0x13e + +#define BTN_DIGI 0x140 +#define BTN_TOOL_PEN 0x140 +#define BTN_TOOL_RUBBER 0x141 +#define BTN_TOOL_BRUSH 0x142 +#define BTN_TOOL_PENCIL 0x143 +#define BTN_TOOL_AIRBRUSH 0x144 +#define BTN_TOOL_FINGER 0x145 +#define BTN_TOOL_MOUSE 0x146 +#define BTN_TOOL_LENS 0x147 +#define BTN_TOUCH 0x14a +#define BTN_STYLUS 0x14b +#define BTN_STYLUS2 0x14c +#define BTN_TOOL_DOUBLETAP 0x14d +#define BTN_TOOL_TRIPLETAP 0x14e + +#define BTN_WHEEL 0x150 +#define BTN_GEAR_DOWN 0x150 +#define BTN_GEAR_UP 0x151 + +#define KEY_OK 0x160 +#define KEY_SELECT 0x161 +#define KEY_GOTO 0x162 +#define KEY_CLEAR 0x163 +#define KEY_POWER2 0x164 +#define KEY_OPTION 0x165 +#define KEY_INFO 0x166 +#define KEY_TIME 0x167 +#define KEY_VENDOR 0x168 +#define KEY_ARCHIVE 0x169 +#define KEY_PROGRAM 0x16a +#define KEY_CHANNEL 0x16b +#define KEY_FAVORITES 0x16c +#define KEY_EPG 0x16d +#define KEY_PVR 0x16e +#define KEY_MHP 0x16f +#define KEY_LANGUAGE 0x170 +#define KEY_TITLE 0x171 +#define KEY_SUBTITLE 0x172 +#define KEY_ANGLE 0x173 +#define KEY_ZOOM 0x174 +#define KEY_MODE 0x175 +#define KEY_KEYBOARD 0x176 +#define KEY_SCREEN 0x177 +#define KEY_PC 0x178 +#define KEY_TV 0x179 +#define KEY_TV2 0x17a +#define KEY_VCR 0x17b +#define KEY_VCR2 0x17c +#define KEY_SAT 0x17d +#define KEY_SAT2 0x17e +#define KEY_CD 0x17f +#define KEY_TAPE 0x180 +#define KEY_RADIO 0x181 +#define KEY_TUNER 0x182 +#define KEY_PLAYER 0x183 +#define KEY_TEXT 0x184 +#define KEY_DVD 0x185 +#define KEY_AUX 0x186 +#define KEY_MP3 0x187 +#define KEY_AUDIO 0x188 +#define KEY_VIDEO 0x189 +#define KEY_DIRECTORY 0x18a +#define KEY_LIST 0x18b +#define KEY_MEMO 0x18c +#define KEY_CALENDAR 0x18d +#define KEY_RED 0x18e +#define KEY_GREEN 0x18f +#define KEY_YELLOW 0x190 +#define KEY_BLUE 0x191 +#define KEY_CHANNELUP 0x192 +#define KEY_CHANNELDOWN 0x193 +#define KEY_FIRST 0x194 +#define KEY_LAST 0x195 +#define KEY_AB 0x196 +#define KEY_NEXT 0x197 +#define KEY_RESTART 0x198 +#define KEY_SLOW 0x199 +#define KEY_SHUFFLE 0x19a +#define KEY_BREAK 0x19b +#define KEY_PREVIOUS 0x19c +#define KEY_DIGITS 0x19d +#define KEY_TEEN 0x19e +#define KEY_TWEN 0x19f + +#define KEY_FRAMEBACK 0x1b2 +#define KEY_FRAMEFORWARD 0x1b3 +#define KEY_CONTEXT_MENU 0x1fb + +#define KEY_MAX 0x1ff /* Relative axes */ -#define REL_X 0x00 -#define REL_Y 0x01 -#define REL_Z 0x02 -#define REL_RX 0x03 -#define REL_RY 0x04 -#define REL_RZ 0x05 -#define REL_HWHEEL 0x06 -#define REL_DIAL 0x07 -#define REL_WHEEL 0x08 -#define REL_MISC 0x09 -#define REL_MAX 0x0f +#define REL_X 0x00 +#define REL_Y 0x01 +#define REL_Z 0x02 +#define REL_RX 0x03 +#define REL_RY 0x04 +#define REL_RZ 0x05 +#define REL_HWHEEL 0x06 +#define REL_DIAL 0x07 +#define REL_WHEEL 0x08 +#define REL_MISC 0x09 +#define REL_MAX 0x0f /* Absolute axes */ -#define ABS_X 0x00 -#define ABS_Y 0x01 -#define ABS_Z 0x02 -#define ABS_RX 0x03 -#define ABS_RY 0x04 -#define ABS_RZ 0x05 -#define ABS_THROTTLE 0x06 -#define ABS_RUDDER 0x07 -#define ABS_WHEEL 0x08 -#define ABS_GAS 0x09 -#define ABS_BRAKE 0x0a -#define ABS_HAT0X 0x10 -#define ABS_HAT0Y 0x11 -#define ABS_HAT1X 0x12 -#define ABS_HAT1Y 0x13 -#define ABS_HAT2X 0x14 -#define ABS_HAT2Y 0x15 -#define ABS_HAT3X 0x16 -#define ABS_HAT3Y 0x17 -#define ABS_PRESSURE 0x18 -#define ABS_DISTANCE 0x19 -#define ABS_TILT_X 0x1a -#define ABS_TILT_Y 0x1b -#define ABS_TOOL_WIDTH 0x1c -#define ABS_VOLUME 0x20 -#define ABS_MISC 0x28 -#define ABS_MAX 0x3f +#define ABS_X 0x00 +#define ABS_Y 0x01 +#define ABS_Z 0x02 +#define ABS_RX 0x03 +#define ABS_RY 0x04 +#define ABS_RZ 0x05 +#define ABS_THROTTLE 0x06 +#define ABS_RUDDER 0x07 +#define ABS_WHEEL 0x08 +#define ABS_GAS 0x09 +#define ABS_BRAKE 0x0a +#define ABS_HAT0X 0x10 +#define ABS_HAT0Y 0x11 +#define ABS_HAT1X 0x12 +#define ABS_HAT1Y 0x13 +#define ABS_HAT2X 0x14 +#define ABS_HAT2Y 0x15 +#define ABS_HAT3X 0x16 +#define ABS_HAT3Y 0x17 +#define ABS_PRESSURE 0x18 +#define ABS_DISTANCE 0x19 +#define ABS_TILT_X 0x1a +#define ABS_TILT_Y 0x1b +#define ABS_TOOL_WIDTH 0x1c +#define ABS_VOLUME 0x20 +#define ABS_MISC 0x28 +#define ABS_MAX 0x3f /* Switch events */ -#define SW_0 0x00 -#define SW_1 0x01 -#define SW_2 0x02 -#define SW_3 0x03 -#define SW_4 0x04 -#define SW_5 0x05 -#define SW_6 0x06 -#define SW_7 0x07 -#define SW_MAX 0x0f +#define SW_0 0x00 +#define SW_1 0x01 +#define SW_2 0x02 +#define SW_3 0x03 +#define SW_4 0x04 +#define SW_5 0x05 +#define SW_6 0x06 +#define SW_7 0x07 +#define SW_MAX 0x0f /* Misc events */ -#define MSC_SERIAL 0x00 -#define MSC_PULSELED 0x01 -#define MSC_GESTURE 0x02 -#define MSC_RAW 0x03 -#define MSC_SCAN 0x04 -#define MSC_MAX 0x07 +#define MSC_SERIAL 0x00 +#define MSC_PULSELED 0x01 +#define MSC_GESTURE 0x02 +#define MSC_RAW 0x03 +#define MSC_SCAN 0x04 +#define MSC_MAX 0x07 /* LEDs */ -#define LED_NUML 0x00 -#define LED_CAPSL 0x01 -#define LED_SCROLLL 0x02 -#define LED_COMPOSE 0x03 -#define LED_KANA 0x04 -#define LED_SLEEP 0x05 -#define LED_SUSPEND 0x06 -#define LED_MUTE 0x07 -#define LED_MISC 0x08 -#define LED_MAIL 0x09 -#define LED_CHARGING 0x0a -#define LED_MAX 0x0f +#define LED_NUML 0x00 +#define LED_CAPSL 0x01 +#define LED_SCROLLL 0x02 +#define LED_COMPOSE 0x03 +#define LED_KANA 0x04 +#define LED_SLEEP 0x05 +#define LED_SUSPEND 0x06 +#define LED_MUTE 0x07 +#define LED_MISC 0x08 +#define LED_MAIL 0x09 +#define LED_CHARGING 0x0a +#define LED_MAX 0x0f /* Autorepeat values */ -#define REP_DELAY 0x00 -#define REP_PERIOD 0x01 -#define REP_MAX 0x01 +#define REP_DELAY 0x00 +#define REP_PERIOD 0x01 +#define REP_MAX 0x01 /* Sounds */ -#define SND_CLICK 0x00 -#define SND_BELL 0x01 -#define SND_TONE 0x02 -#define SND_MAX 0x07 +#define SND_CLICK 0x00 +#define SND_BELL 0x01 +#define SND_TONE 0x02 +#define SND_MAX 0x07 /* Identifiers */ -#define ID_BUS 0 -#define ID_VENDOR 1 -#define ID_PRODUCT 2 -#define ID_VERSION 3 - -#define BUS_PCI 0x01 -#define BUS_ISAPNP 0x02 -#define BUS_USB 0x03 -#define BUS_HIL 0x04 -#define BUS_BLUETOOTH 0x05 - -#define BUS_ISA 0x10 -#define BUS_I8042 0x11 -#define BUS_XTKBD 0x12 -#define BUS_RS232 0x13 -#define BUS_GAMEPORT 0x14 -#define BUS_PARPORT 0x15 -#define BUS_AMIGA 0x16 -#define BUS_ADB 0x17 -#define BUS_I2C 0x18 -#define BUS_HOST 0x19 -#define BUS_GSC 0x1A +#define ID_BUS 0 +#define ID_VENDOR 1 +#define ID_PRODUCT 2 +#define ID_VERSION 3 + +#define BUS_PCI 0x01 +#define BUS_ISAPNP 0x02 +#define BUS_USB 0x03 +#define BUS_HIL 0x04 +#define BUS_BLUETOOTH 0x05 + +#define BUS_ISA 0x10 +#define BUS_I8042 0x11 +#define BUS_XTKBD 0x12 +#define BUS_RS232 0x13 +#define BUS_GAMEPORT 0x14 +#define BUS_PARPORT 0x15 +#define BUS_AMIGA 0x16 +#define BUS_ADB 0x17 +#define BUS_I2C 0x18 +#define BUS_HOST 0x19 +#define BUS_GSC 0x1A /* User input interface */ -#define UINPUT_IOCTL_BASE 'U' +#define UINPUT_IOCTL_BASE 'U' -#define UI_DEV_CREATE _IO(UINPUT_IOCTL_BASE, 1) -#define UI_DEV_DESTROY _IO(UINPUT_IOCTL_BASE, 2) +#define UI_DEV_CREATE _IO(UINPUT_IOCTL_BASE, 1) +#define UI_DEV_DESTROY _IO(UINPUT_IOCTL_BASE, 2) -#define UI_SET_EVBIT _IOW(UINPUT_IOCTL_BASE, 100, int) -#define UI_SET_KEYBIT _IOW(UINPUT_IOCTL_BASE, 101, int) -#define UI_SET_RELBIT _IOW(UINPUT_IOCTL_BASE, 102, int) -#define UI_SET_ABSBIT _IOW(UINPUT_IOCTL_BASE, 103, int) -#define UI_SET_MSCBIT _IOW(UINPUT_IOCTL_BASE, 104, int) -#define UI_SET_LEDBIT _IOW(UINPUT_IOCTL_BASE, 105, int) -#define UI_SET_SNDBIT _IOW(UINPUT_IOCTL_BASE, 106, int) -#define UI_SET_FFBIT _IOW(UINPUT_IOCTL_BASE, 107, int) -#define UI_SET_PHYS _IOW(UINPUT_IOCTL_BASE, 108, char*) -#define UI_SET_SWBIT _IOW(UINPUT_IOCTL_BASE, 109, int) +#define UI_SET_EVBIT _IOW(UINPUT_IOCTL_BASE, 100, int) +#define UI_SET_KEYBIT _IOW(UINPUT_IOCTL_BASE, 101, int) +#define UI_SET_RELBIT _IOW(UINPUT_IOCTL_BASE, 102, int) +#define UI_SET_ABSBIT _IOW(UINPUT_IOCTL_BASE, 103, int) +#define UI_SET_MSCBIT _IOW(UINPUT_IOCTL_BASE, 104, int) +#define UI_SET_LEDBIT _IOW(UINPUT_IOCTL_BASE, 105, int) +#define UI_SET_SNDBIT _IOW(UINPUT_IOCTL_BASE, 106, int) +#define UI_SET_FFBIT _IOW(UINPUT_IOCTL_BASE, 107, int) +#define UI_SET_PHYS _IOW(UINPUT_IOCTL_BASE, 108, char*) +#define UI_SET_SWBIT _IOW(UINPUT_IOCTL_BASE, 109, int) #ifndef NBITS -#define NBITS(x) ((((x) - 1) / (sizeof(long) * 8)) + 1) +#define NBITS(x) ((((x)-1) / (sizeof(long) * 8)) + 1) #endif -#define UINPUT_MAX_NAME_SIZE 80 - +#define UINPUT_MAX_NAME_SIZE 80 /******************************************************************************* -** Type definitions and return values -********************************************************************************/ + * Type definitions and return values + *******************************************************************************/ struct uinput_id { - uint16_t bustype; - uint16_t vendor; - uint16_t product; - uint16_t version; + uint16_t bustype; + uint16_t vendor; + uint16_t product; + uint16_t version; }; struct uinput_dev { - char name[UINPUT_MAX_NAME_SIZE]; - struct uinput_id id; - int ff_effects_max; - int absmax[ABS_MAX + 1]; - int absmin[ABS_MAX + 1]; - int absfuzz[ABS_MAX + 1]; - int absflat[ABS_MAX + 1]; + char name[UINPUT_MAX_NAME_SIZE]; + struct uinput_id id; + int ff_effects_max; + int absmax[ABS_MAX + 1]; + int absmin[ABS_MAX + 1]; + int absfuzz[ABS_MAX + 1]; + int absflat[ABS_MAX + 1]; }; struct uinput_event { - struct timeval time; - uint16_t type; - uint16_t code; - int32_t value; + struct timeval time; + uint16_t type; + uint16_t code; + int32_t value; }; #endif /* __UINPUT_H */ diff --git a/btif/src/bluetooth.cc b/btif/src/bluetooth.cc index 0598761a5..2e47b600f 100644 --- a/btif/src/bluetooth.cc +++ b/btif/src/bluetooth.cc @@ -46,90 +46,87 @@ #include #include "bt_utils.h" +#include "btif/include/btif_debug_btsnoop.h" +#include "btif/include/btif_debug_conn.h" #include "btif_a2dp.h" #include "btif_api.h" +#include "btif_config.h" #include "btif_debug.h" +#include "btif_storage.h" #include "btsnoop.h" #include "btsnoop_mem.h" #include "device/include/interop.h" -#include "osi/include/allocation_tracker.h" #include "osi/include/alarm.h" +#include "osi/include/allocation_tracker.h" #include "osi/include/log.h" #include "osi/include/metrics.h" #include "osi/include/osi.h" #include "osi/include/wakelock.h" #include "stack_manager.h" -#include "btif_config.h" -#include "btif_storage.h" -#include "btif/include/btif_debug_btsnoop.h" -#include "btif/include/btif_debug_conn.h" /************************************************************************************ -** Static variables -************************************************************************************/ + * Static variables + ***********************************************************************************/ -bt_callbacks_t *bt_hal_cbacks = NULL; +bt_callbacks_t* bt_hal_cbacks = NULL; bool restricted_mode = false; /************************************************************************************ -** Externs -************************************************************************************/ + * Externs + ***********************************************************************************/ /* list all extended interfaces here */ /* handsfree profile */ -extern bthf_interface_t *btif_hf_get_interface(); +extern bthf_interface_t* btif_hf_get_interface(); /* handsfree profile - client */ -extern bthf_client_interface_t *btif_hf_client_get_interface(); +extern bthf_client_interface_t* btif_hf_client_get_interface(); /* advanced audio profile */ -extern btav_interface_t *btif_av_get_src_interface(); -extern btav_interface_t *btif_av_get_sink_interface(); +extern btav_interface_t* btif_av_get_src_interface(); +extern btav_interface_t* btif_av_get_sink_interface(); /*rfc l2cap*/ -extern btsock_interface_t *btif_sock_get_interface(); +extern btsock_interface_t* btif_sock_get_interface(); /* hid host profile */ -extern bthh_interface_t *btif_hh_get_interface(); +extern bthh_interface_t* btif_hh_get_interface(); /* health device profile */ -extern bthl_interface_t *btif_hl_get_interface(); +extern bthl_interface_t* btif_hl_get_interface(); /*pan*/ -extern btpan_interface_t *btif_pan_get_interface(); +extern btpan_interface_t* btif_pan_get_interface(); /*map client*/ -extern btmce_interface_t *btif_mce_get_interface(); +extern btmce_interface_t* btif_mce_get_interface(); #if (BLE_INCLUDED == TRUE) /* gatt */ -extern const btgatt_interface_t *btif_gatt_get_interface(); +extern const btgatt_interface_t* btif_gatt_get_interface(); #endif /* avrc target */ -extern btrc_interface_t *btif_rc_get_interface(); +extern btrc_interface_t* btif_rc_get_interface(); /* avrc controller */ -extern btrc_interface_t *btif_rc_ctrl_get_interface(); +extern btrc_interface_t* btif_rc_ctrl_get_interface(); /*SDP search client*/ -extern btsdp_interface_t *btif_sdp_get_interface(); +extern btsdp_interface_t* btif_sdp_get_interface(); /************************************************************************************ -** Functions -************************************************************************************/ + * Functions + ***********************************************************************************/ -static bool interface_ready(void) { - return bt_hal_cbacks != NULL; -} +static bool interface_ready(void) { return bt_hal_cbacks != NULL; } -static bool is_profile(const char *p1, const char *p2) { +static bool is_profile(const char* p1, const char* p2) { assert(p1); assert(p2); return strlen(p1) == strlen(p2) && strncmp(p1, p2, strlen(p2)) == 0; } /***************************************************************************** -** -** BLUETOOTH HAL INTERFACE FUNCTIONS -** -*****************************************************************************/ + * + * BLUETOOTH HAL INTERFACE FUNCTIONS + * + ****************************************************************************/ -static int init(bt_callbacks_t *callbacks) { +static int init(bt_callbacks_t* callbacks) { LOG_INFO(LOG_TAG, "%s", __func__); - if (interface_ready()) - return BT_STATUS_DONE; + if (interface_ready()) return BT_STATUS_DONE; #ifdef BLUEDROID_DEBUG allocation_tracker_init(); @@ -146,327 +143,276 @@ static int enable(bool start_restricted) { restricted_mode = start_restricted; - if (!interface_ready()) - return BT_STATUS_NOT_READY; + if (!interface_ready()) return BT_STATUS_NOT_READY; stack_manager_get_interface()->start_up_stack_async(); return BT_STATUS_SUCCESS; } static int disable(void) { - if (!interface_ready()) - return BT_STATUS_NOT_READY; + if (!interface_ready()) return BT_STATUS_NOT_READY; stack_manager_get_interface()->shut_down_stack_async(); return BT_STATUS_SUCCESS; } -static void cleanup(void) { - stack_manager_get_interface()->clean_up_stack(); -} +static void cleanup(void) { stack_manager_get_interface()->clean_up_stack(); } -bool is_restricted_mode() { - return restricted_mode; -} +bool is_restricted_mode() { return restricted_mode; } -static int get_adapter_properties(void) -{ - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; +static int get_adapter_properties(void) { + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_get_adapter_properties(); + return btif_get_adapter_properties(); } -static int get_adapter_property(bt_property_type_t type) -{ - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; +static int get_adapter_property(bt_property_type_t type) { + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_get_adapter_property(type); + return btif_get_adapter_property(type); } -static int set_adapter_property(const bt_property_t *property) -{ - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; +static int set_adapter_property(const bt_property_t* property) { + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_set_adapter_property(property); + return btif_set_adapter_property(property); } -int get_remote_device_properties(bt_bdaddr_t *remote_addr) -{ - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; +int get_remote_device_properties(bt_bdaddr_t* remote_addr) { + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_get_remote_device_properties(remote_addr); + return btif_get_remote_device_properties(remote_addr); } -int get_remote_device_property(bt_bdaddr_t *remote_addr, bt_property_type_t type) -{ - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; +int get_remote_device_property(bt_bdaddr_t* remote_addr, + bt_property_type_t type) { + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_get_remote_device_property(remote_addr, type); + return btif_get_remote_device_property(remote_addr, type); } -int set_remote_device_property(bt_bdaddr_t *remote_addr, const bt_property_t *property) -{ - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; +int set_remote_device_property(bt_bdaddr_t* remote_addr, + const bt_property_t* property) { + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_set_remote_device_property(remote_addr, property); + return btif_set_remote_device_property(remote_addr, property); } -int get_remote_service_record(bt_bdaddr_t *remote_addr, bt_uuid_t *uuid) -{ - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; +int get_remote_service_record(bt_bdaddr_t* remote_addr, bt_uuid_t* uuid) { + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_get_remote_service_record(remote_addr, uuid); + return btif_get_remote_service_record(remote_addr, uuid); } -int get_remote_services(bt_bdaddr_t *remote_addr) -{ - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; +int get_remote_services(bt_bdaddr_t* remote_addr) { + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_dm_get_remote_services(remote_addr); + return btif_dm_get_remote_services(remote_addr); } -static int start_discovery(void) -{ - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; +static int start_discovery(void) { + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_dm_start_discovery(); + return btif_dm_start_discovery(); } -static int cancel_discovery(void) -{ - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; +static int cancel_discovery(void) { + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_dm_cancel_discovery(); + return btif_dm_cancel_discovery(); } -static int create_bond(const bt_bdaddr_t *bd_addr, int transport) -{ - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; +static int create_bond(const bt_bdaddr_t* bd_addr, int transport) { + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_dm_create_bond(bd_addr, transport); + return btif_dm_create_bond(bd_addr, transport); } -static int create_bond_out_of_band(const bt_bdaddr_t *bd_addr, int transport, - const bt_out_of_band_data_t *oob_data) -{ - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; +static int create_bond_out_of_band(const bt_bdaddr_t* bd_addr, int transport, + const bt_out_of_band_data_t* oob_data) { + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_dm_create_bond_out_of_band(bd_addr, transport, oob_data); + return btif_dm_create_bond_out_of_band(bd_addr, transport, oob_data); } -static int cancel_bond(const bt_bdaddr_t *bd_addr) -{ - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; +static int cancel_bond(const bt_bdaddr_t* bd_addr) { + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_dm_cancel_bond(bd_addr); + return btif_dm_cancel_bond(bd_addr); } -static int remove_bond(const bt_bdaddr_t *bd_addr) -{ - if (is_restricted_mode() && !btif_storage_is_restricted_device(bd_addr)) - return BT_STATUS_SUCCESS; +static int remove_bond(const bt_bdaddr_t* bd_addr) { + if (is_restricted_mode() && !btif_storage_is_restricted_device(bd_addr)) + return BT_STATUS_SUCCESS; - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_dm_remove_bond(bd_addr); + return btif_dm_remove_bond(bd_addr); } -static int get_connection_state(const bt_bdaddr_t *bd_addr) -{ - /* sanity check */ - if (interface_ready() == false) - return 0; +static int get_connection_state(const bt_bdaddr_t* bd_addr) { + /* sanity check */ + if (interface_ready() == false) return 0; - return btif_dm_get_connection_state(bd_addr); + return btif_dm_get_connection_state(bd_addr); } -static int pin_reply(const bt_bdaddr_t *bd_addr, uint8_t accept, - uint8_t pin_len, bt_pin_code_t *pin_code) -{ - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; +static int pin_reply(const bt_bdaddr_t* bd_addr, uint8_t accept, + uint8_t pin_len, bt_pin_code_t* pin_code) { + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_dm_pin_reply(bd_addr, accept, pin_len, pin_code); + return btif_dm_pin_reply(bd_addr, accept, pin_len, pin_code); } -static int ssp_reply(const bt_bdaddr_t *bd_addr, bt_ssp_variant_t variant, - uint8_t accept, uint32_t passkey) -{ - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; +static int ssp_reply(const bt_bdaddr_t* bd_addr, bt_ssp_variant_t variant, + uint8_t accept, uint32_t passkey) { + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_dm_ssp_reply(bd_addr, variant, accept, passkey); + return btif_dm_ssp_reply(bd_addr, variant, accept, passkey); } -static int read_energy_info() -{ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; - btif_dm_read_energy_info(); - return BT_STATUS_SUCCESS; +static int read_energy_info() { + if (interface_ready() == false) return BT_STATUS_NOT_READY; + btif_dm_read_energy_info(); + return BT_STATUS_SUCCESS; } -static void dump(int fd, const char **arguments) -{ - if (arguments != NULL && arguments[0] != NULL) { - if (strncmp(arguments[0], "--proto-text", 12) == 0) { - btif_update_a2dp_metrics(); - metrics_print(fd, true); - return; - } - if (strncmp(arguments[0], "--proto-bin", 11) == 0) { - btif_update_a2dp_metrics(); - metrics_write(fd, true); - return; - } +static void dump(int fd, const char** arguments) { + if (arguments != NULL && arguments[0] != NULL) { + if (strncmp(arguments[0], "--proto-text", 12) == 0) { + btif_update_a2dp_metrics(); + metrics_print(fd, true); + return; + } + if (strncmp(arguments[0], "--proto-bin", 11) == 0) { + btif_update_a2dp_metrics(); + metrics_write(fd, true); + return; } - btif_debug_conn_dump(fd); - btif_debug_bond_event_dump(fd); - btif_debug_a2dp_dump(fd); - btif_debug_config_dump(fd); - wakelock_debug_dump(fd); - alarm_debug_dump(fd); + } + btif_debug_conn_dump(fd); + btif_debug_bond_event_dump(fd); + btif_debug_a2dp_dump(fd); + btif_debug_config_dump(fd); + wakelock_debug_dump(fd); + alarm_debug_dump(fd); #if (BTSNOOP_MEM == TRUE) - btif_debug_btsnoop_dump(fd); + btif_debug_btsnoop_dump(fd); #endif - close(fd); + close(fd); } -static const void* get_profile_interface (const char *profile_id) -{ - LOG_INFO(LOG_TAG, "get_profile_interface %s", profile_id); +static const void* get_profile_interface(const char* profile_id) { + LOG_INFO(LOG_TAG, "get_profile_interface %s", profile_id); - /* sanity check */ - if (interface_ready() == false) - return NULL; + /* sanity check */ + if (interface_ready() == false) return NULL; - /* check for supported profile interfaces */ - if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID)) - return btif_hf_get_interface(); + /* check for supported profile interfaces */ + if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID)) + return btif_hf_get_interface(); - if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID)) - return btif_hf_client_get_interface(); + if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID)) + return btif_hf_client_get_interface(); - if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID)) - return btif_sock_get_interface(); + if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID)) + return btif_sock_get_interface(); - if (is_profile(profile_id, BT_PROFILE_PAN_ID)) - return btif_pan_get_interface(); + if (is_profile(profile_id, BT_PROFILE_PAN_ID)) + return btif_pan_get_interface(); - if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID)) - return btif_av_get_src_interface(); + if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID)) + return btif_av_get_src_interface(); - if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID)) - return btif_av_get_sink_interface(); + if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID)) + return btif_av_get_sink_interface(); - if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID)) - return btif_hh_get_interface(); + if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID)) + return btif_hh_get_interface(); - if (is_profile(profile_id, BT_PROFILE_HEALTH_ID)) - return btif_hl_get_interface(); + if (is_profile(profile_id, BT_PROFILE_HEALTH_ID)) + return btif_hl_get_interface(); - if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID)) - return btif_sdp_get_interface(); + if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID)) + return btif_sdp_get_interface(); #if (BTA_GATT_INCLUDED == TRUE && BLE_INCLUDED == TRUE) - if (is_profile(profile_id, BT_PROFILE_GATT_ID)) - return btif_gatt_get_interface(); + if (is_profile(profile_id, BT_PROFILE_GATT_ID)) + return btif_gatt_get_interface(); #endif - if (is_profile(profile_id, BT_PROFILE_AV_RC_ID)) - return btif_rc_get_interface(); + if (is_profile(profile_id, BT_PROFILE_AV_RC_ID)) + return btif_rc_get_interface(); - if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID)) - return btif_rc_ctrl_get_interface(); + if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID)) + return btif_rc_ctrl_get_interface(); - return NULL; + return NULL; } -int dut_mode_configure(uint8_t enable) -{ - LOG_INFO(LOG_TAG, "dut_mode_configure"); +int dut_mode_configure(uint8_t enable) { + LOG_INFO(LOG_TAG, "dut_mode_configure"); - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_dut_mode_configure(enable); + return btif_dut_mode_configure(enable); } -int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) -{ - LOG_INFO(LOG_TAG, "dut_mode_send"); +int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) { + LOG_INFO(LOG_TAG, "dut_mode_send"); - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_dut_mode_send(opcode, buf, len); + return btif_dut_mode_send(opcode, buf, len); } #if (BLE_INCLUDED == TRUE) -int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) -{ - LOG_INFO(LOG_TAG, "le_test_mode"); +int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) { + LOG_INFO(LOG_TAG, "le_test_mode"); - /* sanity check */ - if (interface_ready() == false) - return BT_STATUS_NOT_READY; + /* sanity check */ + if (interface_ready() == false) return BT_STATUS_NOT_READY; - return btif_le_test_mode(opcode, buf, len); + return btif_le_test_mode(opcode, buf, len); } #endif -int config_hci_snoop_log(uint8_t enable) -{ - LOG_INFO(LOG_TAG, "config_hci_snoop_log"); +int config_hci_snoop_log(uint8_t enable) { + LOG_INFO(LOG_TAG, "config_hci_snoop_log"); - if (!interface_ready()) - return BT_STATUS_NOT_READY; + if (!interface_ready()) return BT_STATUS_NOT_READY; - btsnoop_get_interface()->set_api_wants_to_log(enable); - return BT_STATUS_SUCCESS; + btsnoop_get_interface()->set_api_wants_to_log(enable); + return BT_STATUS_SUCCESS; } -static int set_os_callouts(bt_os_callouts_t *callouts) { - wakelock_set_os_callouts(callouts); - return BT_STATUS_SUCCESS; +static int set_os_callouts(bt_os_callouts_t* callouts) { + wakelock_set_os_callouts(callouts); + return BT_STATUS_SUCCESS; } static int config_clear(void) { - LOG_INFO(LOG_TAG, "%s", __func__); - return btif_config_clear() ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; + LOG_INFO(LOG_TAG, "%s", __func__); + return btif_config_clear() ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } static const bt_interface_t bluetoothInterface = { @@ -509,28 +455,28 @@ static const bt_interface_t bluetoothInterface = { interop_database_add, }; -const bt_interface_t* bluetooth__get_bluetooth_interface () -{ - /* fixme -- add property to disable bt interface ? */ +const bt_interface_t* bluetooth__get_bluetooth_interface() { + /* fixme -- add property to disable bt interface ? */ - return &bluetoothInterface; + return &bluetoothInterface; } -static int close_bluetooth_stack(struct hw_device_t* device) -{ - UNUSED(device); - cleanup(); - return 0; +static int close_bluetooth_stack(struct hw_device_t* device) { + UNUSED(device); + cleanup(); + return 0; } -static int open_bluetooth_stack(const struct hw_module_t *module, UNUSED_ATTR char const *name, struct hw_device_t **abstraction) { +static int open_bluetooth_stack(const struct hw_module_t* module, + UNUSED_ATTR char const* name, + struct hw_device_t** abstraction) { static bluetooth_device_t device; device.common.tag = HARDWARE_DEVICE_TAG; device.common.version = 0; device.common.close = close_bluetooth_stack; device.get_bluetooth_interface = bluetooth__get_bluetooth_interface; - device.common.module = (struct hw_module_t *)module; - *abstraction = (struct hw_device_t *)&device; + device.common.module = (struct hw_module_t*)module; + *abstraction = (struct hw_device_t*)&device; return 0; } @@ -545,5 +491,4 @@ EXPORT_SYMBOL struct hw_module_t HAL_MODULE_INFO_SYM = { .id = BT_HARDWARE_MODULE_ID, .name = "Bluetooth Stack", .author = "The Android Open Source Project", - .methods = &bt_stack_module_methods -}; + .methods = &bt_stack_module_methods}; diff --git a/btif/src/btif_av.cc b/btif/src/btif_av.cc index 297f96f64..a753680af 100644 --- a/btif/src/btif_av.cc +++ b/btif/src/btif_av.cc @@ -28,6 +28,7 @@ #include #include "audio_a2dp_hw.h" +#include "bt_common.h" #include "bt_utils.h" #include "bta_api.h" #include "btif_a2dp.h" @@ -37,23 +38,22 @@ #include "btif_profile_queue.h" #include "btif_util.h" #include "btu.h" -#include "bt_common.h" #include "osi/include/allocator.h" /***************************************************************************** -** Constants & Macros -******************************************************************************/ + * Constants & Macros + *****************************************************************************/ #define BTIF_AV_SERVICE_NAME "Advanced Audio" #define BTIF_AVK_SERVICE_NAME "Advanced Audio Sink" -#define BTIF_TIMEOUT_AV_OPEN_ON_RC_MS (2 * 1000) +#define BTIF_TIMEOUT_AV_OPEN_ON_RC_MS (2 * 1000) typedef enum { - BTIF_AV_STATE_IDLE = 0x0, - BTIF_AV_STATE_OPENING, - BTIF_AV_STATE_OPENED, - BTIF_AV_STATE_STARTED, - BTIF_AV_STATE_CLOSING + BTIF_AV_STATE_IDLE = 0x0, + BTIF_AV_STATE_OPENING, + BTIF_AV_STATE_OPENED, + BTIF_AV_STATE_STARTED, + BTIF_AV_STATE_CLOSING } btif_av_state_t; /* Should not need dedicated suspend state as actual actions are no @@ -62,1350 +62,1285 @@ typedef enum { we are in the process of a local suspend */ #define BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING 0x1 -#define BTIF_AV_FLAG_REMOTE_SUSPEND 0x2 -#define BTIF_AV_FLAG_PENDING_START 0x4 -#define BTIF_AV_FLAG_PENDING_STOP 0x8 +#define BTIF_AV_FLAG_REMOTE_SUSPEND 0x2 +#define BTIF_AV_FLAG_PENDING_START 0x4 +#define BTIF_AV_FLAG_PENDING_STOP 0x8 /***************************************************************************** -** Local type definitions -******************************************************************************/ - -typedef struct -{ - tBTA_AV_HNDL bta_handle; - bt_bdaddr_t peer_bda; - btif_sm_handle_t sm_handle; - uint8_t flags; - tBTA_AV_EDR edr; - uint8_t peer_sep; /* sep type of peer device */ + * Local type definitions + *****************************************************************************/ + +typedef struct { + tBTA_AV_HNDL bta_handle; + bt_bdaddr_t peer_bda; + btif_sm_handle_t sm_handle; + uint8_t flags; + tBTA_AV_EDR edr; + uint8_t peer_sep; /* sep type of peer device */ } btif_av_cb_t; -typedef struct -{ - bt_bdaddr_t *target_bda; - uint16_t uuid; +typedef struct { + bt_bdaddr_t* target_bda; + uint16_t uuid; } btif_av_connect_req_t; -typedef struct -{ - int sample_rate; - int channel_count; - bt_bdaddr_t peer_bd; +typedef struct { + int sample_rate; + int channel_count; + bt_bdaddr_t peer_bd; } btif_av_sink_config_req_t; /***************************************************************************** -** Static variables -******************************************************************************/ -static btav_callbacks_t *bt_av_src_callbacks = NULL; -static btav_callbacks_t *bt_av_sink_callbacks = NULL; + * Static variables + *****************************************************************************/ +static btav_callbacks_t* bt_av_src_callbacks = NULL; +static btav_callbacks_t* bt_av_sink_callbacks = NULL; static btif_av_cb_t btif_av_cb = {0, {{0}}, 0, 0, 0, 0}; -static alarm_t *av_open_on_rc_timer = NULL; +static alarm_t* av_open_on_rc_timer = NULL; /* both interface and media task needs to be ready to alloc incoming request */ -#define CHECK_BTAV_INIT() if (((bt_av_src_callbacks == NULL) &&(bt_av_sink_callbacks == NULL)) \ - || (btif_av_cb.sm_handle == NULL))\ -{\ - BTIF_TRACE_WARNING("%s: BTAV not initialized", __func__);\ - return BT_STATUS_NOT_READY;\ -}\ -else\ -{\ - BTIF_TRACE_EVENT("%s", __func__);\ -} +#define CHECK_BTAV_INIT() \ + if (((bt_av_src_callbacks == NULL) && (bt_av_sink_callbacks == NULL)) || \ + (btif_av_cb.sm_handle == NULL)) { \ + BTIF_TRACE_WARNING("%s: BTAV not initialized", __func__); \ + return BT_STATUS_NOT_READY; \ + } else { \ + BTIF_TRACE_EVENT("%s", __func__); \ + } /* Helper macro to avoid code duplication in the state machine handlers */ -#define CHECK_RC_EVENT(e, d) \ - case BTA_AV_RC_OPEN_EVT: \ - case BTA_AV_RC_BROWSE_OPEN_EVT: \ - case BTA_AV_RC_CLOSE_EVT: \ - case BTA_AV_RC_BROWSE_CLOSE_EVT: \ - case BTA_AV_REMOTE_CMD_EVT: \ - case BTA_AV_VENDOR_CMD_EVT: \ - case BTA_AV_META_MSG_EVT: \ - case BTA_AV_RC_FEAT_EVT: \ - case BTA_AV_REMOTE_RSP_EVT: \ - { \ - btif_rc_handler(e, d);\ - }break; \ - -static bool btif_av_state_idle_handler(btif_sm_event_t event, void *data); -static bool btif_av_state_opening_handler(btif_sm_event_t event, void *data); -static bool btif_av_state_opened_handler(btif_sm_event_t event, void *data); -static bool btif_av_state_started_handler(btif_sm_event_t event, void *data); -static bool btif_av_state_closing_handler(btif_sm_event_t event, void *data); - -static const btif_sm_handler_t btif_av_state_handlers[] = -{ - btif_av_state_idle_handler, - btif_av_state_opening_handler, - btif_av_state_opened_handler, - btif_av_state_started_handler, - btif_av_state_closing_handler -}; - -static void btif_av_event_free_data(btif_sm_event_t event, void *p_data); +#define CHECK_RC_EVENT(e, d) \ + case BTA_AV_RC_OPEN_EVT: \ + case BTA_AV_RC_BROWSE_OPEN_EVT: \ + case BTA_AV_RC_CLOSE_EVT: \ + case BTA_AV_RC_BROWSE_CLOSE_EVT: \ + case BTA_AV_REMOTE_CMD_EVT: \ + case BTA_AV_VENDOR_CMD_EVT: \ + case BTA_AV_META_MSG_EVT: \ + case BTA_AV_RC_FEAT_EVT: \ + case BTA_AV_REMOTE_RSP_EVT: { \ + btif_rc_handler(e, d); \ + } break; + +static bool btif_av_state_idle_handler(btif_sm_event_t event, void* data); +static bool btif_av_state_opening_handler(btif_sm_event_t event, void* data); +static bool btif_av_state_opened_handler(btif_sm_event_t event, void* data); +static bool btif_av_state_started_handler(btif_sm_event_t event, void* data); +static bool btif_av_state_closing_handler(btif_sm_event_t event, void* data); + +static const btif_sm_handler_t btif_av_state_handlers[] = { + btif_av_state_idle_handler, btif_av_state_opening_handler, + btif_av_state_opened_handler, btif_av_state_started_handler, + btif_av_state_closing_handler}; + +static void btif_av_event_free_data(btif_sm_event_t event, void* p_data); /************************************************************************* -** Extern functions -*************************************************************************/ -extern void btif_rc_handler(tBTA_AV_EVT event, tBTA_AV *p_data); + * Extern functions + ************************************************************************/ +extern void btif_rc_handler(tBTA_AV_EVT event, tBTA_AV* p_data); extern bool btif_rc_get_connected_peer(BD_ADDR peer_addr); extern uint8_t btif_rc_get_connected_peer_handle(BD_ADDR peer_addr); -extern void btif_rc_check_handle_pending_play (BD_ADDR peer_addr, bool bSendToApp); +extern void btif_rc_check_handle_pending_play(BD_ADDR peer_addr, + bool bSendToApp); -extern fixed_queue_t *btu_general_alarm_queue; +extern fixed_queue_t* btu_general_alarm_queue; /***************************************************************************** -** Local helper functions -******************************************************************************/ - -const char *dump_av_sm_state_name(btif_av_state_t state) -{ - switch (state) - { - CASE_RETURN_STR(BTIF_AV_STATE_IDLE) - CASE_RETURN_STR(BTIF_AV_STATE_OPENING) - CASE_RETURN_STR(BTIF_AV_STATE_OPENED) - CASE_RETURN_STR(BTIF_AV_STATE_STARTED) - CASE_RETURN_STR(BTIF_AV_STATE_CLOSING) - default: return "UNKNOWN_STATE"; - } + * Local helper functions + *****************************************************************************/ + +const char* dump_av_sm_state_name(btif_av_state_t state) { + switch (state) { + CASE_RETURN_STR(BTIF_AV_STATE_IDLE) + CASE_RETURN_STR(BTIF_AV_STATE_OPENING) + CASE_RETURN_STR(BTIF_AV_STATE_OPENED) + CASE_RETURN_STR(BTIF_AV_STATE_STARTED) + CASE_RETURN_STR(BTIF_AV_STATE_CLOSING) + default: + return "UNKNOWN_STATE"; + } } -const char *dump_av_sm_event_name(btif_av_sm_event_t event) -{ - switch((int)event) - { - CASE_RETURN_STR(BTA_AV_ENABLE_EVT) - CASE_RETURN_STR(BTA_AV_REGISTER_EVT) - CASE_RETURN_STR(BTA_AV_OPEN_EVT) - CASE_RETURN_STR(BTA_AV_CLOSE_EVT) - CASE_RETURN_STR(BTA_AV_START_EVT) - CASE_RETURN_STR(BTA_AV_STOP_EVT) - CASE_RETURN_STR(BTA_AV_PROTECT_REQ_EVT) - CASE_RETURN_STR(BTA_AV_PROTECT_RSP_EVT) - CASE_RETURN_STR(BTA_AV_RC_OPEN_EVT) - CASE_RETURN_STR(BTA_AV_RC_CLOSE_EVT) - CASE_RETURN_STR(BTA_AV_RC_BROWSE_OPEN_EVT) - CASE_RETURN_STR(BTA_AV_RC_BROWSE_CLOSE_EVT) - CASE_RETURN_STR(BTA_AV_REMOTE_CMD_EVT) - CASE_RETURN_STR(BTA_AV_REMOTE_RSP_EVT) - CASE_RETURN_STR(BTA_AV_VENDOR_CMD_EVT) - CASE_RETURN_STR(BTA_AV_VENDOR_RSP_EVT) - CASE_RETURN_STR(BTA_AV_RECONFIG_EVT) - CASE_RETURN_STR(BTA_AV_SUSPEND_EVT) - CASE_RETURN_STR(BTA_AV_PENDING_EVT) - CASE_RETURN_STR(BTA_AV_META_MSG_EVT) - CASE_RETURN_STR(BTA_AV_REJECT_EVT) - CASE_RETURN_STR(BTA_AV_RC_FEAT_EVT) - CASE_RETURN_STR(BTA_AV_OFFLOAD_START_RSP_EVT) - CASE_RETURN_STR(BTIF_SM_ENTER_EVT) - CASE_RETURN_STR(BTIF_SM_EXIT_EVT) - CASE_RETURN_STR(BTIF_AV_CONNECT_REQ_EVT) - CASE_RETURN_STR(BTIF_AV_DISCONNECT_REQ_EVT) - CASE_RETURN_STR(BTIF_AV_START_STREAM_REQ_EVT) - CASE_RETURN_STR(BTIF_AV_STOP_STREAM_REQ_EVT) - CASE_RETURN_STR(BTIF_AV_SUSPEND_STREAM_REQ_EVT) - CASE_RETURN_STR(BTIF_AV_SINK_CONFIG_REQ_EVT) - CASE_RETURN_STR(BTIF_AV_OFFLOAD_START_REQ_EVT) - default: return "UNKNOWN_EVENT"; - } +const char* dump_av_sm_event_name(btif_av_sm_event_t event) { + switch ((int)event) { + CASE_RETURN_STR(BTA_AV_ENABLE_EVT) + CASE_RETURN_STR(BTA_AV_REGISTER_EVT) + CASE_RETURN_STR(BTA_AV_OPEN_EVT) + CASE_RETURN_STR(BTA_AV_CLOSE_EVT) + CASE_RETURN_STR(BTA_AV_START_EVT) + CASE_RETURN_STR(BTA_AV_STOP_EVT) + CASE_RETURN_STR(BTA_AV_PROTECT_REQ_EVT) + CASE_RETURN_STR(BTA_AV_PROTECT_RSP_EVT) + CASE_RETURN_STR(BTA_AV_RC_OPEN_EVT) + CASE_RETURN_STR(BTA_AV_RC_CLOSE_EVT) + CASE_RETURN_STR(BTA_AV_RC_BROWSE_OPEN_EVT) + CASE_RETURN_STR(BTA_AV_RC_BROWSE_CLOSE_EVT) + CASE_RETURN_STR(BTA_AV_REMOTE_CMD_EVT) + CASE_RETURN_STR(BTA_AV_REMOTE_RSP_EVT) + CASE_RETURN_STR(BTA_AV_VENDOR_CMD_EVT) + CASE_RETURN_STR(BTA_AV_VENDOR_RSP_EVT) + CASE_RETURN_STR(BTA_AV_RECONFIG_EVT) + CASE_RETURN_STR(BTA_AV_SUSPEND_EVT) + CASE_RETURN_STR(BTA_AV_PENDING_EVT) + CASE_RETURN_STR(BTA_AV_META_MSG_EVT) + CASE_RETURN_STR(BTA_AV_REJECT_EVT) + CASE_RETURN_STR(BTA_AV_RC_FEAT_EVT) + CASE_RETURN_STR(BTA_AV_OFFLOAD_START_RSP_EVT) + CASE_RETURN_STR(BTIF_SM_ENTER_EVT) + CASE_RETURN_STR(BTIF_SM_EXIT_EVT) + CASE_RETURN_STR(BTIF_AV_CONNECT_REQ_EVT) + CASE_RETURN_STR(BTIF_AV_DISCONNECT_REQ_EVT) + CASE_RETURN_STR(BTIF_AV_START_STREAM_REQ_EVT) + CASE_RETURN_STR(BTIF_AV_STOP_STREAM_REQ_EVT) + CASE_RETURN_STR(BTIF_AV_SUSPEND_STREAM_REQ_EVT) + CASE_RETURN_STR(BTIF_AV_SINK_CONFIG_REQ_EVT) + CASE_RETURN_STR(BTIF_AV_OFFLOAD_START_REQ_EVT) + default: + return "UNKNOWN_EVENT"; + } } /**************************************************************************** -** Local helper functions -*****************************************************************************/ + * Local helper functions + ****************************************************************************/ /******************************************************************************* -** -** Function btif_initiate_av_open_timer_timeout -** -** Description Timer to trigger AV open if the remote headset establishes -** RC connection w/o AV connection. The timer is needed to IOP -** with headsets that do establish AV after RC connection. -** -** Returns void -** -*******************************************************************************/ -static void btif_initiate_av_open_timer_timeout(UNUSED_ATTR void *data) -{ - BD_ADDR peer_addr; - btif_av_connect_req_t connect_req; - - /* is there at least one RC connection - There should be */ - if (btif_rc_get_connected_peer(peer_addr)) { - BTIF_TRACE_DEBUG("%s Issuing connect to the remote RC peer", __func__); - /* In case of AVRCP connection request, we will initiate SRC connection */ - connect_req.target_bda = (bt_bdaddr_t*)&peer_addr; - if(bt_av_sink_callbacks != NULL) - connect_req.uuid = UUID_SERVCLASS_AUDIO_SINK; - else if(bt_av_src_callbacks != NULL) - connect_req.uuid = UUID_SERVCLASS_AUDIO_SOURCE; - btif_sm_dispatch(btif_av_cb.sm_handle, BTIF_AV_CONNECT_REQ_EVT, (char*)&connect_req); - } - else - { - BTIF_TRACE_ERROR("%s No connected RC peers", __func__); - } + * + * Function btif_initiate_av_open_timer_timeout + * + * Description Timer to trigger AV open if the remote headset establishes + * RC connection w/o AV connection. The timer is needed to IOP + * with headsets that do establish AV after RC connection. + * + * Returns void + * + ******************************************************************************/ +static void btif_initiate_av_open_timer_timeout(UNUSED_ATTR void* data) { + BD_ADDR peer_addr; + btif_av_connect_req_t connect_req; + + /* is there at least one RC connection - There should be */ + if (btif_rc_get_connected_peer(peer_addr)) { + BTIF_TRACE_DEBUG("%s Issuing connect to the remote RC peer", __func__); + /* In case of AVRCP connection request, we will initiate SRC connection */ + connect_req.target_bda = (bt_bdaddr_t*)&peer_addr; + if (bt_av_sink_callbacks != NULL) + connect_req.uuid = UUID_SERVCLASS_AUDIO_SINK; + else if (bt_av_src_callbacks != NULL) + connect_req.uuid = UUID_SERVCLASS_AUDIO_SOURCE; + btif_sm_dispatch(btif_av_cb.sm_handle, BTIF_AV_CONNECT_REQ_EVT, + (char*)&connect_req); + } else { + BTIF_TRACE_ERROR("%s No connected RC peers", __func__); + } } /***************************************************************************** -** Static functions -******************************************************************************/ + * Static functions + *****************************************************************************/ /******************************************************************************* -** -** Function btif_report_connection_state -** -** Description Updates the components via the callbacks about the connection -** state of a2dp connection. -** -** Returns None -** -*******************************************************************************/ -static void btif_report_connection_state(btav_connection_state_t state, bt_bdaddr_t *bd_addr) -{ - if (bt_av_sink_callbacks != NULL) { - HAL_CBACK(bt_av_sink_callbacks, connection_state_cb, state, bd_addr); - } else if (bt_av_src_callbacks != NULL) { - HAL_CBACK(bt_av_src_callbacks, connection_state_cb, state, bd_addr); - } + * + * Function btif_report_connection_state + * + * Description Updates the components via the callbacks about the + *connection + * state of a2dp connection. + * + * Returns None + * + ******************************************************************************/ +static void btif_report_connection_state(btav_connection_state_t state, + bt_bdaddr_t* bd_addr) { + if (bt_av_sink_callbacks != NULL) { + HAL_CBACK(bt_av_sink_callbacks, connection_state_cb, state, bd_addr); + } else if (bt_av_src_callbacks != NULL) { + HAL_CBACK(bt_av_src_callbacks, connection_state_cb, state, bd_addr); + } } /******************************************************************************* -** -** Function btif_report_audio_state -** -** Description Updates the components via the callbacks about the audio -** state of a2dp connection. The state is updated when either -** the remote ends starts streaming (started state) or whenever -** it transitions out of started state (to opened or streaming) -** state. -** -** Returns None -** -*******************************************************************************/ -static void btif_report_audio_state(btav_audio_state_t state, bt_bdaddr_t *bd_addr) -{ - if (bt_av_sink_callbacks != NULL) { - HAL_CBACK(bt_av_sink_callbacks, audio_state_cb, state, bd_addr); - } else if (bt_av_src_callbacks != NULL) { - HAL_CBACK(bt_av_src_callbacks, audio_state_cb, state, bd_addr); - } + * + * Function btif_report_audio_state + * + * Description Updates the components via the callbacks about the audio + * state of a2dp connection. The state is updated when either + * the remote ends starts streaming (started state) or whenever + * it transitions out of started state (to opened or streaming) + * state. + * + * Returns None + * + ******************************************************************************/ +static void btif_report_audio_state(btav_audio_state_t state, + bt_bdaddr_t* bd_addr) { + if (bt_av_sink_callbacks != NULL) { + HAL_CBACK(bt_av_sink_callbacks, audio_state_cb, state, bd_addr); + } else if (bt_av_src_callbacks != NULL) { + HAL_CBACK(bt_av_src_callbacks, audio_state_cb, state, bd_addr); + } } /***************************************************************************** -** -** Function btif_av_state_idle_handler -** -** Description State managing disconnected AV link -** -** Returns true if event was processed, false otherwise -** -*******************************************************************************/ - -static bool btif_av_state_idle_handler(btif_sm_event_t event, void *p_data) -{ - BTIF_TRACE_DEBUG("%s event:%s flags %x", __func__, - dump_av_sm_event_name((btif_av_sm_event_t)event), btif_av_cb.flags); - - switch (event) - { - case BTIF_SM_ENTER_EVT: - /* clear the peer_bda */ - memset(&btif_av_cb.peer_bda, 0, sizeof(bt_bdaddr_t)); - btif_av_cb.flags = 0; - btif_av_cb.edr = 0; - bta_av_co_init(); - btif_a2dp_on_idle(); - break; - - case BTIF_SM_EXIT_EVT: - break; - - case BTA_AV_ENABLE_EVT: - break; - - case BTA_AV_REGISTER_EVT: - btif_av_cb.bta_handle = ((tBTA_AV*)p_data)->registr.hndl; - break; - - case BTA_AV_PENDING_EVT: - case BTIF_AV_CONNECT_REQ_EVT: - { - if (event == BTIF_AV_CONNECT_REQ_EVT) - { - memcpy(&btif_av_cb.peer_bda, ((btif_av_connect_req_t*)p_data)->target_bda, - sizeof(bt_bdaddr_t)); - BTA_AvOpen(btif_av_cb.peer_bda.address, btif_av_cb.bta_handle, - true, BTA_SEC_AUTHENTICATE, ((btif_av_connect_req_t*)p_data)->uuid); - } - else if (event == BTA_AV_PENDING_EVT) - { - bdcpy(btif_av_cb.peer_bda.address, ((tBTA_AV*)p_data)->pend.bd_addr); - if (bt_av_src_callbacks != NULL) - { - BTA_AvOpen(btif_av_cb.peer_bda.address, btif_av_cb.bta_handle, - true, BTA_SEC_AUTHENTICATE, UUID_SERVCLASS_AUDIO_SOURCE); - } - if (bt_av_sink_callbacks != NULL) - { - BTA_AvOpen(btif_av_cb.peer_bda.address, btif_av_cb.bta_handle, - true, BTA_SEC_AUTHENTICATE, UUID_SERVCLASS_AUDIO_SINK); - } - } - btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_OPENING); - } break; - - case BTA_AV_RC_OPEN_EVT: - /* IOP_FIX: Jabra 620 only does RC open without AV open whenever it connects. So - * as per the AV WP, an AVRC connection cannot exist without an AV connection. Therefore, - * we initiate an AV connection if an RC_OPEN_EVT is received when we are in AV_CLOSED state. - * We initiate the AV connection after a small 3s timeout to avoid any collisions from the - * headsets, as some headsets initiate the AVRC connection first and then - * immediately initiate the AV connection - * - * TODO: We may need to do this only on an AVRCP Play. FixMe - */ - - BTIF_TRACE_DEBUG("BTA_AV_RC_OPEN_EVT received w/o AV"); - alarm_set_on_queue(av_open_on_rc_timer, - BTIF_TIMEOUT_AV_OPEN_ON_RC_MS, - btif_initiate_av_open_timer_timeout, NULL, - btu_general_alarm_queue); - btif_rc_handler(event, (tBTA_AV*)p_data); - break; - - case BTA_AV_RC_BROWSE_OPEN_EVT: - BTIF_TRACE_DEBUG("BTA_AV_RC_BROWSE_OPEN_EVT received"); - btif_rc_handler(event, (tBTA_AV*)p_data); - break; - - /* - * In case Signalling channel is not down - * and remote started Streaming Procedure - * we have to handle config and open event in - * idle_state. We hit these scenarios while running - * PTS test case for AVRCP Controller - */ - case BTIF_AV_SINK_CONFIG_REQ_EVT: - { - btif_av_sink_config_req_t req; - // copy to avoid alignment problems - memcpy(&req, p_data, sizeof(req)); - - BTIF_TRACE_WARNING("BTIF_AV_SINK_CONFIG_REQ_EVT %d %d", req.sample_rate, - req.channel_count); - if (bt_av_sink_callbacks != NULL) { - HAL_CBACK(bt_av_sink_callbacks, audio_config_cb, &(req.peer_bd), - req.sample_rate, req.channel_count); - } - } break; - - case BTA_AV_OPEN_EVT: - { - tBTA_AV *p_bta_data = (tBTA_AV*)p_data; - btav_connection_state_t state; - btif_sm_state_t av_state; - BTIF_TRACE_DEBUG("status:%d, edr 0x%x",p_bta_data->open.status, - p_bta_data->open.edr); - - if (p_bta_data->open.status == BTA_AV_SUCCESS) - { - state = BTAV_CONNECTION_STATE_CONNECTED; - av_state = BTIF_AV_STATE_OPENED; - btif_av_cb.edr = p_bta_data->open.edr; - - btif_av_cb.peer_sep = p_bta_data->open.sep; - } - else - { - BTIF_TRACE_WARNING("BTA_AV_OPEN_EVT::FAILED status: %d", - p_bta_data->open.status ); - state = BTAV_CONNECTION_STATE_DISCONNECTED; - av_state = BTIF_AV_STATE_IDLE; - } - - /* inform the application of the event */ - btif_report_connection_state(state, &(btif_av_cb.peer_bda)); - /* change state to open/idle based on the status */ - btif_sm_change_state(btif_av_cb.sm_handle, av_state); - if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) - { - /* if queued PLAY command, send it now */ - btif_rc_check_handle_pending_play(p_bta_data->open.bd_addr, - (p_bta_data->open.status == BTA_AV_SUCCESS)); - } - else if ((btif_av_cb.peer_sep == AVDT_TSEP_SRC) && - (p_bta_data->open.status == BTA_AV_SUCCESS)) - { - /* Bring up AVRCP connection too */ - BTA_AvOpenRc(btif_av_cb.bta_handle); - } - btif_queue_advance(); - } break; - - case BTA_AV_REMOTE_CMD_EVT: - case BTA_AV_VENDOR_CMD_EVT: - case BTA_AV_META_MSG_EVT: - case BTA_AV_RC_FEAT_EVT: - case BTA_AV_REMOTE_RSP_EVT: - btif_rc_handler(event, (tBTA_AV*)p_data); - break; - - case BTA_AV_RC_CLOSE_EVT: - BTIF_TRACE_DEBUG("BTA_AV_RC_CLOSE_EVT: Stopping AV timer."); - alarm_cancel(av_open_on_rc_timer); - btif_rc_handler(event, (tBTA_AV*)p_data); - break; - - case BTIF_AV_OFFLOAD_START_REQ_EVT: - BTIF_TRACE_ERROR("BTIF_AV_OFFLOAD_START_REQ_EVT: Stream not Started IDLE"); - btif_a2dp_on_offload_started(BTA_AV_FAIL); - break; - - default: - BTIF_TRACE_WARNING("%s : unhandled event:%s", __func__, - dump_av_sm_event_name((btif_av_sm_event_t)event)); - return false; + * + * Function btif_av_state_idle_handler + * + * Description State managing disconnected AV link + * + * Returns true if event was processed, false otherwise + * + ******************************************************************************/ - } +static bool btif_av_state_idle_handler(btif_sm_event_t event, void* p_data) { + BTIF_TRACE_DEBUG("%s event:%s flags %x", __func__, + dump_av_sm_event_name((btif_av_sm_event_t)event), + btif_av_cb.flags); + + switch (event) { + case BTIF_SM_ENTER_EVT: + /* clear the peer_bda */ + memset(&btif_av_cb.peer_bda, 0, sizeof(bt_bdaddr_t)); + btif_av_cb.flags = 0; + btif_av_cb.edr = 0; + bta_av_co_init(); + btif_a2dp_on_idle(); + break; + + case BTIF_SM_EXIT_EVT: + break; + + case BTA_AV_ENABLE_EVT: + break; + + case BTA_AV_REGISTER_EVT: + btif_av_cb.bta_handle = ((tBTA_AV*)p_data)->registr.hndl; + break; + + case BTA_AV_PENDING_EVT: + case BTIF_AV_CONNECT_REQ_EVT: { + if (event == BTIF_AV_CONNECT_REQ_EVT) { + memcpy(&btif_av_cb.peer_bda, + ((btif_av_connect_req_t*)p_data)->target_bda, + sizeof(bt_bdaddr_t)); + BTA_AvOpen(btif_av_cb.peer_bda.address, btif_av_cb.bta_handle, true, + BTA_SEC_AUTHENTICATE, + ((btif_av_connect_req_t*)p_data)->uuid); + } else if (event == BTA_AV_PENDING_EVT) { + bdcpy(btif_av_cb.peer_bda.address, ((tBTA_AV*)p_data)->pend.bd_addr); + if (bt_av_src_callbacks != NULL) { + BTA_AvOpen(btif_av_cb.peer_bda.address, btif_av_cb.bta_handle, true, + BTA_SEC_AUTHENTICATE, UUID_SERVCLASS_AUDIO_SOURCE); + } + if (bt_av_sink_callbacks != NULL) { + BTA_AvOpen(btif_av_cb.peer_bda.address, btif_av_cb.bta_handle, true, + BTA_SEC_AUTHENTICATE, UUID_SERVCLASS_AUDIO_SINK); + } + } + btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_OPENING); + } break; + + case BTA_AV_RC_OPEN_EVT: + /* IOP_FIX: Jabra 620 only does RC open without AV open whenever it + * connects. So + * as per the AV WP, an AVRC connection cannot exist without an AV + * connection. Therefore, + * we initiate an AV connection if an RC_OPEN_EVT is received when we are + * in AV_CLOSED state. + * We initiate the AV connection after a small 3s timeout to avoid any + * collisions from the + * headsets, as some headsets initiate the AVRC connection first and then + * immediately initiate the AV connection + * + * TODO: We may need to do this only on an AVRCP Play. FixMe + */ + + BTIF_TRACE_DEBUG("BTA_AV_RC_OPEN_EVT received w/o AV"); + alarm_set_on_queue(av_open_on_rc_timer, BTIF_TIMEOUT_AV_OPEN_ON_RC_MS, + btif_initiate_av_open_timer_timeout, NULL, + btu_general_alarm_queue); + btif_rc_handler(event, (tBTA_AV*)p_data); + break; + + case BTA_AV_RC_BROWSE_OPEN_EVT: + BTIF_TRACE_DEBUG("BTA_AV_RC_BROWSE_OPEN_EVT received"); + btif_rc_handler(event, (tBTA_AV*)p_data); + break; + + /* + * In case Signalling channel is not down + * and remote started Streaming Procedure + * we have to handle config and open event in + * idle_state. We hit these scenarios while running + * PTS test case for AVRCP Controller + */ + case BTIF_AV_SINK_CONFIG_REQ_EVT: { + btif_av_sink_config_req_t req; + // copy to avoid alignment problems + memcpy(&req, p_data, sizeof(req)); + + BTIF_TRACE_WARNING("BTIF_AV_SINK_CONFIG_REQ_EVT %d %d", req.sample_rate, + req.channel_count); + if (bt_av_sink_callbacks != NULL) { + HAL_CBACK(bt_av_sink_callbacks, audio_config_cb, &(req.peer_bd), + req.sample_rate, req.channel_count); + } + } break; + + case BTA_AV_OPEN_EVT: { + tBTA_AV* p_bta_data = (tBTA_AV*)p_data; + btav_connection_state_t state; + btif_sm_state_t av_state; + BTIF_TRACE_DEBUG("status:%d, edr 0x%x", p_bta_data->open.status, + p_bta_data->open.edr); + + if (p_bta_data->open.status == BTA_AV_SUCCESS) { + state = BTAV_CONNECTION_STATE_CONNECTED; + av_state = BTIF_AV_STATE_OPENED; + btif_av_cb.edr = p_bta_data->open.edr; + + btif_av_cb.peer_sep = p_bta_data->open.sep; + } else { + BTIF_TRACE_WARNING("BTA_AV_OPEN_EVT::FAILED status: %d", + p_bta_data->open.status); + state = BTAV_CONNECTION_STATE_DISCONNECTED; + av_state = BTIF_AV_STATE_IDLE; + } + + /* inform the application of the event */ + btif_report_connection_state(state, &(btif_av_cb.peer_bda)); + /* change state to open/idle based on the status */ + btif_sm_change_state(btif_av_cb.sm_handle, av_state); + if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) { + /* if queued PLAY command, send it now */ + btif_rc_check_handle_pending_play( + p_bta_data->open.bd_addr, + (p_bta_data->open.status == BTA_AV_SUCCESS)); + } else if ((btif_av_cb.peer_sep == AVDT_TSEP_SRC) && + (p_bta_data->open.status == BTA_AV_SUCCESS)) { + /* Bring up AVRCP connection too */ + BTA_AvOpenRc(btif_av_cb.bta_handle); + } + btif_queue_advance(); + } break; + + case BTA_AV_REMOTE_CMD_EVT: + case BTA_AV_VENDOR_CMD_EVT: + case BTA_AV_META_MSG_EVT: + case BTA_AV_RC_FEAT_EVT: + case BTA_AV_REMOTE_RSP_EVT: + btif_rc_handler(event, (tBTA_AV*)p_data); + break; + + case BTA_AV_RC_CLOSE_EVT: + BTIF_TRACE_DEBUG("BTA_AV_RC_CLOSE_EVT: Stopping AV timer."); + alarm_cancel(av_open_on_rc_timer); + btif_rc_handler(event, (tBTA_AV*)p_data); + break; + + case BTIF_AV_OFFLOAD_START_REQ_EVT: + BTIF_TRACE_ERROR( + "BTIF_AV_OFFLOAD_START_REQ_EVT: Stream not Started IDLE"); + btif_a2dp_on_offload_started(BTA_AV_FAIL); + break; - return true; + default: + BTIF_TRACE_WARNING("%s : unhandled event:%s", __func__, + dump_av_sm_event_name((btif_av_sm_event_t)event)); + return false; + } + + return true; } /***************************************************************************** -** -** Function btif_av_state_opening_handler -** -** Description Intermediate state managing events during establishment -** of avdtp channel -** -** Returns true if event was processed, false otherwise -** -*******************************************************************************/ - -static bool btif_av_state_opening_handler(btif_sm_event_t event, void *p_data) -{ - BTIF_TRACE_DEBUG("%s event:%s flags %x", __func__, - dump_av_sm_event_name((btif_av_sm_event_t)event), btif_av_cb.flags); - - switch (event) - { - case BTIF_SM_ENTER_EVT: - /* inform the application that we are entering connecting state */ - btif_report_connection_state(BTAV_CONNECTION_STATE_CONNECTING, &(btif_av_cb.peer_bda)); - break; - - case BTIF_SM_EXIT_EVT: - break; - - case BTA_AV_REJECT_EVT: - BTIF_TRACE_DEBUG(" Received BTA_AV_REJECT_EVT "); - btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb.peer_bda)); - btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE); - break; - - case BTA_AV_OPEN_EVT: - { - tBTA_AV *p_bta_data = (tBTA_AV*)p_data; - btav_connection_state_t state; - btif_sm_state_t av_state; - BTIF_TRACE_DEBUG("status:%d, edr 0x%x",p_bta_data->open.status, - p_bta_data->open.edr); - - if (p_bta_data->open.status == BTA_AV_SUCCESS) - { - state = BTAV_CONNECTION_STATE_CONNECTED; - av_state = BTIF_AV_STATE_OPENED; - btif_av_cb.edr = p_bta_data->open.edr; - - btif_av_cb.peer_sep = p_bta_data->open.sep; - } - else - { - BTIF_TRACE_WARNING("BTA_AV_OPEN_EVT::FAILED status: %d", - p_bta_data->open.status ); - BD_ADDR peer_addr; - uint8_t peer_handle = BTRC_HANDLE_NONE; - if ((btif_rc_get_connected_peer(peer_addr)) - &&(!bdcmp(btif_av_cb.peer_bda.address, peer_addr))) - { - /* - * Disconnect AVRCP connection, if - * A2DP conneciton failed, for any reason - */ - BTIF_TRACE_WARNING(" Disconnecting AVRCP "); - peer_handle = btif_rc_get_connected_peer_handle(peer_addr); - if (peer_handle != BTRC_HANDLE_NONE) { - BTA_AvCloseRc(peer_handle); - } - } - state = BTAV_CONNECTION_STATE_DISCONNECTED; - av_state = BTIF_AV_STATE_IDLE; - } - - /* inform the application of the event */ - btif_report_connection_state(state, &(btif_av_cb.peer_bda)); - /* change state to open/idle based on the status */ - btif_sm_change_state(btif_av_cb.sm_handle, av_state); - if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) - { - /* if queued PLAY command, send it now */ - btif_rc_check_handle_pending_play(p_bta_data->open.bd_addr, - (p_bta_data->open.status == BTA_AV_SUCCESS)); - } - else if ((btif_av_cb.peer_sep == AVDT_TSEP_SRC) && - (p_bta_data->open.status == BTA_AV_SUCCESS)) - { - /* Bring up AVRCP connection too */ - BTA_AvOpenRc(btif_av_cb.bta_handle); - } - btif_queue_advance(); - } break; - - case BTIF_AV_SINK_CONFIG_REQ_EVT: - { - btif_av_sink_config_req_t req; - // copy to avoid alignment problems - memcpy(&req, p_data, sizeof(req)); - - BTIF_TRACE_WARNING("BTIF_AV_SINK_CONFIG_REQ_EVT %d %d", req.sample_rate, - req.channel_count); - if (btif_av_cb.peer_sep == AVDT_TSEP_SRC && bt_av_sink_callbacks != NULL) { - HAL_CBACK(bt_av_sink_callbacks, audio_config_cb, &(btif_av_cb.peer_bda), - req.sample_rate, req.channel_count); - } - } break; - - case BTIF_AV_CONNECT_REQ_EVT: - // Check for device, if same device which moved to opening then ignore callback - if (memcmp ((bt_bdaddr_t*)p_data, &(btif_av_cb.peer_bda), - sizeof(btif_av_cb.peer_bda)) == 0) - { - BTIF_TRACE_DEBUG("%s: Same device moved to Opening state,ignore Connect Req", __func__); - btif_queue_advance(); - break; - } - else - { - BTIF_TRACE_DEBUG("%s: Moved from idle by Incoming Connection request", __func__); - btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, (bt_bdaddr_t*)p_data); - btif_queue_advance(); - break; - } - - case BTA_AV_PENDING_EVT: - // Check for device, if same device which moved to opening then ignore callback - if (memcmp (((tBTA_AV*)p_data)->pend.bd_addr, &(btif_av_cb.peer_bda), - sizeof(btif_av_cb.peer_bda)) == 0) - { - BTIF_TRACE_DEBUG("%s: Same device moved to Opening state,ignore Pending Req", __func__); - break; - } - else - { - BTIF_TRACE_DEBUG("%s: Moved from idle by outgoing Connection request", __func__); - BTA_AvDisconnect(((tBTA_AV*)p_data)->pend.bd_addr); - break; - } - - case BTIF_AV_OFFLOAD_START_REQ_EVT: - BTIF_TRACE_ERROR("BTIF_AV_OFFLOAD_START_REQ_EVT: Stream not Started OPENING"); - btif_a2dp_on_offload_started(BTA_AV_FAIL); - break; - - case BTA_AV_CLOSE_EVT: - btif_a2dp_on_stopped(NULL); - btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, - &(btif_av_cb.peer_bda)); - btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE); - break; - - CHECK_RC_EVENT(event, (tBTA_AV*)p_data); - - default: - BTIF_TRACE_WARNING("%s : unhandled event:%s", __func__, - dump_av_sm_event_name((btif_av_sm_event_t)event)); - return false; - - } - return true; + * + * Function btif_av_state_opening_handler + * + * Description Intermediate state managing events during establishment + * of avdtp channel + * + * Returns true if event was processed, false otherwise + * + ******************************************************************************/ + +static bool btif_av_state_opening_handler(btif_sm_event_t event, void* p_data) { + BTIF_TRACE_DEBUG("%s event:%s flags %x", __func__, + dump_av_sm_event_name((btif_av_sm_event_t)event), + btif_av_cb.flags); + + switch (event) { + case BTIF_SM_ENTER_EVT: + /* inform the application that we are entering connecting state */ + btif_report_connection_state(BTAV_CONNECTION_STATE_CONNECTING, + &(btif_av_cb.peer_bda)); + break; + + case BTIF_SM_EXIT_EVT: + break; + + case BTA_AV_REJECT_EVT: + BTIF_TRACE_DEBUG(" Received BTA_AV_REJECT_EVT "); + btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, + &(btif_av_cb.peer_bda)); + btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE); + break; + + case BTA_AV_OPEN_EVT: { + tBTA_AV* p_bta_data = (tBTA_AV*)p_data; + btav_connection_state_t state; + btif_sm_state_t av_state; + BTIF_TRACE_DEBUG("status:%d, edr 0x%x", p_bta_data->open.status, + p_bta_data->open.edr); + + if (p_bta_data->open.status == BTA_AV_SUCCESS) { + state = BTAV_CONNECTION_STATE_CONNECTED; + av_state = BTIF_AV_STATE_OPENED; + btif_av_cb.edr = p_bta_data->open.edr; + + btif_av_cb.peer_sep = p_bta_data->open.sep; + } else { + BTIF_TRACE_WARNING("BTA_AV_OPEN_EVT::FAILED status: %d", + p_bta_data->open.status); + BD_ADDR peer_addr; + uint8_t peer_handle = BTRC_HANDLE_NONE; + if ((btif_rc_get_connected_peer(peer_addr)) && + (!bdcmp(btif_av_cb.peer_bda.address, peer_addr))) { + /* + * Disconnect AVRCP connection, if + * A2DP conneciton failed, for any reason + */ + BTIF_TRACE_WARNING(" Disconnecting AVRCP "); + peer_handle = btif_rc_get_connected_peer_handle(peer_addr); + if (peer_handle != BTRC_HANDLE_NONE) { + BTA_AvCloseRc(peer_handle); + } + } + state = BTAV_CONNECTION_STATE_DISCONNECTED; + av_state = BTIF_AV_STATE_IDLE; + } + + /* inform the application of the event */ + btif_report_connection_state(state, &(btif_av_cb.peer_bda)); + /* change state to open/idle based on the status */ + btif_sm_change_state(btif_av_cb.sm_handle, av_state); + if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) { + /* if queued PLAY command, send it now */ + btif_rc_check_handle_pending_play( + p_bta_data->open.bd_addr, + (p_bta_data->open.status == BTA_AV_SUCCESS)); + } else if ((btif_av_cb.peer_sep == AVDT_TSEP_SRC) && + (p_bta_data->open.status == BTA_AV_SUCCESS)) { + /* Bring up AVRCP connection too */ + BTA_AvOpenRc(btif_av_cb.bta_handle); + } + btif_queue_advance(); + } break; + + case BTIF_AV_SINK_CONFIG_REQ_EVT: { + btif_av_sink_config_req_t req; + // copy to avoid alignment problems + memcpy(&req, p_data, sizeof(req)); + + BTIF_TRACE_WARNING("BTIF_AV_SINK_CONFIG_REQ_EVT %d %d", req.sample_rate, + req.channel_count); + if (btif_av_cb.peer_sep == AVDT_TSEP_SRC && + bt_av_sink_callbacks != NULL) { + HAL_CBACK(bt_av_sink_callbacks, audio_config_cb, &(btif_av_cb.peer_bda), + req.sample_rate, req.channel_count); + } + } break; + + case BTIF_AV_CONNECT_REQ_EVT: + // Check for device, if same device which moved to opening then ignore + // callback + if (memcmp((bt_bdaddr_t*)p_data, &(btif_av_cb.peer_bda), + sizeof(btif_av_cb.peer_bda)) == 0) { + BTIF_TRACE_DEBUG( + "%s: Same device moved to Opening state,ignore Connect Req", + __func__); + btif_queue_advance(); + break; + } else { + BTIF_TRACE_DEBUG("%s: Moved from idle by Incoming Connection request", + __func__); + btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, + (bt_bdaddr_t*)p_data); + btif_queue_advance(); + break; + } + + case BTA_AV_PENDING_EVT: + // Check for device, if same device which moved to opening then ignore + // callback + if (memcmp(((tBTA_AV*)p_data)->pend.bd_addr, &(btif_av_cb.peer_bda), + sizeof(btif_av_cb.peer_bda)) == 0) { + BTIF_TRACE_DEBUG( + "%s: Same device moved to Opening state,ignore Pending Req", + __func__); + break; + } else { + BTIF_TRACE_DEBUG("%s: Moved from idle by outgoing Connection request", + __func__); + BTA_AvDisconnect(((tBTA_AV*)p_data)->pend.bd_addr); + break; + } + + case BTIF_AV_OFFLOAD_START_REQ_EVT: + BTIF_TRACE_ERROR( + "BTIF_AV_OFFLOAD_START_REQ_EVT: Stream not Started OPENING"); + btif_a2dp_on_offload_started(BTA_AV_FAIL); + break; + + case BTA_AV_CLOSE_EVT: + btif_a2dp_on_stopped(NULL); + btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, + &(btif_av_cb.peer_bda)); + btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE); + break; + + CHECK_RC_EVENT(event, (tBTA_AV*)p_data); + + default: + BTIF_TRACE_WARNING("%s : unhandled event:%s", __func__, + dump_av_sm_event_name((btif_av_sm_event_t)event)); + return false; + } + return true; } /***************************************************************************** -** -** Function btif_av_state_closing_handler -** -** Description Intermediate state managing events during closing -** of avdtp channel -** -** Returns true if event was processed, false otherwise -** -*******************************************************************************/ - -static bool btif_av_state_closing_handler(btif_sm_event_t event, void *p_data) -{ - BTIF_TRACE_DEBUG("%s event:%s flags %x", __func__, - dump_av_sm_event_name((btif_av_sm_event_t)event), btif_av_cb.flags); - - switch (event) - { - case BTIF_SM_ENTER_EVT: - if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) - { - /* immediately stop transmission of frames */ - btif_a2dp_source_set_tx_flush(true); - /* wait for audioflinger to stop a2dp */ - } - if (btif_av_cb.peer_sep == AVDT_TSEP_SRC) - { - btif_a2dp_sink_set_rx_flush(true); - } - break; - - case BTA_AV_STOP_EVT: - case BTIF_AV_STOP_STREAM_REQ_EVT: - btif_a2dp_on_stopped(NULL); - break; - - case BTIF_SM_EXIT_EVT: - break; - - case BTA_AV_CLOSE_EVT: - - /* inform the application that we are disconnecting */ - btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb.peer_bda)); - - btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE); - break; - - /* Handle the RC_CLOSE event for the cleanup */ - case BTA_AV_RC_CLOSE_EVT: - btif_rc_handler(event, (tBTA_AV*)p_data); - break; - - /* Handle the RC_BROWSE_CLOSE event for tetsing*/ - case BTA_AV_RC_BROWSE_CLOSE_EVT: - btif_rc_handler(event, (tBTA_AV*)p_data); - break; - - case BTIF_AV_OFFLOAD_START_REQ_EVT: - BTIF_TRACE_ERROR("BTIF_AV_OFFLOAD_START_REQ_EVT: Stream not Started Closing"); - btif_a2dp_on_offload_started(BTA_AV_FAIL); - break; - - default: - BTIF_TRACE_WARNING("%s : unhandled event:%s", __func__, - dump_av_sm_event_name((btif_av_sm_event_t)event)); - return false; - } - return true; + * + * Function btif_av_state_closing_handler + * + * Description Intermediate state managing events during closing + * of avdtp channel + * + * Returns true if event was processed, false otherwise + * + ******************************************************************************/ + +static bool btif_av_state_closing_handler(btif_sm_event_t event, void* p_data) { + BTIF_TRACE_DEBUG("%s event:%s flags %x", __func__, + dump_av_sm_event_name((btif_av_sm_event_t)event), + btif_av_cb.flags); + + switch (event) { + case BTIF_SM_ENTER_EVT: + if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) { + /* immediately stop transmission of frames */ + btif_a2dp_source_set_tx_flush(true); + /* wait for audioflinger to stop a2dp */ + } + if (btif_av_cb.peer_sep == AVDT_TSEP_SRC) { + btif_a2dp_sink_set_rx_flush(true); + } + break; + + case BTA_AV_STOP_EVT: + case BTIF_AV_STOP_STREAM_REQ_EVT: + btif_a2dp_on_stopped(NULL); + break; + + case BTIF_SM_EXIT_EVT: + break; + + case BTA_AV_CLOSE_EVT: + + /* inform the application that we are disconnecting */ + btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, + &(btif_av_cb.peer_bda)); + + btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE); + break; + + /* Handle the RC_CLOSE event for the cleanup */ + case BTA_AV_RC_CLOSE_EVT: + btif_rc_handler(event, (tBTA_AV*)p_data); + break; + + /* Handle the RC_BROWSE_CLOSE event for tetsing*/ + case BTA_AV_RC_BROWSE_CLOSE_EVT: + btif_rc_handler(event, (tBTA_AV*)p_data); + break; + + case BTIF_AV_OFFLOAD_START_REQ_EVT: + BTIF_TRACE_ERROR( + "BTIF_AV_OFFLOAD_START_REQ_EVT: Stream not Started Closing"); + btif_a2dp_on_offload_started(BTA_AV_FAIL); + break; + + default: + BTIF_TRACE_WARNING("%s : unhandled event:%s", __func__, + dump_av_sm_event_name((btif_av_sm_event_t)event)); + return false; + } + return true; } /***************************************************************************** -** -** Function btif_av_state_opened_handler -** -** Description Handles AV events while AVDTP is in OPEN state -** -** Returns true if event was processed, false otherwise -** -*******************************************************************************/ - -static bool btif_av_state_opened_handler(btif_sm_event_t event, void *p_data) -{ - tBTA_AV *p_av = (tBTA_AV*)p_data; - - BTIF_TRACE_DEBUG("%s event:%s flags %x", __func__, - dump_av_sm_event_name((btif_av_sm_event_t)event), btif_av_cb.flags); - - if ( (event == BTA_AV_REMOTE_CMD_EVT) && (btif_av_cb.flags & BTIF_AV_FLAG_REMOTE_SUSPEND) && - (p_av->remote_cmd.rc_id == BTA_AV_RC_PLAY) ) - { - BTIF_TRACE_EVENT("%s: Resetting remote suspend flag on RC PLAY", __func__); - btif_av_cb.flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND; - } + * + * Function btif_av_state_opened_handler + * + * Description Handles AV events while AVDTP is in OPEN state + * + * Returns true if event was processed, false otherwise + * + ******************************************************************************/ - switch (event) - { - case BTIF_SM_ENTER_EVT: - btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_STOP; - btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_START; - break; - - case BTIF_SM_EXIT_EVT: - btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_START; - break; - - case BTIF_AV_START_STREAM_REQ_EVT: - if (btif_av_cb.peer_sep != AVDT_TSEP_SRC) - btif_a2dp_source_setup_codec(); - BTA_AvStart(); - btif_av_cb.flags |= BTIF_AV_FLAG_PENDING_START; - break; - - case BTA_AV_START_EVT: - { - BTIF_TRACE_EVENT("BTA_AV_START_EVT status %d, suspending %d, init %d", - p_av->start.status, p_av->start.suspending, p_av->start.initiator); - - if ((p_av->start.status == BTA_SUCCESS) && (p_av->start.suspending == true)) - return true; - - /* if remote tries to start a2dp when DUT is a2dp source - * then suspend. In case a2dp is sink and call is active - * then disconnect the AVDTP channel - */ - if (!(btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START)) - { - if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) - { - BTIF_TRACE_EVENT("%s: trigger suspend as remote initiated!!", __func__); - btif_dispatch_sm_event(BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL, 0); - } - } - - /* In case peer is A2DP SRC we do not want to ack commands on UIPC*/ - if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) - { - if (btif_a2dp_on_started(&p_av->start, - ((btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) != 0))) - { - /* only clear pending flag after acknowledgement */ - btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_START; - } - } - - /* remain in open state if status failed */ - if (p_av->start.status != BTA_AV_SUCCESS) - return false; - - if (btif_av_cb.peer_sep == AVDT_TSEP_SRC) - { - btif_a2dp_sink_set_rx_flush(false); /* remove flush state, ready for streaming*/ - } - - /* change state to started, send acknowledgement if start is pending */ - if (btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) { - if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) - btif_a2dp_on_started(NULL, true); - /* pending start flag will be cleared when exit current state */ - } - btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_STARTED); - - } break; - - case BTIF_AV_DISCONNECT_REQ_EVT: - BTA_AvClose(btif_av_cb.bta_handle); - if (btif_av_cb.peer_sep == AVDT_TSEP_SRC) { - BTA_AvCloseRc(btif_av_cb.bta_handle); - } - - /* inform the application that we are disconnecting */ - btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTING, &(btif_av_cb.peer_bda)); - break; - - case BTA_AV_CLOSE_EVT: - /* avdtp link is closed */ - btif_a2dp_on_stopped(NULL); - - /* inform the application that we are disconnected */ - btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb.peer_bda)); - - /* change state to idle, send acknowledgement if start is pending */ - if (btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) { - btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE); - /* pending start flag will be cleared when exit current state */ - } - btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE); - break; - - case BTA_AV_RECONFIG_EVT: - if((btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) && - (p_av->reconfig.status == BTA_AV_SUCCESS)) - { - APPL_TRACE_WARNING("reconfig done BTA_AVstart()"); - BTA_AvStart(); - } - else if(btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) - { - btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_START; - btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE); - } - break; - - case BTIF_AV_CONNECT_REQ_EVT: - if (memcmp ((bt_bdaddr_t*)p_data, &(btif_av_cb.peer_bda), - sizeof(btif_av_cb.peer_bda)) == 0) - { - BTIF_TRACE_DEBUG("%s: Ignore BTIF_AV_CONNECT_REQ_EVT for same device", __func__); - } - else - { - BTIF_TRACE_DEBUG("%s: Moved to opened by Other Incoming Conn req", __func__); - btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, - (bt_bdaddr_t*)p_data); - } - btif_queue_advance(); - break; - - case BTIF_AV_OFFLOAD_START_REQ_EVT: - BTIF_TRACE_ERROR("BTIF_AV_OFFLOAD_START_REQ_EVT: Stream not Started Opened"); - btif_a2dp_on_offload_started(BTA_AV_FAIL); - break; - - CHECK_RC_EVENT(event, (tBTA_AV*)p_data); - - default: - BTIF_TRACE_WARNING("%s : unhandled event:%s", __func__, - dump_av_sm_event_name((btif_av_sm_event_t)event)); - return false; +static bool btif_av_state_opened_handler(btif_sm_event_t event, void* p_data) { + tBTA_AV* p_av = (tBTA_AV*)p_data; - } - return true; + BTIF_TRACE_DEBUG("%s event:%s flags %x", __func__, + dump_av_sm_event_name((btif_av_sm_event_t)event), + btif_av_cb.flags); + + if ((event == BTA_AV_REMOTE_CMD_EVT) && + (btif_av_cb.flags & BTIF_AV_FLAG_REMOTE_SUSPEND) && + (p_av->remote_cmd.rc_id == BTA_AV_RC_PLAY)) { + BTIF_TRACE_EVENT("%s: Resetting remote suspend flag on RC PLAY", __func__); + btif_av_cb.flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND; + } + + switch (event) { + case BTIF_SM_ENTER_EVT: + btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_STOP; + btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_START; + break; + + case BTIF_SM_EXIT_EVT: + btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_START; + break; + + case BTIF_AV_START_STREAM_REQ_EVT: + if (btif_av_cb.peer_sep != AVDT_TSEP_SRC) btif_a2dp_source_setup_codec(); + BTA_AvStart(); + btif_av_cb.flags |= BTIF_AV_FLAG_PENDING_START; + break; + + case BTA_AV_START_EVT: { + BTIF_TRACE_EVENT("BTA_AV_START_EVT status %d, suspending %d, init %d", + p_av->start.status, p_av->start.suspending, + p_av->start.initiator); + + if ((p_av->start.status == BTA_SUCCESS) && + (p_av->start.suspending == true)) + return true; + + /* if remote tries to start a2dp when DUT is a2dp source + * then suspend. In case a2dp is sink and call is active + * then disconnect the AVDTP channel + */ + if (!(btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START)) { + if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) { + BTIF_TRACE_EVENT("%s: trigger suspend as remote initiated!!", + __func__); + btif_dispatch_sm_event(BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL, 0); + } + } + + /* In case peer is A2DP SRC we do not want to ack commands on UIPC*/ + if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) { + if (btif_a2dp_on_started( + &p_av->start, + ((btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) != 0))) { + /* only clear pending flag after acknowledgement */ + btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_START; + } + } + + /* remain in open state if status failed */ + if (p_av->start.status != BTA_AV_SUCCESS) return false; + + if (btif_av_cb.peer_sep == AVDT_TSEP_SRC) { + btif_a2dp_sink_set_rx_flush( + false); /* remove flush state, ready for streaming*/ + } + + /* change state to started, send acknowledgement if start is pending */ + if (btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) { + if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) + btif_a2dp_on_started(NULL, true); + /* pending start flag will be cleared when exit current state */ + } + btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_STARTED); + + } break; + + case BTIF_AV_DISCONNECT_REQ_EVT: + BTA_AvClose(btif_av_cb.bta_handle); + if (btif_av_cb.peer_sep == AVDT_TSEP_SRC) { + BTA_AvCloseRc(btif_av_cb.bta_handle); + } + + /* inform the application that we are disconnecting */ + btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTING, + &(btif_av_cb.peer_bda)); + break; + + case BTA_AV_CLOSE_EVT: + /* avdtp link is closed */ + btif_a2dp_on_stopped(NULL); + + /* inform the application that we are disconnected */ + btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, + &(btif_av_cb.peer_bda)); + + /* change state to idle, send acknowledgement if start is pending */ + if (btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) { + btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE); + /* pending start flag will be cleared when exit current state */ + } + btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE); + break; + + case BTA_AV_RECONFIG_EVT: + if ((btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) && + (p_av->reconfig.status == BTA_AV_SUCCESS)) { + APPL_TRACE_WARNING("reconfig done BTA_AVstart()"); + BTA_AvStart(); + } else if (btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) { + btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_START; + btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE); + } + break; + + case BTIF_AV_CONNECT_REQ_EVT: + if (memcmp((bt_bdaddr_t*)p_data, &(btif_av_cb.peer_bda), + sizeof(btif_av_cb.peer_bda)) == 0) { + BTIF_TRACE_DEBUG("%s: Ignore BTIF_AV_CONNECT_REQ_EVT for same device", + __func__); + } else { + BTIF_TRACE_DEBUG("%s: Moved to opened by Other Incoming Conn req", + __func__); + btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, + (bt_bdaddr_t*)p_data); + } + btif_queue_advance(); + break; + + case BTIF_AV_OFFLOAD_START_REQ_EVT: + BTIF_TRACE_ERROR( + "BTIF_AV_OFFLOAD_START_REQ_EVT: Stream not Started Opened"); + btif_a2dp_on_offload_started(BTA_AV_FAIL); + break; + + CHECK_RC_EVENT(event, (tBTA_AV*)p_data); + + default: + BTIF_TRACE_WARNING("%s : unhandled event:%s", __func__, + dump_av_sm_event_name((btif_av_sm_event_t)event)); + return false; + } + return true; } /***************************************************************************** -** -** Function btif_av_state_started_handler -** -** Description Handles AV events while A2DP stream is started -** -** Returns true if event was processed, false otherwise -** -*******************************************************************************/ - -static bool btif_av_state_started_handler(btif_sm_event_t event, void *p_data) -{ - tBTA_AV *p_av = (tBTA_AV*)p_data; - - BTIF_TRACE_DEBUG("%s event:%s flags %x", __func__, - dump_av_sm_event_name((btif_av_sm_event_t)event), btif_av_cb.flags); - - switch (event) - { - case BTIF_SM_ENTER_EVT: - - /* we are again in started state, clear any remote suspend flags */ - btif_av_cb.flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND; - - /** - * Report to components above that we have entered the streaming - * stage, this should usually be followed by focus grant. - * see update_audio_focus_state() - */ - btif_report_audio_state(BTAV_AUDIO_STATE_STARTED, &(btif_av_cb.peer_bda)); - - /* increase the a2dp consumer task priority temporarily when start - ** audio playing, to avoid overflow the audio packet queue. */ - adjust_priority_a2dp(true); - - break; - - case BTIF_SM_EXIT_EVT: - /* restore the a2dp consumer task priority when stop audio playing. */ - adjust_priority_a2dp(false); - - break; - - case BTIF_AV_START_STREAM_REQ_EVT: - /* we were remotely started, just ack back the local request */ - if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) - btif_a2dp_on_started(NULL, true); - break; - - /* fixme -- use suspend = true always to work around issue with BTA AV */ - case BTIF_AV_STOP_STREAM_REQ_EVT: - case BTIF_AV_SUSPEND_STREAM_REQ_EVT: + * + * Function btif_av_state_started_handler + * + * Description Handles AV events while A2DP stream is started + * + * Returns true if event was processed, false otherwise + * + ******************************************************************************/ - /* set pending flag to ensure btif task is not trying to restart - stream while suspend is in progress */ - btif_av_cb.flags |= BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING; +static bool btif_av_state_started_handler(btif_sm_event_t event, void* p_data) { + tBTA_AV* p_av = (tBTA_AV*)p_data; - /* if we were remotely suspended but suspend locally, local suspend - always overrides */ - btif_av_cb.flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND; + BTIF_TRACE_DEBUG("%s event:%s flags %x", __func__, + dump_av_sm_event_name((btif_av_sm_event_t)event), + btif_av_cb.flags); - if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) { - /* - * Immediately stop transmission of frames while suspend is - * pending. - */ - btif_a2dp_source_set_tx_flush(true); - } + switch (event) { + case BTIF_SM_ENTER_EVT: - if (btif_av_cb.peer_sep == AVDT_TSEP_SRC) { - btif_a2dp_on_stopped(NULL); - } + /* we are again in started state, clear any remote suspend flags */ + btif_av_cb.flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND; - BTA_AvStop(true); - break; + /** + * Report to components above that we have entered the streaming + * stage, this should usually be followed by focus grant. + * see update_audio_focus_state() + */ + btif_report_audio_state(BTAV_AUDIO_STATE_STARTED, &(btif_av_cb.peer_bda)); - case BTIF_AV_DISCONNECT_REQ_EVT: + /* increase the a2dp consumer task priority temporarily when start + ** audio playing, to avoid overflow the audio packet queue. */ + adjust_priority_a2dp(true); - /* request avdtp to close */ - BTA_AvClose(btif_av_cb.bta_handle); - if (btif_av_cb.peer_sep == AVDT_TSEP_SRC) { - BTA_AvCloseRc(btif_av_cb.bta_handle); - } + break; - /* inform the application that we are disconnecting */ - btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTING, &(btif_av_cb.peer_bda)); + case BTIF_SM_EXIT_EVT: + /* restore the a2dp consumer task priority when stop audio playing. */ + adjust_priority_a2dp(false); - /* wait in closing state until fully closed */ - btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_CLOSING); - break; + break; - case BTA_AV_SUSPEND_EVT: + case BTIF_AV_START_STREAM_REQ_EVT: + /* we were remotely started, just ack back the local request */ + if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) + btif_a2dp_on_started(NULL, true); + break; - BTIF_TRACE_EVENT("BTA_AV_SUSPEND_EVT status %d, init %d", - p_av->suspend.status, p_av->suspend.initiator); + /* fixme -- use suspend = true always to work around issue with BTA AV */ + case BTIF_AV_STOP_STREAM_REQ_EVT: + case BTIF_AV_SUSPEND_STREAM_REQ_EVT: - /* a2dp suspended, stop media task until resumed */ - btif_a2dp_on_suspended(&p_av->suspend); + /* set pending flag to ensure btif task is not trying to restart + stream while suspend is in progress */ + btif_av_cb.flags |= BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING; - /* if not successful, remain in current state */ - if (p_av->suspend.status != BTA_AV_SUCCESS) - { - btif_av_cb.flags &= ~BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING; + /* if we were remotely suspended but suspend locally, local suspend + always overrides */ + btif_av_cb.flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND; - if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) - { - /* suspend failed, reset back tx flush state */ - btif_a2dp_source_set_tx_flush(false); - } - return false; - } + if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) { + /* + * Immediately stop transmission of frames while suspend is + * pending. + */ + btif_a2dp_source_set_tx_flush(true); + } - if (p_av->suspend.initiator != true) - { - /* remote suspend, notify HAL and await audioflinger to - suspend/stop stream */ + if (btif_av_cb.peer_sep == AVDT_TSEP_SRC) { + btif_a2dp_on_stopped(NULL); + } - /* set remote suspend flag to block media task from restarting - stream only if we did not already initiate a local suspend */ - if ((btif_av_cb.flags & BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING) == 0) - btif_av_cb.flags |= BTIF_AV_FLAG_REMOTE_SUSPEND; + BTA_AvStop(true); + break; - btif_report_audio_state(BTAV_AUDIO_STATE_REMOTE_SUSPEND, &(btif_av_cb.peer_bda)); - } - else - { - btif_report_audio_state(BTAV_AUDIO_STATE_STOPPED, &(btif_av_cb.peer_bda)); - } + case BTIF_AV_DISCONNECT_REQ_EVT: - btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_OPENED); + /* request avdtp to close */ + BTA_AvClose(btif_av_cb.bta_handle); + if (btif_av_cb.peer_sep == AVDT_TSEP_SRC) { + BTA_AvCloseRc(btif_av_cb.bta_handle); + } - /* suspend completed and state changed, clear pending status */ - btif_av_cb.flags &= ~BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING; - break; + /* inform the application that we are disconnecting */ + btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTING, + &(btif_av_cb.peer_bda)); - case BTA_AV_STOP_EVT: + /* wait in closing state until fully closed */ + btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_CLOSING); + break; - btif_av_cb.flags |= BTIF_AV_FLAG_PENDING_STOP; - btif_a2dp_on_stopped(&p_av->suspend); + case BTA_AV_SUSPEND_EVT: - btif_report_audio_state(BTAV_AUDIO_STATE_STOPPED, &(btif_av_cb.peer_bda)); + BTIF_TRACE_EVENT("BTA_AV_SUSPEND_EVT status %d, init %d", + p_av->suspend.status, p_av->suspend.initiator); - /* if stop was successful, change state to open */ - if (p_av->suspend.status == BTA_AV_SUCCESS) - btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_OPENED); + /* a2dp suspended, stop media task until resumed */ + btif_a2dp_on_suspended(&p_av->suspend); - break; + /* if not successful, remain in current state */ + if (p_av->suspend.status != BTA_AV_SUCCESS) { + btif_av_cb.flags &= ~BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING; - case BTA_AV_CLOSE_EVT: + if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) { + /* suspend failed, reset back tx flush state */ + btif_a2dp_source_set_tx_flush(false); + } + return false; + } - btif_av_cb.flags |= BTIF_AV_FLAG_PENDING_STOP; + if (p_av->suspend.initiator != true) { + /* remote suspend, notify HAL and await audioflinger to + suspend/stop stream */ - /* avdtp link is closed */ - btif_a2dp_on_stopped(NULL); + /* set remote suspend flag to block media task from restarting + stream only if we did not already initiate a local suspend */ + if ((btif_av_cb.flags & BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING) == 0) + btif_av_cb.flags |= BTIF_AV_FLAG_REMOTE_SUSPEND; - /* inform the application that we are disconnected */ - btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb.peer_bda)); + btif_report_audio_state(BTAV_AUDIO_STATE_REMOTE_SUSPEND, + &(btif_av_cb.peer_bda)); + } else { + btif_report_audio_state(BTAV_AUDIO_STATE_STOPPED, + &(btif_av_cb.peer_bda)); + } - btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE); - break; + btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_OPENED); - case BTIF_AV_OFFLOAD_START_REQ_EVT: - BTA_AvOffloadStart(btif_av_cb.bta_handle); - break; + /* suspend completed and state changed, clear pending status */ + btif_av_cb.flags &= ~BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING; + break; - case BTA_AV_OFFLOAD_START_RSP_EVT: - btif_a2dp_on_offload_started(p_av->status); - break; + case BTA_AV_STOP_EVT: - CHECK_RC_EVENT(event, (tBTA_AV*)p_data); + btif_av_cb.flags |= BTIF_AV_FLAG_PENDING_STOP; + btif_a2dp_on_stopped(&p_av->suspend); - default: - BTIF_TRACE_WARNING("%s: unhandled event: %s", __func__, - dump_av_sm_event_name((btif_av_sm_event_t)event)); - return false; - } + btif_report_audio_state(BTAV_AUDIO_STATE_STOPPED, &(btif_av_cb.peer_bda)); - return true; + /* if stop was successful, change state to open */ + if (p_av->suspend.status == BTA_AV_SUCCESS) + btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_OPENED); + + break; + + case BTA_AV_CLOSE_EVT: + + btif_av_cb.flags |= BTIF_AV_FLAG_PENDING_STOP; + + /* avdtp link is closed */ + btif_a2dp_on_stopped(NULL); + + /* inform the application that we are disconnected */ + btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, + &(btif_av_cb.peer_bda)); + + btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE); + break; + + case BTIF_AV_OFFLOAD_START_REQ_EVT: + BTA_AvOffloadStart(btif_av_cb.bta_handle); + break; + + case BTA_AV_OFFLOAD_START_RSP_EVT: + btif_a2dp_on_offload_started(p_av->status); + break; + + CHECK_RC_EVENT(event, (tBTA_AV*)p_data); + + default: + BTIF_TRACE_WARNING("%s: unhandled event: %s", __func__, + dump_av_sm_event_name((btif_av_sm_event_t)event)); + return false; + } + + return true; } /***************************************************************************** -** Local event handlers -******************************************************************************/ - -static void btif_av_handle_event(uint16_t event, char* p_param) -{ - BTIF_TRACE_EVENT("%s event:%s", __func__, - dump_av_sm_event_name((btif_av_sm_event_t)event)); - switch(event) - { - case BTIF_AV_CLEANUP_REQ_EVT: - btif_a2dp_source_shutdown(); - btif_a2dp_sink_shutdown(); - break; - - case BTA_AV_REGISTER_EVT: - if (btif_av_cb.sm_handle == NULL) - { - btif_av_cb.bta_handle = ((tBTA_AV*)p_param)->registr.hndl; - BTIF_TRACE_DEBUG("%s: BTA AV Handle updated", __func__); - } - /* FALLTHROUGH */ - default: - btif_sm_dispatch(btif_av_cb.sm_handle, event, (void*)p_param); - btif_av_event_free_data(event, p_param); - } + * Local event handlers + *****************************************************************************/ + +static void btif_av_handle_event(uint16_t event, char* p_param) { + BTIF_TRACE_EVENT("%s event:%s", __func__, + dump_av_sm_event_name((btif_av_sm_event_t)event)); + switch (event) { + case BTIF_AV_CLEANUP_REQ_EVT: + btif_a2dp_source_shutdown(); + btif_a2dp_sink_shutdown(); + break; + + case BTA_AV_REGISTER_EVT: + if (btif_av_cb.sm_handle == NULL) { + btif_av_cb.bta_handle = ((tBTA_AV*)p_param)->registr.hndl; + BTIF_TRACE_DEBUG("%s: BTA AV Handle updated", __func__); + } + /* FALLTHROUGH */ + default: + btif_sm_dispatch(btif_av_cb.sm_handle, event, (void*)p_param); + btif_av_event_free_data(event, p_param); + } } -void btif_av_event_deep_copy(uint16_t event, char *p_dest, char *p_src) -{ - BTIF_TRACE_DEBUG("%s", __func__); - tBTA_AV *av_src = (tBTA_AV *)p_src; - tBTA_AV *av_dest = (tBTA_AV *)p_dest; - - // First copy the structure - maybe_non_aligned_memcpy(av_dest, av_src, sizeof(*av_src)); - switch (event) - { - case BTA_AV_META_MSG_EVT: - if (av_src->meta_msg.p_data && av_src->meta_msg.len) - { - av_dest->meta_msg.p_data = (uint8_t*)osi_calloc(av_src->meta_msg.len); - memcpy(av_dest->meta_msg.p_data, av_src->meta_msg.p_data, - av_src->meta_msg.len); - } - - if (av_src->meta_msg.p_msg) - { - av_dest->meta_msg.p_msg = (tAVRC_MSG*)osi_calloc(sizeof(tAVRC_MSG)); - memcpy(av_dest->meta_msg.p_msg, av_src->meta_msg.p_msg, - sizeof(tAVRC_MSG)); - - tAVRC_MSG *p_msg_src = av_src->meta_msg.p_msg; - tAVRC_MSG *p_msg_dest = av_dest->meta_msg.p_msg; - - if ((p_msg_src->hdr.opcode == AVRC_OP_VENDOR) && - (p_msg_src->vendor.p_vendor_data && - p_msg_src->vendor.vendor_len)) { - p_msg_dest->vendor.p_vendor_data = (uint8_t *)osi_calloc( - p_msg_src->vendor.vendor_len); - memcpy(p_msg_dest->vendor.p_vendor_data, - p_msg_src->vendor.p_vendor_data, - p_msg_src->vendor.vendor_len); - } - } - break; - - default: - break; - } +void btif_av_event_deep_copy(uint16_t event, char* p_dest, char* p_src) { + BTIF_TRACE_DEBUG("%s", __func__); + tBTA_AV* av_src = (tBTA_AV*)p_src; + tBTA_AV* av_dest = (tBTA_AV*)p_dest; + + // First copy the structure + maybe_non_aligned_memcpy(av_dest, av_src, sizeof(*av_src)); + switch (event) { + case BTA_AV_META_MSG_EVT: + if (av_src->meta_msg.p_data && av_src->meta_msg.len) { + av_dest->meta_msg.p_data = (uint8_t*)osi_calloc(av_src->meta_msg.len); + memcpy(av_dest->meta_msg.p_data, av_src->meta_msg.p_data, + av_src->meta_msg.len); + } + + if (av_src->meta_msg.p_msg) { + av_dest->meta_msg.p_msg = (tAVRC_MSG*)osi_calloc(sizeof(tAVRC_MSG)); + memcpy(av_dest->meta_msg.p_msg, av_src->meta_msg.p_msg, + sizeof(tAVRC_MSG)); + + tAVRC_MSG* p_msg_src = av_src->meta_msg.p_msg; + tAVRC_MSG* p_msg_dest = av_dest->meta_msg.p_msg; + + if ((p_msg_src->hdr.opcode == AVRC_OP_VENDOR) && + (p_msg_src->vendor.p_vendor_data && p_msg_src->vendor.vendor_len)) { + p_msg_dest->vendor.p_vendor_data = + (uint8_t*)osi_calloc(p_msg_src->vendor.vendor_len); + memcpy(p_msg_dest->vendor.p_vendor_data, + p_msg_src->vendor.p_vendor_data, p_msg_src->vendor.vendor_len); + } + } + break; + + default: + break; + } } -static void btif_av_event_free_data(btif_sm_event_t event, void *p_data) -{ - switch (event) - { - case BTA_AV_META_MSG_EVT: - { - tBTA_AV *av = (tBTA_AV *)p_data; - osi_free_and_reset((void **)&av->meta_msg.p_data); - - if (av->meta_msg.p_msg) { - if (av->meta_msg.p_msg->hdr.opcode == AVRC_OP_VENDOR) { - osi_free(av->meta_msg.p_msg->vendor.p_vendor_data); - } - osi_free_and_reset((void **)&av->meta_msg.p_msg); - } - } - break; - - default: - break; - } +static void btif_av_event_free_data(btif_sm_event_t event, void* p_data) { + switch (event) { + case BTA_AV_META_MSG_EVT: { + tBTA_AV* av = (tBTA_AV*)p_data; + osi_free_and_reset((void**)&av->meta_msg.p_data); + + if (av->meta_msg.p_msg) { + if (av->meta_msg.p_msg->hdr.opcode == AVRC_OP_VENDOR) { + osi_free(av->meta_msg.p_msg->vendor.p_vendor_data); + } + osi_free_and_reset((void**)&av->meta_msg.p_msg); + } + } break; + + default: + break; + } } -static void bte_av_callback(tBTA_AV_EVT event, tBTA_AV *p_data) -{ - btif_transfer_context(btif_av_handle_event, event, - (char*)p_data, sizeof(tBTA_AV), btif_av_event_deep_copy); +static void bte_av_callback(tBTA_AV_EVT event, tBTA_AV* p_data) { + btif_transfer_context(btif_av_handle_event, event, (char*)p_data, + sizeof(tBTA_AV), btif_av_event_deep_copy); } static void bte_av_sink_media_callback(tBTA_AV_EVT event, - tBTA_AV_MEDIA *p_data) -{ - switch (event) { + tBTA_AV_MEDIA* p_data) { + switch (event) { case BTA_AV_SINK_MEDIA_DATA_EVT: { - btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle); - if ((state == BTIF_AV_STATE_STARTED) || - (state == BTIF_AV_STATE_OPENED)) { - uint8_t queue_len = btif_a2dp_sink_enqueue_buf((BT_HDR *)p_data); - BTIF_TRACE_DEBUG("%s: packets in sink queue %d", __func__, - queue_len); - } - break; + btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle); + if ((state == BTIF_AV_STATE_STARTED) || (state == BTIF_AV_STATE_OPENED)) { + uint8_t queue_len = btif_a2dp_sink_enqueue_buf((BT_HDR*)p_data); + BTIF_TRACE_DEBUG("%s: packets in sink queue %d", __func__, queue_len); + } + break; } case BTA_AV_SINK_MEDIA_CFG_EVT: { - btif_av_sink_config_req_t config_req; - - /* send a command to BT Media Task */ - btif_a2dp_sink_update_decoder((uint8_t *)(p_data->avk_config.codec_info)); - /* Switch to BTIF context */ - config_req.sample_rate = - A2DP_GetTrackFrequency(p_data->avk_config.codec_info); - if (config_req.sample_rate == -1) { - APPL_TRACE_ERROR("%s: cannot get the track frequency", __func__); - break; - } - config_req.channel_count = - A2DP_GetTrackChannelCount(p_data->avk_config.codec_info); - if (config_req.channel_count == -1) { - APPL_TRACE_ERROR("%s: cannot get the channel count", __func__); - break; - } - - memcpy(&config_req.peer_bd, (uint8_t *)(p_data->avk_config.bd_addr), - sizeof(config_req.peer_bd)); - btif_transfer_context(btif_av_handle_event, - BTIF_AV_SINK_CONFIG_REQ_EVT, - (char *)&config_req, sizeof(config_req), NULL); + btif_av_sink_config_req_t config_req; + + /* send a command to BT Media Task */ + btif_a2dp_sink_update_decoder((uint8_t*)(p_data->avk_config.codec_info)); + /* Switch to BTIF context */ + config_req.sample_rate = + A2DP_GetTrackFrequency(p_data->avk_config.codec_info); + if (config_req.sample_rate == -1) { + APPL_TRACE_ERROR("%s: cannot get the track frequency", __func__); break; - } - default: + } + config_req.channel_count = + A2DP_GetTrackChannelCount(p_data->avk_config.codec_info); + if (config_req.channel_count == -1) { + APPL_TRACE_ERROR("%s: cannot get the channel count", __func__); break; + } + + memcpy(&config_req.peer_bd, (uint8_t*)(p_data->avk_config.bd_addr), + sizeof(config_req.peer_bd)); + btif_transfer_context(btif_av_handle_event, BTIF_AV_SINK_CONFIG_REQ_EVT, + (char*)&config_req, sizeof(config_req), NULL); + break; } + default: + break; + } } /******************************************************************************* -** -** Function btif_av_init -** -** Description Initializes btif AV if not already done -** -** Returns bt_status_t -** -*******************************************************************************/ - -bt_status_t btif_av_init(int service_id) -{ - if (btif_av_cb.sm_handle == NULL) { - alarm_free(av_open_on_rc_timer); - av_open_on_rc_timer = alarm_new("btif_av.av_open_on_rc_timer"); - - switch (service_id) { - case BTA_A2DP_SOURCE_SERVICE_ID: - if (!btif_a2dp_source_startup()) - return BT_STATUS_FAIL; // Already running - break; - case BTA_A2DP_SINK_SERVICE_ID: - if (!btif_a2dp_sink_startup()) - return BT_STATUS_FAIL; // Already running - break; - default: - break; - } + * + * Function btif_av_init + * + * Description Initializes btif AV if not already done + * + * Returns bt_status_t + * + ******************************************************************************/ - btif_enable_service(service_id); +bt_status_t btif_av_init(int service_id) { + if (btif_av_cb.sm_handle == NULL) { + alarm_free(av_open_on_rc_timer); + av_open_on_rc_timer = alarm_new("btif_av.av_open_on_rc_timer"); - /* Also initialize the AV state machine */ - btif_av_cb.sm_handle = - btif_sm_init((const btif_sm_handler_t*)btif_av_state_handlers, - BTIF_AV_STATE_IDLE); + switch (service_id) { + case BTA_A2DP_SOURCE_SERVICE_ID: + if (!btif_a2dp_source_startup()) + return BT_STATUS_FAIL; // Already running + break; + case BTA_A2DP_SINK_SERVICE_ID: + if (!btif_a2dp_sink_startup()) + return BT_STATUS_FAIL; // Already running + break; + default: + break; } - return BT_STATUS_SUCCESS; + btif_enable_service(service_id); + + /* Also initialize the AV state machine */ + btif_av_cb.sm_handle = btif_sm_init( + (const btif_sm_handler_t*)btif_av_state_handlers, BTIF_AV_STATE_IDLE); + } + + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function init_src -** -** Description Initializes the AV interface for source mode -** -** Returns bt_status_t -** -*******************************************************************************/ - -static bt_status_t init_src(btav_callbacks_t* callbacks) -{ - BTIF_TRACE_EVENT("%s()", __func__); - - bt_status_t status = btif_av_init(BTA_A2DP_SOURCE_SERVICE_ID); - if (status == BT_STATUS_SUCCESS) - bt_av_src_callbacks = callbacks; - - return status; + * + * Function init_src + * + * Description Initializes the AV interface for source mode + * + * Returns bt_status_t + * + ******************************************************************************/ + +static bt_status_t init_src(btav_callbacks_t* callbacks) { + BTIF_TRACE_EVENT("%s()", __func__); + + bt_status_t status = btif_av_init(BTA_A2DP_SOURCE_SERVICE_ID); + if (status == BT_STATUS_SUCCESS) bt_av_src_callbacks = callbacks; + + return status; } /******************************************************************************* -** -** Function init_sink -** -** Description Initializes the AV interface for sink mode -** -** Returns bt_status_t -** -*******************************************************************************/ - -static bt_status_t init_sink(btav_callbacks_t* callbacks) -{ - BTIF_TRACE_EVENT("%s()", __func__); - - bt_status_t status = btif_av_init(BTA_A2DP_SINK_SERVICE_ID); - if (status == BT_STATUS_SUCCESS) - bt_av_sink_callbacks = callbacks; - - return status; + * + * Function init_sink + * + * Description Initializes the AV interface for sink mode + * + * Returns bt_status_t + * + ******************************************************************************/ + +static bt_status_t init_sink(btav_callbacks_t* callbacks) { + BTIF_TRACE_EVENT("%s()", __func__); + + bt_status_t status = btif_av_init(BTA_A2DP_SINK_SERVICE_ID); + if (status == BT_STATUS_SUCCESS) bt_av_sink_callbacks = callbacks; + + return status; } /******************************************************************************* -** -** Function update_audio_focus_state -** -** Description Updates the final focus state reported by components calling -** this module. -** -** Returns None -** -*******************************************************************************/ -static void update_audio_focus_state(int state) -{ - BTIF_TRACE_DEBUG("%s: state %d",__func__, state); - btif_a2dp_sink_set_focus_state_req((btif_a2dp_sink_focus_state_t)state); + * + * Function update_audio_focus_state + * + * Description Updates the final focus state reported by components calling + * this module. + * + * Returns None + * + ******************************************************************************/ +static void update_audio_focus_state(int state) { + BTIF_TRACE_DEBUG("%s: state %d", __func__, state); + btif_a2dp_sink_set_focus_state_req((btif_a2dp_sink_focus_state_t)state); } /******************************************************************************* -** -** Function update_audio_track_gain -** -** Description Updates the track gain (used for ducking). -** -** Returns None -** -*******************************************************************************/ -static void update_audio_track_gain(float gain) -{ - BTIF_TRACE_DEBUG("%s: gain %f",__func__, gain); - btif_a2dp_sink_set_audio_track_gain(gain); + * + * Function update_audio_track_gain + * + * Description Updates the track gain (used for ducking). + * + * Returns None + * + ******************************************************************************/ +static void update_audio_track_gain(float gain) { + BTIF_TRACE_DEBUG("%s: gain %f", __func__, gain); + btif_a2dp_sink_set_audio_track_gain(gain); } /******************************************************************************* -** -** Function connect -** -** Description Establishes the AV signalling channel with the remote headset -** -** Returns bt_status_t -** -*******************************************************************************/ - -static bt_status_t connect_int(bt_bdaddr_t *bd_addr, uint16_t uuid) -{ - btif_av_connect_req_t connect_req; - connect_req.target_bda = bd_addr; - connect_req.uuid = uuid; - BTIF_TRACE_EVENT("%s", __func__); - - btif_sm_dispatch(btif_av_cb.sm_handle, BTIF_AV_CONNECT_REQ_EVT, (char*)&connect_req); - - return BT_STATUS_SUCCESS; + * + * Function connect + * + * Description Establishes the AV signalling channel with the remote + *headset + * + * Returns bt_status_t + * + ******************************************************************************/ + +static bt_status_t connect_int(bt_bdaddr_t* bd_addr, uint16_t uuid) { + btif_av_connect_req_t connect_req; + connect_req.target_bda = bd_addr; + connect_req.uuid = uuid; + BTIF_TRACE_EVENT("%s", __func__); + + btif_sm_dispatch(btif_av_cb.sm_handle, BTIF_AV_CONNECT_REQ_EVT, + (char*)&connect_req); + + return BT_STATUS_SUCCESS; } -static bt_status_t src_connect_sink(bt_bdaddr_t *bd_addr) -{ - BTIF_TRACE_EVENT("%s", __func__); - CHECK_BTAV_INIT(); +static bt_status_t src_connect_sink(bt_bdaddr_t* bd_addr) { + BTIF_TRACE_EVENT("%s", __func__); + CHECK_BTAV_INIT(); - return btif_queue_connect(UUID_SERVCLASS_AUDIO_SOURCE, bd_addr, connect_int); + return btif_queue_connect(UUID_SERVCLASS_AUDIO_SOURCE, bd_addr, connect_int); } -static bt_status_t sink_connect_src(bt_bdaddr_t *bd_addr) -{ - BTIF_TRACE_EVENT("%s", __func__); - CHECK_BTAV_INIT(); +static bt_status_t sink_connect_src(bt_bdaddr_t* bd_addr) { + BTIF_TRACE_EVENT("%s", __func__); + CHECK_BTAV_INIT(); - return btif_queue_connect(UUID_SERVCLASS_AUDIO_SINK, bd_addr, connect_int); + return btif_queue_connect(UUID_SERVCLASS_AUDIO_SINK, bd_addr, connect_int); } /******************************************************************************* -** -** Function disconnect -** -** Description Tears down the AV signalling channel with the remote headset -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t disconnect(bt_bdaddr_t *bd_addr) -{ - BTIF_TRACE_EVENT("%s", __func__); - - CHECK_BTAV_INIT(); - - /* Switch to BTIF context */ - return btif_transfer_context(btif_av_handle_event, BTIF_AV_DISCONNECT_REQ_EVT, - (char*)bd_addr, sizeof(bt_bdaddr_t), NULL); + * + * Function disconnect + * + * Description Tears down the AV signalling channel with the remote headset + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t disconnect(bt_bdaddr_t* bd_addr) { + BTIF_TRACE_EVENT("%s", __func__); + + CHECK_BTAV_INIT(); + + /* Switch to BTIF context */ + return btif_transfer_context(btif_av_handle_event, BTIF_AV_DISCONNECT_REQ_EVT, + (char*)bd_addr, sizeof(bt_bdaddr_t), NULL); } /******************************************************************************* -** -** Function cleanup -** -** Description Shuts down the AV interface and does the cleanup -** -** Returns None -** -*******************************************************************************/ -static void cleanup(int service_uuid) -{ - BTIF_TRACE_EVENT("%s", __func__); - - btif_transfer_context(btif_av_handle_event, BTIF_AV_CLEANUP_REQ_EVT, NULL, 0, NULL); - - btif_disable_service(service_uuid); + * + * Function cleanup + * + * Description Shuts down the AV interface and does the cleanup + * + * Returns None + * + ******************************************************************************/ +static void cleanup(int service_uuid) { + BTIF_TRACE_EVENT("%s", __func__); - alarm_free(av_open_on_rc_timer); - av_open_on_rc_timer = NULL; + btif_transfer_context(btif_av_handle_event, BTIF_AV_CLEANUP_REQ_EVT, NULL, 0, + NULL); - /* Also shut down the AV state machine */ - btif_sm_shutdown(btif_av_cb.sm_handle); - btif_av_cb.sm_handle = NULL; + btif_disable_service(service_uuid); + + alarm_free(av_open_on_rc_timer); + av_open_on_rc_timer = NULL; + + /* Also shut down the AV state machine */ + btif_sm_shutdown(btif_av_cb.sm_handle); + btif_av_cb.sm_handle = NULL; } static void cleanup_src(void) { - BTIF_TRACE_EVENT("%s", __func__); + BTIF_TRACE_EVENT("%s", __func__); - if (bt_av_src_callbacks) - { - bt_av_src_callbacks = NULL; - if (bt_av_sink_callbacks == NULL) - cleanup(BTA_A2DP_SOURCE_SERVICE_ID); - } + if (bt_av_src_callbacks) { + bt_av_src_callbacks = NULL; + if (bt_av_sink_callbacks == NULL) cleanup(BTA_A2DP_SOURCE_SERVICE_ID); + } } static void cleanup_sink(void) { - BTIF_TRACE_EVENT("%s", __func__); + BTIF_TRACE_EVENT("%s", __func__); - if (bt_av_sink_callbacks) - { - bt_av_sink_callbacks = NULL; - if (bt_av_src_callbacks == NULL) - cleanup(BTA_A2DP_SINK_SERVICE_ID); - } + if (bt_av_sink_callbacks) { + bt_av_sink_callbacks = NULL; + if (bt_av_src_callbacks == NULL) cleanup(BTA_A2DP_SINK_SERVICE_ID); + } } static const btav_interface_t bt_av_src_interface = { @@ -1429,284 +1364,265 @@ static const btav_interface_t bt_av_sink_interface = { }; /******************************************************************************* -** -** Function btif_av_get_addr -** -** Description Fetches current AV BD address -** -** Returns BD address -** -*******************************************************************************/ - -bt_bdaddr_t btif_av_get_addr(void) -{ - return btif_av_cb.peer_bda; -} + * + * Function btif_av_get_addr + * + * Description Fetches current AV BD address + * + * Returns BD address + * + ******************************************************************************/ + +bt_bdaddr_t btif_av_get_addr(void) { return btif_av_cb.peer_bda; } /******************************************************************************* -** Function btif_av_is_sink_enabled -** -** Description Checks if A2DP Sink is enabled or not -** -** Returns true if A2DP Sink is enabled, false otherwise -** -*******************************************************************************/ - -bool btif_av_is_sink_enabled(void) -{ - return (bt_av_sink_callbacks != NULL) ? true : false; + * Function btif_av_is_sink_enabled + * + * Description Checks if A2DP Sink is enabled or not + * + * Returns true if A2DP Sink is enabled, false otherwise + * + ******************************************************************************/ + +bool btif_av_is_sink_enabled(void) { + return (bt_av_sink_callbacks != NULL) ? true : false; } /******************************************************************************* -** -** Function btif_av_stream_ready -** -** Description Checks whether AV is ready for starting a stream -** -** Returns None -** -*******************************************************************************/ - -bool btif_av_stream_ready(void) -{ - btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle); - - BTIF_TRACE_DEBUG("btif_av_stream_ready : sm hdl %d, state %d, flags %x", - btif_av_cb.sm_handle, state, btif_av_cb.flags); - - /* also make sure main adapter is enabled */ - if (btif_is_enabled() == 0) - { - BTIF_TRACE_EVENT("main adapter not enabled"); - return false; - } + * + * Function btif_av_stream_ready + * + * Description Checks whether AV is ready for starting a stream + * + * Returns None + * + ******************************************************************************/ - /* check if we are remotely suspended or stop is pending */ - if (btif_av_cb.flags & (BTIF_AV_FLAG_REMOTE_SUSPEND|BTIF_AV_FLAG_PENDING_STOP)) - return false; +bool btif_av_stream_ready(void) { + btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle); + + BTIF_TRACE_DEBUG("btif_av_stream_ready : sm hdl %d, state %d, flags %x", + btif_av_cb.sm_handle, state, btif_av_cb.flags); - return (state == BTIF_AV_STATE_OPENED); + /* also make sure main adapter is enabled */ + if (btif_is_enabled() == 0) { + BTIF_TRACE_EVENT("main adapter not enabled"); + return false; + } + + /* check if we are remotely suspended or stop is pending */ + if (btif_av_cb.flags & + (BTIF_AV_FLAG_REMOTE_SUSPEND | BTIF_AV_FLAG_PENDING_STOP)) + return false; + + return (state == BTIF_AV_STATE_OPENED); } /******************************************************************************* -** -** Function btif_av_stream_started_ready -** -** Description Checks whether AV ready for media start in streaming state -** -** Returns None -** -*******************************************************************************/ - -bool btif_av_stream_started_ready(void) -{ - btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle); - - BTIF_TRACE_DEBUG("btif_av_stream_started : sm hdl %d, state %d, flags %x", - btif_av_cb.sm_handle, state, btif_av_cb.flags); - - /* disallow media task to start if we have pending actions */ - if (btif_av_cb.flags & (BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING | BTIF_AV_FLAG_REMOTE_SUSPEND - | BTIF_AV_FLAG_PENDING_STOP)) - return false; + * + * Function btif_av_stream_started_ready + * + * Description Checks whether AV ready for media start in streaming state + * + * Returns None + * + ******************************************************************************/ - return (state == BTIF_AV_STATE_STARTED); +bool btif_av_stream_started_ready(void) { + btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle); + + BTIF_TRACE_DEBUG("btif_av_stream_started : sm hdl %d, state %d, flags %x", + btif_av_cb.sm_handle, state, btif_av_cb.flags); + + /* disallow media task to start if we have pending actions */ + if (btif_av_cb.flags & + (BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING | BTIF_AV_FLAG_REMOTE_SUSPEND | + BTIF_AV_FLAG_PENDING_STOP)) + return false; + + return (state == BTIF_AV_STATE_STARTED); } /******************************************************************************* -** -** Function btif_dispatch_sm_event -** -** Description Send event to AV statemachine -** -** Returns None -** -*******************************************************************************/ + * + * Function btif_dispatch_sm_event + * + * Description Send event to AV statemachine + * + * Returns None + * + ******************************************************************************/ /* used to pass events to AV statemachine from other tasks */ -void btif_dispatch_sm_event(btif_av_sm_event_t event, void *p_data, int len) -{ - /* Switch to BTIF context */ - btif_transfer_context(btif_av_handle_event, event, - (char*)p_data, len, NULL); +void btif_dispatch_sm_event(btif_av_sm_event_t event, void* p_data, int len) { + /* Switch to BTIF context */ + btif_transfer_context(btif_av_handle_event, event, (char*)p_data, len, NULL); } /******************************************************************************* -** -** Function btif_av_execute_service -** -** Description Initializes/Shuts down the service -** -** Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_av_execute_service(bool b_enable) -{ - if (b_enable) - { - /* TODO: Removed BTA_SEC_AUTHORIZE since the Java/App does not - * handle this request in order to allow incoming connections to succeed. - * We need to put this back once support for this is added */ - - /* Added BTA_AV_FEAT_NO_SCO_SSPD - this ensures that the BTA does not - * auto-suspend av streaming on AG events(SCO or Call). The suspend shall - * be initiated by the app/audioflinger layers */ - /* Support for browsing for SDP record should work only if we enable BROWSE - * while registering. */ + * + * Function btif_av_execute_service + * + * Description Initializes/Shuts down the service + * + * Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_av_execute_service(bool b_enable) { + if (b_enable) { +/* TODO: Removed BTA_SEC_AUTHORIZE since the Java/App does not + * handle this request in order to allow incoming connections to succeed. + * We need to put this back once support for this is added */ + +/* Added BTA_AV_FEAT_NO_SCO_SSPD - this ensures that the BTA does not + * auto-suspend av streaming on AG events(SCO or Call). The suspend shall + * be initiated by the app/audioflinger layers */ +/* Support for browsing for SDP record should work only if we enable BROWSE + * while registering. */ #if (AVRC_METADATA_INCLUDED == TRUE) - BTA_AvEnable(BTA_SEC_AUTHENTICATE, - BTA_AV_FEAT_RCTG|BTA_AV_FEAT_METADATA|BTA_AV_FEAT_VENDOR|BTA_AV_FEAT_NO_SCO_SSPD + BTA_AvEnable(BTA_SEC_AUTHENTICATE, + BTA_AV_FEAT_RCTG | BTA_AV_FEAT_METADATA | BTA_AV_FEAT_VENDOR | + BTA_AV_FEAT_NO_SCO_SSPD #if (AVRC_ADV_CTRL_INCLUDED == TRUE) - |BTA_AV_FEAT_RCCT - |BTA_AV_FEAT_ADV_CTRL - |BTA_AV_FEAT_BROWSE + | BTA_AV_FEAT_RCCT | BTA_AV_FEAT_ADV_CTRL | + BTA_AV_FEAT_BROWSE #endif - ,bte_av_callback); + , + bte_av_callback); #else - BTA_AvEnable(BTA_SEC_AUTHENTICATE, (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_NO_SCO_SSPD), - bte_av_callback); + BTA_AvEnable(BTA_SEC_AUTHENTICATE, + (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_NO_SCO_SSPD), bte_av_callback); #endif - BTA_AvRegister(BTA_AV_CHNL_AUDIO, BTIF_AV_SERVICE_NAME, 0, NULL, - UUID_SERVCLASS_AUDIO_SOURCE); - } - else { - BTA_AvDeregister(btif_av_cb.bta_handle); - BTA_AvDisable(); - } - return BT_STATUS_SUCCESS; + BTA_AvRegister(BTA_AV_CHNL_AUDIO, BTIF_AV_SERVICE_NAME, 0, NULL, + UUID_SERVCLASS_AUDIO_SOURCE); + } else { + BTA_AvDeregister(btif_av_cb.bta_handle); + BTA_AvDisable(); + } + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_av_sink_execute_service -** -** Description Initializes/Shuts down the service -** -** Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_av_sink_execute_service(bool b_enable) -{ - if (b_enable) - { - /* Added BTA_AV_FEAT_NO_SCO_SSPD - this ensures that the BTA does not - * auto-suspend av streaming on AG events(SCO or Call). The suspend shall - * be initiated by the app/audioflinger layers */ - BTA_AvEnable(BTA_SEC_AUTHENTICATE, BTA_AV_FEAT_NO_SCO_SSPD|BTA_AV_FEAT_RCCT| - BTA_AV_FEAT_METADATA|BTA_AV_FEAT_VENDOR| - BTA_AV_FEAT_ADV_CTRL|BTA_AV_FEAT_RCTG|BTA_AV_FEAT_BROWSE, - bte_av_callback); - BTA_AvRegister(BTA_AV_CHNL_AUDIO, BTIF_AVK_SERVICE_NAME, 0, - bte_av_sink_media_callback, UUID_SERVCLASS_AUDIO_SINK); - } - else { - BTA_AvDeregister(btif_av_cb.bta_handle); - BTA_AvDisable(); - } - return BT_STATUS_SUCCESS; + * + * Function btif_av_sink_execute_service + * + * Description Initializes/Shuts down the service + * + * Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_av_sink_execute_service(bool b_enable) { + if (b_enable) { + /* Added BTA_AV_FEAT_NO_SCO_SSPD - this ensures that the BTA does not + * auto-suspend av streaming on AG events(SCO or Call). The suspend shall + * be initiated by the app/audioflinger layers */ + BTA_AvEnable(BTA_SEC_AUTHENTICATE, + BTA_AV_FEAT_NO_SCO_SSPD | BTA_AV_FEAT_RCCT | + BTA_AV_FEAT_METADATA | BTA_AV_FEAT_VENDOR | + BTA_AV_FEAT_ADV_CTRL | BTA_AV_FEAT_RCTG | + BTA_AV_FEAT_BROWSE, + bte_av_callback); + BTA_AvRegister(BTA_AV_CHNL_AUDIO, BTIF_AVK_SERVICE_NAME, 0, + bte_av_sink_media_callback, UUID_SERVCLASS_AUDIO_SINK); + } else { + BTA_AvDeregister(btif_av_cb.bta_handle); + BTA_AvDisable(); + } + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_av_get_src_interface -** -** Description Get the AV callback interface for A2DP source profile -** -** Returns btav_interface_t -** -*******************************************************************************/ -const btav_interface_t *btif_av_get_src_interface(void) -{ - BTIF_TRACE_EVENT("%s", __func__); - return &bt_av_src_interface; + * + * Function btif_av_get_src_interface + * + * Description Get the AV callback interface for A2DP source profile + * + * Returns btav_interface_t + * + ******************************************************************************/ +const btav_interface_t* btif_av_get_src_interface(void) { + BTIF_TRACE_EVENT("%s", __func__); + return &bt_av_src_interface; } /******************************************************************************* -** -** Function btif_av_get_sink_interface -** -** Description Get the AV callback interface for A2DP sink profile -** -** Returns btav_interface_t -** -*******************************************************************************/ -const btav_interface_t *btif_av_get_sink_interface(void) -{ - BTIF_TRACE_EVENT("%s", __func__); - return &bt_av_sink_interface; + * + * Function btif_av_get_sink_interface + * + * Description Get the AV callback interface for A2DP sink profile + * + * Returns btav_interface_t + * + ******************************************************************************/ +const btav_interface_t* btif_av_get_sink_interface(void) { + BTIF_TRACE_EVENT("%s", __func__); + return &bt_av_sink_interface; } /******************************************************************************* -** -** Function btif_av_is_connected -** -** Description Checks if av has a connected sink -** -** Returns bool -** -*******************************************************************************/ -bool btif_av_is_connected(void) -{ - btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle); - return ((state == BTIF_AV_STATE_OPENED) || (state == BTIF_AV_STATE_STARTED)); + * + * Function btif_av_is_connected + * + * Description Checks if av has a connected sink + * + * Returns bool + * + ******************************************************************************/ +bool btif_av_is_connected(void) { + btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle); + return ((state == BTIF_AV_STATE_OPENED) || (state == BTIF_AV_STATE_STARTED)); } -uint8_t btif_av_get_peer_sep(void) -{ - return btif_av_cb.peer_sep; -} +uint8_t btif_av_get_peer_sep(void) { return btif_av_cb.peer_sep; } /******************************************************************************* -** -** Function btif_av_is_peer_edr -** -** Description Check if the connected a2dp device supports -** EDR or not. Only when connected this function -** will accurately provide a true capability of -** remote peer. If not connected it will always be false. -** -** Returns true if remote device is capable of EDR -** -*******************************************************************************/ -bool btif_av_is_peer_edr(void) -{ - ASSERTC(btif_av_is_connected(), "No active a2dp connection", 0); - - if (btif_av_cb.edr) - return true; - else - return false; + * + * Function btif_av_is_peer_edr + * + * Description Check if the connected a2dp device supports + * EDR or not. Only when connected this function + * will accurately provide a true capability of + * remote peer. If not connected it will always be false. + * + * Returns true if remote device is capable of EDR + * + ******************************************************************************/ +bool btif_av_is_peer_edr(void) { + ASSERTC(btif_av_is_connected(), "No active a2dp connection", 0); + + if (btif_av_cb.edr) + return true; + else + return false; } /****************************************************************************** -** -** Function btif_av_clear_remote_suspend_flag -** -** Description Clears btif_av_cd.flags if BTIF_AV_FLAG_REMOTE_SUSPEND is set -** -** Returns void -******************************************************************************/ -void btif_av_clear_remote_suspend_flag(void) -{ - BTIF_TRACE_DEBUG("%s: flag :%x",__func__, btif_av_cb.flags); - btif_av_cb.flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND; + * + * Function btif_av_clear_remote_suspend_flag + * + * Description Clears btif_av_cd.flags if BTIF_AV_FLAG_REMOTE_SUSPEND is set + * + * Returns void + *****************************************************************************/ +void btif_av_clear_remote_suspend_flag(void) { + BTIF_TRACE_DEBUG("%s: flag :%x", __func__, btif_av_cb.flags); + btif_av_cb.flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND; } /******************************************************************************* -** -** Function btif_av_peer_supports_3mbps -** -** Description Check if the connected A2DP device supports -** 3 Mbps EDR. This function only works if connected. -** If not connected it will always be false. -** -** Returns true if remote device is EDR and supports 3 Mbps -** -*******************************************************************************/ -bool btif_av_peer_supports_3mbps(void) -{ - bool is3mbps = ((btif_av_cb.edr & BTA_AV_EDR_3MBPS) != 0); - BTIF_TRACE_DEBUG("%s: connected %d, edr_3mbps %d", __func__, - btif_av_is_connected(), is3mbps); - return (btif_av_is_connected() && is3mbps); + * + * Function btif_av_peer_supports_3mbps + * + * Description Check if the connected A2DP device supports + * 3 Mbps EDR. This function only works if connected. + * If not connected it will always be false. + * + * Returns true if remote device is EDR and supports 3 Mbps + * + ******************************************************************************/ +bool btif_av_peer_supports_3mbps(void) { + bool is3mbps = ((btif_av_cb.edr & BTA_AV_EDR_3MBPS) != 0); + BTIF_TRACE_DEBUG("%s: connected %d, edr_3mbps %d", __func__, + btif_av_is_connected(), is3mbps); + return (btif_av_is_connected() && is3mbps); } diff --git a/btif/src/btif_avrcp_audio_track.cc b/btif/src/btif_avrcp_audio_track.cc index 73d79970d..2833dfb87 100644 --- a/btif/src/btif_avrcp_audio_track.cc +++ b/btif/src/btif_avrcp_audio_track.cc @@ -26,128 +26,116 @@ using namespace android; -typedef struct { - android::sp track; -} BtifAvrcpAudioTrack; +typedef struct { android::sp track; } BtifAvrcpAudioTrack; #if (DUMP_PCM_DATA == TRUE) -FILE *outputPcmSampleFile; +FILE* outputPcmSampleFile; char outputFilename[50] = "/data/misc/bluedroid/output_sample.pcm"; #endif -void *BtifAvrcpAudioTrackCreate(int trackFreq, int channelType) -{ - LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btCreateTrack freq %d channel %d ", - __func__, trackFreq, channelType); - sp track = - new android::AudioTrack(AUDIO_STREAM_MUSIC, trackFreq, AUDIO_FORMAT_PCM_16_BIT, - channelType, (size_t) 0 /*frameCount*/, - (audio_output_flags_t)AUDIO_OUTPUT_FLAG_FAST, - NULL /*callback_t*/, NULL /*void* user*/, 0 /*notificationFrames*/, - AUDIO_SESSION_ALLOCATE, android::AudioTrack::TRANSFER_SYNC); - assert(track != NULL); - - BtifAvrcpAudioTrack *trackHolder = new BtifAvrcpAudioTrack; - assert(trackHolder != NULL); - trackHolder->track = track; - - if (trackHolder->track->initCheck() != 0) - { - return nullptr; - } +void* BtifAvrcpAudioTrackCreate(int trackFreq, int channelType) { + LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btCreateTrack freq %d channel %d ", + __func__, trackFreq, channelType); + sp track = new android::AudioTrack( + AUDIO_STREAM_MUSIC, trackFreq, AUDIO_FORMAT_PCM_16_BIT, channelType, + (size_t)0 /*frameCount*/, (audio_output_flags_t)AUDIO_OUTPUT_FLAG_FAST, + NULL /*callback_t*/, NULL /*void* user*/, 0 /*notificationFrames*/, + AUDIO_SESSION_ALLOCATE, android::AudioTrack::TRANSFER_SYNC); + assert(track != NULL); + + BtifAvrcpAudioTrack* trackHolder = new BtifAvrcpAudioTrack; + assert(trackHolder != NULL); + trackHolder->track = track; + + if (trackHolder->track->initCheck() != 0) { + return nullptr; + } #if (DUMP_PCM_DATA == TRUE) - outputPcmSampleFile = fopen(outputFilename, "ab"); + outputPcmSampleFile = fopen(outputFilename, "ab"); #endif - trackHolder->track->setVolume(1, 1); - return (void *)trackHolder; + trackHolder->track->setVolume(1, 1); + return (void*)trackHolder; } -void BtifAvrcpAudioTrackStart(void *handle) -{ - assert(handle != NULL); - BtifAvrcpAudioTrack *trackHolder = static_cast(handle); - assert(trackHolder != NULL); - assert(trackHolder->track != NULL); - LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__); - trackHolder->track->start(); +void BtifAvrcpAudioTrackStart(void* handle) { + assert(handle != NULL); + BtifAvrcpAudioTrack* trackHolder = static_cast(handle); + assert(trackHolder != NULL); + assert(trackHolder->track != NULL); + LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__); + trackHolder->track->start(); } -void BtifAvrcpAudioTrackStop(void *handle) -{ - if (handle == NULL) { - LOG_DEBUG(LOG_TAG, "%s handle is null.", __func__); - return; - } - BtifAvrcpAudioTrack *trackHolder = static_cast(handle); - if (trackHolder != NULL && trackHolder->track != NULL) { - LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__); - trackHolder->track->stop(); - } +void BtifAvrcpAudioTrackStop(void* handle) { + if (handle == NULL) { + LOG_DEBUG(LOG_TAG, "%s handle is null.", __func__); + return; + } + BtifAvrcpAudioTrack* trackHolder = static_cast(handle); + if (trackHolder != NULL && trackHolder->track != NULL) { + LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__); + trackHolder->track->stop(); + } } -void BtifAvrcpAudioTrackDelete(void *handle) -{ - if (handle == NULL) { - LOG_DEBUG(LOG_TAG, "%s handle is null.", __func__); - return; - } - BtifAvrcpAudioTrack *trackHolder = static_cast(handle); - if (trackHolder != NULL && trackHolder->track != NULL) { - LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__); - delete trackHolder; - } +void BtifAvrcpAudioTrackDelete(void* handle) { + if (handle == NULL) { + LOG_DEBUG(LOG_TAG, "%s handle is null.", __func__); + return; + } + BtifAvrcpAudioTrack* trackHolder = static_cast(handle); + if (trackHolder != NULL && trackHolder->track != NULL) { + LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__); + delete trackHolder; + } #if (DUMP_PCM_DATA == TRUE) - if (outputPcmSampleFile) - { - fclose(outputPcmSampleFile); - } - outputPcmSampleFile = NULL; + if (outputPcmSampleFile) { + fclose(outputPcmSampleFile); + } + outputPcmSampleFile = NULL; #endif } -void BtifAvrcpAudioTrackPause(void *handle) -{ - if (handle == NULL) { - LOG_DEBUG(LOG_TAG, "%s handle is null.", __func__); - return; - } - BtifAvrcpAudioTrack *trackHolder = static_cast(handle); - if (trackHolder != NULL && trackHolder->track != NULL) { - LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__); - trackHolder->track->pause(); - trackHolder->track->flush(); - } +void BtifAvrcpAudioTrackPause(void* handle) { + if (handle == NULL) { + LOG_DEBUG(LOG_TAG, "%s handle is null.", __func__); + return; + } + BtifAvrcpAudioTrack* trackHolder = static_cast(handle); + if (trackHolder != NULL && trackHolder->track != NULL) { + LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__); + trackHolder->track->pause(); + trackHolder->track->flush(); + } } -void BtifAvrcpSetAudioTrackGain(void *handle, float gain) -{ - if (handle == NULL) { - LOG_DEBUG(LOG_TAG, "%s handle is null.", __func__); - return; - } - BtifAvrcpAudioTrack *trackHolder = static_cast(handle); - if (trackHolder != NULL && trackHolder->track != NULL) { - LOG_VERBOSE(LOG_TAG, "%s set gain %f", __func__, gain); - trackHolder->track->setVolume(gain); - } +void BtifAvrcpSetAudioTrackGain(void* handle, float gain) { + if (handle == NULL) { + LOG_DEBUG(LOG_TAG, "%s handle is null.", __func__); + return; + } + BtifAvrcpAudioTrack* trackHolder = static_cast(handle); + if (trackHolder != NULL && trackHolder->track != NULL) { + LOG_VERBOSE(LOG_TAG, "%s set gain %f", __func__, gain); + trackHolder->track->setVolume(gain); + } } -int BtifAvrcpAudioTrackWriteData(void *handle, void *audioBuffer, int bufferlen) -{ - BtifAvrcpAudioTrack *trackHolder = static_cast(handle); - assert(trackHolder != NULL); - assert(trackHolder->track != NULL); - int retval = -1; +int BtifAvrcpAudioTrackWriteData(void* handle, void* audioBuffer, + int bufferlen) { + BtifAvrcpAudioTrack* trackHolder = static_cast(handle); + assert(trackHolder != NULL); + assert(trackHolder->track != NULL); + int retval = -1; #if (DUMP_PCM_DATA == TRUE) - if (outputPcmSampleFile) - { - fwrite ((audioBuffer), 1, (size_t)bufferlen, outputPcmSampleFile); - } + if (outputPcmSampleFile) { + fwrite((audioBuffer), 1, (size_t)bufferlen, outputPcmSampleFile); + } #endif - retval = trackHolder->track->write(audioBuffer, (size_t)bufferlen); - LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btWriteData len = %d ret = %d", - __func__, bufferlen, retval); - return retval; + retval = trackHolder->track->write(audioBuffer, (size_t)bufferlen); + LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btWriteData len = %d ret = %d", __func__, + bufferlen, retval); + return retval; } diff --git a/btif/src/btif_config.cc b/btif/src/btif_config.cc index b86de01a1..6b4a2ebfb 100644 --- a/btif/src/btif_config.cc +++ b/btif/src/btif_config.cc @@ -23,10 +23,10 @@ #include #include #include -#include #include #include #include +#include #include @@ -56,23 +56,24 @@ static const char* TIME_STRING_FORMAT = "%Y-%m-%d %H:%M:%S"; // TODO(armansito): Find a better way than searching by a hardcoded path. #if defined(OS_GENERIC) -static const char *CONFIG_FILE_PATH = "bt_config.conf"; -static const char *CONFIG_BACKUP_PATH = "bt_config.bak"; -static const char *CONFIG_LEGACY_FILE_PATH = "bt_config.xml"; -#else // !defined(OS_GENERIC) -static const char *CONFIG_FILE_PATH = "/data/misc/bluedroid/bt_config.conf"; -static const char *CONFIG_BACKUP_PATH = "/data/misc/bluedroid/bt_config.bak"; -static const char *CONFIG_LEGACY_FILE_PATH = "/data/misc/bluedroid/bt_config.xml"; +static const char* CONFIG_FILE_PATH = "bt_config.conf"; +static const char* CONFIG_BACKUP_PATH = "bt_config.bak"; +static const char* CONFIG_LEGACY_FILE_PATH = "bt_config.xml"; +#else // !defined(OS_GENERIC) +static const char* CONFIG_FILE_PATH = "/data/misc/bluedroid/bt_config.conf"; +static const char* CONFIG_BACKUP_PATH = "/data/misc/bluedroid/bt_config.bak"; +static const char* CONFIG_LEGACY_FILE_PATH = + "/data/misc/bluedroid/bt_config.xml"; #endif // defined(OS_GENERIC) static const period_ms_t CONFIG_SETTLE_PERIOD_MS = 3000; -static void timer_config_save_cb(void *data); -static void btif_config_write(uint16_t event, char *p_param); +static void timer_config_save_cb(void* data); +static void btif_config_write(uint16_t event, char* p_param); static bool is_factory_reset(void); static void delete_config_files(void); -static void btif_config_remove_unpaired(config_t *config); -static void btif_config_remove_restricted(config_t *config); -static config_t *btif_config_open(const char* filename); +static void btif_config_remove_unpaired(config_t* config); +static void btif_config_remove_restricted(config_t* config); +static config_t* btif_config_open(const char* filename); static enum ConfigSource { NOT_LOADED, @@ -86,55 +87,51 @@ static enum ConfigSource { static int btif_config_devices_loaded = -1; static char btif_config_time_created[TIME_STRING_LENGTH]; -// TODO(zachoverflow): Move these two functions out, because they are too specific for this file +// TODO(zachoverflow): Move these two functions out, because they are too +// specific for this file // {grumpy-cat/no, monty-python/you-make-me-sad} -bool btif_get_device_type(const BD_ADDR bd_addr, int *p_device_type) -{ - if (p_device_type == NULL) - return false; +bool btif_get_device_type(const BD_ADDR bd_addr, int* p_device_type) { + if (p_device_type == NULL) return false; - bt_bdaddr_t bda; - bdcpy(bda.address, bd_addr); + bt_bdaddr_t bda; + bdcpy(bda.address, bd_addr); - bdstr_t bd_addr_str; - bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str)); + bdstr_t bd_addr_str; + bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str)); - if (!btif_config_get_int(bd_addr_str, "DevType", p_device_type)) - return false; + if (!btif_config_get_int(bd_addr_str, "DevType", p_device_type)) return false; - LOG_DEBUG(LOG_TAG, "%s: Device [%s] type %d", __func__, bd_addr_str, *p_device_type); - return true; + LOG_DEBUG(LOG_TAG, "%s: Device [%s] type %d", __func__, bd_addr_str, + *p_device_type); + return true; } -bool btif_get_address_type(const BD_ADDR bd_addr, int *p_addr_type) -{ - if (p_addr_type == NULL) - return false; +bool btif_get_address_type(const BD_ADDR bd_addr, int* p_addr_type) { + if (p_addr_type == NULL) return false; - bt_bdaddr_t bda; - bdcpy(bda.address, bd_addr); + bt_bdaddr_t bda; + bdcpy(bda.address, bd_addr); - bdstr_t bd_addr_str; - bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str)); + bdstr_t bd_addr_str; + bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str)); - if (!btif_config_get_int(bd_addr_str, "AddrType", p_addr_type)) - return false; + if (!btif_config_get_int(bd_addr_str, "AddrType", p_addr_type)) return false; - LOG_DEBUG(LOG_TAG, "%s: Device [%s] address type %d", __func__, bd_addr_str, *p_addr_type); - return true; + LOG_DEBUG(LOG_TAG, "%s: Device [%s] address type %d", __func__, bd_addr_str, + *p_addr_type); + return true; } static std::mutex config_lock; // protects operations on |config|. -static config_t *config; -static alarm_t *config_timer; +static config_t* config; +static alarm_t* config_timer; // Module lifecycle functions -static future_t *init(void) { +static future_t* init(void) { std::unique_lock lock(config_lock); - if (is_factory_reset()) - delete_config_files(); + if (is_factory_reset()) delete_config_files(); std::string file_source; @@ -142,19 +139,23 @@ static future_t *init(void) { btif_config_source = ORIGINAL; if (!config) { LOG_WARN(LOG_TAG, "%s unable to load config file: %s; using backup.", - __func__, CONFIG_FILE_PATH); + __func__, CONFIG_FILE_PATH); config = btif_config_open(CONFIG_BACKUP_PATH); btif_config_source = BACKUP; file_source = "Backup"; } if (!config) { - LOG_WARN(LOG_TAG, "%s unable to load backup; attempting to transcode legacy file.", __func__); + LOG_WARN(LOG_TAG, + "%s unable to load backup; attempting to transcode legacy file.", + __func__); config = btif_config_transcode(CONFIG_LEGACY_FILE_PATH); btif_config_source = LEGACY; file_source = "Legacy"; } if (!config) { - LOG_ERROR(LOG_TAG, "%s unable to transcode legacy file; creating empty config.", __func__); + LOG_ERROR(LOG_TAG, + "%s unable to transcode legacy file; creating empty config.", + __func__); config = config_new_empty(); btif_config_source = NEW_FILE; file_source = "Empty"; @@ -171,8 +172,7 @@ static future_t *init(void) { btif_config_remove_unpaired(config); // Cleanup temporary pairings if we have left guest mode - if (!is_restricted_mode()) - btif_config_remove_restricted(config); + if (!is_restricted_mode()) btif_config_remove_restricted(config); // Read or set config file creation timestamp const char* time_str; @@ -182,8 +182,10 @@ static future_t *init(void) { } else { time_t current_time = time(NULL); struct tm* time_created = localtime(¤t_time); - strftime(btif_config_time_created, TIME_STRING_LENGTH, TIME_STRING_FORMAT, time_created); - config_set_string(config, INFO_SECTION, FILE_TIMESTAMP, btif_config_time_created); + strftime(btif_config_time_created, TIME_STRING_LENGTH, TIME_STRING_FORMAT, + time_created); + config_set_string(config, INFO_SECTION, FILE_TIMESTAMP, + btif_config_time_created); } // TODO(sharvil): use a non-wake alarm for this once we have @@ -208,10 +210,9 @@ error: return future_new_immediate(FUTURE_FAIL); } -static config_t *btif_config_open(const char *filename) { - config_t *config = config_new(filename); - if (!config) - return NULL; +static config_t* btif_config_open(const char* filename) { + config_t* config = config_new(filename); + if (!config) return NULL; if (!config_has_section(config, "Adapter")) { LOG_ERROR(LOG_TAG, "Config is missing adapter section"); @@ -222,12 +223,12 @@ static config_t *btif_config_open(const char *filename) { return config; } -static future_t *shut_down(void) { +static future_t* shut_down(void) { btif_config_flush(); return future_new_immediate(FUTURE_SUCCESS); } -static future_t *clean_up(void) { +static future_t* clean_up(void) { btif_config_flush(); alarm_free(config_timer); @@ -237,15 +238,13 @@ static future_t *clean_up(void) { return future_new_immediate(FUTURE_SUCCESS); } -EXPORT_SYMBOL module_t btif_config_module = { - .name = BTIF_CONFIG_MODULE, - .init = init, - .start_up = NULL, - .shut_down = shut_down, - .clean_up = clean_up -}; +EXPORT_SYMBOL module_t btif_config_module = {.name = BTIF_CONFIG_MODULE, + .init = init, + .start_up = NULL, + .shut_down = shut_down, + .clean_up = clean_up}; -bool btif_config_has_section(const char *section) { +bool btif_config_has_section(const char* section) { assert(config != NULL); assert(section != NULL); @@ -253,7 +252,7 @@ bool btif_config_has_section(const char *section) { return config_has_section(config, section); } -bool btif_config_exist(const char *section, const char *key) { +bool btif_config_exist(const char* section, const char* key) { assert(config != NULL); assert(section != NULL); assert(key != NULL); @@ -262,7 +261,7 @@ bool btif_config_exist(const char *section, const char *key) { return config_has_key(config, section, key); } -bool btif_config_get_int(const char *section, const char *key, int *value) { +bool btif_config_get_int(const char* section, const char* key, int* value) { assert(config != NULL); assert(section != NULL); assert(key != NULL); @@ -270,13 +269,12 @@ bool btif_config_get_int(const char *section, const char *key, int *value) { std::unique_lock lock(config_lock); bool ret = config_has_key(config, section, key); - if (ret) - *value = config_get_int(config, section, key, *value); + if (ret) *value = config_get_int(config, section, key, *value); return ret; } -bool btif_config_set_int(const char *section, const char *key, int value) { +bool btif_config_set_int(const char* section, const char* key, int value) { assert(config != NULL); assert(section != NULL); assert(key != NULL); @@ -287,7 +285,8 @@ bool btif_config_set_int(const char *section, const char *key, int value) { return true; } -bool btif_config_get_str(const char *section, const char *key, char *value, int *size_bytes) { +bool btif_config_get_str(const char* section, const char* key, char* value, + int* size_bytes) { assert(config != NULL); assert(section != NULL); assert(key != NULL); @@ -296,9 +295,8 @@ bool btif_config_get_str(const char *section, const char *key, char *value, int { std::unique_lock lock(config_lock); - const char *stored_value = config_get_string(config, section, key, NULL); - if (!stored_value) - return false; + const char* stored_value = config_get_string(config, section, key, NULL); + if (!stored_value) return false; strlcpy(value, stored_value, *size_bytes); } @@ -306,7 +304,8 @@ bool btif_config_get_str(const char *section, const char *key, char *value, int return true; } -bool btif_config_set_str(const char *section, const char *key, const char *value) { +bool btif_config_set_str(const char* section, const char* key, + const char* value) { assert(config != NULL); assert(section != NULL); assert(key != NULL); @@ -317,7 +316,8 @@ bool btif_config_set_str(const char *section, const char *key, const char *value return true; } -bool btif_config_get_bin(const char *section, const char *key, uint8_t *value, size_t *length) { +bool btif_config_get_bin(const char* section, const char* key, uint8_t* value, + size_t* length) { assert(config != NULL); assert(section != NULL); assert(key != NULL); @@ -325,18 +325,15 @@ bool btif_config_get_bin(const char *section, const char *key, uint8_t *value, s assert(length != NULL); std::unique_lock lock(config_lock); - const char *value_str = config_get_string(config, section, key, NULL); + const char* value_str = config_get_string(config, section, key, NULL); - if (!value_str) - return false; + if (!value_str) return false; size_t value_len = strlen(value_str); - if ((value_len % 2) != 0 || *length < (value_len / 2)) - return false; + if ((value_len % 2) != 0 || *length < (value_len / 2)) return false; for (size_t i = 0; i < value_len; ++i) - if (!isxdigit(value_str[i])) - return false; + if (!isxdigit(value_str[i])) return false; for (*length = 0; *value_str; value_str += 2, *length += 1) sscanf(value_str, "%02hhx", &value[*length]); @@ -344,31 +341,30 @@ bool btif_config_get_bin(const char *section, const char *key, uint8_t *value, s return true; } -size_t btif_config_get_bin_length(const char *section, const char *key) { +size_t btif_config_get_bin_length(const char* section, const char* key) { assert(config != NULL); assert(section != NULL); assert(key != NULL); std::unique_lock lock(config_lock); - const char *value_str = config_get_string(config, section, key, NULL); - if (!value_str) - return 0; + const char* value_str = config_get_string(config, section, key, NULL); + if (!value_str) return 0; size_t value_len = strlen(value_str); return ((value_len % 2) != 0) ? 0 : (value_len / 2); } -bool btif_config_set_bin(const char *section, const char *key, const uint8_t *value, size_t length) { - const char *lookup = "0123456789abcdef"; +bool btif_config_set_bin(const char* section, const char* key, + const uint8_t* value, size_t length) { + const char* lookup = "0123456789abcdef"; assert(config != NULL); assert(section != NULL); assert(key != NULL); - if (length > 0) - assert(value != NULL); + if (length > 0) assert(value != NULL); - char *str = (char *)osi_calloc(length * 2 + 1); + char* str = (char*)osi_calloc(length * 2 + 1); for (size_t i = 0; i < length; ++i) { str[(i * 2) + 0] = lookup[(value[i] >> 4) & 0x0F]; @@ -384,29 +380,32 @@ bool btif_config_set_bin(const char *section, const char *key, const uint8_t *va return true; } -const btif_config_section_iter_t *btif_config_section_begin(void) { +const btif_config_section_iter_t* btif_config_section_begin(void) { assert(config != NULL); - return (const btif_config_section_iter_t *)config_section_begin(config); + return (const btif_config_section_iter_t*)config_section_begin(config); } -const btif_config_section_iter_t *btif_config_section_end(void) { +const btif_config_section_iter_t* btif_config_section_end(void) { assert(config != NULL); - return (const btif_config_section_iter_t *)config_section_end(config); + return (const btif_config_section_iter_t*)config_section_end(config); } -const btif_config_section_iter_t *btif_config_section_next(const btif_config_section_iter_t *section) { +const btif_config_section_iter_t* btif_config_section_next( + const btif_config_section_iter_t* section) { assert(config != NULL); assert(section != NULL); - return (const btif_config_section_iter_t *)config_section_next((const config_section_node_t *)section); + return (const btif_config_section_iter_t*)config_section_next( + (const config_section_node_t*)section); } -const char *btif_config_section_name(const btif_config_section_iter_t *section) { +const char* btif_config_section_name( + const btif_config_section_iter_t* section) { assert(config != NULL); assert(section != NULL); - return config_section_name((const config_section_node_t *)section); + return config_section_name((const config_section_node_t*)section); } -bool btif_config_remove(const char *section, const char *key) { +bool btif_config_remove(const char* section, const char* key) { assert(config != NULL); assert(section != NULL); assert(key != NULL); @@ -440,43 +439,43 @@ bool btif_config_clear(void) { config_free(config); config = config_new_empty(); - if (config == NULL) - return false; + if (config == NULL) return false; bool ret = config_save(config, CONFIG_FILE_PATH); btif_config_source = RESET; return ret; } -static void timer_config_save_cb(UNUSED_ATTR void *data) { +static void timer_config_save_cb(UNUSED_ATTR void* data) { // Moving file I/O to btif context instead of timer callback because // it usually takes a lot of time to be completed, introducing // delays during A2DP playback causing blips or choppiness. btif_transfer_context(btif_config_write, 0, NULL, 0, NULL); } -static void btif_config_write(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param) { +static void btif_config_write(UNUSED_ATTR uint16_t event, + UNUSED_ATTR char* p_param) { assert(config != NULL); assert(config_timer != NULL); std::unique_lock lock(config_lock); rename(CONFIG_FILE_PATH, CONFIG_BACKUP_PATH); - config_t *config_paired = config_new_clone(config); + config_t* config_paired = config_new_clone(config); btif_config_remove_unpaired(config_paired); config_save(config_paired, CONFIG_FILE_PATH); config_free(config_paired); } -static void btif_config_remove_unpaired(config_t *conf) { +static void btif_config_remove_unpaired(config_t* conf) { assert(conf != NULL); int paired_devices = 0; // The paired config used to carry information about // discovered devices during regular inquiry scans. // We remove these now and cache them in memory instead. - const config_section_node_t *snode = config_section_begin(conf); + const config_section_node_t* snode = config_section_begin(conf); while (snode != config_section_end(conf)) { - const char *section = config_section_name(snode); + const char* section = config_section_name(snode); if (string_is_bdaddr(section)) { if (!config_has_key(conf, section, "LinkKey") && !config_has_key(conf, section, "LE_KEY_PENC") && @@ -499,45 +498,46 @@ static void btif_config_remove_unpaired(config_t *conf) { } void btif_debug_config_dump(int fd) { - dprintf(fd, "\nBluetooth Config:\n"); - - dprintf(fd, " Config Source: "); - switch(btif_config_source) { - case NOT_LOADED: - dprintf(fd, "Not loaded\n"); - break; - case ORIGINAL: - dprintf(fd, "Original file\n"); - break; - case BACKUP: - dprintf(fd, "Backup file\n"); - break; - case LEGACY: - dprintf(fd, "Legacy file\n"); - break; - case NEW_FILE: - dprintf(fd, "New file\n"); - break; - case RESET: - dprintf(fd, "Reset file\n"); - break; - } + dprintf(fd, "\nBluetooth Config:\n"); + + dprintf(fd, " Config Source: "); + switch (btif_config_source) { + case NOT_LOADED: + dprintf(fd, "Not loaded\n"); + break; + case ORIGINAL: + dprintf(fd, "Original file\n"); + break; + case BACKUP: + dprintf(fd, "Backup file\n"); + break; + case LEGACY: + dprintf(fd, "Legacy file\n"); + break; + case NEW_FILE: + dprintf(fd, "New file\n"); + break; + case RESET: + dprintf(fd, "Reset file\n"); + break; + } - dprintf(fd, " Devices loaded: %d\n", btif_config_devices_loaded); - dprintf(fd, " File created/tagged: %s\n", btif_config_time_created); - dprintf(fd, " File source: %s\n", config_get_string(config, INFO_SECTION, - FILE_SOURCE, "Original")); + dprintf(fd, " Devices loaded: %d\n", btif_config_devices_loaded); + dprintf(fd, " File created/tagged: %s\n", btif_config_time_created); + dprintf(fd, " File source: %s\n", + config_get_string(config, INFO_SECTION, FILE_SOURCE, "Original")); } static void btif_config_remove_restricted(config_t* config) { assert(config != NULL); - const config_section_node_t *snode = config_section_begin(config); + const config_section_node_t* snode = config_section_begin(config); while (snode != config_section_end(config)) { - const char *section = config_section_name(snode); - if (string_is_bdaddr(section) && config_has_key(config, section, "Restricted")) { - BTIF_TRACE_DEBUG("%s: Removing restricted device %s", __func__, section); - config_remove_section(config, section); + const char* section = config_section_name(snode); + if (string_is_bdaddr(section) && + config_has_key(config, section, "Restricted")) { + BTIF_TRACE_DEBUG("%s: Removing restricted device %s", __func__, section); + config_remove_section(config, section); } snode = config_section_next(snode); } diff --git a/btif/src/btif_config_transcode.cc b/btif/src/btif_config_transcode.cc index 302ea422d..102a3af41 100644 --- a/btif/src/btif_config_transcode.cc +++ b/btif/src/btif_config_transcode.cc @@ -25,32 +25,38 @@ using namespace tinyxml2; -config_t *btif_config_transcode(const char *xml_filename) { +config_t* btif_config_transcode(const char* xml_filename) { XMLDocument document; int error = document.LoadFile(xml_filename); if (error != XML_SUCCESS) { - LOG_ERROR(LOG_TAG, "%s unable to load XML file '%s': %d", __func__, xml_filename, error); + LOG_ERROR(LOG_TAG, "%s unable to load XML file '%s': %d", __func__, + xml_filename, error); return NULL; } - XMLElement *rootElement = document.RootElement(); + XMLElement* rootElement = document.RootElement(); if (!rootElement) { - LOG_ERROR(LOG_TAG, "%s unable to find root element; assuming corrupted config file.", __func__); + LOG_ERROR(LOG_TAG, + "%s unable to find root element; assuming corrupted config file.", + __func__); return NULL; } - config_t *config = config_new_empty(); + config_t* config = config_new_empty(); if (!config) { LOG_ERROR(LOG_TAG, "%s unable to allocate config object.", __func__); return NULL; } - for (XMLElement *i = rootElement->FirstChildElement(); i != NULL; i = i->NextSiblingElement()) - for (XMLElement *j = i->FirstChildElement(); j != NULL; j = j->NextSiblingElement()) { - const char *section = j->Attribute("Tag"); - for (XMLElement *k = j->FirstChildElement(); k != NULL; k = k->NextSiblingElement()) { - const char *key = k->Attribute("Tag"); - const char *value = k->GetText(); + for (XMLElement* i = rootElement->FirstChildElement(); i != NULL; + i = i->NextSiblingElement()) + for (XMLElement* j = i->FirstChildElement(); j != NULL; + j = j->NextSiblingElement()) { + const char* section = j->Attribute("Tag"); + for (XMLElement* k = j->FirstChildElement(); k != NULL; + k = k->NextSiblingElement()) { + const char* key = k->Attribute("Tag"); + const char* value = k->GetText(); if (section && key && value) config_set_string(config, section, key, value); } diff --git a/btif/src/btif_core.cc b/btif/src/btif_core.cc index 60111a43e..2d1425118 100644 --- a/btif/src/btif_core.cc +++ b/btif/src/btif_core.cc @@ -43,13 +43,13 @@ #include #include "bdaddr.h" +#include "bt_common.h" #include "bt_utils.h" #include "bta_api.h" #include "bte.h" #include "btif_api.h" #include "btif_av.h" #include "btif_config.h" -#include "btif_config.h" #include "btif_pan.h" #include "btif_profile_queue.h" #include "btif_sock.h" @@ -58,7 +58,6 @@ #include "btif_util.h" #include "btu.h" #include "device/include/controller.h" -#include "bt_common.h" #include "osi/include/fixed_queue.h" #include "osi/include/future.h" #include "osi/include/log.h" @@ -68,8 +67,8 @@ #include "stack_manager.h" /************************************************************************************ -** Constants & Macros -************************************************************************************/ + * Constants & Macros + ***********************************************************************************/ #ifndef BTE_DID_CONF_FILE // TODO(armansito): Find a better way than searching by a hardcoded path. @@ -81,10 +80,11 @@ #endif // BTE_DID_CONF_FILE /************************************************************************************ -** Local type definitions -************************************************************************************/ + * Local type definitions + ***********************************************************************************/ -/* These type definitions are used when passing data from the HAL to BTIF context +/* These type definitions are used when passing data from the HAL to BTIF +* context * in the downstream path for the adapter and remote_device property APIs */ typedef struct { @@ -103,15 +103,15 @@ typedef union { } btif_storage_req_t; typedef enum { - BTIF_CORE_STATE_DISABLED = 0, - BTIF_CORE_STATE_ENABLING, - BTIF_CORE_STATE_ENABLED, - BTIF_CORE_STATE_DISABLING + BTIF_CORE_STATE_DISABLED = 0, + BTIF_CORE_STATE_ENABLING, + BTIF_CORE_STATE_ENABLED, + BTIF_CORE_STATE_DISABLING } btif_core_state_t; /************************************************************************************ -** Static variables -************************************************************************************/ + * Static variables + ***********************************************************************************/ bt_bdaddr_t btif_local_bd_addr; @@ -125,111 +125,111 @@ static tBTA_SERVICE_MASK btif_enabled_services = 0; */ static uint8_t btif_dut_mode = 0; -static thread_t *bt_jni_workqueue_thread; -static const char *BT_JNI_WORKQUEUE_NAME = "bt_jni_workqueue"; +static thread_t* bt_jni_workqueue_thread; +static const char* BT_JNI_WORKQUEUE_NAME = "bt_jni_workqueue"; static uid_set_t* uid_set = NULL; base::MessageLoop* message_loop_ = NULL; -base::RunLoop *jni_run_loop = NULL; +base::RunLoop* jni_run_loop = NULL; /************************************************************************************ -** Static functions -************************************************************************************/ + * Static functions + ***********************************************************************************/ static void btif_jni_associate(); static void btif_jni_disassociate(); -static bool btif_fetch_property(const char *key, bt_bdaddr_t *addr); +static bool btif_fetch_property(const char* key, bt_bdaddr_t* addr); /* sends message to btif task */ -static void btif_sendmsg(void *p_msg); +static void btif_sendmsg(void* p_msg); /************************************************************************************ -** Externs -************************************************************************************/ -extern fixed_queue_t *btu_hci_msg_queue; + * Externs + ***********************************************************************************/ +extern fixed_queue_t* btu_hci_msg_queue; -void btif_dm_execute_service_request(uint16_t event, char *p_param); +void btif_dm_execute_service_request(uint16_t event, char* p_param); #ifdef BTIF_DM_OOB_TEST void btif_dm_load_local_oob(void); #endif /******************************************************************************* -** -** Function btif_context_switched -** -** Description Callback used to execute transferred context callback -** -** p_msg : message to be executed in btif context -** -** Returns void -** -*******************************************************************************/ - -static void btif_context_switched(void *p_msg) -{ - - BTIF_TRACE_VERBOSE("btif_context_switched"); - - tBTIF_CONTEXT_SWITCH_CBACK *p = (tBTIF_CONTEXT_SWITCH_CBACK *) p_msg; - - /* each callback knows how to parse the data */ - if (p->p_cb) - p->p_cb(p->event, p->p_param); -} + * + * Function btif_context_switched + * + * Description Callback used to execute transferred context callback + * + * p_msg : message to be executed in btif context + * + * Returns void + * + ******************************************************************************/ +static void btif_context_switched(void* p_msg) { + BTIF_TRACE_VERBOSE("btif_context_switched"); + + tBTIF_CONTEXT_SWITCH_CBACK* p = (tBTIF_CONTEXT_SWITCH_CBACK*)p_msg; + + /* each callback knows how to parse the data */ + if (p->p_cb) p->p_cb(p->event, p->p_param); +} /******************************************************************************* -** -** Function btif_transfer_context -** -** Description This function switches context to btif task -** -** p_cback : callback used to process message in btif context -** event : event id of message -** p_params : parameter area passed to callback (copied) -** param_len : length of parameter area -** p_copy_cback : If set this function will be invoked for deep copy -** -** Returns void -** -*******************************************************************************/ - -bt_status_t btif_transfer_context (tBTIF_CBACK *p_cback, uint16_t event, char* p_params, int param_len, tBTIF_COPY_CBACK *p_copy_cback) -{ - tBTIF_CONTEXT_SWITCH_CBACK *p_msg = - (tBTIF_CONTEXT_SWITCH_CBACK *)osi_malloc(sizeof(tBTIF_CONTEXT_SWITCH_CBACK) + param_len); - - BTIF_TRACE_VERBOSE("btif_transfer_context event %d, len %d", event, param_len); - - /* allocate and send message that will be executed in btif context */ - p_msg->hdr.event = BT_EVT_CONTEXT_SWITCH_EVT; /* internal event */ - p_msg->p_cb = p_cback; - - p_msg->event = event; /* callback event */ - - /* check if caller has provided a copy callback to do the deep copy */ - if (p_copy_cback) { - p_copy_cback(event, p_msg->p_param, p_params); - } else if (p_params) { - memcpy(p_msg->p_param, p_params, param_len); /* callback parameter data */ - } + * + * Function btif_transfer_context + * + * Description This function switches context to btif task + * + * p_cback : callback used to process message in btif context + * event : event id of message + * p_params : parameter area passed to callback (copied) + * param_len : length of parameter area + * p_copy_cback : If set this function will be invoked for deep + *copy + * + * Returns void + * + ******************************************************************************/ - btif_sendmsg(p_msg); +bt_status_t btif_transfer_context(tBTIF_CBACK* p_cback, uint16_t event, + char* p_params, int param_len, + tBTIF_COPY_CBACK* p_copy_cback) { + tBTIF_CONTEXT_SWITCH_CBACK* p_msg = (tBTIF_CONTEXT_SWITCH_CBACK*)osi_malloc( + sizeof(tBTIF_CONTEXT_SWITCH_CBACK) + param_len); - return BT_STATUS_SUCCESS; + BTIF_TRACE_VERBOSE("btif_transfer_context event %d, len %d", event, + param_len); + + /* allocate and send message that will be executed in btif context */ + p_msg->hdr.event = BT_EVT_CONTEXT_SWITCH_EVT; /* internal event */ + p_msg->p_cb = p_cback; + + p_msg->event = event; /* callback event */ + + /* check if caller has provided a copy callback to do the deep copy */ + if (p_copy_cback) { + p_copy_cback(event, p_msg->p_param, p_params); + } else if (p_params) { + memcpy(p_msg->p_param, p_params, param_len); /* callback parameter data */ + } + + btif_sendmsg(p_msg); + + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function do_in_jni_thread -** -** Description This function posts a task into the btif message loop, that -** executes it in the JNI message loop. -** -** Returns void -** -*******************************************************************************/ + * + * Function do_in_jni_thread + * + * Description This function posts a task into the btif message loop, that + * executes it in the JNI message loop. + * + * Returns void + * + ******************************************************************************/ bt_status_t do_in_jni_thread(const base::Closure& task) { - if (!message_loop_ || !message_loop_ ->task_runner().get()) { - BTIF_TRACE_WARNING("%s: Dropped message, message_loop not initialized yet!", __func__); + if (!message_loop_ || !message_loop_->task_runner().get()) { + BTIF_TRACE_WARNING("%s: Dropped message, message_loop not initialized yet!", + __func__); return BT_STATUS_FAIL; } @@ -241,36 +241,33 @@ bt_status_t do_in_jni_thread(const base::Closure& task) { } /******************************************************************************* -** -** Function btif_is_dut_mode -** -** Description checks if BTIF is currently in DUT mode -** -** Returns 1 if test mode, otherwize 0 -** -*******************************************************************************/ - -uint8_t btif_is_dut_mode(void) -{ - return (btif_dut_mode == 1); -} + * + * Function btif_is_dut_mode + * + * Description checks if BTIF is currently in DUT mode + * + * Returns 1 if test mode, otherwize 0 + * + ******************************************************************************/ + +uint8_t btif_is_dut_mode(void) { return (btif_dut_mode == 1); } /******************************************************************************* -** -** Function btif_is_enabled -** -** Description checks if main adapter is fully enabled -** -** Returns 1 if fully enabled, otherwize 0 -** -*******************************************************************************/ - -int btif_is_enabled(void) -{ - return ((!btif_is_dut_mode()) && (stack_manager_get_interface()->get_stack_is_running())); + * + * Function btif_is_enabled + * + * Description checks if main adapter is fully enabled + * + * Returns 1 if fully enabled, otherwize 0 + * + ******************************************************************************/ + +int btif_is_enabled(void) { + return ((!btif_is_dut_mode()) && + (stack_manager_get_interface()->get_stack_is_running())); } -void btif_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param) { +void btif_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char* p_param) { BTIF_TRACE_DEBUG("btif_task: received trigger stack init event"); #if (BLE_INCLUDED == TRUE) btif_dm_load_ble_local_keys(); @@ -279,17 +276,17 @@ void btif_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param) { } /******************************************************************************* -** -** Function btif_task -** -** Description BTIF task handler managing all messages being passed -** Bluetooth HAL and BTA. -** -** Returns void -** -*******************************************************************************/ -static void bt_jni_msg_ready(void *context) { - BT_HDR *p_msg = (BT_HDR *)context; + * + * Function btif_task + * + * Description BTIF task handler managing all messages being passed + * Bluetooth HAL and BTA. + * + * Returns void + * + ******************************************************************************/ +static void bt_jni_msg_ready(void* context) { + BT_HDR* p_msg = (BT_HDR*)context; BTIF_TRACE_VERBOSE("btif task fetched event %x", p_msg->event); @@ -305,135 +302,130 @@ static void bt_jni_msg_ready(void *context) { } /******************************************************************************* -** -** Function btif_sendmsg -** -** Description Sends msg to BTIF task -** -** Returns void -** -*******************************************************************************/ - -void btif_sendmsg(void *p_msg) -{ + * + * Function btif_sendmsg + * + * Description Sends msg to BTIF task + * + * Returns void + * + ******************************************************************************/ + +void btif_sendmsg(void* p_msg) { do_in_jni_thread(base::Bind(&bt_jni_msg_ready, p_msg)); } -void btif_thread_post(thread_fn func, void *context) { +void btif_thread_post(thread_fn func, void* context) { do_in_jni_thread(base::Bind(func, context)); } -static bool btif_fetch_property(const char *key, bt_bdaddr_t *addr) { - char val[PROPERTY_VALUE_MAX] = {0}; +static bool btif_fetch_property(const char* key, bt_bdaddr_t* addr) { + char val[PROPERTY_VALUE_MAX] = {0}; - if (osi_property_get(key, val, NULL)) { - if (string_to_bdaddr(val, addr)) { - BTIF_TRACE_DEBUG("%s: Got BDA %s", __func__, val); - return true; - } - BTIF_TRACE_DEBUG("%s: System Property did not contain valid bdaddr", __func__); + if (osi_property_get(key, val, NULL)) { + if (string_to_bdaddr(val, addr)) { + BTIF_TRACE_DEBUG("%s: Got BDA %s", __func__, val); + return true; } - return false; + BTIF_TRACE_DEBUG("%s: System Property did not contain valid bdaddr", + __func__); + } + return false; } -static void btif_fetch_local_bdaddr(bt_bdaddr_t *local_addr) -{ - char val[PROPERTY_VALUE_MAX] = {0}; - uint8_t valid_bda = false; - int val_size = 0; - - const uint8_t null_bdaddr[BD_ADDR_LEN] = {0,0,0,0,0,0}; - - /* Get local bdaddr storage path from property */ - if (osi_property_get(PROPERTY_BT_BDADDR_PATH, val, NULL)) - { - int addr_fd; - - BTIF_TRACE_DEBUG("%s, local bdaddr is stored in %s", __func__, val); - - if ((addr_fd = open(val, O_RDONLY)) != -1) - { - memset(val, 0, sizeof(val)); - read(addr_fd, val, FACTORY_BT_BDADDR_STORAGE_LEN); - /* If this is not a reserved/special bda, then use it */ - if ((string_to_bdaddr(val, local_addr)) && - (memcmp(local_addr->address, null_bdaddr, BD_ADDR_LEN) != 0)) - { - valid_bda = true; - BTIF_TRACE_DEBUG("%s: Got Factory BDA %s", __func__, val); - } - close(addr_fd); - } +static void btif_fetch_local_bdaddr(bt_bdaddr_t* local_addr) { + char val[PROPERTY_VALUE_MAX] = {0}; + uint8_t valid_bda = false; + int val_size = 0; + + const uint8_t null_bdaddr[BD_ADDR_LEN] = {0, 0, 0, 0, 0, 0}; + + /* Get local bdaddr storage path from property */ + if (osi_property_get(PROPERTY_BT_BDADDR_PATH, val, NULL)) { + int addr_fd; + + BTIF_TRACE_DEBUG("%s, local bdaddr is stored in %s", __func__, val); + + if ((addr_fd = open(val, O_RDONLY)) != -1) { + memset(val, 0, sizeof(val)); + read(addr_fd, val, FACTORY_BT_BDADDR_STORAGE_LEN); + /* If this is not a reserved/special bda, then use it */ + if ((string_to_bdaddr(val, local_addr)) && + (memcmp(local_addr->address, null_bdaddr, BD_ADDR_LEN) != 0)) { + valid_bda = true; + BTIF_TRACE_DEBUG("%s: Got Factory BDA %s", __func__, val); + } + close(addr_fd); } + } - if(!valid_bda) - { - val_size = sizeof(val); - if(btif_config_get_str("Adapter", "Address", val, &val_size)) - { - string_to_bdaddr(val, local_addr); - BTIF_TRACE_DEBUG("local bdaddr from bt_config.xml is %s", val); - return; - } - } - - /* No factory BDADDR found. Look for previously generated random BDA */ - if (!valid_bda) { - valid_bda = btif_fetch_property(PERSIST_BDADDR_PROPERTY, local_addr); + if (!valid_bda) { + val_size = sizeof(val); + if (btif_config_get_str("Adapter", "Address", val, &val_size)) { + string_to_bdaddr(val, local_addr); + BTIF_TRACE_DEBUG("local bdaddr from bt_config.xml is %s", val); + return; } + } - /* No BDADDR found in file. Look for BDA in factory property */ - if (!valid_bda) { - valid_bda = btif_fetch_property(FACTORY_BT_ADDR_PROPERTY, local_addr); - } + /* No factory BDADDR found. Look for previously generated random BDA */ + if (!valid_bda) { + valid_bda = btif_fetch_property(PERSIST_BDADDR_PROPERTY, local_addr); + } - /* Generate new BDA if necessary */ - if (!valid_bda) - { - bdstr_t bdstr; + /* No BDADDR found in file. Look for BDA in factory property */ + if (!valid_bda) { + valid_bda = btif_fetch_property(FACTORY_BT_ADDR_PROPERTY, local_addr); + } - /* No autogen BDA. Generate one now. */ - local_addr->address[0] = 0x22; - local_addr->address[1] = 0x22; - local_addr->address[2] = (uint8_t) osi_rand(); - local_addr->address[3] = (uint8_t) osi_rand(); - local_addr->address[4] = (uint8_t) osi_rand(); - local_addr->address[5] = (uint8_t) osi_rand(); + /* Generate new BDA if necessary */ + if (!valid_bda) { + bdstr_t bdstr; - /* Convert to ascii, and store as a persistent property */ - bdaddr_to_string(local_addr, bdstr, sizeof(bdstr)); + /* No autogen BDA. Generate one now. */ + local_addr->address[0] = 0x22; + local_addr->address[1] = 0x22; + local_addr->address[2] = (uint8_t)osi_rand(); + local_addr->address[3] = (uint8_t)osi_rand(); + local_addr->address[4] = (uint8_t)osi_rand(); + local_addr->address[5] = (uint8_t)osi_rand(); - BTIF_TRACE_DEBUG("No preset BDA. Generating BDA: %s for prop %s", - (char*)bdstr, PERSIST_BDADDR_PROPERTY); + /* Convert to ascii, and store as a persistent property */ + bdaddr_to_string(local_addr, bdstr, sizeof(bdstr)); - if (osi_property_set(PERSIST_BDADDR_PROPERTY, (char*)bdstr) < 0) - BTIF_TRACE_ERROR("Failed to set random BDA in prop %s",PERSIST_BDADDR_PROPERTY); - } + BTIF_TRACE_DEBUG("No preset BDA. Generating BDA: %s for prop %s", + (char*)bdstr, PERSIST_BDADDR_PROPERTY); - //save the bd address to config file - bdstr_t bdstr; - bdaddr_to_string(local_addr, bdstr, sizeof(bdstr)); - val_size = sizeof(val); - if (btif_config_get_str("Adapter", "Address", val, &val_size)) - { - if (strcmp(bdstr, val) ==0) - { - // BDA is already present in the config file. - return; - } + if (osi_property_set(PERSIST_BDADDR_PROPERTY, (char*)bdstr) < 0) + BTIF_TRACE_ERROR("Failed to set random BDA in prop %s", + PERSIST_BDADDR_PROPERTY); + } + + // save the bd address to config file + bdstr_t bdstr; + bdaddr_to_string(local_addr, bdstr, sizeof(bdstr)); + val_size = sizeof(val); + if (btif_config_get_str("Adapter", "Address", val, &val_size)) { + if (strcmp(bdstr, val) == 0) { + // BDA is already present in the config file. + return; } - btif_config_set_str("Adapter", "Address", bdstr); + } + btif_config_set_str("Adapter", "Address", bdstr); } -void run_message_loop(UNUSED_ATTR void *context) { - // TODO(jpawlowski): exit_manager should be defined in main(), but there is no main method. - // It is therefore defined in bt_jni_workqueue_thread, and will be deleted when we free it. +void run_message_loop(UNUSED_ATTR void* context) { + // TODO(jpawlowski): exit_manager should be defined in main(), but there is no + // main method. + // It is therefore defined in bt_jni_workqueue_thread, and will be deleted + // when we free it. base::AtExitManager exit_manager; message_loop_ = new base::MessageLoop(base::MessageLoop::Type::TYPE_DEFAULT); // Associate this workqueue thread with JNI. - message_loop_->task_runner()->PostTask(FROM_HERE, base::Bind(&btif_jni_associate)); + message_loop_->task_runner()->PostTask(FROM_HERE, + base::Bind(&btif_jni_associate)); jni_run_loop = new base::RunLoop(); jni_run_loop->Run(); @@ -445,14 +437,14 @@ void run_message_loop(UNUSED_ATTR void *context) { jni_run_loop = NULL; } /******************************************************************************* -** -** Function btif_init_bluetooth -** -** Description Creates BTIF task and prepares BT scheduler for startup -** -** Returns bt_status_t -** -*******************************************************************************/ + * + * Function btif_init_bluetooth + * + * Description Creates BTIF task and prepares BT scheduler for startup + * + * Returns bt_status_t + * + ******************************************************************************/ bt_status_t btif_init_bluetooth() { bte_main_boot_entry(); @@ -462,7 +454,8 @@ bt_status_t btif_init_bluetooth() { bt_jni_workqueue_thread = thread_new(BT_JNI_WORKQUEUE_NAME); if (bt_jni_workqueue_thread == NULL) { - LOG_ERROR(LOG_TAG, "%s Unable to create thread %s", __func__, BT_JNI_WORKQUEUE_NAME); + LOG_ERROR(LOG_TAG, "%s Unable to create thread %s", __func__, + BT_JNI_WORKQUEUE_NAME); goto error_exit; } @@ -471,889 +464,842 @@ bt_status_t btif_init_bluetooth() { return BT_STATUS_SUCCESS; error_exit:; - thread_free(bt_jni_workqueue_thread); + thread_free(bt_jni_workqueue_thread); - bt_jni_workqueue_thread = NULL; + bt_jni_workqueue_thread = NULL; - return BT_STATUS_FAIL; + return BT_STATUS_FAIL; } /******************************************************************************* -** -** Function btif_enable_bluetooth_evt -** -** Description Event indicating bluetooth enable is completed -** Notifies HAL user with updated adapter state -** -** Returns void -** -*******************************************************************************/ - -void btif_enable_bluetooth_evt(tBTA_STATUS status) -{ - const controller_t *controller = controller_get_interface(); - bdstr_t bdstr; - bdaddr_to_string(controller->get_address(), bdstr, sizeof(bdstr)); - - BTIF_TRACE_DEBUG("%s: status %d, local bd [%s]", __func__, status, bdstr); - - if (bdcmp(btif_local_bd_addr.address, controller->get_address()->address)) - { - // TODO(zachoverflow): this whole code path seems like a bad time waiting to happen - // We open the vendor library using the old address. - bdstr_t old_address; - bt_property_t prop; - - bdaddr_to_string(&btif_local_bd_addr, old_address, sizeof(old_address)); - - /** - * The Controller's BDADDR does not match to the BTIF's initial BDADDR! - * This could be because the factory BDADDR was stored separately in - * the Controller's non-volatile memory rather than in device's file - * system. - **/ - BTIF_TRACE_WARNING("***********************************************"); - BTIF_TRACE_WARNING("BTIF init BDA was %s", old_address); - BTIF_TRACE_WARNING("Controller BDA is %s", bdstr); - BTIF_TRACE_WARNING("***********************************************"); - - btif_local_bd_addr = *controller->get_address(); - - //save the bd address to config file - btif_config_set_str("Adapter", "Address", bdstr); - btif_config_save(); - - //fire HAL callback for property change - prop.type = BT_PROPERTY_BDADDR; - prop.val = (void*)&btif_local_bd_addr; - prop.len = sizeof(bt_bdaddr_t); - HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop); - } + * + * Function btif_enable_bluetooth_evt + * + * Description Event indicating bluetooth enable is completed + * Notifies HAL user with updated adapter state + * + * Returns void + * + ******************************************************************************/ + +void btif_enable_bluetooth_evt(tBTA_STATUS status) { + const controller_t* controller = controller_get_interface(); + bdstr_t bdstr; + bdaddr_to_string(controller->get_address(), bdstr, sizeof(bdstr)); + + BTIF_TRACE_DEBUG("%s: status %d, local bd [%s]", __func__, status, bdstr); + + if (bdcmp(btif_local_bd_addr.address, controller->get_address()->address)) { + // TODO(zachoverflow): this whole code path seems like a bad time waiting to + // happen + // We open the vendor library using the old address. + bdstr_t old_address; + bt_property_t prop; - bte_main_postload_cfg(); + bdaddr_to_string(&btif_local_bd_addr, old_address, sizeof(old_address)); + + /** + * The Controller's BDADDR does not match to the BTIF's initial BDADDR! + * This could be because the factory BDADDR was stored separately in + * the Controller's non-volatile memory rather than in device's file + * system. + **/ + BTIF_TRACE_WARNING("***********************************************"); + BTIF_TRACE_WARNING("BTIF init BDA was %s", old_address); + BTIF_TRACE_WARNING("Controller BDA is %s", bdstr); + BTIF_TRACE_WARNING("***********************************************"); + + btif_local_bd_addr = *controller->get_address(); + + // save the bd address to config file + btif_config_set_str("Adapter", "Address", bdstr); + btif_config_save(); + + // fire HAL callback for property change + prop.type = BT_PROPERTY_BDADDR; + prop.val = (void*)&btif_local_bd_addr; + prop.len = sizeof(bt_bdaddr_t); + HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, + &prop); + } + + bte_main_postload_cfg(); #if (HCILP_INCLUDED == TRUE) - bte_main_enable_lpm(true); + bte_main_enable_lpm(true); #endif - /* add passing up bd address as well ? */ + /* add passing up bd address as well ? */ - /* callback to HAL */ - if (status == BTA_SUCCESS) - { - uid_set = uid_set_create(); + /* callback to HAL */ + if (status == BTA_SUCCESS) { + uid_set = uid_set_create(); - btif_dm_init(uid_set); + btif_dm_init(uid_set); - /* init rfcomm & l2cap api */ - btif_sock_init(uid_set); + /* init rfcomm & l2cap api */ + btif_sock_init(uid_set); - /* init pan */ - btif_pan_init(); + /* init pan */ + btif_pan_init(); - /* load did configuration */ - bte_load_did_conf(BTE_DID_CONF_FILE); + /* load did configuration */ + bte_load_did_conf(BTE_DID_CONF_FILE); #ifdef BTIF_DM_OOB_TEST - btif_dm_load_local_oob(); + btif_dm_load_local_oob(); #endif - future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS); - } - else - { - /* cleanup rfcomm & l2cap api */ - btif_sock_cleanup(); + future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS); + } else { + /* cleanup rfcomm & l2cap api */ + btif_sock_cleanup(); - btif_pan_cleanup(); + btif_pan_cleanup(); - future_ready(stack_manager_get_hack_future(), FUTURE_FAIL); - } + future_ready(stack_manager_get_hack_future(), FUTURE_FAIL); + } } /******************************************************************************* -** -** Function btif_disable_bluetooth -** -** Description Inititates shutdown of Bluetooth system. -** Any active links will be dropped and device entering -** non connectable/discoverable mode -** -** Returns void -** -*******************************************************************************/ -bt_status_t btif_disable_bluetooth(void) -{ - BTIF_TRACE_DEBUG("BTIF DISABLE BLUETOOTH"); + * + * Function btif_disable_bluetooth + * + * Description Inititates shutdown of Bluetooth system. + * Any active links will be dropped and device entering + * non connectable/discoverable mode + * + * Returns void + * + ******************************************************************************/ +bt_status_t btif_disable_bluetooth(void) { + BTIF_TRACE_DEBUG("BTIF DISABLE BLUETOOTH"); #if (BLE_INCLUDED == TRUE) - btm_ble_multi_adv_cleanup(); - // TODO(jpawlowski): this should do whole BTA_VendorCleanup(), but it would kill - // the stack now. + btm_ble_multi_adv_cleanup(); +// TODO(jpawlowski): this should do whole BTA_VendorCleanup(), but it would kill +// the stack now. #endif - btif_dm_on_disable(); - /* cleanup rfcomm & l2cap api */ - btif_sock_cleanup(); - btif_pan_cleanup(); - BTA_DisableBluetooth(); + btif_dm_on_disable(); + /* cleanup rfcomm & l2cap api */ + btif_sock_cleanup(); + btif_pan_cleanup(); + BTA_DisableBluetooth(); - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_disable_bluetooth_evt -** -** Description Event notifying BT disable is now complete. -** Terminates main stack tasks and notifies HAL -** user with updated BT state. -** -** Returns void -** -*******************************************************************************/ - -void btif_disable_bluetooth_evt(void) -{ - BTIF_TRACE_DEBUG("%s", __func__); + * + * Function btif_disable_bluetooth_evt + * + * Description Event notifying BT disable is now complete. + * Terminates main stack tasks and notifies HAL + * user with updated BT state. + * + * Returns void + * + ******************************************************************************/ + +void btif_disable_bluetooth_evt(void) { + BTIF_TRACE_DEBUG("%s", __func__); #if (HCILP_INCLUDED == TRUE) - bte_main_enable_lpm(false); + bte_main_enable_lpm(false); #endif - bte_main_disable(); + bte_main_disable(); - /* callback to HAL */ - future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS); + /* callback to HAL */ + future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS); } /******************************************************************************* -** -** Function btif_cleanup_bluetooth -** -** Description Cleanup BTIF state. -** -** Returns void -** -*******************************************************************************/ - -bt_status_t btif_cleanup_bluetooth(void) -{ - BTIF_TRACE_DEBUG("%s", __func__); + * + * Function btif_cleanup_bluetooth + * + * Description Cleanup BTIF state. + * + * Returns void + * + ******************************************************************************/ + +bt_status_t btif_cleanup_bluetooth(void) { + BTIF_TRACE_DEBUG("%s", __func__); #if (BLE_INCLUDED == TRUE) - BTA_VendorCleanup(); + BTA_VendorCleanup(); #endif - btif_dm_cleanup(); - btif_jni_disassociate(); - btif_queue_release(); + btif_dm_cleanup(); + btif_jni_disassociate(); + btif_queue_release(); - if (jni_run_loop && message_loop_) { - message_loop_->task_runner()->PostTask(FROM_HERE, jni_run_loop->QuitClosure()); - } + if (jni_run_loop && message_loop_) { + message_loop_->task_runner()->PostTask(FROM_HERE, + jni_run_loop->QuitClosure()); + } - thread_free(bt_jni_workqueue_thread); - bt_jni_workqueue_thread = NULL; + thread_free(bt_jni_workqueue_thread); + bt_jni_workqueue_thread = NULL; - bte_main_cleanup(); + bte_main_cleanup(); - btif_dut_mode = 0; + btif_dut_mode = 0; - BTIF_TRACE_DEBUG("%s done", __func__); + BTIF_TRACE_DEBUG("%s done", __func__); - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_dut_mode_cback -** -** Description Callback invoked on completion of vendor specific test mode command -** -** Returns None -** -*******************************************************************************/ -static void btif_dut_mode_cback( tBTM_VSC_CMPL *p ) -{ - UNUSED(p); - /* For now nothing to be done. */ + * + * Function btif_dut_mode_cback + * + * Description Callback invoked on completion of vendor specific test mode + *command + * + * Returns None + * + ******************************************************************************/ +static void btif_dut_mode_cback(tBTM_VSC_CMPL* p) { + UNUSED(p); + /* For now nothing to be done. */ } /******************************************************************************* -** -** Function btif_dut_mode_configure -** -** Description Configure Test Mode - 'enable' to 1 puts the device in test mode and 0 exits -** test mode -** -** Returns BT_STATUS_SUCCESS on success -** -*******************************************************************************/ -bt_status_t btif_dut_mode_configure(uint8_t enable) -{ - BTIF_TRACE_DEBUG("%s", __func__); - - if (!stack_manager_get_interface()->get_stack_is_running()) { - BTIF_TRACE_ERROR("btif_dut_mode_configure : Bluetooth not enabled"); - return BT_STATUS_NOT_READY; - } + * + * Function btif_dut_mode_configure + * + * Description Configure Test Mode - 'enable' to 1 puts the device in test + *mode and 0 exits + * test mode + * + * Returns BT_STATUS_SUCCESS on success + * + ******************************************************************************/ +bt_status_t btif_dut_mode_configure(uint8_t enable) { + BTIF_TRACE_DEBUG("%s", __func__); - btif_dut_mode = enable; - if (enable == 1) { - BTA_EnableTestMode(); - } else { - BTA_DisableTestMode(); - } - return BT_STATUS_SUCCESS; + if (!stack_manager_get_interface()->get_stack_is_running()) { + BTIF_TRACE_ERROR("btif_dut_mode_configure : Bluetooth not enabled"); + return BT_STATUS_NOT_READY; + } + + btif_dut_mode = enable; + if (enable == 1) { + BTA_EnableTestMode(); + } else { + BTA_DisableTestMode(); + } + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_dut_mode_send -** -** Description Sends a HCI Vendor specific command to the controller -** -** Returns BT_STATUS_SUCCESS on success -** -*******************************************************************************/ -bt_status_t btif_dut_mode_send(uint16_t opcode, uint8_t *buf, uint8_t len) -{ - /* TODO: Check that opcode is a vendor command group */ - BTIF_TRACE_DEBUG("%s", __func__); - if (!btif_is_dut_mode()) { - BTIF_TRACE_ERROR("Bluedroid HAL needs to be init with test_mode set to 1."); - return BT_STATUS_FAIL; - } - BTM_VendorSpecificCommand(opcode, len, buf, btif_dut_mode_cback); - return BT_STATUS_SUCCESS; + * + * Function btif_dut_mode_send + * + * Description Sends a HCI Vendor specific command to the controller + * + * Returns BT_STATUS_SUCCESS on success + * + ******************************************************************************/ +bt_status_t btif_dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) { + /* TODO: Check that opcode is a vendor command group */ + BTIF_TRACE_DEBUG("%s", __func__); + if (!btif_is_dut_mode()) { + BTIF_TRACE_ERROR("Bluedroid HAL needs to be init with test_mode set to 1."); + return BT_STATUS_FAIL; + } + BTM_VendorSpecificCommand(opcode, len, buf, btif_dut_mode_cback); + return BT_STATUS_SUCCESS; } /***************************************************************************** -** -** btif api adapter property functions -** -*****************************************************************************/ - -static bt_status_t btif_in_get_adapter_properties(void) -{ - bt_property_t properties[6]; - uint32_t num_props; - - bt_bdaddr_t addr; - bt_bdname_t name; - bt_scan_mode_t mode; - uint32_t disc_timeout; - bt_bdaddr_t bonded_devices[BTM_SEC_MAX_DEVICE_RECORDS]; - bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS]; - num_props = 0; - - /* BD_ADDR */ - BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDADDR, - sizeof(addr), &addr); - btif_storage_get_adapter_property(&properties[num_props]); - num_props++; - - /* BD_NAME */ - BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDNAME, - sizeof(name), &name); - btif_storage_get_adapter_property(&properties[num_props]); - num_props++; - - /* SCAN_MODE */ - BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_SCAN_MODE, - sizeof(mode), &mode); - btif_storage_get_adapter_property(&properties[num_props]); - num_props++; - - /* DISC_TIMEOUT */ - BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT, - sizeof(disc_timeout), &disc_timeout); - btif_storage_get_adapter_property(&properties[num_props]); - num_props++; - - /* BONDED_DEVICES */ - BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_BONDED_DEVICES, - sizeof(bonded_devices), bonded_devices); - btif_storage_get_adapter_property(&properties[num_props]); - num_props++; - - /* LOCAL UUIDs */ - BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_UUIDS, - sizeof(local_uuids), local_uuids); - btif_storage_get_adapter_property(&properties[num_props]); - num_props++; - - HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, - BT_STATUS_SUCCESS, num_props, properties); + * + * btif api adapter property functions + * + ****************************************************************************/ + +static bt_status_t btif_in_get_adapter_properties(void) { + bt_property_t properties[6]; + uint32_t num_props; + + bt_bdaddr_t addr; + bt_bdname_t name; + bt_scan_mode_t mode; + uint32_t disc_timeout; + bt_bdaddr_t bonded_devices[BTM_SEC_MAX_DEVICE_RECORDS]; + bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS]; + num_props = 0; + + /* BD_ADDR */ + BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDADDR, + sizeof(addr), &addr); + btif_storage_get_adapter_property(&properties[num_props]); + num_props++; + + /* BD_NAME */ + BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDNAME, + sizeof(name), &name); + btif_storage_get_adapter_property(&properties[num_props]); + num_props++; + + /* SCAN_MODE */ + BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], + BT_PROPERTY_ADAPTER_SCAN_MODE, sizeof(mode), + &mode); + btif_storage_get_adapter_property(&properties[num_props]); + num_props++; + + /* DISC_TIMEOUT */ + BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], + BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT, + sizeof(disc_timeout), &disc_timeout); + btif_storage_get_adapter_property(&properties[num_props]); + num_props++; + + /* BONDED_DEVICES */ + BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], + BT_PROPERTY_ADAPTER_BONDED_DEVICES, + sizeof(bonded_devices), bonded_devices); + btif_storage_get_adapter_property(&properties[num_props]); + num_props++; + + /* LOCAL UUIDs */ + BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_UUIDS, + sizeof(local_uuids), local_uuids); + btif_storage_get_adapter_property(&properties[num_props]); + num_props++; + + HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, num_props, + properties); - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } -static bt_status_t btif_in_get_remote_device_properties(bt_bdaddr_t *bd_addr) -{ - bt_property_t remote_properties[8]; - uint32_t num_props = 0; - - bt_bdname_t name, alias; - uint32_t cod, devtype; - bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS]; - - memset(remote_properties, 0, sizeof(remote_properties)); - BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_BDNAME, - sizeof(name), &name); - btif_storage_get_remote_device_property(bd_addr, - &remote_properties[num_props]); - num_props++; - - BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_REMOTE_FRIENDLY_NAME, - sizeof(alias), &alias); - btif_storage_get_remote_device_property(bd_addr, - &remote_properties[num_props]); - num_props++; - - BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_CLASS_OF_DEVICE, - sizeof(cod), &cod); - btif_storage_get_remote_device_property(bd_addr, - &remote_properties[num_props]); - num_props++; - - BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_TYPE_OF_DEVICE, - sizeof(devtype), &devtype); - btif_storage_get_remote_device_property(bd_addr, - &remote_properties[num_props]); - num_props++; - - BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_UUIDS, - sizeof(remote_uuids), remote_uuids); - btif_storage_get_remote_device_property(bd_addr, - &remote_properties[num_props]); - num_props++; - - HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, - BT_STATUS_SUCCESS, bd_addr, num_props, remote_properties); +static bt_status_t btif_in_get_remote_device_properties(bt_bdaddr_t* bd_addr) { + bt_property_t remote_properties[8]; + uint32_t num_props = 0; + + bt_bdname_t name, alias; + uint32_t cod, devtype; + bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS]; + + memset(remote_properties, 0, sizeof(remote_properties)); + BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_BDNAME, + sizeof(name), &name); + btif_storage_get_remote_device_property(bd_addr, + &remote_properties[num_props]); + num_props++; + + BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], + BT_PROPERTY_REMOTE_FRIENDLY_NAME, sizeof(alias), + &alias); + btif_storage_get_remote_device_property(bd_addr, + &remote_properties[num_props]); + num_props++; + + BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], + BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod); + btif_storage_get_remote_device_property(bd_addr, + &remote_properties[num_props]); + num_props++; + + BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], + BT_PROPERTY_TYPE_OF_DEVICE, sizeof(devtype), + &devtype); + btif_storage_get_remote_device_property(bd_addr, + &remote_properties[num_props]); + num_props++; + + BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_UUIDS, + sizeof(remote_uuids), remote_uuids); + btif_storage_get_remote_device_property(bd_addr, + &remote_properties[num_props]); + num_props++; + + HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, BT_STATUS_SUCCESS, + bd_addr, num_props, remote_properties); - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } - /******************************************************************************* -** -** Function execute_storage_request -** -** Description Executes adapter storage request in BTIF context -** -** Returns bt_status_t -** -*******************************************************************************/ - -static void execute_storage_request(uint16_t event, char *p_param) -{ - bt_status_t status = BT_STATUS_SUCCESS; - - BTIF_TRACE_EVENT("execute storage request event : %d", event); - - switch(event) - { - case BTIF_CORE_STORAGE_ADAPTER_WRITE: - { - btif_storage_req_t *p_req = (btif_storage_req_t*)p_param; - bt_property_t *p_prop = &(p_req->write_req.prop); - BTIF_TRACE_EVENT("type: %d, len %d, 0x%x", p_prop->type, - p_prop->len, p_prop->val); - - status = btif_storage_set_adapter_property(p_prop); - HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, p_prop); - } break; - - case BTIF_CORE_STORAGE_ADAPTER_READ: - { - btif_storage_req_t *p_req = (btif_storage_req_t*)p_param; - char buf[512]; - bt_property_t prop; - prop.type = p_req->read_req.type; - prop.val = (void*)buf; - prop.len = sizeof(buf); - if (prop.type == BT_PROPERTY_LOCAL_LE_FEATURES) - { - #if (BLE_INCLUDED == true) - tBTM_BLE_VSC_CB cmn_vsc_cb; - bt_local_le_features_t local_le_features; - - /* LE features are not stored in storage. Should be retrived from stack */ - BTM_BleGetVendorCapabilities(&cmn_vsc_cb); - local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled(); - - prop.len = sizeof (bt_local_le_features_t); - if (cmn_vsc_cb.filter_support == 1) - local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter; - else - local_le_features.max_adv_filter_supported = 0; - local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max; - local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz; - local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading; - local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg; - local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support; - local_le_features.version_supported = cmn_vsc_cb.version_supported; - local_le_features.total_trackable_advertisers = - cmn_vsc_cb.total_trackable_advertisers; - - local_le_features.extended_scan_support = cmn_vsc_cb.extended_scan_support > 0; - local_le_features.debug_logging_supported = cmn_vsc_cb.debug_logging_supported > 0; - memcpy(prop.val, &local_le_features, prop.len); - #endif - } - else - { - status = btif_storage_get_adapter_property(&prop); - } - HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, &prop); - } break; - - case BTIF_CORE_STORAGE_ADAPTER_READ_ALL: - { - status = btif_in_get_adapter_properties(); - } break; - - case BTIF_CORE_STORAGE_NOTIFY_STATUS: - { - HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 0, NULL); - } break; + * + * Function execute_storage_request + * + * Description Executes adapter storage request in BTIF context + * + * Returns bt_status_t + * + ******************************************************************************/ - default: - BTIF_TRACE_ERROR("%s invalid event id (%d)", __func__, event); - break; - } +static void execute_storage_request(uint16_t event, char* p_param) { + bt_status_t status = BT_STATUS_SUCCESS; + + BTIF_TRACE_EVENT("execute storage request event : %d", event); + + switch (event) { + case BTIF_CORE_STORAGE_ADAPTER_WRITE: { + btif_storage_req_t* p_req = (btif_storage_req_t*)p_param; + bt_property_t* p_prop = &(p_req->write_req.prop); + BTIF_TRACE_EVENT("type: %d, len %d, 0x%x", p_prop->type, p_prop->len, + p_prop->val); + + status = btif_storage_set_adapter_property(p_prop); + HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, p_prop); + } break; + + case BTIF_CORE_STORAGE_ADAPTER_READ: { + btif_storage_req_t* p_req = (btif_storage_req_t*)p_param; + char buf[512]; + bt_property_t prop; + prop.type = p_req->read_req.type; + prop.val = (void*)buf; + prop.len = sizeof(buf); + if (prop.type == BT_PROPERTY_LOCAL_LE_FEATURES) { +#if (BLE_INCLUDED == true) + tBTM_BLE_VSC_CB cmn_vsc_cb; + bt_local_le_features_t local_le_features; + + /* LE features are not stored in storage. Should be retrived from stack + */ + BTM_BleGetVendorCapabilities(&cmn_vsc_cb); + local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled(); + + prop.len = sizeof(bt_local_le_features_t); + if (cmn_vsc_cb.filter_support == 1) + local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter; + else + local_le_features.max_adv_filter_supported = 0; + local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max; + local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz; + local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading; + local_le_features.scan_result_storage_size = + cmn_vsc_cb.tot_scan_results_strg; + local_le_features.activity_energy_info_supported = + cmn_vsc_cb.energy_support; + local_le_features.version_supported = cmn_vsc_cb.version_supported; + local_le_features.total_trackable_advertisers = + cmn_vsc_cb.total_trackable_advertisers; + + local_le_features.extended_scan_support = + cmn_vsc_cb.extended_scan_support > 0; + local_le_features.debug_logging_supported = + cmn_vsc_cb.debug_logging_supported > 0; + memcpy(prop.val, &local_le_features, prop.len); +#endif + } else { + status = btif_storage_get_adapter_property(&prop); + } + HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, &prop); + } break; + + case BTIF_CORE_STORAGE_ADAPTER_READ_ALL: { + status = btif_in_get_adapter_properties(); + } break; + + case BTIF_CORE_STORAGE_NOTIFY_STATUS: { + HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 0, NULL); + } break; + + default: + BTIF_TRACE_ERROR("%s invalid event id (%d)", __func__, event); + break; + } } -static void execute_storage_remote_request(uint16_t event, char *p_param) -{ - bt_status_t status = BT_STATUS_FAIL; - bt_property_t prop; +static void execute_storage_remote_request(uint16_t event, char* p_param) { + bt_status_t status = BT_STATUS_FAIL; + bt_property_t prop; - BTIF_TRACE_EVENT("execute storage remote request event : %d", event); - - switch (event) - { - case BTIF_CORE_STORAGE_REMOTE_READ: - { - char buf[1024]; - btif_storage_req_t *p_req = (btif_storage_req_t*)p_param; - prop.type = p_req->read_req.type; - prop.val = (void*) buf; - prop.len = sizeof(buf); - - status = btif_storage_get_remote_device_property(&(p_req->read_req.bd_addr), - &prop); - HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, - status, &(p_req->read_req.bd_addr), 1, &prop); - }break; - case BTIF_CORE_STORAGE_REMOTE_WRITE: - { - btif_storage_req_t *p_req = (btif_storage_req_t*)p_param; - status = btif_storage_set_remote_device_property(&(p_req->write_req.bd_addr), - &(p_req->write_req.prop)); - }break; - case BTIF_CORE_STORAGE_REMOTE_READ_ALL: - { - btif_storage_req_t *p_req = (btif_storage_req_t*)p_param; - btif_in_get_remote_device_properties(&p_req->read_req.bd_addr); - }break; - } + BTIF_TRACE_EVENT("execute storage remote request event : %d", event); + + switch (event) { + case BTIF_CORE_STORAGE_REMOTE_READ: { + char buf[1024]; + btif_storage_req_t* p_req = (btif_storage_req_t*)p_param; + prop.type = p_req->read_req.type; + prop.val = (void*)buf; + prop.len = sizeof(buf); + + status = btif_storage_get_remote_device_property( + &(p_req->read_req.bd_addr), &prop); + HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, status, + &(p_req->read_req.bd_addr), 1, &prop); + } break; + case BTIF_CORE_STORAGE_REMOTE_WRITE: { + btif_storage_req_t* p_req = (btif_storage_req_t*)p_param; + status = btif_storage_set_remote_device_property( + &(p_req->write_req.bd_addr), &(p_req->write_req.prop)); + } break; + case BTIF_CORE_STORAGE_REMOTE_READ_ALL: { + btif_storage_req_t* p_req = (btif_storage_req_t*)p_param; + btif_in_get_remote_device_properties(&p_req->read_req.bd_addr); + } break; + } } void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props, - bt_property_t *p_props) -{ - HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, - status, num_props, p_props); - + bt_property_t* p_props) { + HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, num_props, p_props); } -void btif_remote_properties_evt(bt_status_t status, bt_bdaddr_t *remote_addr, - uint32_t num_props, bt_property_t *p_props) -{ - HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, - status, remote_addr, num_props, p_props); +void btif_remote_properties_evt(bt_status_t status, bt_bdaddr_t* remote_addr, + uint32_t num_props, bt_property_t* p_props) { + HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, status, remote_addr, + num_props, p_props); } /******************************************************************************* -** -** Function btif_in_storage_request_copy_cb -** -** Description Switch context callback function to perform the deep copy for -** both the adapter and remote_device property API -** -** Returns None -** -*******************************************************************************/ -static void btif_in_storage_request_copy_cb(uint16_t event, - char *p_new_buf, char *p_old_buf) -{ - btif_storage_req_t *new_req = (btif_storage_req_t*)p_new_buf; - btif_storage_req_t *old_req = (btif_storage_req_t*)p_old_buf; - - BTIF_TRACE_EVENT("%s", __func__); - switch (event) - { - case BTIF_CORE_STORAGE_REMOTE_WRITE: - case BTIF_CORE_STORAGE_ADAPTER_WRITE: - { - bdcpy(new_req->write_req.bd_addr.address, old_req->write_req.bd_addr.address); - /* Copy the member variables one at a time */ - new_req->write_req.prop.type = old_req->write_req.prop.type; - new_req->write_req.prop.len = old_req->write_req.prop.len; - - new_req->write_req.prop.val = (uint8_t *)(p_new_buf + sizeof(btif_storage_req_t)); - memcpy(new_req->write_req.prop.val, old_req->write_req.prop.val, - old_req->write_req.prop.len); - }break; - } + * + * Function btif_in_storage_request_copy_cb + * + * Description Switch context callback function to perform the deep copy for + * both the adapter and remote_device property API + * + * Returns None + * + ******************************************************************************/ +static void btif_in_storage_request_copy_cb(uint16_t event, char* p_new_buf, + char* p_old_buf) { + btif_storage_req_t* new_req = (btif_storage_req_t*)p_new_buf; + btif_storage_req_t* old_req = (btif_storage_req_t*)p_old_buf; + + BTIF_TRACE_EVENT("%s", __func__); + switch (event) { + case BTIF_CORE_STORAGE_REMOTE_WRITE: + case BTIF_CORE_STORAGE_ADAPTER_WRITE: { + bdcpy(new_req->write_req.bd_addr.address, + old_req->write_req.bd_addr.address); + /* Copy the member variables one at a time */ + new_req->write_req.prop.type = old_req->write_req.prop.type; + new_req->write_req.prop.len = old_req->write_req.prop.len; + + new_req->write_req.prop.val = + (uint8_t*)(p_new_buf + sizeof(btif_storage_req_t)); + memcpy(new_req->write_req.prop.val, old_req->write_req.prop.val, + old_req->write_req.prop.len); + } break; + } } /******************************************************************************* -** -** Function btif_get_adapter_properties -** -** Description Fetch all available properties (local & remote) -** -** Returns bt_status_t -** -*******************************************************************************/ - -bt_status_t btif_get_adapter_properties(void) -{ - BTIF_TRACE_EVENT("%s", __func__); - - if (!btif_is_enabled()) - return BT_STATUS_NOT_READY; - - return btif_transfer_context(execute_storage_request, - BTIF_CORE_STORAGE_ADAPTER_READ_ALL, - NULL, 0, NULL); + * + * Function btif_get_adapter_properties + * + * Description Fetch all available properties (local & remote) + * + * Returns bt_status_t + * + ******************************************************************************/ + +bt_status_t btif_get_adapter_properties(void) { + BTIF_TRACE_EVENT("%s", __func__); + + if (!btif_is_enabled()) return BT_STATUS_NOT_READY; + + return btif_transfer_context(execute_storage_request, + BTIF_CORE_STORAGE_ADAPTER_READ_ALL, NULL, 0, + NULL); } /******************************************************************************* -** -** Function btif_get_adapter_property -** -** Description Fetches property value from local cache -** -** Returns bt_status_t -** -*******************************************************************************/ - -bt_status_t btif_get_adapter_property(bt_property_type_t type) -{ - btif_storage_req_t req; - - BTIF_TRACE_EVENT("%s %d", __func__, type); - - /* Allow get_adapter_property only for BDADDR and BDNAME if BT is disabled */ - if (!btif_is_enabled() && (type != BT_PROPERTY_BDADDR) && (type != BT_PROPERTY_BDNAME)) - return BT_STATUS_NOT_READY; - - memset(&(req.read_req.bd_addr), 0, sizeof(bt_bdaddr_t)); - req.read_req.type = type; - - return btif_transfer_context(execute_storage_request, - BTIF_CORE_STORAGE_ADAPTER_READ, - (char*)&req, sizeof(btif_storage_req_t), NULL); + * + * Function btif_get_adapter_property + * + * Description Fetches property value from local cache + * + * Returns bt_status_t + * + ******************************************************************************/ + +bt_status_t btif_get_adapter_property(bt_property_type_t type) { + btif_storage_req_t req; + + BTIF_TRACE_EVENT("%s %d", __func__, type); + + /* Allow get_adapter_property only for BDADDR and BDNAME if BT is disabled */ + if (!btif_is_enabled() && (type != BT_PROPERTY_BDADDR) && + (type != BT_PROPERTY_BDNAME)) + return BT_STATUS_NOT_READY; + + memset(&(req.read_req.bd_addr), 0, sizeof(bt_bdaddr_t)); + req.read_req.type = type; + + return btif_transfer_context(execute_storage_request, + BTIF_CORE_STORAGE_ADAPTER_READ, (char*)&req, + sizeof(btif_storage_req_t), NULL); } /******************************************************************************* -** -** Function btif_set_adapter_property -** -** Description Updates core stack with property value and stores it in -** local cache -** -** Returns bt_status_t -** -*******************************************************************************/ - -bt_status_t btif_set_adapter_property(const bt_property_t *property) -{ - btif_storage_req_t req; - bt_status_t status = BT_STATUS_SUCCESS; - int storage_req_id = BTIF_CORE_STORAGE_NOTIFY_STATUS; /* default */ - char bd_name[BTM_MAX_LOC_BD_NAME_LEN +1]; - uint16_t name_len = 0; - - BTIF_TRACE_EVENT("btif_set_adapter_property type: %d, len %d, 0x%x", - property->type, property->len, property->val); - - if (!btif_is_enabled()) - return BT_STATUS_NOT_READY; - - switch(property->type) - { - case BT_PROPERTY_BDNAME: - { - name_len = property->len > BTM_MAX_LOC_BD_NAME_LEN ? BTM_MAX_LOC_BD_NAME_LEN: - property->len; - memcpy(bd_name,property->val, name_len); - bd_name[name_len] = '\0'; - - BTIF_TRACE_EVENT("set property name : %s", (char *)bd_name); - - BTA_DmSetDeviceName((char *)bd_name); - - storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE; - } - break; - - case BT_PROPERTY_ADAPTER_SCAN_MODE: - { - bt_scan_mode_t mode = *(bt_scan_mode_t*)property->val; - tBTA_DM_DISC disc_mode; - tBTA_DM_CONN conn_mode; - - switch(mode) - { - case BT_SCAN_MODE_NONE: - disc_mode = BTA_DM_NON_DISC; - conn_mode = BTA_DM_NON_CONN; - break; - - case BT_SCAN_MODE_CONNECTABLE: - disc_mode = BTA_DM_NON_DISC; - conn_mode = BTA_DM_CONN; - break; - - case BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE: - disc_mode = BTA_DM_GENERAL_DISC; - conn_mode = BTA_DM_CONN; - break; - - default: - BTIF_TRACE_ERROR("invalid scan mode (0x%x)", mode); - return BT_STATUS_PARM_INVALID; - } - - BTIF_TRACE_EVENT("set property scan mode : %x", mode); - - BTA_DmSetVisibility(disc_mode, conn_mode, BTA_DM_IGNORE, BTA_DM_IGNORE); - - storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE; - } - break; - case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: - { - /* Nothing to do beside store the value in NV. Java - will change the SCAN_MODE property after setting timeout, - if required */ - storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE; - } - break; - case BT_PROPERTY_BDADDR: - case BT_PROPERTY_UUIDS: - case BT_PROPERTY_ADAPTER_BONDED_DEVICES: - case BT_PROPERTY_REMOTE_FRIENDLY_NAME: - /* no write support through HAL, these properties are only populated from BTA events */ - status = BT_STATUS_FAIL; - break; - default: - BTIF_TRACE_ERROR("btif_get_adapter_property : invalid type %d", - property->type); - status = BT_STATUS_FAIL; - break; - } + * + * Function btif_set_adapter_property + * + * Description Updates core stack with property value and stores it in + * local cache + * + * Returns bt_status_t + * + ******************************************************************************/ - if (storage_req_id != BTIF_CORE_STORAGE_NO_ACTION) - { - /* pass on to storage for updating local database */ +bt_status_t btif_set_adapter_property(const bt_property_t* property) { + btif_storage_req_t req; + bt_status_t status = BT_STATUS_SUCCESS; + int storage_req_id = BTIF_CORE_STORAGE_NOTIFY_STATUS; /* default */ + char bd_name[BTM_MAX_LOC_BD_NAME_LEN + 1]; + uint16_t name_len = 0; - memset(&(req.write_req.bd_addr), 0, sizeof(bt_bdaddr_t)); - memcpy(&(req.write_req.prop), property, sizeof(bt_property_t)); + BTIF_TRACE_EVENT("btif_set_adapter_property type: %d, len %d, 0x%x", + property->type, property->len, property->val); - return btif_transfer_context(execute_storage_request, - storage_req_id, - (char*)&req, - sizeof(btif_storage_req_t)+property->len, - btif_in_storage_request_copy_cb); - } + if (!btif_is_enabled()) return BT_STATUS_NOT_READY; + + switch (property->type) { + case BT_PROPERTY_BDNAME: { + name_len = property->len > BTM_MAX_LOC_BD_NAME_LEN + ? BTM_MAX_LOC_BD_NAME_LEN + : property->len; + memcpy(bd_name, property->val, name_len); + bd_name[name_len] = '\0'; + + BTIF_TRACE_EVENT("set property name : %s", (char*)bd_name); + + BTA_DmSetDeviceName((char*)bd_name); + + storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE; + } break; + + case BT_PROPERTY_ADAPTER_SCAN_MODE: { + bt_scan_mode_t mode = *(bt_scan_mode_t*)property->val; + tBTA_DM_DISC disc_mode; + tBTA_DM_CONN conn_mode; + + switch (mode) { + case BT_SCAN_MODE_NONE: + disc_mode = BTA_DM_NON_DISC; + conn_mode = BTA_DM_NON_CONN; + break; + + case BT_SCAN_MODE_CONNECTABLE: + disc_mode = BTA_DM_NON_DISC; + conn_mode = BTA_DM_CONN; + break; + + case BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE: + disc_mode = BTA_DM_GENERAL_DISC; + conn_mode = BTA_DM_CONN; + break; - return status; + default: + BTIF_TRACE_ERROR("invalid scan mode (0x%x)", mode); + return BT_STATUS_PARM_INVALID; + } + + BTIF_TRACE_EVENT("set property scan mode : %x", mode); + + BTA_DmSetVisibility(disc_mode, conn_mode, BTA_DM_IGNORE, BTA_DM_IGNORE); + + storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE; + } break; + case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: { + /* Nothing to do beside store the value in NV. Java + will change the SCAN_MODE property after setting timeout, + if required */ + storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE; + } break; + case BT_PROPERTY_BDADDR: + case BT_PROPERTY_UUIDS: + case BT_PROPERTY_ADAPTER_BONDED_DEVICES: + case BT_PROPERTY_REMOTE_FRIENDLY_NAME: + /* no write support through HAL, these properties are only populated from + * BTA events */ + status = BT_STATUS_FAIL; + break; + default: + BTIF_TRACE_ERROR("btif_get_adapter_property : invalid type %d", + property->type); + status = BT_STATUS_FAIL; + break; + } + if (storage_req_id != BTIF_CORE_STORAGE_NO_ACTION) { + /* pass on to storage for updating local database */ + + memset(&(req.write_req.bd_addr), 0, sizeof(bt_bdaddr_t)); + memcpy(&(req.write_req.prop), property, sizeof(bt_property_t)); + + return btif_transfer_context(execute_storage_request, storage_req_id, + (char*)&req, + sizeof(btif_storage_req_t) + property->len, + btif_in_storage_request_copy_cb); + } + + return status; } /******************************************************************************* -** -** Function btif_get_remote_device_property -** -** Description Fetches the remote device property from the NVRAM -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_get_remote_device_property(bt_bdaddr_t *remote_addr, - bt_property_type_t type) -{ - btif_storage_req_t req; - - if (!btif_is_enabled()) - return BT_STATUS_NOT_READY; - - memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t)); - req.read_req.type = type; - return btif_transfer_context(execute_storage_remote_request, - BTIF_CORE_STORAGE_REMOTE_READ, - (char*)&req, sizeof(btif_storage_req_t), - NULL); + * + * Function btif_get_remote_device_property + * + * Description Fetches the remote device property from the NVRAM + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_get_remote_device_property(bt_bdaddr_t* remote_addr, + bt_property_type_t type) { + btif_storage_req_t req; + + if (!btif_is_enabled()) return BT_STATUS_NOT_READY; + + memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t)); + req.read_req.type = type; + return btif_transfer_context(execute_storage_remote_request, + BTIF_CORE_STORAGE_REMOTE_READ, (char*)&req, + sizeof(btif_storage_req_t), NULL); } /******************************************************************************* -** -** Function btif_get_remote_device_properties -** -** Description Fetches all the remote device properties from NVRAM -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_get_remote_device_properties(bt_bdaddr_t *remote_addr) -{ - btif_storage_req_t req; - - if (!btif_is_enabled()) - return BT_STATUS_NOT_READY; - - memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t)); - return btif_transfer_context(execute_storage_remote_request, - BTIF_CORE_STORAGE_REMOTE_READ_ALL, - (char*)&req, sizeof(btif_storage_req_t), - NULL); + * + * Function btif_get_remote_device_properties + * + * Description Fetches all the remote device properties from NVRAM + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_get_remote_device_properties(bt_bdaddr_t* remote_addr) { + btif_storage_req_t req; + + if (!btif_is_enabled()) return BT_STATUS_NOT_READY; + + memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t)); + return btif_transfer_context(execute_storage_remote_request, + BTIF_CORE_STORAGE_REMOTE_READ_ALL, (char*)&req, + sizeof(btif_storage_req_t), NULL); } /******************************************************************************* -** -** Function btif_set_remote_device_property -** -** Description Writes the remote device property to NVRAM. -** Currently, BT_PROPERTY_REMOTE_FRIENDLY_NAME is the only -** remote device property that can be set -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_set_remote_device_property(bt_bdaddr_t *remote_addr, - const bt_property_t *property) -{ - btif_storage_req_t req; - - if (!btif_is_enabled()) - return BT_STATUS_NOT_READY; - - memcpy(&(req.write_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t)); - memcpy(&(req.write_req.prop), property, sizeof(bt_property_t)); + * + * Function btif_set_remote_device_property + * + * Description Writes the remote device property to NVRAM. + * Currently, BT_PROPERTY_REMOTE_FRIENDLY_NAME is the only + * remote device property that can be set + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_set_remote_device_property(bt_bdaddr_t* remote_addr, + const bt_property_t* property) { + btif_storage_req_t req; - return btif_transfer_context(execute_storage_remote_request, - BTIF_CORE_STORAGE_REMOTE_WRITE, - (char*)&req, - sizeof(btif_storage_req_t)+property->len, - btif_in_storage_request_copy_cb); -} + if (!btif_is_enabled()) return BT_STATUS_NOT_READY; + memcpy(&(req.write_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t)); + memcpy(&(req.write_req.prop), property, sizeof(bt_property_t)); -/******************************************************************************* -** -** Function btif_get_remote_service_record -** -** Description Looks up the service matching uuid on the remote device -** and fetches the SCN and service_name if the UUID is found -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_get_remote_service_record(bt_bdaddr_t *remote_addr, - bt_uuid_t *uuid) -{ - if (!btif_is_enabled()) - return BT_STATUS_NOT_READY; - - return btif_dm_get_remote_service_record(remote_addr, uuid); + return btif_transfer_context(execute_storage_remote_request, + BTIF_CORE_STORAGE_REMOTE_WRITE, (char*)&req, + sizeof(btif_storage_req_t) + property->len, + btif_in_storage_request_copy_cb); } +/******************************************************************************* + * + * Function btif_get_remote_service_record + * + * Description Looks up the service matching uuid on the remote device + * and fetches the SCN and service_name if the UUID is found + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_get_remote_service_record(bt_bdaddr_t* remote_addr, + bt_uuid_t* uuid) { + if (!btif_is_enabled()) return BT_STATUS_NOT_READY; + + return btif_dm_get_remote_service_record(remote_addr, uuid); +} /******************************************************************************* -** -** Function btif_get_enabled_services_mask -** -** Description Fetches currently enabled services -** -** Returns tBTA_SERVICE_MASK -** -*******************************************************************************/ - -tBTA_SERVICE_MASK btif_get_enabled_services_mask(void) -{ - return btif_enabled_services; + * + * Function btif_get_enabled_services_mask + * + * Description Fetches currently enabled services + * + * Returns tBTA_SERVICE_MASK + * + ******************************************************************************/ + +tBTA_SERVICE_MASK btif_get_enabled_services_mask(void) { + return btif_enabled_services; } /******************************************************************************* -** -** Function btif_enable_service -** -** Description Enables the service 'service_ID' to the service_mask. -** Upon BT enable, BTIF core shall invoke the BTA APIs to -** enable the profiles -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id) -{ - tBTA_SERVICE_ID *p_id = &service_id; - - /* If BT is enabled, we need to switch to BTIF context and trigger the - * enable for that profile - * - * Otherwise, we just set the flag. On BT_Enable, the DM will trigger - * enable for the profiles that have been enabled */ - - btif_enabled_services |= (1 << service_id); - - BTIF_TRACE_DEBUG("%s: current services:0x%x", __func__, btif_enabled_services); - - if (btif_is_enabled()) - { - btif_transfer_context(btif_dm_execute_service_request, - BTIF_DM_ENABLE_SERVICE, - (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL); - } + * + * Function btif_enable_service + * + * Description Enables the service 'service_ID' to the service_mask. + * Upon BT enable, BTIF core shall invoke the BTA APIs to + * enable the profiles + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id) { + tBTA_SERVICE_ID* p_id = &service_id; - return BT_STATUS_SUCCESS; + /* If BT is enabled, we need to switch to BTIF context and trigger the + * enable for that profile + * + * Otherwise, we just set the flag. On BT_Enable, the DM will trigger + * enable for the profiles that have been enabled */ + + btif_enabled_services |= (1 << service_id); + + BTIF_TRACE_DEBUG("%s: current services:0x%x", __func__, + btif_enabled_services); + + if (btif_is_enabled()) { + btif_transfer_context(btif_dm_execute_service_request, + BTIF_DM_ENABLE_SERVICE, (char*)p_id, + sizeof(tBTA_SERVICE_ID), NULL); + } + + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_disable_service -** -** Description Disables the service 'service_ID' to the service_mask. -** Upon BT disable, BTIF core shall invoke the BTA APIs to -** disable the profiles -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id) -{ - tBTA_SERVICE_ID *p_id = &service_id; - - /* If BT is enabled, we need to switch to BTIF context and trigger the - * disable for that profile so that the appropriate uuid_property_changed will - * be triggerred. Otherwise, we just need to clear the service_id in the mask - */ - - btif_enabled_services &= (tBTA_SERVICE_MASK)(~(1< #include +#include #include "btif/include/btif_debug.h" #include "btif/include/btif_debug_btsnoop.h" diff --git a/btif/src/btif_debug_btsnoop.cc b/btif/src/btif_debug_btsnoop.cc index 7f1519237..6d636e3c8 100644 --- a/btif/src/btif_debug_btsnoop.cc +++ b/btif/src/btif_debug_btsnoop.cc @@ -39,22 +39,25 @@ static const size_t BLOCK_SIZE = 16384; // Maximum line length in bugreport (should be multiple of 4 for base64 output) static const uint8_t MAX_LINE_LENGTH = 128; -static ringbuffer_t *buffer = NULL; +static ringbuffer_t* buffer = NULL; static uint64_t last_timestamp_ms = 0; -static size_t btsnoop_calculate_packet_length(uint16_t type, const uint8_t *data, size_t length); +static size_t btsnoop_calculate_packet_length(uint16_t type, + const uint8_t* data, + size_t length); -static void btsnoop_cb(const uint16_t type, const uint8_t *data, const size_t length) { +static void btsnoop_cb(const uint16_t type, const uint8_t* data, + const size_t length) { btsnooz_header_t header; size_t included_length = btsnoop_calculate_packet_length(type, data, length); - if (included_length == 0) - return; + if (included_length == 0) return; // Make room in the ring buffer - while (ringbuffer_available(buffer) < (included_length + sizeof(btsnooz_header_t))) { - ringbuffer_pop(buffer, (uint8_t *)&header, sizeof(btsnooz_header_t)); + while (ringbuffer_available(buffer) < + (included_length + sizeof(btsnooz_header_t))) { + ringbuffer_pop(buffer, (uint8_t*)&header, sizeof(btsnooz_header_t)); ringbuffer_delete(buffer, header.length - 1); } @@ -64,15 +67,17 @@ static void btsnoop_cb(const uint16_t type, const uint8_t *data, const size_t le header.type = REDUCE_HCI_TYPE_TO_SIGNIFICANT_BITS(type); header.length = included_length + 1; // +1 for type byte - header.packet_length = length + 1; // +1 for type byte. + header.packet_length = length + 1; // +1 for type byte. header.delta_time_ms = last_timestamp_ms ? now - last_timestamp_ms : 0; last_timestamp_ms = now; - ringbuffer_insert(buffer, (uint8_t *)&header, sizeof(btsnooz_header_t)); + ringbuffer_insert(buffer, (uint8_t*)&header, sizeof(btsnooz_header_t)); ringbuffer_insert(buffer, data, included_length); } -static size_t btsnoop_calculate_packet_length(uint16_t type, const uint8_t *data, size_t length) { +static size_t btsnoop_calculate_packet_length(uint16_t type, + const uint8_t* data, + size_t length) { static const size_t HCI_ACL_HEADER_SIZE = 4; static const size_t L2CAP_HEADER_SIZE = 4; static const size_t L2CAP_CID_OFFSET = (HCI_ACL_HEADER_SIZE + 2); @@ -93,15 +98,16 @@ static size_t btsnoop_calculate_packet_length(uint16_t type, const uint8_t *data return length; case BT_EVT_TO_LM_HCI_ACL: - case BT_EVT_TO_BTU_HCI_ACL: - { + case BT_EVT_TO_BTU_HCI_ACL: { size_t len_hci_acl = HCI_ACL_HEADER_SIZE + L2CAP_HEADER_SIZE; // Check if we have enough data for an L2CAP header if (length > len_hci_acl) { - uint16_t l2cap_cid = data[L2CAP_CID_OFFSET] | (data[L2CAP_CID_OFFSET + 1] << 8); + uint16_t l2cap_cid = + data[L2CAP_CID_OFFSET] | (data[L2CAP_CID_OFFSET + 1] << 8); if (l2cap_cid == L2CAP_SIGNALING_CID) { // For the signaling CID, take the full packet. - // That way, the PSM setup is captured, allowing decoding of PSMs down the road. + // That way, the PSM setup is captured, allowing decoding of PSMs down + // the road. return length; } else { // Otherwise, return as much as we reasonably can @@ -113,14 +119,15 @@ static size_t btsnoop_calculate_packet_length(uint16_t type, const uint8_t *data case BT_EVT_TO_LM_HCI_SCO: case BT_EVT_TO_BTU_HCI_SCO: - // We're not logging SCO packets at this time since they are not currently used. - // FALLTHROUGH + // We're not logging SCO packets at this time since they are not currently + // used. + // FALLTHROUGH default: return 0; } } -static bool btsnoop_compress(ringbuffer_t *rb_dst, ringbuffer_t *rb_src) { +static bool btsnoop_compress(ringbuffer_t* rb_dst, ringbuffer_t* rb_src) { assert(rb_dst != NULL); assert(rb_src != NULL); @@ -129,16 +136,17 @@ static bool btsnoop_compress(ringbuffer_t *rb_dst, ringbuffer_t *rb_src) { zs.zfree = Z_NULL; zs.opaque = Z_NULL; - if (deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK) - return false; + if (deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK) return false; bool rc = true; uint8_t block_src[BLOCK_SIZE]; uint8_t block_dst[BLOCK_SIZE]; - const size_t num_blocks = (ringbuffer_size(rb_src) + BLOCK_SIZE - 1) / BLOCK_SIZE; + const size_t num_blocks = + (ringbuffer_size(rb_src) + BLOCK_SIZE - 1) / BLOCK_SIZE; for (size_t i = 0; i < num_blocks; ++i) { - zs.avail_in = ringbuffer_peek(rb_src, i * BLOCK_SIZE, block_src, BLOCK_SIZE); + zs.avail_in = + ringbuffer_peek(rb_src, i * BLOCK_SIZE, block_src, BLOCK_SIZE); zs.next_in = block_src; do { @@ -161,15 +169,15 @@ static bool btsnoop_compress(ringbuffer_t *rb_dst, ringbuffer_t *rb_src) { } void btif_debug_btsnoop_init(void) { - if (buffer == NULL) - buffer = ringbuffer_init(BTSNOOP_MEM_BUFFER_SIZE); + if (buffer == NULL) buffer = ringbuffer_init(BTSNOOP_MEM_BUFFER_SIZE); btsnoop_mem_set_callback(btsnoop_cb); } void btif_debug_btsnoop_dump(int fd) { - dprintf(fd, "--- BEGIN:BTSNOOP_LOG_SUMMARY (%zu bytes in) ---\n", ringbuffer_size(buffer)); + dprintf(fd, "--- BEGIN:BTSNOOP_LOG_SUMMARY (%zu bytes in) ---\n", + ringbuffer_size(buffer)); - ringbuffer_t *ringbuffer = ringbuffer_init(BTSNOOP_MEM_BUFFER_SIZE); + ringbuffer_t* ringbuffer = ringbuffer_init(BTSNOOP_MEM_BUFFER_SIZE); if (ringbuffer == NULL) { dprintf(fd, "%s Unable to allocate memory for compression", __func__); return; @@ -180,7 +188,8 @@ void btif_debug_btsnoop_dump(int fd) { btsnooz_preamble_t preamble; preamble.version = BTSNOOZ_CURRENT_VERSION; preamble.last_timestamp_ms = last_timestamp_ms; - ringbuffer_insert(ringbuffer, (uint8_t *)&preamble, sizeof(btsnooz_preamble_t)); + ringbuffer_insert(ringbuffer, (uint8_t*)&preamble, + sizeof(btsnooz_preamble_t)); // Compress data diff --git a/btif/src/btif_debug_conn.cc b/btif/src/btif_debug_conn.cc index 6f2ad3afa..1b18ae420 100644 --- a/btif/src/btif_debug_conn.cc +++ b/btif/src/btif_debug_conn.cc @@ -24,8 +24,8 @@ #include "btif/include/btif_debug.h" #include "btif/include/btif_debug_conn.h" -#define NUM_CONNECTION_EVENTS 16 -#define TEMP_BUFFER_SIZE 30 +#define NUM_CONNECTION_EVENTS 16 +#define TEMP_BUFFER_SIZE 30 typedef struct conn_event_t { uint64_t ts; @@ -37,10 +37,10 @@ typedef struct conn_event_t { static conn_event_t connection_events[NUM_CONNECTION_EVENTS]; static uint8_t current_event = 0; -static char *format_ts(const uint64_t ts, char *buffer, int len) { +static char* format_ts(const uint64_t ts, char* buffer, int len) { const uint64_t ms = ts / 1000; const time_t secs = ms / 1000; - struct tm *ptm = localtime(&secs); + struct tm* ptm = localtime(&secs); char tempbuff[20]; strftime(tempbuff, sizeof(tempbuff), "%m-%d %H:%M:%S", ptm); @@ -49,7 +49,7 @@ static char *format_ts(const uint64_t ts, char *buffer, int len) { return buffer; } -static const char *format_state(const btif_debug_conn_state_t state) { +static const char* format_state(const btif_debug_conn_state_t state) { switch (state) { case BTIF_DEBUG_CONNECTED: return "CONNECTED "; @@ -61,15 +61,15 @@ static const char *format_state(const btif_debug_conn_state_t state) { static void next_event() { ++current_event; - if (current_event == NUM_CONNECTION_EVENTS) - current_event = 0; + if (current_event == NUM_CONNECTION_EVENTS) current_event = 0; } -void btif_debug_conn_state(const bt_bdaddr_t bda, const btif_debug_conn_state_t state, - const tGATT_DISCONN_REASON disconnect_reason) { +void btif_debug_conn_state(const bt_bdaddr_t bda, + const btif_debug_conn_state_t state, + const tGATT_DISCONN_REASON disconnect_reason) { next_event(); - conn_event_t *evt = &connection_events[current_event]; + conn_event_t* evt = &connection_events[current_event]; evt->ts = btif_debug_ts(); evt->state = state; evt->disconnect_reason = disconnect_reason; @@ -77,25 +77,23 @@ void btif_debug_conn_state(const bt_bdaddr_t bda, const btif_debug_conn_state_t } void btif_debug_conn_dump(int fd) { - const uint8_t current_event_local = current_event; // Cache to avoid threading issues + const uint8_t current_event_local = + current_event; // Cache to avoid threading issues uint8_t dump_event = current_event_local; char ts_buffer[TEMP_BUFFER_SIZE] = {0}; char name_buffer[TEMP_BUFFER_SIZE] = {0}; dprintf(fd, "\nConnection Events:\n"); - if (connection_events[dump_event].ts == 0) - dprintf(fd, " None\n"); + if (connection_events[dump_event].ts == 0) dprintf(fd, " None\n"); while (connection_events[dump_event].ts) { - conn_event_t *evt = &connection_events[dump_event]; - dprintf(fd, " %s %s %s", - format_ts(evt->ts, ts_buffer, sizeof(ts_buffer)), + conn_event_t* evt = &connection_events[dump_event]; + dprintf(fd, " %s %s %s", format_ts(evt->ts, ts_buffer, sizeof(ts_buffer)), format_state(evt->state), - bdaddr_to_string(&evt->bda, name_buffer, sizeof(name_buffer)) - ); + bdaddr_to_string(&evt->bda, name_buffer, sizeof(name_buffer))); if (evt->state == BTIF_DEBUG_DISCONNECTED) dprintf(fd, " reason=%d", evt->disconnect_reason); - dprintf(fd,"\n"); + dprintf(fd, "\n"); // Go to previous event; wrap if needed if (dump_event > 0) @@ -104,7 +102,6 @@ void btif_debug_conn_dump(int fd) { dump_event = NUM_CONNECTION_EVENTS - 1; // Check if we dumped all events - if (dump_event == current_event_local) - break; + if (dump_event == current_event_local) break; } } diff --git a/btif/src/btif_dm.cc b/btif/src/btif_dm.cc index e5c3a3ab2..a7348016b 100644 --- a/btif/src/btif_dm.cc +++ b/btif/src/btif_dm.cc @@ -43,6 +43,7 @@ #include #include "bdaddr.h" +#include "bt_common.h" #include "bta_gatt_api.h" #include "btif_api.h" #include "btif_config.h" @@ -51,159 +52,145 @@ #include "btif_storage.h" #include "btif_util.h" #include "btu.h" -#include "bt_common.h" -#include "bta_gatt_api.h" #include "device/include/interop.h" #include "include/stack_config.h" #include "osi/include/allocator.h" #include "osi/include/log.h" #include "osi/include/metrics.h" #include "osi/include/properties.h" -#include "stack_config.h" #include "stack/btm/btm_int.h" +#include "stack_config.h" /****************************************************************************** -** Constants & Macros -******************************************************************************/ + * Constants & Macros + *****************************************************************************/ -#define COD_MASK 0x07FF +#define COD_MASK 0x07FF #define COD_UNCLASSIFIED ((0x1F) << 8) -#define COD_HID_KEYBOARD 0x0540 -#define COD_HID_POINTING 0x0580 -#define COD_HID_COMBO 0x05C0 -#define COD_HID_MAJOR 0x0500 -#define COD_HID_MASK 0x0700 -#define COD_AV_HEADSETS 0x0404 -#define COD_AV_HANDSFREE 0x0408 -#define COD_AV_HEADPHONES 0x0418 -#define COD_AV_PORTABLE_AUDIO 0x041C -#define COD_AV_HIFI_AUDIO 0x0428 - -#define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0 -#define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10 +#define COD_HID_KEYBOARD 0x0540 +#define COD_HID_POINTING 0x0580 +#define COD_HID_COMBO 0x05C0 +#define COD_HID_MAJOR 0x0500 +#define COD_HID_MASK 0x0700 +#define COD_AV_HEADSETS 0x0404 +#define COD_AV_HANDSFREE 0x0408 +#define COD_AV_HEADPHONES 0x0418 +#define COD_AV_PORTABLE_AUDIO 0x041C +#define COD_AV_HIFI_AUDIO 0x0428 + +#define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0 +#define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10 #define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2 -#define NUM_TIMEOUT_RETRIES 5 +#define NUM_TIMEOUT_RETRIES 5 #define PROPERTY_PRODUCT_MODEL "ro.product.model" -#define DEFAULT_LOCAL_NAME_MAX 31 +#define DEFAULT_LOCAL_NAME_MAX 31 #if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN) - #error "default btif local name size exceeds stack supported length" +#error "default btif local name size exceeds stack supported length" #endif #if (BTA_HOST_INTERLEAVE_SEARCH == TRUE) -#define BTIF_DM_INTERLEAVE_DURATION_BR_ONE 2 -#define BTIF_DM_INTERLEAVE_DURATION_LE_ONE 2 -#define BTIF_DM_INTERLEAVE_DURATION_BR_TWO 3 -#define BTIF_DM_INTERLEAVE_DURATION_LE_TWO 4 +#define BTIF_DM_INTERLEAVE_DURATION_BR_ONE 2 +#define BTIF_DM_INTERLEAVE_DURATION_LE_ONE 2 +#define BTIF_DM_INTERLEAVE_DURATION_BR_TWO 3 +#define BTIF_DM_INTERLEAVE_DURATION_LE_TWO 4 #endif -#define ENCRYPTED_BREDR 2 -#define ENCRYPTED_LE 4 - -typedef struct -{ - bt_bond_state_t state; - bt_bdaddr_t static_bdaddr; - BD_ADDR bd_addr; - tBTM_BOND_TYPE bond_type; - uint8_t pin_code_len; - uint8_t is_ssp; - uint8_t auth_req; - uint8_t io_cap; - uint8_t autopair_attempts; - uint8_t timeout_retries; - uint8_t is_local_initiated; - uint8_t sdp_attempts; +#define ENCRYPTED_BREDR 2 +#define ENCRYPTED_LE 4 + +typedef struct { + bt_bond_state_t state; + bt_bdaddr_t static_bdaddr; + BD_ADDR bd_addr; + tBTM_BOND_TYPE bond_type; + uint8_t pin_code_len; + uint8_t is_ssp; + uint8_t auth_req; + uint8_t io_cap; + uint8_t autopair_attempts; + uint8_t timeout_retries; + uint8_t is_local_initiated; + uint8_t sdp_attempts; #if (BLE_INCLUDED == TRUE) - bool is_le_only; - bool is_le_nc; /* LE Numeric comparison */ - btif_dm_ble_cb_t ble; + bool is_le_only; + bool is_le_nc; /* LE Numeric comparison */ + btif_dm_ble_cb_t ble; #endif } btif_dm_pairing_cb_t; -typedef struct -{ - uint8_t ir[BT_OCTET16_LEN]; - uint8_t irk[BT_OCTET16_LEN]; - uint8_t dhk[BT_OCTET16_LEN]; -}btif_dm_local_key_id_t; - -typedef struct -{ - bool is_er_rcvd; - uint8_t er[BT_OCTET16_LEN]; - bool is_id_keys_rcvd; - btif_dm_local_key_id_t id_keys; /* ID kyes */ - -}btif_dm_local_key_cb_t; - -typedef struct -{ - BD_ADDR bd_addr; - BD_NAME bd_name; +typedef struct { + uint8_t ir[BT_OCTET16_LEN]; + uint8_t irk[BT_OCTET16_LEN]; + uint8_t dhk[BT_OCTET16_LEN]; +} btif_dm_local_key_id_t; + +typedef struct { + bool is_er_rcvd; + uint8_t er[BT_OCTET16_LEN]; + bool is_id_keys_rcvd; + btif_dm_local_key_id_t id_keys; /* ID kyes */ + +} btif_dm_local_key_cb_t; + +typedef struct { + BD_ADDR bd_addr; + BD_NAME bd_name; } btif_dm_remote_name_t; /* this structure holds optional OOB data for remote device */ -typedef struct -{ - BD_ADDR bdaddr; /* peer bdaddr */ - bt_out_of_band_data_t oob_data; +typedef struct { + BD_ADDR bdaddr; /* peer bdaddr */ + bt_out_of_band_data_t oob_data; } btif_dm_oob_cb_t; -typedef struct -{ - bt_bdaddr_t bdaddr; - uint8_t transport; /* 0=Unknown, 1=BR/EDR, 2=LE */ +typedef struct { + bt_bdaddr_t bdaddr; + uint8_t transport; /* 0=Unknown, 1=BR/EDR, 2=LE */ } btif_dm_create_bond_cb_t; -typedef struct -{ - uint8_t status; - uint8_t ctrl_state; - uint64_t tx_time; - uint64_t rx_time; - uint64_t idle_time; - uint64_t energy_used; +typedef struct { + uint8_t status; + uint8_t ctrl_state; + uint64_t tx_time; + uint64_t rx_time; + uint64_t idle_time; + uint64_t energy_used; } btif_activity_energy_info_cb_t; -typedef struct -{ - unsigned int manufact_id; -}skip_sdp_entry_t; - -typedef enum -{ - BTIF_DM_FUNC_CREATE_BOND, - BTIF_DM_FUNC_CANCEL_BOND, - BTIF_DM_FUNC_REMOVE_BOND, - BTIF_DM_FUNC_BOND_STATE_CHANGED, +typedef struct { unsigned int manufact_id; } skip_sdp_entry_t; + +typedef enum { + BTIF_DM_FUNC_CREATE_BOND, + BTIF_DM_FUNC_CANCEL_BOND, + BTIF_DM_FUNC_REMOVE_BOND, + BTIF_DM_FUNC_BOND_STATE_CHANGED, } bt_bond_function_t; -typedef struct -{ - bt_bdaddr_t bd_addr; - bt_bond_function_t function; - bt_bond_state_t state; - struct timespec timestamp; +typedef struct { + bt_bdaddr_t bd_addr; + bt_bond_function_t function; + bt_bond_state_t state; + struct timespec timestamp; } btif_bond_event_t; -#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id)) +#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id)) #define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb" #define MAX_BTIF_BOND_EVENT_ENTRIES 15 -static skip_sdp_entry_t sdp_blacklist[] = {{76}}; //Apple Mouse and Keyboard +static skip_sdp_entry_t sdp_blacklist[] = {{76}}; // Apple Mouse and Keyboard /* This flag will be true if HCI_Inquiry is in progress */ static bool btif_dm_inquiry_in_progress = false; /************************************************************************************ -** Static variables -************************************************************************************/ -static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'}; + * Static variables + ***********************************************************************************/ +static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX + 1] = {'\0'}; static uid_set_t* uid_set = NULL; /* A circular array to keep track of the most recent bond events */ @@ -218,3409 +205,3216 @@ static size_t btif_events_start_index = 0; static size_t btif_events_end_index = 0; /****************************************************************************** -** Static functions -******************************************************************************/ + * Static functions + *****************************************************************************/ static btif_dm_pairing_cb_t pairing_cb; -static btif_dm_oob_cb_t oob_cb; +static btif_dm_oob_cb_t oob_cb; static void btif_dm_generic_evt(uint16_t event, char* p_param); -static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport); -static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name); +static void btif_dm_cb_create_bond(bt_bdaddr_t* bd_addr, + tBTA_TRANSPORT transport); +static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME* p_remote_name); static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name, - DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type); + DEV_CLASS dev_class, + tBT_DEVICE_TYPE dev_type); #if (BLE_INCLUDED == TRUE) static btif_dm_local_key_cb_t ble_local_key_cb; -static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif); -static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl); -static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req); -static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req) ; -static void btif_dm_ble_oob_req_evt(tBTA_DM_SP_RMT_OOB *req_oob_type); -static void btif_dm_ble_sc_oob_req_evt(tBTA_DM_SP_RMT_OOB *req_oob_type); +static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF* p_ssp_key_notif); +static void btif_dm_ble_auth_cmpl_evt(tBTA_DM_AUTH_CMPL* p_auth_cmpl); +static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ* p_pin_req); +static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF* p_notif_req); +static void btif_dm_ble_oob_req_evt(tBTA_DM_SP_RMT_OOB* req_oob_type); +static void btif_dm_ble_sc_oob_req_evt(tBTA_DM_SP_RMT_OOB* req_oob_type); #endif static void bte_scan_filt_param_cfg_evt(uint8_t action_type, - tBTA_DM_BLE_PF_AVBL_SPACE avbl_space, - tBTA_DM_BLE_REF_VALUE ref_value, - tBTA_STATUS status); + tBTA_DM_BLE_PF_AVBL_SPACE avbl_space, + tBTA_DM_BLE_REF_VALUE ref_value, + tBTA_STATUS status); static char* btif_get_default_local_name(); -static void btif_stats_add_bond_event(const bt_bdaddr_t *bd_addr, +static void btif_stats_add_bond_event(const bt_bdaddr_t* bd_addr, bt_bond_function_t function, bt_bond_state_t state); /****************************************************************************** -** Externs -******************************************************************************/ + * Externs + *****************************************************************************/ extern bt_status_t btif_hf_execute_service(bool b_enable); extern bt_status_t btif_av_execute_service(bool b_enable); extern bt_status_t btif_av_sink_execute_service(bool b_enable); extern bt_status_t btif_hh_execute_service(bool b_enable); extern bt_status_t btif_hf_client_execute_service(bool b_enable); extern bt_status_t btif_sdp_execute_service(bool b_enable); -extern int btif_hh_connect(bt_bdaddr_t *bd_addr); -extern void bta_gatt_convert_uuid16_to_uuid128(uint8_t uuid_128[LEN_UUID_128], uint16_t uuid_16); +extern int btif_hh_connect(bt_bdaddr_t* bd_addr); +extern void bta_gatt_convert_uuid16_to_uuid128(uint8_t uuid_128[LEN_UUID_128], + uint16_t uuid_16); /****************************************************************************** -** Functions -******************************************************************************/ + * Functions + *****************************************************************************/ -static bool is_empty_128bit(uint8_t *data) -{ - static const uint8_t zero[16] = { 0 }; - return !memcmp(zero, data, sizeof(zero)); +static bool is_empty_128bit(uint8_t* data) { + static const uint8_t zero[16] = {0}; + return !memcmp(zero, data, sizeof(zero)); } -static void btif_dm_data_copy(uint16_t event, char *dst, char *src) -{ - tBTA_DM_SEC *dst_dm_sec = (tBTA_DM_SEC*)dst; - tBTA_DM_SEC *src_dm_sec = (tBTA_DM_SEC*)src; +static void btif_dm_data_copy(uint16_t event, char* dst, char* src) { + tBTA_DM_SEC* dst_dm_sec = (tBTA_DM_SEC*)dst; + tBTA_DM_SEC* src_dm_sec = (tBTA_DM_SEC*)src; - if (!src_dm_sec) - return; + if (!src_dm_sec) return; - assert(dst_dm_sec); - maybe_non_aligned_memcpy(dst_dm_sec, src_dm_sec, sizeof(*src_dm_sec)); + assert(dst_dm_sec); + maybe_non_aligned_memcpy(dst_dm_sec, src_dm_sec, sizeof(*src_dm_sec)); - if (event == BTA_DM_BLE_KEY_EVT) - { - dst_dm_sec->ble_key.p_key_value = (tBTM_LE_KEY_VALUE*)osi_malloc(sizeof(tBTM_LE_KEY_VALUE)); - assert(src_dm_sec->ble_key.p_key_value); - memcpy(dst_dm_sec->ble_key.p_key_value, src_dm_sec->ble_key.p_key_value, sizeof(tBTM_LE_KEY_VALUE)); - } + if (event == BTA_DM_BLE_KEY_EVT) { + dst_dm_sec->ble_key.p_key_value = + (tBTM_LE_KEY_VALUE*)osi_malloc(sizeof(tBTM_LE_KEY_VALUE)); + assert(src_dm_sec->ble_key.p_key_value); + memcpy(dst_dm_sec->ble_key.p_key_value, src_dm_sec->ble_key.p_key_value, + sizeof(tBTM_LE_KEY_VALUE)); + } } -static void btif_dm_data_free(uint16_t event, tBTA_DM_SEC *dm_sec) -{ - if (event == BTA_DM_BLE_KEY_EVT) - osi_free_and_reset((void **)&dm_sec->ble_key.p_key_value); +static void btif_dm_data_free(uint16_t event, tBTA_DM_SEC* dm_sec) { + if (event == BTA_DM_BLE_KEY_EVT) + osi_free_and_reset((void**)&dm_sec->ble_key.p_key_value); } -void btif_dm_init(uid_set_t* set) -{ - uid_set = set; -} +void btif_dm_init(uid_set_t* set) { uid_set = set; } -void btif_dm_cleanup(void) -{ - if (uid_set) { - uid_set_destroy(uid_set); - uid_set = NULL; - } +void btif_dm_cleanup(void) { + if (uid_set) { + uid_set_destroy(uid_set); + uid_set = NULL; + } } bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id, - bool b_enable) -{ - BTIF_TRACE_DEBUG("%s service_id: %d", __func__, service_id); - /* Check the service_ID and invoke the profile's BT state changed API */ - switch (service_id) - { - case BTA_HFP_SERVICE_ID: - case BTA_HSP_SERVICE_ID: - { - btif_hf_execute_service(b_enable); - }break; - case BTA_A2DP_SOURCE_SERVICE_ID: - { - btif_av_execute_service(b_enable); - }break; - case BTA_A2DP_SINK_SERVICE_ID: - { - btif_av_sink_execute_service(b_enable); - }break; - case BTA_HID_SERVICE_ID: - { - btif_hh_execute_service(b_enable); - }break; - case BTA_HFP_HS_SERVICE_ID: - { - btif_hf_client_execute_service(b_enable); - }break; - case BTA_SDP_SERVICE_ID: - { - btif_sdp_execute_service(b_enable); - }break; - default: - BTIF_TRACE_ERROR("%s: Unknown service %d being %s", - __func__, service_id, - (b_enable) ? "enabled" : "disabled"); - return BT_STATUS_FAIL; - } - return BT_STATUS_SUCCESS; + bool b_enable) { + BTIF_TRACE_DEBUG("%s service_id: %d", __func__, service_id); + /* Check the service_ID and invoke the profile's BT state changed API */ + switch (service_id) { + case BTA_HFP_SERVICE_ID: + case BTA_HSP_SERVICE_ID: { + btif_hf_execute_service(b_enable); + } break; + case BTA_A2DP_SOURCE_SERVICE_ID: { + btif_av_execute_service(b_enable); + } break; + case BTA_A2DP_SINK_SERVICE_ID: { + btif_av_sink_execute_service(b_enable); + } break; + case BTA_HID_SERVICE_ID: { + btif_hh_execute_service(b_enable); + } break; + case BTA_HFP_HS_SERVICE_ID: { + btif_hf_client_execute_service(b_enable); + } break; + case BTA_SDP_SERVICE_ID: { + btif_sdp_execute_service(b_enable); + } break; + default: + BTIF_TRACE_ERROR("%s: Unknown service %d being %s", __func__, service_id, + (b_enable) ? "enabled" : "disabled"); + return BT_STATUS_FAIL; + } + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function check_eir_remote_name -** -** Description Check if remote name is in the EIR data -** -** Returns true if remote name found -** Populate p_remote_name, if provided and remote name found -** -*******************************************************************************/ -static bool check_eir_remote_name(tBTA_DM_SEARCH *p_search_data, - uint8_t *p_remote_name, uint8_t *p_remote_name_len) -{ - uint8_t *p_eir_remote_name = NULL; - uint8_t remote_name_len = 0; - - /* Check EIR for remote name and services */ - if (p_search_data->inq_res.p_eir) - { - p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir, - BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len); - if (!p_eir_remote_name) - { - p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir, - BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len); - } + * + * Function check_eir_remote_name + * + * Description Check if remote name is in the EIR data + * + * Returns true if remote name found + * Populate p_remote_name, if provided and remote name found + * + ******************************************************************************/ +static bool check_eir_remote_name(tBTA_DM_SEARCH* p_search_data, + uint8_t* p_remote_name, + uint8_t* p_remote_name_len) { + uint8_t* p_eir_remote_name = NULL; + uint8_t remote_name_len = 0; + + /* Check EIR for remote name and services */ + if (p_search_data->inq_res.p_eir) { + p_eir_remote_name = + BTM_CheckEirData(p_search_data->inq_res.p_eir, + BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len); + if (!p_eir_remote_name) { + p_eir_remote_name = + BTM_CheckEirData(p_search_data->inq_res.p_eir, + BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len); + } - if (p_eir_remote_name) - { - if (remote_name_len > BD_NAME_LEN) - remote_name_len = BD_NAME_LEN; + if (p_eir_remote_name) { + if (remote_name_len > BD_NAME_LEN) remote_name_len = BD_NAME_LEN; - if (p_remote_name && p_remote_name_len) - { - memcpy(p_remote_name, p_eir_remote_name, remote_name_len); - *(p_remote_name + remote_name_len) = 0; - *p_remote_name_len = remote_name_len; - } + if (p_remote_name && p_remote_name_len) { + memcpy(p_remote_name, p_eir_remote_name, remote_name_len); + *(p_remote_name + remote_name_len) = 0; + *p_remote_name_len = remote_name_len; + } - return true; - } + return true; } + } - return false; - + return false; } /******************************************************************************* -** -** Function check_cached_remote_name -** -** Description Check if remote name is in the NVRAM cache -** -** Returns true if remote name found -** Populate p_remote_name, if provided and remote name found -** -*******************************************************************************/ -static bool check_cached_remote_name(tBTA_DM_SEARCH *p_search_data, - uint8_t *p_remote_name, uint8_t *p_remote_name_len) -{ - bt_bdname_t bdname; - bt_bdaddr_t remote_bdaddr; - bt_property_t prop_name; - - /* check if we already have it in our btif_storage cache */ - bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr); - BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME, - sizeof(bt_bdname_t), &bdname); - if (btif_storage_get_remote_device_property( - &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS) - { - if (p_remote_name && p_remote_name_len) - { - strcpy((char *)p_remote_name, (char *)bdname.name); - *p_remote_name_len = strlen((char *)p_remote_name); - } - return true; + * + * Function check_cached_remote_name + * + * Description Check if remote name is in the NVRAM cache + * + * Returns true if remote name found + * Populate p_remote_name, if provided and remote name found + * + ******************************************************************************/ +static bool check_cached_remote_name(tBTA_DM_SEARCH* p_search_data, + uint8_t* p_remote_name, + uint8_t* p_remote_name_len) { + bt_bdname_t bdname; + bt_bdaddr_t remote_bdaddr; + bt_property_t prop_name; + + /* check if we already have it in our btif_storage cache */ + bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr); + BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME, + sizeof(bt_bdname_t), &bdname); + if (btif_storage_get_remote_device_property(&remote_bdaddr, &prop_name) == + BT_STATUS_SUCCESS) { + if (p_remote_name && p_remote_name_len) { + strcpy((char*)p_remote_name, (char*)bdname.name); + *p_remote_name_len = strlen((char*)p_remote_name); } + return true; + } - return false; + return false; } -static uint32_t get_cod(const bt_bdaddr_t *remote_bdaddr) { - uint32_t remote_cod; - bt_property_t prop_name; +static uint32_t get_cod(const bt_bdaddr_t* remote_bdaddr) { + uint32_t remote_cod; + bt_property_t prop_name; - /* check if we already have it in our btif_storage cache */ - BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE, - sizeof(uint32_t), &remote_cod); - if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS) - { - LOG_INFO(LOG_TAG, "%s remote_cod = 0x%08x", __func__, remote_cod); - return remote_cod & COD_MASK; - } + /* check if we already have it in our btif_storage cache */ + BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE, + sizeof(uint32_t), &remote_cod); + if (btif_storage_get_remote_device_property( + (bt_bdaddr_t*)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS) { + LOG_INFO(LOG_TAG, "%s remote_cod = 0x%08x", __func__, remote_cod); + return remote_cod & COD_MASK; + } - return 0; + return 0; } -bool check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod) -{ - return get_cod(remote_bdaddr) == cod; +bool check_cod(const bt_bdaddr_t* remote_bdaddr, uint32_t cod) { + return get_cod(remote_bdaddr) == cod; } -bool check_cod_hid(const bt_bdaddr_t *remote_bdaddr) -{ - return (get_cod(remote_bdaddr) & COD_HID_MASK) == COD_HID_MAJOR; +bool check_cod_hid(const bt_bdaddr_t* remote_bdaddr) { + return (get_cod(remote_bdaddr) & COD_HID_MASK) == COD_HID_MAJOR; } -bool check_hid_le(const bt_bdaddr_t *remote_bdaddr) -{ - uint32_t remote_dev_type; - bt_property_t prop_name; +bool check_hid_le(const bt_bdaddr_t* remote_bdaddr) { + uint32_t remote_dev_type; + bt_property_t prop_name; - /* check if we already have it in our btif_storage cache */ - BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE, - sizeof(uint32_t), &remote_dev_type); - if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, - &prop_name) == BT_STATUS_SUCCESS) - { - if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE) - { - bdstr_t bdstr; - bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr)); - if(btif_config_exist(bdstr, "HidAppId")) - return true; - } + /* check if we already have it in our btif_storage cache */ + BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_TYPE_OF_DEVICE, + sizeof(uint32_t), &remote_dev_type); + if (btif_storage_get_remote_device_property( + (bt_bdaddr_t*)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS) { + if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE) { + bdstr_t bdstr; + bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr)); + if (btif_config_exist(bdstr, "HidAppId")) return true; } - return false; + } + return false; } /***************************************************************************** -** -** Function check_sdp_bl -** -** Description Checks if a given device is blacklisted to skip sdp -** -** Parameters skip_sdp_entry -** -** Returns true if the device is present in blacklist, else false -** -*******************************************************************************/ -bool check_sdp_bl(const bt_bdaddr_t *remote_bdaddr) -{ - uint16_t manufacturer = 0; - uint8_t lmp_ver = 0; - uint16_t lmp_subver = 0; - bt_property_t prop_name; - bt_remote_version_t info; - - if (remote_bdaddr == NULL) - return false; - -/* fetch additional info about remote device used in iop query */ - BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver, - &manufacturer, &lmp_subver); - - /* if not available yet, try fetching from config database */ - BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO, - sizeof(bt_remote_version_t), &info); - - if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, - &prop_name) != BT_STATUS_SUCCESS) - { - - return false; - } - manufacturer = info.manufacturer; + * + * Function check_sdp_bl + * + * Description Checks if a given device is blacklisted to skip sdp + * + * Parameters skip_sdp_entry + * + * Returns true if the device is present in blacklist, else false + * + ******************************************************************************/ +bool check_sdp_bl(const bt_bdaddr_t* remote_bdaddr) { + uint16_t manufacturer = 0; + uint8_t lmp_ver = 0; + uint16_t lmp_subver = 0; + bt_property_t prop_name; + bt_remote_version_t info; - for (unsigned int i = 0; i < ARRAY_SIZE(sdp_blacklist); i++) - { - if (manufacturer == sdp_blacklist[i].manufact_id) - return true; - } + if (remote_bdaddr == NULL) return false; + + /* fetch additional info about remote device used in iop query */ + BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver, &manufacturer, + &lmp_subver); + + /* if not available yet, try fetching from config database */ + BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO, + sizeof(bt_remote_version_t), &info); + + if (btif_storage_get_remote_device_property( + (bt_bdaddr_t*)remote_bdaddr, &prop_name) != BT_STATUS_SUCCESS) { return false; -} + } + manufacturer = info.manufacturer; -static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state) -{ + for (unsigned int i = 0; i < ARRAY_SIZE(sdp_blacklist); i++) { + if (manufacturer == sdp_blacklist[i].manufact_id) return true; + } + return false; +} - btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_BOND_STATE_CHANGED, state); +static void bond_state_changed(bt_status_t status, bt_bdaddr_t* bd_addr, + bt_bond_state_t state) { + btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_BOND_STATE_CHANGED, state); - // Send bonding state only once - based on outgoing/incoming we may receive duplicates - if ((pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING)) - { - // Cross key pairing so send callback for static address - if (!bdaddr_is_empty(&pairing_cb.static_bdaddr)) - { - HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state); - } - return; + // Send bonding state only once - based on outgoing/incoming we may receive + // duplicates + if ((pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING)) { + // Cross key pairing so send callback for static address + if (!bdaddr_is_empty(&pairing_cb.static_bdaddr)) { + HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state); } + return; + } - if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY) - state = BT_BOND_STATE_NONE; + if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY) state = BT_BOND_STATE_NONE; - BTIF_TRACE_DEBUG("%s: state=%d, prev_state=%d, sdp_attempts = %d", __func__, - state, pairing_cb.state, pairing_cb.sdp_attempts); + BTIF_TRACE_DEBUG("%s: state=%d, prev_state=%d, sdp_attempts = %d", __func__, + state, pairing_cb.state, pairing_cb.sdp_attempts); - HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state); + HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state); - if (state == BT_BOND_STATE_BONDING) - { - pairing_cb.state = state; - bdcpy(pairing_cb.bd_addr, bd_addr->address); - } else { - if (!pairing_cb.sdp_attempts) - memset(&pairing_cb, 0, sizeof(pairing_cb)); - else - BTIF_TRACE_DEBUG("%s: BR-EDR service discovery active", __func__); - } + if (state == BT_BOND_STATE_BONDING) { + pairing_cb.state = state; + bdcpy(pairing_cb.bd_addr, bd_addr->address); + } else { + if (!pairing_cb.sdp_attempts) + memset(&pairing_cb, 0, sizeof(pairing_cb)); + else + BTIF_TRACE_DEBUG("%s: BR-EDR service discovery active", __func__); + } } /* store remote version in bt config to always have access to it post pairing*/ -static void btif_update_remote_version_property(bt_bdaddr_t *p_bd) -{ - bt_property_t property; - uint8_t lmp_ver = 0; - uint16_t lmp_subver = 0; - uint16_t mfct_set = 0; - tBTM_STATUS btm_status; - bt_remote_version_t info; - bt_status_t status; - bdstr_t bdstr; - - btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver, - &mfct_set, &lmp_subver); - - LOG_DEBUG(LOG_TAG, "remote version info [%s]: %x, %x, %x", bdaddr_to_string(p_bd, bdstr, sizeof(bdstr)), - lmp_ver, mfct_set, lmp_subver); - - if (btm_status == BTM_SUCCESS) - { - // Always update cache to ensure we have availability whenever BTM API is not populated - info.manufacturer = mfct_set; - info.sub_ver = lmp_subver; - info.version = lmp_ver; - BTIF_STORAGE_FILL_PROPERTY(&property, - BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t), - &info); - status = btif_storage_set_remote_device_property(p_bd, &property); - ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status); - } +static void btif_update_remote_version_property(bt_bdaddr_t* p_bd) { + bt_property_t property; + uint8_t lmp_ver = 0; + uint16_t lmp_subver = 0; + uint16_t mfct_set = 0; + tBTM_STATUS btm_status; + bt_remote_version_t info; + bt_status_t status; + bdstr_t bdstr; + + btm_status = + BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver, &mfct_set, &lmp_subver); + + LOG_DEBUG(LOG_TAG, "remote version info [%s]: %x, %x, %x", + bdaddr_to_string(p_bd, bdstr, sizeof(bdstr)), lmp_ver, mfct_set, + lmp_subver); + + if (btm_status == BTM_SUCCESS) { + // Always update cache to ensure we have availability whenever BTM API is + // not populated + info.manufacturer = mfct_set; + info.sub_ver = lmp_subver; + info.version = lmp_ver; + BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_REMOTE_VERSION_INFO, + sizeof(bt_remote_version_t), &info); + status = btif_storage_set_remote_device_property(p_bd, &property); + ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", + status); + } } static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name, - DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type) -{ - int num_properties = 0; - bt_property_t properties[3]; - bt_bdaddr_t bdaddr; - bt_status_t status; - uint32_t cod; - bt_device_type_t dev_type; - - memset(properties, 0, sizeof(properties)); - bdcpy(bdaddr.address, bd_addr); - - /* remote name */ - if (strlen((const char *) bd_name)) - { - BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], - BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name); - status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]); - ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status); - num_properties++; - } - - /* class of device */ - cod = devclass2uint(dev_class); - BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod); - if ( cod == 0) { - /* Try to retrieve cod from storage */ - BTIF_TRACE_DEBUG("%s cod is 0, checking cod from storage", __func__); - BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], - BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod); - status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]); - BTIF_TRACE_DEBUG("%s cod retrieved from storage is 0x%06x", __func__, cod); - if ( cod == 0) { - BTIF_TRACE_DEBUG("%s cod is again 0, set as unclassified", __func__); - cod = COD_UNCLASSIFIED; - } - } - - BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], - BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod); - status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]); - ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status); + DEV_CLASS dev_class, + tBT_DEVICE_TYPE device_type) { + int num_properties = 0; + bt_property_t properties[3]; + bt_bdaddr_t bdaddr; + bt_status_t status; + uint32_t cod; + bt_device_type_t dev_type; + + memset(properties, 0, sizeof(properties)); + bdcpy(bdaddr.address, bd_addr); + + /* remote name */ + if (strlen((const char*)bd_name)) { + BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], BT_PROPERTY_BDNAME, + strlen((char*)bd_name), bd_name); + status = btif_storage_set_remote_device_property( + &bdaddr, &properties[num_properties]); + ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", + status); num_properties++; - - /* device type */ - bt_property_t prop_name; - uint8_t remote_dev_type; - BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_TYPE_OF_DEVICE, - sizeof(uint8_t), &remote_dev_type); - if (btif_storage_get_remote_device_property(&bdaddr, &prop_name) == BT_STATUS_SUCCESS) - dev_type = (bt_device_type_t) (remote_dev_type | device_type); - else - dev_type = (bt_device_type_t) device_type; - + } + + /* class of device */ + cod = devclass2uint(dev_class); + BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod); + if (cod == 0) { + /* Try to retrieve cod from storage */ + BTIF_TRACE_DEBUG("%s cod is 0, checking cod from storage", __func__); BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], - BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type); - status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]); - ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status); - num_properties++; - - HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, - status, &bdaddr, num_properties, properties); + BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod); + status = btif_storage_get_remote_device_property( + &bdaddr, &properties[num_properties]); + BTIF_TRACE_DEBUG("%s cod retrieved from storage is 0x%06x", __func__, cod); + if (cod == 0) { + BTIF_TRACE_DEBUG("%s cod is again 0, set as unclassified", __func__); + cod = COD_UNCLASSIFIED; + } + } + + BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], + BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod); + status = btif_storage_set_remote_device_property(&bdaddr, + &properties[num_properties]); + ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", + status); + num_properties++; + + /* device type */ + bt_property_t prop_name; + uint8_t remote_dev_type; + BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_TYPE_OF_DEVICE, + sizeof(uint8_t), &remote_dev_type); + if (btif_storage_get_remote_device_property(&bdaddr, &prop_name) == + BT_STATUS_SUCCESS) + dev_type = (bt_device_type_t)(remote_dev_type | device_type); + else + dev_type = (bt_device_type_t)device_type; + + BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], + BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), + &dev_type); + status = btif_storage_set_remote_device_property(&bdaddr, + &properties[num_properties]); + ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", + status); + num_properties++; + + HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, status, &bdaddr, + num_properties, properties); } /******************************************************************************* -** -** Function btif_dm_cb_hid_remote_name -** -** Description Remote name callback for HID device. Called in btif context -** Special handling for HID devices -** -** Returns void -** -*******************************************************************************/ -static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name) -{ - BTIF_TRACE_DEBUG("%s: status=%d pairing_cb.state=%d", __func__, p_remote_name->status, pairing_cb.state); - if (pairing_cb.state == BT_BOND_STATE_BONDING) - { - bt_bdaddr_t remote_bd; - - bdcpy(remote_bd.address, pairing_cb.bd_addr); - - if (p_remote_name->status == BTM_SUCCESS) - { - bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED); - } - else - bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE); - } + * + * Function btif_dm_cb_hid_remote_name + * + * Description Remote name callback for HID device. Called in btif context + * Special handling for HID devices + * + * Returns void + * + ******************************************************************************/ +static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME* p_remote_name) { + BTIF_TRACE_DEBUG("%s: status=%d pairing_cb.state=%d", __func__, + p_remote_name->status, pairing_cb.state); + if (pairing_cb.state == BT_BOND_STATE_BONDING) { + bt_bdaddr_t remote_bd; + + bdcpy(remote_bd.address, pairing_cb.bd_addr); + + if (p_remote_name->status == BTM_SUCCESS) { + bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED); + } else + bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE); + } } /******************************************************************************* -** -** Function btif_dm_cb_create_bond -** -** Description Create bond initiated from the BTIF thread context -** Special handling for HID devices -** -** Returns void -** -*******************************************************************************/ -static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport) -{ - bool is_hid = check_cod(bd_addr, COD_HID_POINTING); - bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING); + * + * Function btif_dm_cb_create_bond + * + * Description Create bond initiated from the BTIF thread context + * Special handling for HID devices + * + * Returns void + * + ******************************************************************************/ +static void btif_dm_cb_create_bond(bt_bdaddr_t* bd_addr, + tBTA_TRANSPORT transport) { + bool is_hid = check_cod(bd_addr, COD_HID_POINTING); + bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING); #if (BLE_INCLUDED == TRUE) - int device_type; - int addr_type; - bdstr_t bdstr; - bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)); - if (transport == BT_TRANSPORT_LE) - { - if (!btif_config_get_int((char const *)&bdstr,"DevType", &device_type)) - { - btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE); - } - if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != BT_STATUS_SUCCESS) - { - btif_storage_set_remote_addr_type(bd_addr, BLE_ADDR_PUBLIC); - } - } - if((btif_config_get_int((char const *)&bdstr,"DevType", &device_type) && - (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) && - (device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE) || (transport == BT_TRANSPORT_LE)) - { - BTA_DmAddBleDevice(bd_addr->address, addr_type, device_type); - } + int device_type; + int addr_type; + bdstr_t bdstr; + bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)); + if (transport == BT_TRANSPORT_LE) { + if (!btif_config_get_int((char const*)&bdstr, "DevType", &device_type)) { + btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE); + } + if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != + BT_STATUS_SUCCESS) { + btif_storage_set_remote_addr_type(bd_addr, BLE_ADDR_PUBLIC); + } + } + if ((btif_config_get_int((char const*)&bdstr, "DevType", &device_type) && + (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == + BT_STATUS_SUCCESS) && + (device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE) || + (transport == BT_TRANSPORT_LE)) { + BTA_DmAddBleDevice(bd_addr->address, addr_type, device_type); + } #endif #if (BLE_INCLUDED == TRUE) - if(is_hid && (device_type & BT_DEVICE_TYPE_BLE) == 0) + if (is_hid && (device_type & BT_DEVICE_TYPE_BLE) == 0) #else - if(is_hid) + if (is_hid) #endif - { - bt_status_t status; - status = (bt_status_t) btif_hh_connect(bd_addr); - if(status != BT_STATUS_SUCCESS) - bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE); - } - else - { - BTA_DmBondByTransport((uint8_t *)bd_addr->address, transport); - } - /* Track originator of bond creation */ - pairing_cb.is_local_initiated = true; - + { + bt_status_t status; + status = (bt_status_t)btif_hh_connect(bd_addr); + if (status != BT_STATUS_SUCCESS) + bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE); + } else { + BTA_DmBondByTransport((uint8_t*)bd_addr->address, transport); + } + /* Track originator of bond creation */ + pairing_cb.is_local_initiated = true; } /******************************************************************************* -** -** Function btif_dm_cb_remove_bond -** -** Description remove bond initiated from the BTIF thread context -** Special handling for HID devices -** -** Returns void -** -*******************************************************************************/ -void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr) -{ - /*special handling for HID devices */ - /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there - is a valid hid connection with this bd_addr. If yes VUP will be issued.*/ + * + * Function btif_dm_cb_remove_bond + * + * Description remove bond initiated from the BTIF thread context + * Special handling for HID devices + * + * Returns void + * + ******************************************************************************/ +void btif_dm_cb_remove_bond(bt_bdaddr_t* bd_addr) { +/*special handling for HID devices */ +/* VUP needs to be sent if its a HID Device. The HID HOST module will check if +there +is a valid hid connection with this bd_addr. If yes VUP will be issued.*/ #if (BTA_HH_INCLUDED == TRUE) - if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS) + if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS) #endif - { - BTIF_TRACE_DEBUG("%s: Removing HH device", __func__); - BTA_DmRemoveDevice((uint8_t *)bd_addr->address); - } + { + BTIF_TRACE_DEBUG("%s: Removing HH device", __func__); + BTA_DmRemoveDevice((uint8_t*)bd_addr->address); + } } /******************************************************************************* -** -** Function btif_dm_get_connection_state -** -** Description Returns whether the remote device is currently connected -** and whether encryption is active for the connection -** -** Returns 0 if not connected; 1 if connected and > 1 if connection is -** encrypted -** -*******************************************************************************/ -uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr) -{ - uint8_t *bda = (uint8_t*)bd_addr->address; - uint16_t rc = BTA_DmGetConnectionState(bda); - - if (rc != 0) - { - uint8_t flags = 0; - - BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_BR_EDR); - BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __func__, flags); - if (flags & BTM_SEC_FLAG_ENCRYPTED) - rc |= ENCRYPTED_BREDR; - - BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_LE); - BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __func__, flags); - if (flags & BTM_SEC_FLAG_ENCRYPTED) - rc |= ENCRYPTED_LE; - } + * + * Function btif_dm_get_connection_state + * + * Description Returns whether the remote device is currently connected + * and whether encryption is active for the connection + * + * Returns 0 if not connected; 1 if connected and > 1 if connection is + * encrypted + * + ******************************************************************************/ +uint16_t btif_dm_get_connection_state(const bt_bdaddr_t* bd_addr) { + uint8_t* bda = (uint8_t*)bd_addr->address; + uint16_t rc = BTA_DmGetConnectionState(bda); - return rc; -} + if (rc != 0) { + uint8_t flags = 0; -/******************************************************************************* -** -** Function search_devices_copy_cb -** -** Description Deep copy callback for search devices event -** -** Returns void -** -*******************************************************************************/ -static void search_devices_copy_cb(uint16_t event, char *p_dest, char *p_src) -{ - tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest; - tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src; - - if (!p_src) - return; + BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_BR_EDR); + BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __func__, flags); + if (flags & BTM_SEC_FLAG_ENCRYPTED) rc |= ENCRYPTED_BREDR; - BTIF_TRACE_DEBUG("%s: event=%s", __func__, dump_dm_search_event(event)); - maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data)); - switch (event) - { - case BTA_DM_INQ_RES_EVT: - { - if (p_src_data->inq_res.p_eir) - { - p_dest_data->inq_res.p_eir = (uint8_t *)(p_dest + sizeof(tBTA_DM_SEARCH)); - memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN); - } - } - break; + BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_LE); + BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __func__, flags); + if (flags & BTM_SEC_FLAG_ENCRYPTED) rc |= ENCRYPTED_LE; + } - case BTA_DM_DISC_RES_EVT: - { - if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data) - { - p_dest_data->disc_res.p_raw_data = (uint8_t *)(p_dest + sizeof(tBTA_DM_SEARCH)); - memcpy(p_dest_data->disc_res.p_raw_data, - p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size); - } - } - break; - } + return rc; } -static void search_services_copy_cb(uint16_t event, char *p_dest, char *p_src) -{ - tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest; - tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src; - - if (!p_src) - return; - maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data)); - switch (event) - { - case BTA_DM_DISC_RES_EVT: - { - if (p_src_data->disc_res.result == BTA_SUCCESS) - { - if (p_src_data->disc_res.num_uuids > 0) - { - p_dest_data->disc_res.p_uuid_list = - (uint8_t*)(p_dest + sizeof(tBTA_DM_SEARCH)); - memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list, - p_src_data->disc_res.num_uuids*MAX_UUID_SIZE); - osi_free_and_reset((void **)&p_src_data->disc_res.p_uuid_list); - } - osi_free_and_reset((void **)&p_src_data->disc_res.p_raw_data); - } - } break; - } +/******************************************************************************* + * + * Function search_devices_copy_cb + * + * Description Deep copy callback for search devices event + * + * Returns void + * + ******************************************************************************/ +static void search_devices_copy_cb(uint16_t event, char* p_dest, char* p_src) { + tBTA_DM_SEARCH* p_dest_data = (tBTA_DM_SEARCH*)p_dest; + tBTA_DM_SEARCH* p_src_data = (tBTA_DM_SEARCH*)p_src; + + if (!p_src) return; + + BTIF_TRACE_DEBUG("%s: event=%s", __func__, dump_dm_search_event(event)); + maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data)); + switch (event) { + case BTA_DM_INQ_RES_EVT: { + if (p_src_data->inq_res.p_eir) { + p_dest_data->inq_res.p_eir = + (uint8_t*)(p_dest + sizeof(tBTA_DM_SEARCH)); + memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, + HCI_EXT_INQ_RESPONSE_LEN); + } + } break; + + case BTA_DM_DISC_RES_EVT: { + if (p_src_data->disc_res.raw_data_size && + p_src_data->disc_res.p_raw_data) { + p_dest_data->disc_res.p_raw_data = + (uint8_t*)(p_dest + sizeof(tBTA_DM_SEARCH)); + memcpy(p_dest_data->disc_res.p_raw_data, + p_src_data->disc_res.p_raw_data, + p_src_data->disc_res.raw_data_size); + } + } break; + } +} + +static void search_services_copy_cb(uint16_t event, char* p_dest, char* p_src) { + tBTA_DM_SEARCH* p_dest_data = (tBTA_DM_SEARCH*)p_dest; + tBTA_DM_SEARCH* p_src_data = (tBTA_DM_SEARCH*)p_src; + + if (!p_src) return; + maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data)); + switch (event) { + case BTA_DM_DISC_RES_EVT: { + if (p_src_data->disc_res.result == BTA_SUCCESS) { + if (p_src_data->disc_res.num_uuids > 0) { + p_dest_data->disc_res.p_uuid_list = + (uint8_t*)(p_dest + sizeof(tBTA_DM_SEARCH)); + memcpy(p_dest_data->disc_res.p_uuid_list, + p_src_data->disc_res.p_uuid_list, + p_src_data->disc_res.num_uuids * MAX_UUID_SIZE); + osi_free_and_reset((void**)&p_src_data->disc_res.p_uuid_list); + } + osi_free_and_reset((void**)&p_src_data->disc_res.p_raw_data); + } + } break; + } } /****************************************************************************** -** -** BTIF DM callback events -** -*****************************************************************************/ + * + * BTIF DM callback events + * + ****************************************************************************/ /******************************************************************************* -** -** Function btif_dm_pin_req_evt -** -** Description Executes pin request event in btif context -** -** Returns void -** -*******************************************************************************/ -static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req) -{ - bt_bdaddr_t bd_addr; - bt_bdname_t bd_name; - uint32_t cod; - bt_pin_code_t pin_code; - int dev_type; - - /* Remote properties update */ - if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type)) - { - dev_type = BT_DEVICE_TYPE_BREDR; - } - btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name, - p_pin_req->dev_class, (tBT_DEVICE_TYPE) dev_type); - - bdcpy(bd_addr.address, p_pin_req->bd_addr); - memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN); - - bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); - - cod = devclass2uint(p_pin_req->dev_class); - - if (cod == 0) { - BTIF_TRACE_DEBUG("%s cod is 0, set as unclassified", __func__); - cod = COD_UNCLASSIFIED; - } - - /* check for auto pair possiblity only if bond was initiated by local device */ - if (pairing_cb.is_local_initiated && (p_pin_req->min_16_digit == false)) - { - if (check_cod(&bd_addr, COD_AV_HEADSETS) || - check_cod(&bd_addr, COD_AV_HEADPHONES) || - check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) || - check_cod(&bd_addr, COD_AV_HIFI_AUDIO) || - check_cod(&bd_addr, COD_HID_POINTING)) - { - /* Check if this device can be auto paired */ - if (!interop_match_addr(INTEROP_DISABLE_AUTO_PAIRING, &bd_addr) && - !interop_match_name(INTEROP_DISABLE_AUTO_PAIRING, (const char *)bd_name.name) && - (pairing_cb.autopair_attempts == 0)) - { - BTIF_TRACE_DEBUG("%s() Attempting auto pair", __func__); - pin_code.pin[0] = 0x30; - pin_code.pin[1] = 0x30; - pin_code.pin[2] = 0x30; - pin_code.pin[3] = 0x30; - - pairing_cb.autopair_attempts++; - BTA_DmPinReply( (uint8_t*)bd_addr.address, true, 4, pin_code.pin); - return; - } - } - else if (check_cod(&bd_addr, COD_HID_KEYBOARD) || - check_cod(&bd_addr, COD_HID_COMBO)) - { - if ((interop_match_addr(INTEROP_KEYBOARD_REQUIRES_FIXED_PIN, &bd_addr) == true) && - (pairing_cb.autopair_attempts == 0)) - { - BTIF_TRACE_DEBUG("%s() Attempting auto pair", __func__); - pin_code.pin[0] = 0x30; - pin_code.pin[1] = 0x30; - pin_code.pin[2] = 0x30; - pin_code.pin[3] = 0x30; - - pairing_cb.autopair_attempts++; - BTA_DmPinReply( (uint8_t*)bd_addr.address, true, 4, pin_code.pin); - return; - } - } + * + * Function btif_dm_pin_req_evt + * + * Description Executes pin request event in btif context + * + * Returns void + * + ******************************************************************************/ +static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ* p_pin_req) { + bt_bdaddr_t bd_addr; + bt_bdname_t bd_name; + uint32_t cod; + bt_pin_code_t pin_code; + int dev_type; + + /* Remote properties update */ + if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type)) { + dev_type = BT_DEVICE_TYPE_BREDR; + } + btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name, + p_pin_req->dev_class, + (tBT_DEVICE_TYPE)dev_type); + + bdcpy(bd_addr.address, p_pin_req->bd_addr); + memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN); + + bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); + + cod = devclass2uint(p_pin_req->dev_class); + + if (cod == 0) { + BTIF_TRACE_DEBUG("%s cod is 0, set as unclassified", __func__); + cod = COD_UNCLASSIFIED; + } + + /* check for auto pair possiblity only if bond was initiated by local device + */ + if (pairing_cb.is_local_initiated && (p_pin_req->min_16_digit == false)) { + if (check_cod(&bd_addr, COD_AV_HEADSETS) || + check_cod(&bd_addr, COD_AV_HEADPHONES) || + check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) || + check_cod(&bd_addr, COD_AV_HIFI_AUDIO) || + check_cod(&bd_addr, COD_HID_POINTING)) { + /* Check if this device can be auto paired */ + if (!interop_match_addr(INTEROP_DISABLE_AUTO_PAIRING, &bd_addr) && + !interop_match_name(INTEROP_DISABLE_AUTO_PAIRING, + (const char*)bd_name.name) && + (pairing_cb.autopair_attempts == 0)) { + BTIF_TRACE_DEBUG("%s() Attempting auto pair", __func__); + pin_code.pin[0] = 0x30; + pin_code.pin[1] = 0x30; + pin_code.pin[2] = 0x30; + pin_code.pin[3] = 0x30; + + pairing_cb.autopair_attempts++; + BTA_DmPinReply((uint8_t*)bd_addr.address, true, 4, pin_code.pin); + return; + } + } else if (check_cod(&bd_addr, COD_HID_KEYBOARD) || + check_cod(&bd_addr, COD_HID_COMBO)) { + if ((interop_match_addr(INTEROP_KEYBOARD_REQUIRES_FIXED_PIN, &bd_addr) == + true) && + (pairing_cb.autopair_attempts == 0)) { + BTIF_TRACE_DEBUG("%s() Attempting auto pair", __func__); + pin_code.pin[0] = 0x30; + pin_code.pin[1] = 0x30; + pin_code.pin[2] = 0x30; + pin_code.pin[3] = 0x30; + + pairing_cb.autopair_attempts++; + BTA_DmPinReply((uint8_t*)bd_addr.address, true, 4, pin_code.pin); + return; + } } - HAL_CBACK(bt_hal_cbacks, pin_request_cb, - &bd_addr, &bd_name, cod, p_pin_req->min_16_digit); + } + HAL_CBACK(bt_hal_cbacks, pin_request_cb, &bd_addr, &bd_name, cod, + p_pin_req->min_16_digit); } /******************************************************************************* -** -** Function btif_dm_ssp_cfm_req_evt -** -** Description Executes SSP confirm request event in btif context -** -** Returns void -** -*******************************************************************************/ -static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req) -{ - bt_bdaddr_t bd_addr; - bt_bdname_t bd_name; - uint32_t cod; - bool is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING); - int dev_type; - - BTIF_TRACE_DEBUG("%s", __func__); - - /* Remote properties update */ - if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type)) - { - dev_type = BT_DEVICE_TYPE_BREDR; - } - btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name, - p_ssp_cfm_req->dev_class, (tBT_DEVICE_TYPE) dev_type); - - bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr); - memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN); - - /* Set the pairing_cb based on the local & remote authentication requirements */ - bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); - - /* if just_works and bonding bit is not set treat this as temporary */ - if (p_ssp_cfm_req->just_works && !(p_ssp_cfm_req->loc_auth_req & BTM_AUTH_BONDS) && - !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) && - !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING))) - pairing_cb.bond_type = BOND_TYPE_TEMPORARY; - else - pairing_cb.bond_type = BOND_TYPE_PERSISTENT; + * + * Function btif_dm_ssp_cfm_req_evt + * + * Description Executes SSP confirm request event in btif context + * + * Returns void + * + ******************************************************************************/ +static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ* p_ssp_cfm_req) { + bt_bdaddr_t bd_addr; + bt_bdname_t bd_name; + uint32_t cod; + bool is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING); + int dev_type; + + BTIF_TRACE_DEBUG("%s", __func__); + + /* Remote properties update */ + if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type)) { + dev_type = BT_DEVICE_TYPE_BREDR; + } + btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name, + p_ssp_cfm_req->dev_class, + (tBT_DEVICE_TYPE)dev_type); + + bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr); + memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN); + + /* Set the pairing_cb based on the local & remote authentication requirements + */ + bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); + + /* if just_works and bonding bit is not set treat this as temporary */ + if (p_ssp_cfm_req->just_works && + !(p_ssp_cfm_req->loc_auth_req & BTM_AUTH_BONDS) && + !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) && + !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING))) + pairing_cb.bond_type = BOND_TYPE_TEMPORARY; + else + pairing_cb.bond_type = BOND_TYPE_PERSISTENT; - btm_set_bond_type_dev(p_ssp_cfm_req->bd_addr, pairing_cb.bond_type); + btm_set_bond_type_dev(p_ssp_cfm_req->bd_addr, pairing_cb.bond_type); - pairing_cb.is_ssp = true; + pairing_cb.is_ssp = true; - /* If JustWorks auto-accept */ - if (p_ssp_cfm_req->just_works) - { - /* Pairing consent for JustWorks needed if: - * 1. Incoming (non-temporary) pairing is detected AND - * 2. local IO capabilities are DisplayYesNo AND - * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput; - */ - if (is_incoming && pairing_cb.bond_type != BOND_TYPE_TEMPORARY && - ((p_ssp_cfm_req->loc_io_caps == HCI_IO_CAP_DISPLAY_YESNO) && - (p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_DISPLAY_ONLY || - p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_NO_IO))) - { - BTIF_TRACE_EVENT("%s: User consent needed for incoming pairing request. loc_io_caps: %d, rmt_io_caps: %d", - __func__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps); - } - else - { - BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __func__); - btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, true, 0); - return; - } + /* If JustWorks auto-accept */ + if (p_ssp_cfm_req->just_works) { + /* Pairing consent for JustWorks needed if: + * 1. Incoming (non-temporary) pairing is detected AND + * 2. local IO capabilities are DisplayYesNo AND + * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput; + */ + if (is_incoming && pairing_cb.bond_type != BOND_TYPE_TEMPORARY && + ((p_ssp_cfm_req->loc_io_caps == HCI_IO_CAP_DISPLAY_YESNO) && + (p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_DISPLAY_ONLY || + p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_NO_IO))) { + BTIF_TRACE_EVENT( + "%s: User consent needed for incoming pairing request. loc_io_caps: " + "%d, rmt_io_caps: %d", + __func__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps); + } else { + BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __func__); + btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, true, 0); + return; } + } - cod = devclass2uint(p_ssp_cfm_req->dev_class); + cod = devclass2uint(p_ssp_cfm_req->dev_class); - if (cod == 0) { - LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__); - cod = COD_UNCLASSIFIED; - } + if (cod == 0) { + LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__); + cod = COD_UNCLASSIFIED; + } - pairing_cb.sdp_attempts = 0; - HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod, - (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION), - p_ssp_cfm_req->num_val); + pairing_cb.sdp_attempts = 0; + HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod, + (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT + : BT_SSP_VARIANT_PASSKEY_CONFIRMATION), + p_ssp_cfm_req->num_val); } -static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif) -{ - bt_bdaddr_t bd_addr; - bt_bdname_t bd_name; - uint32_t cod; - int dev_type; +static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF* p_ssp_key_notif) { + bt_bdaddr_t bd_addr; + bt_bdname_t bd_name; + uint32_t cod; + int dev_type; - BTIF_TRACE_DEBUG("%s", __func__); + BTIF_TRACE_DEBUG("%s", __func__); - /* Remote properties update */ - if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type)) - { - dev_type = BT_DEVICE_TYPE_BREDR; - } - btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name, - p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE) dev_type); + /* Remote properties update */ + if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type)) { + dev_type = BT_DEVICE_TYPE_BREDR; + } + btif_update_remote_properties( + p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name, + p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE)dev_type); - bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr); - memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN); + bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr); + memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN); - bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); - pairing_cb.is_ssp = true; - cod = devclass2uint(p_ssp_key_notif->dev_class); + bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); + pairing_cb.is_ssp = true; + cod = devclass2uint(p_ssp_key_notif->dev_class); - if (cod == 0) { - LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__); - cod = COD_UNCLASSIFIED; - } + if (cod == 0) { + LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__); + cod = COD_UNCLASSIFIED; + } - HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, - cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION, - p_ssp_key_notif->passkey); + HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod, + BT_SSP_VARIANT_PASSKEY_NOTIFICATION, p_ssp_key_notif->passkey); } /******************************************************************************* -** -** Function btif_dm_auth_cmpl_evt -** -** Description Executes authentication complete event in btif context -** -** Returns void -** -*******************************************************************************/ -static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl) -{ - /* Save link key, if not temporary */ - bt_bdaddr_t bd_addr; - bt_status_t status = BT_STATUS_FAIL; - bt_bond_state_t state = BT_BOND_STATE_NONE; - bool skip_sdp = false; - - BTIF_TRACE_DEBUG("%s: bond state=%d", __func__, pairing_cb.state); + * + * Function btif_dm_auth_cmpl_evt + * + * Description Executes authentication complete event in btif context + * + * Returns void + * + ******************************************************************************/ +static void btif_dm_auth_cmpl_evt(tBTA_DM_AUTH_CMPL* p_auth_cmpl) { + /* Save link key, if not temporary */ + bt_bdaddr_t bd_addr; + bt_status_t status = BT_STATUS_FAIL; + bt_bond_state_t state = BT_BOND_STATE_NONE; + bool skip_sdp = false; + + BTIF_TRACE_DEBUG("%s: bond state=%d", __func__, pairing_cb.state); + + bdcpy(bd_addr.address, p_auth_cmpl->bd_addr); + if ((p_auth_cmpl->success == true) && (p_auth_cmpl->key_present)) { + if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) || + (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) || + (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) || + (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB_P_256) || + pairing_cb.bond_type == BOND_TYPE_PERSISTENT) { + bt_status_t ret; + BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d", + __func__, p_auth_cmpl->key_type, pairing_cb.bond_type); + ret = btif_storage_add_bonded_device(&bd_addr, p_auth_cmpl->key, + p_auth_cmpl->key_type, + pairing_cb.pin_code_len); + ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret); + } else { + BTIF_TRACE_DEBUG( + "%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d", + __func__, p_auth_cmpl->key_type, pairing_cb.bond_type); + if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY) { + BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing", + __func__); + btif_storage_remove_bonded_device(&bd_addr); + bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE); + return; + } + } + } + + // We could have received a new link key without going through the pairing + // flow. + // If so, we don't want to perform SDP or any other operations on the + // authenticated + // device. + if (bdcmp(p_auth_cmpl->bd_addr, pairing_cb.bd_addr) != 0) { + char address[32]; + bt_bdaddr_t bt_bdaddr; + + memcpy(bt_bdaddr.address, p_auth_cmpl->bd_addr, sizeof(bt_bdaddr.address)); + bdaddr_to_string(&bt_bdaddr, address, sizeof(address)); + LOG_INFO(LOG_TAG, + "%s skipping SDP since we did not initiate pairing to %s.", + __func__, address); + return; + } + // Skip SDP for certain HID Devices + if (p_auth_cmpl->success) { +#if (BLE_INCLUDED == TRUE) + btif_storage_set_remote_addr_type(&bd_addr, p_auth_cmpl->addr_type); +#endif + btif_update_remote_properties(p_auth_cmpl->bd_addr, p_auth_cmpl->bd_name, + NULL, p_auth_cmpl->dev_type); + pairing_cb.timeout_retries = 0; + status = BT_STATUS_SUCCESS; + state = BT_BOND_STATE_BONDED; bdcpy(bd_addr.address, p_auth_cmpl->bd_addr); - if ( (p_auth_cmpl->success == true) && (p_auth_cmpl->key_present) ) - { - if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) || - (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) || - (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) || - (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB_P_256) || - pairing_cb.bond_type == BOND_TYPE_PERSISTENT) - { - bt_status_t ret; - BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d", - __func__, p_auth_cmpl->key_type, pairing_cb.bond_type); - ret = btif_storage_add_bonded_device(&bd_addr, - p_auth_cmpl->key, p_auth_cmpl->key_type, - pairing_cb.pin_code_len); - ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret); - } - else - { - BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d", - __func__, p_auth_cmpl->key_type, pairing_cb.bond_type); - if(pairing_cb.bond_type == BOND_TYPE_TEMPORARY) - { - BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing", - __func__); - btif_storage_remove_bonded_device(&bd_addr); - bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE); - return; - } - } - } - // We could have received a new link key without going through the pairing flow. - // If so, we don't want to perform SDP or any other operations on the authenticated - // device. - if (bdcmp(p_auth_cmpl->bd_addr, pairing_cb.bd_addr) != 0) { - char address[32]; - bt_bdaddr_t bt_bdaddr; - - memcpy(bt_bdaddr.address, p_auth_cmpl->bd_addr, - sizeof(bt_bdaddr.address)); - bdaddr_to_string(&bt_bdaddr, address, sizeof(address)); - LOG_INFO(LOG_TAG, "%s skipping SDP since we did not initiate pairing to %s.", __func__, address); - return; + if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr)) { + LOG_WARN(LOG_TAG, "%s:skip SDP", __func__); + skip_sdp = true; } + if (!pairing_cb.is_local_initiated && skip_sdp) { + bond_state_changed(status, &bd_addr, state); - // Skip SDP for certain HID Devices - if (p_auth_cmpl->success) - { -#if (BLE_INCLUDED == TRUE) - btif_storage_set_remote_addr_type(&bd_addr, p_auth_cmpl->addr_type); -#endif - btif_update_remote_properties(p_auth_cmpl->bd_addr, - p_auth_cmpl->bd_name, NULL, p_auth_cmpl->dev_type); - pairing_cb.timeout_retries = 0; - status = BT_STATUS_SUCCESS; - state = BT_BOND_STATE_BONDED; - bdcpy(bd_addr.address, p_auth_cmpl->bd_addr); - - if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr)) - { - LOG_WARN(LOG_TAG, "%s:skip SDP", __func__); - skip_sdp = true; - } - if(!pairing_cb.is_local_initiated && skip_sdp) - { - bond_state_changed(status, &bd_addr, state); - - LOG_WARN(LOG_TAG, "%s: Incoming HID Connection",__func__); - bt_property_t prop; - bt_bdaddr_t bd_addr; - bt_uuid_t uuid; - char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE; + LOG_WARN(LOG_TAG, "%s: Incoming HID Connection", __func__); + bt_property_t prop; + bt_bdaddr_t bd_addr; + bt_uuid_t uuid; + char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE; - string_to_uuid(uuid_str, &uuid); + string_to_uuid(uuid_str, &uuid); - prop.type = BT_PROPERTY_UUIDS; - prop.val = uuid.uu; - prop.len = MAX_UUID_SIZE; + prop.type = BT_PROPERTY_UUIDS; + prop.val = uuid.uu; + prop.len = MAX_UUID_SIZE; - /* Send the event to the BTIF */ - HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, - BT_STATUS_SUCCESS, &bd_addr, 1, &prop); - } - else - { + /* Send the event to the BTIF */ + HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, BT_STATUS_SUCCESS, + &bd_addr, 1, &prop); + } else { #if (BLE_INCLUDED == TRUE) - bool is_crosskey = false; - /* If bonded due to cross-key, save the static address too*/ - if(pairing_cb.state == BT_BOND_STATE_BONDING && - (bdcmp(p_auth_cmpl->bd_addr, pairing_cb.bd_addr) != 0)) - { - BTIF_TRACE_DEBUG("%s: bonding initiated due to cross key, adding static address", - __func__); - bdcpy(pairing_cb.static_bdaddr.address, p_auth_cmpl->bd_addr); - is_crosskey = true; - } - if (!is_crosskey || !(stack_config_get_interface()->get_pts_crosskey_sdp_disable())) - { + bool is_crosskey = false; + /* If bonded due to cross-key, save the static address too*/ + if (pairing_cb.state == BT_BOND_STATE_BONDING && + (bdcmp(p_auth_cmpl->bd_addr, pairing_cb.bd_addr) != 0)) { + BTIF_TRACE_DEBUG( + "%s: bonding initiated due to cross key, adding static address", + __func__); + bdcpy(pairing_cb.static_bdaddr.address, p_auth_cmpl->bd_addr); + is_crosskey = true; + } + if (!is_crosskey || + !(stack_config_get_interface()->get_pts_crosskey_sdp_disable())) { #endif - // Ensure inquiry is stopped before attempting service discovery - btif_dm_cancel_discovery(); + // Ensure inquiry is stopped before attempting service discovery + btif_dm_cancel_discovery(); - /* Trigger SDP on the device */ - pairing_cb.sdp_attempts = 1; - btif_dm_get_remote_services(&bd_addr); + /* Trigger SDP on the device */ + pairing_cb.sdp_attempts = 1; + btif_dm_get_remote_services(&bd_addr); #if (BLE_INCLUDED == TRUE) - } + } #endif - } - // Do not call bond_state_changed_cb yet. Wait until remote service discovery is complete } - else - { - // Map the HCI fail reason to bt status - switch(p_auth_cmpl->fail_reason) - { - case HCI_ERR_PAGE_TIMEOUT: - if (interop_match_addr(INTEROP_AUTO_RETRY_PAIRING, &bd_addr) - && pairing_cb.timeout_retries) - { - BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...", __func__, pairing_cb.timeout_retries); - --pairing_cb.timeout_retries; - btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN); - return; - } - /* Fall-through */ - case HCI_ERR_CONNECTION_TOUT: - status = BT_STATUS_RMT_DEV_DOWN; - break; - - case HCI_ERR_PAIRING_NOT_ALLOWED: - btif_storage_remove_bonded_device(&bd_addr); - status = BT_STATUS_AUTH_REJECTED; - break; - - case HCI_ERR_LMP_RESPONSE_TIMEOUT: - status = BT_STATUS_AUTH_FAILURE; - break; - - /* map the auth failure codes, so we can retry pairing if necessary */ - case HCI_ERR_AUTH_FAILURE: - case HCI_ERR_KEY_MISSING: - btif_storage_remove_bonded_device(&bd_addr); - case HCI_ERR_HOST_REJECT_SECURITY: - case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE: - case HCI_ERR_UNIT_KEY_USED: - case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED: - case HCI_ERR_INSUFFCIENT_SECURITY: - case HCI_ERR_PEER_USER: - case HCI_ERR_UNSPECIFIED: - BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d", - __func__, p_auth_cmpl->fail_reason); - if (pairing_cb.autopair_attempts == 1) - { - /* Create the Bond once again */ - BTIF_TRACE_WARNING("%s() auto pair failed. Reinitiate Bond", __func__); - btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN); - return; - } - else - { - /* if autopair attempts are more than 1, or not attempted */ - status = BT_STATUS_AUTH_FAILURE; - } - break; - - default: - status = BT_STATUS_FAIL; + // Do not call bond_state_changed_cb yet. Wait until remote service + // discovery is complete + } else { + // Map the HCI fail reason to bt status + switch (p_auth_cmpl->fail_reason) { + case HCI_ERR_PAGE_TIMEOUT: + if (interop_match_addr(INTEROP_AUTO_RETRY_PAIRING, &bd_addr) && + pairing_cb.timeout_retries) { + BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...", + __func__, pairing_cb.timeout_retries); + --pairing_cb.timeout_retries; + btif_dm_cb_create_bond(&bd_addr, BTA_TRANSPORT_UNKNOWN); + return; } - /* Special Handling for HID Devices */ - if (check_cod(&bd_addr, COD_HID_POINTING)) { - /* Remove Device as bonded in nvram as authentication failed */ - BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __func__); - btif_storage_remove_bonded_device(&bd_addr); - } - bond_state_changed(status, &bd_addr, state); - } -} + /* Fall-through */ + case HCI_ERR_CONNECTION_TOUT: + status = BT_STATUS_RMT_DEV_DOWN; + break; -/****************************************************************************** -** -** Function btif_dm_search_devices_evt -** -** Description Executes search devices callback events in btif context -** -** Returns void -** -******************************************************************************/ -static void btif_dm_search_devices_evt (uint16_t event, char *p_param) -{ - tBTA_DM_SEARCH *p_search_data; - BTIF_TRACE_EVENT("%s event=%s", __func__, dump_dm_search_event(event)); - - switch (event) - { - case BTA_DM_DISC_RES_EVT: - { - p_search_data = (tBTA_DM_SEARCH *)p_param; - /* Remote name update */ - if (strlen((const char *) p_search_data->disc_res.bd_name)) - { - bt_property_t properties[1]; - bt_bdaddr_t bdaddr; - bt_status_t status; - - properties[0].type = BT_PROPERTY_BDNAME; - properties[0].val = p_search_data->disc_res.bd_name; - properties[0].len = strlen((char *)p_search_data->disc_res.bd_name); - bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr); - - status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]); - ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status); - HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, - status, &bdaddr, 1, properties); - } - /* TODO: Services? */ + case HCI_ERR_PAIRING_NOT_ALLOWED: + btif_storage_remove_bonded_device(&bd_addr); + status = BT_STATUS_AUTH_REJECTED; + break; + + case HCI_ERR_LMP_RESPONSE_TIMEOUT: + status = BT_STATUS_AUTH_FAILURE; + break; + + /* map the auth failure codes, so we can retry pairing if necessary */ + case HCI_ERR_AUTH_FAILURE: + case HCI_ERR_KEY_MISSING: + btif_storage_remove_bonded_device(&bd_addr); + case HCI_ERR_HOST_REJECT_SECURITY: + case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE: + case HCI_ERR_UNIT_KEY_USED: + case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED: + case HCI_ERR_INSUFFCIENT_SECURITY: + case HCI_ERR_PEER_USER: + case HCI_ERR_UNSPECIFIED: + BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d", __func__, + p_auth_cmpl->fail_reason); + if (pairing_cb.autopair_attempts == 1) { + /* Create the Bond once again */ + BTIF_TRACE_WARNING("%s() auto pair failed. Reinitiate Bond", + __func__); + btif_dm_cb_create_bond(&bd_addr, BTA_TRANSPORT_UNKNOWN); + return; + } else { + /* if autopair attempts are more than 1, or not attempted */ + status = BT_STATUS_AUTH_FAILURE; } break; - case BTA_DM_INQ_RES_EVT: - { - /* inquiry result */ - uint32_t cod; - bt_bdname_t bdname; - bt_bdaddr_t bdaddr; - uint8_t remote_name_len; - tBTA_SERVICE_MASK services = 0; - bdstr_t bdstr; + default: + status = BT_STATUS_FAIL; + } + /* Special Handling for HID Devices */ + if (check_cod(&bd_addr, COD_HID_POINTING)) { + /* Remove Device as bonded in nvram as authentication failed */ + BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", + __func__); + btif_storage_remove_bonded_device(&bd_addr); + } + bond_state_changed(status, &bd_addr, state); + } +} - p_search_data = (tBTA_DM_SEARCH *)p_param; - bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr); +/****************************************************************************** + * + * Function btif_dm_search_devices_evt + * + * Description Executes search devices callback events in btif context + * + * Returns void + * + *****************************************************************************/ +static void btif_dm_search_devices_evt(uint16_t event, char* p_param) { + tBTA_DM_SEARCH* p_search_data; + BTIF_TRACE_EVENT("%s event=%s", __func__, dump_dm_search_event(event)); + + switch (event) { + case BTA_DM_DISC_RES_EVT: { + p_search_data = (tBTA_DM_SEARCH*)p_param; + /* Remote name update */ + if (strlen((const char*)p_search_data->disc_res.bd_name)) { + bt_property_t properties[1]; + bt_bdaddr_t bdaddr; + bt_status_t status; - BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __func__, bdaddr_to_string(&bdaddr, bdstr, sizeof(bdstr)), + properties[0].type = BT_PROPERTY_BDNAME; + properties[0].val = p_search_data->disc_res.bd_name; + properties[0].len = strlen((char*)p_search_data->disc_res.bd_name); + bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr); + + status = + btif_storage_set_remote_device_property(&bdaddr, &properties[0]); + ASSERTC(status == BT_STATUS_SUCCESS, + "failed to save remote device property", status); + HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, status, &bdaddr, + 1, properties); + } + /* TODO: Services? */ + } break; + + case BTA_DM_INQ_RES_EVT: { + /* inquiry result */ + uint32_t cod; + bt_bdname_t bdname; + bt_bdaddr_t bdaddr; + uint8_t remote_name_len; + tBTA_SERVICE_MASK services = 0; + bdstr_t bdstr; + + p_search_data = (tBTA_DM_SEARCH*)p_param; + bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr); + + BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __func__, + bdaddr_to_string(&bdaddr, bdstr, sizeof(bdstr)), #if (BLE_INCLUDED == TRUE) - p_search_data->inq_res.device_type); + p_search_data->inq_res.device_type); #else - BT_DEVICE_TYPE_BREDR); + BT_DEVICE_TYPE_BREDR); #endif - bdname.name[0] = 0; - - cod = devclass2uint (p_search_data->inq_res.dev_class); - - if (cod == 0) { - LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__); - cod = COD_UNCLASSIFIED; - } - - if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len)) - check_cached_remote_name(p_search_data, bdname.name, &remote_name_len); - - /* Check EIR for remote name and services */ - if (p_search_data->inq_res.p_eir) - { - BTA_GetEirService(p_search_data->inq_res.p_eir, &services); - BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __func__, (uint32_t)services); - /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */ - } - - { - bt_property_t properties[5]; - bt_device_type_t dev_type; - uint32_t num_properties = 0; - bt_status_t status; - int addr_type = 0; - - memset(properties, 0, sizeof(properties)); - /* BD_ADDR */ - BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], - BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr); - num_properties++; - /* BD_NAME */ - /* Don't send BDNAME if it is empty */ - if (bdname.name[0]) - { - BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], - BT_PROPERTY_BDNAME, - strlen((char *)bdname.name), &bdname); - num_properties++; - } - - /* DEV_CLASS */ - BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], - BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod); - num_properties++; - /* DEV_TYPE */ -#if (BLE_INCLUDED == TRUE) - /* FixMe: Assumption is that bluetooth.h and BTE enums match */ - - /* Verify if the device is dual mode in NVRAM */ - int stored_device_type = 0; - if (btif_get_device_type(bdaddr.address, &stored_device_type) && - ((stored_device_type != BT_DEVICE_TYPE_BREDR && - p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BREDR) || - (stored_device_type != BT_DEVICE_TYPE_BLE && - p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE))) { - dev_type = (bt_device_type_t)BT_DEVICE_TYPE_DUMO; - } else { - dev_type = (bt_device_type_t)p_search_data->inq_res.device_type; - } - - if (p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE) - addr_type = p_search_data->inq_res.ble_addr_type; -#else - dev_type = BT_DEVICE_TYPE_BREDR; -#endif - BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], - BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type); - num_properties++; - /* RSSI */ - BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], - BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t), - &(p_search_data->inq_res.rssi)); - num_properties++; - - status = btif_storage_add_remote_device(&bdaddr, num_properties, properties); - ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status); -#if (BLE_INCLUDED == TRUE) - status = btif_storage_set_remote_addr_type(&bdaddr, addr_type); - ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status); -#endif - /* Callback to notify upper layer of device */ - HAL_CBACK(bt_hal_cbacks, device_found_cb, - num_properties, properties); - } + bdname.name[0] = 0; + + cod = devclass2uint(p_search_data->inq_res.dev_class); + + if (cod == 0) { + LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__); + cod = COD_UNCLASSIFIED; + } + + if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len)) + check_cached_remote_name(p_search_data, bdname.name, &remote_name_len); + + /* Check EIR for remote name and services */ + if (p_search_data->inq_res.p_eir) { + BTA_GetEirService(p_search_data->inq_res.p_eir, &services); + BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __func__, + (uint32_t)services); + /* TODO: Get the service list and check to see which uuids we got and + * send it back to the client. */ + } + + { + bt_property_t properties[5]; + bt_device_type_t dev_type; + uint32_t num_properties = 0; + bt_status_t status; + int addr_type = 0; + + memset(properties, 0, sizeof(properties)); + /* BD_ADDR */ + BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], + BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr); + num_properties++; + /* BD_NAME */ + /* Don't send BDNAME if it is empty */ + if (bdname.name[0]) { + BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], + BT_PROPERTY_BDNAME, + strlen((char*)bdname.name), &bdname); + num_properties++; } - break; - case BTA_DM_INQ_CMPL_EVT: - { + /* DEV_CLASS */ + BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], + BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), + &cod); + num_properties++; +/* DEV_TYPE */ #if (BLE_INCLUDED == TRUE) - tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param; - memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS)); - BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL, - bte_scan_filt_param_cfg_evt, 0); -#endif - } - break; - case BTA_DM_DISC_CMPL_EVT: - { - HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED); + /* FixMe: Assumption is that bluetooth.h and BTE enums match */ + + /* Verify if the device is dual mode in NVRAM */ + int stored_device_type = 0; + if (btif_get_device_type(bdaddr.address, &stored_device_type) && + ((stored_device_type != BT_DEVICE_TYPE_BREDR && + p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BREDR) || + (stored_device_type != BT_DEVICE_TYPE_BLE && + p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE))) { + dev_type = (bt_device_type_t)BT_DEVICE_TYPE_DUMO; + } else { + dev_type = (bt_device_type_t)p_search_data->inq_res.device_type; } - break; - case BTA_DM_SEARCH_CANCEL_CMPL_EVT: - { - /* if inquiry is not in progress and we get a cancel event, then - * it means we are done with inquiry, but remote_name fetches are in - * progress - * - * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt - * but instead wait for the cancel_cmpl_evt via the Busy Level - * - */ - if (btif_dm_inquiry_in_progress == false) - { + + if (p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE) + addr_type = p_search_data->inq_res.ble_addr_type; +#else + dev_type = BT_DEVICE_TYPE_BREDR; +#endif + BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], + BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), + &dev_type); + num_properties++; + /* RSSI */ + BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], + BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t), + &(p_search_data->inq_res.rssi)); + num_properties++; + + status = + btif_storage_add_remote_device(&bdaddr, num_properties, properties); + ASSERTC(status == BT_STATUS_SUCCESS, + "failed to save remote device (inquiry)", status); #if (BLE_INCLUDED == TRUE) - tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param; - memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS)); - BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL, - bte_scan_filt_param_cfg_evt, 0); + status = btif_storage_set_remote_addr_type(&bdaddr, addr_type); + ASSERTC(status == BT_STATUS_SUCCESS, + "failed to save remote addr type (inquiry)", status); #endif - HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED); - } - } - break; - } + /* Callback to notify upper layer of device */ + HAL_CBACK(bt_hal_cbacks, device_found_cb, num_properties, properties); + } + } break; + + case BTA_DM_INQ_CMPL_EVT: { +#if (BLE_INCLUDED == TRUE) + tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param; + memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS)); + BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, + NULL, bte_scan_filt_param_cfg_evt, 0); +#endif + } break; + case BTA_DM_DISC_CMPL_EVT: { + HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, + BT_DISCOVERY_STOPPED); + } break; + case BTA_DM_SEARCH_CANCEL_CMPL_EVT: { + /* if inquiry is not in progress and we get a cancel event, then + * it means we are done with inquiry, but remote_name fetches are in + * progress + * + * if inquiry is in progress, then we don't want to act on this + * cancel_cmpl_evt + * but instead wait for the cancel_cmpl_evt via the Busy Level + * + */ + if (btif_dm_inquiry_in_progress == false) { +#if (BLE_INCLUDED == TRUE) + tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param; + memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS)); + BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, + &adv_filt_param, NULL, + bte_scan_filt_param_cfg_evt, 0); +#endif + HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, + BT_DISCOVERY_STOPPED); + } + } break; + } } /******************************************************************************* -** -** Function btif_dm_search_services_evt -** -** Description Executes search services event in btif context -** -** Returns void -** -*******************************************************************************/ -static void btif_dm_search_services_evt(uint16_t event, char *p_param) -{ - tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param; - - BTIF_TRACE_EVENT("%s: event = %d", __func__, event); - switch (event) - { - case BTA_DM_DISC_RES_EVT: - { - bt_property_t prop; - uint32_t i = 0; - bt_bdaddr_t bd_addr; - bt_status_t ret; - - bdcpy(bd_addr.address, p_data->disc_res.bd_addr); - - BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __func__, - p_data->disc_res.result, p_data->disc_res.services); - if ((p_data->disc_res.result != BTA_SUCCESS) && - (pairing_cb.state == BT_BOND_STATE_BONDING ) && - (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING)) - { - BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __func__); - pairing_cb.sdp_attempts++; - btif_dm_get_remote_services(&bd_addr); - return; - } - prop.type = BT_PROPERTY_UUIDS; - prop.len = 0; - if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) - { - prop.val = p_data->disc_res.p_uuid_list; - prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE; - for (i=0; i < p_data->disc_res.num_uuids; i++) - { - char temp[256]; - uuid_to_string_legacy((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp, - sizeof(temp)); - LOG_INFO(LOG_TAG, "%s index:%d uuid:%s", __func__, i, temp); - } - } - - /* onUuidChanged requires getBondedDevices to be populated. - ** bond_state_changed needs to be sent prior to remote_device_property - */ - if ((pairing_cb.state == BT_BOND_STATE_BONDING) && - ((bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0) || - (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == 0)) && - pairing_cb.sdp_attempts > 0) - { - BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED", - __func__); - pairing_cb.sdp_attempts = 0; - - // If bonding occured due to cross-key pairing, send bonding callback - // for static address now - if (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == 0) - bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); - - bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED); - } - - if (p_data->disc_res.num_uuids != 0) - { - /* Also write this to the NVRAM */ - ret = btif_storage_set_remote_device_property(&bd_addr, &prop); - ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret); - /* Send the event to the BTIF */ - HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, - BT_STATUS_SUCCESS, &bd_addr, 1, &prop); - } + * + * Function btif_dm_search_services_evt + * + * Description Executes search services event in btif context + * + * Returns void + * + ******************************************************************************/ +static void btif_dm_search_services_evt(uint16_t event, char* p_param) { + tBTA_DM_SEARCH* p_data = (tBTA_DM_SEARCH*)p_param; + + BTIF_TRACE_EVENT("%s: event = %d", __func__, event); + switch (event) { + case BTA_DM_DISC_RES_EVT: { + bt_property_t prop; + uint32_t i = 0; + bt_bdaddr_t bd_addr; + bt_status_t ret; + + bdcpy(bd_addr.address, p_data->disc_res.bd_addr); + + BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __func__, + p_data->disc_res.result, p_data->disc_res.services); + if ((p_data->disc_res.result != BTA_SUCCESS) && + (pairing_cb.state == BT_BOND_STATE_BONDING) && + (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING)) { + BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", + __func__); + pairing_cb.sdp_attempts++; + btif_dm_get_remote_services(&bd_addr); + return; + } + prop.type = BT_PROPERTY_UUIDS; + prop.len = 0; + if ((p_data->disc_res.result == BTA_SUCCESS) && + (p_data->disc_res.num_uuids > 0)) { + prop.val = p_data->disc_res.p_uuid_list; + prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE; + for (i = 0; i < p_data->disc_res.num_uuids; i++) { + char temp[256]; + uuid_to_string_legacy( + (bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i * MAX_UUID_SIZE)), + temp, sizeof(temp)); + LOG_INFO(LOG_TAG, "%s index:%d uuid:%s", __func__, i, temp); } - break; + } + + /* onUuidChanged requires getBondedDevices to be populated. + ** bond_state_changed needs to be sent prior to remote_device_property + */ + if ((pairing_cb.state == BT_BOND_STATE_BONDING) && + ((bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0) || + (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == + 0)) && + pairing_cb.sdp_attempts > 0) { + BTIF_TRACE_DEBUG( + "%s Remote Service SDP done. Call bond_state_changed_cb BONDED", + __func__); + pairing_cb.sdp_attempts = 0; + + // If bonding occured due to cross-key pairing, send bonding callback + // for static address now + if (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == + 0) + bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, + BT_BOND_STATE_BONDING); + + bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED); + } + + if (p_data->disc_res.num_uuids != 0) { + /* Also write this to the NVRAM */ + ret = btif_storage_set_remote_device_property(&bd_addr, &prop); + ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", + ret); + /* Send the event to the BTIF */ + HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, BT_STATUS_SUCCESS, + &bd_addr, 1, &prop); + } + } break; + + case BTA_DM_DISC_CMPL_EVT: + /* fixme */ + break; + + case BTA_DM_SEARCH_CANCEL_CMPL_EVT: + /* no-op */ + break; - case BTA_DM_DISC_CMPL_EVT: - /* fixme */ - break; +#if (BLE_INCLUDED == TRUE) + case BTA_DM_DISC_BLE_RES_EVT: { + BTIF_TRACE_DEBUG("%s:, services 0x%x)", __func__, + p_data->disc_ble_res.service.uu.uuid16); + bt_uuid_t uuid; + int i = 0; + int j = 15; + int num_properties = 0; + if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID) { + BTIF_TRACE_DEBUG("%s: Found HOGP UUID", __func__); + bt_property_t prop[2]; + bt_bdaddr_t bd_addr; + char temp[256]; + bt_status_t ret; + + bta_gatt_convert_uuid16_to_uuid128( + uuid.uu, p_data->disc_ble_res.service.uu.uuid16); + + while (i < j) { + unsigned char c = uuid.uu[j]; + uuid.uu[j] = uuid.uu[i]; + uuid.uu[i] = c; + i++; + j--; + } - case BTA_DM_SEARCH_CANCEL_CMPL_EVT: - /* no-op */ - break; + uuid_to_string_legacy(&uuid, temp, sizeof(temp)); + LOG_INFO(LOG_TAG, "%s uuid:%s", __func__, temp); -#if (BLE_INCLUDED == TRUE) - case BTA_DM_DISC_BLE_RES_EVT: - { - BTIF_TRACE_DEBUG("%s:, services 0x%x)", __func__, - p_data->disc_ble_res.service.uu.uuid16); - bt_uuid_t uuid; - int i = 0; - int j = 15; - int num_properties = 0; - if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID) - { - BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__func__); - bt_property_t prop[2]; - bt_bdaddr_t bd_addr; - char temp[256]; - bt_status_t ret; - - bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16); - - while(i < j ) - { - unsigned char c = uuid.uu[j]; - uuid.uu[j] = uuid.uu[i]; - uuid.uu[i] = c; - i++; - j--; - } - - uuid_to_string_legacy(&uuid, temp, sizeof(temp)); - LOG_INFO(LOG_TAG, "%s uuid:%s", __func__, temp); - - bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr); - prop[0].type = BT_PROPERTY_UUIDS; - prop[0].val = uuid.uu; - prop[0].len = MAX_UUID_SIZE; - - /* Also write this to the NVRAM */ - ret = btif_storage_set_remote_device_property(&bd_addr, &prop[0]); - ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret); - num_properties++; - - /* Remote name update */ - if (strnlen((const char *) p_data->disc_ble_res.bd_name, BD_NAME_LEN)) - { - prop[1].type = BT_PROPERTY_BDNAME; - prop[1].val = p_data->disc_ble_res.bd_name; - prop[1].len = strnlen((char *)p_data->disc_ble_res.bd_name, BD_NAME_LEN); - - ret = btif_storage_set_remote_device_property(&bd_addr, &prop[1]); - ASSERTC(ret == BT_STATUS_SUCCESS, "failed to save remote device property", ret); - num_properties++; - } - - /* Send the event to the BTIF */ - HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, - BT_STATUS_SUCCESS, &bd_addr, num_properties, prop); - - } + bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr); + prop[0].type = BT_PROPERTY_UUIDS; + prop[0].val = uuid.uu; + prop[0].len = MAX_UUID_SIZE; + + /* Also write this to the NVRAM */ + ret = btif_storage_set_remote_device_property(&bd_addr, &prop[0]); + ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", + ret); + num_properties++; + + /* Remote name update */ + if (strnlen((const char*)p_data->disc_ble_res.bd_name, BD_NAME_LEN)) { + prop[1].type = BT_PROPERTY_BDNAME; + prop[1].val = p_data->disc_ble_res.bd_name; + prop[1].len = + strnlen((char*)p_data->disc_ble_res.bd_name, BD_NAME_LEN); + + ret = btif_storage_set_remote_device_property(&bd_addr, &prop[1]); + ASSERTC(ret == BT_STATUS_SUCCESS, + "failed to save remote device property", ret); + num_properties++; } - break; + + /* Send the event to the BTIF */ + HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, BT_STATUS_SUCCESS, + &bd_addr, num_properties, prop); + } + } break; #endif /* BLE_INCLUDED */ - default: - { - ASSERTC(0, "unhandled search services event", event); - } - break; - } + default: { ASSERTC(0, "unhandled search services event", event); } break; + } } /******************************************************************************* -** -** Function btif_dm_remote_service_record_evt -** -** Description Executes search service record event in btif context -** -** Returns void -** -*******************************************************************************/ -static void btif_dm_remote_service_record_evt(uint16_t event, char *p_param) -{ - tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param; - - BTIF_TRACE_EVENT("%s: event = %d", __func__, event); - switch (event) - { - case BTA_DM_DISC_RES_EVT: - { - bt_service_record_t rec; - bt_property_t prop; - bt_bdaddr_t bd_addr; - - memset(&rec, 0, sizeof(bt_service_record_t)); - bdcpy(bd_addr.address, p_data->disc_res.bd_addr); - - BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __func__, - p_data->disc_res.result, p_data->disc_res.services); - prop.type = BT_PROPERTY_SERVICE_RECORD; - prop.val = (void*)&rec; - prop.len = sizeof(rec); - - /* disc_res.result is overloaded with SCN. Cannot check result */ - p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK; - /* TODO: Get the UUID as well */ - rec.channel = p_data->disc_res.result - 3; - /* TODO: Need to get the service name using p_raw_data */ - rec.name[0] = 0; - - HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, - BT_STATUS_SUCCESS, &bd_addr, 1, &prop); - } - break; - - default: - { - ASSERTC(0, "unhandled remote service record event", event); - } - break; - } + * + * Function btif_dm_remote_service_record_evt + * + * Description Executes search service record event in btif context + * + * Returns void + * + ******************************************************************************/ +static void btif_dm_remote_service_record_evt(uint16_t event, char* p_param) { + tBTA_DM_SEARCH* p_data = (tBTA_DM_SEARCH*)p_param; + + BTIF_TRACE_EVENT("%s: event = %d", __func__, event); + switch (event) { + case BTA_DM_DISC_RES_EVT: { + bt_service_record_t rec; + bt_property_t prop; + bt_bdaddr_t bd_addr; + + memset(&rec, 0, sizeof(bt_service_record_t)); + bdcpy(bd_addr.address, p_data->disc_res.bd_addr); + + BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __func__, + p_data->disc_res.result, p_data->disc_res.services); + prop.type = BT_PROPERTY_SERVICE_RECORD; + prop.val = (void*)&rec; + prop.len = sizeof(rec); + + /* disc_res.result is overloaded with SCN. Cannot check result */ + p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK; + /* TODO: Get the UUID as well */ + rec.channel = p_data->disc_res.result - 3; + /* TODO: Need to get the service name using p_raw_data */ + rec.name[0] = 0; + + HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, BT_STATUS_SUCCESS, + &bd_addr, 1, &prop); + } break; + + default: { + ASSERTC(0, "unhandled remote service record event", event); + } break; + } } /******************************************************************************* -** -** Function btif_dm_upstreams_cback -** -** Description Executes UPSTREAMS events in btif context -** -** Returns void -** -*******************************************************************************/ -static void btif_dm_upstreams_evt(uint16_t event, char* p_param) -{ - tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param; - tBTA_SERVICE_MASK service_mask; - uint32_t i; - bt_bdaddr_t bd_addr; - - BTIF_TRACE_EVENT("%s: ev: %s", __func__, dump_dm_event(event)); - - switch (event) - { - case BTA_DM_ENABLE_EVT: - { - BD_NAME bdname; - bt_status_t status; - bt_property_t prop; - prop.type = BT_PROPERTY_BDNAME; - prop.len = BD_NAME_LEN; - prop.val = (void*)bdname; - - status = btif_storage_get_adapter_property(&prop); - if (status == BT_STATUS_SUCCESS) - { - /* A name exists in the storage. Make this the device name */ - BTA_DmSetDeviceName((char*)prop.val); - } - else - { - /* Storage does not have a name yet. - * Use the default name and write it to the chip - */ - BTA_DmSetDeviceName(btif_get_default_local_name()); - } + * + * Function btif_dm_upstreams_cback + * + * Description Executes UPSTREAMS events in btif context + * + * Returns void + * + ******************************************************************************/ +static void btif_dm_upstreams_evt(uint16_t event, char* p_param) { + tBTA_DM_SEC* p_data = (tBTA_DM_SEC*)p_param; + tBTA_SERVICE_MASK service_mask; + uint32_t i; + bt_bdaddr_t bd_addr; + + BTIF_TRACE_EVENT("%s: ev: %s", __func__, dump_dm_event(event)); + + switch (event) { + case BTA_DM_ENABLE_EVT: { + BD_NAME bdname; + bt_status_t status; + bt_property_t prop; + prop.type = BT_PROPERTY_BDNAME; + prop.len = BD_NAME_LEN; + prop.val = (void*)bdname; + + status = btif_storage_get_adapter_property(&prop); + if (status == BT_STATUS_SUCCESS) { + /* A name exists in the storage. Make this the device name */ + BTA_DmSetDeviceName((char*)prop.val); + } else { + /* Storage does not have a name yet. + * Use the default name and write it to the chip + */ + BTA_DmSetDeviceName(btif_get_default_local_name()); + } #if (BLE_INCLUDED == TRUE) - /* Enable local privacy */ - BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED); + /* Enable local privacy */ + BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED); #endif - /* for each of the enabled services in the mask, trigger the profile - * enable */ - service_mask = btif_get_enabled_services_mask(); - for (i=0; i <= BTA_MAX_SERVICE_ID; i++) - { - if (service_mask & - (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i))) - { - btif_in_execute_service_request(i, true); - } - } - /* clear control blocks */ - memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t)); - pairing_cb.bond_type = BOND_TYPE_PERSISTENT; - - /* This function will also trigger the adapter_properties_cb - ** and bonded_devices_info_cb - */ - btif_storage_load_bonded_devices(); - - btif_enable_bluetooth_evt(p_data->enable.status); + /* for each of the enabled services in the mask, trigger the profile + * enable */ + service_mask = btif_get_enabled_services_mask(); + for (i = 0; i <= BTA_MAX_SERVICE_ID; i++) { + if (service_mask & + (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i))) { + btif_in_execute_service_request(i, true); } - break; - - case BTA_DM_DISABLE_EVT: - /* for each of the enabled services in the mask, trigger the profile - * disable */ - service_mask = btif_get_enabled_services_mask(); - for (i=0; i <= BTA_MAX_SERVICE_ID; i++) - { - if (service_mask & - (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i))) - { - btif_in_execute_service_request(i, false); - } - } - btif_disable_bluetooth_evt(); - break; - - case BTA_DM_PIN_REQ_EVT: - btif_dm_pin_req_evt(&p_data->pin_req); - break; - - case BTA_DM_AUTH_CMPL_EVT: - btif_dm_auth_cmpl_evt(&p_data->auth_cmpl); - break; - - case BTA_DM_BOND_CANCEL_CMPL_EVT: - if (pairing_cb.state == BT_BOND_STATE_BONDING) - { - bdcpy(bd_addr.address, pairing_cb.bd_addr); - btm_set_bond_type_dev(pairing_cb.bd_addr, BOND_TYPE_UNKNOWN); - bond_state_changed((bt_status_t)p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE); - } - break; - - case BTA_DM_SP_CFM_REQ_EVT: - btif_dm_ssp_cfm_req_evt(&p_data->cfm_req); - break; - case BTA_DM_SP_KEY_NOTIF_EVT: - btif_dm_ssp_key_notif_evt(&p_data->key_notif); - break; - - case BTA_DM_DEV_UNPAIRED_EVT: - bdcpy(bd_addr.address, p_data->link_down.bd_addr); - btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN); - - /*special handling for HID devices */ - #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == true)) - btif_hh_remove_device(bd_addr); - #endif - btif_storage_remove_bonded_device(&bd_addr); - bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE); - break; - - case BTA_DM_BUSY_LEVEL_EVT: - { - - if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK) - { - if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED) - { - HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, - BT_DISCOVERY_STARTED); - btif_dm_inquiry_in_progress = true; - } - else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED) - { - HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, - BT_DISCOVERY_STOPPED); - btif_dm_inquiry_in_progress = false; - } - else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE) - { - btif_dm_inquiry_in_progress = false; - } - } - }break; - - case BTA_DM_LINK_UP_EVT: - bdcpy(bd_addr.address, p_data->link_up.bd_addr); - BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED"); - - btif_update_remote_version_property(&bd_addr); - - HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS, - &bd_addr, BT_ACL_STATE_CONNECTED); - break; - - case BTA_DM_LINK_DOWN_EVT: - bdcpy(bd_addr.address, p_data->link_down.bd_addr); - btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN); - BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED"); - HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS, - &bd_addr, BT_ACL_STATE_DISCONNECTED); - break; - - case BTA_DM_HW_ERROR_EVT: - BTIF_TRACE_ERROR("Received H/W Error. "); - /* Flush storage data */ - btif_config_flush(); - usleep(100000); /* 100milliseconds */ - /* Killing the process to force a restart as part of fault tolerance */ - kill(getpid(), SIGKILL); - break; - -#if (BLE_INCLUDED == TRUE) - case BTA_DM_BLE_KEY_EVT: - BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT key_type=0x%02x ", p_data->ble_key.key_type); - - /* If this pairing is by-product of local initiated GATT client Read or Write, - BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not - have setup properly. Setup pairing_cb and notify App about Bonding state now*/ - if (pairing_cb.state != BT_BOND_STATE_BONDING) - { - BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now"); - bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr, - BT_BOND_STATE_BONDING); - } - else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0) - { - BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type); - break; - } - - switch (p_data->ble_key.key_type) - { - case BTA_LE_KEY_PENC: - BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC"); - pairing_cb.ble.is_penc_key_rcvd = true; - pairing_cb.ble.penc_key = p_data->ble_key.p_key_value->penc_key; - break; - - case BTA_LE_KEY_PID: - BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID"); - pairing_cb.ble.is_pid_key_rcvd = true; - pairing_cb.ble.pid_key = p_data->ble_key.p_key_value->pid_key; - break; - - case BTA_LE_KEY_PCSRK: - BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK"); - pairing_cb.ble.is_pcsrk_key_rcvd = true; - pairing_cb.ble.pcsrk_key = p_data->ble_key.p_key_value->pcsrk_key; - break; - - case BTA_LE_KEY_LENC: - BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC"); - pairing_cb.ble.is_lenc_key_rcvd = true; - pairing_cb.ble.lenc_key = p_data->ble_key.p_key_value->lenc_key; - break; - - case BTA_LE_KEY_LCSRK: - BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK"); - pairing_cb.ble.is_lcsrk_key_rcvd = true; - pairing_cb.ble.lcsrk_key = p_data->ble_key.p_key_value->lcsrk_key; - break; - - case BTA_LE_KEY_LID: - BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LID"); - pairing_cb.ble.is_lidk_key_rcvd = true; - break; - - default: - BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type); - break; - } - break; - case BTA_DM_BLE_SEC_REQ_EVT: - BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. "); - btif_dm_ble_sec_req_evt(&p_data->ble_req); - break; - case BTA_DM_BLE_PASSKEY_NOTIF_EVT: - BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. "); - btif_dm_ble_key_notif_evt(&p_data->key_notif); - break; - case BTA_DM_BLE_PASSKEY_REQ_EVT: - BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. "); - btif_dm_ble_passkey_req_evt(&p_data->pin_req); - break; - case BTA_DM_BLE_NC_REQ_EVT: - BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. "); - btif_dm_ble_key_nc_req_evt(&p_data->key_notif); - break; - case BTA_DM_BLE_OOB_REQ_EVT: - BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. "); - btif_dm_ble_oob_req_evt(&p_data->rmt_oob); - break; - case BTA_DM_BLE_SC_OOB_REQ_EVT: - BTIF_TRACE_DEBUG("BTA_DM_BLE_SC_OOB_REQ_EVT. "); - btif_dm_ble_sc_oob_req_evt(&p_data->rmt_oob); - break; - case BTA_DM_BLE_LOCAL_IR_EVT: - BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. "); - ble_local_key_cb.is_id_keys_rcvd = true; - memcpy(&ble_local_key_cb.id_keys.irk[0], - &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16)); - memcpy(&ble_local_key_cb.id_keys.ir[0], - &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16)); - memcpy(&ble_local_key_cb.id_keys.dhk[0], - &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16)); - btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0], - BTIF_DM_LE_LOCAL_KEY_IRK, - BT_OCTET16_LEN); - btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0], - BTIF_DM_LE_LOCAL_KEY_IR, - BT_OCTET16_LEN); - btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0], - BTIF_DM_LE_LOCAL_KEY_DHK, - BT_OCTET16_LEN); - break; - case BTA_DM_BLE_LOCAL_ER_EVT: - BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. "); - ble_local_key_cb.is_er_rcvd = true; - memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16)); - btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0], - BTIF_DM_LE_LOCAL_KEY_ER, - BT_OCTET16_LEN); - break; - - case BTA_DM_BLE_AUTH_CMPL_EVT: - BTIF_TRACE_DEBUG("BTA_DM_BLE_AUTH_CMPL_EVT. "); - btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl); - break; - - case BTA_DM_LE_FEATURES_READ: - { - tBTM_BLE_VSC_CB cmn_vsc_cb; - bt_local_le_features_t local_le_features; - char buf[512]; - bt_property_t prop; - prop.type = BT_PROPERTY_LOCAL_LE_FEATURES; - prop.val = (void*)buf; - prop.len = sizeof(buf); - - /* LE features are not stored in storage. Should be retrived from stack */ - BTM_BleGetVendorCapabilities(&cmn_vsc_cb); - local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled(); - - prop.len = sizeof (bt_local_le_features_t); - if (cmn_vsc_cb.filter_support == 1) - local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter; - else - local_le_features.max_adv_filter_supported = 0; - local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max; - local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz; - local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading; - local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support; - local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg; - local_le_features.version_supported = cmn_vsc_cb.version_supported; - local_le_features.total_trackable_advertisers = - cmn_vsc_cb.total_trackable_advertisers; - - local_le_features.extended_scan_support = cmn_vsc_cb.extended_scan_support > 0; - local_le_features.debug_logging_supported = cmn_vsc_cb.debug_logging_supported > 0; - - memcpy(prop.val, &local_le_features, prop.len); - HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop); - break; - } - - case BTA_DM_ENER_INFO_READ: - { - btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param; - bt_activity_energy_info energy_info; - energy_info.status = p_ener_data->status; - energy_info.ctrl_state = p_ener_data->ctrl_state; - energy_info.rx_time = p_ener_data->rx_time; - energy_info.tx_time = p_ener_data->tx_time; - energy_info.idle_time = p_ener_data->idle_time; - energy_info.energy_used = p_ener_data->energy_used; - - bt_uid_traffic_t* data = uid_set_read_and_clear(uid_set); - HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info, data); - osi_free(data); - break; + } + /* clear control blocks */ + memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t)); + pairing_cb.bond_type = BOND_TYPE_PERSISTENT; + + /* This function will also trigger the adapter_properties_cb + ** and bonded_devices_info_cb + */ + btif_storage_load_bonded_devices(); + + btif_enable_bluetooth_evt(p_data->enable.status); + } break; + + case BTA_DM_DISABLE_EVT: + /* for each of the enabled services in the mask, trigger the profile + * disable */ + service_mask = btif_get_enabled_services_mask(); + for (i = 0; i <= BTA_MAX_SERVICE_ID; i++) { + if (service_mask & + (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i))) { + btif_in_execute_service_request(i, false); } -#endif + } + btif_disable_bluetooth_evt(); + break; - case BTA_DM_AUTHORIZE_EVT: - case BTA_DM_SIG_STRENGTH_EVT: - case BTA_DM_SP_RMT_OOB_EVT: - case BTA_DM_SP_KEYPRESS_EVT: - case BTA_DM_ROLE_CHG_EVT: + case BTA_DM_PIN_REQ_EVT: + btif_dm_pin_req_evt(&p_data->pin_req); + break; - default: - BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event ); - break; - } - - btif_dm_data_free(event, p_data); -} + case BTA_DM_AUTH_CMPL_EVT: + btif_dm_auth_cmpl_evt(&p_data->auth_cmpl); + break; -/******************************************************************************* -** -** Function btif_dm_generic_evt -** -** Description Executes non-BTA upstream events in BTIF context -** -** Returns void -** -*******************************************************************************/ -static void btif_dm_generic_evt(uint16_t event, char* p_param) -{ - BTIF_TRACE_EVENT("%s: event=%d", __func__, event); - switch(event) - { - case BTIF_DM_CB_DISCOVERY_STARTED: - { - HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED); - } - break; - - case BTIF_DM_CB_CREATE_BOND: - { - pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES; - btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param; - btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport); - } - break; - - case BTIF_DM_CB_REMOVE_BOND: - { - btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param); + case BTA_DM_BOND_CANCEL_CMPL_EVT: + if (pairing_cb.state == BT_BOND_STATE_BONDING) { + bdcpy(bd_addr.address, pairing_cb.bd_addr); + btm_set_bond_type_dev(pairing_cb.bd_addr, BOND_TYPE_UNKNOWN); + bond_state_changed((bt_status_t)p_data->bond_cancel_cmpl.result, + &bd_addr, BT_BOND_STATE_NONE); + } + break; + + case BTA_DM_SP_CFM_REQ_EVT: + btif_dm_ssp_cfm_req_evt(&p_data->cfm_req); + break; + case BTA_DM_SP_KEY_NOTIF_EVT: + btif_dm_ssp_key_notif_evt(&p_data->key_notif); + break; + + case BTA_DM_DEV_UNPAIRED_EVT: + bdcpy(bd_addr.address, p_data->link_down.bd_addr); + btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN); + +/*special handling for HID devices */ +#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == true)) + btif_hh_remove_device(bd_addr); +#endif + btif_storage_remove_bonded_device(&bd_addr); + bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE); + break; + + case BTA_DM_BUSY_LEVEL_EVT: { + if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK) { + if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED) { + HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, + BT_DISCOVERY_STARTED); + btif_dm_inquiry_in_progress = true; + } else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED) { + HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, + BT_DISCOVERY_STOPPED); + btif_dm_inquiry_in_progress = false; + } else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE) { + btif_dm_inquiry_in_progress = false; } - break; + } + } break; + + case BTA_DM_LINK_UP_EVT: + bdcpy(bd_addr.address, p_data->link_up.bd_addr); + BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED"); + + btif_update_remote_version_property(&bd_addr); + + HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS, + &bd_addr, BT_ACL_STATE_CONNECTED); + break; + + case BTA_DM_LINK_DOWN_EVT: + bdcpy(bd_addr.address, p_data->link_down.bd_addr); + btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN); + BTIF_TRACE_DEBUG( + "BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED"); + HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS, + &bd_addr, BT_ACL_STATE_DISCONNECTED); + break; + + case BTA_DM_HW_ERROR_EVT: + BTIF_TRACE_ERROR("Received H/W Error. "); + /* Flush storage data */ + btif_config_flush(); + usleep(100000); /* 100milliseconds */ + /* Killing the process to force a restart as part of fault tolerance */ + kill(getpid(), SIGKILL); + break; - case BTIF_DM_CB_HID_REMOTE_NAME: - { - btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param); - } +#if (BLE_INCLUDED == TRUE) + case BTA_DM_BLE_KEY_EVT: + BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT key_type=0x%02x ", + p_data->ble_key.key_type); + + /* If this pairing is by-product of local initiated GATT client Read or + Write, + BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would + not + have setup properly. Setup pairing_cb and notify App about Bonding state + now*/ + if (pairing_cb.state != BT_BOND_STATE_BONDING) { + BTIF_TRACE_DEBUG( + "Bond state not sent to App so far.Notify the app now"); + bond_state_changed(BT_STATUS_SUCCESS, + (bt_bdaddr_t*)p_data->ble_key.bd_addr, + BT_BOND_STATE_BONDING); + } else if (memcmp(pairing_cb.bd_addr, p_data->ble_key.bd_addr, + BD_ADDR_LEN) != 0) { + BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ", + p_data->ble_key.key_type); break; + } + + switch (p_data->ble_key.key_type) { + case BTA_LE_KEY_PENC: + BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC"); + pairing_cb.ble.is_penc_key_rcvd = true; + pairing_cb.ble.penc_key = p_data->ble_key.p_key_value->penc_key; + break; + + case BTA_LE_KEY_PID: + BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID"); + pairing_cb.ble.is_pid_key_rcvd = true; + pairing_cb.ble.pid_key = p_data->ble_key.p_key_value->pid_key; + break; + + case BTA_LE_KEY_PCSRK: + BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK"); + pairing_cb.ble.is_pcsrk_key_rcvd = true; + pairing_cb.ble.pcsrk_key = p_data->ble_key.p_key_value->pcsrk_key; + break; + + case BTA_LE_KEY_LENC: + BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC"); + pairing_cb.ble.is_lenc_key_rcvd = true; + pairing_cb.ble.lenc_key = p_data->ble_key.p_key_value->lenc_key; + break; + + case BTA_LE_KEY_LCSRK: + BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK"); + pairing_cb.ble.is_lcsrk_key_rcvd = true; + pairing_cb.ble.lcsrk_key = p_data->ble_key.p_key_value->lcsrk_key; + break; + + case BTA_LE_KEY_LID: + BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LID"); + pairing_cb.ble.is_lidk_key_rcvd = true; + break; - case BTIF_DM_CB_BOND_STATE_BONDING: - { - bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING); - } - break; - case BTIF_DM_CB_LE_TX_TEST: - case BTIF_DM_CB_LE_RX_TEST: - { - uint8_t status; - STREAM_TO_UINT8(status, p_param); - HAL_CBACK(bt_hal_cbacks, le_test_mode_cb, - (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0); - } - break; - case BTIF_DM_CB_LE_TEST_END: - { - uint8_t status; - uint16_t count = 0; - STREAM_TO_UINT8(status, p_param); - if (status == 0) - STREAM_TO_UINT16(count, p_param); - HAL_CBACK(bt_hal_cbacks, le_test_mode_cb, - (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count); - } - break; default: - { - BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __func__, event); - } - break; + BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", + p_data->ble_key.key_type); + break; + } + break; + case BTA_DM_BLE_SEC_REQ_EVT: + BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. "); + btif_dm_ble_sec_req_evt(&p_data->ble_req); + break; + case BTA_DM_BLE_PASSKEY_NOTIF_EVT: + BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. "); + btif_dm_ble_key_notif_evt(&p_data->key_notif); + break; + case BTA_DM_BLE_PASSKEY_REQ_EVT: + BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. "); + btif_dm_ble_passkey_req_evt(&p_data->pin_req); + break; + case BTA_DM_BLE_NC_REQ_EVT: + BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. "); + btif_dm_ble_key_nc_req_evt(&p_data->key_notif); + break; + case BTA_DM_BLE_OOB_REQ_EVT: + BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. "); + btif_dm_ble_oob_req_evt(&p_data->rmt_oob); + break; + case BTA_DM_BLE_SC_OOB_REQ_EVT: + BTIF_TRACE_DEBUG("BTA_DM_BLE_SC_OOB_REQ_EVT. "); + btif_dm_ble_sc_oob_req_evt(&p_data->rmt_oob); + break; + case BTA_DM_BLE_LOCAL_IR_EVT: + BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. "); + ble_local_key_cb.is_id_keys_rcvd = true; + memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], + sizeof(BT_OCTET16)); + memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], + sizeof(BT_OCTET16)); + memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], + sizeof(BT_OCTET16)); + btif_storage_add_ble_local_key((char*)&ble_local_key_cb.id_keys.irk[0], + BTIF_DM_LE_LOCAL_KEY_IRK, BT_OCTET16_LEN); + btif_storage_add_ble_local_key((char*)&ble_local_key_cb.id_keys.ir[0], + BTIF_DM_LE_LOCAL_KEY_IR, BT_OCTET16_LEN); + btif_storage_add_ble_local_key((char*)&ble_local_key_cb.id_keys.dhk[0], + BTIF_DM_LE_LOCAL_KEY_DHK, BT_OCTET16_LEN); + break; + case BTA_DM_BLE_LOCAL_ER_EVT: + BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. "); + ble_local_key_cb.is_er_rcvd = true; + memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16)); + btif_storage_add_ble_local_key((char*)&ble_local_key_cb.er[0], + BTIF_DM_LE_LOCAL_KEY_ER, BT_OCTET16_LEN); + break; + + case BTA_DM_BLE_AUTH_CMPL_EVT: + BTIF_TRACE_DEBUG("BTA_DM_BLE_AUTH_CMPL_EVT. "); + btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl); + break; + + case BTA_DM_LE_FEATURES_READ: { + tBTM_BLE_VSC_CB cmn_vsc_cb; + bt_local_le_features_t local_le_features; + char buf[512]; + bt_property_t prop; + prop.type = BT_PROPERTY_LOCAL_LE_FEATURES; + prop.val = (void*)buf; + prop.len = sizeof(buf); + + /* LE features are not stored in storage. Should be retrived from stack */ + BTM_BleGetVendorCapabilities(&cmn_vsc_cb); + local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled(); + + prop.len = sizeof(bt_local_le_features_t); + if (cmn_vsc_cb.filter_support == 1) + local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter; + else + local_le_features.max_adv_filter_supported = 0; + local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max; + local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz; + local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading; + local_le_features.activity_energy_info_supported = + cmn_vsc_cb.energy_support; + local_le_features.scan_result_storage_size = + cmn_vsc_cb.tot_scan_results_strg; + local_le_features.version_supported = cmn_vsc_cb.version_supported; + local_le_features.total_trackable_advertisers = + cmn_vsc_cb.total_trackable_advertisers; + + local_le_features.extended_scan_support = + cmn_vsc_cb.extended_scan_support > 0; + local_le_features.debug_logging_supported = + cmn_vsc_cb.debug_logging_supported > 0; + + memcpy(prop.val, &local_le_features, prop.len); + HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, + &prop); + break; + } + + case BTA_DM_ENER_INFO_READ: { + btif_activity_energy_info_cb_t* p_ener_data = + (btif_activity_energy_info_cb_t*)p_param; + bt_activity_energy_info energy_info; + energy_info.status = p_ener_data->status; + energy_info.ctrl_state = p_ener_data->ctrl_state; + energy_info.rx_time = p_ener_data->rx_time; + energy_info.tx_time = p_ener_data->tx_time; + energy_info.idle_time = p_ener_data->idle_time; + energy_info.energy_used = p_ener_data->energy_used; + + bt_uid_traffic_t* data = uid_set_read_and_clear(uid_set); + HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info, data); + osi_free(data); + break; } -} +#endif -/******************************************************************************* -** -** Function bte_dm_evt -** -** Description Switches context from BTE to BTIF for all DM events -** -** Returns void -** -*******************************************************************************/ + case BTA_DM_AUTHORIZE_EVT: + case BTA_DM_SIG_STRENGTH_EVT: + case BTA_DM_SP_RMT_OOB_EVT: + case BTA_DM_SP_KEYPRESS_EVT: + case BTA_DM_ROLE_CHG_EVT: -void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data) -{ - /* switch context to btif task context (copy full union size for convenience) */ - bt_status_t status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, - (char*)p_data, sizeof(tBTA_DM_SEC), btif_dm_data_copy); + default: + BTIF_TRACE_WARNING("btif_dm_cback : unhandled event (%d)", event); + break; + } - /* catch any failed context transfers */ - ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); + btif_dm_data_free(event, p_data); } /******************************************************************************* -** -** Function bte_search_devices_evt -** -** Description Switches context from BTE to BTIF for DM search events -** -** Returns void -** -*******************************************************************************/ -static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data) -{ - uint16_t param_len = 0; - - if (p_data) - param_len += sizeof(tBTA_DM_SEARCH); - /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */ - switch (event) - { - case BTA_DM_INQ_RES_EVT: - { - if (p_data->inq_res.p_eir) - param_len += HCI_EXT_INQ_RESPONSE_LEN; - } - break; + * + * Function btif_dm_generic_evt + * + * Description Executes non-BTA upstream events in BTIF context + * + * Returns void + * + ******************************************************************************/ +static void btif_dm_generic_evt(uint16_t event, char* p_param) { + BTIF_TRACE_EVENT("%s: event=%d", __func__, event); + switch (event) { + case BTIF_DM_CB_DISCOVERY_STARTED: { + HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, + BT_DISCOVERY_STARTED); + } break; + + case BTIF_DM_CB_CREATE_BOND: { + pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES; + btif_dm_create_bond_cb_t* create_bond_cb = + (btif_dm_create_bond_cb_t*)p_param; + btif_dm_cb_create_bond(&create_bond_cb->bdaddr, + create_bond_cb->transport); + } break; + + case BTIF_DM_CB_REMOVE_BOND: { + btif_dm_cb_remove_bond((bt_bdaddr_t*)p_param); + } break; + + case BTIF_DM_CB_HID_REMOTE_NAME: { + btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME*)p_param); + } break; + + case BTIF_DM_CB_BOND_STATE_BONDING: { + bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_param, + BT_BOND_STATE_BONDING); + } break; + case BTIF_DM_CB_LE_TX_TEST: + case BTIF_DM_CB_LE_RX_TEST: { + uint8_t status; + STREAM_TO_UINT8(status, p_param); + HAL_CBACK(bt_hal_cbacks, le_test_mode_cb, + (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0); + } break; + case BTIF_DM_CB_LE_TEST_END: { + uint8_t status; + uint16_t count = 0; + STREAM_TO_UINT8(status, p_param); + if (status == 0) STREAM_TO_UINT16(count, p_param); + HAL_CBACK(bt_hal_cbacks, le_test_mode_cb, + (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count); + } break; + default: { + BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __func__, event); + } break; + } +} - case BTA_DM_DISC_RES_EVT: - { - if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data) - param_len += p_data->disc_res.raw_data_size; - } - break; - } - BTIF_TRACE_DEBUG("%s event=%s param_len=%d", __func__, dump_dm_search_event(event), param_len); +/******************************************************************************* + * + * Function bte_dm_evt + * + * Description Switches context from BTE to BTIF for all DM events + * + * Returns void + * + ******************************************************************************/ + +void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC* p_data) { + /* switch context to btif task context (copy full union size for convenience) + */ + bt_status_t status = btif_transfer_context( + btif_dm_upstreams_evt, (uint16_t)event, (char*)p_data, + sizeof(tBTA_DM_SEC), btif_dm_data_copy); - /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */ - if (event == BTA_DM_INQ_RES_EVT) - p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL); + /* catch any failed context transfers */ + ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); +} - btif_transfer_context (btif_dm_search_devices_evt , (uint16_t) event, (char*)p_data, param_len, - (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL); +/******************************************************************************* + * + * Function bte_search_devices_evt + * + * Description Switches context from BTE to BTIF for DM search events + * + * Returns void + * + ******************************************************************************/ +static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, + tBTA_DM_SEARCH* p_data) { + uint16_t param_len = 0; + + if (p_data) param_len += sizeof(tBTA_DM_SEARCH); + /* Allocate buffer to hold the pointers (deep copy). The pointers will point + * to the end of the tBTA_DM_SEARCH */ + switch (event) { + case BTA_DM_INQ_RES_EVT: { + if (p_data->inq_res.p_eir) param_len += HCI_EXT_INQ_RESPONSE_LEN; + } break; + + case BTA_DM_DISC_RES_EVT: { + if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data) + param_len += p_data->disc_res.raw_data_size; + } break; + } + BTIF_TRACE_DEBUG("%s event=%s param_len=%d", __func__, + dump_dm_search_event(event), param_len); + + /* if remote name is available in EIR, set teh flag so that stack doesnt + * trigger RNR */ + if (event == BTA_DM_INQ_RES_EVT) + p_data->inq_res.remt_name_not_required = + check_eir_remote_name(p_data, NULL, NULL); + + btif_transfer_context( + btif_dm_search_devices_evt, (uint16_t)event, (char*)p_data, param_len, + (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL); } /******************************************************************************* -** -** Function bte_dm_search_services_evt -** -** Description Switches context from BTE to BTIF for DM search services -** event -** -** Returns void -** -*******************************************************************************/ -static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data) -{ - uint16_t param_len = 0; - if (p_data) - param_len += sizeof(tBTA_DM_SEARCH); - switch (event) - { - case BTA_DM_DISC_RES_EVT: - { - if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) { - param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE); - } - } break; - } - /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure - * if raw_data is needed. */ - btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len, - (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL); + * + * Function bte_dm_search_services_evt + * + * Description Switches context from BTE to BTIF for DM search services + * event + * + * Returns void + * + ******************************************************************************/ +static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, + tBTA_DM_SEARCH* p_data) { + uint16_t param_len = 0; + if (p_data) param_len += sizeof(tBTA_DM_SEARCH); + switch (event) { + case BTA_DM_DISC_RES_EVT: { + if ((p_data->disc_res.result == BTA_SUCCESS) && + (p_data->disc_res.num_uuids > 0)) { + param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE); + } + } break; + } + /* TODO: The only other member that needs a deep copy is the p_raw_data. But + * not sure + * if raw_data is needed. */ + btif_transfer_context( + btif_dm_search_services_evt, event, (char*)p_data, param_len, + (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL); } /******************************************************************************* -** -** Function bte_dm_remote_service_record_evt -** -** Description Switches context from BTE to BTIF for DM search service -** record event -** -** Returns void -** -*******************************************************************************/ -static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data) -{ - /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */ - btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL); + * + * Function bte_dm_remote_service_record_evt + * + * Description Switches context from BTE to BTIF for DM search service + * record event + * + * Returns void + * + ******************************************************************************/ +static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, + tBTA_DM_SEARCH* p_data) { + /* TODO: The only member that needs a deep copy is the p_raw_data. But not + * sure yet if this is needed. */ + btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, + sizeof(tBTA_DM_SEARCH), NULL); } #if (BLE_INCLUDED == TRUE) /******************************************************************************* -** -** Function bta_energy_info_cb -** -** Description Switches context from BTE to BTIF for DM energy info event -** -** Returns void -** -*******************************************************************************/ -static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time, - tBTA_DM_BLE_IDLE_TIME_MS idle_time, - tBTA_DM_BLE_ENERGY_USED energy_used, - tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status) -{ - BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld", - status, ctrl_state, tx_time, rx_time, idle_time, energy_used); - - btif_activity_energy_info_cb_t btif_cb; - btif_cb.status = status; - btif_cb.ctrl_state = ctrl_state; - btif_cb.tx_time = (uint64_t) tx_time; - btif_cb.rx_time = (uint64_t) rx_time; - btif_cb.idle_time =(uint64_t) idle_time; - btif_cb.energy_used =(uint64_t) energy_used; - btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ, - (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL); + * + * Function bta_energy_info_cb + * + * Description Switches context from BTE to BTIF for DM energy info event + * + * Returns void + * + ******************************************************************************/ +static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, + tBTA_DM_BLE_RX_TIME_MS rx_time, + tBTA_DM_BLE_IDLE_TIME_MS idle_time, + tBTA_DM_BLE_ENERGY_USED energy_used, + tBTA_DM_CONTRL_STATE ctrl_state, + tBTA_STATUS status) { + BTIF_TRACE_DEBUG( + "energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, " + "idle_time=%ld,used=%ld", + status, ctrl_state, tx_time, rx_time, idle_time, energy_used); + + btif_activity_energy_info_cb_t btif_cb; + btif_cb.status = status; + btif_cb.ctrl_state = ctrl_state; + btif_cb.tx_time = (uint64_t)tx_time; + btif_cb.rx_time = (uint64_t)rx_time; + btif_cb.idle_time = (uint64_t)idle_time; + btif_cb.energy_used = (uint64_t)energy_used; + btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ, + (char*)&btif_cb, sizeof(btif_activity_energy_info_cb_t), + NULL); } #endif /******************************************************************************* -** -** Function bte_scan_filt_param_cfg_evt -** -** Description Scan filter param config event -** -** Returns void -** -*******************************************************************************/ + * + * Function bte_scan_filt_param_cfg_evt + * + * Description Scan filter param config event + * + * Returns void + * + ******************************************************************************/ static void bte_scan_filt_param_cfg_evt(uint8_t action_type, tBTA_DM_BLE_PF_AVBL_SPACE avbl_space, - tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status) -{ - /* This event occurs on calling BTA_DmBleCfgFilterCondition internally, - ** and that is why there is no HAL callback - */ - if(BTA_SUCCESS != status) - { - BTIF_TRACE_ERROR("%s, %d", __func__, status); - } - else - { - BTIF_TRACE_DEBUG("%s", __func__); - } + tBTA_DM_BLE_REF_VALUE ref_value, + tBTA_STATUS status) { + /* This event occurs on calling BTA_DmBleCfgFilterCondition internally, + ** and that is why there is no HAL callback + */ + if (BTA_SUCCESS != status) { + BTIF_TRACE_ERROR("%s, %d", __func__, status); + } else { + BTIF_TRACE_DEBUG("%s", __func__); + } } /***************************************************************************** -** -** btif api functions (no context switch) -** -*****************************************************************************/ + * + * btif api functions (no context switch) + * + ****************************************************************************/ /******************************************************************************* -** -** Function btif_dm_start_discovery -** -** Description Start device discovery/inquiry -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_start_discovery(void) -{ - tBTA_DM_INQ inq_params; - tBTA_SERVICE_MASK services = 0; - tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param; - - BTIF_TRACE_EVENT("%s", __func__); + * + * Function btif_dm_start_discovery + * + * Description Start device discovery/inquiry + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_start_discovery(void) { + tBTA_DM_INQ inq_params; + tBTA_SERVICE_MASK services = 0; + tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param; + + BTIF_TRACE_EVENT("%s", __func__); #if (BLE_INCLUDED == TRUE) - memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS)); - /* Cleanup anything remaining on index 0 */ - BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL, - bte_scan_filt_param_cfg_evt, 0); - - /* Add an allow-all filter on index 0*/ - adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE; - adv_filt_param.feat_seln = ALLOW_ALL_FILTER; - adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR; - adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR; - adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE; - adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE; - BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL, - bte_scan_filt_param_cfg_evt, 0); - - /* TODO: Do we need to handle multiple inquiries at the same time? */ - - /* Set inquiry params and call API */ - inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY; + memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS)); + /* Cleanup anything remaining on index 0 */ + BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, + NULL, bte_scan_filt_param_cfg_evt, 0); + + /* Add an allow-all filter on index 0*/ + adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE; + adv_filt_param.feat_seln = ALLOW_ALL_FILTER; + adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR; + adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR; + adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE; + adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE; + BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL, + bte_scan_filt_param_cfg_evt, 0); + + /* TODO: Do we need to handle multiple inquiries at the same time? */ + + /* Set inquiry params and call API */ + inq_params.mode = BTA_DM_GENERAL_INQUIRY | BTA_BLE_GENERAL_INQUIRY; #if (BTA_HOST_INTERLEAVE_SEARCH == TRUE) - inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE; - inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE; - inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO; - inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO; + inq_params.intl_duration[0] = BTIF_DM_INTERLEAVE_DURATION_BR_ONE; + inq_params.intl_duration[1] = BTIF_DM_INTERLEAVE_DURATION_LE_ONE; + inq_params.intl_duration[2] = BTIF_DM_INTERLEAVE_DURATION_BR_TWO; + inq_params.intl_duration[3] = BTIF_DM_INTERLEAVE_DURATION_LE_TWO; #endif #else - inq_params.mode = BTA_DM_GENERAL_INQUIRY; + inq_params.mode = BTA_DM_GENERAL_INQUIRY; #endif - inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION; + inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION; - inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS; - inq_params.report_dup = true; + inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS; + inq_params.report_dup = true; - inq_params.filter_type = BTA_DM_INQ_CLR; - /* TODO: Filter device by BDA needs to be implemented here */ + inq_params.filter_type = BTA_DM_INQ_CLR; + /* TODO: Filter device by BDA needs to be implemented here */ - /* Will be enabled to true once inquiry busy level has been received */ - btif_dm_inquiry_in_progress = false; - /* find nearby devices */ - BTA_DmSearch(&inq_params, services, bte_search_devices_evt); + /* Will be enabled to true once inquiry busy level has been received */ + btif_dm_inquiry_in_progress = false; + /* find nearby devices */ + BTA_DmSearch(&inq_params, services, bte_search_devices_evt); - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_dm_cancel_discovery -** -** Description Cancels search -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_cancel_discovery(void) -{ - BTIF_TRACE_EVENT("%s", __func__); - BTA_DmSearchCancel(); - return BT_STATUS_SUCCESS; + * + * Function btif_dm_cancel_discovery + * + * Description Cancels search + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_cancel_discovery(void) { + BTIF_TRACE_EVENT("%s", __func__); + BTA_DmSearchCancel(); + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_dm_create_bond -** -** Description Initiate bonding with the specified device -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport) -{ - btif_dm_create_bond_cb_t create_bond_cb; - create_bond_cb.transport = transport; - bdcpy(create_bond_cb.bdaddr.address, bd_addr->address); + * + * Function btif_dm_create_bond + * + * Description Initiate bonding with the specified device + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_create_bond(const bt_bdaddr_t* bd_addr, int transport) { + btif_dm_create_bond_cb_t create_bond_cb; + create_bond_cb.transport = transport; + bdcpy(create_bond_cb.bdaddr.address, bd_addr->address); - bdstr_t bdstr; - BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __func__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport); - if (pairing_cb.state != BT_BOND_STATE_NONE) - return BT_STATUS_BUSY; + bdstr_t bdstr; + BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __func__, + bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport); + if (pairing_cb.state != BT_BOND_STATE_NONE) return BT_STATUS_BUSY; - btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_CREATE_BOND, pairing_cb.state); + btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_CREATE_BOND, + pairing_cb.state); - btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND, - (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL); + btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND, + (char*)&create_bond_cb, + sizeof(btif_dm_create_bond_cb_t), NULL); - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_dm_create_bond_out_of_band -** -** Description Initiate bonding with the specified device using out of band data -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_create_bond_out_of_band(const bt_bdaddr_t *bd_addr, int transport, const bt_out_of_band_data_t *oob_data) -{ - bdcpy(oob_cb.bdaddr, bd_addr->address); - memcpy(&oob_cb.oob_data, oob_data, sizeof(bt_out_of_band_data_t)); - - bdstr_t bdstr; - BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __func__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport); - return btif_dm_create_bond(bd_addr, transport); + * + * Function btif_dm_create_bond_out_of_band + * + * Description Initiate bonding with the specified device using out of band + *data + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_create_bond_out_of_band( + const bt_bdaddr_t* bd_addr, int transport, + const bt_out_of_band_data_t* oob_data) { + bdcpy(oob_cb.bdaddr, bd_addr->address); + memcpy(&oob_cb.oob_data, oob_data, sizeof(bt_out_of_band_data_t)); + + bdstr_t bdstr; + BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __func__, + bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport); + return btif_dm_create_bond(bd_addr, transport); } /******************************************************************************* -** -** Function btif_dm_cancel_bond -** -** Description Initiate bonding with the specified device -** -** Returns bt_status_t -** -*******************************************************************************/ - -bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr) -{ - bdstr_t bdstr; - - BTIF_TRACE_EVENT("%s: bd_addr=%s", __func__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr))); - - btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_CANCEL_BOND, pairing_cb.state); - - /* TODO: - ** 1. Restore scan modes - ** 2. special handling for HID devices - */ - if (pairing_cb.state == BT_BOND_STATE_BONDING) - { + * + * Function btif_dm_cancel_bond + * + * Description Initiate bonding with the specified device + * + * Returns bt_status_t + * + ******************************************************************************/ + +bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t* bd_addr) { + bdstr_t bdstr; + + BTIF_TRACE_EVENT("%s: bd_addr=%s", __func__, + bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr))); + + btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_CANCEL_BOND, + pairing_cb.state); + /* TODO: + ** 1. Restore scan modes + ** 2. special handling for HID devices + */ + if (pairing_cb.state == BT_BOND_STATE_BONDING) { #if (BLE_INCLUDED == TRUE) - if (pairing_cb.is_ssp) - { - if (pairing_cb.is_le_only) - { - BTA_DmBleSecurityGrant((uint8_t *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT); - } - else - { - BTA_DmConfirm( (uint8_t *)bd_addr->address, false); - BTA_DmBondCancel ((uint8_t *)bd_addr->address); - btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr); - } - } - else - { - if (pairing_cb.is_le_only) - { - BTA_DmBondCancel ((uint8_t *)bd_addr->address); - } - else - { - BTA_DmPinReply( (uint8_t *)bd_addr->address, false, 0, NULL); - } - /* Cancel bonding, in case it is in ACL connection setup state */ - BTA_DmBondCancel ((uint8_t *)bd_addr->address); - } + if (pairing_cb.is_ssp) { + if (pairing_cb.is_le_only) { + BTA_DmBleSecurityGrant((uint8_t*)bd_addr->address, + BTA_DM_SEC_PAIR_NOT_SPT); + } else { + BTA_DmConfirm((uint8_t*)bd_addr->address, false); + BTA_DmBondCancel((uint8_t*)bd_addr->address); + btif_storage_remove_bonded_device((bt_bdaddr_t*)bd_addr); + } + } else { + if (pairing_cb.is_le_only) { + BTA_DmBondCancel((uint8_t*)bd_addr->address); + } else { + BTA_DmPinReply((uint8_t*)bd_addr->address, false, 0, NULL); + } + /* Cancel bonding, in case it is in ACL connection setup state */ + BTA_DmBondCancel((uint8_t*)bd_addr->address); + } #else - if (pairing_cb.is_ssp) - { - BTA_DmConfirm( (uint8_t *)bd_addr->address, false); - } - else - { - BTA_DmPinReply( (uint8_t *)bd_addr->address, false, 0, NULL); - } - /* Cancel bonding, in case it is in ACL connection setup state */ - BTA_DmBondCancel ((uint8_t *)bd_addr->address); - btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr); -#endif + if (pairing_cb.is_ssp) { + BTA_DmConfirm((uint8_t*)bd_addr->address, false); + } else { + BTA_DmPinReply((uint8_t*)bd_addr->address, false, 0, NULL); } + /* Cancel bonding, in case it is in ACL connection setup state */ + BTA_DmBondCancel((uint8_t*)bd_addr->address); + btif_storage_remove_bonded_device((bt_bdaddr_t*)bd_addr); +#endif + } - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_dm_hh_open_failed -** -** Description informs the upper layers if the HH have failed during bonding -** -** Returns none -** -*******************************************************************************/ - -void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr) -{ - if (pairing_cb.state == BT_BOND_STATE_BONDING && - bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0) - { - bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE); - } + * + * Function btif_dm_hh_open_failed + * + * Description informs the upper layers if the HH have failed during + *bonding + * + * Returns none + * + ******************************************************************************/ + +void btif_dm_hh_open_failed(bt_bdaddr_t* bdaddr) { + if (pairing_cb.state == BT_BOND_STATE_BONDING && + bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0) { + bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE); + } } /******************************************************************************* -** -** Function btif_dm_remove_bond -** -** Description Removes bonding with the specified device -** -** Returns bt_status_t -** -*******************************************************************************/ + * + * Function btif_dm_remove_bond + * + * Description Removes bonding with the specified device + * + * Returns bt_status_t + * + ******************************************************************************/ -bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr) -{ - bdstr_t bdstr; +bt_status_t btif_dm_remove_bond(const bt_bdaddr_t* bd_addr) { + bdstr_t bdstr; - BTIF_TRACE_EVENT("%s: bd_addr=%s", __func__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr))); + BTIF_TRACE_EVENT("%s: bd_addr=%s", __func__, + bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr))); - btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_REMOVE_BOND, pairing_cb.state); + btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_REMOVE_BOND, + pairing_cb.state); - btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND, - (char *)bd_addr, sizeof(bt_bdaddr_t), NULL); + btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND, + (char*)bd_addr, sizeof(bt_bdaddr_t), NULL); - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_dm_pin_reply -** -** Description BT legacy pairing - PIN code reply -** -** Returns bt_status_t -** -*******************************************************************************/ - -bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept, - uint8_t pin_len, bt_pin_code_t *pin_code) -{ - BTIF_TRACE_EVENT("%s: accept=%d", __func__, accept); - if (pin_code == NULL || pin_len > PIN_CODE_LEN) - return BT_STATUS_FAIL; -#if (BLE_INCLUDED == TRUE) + * + * Function btif_dm_pin_reply + * + * Description BT legacy pairing - PIN code reply + * + * Returns bt_status_t + * + ******************************************************************************/ - if (pairing_cb.is_le_only) - { - int i; - uint32_t passkey = 0; - int multi[] = {100000, 10000, 1000, 100, 10,1}; - BD_ADDR remote_bd_addr; - bdcpy(remote_bd_addr, bd_addr->address); - for (i = 0; i < 6; i++) - { - passkey += (multi[i] * (pin_code->pin[i] - '0')); - } - BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey); - BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey); +bt_status_t btif_dm_pin_reply(const bt_bdaddr_t* bd_addr, uint8_t accept, + uint8_t pin_len, bt_pin_code_t* pin_code) { + BTIF_TRACE_EVENT("%s: accept=%d", __func__, accept); + if (pin_code == NULL || pin_len > PIN_CODE_LEN) return BT_STATUS_FAIL; +#if (BLE_INCLUDED == TRUE) - } - else - { - BTA_DmPinReply( (uint8_t *)bd_addr->address, accept, pin_len, pin_code->pin); - if (accept) - pairing_cb.pin_code_len = pin_len; - } + if (pairing_cb.is_le_only) { + int i; + uint32_t passkey = 0; + int multi[] = {100000, 10000, 1000, 100, 10, 1}; + BD_ADDR remote_bd_addr; + bdcpy(remote_bd_addr, bd_addr->address); + for (i = 0; i < 6; i++) { + passkey += (multi[i] * (pin_code->pin[i] - '0')); + } + BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey); + BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey); + + } else { + BTA_DmPinReply((uint8_t*)bd_addr->address, accept, pin_len, pin_code->pin); + if (accept) pairing_cb.pin_code_len = pin_len; + } #else - BTA_DmPinReply( (uint8_t *)bd_addr->address, accept, pin_len, pin_code->pin); + BTA_DmPinReply((uint8_t*)bd_addr->address, accept, pin_len, pin_code->pin); - if (accept) - pairing_cb.pin_code_len = pin_len; + if (accept) pairing_cb.pin_code_len = pin_len; #endif - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_dm_ssp_reply -** -** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr, - bt_ssp_variant_t variant, uint8_t accept, - uint32_t passkey) -{ - UNUSED(passkey); - - if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY) - { - /* This is not implemented in the stack. - * For devices with display, this is not needed - */ - BTIF_TRACE_WARNING("%s: Not implemented", __func__); - return BT_STATUS_FAIL; - } - /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */ - BTIF_TRACE_EVENT("%s: accept=%d", __func__, accept); + * + * Function btif_dm_ssp_reply + * + * Description BT SSP Reply - Just Works, Numeric Comparison & Passkey + *Entry + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t* bd_addr, + bt_ssp_variant_t variant, uint8_t accept, + uint32_t passkey) { + UNUSED(passkey); + + if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY) { + /* This is not implemented in the stack. + * For devices with display, this is not needed + */ + BTIF_TRACE_WARNING("%s: Not implemented", __func__); + return BT_STATUS_FAIL; + } + /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */ + BTIF_TRACE_EVENT("%s: accept=%d", __func__, accept); #if (BLE_INCLUDED == TRUE) - if (pairing_cb.is_le_only) - { - if(pairing_cb.is_le_nc) - { - BTA_DmBleConfirmReply((uint8_t *)bd_addr->address,accept); - } else { - if (accept) - BTA_DmBleSecurityGrant((uint8_t *)bd_addr->address,BTA_DM_SEC_GRANTED); - else - BTA_DmBleSecurityGrant((uint8_t *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT); - } + if (pairing_cb.is_le_only) { + if (pairing_cb.is_le_nc) { + BTA_DmBleConfirmReply((uint8_t*)bd_addr->address, accept); } else { - BTA_DmConfirm( (uint8_t *)bd_addr->address, accept); - } + if (accept) + BTA_DmBleSecurityGrant((uint8_t*)bd_addr->address, BTA_DM_SEC_GRANTED); + else + BTA_DmBleSecurityGrant((uint8_t*)bd_addr->address, + BTA_DM_SEC_PAIR_NOT_SPT); + } + } else { + BTA_DmConfirm((uint8_t*)bd_addr->address, accept); + } #else - BTA_DmConfirm( (uint8_t *)bd_addr->address, accept); + BTA_DmConfirm((uint8_t*)bd_addr->address, accept); #endif - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_dm_get_adapter_property -** -** Description Queries the BTA for the adapter property -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_get_adapter_property(bt_property_t *prop) -{ - BTIF_TRACE_EVENT("%s: type=0x%x", __func__, prop->type); - switch (prop->type) - { - case BT_PROPERTY_BDNAME: - { - bt_bdname_t *bd_name = (bt_bdname_t*)prop->val; - strncpy((char *)bd_name->name, (char *)btif_get_default_local_name(), - sizeof(bd_name->name) - 1); - bd_name->name[sizeof(bd_name->name) - 1] = 0; - prop->len = strlen((char *)bd_name->name); - } - break; + * + * Function btif_dm_get_adapter_property + * + * Description Queries the BTA for the adapter property + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_get_adapter_property(bt_property_t* prop) { + BTIF_TRACE_EVENT("%s: type=0x%x", __func__, prop->type); + switch (prop->type) { + case BT_PROPERTY_BDNAME: { + bt_bdname_t* bd_name = (bt_bdname_t*)prop->val; + strncpy((char*)bd_name->name, (char*)btif_get_default_local_name(), + sizeof(bd_name->name) - 1); + bd_name->name[sizeof(bd_name->name) - 1] = 0; + prop->len = strlen((char*)bd_name->name); + } break; + + case BT_PROPERTY_ADAPTER_SCAN_MODE: { + /* if the storage does not have it. Most likely app never set it. Default + * is NONE */ + bt_scan_mode_t* mode = (bt_scan_mode_t*)prop->val; + *mode = BT_SCAN_MODE_NONE; + prop->len = sizeof(bt_scan_mode_t); + } break; + + case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: { + uint32_t* tmt = (uint32_t*)prop->val; + *tmt = 120; /* default to 120s, if not found in NV */ + prop->len = sizeof(uint32_t); + } break; + + default: + prop->len = 0; + return BT_STATUS_FAIL; + } + return BT_STATUS_SUCCESS; +} - case BT_PROPERTY_ADAPTER_SCAN_MODE: - { - /* if the storage does not have it. Most likely app never set it. Default is NONE */ - bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val; - *mode = BT_SCAN_MODE_NONE; - prop->len = sizeof(bt_scan_mode_t); - } - break; +/******************************************************************************* + * + * Function btif_dm_get_remote_services + * + * Description Start SDP to get remote services + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_get_remote_services(bt_bdaddr_t* remote_addr) { + bdstr_t bdstr; - case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: - { - uint32_t *tmt = (uint32_t*)prop->val; - *tmt = 120; /* default to 120s, if not found in NV */ - prop->len = sizeof(uint32_t); - } - break; + BTIF_TRACE_EVENT("%s: remote_addr=%s", __func__, + bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr))); - default: - prop->len = 0; - return BT_STATUS_FAIL; - } - return BT_STATUS_SUCCESS; + BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK, + bte_dm_search_services_evt, true); + + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_dm_get_remote_services -** -** Description Start SDP to get remote services -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr) -{ - bdstr_t bdstr; + * + * Function btif_dm_get_remote_services_transport + * + * Description Start SDP to get remote services by transport + * + * Returns bt_status_t + * + ******************************************************************************/ +bt_status_t btif_dm_get_remote_services_by_transport(bt_bdaddr_t* remote_addr, + const int transport) { + BTIF_TRACE_EVENT("%s", __func__); - BTIF_TRACE_EVENT("%s: remote_addr=%s", __func__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr))); + /* Set the mask extension */ + tBTA_SERVICE_MASK_EXT mask_ext; + mask_ext.num_uuid = 0; + mask_ext.p_uuid = NULL; + mask_ext.srvc_mask = BTA_ALL_SERVICE_MASK; - BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK, - bte_dm_search_services_evt, true); + BTA_DmDiscoverByTransport(remote_addr->address, &mask_ext, + bte_dm_search_services_evt, true, transport); - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_dm_get_remote_services_transport -** -** Description Start SDP to get remote services by transport -** -** Returns bt_status_t -** -*******************************************************************************/ -bt_status_t btif_dm_get_remote_services_by_transport(bt_bdaddr_t *remote_addr, const int transport) -{ - BTIF_TRACE_EVENT("%s", __func__); - - /* Set the mask extension */ - tBTA_SERVICE_MASK_EXT mask_ext; - mask_ext.num_uuid = 0; - mask_ext.p_uuid = NULL; - mask_ext.srvc_mask = BTA_ALL_SERVICE_MASK; - - BTA_DmDiscoverByTransport(remote_addr->address, &mask_ext, - bte_dm_search_services_evt, true, transport); - - return BT_STATUS_SUCCESS; -} + * + * Function btif_dm_get_remote_service_record + * + * Description Start SDP to get remote service record + * + * + * Returns bt_status_t + ******************************************************************************/ +bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t* remote_addr, + bt_uuid_t* uuid) { + tSDP_UUID sdp_uuid; + bdstr_t bdstr; -/******************************************************************************* -** -** Function btif_dm_get_remote_service_record -** -** Description Start SDP to get remote service record -** -** -** Returns bt_status_t -*******************************************************************************/ -bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr, - bt_uuid_t *uuid) -{ - tSDP_UUID sdp_uuid; - bdstr_t bdstr; - - BTIF_TRACE_EVENT("%s: remote_addr=%s", __func__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr))); - - sdp_uuid.len = MAX_UUID_SIZE; - memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE); - - BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid, - bte_dm_remote_service_record_evt, true); - - return BT_STATUS_SUCCESS; -} - -void btif_dm_execute_service_request(uint16_t event, char *p_param) -{ - bool b_enable = false; - bt_status_t status; - if (event == BTIF_DM_ENABLE_SERVICE) - { - b_enable = true; - } - status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable); - if (status == BT_STATUS_SUCCESS) - { - bt_property_t property; - bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS]; - - /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */ - BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS, - sizeof(local_uuids), local_uuids); - btif_storage_get_adapter_property(&property); - HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, - BT_STATUS_SUCCESS, 1, &property); - } - return; + BTIF_TRACE_EVENT("%s: remote_addr=%s", __func__, + bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr))); + + sdp_uuid.len = MAX_UUID_SIZE; + memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE); + + BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid, + bte_dm_remote_service_record_evt, true); + + return BT_STATUS_SUCCESS; } -void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data, - tBTA_AUTH_REQ *p_auth_req, bool is_orig) -{ - uint8_t yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req; - /* if local initiated: - ** 1. set DD + MITM - ** if remote initiated: - ** 1. Copy over the auth_req from peer's io_rsp - ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone) - ** as a fallback set MITM+GB if peer had MITM set +void btif_dm_execute_service_request(uint16_t event, char* p_param) { + bool b_enable = false; + bt_status_t status; + if (event == BTIF_DM_ENABLE_SERVICE) { + b_enable = true; + } + status = + btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable); + if (status == BT_STATUS_SUCCESS) { + bt_property_t property; + bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS]; + + /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */ + BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS, + sizeof(local_uuids), local_uuids); + btif_storage_get_adapter_property(&property); + HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, + &property); + } + return; +} + +void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP* p_io_cap, + tBTA_OOB_DATA* p_oob_data, tBTA_AUTH_REQ* p_auth_req, + bool is_orig) { + uint8_t yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req; + /* if local initiated: + ** 1. set DD + MITM + ** if remote initiated: + ** 1. Copy over the auth_req from peer's io_rsp + ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo + *(iPhone) + ** as a fallback set MITM+GB if peer had MITM set + */ + UNUSED(bd_addr); + UNUSED(p_io_cap); + UNUSED(p_oob_data); + + BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __func__, *p_auth_req); + if (pairing_cb.is_local_initiated) { + /* if initing/responding to a dedicated bonding, use dedicate bonding bit */ + *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES; + } else if (!is_orig) { + /* peer initiated paring. They probably know what they want. + ** Copy the mitm from peer device. */ - UNUSED (bd_addr); - UNUSED (p_io_cap); - UNUSED (p_oob_data); - - BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __func__, *p_auth_req); - if(pairing_cb.is_local_initiated) - { - /* if initing/responding to a dedicated bonding, use dedicate bonding bit */ - *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES; - } - else if (!is_orig) - { - /* peer initiated paring. They probably know what they want. - ** Copy the mitm from peer device. - */ - BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d", - __func__, pairing_cb.auth_req); - *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS); - - /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */ - if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) ) - *p_auth_req |= BTA_AUTH_SP_YES; - } - else if (yes_no_bit) - { - /* set the general bonding bit for stored device */ - *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit; - } - BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __func__, *p_auth_req); + BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d", __func__, + pairing_cb.auth_req); + *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS); + + /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, + * force MITM */ + if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO)) + *p_auth_req |= BTA_AUTH_SP_YES; + } else if (yes_no_bit) { + /* set the general bonding bit for stored device */ + *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit; + } + BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __func__, *p_auth_req); } void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap, - tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req) -{ - UNUSED (bd_addr); - UNUSED (oob_data); - - if(auth_req & BTA_AUTH_BONDS) - { - BTIF_TRACE_DEBUG("%s auth_req:%d", __func__, auth_req); - pairing_cb.auth_req = auth_req; - pairing_cb.io_cap = io_cap; - } -} + tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req) { + UNUSED(bd_addr); + UNUSED(oob_data); -void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_has_oob_data) -{ - if (is_empty_128bit(oob_cb.oob_data.c192)) - { - *p_has_oob_data = false; - } - else - { - *p_has_oob_data = true; - } - BTIF_TRACE_DEBUG("%s: *p_has_oob_data=%d", __func__, *p_has_oob_data); + if (auth_req & BTA_AUTH_BONDS) { + BTIF_TRACE_DEBUG("%s auth_req:%d", __func__, auth_req); + pairing_cb.auth_req = auth_req; + pairing_cb.io_cap = io_cap; + } } -void btif_dm_set_oob_for_le_io_req(BD_ADDR bd_addr, tBTA_OOB_DATA *p_has_oob_data, - tBTA_LE_AUTH_REQ *p_auth_req) -{ +void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA* p_has_oob_data) { + if (is_empty_128bit(oob_cb.oob_data.c192)) { + *p_has_oob_data = false; + } else { + *p_has_oob_data = true; + } + BTIF_TRACE_DEBUG("%s: *p_has_oob_data=%d", __func__, *p_has_oob_data); +} - if (!is_empty_128bit(oob_cb.oob_data.le_sc_c) && - !is_empty_128bit(oob_cb.oob_data.le_sc_r)) { - /* We have LE SC OOB data */ +void btif_dm_set_oob_for_le_io_req(BD_ADDR bd_addr, + tBTA_OOB_DATA* p_has_oob_data, + tBTA_LE_AUTH_REQ* p_auth_req) { + if (!is_empty_128bit(oob_cb.oob_data.le_sc_c) && + !is_empty_128bit(oob_cb.oob_data.le_sc_r)) { + /* We have LE SC OOB data */ - /* make sure OOB data is for this particular device */ - if (memcmp(bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) == 0) { - *p_auth_req = ((*p_auth_req) | BTM_LE_AUTH_REQ_SC_ONLY); - *p_has_oob_data = true; - } else { - *p_has_oob_data = false; - BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address", - __func__); - } - } else if (!is_empty_128bit(oob_cb.oob_data.sm_tk)) { - /* We have security manager TK */ + /* make sure OOB data is for this particular device */ + if (memcmp(bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) == 0) { + *p_auth_req = ((*p_auth_req) | BTM_LE_AUTH_REQ_SC_ONLY); + *p_has_oob_data = true; + } else { + *p_has_oob_data = false; + BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address", + __func__); + } + } else if (!is_empty_128bit(oob_cb.oob_data.sm_tk)) { + /* We have security manager TK */ - /* make sure OOB data is for this particular device */ - if (memcmp(bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) == 0) { - // When using OOB with TK, SC Secure Connections bit must be disabled. - tBTA_LE_AUTH_REQ mask = ~BTM_LE_AUTH_REQ_SC_ONLY; - *p_auth_req = ((*p_auth_req) & mask); + /* make sure OOB data is for this particular device */ + if (memcmp(bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) == 0) { + // When using OOB with TK, SC Secure Connections bit must be disabled. + tBTA_LE_AUTH_REQ mask = ~BTM_LE_AUTH_REQ_SC_ONLY; + *p_auth_req = ((*p_auth_req) & mask); - *p_has_oob_data = true; - } else { - *p_has_oob_data = false; - BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address", - __func__); - } + *p_has_oob_data = true; } else { - *p_has_oob_data = false; + *p_has_oob_data = false; + BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address", + __func__); } - BTIF_TRACE_DEBUG("%s *p_has_oob_data=%d", __func__, *p_has_oob_data); + } else { + *p_has_oob_data = false; + } + BTIF_TRACE_DEBUG("%s *p_has_oob_data=%d", __func__, *p_has_oob_data); } #ifdef BTIF_DM_OOB_TEST -void btif_dm_load_local_oob(void) -{ - char prop_oob[PROPERTY_VALUE_MAX]; +void btif_dm_load_local_oob(void) { + char prop_oob[PROPERTY_VALUE_MAX]; + osi_property_get("service.brcm.bt.oob", prop_oob, "3"); + BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob); + if (prop_oob[0] != '3') { + if (is_empty_128bit(oob_cb.oob_data.c192)) { + BTIF_TRACE_DEBUG("%s: read OOB, call BTA_DmLocalOob()", __func__); + BTA_DmLocalOob(); + } + } +} + +void btif_dm_proc_loc_oob(bool valid, BT_OCTET16 c, BT_OCTET16 r) { + FILE* fp; + const char* path_a = "/data/misc/bluedroid/LOCAL/a.key"; + const char* path_b = "/data/misc/bluedroid/LOCAL/b.key"; + const char* path = NULL; + char prop_oob[PROPERTY_VALUE_MAX]; + BTIF_TRACE_DEBUG("%s: valid=%d", __func__, valid); + if (is_empty_128bit(oob_cb.oob_data.c192) && valid) { + BTIF_TRACE_DEBUG("save local OOB data in memory"); + memcpy(oob_cb.oob_data.c192, c, BT_OCTET16_LEN); + memcpy(oob_cb.oob_data.r192, r, BT_OCTET16_LEN); osi_property_get("service.brcm.bt.oob", prop_oob, "3"); BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob); - if (prop_oob[0] != '3') - { - if (is_empty_128bit(oob_cb.oob_data.c192)) - { - BTIF_TRACE_DEBUG("%s: read OOB, call BTA_DmLocalOob()", __func__); - BTA_DmLocalOob(); - } - } -} - -void btif_dm_proc_loc_oob(bool valid, BT_OCTET16 c, BT_OCTET16 r) -{ - FILE *fp; - const char *path_a = "/data/misc/bluedroid/LOCAL/a.key"; - const char *path_b = "/data/misc/bluedroid/LOCAL/b.key"; - const char *path = NULL; - char prop_oob[PROPERTY_VALUE_MAX]; - BTIF_TRACE_DEBUG("%s: valid=%d", __func__, valid); - if (is_empty_128bit(oob_cb.oob_data.c192) && valid) - { - BTIF_TRACE_DEBUG("save local OOB data in memory"); - memcpy(oob_cb.oob_data.c192, c, BT_OCTET16_LEN); - memcpy(oob_cb.oob_data.r192, r, BT_OCTET16_LEN); - osi_property_get("service.brcm.bt.oob", prop_oob, "3"); - BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob); - if (prop_oob[0] == '1') - path = path_a; - else if (prop_oob[0] == '2') - path = path_b; - if (path) - { - fp = fopen(path, "wb+"); - if (fp == NULL) - { - BTIF_TRACE_DEBUG("%s: failed to save local OOB data to %s", __func__, path); - } - else - { - BTIF_TRACE_DEBUG("%s: save local OOB data into file %s", __func__, path); - fwrite (c , 1 , BT_OCTET16_LEN , fp ); - fwrite (r , 1 , BT_OCTET16_LEN , fp ); - fclose(fp); - } - } - } + if (prop_oob[0] == '1') + path = path_a; + else if (prop_oob[0] == '2') + path = path_b; + if (path) { + fp = fopen(path, "wb+"); + if (fp == NULL) { + BTIF_TRACE_DEBUG("%s: failed to save local OOB data to %s", __func__, + path); + } else { + BTIF_TRACE_DEBUG("%s: save local OOB data into file %s", __func__, + path); + fwrite(c, 1, BT_OCTET16_LEN, fp); + fwrite(r, 1, BT_OCTET16_LEN, fp); + fclose(fp); + } + } + } } /******************************************************************************* -** -** Function btif_dm_get_smp_config -** -** Description Retrieve the SMP pairing options from the bt_stack.conf -** file. To provide specific pairing options for the host -** add a node with label "SmpOptions" to the config file -** and assign it a comma separated list of 5 values in the -** format: auth, io, ikey, rkey, ksize, oob -** eg: PTS_SmpOptions=0xD,0x4,0xf,0xf,0x10 -** -** Parameters: tBTE_APPL_CFG*: pointer to struct defining pairing options -** -** Returns true if the options were successfully read, else false -** -*******************************************************************************/ + * + * Function btif_dm_get_smp_config + * + * Description Retrieve the SMP pairing options from the bt_stack.conf + * file. To provide specific pairing options for the host + * add a node with label "SmpOptions" to the config file + * and assign it a comma separated list of 5 values in the + * format: auth, io, ikey, rkey, ksize, oob + * eg: PTS_SmpOptions=0xD,0x4,0xf,0xf,0x10 + * + * Parameters: tBTE_APPL_CFG*: pointer to struct defining pairing options + * + * Returns true if the options were successfully read, else false + * + ******************************************************************************/ bool btif_dm_get_smp_config(tBTE_APPL_CFG* p_cfg) { + if (!stack_config_get_interface()->get_pts_smp_options()) { + BTIF_TRACE_DEBUG("%s: SMP options not found in configuration", __func__); + return false; + } - if(!stack_config_get_interface()->get_pts_smp_options()) { - BTIF_TRACE_DEBUG ("%s: SMP options not found in configuration", __func__); - return false; - } - - char conf[64]; - const char* recv = stack_config_get_interface()->get_pts_smp_options(); - char* pch; - char* endptr; - - strncpy(conf, recv, 64); - conf[63] = 0; // null terminate + char conf[64]; + const char* recv = stack_config_get_interface()->get_pts_smp_options(); + char* pch; + char* endptr; - if ((pch = strtok(conf, ",")) != NULL) - p_cfg->ble_auth_req = (uint8_t) strtoul(pch, &endptr, 16); - else - return false; + strncpy(conf, recv, 64); + conf[63] = 0; // null terminate - if ((pch = strtok(NULL, ",")) != NULL) - p_cfg->ble_io_cap = (uint8_t) strtoul(pch, &endptr, 16); - else - return false; + if ((pch = strtok(conf, ",")) != NULL) + p_cfg->ble_auth_req = (uint8_t)strtoul(pch, &endptr, 16); + else + return false; - if ((pch = strtok(NULL, ",")) != NULL) - p_cfg->ble_init_key = (uint8_t) strtoul(pch, &endptr, 16); - else - return false; + if ((pch = strtok(NULL, ",")) != NULL) + p_cfg->ble_io_cap = (uint8_t)strtoul(pch, &endptr, 16); + else + return false; - if ((pch = strtok(NULL, ",")) != NULL) - p_cfg->ble_resp_key = (uint8_t) strtoul(pch, &endptr, 16); - else - return false; + if ((pch = strtok(NULL, ",")) != NULL) + p_cfg->ble_init_key = (uint8_t)strtoul(pch, &endptr, 16); + else + return false; - if ((pch = strtok(NULL, ",")) != NULL) - p_cfg->ble_max_key_size = (uint8_t) strtoul(pch, &endptr, 16); - else - return false; + if ((pch = strtok(NULL, ",")) != NULL) + p_cfg->ble_resp_key = (uint8_t)strtoul(pch, &endptr, 16); + else + return false; - return true; -} + if ((pch = strtok(NULL, ",")) != NULL) + p_cfg->ble_max_key_size = (uint8_t)strtoul(pch, &endptr, 16); + else + return false; -bool btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r) -{ - char t[128]; - FILE *fp; - const char *path_a = "/data/misc/bluedroid/LOCAL/a.key"; - const char *path_b = "/data/misc/bluedroid/LOCAL/b.key"; - const char *path = NULL; - char prop_oob[PROPERTY_VALUE_MAX]; - bool result = false; - bt_bdaddr_t bt_bd_addr; - bdcpy(oob_cb.bdaddr, bd_addr); - osi_property_get("service.brcm.bt.oob", prop_oob, "3"); - BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob); - if (prop_oob[0] == '1') - path = path_b; - else if (prop_oob[0] == '2') - path = path_a; - if (path) - { - fp = fopen(path, "rb"); - if (fp == NULL) - { - BTIF_TRACE_DEBUG("%s: failed to read OOB keys from %s", __func__, path); - return false; - } - else - { - BTIF_TRACE_DEBUG("%s: read OOB data from %s", __func__, path); - fread (p_c , 1 , BT_OCTET16_LEN , fp ); - fread (p_r , 1 , BT_OCTET16_LEN , fp ); - fclose(fp); - } - BTIF_TRACE_DEBUG("----%s: true", __func__); - snprintf(t, sizeof(t), "%02x:%02x:%02x:%02x:%02x:%02x", - oob_cb.bdaddr[0], oob_cb.bdaddr[1], oob_cb.bdaddr[2], - oob_cb.bdaddr[3], oob_cb.bdaddr[4], oob_cb.bdaddr[5]); - BTIF_TRACE_DEBUG("----%s: peer_bdaddr = %s", __func__, t); - snprintf(t, sizeof(t), "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", - p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7], - p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14], p_c[15]); - BTIF_TRACE_DEBUG("----%s: c = %s", __func__, t); - snprintf(t, sizeof(t), "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", - p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7], - p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14], p_r[15]); - BTIF_TRACE_DEBUG("----%s: r = %s", __func__, t); - bdcpy(bt_bd_addr.address, bd_addr); - btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING, - (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL); - result = true; - } - BTIF_TRACE_DEBUG("%s: result=%d", __func__, result); - return result; + return true; +} + +bool btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r) { + char t[128]; + FILE* fp; + const char* path_a = "/data/misc/bluedroid/LOCAL/a.key"; + const char* path_b = "/data/misc/bluedroid/LOCAL/b.key"; + const char* path = NULL; + char prop_oob[PROPERTY_VALUE_MAX]; + bool result = false; + bt_bdaddr_t bt_bd_addr; + bdcpy(oob_cb.bdaddr, bd_addr); + osi_property_get("service.brcm.bt.oob", prop_oob, "3"); + BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob); + if (prop_oob[0] == '1') + path = path_b; + else if (prop_oob[0] == '2') + path = path_a; + if (path) { + fp = fopen(path, "rb"); + if (fp == NULL) { + BTIF_TRACE_DEBUG("%s: failed to read OOB keys from %s", __func__, path); + return false; + } else { + BTIF_TRACE_DEBUG("%s: read OOB data from %s", __func__, path); + fread(p_c, 1, BT_OCTET16_LEN, fp); + fread(p_r, 1, BT_OCTET16_LEN, fp); + fclose(fp); + } + BTIF_TRACE_DEBUG("----%s: true", __func__); + snprintf(t, sizeof(t), "%02x:%02x:%02x:%02x:%02x:%02x", oob_cb.bdaddr[0], + oob_cb.bdaddr[1], oob_cb.bdaddr[2], oob_cb.bdaddr[3], + oob_cb.bdaddr[4], oob_cb.bdaddr[5]); + BTIF_TRACE_DEBUG("----%s: peer_bdaddr = %s", __func__, t); + snprintf(t, sizeof(t), + "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x " + "%02x %02x %02x", + p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7], + p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14], + p_c[15]); + BTIF_TRACE_DEBUG("----%s: c = %s", __func__, t); + snprintf(t, sizeof(t), + "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x " + "%02x %02x %02x", + p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7], + p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14], + p_r[15]); + BTIF_TRACE_DEBUG("----%s: r = %s", __func__, t); + bdcpy(bt_bd_addr.address, bd_addr); + btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING, + (char*)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL); + result = true; + } + BTIF_TRACE_DEBUG("%s: result=%d", __func__, result); + return result; } #endif /* BTIF_DM_OOB_TEST */ #if (BLE_INCLUDED == TRUE) -static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif) -{ - bt_bdaddr_t bd_addr; - bt_bdname_t bd_name; - uint32_t cod; - int dev_type; +static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF* p_ssp_key_notif) { + bt_bdaddr_t bd_addr; + bt_bdname_t bd_name; + uint32_t cod; + int dev_type; - BTIF_TRACE_DEBUG("%s", __func__); + BTIF_TRACE_DEBUG("%s", __func__); - /* Remote name update */ - if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type)) - { - dev_type = BT_DEVICE_TYPE_BLE; - } - btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name, - (tBT_DEVICE_TYPE) dev_type); - bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr); - memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN); + /* Remote name update */ + if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type)) { + dev_type = BT_DEVICE_TYPE_BLE; + } + btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr, + p_ssp_key_notif->bd_name, + (tBT_DEVICE_TYPE)dev_type); + bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr); + memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN); - bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); - pairing_cb.is_ssp = false; - cod = COD_UNCLASSIFIED; + bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); + pairing_cb.is_ssp = false; + cod = COD_UNCLASSIFIED; - HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, - cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION, - p_ssp_key_notif->passkey); + HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod, + BT_SSP_VARIANT_PASSKEY_NOTIFICATION, p_ssp_key_notif->passkey); } /******************************************************************************* -** -** Function btif_dm_ble_auth_cmpl_evt -** -** Description Executes authentication complete event in btif context -** -** Returns void -** -*******************************************************************************/ -static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl) -{ - /* Save link key, if not temporary */ - bt_bdaddr_t bd_addr; - bt_status_t status = BT_STATUS_FAIL; - bt_bond_state_t state = BT_BOND_STATE_NONE; - - bdcpy(bd_addr.address, p_auth_cmpl->bd_addr); - - /* Clear OOB data */ - memset(&oob_cb, 0, sizeof(oob_cb)); - - if ( (p_auth_cmpl->success == true) && (p_auth_cmpl->key_present) ) - { - /* store keys */ - } - if (p_auth_cmpl->success) - { - status = BT_STATUS_SUCCESS; - state = BT_BOND_STATE_BONDED; - int addr_type; - bt_bdaddr_t bdaddr; - bdcpy(bdaddr.address, p_auth_cmpl->bd_addr); - if (btif_storage_get_remote_addr_type(&bdaddr, &addr_type) != BT_STATUS_SUCCESS) - btif_storage_set_remote_addr_type(&bdaddr, p_auth_cmpl->addr_type); - - /* Test for temporary bonding */ - if (btm_get_bond_type_dev(p_auth_cmpl->bd_addr) == BOND_TYPE_TEMPORARY) { - BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing", - __func__); - btif_storage_remove_bonded_device(&bdaddr); - state = BT_BOND_STATE_NONE; - } else { - btif_dm_save_ble_bonding_keys(); - BTA_GATTC_Refresh(bd_addr.address); - btif_dm_get_remote_services_by_transport(&bd_addr, BTA_GATT_TRANSPORT_LE); - } - } - else - { - /*Map the HCI fail reason to bt status */ - switch (p_auth_cmpl->fail_reason) - { - case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL: - case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL: - case BTA_DM_AUTH_SMP_UNKNOWN_ERR: - case BTA_DM_AUTH_SMP_CONN_TOUT: - btif_dm_remove_ble_bonding_keys(); - status = BT_STATUS_AUTH_FAILURE; - break; - case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT: - status = BT_STATUS_AUTH_REJECTED; - break; - default: - btif_dm_remove_ble_bonding_keys(); - status = BT_STATUS_FAIL; - break; - } - } - bond_state_changed(status, &bd_addr, state); -} - -void btif_dm_load_ble_local_keys(void) -{ - memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t)); - - if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0], - BT_OCTET16_LEN)== BT_STATUS_SUCCESS) - { - ble_local_key_cb.is_er_rcvd = true; - BTIF_TRACE_DEBUG("%s BLE ER key loaded",__func__ ); - } - - if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0], - BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&& - (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0], - BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&& - (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0], - BT_OCTET16_LEN)== BT_STATUS_SUCCESS)) - { - ble_local_key_cb.is_id_keys_rcvd = true; - BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__func__ ); - } - -} -void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er, - tBTA_BLE_LOCAL_ID_KEYS *p_id_keys) -{ - if (ble_local_key_cb.is_er_rcvd ) - { - memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16)); - *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER; - } - - if (ble_local_key_cb.is_id_keys_rcvd) - { - memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16)); - memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16)); - memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16)); - *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID; - } - BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__func__, *p_key_mask); -} - -void btif_dm_save_ble_bonding_keys(void) -{ - - bt_bdaddr_t bd_addr; - - BTIF_TRACE_DEBUG("%s",__func__ ); - - bdcpy(bd_addr.address, pairing_cb.bd_addr); - - if (pairing_cb.ble.is_penc_key_rcvd) - { - btif_storage_add_ble_bonding_key(&bd_addr, - (char *) &pairing_cb.ble.penc_key, - BTIF_DM_LE_KEY_PENC, - sizeof(tBTM_LE_PENC_KEYS)); - } - - if (pairing_cb.ble.is_pid_key_rcvd) - { - btif_storage_add_ble_bonding_key(&bd_addr, - (char *) &pairing_cb.ble.pid_key, - BTIF_DM_LE_KEY_PID, - sizeof(tBTM_LE_PID_KEYS)); - } - - if (pairing_cb.ble.is_pcsrk_key_rcvd) - { - btif_storage_add_ble_bonding_key(&bd_addr, - (char *) &pairing_cb.ble.pcsrk_key, - BTIF_DM_LE_KEY_PCSRK, - sizeof(tBTM_LE_PCSRK_KEYS)); - } - - if (pairing_cb.ble.is_lenc_key_rcvd) - { - btif_storage_add_ble_bonding_key(&bd_addr, - (char *) &pairing_cb.ble.lenc_key, - BTIF_DM_LE_KEY_LENC, - sizeof(tBTM_LE_LENC_KEYS)); - } - - if (pairing_cb.ble.is_lcsrk_key_rcvd) - { - btif_storage_add_ble_bonding_key(&bd_addr, - (char *) &pairing_cb.ble.lcsrk_key, - BTIF_DM_LE_KEY_LCSRK, - sizeof(tBTM_LE_LCSRK_KEYS)); - } - - if (pairing_cb.ble.is_lidk_key_rcvd) - { - btif_storage_add_ble_bonding_key(&bd_addr, - NULL, - BTIF_DM_LE_KEY_LID, - 0); + * + * Function btif_dm_ble_auth_cmpl_evt + * + * Description Executes authentication complete event in btif context + * + * Returns void + * + ******************************************************************************/ +static void btif_dm_ble_auth_cmpl_evt(tBTA_DM_AUTH_CMPL* p_auth_cmpl) { + /* Save link key, if not temporary */ + bt_bdaddr_t bd_addr; + bt_status_t status = BT_STATUS_FAIL; + bt_bond_state_t state = BT_BOND_STATE_NONE; + + bdcpy(bd_addr.address, p_auth_cmpl->bd_addr); + + /* Clear OOB data */ + memset(&oob_cb, 0, sizeof(oob_cb)); + + if ((p_auth_cmpl->success == true) && (p_auth_cmpl->key_present)) { + /* store keys */ + } + if (p_auth_cmpl->success) { + status = BT_STATUS_SUCCESS; + state = BT_BOND_STATE_BONDED; + int addr_type; + bt_bdaddr_t bdaddr; + bdcpy(bdaddr.address, p_auth_cmpl->bd_addr); + if (btif_storage_get_remote_addr_type(&bdaddr, &addr_type) != + BT_STATUS_SUCCESS) + btif_storage_set_remote_addr_type(&bdaddr, p_auth_cmpl->addr_type); + + /* Test for temporary bonding */ + if (btm_get_bond_type_dev(p_auth_cmpl->bd_addr) == BOND_TYPE_TEMPORARY) { + BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing", + __func__); + btif_storage_remove_bonded_device(&bdaddr); + state = BT_BOND_STATE_NONE; + } else { + btif_dm_save_ble_bonding_keys(); + BTA_GATTC_Refresh(bd_addr.address); + btif_dm_get_remote_services_by_transport(&bd_addr, BTA_GATT_TRANSPORT_LE); + } + } else { + /*Map the HCI fail reason to bt status */ + switch (p_auth_cmpl->fail_reason) { + case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL: + case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL: + case BTA_DM_AUTH_SMP_UNKNOWN_ERR: + case BTA_DM_AUTH_SMP_CONN_TOUT: + btif_dm_remove_ble_bonding_keys(); + status = BT_STATUS_AUTH_FAILURE; + break; + case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT: + status = BT_STATUS_AUTH_REJECTED; + break; + default: + btif_dm_remove_ble_bonding_keys(); + status = BT_STATUS_FAIL; + break; } - -} - -void btif_dm_remove_ble_bonding_keys(void) -{ - bt_bdaddr_t bd_addr; - - BTIF_TRACE_DEBUG("%s",__func__ ); - - bdcpy(bd_addr.address, pairing_cb.bd_addr); - btif_storage_remove_ble_bonding_keys(&bd_addr); + } + bond_state_changed(status, &bd_addr, state); +} + +void btif_dm_load_ble_local_keys(void) { + memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t)); + + if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER, + (char*)&ble_local_key_cb.er[0], + BT_OCTET16_LEN) == BT_STATUS_SUCCESS) { + ble_local_key_cb.is_er_rcvd = true; + BTIF_TRACE_DEBUG("%s BLE ER key loaded", __func__); + } + + if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR, + (char*)&ble_local_key_cb.id_keys.ir[0], + BT_OCTET16_LEN) == BT_STATUS_SUCCESS) && + (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, + (char*)&ble_local_key_cb.id_keys.irk[0], + BT_OCTET16_LEN) == BT_STATUS_SUCCESS) && + (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK, + (char*)&ble_local_key_cb.id_keys.dhk[0], + BT_OCTET16_LEN) == BT_STATUS_SUCCESS)) { + ble_local_key_cb.is_id_keys_rcvd = true; + BTIF_TRACE_DEBUG("%s BLE ID keys loaded", __func__); + } +} +void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK* p_key_mask, + BT_OCTET16 er, + tBTA_BLE_LOCAL_ID_KEYS* p_id_keys) { + if (ble_local_key_cb.is_er_rcvd) { + memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16)); + *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER; + } + + if (ble_local_key_cb.is_id_keys_rcvd) { + memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], + sizeof(BT_OCTET16)); + memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], + sizeof(BT_OCTET16)); + memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], + sizeof(BT_OCTET16)); + *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID; + } + BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x", __func__, *p_key_mask); +} + +void btif_dm_save_ble_bonding_keys(void) { + bt_bdaddr_t bd_addr; + + BTIF_TRACE_DEBUG("%s", __func__); + + bdcpy(bd_addr.address, pairing_cb.bd_addr); + + if (pairing_cb.ble.is_penc_key_rcvd) { + btif_storage_add_ble_bonding_key(&bd_addr, (char*)&pairing_cb.ble.penc_key, + BTIF_DM_LE_KEY_PENC, + sizeof(tBTM_LE_PENC_KEYS)); + } + + if (pairing_cb.ble.is_pid_key_rcvd) { + btif_storage_add_ble_bonding_key(&bd_addr, (char*)&pairing_cb.ble.pid_key, + BTIF_DM_LE_KEY_PID, + sizeof(tBTM_LE_PID_KEYS)); + } + + if (pairing_cb.ble.is_pcsrk_key_rcvd) { + btif_storage_add_ble_bonding_key(&bd_addr, (char*)&pairing_cb.ble.pcsrk_key, + BTIF_DM_LE_KEY_PCSRK, + sizeof(tBTM_LE_PCSRK_KEYS)); + } + + if (pairing_cb.ble.is_lenc_key_rcvd) { + btif_storage_add_ble_bonding_key(&bd_addr, (char*)&pairing_cb.ble.lenc_key, + BTIF_DM_LE_KEY_LENC, + sizeof(tBTM_LE_LENC_KEYS)); + } + + if (pairing_cb.ble.is_lcsrk_key_rcvd) { + btif_storage_add_ble_bonding_key(&bd_addr, (char*)&pairing_cb.ble.lcsrk_key, + BTIF_DM_LE_KEY_LCSRK, + sizeof(tBTM_LE_LCSRK_KEYS)); + } + + if (pairing_cb.ble.is_lidk_key_rcvd) { + btif_storage_add_ble_bonding_key(&bd_addr, NULL, BTIF_DM_LE_KEY_LID, 0); + } +} + +void btif_dm_remove_ble_bonding_keys(void) { + bt_bdaddr_t bd_addr; + + BTIF_TRACE_DEBUG("%s", __func__); + + bdcpy(bd_addr.address, pairing_cb.bd_addr); + btif_storage_remove_ble_bonding_keys(&bd_addr); } /******************************************************************************* -** -** Function btif_dm_ble_sec_req_evt -** -** Description Eprocess security request event in btif context -** -** Returns void -** -*******************************************************************************/ -void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req) -{ - bt_bdaddr_t bd_addr; - bt_bdname_t bd_name; - uint32_t cod; - int dev_type; + * + * Function btif_dm_ble_sec_req_evt + * + * Description Eprocess security request event in btif context + * + * Returns void + * + ******************************************************************************/ +void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ* p_ble_req) { + bt_bdaddr_t bd_addr; + bt_bdname_t bd_name; + uint32_t cod; + int dev_type; - BTIF_TRACE_DEBUG("%s", __func__); + BTIF_TRACE_DEBUG("%s", __func__); - if (pairing_cb.state == BT_BOND_STATE_BONDING) - { - BTIF_TRACE_DEBUG("%s Discard security request", __func__); - return; - } + if (pairing_cb.state == BT_BOND_STATE_BONDING) { + BTIF_TRACE_DEBUG("%s Discard security request", __func__); + return; + } - /* Remote name update */ - if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type)) - { - dev_type = BT_DEVICE_TYPE_BLE; - } - btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name, - (tBT_DEVICE_TYPE) dev_type); + /* Remote name update */ + if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type)) { + dev_type = BT_DEVICE_TYPE_BLE; + } + btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name, + (tBT_DEVICE_TYPE)dev_type); - bdcpy(bd_addr.address, p_ble_req->bd_addr); - memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN); + bdcpy(bd_addr.address, p_ble_req->bd_addr); + memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN); - bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); + bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); - pairing_cb.bond_type = BOND_TYPE_PERSISTENT; - pairing_cb.is_le_only = true; - pairing_cb.is_le_nc = false; - pairing_cb.is_ssp = true; - btm_set_bond_type_dev(p_ble_req->bd_addr, pairing_cb.bond_type); + pairing_cb.bond_type = BOND_TYPE_PERSISTENT; + pairing_cb.is_le_only = true; + pairing_cb.is_le_nc = false; + pairing_cb.is_ssp = true; + btm_set_bond_type_dev(p_ble_req->bd_addr, pairing_cb.bond_type); - cod = COD_UNCLASSIFIED; + cod = COD_UNCLASSIFIED; - HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod, - BT_SSP_VARIANT_CONSENT, 0); + HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod, + BT_SSP_VARIANT_CONSENT, 0); } /******************************************************************************* -** -** Function btif_dm_ble_passkey_req_evt -** -** Description Executes pin request event in btif context -** -** Returns void -** -*******************************************************************************/ -static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req) -{ - bt_bdaddr_t bd_addr; - bt_bdname_t bd_name; - uint32_t cod; - int dev_type; - - /* Remote name update */ - if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type)) - { - dev_type = BT_DEVICE_TYPE_BLE; - } - btif_dm_update_ble_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name, - (tBT_DEVICE_TYPE) dev_type); + * + * Function btif_dm_ble_passkey_req_evt + * + * Description Executes pin request event in btif context + * + * Returns void + * + ******************************************************************************/ +static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ* p_pin_req) { + bt_bdaddr_t bd_addr; + bt_bdname_t bd_name; + uint32_t cod; + int dev_type; - bdcpy(bd_addr.address, p_pin_req->bd_addr); - memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN); + /* Remote name update */ + if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type)) { + dev_type = BT_DEVICE_TYPE_BLE; + } + btif_dm_update_ble_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name, + (tBT_DEVICE_TYPE)dev_type); - bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); - pairing_cb.is_le_only = true; + bdcpy(bd_addr.address, p_pin_req->bd_addr); + memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN); - cod = COD_UNCLASSIFIED; + bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); + pairing_cb.is_le_only = true; + + cod = COD_UNCLASSIFIED; - HAL_CBACK(bt_hal_cbacks, pin_request_cb, - &bd_addr, &bd_name, cod, false); + HAL_CBACK(bt_hal_cbacks, pin_request_cb, &bd_addr, &bd_name, cod, false); } -static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req) -{ - /* TODO implement key notification for numeric comparison */ - BTIF_TRACE_DEBUG("%s", __func__); +static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF* p_notif_req) { + /* TODO implement key notification for numeric comparison */ + BTIF_TRACE_DEBUG("%s", __func__); - /* Remote name update */ - btif_update_remote_properties(p_notif_req->bd_addr , p_notif_req->bd_name, - NULL, BT_DEVICE_TYPE_BLE); + /* Remote name update */ + btif_update_remote_properties(p_notif_req->bd_addr, p_notif_req->bd_name, + NULL, BT_DEVICE_TYPE_BLE); - bt_bdaddr_t bd_addr; - bdcpy(bd_addr.address, p_notif_req->bd_addr); + bt_bdaddr_t bd_addr; + bdcpy(bd_addr.address, p_notif_req->bd_addr); - bt_bdname_t bd_name; - memcpy(bd_name.name, p_notif_req->bd_name, BD_NAME_LEN); + bt_bdname_t bd_name; + memcpy(bd_name.name, p_notif_req->bd_name, BD_NAME_LEN); - bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); - pairing_cb.is_ssp = false; - pairing_cb.is_le_only = true; - pairing_cb.is_le_nc = true; + bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); + pairing_cb.is_ssp = false; + pairing_cb.is_le_only = true; + pairing_cb.is_le_nc = true; - HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, - COD_UNCLASSIFIED, BT_SSP_VARIANT_PASSKEY_CONFIRMATION, - p_notif_req->passkey); + HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, COD_UNCLASSIFIED, + BT_SSP_VARIANT_PASSKEY_CONFIRMATION, p_notif_req->passkey); } -static void btif_dm_ble_oob_req_evt(tBTA_DM_SP_RMT_OOB *req_oob_type) -{ - BTIF_TRACE_DEBUG("%s", __func__); +static void btif_dm_ble_oob_req_evt(tBTA_DM_SP_RMT_OOB* req_oob_type) { + BTIF_TRACE_DEBUG("%s", __func__); - bt_bdaddr_t bd_addr; - bdcpy(bd_addr.address, req_oob_type->bd_addr); - /* We already checked if OOB data is present in - * btif_dm_set_oob_for_le_io_req, but check here again. If it's not present - * do nothing, pairing will timeout. - */ - if (is_empty_128bit(oob_cb.oob_data.sm_tk)) { - return; - } + bt_bdaddr_t bd_addr; + bdcpy(bd_addr.address, req_oob_type->bd_addr); + /* We already checked if OOB data is present in + * btif_dm_set_oob_for_le_io_req, but check here again. If it's not present + * do nothing, pairing will timeout. + */ + if (is_empty_128bit(oob_cb.oob_data.sm_tk)) { + return; + } - /* make sure OOB data is for this particular device */ - if (memcmp(req_oob_type->bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) != 0) { - BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address", __func__); - return; - } + /* make sure OOB data is for this particular device */ + if (memcmp(req_oob_type->bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) != 0) { + BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address", + __func__); + return; + } - /* Remote name update */ - btif_update_remote_properties(req_oob_type->bd_addr , req_oob_type->bd_name, - NULL, BT_DEVICE_TYPE_BLE); + /* Remote name update */ + btif_update_remote_properties(req_oob_type->bd_addr, req_oob_type->bd_name, + NULL, BT_DEVICE_TYPE_BLE); - bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); - pairing_cb.is_ssp = false; - pairing_cb.is_le_only = true; - pairing_cb.is_le_nc = false; + bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); + pairing_cb.is_ssp = false; + pairing_cb.is_le_only = true; + pairing_cb.is_le_nc = false; - BTM_BleOobDataReply(req_oob_type->bd_addr, 0, 16, oob_cb.oob_data.sm_tk); + BTM_BleOobDataReply(req_oob_type->bd_addr, 0, 16, oob_cb.oob_data.sm_tk); } +static void btif_dm_ble_sc_oob_req_evt(tBTA_DM_SP_RMT_OOB* req_oob_type) { + BTIF_TRACE_DEBUG("%s", __func__); -static void btif_dm_ble_sc_oob_req_evt(tBTA_DM_SP_RMT_OOB *req_oob_type) -{ - BTIF_TRACE_DEBUG("%s", __func__); + bt_bdaddr_t bd_addr; + bdcpy(bd_addr.address, req_oob_type->bd_addr); - bt_bdaddr_t bd_addr; - bdcpy(bd_addr.address, req_oob_type->bd_addr); - - /* We already checked if OOB data is present in - * btif_dm_set_oob_for_le_io_req, but check here again. If it's not present - * do nothing, pairing will timeout. - */ - if (!is_empty_128bit(oob_cb.oob_data.le_sc_c) && - !is_empty_128bit(oob_cb.oob_data.le_sc_r)) { - return; - } + /* We already checked if OOB data is present in + * btif_dm_set_oob_for_le_io_req, but check here again. If it's not present + * do nothing, pairing will timeout. + */ + if (!is_empty_128bit(oob_cb.oob_data.le_sc_c) && + !is_empty_128bit(oob_cb.oob_data.le_sc_r)) { + return; + } - /* make sure OOB data is for this particular device */ - if (memcmp(req_oob_type->bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) != 0) { - BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address", __func__); - return; - } + /* make sure OOB data is for this particular device */ + if (memcmp(req_oob_type->bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) != 0) { + BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address", + __func__); + return; + } - /* Remote name update */ - btif_update_remote_properties(req_oob_type->bd_addr , req_oob_type->bd_name, - NULL, BT_DEVICE_TYPE_BLE); + /* Remote name update */ + btif_update_remote_properties(req_oob_type->bd_addr, req_oob_type->bd_name, + NULL, BT_DEVICE_TYPE_BLE); - bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); - pairing_cb.is_ssp = false; - pairing_cb.is_le_only = true; //TODO: we can derive classic pairing from this one - pairing_cb.is_le_nc = false; + bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING); + pairing_cb.is_ssp = false; + pairing_cb.is_le_only = + true; // TODO: we can derive classic pairing from this one + pairing_cb.is_le_nc = false; - BTM_BleSecureConnectionOobDataReply(req_oob_type->bd_addr, - oob_cb.oob_data.le_sc_c, - oob_cb.oob_data.le_sc_r); + BTM_BleSecureConnectionOobDataReply( + req_oob_type->bd_addr, oob_cb.oob_data.le_sc_c, oob_cb.oob_data.le_sc_r); } -void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name, - tBT_DEVICE_TYPE dev_type) -{ - btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type); +void btif_dm_update_ble_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name, + tBT_DEVICE_TYPE dev_type) { + btif_update_remote_properties(bd_addr, bd_name, NULL, dev_type); } -static void btif_dm_ble_tx_test_cback(void *p) -{ - btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST, - (char *)p, 1, NULL); +static void btif_dm_ble_tx_test_cback(void* p) { + btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST, (char*)p, 1, + NULL); } -static void btif_dm_ble_rx_test_cback(void *p) -{ - btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST, - (char *)p, 1, NULL); +static void btif_dm_ble_rx_test_cback(void* p) { + btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST, (char*)p, 1, + NULL); } -static void btif_dm_ble_test_end_cback(void *p) -{ - btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END, - (char *)p, 3, NULL); +static void btif_dm_ble_test_end_cback(void* p) { + btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END, (char*)p, + 3, NULL); } /******************************************************************************* -** -** Function btif_le_test_mode -** -** Description Sends a HCI BLE Test command to the Controller -** -** Returns BT_STATUS_SUCCESS on success -** -*******************************************************************************/ -bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len) -{ - switch (opcode) { - case HCI_BLE_TRANSMITTER_TEST: - if (len != 3) return BT_STATUS_PARM_INVALID; - BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback); - break; - case HCI_BLE_RECEIVER_TEST: - if (len != 1) return BT_STATUS_PARM_INVALID; - BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback); - break; - case HCI_BLE_TEST_END: - BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback); - break; - default: - BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __func__, opcode); - return BT_STATUS_UNSUPPORTED; - } - return BT_STATUS_SUCCESS; + * + * Function btif_le_test_mode + * + * Description Sends a HCI BLE Test command to the Controller + * + * Returns BT_STATUS_SUCCESS on success + * + ******************************************************************************/ +bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) { + switch (opcode) { + case HCI_BLE_TRANSMITTER_TEST: + if (len != 3) return BT_STATUS_PARM_INVALID; + BTM_BleTransmitterTest(buf[0], buf[1], buf[2], btif_dm_ble_tx_test_cback); + break; + case HCI_BLE_RECEIVER_TEST: + if (len != 1) return BT_STATUS_PARM_INVALID; + BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback); + break; + case HCI_BLE_TEST_END: + BTM_BleTestEnd((tBTM_CMPL_CB*)btif_dm_ble_test_end_cback); + break; + default: + BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __func__, + opcode); + return BT_STATUS_UNSUPPORTED; + } + return BT_STATUS_SUCCESS; } #endif -void btif_dm_on_disable() -{ - /* cancel any pending pairing requests */ - if (pairing_cb.state == BT_BOND_STATE_BONDING) - { - bt_bdaddr_t bd_addr; +void btif_dm_on_disable() { + /* cancel any pending pairing requests */ + if (pairing_cb.state == BT_BOND_STATE_BONDING) { + bt_bdaddr_t bd_addr; - BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __func__); - bdcpy(bd_addr.address, pairing_cb.bd_addr); - btif_dm_cancel_bond(&bd_addr); - } + BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __func__); + bdcpy(bd_addr.address, pairing_cb.bd_addr); + btif_dm_cancel_bond(&bd_addr); + } } /******************************************************************************* -** -** Function btif_dm_read_energy_info -** -** Description Reads the energy info from controller -** -** Returns void -** -*******************************************************************************/ -void btif_dm_read_energy_info() -{ + * + * Function btif_dm_read_energy_info + * + * Description Reads the energy info from controller + * + * Returns void + * + ******************************************************************************/ +void btif_dm_read_energy_info() { #if (BLE_INCLUDED == TRUE) - BTA_DmBleGetEnergyInfo(bta_energy_info_cb); + BTA_DmBleGetEnergyInfo(bta_energy_info_cb); #endif } static char* btif_get_default_local_name() { - if (btif_default_local_name[0] == '\0') - { - int max_len = sizeof(btif_default_local_name) - 1; - if (BTM_DEF_LOCAL_NAME[0] != '\0') - { - strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len); - } - else - { - char prop_model[PROPERTY_VALUE_MAX]; - osi_property_get(PROPERTY_PRODUCT_MODEL, prop_model, ""); - strncpy(btif_default_local_name, prop_model, max_len); - } - btif_default_local_name[max_len] = '\0'; + if (btif_default_local_name[0] == '\0') { + int max_len = sizeof(btif_default_local_name) - 1; + if (BTM_DEF_LOCAL_NAME[0] != '\0') { + strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len); + } else { + char prop_model[PROPERTY_VALUE_MAX]; + osi_property_get(PROPERTY_PRODUCT_MODEL, prop_model, ""); + strncpy(btif_default_local_name, prop_model, max_len); } - return btif_default_local_name; + btif_default_local_name[max_len] = '\0'; + } + return btif_default_local_name; } -static void btif_stats_add_bond_event(const bt_bdaddr_t *bd_addr, +static void btif_stats_add_bond_event(const bt_bdaddr_t* bd_addr, bt_bond_function_t function, bt_bond_state_t state) { - std::unique_lock lock(bond_event_lock); - - btif_bond_event_t* event = &btif_dm_bond_events[btif_events_end_index]; - memcpy(&event->bd_addr, bd_addr, sizeof(bt_bdaddr_t)); - event->function = function; - event->state = state; - clock_gettime(CLOCK_REALTIME, &event->timestamp); - - btif_num_bond_events++; - btif_events_end_index = (btif_events_end_index + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1); - if (btif_events_end_index == btif_events_start_index) { - btif_events_start_index = (btif_events_start_index + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1); - } - - int type; - btif_get_device_type(bd_addr->address, &type); - - device_type_t device_type; - switch (type) { - case BT_DEVICE_TYPE_BREDR: - device_type = DEVICE_TYPE_BREDR; - break; - case BT_DEVICE_TYPE_BLE: - device_type = DEVICE_TYPE_LE; - break; - case BT_DEVICE_TYPE_DUMO: - device_type = DEVICE_TYPE_DUMO; - break; - default: - device_type = DEVICE_TYPE_UNKNOWN; - break; - } - - uint32_t cod = get_cod(bd_addr); - uint64_t ts = event->timestamp.tv_sec * 1000 + - event->timestamp.tv_nsec / 1000000; - metrics_pair_event(0, ts, cod, device_type); - + std::unique_lock lock(bond_event_lock); + + btif_bond_event_t* event = &btif_dm_bond_events[btif_events_end_index]; + memcpy(&event->bd_addr, bd_addr, sizeof(bt_bdaddr_t)); + event->function = function; + event->state = state; + clock_gettime(CLOCK_REALTIME, &event->timestamp); + + btif_num_bond_events++; + btif_events_end_index = + (btif_events_end_index + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1); + if (btif_events_end_index == btif_events_start_index) { + btif_events_start_index = + (btif_events_start_index + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1); + } + + int type; + btif_get_device_type(bd_addr->address, &type); + + device_type_t device_type; + switch (type) { + case BT_DEVICE_TYPE_BREDR: + device_type = DEVICE_TYPE_BREDR; + break; + case BT_DEVICE_TYPE_BLE: + device_type = DEVICE_TYPE_LE; + break; + case BT_DEVICE_TYPE_DUMO: + device_type = DEVICE_TYPE_DUMO; + break; + default: + device_type = DEVICE_TYPE_UNKNOWN; + break; + } + + uint32_t cod = get_cod(bd_addr); + uint64_t ts = + event->timestamp.tv_sec * 1000 + event->timestamp.tv_nsec / 1000000; + metrics_pair_event(0, ts, cod, device_type); } void btif_debug_bond_event_dump(int fd) { - std::unique_lock lock(bond_event_lock); - dprintf(fd, "\nBond Events: \n"); - dprintf(fd, " Total Number of events: %zu\n", btif_num_bond_events); - if (btif_num_bond_events > 0) - dprintf(fd, " Time BD_ADDR Function State\n"); - - for (size_t i = btif_events_start_index; i != btif_events_end_index; - i = (i + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1)) { - btif_bond_event_t* event = &btif_dm_bond_events[i]; - - char eventtime[20]; - char temptime[20]; - struct tm *tstamp = localtime(&event->timestamp.tv_sec); - strftime(temptime, sizeof(temptime), "%H:%M:%S", tstamp); - snprintf(eventtime, sizeof(eventtime), "%s.%03ld", temptime, - event->timestamp.tv_nsec / 1000000); - - char bdaddr[18]; - bdaddr_to_string(&event->bd_addr, bdaddr, sizeof(bdaddr)); - - const char* func_name; - switch (event->function) { - case BTIF_DM_FUNC_CREATE_BOND: - func_name = "btif_dm_create_bond"; - break; - case BTIF_DM_FUNC_REMOVE_BOND: - func_name = "btif_dm_remove_bond"; - break; - case BTIF_DM_FUNC_BOND_STATE_CHANGED: - func_name = "bond_state_changed "; - break; - default: - func_name = "Invalid value "; - break; - } + std::unique_lock lock(bond_event_lock); + dprintf(fd, "\nBond Events: \n"); + dprintf(fd, " Total Number of events: %zu\n", btif_num_bond_events); + if (btif_num_bond_events > 0) + dprintf(fd, + " Time BD_ADDR Function State\n"); + + for (size_t i = btif_events_start_index; i != btif_events_end_index; + i = (i + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1)) { + btif_bond_event_t* event = &btif_dm_bond_events[i]; + + char eventtime[20]; + char temptime[20]; + struct tm* tstamp = localtime(&event->timestamp.tv_sec); + strftime(temptime, sizeof(temptime), "%H:%M:%S", tstamp); + snprintf(eventtime, sizeof(eventtime), "%s.%03ld", temptime, + event->timestamp.tv_nsec / 1000000); + + char bdaddr[18]; + bdaddr_to_string(&event->bd_addr, bdaddr, sizeof(bdaddr)); + + const char* func_name; + switch (event->function) { + case BTIF_DM_FUNC_CREATE_BOND: + func_name = "btif_dm_create_bond"; + break; + case BTIF_DM_FUNC_REMOVE_BOND: + func_name = "btif_dm_remove_bond"; + break; + case BTIF_DM_FUNC_BOND_STATE_CHANGED: + func_name = "bond_state_changed "; + break; + default: + func_name = "Invalid value "; + break; + } - const char* bond_state; - switch (event->state) { - case BT_BOND_STATE_NONE: - bond_state = "BOND_STATE_NONE"; - break; - case BT_BOND_STATE_BONDING: - bond_state = "BOND_STATE_BONDING"; - break; - case BT_BOND_STATE_BONDED: - bond_state = "BOND_STATE_BONDED"; - break; - default: - bond_state = "Invalid bond state"; - break; - } - dprintf(fd, " %s %s %s %s\n", eventtime, bdaddr, func_name, bond_state); + const char* bond_state; + switch (event->state) { + case BT_BOND_STATE_NONE: + bond_state = "BOND_STATE_NONE"; + break; + case BT_BOND_STATE_BONDING: + bond_state = "BOND_STATE_BONDING"; + break; + case BT_BOND_STATE_BONDED: + bond_state = "BOND_STATE_BONDED"; + break; + default: + bond_state = "Invalid bond state"; + break; } + dprintf(fd, " %s %s %s %s\n", eventtime, bdaddr, func_name, bond_state); + } } diff --git a/btif/src/btif_gatt.cc b/btif/src/btif_gatt.cc index d24ed8ec6..d9926e298 100644 --- a/btif/src/btif_gatt.cc +++ b/btif/src/btif_gatt.cc @@ -44,39 +44,36 @@ #include "btif_gatt_util.h" #include "btif_storage.h" -const btgatt_callbacks_t *bt_gatt_callbacks = NULL; +const btgatt_callbacks_t* bt_gatt_callbacks = NULL; /******************************************************************************* -** -** Function btif_gatt_init -** -** Description Initializes the GATT interface -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t btif_gatt_init( const btgatt_callbacks_t* callbacks ) -{ - bt_gatt_callbacks = callbacks; - return BT_STATUS_SUCCESS; + * + * Function btif_gatt_init + * + * Description Initializes the GATT interface + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t btif_gatt_init(const btgatt_callbacks_t* callbacks) { + bt_gatt_callbacks = callbacks; + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_gatt_cleanup -** -** Description Closes the GATT interface -** -** Returns void -** -*******************************************************************************/ -static void btif_gatt_cleanup( void ) -{ - if (bt_gatt_callbacks) - bt_gatt_callbacks = NULL; + * + * Function btif_gatt_cleanup + * + * Description Closes the GATT interface + * + * Returns void + * + ******************************************************************************/ +static void btif_gatt_cleanup(void) { + if (bt_gatt_callbacks) bt_gatt_callbacks = NULL; - BTA_GATTC_Disable(); - BTA_GATTS_Disable(); + BTA_GATTC_Disable(); + BTA_GATTS_Disable(); } static btgatt_interface_t btgattInterface = { @@ -87,20 +84,19 @@ static btgatt_interface_t btgattInterface = { &btgattClientInterface, &btgattServerInterface, - nullptr //filled in btif_gatt_get_interface + nullptr // filled in btif_gatt_get_interface }; /******************************************************************************* -** -** Function btif_gatt_get_interface -** -** Description Get the gatt callback interface -** -** Returns btgatt_interface_t -** -*******************************************************************************/ -const btgatt_interface_t *btif_gatt_get_interface() -{ + * + * Function btif_gatt_get_interface + * + * Description Get the gatt callback interface + * + * Returns btgatt_interface_t + * + ******************************************************************************/ +const btgatt_interface_t* btif_gatt_get_interface() { // TODO(jpawlowski) right now initializing advertiser field in static // structure cause explosion of dependencies. It must be initialized here // until those dependencies are properly abstracted for tests. diff --git a/btif/src/btif_gatt_client.cc b/btif/src/btif_gatt_client.cc index d21c9e410..c034c0f9f 100644 --- a/btif/src/btif_gatt_client.cc +++ b/btif/src/btif_gatt_client.cc @@ -52,7 +52,6 @@ #include "btif_gatt.h" #include "btif_gatt_util.h" #include "btif_storage.h" -#include "btif_storage.h" #include "osi/include/log.h" #include "vendor_api.h" @@ -60,14 +59,14 @@ using base::Bind; using base::Owned; using std::vector; -extern bt_status_t do_in_jni_thread(const base::Closure &task); +extern bt_status_t do_in_jni_thread(const base::Closure& task); extern bt_status_t btif_gattc_test_command_impl(int command, - btgatt_test_params_t *params); -extern const btgatt_callbacks_t *bt_gatt_callbacks; + btgatt_test_params_t* params); +extern const btgatt_callbacks_t* bt_gatt_callbacks; /******************************************************************************* -** Constants & Macros -********************************************************************************/ + * Constants & Macros + *******************************************************************************/ #define CLI_CBACK_IN_JNI(P_CBACK, ...) \ if (bt_gatt_callbacks && bt_gatt_callbacks->client->P_CBACK) { \ @@ -77,15 +76,16 @@ extern const btgatt_callbacks_t *bt_gatt_callbacks; ASSERTC(0, "Callback is NULL", 0); \ } -#define CHECK_BTGATT_INIT() \ - if (bt_gatt_callbacks == NULL) { \ +#define CHECK_BTGATT_INIT() \ + if (bt_gatt_callbacks == NULL) { \ LOG_WARN(LOG_TAG, "%s: BTGATT not initialized", __func__); \ - return BT_STATUS_NOT_READY; \ - } else { \ + return BT_STATUS_NOT_READY; \ + } else { \ LOG_VERBOSE(LOG_TAG, "%s", __func__); \ } -#define BLE_RESOLVE_ADDR_MSB 0x40 /* bit7, bit6 is 01 to be resolvable random \ +#define BLE_RESOLVE_ADDR_MSB \ + 0x40 /* bit7, bit6 is 01 to be resolvable random \ */ #define BLE_RESOLVE_ADDR_MASK 0xc0 /* bit 6, and bit7 */ #define BTM_BLE_IS_RESOLVE_BDA(x) \ @@ -94,7 +94,7 @@ extern const btgatt_callbacks_t *bt_gatt_callbacks; namespace std { template <> struct hash { - size_t operator()(const bt_bdaddr_t &f) const { + size_t operator()(const bt_bdaddr_t& f) const { return f.address[0] + f.address[1] + f.address[2] + f.address[3] + f.address[4] + f.address[5]; } @@ -102,7 +102,7 @@ struct hash { template <> struct equal_to { - size_t operator()(const bt_bdaddr_t &x, const bt_bdaddr_t &y) const { + size_t operator()(const bt_bdaddr_t& x, const bt_bdaddr_t& y) const { return memcmp(x.address, y.address, BD_ADDR_LEN); } }; @@ -181,10 +181,10 @@ bool btif_gattc_find_bdaddr(BD_ADDR p_bda) { return (p_dev_cb.count(bd_addr) != 0); } -void btif_gattc_upstreams_evt(uint16_t event, char *p_param) { +void btif_gattc_upstreams_evt(uint16_t event, char* p_param) { LOG_VERBOSE(LOG_TAG, "%s: Event %d", __func__, event); - tBTA_GATTC *p_data = (tBTA_GATTC *)p_param; + tBTA_GATTC* p_data = (tBTA_GATTC*)p_param; switch (event) { case BTA_GATTC_REG_EVT: { bt_uuid_t app_uuid; @@ -286,10 +286,10 @@ void btif_gattc_upstreams_evt(uint16_t event, char *p_param) { } } -void bta_gattc_cback(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) { +void bta_gattc_cback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data) { bt_status_t status = btif_transfer_context(btif_gattc_upstreams_evt, (uint16_t)event, - (char *)p_data, sizeof(tBTA_GATTC), NULL); + (char*)p_data, sizeof(tBTA_GATTC), NULL); ASSERTC(status == BT_STATUS_SUCCESS, "Context transfer failed!", status); } @@ -316,7 +316,8 @@ void bta_batch_scan_setup_cb(tBTA_BLE_BATCH_SCAN_EVT evt, } case BTA_BLE_BATCH_SCAN_DATA_EVT: { - CLI_CBACK_IN_JNI(batchscan_reports_cb, ref_value, status, 0, 0, vector()); + CLI_CBACK_IN_JNI(batchscan_reports_cb, ref_value, status, 0, 0, + vector()); return; } @@ -336,13 +337,12 @@ void bta_batch_scan_threshold_cb(tBTA_DM_BLE_REF_VALUE ref_value) { void bta_batch_scan_reports_cb(tBTA_DM_BLE_REF_VALUE ref_value, uint8_t report_format, uint8_t num_records, - uint16_t data_len, uint8_t *p_rep_data, + uint16_t data_len, uint8_t* p_rep_data, tBTA_STATUS status) { - BTIF_TRACE_DEBUG("%s - client_if:%d, %d, %d, %d", __func__, ref_value, - status, num_records, data_len); + BTIF_TRACE_DEBUG("%s - client_if:%d, %d, %d, %d", __func__, ref_value, status, + num_records, data_len); if (data_len > 0) { - vector data(p_rep_data, p_rep_data + data_len); osi_free(p_rep_data); @@ -358,18 +358,16 @@ void bta_scan_results_cb_impl(bt_bdaddr_t bd_addr, tBT_DEVICE_TYPE device_type, int8_t rssi, uint8_t addr_type, vector value) { uint8_t remote_name_len; - const uint8_t *p_eir_remote_name = NULL; + const uint8_t* p_eir_remote_name = NULL; bt_device_type_t dev_type; bt_property_t properties; - p_eir_remote_name = BTM_CheckEirData(value.data(), - BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, - &remote_name_len); + p_eir_remote_name = BTM_CheckEirData( + value.data(), BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len); if (p_eir_remote_name == NULL) { - p_eir_remote_name = BTM_CheckEirData(value.data(), - BT_EIR_SHORTENED_LOCAL_NAME_TYPE, - &remote_name_len); + p_eir_remote_name = BTM_CheckEirData( + value.data(), BT_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len); } if ((addr_type != BLE_ADDR_RANDOM) || (p_eir_remote_name)) { @@ -381,8 +379,8 @@ void bta_scan_results_cb_impl(bt_bdaddr_t bd_addr, tBT_DEVICE_TYPE device_type, memcpy(bdname.name, p_eir_remote_name, remote_name_len); bdname.name[remote_name_len] = '\0'; - LOG_VERBOSE(LOG_TAG, "%s BLE device name=%s len=%d dev_type=%d", __func__, - bdname.name, remote_name_len, device_type); + LOG_VERBOSE(LOG_TAG, "%s BLE device name=%s len=%d dev_type=%d", + __func__, bdname.name, remote_name_len, device_type); btif_dm_update_ble_remote_properties(bd_addr.address, bdname.name, device_type); } @@ -396,10 +394,11 @@ void bta_scan_results_cb_impl(bt_bdaddr_t bd_addr, tBT_DEVICE_TYPE device_type, btif_storage_set_remote_addr_type(&bd_addr, addr_type); - HAL_CBACK(bt_gatt_callbacks, client->scan_result_cb, &bd_addr, rssi, std::move(value)); + HAL_CBACK(bt_gatt_callbacks, client->scan_result_cb, &bd_addr, rssi, + std::move(value)); } -void bta_scan_results_cb(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data) { +void bta_scan_results_cb(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH* p_data) { uint8_t len; if (event == BTA_DM_INQ_CMPL_EVT) { @@ -415,7 +414,8 @@ void bta_scan_results_cb(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data) { vector value(BTGATT_MAX_ATTR_LEN); if (p_data->inq_res.p_eir) { - value.insert(value.begin(), p_data->inq_res.p_eir, p_data->inq_res.p_eir + 62); + value.insert(value.begin(), p_data->inq_res.p_eir, + p_data->inq_res.p_eir + 62); if (BTM_CheckEirData(p_data->inq_res.p_eir, BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &len)) { @@ -430,21 +430,20 @@ void bta_scan_results_cb(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data) { p_data->inq_res.ble_addr_type, std::move(value))); } -void bta_track_adv_event_cb(tBTA_DM_BLE_TRACK_ADV_DATA *p_track_adv_data) { - btgatt_track_adv_info_t *btif_scan_track_cb = new btgatt_track_adv_info_t; +void bta_track_adv_event_cb(tBTA_DM_BLE_TRACK_ADV_DATA* p_track_adv_data) { + btgatt_track_adv_info_t* btif_scan_track_cb = new btgatt_track_adv_info_t; BTIF_TRACE_DEBUG("%s", __func__); btif_gatt_move_track_adv_data(btif_scan_track_cb, - (btgatt_track_adv_info_t *)p_track_adv_data); + (btgatt_track_adv_info_t*)p_track_adv_data); CLI_CBACK_IN_JNI(track_adv_event_cb, Owned(btif_scan_track_cb)); } -void btm_read_rssi_cb(tBTM_RSSI_RESULTS *p_result) { - if (!p_result) - return; +void btm_read_rssi_cb(tBTM_RSSI_RESULTS* p_result) { + if (!p_result) return; - bt_bdaddr_t *addr = new bt_bdaddr_t; + bt_bdaddr_t* addr = new bt_bdaddr_t; bdcpy(addr->address, p_result->rem_bda); CLI_CBACK_IN_JNI(read_remote_rssi_cb, rssi_request_client_if, base::Owned(addr), p_result->rssi, p_result->status); @@ -477,14 +476,14 @@ void bta_scan_filt_status_cb(uint8_t action, tBTA_STATUS status, } /******************************************************************************* -** Client API Functions -********************************************************************************/ + * Client API Functions + *******************************************************************************/ void btif_gattc_register_app_impl(tBT_UUID uuid) { BTA_GATTC_AppRegister(&uuid, bta_gattc_cback); } -bt_status_t btif_gattc_register_app(bt_uuid_t *uuid) { +bt_status_t btif_gattc_register_app(bt_uuid_t* uuid) { CHECK_BTGATT_INIT(); tBT_UUID bt_uuid; @@ -506,7 +505,7 @@ bt_status_t btif_gattc_scan(bool start) { if (start) { btif_gattc_init_dev_cb(); return do_in_jni_thread(Bind(&BTA_DmBleObserve, true, 0, - (tBTA_DM_SEARCH_CBACK *)bta_scan_results_cb)); + (tBTA_DM_SEARCH_CBACK*)bta_scan_results_cb)); } else { return do_in_jni_thread(Bind(&BTA_DmBleObserve, false, 0, nullptr)); } @@ -536,7 +535,7 @@ void btif_gattc_open_impl(int client_if, BD_ADDR address, bool is_direct, BTM_BleGetVendorCapabilities(&vnd_capabilities); if (!vnd_capabilities.rpa_offloading) { HAL_CBACK(bt_gatt_callbacks, client->open_cb, 0, BT_STATUS_UNSUPPORTED, - client_if, (bt_bdaddr_t *)&address); + client_if, (bt_bdaddr_t*)&address); return; } } @@ -571,11 +570,11 @@ void btif_gattc_open_impl(int client_if, BD_ADDR address, bool is_direct, BTA_GATTC_Open(client_if, address, is_direct, transport); } -bt_status_t btif_gattc_open(int client_if, const bt_bdaddr_t *bd_addr, +bt_status_t btif_gattc_open(int client_if, const bt_bdaddr_t* bd_addr, bool is_direct, int transport) { CHECK_BTGATT_INIT(); // Closure will own this value and free it. - uint8_t *address = new BD_ADDR; + uint8_t* address = new BD_ADDR; bdcpy(address, bd_addr->address); return do_in_jni_thread(Bind(&btif_gattc_open_impl, client_if, base::Owned(address), is_direct, transport)); @@ -592,11 +591,11 @@ void btif_gattc_close_impl(int client_if, BD_ADDR address, int conn_id) { BTA_GATTC_CancelOpen(client_if, address, false); } -bt_status_t btif_gattc_close(int client_if, const bt_bdaddr_t *bd_addr, +bt_status_t btif_gattc_close(int client_if, const bt_bdaddr_t* bd_addr, int conn_id) { CHECK_BTGATT_INIT(); // Closure will own this value and free it. - uint8_t *address = new BD_ADDR; + uint8_t* address = new BD_ADDR; bdcpy(address, bd_addr->address); return do_in_jni_thread( Bind(&btif_gattc_close_impl, client_if, base::Owned(address), conn_id)); @@ -612,19 +611,19 @@ bt_status_t btif_gattc_listen(int client_if, bool start) { #endif } -bt_status_t btif_gattc_refresh(int client_if, const bt_bdaddr_t *bd_addr) { +bt_status_t btif_gattc_refresh(int client_if, const bt_bdaddr_t* bd_addr) { CHECK_BTGATT_INIT(); // Closure will own this value and free it. - uint8_t *address = new BD_ADDR; + uint8_t* address = new BD_ADDR; bdcpy(address, bd_addr->address); return do_in_jni_thread(Bind(&BTA_GATTC_Refresh, base::Owned(address))); } -bt_status_t btif_gattc_search_service(int conn_id, bt_uuid_t *filter_uuid) { +bt_status_t btif_gattc_search_service(int conn_id, bt_uuid_t* filter_uuid) { CHECK_BTGATT_INIT(); if (filter_uuid) { - tBT_UUID *uuid = new tBT_UUID; + tBT_UUID* uuid = new tBT_UUID; btif_to_bta_uuid(uuid, filter_uuid); return do_in_jni_thread( Bind(&BTA_GATTC_ServiceSearchRequest, conn_id, base::Owned(uuid))); @@ -635,7 +634,7 @@ bt_status_t btif_gattc_search_service(int conn_id, bt_uuid_t *filter_uuid) { } void btif_gattc_get_gatt_db_impl(int conn_id) { - btgatt_db_element_t *db = NULL; + btgatt_db_element_t* db = NULL; int count = 0; BTA_GATTC_GetGattDb(conn_id, 0x0000, 0xFFFF, &db, &count); @@ -649,8 +648,8 @@ bt_status_t btif_gattc_get_gatt_db(int conn_id) { } void read_char_cb(uint16_t conn_id, tGATT_STATUS status, uint16_t handle, - uint16_t len, uint8_t *value, void *data) { - btgatt_read_params_t *params = new btgatt_read_params_t; + uint16_t len, uint8_t* value, void* data) { + btgatt_read_params_t* params = new btgatt_read_params_t; params->value_type = 0x00 /* GATTC_READ_VALUE_TYPE_VALUE */; params->status = status; params->handle = handle; @@ -669,8 +668,8 @@ bt_status_t btif_gattc_read_char(int conn_id, uint16_t handle, int auth_req) { } void read_desc_cb(uint16_t conn_id, tGATT_STATUS status, uint16_t handle, - uint16_t len, uint8_t *value, void *data) { - btgatt_read_params_t *params = new btgatt_read_params_t; + uint16_t len, uint8_t* value, void* data) { + btgatt_read_params_t* params = new btgatt_read_params_t; params->value_type = 0x00 /* GATTC_READ_VALUE_TYPE_VALUE */; params->status = status; params->handle = handle; @@ -689,7 +688,7 @@ bt_status_t btif_gattc_read_char_descr(int conn_id, uint16_t handle, } void write_char_cb(uint16_t conn_id, tGATT_STATUS status, uint16_t handle, - void *data) { + void* data) { CLI_CBACK_IN_JNI(write_characteristic_cb, conn_id, status, handle); } @@ -697,8 +696,7 @@ bt_status_t btif_gattc_write_char(int conn_id, uint16_t handle, int write_type, int auth_req, vector value) { CHECK_BTGATT_INIT(); - if (value.size() > BTGATT_MAX_ATTR_LEN) - value.resize(BTGATT_MAX_ATTR_LEN); + if (value.size() > BTGATT_MAX_ATTR_LEN) value.resize(BTGATT_MAX_ATTR_LEN); return do_in_jni_thread(Bind(&BTA_GATTC_WriteCharValue, conn_id, handle, write_type, std::move(value), auth_req, @@ -706,7 +704,7 @@ bt_status_t btif_gattc_write_char(int conn_id, uint16_t handle, int write_type, } void write_descr_cb(uint16_t conn_id, tGATT_STATUS status, uint16_t handle, - void *data) { + void* data) { CLI_CBACK_IN_JNI(write_descriptor_cb, conn_id, status, handle); } @@ -714,12 +712,11 @@ bt_status_t btif_gattc_write_char_descr(int conn_id, uint16_t handle, int auth_req, vector value) { CHECK_BTGATT_INIT(); - if (value.size() > BTGATT_MAX_ATTR_LEN) - value.resize(BTGATT_MAX_ATTR_LEN); + if (value.size() > BTGATT_MAX_ATTR_LEN) value.resize(BTGATT_MAX_ATTR_LEN); return do_in_jni_thread(Bind(&BTA_GATTC_WriteCharDescr, conn_id, handle, - std::move(value), auth_req, - write_descr_cb, nullptr)); + std::move(value), auth_req, write_descr_cb, + nullptr)); } bt_status_t btif_gattc_execute_write(int conn_id, int execute) { @@ -731,7 +728,7 @@ bt_status_t btif_gattc_execute_write(int conn_id, int execute) { void btif_gattc_reg_for_notification_impl(tBTA_GATTC_IF client_if, const BD_ADDR bda, uint16_t handle) { tBTA_GATT_STATUS status = BTA_GATTC_RegisterForNotifications( - client_if, const_cast(bda), handle); + client_if, const_cast(bda), handle); // TODO(jpawlowski): conn_id is currently unused HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb, @@ -739,11 +736,11 @@ void btif_gattc_reg_for_notification_impl(tBTA_GATTC_IF client_if, } bt_status_t btif_gattc_reg_for_notification(int client_if, - const bt_bdaddr_t *bd_addr, + const bt_bdaddr_t* bd_addr, uint16_t handle) { CHECK_BTGATT_INIT(); - uint8_t *address = new BD_ADDR; + uint8_t* address = new BD_ADDR; bdcpy(address, bd_addr->address); return do_in_jni_thread( Bind(base::IgnoreResult(&btif_gattc_reg_for_notification_impl), client_if, @@ -751,9 +748,10 @@ bt_status_t btif_gattc_reg_for_notification(int client_if, } void btif_gattc_dereg_for_notification_impl(tBTA_GATTC_IF client_if, - const BD_ADDR bda, uint16_t handle) { + const BD_ADDR bda, + uint16_t handle) { tBTA_GATT_STATUS status = BTA_GATTC_DeregisterForNotifications( - client_if, const_cast(bda), handle); + client_if, const_cast(bda), handle); // TODO(jpawlowski): conn_id is currently unused HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb, @@ -761,11 +759,11 @@ void btif_gattc_dereg_for_notification_impl(tBTA_GATTC_IF client_if, } bt_status_t btif_gattc_dereg_for_notification(int client_if, - const bt_bdaddr_t *bd_addr, + const bt_bdaddr_t* bd_addr, uint16_t handle) { CHECK_BTGATT_INIT(); - uint8_t *address = new BD_ADDR; + uint8_t* address = new BD_ADDR; bdcpy(address, bd_addr->address); return do_in_jni_thread( Bind(base::IgnoreResult(&btif_gattc_dereg_for_notification_impl), @@ -773,15 +771,15 @@ bt_status_t btif_gattc_dereg_for_notification(int client_if, } bt_status_t btif_gattc_read_remote_rssi(int client_if, - const bt_bdaddr_t *bd_addr) { + const bt_bdaddr_t* bd_addr) { CHECK_BTGATT_INIT(); rssi_request_client_if = client_if; // Closure will own this value and free it. - uint8_t *address = new BD_ADDR; + uint8_t* address = new BD_ADDR; bdcpy(address, bd_addr->address); return do_in_jni_thread(Bind(base::IgnoreResult(&BTM_ReadRSSI), base::Owned(address), - (tBTM_CMPL_CB *)btm_read_rssi_cb)); + (tBTM_CMPL_CB*)btm_read_rssi_cb)); } bt_status_t btif_gattc_configure_mtu(int conn_id, int mtu) { @@ -794,25 +792,25 @@ void btif_gattc_conn_parameter_update_impl(bt_bdaddr_t addr, int min_interval, int max_interval, int latency, int timeout) { if (BTA_DmGetConnectionState(addr.address)) - BTA_DmBleUpdateConnectionParams(addr.address, min_interval, - max_interval, latency, timeout); + BTA_DmBleUpdateConnectionParams(addr.address, min_interval, max_interval, + latency, timeout); else - BTA_DmSetBlePrefConnParams(addr.address, min_interval, - max_interval, latency, timeout); + BTA_DmSetBlePrefConnParams(addr.address, min_interval, max_interval, + latency, timeout); } -bt_status_t btif_gattc_conn_parameter_update(const bt_bdaddr_t *bd_addr, +bt_status_t btif_gattc_conn_parameter_update(const bt_bdaddr_t* bd_addr, int min_interval, int max_interval, int latency, int timeout) { CHECK_BTGATT_INIT(); return do_in_jni_thread( - Bind(base::IgnoreResult(&btif_gattc_conn_parameter_update_impl), - *bd_addr, min_interval, max_interval, latency, timeout)); + Bind(base::IgnoreResult(&btif_gattc_conn_parameter_update_impl), *bd_addr, + min_interval, max_interval, latency, timeout)); } void btif_gattc_scan_filter_param_setup_impl( int client_if, uint8_t action, int filt_index, - tBTA_DM_BLE_PF_FILT_PARAMS *adv_filt_param) { + tBTA_DM_BLE_PF_FILT_PARAMS* adv_filt_param) { if (1 == adv_filt_param->dely_mode) BTA_DmBleTrackAdvertiser(client_if, bta_track_adv_event_cb); BTA_DmBleScanFilterSetup(action, filt_index, adv_filt_param, NULL, @@ -824,7 +822,7 @@ bt_status_t btif_gattc_scan_filter_param_setup( CHECK_BTGATT_INIT(); BTIF_TRACE_DEBUG("%s", __func__); - tBTA_DM_BLE_PF_FILT_PARAMS *adv_filt_param = new tBTA_DM_BLE_PF_FILT_PARAMS; + tBTA_DM_BLE_PF_FILT_PARAMS* adv_filt_param = new tBTA_DM_BLE_PF_FILT_PARAMS; adv_filt_param->feat_seln = filt_param.feat_seln; adv_filt_param->list_logic_type = filt_param.list_logic_type; adv_filt_param->filt_logic_type = filt_param.filt_logic_type; @@ -843,7 +841,7 @@ bt_status_t btif_gattc_scan_filter_param_setup( } void btif_gattc_scan_filter_add_srvc_uuid(tBT_UUID uuid, - tBTA_DM_BLE_PF_COND_MASK *p_uuid_mask, + tBTA_DM_BLE_PF_COND_MASK* p_uuid_mask, int action, int filt_type, int filt_index, int client_if) { tBTA_DM_BLE_PF_COND_PARAM cond; @@ -858,9 +856,9 @@ void btif_gattc_scan_filter_add_srvc_uuid(tBT_UUID uuid, &bta_scan_filt_cfg_cb, client_if); } -void btif_gattc_scan_filter_add_local_name(vector data, - int action, int filt_type, - int filt_index, int client_if) { +void btif_gattc_scan_filter_add_local_name(vector data, int action, + int filt_type, int filt_index, + int client_if) { tBTA_DM_BLE_PF_COND_PARAM cond; memset(&cond, 0, sizeof(tBTA_DM_BLE_PF_COND_PARAM)); @@ -903,8 +901,8 @@ void btif_gattc_scan_filter_add_data_pattern(vector pattern, bt_status_t btif_gattc_scan_filter_add_remove( int client_if, int action, int filt_type, int filt_index, int company_id, - int company_id_mask, const bt_uuid_t *p_uuid, const bt_uuid_t *p_uuid_mask, - const bt_bdaddr_t *bd_addr, char addr_type, vector data, + int company_id_mask, const bt_uuid_t* p_uuid, const bt_uuid_t* p_uuid_mask, + const bt_bdaddr_t* bd_addr, char addr_type, vector data, vector mask) { CHECK_BTGATT_INIT(); BTIF_TRACE_DEBUG("%s, %d, %d", __func__, action, filt_type); @@ -916,7 +914,7 @@ bt_status_t btif_gattc_scan_filter_add_remove( switch (filt_type) { case BTA_DM_BLE_PF_ADDR_FILTER: // 0 { - tBTA_DM_BLE_PF_COND_PARAM *cond = new tBTA_DM_BLE_PF_COND_PARAM; + tBTA_DM_BLE_PF_COND_PARAM* cond = new tBTA_DM_BLE_PF_COND_PARAM; memset(cond, 0, sizeof(tBTA_DM_BLE_PF_COND_PARAM)); bdcpy(cond->target_addr.bda, bd_addr->address); @@ -937,7 +935,7 @@ bt_status_t btif_gattc_scan_filter_add_remove( btif_to_bta_uuid(&bt_uuid, p_uuid); if (p_uuid_mask != NULL) { - tBTA_DM_BLE_PF_COND_MASK *uuid_mask = new tBTA_DM_BLE_PF_COND_MASK; + tBTA_DM_BLE_PF_COND_MASK* uuid_mask = new tBTA_DM_BLE_PF_COND_MASK; btif_to_bta_uuid_mask(uuid_mask, p_uuid_mask, p_uuid); return do_in_jni_thread(Bind(&btif_gattc_scan_filter_add_srvc_uuid, bt_uuid, base::Owned(uuid_mask), action, @@ -951,7 +949,7 @@ bt_status_t btif_gattc_scan_filter_add_remove( case BTA_DM_BLE_PF_SRVC_SOL_UUID: // 3 { - tBTA_DM_BLE_PF_COND_PARAM *cond = new tBTA_DM_BLE_PF_COND_PARAM; + tBTA_DM_BLE_PF_COND_PARAM* cond = new tBTA_DM_BLE_PF_COND_PARAM; memset(cond, 0, sizeof(tBTA_DM_BLE_PF_COND_PARAM)); cond->solicitate_uuid.p_target_addr = NULL; @@ -972,17 +970,17 @@ bt_status_t btif_gattc_scan_filter_add_remove( case BTA_DM_BLE_PF_MANU_DATA: // 5 { - return do_in_jni_thread( - Bind(&btif_gattc_scan_filter_add_manu_data, company_id, - company_id_mask, std::move(data), std::move(mask), action, - filt_type, filt_index, client_if)); + return do_in_jni_thread(Bind(&btif_gattc_scan_filter_add_manu_data, + company_id, company_id_mask, std::move(data), + std::move(mask), action, filt_type, + filt_index, client_if)); } case BTA_DM_BLE_PF_SRVC_DATA_PATTERN: // 6 { - return do_in_jni_thread(Bind( - &btif_gattc_scan_filter_add_data_pattern, std::move(data), - std::move(mask), action, filt_type, filt_index, client_if)); + return do_in_jni_thread(Bind(&btif_gattc_scan_filter_add_data_pattern, + std::move(data), std::move(mask), action, + filt_type, filt_index, client_if)); } default: @@ -1005,7 +1003,7 @@ bt_status_t btif_gattc_scan_filter_enable(int client_if, bool enable) { CHECK_BTGATT_INIT(); BTIF_TRACE_DEBUG("%s: enable: %d", __func__, enable); - uint8_t action = enable ? 1: 0; + uint8_t action = enable ? 1 : 0; return do_in_jni_thread(Bind(&BTA_DmEnableScanFilter, action, &bta_scan_filt_status_cb, client_if)); @@ -1020,7 +1018,7 @@ bt_status_t btif_gattc_set_scan_parameters(int client_if, int scan_interval, (tBLE_SCAN_PARAM_SETUP_CBACK)bta_scan_param_setup_cb)); } -int btif_gattc_get_device_type(const bt_bdaddr_t *bd_addr) { +int btif_gattc_get_device_type(const bt_bdaddr_t* bd_addr) { int device_type = 0; char bd_addr_str[18] = {0}; @@ -1037,9 +1035,9 @@ bt_status_t btif_gattc_cfg_storage(int client_if, int batch_scan_full_max, return do_in_jni_thread( Bind(BTA_DmBleSetStorageParams, batch_scan_full_max, batch_scan_trunc_max, batch_scan_notify_threshold, - (tBTA_BLE_SCAN_SETUP_CBACK *)bta_batch_scan_setup_cb, - (tBTA_BLE_SCAN_THRESHOLD_CBACK *)bta_batch_scan_threshold_cb, - (tBTA_BLE_SCAN_REP_CBACK *)bta_batch_scan_reports_cb, + (tBTA_BLE_SCAN_SETUP_CBACK*)bta_batch_scan_setup_cb, + (tBTA_BLE_SCAN_THRESHOLD_CBACK*)bta_batch_scan_threshold_cb, + (tBTA_BLE_SCAN_REP_CBACK*)bta_batch_scan_reports_cb, (tBTA_DM_BLE_REF_VALUE)client_if)); } @@ -1062,7 +1060,7 @@ bt_status_t btif_gattc_read_batch_scan_reports(int client_if, int scan_mode) { return do_in_jni_thread(Bind(BTA_DmBleReadScanReports, scan_mode, client_if)); } -bt_status_t btif_gattc_test_command(int command, btgatt_test_params_t *params) { +bt_status_t btif_gattc_test_command(int command, btgatt_test_params_t* params) { return btif_gattc_test_command_impl(command, params); } diff --git a/btif/src/btif_gatt_server.cc b/btif/src/btif_gatt_server.cc index bb067d5b9..d27d4bd9e 100644 --- a/btif/src/btif_gatt_server.cc +++ b/btif/src/btif_gatt_server.cc @@ -40,6 +40,7 @@ #if (BLE_INCLUDED == TRUE) +#include "bt_common.h" #include "bta_api.h" #include "bta_gatt_api.h" #include "btif_config.h" @@ -47,7 +48,6 @@ #include "btif_gatt.h" #include "btif_gatt_util.h" #include "btif_storage.h" -#include "bt_common.h" #include "osi/include/log.h" using base::Bind; @@ -57,242 +57,220 @@ using std::vector; extern bt_status_t do_in_jni_thread(const base::Closure& task); /************************************************************************************ -** Constants & Macros -************************************************************************************/ - -#define CHECK_BTGATT_INIT() if (bt_gatt_callbacks == NULL)\ - {\ - LOG_WARN(LOG_TAG, "%s: BTGATT not initialized", __func__);\ - return BT_STATUS_NOT_READY;\ - } else {\ - LOG_VERBOSE(LOG_TAG, "%s", __func__);\ - } + * Constants & Macros + ***********************************************************************************/ + +#define CHECK_BTGATT_INIT() \ + if (bt_gatt_callbacks == NULL) { \ + LOG_WARN(LOG_TAG, "%s: BTGATT not initialized", __func__); \ + return BT_STATUS_NOT_READY; \ + } else { \ + LOG_VERBOSE(LOG_TAG, "%s", __func__); \ + } /************************************************************************************ -** Static variables -************************************************************************************/ + * Static variables + ***********************************************************************************/ -extern const btgatt_callbacks_t *bt_gatt_callbacks; +extern const btgatt_callbacks_t* bt_gatt_callbacks; /************************************************************************************ -** Static functions -************************************************************************************/ + * Static functions + ***********************************************************************************/ -static void btapp_gatts_copy_req_data(uint16_t event, char *p_dest, char *p_src) -{ - tBTA_GATTS *p_dest_data = (tBTA_GATTS*) p_dest; - tBTA_GATTS *p_src_data = (tBTA_GATTS*) p_src; +static void btapp_gatts_copy_req_data(uint16_t event, char* p_dest, + char* p_src) { + tBTA_GATTS* p_dest_data = (tBTA_GATTS*)p_dest; + tBTA_GATTS* p_src_data = (tBTA_GATTS*)p_src; + + if (!p_src_data || !p_dest_data) return; + + // Copy basic structure first + maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data)); + + // Allocate buffer for request data if necessary + switch (event) { + case BTA_GATTS_READ_CHARACTERISTIC_EVT: + case BTA_GATTS_READ_DESCRIPTOR_EVT: + case BTA_GATTS_WRITE_CHARACTERISTIC_EVT: + case BTA_GATTS_WRITE_DESCRIPTOR_EVT: + case BTA_GATTS_EXEC_WRITE_EVT: + case BTA_GATTS_MTU_EVT: + p_dest_data->req_data.p_data = + (tBTA_GATTS_REQ_DATA*)osi_malloc(sizeof(tBTA_GATTS_REQ_DATA)); + memcpy(p_dest_data->req_data.p_data, p_src_data->req_data.p_data, + sizeof(tBTA_GATTS_REQ_DATA)); + break; + + default: + break; + } +} - if (!p_src_data || !p_dest_data) - return; +static void btapp_gatts_free_req_data(uint16_t event, tBTA_GATTS* p_data) { + switch (event) { + case BTA_GATTS_READ_CHARACTERISTIC_EVT: + case BTA_GATTS_READ_DESCRIPTOR_EVT: + case BTA_GATTS_WRITE_CHARACTERISTIC_EVT: + case BTA_GATTS_WRITE_DESCRIPTOR_EVT: + case BTA_GATTS_EXEC_WRITE_EVT: + case BTA_GATTS_MTU_EVT: + if (p_data != NULL) osi_free_and_reset((void**)&p_data->req_data.p_data); + break; + + default: + break; + } +} - // Copy basic structure first - maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data)); - - // Allocate buffer for request data if necessary - switch (event) - { - case BTA_GATTS_READ_CHARACTERISTIC_EVT: - case BTA_GATTS_READ_DESCRIPTOR_EVT: - case BTA_GATTS_WRITE_CHARACTERISTIC_EVT: - case BTA_GATTS_WRITE_DESCRIPTOR_EVT: - case BTA_GATTS_EXEC_WRITE_EVT: - case BTA_GATTS_MTU_EVT: - p_dest_data->req_data.p_data = (tBTA_GATTS_REQ_DATA *)osi_malloc(sizeof(tBTA_GATTS_REQ_DATA)); - memcpy(p_dest_data->req_data.p_data, p_src_data->req_data.p_data, - sizeof(tBTA_GATTS_REQ_DATA)); - break; - - default: - break; +static void btapp_gatts_handle_cback(uint16_t event, char* p_param) { + LOG_VERBOSE(LOG_TAG, "%s: Event %d", __func__, event); + + tBTA_GATTS* p_data = (tBTA_GATTS*)p_param; + switch (event) { + case BTA_GATTS_REG_EVT: { + bt_uuid_t app_uuid; + bta_to_btif_uuid(&app_uuid, &p_data->reg_oper.uuid); + HAL_CBACK(bt_gatt_callbacks, server->register_server_cb, + p_data->reg_oper.status, p_data->reg_oper.server_if, &app_uuid); + break; } -} -static void btapp_gatts_free_req_data(uint16_t event, tBTA_GATTS *p_data) -{ - switch (event) - { - case BTA_GATTS_READ_CHARACTERISTIC_EVT: - case BTA_GATTS_READ_DESCRIPTOR_EVT: - case BTA_GATTS_WRITE_CHARACTERISTIC_EVT: - case BTA_GATTS_WRITE_DESCRIPTOR_EVT: - case BTA_GATTS_EXEC_WRITE_EVT: - case BTA_GATTS_MTU_EVT: - if (p_data != NULL) - osi_free_and_reset((void **)&p_data->req_data.p_data); - break; - - default: - break; + case BTA_GATTS_DEREG_EVT: + break; + + case BTA_GATTS_CONNECT_EVT: { + bt_bdaddr_t bda; + bdcpy(bda.address, p_data->conn.remote_bda); + + btif_gatt_check_encrypted_link(p_data->conn.remote_bda, + p_data->conn.transport); + + HAL_CBACK(bt_gatt_callbacks, server->connection_cb, p_data->conn.conn_id, + p_data->conn.server_if, true, &bda); + break; } -} -static void btapp_gatts_handle_cback(uint16_t event, char* p_param) -{ - LOG_VERBOSE(LOG_TAG, "%s: Event %d", __func__, event); - - tBTA_GATTS *p_data = (tBTA_GATTS*)p_param; - switch (event) - { - case BTA_GATTS_REG_EVT: - { - bt_uuid_t app_uuid; - bta_to_btif_uuid(&app_uuid, &p_data->reg_oper.uuid); - HAL_CBACK(bt_gatt_callbacks, server->register_server_cb - , p_data->reg_oper.status - , p_data->reg_oper.server_if - , &app_uuid - ); - break; - } - - case BTA_GATTS_DEREG_EVT: - break; - - case BTA_GATTS_CONNECT_EVT: - { - bt_bdaddr_t bda; - bdcpy(bda.address, p_data->conn.remote_bda); - - btif_gatt_check_encrypted_link(p_data->conn.remote_bda, p_data->conn.transport); - - HAL_CBACK(bt_gatt_callbacks, server->connection_cb, - p_data->conn.conn_id, p_data->conn.server_if, true, &bda); - break; - } - - case BTA_GATTS_DISCONNECT_EVT: - { - bt_bdaddr_t bda; - bdcpy(bda.address, p_data->conn.remote_bda); - - HAL_CBACK(bt_gatt_callbacks, server->connection_cb, - p_data->conn.conn_id, p_data->conn.server_if, false, &bda); - break; - } - - case BTA_GATTS_STOP_EVT: - HAL_CBACK(bt_gatt_callbacks, server->service_stopped_cb, - p_data->srvc_oper.status, - p_data->srvc_oper.server_if, - p_data->srvc_oper.service_id); - break; - - case BTA_GATTS_DELELTE_EVT: - HAL_CBACK(bt_gatt_callbacks, server->service_deleted_cb, - p_data->srvc_oper.status, - p_data->srvc_oper.server_if, - p_data->srvc_oper.service_id); - break; - - case BTA_GATTS_READ_CHARACTERISTIC_EVT: - { - bt_bdaddr_t bda; - bdcpy(bda.address, p_data->req_data.remote_bda); - - HAL_CBACK(bt_gatt_callbacks, server->request_read_characteristic_cb, - p_data->req_data.conn_id,p_data->req_data.trans_id, &bda, - p_data->req_data.p_data->read_req.handle, - p_data->req_data.p_data->read_req.offset, - p_data->req_data.p_data->read_req.is_long); - break; - } - - case BTA_GATTS_READ_DESCRIPTOR_EVT: - { - bt_bdaddr_t bda; - bdcpy(bda.address, p_data->req_data.remote_bda); - - HAL_CBACK(bt_gatt_callbacks, server->request_read_descriptor_cb, - p_data->req_data.conn_id,p_data->req_data.trans_id, &bda, - p_data->req_data.p_data->read_req.handle, - p_data->req_data.p_data->read_req.offset, - p_data->req_data.p_data->read_req.is_long); - break; - } - - case BTA_GATTS_WRITE_CHARACTERISTIC_EVT: - { - bt_bdaddr_t bda; - bdcpy(bda.address, p_data->req_data.remote_bda); - const auto &req = p_data->req_data.p_data->write_req; - vector value(req.value, req.value + req.len); - HAL_CBACK(bt_gatt_callbacks, server->request_write_characteristic_cb, - p_data->req_data.conn_id,p_data->req_data.trans_id, &bda, - req.handle, req.offset, req.need_rsp, req.is_prep, value); - break; - } - - case BTA_GATTS_WRITE_DESCRIPTOR_EVT: - { - bt_bdaddr_t bda; - bdcpy(bda.address, p_data->req_data.remote_bda); - const auto &req = p_data->req_data.p_data->write_req; - vector value(req.value, req.value + req.len); - HAL_CBACK(bt_gatt_callbacks, server->request_write_descriptor_cb, - p_data->req_data.conn_id,p_data->req_data.trans_id, &bda, - req.handle, req.offset, req.need_rsp, req.is_prep, value); - break; - } - - case BTA_GATTS_EXEC_WRITE_EVT: - { - bt_bdaddr_t bda; - bdcpy(bda.address, p_data->req_data.remote_bda); - - HAL_CBACK(bt_gatt_callbacks, server->request_exec_write_cb, - p_data->req_data.conn_id,p_data->req_data.trans_id, &bda, - p_data->req_data.p_data->exec_write); - break; - } - - case BTA_GATTS_CONF_EVT: - HAL_CBACK(bt_gatt_callbacks, server->indication_sent_cb, - p_data->req_data.conn_id, p_data->req_data.status); - break; - - case BTA_GATTS_CONGEST_EVT: - HAL_CBACK(bt_gatt_callbacks, server->congestion_cb - , p_data->congest.conn_id - , p_data->congest.congested - ); - break; - - case BTA_GATTS_MTU_EVT: - HAL_CBACK(bt_gatt_callbacks, server->mtu_changed_cb - , p_data->req_data.conn_id - , p_data->req_data.p_data->mtu - ); - break; - - case BTA_GATTS_OPEN_EVT: - case BTA_GATTS_CANCEL_OPEN_EVT: - case BTA_GATTS_CLOSE_EVT: - LOG_DEBUG(LOG_TAG, "%s: Empty event (%d)!", __func__, event); - break; - - default: - LOG_ERROR(LOG_TAG, "%s: Unhandled event (%d)!", __func__, event); - break; + case BTA_GATTS_DISCONNECT_EVT: { + bt_bdaddr_t bda; + bdcpy(bda.address, p_data->conn.remote_bda); + + HAL_CBACK(bt_gatt_callbacks, server->connection_cb, p_data->conn.conn_id, + p_data->conn.server_if, false, &bda); + break; } - btapp_gatts_free_req_data(event, p_data); + case BTA_GATTS_STOP_EVT: + HAL_CBACK(bt_gatt_callbacks, server->service_stopped_cb, + p_data->srvc_oper.status, p_data->srvc_oper.server_if, + p_data->srvc_oper.service_id); + break; + + case BTA_GATTS_DELELTE_EVT: + HAL_CBACK(bt_gatt_callbacks, server->service_deleted_cb, + p_data->srvc_oper.status, p_data->srvc_oper.server_if, + p_data->srvc_oper.service_id); + break; + + case BTA_GATTS_READ_CHARACTERISTIC_EVT: { + bt_bdaddr_t bda; + bdcpy(bda.address, p_data->req_data.remote_bda); + + HAL_CBACK(bt_gatt_callbacks, server->request_read_characteristic_cb, + p_data->req_data.conn_id, p_data->req_data.trans_id, &bda, + p_data->req_data.p_data->read_req.handle, + p_data->req_data.p_data->read_req.offset, + p_data->req_data.p_data->read_req.is_long); + break; + } + + case BTA_GATTS_READ_DESCRIPTOR_EVT: { + bt_bdaddr_t bda; + bdcpy(bda.address, p_data->req_data.remote_bda); + + HAL_CBACK(bt_gatt_callbacks, server->request_read_descriptor_cb, + p_data->req_data.conn_id, p_data->req_data.trans_id, &bda, + p_data->req_data.p_data->read_req.handle, + p_data->req_data.p_data->read_req.offset, + p_data->req_data.p_data->read_req.is_long); + break; + } + + case BTA_GATTS_WRITE_CHARACTERISTIC_EVT: { + bt_bdaddr_t bda; + bdcpy(bda.address, p_data->req_data.remote_bda); + const auto& req = p_data->req_data.p_data->write_req; + vector value(req.value, req.value + req.len); + HAL_CBACK(bt_gatt_callbacks, server->request_write_characteristic_cb, + p_data->req_data.conn_id, p_data->req_data.trans_id, &bda, + req.handle, req.offset, req.need_rsp, req.is_prep, value); + break; + } + + case BTA_GATTS_WRITE_DESCRIPTOR_EVT: { + bt_bdaddr_t bda; + bdcpy(bda.address, p_data->req_data.remote_bda); + const auto& req = p_data->req_data.p_data->write_req; + vector value(req.value, req.value + req.len); + HAL_CBACK(bt_gatt_callbacks, server->request_write_descriptor_cb, + p_data->req_data.conn_id, p_data->req_data.trans_id, &bda, + req.handle, req.offset, req.need_rsp, req.is_prep, value); + break; + } + + case BTA_GATTS_EXEC_WRITE_EVT: { + bt_bdaddr_t bda; + bdcpy(bda.address, p_data->req_data.remote_bda); + + HAL_CBACK(bt_gatt_callbacks, server->request_exec_write_cb, + p_data->req_data.conn_id, p_data->req_data.trans_id, &bda, + p_data->req_data.p_data->exec_write); + break; + } + + case BTA_GATTS_CONF_EVT: + HAL_CBACK(bt_gatt_callbacks, server->indication_sent_cb, + p_data->req_data.conn_id, p_data->req_data.status); + break; + + case BTA_GATTS_CONGEST_EVT: + HAL_CBACK(bt_gatt_callbacks, server->congestion_cb, + p_data->congest.conn_id, p_data->congest.congested); + break; + + case BTA_GATTS_MTU_EVT: + HAL_CBACK(bt_gatt_callbacks, server->mtu_changed_cb, + p_data->req_data.conn_id, p_data->req_data.p_data->mtu); + break; + + case BTA_GATTS_OPEN_EVT: + case BTA_GATTS_CANCEL_OPEN_EVT: + case BTA_GATTS_CLOSE_EVT: + LOG_DEBUG(LOG_TAG, "%s: Empty event (%d)!", __func__, event); + break; + + default: + LOG_ERROR(LOG_TAG, "%s: Unhandled event (%d)!", __func__, event); + break; + } + + btapp_gatts_free_req_data(event, p_data); } -static void btapp_gatts_cback(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) -{ - bt_status_t status; - status = btif_transfer_context(btapp_gatts_handle_cback, (uint16_t) event, - (char*)p_data, sizeof(tBTA_GATTS), btapp_gatts_copy_req_data); - ASSERTC(status == BT_STATUS_SUCCESS, "Context transfer failed!", status); +static void btapp_gatts_cback(tBTA_GATTS_EVT event, tBTA_GATTS* p_data) { + bt_status_t status; + status = btif_transfer_context(btapp_gatts_handle_cback, (uint16_t)event, + (char*)p_data, sizeof(tBTA_GATTS), + btapp_gatts_copy_req_data); + ASSERTC(status == BT_STATUS_SUCCESS, "Context transfer failed!", status); } /************************************************************************************ -** Server API Functions -************************************************************************************/ -static bt_status_t btif_gatts_register_app(bt_uuid_t *bt_uuid) { + * Server API Functions + ***********************************************************************************/ +static bt_status_t btif_gatts_register_app(bt_uuid_t* bt_uuid) { CHECK_BTGATT_INIT(); - tBT_UUID *uuid = new tBT_UUID; + tBT_UUID* uuid = new tBT_UUID; btif_to_bta_uuid(uuid, bt_uuid); return do_in_jni_thread( @@ -350,18 +328,17 @@ static void btif_gatts_open_impl(int server_if, BD_ADDR address, bool is_direct, BTA_GATTS_Open(server_if, address, is_direct, transport); } -static bt_status_t btif_gatts_open(int server_if, const bt_bdaddr_t *bd_addr, +static bt_status_t btif_gatts_open(int server_if, const bt_bdaddr_t* bd_addr, bool is_direct, int transport) { CHECK_BTGATT_INIT(); - uint8_t *address = new BD_ADDR; + uint8_t* address = new BD_ADDR; bdcpy(address, bd_addr->address); return do_in_jni_thread(Bind(&btif_gatts_open_impl, server_if, base::Owned(address), is_direct, transport)); } -static void btif_gatts_close_impl(int server_if, BD_ADDR address, - int conn_id) { +static void btif_gatts_close_impl(int server_if, BD_ADDR address, int conn_id) { // Cancel pending foreground/background connections BTA_GATTS_CancelOpen(server_if, address, true); BTA_GATTS_CancelOpen(server_if, address, false); @@ -370,37 +347,39 @@ static void btif_gatts_close_impl(int server_if, BD_ADDR address, if (conn_id != 0) BTA_GATTS_Close(conn_id); } -static bt_status_t btif_gatts_close(int server_if, const bt_bdaddr_t *bd_addr, +static bt_status_t btif_gatts_close(int server_if, const bt_bdaddr_t* bd_addr, int conn_id) { CHECK_BTGATT_INIT(); - uint8_t *address = new BD_ADDR; + uint8_t* address = new BD_ADDR; bdcpy(address, bd_addr->address); return do_in_jni_thread( Bind(&btif_gatts_close_impl, server_if, base::Owned(address), conn_id)); } -static void add_service_impl(int server_if, vector service) { +static void add_service_impl(int server_if, + vector service) { int status = BTA_GATTS_AddService(server_if, service); - HAL_CBACK(bt_gatt_callbacks, server->service_added_cb, status, server_if, std::move(service)); + HAL_CBACK(bt_gatt_callbacks, server->service_added_cb, status, server_if, + std::move(service)); } static bt_status_t btif_gatts_add_service(int server_if, vector service) { CHECK_BTGATT_INIT(); - return do_in_jni_thread(Bind(&add_service_impl, server_if, std::move(service))); + return do_in_jni_thread( + Bind(&add_service_impl, server_if, std::move(service))); } -static bt_status_t btif_gatts_stop_service(int server_if, int service_handle) -{ - CHECK_BTGATT_INIT(); - return do_in_jni_thread(Bind(&BTA_GATTS_StopService, service_handle)); +static bt_status_t btif_gatts_stop_service(int server_if, int service_handle) { + CHECK_BTGATT_INIT(); + return do_in_jni_thread(Bind(&BTA_GATTS_StopService, service_handle)); } -static bt_status_t btif_gatts_delete_service(int server_if, int service_handle) -{ - CHECK_BTGATT_INIT(); - return do_in_jni_thread(Bind(&BTA_GATTS_DeleteService, service_handle)); +static bt_status_t btif_gatts_delete_service(int server_if, + int service_handle) { + CHECK_BTGATT_INIT(); + return do_in_jni_thread(Bind(&BTA_GATTS_DeleteService, service_handle)); } static bt_status_t btif_gatts_send_indication(int server_if, @@ -409,8 +388,7 @@ static bt_status_t btif_gatts_send_indication(int server_if, vector value) { CHECK_BTGATT_INIT(); - if (value.size() > BTGATT_MAX_ATTR_LEN) - value.resize(BTGATT_MAX_ATTR_LEN); + if (value.size() > BTGATT_MAX_ATTR_LEN) value.resize(BTGATT_MAX_ATTR_LEN); return do_in_jni_thread(Bind(&BTA_GATTS_HandleValueIndication, conn_id, attribute_handle, std::move(value), confirm)); @@ -431,22 +409,17 @@ static void btif_gatts_send_response_impl(int conn_id, int trans_id, int status, static bt_status_t btif_gatts_send_response(int conn_id, int trans_id, int status, - btgatt_response_t *response) { + btgatt_response_t* response) { CHECK_BTGATT_INIT(); return do_in_jni_thread(Bind(&btif_gatts_send_response_impl, conn_id, trans_id, status, *response)); } const btgatt_server_interface_t btgattServerInterface = { - btif_gatts_register_app, - btif_gatts_unregister_app, - btif_gatts_open, - btif_gatts_close, - btif_gatts_add_service, - btif_gatts_stop_service, - btif_gatts_delete_service, - btif_gatts_send_indication, - btif_gatts_send_response -}; + btif_gatts_register_app, btif_gatts_unregister_app, + btif_gatts_open, btif_gatts_close, + btif_gatts_add_service, btif_gatts_stop_service, + btif_gatts_delete_service, btif_gatts_send_indication, + btif_gatts_send_response}; #endif diff --git a/btif/src/btif_gatt_test.cc b/btif/src/btif_gatt_test.cc index f1ead43d6..d72af71f1 100644 --- a/btif/src/btif_gatt_test.cc +++ b/btif/src/btif_gatt_test.cc @@ -44,25 +44,21 @@ * Typedefs & Macros *******************************************************************************/ -typedef struct -{ - tGATT_IF gatt_if; - uint16_t conn_id; +typedef struct { + tGATT_IF gatt_if; + uint16_t conn_id; } btif_test_cb_t; /******************************************************************************* * Static variables *******************************************************************************/ -static const char * disc_name[GATT_DISC_MAX] = -{ - "Unknown", - "GATT_DISC_SRVC_ALL", - "GATT_DISC_SRVC_BY_UUID", - "GATT_DISC_INC_SRVC", - "GATT_DISC_CHAR", - "GATT_DISC_CHAR_DSCPT" -}; +static const char* disc_name[GATT_DISC_MAX] = {"Unknown", + "GATT_DISC_SRVC_ALL", + "GATT_DISC_SRVC_BY_UUID", + "GATT_DISC_INC_SRVC", + "GATT_DISC_CHAR", + "GATT_DISC_CHAR_DSCPT"}; static btif_test_cb_t test_cb; @@ -70,233 +66,235 @@ static btif_test_cb_t test_cb; * Callback functions *******************************************************************************/ -static char * format_uuid(tBT_UUID bt_uuid, char *str_buf, size_t buf_size) -{ - - - if (bt_uuid.len == LEN_UUID_16) - { - snprintf(str_buf, buf_size, "0x%04x", bt_uuid.uu.uuid16); - } - else if (bt_uuid.len == LEN_UUID_128) - { - int x = snprintf(str_buf, buf_size, - "%02x%02x%02x%02x-%02x%02x-%02x%02x", - bt_uuid.uu.uuid128[15], bt_uuid.uu.uuid128[14], - bt_uuid.uu.uuid128[13], bt_uuid.uu.uuid128[12], - bt_uuid.uu.uuid128[11], bt_uuid.uu.uuid128[10], - bt_uuid.uu.uuid128[9], bt_uuid.uu.uuid128[8]); - snprintf(&str_buf[x], buf_size - x, - "%02x%02x-%02x%02x%02x%02x%02x%02x", - bt_uuid.uu.uuid128[7], bt_uuid.uu.uuid128[6], - bt_uuid.uu.uuid128[5], bt_uuid.uu.uuid128[4], - bt_uuid.uu.uuid128[3], bt_uuid.uu.uuid128[2], - bt_uuid.uu.uuid128[1], bt_uuid.uu.uuid128[0]); - } - else { - snprintf(str_buf, buf_size, "Unknown (len=%d)", bt_uuid.len); - } - - return str_buf; +static char* format_uuid(tBT_UUID bt_uuid, char* str_buf, size_t buf_size) { + if (bt_uuid.len == LEN_UUID_16) { + snprintf(str_buf, buf_size, "0x%04x", bt_uuid.uu.uuid16); + } else if (bt_uuid.len == LEN_UUID_128) { + int x = snprintf(str_buf, buf_size, "%02x%02x%02x%02x-%02x%02x-%02x%02x", + bt_uuid.uu.uuid128[15], bt_uuid.uu.uuid128[14], + bt_uuid.uu.uuid128[13], bt_uuid.uu.uuid128[12], + bt_uuid.uu.uuid128[11], bt_uuid.uu.uuid128[10], + bt_uuid.uu.uuid128[9], bt_uuid.uu.uuid128[8]); + snprintf(&str_buf[x], buf_size - x, "%02x%02x-%02x%02x%02x%02x%02x%02x", + bt_uuid.uu.uuid128[7], bt_uuid.uu.uuid128[6], + bt_uuid.uu.uuid128[5], bt_uuid.uu.uuid128[4], + bt_uuid.uu.uuid128[3], bt_uuid.uu.uuid128[2], + bt_uuid.uu.uuid128[1], bt_uuid.uu.uuid128[0]); + } else { + snprintf(str_buf, buf_size, "Unknown (len=%d)", bt_uuid.len); + } + + return str_buf; } -static void btif_test_connect_cback(tGATT_IF gatt_if, BD_ADDR bda, uint16_t conn_id, - bool connected, tGATT_DISCONN_REASON reason, tBT_TRANSPORT transport) -{ - UNUSED(gatt_if); - UNUSED(bda); - UNUSED(reason); - UNUSED (transport); - - LOG_DEBUG(LOG_TAG, "%s: conn_id=%d, connected=%d", __func__, conn_id, connected); - test_cb.conn_id = connected ? conn_id : 0; +static void btif_test_connect_cback(tGATT_IF gatt_if, BD_ADDR bda, + uint16_t conn_id, bool connected, + tGATT_DISCONN_REASON reason, + tBT_TRANSPORT transport) { + UNUSED(gatt_if); + UNUSED(bda); + UNUSED(reason); + UNUSED(transport); + + LOG_DEBUG(LOG_TAG, "%s: conn_id=%d, connected=%d", __func__, conn_id, + connected); + test_cb.conn_id = connected ? conn_id : 0; } static void btif_test_command_complete_cback(uint16_t conn_id, tGATTC_OPTYPE op, - tGATT_STATUS status, tGATT_CL_COMPLETE *p_data) -{ - LOG_DEBUG(LOG_TAG, "%s: op_code=0x%02x, conn_id=0x%x. status=0x%x", - __func__, op, conn_id, status); - - switch (op) - { - case GATTC_OPTYPE_READ: - case GATTC_OPTYPE_WRITE: - case GATTC_OPTYPE_CONFIG: - case GATTC_OPTYPE_EXE_WRITE: - case GATTC_OPTYPE_NOTIFICATION: - break; - - case GATTC_OPTYPE_INDICATION: - GATTC_SendHandleValueConfirm(conn_id, p_data->handle); - break; - - default: - LOG_DEBUG(LOG_TAG, "%s: Unknown op_code (0x%02x)", __func__, op); - break; - } + tGATT_STATUS status, + tGATT_CL_COMPLETE* p_data) { + LOG_DEBUG(LOG_TAG, "%s: op_code=0x%02x, conn_id=0x%x. status=0x%x", __func__, + op, conn_id, status); + + switch (op) { + case GATTC_OPTYPE_READ: + case GATTC_OPTYPE_WRITE: + case GATTC_OPTYPE_CONFIG: + case GATTC_OPTYPE_EXE_WRITE: + case GATTC_OPTYPE_NOTIFICATION: + break; + + case GATTC_OPTYPE_INDICATION: + GATTC_SendHandleValueConfirm(conn_id, p_data->handle); + break; + + default: + LOG_DEBUG(LOG_TAG, "%s: Unknown op_code (0x%02x)", __func__, op); + break; + } } -static void btif_test_discovery_result_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type, - tGATT_DISC_RES *p_data) -{ - char str_buf[50]; - UNUSED(conn_id); - - LOG_DEBUG(LOG_TAG, "------ GATT Discovery result %-22s -------", disc_name[disc_type]); - LOG_DEBUG(LOG_TAG, " Attribute handle: 0x%04x (%d)", p_data->handle, p_data->handle); - - if (disc_type != GATT_DISC_CHAR_DSCPT) { - LOG_DEBUG(LOG_TAG, " Attribute type: %s", format_uuid(p_data->type, str_buf, sizeof(str_buf))); - } - - switch (disc_type) - { - case GATT_DISC_SRVC_ALL: - LOG_DEBUG(LOG_TAG, " Handle range: 0x%04x ~ 0x%04x (%d ~ %d)", - p_data->handle, p_data->value.group_value.e_handle, - p_data->handle, p_data->value.group_value.e_handle); - LOG_DEBUG(LOG_TAG, " Service UUID: %s", - format_uuid(p_data->value.group_value.service_type, str_buf, sizeof(str_buf))); - break; - - case GATT_DISC_SRVC_BY_UUID: - LOG_DEBUG(LOG_TAG, " Handle range: 0x%04x ~ 0x%04x (%d ~ %d)", - p_data->handle, p_data->value.handle, - p_data->handle, p_data->value.handle); - break; - - case GATT_DISC_INC_SRVC: - LOG_DEBUG(LOG_TAG, " Handle range: 0x%04x ~ 0x%04x (%d ~ %d)", - p_data->value.incl_service.s_handle, p_data->value.incl_service.e_handle, - p_data->value.incl_service.s_handle, p_data->value.incl_service.e_handle); - LOG_DEBUG(LOG_TAG, " Service UUID: %s", - format_uuid(p_data->value.incl_service.service_type, str_buf, sizeof(str_buf))); - break; - - case GATT_DISC_CHAR: - LOG_DEBUG(LOG_TAG, " Properties: 0x%02x", - p_data->value.dclr_value.char_prop); - LOG_DEBUG(LOG_TAG, " Characteristic UUID: %s", - format_uuid(p_data->value.dclr_value.char_uuid, str_buf, sizeof(str_buf))); - break; - - case GATT_DISC_CHAR_DSCPT: - LOG_DEBUG(LOG_TAG, " Descriptor UUID: %s", format_uuid(p_data->type, str_buf, sizeof(str_buf))); - break; - } - - LOG_DEBUG(LOG_TAG, "-----------------------------------------------------------"); +static void btif_test_discovery_result_cback(uint16_t conn_id, + tGATT_DISC_TYPE disc_type, + tGATT_DISC_RES* p_data) { + char str_buf[50]; + UNUSED(conn_id); + + LOG_DEBUG(LOG_TAG, "------ GATT Discovery result %-22s -------", + disc_name[disc_type]); + LOG_DEBUG(LOG_TAG, " Attribute handle: 0x%04x (%d)", p_data->handle, + p_data->handle); + + if (disc_type != GATT_DISC_CHAR_DSCPT) { + LOG_DEBUG(LOG_TAG, " Attribute type: %s", + format_uuid(p_data->type, str_buf, sizeof(str_buf))); + } + + switch (disc_type) { + case GATT_DISC_SRVC_ALL: + LOG_DEBUG(LOG_TAG, " Handle range: 0x%04x ~ 0x%04x (%d ~ %d)", + p_data->handle, p_data->value.group_value.e_handle, + p_data->handle, p_data->value.group_value.e_handle); + LOG_DEBUG(LOG_TAG, " Service UUID: %s", + format_uuid(p_data->value.group_value.service_type, str_buf, + sizeof(str_buf))); + break; + + case GATT_DISC_SRVC_BY_UUID: + LOG_DEBUG(LOG_TAG, " Handle range: 0x%04x ~ 0x%04x (%d ~ %d)", + p_data->handle, p_data->value.handle, p_data->handle, + p_data->value.handle); + break; + + case GATT_DISC_INC_SRVC: + LOG_DEBUG(LOG_TAG, " Handle range: 0x%04x ~ 0x%04x (%d ~ %d)", + p_data->value.incl_service.s_handle, + p_data->value.incl_service.e_handle, + p_data->value.incl_service.s_handle, + p_data->value.incl_service.e_handle); + LOG_DEBUG(LOG_TAG, " Service UUID: %s", + format_uuid(p_data->value.incl_service.service_type, str_buf, + sizeof(str_buf))); + break; + + case GATT_DISC_CHAR: + LOG_DEBUG(LOG_TAG, " Properties: 0x%02x", + p_data->value.dclr_value.char_prop); + LOG_DEBUG(LOG_TAG, " Characteristic UUID: %s", + format_uuid(p_data->value.dclr_value.char_uuid, str_buf, + sizeof(str_buf))); + break; + + case GATT_DISC_CHAR_DSCPT: + LOG_DEBUG(LOG_TAG, " Descriptor UUID: %s", + format_uuid(p_data->type, str_buf, sizeof(str_buf))); + break; + } + + LOG_DEBUG(LOG_TAG, + "-----------------------------------------------------------"); } static void btif_test_discovery_complete_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type, - tGATT_STATUS status) -{ - UNUSED(conn_id); - UNUSED(disc_type); - LOG_DEBUG(LOG_TAG, "%s: status=%d", __func__, status); + tGATT_STATUS status) { + UNUSED(conn_id); + UNUSED(disc_type); + LOG_DEBUG(LOG_TAG, "%s: status=%d", __func__, status); } -static tGATT_CBACK btif_test_callbacks = -{ - btif_test_connect_cback , - btif_test_command_complete_cback, - btif_test_discovery_result_cback, - btif_test_discovery_complete_cback, - NULL, - NULL, - NULL -}; +static tGATT_CBACK btif_test_callbacks = {btif_test_connect_cback, + btif_test_command_complete_cback, + btif_test_discovery_result_cback, + btif_test_discovery_complete_cback, + NULL, + NULL, + NULL}; /******************************************************************************* * Implementation *******************************************************************************/ -bt_status_t btif_gattc_test_command_impl(int command, btgatt_test_params_t* params) -{ - switch(command) { - case 0x01: /* Enable */ - { - LOG_DEBUG(LOG_TAG, "%s: ENABLE - enable=%d", __func__, params->u1); - if (params->u1) - { - tBT_UUID app_uuid = {LEN_UUID_128,{0xAE}}; - test_cb.gatt_if = GATT_Register(&app_uuid, &btif_test_callbacks); - GATT_StartIf(test_cb.gatt_if); - } else { - GATT_Deregister(test_cb.gatt_if); - test_cb.gatt_if = 0; - } - break; - } - - case 0x02: /* Connect */ - { - LOG_DEBUG(LOG_TAG, "%s: CONNECT - device=%02x:%02x:%02x:%02x:%02x:%02x (dev_type=%d, addr_type=%d)", - __func__, - params->bda1->address[0], params->bda1->address[1], +bt_status_t btif_gattc_test_command_impl(int command, + btgatt_test_params_t* params) { + switch (command) { + case 0x01: /* Enable */ + { + LOG_DEBUG(LOG_TAG, "%s: ENABLE - enable=%d", __func__, params->u1); + if (params->u1) { + tBT_UUID app_uuid = {LEN_UUID_128, {0xAE}}; + test_cb.gatt_if = GATT_Register(&app_uuid, &btif_test_callbacks); + GATT_StartIf(test_cb.gatt_if); + } else { + GATT_Deregister(test_cb.gatt_if); + test_cb.gatt_if = 0; + } + break; + } + + case 0x02: /* Connect */ + { + LOG_DEBUG(LOG_TAG, + "%s: CONNECT - device=%02x:%02x:%02x:%02x:%02x:%02x " + "(dev_type=%d, addr_type=%d)", + __func__, params->bda1->address[0], params->bda1->address[1], params->bda1->address[2], params->bda1->address[3], - params->bda1->address[4], params->bda1->address[5], - params->u1, params->u2); - - if (params->u1 == BT_DEVICE_TYPE_BLE) - BTM_SecAddBleDevice(params->bda1->address, NULL, BT_DEVICE_TYPE_BLE, params->u2); - - if ( !GATT_Connect(test_cb.gatt_if, params->bda1->address, true, BT_TRANSPORT_LE, false) ) - { - LOG_ERROR(LOG_TAG, "%s: GATT_Connect failed!", __func__); - } - break; - } - - case 0x03: /* Disconnect */ - { - LOG_DEBUG(LOG_TAG, "%s: DISCONNECT - conn_id=%d", __func__, test_cb.conn_id); - GATT_Disconnect(test_cb.conn_id); - break; - } - - case 0x04: /* Discover */ - { - char buf[50] = {0}; - tGATT_DISC_PARAM param; - memset(¶m, 0, sizeof(tGATT_DISC_PARAM)); - - if (params->u1 >= GATT_DISC_MAX) - { - LOG_ERROR(LOG_TAG, "%s: DISCOVER - Invalid type (%d)!", __func__, params->u1); - return (bt_status_t)0; - } - - param.s_handle = params->u2; - param.e_handle = params->u3; - btif_to_bta_uuid(¶m.service, params->uuid1); - - LOG_DEBUG(LOG_TAG, "%s: DISCOVER (%s), conn_id=%d, uuid=%s, handles=0x%04x-0x%04x", - __func__, disc_name[params->u1], test_cb.conn_id, - format_uuid(param.service, buf, sizeof(buf)), params->u2, params->u3); - GATTC_Discover(test_cb.conn_id, params->u1, ¶m); - break; - } - - case 0xF0: /* Pairing configuration */ - LOG_DEBUG(LOG_TAG, "%s: Setting pairing config auth=%d, iocaps=%d, keys=%d/%d/%d", - __func__, params->u1, params->u2, params->u3, params->u4, - params->u5); - - bte_appl_cfg.ble_auth_req = params->u1; - bte_appl_cfg.ble_io_cap = params->u2; - bte_appl_cfg.ble_init_key = params->u3; - bte_appl_cfg.ble_resp_key = params->u4; - bte_appl_cfg.ble_max_key_size = params->u5; - break; - - default: - LOG_ERROR(LOG_TAG, "%s: UNKNOWN TEST COMMAND 0x%02x", __func__, command); - break; + params->bda1->address[4], params->bda1->address[5], params->u1, + params->u2); + + if (params->u1 == BT_DEVICE_TYPE_BLE) + BTM_SecAddBleDevice(params->bda1->address, NULL, BT_DEVICE_TYPE_BLE, + params->u2); + + if (!GATT_Connect(test_cb.gatt_if, params->bda1->address, true, + BT_TRANSPORT_LE, false)) { + LOG_ERROR(LOG_TAG, "%s: GATT_Connect failed!", __func__); + } + break; } - return (bt_status_t)0; + + case 0x03: /* Disconnect */ + { + LOG_DEBUG(LOG_TAG, "%s: DISCONNECT - conn_id=%d", __func__, + test_cb.conn_id); + GATT_Disconnect(test_cb.conn_id); + break; + } + + case 0x04: /* Discover */ + { + char buf[50] = {0}; + tGATT_DISC_PARAM param; + memset(¶m, 0, sizeof(tGATT_DISC_PARAM)); + + if (params->u1 >= GATT_DISC_MAX) { + LOG_ERROR(LOG_TAG, "%s: DISCOVER - Invalid type (%d)!", __func__, + params->u1); + return (bt_status_t)0; + } + + param.s_handle = params->u2; + param.e_handle = params->u3; + btif_to_bta_uuid(¶m.service, params->uuid1); + + LOG_DEBUG(LOG_TAG, + "%s: DISCOVER (%s), conn_id=%d, uuid=%s, handles=0x%04x-0x%04x", + __func__, disc_name[params->u1], test_cb.conn_id, + format_uuid(param.service, buf, sizeof(buf)), params->u2, + params->u3); + GATTC_Discover(test_cb.conn_id, params->u1, ¶m); + break; + } + + case 0xF0: /* Pairing configuration */ + LOG_DEBUG(LOG_TAG, + "%s: Setting pairing config auth=%d, iocaps=%d, keys=%d/%d/%d", + __func__, params->u1, params->u2, params->u3, params->u4, + params->u5); + + bte_appl_cfg.ble_auth_req = params->u1; + bte_appl_cfg.ble_io_cap = params->u2; + bte_appl_cfg.ble_init_key = params->u3; + bte_appl_cfg.ble_resp_key = params->u4; + bte_appl_cfg.ble_max_key_size = params->u5; + break; + + default: + LOG_ERROR(LOG_TAG, "%s: UNKNOWN TEST COMMAND 0x%02x", __func__, command); + break; + } + return (bt_status_t)0; } #endif diff --git a/btif/src/btif_gatt_util.cc b/btif/src/btif_gatt_util.cc index 1c74e3d1b..5c23d419c 100644 --- a/btif/src/btif_gatt_util.cc +++ b/btif/src/btif_gatt_util.cc @@ -29,6 +29,7 @@ #include #include "bdaddr.h" +#include "bt_common.h" #include "bta_api.h" #include "bta_gatt_api.h" #include "bta_jv_api.h" @@ -38,198 +39,173 @@ #include "btif_gatt.h" #include "btif_storage.h" #include "btif_util.h" -#include "bt_common.h" #if (BTA_GATT_INCLUDED == TRUE) -#define GATTC_READ_VALUE_TYPE_VALUE 0x0000 /* Attribute value itself */ -#define GATTC_READ_VALUE_TYPE_AGG_FORMAT 0x2905 /* Characteristic Aggregate Format*/ - -static unsigned char BASE_UUID[16] = { - 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, - 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 -}; - -int uuidType(const unsigned char* p_uuid) -{ - int i = 0; - int match = 0; - int all_zero = 1; - - for(i = 0; i != 16; ++i) - { - if (i == 12 || i == 13) - continue; - - if (p_uuid[i] == BASE_UUID[i]) - ++match; - - if (p_uuid[i] != 0) - all_zero = 0; - } - if (all_zero) - return 0; - if (match == 12) - return LEN_UUID_32; - if (match == 14) - return LEN_UUID_16; - return LEN_UUID_128; +#define GATTC_READ_VALUE_TYPE_VALUE 0x0000 /* Attribute value itself */ +#define GATTC_READ_VALUE_TYPE_AGG_FORMAT \ + 0x2905 /* Characteristic Aggregate Format*/ + +static unsigned char BASE_UUID[16] = {0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, + 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00}; + +int uuidType(const unsigned char* p_uuid) { + int i = 0; + int match = 0; + int all_zero = 1; + + for (i = 0; i != 16; ++i) { + if (i == 12 || i == 13) continue; + + if (p_uuid[i] == BASE_UUID[i]) ++match; + + if (p_uuid[i] != 0) all_zero = 0; + } + if (all_zero) return 0; + if (match == 12) return LEN_UUID_32; + if (match == 14) return LEN_UUID_16; + return LEN_UUID_128; } /******************************************************************************* * BTIF -> BTA conversion functions *******************************************************************************/ -void btif_to_bta_uuid(tBT_UUID *p_dest, const bt_uuid_t *p_src) -{ - char *p_byte = (char*)p_src; - int i = 0; - - p_dest->len = uuidType(p_src->uu); - - switch (p_dest->len) - { - case LEN_UUID_16: - p_dest->uu.uuid16 = (p_src->uu[13] << 8) + p_src->uu[12]; - break; - - case LEN_UUID_32: - p_dest->uu.uuid32 = (p_src->uu[13] << 8) + p_src->uu[12]; - p_dest->uu.uuid32 += (p_src->uu[15] << 24) + (p_src->uu[14] << 16); - break; - - case LEN_UUID_128: - for(i = 0; i != 16; ++i) - p_dest->uu.uuid128[i] = p_byte[i]; - break; - - default: - LOG_ERROR(LOG_TAG, "%s: Unknown UUID length %d!", __func__, p_dest->len); - break; - } +void btif_to_bta_uuid(tBT_UUID* p_dest, const bt_uuid_t* p_src) { + char* p_byte = (char*)p_src; + int i = 0; + + p_dest->len = uuidType(p_src->uu); + + switch (p_dest->len) { + case LEN_UUID_16: + p_dest->uu.uuid16 = (p_src->uu[13] << 8) + p_src->uu[12]; + break; + + case LEN_UUID_32: + p_dest->uu.uuid32 = (p_src->uu[13] << 8) + p_src->uu[12]; + p_dest->uu.uuid32 += (p_src->uu[15] << 24) + (p_src->uu[14] << 16); + break; + + case LEN_UUID_128: + for (i = 0; i != 16; ++i) p_dest->uu.uuid128[i] = p_byte[i]; + break; + + default: + LOG_ERROR(LOG_TAG, "%s: Unknown UUID length %d!", __func__, p_dest->len); + break; + } } -void btif_to_bta_response(tBTA_GATTS_RSP *p_dest, btgatt_response_t* p_src) -{ - p_dest->attr_value.auth_req = p_src->attr_value.auth_req; - p_dest->attr_value.handle = p_src->attr_value.handle; - p_dest->attr_value.len = p_src->attr_value.len; - p_dest->attr_value.offset = p_src->attr_value.offset; - memcpy(p_dest->attr_value.value, p_src->attr_value.value, GATT_MAX_ATTR_LEN); +void btif_to_bta_response(tBTA_GATTS_RSP* p_dest, btgatt_response_t* p_src) { + p_dest->attr_value.auth_req = p_src->attr_value.auth_req; + p_dest->attr_value.handle = p_src->attr_value.handle; + p_dest->attr_value.len = p_src->attr_value.len; + p_dest->attr_value.offset = p_src->attr_value.offset; + memcpy(p_dest->attr_value.value, p_src->attr_value.value, GATT_MAX_ATTR_LEN); } -void btif_to_bta_uuid_mask(tBTA_DM_BLE_PF_COND_MASK *p_mask, const bt_uuid_t *uuid_mask, const bt_uuid_t *svc_uuid) -{ - char *p_byte = (char*)uuid_mask; - int uuid_len = uuidType(svc_uuid->uu); - int i = 0; - - switch (uuid_len) - { - case LEN_UUID_16: - p_mask->uuid16_mask = (uuid_mask->uu[13] << 8) + uuid_mask->uu[12]; - break; - - case LEN_UUID_32: - p_mask->uuid32_mask = (uuid_mask->uu[13] << 8) + uuid_mask->uu[12]; - p_mask->uuid32_mask += (uuid_mask->uu[15] << 24) + (uuid_mask->uu[14] << 16); - break; - - case LEN_UUID_128: - for(i = 0; i != 16; ++i) - p_mask->uuid128_mask[i] = p_byte[i]; - break; - - default: - break; - } +void btif_to_bta_uuid_mask(tBTA_DM_BLE_PF_COND_MASK* p_mask, + const bt_uuid_t* uuid_mask, + const bt_uuid_t* svc_uuid) { + char* p_byte = (char*)uuid_mask; + int uuid_len = uuidType(svc_uuid->uu); + int i = 0; + + switch (uuid_len) { + case LEN_UUID_16: + p_mask->uuid16_mask = (uuid_mask->uu[13] << 8) + uuid_mask->uu[12]; + break; + + case LEN_UUID_32: + p_mask->uuid32_mask = (uuid_mask->uu[13] << 8) + uuid_mask->uu[12]; + p_mask->uuid32_mask += + (uuid_mask->uu[15] << 24) + (uuid_mask->uu[14] << 16); + break; + + case LEN_UUID_128: + for (i = 0; i != 16; ++i) p_mask->uuid128_mask[i] = p_byte[i]; + break; + + default: + break; + } } /******************************************************************************* * BTA -> BTIF conversion functions *******************************************************************************/ -void bta_to_btif_uuid(bt_uuid_t *p_dest, tBT_UUID *p_src) -{ - int i = 0; - - if (p_src->len == LEN_UUID_16 || p_src->len == LEN_UUID_32) - { - for(i=0; i != 16; ++i) - p_dest->uu[i] = BASE_UUID[i]; - } - - switch (p_src->len) - { - case 0: - break; - - case LEN_UUID_16: - p_dest->uu[12] = p_src->uu.uuid16 & 0xff; - p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff; - break; - - case LEN_UUID_32: - p_dest->uu[12] = p_src->uu.uuid16 & 0xff; - p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff; - p_dest->uu[14] = (p_src->uu.uuid32 >> 16) & 0xff; - p_dest->uu[15] = (p_src->uu.uuid32 >> 24) & 0xff; - break; - - case LEN_UUID_128: - for(i=0; i != 16; ++i) - p_dest->uu[i] = p_src->uu.uuid128[i]; - break; - - default: - LOG_ERROR(LOG_TAG, "%s: Unknown UUID length %d!", __func__, p_src->len); - break; - } +void bta_to_btif_uuid(bt_uuid_t* p_dest, tBT_UUID* p_src) { + int i = 0; + + if (p_src->len == LEN_UUID_16 || p_src->len == LEN_UUID_32) { + for (i = 0; i != 16; ++i) p_dest->uu[i] = BASE_UUID[i]; + } + + switch (p_src->len) { + case 0: + break; + + case LEN_UUID_16: + p_dest->uu[12] = p_src->uu.uuid16 & 0xff; + p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff; + break; + + case LEN_UUID_32: + p_dest->uu[12] = p_src->uu.uuid16 & 0xff; + p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff; + p_dest->uu[14] = (p_src->uu.uuid32 >> 16) & 0xff; + p_dest->uu[15] = (p_src->uu.uuid32 >> 24) & 0xff; + break; + + case LEN_UUID_128: + for (i = 0; i != 16; ++i) p_dest->uu[i] = p_src->uu.uuid128[i]; + break; + + default: + LOG_ERROR(LOG_TAG, "%s: Unknown UUID length %d!", __func__, p_src->len); + break; + } } /******************************************************************************* * Utility functions *******************************************************************************/ -uint16_t get_uuid16(tBT_UUID *p_uuid) -{ - if (p_uuid->len == LEN_UUID_16) - { - return p_uuid->uu.uuid16; - } - else if (p_uuid->len == LEN_UUID_128) - { - uint16_t u16; - uint8_t *p = &p_uuid->uu.uuid128[LEN_UUID_128 - 4]; - STREAM_TO_UINT16(u16, p); - return u16; - } - else /* p_uuid->len == LEN_UUID_32 */ - { - return(uint16_t) p_uuid->uu.uuid32; - } +uint16_t get_uuid16(tBT_UUID* p_uuid) { + if (p_uuid->len == LEN_UUID_16) { + return p_uuid->uu.uuid16; + } else if (p_uuid->len == LEN_UUID_128) { + uint16_t u16; + uint8_t* p = &p_uuid->uu.uuid128[LEN_UUID_128 - 4]; + STREAM_TO_UINT16(u16, p); + return u16; + } else /* p_uuid->len == LEN_UUID_32 */ + { + return (uint16_t)p_uuid->uu.uuid32; + } } -uint16_t set_read_value(btgatt_read_params_t *p_dest, tBTA_GATTC_READ *p_src) -{ - uint16_t len = 0; +uint16_t set_read_value(btgatt_read_params_t* p_dest, tBTA_GATTC_READ* p_src) { + uint16_t len = 0; - p_dest->status = p_src->status; - p_dest->handle = p_src->handle; + p_dest->status = p_src->status; + p_dest->handle = p_src->handle; - if (( p_src->status == BTA_GATT_OK ) &&(p_src->len != 0)) - { - LOG_INFO(LOG_TAG, "%s len = %d ", __func__, p_src->len); - p_dest->value.len = p_src->len; - memcpy(p_dest->value.value, p_src->value, p_src->len); + if ((p_src->status == BTA_GATT_OK) && (p_src->len != 0)) { + LOG_INFO(LOG_TAG, "%s len = %d ", __func__, p_src->len); + p_dest->value.len = p_src->len; + memcpy(p_dest->value.value, p_src->value, p_src->len); - len += p_src->len; - } else { - p_dest->value.len = 0; - } + len += p_src->len; + } else { + p_dest->value.len = 0; + } - p_dest->value_type = GATTC_READ_VALUE_TYPE_VALUE; - return len; + p_dest->value_type = GATTC_READ_VALUE_TYPE_VALUE; + return len; } /******************************************************************************* @@ -237,71 +213,64 @@ uint16_t set_read_value(btgatt_read_params_t *p_dest, tBTA_GATTC_READ *p_src) *******************************************************************************/ #if (BLE_DELAY_REQUEST_ENC == FALSE) -static bool btif_gatt_is_link_encrypted (BD_ADDR bd_addr) -{ - if (bd_addr == NULL) - return false; +static bool btif_gatt_is_link_encrypted(BD_ADDR bd_addr) { + if (bd_addr == NULL) return false; - return BTA_JvIsEncrypted(bd_addr); + return BTA_JvIsEncrypted(bd_addr); } -static void btif_gatt_set_encryption_cb (BD_ADDR bd_addr, tBTA_TRANSPORT transport, tBTA_STATUS result) -{ - UNUSED(bd_addr); - UNUSED(transport); +static void btif_gatt_set_encryption_cb(BD_ADDR bd_addr, + tBTA_TRANSPORT transport, + tBTA_STATUS result) { + UNUSED(bd_addr); + UNUSED(transport); - if (result != BTA_SUCCESS && result != BTA_BUSY) - { - BTIF_TRACE_WARNING("%s() - Encryption failed (%d)", __func__, result); - } + if (result != BTA_SUCCESS && result != BTA_BUSY) { + BTIF_TRACE_WARNING("%s() - Encryption failed (%d)", __func__, result); + } } #endif -void btif_gatt_check_encrypted_link (BD_ADDR bd_addr, tBTA_GATT_TRANSPORT transport_link) -{ +void btif_gatt_check_encrypted_link(BD_ADDR bd_addr, + tBTA_GATT_TRANSPORT transport_link) { #if (BLE_DELAY_REQUEST_ENC == FALSE) - char buf[100]; - - bt_bdaddr_t bda; - bdcpy(bda.address, bd_addr); - - if ((btif_storage_get_ble_bonding_key(&bda, BTIF_DM_LE_KEY_PENC, - buf, sizeof(tBTM_LE_PENC_KEYS)) == BT_STATUS_SUCCESS) - && !btif_gatt_is_link_encrypted(bd_addr)) - { - BTIF_TRACE_DEBUG ("%s: transport = %d", __func__, transport_link); - BTA_DmSetEncryption(bd_addr,transport_link, - &btif_gatt_set_encryption_cb, BTM_BLE_SEC_ENCRYPT); - } + char buf[100]; + + bt_bdaddr_t bda; + bdcpy(bda.address, bd_addr); + + if ((btif_storage_get_ble_bonding_key(&bda, BTIF_DM_LE_KEY_PENC, buf, + sizeof(tBTM_LE_PENC_KEYS)) == + BT_STATUS_SUCCESS) && + !btif_gatt_is_link_encrypted(bd_addr)) { + BTIF_TRACE_DEBUG("%s: transport = %d", __func__, transport_link); + BTA_DmSetEncryption(bd_addr, transport_link, &btif_gatt_set_encryption_cb, + BTM_BLE_SEC_ENCRYPT); + } #else - UNUSED(bd_addr); - UNUSED(transport_link); + UNUSED(bd_addr); + UNUSED(transport_link); #endif } -#endif // BTA_GATT_INCLUDED - -void btif_gatt_move_track_adv_data(btgatt_track_adv_info_t *p_dest, - btgatt_track_adv_info_t *p_src) -{ - memset(p_dest, 0, sizeof(btgatt_track_adv_info_t)); - - memcpy(p_dest, p_src, sizeof(btgatt_track_adv_info_t)); - - if (p_src->adv_pkt_len > 0) - { - p_dest->p_adv_pkt_data = (uint8_t*)osi_malloc(p_src->adv_pkt_len); - memcpy(p_dest->p_adv_pkt_data, p_src->p_adv_pkt_data, - p_src->adv_pkt_len); - osi_free_and_reset((void **)&p_src->p_adv_pkt_data); - } - - if (p_src->scan_rsp_len > 0) - { - p_dest->p_scan_rsp_data = (uint8_t*)osi_malloc(p_src->scan_rsp_len); - memcpy(p_dest->p_scan_rsp_data, p_src->p_scan_rsp_data, - p_src->scan_rsp_len); - osi_free_and_reset((void **)&p_src->p_scan_rsp_data); - } -} +#endif // BTA_GATT_INCLUDED +void btif_gatt_move_track_adv_data(btgatt_track_adv_info_t* p_dest, + btgatt_track_adv_info_t* p_src) { + memset(p_dest, 0, sizeof(btgatt_track_adv_info_t)); + + memcpy(p_dest, p_src, sizeof(btgatt_track_adv_info_t)); + + if (p_src->adv_pkt_len > 0) { + p_dest->p_adv_pkt_data = (uint8_t*)osi_malloc(p_src->adv_pkt_len); + memcpy(p_dest->p_adv_pkt_data, p_src->p_adv_pkt_data, p_src->adv_pkt_len); + osi_free_and_reset((void**)&p_src->p_adv_pkt_data); + } + + if (p_src->scan_rsp_len > 0) { + p_dest->p_scan_rsp_data = (uint8_t*)osi_malloc(p_src->scan_rsp_len); + memcpy(p_dest->p_scan_rsp_data, p_src->p_scan_rsp_data, + p_src->scan_rsp_len); + osi_free_and_reset((void**)&p_src->p_scan_rsp_data); + } +} diff --git a/btif/src/btif_hf.cc b/btif/src/btif_hf.cc index cc0deb8bb..5c91ec64d 100644 --- a/btif/src/btif_hf.cc +++ b/btif/src/btif_hf.cc @@ -42,8 +42,8 @@ #include "btif_util.h" /************************************************************************************ -** Constants & Macros -************************************************************************************/ + * Constants & Macros + ***********************************************************************************/ #ifndef BTIF_HSAG_SERVICE_NAME #define BTIF_HSAG_SERVICE_NAME ("Headset Gateway") #endif @@ -53,1563 +53,1469 @@ #endif #ifndef BTIF_HF_SERVICES -#define BTIF_HF_SERVICES (BTA_HSP_SERVICE_MASK | BTA_HFP_SERVICE_MASK ) +#define BTIF_HF_SERVICES (BTA_HSP_SERVICE_MASK | BTA_HFP_SERVICE_MASK) #endif #ifndef BTIF_HF_SERVICE_NAMES -#define BTIF_HF_SERVICE_NAMES {BTIF_HSAG_SERVICE_NAME , BTIF_HFAG_SERVICE_NAME} +#define BTIF_HF_SERVICE_NAMES \ + { BTIF_HSAG_SERVICE_NAME, BTIF_HFAG_SERVICE_NAME } #endif #ifndef BTIF_HF_SECURITY -#define BTIF_HF_SECURITY (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT) +#define BTIF_HF_SECURITY (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT) #endif #if (BTM_WBS_INCLUDED == TRUE) #ifndef BTIF_HF_FEATURES -#define BTIF_HF_FEATURES ( BTA_AG_FEAT_3WAY | \ - BTA_AG_FEAT_ECNR | \ - BTA_AG_FEAT_REJECT | \ - BTA_AG_FEAT_ECS | \ - BTA_AG_FEAT_EXTERR | \ - BTA_AG_FEAT_BTRH | \ - BTA_AG_FEAT_VREC | \ - BTA_AG_FEAT_CODEC |\ - BTA_AG_FEAT_HF_IND | \ - BTA_AG_FEAT_ESCO | \ - BTA_AG_FEAT_UNAT) +#define BTIF_HF_FEATURES \ + (BTA_AG_FEAT_3WAY | BTA_AG_FEAT_ECNR | BTA_AG_FEAT_REJECT | \ + BTA_AG_FEAT_ECS | BTA_AG_FEAT_EXTERR | BTA_AG_FEAT_BTRH | \ + BTA_AG_FEAT_VREC | BTA_AG_FEAT_CODEC | BTA_AG_FEAT_HF_IND | \ + BTA_AG_FEAT_ESCO | BTA_AG_FEAT_UNAT) #endif #else #ifndef BTIF_HF_FEATURES -#define BTIF_HF_FEATURES ( BTA_AG_FEAT_3WAY | \ - BTA_AG_FEAT_ECNR | \ - BTA_AG_FEAT_REJECT | \ - BTA_AG_FEAT_ECS | \ - BTA_AG_FEAT_EXTERR | \ - BTA_AG_FEAT_BTRH | \ - BTA_AG_FEAT_VREC | \ - BTA_AG_FEAT_HF_IND | \ - BTA_AG_FEAT_ESCO | \ - BTA_AG_FEAT_UNAT) +#define BTIF_HF_FEATURES \ + (BTA_AG_FEAT_3WAY | BTA_AG_FEAT_ECNR | BTA_AG_FEAT_REJECT | \ + BTA_AG_FEAT_ECS | BTA_AG_FEAT_EXTERR | BTA_AG_FEAT_BTRH | \ + BTA_AG_FEAT_VREC | BTA_AG_FEAT_HF_IND | BTA_AG_FEAT_ESCO | \ + BTA_AG_FEAT_UNAT) #endif #endif -#define BTIF_HF_CALL_END_TIMEOUT 6 +#define BTIF_HF_CALL_END_TIMEOUT 6 -#define BTIF_HF_INVALID_IDX (-1) +#define BTIF_HF_INVALID_IDX (-1) /* Number of BTIF-HF control blocks */ -#define BTIF_HF_NUM_CB 2 +#define BTIF_HF_NUM_CB 2 /* Max HF clients supported from App */ uint16_t btif_max_hf_clients = 1; /* HF app ids for service registration */ typedef enum { - BTIF_HF_ID_1 = 0, - BTIF_HF_ID_2, + BTIF_HF_ID_1 = 0, + BTIF_HF_ID_2, #if (BTIF_HF_NUM_CB == 3) - BTIF_HF_ID_3 + BTIF_HF_ID_3 #endif } bthf_hf_id_t; uint16_t bthf_hf_id[BTIF_HF_NUM_CB] = {BTIF_HF_ID_1, BTIF_HF_ID_2, - #if (BTIF_HF_NUM_CB == 3) - BTIF_HF_ID_3 - #endif - }; +#if (BTIF_HF_NUM_CB == 3) + BTIF_HF_ID_3 +#endif +}; /************************************************************************************ -** Local type definitions -************************************************************************************/ + * Local type definitions + ***********************************************************************************/ /************************************************************************************ -** Static variables -************************************************************************************/ -static bthf_callbacks_t *bt_hf_callbacks = NULL; + * Static variables + ***********************************************************************************/ +static bthf_callbacks_t* bt_hf_callbacks = NULL; static int hf_idx = BTIF_HF_INVALID_IDX; -#define CHECK_BTHF_INIT() if (bt_hf_callbacks == NULL)\ - {\ - BTIF_TRACE_WARNING("BTHF: %s: BTHF not initialized", __func__);\ - return BT_STATUS_NOT_READY;\ - }\ - else\ - {\ - BTIF_TRACE_EVENT("BTHF: %s", __func__);\ - } - -#define CHECK_BTHF_SLC_CONNECTED() if (bt_hf_callbacks == NULL)\ - {\ - BTIF_TRACE_WARNING("BTHF: %s: BTHF not initialized", __func__);\ - return BT_STATUS_NOT_READY;\ - }\ - else if (btif_hf_cb.state != BTHF_CONNECTION_STATE_SLC_CONNECTED)\ - {\ - BTIF_TRACE_WARNING("BTHF: %s: SLC connection not up. state=%s", __func__, dump_hf_conn_state(btif_hf_cb.state));\ - return BT_STATUS_NOT_READY;\ - }\ - else\ - {\ - BTIF_TRACE_EVENT("BTHF: %s", __func__);\ - } +#define CHECK_BTHF_INIT() \ + if (bt_hf_callbacks == NULL) { \ + BTIF_TRACE_WARNING("BTHF: %s: BTHF not initialized", __func__); \ + return BT_STATUS_NOT_READY; \ + } else { \ + BTIF_TRACE_EVENT("BTHF: %s", __func__); \ + } + +#define CHECK_BTHF_SLC_CONNECTED() \ + if (bt_hf_callbacks == NULL) { \ + BTIF_TRACE_WARNING("BTHF: %s: BTHF not initialized", __func__); \ + return BT_STATUS_NOT_READY; \ + } else if (btif_hf_cb.state != BTHF_CONNECTION_STATE_SLC_CONNECTED) { \ + BTIF_TRACE_WARNING("BTHF: %s: SLC connection not up. state=%s", __func__, \ + dump_hf_conn_state(btif_hf_cb.state)); \ + return BT_STATUS_NOT_READY; \ + } else { \ + BTIF_TRACE_EVENT("BTHF: %s", __func__); \ + } /* BTIF-HF control block to map bdaddr to BTA handle */ -typedef struct _btif_hf_cb -{ - uint16_t handle; - bt_bdaddr_t connected_bda; - bthf_connection_state_t state; - bthf_vr_state_t vr_state; - tBTA_AG_PEER_FEAT peer_feat; - int num_active; - int num_held; - struct timespec call_end_timestamp; - struct timespec connected_timestamp; - bthf_call_state_t call_setup_state; +typedef struct _btif_hf_cb { + uint16_t handle; + bt_bdaddr_t connected_bda; + bthf_connection_state_t state; + bthf_vr_state_t vr_state; + tBTA_AG_PEER_FEAT peer_feat; + int num_active; + int num_held; + struct timespec call_end_timestamp; + struct timespec connected_timestamp; + bthf_call_state_t call_setup_state; } btif_hf_cb_t; static btif_hf_cb_t btif_hf_cb[BTIF_HF_NUM_CB]; /************************************************************************************ -** Static functions -************************************************************************************/ + * Static functions + ***********************************************************************************/ /************************************************************************************ -** Externs -************************************************************************************/ -/* By default, even though codec negotiation is enabled, we will not use WBS as the default + * Externs + ***********************************************************************************/ +/* By default, even though codec negotiation is enabled, we will not use WBS as +* the default * codec unless this variable is set to true. */ #ifndef BTIF_HF_WBS_PREFERRED -#define BTIF_HF_WBS_PREFERRED false +#define BTIF_HF_WBS_PREFERRED false #endif bool btif_conf_hf_force_wbs = BTIF_HF_WBS_PREFERRED; /************************************************************************************ -** Functions -************************************************************************************/ + * Functions + ***********************************************************************************/ /******************************************************************************* -** -** Function is_connected -** -** Description Internal function to check if HF is connected -** -** Returns true if connected -** -*******************************************************************************/ -static bool is_connected(bt_bdaddr_t *bd_addr) -{ - int i; - for (i = 0; i < btif_max_hf_clients; ++i) - { - if (((btif_hf_cb[i].state == BTHF_CONNECTION_STATE_CONNECTED) || - (btif_hf_cb[i].state == BTHF_CONNECTION_STATE_SLC_CONNECTED)) && - ((bd_addr == NULL) || (bdcmp(bd_addr->address, - btif_hf_cb[i].connected_bda.address) == 0))) - return true; - } - return false; + * + * Function is_connected + * + * Description Internal function to check if HF is connected + * + * Returns true if connected + * + ******************************************************************************/ +static bool is_connected(bt_bdaddr_t* bd_addr) { + int i; + for (i = 0; i < btif_max_hf_clients; ++i) { + if (((btif_hf_cb[i].state == BTHF_CONNECTION_STATE_CONNECTED) || + (btif_hf_cb[i].state == BTHF_CONNECTION_STATE_SLC_CONNECTED)) && + ((bd_addr == NULL) || + (bdcmp(bd_addr->address, btif_hf_cb[i].connected_bda.address) == 0))) + return true; + } + return false; } /******************************************************************************* -** -** Function btif_hf_idx_by_bdaddr -** -** Description Internal function to get idx by bdaddr -** -** Returns idx -** -*******************************************************************************/ -static int btif_hf_idx_by_bdaddr(bt_bdaddr_t *bd_addr) -{ - int i; - for (i = 0; i < btif_max_hf_clients; ++i) - { - if ((bdcmp(bd_addr->address, - btif_hf_cb[i].connected_bda.address) == 0)) - return i; - } - return BTIF_HF_INVALID_IDX; + * + * Function btif_hf_idx_by_bdaddr + * + * Description Internal function to get idx by bdaddr + * + * Returns idx + * + ******************************************************************************/ +static int btif_hf_idx_by_bdaddr(bt_bdaddr_t* bd_addr) { + int i; + for (i = 0; i < btif_max_hf_clients; ++i) { + if ((bdcmp(bd_addr->address, btif_hf_cb[i].connected_bda.address) == 0)) + return i; + } + return BTIF_HF_INVALID_IDX; } /******************************************************************************* -** -** Function callstate_to_callsetup -** -** Description Converts HAL call state to BTA call setup indicator value -** -** Returns BTA call indicator value -** -*******************************************************************************/ -static uint8_t callstate_to_callsetup(bthf_call_state_t call_state) -{ - uint8_t call_setup = 0; - if (call_state == BTHF_CALL_STATE_INCOMING) - call_setup = 1; - if (call_state == BTHF_CALL_STATE_DIALING) - call_setup = 2; - if (call_state == BTHF_CALL_STATE_ALERTING) - call_setup = 3; - - return call_setup; + * + * Function callstate_to_callsetup + * + * Description Converts HAL call state to BTA call setup indicator value + * + * Returns BTA call indicator value + * + ******************************************************************************/ +static uint8_t callstate_to_callsetup(bthf_call_state_t call_state) { + uint8_t call_setup = 0; + if (call_state == BTHF_CALL_STATE_INCOMING) call_setup = 1; + if (call_state == BTHF_CALL_STATE_DIALING) call_setup = 2; + if (call_state == BTHF_CALL_STATE_ALERTING) call_setup = 3; + + return call_setup; } /******************************************************************************* -** -** Function send_at_result -** -** Description Send AT result code (OK/ERROR) -** -** Returns void -** -*******************************************************************************/ -static void send_at_result(uint8_t ok_flag, uint16_t errcode, int idx) -{ - tBTA_AG_RES_DATA ag_res; - memset (&ag_res, 0, sizeof (ag_res)); - - ag_res.ok_flag = ok_flag; - if (ok_flag == BTA_AG_OK_ERROR) - { - ag_res.errcode = errcode; - } + * + * Function send_at_result + * + * Description Send AT result code (OK/ERROR) + * + * Returns void + * + ******************************************************************************/ +static void send_at_result(uint8_t ok_flag, uint16_t errcode, int idx) { + tBTA_AG_RES_DATA ag_res; + memset(&ag_res, 0, sizeof(ag_res)); - BTA_AgResult (btif_hf_cb[idx].handle, BTA_AG_UNAT_RES, &ag_res); + ag_res.ok_flag = ok_flag; + if (ok_flag == BTA_AG_OK_ERROR) { + ag_res.errcode = errcode; + } + + BTA_AgResult(btif_hf_cb[idx].handle, BTA_AG_UNAT_RES, &ag_res); } /******************************************************************************* -** -** Function send_indicator_update -** -** Description Send indicator update (CIEV) -** -** Returns void -** -*******************************************************************************/ -static void send_indicator_update (uint16_t indicator, uint16_t value) -{ - tBTA_AG_RES_DATA ag_res; + * + * Function send_indicator_update + * + * Description Send indicator update (CIEV) + * + * Returns void + * + ******************************************************************************/ +static void send_indicator_update(uint16_t indicator, uint16_t value) { + tBTA_AG_RES_DATA ag_res; - memset(&ag_res, 0, sizeof(tBTA_AG_RES_DATA)); - ag_res.ind.id = indicator; - ag_res.ind.value = value; + memset(&ag_res, 0, sizeof(tBTA_AG_RES_DATA)); + ag_res.ind.id = indicator; + ag_res.ind.value = value; - BTA_AgResult(BTA_AG_HANDLE_ALL, BTA_AG_IND_RES, &ag_res); + BTA_AgResult(BTA_AG_HANDLE_ALL, BTA_AG_IND_RES, &ag_res); } -void clear_phone_state_multihf(int idx) -{ - btif_hf_cb[idx].call_setup_state = BTHF_CALL_STATE_IDLE; - btif_hf_cb[idx].num_active = btif_hf_cb[idx].num_held = 0; +void clear_phone_state_multihf(int idx) { + btif_hf_cb[idx].call_setup_state = BTHF_CALL_STATE_IDLE; + btif_hf_cb[idx].num_active = btif_hf_cb[idx].num_held = 0; } /******************************************************************************* -** -** Function btif_hf_latest_connected_idx -** -** Description Returns idx for latest connected HF -** -** Returns int -** -*******************************************************************************/ -static int btif_hf_latest_connected_idx() -{ - struct timespec now, conn_time_delta; - int latest_conn_idx = BTIF_HF_INVALID_IDX, i; - - clock_gettime(CLOCK_MONOTONIC, &now); - conn_time_delta.tv_sec = now.tv_sec; - - for (i = 0; i < btif_max_hf_clients; i++) - { - if (btif_hf_cb[i].state == BTHF_CONNECTION_STATE_SLC_CONNECTED) - { - if ((now.tv_sec - btif_hf_cb[i].connected_timestamp.tv_sec) - < conn_time_delta.tv_sec) - { - conn_time_delta.tv_sec = - now.tv_sec - btif_hf_cb[i].connected_timestamp.tv_sec; - latest_conn_idx = i; - } - } + * + * Function btif_hf_latest_connected_idx + * + * Description Returns idx for latest connected HF + * + * Returns int + * + ******************************************************************************/ +static int btif_hf_latest_connected_idx() { + struct timespec now, conn_time_delta; + int latest_conn_idx = BTIF_HF_INVALID_IDX, i; + + clock_gettime(CLOCK_MONOTONIC, &now); + conn_time_delta.tv_sec = now.tv_sec; + + for (i = 0; i < btif_max_hf_clients; i++) { + if (btif_hf_cb[i].state == BTHF_CONNECTION_STATE_SLC_CONNECTED) { + if ((now.tv_sec - btif_hf_cb[i].connected_timestamp.tv_sec) < + conn_time_delta.tv_sec) { + conn_time_delta.tv_sec = + now.tv_sec - btif_hf_cb[i].connected_timestamp.tv_sec; + latest_conn_idx = i; } - return latest_conn_idx; + } + } + return latest_conn_idx; } /******************************************************************************* -** -** Function btif_hf_check_if_slc_connected -** -** Description Returns BT_STATUS_SUCCESS if SLC is up for any HF -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t btif_hf_check_if_slc_connected() -{ - if (bt_hf_callbacks == NULL) - { - BTIF_TRACE_WARNING("BTHF: %s: BTHF not initialized", __func__); - return BT_STATUS_NOT_READY; - } - else - { - int i; - for (i = 0; i < btif_max_hf_clients; i++) - { - if (btif_hf_cb[i].state == BTHF_CONNECTION_STATE_SLC_CONNECTED) - { - BTIF_TRACE_EVENT("BTHF: %s: slc connected for idx = %d", - __func__, i); - return BT_STATUS_SUCCESS; - } - } - BTIF_TRACE_WARNING("BTHF: %s: No SLC connection up", __func__); - return BT_STATUS_NOT_READY; + * + * Function btif_hf_check_if_slc_connected + * + * Description Returns BT_STATUS_SUCCESS if SLC is up for any HF + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t btif_hf_check_if_slc_connected() { + if (bt_hf_callbacks == NULL) { + BTIF_TRACE_WARNING("BTHF: %s: BTHF not initialized", __func__); + return BT_STATUS_NOT_READY; + } else { + int i; + for (i = 0; i < btif_max_hf_clients; i++) { + if (btif_hf_cb[i].state == BTHF_CONNECTION_STATE_SLC_CONNECTED) { + BTIF_TRACE_EVENT("BTHF: %s: slc connected for idx = %d", __func__, i); + return BT_STATUS_SUCCESS; + } } + BTIF_TRACE_WARNING("BTHF: %s: No SLC connection up", __func__); + return BT_STATUS_NOT_READY; + } } /***************************************************************************** -** Section name (Group of functions) -*****************************************************************************/ + * Section name (Group of functions) + ****************************************************************************/ /***************************************************************************** -** -** btif hf api functions (no context switch) -** -*****************************************************************************/ + * + * btif hf api functions (no context switch) + * + ****************************************************************************/ /******************************************************************************* -** -** Function btif_hf_upstreams_evt -** -** Description Executes HF UPSTREAMS events in btif context -** -** Returns void -** -*******************************************************************************/ -static void btif_hf_upstreams_evt(uint16_t event, char* p_param) -{ - tBTA_AG *p_data = (tBTA_AG *)p_param; - bdstr_t bdstr; - int idx = p_data->hdr.handle - 1; - - BTIF_TRACE_DEBUG("%s: event=%s", __func__, dump_hf_event(event)); - - if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) - { - BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); - return; - } - - switch (event) - { - case BTA_AG_ENABLE_EVT: - case BTA_AG_DISABLE_EVT: - break; - - case BTA_AG_REGISTER_EVT: - btif_hf_cb[idx].handle = p_data->reg.hdr.handle; - BTIF_TRACE_DEBUG("%s: BTA_AG_REGISTER_EVT," - "btif_hf_cb.handle = %d", __func__, btif_hf_cb[idx].handle); - break; - - case BTA_AG_OPEN_EVT: - if (p_data->open.status == BTA_AG_SUCCESS) - { - bdcpy(btif_hf_cb[idx].connected_bda.address, - p_data->open.bd_addr); - btif_hf_cb[idx].state = BTHF_CONNECTION_STATE_CONNECTED; - btif_hf_cb[idx].peer_feat = 0; - clear_phone_state_multihf(idx); - } - else if (btif_hf_cb[idx].state == BTHF_CONNECTION_STATE_CONNECTING) - { - btif_hf_cb[idx].state = BTHF_CONNECTION_STATE_DISCONNECTED; - } - else - { - BTIF_TRACE_WARNING("%s: AG open failed, but another device connected. status=%d state=%d connected device=%s", - __func__, p_data->open.status, btif_hf_cb[idx].state, - bdaddr_to_string(&btif_hf_cb[idx].connected_bda, bdstr, sizeof(bdstr))); - break; - } - - HAL_CBACK(bt_hf_callbacks, connection_state_cb, btif_hf_cb[idx].state, - &btif_hf_cb[idx].connected_bda); - - if (btif_hf_cb[idx].state == BTHF_CONNECTION_STATE_DISCONNECTED) - bdsetany(btif_hf_cb[idx].connected_bda.address); - - if (p_data->open.status != BTA_AG_SUCCESS) - btif_queue_advance(); - break; - - case BTA_AG_CLOSE_EVT: - btif_hf_cb[idx].connected_timestamp.tv_sec = 0; - btif_hf_cb[idx].state = BTHF_CONNECTION_STATE_DISCONNECTED; - BTIF_TRACE_DEBUG("%s: BTA_AG_CLOSE_EVT," - "idx = %d, btif_hf_cb.handle = %d", __func__, idx, - btif_hf_cb[idx].handle); - HAL_CBACK(bt_hf_callbacks, connection_state_cb, btif_hf_cb[idx].state, - &btif_hf_cb[idx].connected_bda); - bdsetany(btif_hf_cb[idx].connected_bda.address); - btif_hf_cb[idx].peer_feat = 0; - clear_phone_state_multihf(idx); - hf_idx = btif_hf_latest_connected_idx(); - /* If AG_OPEN was received but SLC was not setup in a specified time (10 seconds), - ** then AG_CLOSE may be received. We need to advance the queue here - */ - btif_queue_advance(); - break; - - case BTA_AG_CONN_EVT: - clock_gettime(CLOCK_MONOTONIC, - &btif_hf_cb[idx].connected_timestamp); - BTIF_TRACE_DEBUG("%s: BTA_AG_CONN_EVT, idx = %d ", - __func__, idx); - btif_hf_cb[idx].peer_feat = p_data->conn.peer_feat; - btif_hf_cb[idx].state = BTHF_CONNECTION_STATE_SLC_CONNECTED; - hf_idx = btif_hf_latest_connected_idx(); - - HAL_CBACK(bt_hf_callbacks, connection_state_cb, btif_hf_cb[idx].state, - &btif_hf_cb[idx].connected_bda); - btif_queue_advance(); - break; - - case BTA_AG_AUDIO_OPEN_EVT: - hf_idx = idx; - HAL_CBACK(bt_hf_callbacks, audio_state_cb, BTHF_AUDIO_STATE_CONNECTED, - &btif_hf_cb[idx].connected_bda); - break; - - case BTA_AG_AUDIO_CLOSE_EVT: - HAL_CBACK(bt_hf_callbacks, audio_state_cb, BTHF_AUDIO_STATE_DISCONNECTED, - &btif_hf_cb[idx].connected_bda); - break; - - /* BTA auto-responds, silently discard */ - case BTA_AG_SPK_EVT: - case BTA_AG_MIC_EVT: - HAL_CBACK(bt_hf_callbacks, volume_cmd_cb, - (event == BTA_AG_SPK_EVT) ? BTHF_VOLUME_TYPE_SPK : - BTHF_VOLUME_TYPE_MIC, p_data->val.num, - &btif_hf_cb[idx].connected_bda); - break; + * + * Function btif_hf_upstreams_evt + * + * Description Executes HF UPSTREAMS events in btif context + * + * Returns void + * + ******************************************************************************/ +static void btif_hf_upstreams_evt(uint16_t event, char* p_param) { + tBTA_AG* p_data = (tBTA_AG*)p_param; + bdstr_t bdstr; + int idx = p_data->hdr.handle - 1; + + BTIF_TRACE_DEBUG("%s: event=%s", __func__, dump_hf_event(event)); + + if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) { + BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); + return; + } + + switch (event) { + case BTA_AG_ENABLE_EVT: + case BTA_AG_DISABLE_EVT: + break; + + case BTA_AG_REGISTER_EVT: + btif_hf_cb[idx].handle = p_data->reg.hdr.handle; + BTIF_TRACE_DEBUG( + "%s: BTA_AG_REGISTER_EVT," + "btif_hf_cb.handle = %d", + __func__, btif_hf_cb[idx].handle); + break; + + case BTA_AG_OPEN_EVT: + if (p_data->open.status == BTA_AG_SUCCESS) { + bdcpy(btif_hf_cb[idx].connected_bda.address, p_data->open.bd_addr); + btif_hf_cb[idx].state = BTHF_CONNECTION_STATE_CONNECTED; + btif_hf_cb[idx].peer_feat = 0; + clear_phone_state_multihf(idx); + } else if (btif_hf_cb[idx].state == BTHF_CONNECTION_STATE_CONNECTING) { + btif_hf_cb[idx].state = BTHF_CONNECTION_STATE_DISCONNECTED; + } else { + BTIF_TRACE_WARNING( + "%s: AG open failed, but another device connected. status=%d " + "state=%d connected device=%s", + __func__, p_data->open.status, btif_hf_cb[idx].state, + bdaddr_to_string(&btif_hf_cb[idx].connected_bda, bdstr, + sizeof(bdstr))); + break; + } - case BTA_AG_AT_A_EVT: - if ((btif_hf_cb[0].num_held + btif_hf_cb[0].num_active) == 0) - hf_idx = idx; - else - BTIF_TRACE_DEBUG("Donot set hf_idx for ATA since already in a call"); + HAL_CBACK(bt_hf_callbacks, connection_state_cb, btif_hf_cb[idx].state, + &btif_hf_cb[idx].connected_bda); + + if (btif_hf_cb[idx].state == BTHF_CONNECTION_STATE_DISCONNECTED) + bdsetany(btif_hf_cb[idx].connected_bda.address); + + if (p_data->open.status != BTA_AG_SUCCESS) btif_queue_advance(); + break; + + case BTA_AG_CLOSE_EVT: + btif_hf_cb[idx].connected_timestamp.tv_sec = 0; + btif_hf_cb[idx].state = BTHF_CONNECTION_STATE_DISCONNECTED; + BTIF_TRACE_DEBUG( + "%s: BTA_AG_CLOSE_EVT," + "idx = %d, btif_hf_cb.handle = %d", + __func__, idx, btif_hf_cb[idx].handle); + HAL_CBACK(bt_hf_callbacks, connection_state_cb, btif_hf_cb[idx].state, + &btif_hf_cb[idx].connected_bda); + bdsetany(btif_hf_cb[idx].connected_bda.address); + btif_hf_cb[idx].peer_feat = 0; + clear_phone_state_multihf(idx); + hf_idx = btif_hf_latest_connected_idx(); + /* If AG_OPEN was received but SLC was not setup in a specified time (10 + *seconds), + ** then AG_CLOSE may be received. We need to advance the queue here + */ + btif_queue_advance(); + break; + + case BTA_AG_CONN_EVT: + clock_gettime(CLOCK_MONOTONIC, &btif_hf_cb[idx].connected_timestamp); + BTIF_TRACE_DEBUG("%s: BTA_AG_CONN_EVT, idx = %d ", __func__, idx); + btif_hf_cb[idx].peer_feat = p_data->conn.peer_feat; + btif_hf_cb[idx].state = BTHF_CONNECTION_STATE_SLC_CONNECTED; + hf_idx = btif_hf_latest_connected_idx(); + + HAL_CBACK(bt_hf_callbacks, connection_state_cb, btif_hf_cb[idx].state, + &btif_hf_cb[idx].connected_bda); + btif_queue_advance(); + break; + + case BTA_AG_AUDIO_OPEN_EVT: + hf_idx = idx; + HAL_CBACK(bt_hf_callbacks, audio_state_cb, BTHF_AUDIO_STATE_CONNECTED, + &btif_hf_cb[idx].connected_bda); + break; + + case BTA_AG_AUDIO_CLOSE_EVT: + HAL_CBACK(bt_hf_callbacks, audio_state_cb, BTHF_AUDIO_STATE_DISCONNECTED, + &btif_hf_cb[idx].connected_bda); + break; + + /* BTA auto-responds, silently discard */ + case BTA_AG_SPK_EVT: + case BTA_AG_MIC_EVT: + HAL_CBACK(bt_hf_callbacks, volume_cmd_cb, + (event == BTA_AG_SPK_EVT) ? BTHF_VOLUME_TYPE_SPK + : BTHF_VOLUME_TYPE_MIC, + p_data->val.num, &btif_hf_cb[idx].connected_bda); + break; + + case BTA_AG_AT_A_EVT: + if ((btif_hf_cb[0].num_held + btif_hf_cb[0].num_active) == 0) + hf_idx = idx; + else + BTIF_TRACE_DEBUG("Donot set hf_idx for ATA since already in a call"); - HAL_CBACK(bt_hf_callbacks, answer_call_cmd_cb, - &btif_hf_cb[idx].connected_bda); - break; + HAL_CBACK(bt_hf_callbacks, answer_call_cmd_cb, + &btif_hf_cb[idx].connected_bda); + break; - /* Java needs to send OK/ERROR for these commands */ - case BTA_AG_AT_BLDN_EVT: - case BTA_AG_AT_D_EVT: - if ((btif_hf_cb[0].num_held + btif_hf_cb[0].num_active) == 0) - hf_idx = idx; - else - BTIF_TRACE_DEBUG("Donot set hf_idx for BLDN/D since already in a call"); + /* Java needs to send OK/ERROR for these commands */ + case BTA_AG_AT_BLDN_EVT: + case BTA_AG_AT_D_EVT: + if ((btif_hf_cb[0].num_held + btif_hf_cb[0].num_active) == 0) + hf_idx = idx; + else + BTIF_TRACE_DEBUG("Donot set hf_idx for BLDN/D since already in a call"); - HAL_CBACK(bt_hf_callbacks, dial_call_cmd_cb, + HAL_CBACK(bt_hf_callbacks, dial_call_cmd_cb, (event == BTA_AG_AT_D_EVT) ? p_data->val.str : NULL, - &btif_hf_cb[idx].connected_bda); - break; - - case BTA_AG_AT_CHUP_EVT: - HAL_CBACK(bt_hf_callbacks, hangup_call_cmd_cb, - &btif_hf_cb[idx].connected_bda); - break; - - case BTA_AG_AT_CIND_EVT: - HAL_CBACK(bt_hf_callbacks, cind_cmd_cb, - &btif_hf_cb[idx].connected_bda); - break; - - case BTA_AG_AT_VTS_EVT: - HAL_CBACK(bt_hf_callbacks, dtmf_cmd_cb, p_data->val.str[0], - &btif_hf_cb[idx].connected_bda); - break; - - case BTA_AG_AT_BVRA_EVT: - HAL_CBACK(bt_hf_callbacks, vr_cmd_cb, - (p_data->val.num == 1) ? BTHF_VR_STATE_STARTED : - BTHF_VR_STATE_STOPPED, &btif_hf_cb[idx].connected_bda); - break; - - case BTA_AG_AT_NREC_EVT: - HAL_CBACK(bt_hf_callbacks, nrec_cmd_cb, + &btif_hf_cb[idx].connected_bda); + break; + + case BTA_AG_AT_CHUP_EVT: + HAL_CBACK(bt_hf_callbacks, hangup_call_cmd_cb, + &btif_hf_cb[idx].connected_bda); + break; + + case BTA_AG_AT_CIND_EVT: + HAL_CBACK(bt_hf_callbacks, cind_cmd_cb, &btif_hf_cb[idx].connected_bda); + break; + + case BTA_AG_AT_VTS_EVT: + HAL_CBACK(bt_hf_callbacks, dtmf_cmd_cb, p_data->val.str[0], + &btif_hf_cb[idx].connected_bda); + break; + + case BTA_AG_AT_BVRA_EVT: + HAL_CBACK(bt_hf_callbacks, vr_cmd_cb, + (p_data->val.num == 1) ? BTHF_VR_STATE_STARTED + : BTHF_VR_STATE_STOPPED, + &btif_hf_cb[idx].connected_bda); + break; + + case BTA_AG_AT_NREC_EVT: + HAL_CBACK(bt_hf_callbacks, nrec_cmd_cb, (p_data->val.num == 1) ? BTHF_NREC_START : BTHF_NREC_STOP, - &btif_hf_cb[idx].connected_bda); - break; + &btif_hf_cb[idx].connected_bda); + break; - /* TODO: Add a callback for CBC */ - case BTA_AG_AT_CBC_EVT: - break; + /* TODO: Add a callback for CBC */ + case BTA_AG_AT_CBC_EVT: + break; - case BTA_AG_AT_CKPD_EVT: - HAL_CBACK(bt_hf_callbacks, key_pressed_cmd_cb, - &btif_hf_cb[idx].connected_bda); - break; + case BTA_AG_AT_CKPD_EVT: + HAL_CBACK(bt_hf_callbacks, key_pressed_cmd_cb, + &btif_hf_cb[idx].connected_bda); + break; #if (BTM_WBS_INCLUDED == TRUE) - case BTA_AG_WBS_EVT: - BTIF_TRACE_DEBUG("BTA_AG_WBS_EVT Set codec status %d codec %d 1=CVSD 2=MSBC", \ - p_data->val.hdr.status, p_data->val.num); - if(p_data->val.num == BTA_AG_CODEC_CVSD) - { HAL_CBACK(bt_hf_callbacks, wbs_cb, BTHF_WBS_NO, &btif_hf_cb[idx].connected_bda);} - else if(p_data->val.num == BTA_AG_CODEC_MSBC) - {HAL_CBACK(bt_hf_callbacks, wbs_cb, BTHF_WBS_YES, &btif_hf_cb[idx].connected_bda);} - else - {HAL_CBACK(bt_hf_callbacks, wbs_cb, BTHF_WBS_NONE, &btif_hf_cb[idx].connected_bda);} - break; + case BTA_AG_WBS_EVT: + BTIF_TRACE_DEBUG( + "BTA_AG_WBS_EVT Set codec status %d codec %d 1=CVSD 2=MSBC", + p_data->val.hdr.status, p_data->val.num); + if (p_data->val.num == BTA_AG_CODEC_CVSD) { + HAL_CBACK(bt_hf_callbacks, wbs_cb, BTHF_WBS_NO, + &btif_hf_cb[idx].connected_bda); + } else if (p_data->val.num == BTA_AG_CODEC_MSBC) { + HAL_CBACK(bt_hf_callbacks, wbs_cb, BTHF_WBS_YES, + &btif_hf_cb[idx].connected_bda); + } else { + HAL_CBACK(bt_hf_callbacks, wbs_cb, BTHF_WBS_NONE, + &btif_hf_cb[idx].connected_bda); + } + break; #endif - /* Java needs to send OK/ERROR for these commands */ - case BTA_AG_AT_CHLD_EVT: - HAL_CBACK(bt_hf_callbacks, chld_cmd_cb, (bthf_chld_type_t)atoi(p_data->val.str), - &btif_hf_cb[idx].connected_bda); - break; - - case BTA_AG_AT_CLCC_EVT: - HAL_CBACK(bt_hf_callbacks, clcc_cmd_cb, - &btif_hf_cb[idx].connected_bda); - break; - - case BTA_AG_AT_COPS_EVT: - HAL_CBACK(bt_hf_callbacks, cops_cmd_cb, - &btif_hf_cb[idx].connected_bda); - break; - - case BTA_AG_AT_UNAT_EVT: - HAL_CBACK(bt_hf_callbacks, unknown_at_cmd_cb, p_data->val.str, - &btif_hf_cb[idx].connected_bda); - break; - - case BTA_AG_AT_CNUM_EVT: - HAL_CBACK(bt_hf_callbacks, cnum_cmd_cb, - &btif_hf_cb[idx].connected_bda); - break; - - /* TODO: Some of these commands may need to be sent to app. For now respond with error */ - case BTA_AG_AT_BINP_EVT: - case BTA_AG_AT_BTRH_EVT: - send_at_result(BTA_AG_OK_ERROR, BTA_AG_ERR_OP_NOT_SUPPORTED, idx); - break; - case BTA_AG_AT_BAC_EVT: - BTIF_TRACE_DEBUG("AG Bitmap of peer-codecs %d", p_data->val.num); + /* Java needs to send OK/ERROR for these commands */ + case BTA_AG_AT_CHLD_EVT: + HAL_CBACK(bt_hf_callbacks, chld_cmd_cb, + (bthf_chld_type_t)atoi(p_data->val.str), + &btif_hf_cb[idx].connected_bda); + break; + + case BTA_AG_AT_CLCC_EVT: + HAL_CBACK(bt_hf_callbacks, clcc_cmd_cb, &btif_hf_cb[idx].connected_bda); + break; + + case BTA_AG_AT_COPS_EVT: + HAL_CBACK(bt_hf_callbacks, cops_cmd_cb, &btif_hf_cb[idx].connected_bda); + break; + + case BTA_AG_AT_UNAT_EVT: + HAL_CBACK(bt_hf_callbacks, unknown_at_cmd_cb, p_data->val.str, + &btif_hf_cb[idx].connected_bda); + break; + + case BTA_AG_AT_CNUM_EVT: + HAL_CBACK(bt_hf_callbacks, cnum_cmd_cb, &btif_hf_cb[idx].connected_bda); + break; + + /* TODO: Some of these commands may need to be sent to app. For now respond + * with error */ + case BTA_AG_AT_BINP_EVT: + case BTA_AG_AT_BTRH_EVT: + send_at_result(BTA_AG_OK_ERROR, BTA_AG_ERR_OP_NOT_SUPPORTED, idx); + break; + case BTA_AG_AT_BAC_EVT: + BTIF_TRACE_DEBUG("AG Bitmap of peer-codecs %d", p_data->val.num); #if (BTM_WBS_INCLUDED == TRUE) - /* If the peer supports mSBC and the BTIF prefferred codec is also mSBC, then - we should set the BTA AG Codec to mSBC. This would trigger a +BCS to mSBC at the time - of SCO connection establishment */ - if ((btif_conf_hf_force_wbs == true) && (p_data->val.num & BTA_AG_CODEC_MSBC)) - { - BTIF_TRACE_EVENT("%s btif_hf override-Preferred Codec to MSBC", __func__); - BTA_AgSetCodec(btif_hf_cb[idx].handle,BTA_AG_CODEC_MSBC); - } - else - { - BTIF_TRACE_EVENT("%s btif_hf override-Preferred Codec to CVSD", __func__); - BTA_AgSetCodec(btif_hf_cb[idx].handle,BTA_AG_CODEC_CVSD); - } + /* If the peer supports mSBC and the BTIF prefferred codec is also mSBC, + then + we should set the BTA AG Codec to mSBC. This would trigger a +BCS to mSBC + at the time + of SCO connection establishment */ + if ((btif_conf_hf_force_wbs == true) && + (p_data->val.num & BTA_AG_CODEC_MSBC)) { + BTIF_TRACE_EVENT("%s btif_hf override-Preferred Codec to MSBC", + __func__); + BTA_AgSetCodec(btif_hf_cb[idx].handle, BTA_AG_CODEC_MSBC); + } else { + BTIF_TRACE_EVENT("%s btif_hf override-Preferred Codec to CVSD", + __func__); + BTA_AgSetCodec(btif_hf_cb[idx].handle, BTA_AG_CODEC_CVSD); + } #endif - break; - case BTA_AG_AT_BCS_EVT: - BTIF_TRACE_DEBUG("AG final seleded codec is %d 1=CVSD 2=MSBC", p_data->val.num); - /* no BTHF_WBS_NONE case, becuase HF1.6 supported device can send BCS */ - HAL_CBACK(bt_hf_callbacks, wbs_cb,(p_data->val.num == BTA_AG_CODEC_MSBC) ? \ - BTHF_WBS_YES : BTHF_WBS_NO, &btif_hf_cb[idx].connected_bda); - break; - - case BTA_AG_AT_BIND_EVT: - if (p_data->val.hdr.status == BTA_AG_SUCCESS) - { - HAL_CBACK(bt_hf_callbacks, bind_cb,p_data->val.str, - &btif_hf_cb[idx].connected_bda); - } - break; + break; + case BTA_AG_AT_BCS_EVT: + BTIF_TRACE_DEBUG("AG final seleded codec is %d 1=CVSD 2=MSBC", + p_data->val.num); + /* no BTHF_WBS_NONE case, becuase HF1.6 supported device can send BCS */ + HAL_CBACK( + bt_hf_callbacks, wbs_cb, + (p_data->val.num == BTA_AG_CODEC_MSBC) ? BTHF_WBS_YES : BTHF_WBS_NO, + &btif_hf_cb[idx].connected_bda); + break; + + case BTA_AG_AT_BIND_EVT: + if (p_data->val.hdr.status == BTA_AG_SUCCESS) { + HAL_CBACK(bt_hf_callbacks, bind_cb, p_data->val.str, + &btif_hf_cb[idx].connected_bda); + } + break; - case BTA_AG_AT_BIEV_EVT: - if (p_data->val.hdr.status == BTA_AG_SUCCESS) - { - HAL_CBACK(bt_hf_callbacks, biev_cb, (bthf_hf_ind_type_t)p_data->val.lidx, (int)p_data->val.num, - &btif_hf_cb[idx].connected_bda); - } - break; - default: - BTIF_TRACE_WARNING("%s: Unhandled event: %d", __func__, event); - break; - } + case BTA_AG_AT_BIEV_EVT: + if (p_data->val.hdr.status == BTA_AG_SUCCESS) { + HAL_CBACK(bt_hf_callbacks, biev_cb, + (bthf_hf_ind_type_t)p_data->val.lidx, (int)p_data->val.num, + &btif_hf_cb[idx].connected_bda); + } + break; + default: + BTIF_TRACE_WARNING("%s: Unhandled event: %d", __func__, event); + break; + } } /******************************************************************************* -** -** Function bte_hf_evt -** -** Description Switches context from BTE to BTIF for all HF events -** -** Returns void -** -*******************************************************************************/ - -static void bte_hf_evt(tBTA_AG_EVT event, tBTA_AG *p_data) -{ - bt_status_t status; - int param_len = 0; - - /* TODO: BTA sends the union members and not tBTA_AG. If using param_len=sizeof(tBTA_AG), we get a crash on memcpy */ - if (BTA_AG_REGISTER_EVT == event) - param_len = sizeof(tBTA_AG_REGISTER); - else if (BTA_AG_OPEN_EVT == event) - param_len = sizeof(tBTA_AG_OPEN); - else if (BTA_AG_CONN_EVT == event) - param_len = sizeof(tBTA_AG_CONN); - else if ( (BTA_AG_CLOSE_EVT == event) || (BTA_AG_AUDIO_OPEN_EVT == event) || (BTA_AG_AUDIO_CLOSE_EVT == event)) - param_len = sizeof(tBTA_AG_HDR); - else if (p_data) - param_len = sizeof(tBTA_AG_VAL); - - /* switch context to btif task context (copy full union size for convenience) */ - status = btif_transfer_context(btif_hf_upstreams_evt, (uint16_t)event, (char*)p_data, param_len, NULL); - - /* catch any failed context transfers */ - ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); + * + * Function bte_hf_evt + * + * Description Switches context from BTE to BTIF for all HF events + * + * Returns void + * + ******************************************************************************/ + +static void bte_hf_evt(tBTA_AG_EVT event, tBTA_AG* p_data) { + bt_status_t status; + int param_len = 0; + + /* TODO: BTA sends the union members and not tBTA_AG. If using + * param_len=sizeof(tBTA_AG), we get a crash on memcpy */ + if (BTA_AG_REGISTER_EVT == event) + param_len = sizeof(tBTA_AG_REGISTER); + else if (BTA_AG_OPEN_EVT == event) + param_len = sizeof(tBTA_AG_OPEN); + else if (BTA_AG_CONN_EVT == event) + param_len = sizeof(tBTA_AG_CONN); + else if ((BTA_AG_CLOSE_EVT == event) || (BTA_AG_AUDIO_OPEN_EVT == event) || + (BTA_AG_AUDIO_CLOSE_EVT == event)) + param_len = sizeof(tBTA_AG_HDR); + else if (p_data) + param_len = sizeof(tBTA_AG_VAL); + + /* switch context to btif task context (copy full union size for convenience) + */ + status = btif_transfer_context(btif_hf_upstreams_evt, (uint16_t)event, + (char*)p_data, param_len, NULL); + + /* catch any failed context transfers */ + ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); } /******************************************************************************* -** -** Function btif_in_hf_generic_evt -** -** Description Processes generic events to be sent to JNI that are not triggered from the BTA. -** Always runs in BTIF context -** -** Returns void -** -*******************************************************************************/ -static void btif_in_hf_generic_evt(uint16_t event, char *p_param) -{ - int idx = btif_hf_idx_by_bdaddr((bt_bdaddr_t *)p_param); - - BTIF_TRACE_EVENT("%s: event=%d", __func__, event); - - if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) - { - BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); - return; - } - - switch (event) { - case BTIF_HFP_CB_AUDIO_CONNECTING: - { - HAL_CBACK(bt_hf_callbacks, audio_state_cb, BTHF_AUDIO_STATE_CONNECTING, - &btif_hf_cb[idx].connected_bda); - } break; - default: - { - BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __func__, event); - } - break; - } + * + * Function btif_in_hf_generic_evt + * + * Description Processes generic events to be sent to JNI that are not + *triggered from the BTA. + * Always runs in BTIF context + * + * Returns void + * + ******************************************************************************/ +static void btif_in_hf_generic_evt(uint16_t event, char* p_param) { + int idx = btif_hf_idx_by_bdaddr((bt_bdaddr_t*)p_param); + + BTIF_TRACE_EVENT("%s: event=%d", __func__, event); + + if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) { + BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); + return; + } + + switch (event) { + case BTIF_HFP_CB_AUDIO_CONNECTING: { + HAL_CBACK(bt_hf_callbacks, audio_state_cb, BTHF_AUDIO_STATE_CONNECTING, + &btif_hf_cb[idx].connected_bda); + } break; + default: { + BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __func__, event); + } break; + } } /******************************************************************************* -** -** Function btif_hf_init -** -** Description initializes the hf interface -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t init( bthf_callbacks_t* callbacks, int max_hf_clients) -{ - btif_max_hf_clients = max_hf_clients; - BTIF_TRACE_DEBUG("%s - max_hf_clients=%d", __func__, btif_max_hf_clients); - - bt_hf_callbacks = callbacks; - memset(&btif_hf_cb, 0, sizeof(btif_hf_cb)); - - /* Invoke the enable service API to the core to set the appropriate service_id - * Internally, the HSP_SERVICE_ID shall also be enabled if HFP is enabled (phone) - * othwerwise only HSP is enabled (tablet) - */ + * + * Function btif_hf_init + * + * Description initializes the hf interface + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t init(bthf_callbacks_t* callbacks, int max_hf_clients) { + btif_max_hf_clients = max_hf_clients; + BTIF_TRACE_DEBUG("%s - max_hf_clients=%d", __func__, btif_max_hf_clients); + + bt_hf_callbacks = callbacks; + memset(&btif_hf_cb, 0, sizeof(btif_hf_cb)); + +/* Invoke the enable service API to the core to set the appropriate service_id + * Internally, the HSP_SERVICE_ID shall also be enabled if HFP is enabled + * (phone) + * othwerwise only HSP is enabled (tablet) +*/ #if (defined(BTIF_HF_SERVICES) && (BTIF_HF_SERVICES & BTA_HFP_SERVICE_MASK)) - btif_enable_service(BTA_HFP_SERVICE_ID); + btif_enable_service(BTA_HFP_SERVICE_ID); #else - btif_enable_service(BTA_HSP_SERVICE_ID); + btif_enable_service(BTA_HSP_SERVICE_ID); #endif - for (int i = 0; i < btif_max_hf_clients; i++) - clear_phone_state_multihf(i); + for (int i = 0; i < btif_max_hf_clients; i++) clear_phone_state_multihf(i); - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function connect -** -** Description connect to headset -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t connect_int(bt_bdaddr_t *bd_addr, uint16_t uuid) -{ - CHECK_BTHF_INIT(); - int i; - for (i = 0; i < btif_max_hf_clients;) - { - if (((btif_hf_cb[i].state == BTHF_CONNECTION_STATE_CONNECTED) || - (btif_hf_cb[i].state == BTHF_CONNECTION_STATE_SLC_CONNECTED))) - i++; - else - break; - } + * + * Function connect + * + * Description connect to headset + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t connect_int(bt_bdaddr_t* bd_addr, uint16_t uuid) { + CHECK_BTHF_INIT(); + int i; + for (i = 0; i < btif_max_hf_clients;) { + if (((btif_hf_cb[i].state == BTHF_CONNECTION_STATE_CONNECTED) || + (btif_hf_cb[i].state == BTHF_CONNECTION_STATE_SLC_CONNECTED))) + i++; + else + break; + } - if (i == btif_max_hf_clients) - return BT_STATUS_BUSY; + if (i == btif_max_hf_clients) return BT_STATUS_BUSY; - if (!is_connected(bd_addr)) - { - btif_hf_cb[i].state = BTHF_CONNECTION_STATE_CONNECTING; - bdcpy(btif_hf_cb[i].connected_bda.address, bd_addr->address); + if (!is_connected(bd_addr)) { + btif_hf_cb[i].state = BTHF_CONNECTION_STATE_CONNECTING; + bdcpy(btif_hf_cb[i].connected_bda.address, bd_addr->address); - BTA_AgOpen(btif_hf_cb[i].handle, btif_hf_cb[i].connected_bda.address, - BTIF_HF_SECURITY, BTIF_HF_SERVICES); - return BT_STATUS_SUCCESS; - } + BTA_AgOpen(btif_hf_cb[i].handle, btif_hf_cb[i].connected_bda.address, + BTIF_HF_SECURITY, BTIF_HF_SERVICES); + return BT_STATUS_SUCCESS; + } - return BT_STATUS_BUSY; + return BT_STATUS_BUSY; } -static bt_status_t connect( bt_bdaddr_t *bd_addr ) -{ - CHECK_BTHF_INIT(); - return btif_queue_connect(UUID_SERVCLASS_AG_HANDSFREE, bd_addr, connect_int); +static bt_status_t connect(bt_bdaddr_t* bd_addr) { + CHECK_BTHF_INIT(); + return btif_queue_connect(UUID_SERVCLASS_AG_HANDSFREE, bd_addr, connect_int); } /******************************************************************************* -** -** Function disconnect -** -** Description disconnect from headset -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t disconnect( bt_bdaddr_t *bd_addr ) -{ - CHECK_BTHF_INIT(); - - int idx = btif_hf_idx_by_bdaddr(bd_addr); - - if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) - { - BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); - return BT_STATUS_FAIL; - } + * + * Function disconnect + * + * Description disconnect from headset + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t disconnect(bt_bdaddr_t* bd_addr) { + CHECK_BTHF_INIT(); - if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) - { - BTA_AgClose(btif_hf_cb[idx].handle); - return BT_STATUS_SUCCESS; - } + int idx = btif_hf_idx_by_bdaddr(bd_addr); + if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) { + BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); return BT_STATUS_FAIL; + } + + if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) { + BTA_AgClose(btif_hf_cb[idx].handle); + return BT_STATUS_SUCCESS; + } + + return BT_STATUS_FAIL; } /******************************************************************************* -** -** Function connect_audio -** -** Description create an audio connection -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t connect_audio( bt_bdaddr_t *bd_addr ) -{ - CHECK_BTHF_INIT(); - - int idx = btif_hf_idx_by_bdaddr(bd_addr); - - if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) - { - BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); - return BT_STATUS_FAIL; - } + * + * Function connect_audio + * + * Description create an audio connection + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t connect_audio(bt_bdaddr_t* bd_addr) { + CHECK_BTHF_INIT(); - /* Check if SLC is connected */ - if (btif_hf_check_if_slc_connected() != BT_STATUS_SUCCESS) - return BT_STATUS_NOT_READY; + int idx = btif_hf_idx_by_bdaddr(bd_addr); - if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) - { - BTA_AgAudioOpen(btif_hf_cb[idx].handle); + if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) { + BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); + return BT_STATUS_FAIL; + } - /* Inform the application that the audio connection has been initiated successfully */ - btif_transfer_context(btif_in_hf_generic_evt, BTIF_HFP_CB_AUDIO_CONNECTING, - (char *)bd_addr, sizeof(bt_bdaddr_t), NULL); - return BT_STATUS_SUCCESS; - } + /* Check if SLC is connected */ + if (btif_hf_check_if_slc_connected() != BT_STATUS_SUCCESS) + return BT_STATUS_NOT_READY; - return BT_STATUS_FAIL; + if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) { + BTA_AgAudioOpen(btif_hf_cb[idx].handle); + + /* Inform the application that the audio connection has been initiated + * successfully */ + btif_transfer_context(btif_in_hf_generic_evt, BTIF_HFP_CB_AUDIO_CONNECTING, + (char*)bd_addr, sizeof(bt_bdaddr_t), NULL); + return BT_STATUS_SUCCESS; + } + + return BT_STATUS_FAIL; } /******************************************************************************* -** -** Function disconnect_audio -** -** Description close the audio connection -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t disconnect_audio( bt_bdaddr_t *bd_addr ) -{ - CHECK_BTHF_INIT(); - - int idx = btif_hf_idx_by_bdaddr(bd_addr); - - if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) - { - BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); - return BT_STATUS_FAIL; - } + * + * Function disconnect_audio + * + * Description close the audio connection + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t disconnect_audio(bt_bdaddr_t* bd_addr) { + CHECK_BTHF_INIT(); - if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) - { - BTA_AgAudioClose(btif_hf_cb[idx].handle); - return BT_STATUS_SUCCESS; - } + int idx = btif_hf_idx_by_bdaddr(bd_addr); + if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) { + BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); return BT_STATUS_FAIL; + } + + if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) { + BTA_AgAudioClose(btif_hf_cb[idx].handle); + return BT_STATUS_SUCCESS; + } + + return BT_STATUS_FAIL; } /******************************************************************************* -** -** Function start_voice_recognition -** -** Description start voice recognition -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t start_voice_recognition(bt_bdaddr_t *bd_addr) -{ - CHECK_BTHF_INIT(); - - int idx = btif_hf_idx_by_bdaddr(bd_addr); - - if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) - { - BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); - return BT_STATUS_FAIL; - } + * + * Function start_voice_recognition + * + * Description start voice recognition + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t start_voice_recognition(bt_bdaddr_t* bd_addr) { + CHECK_BTHF_INIT(); - if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) - { - if (btif_hf_cb[idx].peer_feat & BTA_AG_PEER_FEAT_VREC) - { - tBTA_AG_RES_DATA ag_res; - memset(&ag_res, 0, sizeof(ag_res)); - ag_res.state = 1; - BTA_AgResult (btif_hf_cb[idx].handle, BTA_AG_BVRA_RES, &ag_res); + int idx = btif_hf_idx_by_bdaddr(bd_addr); - return BT_STATUS_SUCCESS; - } - else - { - return BT_STATUS_UNSUPPORTED; - } + if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) { + BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); + return BT_STATUS_FAIL; + } + + if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) { + if (btif_hf_cb[idx].peer_feat & BTA_AG_PEER_FEAT_VREC) { + tBTA_AG_RES_DATA ag_res; + memset(&ag_res, 0, sizeof(ag_res)); + ag_res.state = 1; + BTA_AgResult(btif_hf_cb[idx].handle, BTA_AG_BVRA_RES, &ag_res); + + return BT_STATUS_SUCCESS; + } else { + return BT_STATUS_UNSUPPORTED; } + } - return BT_STATUS_NOT_READY; + return BT_STATUS_NOT_READY; } /******************************************************************************* -** -** Function stop_voice_recognition -** -** Description stop voice recognition -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t stop_voice_recognition(bt_bdaddr_t *bd_addr) -{ - CHECK_BTHF_INIT(); - - int idx = btif_hf_idx_by_bdaddr(bd_addr); - - if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) - { - BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); - return BT_STATUS_FAIL; - } + * + * Function stop_voice_recognition + * + * Description stop voice recognition + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t stop_voice_recognition(bt_bdaddr_t* bd_addr) { + CHECK_BTHF_INIT(); - if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) - { - if (btif_hf_cb[idx].peer_feat & BTA_AG_PEER_FEAT_VREC) - { - tBTA_AG_RES_DATA ag_res; - memset(&ag_res, 0, sizeof(ag_res)); - ag_res.state = 0; - BTA_AgResult (btif_hf_cb[idx].handle, BTA_AG_BVRA_RES, &ag_res); + int idx = btif_hf_idx_by_bdaddr(bd_addr); - return BT_STATUS_SUCCESS; - } - else - { - return BT_STATUS_UNSUPPORTED; - } + if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) { + BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); + return BT_STATUS_FAIL; + } + + if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) { + if (btif_hf_cb[idx].peer_feat & BTA_AG_PEER_FEAT_VREC) { + tBTA_AG_RES_DATA ag_res; + memset(&ag_res, 0, sizeof(ag_res)); + ag_res.state = 0; + BTA_AgResult(btif_hf_cb[idx].handle, BTA_AG_BVRA_RES, &ag_res); + + return BT_STATUS_SUCCESS; + } else { + return BT_STATUS_UNSUPPORTED; } + } - return BT_STATUS_NOT_READY; + return BT_STATUS_NOT_READY; } /******************************************************************************* -** -** Function volume_control -** -** Description volume control -** -** Returns bt_status_t -** -*******************************************************************************/ + * + * Function volume_control + * + * Description volume control + * + * Returns bt_status_t + * + ******************************************************************************/ static bt_status_t volume_control(bthf_volume_type_t type, int volume, - bt_bdaddr_t *bd_addr) -{ - CHECK_BTHF_INIT(); + bt_bdaddr_t* bd_addr) { + CHECK_BTHF_INIT(); - int idx = btif_hf_idx_by_bdaddr(bd_addr); - - if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) - { - BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); - return BT_STATUS_FAIL; - } - - tBTA_AG_RES_DATA ag_res; - memset(&ag_res, 0, sizeof(tBTA_AG_RES_DATA)); - if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) - { - ag_res.num = volume; - BTA_AgResult(btif_hf_cb[idx].handle, - (type == BTHF_VOLUME_TYPE_SPK) ? BTA_AG_SPK_RES : BTA_AG_MIC_RES, - &ag_res); - return BT_STATUS_SUCCESS; - } + int idx = btif_hf_idx_by_bdaddr(bd_addr); + if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) { + BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); return BT_STATUS_FAIL; + } + + tBTA_AG_RES_DATA ag_res; + memset(&ag_res, 0, sizeof(tBTA_AG_RES_DATA)); + if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) { + ag_res.num = volume; + BTA_AgResult( + btif_hf_cb[idx].handle, + (type == BTHF_VOLUME_TYPE_SPK) ? BTA_AG_SPK_RES : BTA_AG_MIC_RES, + &ag_res); + return BT_STATUS_SUCCESS; + } + + return BT_STATUS_FAIL; } /******************************************************************************* -** -** Function device_status_notification -** -** Description Combined device status change notification -** -** Returns bt_status_t -** -*******************************************************************************/ + * + * Function device_status_notification + * + * Description Combined device status change notification + * + * Returns bt_status_t + * + ******************************************************************************/ static bt_status_t device_status_notification(bthf_network_state_t ntk_state, - bthf_service_type_t svc_type, int signal, int batt_chg) -{ - CHECK_BTHF_INIT(); - - if (is_connected(NULL)) - { - /* send all indicators to BTA. - ** BTA will make sure no duplicates are sent out - */ - send_indicator_update(BTA_AG_IND_SERVICE, - (ntk_state == BTHF_NETWORK_STATE_AVAILABLE) ? 1 : 0); - send_indicator_update(BTA_AG_IND_ROAM, - (svc_type == BTHF_SERVICE_TYPE_HOME) ? 0 : 1); - send_indicator_update(BTA_AG_IND_SIGNAL, signal); - send_indicator_update(BTA_AG_IND_BATTCHG, batt_chg); - return BT_STATUS_SUCCESS; - } + bthf_service_type_t svc_type, + int signal, int batt_chg) { + CHECK_BTHF_INIT(); + if (is_connected(NULL)) { + /* send all indicators to BTA. + ** BTA will make sure no duplicates are sent out + */ + send_indicator_update(BTA_AG_IND_SERVICE, + (ntk_state == BTHF_NETWORK_STATE_AVAILABLE) ? 1 : 0); + send_indicator_update(BTA_AG_IND_ROAM, + (svc_type == BTHF_SERVICE_TYPE_HOME) ? 0 : 1); + send_indicator_update(BTA_AG_IND_SIGNAL, signal); + send_indicator_update(BTA_AG_IND_BATTCHG, batt_chg); return BT_STATUS_SUCCESS; + } + + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function cops_response -** -** Description Response for COPS command -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t cops_response(const char *cops, bt_bdaddr_t *bd_addr) -{ - CHECK_BTHF_INIT(); - - int idx = btif_hf_idx_by_bdaddr(bd_addr); - - if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) - { - BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); - return BT_STATUS_FAIL; - } - - if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) - { - tBTA_AG_RES_DATA ag_res; + * + * Function cops_response + * + * Description Response for COPS command + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t cops_response(const char* cops, bt_bdaddr_t* bd_addr) { + CHECK_BTHF_INIT(); - /* Format the response */ - snprintf (ag_res.str, sizeof(ag_res.str), "0,0,\"%.16s\"", cops); - ag_res.ok_flag = BTA_AG_OK_DONE; + int idx = btif_hf_idx_by_bdaddr(bd_addr); - BTA_AgResult (btif_hf_cb[idx].handle, BTA_AG_COPS_RES, &ag_res); - return BT_STATUS_SUCCESS; - } + if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) { + BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); return BT_STATUS_FAIL; + } + + if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) { + tBTA_AG_RES_DATA ag_res; + + /* Format the response */ + snprintf(ag_res.str, sizeof(ag_res.str), "0,0,\"%.16s\"", cops); + ag_res.ok_flag = BTA_AG_OK_DONE; + + BTA_AgResult(btif_hf_cb[idx].handle, BTA_AG_COPS_RES, &ag_res); + return BT_STATUS_SUCCESS; + } + return BT_STATUS_FAIL; } /******************************************************************************* -** -** Function cind_response -** -** Description Response for CIND command -** -** Returns bt_status_t -** -*******************************************************************************/ + * + * Function cind_response + * + * Description Response for CIND command + * + * Returns bt_status_t + * + ******************************************************************************/ static bt_status_t cind_response(int svc, int num_active, int num_held, - bthf_call_state_t call_setup_state, - int signal, int roam, int batt_chg, - bt_bdaddr_t *bd_addr) -{ - CHECK_BTHF_INIT(); - - int idx = btif_hf_idx_by_bdaddr(bd_addr); - - if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) - { - BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); - return BT_STATUS_FAIL; - } - - if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) - { - tBTA_AG_RES_DATA ag_res; - - memset (&ag_res, 0, sizeof (ag_res)); - /* per the errata 2043, call=1 implies atleast one call is in progress (active/held) - ** https://www.bluetooth.org/errata/errata_view.cfm?errata_id=2043 - **/ - snprintf (ag_res.str, sizeof(ag_res.str), "%d,%d,%d,%d,%d,%d,%d", - (num_active + num_held) ? 1 : 0, /* Call state */ - callstate_to_callsetup(call_setup_state), /* Callsetup state */ - svc, /* network service */ - signal, /* Signal strength */ - roam, /* Roaming indicator */ - batt_chg, /* Battery level */ - ((num_held == 0) ? 0 : ((num_active == 0) ? 2 : 1))); /* Call held */ - - BTA_AgResult (btif_hf_cb[idx].handle, BTA_AG_CIND_RES, &ag_res); + bthf_call_state_t call_setup_state, int signal, + int roam, int batt_chg, bt_bdaddr_t* bd_addr) { + CHECK_BTHF_INIT(); - return BT_STATUS_SUCCESS; - } + int idx = btif_hf_idx_by_bdaddr(bd_addr); + if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) { + BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); return BT_STATUS_FAIL; -} - -/******************************************************************************* -** -** Function bind_response -** -** Description Send +BIND response -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t bind_response(bthf_hf_ind_type_t ind_id, bthf_hf_ind_status_t ind_status, - bt_bdaddr_t * bd_addr) -{ - CHECK_BTHF_INIT(); - - int index = btif_hf_idx_by_bdaddr(bd_addr); - if (!is_connected(bd_addr) || index == BTIF_HF_INVALID_IDX) - return BT_STATUS_FAIL; + } + if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) { tBTA_AG_RES_DATA ag_res; + memset(&ag_res, 0, sizeof(ag_res)); - ag_res.ind.id = ind_id; - ag_res.ind.on_demand = (ind_status == BTHF_HF_IND_ENABLED); + /* per the errata 2043, call=1 implies atleast one call is in progress + *(active/held) + ** https://www.bluetooth.org/errata/errata_view.cfm?errata_id=2043 + **/ + snprintf( + ag_res.str, sizeof(ag_res.str), "%d,%d,%d,%d,%d,%d,%d", + (num_active + num_held) ? 1 : 0, /* Call state */ + callstate_to_callsetup(call_setup_state), /* Callsetup state */ + svc, /* network service */ + signal, /* Signal strength */ + roam, /* Roaming indicator */ + batt_chg, /* Battery level */ + ((num_held == 0) ? 0 : ((num_active == 0) ? 2 : 1))); /* Call held */ + + BTA_AgResult(btif_hf_cb[idx].handle, BTA_AG_CIND_RES, &ag_res); - BTA_AgResult(btif_hf_cb[index].handle, BTA_AG_BIND_RES, &ag_res); return BT_STATUS_SUCCESS; + } + + return BT_STATUS_FAIL; } /******************************************************************************* -** -** Function formatted_at_response -** -** Description Pre-formatted AT response, typically in response to unknown AT cmd -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t formatted_at_response(const char *rsp, bt_bdaddr_t *bd_addr) -{ - CHECK_BTHF_INIT(); - tBTA_AG_RES_DATA ag_res; - int idx = btif_hf_idx_by_bdaddr(bd_addr); - - if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) - { - BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); - return BT_STATUS_FAIL; - } + * + * Function bind_response + * + * Description Send +BIND response + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t bind_response(bthf_hf_ind_type_t ind_id, + bthf_hf_ind_status_t ind_status, + bt_bdaddr_t* bd_addr) { + CHECK_BTHF_INIT(); - if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) - { - /* Format the response and send */ - memset (&ag_res, 0, sizeof (ag_res)); - strncpy(ag_res.str, rsp, BTA_AG_AT_MAX_LEN); - BTA_AgResult (btif_hf_cb[idx].handle, BTA_AG_UNAT_RES, &ag_res); + int index = btif_hf_idx_by_bdaddr(bd_addr); + if (!is_connected(bd_addr) || index == BTIF_HF_INVALID_IDX) + return BT_STATUS_FAIL; - return BT_STATUS_SUCCESS; - } + tBTA_AG_RES_DATA ag_res; + memset(&ag_res, 0, sizeof(ag_res)); + ag_res.ind.id = ind_id; + ag_res.ind.on_demand = (ind_status == BTHF_HF_IND_ENABLED); + BTA_AgResult(btif_hf_cb[index].handle, BTA_AG_BIND_RES, &ag_res); + return BT_STATUS_SUCCESS; +} + +/******************************************************************************* + * + * Function formatted_at_response + * + * Description Pre-formatted AT response, typically in response to unknown + *AT cmd + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t formatted_at_response(const char* rsp, + bt_bdaddr_t* bd_addr) { + CHECK_BTHF_INIT(); + tBTA_AG_RES_DATA ag_res; + int idx = btif_hf_idx_by_bdaddr(bd_addr); + + if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) { + BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); return BT_STATUS_FAIL; + } + + if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) { + /* Format the response and send */ + memset(&ag_res, 0, sizeof(ag_res)); + strncpy(ag_res.str, rsp, BTA_AG_AT_MAX_LEN); + BTA_AgResult(btif_hf_cb[idx].handle, BTA_AG_UNAT_RES, &ag_res); + + return BT_STATUS_SUCCESS; + } + + return BT_STATUS_FAIL; } /******************************************************************************* -** -** Function at_response -** -** Description ok/error response -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t at_response(bthf_at_response_t response_code, - int error_code, bt_bdaddr_t *bd_addr) -{ - CHECK_BTHF_INIT(); - - int idx = btif_hf_idx_by_bdaddr(bd_addr); - - if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) - { - BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); - return BT_STATUS_FAIL; - } + * + * Function at_response + * + * Description ok/error response + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t at_response(bthf_at_response_t response_code, int error_code, + bt_bdaddr_t* bd_addr) { + CHECK_BTHF_INIT(); - if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) - { - send_at_result((response_code == BTHF_AT_RESPONSE_OK) ? BTA_AG_OK_DONE - : BTA_AG_OK_ERROR, error_code, idx); - return BT_STATUS_SUCCESS; - } + int idx = btif_hf_idx_by_bdaddr(bd_addr); + if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) { + BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); return BT_STATUS_FAIL; + } + + if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) { + send_at_result((response_code == BTHF_AT_RESPONSE_OK) ? BTA_AG_OK_DONE + : BTA_AG_OK_ERROR, + error_code, idx); + return BT_STATUS_SUCCESS; + } + + return BT_STATUS_FAIL; } /******************************************************************************* -** -** Function clcc_response -** -** Description response for CLCC command -** Can be iteratively called for each call index. Call index -** of 0 will be treated as NULL termination (Completes response) -** -** Returns bt_status_t -** -*******************************************************************************/ + * + * Function clcc_response + * + * Description response for CLCC command + * Can be iteratively called for each call index. Call index + * of 0 will be treated as NULL termination (Completes + *response) + * + * Returns bt_status_t + * + ******************************************************************************/ static bt_status_t clcc_response(int index, bthf_call_direction_t dir, - bthf_call_state_t state, bthf_call_mode_t mode, - bthf_call_mpty_type_t mpty, const char *number, - bthf_call_addrtype_t type, bt_bdaddr_t *bd_addr) -{ - CHECK_BTHF_INIT(); - - int idx = btif_hf_idx_by_bdaddr(bd_addr); - - if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) - { - BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); - return BT_STATUS_FAIL; - } + bthf_call_state_t state, bthf_call_mode_t mode, + bthf_call_mpty_type_t mpty, const char* number, + bthf_call_addrtype_t type, + bt_bdaddr_t* bd_addr) { + CHECK_BTHF_INIT(); - if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) - { - tBTA_AG_RES_DATA ag_res; + int idx = btif_hf_idx_by_bdaddr(bd_addr); - memset (&ag_res, 0, sizeof (ag_res)); + if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) { + BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); + return BT_STATUS_FAIL; + } - /* Format the response */ - if (index == 0) - { - ag_res.ok_flag = BTA_AG_OK_DONE; - } - else - { - BTIF_TRACE_EVENT("clcc_response: [%d] dir %d state %d mode %d number = %s type = %d", - index, dir, state, mode, number, type); - int xx = snprintf (ag_res.str, sizeof(ag_res.str), "%d,%d,%d,%d,%d", - index, dir, state, mode, mpty); - - if (number) - { - size_t rem_bytes = sizeof(ag_res.str) - xx; - if ((type == BTHF_CALL_ADDRTYPE_INTERNATIONAL) && (*number != '+')) - snprintf (&ag_res.str[xx], rem_bytes, ",\"+%s\",%d", number, type); - else - snprintf (&ag_res.str[xx], rem_bytes, ",\"%s\",%d", number, type); - } - } - BTA_AgResult (btif_hf_cb[idx].handle, BTA_AG_CLCC_RES, &ag_res); + if (is_connected(bd_addr) && (idx != BTIF_HF_INVALID_IDX)) { + tBTA_AG_RES_DATA ag_res; - return BT_STATUS_SUCCESS; + memset(&ag_res, 0, sizeof(ag_res)); + + /* Format the response */ + if (index == 0) { + ag_res.ok_flag = BTA_AG_OK_DONE; + } else { + BTIF_TRACE_EVENT( + "clcc_response: [%d] dir %d state %d mode %d number = %s type = %d", + index, dir, state, mode, number, type); + int xx = snprintf(ag_res.str, sizeof(ag_res.str), "%d,%d,%d,%d,%d", index, + dir, state, mode, mpty); + + if (number) { + size_t rem_bytes = sizeof(ag_res.str) - xx; + if ((type == BTHF_CALL_ADDRTYPE_INTERNATIONAL) && (*number != '+')) + snprintf(&ag_res.str[xx], rem_bytes, ",\"+%s\",%d", number, type); + else + snprintf(&ag_res.str[xx], rem_bytes, ",\"%s\",%d", number, type); + } } + BTA_AgResult(btif_hf_cb[idx].handle, BTA_AG_CLCC_RES, &ag_res); - return BT_STATUS_FAIL; + return BT_STATUS_SUCCESS; + } + + return BT_STATUS_FAIL; } /******************************************************************************* -** -** Function phone_state_change -** -** Description notify of a call state change -** number & type: valid only for incoming & waiting call -** -** Returns bt_status_t -** -*******************************************************************************/ - -static bt_status_t phone_state_change(int num_active, int num_held, bthf_call_state_t call_setup_state, - const char *number, bthf_call_addrtype_t type) -{ - tBTA_AG_RES res = 0xff; - tBTA_AG_RES_DATA ag_res; - bt_status_t status = BT_STATUS_SUCCESS; - bool activeCallUpdated = false; - int idx, i; - - /* hf_idx is index of connected HS that sent ATA/BLDN, - otherwise index of latest connected HS */ - if (hf_idx != BTIF_HF_INVALID_IDX) - idx = hf_idx; - else - idx = btif_hf_latest_connected_idx(); - - BTIF_TRACE_DEBUG("phone_state_change: idx = %d", idx); - - /* Check if SLC is connected */ - if (btif_hf_check_if_slc_connected() != BT_STATUS_SUCCESS) - return BT_STATUS_NOT_READY; - - BTIF_TRACE_DEBUG("phone_state_change: num_active=%d [prev: %d] num_held=%d[prev: %d]" - " call_setup=%s [prev: %s]", num_active, btif_hf_cb[idx].num_active, - num_held, btif_hf_cb[idx].num_held, dump_hf_call_state(call_setup_state), - dump_hf_call_state(btif_hf_cb[idx].call_setup_state)); - - /* if all indicators are 0, send end call and return */ - if (num_active == 0 && num_held == 0 && call_setup_state == BTHF_CALL_STATE_IDLE) - { - BTIF_TRACE_DEBUG("%s: Phone on hook", __func__); - - /* record call termination timestamp if there was an active/held call or - callsetup state > BTHF_CALL_STATE_IDLE */ - if ((btif_hf_cb[idx].call_setup_state != BTHF_CALL_STATE_IDLE ) || - (btif_hf_cb[idx].num_active) ||(btif_hf_cb[idx].num_held)) - { - BTIF_TRACE_DEBUG("%s: Record call termination timestamp", __func__); - clock_gettime(CLOCK_MONOTONIC, &btif_hf_cb[0].call_end_timestamp); - } - BTA_AgResult (BTA_AG_HANDLE_ALL, BTA_AG_END_CALL_RES, NULL); - hf_idx = BTIF_HF_INVALID_IDX; + * + * Function phone_state_change + * + * Description notify of a call state change + * number & type: valid only for incoming & waiting call + * + * Returns bt_status_t + * + ******************************************************************************/ - /* if held call was present, reset that as well */ - if (btif_hf_cb[idx].num_held) - send_indicator_update(BTA_AG_IND_CALLHELD, 0); +static bt_status_t phone_state_change(int num_active, int num_held, + bthf_call_state_t call_setup_state, + const char* number, + bthf_call_addrtype_t type) { + tBTA_AG_RES res = 0xff; + tBTA_AG_RES_DATA ag_res; + bt_status_t status = BT_STATUS_SUCCESS; + bool activeCallUpdated = false; + int idx, i; + + /* hf_idx is index of connected HS that sent ATA/BLDN, + otherwise index of latest connected HS */ + if (hf_idx != BTIF_HF_INVALID_IDX) + idx = hf_idx; + else + idx = btif_hf_latest_connected_idx(); + + BTIF_TRACE_DEBUG("phone_state_change: idx = %d", idx); + + /* Check if SLC is connected */ + if (btif_hf_check_if_slc_connected() != BT_STATUS_SUCCESS) + return BT_STATUS_NOT_READY; - goto update_call_states; + BTIF_TRACE_DEBUG( + "phone_state_change: num_active=%d [prev: %d] num_held=%d[prev: %d]" + " call_setup=%s [prev: %s]", + num_active, btif_hf_cb[idx].num_active, num_held, + btif_hf_cb[idx].num_held, dump_hf_call_state(call_setup_state), + dump_hf_call_state(btif_hf_cb[idx].call_setup_state)); + + /* if all indicators are 0, send end call and return */ + if (num_active == 0 && num_held == 0 && + call_setup_state == BTHF_CALL_STATE_IDLE) { + BTIF_TRACE_DEBUG("%s: Phone on hook", __func__); + + /* record call termination timestamp if there was an active/held call or + callsetup state > BTHF_CALL_STATE_IDLE */ + if ((btif_hf_cb[idx].call_setup_state != BTHF_CALL_STATE_IDLE) || + (btif_hf_cb[idx].num_active) || (btif_hf_cb[idx].num_held)) { + BTIF_TRACE_DEBUG("%s: Record call termination timestamp", __func__); + clock_gettime(CLOCK_MONOTONIC, &btif_hf_cb[0].call_end_timestamp); } + BTA_AgResult(BTA_AG_HANDLE_ALL, BTA_AG_END_CALL_RES, NULL); + hf_idx = BTIF_HF_INVALID_IDX; + + /* if held call was present, reset that as well */ + if (btif_hf_cb[idx].num_held) send_indicator_update(BTA_AG_IND_CALLHELD, 0); + + goto update_call_states; + } + + /* active state can change when: + ** 1. an outgoing/incoming call was answered + ** 2. an held was resumed + ** 3. without callsetup notifications, call became active + ** (3) can happen if call is active and a headset connects to us + ** + ** In the case of (3), we will have to notify the stack of an active + ** call, instead of sending an indicator update. This will also + ** force the SCO to be setup. Handle this special case here prior to + ** call setup handling + */ + if (((num_active + num_held) > 0) && (btif_hf_cb[idx].num_active == 0) && + (btif_hf_cb[idx].num_held == 0) && + (btif_hf_cb[idx].call_setup_state == BTHF_CALL_STATE_IDLE)) { + BTIF_TRACE_DEBUG( + "%s: Active/Held call notification received without call setup update", + __func__); - /* active state can change when: - ** 1. an outgoing/incoming call was answered - ** 2. an held was resumed - ** 3. without callsetup notifications, call became active - ** (3) can happen if call is active and a headset connects to us - ** - ** In the case of (3), we will have to notify the stack of an active - ** call, instead of sending an indicator update. This will also - ** force the SCO to be setup. Handle this special case here prior to - ** call setup handling + memset(&ag_res, 0, sizeof(tBTA_AG_RES_DATA)); + ag_res.audio_handle = BTA_AG_HANDLE_SCO_NO_CHANGE; + /* Addition call setup with the Active call + ** CIND response should have been updated. + ** just open SCO conenction. */ - if ( ((num_active + num_held) > 0) && (btif_hf_cb[idx].num_active == 0) && (btif_hf_cb[idx].num_held == 0) && - (btif_hf_cb[idx].call_setup_state == BTHF_CALL_STATE_IDLE) ) - { - BTIF_TRACE_DEBUG("%s: Active/Held call notification received without call setup update", - __func__); - - memset(&ag_res, 0, sizeof(tBTA_AG_RES_DATA)); - ag_res.audio_handle = BTA_AG_HANDLE_SCO_NO_CHANGE; - /* Addition call setup with the Active call - ** CIND response should have been updated. - ** just open SCO conenction. - */ - if (call_setup_state != BTHF_CALL_STATE_IDLE) - res = BTA_AG_MULTI_CALL_RES; - else - res = BTA_AG_OUT_CALL_CONN_RES; - BTA_AgResult(BTA_AG_HANDLE_ALL, res, &ag_res); - activeCallUpdated = true; - } + if (call_setup_state != BTHF_CALL_STATE_IDLE) + res = BTA_AG_MULTI_CALL_RES; + else + res = BTA_AG_OUT_CALL_CONN_RES; + BTA_AgResult(BTA_AG_HANDLE_ALL, res, &ag_res); + activeCallUpdated = true; + } + + /* Ringing call changed? */ + if (call_setup_state != btif_hf_cb[idx].call_setup_state) { + BTIF_TRACE_DEBUG("%s: Call setup states changed. old: %s new: %s", __func__, + dump_hf_call_state(btif_hf_cb[idx].call_setup_state), + dump_hf_call_state(call_setup_state)); + memset(&ag_res, 0, sizeof(tBTA_AG_RES_DATA)); - /* Ringing call changed? */ - if (call_setup_state != btif_hf_cb[idx].call_setup_state) - { - BTIF_TRACE_DEBUG("%s: Call setup states changed. old: %s new: %s", - __func__, dump_hf_call_state(btif_hf_cb[idx].call_setup_state), - dump_hf_call_state(call_setup_state)); - memset(&ag_res, 0, sizeof(tBTA_AG_RES_DATA)); - - switch (call_setup_state) - { - case BTHF_CALL_STATE_IDLE: - { - switch (btif_hf_cb[idx].call_setup_state) - { - case BTHF_CALL_STATE_INCOMING: - if (num_active > btif_hf_cb[idx].num_active) - { - res = BTA_AG_IN_CALL_CONN_RES; - ag_res.audio_handle = btif_hf_cb[idx].handle; - } - else if (num_held > btif_hf_cb[idx].num_held) - res = BTA_AG_IN_CALL_HELD_RES; - else - res = BTA_AG_CALL_CANCEL_RES; - break; - case BTHF_CALL_STATE_DIALING: - case BTHF_CALL_STATE_ALERTING: - if (num_active > btif_hf_cb[idx].num_active) - { - ag_res.audio_handle = BTA_AG_HANDLE_SCO_NO_CHANGE; - res = BTA_AG_OUT_CALL_CONN_RES; - } - else - res = BTA_AG_CALL_CANCEL_RES; - break; - default: - BTIF_TRACE_ERROR("%s: Incorrect Call setup state transition", __func__); - status = BT_STATUS_PARM_INVALID; - break; - } - } break; - - case BTHF_CALL_STATE_INCOMING: - if (num_active || num_held) - res = BTA_AG_CALL_WAIT_RES; - else - res = BTA_AG_IN_CALL_RES; - if (number) - { - int xx = 0; - if ((type == BTHF_CALL_ADDRTYPE_INTERNATIONAL) && (*number != '+')) - xx = snprintf (ag_res.str, sizeof(ag_res.str), "\"+%s\"", number); - else - xx = snprintf (ag_res.str, sizeof(ag_res.str), "\"%s\"", number); - ag_res.num = type; - - if (res == BTA_AG_CALL_WAIT_RES) - snprintf(&ag_res.str[xx], sizeof(ag_res.str) - xx, ",%d", type); - } - break; - case BTHF_CALL_STATE_DIALING: - if (!(num_active + num_held)) - ag_res.audio_handle = btif_hf_cb[idx].handle; - res = BTA_AG_OUT_CALL_ORIG_RES; - break; - case BTHF_CALL_STATE_ALERTING: - /* if we went from idle->alert, force SCO setup here. dialing usually triggers it */ - if ((btif_hf_cb[idx].call_setup_state == BTHF_CALL_STATE_IDLE) && - !(num_active + num_held)) - ag_res.audio_handle = btif_hf_cb[idx].handle; - res = BTA_AG_OUT_CALL_ALERT_RES; - break; - default: - BTIF_TRACE_ERROR("%s: Incorrect new ringing call state", __func__); - status = BT_STATUS_PARM_INVALID; - break; - } - BTIF_TRACE_DEBUG("%s: Call setup state changed. res=%d, audio_handle=%d", __func__, res, ag_res.audio_handle); - - if (res) - BTA_AgResult(BTA_AG_HANDLE_ALL, res, &ag_res); - - /* if call setup is idle, we have already updated call indicator, jump out */ - if (call_setup_state == BTHF_CALL_STATE_IDLE) - { - /* check & update callheld */ - if ((num_held > 0) && (num_active > 0)) - send_indicator_update(BTA_AG_IND_CALLHELD, 1); - goto update_call_states; + switch (call_setup_state) { + case BTHF_CALL_STATE_IDLE: { + switch (btif_hf_cb[idx].call_setup_state) { + case BTHF_CALL_STATE_INCOMING: + if (num_active > btif_hf_cb[idx].num_active) { + res = BTA_AG_IN_CALL_CONN_RES; + ag_res.audio_handle = btif_hf_cb[idx].handle; + } else if (num_held > btif_hf_cb[idx].num_held) + res = BTA_AG_IN_CALL_HELD_RES; + else + res = BTA_AG_CALL_CANCEL_RES; + break; + case BTHF_CALL_STATE_DIALING: + case BTHF_CALL_STATE_ALERTING: + if (num_active > btif_hf_cb[idx].num_active) { + ag_res.audio_handle = BTA_AG_HANDLE_SCO_NO_CHANGE; + res = BTA_AG_OUT_CALL_CONN_RES; + } else + res = BTA_AG_CALL_CANCEL_RES; + break; + default: + BTIF_TRACE_ERROR("%s: Incorrect Call setup state transition", + __func__); + status = BT_STATUS_PARM_INVALID; + break; } - } - - memset(&ag_res, 0, sizeof(tBTA_AG_RES_DATA)); + } break; - /* per the errata 2043, call=1 implies atleast one call is in progress (active/held) - ** https://www.bluetooth.org/errata/errata_view.cfm?errata_id=2043 - ** Handle call indicator change - **/ - if (!activeCallUpdated && ((num_active + num_held) != - (btif_hf_cb[idx].num_active + btif_hf_cb[idx].num_held)) ) - { - BTIF_TRACE_DEBUG("%s: Active call states changed. old: %d new: %d", __func__, btif_hf_cb[idx].num_active, num_active); - send_indicator_update(BTA_AG_IND_CALL, ((num_active + num_held) > 0) ? 1 : 0); + case BTHF_CALL_STATE_INCOMING: + if (num_active || num_held) + res = BTA_AG_CALL_WAIT_RES; + else + res = BTA_AG_IN_CALL_RES; + if (number) { + int xx = 0; + if ((type == BTHF_CALL_ADDRTYPE_INTERNATIONAL) && (*number != '+')) + xx = snprintf(ag_res.str, sizeof(ag_res.str), "\"+%s\"", number); + else + xx = snprintf(ag_res.str, sizeof(ag_res.str), "\"%s\"", number); + ag_res.num = type; + + if (res == BTA_AG_CALL_WAIT_RES) + snprintf(&ag_res.str[xx], sizeof(ag_res.str) - xx, ",%d", type); + } + break; + case BTHF_CALL_STATE_DIALING: + if (!(num_active + num_held)) + ag_res.audio_handle = btif_hf_cb[idx].handle; + res = BTA_AG_OUT_CALL_ORIG_RES; + break; + case BTHF_CALL_STATE_ALERTING: + /* if we went from idle->alert, force SCO setup here. dialing usually + * triggers it */ + if ((btif_hf_cb[idx].call_setup_state == BTHF_CALL_STATE_IDLE) && + !(num_active + num_held)) + ag_res.audio_handle = btif_hf_cb[idx].handle; + res = BTA_AG_OUT_CALL_ALERT_RES; + break; + default: + BTIF_TRACE_ERROR("%s: Incorrect new ringing call state", __func__); + status = BT_STATUS_PARM_INVALID; + break; } + BTIF_TRACE_DEBUG("%s: Call setup state changed. res=%d, audio_handle=%d", + __func__, res, ag_res.audio_handle); - /* Held Changed? */ - if (num_held != btif_hf_cb[idx].num_held || - ((num_active == 0) && ((num_held + btif_hf_cb[idx].num_held) > 1))) - { - BTIF_TRACE_DEBUG("%s: Held call states changed. old: %d new: %d", - __func__, btif_hf_cb[idx].num_held, num_held); - send_indicator_update(BTA_AG_IND_CALLHELD, ((num_held == 0) ? 0 : ((num_active == 0) ? 2 : 1))); - } + if (res) BTA_AgResult(BTA_AG_HANDLE_ALL, res, &ag_res); - /* Calls Swapped? */ - if ( (call_setup_state == btif_hf_cb[idx].call_setup_state) && - (num_active && num_held) && - (num_active == btif_hf_cb[idx].num_active) && - (num_held == btif_hf_cb[idx].num_held) ) - { - BTIF_TRACE_DEBUG("%s: Calls swapped", __func__); + /* if call setup is idle, we have already updated call indicator, jump out + */ + if (call_setup_state == BTHF_CALL_STATE_IDLE) { + /* check & update callheld */ + if ((num_held > 0) && (num_active > 0)) send_indicator_update(BTA_AG_IND_CALLHELD, 1); + goto update_call_states; } + } + + memset(&ag_res, 0, sizeof(tBTA_AG_RES_DATA)); + + /* per the errata 2043, call=1 implies atleast one call is in progress + *(active/held) + ** https://www.bluetooth.org/errata/errata_view.cfm?errata_id=2043 + ** Handle call indicator change + **/ + if (!activeCallUpdated && + ((num_active + num_held) != + (btif_hf_cb[idx].num_active + btif_hf_cb[idx].num_held))) { + BTIF_TRACE_DEBUG("%s: Active call states changed. old: %d new: %d", + __func__, btif_hf_cb[idx].num_active, num_active); + send_indicator_update(BTA_AG_IND_CALL, + ((num_active + num_held) > 0) ? 1 : 0); + } + + /* Held Changed? */ + if (num_held != btif_hf_cb[idx].num_held || + ((num_active == 0) && ((num_held + btif_hf_cb[idx].num_held) > 1))) { + BTIF_TRACE_DEBUG("%s: Held call states changed. old: %d new: %d", __func__, + btif_hf_cb[idx].num_held, num_held); + send_indicator_update(BTA_AG_IND_CALLHELD, + ((num_held == 0) ? 0 : ((num_active == 0) ? 2 : 1))); + } + + /* Calls Swapped? */ + if ((call_setup_state == btif_hf_cb[idx].call_setup_state) && + (num_active && num_held) && (num_active == btif_hf_cb[idx].num_active) && + (num_held == btif_hf_cb[idx].num_held)) { + BTIF_TRACE_DEBUG("%s: Calls swapped", __func__); + send_indicator_update(BTA_AG_IND_CALLHELD, 1); + } update_call_states: - for (i = 0; i < btif_max_hf_clients; i++) - { - if (btif_hf_cb[i].state == BTHF_CONNECTION_STATE_SLC_CONNECTED) - { - btif_hf_cb[i].num_active = num_active; - btif_hf_cb[i].num_held = num_held; - btif_hf_cb[i].call_setup_state = call_setup_state; - } + for (i = 0; i < btif_max_hf_clients; i++) { + if (btif_hf_cb[i].state == BTHF_CONNECTION_STATE_SLC_CONNECTED) { + btif_hf_cb[i].num_active = num_active; + btif_hf_cb[i].num_held = num_held; + btif_hf_cb[i].call_setup_state = call_setup_state; } - return status; + } + return status; } /******************************************************************************* -** -** Function btif_hf_is_call_idle -** -** Description returns true if no call is in progress -** -** Returns bt_status_t -** -*******************************************************************************/ -bool btif_hf_is_call_idle(void) -{ - if (bt_hf_callbacks == NULL) - return true; - - for (int i = 0; i < btif_max_hf_clients; ++i) - { - if ((btif_hf_cb[i].call_setup_state != BTHF_CALL_STATE_IDLE) - || ((btif_hf_cb[i].num_held + btif_hf_cb[i].num_active) > 0)) - return false; - } + * + * Function btif_hf_is_call_idle + * + * Description returns true if no call is in progress + * + * Returns bt_status_t + * + ******************************************************************************/ +bool btif_hf_is_call_idle(void) { + if (bt_hf_callbacks == NULL) return true; - return true; + for (int i = 0; i < btif_max_hf_clients; ++i) { + if ((btif_hf_cb[i].call_setup_state != BTHF_CALL_STATE_IDLE) || + ((btif_hf_cb[i].num_held + btif_hf_cb[i].num_active) > 0)) + return false; + } + + return true; } /******************************************************************************* -** -** Function btif_hf_call_terminated_recently -** -** Description Checks if a call has been terminated -** -** Returns bt_status_t -** -*******************************************************************************/ -bool btif_hf_call_terminated_recently() -{ - struct timespec now; - - clock_gettime(CLOCK_MONOTONIC, &now); - if (now.tv_sec < btif_hf_cb[0].call_end_timestamp.tv_sec + - BTIF_HF_CALL_END_TIMEOUT) - { - return true; - } - else - { - btif_hf_cb[0].call_end_timestamp.tv_sec = 0; - return false; - } + * + * Function btif_hf_call_terminated_recently + * + * Description Checks if a call has been terminated + * + * Returns bt_status_t + * + ******************************************************************************/ +bool btif_hf_call_terminated_recently() { + struct timespec now; + + clock_gettime(CLOCK_MONOTONIC, &now); + if (now.tv_sec < + btif_hf_cb[0].call_end_timestamp.tv_sec + BTIF_HF_CALL_END_TIMEOUT) { + return true; + } else { + btif_hf_cb[0].call_end_timestamp.tv_sec = 0; + return false; + } } /******************************************************************************* -** -** Function cleanup -** -** Description Closes the HF interface -** -** Returns bt_status_t -** -*******************************************************************************/ -static void cleanup( void ) -{ - BTIF_TRACE_EVENT("%s", __func__); - - if (bt_hf_callbacks) - { - btif_disable_service(BTA_HFP_SERVICE_ID); - bt_hf_callbacks = NULL; - } + * + * Function cleanup + * + * Description Closes the HF interface + * + * Returns bt_status_t + * + ******************************************************************************/ +static void cleanup(void) { + BTIF_TRACE_EVENT("%s", __func__); + + if (bt_hf_callbacks) { + btif_disable_service(BTA_HFP_SERVICE_ID); + bt_hf_callbacks = NULL; + } } /******************************************************************************* -** -** Function configure_wbs -** -** Description set to over-ride the current WBS configuration. -** It will not send codec setting cmd to the controller now. -** It just change the configure. -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t configure_wbs( bt_bdaddr_t *bd_addr , bthf_wbs_config_t config ) -{ - CHECK_BTHF_INIT(); - - int idx = btif_hf_idx_by_bdaddr(bd_addr); - - if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) - { - BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); - return BT_STATUS_FAIL; - } + * + * Function configure_wbs + * + * Description set to over-ride the current WBS configuration. + * It will not send codec setting cmd to the controller now. + * It just change the configure. + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t configure_wbs(bt_bdaddr_t* bd_addr, + bthf_wbs_config_t config) { + CHECK_BTHF_INIT(); - BTIF_TRACE_EVENT("%s config is %d", __func__,config); - if (config == BTHF_WBS_YES) - BTA_AgSetCodec(btif_hf_cb[idx].handle,BTA_AG_CODEC_MSBC); - else if(config == BTHF_WBS_NO) - BTA_AgSetCodec(btif_hf_cb[idx].handle,BTA_AG_CODEC_CVSD); - else - BTA_AgSetCodec(btif_hf_cb[idx].handle,BTA_AG_CODEC_NONE); + int idx = btif_hf_idx_by_bdaddr(bd_addr); - return BT_STATUS_SUCCESS; + if ((idx < 0) || (idx >= BTIF_HF_NUM_CB)) { + BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx); + return BT_STATUS_FAIL; + } + + BTIF_TRACE_EVENT("%s config is %d", __func__, config); + if (config == BTHF_WBS_YES) + BTA_AgSetCodec(btif_hf_cb[idx].handle, BTA_AG_CODEC_MSBC); + else if (config == BTHF_WBS_NO) + BTA_AgSetCodec(btif_hf_cb[idx].handle, BTA_AG_CODEC_CVSD); + else + BTA_AgSetCodec(btif_hf_cb[idx].handle, BTA_AG_CODEC_NONE); + + return BT_STATUS_SUCCESS; } static const bthf_interface_t bthfInterface = { @@ -1635,51 +1541,45 @@ static const bthf_interface_t bthfInterface = { }; /******************************************************************************* -** -** Function btif_hf_execute_service -** -** Description Initializes/Shuts down the service -** -** Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_hf_execute_service(bool b_enable) -{ - const char * p_service_names[] = BTIF_HF_SERVICE_NAMES; - int i; - if (b_enable) - { - /* Enable and register with BTA-AG */ - BTA_AgEnable (BTA_AG_PARSE, bte_hf_evt); - for (i = 0; i < btif_max_hf_clients; i++) - { - BTA_AgRegister(BTIF_HF_SERVICES, BTIF_HF_SECURITY, - BTIF_HF_FEATURES, p_service_names, bthf_hf_id[i]); - } - } - else { - /* De-register AG */ - for (i = 0; i < btif_max_hf_clients; i++) - { - BTA_AgDeregister(btif_hf_cb[i].handle); - } - /* Disable AG */ - BTA_AgDisable(); - } - return BT_STATUS_SUCCESS; + * + * Function btif_hf_execute_service + * + * Description Initializes/Shuts down the service + * + * Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_hf_execute_service(bool b_enable) { + const char* p_service_names[] = BTIF_HF_SERVICE_NAMES; + int i; + if (b_enable) { + /* Enable and register with BTA-AG */ + BTA_AgEnable(BTA_AG_PARSE, bte_hf_evt); + for (i = 0; i < btif_max_hf_clients; i++) { + BTA_AgRegister(BTIF_HF_SERVICES, BTIF_HF_SECURITY, BTIF_HF_FEATURES, + p_service_names, bthf_hf_id[i]); + } + } else { + /* De-register AG */ + for (i = 0; i < btif_max_hf_clients; i++) { + BTA_AgDeregister(btif_hf_cb[i].handle); + } + /* Disable AG */ + BTA_AgDisable(); + } + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_hf_get_interface -** -** Description Get the hf callback interface -** -** Returns bthf_interface_t -** -*******************************************************************************/ -const bthf_interface_t *btif_hf_get_interface() -{ - BTIF_TRACE_EVENT("%s", __func__); - return &bthfInterface; + * + * Function btif_hf_get_interface + * + * Description Get the hf callback interface + * + * Returns bthf_interface_t + * + ******************************************************************************/ +const bthf_interface_t* btif_hf_get_interface() { + BTIF_TRACE_EVENT("%s", __func__); + return &bthfInterface; } diff --git a/btif/src/btif_hf_client.cc b/btif/src/btif_hf_client.cc index f35a16100..b4d42accf 100644 --- a/btif/src/btif_hf_client.cc +++ b/btif/src/btif_hf_client.cc @@ -43,627 +43,596 @@ #include "osi/include/properties.h" /************************************************************************************ -** Constants & Macros -************************************************************************************/ + * Constants & Macros + ***********************************************************************************/ #ifndef BTIF_HF_CLIENT_SERVICE_NAME #define BTIF_HF_CLIENT_SERVICE_NAME ("Handsfree") #endif #ifndef BTIF_HF_CLIENT_SECURITY -#define BTIF_HF_CLIENT_SECURITY (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT) +#define BTIF_HF_CLIENT_SECURITY (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT) #endif #ifndef BTIF_HF_CLIENT_FEATURES -#define BTIF_HF_CLIENT_FEATURES ( BTA_HF_CLIENT_FEAT_ECNR | \ - BTA_HF_CLIENT_FEAT_3WAY | \ - BTA_HF_CLIENT_FEAT_CLI | \ - BTA_HF_CLIENT_FEAT_VREC | \ - BTA_HF_CLIENT_FEAT_VOL | \ - BTA_HF_CLIENT_FEAT_ECS | \ - BTA_HF_CLIENT_FEAT_ECC | \ - BTA_HF_CLIENT_FEAT_CODEC) +#define BTIF_HF_CLIENT_FEATURES \ + (BTA_HF_CLIENT_FEAT_ECNR | BTA_HF_CLIENT_FEAT_3WAY | \ + BTA_HF_CLIENT_FEAT_CLI | BTA_HF_CLIENT_FEAT_VREC | BTA_HF_CLIENT_FEAT_VOL | \ + BTA_HF_CLIENT_FEAT_ECS | BTA_HF_CLIENT_FEAT_ECC | BTA_HF_CLIENT_FEAT_CODEC) #endif /************************************************************************************ -** Local type definitions -************************************************************************************/ + * Local type definitions + ***********************************************************************************/ /************************************************************************************ -** Static variables -************************************************************************************/ -static bthf_client_callbacks_t *bt_hf_client_callbacks = NULL; + * Static variables + ***********************************************************************************/ +static bthf_client_callbacks_t* bt_hf_client_callbacks = NULL; static uint32_t btif_hf_client_features = 0; char btif_hf_client_version[PROPERTY_VALUE_MAX]; -#define CHECK_BTHF_CLIENT_INIT() if (bt_hf_client_callbacks == NULL)\ - {\ - BTIF_TRACE_WARNING("BTHF CLIENT: %s: not initialized", __func__);\ - return BT_STATUS_NOT_READY;\ - }\ - else\ - {\ - BTIF_TRACE_EVENT("BTHF CLIENT: %s", __func__);\ - } - -#define CHECK_BTHF_CLIENT_SLC_CONNECTED() if (bt_hf_client_callbacks == NULL)\ - {\ - BTIF_TRACE_WARNING("BTHF CLIENT: %s: not initialized", __func__);\ - return BT_STATUS_NOT_READY;\ - }\ - else if (btif_hf_client_cb.state != BTHF_CLIENT_CONNECTION_STATE_SLC_CONNECTED)\ - {\ - BTIF_TRACE_WARNING("BTHF CLIENT: %s: SLC connection not up. state=%s",\ - __func__, \ - dump_hf_conn_state(btif_hf_client_cb.state));\ - return BT_STATUS_NOT_READY;\ - }\ - else\ - {\ - BTIF_TRACE_EVENT("BTHF CLIENT: %s", __func__);\ - } +#define CHECK_BTHF_CLIENT_INIT() \ + if (bt_hf_client_callbacks == NULL) { \ + BTIF_TRACE_WARNING("BTHF CLIENT: %s: not initialized", __func__); \ + return BT_STATUS_NOT_READY; \ + } else { \ + BTIF_TRACE_EVENT("BTHF CLIENT: %s", __func__); \ + } + +#define CHECK_BTHF_CLIENT_SLC_CONNECTED() \ + if (bt_hf_client_callbacks == NULL) { \ + BTIF_TRACE_WARNING("BTHF CLIENT: %s: not initialized", __func__); \ + return BT_STATUS_NOT_READY; \ + } else if (btif_hf_client_cb.state != \ + BTHF_CLIENT_CONNECTION_STATE_SLC_CONNECTED) { \ + BTIF_TRACE_WARNING("BTHF CLIENT: %s: SLC connection not up. state=%s", \ + __func__, dump_hf_conn_state(btif_hf_client_cb.state)); \ + return BT_STATUS_NOT_READY; \ + } else { \ + BTIF_TRACE_EVENT("BTHF CLIENT: %s", __func__); \ + } /* BTIF-HF control block to map bdaddr to BTA handle */ -typedef struct -{ - uint16_t handle; - bt_bdaddr_t connected_bda; - bthf_client_connection_state_t state; - bthf_client_vr_state_t vr_state; - tBTA_HF_CLIENT_PEER_FEAT peer_feat; - tBTA_HF_CLIENT_CHLD_FEAT chld_feat; +typedef struct { + uint16_t handle; + bt_bdaddr_t connected_bda; + bthf_client_connection_state_t state; + bthf_client_vr_state_t vr_state; + tBTA_HF_CLIENT_PEER_FEAT peer_feat; + tBTA_HF_CLIENT_CHLD_FEAT chld_feat; } btif_hf_client_cb_t; static btif_hf_client_cb_t btif_hf_client_cb; /************************************************************************************ -** Static functions -************************************************************************************/ + * Static functions + ***********************************************************************************/ /******************************************************************************* -** -** Function btif_in_hf_client_generic_evt -** -** Description Processes generic events to be sent to JNI that are not triggered from the BTA. -** Always runs in BTIF context -** -** Returns void -** -*******************************************************************************/ -static void btif_in_hf_client_generic_evt(uint16_t event, char *p_param) -{ - UNUSED(p_param); - - BTIF_TRACE_EVENT("%s: event=%d", __func__, event); - switch (event) { - case BTIF_HF_CLIENT_CB_AUDIO_CONNECTING: - { - HAL_CBACK(bt_hf_client_callbacks, audio_state_cb, (bthf_client_audio_state_t)BTHF_AUDIO_STATE_CONNECTING, - &btif_hf_client_cb.connected_bda); - } break; - default: - { - BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __func__, event); - } - break; - } + * + * Function btif_in_hf_client_generic_evt + * + * Description Processes generic events to be sent to JNI that are not + *triggered from the BTA. + * Always runs in BTIF context + * + * Returns void + * + ******************************************************************************/ +static void btif_in_hf_client_generic_evt(uint16_t event, char* p_param) { + UNUSED(p_param); + + BTIF_TRACE_EVENT("%s: event=%d", __func__, event); + switch (event) { + case BTIF_HF_CLIENT_CB_AUDIO_CONNECTING: { + HAL_CBACK(bt_hf_client_callbacks, audio_state_cb, + (bthf_client_audio_state_t)BTHF_AUDIO_STATE_CONNECTING, + &btif_hf_client_cb.connected_bda); + } break; + default: { + BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __func__, event); + } break; + } } /************************************************************************************ -** Externs -************************************************************************************/ + * Externs + ***********************************************************************************/ /************************************************************************************ -** Functions -************************************************************************************/ + * Functions + ***********************************************************************************/ -static void clear_state(void) -{ - memset(&btif_hf_client_cb, 0, sizeof(btif_hf_client_cb_t)); +static void clear_state(void) { + memset(&btif_hf_client_cb, 0, sizeof(btif_hf_client_cb_t)); } -static bool is_connected(bt_bdaddr_t *bd_addr) -{ - if (((btif_hf_client_cb.state == BTHF_CLIENT_CONNECTION_STATE_CONNECTED) || - (btif_hf_client_cb.state == BTHF_CLIENT_CONNECTION_STATE_SLC_CONNECTED))&& - ((bd_addr == NULL) || (bdcmp(bd_addr->address, btif_hf_client_cb.connected_bda.address) == 0))) - return true; - return false; +static bool is_connected(bt_bdaddr_t* bd_addr) { + if (((btif_hf_client_cb.state == BTHF_CLIENT_CONNECTION_STATE_CONNECTED) || + (btif_hf_client_cb.state == + BTHF_CLIENT_CONNECTION_STATE_SLC_CONNECTED)) && + ((bd_addr == NULL) || + (bdcmp(bd_addr->address, btif_hf_client_cb.connected_bda.address) == 0))) + return true; + return false; } /***************************************************************************** -** Section name (Group of functions) -*****************************************************************************/ + * Section name (Group of functions) + ****************************************************************************/ /***************************************************************************** -** -** btif hf api functions (no context switch) -** -*****************************************************************************/ + * + * btif hf api functions (no context switch) + * + ****************************************************************************/ /******************************************************************************* -** -** Function btif_hf_client_init -** -** Description initializes the hf interface -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t init( bthf_client_callbacks_t* callbacks ) -{ - BTIF_TRACE_EVENT("%s", __func__); + * + * Function btif_hf_client_init + * + * Description initializes the hf interface + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t init(bthf_client_callbacks_t* callbacks) { + BTIF_TRACE_EVENT("%s", __func__); - bt_hf_client_callbacks = callbacks; + bt_hf_client_callbacks = callbacks; - btif_enable_service(BTA_HFP_HS_SERVICE_ID); + btif_enable_service(BTA_HFP_HS_SERVICE_ID); - clear_state(); + clear_state(); - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function connect -** -** Description connect to audio gateway -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t connect_int( bt_bdaddr_t *bd_addr, uint16_t uuid ) -{ - if (is_connected(bd_addr)) - return BT_STATUS_BUSY; - - btif_hf_client_cb.state = BTHF_CLIENT_CONNECTION_STATE_CONNECTING; - bdcpy(btif_hf_client_cb.connected_bda.address, bd_addr->address); - - BTA_HfClientOpen(btif_hf_client_cb.handle, btif_hf_client_cb.connected_bda.address, - BTIF_HF_CLIENT_SECURITY); + * + * Function connect + * + * Description connect to audio gateway + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t connect_int(bt_bdaddr_t* bd_addr, uint16_t uuid) { + if (is_connected(bd_addr)) return BT_STATUS_BUSY; - return BT_STATUS_SUCCESS; -} + btif_hf_client_cb.state = BTHF_CLIENT_CONNECTION_STATE_CONNECTING; + bdcpy(btif_hf_client_cb.connected_bda.address, bd_addr->address); -static bt_status_t connect( bt_bdaddr_t *bd_addr ) -{ - BTIF_TRACE_EVENT("HFP Client version is %s", btif_hf_client_version); - CHECK_BTHF_CLIENT_INIT(); - return btif_queue_connect(UUID_SERVCLASS_HF_HANDSFREE, bd_addr, connect_int); + BTA_HfClientOpen(btif_hf_client_cb.handle, + btif_hf_client_cb.connected_bda.address, + BTIF_HF_CLIENT_SECURITY); + return BT_STATUS_SUCCESS; } -/******************************************************************************* -** -** Function disconnect -** -** Description disconnect from audio gateway -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t disconnect( bt_bdaddr_t *bd_addr ) -{ - CHECK_BTHF_CLIENT_INIT(); - - if (is_connected(bd_addr)) - { - BTA_HfClientClose(btif_hf_client_cb.handle); - return BT_STATUS_SUCCESS; - } - - return BT_STATUS_FAIL; +static bt_status_t connect(bt_bdaddr_t* bd_addr) { + BTIF_TRACE_EVENT("HFP Client version is %s", btif_hf_client_version); + CHECK_BTHF_CLIENT_INIT(); + return btif_queue_connect(UUID_SERVCLASS_HF_HANDSFREE, bd_addr, connect_int); } /******************************************************************************* -** -** Function connect_audio -** -** Description create an audio connection -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t connect_audio( bt_bdaddr_t *bd_addr ) -{ - CHECK_BTHF_CLIENT_SLC_CONNECTED(); - - if (is_connected(bd_addr)) - { - if ((BTIF_HF_CLIENT_FEATURES & BTA_HF_CLIENT_FEAT_CODEC) && - (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_CODEC)) - { - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_BCC, 0, 0, NULL); - } - else - { - BTA_HfClientAudioOpen(btif_hf_client_cb.handle); - } + * + * Function disconnect + * + * Description disconnect from audio gateway + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t disconnect(bt_bdaddr_t* bd_addr) { + CHECK_BTHF_CLIENT_INIT(); - /* Inform the application that the audio connection has been initiated successfully */ - btif_transfer_context(btif_in_hf_client_generic_evt, BTIF_HF_CLIENT_CB_AUDIO_CONNECTING, - (char *)bd_addr, sizeof(bt_bdaddr_t), NULL); - return BT_STATUS_SUCCESS; - } + if (is_connected(bd_addr)) { + BTA_HfClientClose(btif_hf_client_cb.handle); + return BT_STATUS_SUCCESS; + } - return BT_STATUS_FAIL; + return BT_STATUS_FAIL; } /******************************************************************************* -** -** Function disconnect_audio -** -** Description close the audio connection -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t disconnect_audio( bt_bdaddr_t *bd_addr ) -{ - CHECK_BTHF_CLIENT_SLC_CONNECTED(); - - if (is_connected(bd_addr)) - { - BTA_HfClientAudioClose(btif_hf_client_cb.handle); - return BT_STATUS_SUCCESS; + * + * Function connect_audio + * + * Description create an audio connection + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t connect_audio(bt_bdaddr_t* bd_addr) { + CHECK_BTHF_CLIENT_SLC_CONNECTED(); + + if (is_connected(bd_addr)) { + if ((BTIF_HF_CLIENT_FEATURES & BTA_HF_CLIENT_FEAT_CODEC) && + (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_CODEC)) { + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_BCC, 0, + 0, NULL); + } else { + BTA_HfClientAudioOpen(btif_hf_client_cb.handle); } - return BT_STATUS_FAIL; + /* Inform the application that the audio connection has been initiated + * successfully */ + btif_transfer_context(btif_in_hf_client_generic_evt, + BTIF_HF_CLIENT_CB_AUDIO_CONNECTING, (char*)bd_addr, + sizeof(bt_bdaddr_t), NULL); + return BT_STATUS_SUCCESS; + } + + return BT_STATUS_FAIL; } /******************************************************************************* -** -** Function start_voice_recognition -** -** Description start voice recognition -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t start_voice_recognition() -{ - CHECK_BTHF_CLIENT_SLC_CONNECTED(); - - if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_FEAT_VREC) - { - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_BVRA, 1, 0, NULL); - - return BT_STATUS_SUCCESS; - } + * + * Function disconnect_audio + * + * Description close the audio connection + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t disconnect_audio(bt_bdaddr_t* bd_addr) { + CHECK_BTHF_CLIENT_SLC_CONNECTED(); - return BT_STATUS_UNSUPPORTED; + if (is_connected(bd_addr)) { + BTA_HfClientAudioClose(btif_hf_client_cb.handle); + return BT_STATUS_SUCCESS; + } + + return BT_STATUS_FAIL; } /******************************************************************************* -** -** Function stop_voice_recognition -** -** Description stop voice recognition -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t stop_voice_recognition() -{ - CHECK_BTHF_CLIENT_SLC_CONNECTED(); - - if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_FEAT_VREC) - { - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_BVRA, 0, 0, NULL); - - return BT_STATUS_SUCCESS; - } + * + * Function start_voice_recognition + * + * Description start voice recognition + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t start_voice_recognition() { + CHECK_BTHF_CLIENT_SLC_CONNECTED(); + + if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_FEAT_VREC) { + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_BVRA, 1, + 0, NULL); - return BT_STATUS_UNSUPPORTED; + return BT_STATUS_SUCCESS; + } + + return BT_STATUS_UNSUPPORTED; } /******************************************************************************* -** -** Function volume_control -** -** Description volume control -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t volume_control(bthf_client_volume_type_t type, int volume) -{ - CHECK_BTHF_CLIENT_SLC_CONNECTED(); - - switch (type) - { - case BTHF_CLIENT_VOLUME_TYPE_SPK: - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_VGS, volume, 0, NULL); - break; - case BTHF_CLIENT_VOLUME_TYPE_MIC: - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_VGM, volume, 0, NULL); - break; - default: - return BT_STATUS_UNSUPPORTED; - } + * + * Function stop_voice_recognition + * + * Description stop voice recognition + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t stop_voice_recognition() { + CHECK_BTHF_CLIENT_SLC_CONNECTED(); + + if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_FEAT_VREC) { + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_BVRA, 0, + 0, NULL); return BT_STATUS_SUCCESS; + } + + return BT_STATUS_UNSUPPORTED; } /******************************************************************************* -** -** Function dial -** -** Description place a call -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t dial(const char *number) -{ - CHECK_BTHF_CLIENT_SLC_CONNECTED(); - - if (number) - { - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_ATD, 0, 0, number); - } - else - { - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_BLDN, 0, 0, NULL); - } + * + * Function volume_control + * + * Description volume control + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t volume_control(bthf_client_volume_type_t type, int volume) { + CHECK_BTHF_CLIENT_SLC_CONNECTED(); + + switch (type) { + case BTHF_CLIENT_VOLUME_TYPE_SPK: + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_VGS, + volume, 0, NULL); + break; + case BTHF_CLIENT_VOLUME_TYPE_MIC: + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_VGM, + volume, 0, NULL); + break; + default: + return BT_STATUS_UNSUPPORTED; + } - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function dial_memory -** -** Description place a call with number specified by location (speed dial) -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t dial_memory(int location) -{ - CHECK_BTHF_CLIENT_SLC_CONNECTED(); - - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_ATD, location, 0, NULL); + * + * Function dial + * + * Description place a call + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t dial(const char* number) { + CHECK_BTHF_CLIENT_SLC_CONNECTED(); + + if (number) { + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_ATD, 0, 0, + number); + } else { + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_BLDN, 0, + 0, NULL); + } + + return BT_STATUS_SUCCESS; +} - return BT_STATUS_SUCCESS; +/******************************************************************************* + * + * Function dial_memory + * + * Description place a call with number specified by location (speed dial) + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t dial_memory(int location) { + CHECK_BTHF_CLIENT_SLC_CONNECTED(); + + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_ATD, + location, 0, NULL); + + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function handle_call_action -** -** Description handle specified call related action -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t handle_call_action(bthf_client_call_action_t action, int idx) -{ - CHECK_BTHF_CLIENT_SLC_CONNECTED(); - - switch (action) - { + * + * Function handle_call_action + * + * Description handle specified call related action + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t handle_call_action(bthf_client_call_action_t action, + int idx) { + CHECK_BTHF_CLIENT_SLC_CONNECTED(); + + switch (action) { case BTHF_CLIENT_CALL_ACTION_CHLD_0: - if (btif_hf_client_cb.chld_feat & BTA_HF_CLIENT_CHLD_REL) - { - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CHLD, 0, 0, NULL); - break; - } - return BT_STATUS_UNSUPPORTED; + if (btif_hf_client_cb.chld_feat & BTA_HF_CLIENT_CHLD_REL) { + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CHLD, + 0, 0, NULL); + break; + } + return BT_STATUS_UNSUPPORTED; case BTHF_CLIENT_CALL_ACTION_CHLD_1: - // CHLD 1 is mandatory for 3 way calling - if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_FEAT_3WAY) - { - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CHLD, 1, 0, NULL); - break; - } - return BT_STATUS_UNSUPPORTED; + // CHLD 1 is mandatory for 3 way calling + if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_FEAT_3WAY) { + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CHLD, + 1, 0, NULL); + break; + } + return BT_STATUS_UNSUPPORTED; case BTHF_CLIENT_CALL_ACTION_CHLD_2: - // CHLD 2 is mandatory for 3 way calling - if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_FEAT_3WAY) - { - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CHLD, 2, 0, NULL); - break; - } - return BT_STATUS_UNSUPPORTED; + // CHLD 2 is mandatory for 3 way calling + if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_FEAT_3WAY) { + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CHLD, + 2, 0, NULL); + break; + } + return BT_STATUS_UNSUPPORTED; case BTHF_CLIENT_CALL_ACTION_CHLD_3: - if (btif_hf_client_cb.chld_feat & BTA_HF_CLIENT_CHLD_MERGE) - { - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CHLD, 3, 0, NULL); - break; - } - return BT_STATUS_UNSUPPORTED; + if (btif_hf_client_cb.chld_feat & BTA_HF_CLIENT_CHLD_MERGE) { + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CHLD, + 3, 0, NULL); + break; + } + return BT_STATUS_UNSUPPORTED; case BTHF_CLIENT_CALL_ACTION_CHLD_4: - if (btif_hf_client_cb.chld_feat & BTA_HF_CLIENT_CHLD_MERGE_DETACH) - { - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CHLD, 4, 0, NULL); - break; - } - return BT_STATUS_UNSUPPORTED; + if (btif_hf_client_cb.chld_feat & BTA_HF_CLIENT_CHLD_MERGE_DETACH) { + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CHLD, + 4, 0, NULL); + break; + } + return BT_STATUS_UNSUPPORTED; case BTHF_CLIENT_CALL_ACTION_CHLD_1x: - if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_ECC) - { - if (idx < 1) - { - return BT_STATUS_FAIL; - } - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CHLD, 1, idx, NULL); - break; + if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_ECC) { + if (idx < 1) { + return BT_STATUS_FAIL; } - return BT_STATUS_UNSUPPORTED; + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CHLD, + 1, idx, NULL); + break; + } + return BT_STATUS_UNSUPPORTED; case BTHF_CLIENT_CALL_ACTION_CHLD_2x: - if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_ECC) - { - if (idx < 1) - { - return BT_STATUS_FAIL; - } - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CHLD, 2, idx, NULL); - break; + if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_ECC) { + if (idx < 1) { + return BT_STATUS_FAIL; } - return BT_STATUS_UNSUPPORTED; - case BTHF_CLIENT_CALL_ACTION_ATA: - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_ATA, 0, 0, NULL); + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CHLD, + 2, idx, NULL); break; + } + return BT_STATUS_UNSUPPORTED; + case BTHF_CLIENT_CALL_ACTION_ATA: + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_ATA, 0, + 0, NULL); + break; case BTHF_CLIENT_CALL_ACTION_CHUP: - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CHUP, 0, 0, NULL); - break; + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CHUP, 0, + 0, NULL); + break; case BTHF_CLIENT_CALL_ACTION_BTRH_0: - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_BTRH, 0, 0, NULL); - break; + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_BTRH, 0, + 0, NULL); + break; case BTHF_CLIENT_CALL_ACTION_BTRH_1: - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_BTRH, 1, 0, NULL); - break; + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_BTRH, 1, + 0, NULL); + break; case BTHF_CLIENT_CALL_ACTION_BTRH_2: - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_BTRH, 2, 0, NULL); - break; + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_BTRH, 2, + 0, NULL); + break; default: - return BT_STATUS_FAIL; - - } + return BT_STATUS_FAIL; + } - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function query_current_calls -** -** Description query list of current calls -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t query_current_calls(void) -{ - CHECK_BTHF_CLIENT_SLC_CONNECTED(); - - if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_ECS) - { - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CLCC, 0, 0, NULL); - - return BT_STATUS_SUCCESS; - } + * + * Function query_current_calls + * + * Description query list of current calls + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t query_current_calls(void) { + CHECK_BTHF_CLIENT_SLC_CONNECTED(); + + if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_ECS) { + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CLCC, 0, + 0, NULL); + + return BT_STATUS_SUCCESS; + } - return BT_STATUS_UNSUPPORTED; + return BT_STATUS_UNSUPPORTED; } /******************************************************************************* -** -** Function query_current_operator_name -** -** Description query current selected operator name -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t query_current_operator_name(void) -{ - CHECK_BTHF_CLIENT_SLC_CONNECTED(); - - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_COPS, 0, 0, NULL); + * + * Function query_current_operator_name + * + * Description query current selected operator name + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t query_current_operator_name(void) { + CHECK_BTHF_CLIENT_SLC_CONNECTED(); - return BT_STATUS_SUCCESS; + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_COPS, 0, 0, + NULL); + + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function retieve_subscriber_info -** -** Description retrieve subscriber number information -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t retrieve_subscriber_info(void) -{ - CHECK_BTHF_CLIENT_SLC_CONNECTED(); - - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CNUM, 0, 0, NULL); + * + * Function retieve_subscriber_info + * + * Description retrieve subscriber number information + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t retrieve_subscriber_info(void) { + CHECK_BTHF_CLIENT_SLC_CONNECTED(); - return BT_STATUS_SUCCESS; + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_CNUM, 0, 0, + NULL); + + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function send_dtmf -** -** Description send dtmf -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t send_dtmf(char code) -{ - CHECK_BTHF_CLIENT_SLC_CONNECTED(); - - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_VTS, code, 0, NULL); + * + * Function send_dtmf + * + * Description send dtmf + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t send_dtmf(char code) { + CHECK_BTHF_CLIENT_SLC_CONNECTED(); - return BT_STATUS_SUCCESS; + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_VTS, code, + 0, NULL); + + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function request_last_voice_tag_number -** -** Description Request number from AG for VR purposes -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t request_last_voice_tag_number(void) -{ - CHECK_BTHF_CLIENT_SLC_CONNECTED(); - - if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_VTAG) - { - BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_BINP, 1, 0, NULL); - - return BT_STATUS_SUCCESS; - } + * + * Function request_last_voice_tag_number + * + * Description Request number from AG for VR purposes + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t request_last_voice_tag_number(void) { + CHECK_BTHF_CLIENT_SLC_CONNECTED(); + + if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_VTAG) { + BTA_HfClientSendAT(btif_hf_client_cb.handle, BTA_HF_CLIENT_AT_CMD_BINP, 1, + 0, NULL); - return BT_STATUS_UNSUPPORTED; + return BT_STATUS_SUCCESS; + } + + return BT_STATUS_UNSUPPORTED; } /******************************************************************************* -** -** Function cleanup -** -** Description Closes the HF interface -** -** Returns bt_status_t -** -*******************************************************************************/ -static void cleanup( void ) -{ - BTIF_TRACE_EVENT("%s", __func__); - - if (bt_hf_client_callbacks) - { - btif_disable_service(BTA_HFP_HS_SERVICE_ID); - bt_hf_client_callbacks = NULL; - } + * + * Function cleanup + * + * Description Closes the HF interface + * + * Returns bt_status_t + * + ******************************************************************************/ +static void cleanup(void) { + BTIF_TRACE_EVENT("%s", __func__); + + if (bt_hf_client_callbacks) { + btif_disable_service(BTA_HFP_HS_SERVICE_ID); + bt_hf_client_callbacks = NULL; + } } /******************************************************************************* -** -** Function send_at_cmd -** -** Description Send requested AT command to rempte device. -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t send_at_cmd(int cmd,int val1,int val2,const char *arg) -{ - CHECK_BTHF_CLIENT_SLC_CONNECTED(); - BTIF_TRACE_EVENT("%s Cmd %d val1 %d val2 %d arg %s", - __func__, cmd, val1, val2, (arg != NULL) ? arg : ""); - BTA_HfClientSendAT(btif_hf_client_cb.handle, cmd, val1, val2, arg); + * + * Function send_at_cmd + * + * Description Send requested AT command to rempte device. + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t send_at_cmd(int cmd, int val1, int val2, const char* arg) { + CHECK_BTHF_CLIENT_SLC_CONNECTED(); + BTIF_TRACE_EVENT("%s Cmd %d val1 %d val2 %d arg %s", __func__, cmd, val1, + val2, (arg != NULL) ? arg : ""); + BTA_HfClientSendAT(btif_hf_client_cb.handle, cmd, val1, val2, arg); - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } static const bthf_client_interface_t bthfClientInterface = { @@ -688,299 +657,310 @@ static const bthf_client_interface_t bthfClientInterface = { .send_at_cmd = send_at_cmd, }; -static void process_ind_evt(tBTA_HF_CLIENT_IND *ind) -{ - switch (ind->type) - { - case BTA_HF_CLIENT_IND_CALL: - HAL_CBACK(bt_hf_client_callbacks, call_cb, (bthf_client_call_t)ind->value); - break; - - case BTA_HF_CLIENT_IND_CALLSETUP: - HAL_CBACK(bt_hf_client_callbacks, callsetup_cb, (bthf_client_callsetup_t)ind->value); - break; - case BTA_HF_CLIENT_IND_CALLHELD: - HAL_CBACK(bt_hf_client_callbacks, callheld_cb, (bthf_client_callheld_t)ind->value); - break; - - case BTA_HF_CLIENT_IND_SERVICE: - HAL_CBACK(bt_hf_client_callbacks, network_state_cb, (bthf_client_network_state_t)ind->value); - break; - - case BTA_HF_CLIENT_IND_SIGNAL: - HAL_CBACK(bt_hf_client_callbacks, network_signal_cb, ind->value); - break; - - case BTA_HF_CLIENT_IND_ROAM: - HAL_CBACK(bt_hf_client_callbacks, network_roaming_cb, (bthf_client_service_type_t)ind->value); - break; - - case BTA_HF_CLIENT_IND_BATTCH: - HAL_CBACK(bt_hf_client_callbacks, battery_level_cb, ind->value); - break; - - default: - break; - } +static void process_ind_evt(tBTA_HF_CLIENT_IND* ind) { + switch (ind->type) { + case BTA_HF_CLIENT_IND_CALL: + HAL_CBACK(bt_hf_client_callbacks, call_cb, + (bthf_client_call_t)ind->value); + break; + + case BTA_HF_CLIENT_IND_CALLSETUP: + HAL_CBACK(bt_hf_client_callbacks, callsetup_cb, + (bthf_client_callsetup_t)ind->value); + break; + case BTA_HF_CLIENT_IND_CALLHELD: + HAL_CBACK(bt_hf_client_callbacks, callheld_cb, + (bthf_client_callheld_t)ind->value); + break; + + case BTA_HF_CLIENT_IND_SERVICE: + HAL_CBACK(bt_hf_client_callbacks, network_state_cb, + (bthf_client_network_state_t)ind->value); + break; + + case BTA_HF_CLIENT_IND_SIGNAL: + HAL_CBACK(bt_hf_client_callbacks, network_signal_cb, ind->value); + break; + + case BTA_HF_CLIENT_IND_ROAM: + HAL_CBACK(bt_hf_client_callbacks, network_roaming_cb, + (bthf_client_service_type_t)ind->value); + break; + + case BTA_HF_CLIENT_IND_BATTCH: + HAL_CBACK(bt_hf_client_callbacks, battery_level_cb, ind->value); + break; + + default: + break; + } } /******************************************************************************* -** -** Function btif_hf_client_upstreams_evt -** -** Description Executes HF CLIENT UPSTREAMS events in btif context -** -** Returns void -** -*******************************************************************************/ -static void btif_hf_client_upstreams_evt(uint16_t event, char* p_param) -{ - tBTA_HF_CLIENT *p_data = (tBTA_HF_CLIENT *)p_param; - bdstr_t bdstr; - - BTIF_TRACE_DEBUG("%s: event=%s (%u)", __func__, dump_hf_client_event(event), event); - - switch (event) - { - case BTA_HF_CLIENT_ENABLE_EVT: - case BTA_HF_CLIENT_DISABLE_EVT: - break; - - case BTA_HF_CLIENT_REGISTER_EVT: - btif_hf_client_cb.handle = p_data->reg.handle; - break; - - case BTA_HF_CLIENT_OPEN_EVT: - if (p_data->open.status == BTA_HF_CLIENT_SUCCESS) - { - bdcpy(btif_hf_client_cb.connected_bda.address, p_data->open.bd_addr); - btif_hf_client_cb.state = BTHF_CLIENT_CONNECTION_STATE_CONNECTED; - btif_hf_client_cb.peer_feat = 0; - btif_hf_client_cb.chld_feat = 0; - //clear_phone_state(); - } - else if (btif_hf_client_cb.state == BTHF_CLIENT_CONNECTION_STATE_CONNECTING) - { - btif_hf_client_cb.state = BTHF_CLIENT_CONNECTION_STATE_DISCONNECTED; - } - else - { - BTIF_TRACE_WARNING("%s: HF CLient open failed, but another device connected. status=%d state=%d connected device=%s", - __func__, p_data->open.status, btif_hf_client_cb.state, bdaddr_to_string(&btif_hf_client_cb.connected_bda, bdstr, sizeof(bdstr))); - break; - } - - HAL_CBACK(bt_hf_client_callbacks, connection_state_cb, btif_hf_client_cb.state, - 0, 0, &btif_hf_client_cb.connected_bda); - - if (btif_hf_client_cb.state == BTHF_CLIENT_CONNECTION_STATE_DISCONNECTED) - bdsetany(btif_hf_client_cb.connected_bda.address); - - if (p_data->open.status != BTA_HF_CLIENT_SUCCESS) - btif_queue_advance(); - break; - - case BTA_HF_CLIENT_CONN_EVT: - btif_hf_client_cb.peer_feat = p_data->conn.peer_feat; - btif_hf_client_cb.chld_feat = p_data->conn.chld_feat; - btif_hf_client_cb.state = BTHF_CLIENT_CONNECTION_STATE_SLC_CONNECTED; - - HAL_CBACK(bt_hf_client_callbacks, connection_state_cb, btif_hf_client_cb.state, - btif_hf_client_cb.peer_feat, btif_hf_client_cb.chld_feat, - &btif_hf_client_cb.connected_bda); - - /* Inform the application about in-band ringtone */ - if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_INBAND) - { - HAL_CBACK(bt_hf_client_callbacks, in_band_ring_tone_cb, BTHF_CLIENT_IN_BAND_RINGTONE_PROVIDED); - } - - btif_queue_advance(); - break; - - case BTA_HF_CLIENT_CLOSE_EVT: - btif_hf_client_cb.state = BTHF_CLIENT_CONNECTION_STATE_DISCONNECTED; - HAL_CBACK(bt_hf_client_callbacks, connection_state_cb, btif_hf_client_cb.state, - 0, 0, &btif_hf_client_cb.connected_bda); - bdsetany(btif_hf_client_cb.connected_bda.address); - btif_hf_client_cb.peer_feat = 0; - btif_hf_client_cb.chld_feat = 0; - btif_queue_advance(); - break; - - case BTA_HF_CLIENT_IND_EVT: - process_ind_evt(&p_data->ind); - break; - - case BTA_HF_CLIENT_MIC_EVT: - HAL_CBACK(bt_hf_client_callbacks, volume_change_cb, BTHF_CLIENT_VOLUME_TYPE_MIC, p_data->val.value); - break; - - case BTA_HF_CLIENT_SPK_EVT: - HAL_CBACK(bt_hf_client_callbacks, volume_change_cb, BTHF_CLIENT_VOLUME_TYPE_SPK, p_data->val.value); - break; - - case BTA_HF_CLIENT_VOICE_REC_EVT: - HAL_CBACK(bt_hf_client_callbacks, vr_cmd_cb, (bthf_client_vr_state_t)p_data->val.value); - break; - - case BTA_HF_CLIENT_OPERATOR_NAME_EVT: - HAL_CBACK(bt_hf_client_callbacks, current_operator_cb, p_data->operator_name.name); - break; - - case BTA_HF_CLIENT_CLIP_EVT: - HAL_CBACK(bt_hf_client_callbacks, clip_cb, p_data->number.number); - break; - - case BTA_HF_CLIENT_BINP_EVT: - HAL_CBACK(bt_hf_client_callbacks, last_voice_tag_number_callback, p_data->number.number); - break; - - case BTA_HF_CLIENT_CCWA_EVT: - HAL_CBACK(bt_hf_client_callbacks, call_waiting_cb, p_data->number.number); - break; - - case BTA_HF_CLIENT_AT_RESULT_EVT: - HAL_CBACK(bt_hf_client_callbacks, cmd_complete_cb, (bthf_client_cmd_complete_t)p_data->result.type, p_data->result.cme); - break; - - case BTA_HF_CLIENT_CLCC_EVT: - HAL_CBACK(bt_hf_client_callbacks, current_calls_cb, p_data->clcc.idx, - p_data->clcc.inc ? BTHF_CLIENT_CALL_DIRECTION_INCOMING : BTHF_CLIENT_CALL_DIRECTION_OUTGOING, - (bthf_client_call_state_t)p_data->clcc.status, - p_data->clcc.mpty ? BTHF_CLIENT_CALL_MPTY_TYPE_MULTI : BTHF_CLIENT_CALL_MPTY_TYPE_SINGLE, - p_data->clcc.number_present ? p_data->clcc.number : NULL); - break; - - case BTA_HF_CLIENT_CNUM_EVT: - if (p_data->cnum.service == 4) - { - HAL_CBACK(bt_hf_client_callbacks, subscriber_info_cb, p_data->cnum.number, BTHF_CLIENT_SERVICE_VOICE); - } - else if (p_data->cnum.service == 5) - { - HAL_CBACK(bt_hf_client_callbacks, subscriber_info_cb, p_data->cnum.number, BTHF_CLIENT_SERVICE_FAX); - } - else - { - HAL_CBACK(bt_hf_client_callbacks, subscriber_info_cb, p_data->cnum.number, BTHF_CLIENT_SERVICE_UNKNOWN); - } - break; - - case BTA_HF_CLIENT_BTRH_EVT: - if (p_data->val.value <= BTRH_CLIENT_RESP_AND_HOLD_REJECT) - { - HAL_CBACK(bt_hf_client_callbacks, resp_and_hold_cb, (bthf_client_resp_and_hold_t)p_data->val.value); - } - break; - - case BTA_HF_CLIENT_BSIR_EVT: - if (p_data->val.value != 0) - { - HAL_CBACK(bt_hf_client_callbacks, in_band_ring_tone_cb, BTHF_CLIENT_IN_BAND_RINGTONE_PROVIDED); - } - else - { - HAL_CBACK(bt_hf_client_callbacks, in_band_ring_tone_cb, BTHF_CLIENT_IN_BAND_RINGTONE_NOT_PROVIDED); - } - break; - - case BTA_HF_CLIENT_AUDIO_OPEN_EVT: - HAL_CBACK(bt_hf_client_callbacks, audio_state_cb, BTHF_CLIENT_AUDIO_STATE_CONNECTED, &btif_hf_client_cb.connected_bda); - break; - - case BTA_HF_CLIENT_AUDIO_MSBC_OPEN_EVT: - HAL_CBACK(bt_hf_client_callbacks, audio_state_cb, BTHF_CLIENT_AUDIO_STATE_CONNECTED_MSBC, &btif_hf_client_cb.connected_bda); - break; - - case BTA_HF_CLIENT_AUDIO_CLOSE_EVT: - HAL_CBACK(bt_hf_client_callbacks, audio_state_cb, BTHF_CLIENT_AUDIO_STATE_DISCONNECTED, &btif_hf_client_cb.connected_bda); - break; - case BTA_HF_CLIENT_RING_INDICATION: - HAL_CBACK(bt_hf_client_callbacks, ring_indication_cb); - break; - default: - BTIF_TRACE_WARNING("%s: Unhandled event: %d", __func__, event); - break; - } + * + * Function btif_hf_client_upstreams_evt + * + * Description Executes HF CLIENT UPSTREAMS events in btif context + * + * Returns void + * + ******************************************************************************/ +static void btif_hf_client_upstreams_evt(uint16_t event, char* p_param) { + tBTA_HF_CLIENT* p_data = (tBTA_HF_CLIENT*)p_param; + bdstr_t bdstr; + + BTIF_TRACE_DEBUG("%s: event=%s (%u)", __func__, dump_hf_client_event(event), + event); + + switch (event) { + case BTA_HF_CLIENT_ENABLE_EVT: + case BTA_HF_CLIENT_DISABLE_EVT: + break; + + case BTA_HF_CLIENT_REGISTER_EVT: + btif_hf_client_cb.handle = p_data->reg.handle; + break; + + case BTA_HF_CLIENT_OPEN_EVT: + if (p_data->open.status == BTA_HF_CLIENT_SUCCESS) { + bdcpy(btif_hf_client_cb.connected_bda.address, p_data->open.bd_addr); + btif_hf_client_cb.state = BTHF_CLIENT_CONNECTION_STATE_CONNECTED; + btif_hf_client_cb.peer_feat = 0; + btif_hf_client_cb.chld_feat = 0; + // clear_phone_state(); + } else if (btif_hf_client_cb.state == + BTHF_CLIENT_CONNECTION_STATE_CONNECTING) { + btif_hf_client_cb.state = BTHF_CLIENT_CONNECTION_STATE_DISCONNECTED; + } else { + BTIF_TRACE_WARNING( + "%s: HF CLient open failed, but another device connected. " + "status=%d state=%d connected device=%s", + __func__, p_data->open.status, btif_hf_client_cb.state, + bdaddr_to_string(&btif_hf_client_cb.connected_bda, bdstr, + sizeof(bdstr))); + break; + } + + HAL_CBACK(bt_hf_client_callbacks, connection_state_cb, + btif_hf_client_cb.state, 0, 0, + &btif_hf_client_cb.connected_bda); + + if (btif_hf_client_cb.state == BTHF_CLIENT_CONNECTION_STATE_DISCONNECTED) + bdsetany(btif_hf_client_cb.connected_bda.address); + + if (p_data->open.status != BTA_HF_CLIENT_SUCCESS) btif_queue_advance(); + break; + + case BTA_HF_CLIENT_CONN_EVT: + btif_hf_client_cb.peer_feat = p_data->conn.peer_feat; + btif_hf_client_cb.chld_feat = p_data->conn.chld_feat; + btif_hf_client_cb.state = BTHF_CLIENT_CONNECTION_STATE_SLC_CONNECTED; + + HAL_CBACK(bt_hf_client_callbacks, connection_state_cb, + btif_hf_client_cb.state, btif_hf_client_cb.peer_feat, + btif_hf_client_cb.chld_feat, &btif_hf_client_cb.connected_bda); + + /* Inform the application about in-band ringtone */ + if (btif_hf_client_cb.peer_feat & BTA_HF_CLIENT_PEER_INBAND) { + HAL_CBACK(bt_hf_client_callbacks, in_band_ring_tone_cb, + BTHF_CLIENT_IN_BAND_RINGTONE_PROVIDED); + } + + btif_queue_advance(); + break; + + case BTA_HF_CLIENT_CLOSE_EVT: + btif_hf_client_cb.state = BTHF_CLIENT_CONNECTION_STATE_DISCONNECTED; + HAL_CBACK(bt_hf_client_callbacks, connection_state_cb, + btif_hf_client_cb.state, 0, 0, + &btif_hf_client_cb.connected_bda); + bdsetany(btif_hf_client_cb.connected_bda.address); + btif_hf_client_cb.peer_feat = 0; + btif_hf_client_cb.chld_feat = 0; + btif_queue_advance(); + break; + + case BTA_HF_CLIENT_IND_EVT: + process_ind_evt(&p_data->ind); + break; + + case BTA_HF_CLIENT_MIC_EVT: + HAL_CBACK(bt_hf_client_callbacks, volume_change_cb, + BTHF_CLIENT_VOLUME_TYPE_MIC, p_data->val.value); + break; + + case BTA_HF_CLIENT_SPK_EVT: + HAL_CBACK(bt_hf_client_callbacks, volume_change_cb, + BTHF_CLIENT_VOLUME_TYPE_SPK, p_data->val.value); + break; + + case BTA_HF_CLIENT_VOICE_REC_EVT: + HAL_CBACK(bt_hf_client_callbacks, vr_cmd_cb, + (bthf_client_vr_state_t)p_data->val.value); + break; + + case BTA_HF_CLIENT_OPERATOR_NAME_EVT: + HAL_CBACK(bt_hf_client_callbacks, current_operator_cb, + p_data->operator_name.name); + break; + + case BTA_HF_CLIENT_CLIP_EVT: + HAL_CBACK(bt_hf_client_callbacks, clip_cb, p_data->number.number); + break; + + case BTA_HF_CLIENT_BINP_EVT: + HAL_CBACK(bt_hf_client_callbacks, last_voice_tag_number_callback, + p_data->number.number); + break; + + case BTA_HF_CLIENT_CCWA_EVT: + HAL_CBACK(bt_hf_client_callbacks, call_waiting_cb, p_data->number.number); + break; + + case BTA_HF_CLIENT_AT_RESULT_EVT: + HAL_CBACK(bt_hf_client_callbacks, cmd_complete_cb, + (bthf_client_cmd_complete_t)p_data->result.type, + p_data->result.cme); + break; + + case BTA_HF_CLIENT_CLCC_EVT: + HAL_CBACK(bt_hf_client_callbacks, current_calls_cb, p_data->clcc.idx, + p_data->clcc.inc ? BTHF_CLIENT_CALL_DIRECTION_INCOMING + : BTHF_CLIENT_CALL_DIRECTION_OUTGOING, + (bthf_client_call_state_t)p_data->clcc.status, + p_data->clcc.mpty ? BTHF_CLIENT_CALL_MPTY_TYPE_MULTI + : BTHF_CLIENT_CALL_MPTY_TYPE_SINGLE, + p_data->clcc.number_present ? p_data->clcc.number : NULL); + break; + + case BTA_HF_CLIENT_CNUM_EVT: + if (p_data->cnum.service == 4) { + HAL_CBACK(bt_hf_client_callbacks, subscriber_info_cb, + p_data->cnum.number, BTHF_CLIENT_SERVICE_VOICE); + } else if (p_data->cnum.service == 5) { + HAL_CBACK(bt_hf_client_callbacks, subscriber_info_cb, + p_data->cnum.number, BTHF_CLIENT_SERVICE_FAX); + } else { + HAL_CBACK(bt_hf_client_callbacks, subscriber_info_cb, + p_data->cnum.number, BTHF_CLIENT_SERVICE_UNKNOWN); + } + break; + + case BTA_HF_CLIENT_BTRH_EVT: + if (p_data->val.value <= BTRH_CLIENT_RESP_AND_HOLD_REJECT) { + HAL_CBACK(bt_hf_client_callbacks, resp_and_hold_cb, + (bthf_client_resp_and_hold_t)p_data->val.value); + } + break; + + case BTA_HF_CLIENT_BSIR_EVT: + if (p_data->val.value != 0) { + HAL_CBACK(bt_hf_client_callbacks, in_band_ring_tone_cb, + BTHF_CLIENT_IN_BAND_RINGTONE_PROVIDED); + } else { + HAL_CBACK(bt_hf_client_callbacks, in_band_ring_tone_cb, + BTHF_CLIENT_IN_BAND_RINGTONE_NOT_PROVIDED); + } + break; + + case BTA_HF_CLIENT_AUDIO_OPEN_EVT: + HAL_CBACK(bt_hf_client_callbacks, audio_state_cb, + BTHF_CLIENT_AUDIO_STATE_CONNECTED, + &btif_hf_client_cb.connected_bda); + break; + + case BTA_HF_CLIENT_AUDIO_MSBC_OPEN_EVT: + HAL_CBACK(bt_hf_client_callbacks, audio_state_cb, + BTHF_CLIENT_AUDIO_STATE_CONNECTED_MSBC, + &btif_hf_client_cb.connected_bda); + break; + + case BTA_HF_CLIENT_AUDIO_CLOSE_EVT: + HAL_CBACK(bt_hf_client_callbacks, audio_state_cb, + BTHF_CLIENT_AUDIO_STATE_DISCONNECTED, + &btif_hf_client_cb.connected_bda); + break; + case BTA_HF_CLIENT_RING_INDICATION: + HAL_CBACK(bt_hf_client_callbacks, ring_indication_cb); + break; + default: + BTIF_TRACE_WARNING("%s: Unhandled event: %d", __func__, event); + break; + } } /******************************************************************************* -** -** Function bte_hf_client_evt -** -** Description Switches context from BTE to BTIF for all HF Client events -** -** Returns void -** -*******************************************************************************/ - -static void bte_hf_client_evt(tBTA_HF_CLIENT_EVT event, tBTA_HF_CLIENT *p_data) -{ - bt_status_t status; - - /* switch context to btif task context (copy full union size for convenience) */ - status = btif_transfer_context(btif_hf_client_upstreams_evt, (uint16_t)event, (char*)p_data, sizeof(*p_data), NULL); - - /* catch any failed context transfers */ - ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); + * + * Function bte_hf_client_evt + * + * Description Switches context from BTE to BTIF for all HF Client events + * + * Returns void + * + ******************************************************************************/ + +static void bte_hf_client_evt(tBTA_HF_CLIENT_EVT event, + tBTA_HF_CLIENT* p_data) { + bt_status_t status; + + /* switch context to btif task context (copy full union size for convenience) + */ + status = btif_transfer_context(btif_hf_client_upstreams_evt, (uint16_t)event, + (char*)p_data, sizeof(*p_data), NULL); + + /* catch any failed context transfers */ + ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); } /******************************************************************************* -** -** Function btif_hf_client_execute_service -** -** Description Initializes/Shuts down the service -** -** Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_hf_client_execute_service(bool b_enable) -{ - BTIF_TRACE_EVENT("%s enable:%d", __func__, b_enable); - - osi_property_get("ro.bluetooth.hfp.ver", btif_hf_client_version, "1.5"); - - if (b_enable) - { - /* Enable and register with BTA-HFClient */ - BTA_HfClientEnable(bte_hf_client_evt); - if (strcmp(btif_hf_client_version, "1.6") == 0) - { - BTIF_TRACE_EVENT("Support Codec Nego. %d ", BTIF_HF_CLIENT_FEATURES); - BTA_HfClientRegister(BTIF_HF_CLIENT_SECURITY, BTIF_HF_CLIENT_FEATURES, - BTIF_HF_CLIENT_SERVICE_NAME); - } - else - { - BTIF_TRACE_EVENT("No Codec Nego Supported"); - btif_hf_client_features = BTIF_HF_CLIENT_FEATURES; - btif_hf_client_features = btif_hf_client_features & (~BTA_HF_CLIENT_FEAT_CODEC); - BTIF_TRACE_EVENT("btif_hf_client_features is %d", btif_hf_client_features); - BTA_HfClientRegister(BTIF_HF_CLIENT_SECURITY, btif_hf_client_features, - BTIF_HF_CLIENT_SERVICE_NAME); - } - - } - else - { - BTA_HfClientDeregister(btif_hf_client_cb.handle); - BTA_HfClientDisable(); - } - return BT_STATUS_SUCCESS; + * + * Function btif_hf_client_execute_service + * + * Description Initializes/Shuts down the service + * + * Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_hf_client_execute_service(bool b_enable) { + BTIF_TRACE_EVENT("%s enable:%d", __func__, b_enable); + + osi_property_get("ro.bluetooth.hfp.ver", btif_hf_client_version, "1.5"); + + if (b_enable) { + /* Enable and register with BTA-HFClient */ + BTA_HfClientEnable(bte_hf_client_evt); + if (strcmp(btif_hf_client_version, "1.6") == 0) { + BTIF_TRACE_EVENT("Support Codec Nego. %d ", BTIF_HF_CLIENT_FEATURES); + BTA_HfClientRegister(BTIF_HF_CLIENT_SECURITY, BTIF_HF_CLIENT_FEATURES, + BTIF_HF_CLIENT_SERVICE_NAME); + } else { + BTIF_TRACE_EVENT("No Codec Nego Supported"); + btif_hf_client_features = BTIF_HF_CLIENT_FEATURES; + btif_hf_client_features = + btif_hf_client_features & (~BTA_HF_CLIENT_FEAT_CODEC); + BTIF_TRACE_EVENT("btif_hf_client_features is %d", + btif_hf_client_features); + BTA_HfClientRegister(BTIF_HF_CLIENT_SECURITY, btif_hf_client_features, + BTIF_HF_CLIENT_SERVICE_NAME); + } + + } else { + BTA_HfClientDeregister(btif_hf_client_cb.handle); + BTA_HfClientDisable(); + } + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_hf_get_interface -** -** Description Get the hf callback interface -** -** Returns bthf_interface_t -** -*******************************************************************************/ -const bthf_client_interface_t *btif_hf_client_get_interface(void) -{ - BTIF_TRACE_EVENT("%s", __func__); - return &bthfClientInterface; + * + * Function btif_hf_get_interface + * + * Description Get the hf callback interface + * + * Returns bthf_interface_t + * + ******************************************************************************/ +const bthf_client_interface_t* btif_hf_client_get_interface(void) { + BTIF_TRACE_EVENT("%s", __func__); + return &bthfClientInterface; } diff --git a/btif/src/btif_hh.cc b/btif/src/btif_hh.cc index e2f65720e..c59a8b778 100644 --- a/btif/src/btif_hh.cc +++ b/btif/src/btif_hh.cc @@ -30,49 +30,50 @@ #include "btif_hh.h" #include +#include #include #include -#include #include #include +#include "bt_common.h" #include "bta_api.h" #include "btif_common.h" #include "btif_storage.h" #include "btif_util.h" -#include "bt_common.h" #include "l2c_api.h" #include "osi/include/log.h" -#define BTIF_HH_APP_ID_MI 0x01 -#define BTIF_HH_APP_ID_KB 0x02 +#define BTIF_HH_APP_ID_MI 0x01 +#define BTIF_HH_APP_ID_KB 0x02 -#define COD_HID_KEYBOARD 0x0540 -#define COD_HID_POINTING 0x0580 -#define COD_HID_COMBO 0x05C0 +#define COD_HID_KEYBOARD 0x0540 +#define COD_HID_POINTING 0x0580 +#define COD_HID_COMBO 0x05C0 -#define KEYSTATE_FILEPATH "/data/misc/bluedroid/bt_hh_ks" //keep this in sync with HID host jni +#define KEYSTATE_FILEPATH \ + "/data/misc/bluedroid/bt_hh_ks" // keep this in sync with HID host jni -#define HID_REPORT_CAPSLOCK 0x39 -#define HID_REPORT_NUMLOCK 0x53 +#define HID_REPORT_CAPSLOCK 0x39 +#define HID_REPORT_NUMLOCK 0x53 #define HID_REPORT_SCROLLLOCK 0x47 -//For Apple Magic Mouse +// For Apple Magic Mouse #define MAGICMOUSE_VENDOR_ID 0x05ac #define MAGICMOUSE_PRODUCT_ID 0x030d -#define LOGITECH_KB_MX5500_VENDOR_ID 0x046D +#define LOGITECH_KB_MX5500_VENDOR_ID 0x046D #define LOGITECH_KB_MX5500_PRODUCT_ID 0xB30B -extern fixed_queue_t *btu_general_alarm_queue; +extern fixed_queue_t* btu_general_alarm_queue; extern const int BT_UID; extern const int BT_GID; -static int btif_hh_keylockstates=0; //The current key state of each key +static int btif_hh_keylockstates = 0; // The current key state of each key -#define BTIF_HH_ID_1 0 +#define BTIF_HH_ID_1 0 #define BTIF_HH_DEV_DISCONNECTED 3 -#define BTIF_TIMEOUT_VUP_MS (3 * 1000) +#define BTIF_TIMEOUT_VUP_MS (3 * 1000) #ifndef BTUI_HH_SECURITY #define BTUI_HH_SECURITY (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT) @@ -83,386 +84,375 @@ static int btif_hh_keylockstates=0; //The current key state of each key #endif /* HH request events */ -typedef enum -{ - BTIF_HH_CONNECT_REQ_EVT = 0, - BTIF_HH_DISCONNECT_REQ_EVT, - BTIF_HH_VUP_REQ_EVT +typedef enum { + BTIF_HH_CONNECT_REQ_EVT = 0, + BTIF_HH_DISCONNECT_REQ_EVT, + BTIF_HH_VUP_REQ_EVT } btif_hh_req_evt_t; /************************************************************************************ -** Constants & Macros -************************************************************************************/ -#define BTIF_HH_SERVICES (BTA_HID_SERVICE_MASK) + * Constants & Macros + ***********************************************************************************/ +#define BTIF_HH_SERVICES (BTA_HID_SERVICE_MASK) /************************************************************************************ -** Local type definitions -************************************************************************************/ - -typedef struct hid_kb_list -{ - uint16_t product_id; - uint16_t version_id; - const char* kb_name; + * Local type definitions + ***********************************************************************************/ + +typedef struct hid_kb_list { + uint16_t product_id; + uint16_t version_id; + const char* kb_name; } tHID_KB_LIST; /************************************************************************************ -** Static variables -************************************************************************************/ + * Static variables + ***********************************************************************************/ btif_hh_cb_t btif_hh_cb; -static bthh_callbacks_t *bt_hh_callbacks = NULL; +static bthh_callbacks_t* bt_hh_callbacks = NULL; /* List of HID keyboards for which the NUMLOCK state needs to be * turned ON by default. Add devices to this list to apply the * NUMLOCK state toggle on fpr first connect.*/ -static tHID_KB_LIST hid_kb_numlock_on_list[] = -{ - {LOGITECH_KB_MX5500_PRODUCT_ID, - LOGITECH_KB_MX5500_VENDOR_ID, - "Logitech MX5500 Keyboard"} -}; - -#define CHECK_BTHH_INIT() if (bt_hh_callbacks == NULL)\ - {\ - BTIF_TRACE_WARNING("BTHH: %s: BTHH not initialized", __func__);\ - return BT_STATUS_NOT_READY;\ - }\ - else\ - {\ - BTIF_TRACE_EVENT("BTHH: %s", __func__);\ - } +static tHID_KB_LIST hid_kb_numlock_on_list[] = {{LOGITECH_KB_MX5500_PRODUCT_ID, + LOGITECH_KB_MX5500_VENDOR_ID, + "Logitech MX5500 Keyboard"}}; + +#define CHECK_BTHH_INIT() \ + if (bt_hh_callbacks == NULL) { \ + BTIF_TRACE_WARNING("BTHH: %s: BTHH not initialized", __func__); \ + return BT_STATUS_NOT_READY; \ + } else { \ + BTIF_TRACE_EVENT("BTHH: %s", __func__); \ + } /************************************************************************************ -** Static functions -************************************************************************************/ + * Static functions + ***********************************************************************************/ /************************************************************************************ -** Externs -************************************************************************************/ + * Externs + ***********************************************************************************/ extern void bta_hh_co_destroy(int fd); extern void bta_hh_co_write(int fd, uint8_t* rpt, uint16_t len); -extern bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr); -extern void bta_hh_co_send_hid_info(btif_hh_device_t *p_dev, const char *dev_name, uint16_t vendor_id, - uint16_t product_id, uint16_t version, uint8_t ctry_code, - int dscp_len, uint8_t *p_dscp); -extern bool check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod); -extern void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr); -extern bool check_cod_hid(const bt_bdaddr_t *remote_bdaddr); -extern int scru_ascii_2_hex(char *p_ascii, int len, uint8_t *p_hex); -extern void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr); +extern bt_status_t btif_dm_remove_bond(const bt_bdaddr_t* bd_addr); +extern void bta_hh_co_send_hid_info(btif_hh_device_t* p_dev, + const char* dev_name, uint16_t vendor_id, + uint16_t product_id, uint16_t version, + uint8_t ctry_code, int dscp_len, + uint8_t* p_dscp); +extern bool check_cod(const bt_bdaddr_t* remote_bdaddr, uint32_t cod); +extern void btif_dm_cb_remove_bond(bt_bdaddr_t* bd_addr); +extern bool check_cod_hid(const bt_bdaddr_t* remote_bdaddr); +extern int scru_ascii_2_hex(char* p_ascii, int len, uint8_t* p_hex); +extern void btif_dm_hh_open_failed(bt_bdaddr_t* bdaddr); /***************************************************************************** -** Local Function prototypes -*****************************************************************************/ + * Local Function prototypes + ****************************************************************************/ static void set_keylockstate(int keymask, bool isSet); static void toggle_os_keylockstates(int fd, int changedkeystates); -static void sync_lockstate_on_connect(btif_hh_device_t *p_dev); -//static void hh_update_keyboard_lockstates(btif_hh_device_t *p_dev); -void btif_hh_timer_timeout(void *data); +static void sync_lockstate_on_connect(btif_hh_device_t* p_dev); +// static void hh_update_keyboard_lockstates(btif_hh_device_t *p_dev); +void btif_hh_timer_timeout(void* data); /************************************************************************************ -** Functions -************************************************************************************/ + * Functions + ***********************************************************************************/ -static int get_keylockstates() -{ - return btif_hh_keylockstates; -} +static int get_keylockstates() { return btif_hh_keylockstates; } -static void set_keylockstate(int keymask, bool isSet) -{ - if(isSet) - btif_hh_keylockstates |= keymask; +static void set_keylockstate(int keymask, bool isSet) { + if (isSet) btif_hh_keylockstates |= keymask; } /******************************************************************************* -** -** Function toggle_os_keylockstates -** -** Description Function to toggle the keyboard lock states managed by the linux. -** This function is used in by two call paths -** (1) if the lock state change occurred from an onscreen keyboard, -** this function is called to update the lock state maintained + * + * Function toggle_os_keylockstates + * + * Description Function to toggle the keyboard lock states managed by the + linux. + * This function is used in by two call paths + * (1) if the lock state change occurred from an onscreen + keyboard, + * this function is called to update the lock state maintained for the HID keyboard(s) -** (2) if a HID keyboard is disconnected and reconnected, -** this function is called to update the lock state maintained + * (2) if a HID keyboard is disconnected and reconnected, + * this function is called to update the lock state maintained for the HID keyboard(s) -** Returns void -*******************************************************************************/ - -static void toggle_os_keylockstates(int fd, int changedlockstates) -{ - BTIF_TRACE_EVENT("%s: fd = %d, changedlockstates = 0x%x", - __func__, fd, changedlockstates); - uint8_t hidreport[9]; - int reportIndex; - memset(hidreport,0,9); - hidreport[0]=1; - reportIndex=4; - - if (changedlockstates & BTIF_HH_KEYSTATE_MASK_CAPSLOCK) { - BTIF_TRACE_DEBUG("%s Setting CAPSLOCK", __func__); - hidreport[reportIndex++] = (uint8_t)HID_REPORT_CAPSLOCK; - } - - if (changedlockstates & BTIF_HH_KEYSTATE_MASK_NUMLOCK) { - BTIF_TRACE_DEBUG("%s Setting NUMLOCK", __func__); - hidreport[reportIndex++] = (uint8_t)HID_REPORT_NUMLOCK; - } - - if (changedlockstates & BTIF_HH_KEYSTATE_MASK_SCROLLLOCK) { - BTIF_TRACE_DEBUG("%s Setting SCROLLLOCK", __func__); - hidreport[reportIndex++] = (uint8_t) HID_REPORT_SCROLLLOCK; - } + * Returns void + ******************************************************************************/ - BTIF_TRACE_DEBUG("Writing hidreport #1 to os: "\ - "%s: %x %x %x", __func__, - hidreport[0], hidreport[1], hidreport[2]); - BTIF_TRACE_DEBUG("%s: %x %x %x", __func__, - hidreport[3], hidreport[4], hidreport[5]); - BTIF_TRACE_DEBUG("%s: %x %x %x", __func__, - hidreport[6], hidreport[7], hidreport[8]); - bta_hh_co_write(fd , hidreport, sizeof(hidreport)); - usleep(200000); - memset(hidreport,0,9); - hidreport[0]=1; - BTIF_TRACE_DEBUG("Writing hidreport #2 to os: "\ - "%s: %x %x %x", __func__, - hidreport[0], hidreport[1], hidreport[2]); - BTIF_TRACE_DEBUG("%s: %x %x %x", __func__, - hidreport[3], hidreport[4], hidreport[5]); - BTIF_TRACE_DEBUG("%s: %x %x %x ", __func__, - hidreport[6], hidreport[7], hidreport[8]); - bta_hh_co_write(fd , hidreport, sizeof(hidreport)); +static void toggle_os_keylockstates(int fd, int changedlockstates) { + BTIF_TRACE_EVENT("%s: fd = %d, changedlockstates = 0x%x", __func__, fd, + changedlockstates); + uint8_t hidreport[9]; + int reportIndex; + memset(hidreport, 0, 9); + hidreport[0] = 1; + reportIndex = 4; + + if (changedlockstates & BTIF_HH_KEYSTATE_MASK_CAPSLOCK) { + BTIF_TRACE_DEBUG("%s Setting CAPSLOCK", __func__); + hidreport[reportIndex++] = (uint8_t)HID_REPORT_CAPSLOCK; + } + + if (changedlockstates & BTIF_HH_KEYSTATE_MASK_NUMLOCK) { + BTIF_TRACE_DEBUG("%s Setting NUMLOCK", __func__); + hidreport[reportIndex++] = (uint8_t)HID_REPORT_NUMLOCK; + } + + if (changedlockstates & BTIF_HH_KEYSTATE_MASK_SCROLLLOCK) { + BTIF_TRACE_DEBUG("%s Setting SCROLLLOCK", __func__); + hidreport[reportIndex++] = (uint8_t)HID_REPORT_SCROLLLOCK; + } + + BTIF_TRACE_DEBUG( + "Writing hidreport #1 to os: " + "%s: %x %x %x", + __func__, hidreport[0], hidreport[1], hidreport[2]); + BTIF_TRACE_DEBUG("%s: %x %x %x", __func__, hidreport[3], hidreport[4], + hidreport[5]); + BTIF_TRACE_DEBUG("%s: %x %x %x", __func__, hidreport[6], hidreport[7], + hidreport[8]); + bta_hh_co_write(fd, hidreport, sizeof(hidreport)); + usleep(200000); + memset(hidreport, 0, 9); + hidreport[0] = 1; + BTIF_TRACE_DEBUG( + "Writing hidreport #2 to os: " + "%s: %x %x %x", + __func__, hidreport[0], hidreport[1], hidreport[2]); + BTIF_TRACE_DEBUG("%s: %x %x %x", __func__, hidreport[3], hidreport[4], + hidreport[5]); + BTIF_TRACE_DEBUG("%s: %x %x %x ", __func__, hidreport[6], hidreport[7], + hidreport[8]); + bta_hh_co_write(fd, hidreport, sizeof(hidreport)); } /******************************************************************************* -** -** Function create_pbuf -** -** Description Helper function to create p_buf for send_data or set_report -** -*******************************************************************************/ -static BT_HDR *create_pbuf(uint16_t len, uint8_t *data) -{ - BT_HDR* p_buf = (BT_HDR*)osi_malloc(len + BTA_HH_MIN_OFFSET + sizeof(BT_HDR)); - uint8_t* pbuf_data; - - p_buf->len = len; - p_buf->offset = BTA_HH_MIN_OFFSET; - - pbuf_data = (uint8_t *)(p_buf + 1) + p_buf->offset; - memcpy(pbuf_data, data, len); - - return p_buf; + * + * Function create_pbuf + * + * Description Helper function to create p_buf for send_data or set_report + * + ******************************************************************************/ +static BT_HDR* create_pbuf(uint16_t len, uint8_t* data) { + BT_HDR* p_buf = (BT_HDR*)osi_malloc(len + BTA_HH_MIN_OFFSET + sizeof(BT_HDR)); + uint8_t* pbuf_data; + + p_buf->len = len; + p_buf->offset = BTA_HH_MIN_OFFSET; + + pbuf_data = (uint8_t*)(p_buf + 1) + p_buf->offset; + memcpy(pbuf_data, data, len); + + return p_buf; } /******************************************************************************* -** -** Function update_keyboard_lockstates -** -** Description Sends a report to the keyboard to set the lock states of keys -** -*******************************************************************************/ -static void update_keyboard_lockstates(btif_hh_device_t *p_dev) -{ - uint8_t len = 2; /* reportid + 1 byte report*/ - BD_ADDR* bda; - BT_HDR* p_buf; - uint8_t data[] = {0x01, /* report id */ + * + * Function update_keyboard_lockstates + * + * Description Sends a report to the keyboard to set the lock states of + *keys + * + ******************************************************************************/ +static void update_keyboard_lockstates(btif_hh_device_t* p_dev) { + uint8_t len = 2; /* reportid + 1 byte report*/ + BD_ADDR* bda; + BT_HDR* p_buf; + uint8_t data[] = {0x01, /* report id */ static_cast(btif_hh_keylockstates)}; /* keystate */ - /* Set report for other keyboards */ - BTIF_TRACE_EVENT("%s: setting report on dev_handle %d to 0x%x", - __func__, p_dev->dev_handle, btif_hh_keylockstates); - - /* Get SetReport buffer */ - p_buf = create_pbuf(len, data); - if (p_buf != NULL) { - p_buf->layer_specific = BTA_HH_RPTT_OUTPUT; - bda = (BD_ADDR*) (&p_dev->bd_addr); - BTA_HhSendData(p_dev->dev_handle, *bda, p_buf); - } + /* Set report for other keyboards */ + BTIF_TRACE_EVENT("%s: setting report on dev_handle %d to 0x%x", __func__, + p_dev->dev_handle, btif_hh_keylockstates); + + /* Get SetReport buffer */ + p_buf = create_pbuf(len, data); + if (p_buf != NULL) { + p_buf->layer_specific = BTA_HH_RPTT_OUTPUT; + bda = (BD_ADDR*)(&p_dev->bd_addr); + BTA_HhSendData(p_dev->dev_handle, *bda, p_buf); + } } /******************************************************************************* -** -** Function sync_lockstate_on_connect -** -** Description Function to update the keyboard lock states managed by the OS -** when a HID keyboard is connected or disconnected and reconnected -** Returns void -*******************************************************************************/ -static void sync_lockstate_on_connect(btif_hh_device_t *p_dev) -{ - int keylockstates; - - BTIF_TRACE_EVENT("%s: Syncing keyboard lock states after "\ - "reconnect...",__func__); - /*If the device is connected, update keyboard state */ - update_keyboard_lockstates(p_dev); - - /*Check if the lockstate of caps,scroll,num is set. - If so, send a report to the kernel - so the lockstate is in sync */ - keylockstates = get_keylockstates(); - if (keylockstates) - { - BTIF_TRACE_DEBUG("%s: Sending hid report to kernel "\ - "indicating lock key state 0x%x",__func__, - keylockstates); - usleep(200000); - toggle_os_keylockstates(p_dev->fd, keylockstates); - } - else - { - BTIF_TRACE_DEBUG("%s: NOT sending hid report to kernel "\ - "indicating lock key state 0x%x",__func__, - keylockstates); - } + * + * Function sync_lockstate_on_connect + * + * Description Function to update the keyboard lock states managed by the + *OS + * when a HID keyboard is connected or disconnected and + *reconnected + * Returns void + ******************************************************************************/ +static void sync_lockstate_on_connect(btif_hh_device_t* p_dev) { + int keylockstates; + + BTIF_TRACE_EVENT( + "%s: Syncing keyboard lock states after " + "reconnect...", + __func__); + /*If the device is connected, update keyboard state */ + update_keyboard_lockstates(p_dev); + + /*Check if the lockstate of caps,scroll,num is set. + If so, send a report to the kernel + so the lockstate is in sync */ + keylockstates = get_keylockstates(); + if (keylockstates) { + BTIF_TRACE_DEBUG( + "%s: Sending hid report to kernel " + "indicating lock key state 0x%x", + __func__, keylockstates); + usleep(200000); + toggle_os_keylockstates(p_dev->fd, keylockstates); + } else { + BTIF_TRACE_DEBUG( + "%s: NOT sending hid report to kernel " + "indicating lock key state 0x%x", + __func__, keylockstates); + } } /******************************************************************************* -** -** Function btif_hh_find_connected_dev_by_handle -** -** Description Return the connected device pointer of the specified device handle -** -** Returns Device entry pointer in the device table -*******************************************************************************/ -btif_hh_device_t *btif_hh_find_connected_dev_by_handle(uint8_t handle) -{ - uint32_t i; - for (i = 0; i < BTIF_HH_MAX_HID; i++) { - if (btif_hh_cb.devices[i].dev_status == BTHH_CONN_STATE_CONNECTED && - btif_hh_cb.devices[i].dev_handle == handle) - { - return &btif_hh_cb.devices[i]; - } + * + * Function btif_hh_find_connected_dev_by_handle + * + * Description Return the connected device pointer of the specified device + *handle + * + * Returns Device entry pointer in the device table + ******************************************************************************/ +btif_hh_device_t* btif_hh_find_connected_dev_by_handle(uint8_t handle) { + uint32_t i; + for (i = 0; i < BTIF_HH_MAX_HID; i++) { + if (btif_hh_cb.devices[i].dev_status == BTHH_CONN_STATE_CONNECTED && + btif_hh_cb.devices[i].dev_handle == handle) { + return &btif_hh_cb.devices[i]; } - return NULL; + } + return NULL; } /******************************************************************************* -** -** Function btif_hh_find_dev_by_bda -** -** Description Return the device pointer of the specified bt_bdaddr_t. -** -** Returns Device entry pointer in the device table -*******************************************************************************/ -static btif_hh_device_t *btif_hh_find_dev_by_bda(bt_bdaddr_t *bd_addr) -{ - uint32_t i; - for (i = 0; i < BTIF_HH_MAX_HID; i++) { - if (btif_hh_cb.devices[i].dev_status != BTHH_CONN_STATE_UNKNOWN && - memcmp(&(btif_hh_cb.devices[i].bd_addr), bd_addr, BD_ADDR_LEN) == 0) - { - return &btif_hh_cb.devices[i]; - } + * + * Function btif_hh_find_dev_by_bda + * + * Description Return the device pointer of the specified bt_bdaddr_t. + * + * Returns Device entry pointer in the device table + ******************************************************************************/ +static btif_hh_device_t* btif_hh_find_dev_by_bda(bt_bdaddr_t* bd_addr) { + uint32_t i; + for (i = 0; i < BTIF_HH_MAX_HID; i++) { + if (btif_hh_cb.devices[i].dev_status != BTHH_CONN_STATE_UNKNOWN && + memcmp(&(btif_hh_cb.devices[i].bd_addr), bd_addr, BD_ADDR_LEN) == 0) { + return &btif_hh_cb.devices[i]; } - return NULL; + } + return NULL; } /******************************************************************************* -** -** Function btif_hh_find_connected_dev_by_bda -** -** Description Return the connected device pointer of the specified bt_bdaddr_t. -** -** Returns Device entry pointer in the device table -*******************************************************************************/ -static btif_hh_device_t *btif_hh_find_connected_dev_by_bda(bt_bdaddr_t *bd_addr) -{ - uint32_t i; - for (i = 0; i < BTIF_HH_MAX_HID; i++) { - if (btif_hh_cb.devices[i].dev_status == BTHH_CONN_STATE_CONNECTED && - memcmp(&(btif_hh_cb.devices[i].bd_addr), bd_addr, BD_ADDR_LEN) == 0) - { - return &btif_hh_cb.devices[i]; - } + * + * Function btif_hh_find_connected_dev_by_bda + * + * Description Return the connected device pointer of the specified + *bt_bdaddr_t. + * + * Returns Device entry pointer in the device table + ******************************************************************************/ +static btif_hh_device_t* btif_hh_find_connected_dev_by_bda( + bt_bdaddr_t* bd_addr) { + uint32_t i; + for (i = 0; i < BTIF_HH_MAX_HID; i++) { + if (btif_hh_cb.devices[i].dev_status == BTHH_CONN_STATE_CONNECTED && + memcmp(&(btif_hh_cb.devices[i].bd_addr), bd_addr, BD_ADDR_LEN) == 0) { + return &btif_hh_cb.devices[i]; } - return NULL; + } + return NULL; } /******************************************************************************* -** -** Function btif_hh_stop_vup_timer -** -** Description stop vitual unplug timer -** -** Returns void -*******************************************************************************/ -void btif_hh_stop_vup_timer(bt_bdaddr_t *bd_addr) -{ - btif_hh_device_t *p_dev = btif_hh_find_connected_dev_by_bda(bd_addr); - - if (p_dev != NULL) { - BTIF_TRACE_DEBUG("stop VUP timer"); - alarm_free(p_dev->vup_timer); - p_dev->vup_timer = NULL; - } + * + * Function btif_hh_stop_vup_timer + * + * Description stop vitual unplug timer + * + * Returns void + ******************************************************************************/ +void btif_hh_stop_vup_timer(bt_bdaddr_t* bd_addr) { + btif_hh_device_t* p_dev = btif_hh_find_connected_dev_by_bda(bd_addr); + + if (p_dev != NULL) { + BTIF_TRACE_DEBUG("stop VUP timer"); + alarm_free(p_dev->vup_timer); + p_dev->vup_timer = NULL; + } } /******************************************************************************* -** -** Function btif_hh_start_vup_timer -** -** Description start virtual unplug timer -** -** Returns void -*******************************************************************************/ -void btif_hh_start_vup_timer(bt_bdaddr_t *bd_addr) -{ - BTIF_TRACE_DEBUG("%s", __func__); - - btif_hh_device_t *p_dev = btif_hh_find_connected_dev_by_bda(bd_addr); - assert(p_dev != NULL); + * + * Function btif_hh_start_vup_timer + * + * Description start virtual unplug timer + * + * Returns void + ******************************************************************************/ +void btif_hh_start_vup_timer(bt_bdaddr_t* bd_addr) { + BTIF_TRACE_DEBUG("%s", __func__); - alarm_free(p_dev->vup_timer); - p_dev->vup_timer = alarm_new("btif_hh.vup_timer"); - alarm_set_on_queue(p_dev->vup_timer, BTIF_TIMEOUT_VUP_MS, - btif_hh_timer_timeout, p_dev, - btu_general_alarm_queue); + btif_hh_device_t* p_dev = btif_hh_find_connected_dev_by_bda(bd_addr); + assert(p_dev != NULL); + + alarm_free(p_dev->vup_timer); + p_dev->vup_timer = alarm_new("btif_hh.vup_timer"); + alarm_set_on_queue(p_dev->vup_timer, BTIF_TIMEOUT_VUP_MS, + btif_hh_timer_timeout, p_dev, btu_general_alarm_queue); } /******************************************************************************* -** -** Function btif_hh_add_added_dev -** -** Description Add a new device to the added device list. -** -** Returns true if add successfully, otherwise false. -*******************************************************************************/ -bool btif_hh_add_added_dev(bt_bdaddr_t bda, tBTA_HH_ATTR_MASK attr_mask) -{ - int i; - for (i = 0; i < BTIF_HH_MAX_ADDED_DEV; i++) { - if (memcmp(&(btif_hh_cb.added_devices[i].bd_addr), &bda, BD_ADDR_LEN) == 0) { - BTIF_TRACE_WARNING(" Device %02X:%02X:%02X:%02X:%02X:%02X already added", - bda.address[0], bda.address[1], bda.address[2], bda.address[3], bda.address[4], bda.address[5]); - return false; - } + * + * Function btif_hh_add_added_dev + * + * Description Add a new device to the added device list. + * + * Returns true if add successfully, otherwise false. + ******************************************************************************/ +bool btif_hh_add_added_dev(bt_bdaddr_t bda, tBTA_HH_ATTR_MASK attr_mask) { + int i; + for (i = 0; i < BTIF_HH_MAX_ADDED_DEV; i++) { + if (memcmp(&(btif_hh_cb.added_devices[i].bd_addr), &bda, BD_ADDR_LEN) == + 0) { + BTIF_TRACE_WARNING(" Device %02X:%02X:%02X:%02X:%02X:%02X already added", + bda.address[0], bda.address[1], bda.address[2], + bda.address[3], bda.address[4], bda.address[5]); + return false; } - for (i = 0; i < BTIF_HH_MAX_ADDED_DEV; i++) { - if (btif_hh_cb.added_devices[i].bd_addr.address[0] == 0 && - btif_hh_cb.added_devices[i].bd_addr.address[1] == 0 && - btif_hh_cb.added_devices[i].bd_addr.address[2] == 0 && - btif_hh_cb.added_devices[i].bd_addr.address[3] == 0 && - btif_hh_cb.added_devices[i].bd_addr.address[4] == 0 && - btif_hh_cb.added_devices[i].bd_addr.address[5] == 0) - { - BTIF_TRACE_WARNING(" Added device %02X:%02X:%02X:%02X:%02X:%02X", - bda.address[0], bda.address[1], bda.address[2], bda.address[3], bda.address[4], bda.address[5]); - memcpy(&(btif_hh_cb.added_devices[i].bd_addr), &bda, BD_ADDR_LEN); - btif_hh_cb.added_devices[i].dev_handle = BTA_HH_INVALID_HANDLE; - btif_hh_cb.added_devices[i].attr_mask = attr_mask; - return true; - } + } + for (i = 0; i < BTIF_HH_MAX_ADDED_DEV; i++) { + if (btif_hh_cb.added_devices[i].bd_addr.address[0] == 0 && + btif_hh_cb.added_devices[i].bd_addr.address[1] == 0 && + btif_hh_cb.added_devices[i].bd_addr.address[2] == 0 && + btif_hh_cb.added_devices[i].bd_addr.address[3] == 0 && + btif_hh_cb.added_devices[i].bd_addr.address[4] == 0 && + btif_hh_cb.added_devices[i].bd_addr.address[5] == 0) { + BTIF_TRACE_WARNING(" Added device %02X:%02X:%02X:%02X:%02X:%02X", + bda.address[0], bda.address[1], bda.address[2], + bda.address[3], bda.address[4], bda.address[5]); + memcpy(&(btif_hh_cb.added_devices[i].bd_addr), &bda, BD_ADDR_LEN); + btif_hh_cb.added_devices[i].dev_handle = BTA_HH_INVALID_HANDLE; + btif_hh_cb.added_devices[i].attr_mask = attr_mask; + return true; } + } - BTIF_TRACE_WARNING("%s: Error, out of space to add device",__func__); - return false; + BTIF_TRACE_WARNING("%s: Error, out of space to add device", __func__); + return false; } /******************************************************************************* @@ -473,1199 +463,1191 @@ bool btif_hh_add_added_dev(bt_bdaddr_t bda, tBTA_HH_ATTR_MASK attr_mask) ** ** Returns void *******************************************************************************/ -void btif_hh_remove_device(bt_bdaddr_t bd_addr) -{ - int i; - btif_hh_device_t *p_dev; - btif_hh_added_device_t *p_added_dev; - - LOG_INFO(LOG_TAG, "%s: bda = %02x:%02x:%02x:%02x:%02x:%02x", __func__, - bd_addr.address[0], bd_addr.address[1], bd_addr.address[2], bd_addr.address[3], bd_addr.address[4], bd_addr.address[5]); - - for (i = 0; i < BTIF_HH_MAX_ADDED_DEV; i++) { - p_added_dev = &btif_hh_cb.added_devices[i]; - if (memcmp(&(p_added_dev->bd_addr),&bd_addr, 6) == 0) { - BTA_HhRemoveDev(p_added_dev->dev_handle); - btif_storage_remove_hid_info(&(p_added_dev->bd_addr)); - memset(&(p_added_dev->bd_addr), 0, 6); - p_added_dev->dev_handle = BTA_HH_INVALID_HANDLE; - break; - } - } - - p_dev = btif_hh_find_dev_by_bda(&bd_addr); - if (p_dev == NULL) { - BTIF_TRACE_WARNING(" Oops, can't find device [%02x:%02x:%02x:%02x:%02x:%02x]", - bd_addr.address[0], bd_addr.address[1], bd_addr.address[2], bd_addr.address[3], bd_addr.address[4], bd_addr.address[5]); - return; - } - - /* need to notify up-layer device is disconnected to avoid state out of sync with up-layer */ - HAL_CBACK(bt_hh_callbacks, connection_state_cb, &(p_dev->bd_addr), BTHH_CONN_STATE_DISCONNECTED); - - p_dev->dev_status = BTHH_CONN_STATE_UNKNOWN; - p_dev->dev_handle = BTA_HH_INVALID_HANDLE; - p_dev->ready_for_data = false; - - if (btif_hh_cb.device_num > 0) { - btif_hh_cb.device_num--; - } - else { - BTIF_TRACE_WARNING("%s: device_num = 0", __func__); - } - - p_dev->hh_keep_polling = 0; - p_dev->hh_poll_thread_id = -1; - BTIF_TRACE_DEBUG("%s: uhid fd = %d", __func__, p_dev->fd); - if (p_dev->fd >= 0) { - bta_hh_co_destroy(p_dev->fd); - p_dev->fd = -1; +void btif_hh_remove_device(bt_bdaddr_t bd_addr) { + int i; + btif_hh_device_t* p_dev; + btif_hh_added_device_t* p_added_dev; + + LOG_INFO(LOG_TAG, "%s: bda = %02x:%02x:%02x:%02x:%02x:%02x", __func__, + bd_addr.address[0], bd_addr.address[1], bd_addr.address[2], + bd_addr.address[3], bd_addr.address[4], bd_addr.address[5]); + + for (i = 0; i < BTIF_HH_MAX_ADDED_DEV; i++) { + p_added_dev = &btif_hh_cb.added_devices[i]; + if (memcmp(&(p_added_dev->bd_addr), &bd_addr, 6) == 0) { + BTA_HhRemoveDev(p_added_dev->dev_handle); + btif_storage_remove_hid_info(&(p_added_dev->bd_addr)); + memset(&(p_added_dev->bd_addr), 0, 6); + p_added_dev->dev_handle = BTA_HH_INVALID_HANDLE; + break; } + } + + p_dev = btif_hh_find_dev_by_bda(&bd_addr); + if (p_dev == NULL) { + BTIF_TRACE_WARNING( + " Oops, can't find device [%02x:%02x:%02x:%02x:%02x:%02x]", + bd_addr.address[0], bd_addr.address[1], bd_addr.address[2], + bd_addr.address[3], bd_addr.address[4], bd_addr.address[5]); + return; + } + + /* need to notify up-layer device is disconnected to avoid state out of sync + * with up-layer */ + HAL_CBACK(bt_hh_callbacks, connection_state_cb, &(p_dev->bd_addr), + BTHH_CONN_STATE_DISCONNECTED); + + p_dev->dev_status = BTHH_CONN_STATE_UNKNOWN; + p_dev->dev_handle = BTA_HH_INVALID_HANDLE; + p_dev->ready_for_data = false; + + if (btif_hh_cb.device_num > 0) { + btif_hh_cb.device_num--; + } else { + BTIF_TRACE_WARNING("%s: device_num = 0", __func__); + } + + p_dev->hh_keep_polling = 0; + p_dev->hh_poll_thread_id = -1; + BTIF_TRACE_DEBUG("%s: uhid fd = %d", __func__, p_dev->fd); + if (p_dev->fd >= 0) { + bta_hh_co_destroy(p_dev->fd); + p_dev->fd = -1; + } } -bool btif_hh_copy_hid_info(tBTA_HH_DEV_DSCP_INFO* dest , tBTA_HH_DEV_DSCP_INFO* src) -{ - dest->descriptor.dl_len = 0; - if (src->descriptor.dl_len > 0) { - dest->descriptor.dsc_list = (uint8_t *)osi_malloc(src->descriptor.dl_len); - } - memcpy(dest->descriptor.dsc_list, src->descriptor.dsc_list, src->descriptor.dl_len); - dest->descriptor.dl_len = src->descriptor.dl_len; - dest->vendor_id = src->vendor_id; - dest->product_id = src->product_id; - dest->version = src->version; - dest->ctry_code = src->ctry_code; - dest->ssr_max_latency = src->ssr_max_latency; - dest->ssr_min_tout = src->ssr_min_tout; - return true; +bool btif_hh_copy_hid_info(tBTA_HH_DEV_DSCP_INFO* dest, + tBTA_HH_DEV_DSCP_INFO* src) { + dest->descriptor.dl_len = 0; + if (src->descriptor.dl_len > 0) { + dest->descriptor.dsc_list = (uint8_t*)osi_malloc(src->descriptor.dl_len); + } + memcpy(dest->descriptor.dsc_list, src->descriptor.dsc_list, + src->descriptor.dl_len); + dest->descriptor.dl_len = src->descriptor.dl_len; + dest->vendor_id = src->vendor_id; + dest->product_id = src->product_id; + dest->version = src->version; + dest->ctry_code = src->ctry_code; + dest->ssr_max_latency = src->ssr_max_latency; + dest->ssr_min_tout = src->ssr_min_tout; + return true; } /******************************************************************************* -** -** Function btif_hh_virtual_unplug -** -** Description Virtual unplug initiated from the BTIF thread context -** Special handling for HID mouse- -** -** Returns void -** -*******************************************************************************/ - -bt_status_t btif_hh_virtual_unplug(bt_bdaddr_t *bd_addr) -{ - BTIF_TRACE_DEBUG("%s", __func__); - btif_hh_device_t *p_dev; - char bd_str[18]; - snprintf(bd_str, sizeof(bd_str), "%02X:%02X:%02X:%02X:%02X:%02X", - bd_addr->address[0], bd_addr->address[1], bd_addr->address[2], bd_addr->address[3], - bd_addr->address[4], bd_addr->address[5]); - p_dev = btif_hh_find_dev_by_bda(bd_addr); - if ((p_dev != NULL) && (p_dev->dev_status == BTHH_CONN_STATE_CONNECTED) - && (p_dev->attr_mask & HID_VIRTUAL_CABLE)) - { - BTIF_TRACE_DEBUG("%s Sending BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG", __func__); - /* start the timer */ - btif_hh_start_vup_timer(bd_addr); - p_dev->local_vup = true; - BTA_HhSendCtrl(p_dev->dev_handle, BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG); - return BT_STATUS_SUCCESS; - } - else - { - BTIF_TRACE_ERROR("%s: Error, device %s not opened.", __func__, bd_str); - return BT_STATUS_FAIL; - } + * + * Function btif_hh_virtual_unplug + * + * Description Virtual unplug initiated from the BTIF thread context + * Special handling for HID mouse- + * + * Returns void + * + ******************************************************************************/ + +bt_status_t btif_hh_virtual_unplug(bt_bdaddr_t* bd_addr) { + BTIF_TRACE_DEBUG("%s", __func__); + btif_hh_device_t* p_dev; + char bd_str[18]; + snprintf(bd_str, sizeof(bd_str), "%02X:%02X:%02X:%02X:%02X:%02X", + bd_addr->address[0], bd_addr->address[1], bd_addr->address[2], + bd_addr->address[3], bd_addr->address[4], bd_addr->address[5]); + p_dev = btif_hh_find_dev_by_bda(bd_addr); + if ((p_dev != NULL) && (p_dev->dev_status == BTHH_CONN_STATE_CONNECTED) && + (p_dev->attr_mask & HID_VIRTUAL_CABLE)) { + BTIF_TRACE_DEBUG("%s Sending BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG", __func__); + /* start the timer */ + btif_hh_start_vup_timer(bd_addr); + p_dev->local_vup = true; + BTA_HhSendCtrl(p_dev->dev_handle, BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG); + return BT_STATUS_SUCCESS; + } else { + BTIF_TRACE_ERROR("%s: Error, device %s not opened.", __func__, bd_str); + return BT_STATUS_FAIL; + } } /******************************************************************************* -** -** Function btif_hh_connect -** -** Description connection initiated from the BTIF thread context -** -** Returns int status -** -*******************************************************************************/ - -bt_status_t btif_hh_connect(bt_bdaddr_t *bd_addr) -{ - btif_hh_device_t *dev; - btif_hh_added_device_t *added_dev = NULL; - char bda_str[20]; - int i; - BD_ADDR *bda = (BD_ADDR*)bd_addr; - CHECK_BTHH_INIT(); - dev = btif_hh_find_dev_by_bda(bd_addr); - BTIF_TRACE_DEBUG("Connect _hh"); - snprintf(bda_str, sizeof(bda_str), "%02X:%02X:%02X:%02X:%02X:%02X", - (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); - if (dev == NULL && btif_hh_cb.device_num >= BTIF_HH_MAX_HID) { - // No space for more HID device now. - BTIF_TRACE_WARNING("%s: Error, exceeded the maximum supported HID device number %d", - __func__, BTIF_HH_MAX_HID); - return BT_STATUS_FAIL; - } + * + * Function btif_hh_connect + * + * Description connection initiated from the BTIF thread context + * + * Returns int status + * + ******************************************************************************/ - for (i = 0; i < BTIF_HH_MAX_ADDED_DEV; i++) { - if (memcmp(&(btif_hh_cb.added_devices[i].bd_addr), bd_addr, BD_ADDR_LEN) == 0) { - added_dev = &btif_hh_cb.added_devices[i]; - BTIF_TRACE_WARNING("%s: Device %s already added, attr_mask = 0x%x", - __func__, bda_str, added_dev->attr_mask); - } +bt_status_t btif_hh_connect(bt_bdaddr_t* bd_addr) { + btif_hh_device_t* dev; + btif_hh_added_device_t* added_dev = NULL; + char bda_str[20]; + int i; + BD_ADDR* bda = (BD_ADDR*)bd_addr; + CHECK_BTHH_INIT(); + dev = btif_hh_find_dev_by_bda(bd_addr); + BTIF_TRACE_DEBUG("Connect _hh"); + snprintf(bda_str, sizeof(bda_str), "%02X:%02X:%02X:%02X:%02X:%02X", (*bda)[0], + (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); + if (dev == NULL && btif_hh_cb.device_num >= BTIF_HH_MAX_HID) { + // No space for more HID device now. + BTIF_TRACE_WARNING( + "%s: Error, exceeded the maximum supported HID device number %d", + __func__, BTIF_HH_MAX_HID); + return BT_STATUS_FAIL; + } + + for (i = 0; i < BTIF_HH_MAX_ADDED_DEV; i++) { + if (memcmp(&(btif_hh_cb.added_devices[i].bd_addr), bd_addr, BD_ADDR_LEN) == + 0) { + added_dev = &btif_hh_cb.added_devices[i]; + BTIF_TRACE_WARNING("%s: Device %s already added, attr_mask = 0x%x", + __func__, bda_str, added_dev->attr_mask); } - - if (added_dev != NULL) { - if (added_dev->dev_handle == BTA_HH_INVALID_HANDLE) { - // No space for more HID device now. - BTIF_TRACE_ERROR("%s: Error, device %s added but addition failed", __func__, bda_str); - memset(&(added_dev->bd_addr), 0, 6); - added_dev->dev_handle = BTA_HH_INVALID_HANDLE; - return BT_STATUS_FAIL; - } + } + + if (added_dev != NULL) { + if (added_dev->dev_handle == BTA_HH_INVALID_HANDLE) { + // No space for more HID device now. + BTIF_TRACE_ERROR("%s: Error, device %s added but addition failed", + __func__, bda_str); + memset(&(added_dev->bd_addr), 0, 6); + added_dev->dev_handle = BTA_HH_INVALID_HANDLE; + return BT_STATUS_FAIL; } - - /* Not checking the NORMALLY_Connectible flags from sdp record, and anyways sending this - request from host, for subsequent user initiated connection. If the remote is not in - pagescan mode, we will do 2 retries to connect before giving up */ - tBTA_SEC sec_mask = BTUI_HH_SECURITY; - btif_hh_cb.status = BTIF_HH_DEV_CONNECTING; - BTA_HhOpen(*bda, BTA_HH_PROTO_RPT_MODE, sec_mask); - - HAL_CBACK(bt_hh_callbacks, connection_state_cb, bd_addr, BTHH_CONN_STATE_CONNECTING); - return BT_STATUS_SUCCESS; + } + + /* Not checking the NORMALLY_Connectible flags from sdp record, and anyways + sending this + request from host, for subsequent user initiated connection. If the remote is + not in + pagescan mode, we will do 2 retries to connect before giving up */ + tBTA_SEC sec_mask = BTUI_HH_SECURITY; + btif_hh_cb.status = BTIF_HH_DEV_CONNECTING; + BTA_HhOpen(*bda, BTA_HH_PROTO_RPT_MODE, sec_mask); + + HAL_CBACK(bt_hh_callbacks, connection_state_cb, bd_addr, + BTHH_CONN_STATE_CONNECTING); + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_hh_disconnect -** -** Description disconnection initiated from the BTIF thread context -** -** Returns void -** -*******************************************************************************/ - -void btif_hh_disconnect(bt_bdaddr_t *bd_addr) -{ - btif_hh_device_t *p_dev; - p_dev = btif_hh_find_connected_dev_by_bda(bd_addr); - if (p_dev != NULL) - { - BTA_HhClose(p_dev->dev_handle); - } - else - BTIF_TRACE_DEBUG("%s-- Error: device not connected:",__func__); + * + * Function btif_hh_disconnect + * + * Description disconnection initiated from the BTIF thread context + * + * Returns void + * + ******************************************************************************/ + +void btif_hh_disconnect(bt_bdaddr_t* bd_addr) { + btif_hh_device_t* p_dev; + p_dev = btif_hh_find_connected_dev_by_bda(bd_addr); + if (p_dev != NULL) { + BTA_HhClose(p_dev->dev_handle); + } else + BTIF_TRACE_DEBUG("%s-- Error: device not connected:", __func__); } /******************************************************************************* -** -** Function btif_btif_hh_setreport -** -** Description setreport initiated from the BTIF thread context -** -** Returns void -** -*******************************************************************************/ -void btif_hh_setreport(btif_hh_device_t *p_dev, bthh_report_type_t r_type, uint16_t size, - uint8_t* report) -{ - BT_HDR* p_buf = create_pbuf(size, report); - if (p_buf == NULL) { - APPL_TRACE_ERROR("%s: Error, failed to allocate RPT buffer, size = %d", __func__, size); - return; - } - BTA_HhSetReport(p_dev->dev_handle, r_type, p_buf); + * + * Function btif_btif_hh_setreport + * + * Description setreport initiated from the BTIF thread context + * + * Returns void + * + ******************************************************************************/ +void btif_hh_setreport(btif_hh_device_t* p_dev, bthh_report_type_t r_type, + uint16_t size, uint8_t* report) { + BT_HDR* p_buf = create_pbuf(size, report); + if (p_buf == NULL) { + APPL_TRACE_ERROR("%s: Error, failed to allocate RPT buffer, size = %d", + __func__, size); + return; + } + BTA_HhSetReport(p_dev->dev_handle, r_type, p_buf); } /***************************************************************************** -** Section name (Group of functions) -*****************************************************************************/ + * Section name (Group of functions) + ****************************************************************************/ /***************************************************************************** -** -** btif hh api functions (no context switch) -** -*****************************************************************************/ + * + * btif hh api functions (no context switch) + * + ****************************************************************************/ /******************************************************************************* -** -** Function btif_hh_upstreams_evt -** -** Description Executes HH UPSTREAMS events in btif context -** -** Returns void -** -*******************************************************************************/ -static void btif_hh_upstreams_evt(uint16_t event, char* p_param) -{ - tBTA_HH *p_data = (tBTA_HH *)p_param; - btif_hh_device_t *p_dev = NULL; - int i; - int len, tmplen; - - BTIF_TRACE_DEBUG("%s: event=%s", __func__, dump_hh_event(event)); - - switch (event) - { - case BTA_HH_ENABLE_EVT: - BTIF_TRACE_DEBUG("%s: BTA_HH_ENABLE_EVT: status =%d",__func__, p_data->status); - if (p_data->status == BTA_HH_OK) { - btif_hh_cb.status = BTIF_HH_ENABLED; - BTIF_TRACE_DEBUG("%s--Loading added devices",__func__); - /* Add hid descriptors for already bonded hid devices*/ - btif_storage_load_bonded_hid_info(); - } - else { - btif_hh_cb.status = BTIF_HH_DISABLED; - BTIF_TRACE_WARNING("BTA_HH_ENABLE_EVT: Error, HH enabling failed, status = %d", p_data->status); - } - break; - - case BTA_HH_DISABLE_EVT: - btif_hh_cb.status = BTIF_HH_DISABLED; - if (p_data->status == BTA_HH_OK) { - int i; - // Clear the control block - for (i = 0; i < BTIF_HH_MAX_HID; i++) { - alarm_free(btif_hh_cb.devices[i].vup_timer); - } - memset(&btif_hh_cb, 0, sizeof(btif_hh_cb)); - for (i = 0; i < BTIF_HH_MAX_HID; i++) { - btif_hh_cb.devices[i].dev_status = BTHH_CONN_STATE_UNKNOWN; - } - } - else - BTIF_TRACE_WARNING("BTA_HH_DISABLE_EVT: Error, HH disabling failed, status = %d", p_data->status); - break; - - case BTA_HH_OPEN_EVT: - BTIF_TRACE_WARNING("%s: BTA_HH_OPN_EVT: handle=%d, status =%d",__func__, p_data->conn.handle, p_data->conn.status); - if (p_data->conn.status == BTA_HH_OK) { - p_dev = btif_hh_find_connected_dev_by_handle(p_data->conn.handle); - if (p_dev == NULL) { - BTIF_TRACE_WARNING("BTA_HH_OPEN_EVT: Error, cannot find device with handle %d", p_data->conn.handle); - btif_hh_cb.status = (BTIF_HH_STATUS)BTIF_HH_DEV_DISCONNECTED; - // The connect request must come from device side and exceeded the connected - // HID device number. - BTA_HhClose(p_data->conn.handle); - HAL_CBACK(bt_hh_callbacks, connection_state_cb, (bt_bdaddr_t*) &p_data->conn.bda,BTHH_CONN_STATE_DISCONNECTED); - } - else if (p_dev->fd < 0) { - BTIF_TRACE_WARNING("BTA_HH_OPEN_EVT: Error, failed to find the uhid driver..."); - memcpy(&(p_dev->bd_addr), p_data->conn.bda, BD_ADDR_LEN); - //remove the connection and then try again to reconnect from the mouse side to recover - btif_hh_cb.status = (BTIF_HH_STATUS)BTIF_HH_DEV_DISCONNECTED; - BTA_HhClose(p_data->conn.handle); - } - else { - BTIF_TRACE_WARNING("BTA_HH_OPEN_EVT: Found device...Getting dscp info for handle ... %d",p_data->conn.handle); - memcpy(&(p_dev->bd_addr), p_data->conn.bda, BD_ADDR_LEN); - btif_hh_cb.status = (BTIF_HH_STATUS)BTIF_HH_DEV_CONNECTED; - // Send set_idle if the peer_device is a keyboard - if (check_cod((bt_bdaddr_t*)p_data->conn.bda, COD_HID_KEYBOARD )|| - check_cod((bt_bdaddr_t*)p_data->conn.bda, COD_HID_COMBO)) - BTA_HhSetIdle(p_data->conn.handle, 0); - btif_hh_cb.p_curr_dev = btif_hh_find_connected_dev_by_handle(p_data->conn.handle); - BTA_HhGetDscpInfo(p_data->conn.handle); - p_dev->dev_status = BTHH_CONN_STATE_CONNECTED; - HAL_CBACK(bt_hh_callbacks, connection_state_cb,&(p_dev->bd_addr), p_dev->dev_status); - } - } - else { - bt_bdaddr_t *bdaddr = (bt_bdaddr_t*)p_data->conn.bda; - btif_dm_hh_open_failed(bdaddr); - p_dev = btif_hh_find_dev_by_bda(bdaddr); - if (p_dev != NULL) { - btif_hh_stop_vup_timer(&(p_dev->bd_addr)); - if (p_dev->fd >= 0) { - bta_hh_co_destroy(p_dev->fd); - p_dev->fd = -1; - } - p_dev->dev_status = BTHH_CONN_STATE_DISCONNECTED; - } - HAL_CBACK(bt_hh_callbacks, connection_state_cb, (bt_bdaddr_t*) &p_data->conn.bda,BTHH_CONN_STATE_DISCONNECTED); - btif_hh_cb.status = (BTIF_HH_STATUS)BTIF_HH_DEV_DISCONNECTED; - } - break; - - case BTA_HH_CLOSE_EVT: - BTIF_TRACE_DEBUG("BTA_HH_CLOSE_EVT: status = %d, handle = %d", - p_data->dev_status.status, p_data->dev_status.handle); - p_dev = btif_hh_find_connected_dev_by_handle(p_data->dev_status.handle); - if (p_dev != NULL) { - BTIF_TRACE_DEBUG("%s: uhid fd=%d local_vup=%d", __func__, p_dev->fd, p_dev->local_vup); - btif_hh_stop_vup_timer(&(p_dev->bd_addr)); - /* If this is a locally initiated VUP, remove the bond as ACL got - * disconnected while VUP being processed. - */ - if (p_dev->local_vup) - { - p_dev->local_vup = false; - BTA_DmRemoveDevice((uint8_t *)p_dev->bd_addr.address); - } - - btif_hh_cb.status = (BTIF_HH_STATUS)BTIF_HH_DEV_DISCONNECTED; - p_dev->dev_status = BTHH_CONN_STATE_DISCONNECTED; - - if (p_dev->fd >= 0) { - bta_hh_co_destroy(p_dev->fd); - p_dev->fd = -1; - } - HAL_CBACK(bt_hh_callbacks, connection_state_cb,&(p_dev->bd_addr), p_dev->dev_status); - } - else { - BTIF_TRACE_WARNING("Error: cannot find device with handle %d", p_data->dev_status.handle); - } - break; - - case BTA_HH_GET_RPT_EVT: { - BT_HDR *hdr = p_data->hs_data.rsp_data.p_rpt_data; - uint8_t *data = NULL; - uint16_t len = 0; - - BTIF_TRACE_DEBUG("BTA_HH_GET_RPT_EVT: status = %d, handle = %d", - p_data->hs_data.status, p_data->hs_data.handle); - p_dev = btif_hh_find_connected_dev_by_handle(p_data->hs_data.handle); - if (p_dev) { - /* p_rpt_data is NULL in HANDSHAKE response case */ - if (hdr) { - data = (uint8_t *)(hdr + 1) + hdr->offset; - len = hdr->len; - HAL_CBACK(bt_hh_callbacks, get_report_cb, - (bt_bdaddr_t*) &(p_dev->bd_addr), - (bthh_status_t) p_data->hs_data.status, data, len); - } else { - HAL_CBACK(bt_hh_callbacks, handshake_cb, - (bt_bdaddr_t*) &(p_dev->bd_addr), - (bthh_status_t) p_data->hs_data.status); - } - } else { - BTIF_TRACE_WARNING("Error: cannot find device with handle %d", p_data->hs_data.handle); - } - break; + * + * Function btif_hh_upstreams_evt + * + * Description Executes HH UPSTREAMS events in btif context + * + * Returns void + * + ******************************************************************************/ +static void btif_hh_upstreams_evt(uint16_t event, char* p_param) { + tBTA_HH* p_data = (tBTA_HH*)p_param; + btif_hh_device_t* p_dev = NULL; + int i; + int len, tmplen; + + BTIF_TRACE_DEBUG("%s: event=%s", __func__, dump_hh_event(event)); + + switch (event) { + case BTA_HH_ENABLE_EVT: + BTIF_TRACE_DEBUG("%s: BTA_HH_ENABLE_EVT: status =%d", __func__, + p_data->status); + if (p_data->status == BTA_HH_OK) { + btif_hh_cb.status = BTIF_HH_ENABLED; + BTIF_TRACE_DEBUG("%s--Loading added devices", __func__); + /* Add hid descriptors for already bonded hid devices*/ + btif_storage_load_bonded_hid_info(); + } else { + btif_hh_cb.status = BTIF_HH_DISABLED; + BTIF_TRACE_WARNING( + "BTA_HH_ENABLE_EVT: Error, HH enabling failed, status = %d", + p_data->status); + } + break; + + case BTA_HH_DISABLE_EVT: + btif_hh_cb.status = BTIF_HH_DISABLED; + if (p_data->status == BTA_HH_OK) { + int i; + // Clear the control block + for (i = 0; i < BTIF_HH_MAX_HID; i++) { + alarm_free(btif_hh_cb.devices[i].vup_timer); } - - case BTA_HH_SET_RPT_EVT: - BTIF_TRACE_DEBUG("BTA_HH_SET_RPT_EVT: status = %d, handle = %d", - p_data->dev_status.status, p_data->dev_status.handle); - p_dev = btif_hh_find_connected_dev_by_handle(p_data->dev_status.handle); - if (p_dev != NULL) { - HAL_CBACK(bt_hh_callbacks, handshake_cb, - (bt_bdaddr_t*) &(p_dev->bd_addr), - (bthh_status_t) p_data->hs_data.status); - } - break; - - case BTA_HH_GET_PROTO_EVT: - p_dev = btif_hh_find_connected_dev_by_handle(p_data->dev_status.handle); - BTIF_TRACE_WARNING("BTA_HH_GET_PROTO_EVT: status = %d, handle = %d, proto = [%d], %s", - p_data->hs_data.status, p_data->hs_data.handle, - p_data->hs_data.rsp_data.proto_mode, - (p_data->hs_data.rsp_data.proto_mode == BTA_HH_PROTO_RPT_MODE) ? "Report Mode" : - (p_data->hs_data.rsp_data.proto_mode == BTA_HH_PROTO_BOOT_MODE) ? "Boot Mode" : "Unsupported"); - if (p_data->hs_data.rsp_data.proto_mode != BTA_HH_PROTO_UNKNOWN) { - HAL_CBACK(bt_hh_callbacks, protocol_mode_cb, - (bt_bdaddr_t*) &(p_dev->bd_addr), - (bthh_status_t)p_data->hs_data.status, - (bthh_protocol_mode_t) p_data->hs_data.rsp_data.proto_mode); - } else { - HAL_CBACK(bt_hh_callbacks, handshake_cb, - (bt_bdaddr_t*) &(p_dev->bd_addr), - (bthh_status_t)p_data->hs_data.status); - } - break; - - case BTA_HH_SET_PROTO_EVT: - BTIF_TRACE_DEBUG("BTA_HH_SET_PROTO_EVT: status = %d, handle = %d", - p_data->dev_status.status, p_data->dev_status.handle); - p_dev = btif_hh_find_connected_dev_by_handle(p_data->dev_status.handle); - if (p_dev) { - HAL_CBACK(bt_hh_callbacks, handshake_cb, - (bt_bdaddr_t*)&(p_dev->bd_addr), - (bthh_status_t)p_data->hs_data.status); - } - break; - - case BTA_HH_GET_IDLE_EVT: - BTIF_TRACE_DEBUG("BTA_HH_GET_IDLE_EVT: handle = %d, status = %d, rate = %d", - p_data->hs_data.handle, p_data->hs_data.status, - p_data->hs_data.rsp_data.idle_rate); - break; - - case BTA_HH_SET_IDLE_EVT: - BTIF_TRACE_DEBUG("BTA_HH_SET_IDLE_EVT: status = %d, handle = %d", - p_data->dev_status.status, p_data->dev_status.handle); - break; - - case BTA_HH_GET_DSCP_EVT: - len = p_data->dscp_info.descriptor.dl_len; - BTIF_TRACE_DEBUG("BTA_HH_GET_DSCP_EVT: len = %d", len); - p_dev = btif_hh_cb.p_curr_dev; - if (p_dev == NULL) { - BTIF_TRACE_ERROR("BTA_HH_GET_DSCP_EVT: No HID device is currently connected"); - return; - } - if (p_dev->fd < 0) { - LOG_ERROR(LOG_TAG, "BTA_HH_GET_DSCP_EVT: Error, failed to find the uhid driver..."); - return; - } - { - const char *cached_name = NULL; - bt_bdname_t bdname; - bt_property_t prop_name; - BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME, - sizeof(bt_bdname_t), &bdname); - if (btif_storage_get_remote_device_property( - &p_dev->bd_addr, &prop_name) == BT_STATUS_SUCCESS) - { - cached_name = (char *)bdname.name; - } - else - { - cached_name = "Bluetooth HID"; - } - - BTIF_TRACE_WARNING("%s: name = %s", __func__, cached_name); - bta_hh_co_send_hid_info(p_dev, cached_name, - p_data->dscp_info.vendor_id, p_data->dscp_info.product_id, - p_data->dscp_info.version, p_data->dscp_info.ctry_code, - len, p_data->dscp_info.descriptor.dsc_list); - if (btif_hh_add_added_dev(p_dev->bd_addr, p_dev->attr_mask)) { - BD_ADDR bda; - bdcpy(bda, p_dev->bd_addr.address); - tBTA_HH_DEV_DSCP_INFO dscp_info; - bt_status_t ret; - bdcpy(bda, p_dev->bd_addr.address); - btif_hh_copy_hid_info(&dscp_info, &p_data->dscp_info); - BTIF_TRACE_DEBUG("BTA_HH_GET_DSCP_EVT:bda = %02x:%02x:%02x:%02x:%02x:%02x", - p_dev->bd_addr.address[0], p_dev->bd_addr.address[1], - p_dev->bd_addr.address[2],p_dev->bd_addr.address[3], - p_dev->bd_addr.address[4], p_dev->bd_addr.address[5]); - BTA_HhAddDev(bda, p_dev->attr_mask,p_dev->sub_class,p_dev->app_id, dscp_info); - // write hid info to nvram - ret = btif_storage_add_hid_device_info(&(p_dev->bd_addr), p_dev->attr_mask,p_dev->sub_class,p_dev->app_id, - p_data->dscp_info.vendor_id, p_data->dscp_info.product_id, - p_data->dscp_info.version, p_data->dscp_info.ctry_code, - p_data->dscp_info.ssr_max_latency, p_data->dscp_info.ssr_min_tout, - len, p_data->dscp_info.descriptor.dsc_list); - - ASSERTC(ret == BT_STATUS_SUCCESS, "storing hid info failed", ret); - BTIF_TRACE_WARNING("BTA_HH_GET_DSCP_EVT: Called add device"); - - //Free buffer created for dscp_info; - if (dscp_info.descriptor.dl_len >0 && dscp_info.descriptor.dsc_list != NULL) - { - osi_free_and_reset((void **)&dscp_info.descriptor.dsc_list); - dscp_info.descriptor.dl_len = 0; - } - } - else { - //Device already added. - BTIF_TRACE_WARNING("%s: Device already added ",__func__); - } - /*Sync HID Keyboard lockstates */ - tmplen = sizeof(hid_kb_numlock_on_list) - / sizeof(tHID_KB_LIST); - for(i = 0; i< tmplen; i++) - { - if(p_data->dscp_info.vendor_id - == hid_kb_numlock_on_list[i].version_id && - p_data->dscp_info.product_id - == hid_kb_numlock_on_list[i].product_id) - { - BTIF_TRACE_DEBUG("%s() idx[%d] Enabling "\ - "NUMLOCK for device :: %s", __func__, - i, hid_kb_numlock_on_list[i].kb_name); - /* Enable NUMLOCK by default so that numeric - keys work from first keyboard connect */ - set_keylockstate(BTIF_HH_KEYSTATE_MASK_NUMLOCK, - true); - sync_lockstate_on_connect(p_dev); - /* End Sync HID Keyboard lockstates */ - break; - } - } - } - break; - - case BTA_HH_ADD_DEV_EVT: - BTIF_TRACE_WARNING("BTA_HH_ADD_DEV_EVT: status = %d, handle = %d",p_data->dev_info.status, p_data->dev_info.handle); - int i; - for (i = 0; i < BTIF_HH_MAX_ADDED_DEV; i++) { - if (memcmp(btif_hh_cb.added_devices[i].bd_addr.address, p_data->dev_info.bda, 6) == 0) { - if (p_data->dev_info.status == BTA_HH_OK) { - btif_hh_cb.added_devices[i].dev_handle = p_data->dev_info.handle; - } - else { - memset(btif_hh_cb.added_devices[i].bd_addr.address, 0, 6); - btif_hh_cb.added_devices[i].dev_handle = BTA_HH_INVALID_HANDLE; - } - break; - } - } - break; - case BTA_HH_RMV_DEV_EVT: - BTIF_TRACE_DEBUG("BTA_HH_RMV_DEV_EVT: status = %d, handle = %d", - p_data->dev_info.status, p_data->dev_info.handle); - BTIF_TRACE_DEBUG("BTA_HH_RMV_DEV_EVT:bda = %02x:%02x:%02x:%02x:%02x:%02x", - p_data->dev_info.bda[0], p_data->dev_info.bda[1], p_data->dev_info.bda[2], - p_data->dev_info.bda[3], p_data->dev_info.bda[4], p_data->dev_info.bda[5]); - break; - - case BTA_HH_VC_UNPLUG_EVT: - BTIF_TRACE_DEBUG("BTA_HH_VC_UNPLUG_EVT: status = %d, handle = %d", - p_data->dev_status.status, p_data->dev_status.handle); - p_dev = btif_hh_find_connected_dev_by_handle(p_data->dev_status.handle); - btif_hh_cb.status = (BTIF_HH_STATUS)BTIF_HH_DEV_DISCONNECTED; - if (p_dev != NULL) { - BTIF_TRACE_DEBUG("BTA_HH_VC_UNPLUG_EVT:bda = %02x:%02x:%02x:%02x:%02x:%02x", - p_dev->bd_addr.address[0], p_dev->bd_addr.address[1], - p_dev->bd_addr.address[2],p_dev->bd_addr.address[3], - p_dev->bd_addr.address[4], p_dev->bd_addr.address[5]); - /* Stop the VUP timer */ - btif_hh_stop_vup_timer(&(p_dev->bd_addr)); - p_dev->dev_status = BTHH_CONN_STATE_DISCONNECTED; - BTIF_TRACE_DEBUG("%s---Sending connection state change", __func__); - HAL_CBACK(bt_hh_callbacks, connection_state_cb,&(p_dev->bd_addr), p_dev->dev_status); - BTIF_TRACE_DEBUG("%s---Removing HID bond", __func__); - /* If it is locally initiated VUP or remote device has its major COD as - Peripheral removed the bond.*/ - if (p_dev->local_vup || check_cod_hid(&(p_dev->bd_addr))) - { - p_dev->local_vup = false; - BTA_DmRemoveDevice((uint8_t *)p_dev->bd_addr.address); - } - else - btif_hh_remove_device(p_dev->bd_addr); - HAL_CBACK(bt_hh_callbacks, virtual_unplug_cb,&(p_dev->bd_addr), - (bthh_status_t)p_data->dev_status.status); - } - break; - - case BTA_HH_API_ERR_EVT : - LOG_INFO(LOG_TAG, "BTA_HH API_ERR"); - break; - - default: - BTIF_TRACE_WARNING("%s: Unhandled event: %d", __func__, event); - break; + memset(&btif_hh_cb, 0, sizeof(btif_hh_cb)); + for (i = 0; i < BTIF_HH_MAX_HID; i++) { + btif_hh_cb.devices[i].dev_status = BTHH_CONN_STATE_UNKNOWN; + } + } else + BTIF_TRACE_WARNING( + "BTA_HH_DISABLE_EVT: Error, HH disabling failed, status = %d", + p_data->status); + break; + + case BTA_HH_OPEN_EVT: + BTIF_TRACE_WARNING("%s: BTA_HH_OPN_EVT: handle=%d, status =%d", __func__, + p_data->conn.handle, p_data->conn.status); + if (p_data->conn.status == BTA_HH_OK) { + p_dev = btif_hh_find_connected_dev_by_handle(p_data->conn.handle); + if (p_dev == NULL) { + BTIF_TRACE_WARNING( + "BTA_HH_OPEN_EVT: Error, cannot find device with handle %d", + p_data->conn.handle); + btif_hh_cb.status = (BTIF_HH_STATUS)BTIF_HH_DEV_DISCONNECTED; + // The connect request must come from device side and exceeded the + // connected + // HID device number. + BTA_HhClose(p_data->conn.handle); + HAL_CBACK(bt_hh_callbacks, connection_state_cb, + (bt_bdaddr_t*)&p_data->conn.bda, + BTHH_CONN_STATE_DISCONNECTED); + } else if (p_dev->fd < 0) { + BTIF_TRACE_WARNING( + "BTA_HH_OPEN_EVT: Error, failed to find the uhid driver..."); + memcpy(&(p_dev->bd_addr), p_data->conn.bda, BD_ADDR_LEN); + // remove the connection and then try again to reconnect from the + // mouse side to recover + btif_hh_cb.status = (BTIF_HH_STATUS)BTIF_HH_DEV_DISCONNECTED; + BTA_HhClose(p_data->conn.handle); + } else { + BTIF_TRACE_WARNING( + "BTA_HH_OPEN_EVT: Found device...Getting dscp info for handle " + "... %d", + p_data->conn.handle); + memcpy(&(p_dev->bd_addr), p_data->conn.bda, BD_ADDR_LEN); + btif_hh_cb.status = (BTIF_HH_STATUS)BTIF_HH_DEV_CONNECTED; + // Send set_idle if the peer_device is a keyboard + if (check_cod((bt_bdaddr_t*)p_data->conn.bda, COD_HID_KEYBOARD) || + check_cod((bt_bdaddr_t*)p_data->conn.bda, COD_HID_COMBO)) + BTA_HhSetIdle(p_data->conn.handle, 0); + btif_hh_cb.p_curr_dev = + btif_hh_find_connected_dev_by_handle(p_data->conn.handle); + BTA_HhGetDscpInfo(p_data->conn.handle); + p_dev->dev_status = BTHH_CONN_STATE_CONNECTED; + HAL_CBACK(bt_hh_callbacks, connection_state_cb, &(p_dev->bd_addr), + p_dev->dev_status); + } + } else { + bt_bdaddr_t* bdaddr = (bt_bdaddr_t*)p_data->conn.bda; + btif_dm_hh_open_failed(bdaddr); + p_dev = btif_hh_find_dev_by_bda(bdaddr); + if (p_dev != NULL) { + btif_hh_stop_vup_timer(&(p_dev->bd_addr)); + if (p_dev->fd >= 0) { + bta_hh_co_destroy(p_dev->fd); + p_dev->fd = -1; + } + p_dev->dev_status = BTHH_CONN_STATE_DISCONNECTED; + } + HAL_CBACK(bt_hh_callbacks, connection_state_cb, + (bt_bdaddr_t*)&p_data->conn.bda, + BTHH_CONN_STATE_DISCONNECTED); + btif_hh_cb.status = (BTIF_HH_STATUS)BTIF_HH_DEV_DISCONNECTED; + } + break; + + case BTA_HH_CLOSE_EVT: + BTIF_TRACE_DEBUG("BTA_HH_CLOSE_EVT: status = %d, handle = %d", + p_data->dev_status.status, p_data->dev_status.handle); + p_dev = btif_hh_find_connected_dev_by_handle(p_data->dev_status.handle); + if (p_dev != NULL) { + BTIF_TRACE_DEBUG("%s: uhid fd=%d local_vup=%d", __func__, p_dev->fd, + p_dev->local_vup); + btif_hh_stop_vup_timer(&(p_dev->bd_addr)); + /* If this is a locally initiated VUP, remove the bond as ACL got + * disconnected while VUP being processed. + */ + if (p_dev->local_vup) { + p_dev->local_vup = false; + BTA_DmRemoveDevice((uint8_t*)p_dev->bd_addr.address); } -} -/******************************************************************************* -** -** Function bte_hh_evt -** -** Description Switches context from BTE to BTIF for all HH events -** -** Returns void -** -*******************************************************************************/ - -static void bte_hh_evt(tBTA_HH_EVT event, tBTA_HH *p_data) -{ - bt_status_t status; - int param_len = 0; - - if (BTA_HH_ENABLE_EVT == event) - param_len = sizeof(tBTA_HH_STATUS); - else if (BTA_HH_OPEN_EVT == event) - param_len = sizeof(tBTA_HH_CONN); - else if (BTA_HH_DISABLE_EVT == event) - param_len = sizeof(tBTA_HH_STATUS); - else if (BTA_HH_CLOSE_EVT == event) - param_len = sizeof(tBTA_HH_CBDATA); - else if (BTA_HH_GET_DSCP_EVT == event) - param_len = sizeof(tBTA_HH_DEV_DSCP_INFO); - else if ((BTA_HH_GET_PROTO_EVT == event) || (BTA_HH_GET_RPT_EVT == event)|| (BTA_HH_GET_IDLE_EVT == event)) - param_len = sizeof(tBTA_HH_HSDATA); - else if ((BTA_HH_SET_PROTO_EVT == event) || (BTA_HH_SET_RPT_EVT == event) || (BTA_HH_VC_UNPLUG_EVT == event) || (BTA_HH_SET_IDLE_EVT == event)) - param_len = sizeof(tBTA_HH_CBDATA); - else if ((BTA_HH_ADD_DEV_EVT == event) || (BTA_HH_RMV_DEV_EVT == event) ) - param_len = sizeof(tBTA_HH_DEV_INFO); - else if (BTA_HH_API_ERR_EVT == event) - param_len = 0; - /* switch context to btif task context (copy full union size for convenience) */ - status = btif_transfer_context(btif_hh_upstreams_evt, (uint16_t)event, (char*)p_data, param_len, NULL); - - /* catch any failed context transfers */ - ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); -} + btif_hh_cb.status = (BTIF_HH_STATUS)BTIF_HH_DEV_DISCONNECTED; + p_dev->dev_status = BTHH_CONN_STATE_DISCONNECTED; -/******************************************************************************* -** -** Function btif_hh_handle_evt -** -** Description Switches context for immediate callback -** -** Returns void -** -*******************************************************************************/ - -static void btif_hh_handle_evt(uint16_t event, char *p_param) -{ - bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)p_param; - BTIF_TRACE_EVENT("%s: event=%d", __func__, event); - int ret; - switch(event) - { - case BTIF_HH_CONNECT_REQ_EVT: - { - ret = btif_hh_connect(bd_addr); - if(ret == BT_STATUS_SUCCESS) - { - HAL_CBACK(bt_hh_callbacks, connection_state_cb,bd_addr,BTHH_CONN_STATE_CONNECTING); - } - else - HAL_CBACK(bt_hh_callbacks, connection_state_cb,bd_addr,BTHH_CONN_STATE_DISCONNECTED); + if (p_dev->fd >= 0) { + bta_hh_co_destroy(p_dev->fd); + p_dev->fd = -1; } - break; - - case BTIF_HH_DISCONNECT_REQ_EVT: - { - BTIF_TRACE_EVENT("%s: event=%d", __func__, event); - btif_hh_disconnect(bd_addr); - HAL_CBACK(bt_hh_callbacks, connection_state_cb,bd_addr,BTHH_CONN_STATE_DISCONNECTING); + HAL_CBACK(bt_hh_callbacks, connection_state_cb, &(p_dev->bd_addr), + p_dev->dev_status); + } else { + BTIF_TRACE_WARNING("Error: cannot find device with handle %d", + p_data->dev_status.handle); + } + break; + + case BTA_HH_GET_RPT_EVT: { + BT_HDR* hdr = p_data->hs_data.rsp_data.p_rpt_data; + uint8_t* data = NULL; + uint16_t len = 0; + + BTIF_TRACE_DEBUG("BTA_HH_GET_RPT_EVT: status = %d, handle = %d", + p_data->hs_data.status, p_data->hs_data.handle); + p_dev = btif_hh_find_connected_dev_by_handle(p_data->hs_data.handle); + if (p_dev) { + /* p_rpt_data is NULL in HANDSHAKE response case */ + if (hdr) { + data = (uint8_t*)(hdr + 1) + hdr->offset; + len = hdr->len; + HAL_CBACK(bt_hh_callbacks, get_report_cb, + (bt_bdaddr_t*)&(p_dev->bd_addr), + (bthh_status_t)p_data->hs_data.status, data, len); + } else { + HAL_CBACK(bt_hh_callbacks, handshake_cb, + (bt_bdaddr_t*)&(p_dev->bd_addr), + (bthh_status_t)p_data->hs_data.status); } - break; + } else { + BTIF_TRACE_WARNING("Error: cannot find device with handle %d", + p_data->hs_data.handle); + } + break; + } - case BTIF_HH_VUP_REQ_EVT: - { - BTIF_TRACE_EVENT("%s: event=%d", __func__, event); - ret = btif_hh_virtual_unplug(bd_addr); + case BTA_HH_SET_RPT_EVT: + BTIF_TRACE_DEBUG("BTA_HH_SET_RPT_EVT: status = %d, handle = %d", + p_data->dev_status.status, p_data->dev_status.handle); + p_dev = btif_hh_find_connected_dev_by_handle(p_data->dev_status.handle); + if (p_dev != NULL) { + HAL_CBACK(bt_hh_callbacks, handshake_cb, + (bt_bdaddr_t*)&(p_dev->bd_addr), + (bthh_status_t)p_data->hs_data.status); + } + break; + + case BTA_HH_GET_PROTO_EVT: + p_dev = btif_hh_find_connected_dev_by_handle(p_data->dev_status.handle); + BTIF_TRACE_WARNING( + "BTA_HH_GET_PROTO_EVT: status = %d, handle = %d, proto = [%d], %s", + p_data->hs_data.status, p_data->hs_data.handle, + p_data->hs_data.rsp_data.proto_mode, + (p_data->hs_data.rsp_data.proto_mode == BTA_HH_PROTO_RPT_MODE) + ? "Report Mode" + : (p_data->hs_data.rsp_data.proto_mode == BTA_HH_PROTO_BOOT_MODE) + ? "Boot Mode" + : "Unsupported"); + if (p_data->hs_data.rsp_data.proto_mode != BTA_HH_PROTO_UNKNOWN) { + HAL_CBACK(bt_hh_callbacks, protocol_mode_cb, + (bt_bdaddr_t*)&(p_dev->bd_addr), + (bthh_status_t)p_data->hs_data.status, + (bthh_protocol_mode_t)p_data->hs_data.rsp_data.proto_mode); + } else { + HAL_CBACK(bt_hh_callbacks, handshake_cb, + (bt_bdaddr_t*)&(p_dev->bd_addr), + (bthh_status_t)p_data->hs_data.status); + } + break; + + case BTA_HH_SET_PROTO_EVT: + BTIF_TRACE_DEBUG("BTA_HH_SET_PROTO_EVT: status = %d, handle = %d", + p_data->dev_status.status, p_data->dev_status.handle); + p_dev = btif_hh_find_connected_dev_by_handle(p_data->dev_status.handle); + if (p_dev) { + HAL_CBACK(bt_hh_callbacks, handshake_cb, + (bt_bdaddr_t*)&(p_dev->bd_addr), + (bthh_status_t)p_data->hs_data.status); + } + break; + + case BTA_HH_GET_IDLE_EVT: + BTIF_TRACE_DEBUG( + "BTA_HH_GET_IDLE_EVT: handle = %d, status = %d, rate = %d", + p_data->hs_data.handle, p_data->hs_data.status, + p_data->hs_data.rsp_data.idle_rate); + break; + + case BTA_HH_SET_IDLE_EVT: + BTIF_TRACE_DEBUG("BTA_HH_SET_IDLE_EVT: status = %d, handle = %d", + p_data->dev_status.status, p_data->dev_status.handle); + break; + + case BTA_HH_GET_DSCP_EVT: + len = p_data->dscp_info.descriptor.dl_len; + BTIF_TRACE_DEBUG("BTA_HH_GET_DSCP_EVT: len = %d", len); + p_dev = btif_hh_cb.p_curr_dev; + if (p_dev == NULL) { + BTIF_TRACE_ERROR( + "BTA_HH_GET_DSCP_EVT: No HID device is currently connected"); + return; + } + if (p_dev->fd < 0) { + LOG_ERROR( + LOG_TAG, + "BTA_HH_GET_DSCP_EVT: Error, failed to find the uhid driver..."); + return; + } + { + const char* cached_name = NULL; + bt_bdname_t bdname; + bt_property_t prop_name; + BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME, + sizeof(bt_bdname_t), &bdname); + if (btif_storage_get_remote_device_property( + &p_dev->bd_addr, &prop_name) == BT_STATUS_SUCCESS) { + cached_name = (char*)bdname.name; + } else { + cached_name = "Bluetooth HID"; } - break; - default: - { - BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __func__, event); + BTIF_TRACE_WARNING("%s: name = %s", __func__, cached_name); + bta_hh_co_send_hid_info(p_dev, cached_name, p_data->dscp_info.vendor_id, + p_data->dscp_info.product_id, + p_data->dscp_info.version, + p_data->dscp_info.ctry_code, len, + p_data->dscp_info.descriptor.dsc_list); + if (btif_hh_add_added_dev(p_dev->bd_addr, p_dev->attr_mask)) { + BD_ADDR bda; + bdcpy(bda, p_dev->bd_addr.address); + tBTA_HH_DEV_DSCP_INFO dscp_info; + bt_status_t ret; + bdcpy(bda, p_dev->bd_addr.address); + btif_hh_copy_hid_info(&dscp_info, &p_data->dscp_info); + BTIF_TRACE_DEBUG( + "BTA_HH_GET_DSCP_EVT:bda = %02x:%02x:%02x:%02x:%02x:%02x", + p_dev->bd_addr.address[0], p_dev->bd_addr.address[1], + p_dev->bd_addr.address[2], p_dev->bd_addr.address[3], + p_dev->bd_addr.address[4], p_dev->bd_addr.address[5]); + BTA_HhAddDev(bda, p_dev->attr_mask, p_dev->sub_class, p_dev->app_id, + dscp_info); + // write hid info to nvram + ret = btif_storage_add_hid_device_info( + &(p_dev->bd_addr), p_dev->attr_mask, p_dev->sub_class, + p_dev->app_id, p_data->dscp_info.vendor_id, + p_data->dscp_info.product_id, p_data->dscp_info.version, + p_data->dscp_info.ctry_code, p_data->dscp_info.ssr_max_latency, + p_data->dscp_info.ssr_min_tout, len, + p_data->dscp_info.descriptor.dsc_list); + + ASSERTC(ret == BT_STATUS_SUCCESS, "storing hid info failed", ret); + BTIF_TRACE_WARNING("BTA_HH_GET_DSCP_EVT: Called add device"); + + // Free buffer created for dscp_info; + if (dscp_info.descriptor.dl_len > 0 && + dscp_info.descriptor.dsc_list != NULL) { + osi_free_and_reset((void**)&dscp_info.descriptor.dsc_list); + dscp_info.descriptor.dl_len = 0; + } + } else { + // Device already added. + BTIF_TRACE_WARNING("%s: Device already added ", __func__); } - break; - } + /*Sync HID Keyboard lockstates */ + tmplen = sizeof(hid_kb_numlock_on_list) / sizeof(tHID_KB_LIST); + for (i = 0; i < tmplen; i++) { + if (p_data->dscp_info.vendor_id == + hid_kb_numlock_on_list[i].version_id && + p_data->dscp_info.product_id == + hid_kb_numlock_on_list[i].product_id) { + BTIF_TRACE_DEBUG( + "%s() idx[%d] Enabling " + "NUMLOCK for device :: %s", + __func__, i, hid_kb_numlock_on_list[i].kb_name); + /* Enable NUMLOCK by default so that numeric + keys work from first keyboard connect */ + set_keylockstate(BTIF_HH_KEYSTATE_MASK_NUMLOCK, true); + sync_lockstate_on_connect(p_dev); + /* End Sync HID Keyboard lockstates */ + break; + } + } + } + break; + + case BTA_HH_ADD_DEV_EVT: + BTIF_TRACE_WARNING("BTA_HH_ADD_DEV_EVT: status = %d, handle = %d", + p_data->dev_info.status, p_data->dev_info.handle); + int i; + for (i = 0; i < BTIF_HH_MAX_ADDED_DEV; i++) { + if (memcmp(btif_hh_cb.added_devices[i].bd_addr.address, + p_data->dev_info.bda, 6) == 0) { + if (p_data->dev_info.status == BTA_HH_OK) { + btif_hh_cb.added_devices[i].dev_handle = p_data->dev_info.handle; + } else { + memset(btif_hh_cb.added_devices[i].bd_addr.address, 0, 6); + btif_hh_cb.added_devices[i].dev_handle = BTA_HH_INVALID_HANDLE; + } + break; + } + } + break; + case BTA_HH_RMV_DEV_EVT: + BTIF_TRACE_DEBUG("BTA_HH_RMV_DEV_EVT: status = %d, handle = %d", + p_data->dev_info.status, p_data->dev_info.handle); + BTIF_TRACE_DEBUG("BTA_HH_RMV_DEV_EVT:bda = %02x:%02x:%02x:%02x:%02x:%02x", + p_data->dev_info.bda[0], p_data->dev_info.bda[1], + p_data->dev_info.bda[2], p_data->dev_info.bda[3], + p_data->dev_info.bda[4], p_data->dev_info.bda[5]); + break; + + case BTA_HH_VC_UNPLUG_EVT: + BTIF_TRACE_DEBUG("BTA_HH_VC_UNPLUG_EVT: status = %d, handle = %d", + p_data->dev_status.status, p_data->dev_status.handle); + p_dev = btif_hh_find_connected_dev_by_handle(p_data->dev_status.handle); + btif_hh_cb.status = (BTIF_HH_STATUS)BTIF_HH_DEV_DISCONNECTED; + if (p_dev != NULL) { + BTIF_TRACE_DEBUG( + "BTA_HH_VC_UNPLUG_EVT:bda = %02x:%02x:%02x:%02x:%02x:%02x", + p_dev->bd_addr.address[0], p_dev->bd_addr.address[1], + p_dev->bd_addr.address[2], p_dev->bd_addr.address[3], + p_dev->bd_addr.address[4], p_dev->bd_addr.address[5]); + /* Stop the VUP timer */ + btif_hh_stop_vup_timer(&(p_dev->bd_addr)); + p_dev->dev_status = BTHH_CONN_STATE_DISCONNECTED; + BTIF_TRACE_DEBUG("%s---Sending connection state change", __func__); + HAL_CBACK(bt_hh_callbacks, connection_state_cb, &(p_dev->bd_addr), + p_dev->dev_status); + BTIF_TRACE_DEBUG("%s---Removing HID bond", __func__); + /* If it is locally initiated VUP or remote device has its major COD as + Peripheral removed the bond.*/ + if (p_dev->local_vup || check_cod_hid(&(p_dev->bd_addr))) { + p_dev->local_vup = false; + BTA_DmRemoveDevice((uint8_t*)p_dev->bd_addr.address); + } else + btif_hh_remove_device(p_dev->bd_addr); + HAL_CBACK(bt_hh_callbacks, virtual_unplug_cb, &(p_dev->bd_addr), + (bthh_status_t)p_data->dev_status.status); + } + break; + + case BTA_HH_API_ERR_EVT: + LOG_INFO(LOG_TAG, "BTA_HH API_ERR"); + break; + + default: + BTIF_TRACE_WARNING("%s: Unhandled event: %d", __func__, event); + break; + } } /******************************************************************************* -** -** Function btif_hh_timer_timeout -** -** Description Process timer timeout -** -** Returns void -*******************************************************************************/ -void btif_hh_timer_timeout(void *data) -{ - btif_hh_device_t *p_dev = (btif_hh_device_t *)data; - tBTA_HH_EVT event = BTA_HH_VC_UNPLUG_EVT; - tBTA_HH p_data; - int param_len = sizeof(tBTA_HH_CBDATA); - - BTIF_TRACE_DEBUG("%s", __func__); - if (p_dev->dev_status != BTHH_CONN_STATE_CONNECTED) - return; - - memset(&p_data, 0, sizeof(tBTA_HH)); - p_data.dev_status.status = BTHH_ERR; - p_data.dev_status.handle = p_dev->dev_handle; + * + * Function bte_hh_evt + * + * Description Switches context from BTE to BTIF for all HH events + * + * Returns void + * + ******************************************************************************/ - /* switch context to btif task context */ - btif_transfer_context(btif_hh_upstreams_evt, (uint16_t)event, - (char *)&p_data, param_len, NULL); +static void bte_hh_evt(tBTA_HH_EVT event, tBTA_HH* p_data) { + bt_status_t status; + int param_len = 0; + + if (BTA_HH_ENABLE_EVT == event) + param_len = sizeof(tBTA_HH_STATUS); + else if (BTA_HH_OPEN_EVT == event) + param_len = sizeof(tBTA_HH_CONN); + else if (BTA_HH_DISABLE_EVT == event) + param_len = sizeof(tBTA_HH_STATUS); + else if (BTA_HH_CLOSE_EVT == event) + param_len = sizeof(tBTA_HH_CBDATA); + else if (BTA_HH_GET_DSCP_EVT == event) + param_len = sizeof(tBTA_HH_DEV_DSCP_INFO); + else if ((BTA_HH_GET_PROTO_EVT == event) || (BTA_HH_GET_RPT_EVT == event) || + (BTA_HH_GET_IDLE_EVT == event)) + param_len = sizeof(tBTA_HH_HSDATA); + else if ((BTA_HH_SET_PROTO_EVT == event) || (BTA_HH_SET_RPT_EVT == event) || + (BTA_HH_VC_UNPLUG_EVT == event) || (BTA_HH_SET_IDLE_EVT == event)) + param_len = sizeof(tBTA_HH_CBDATA); + else if ((BTA_HH_ADD_DEV_EVT == event) || (BTA_HH_RMV_DEV_EVT == event)) + param_len = sizeof(tBTA_HH_DEV_INFO); + else if (BTA_HH_API_ERR_EVT == event) + param_len = 0; + /* switch context to btif task context (copy full union size for convenience) + */ + status = btif_transfer_context(btif_hh_upstreams_evt, (uint16_t)event, + (char*)p_data, param_len, NULL); + + /* catch any failed context transfers */ + ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); } /******************************************************************************* -** -** Function btif_hh_init -** -** Description initializes the hh interface -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t init( bthh_callbacks_t* callbacks ) -{ - uint32_t i; - BTIF_TRACE_EVENT("%s", __func__); - - bt_hh_callbacks = callbacks; - memset(&btif_hh_cb, 0, sizeof(btif_hh_cb)); - for (i = 0; i < BTIF_HH_MAX_HID; i++){ - btif_hh_cb.devices[i].dev_status = BTHH_CONN_STATE_UNKNOWN; - } - /* Invoke the enable service API to the core to set the appropriate service_id */ - btif_enable_service(BTA_HID_SERVICE_ID); - return BT_STATUS_SUCCESS; + * + * Function btif_hh_handle_evt + * + * Description Switches context for immediate callback + * + * Returns void + * + ******************************************************************************/ + +static void btif_hh_handle_evt(uint16_t event, char* p_param) { + bt_bdaddr_t* bd_addr = (bt_bdaddr_t*)p_param; + BTIF_TRACE_EVENT("%s: event=%d", __func__, event); + int ret; + switch (event) { + case BTIF_HH_CONNECT_REQ_EVT: { + ret = btif_hh_connect(bd_addr); + if (ret == BT_STATUS_SUCCESS) { + HAL_CBACK(bt_hh_callbacks, connection_state_cb, bd_addr, + BTHH_CONN_STATE_CONNECTING); + } else + HAL_CBACK(bt_hh_callbacks, connection_state_cb, bd_addr, + BTHH_CONN_STATE_DISCONNECTED); + } break; + + case BTIF_HH_DISCONNECT_REQ_EVT: { + BTIF_TRACE_EVENT("%s: event=%d", __func__, event); + btif_hh_disconnect(bd_addr); + HAL_CBACK(bt_hh_callbacks, connection_state_cb, bd_addr, + BTHH_CONN_STATE_DISCONNECTING); + } break; + + case BTIF_HH_VUP_REQ_EVT: { + BTIF_TRACE_EVENT("%s: event=%d", __func__, event); + ret = btif_hh_virtual_unplug(bd_addr); + } break; + + default: { + BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __func__, event); + } break; + } } /******************************************************************************* -** -** Function connect -** -** Description connect to hid device -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t connect( bt_bdaddr_t *bd_addr) -{ - if(btif_hh_cb.status != BTIF_HH_DEV_CONNECTING) - { - btif_transfer_context(btif_hh_handle_evt, BTIF_HH_CONNECT_REQ_EVT, - (char*)bd_addr, sizeof(bt_bdaddr_t), NULL); - return BT_STATUS_SUCCESS; - } - else - return BT_STATUS_BUSY; + * + * Function btif_hh_timer_timeout + * + * Description Process timer timeout + * + * Returns void + ******************************************************************************/ +void btif_hh_timer_timeout(void* data) { + btif_hh_device_t* p_dev = (btif_hh_device_t*)data; + tBTA_HH_EVT event = BTA_HH_VC_UNPLUG_EVT; + tBTA_HH p_data; + int param_len = sizeof(tBTA_HH_CBDATA); + + BTIF_TRACE_DEBUG("%s", __func__); + if (p_dev->dev_status != BTHH_CONN_STATE_CONNECTED) return; + + memset(&p_data, 0, sizeof(tBTA_HH)); + p_data.dev_status.status = BTHH_ERR; + p_data.dev_status.handle = p_dev->dev_handle; + + /* switch context to btif task context */ + btif_transfer_context(btif_hh_upstreams_evt, (uint16_t)event, (char*)&p_data, + param_len, NULL); } /******************************************************************************* -** -** Function disconnect -** -** Description disconnect from hid device -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t disconnect( bt_bdaddr_t *bd_addr ) -{ - CHECK_BTHH_INIT(); - btif_hh_device_t *p_dev; - - if (btif_hh_cb.status == BTIF_HH_DISABLED) - { - BTIF_TRACE_WARNING("%s: Error, HH status = %d", __func__, btif_hh_cb.status); - return BT_STATUS_FAIL; - } - p_dev = btif_hh_find_connected_dev_by_bda(bd_addr); - if (p_dev != NULL) - { - return btif_transfer_context(btif_hh_handle_evt, BTIF_HH_DISCONNECT_REQ_EVT, - (char*)bd_addr, sizeof(bt_bdaddr_t), NULL); - } - else - { - BTIF_TRACE_WARNING("%s: Error, device not opened.", __func__); - return BT_STATUS_FAIL; - } + * + * Function btif_hh_init + * + * Description initializes the hh interface + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t init(bthh_callbacks_t* callbacks) { + uint32_t i; + BTIF_TRACE_EVENT("%s", __func__); + + bt_hh_callbacks = callbacks; + memset(&btif_hh_cb, 0, sizeof(btif_hh_cb)); + for (i = 0; i < BTIF_HH_MAX_HID; i++) { + btif_hh_cb.devices[i].dev_status = BTHH_CONN_STATE_UNKNOWN; + } + /* Invoke the enable service API to the core to set the appropriate service_id + */ + btif_enable_service(BTA_HID_SERVICE_ID); + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function virtual_unplug -** -** Description Virtual UnPlug (VUP) the specified HID device. -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t virtual_unplug (bt_bdaddr_t *bd_addr) -{ - CHECK_BTHH_INIT(); - btif_hh_device_t *p_dev; - char bd_str[18]; - snprintf(bd_str, sizeof(bd_str), "%02X:%02X:%02X:%02X:%02X:%02X", - bd_addr->address[0], bd_addr->address[1], bd_addr->address[2], - bd_addr->address[3], bd_addr->address[4], bd_addr->address[5]); - if (btif_hh_cb.status == BTIF_HH_DISABLED) - { - BTIF_TRACE_ERROR("%s: Error, HH status = %d", __func__, btif_hh_cb.status); - return BT_STATUS_FAIL; - } - p_dev = btif_hh_find_dev_by_bda(bd_addr); - if (!p_dev) - { - BTIF_TRACE_ERROR("%s: Error, device %s not opened.", __func__, bd_str); - return BT_STATUS_FAIL; - } - btif_transfer_context(btif_hh_handle_evt, BTIF_HH_VUP_REQ_EVT, - (char*)bd_addr, sizeof(bt_bdaddr_t), NULL); + * + * Function connect + * + * Description connect to hid device + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t connect(bt_bdaddr_t* bd_addr) { + if (btif_hh_cb.status != BTIF_HH_DEV_CONNECTING) { + btif_transfer_context(btif_hh_handle_evt, BTIF_HH_CONNECT_REQ_EVT, + (char*)bd_addr, sizeof(bt_bdaddr_t), NULL); return BT_STATUS_SUCCESS; + } else + return BT_STATUS_BUSY; } /******************************************************************************* -** -** Function set_info -** -** Description Set the HID device descriptor for the specified HID device. -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t set_info (bt_bdaddr_t *bd_addr, bthh_hid_info_t hid_info ) -{ - CHECK_BTHH_INIT(); - tBTA_HH_DEV_DSCP_INFO dscp_info; - BD_ADDR* bda = (BD_ADDR*) bd_addr; - - BTIF_TRACE_DEBUG("addr = %02X:%02X:%02X:%02X:%02X:%02X", - (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); - BTIF_TRACE_DEBUG("%s: sub_class = 0x%02x, app_id = %d, vendor_id = 0x%04x, " - "product_id = 0x%04x, version= 0x%04x", - __func__, hid_info.sub_class, - hid_info.app_id, hid_info.vendor_id, hid_info.product_id, - hid_info.version); - - if (btif_hh_cb.status == BTIF_HH_DISABLED) - { - BTIF_TRACE_ERROR("%s: Error, HH status = %d", __func__, btif_hh_cb.status); - return BT_STATUS_FAIL; - } - - dscp_info.vendor_id = hid_info.vendor_id; - dscp_info.product_id = hid_info.product_id; - dscp_info.version = hid_info.version; - dscp_info.ctry_code = hid_info.ctry_code; - - dscp_info.descriptor.dl_len = hid_info.dl_len; - dscp_info.descriptor.dsc_list = (uint8_t *)osi_malloc(dscp_info.descriptor.dl_len); - memcpy(dscp_info.descriptor.dsc_list, &(hid_info.dsc_list), hid_info.dl_len); - - if (btif_hh_add_added_dev(*bd_addr, hid_info.attr_mask)) - { - BTA_HhAddDev(*bda, hid_info.attr_mask, hid_info.sub_class, - hid_info.app_id, dscp_info); - } - - osi_free_and_reset((void **)&dscp_info.descriptor.dsc_list); - - return BT_STATUS_SUCCESS; + * + * Function disconnect + * + * Description disconnect from hid device + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t disconnect(bt_bdaddr_t* bd_addr) { + CHECK_BTHH_INIT(); + btif_hh_device_t* p_dev; + + if (btif_hh_cb.status == BTIF_HH_DISABLED) { + BTIF_TRACE_WARNING("%s: Error, HH status = %d", __func__, + btif_hh_cb.status); + return BT_STATUS_FAIL; + } + p_dev = btif_hh_find_connected_dev_by_bda(bd_addr); + if (p_dev != NULL) { + return btif_transfer_context(btif_hh_handle_evt, BTIF_HH_DISCONNECT_REQ_EVT, + (char*)bd_addr, sizeof(bt_bdaddr_t), NULL); + } else { + BTIF_TRACE_WARNING("%s: Error, device not opened.", __func__); + return BT_STATUS_FAIL; + } } /******************************************************************************* -** -** Function get_protocol -** -** Description Get the HID proto mode. -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t get_protocol (bt_bdaddr_t *bd_addr, bthh_protocol_mode_t protocolMode) -{ - CHECK_BTHH_INIT(); - btif_hh_device_t *p_dev; - BD_ADDR* bda = (BD_ADDR*) bd_addr; - UNUSED(protocolMode); - - BTIF_TRACE_DEBUG(" addr = %02X:%02X:%02X:%02X:%02X:%02X", - (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); - - if (btif_hh_cb.status == BTIF_HH_DISABLED) { - BTIF_TRACE_ERROR("%s: Error, HH status = %d", __func__, btif_hh_cb.status); - return BT_STATUS_FAIL; - } - - p_dev = btif_hh_find_connected_dev_by_bda(bd_addr); - if (p_dev != NULL) { - - BTA_HhGetProtoMode(p_dev->dev_handle); - } - else { - return BT_STATUS_FAIL; - } - return BT_STATUS_SUCCESS; + * + * Function virtual_unplug + * + * Description Virtual UnPlug (VUP) the specified HID device. + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t virtual_unplug(bt_bdaddr_t* bd_addr) { + CHECK_BTHH_INIT(); + btif_hh_device_t* p_dev; + char bd_str[18]; + snprintf(bd_str, sizeof(bd_str), "%02X:%02X:%02X:%02X:%02X:%02X", + bd_addr->address[0], bd_addr->address[1], bd_addr->address[2], + bd_addr->address[3], bd_addr->address[4], bd_addr->address[5]); + if (btif_hh_cb.status == BTIF_HH_DISABLED) { + BTIF_TRACE_ERROR("%s: Error, HH status = %d", __func__, btif_hh_cb.status); + return BT_STATUS_FAIL; + } + p_dev = btif_hh_find_dev_by_bda(bd_addr); + if (!p_dev) { + BTIF_TRACE_ERROR("%s: Error, device %s not opened.", __func__, bd_str); + return BT_STATUS_FAIL; + } + btif_transfer_context(btif_hh_handle_evt, BTIF_HH_VUP_REQ_EVT, (char*)bd_addr, + sizeof(bt_bdaddr_t), NULL); + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function set_protocol -** -** Description Set the HID proto mode. -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t set_protocol (bt_bdaddr_t *bd_addr, bthh_protocol_mode_t protocolMode) -{ - CHECK_BTHH_INIT(); - btif_hh_device_t *p_dev; - uint8_t proto_mode = protocolMode; - BD_ADDR* bda = (BD_ADDR*) bd_addr; - - BTIF_TRACE_DEBUG("%s:proto_mode = %d", __func__,protocolMode); - - BTIF_TRACE_DEBUG("addr = %02X:%02X:%02X:%02X:%02X:%02X", - (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); - - if (btif_hh_cb.status == BTIF_HH_DISABLED) { - BTIF_TRACE_ERROR("%s: Error, HH status = %d", __func__, btif_hh_cb.status); - return BT_STATUS_FAIL; - } - - p_dev = btif_hh_find_connected_dev_by_bda(bd_addr); - if (p_dev == NULL) { - BTIF_TRACE_WARNING(" Error, device %02X:%02X:%02X:%02X:%02X:%02X not opened.", - (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); - return BT_STATUS_FAIL; - } - else if (protocolMode != BTA_HH_PROTO_RPT_MODE && protocolMode != BTA_HH_PROTO_BOOT_MODE) { - BTIF_TRACE_WARNING("%s: Error, device proto_mode = %d.", __func__, proto_mode); - return BT_STATUS_FAIL; - } - else { - BTA_HhSetProtoMode(p_dev->dev_handle, protocolMode); - } - - return BT_STATUS_SUCCESS; + * + * Function set_info + * + * Description Set the HID device descriptor for the specified HID device. + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t set_info(bt_bdaddr_t* bd_addr, bthh_hid_info_t hid_info) { + CHECK_BTHH_INIT(); + tBTA_HH_DEV_DSCP_INFO dscp_info; + BD_ADDR* bda = (BD_ADDR*)bd_addr; + + BTIF_TRACE_DEBUG("addr = %02X:%02X:%02X:%02X:%02X:%02X", (*bda)[0], (*bda)[1], + (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); + BTIF_TRACE_DEBUG( + "%s: sub_class = 0x%02x, app_id = %d, vendor_id = 0x%04x, " + "product_id = 0x%04x, version= 0x%04x", + __func__, hid_info.sub_class, hid_info.app_id, hid_info.vendor_id, + hid_info.product_id, hid_info.version); + + if (btif_hh_cb.status == BTIF_HH_DISABLED) { + BTIF_TRACE_ERROR("%s: Error, HH status = %d", __func__, btif_hh_cb.status); + return BT_STATUS_FAIL; + } + + dscp_info.vendor_id = hid_info.vendor_id; + dscp_info.product_id = hid_info.product_id; + dscp_info.version = hid_info.version; + dscp_info.ctry_code = hid_info.ctry_code; + + dscp_info.descriptor.dl_len = hid_info.dl_len; + dscp_info.descriptor.dsc_list = + (uint8_t*)osi_malloc(dscp_info.descriptor.dl_len); + memcpy(dscp_info.descriptor.dsc_list, &(hid_info.dsc_list), hid_info.dl_len); + + if (btif_hh_add_added_dev(*bd_addr, hid_info.attr_mask)) { + BTA_HhAddDev(*bda, hid_info.attr_mask, hid_info.sub_class, hid_info.app_id, + dscp_info); + } + + osi_free_and_reset((void**)&dscp_info.descriptor.dsc_list); + + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function get_report -** -** Description Send a GET_REPORT to HID device. -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t get_report (bt_bdaddr_t *bd_addr, bthh_report_type_t reportType, uint8_t reportId, int bufferSize) -{ - CHECK_BTHH_INIT(); - btif_hh_device_t *p_dev; - BD_ADDR* bda = (BD_ADDR*) bd_addr; - - BTIF_TRACE_DEBUG("%s: r_type = %d, rpt_id = %d, buf_size = %d", __func__, - reportType, reportId, bufferSize); - - BTIF_TRACE_DEBUG("addr = %02X:%02X:%02X:%02X:%02X:%02X", - (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); - - if (btif_hh_cb.status == BTIF_HH_DISABLED) { - BTIF_TRACE_ERROR("%s: Error, HH status = %d", __func__, btif_hh_cb.status); - return BT_STATUS_FAIL; - } - - p_dev = btif_hh_find_connected_dev_by_bda(bd_addr); - if (p_dev == NULL) { - BTIF_TRACE_ERROR("%s: Error, device %02X:%02X:%02X:%02X:%02X:%02X not opened.", __func__, - (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); - return BT_STATUS_FAIL; - } - else if ( ((int) reportType) <= BTA_HH_RPTT_RESRV || ((int) reportType) > BTA_HH_RPTT_FEATURE) { - BTIF_TRACE_ERROR(" Error, device %02X:%02X:%02X:%02X:%02X:%02X not opened.", - (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); - return BT_STATUS_FAIL; - } - else { - BTA_HhGetReport(p_dev->dev_handle, reportType, - reportId, bufferSize); - } + * + * Function get_protocol + * + * Description Get the HID proto mode. + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t get_protocol(bt_bdaddr_t* bd_addr, + bthh_protocol_mode_t protocolMode) { + CHECK_BTHH_INIT(); + btif_hh_device_t* p_dev; + BD_ADDR* bda = (BD_ADDR*)bd_addr; + UNUSED(protocolMode); + + BTIF_TRACE_DEBUG(" addr = %02X:%02X:%02X:%02X:%02X:%02X", (*bda)[0], + (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); + + if (btif_hh_cb.status == BTIF_HH_DISABLED) { + BTIF_TRACE_ERROR("%s: Error, HH status = %d", __func__, btif_hh_cb.status); + return BT_STATUS_FAIL; + } + + p_dev = btif_hh_find_connected_dev_by_bda(bd_addr); + if (p_dev != NULL) { + BTA_HhGetProtoMode(p_dev->dev_handle); + } else { + return BT_STATUS_FAIL; + } + return BT_STATUS_SUCCESS; +} - return BT_STATUS_SUCCESS; +/******************************************************************************* + * + * Function set_protocol + * + * Description Set the HID proto mode. + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t set_protocol(bt_bdaddr_t* bd_addr, + bthh_protocol_mode_t protocolMode) { + CHECK_BTHH_INIT(); + btif_hh_device_t* p_dev; + uint8_t proto_mode = protocolMode; + BD_ADDR* bda = (BD_ADDR*)bd_addr; + + BTIF_TRACE_DEBUG("%s:proto_mode = %d", __func__, protocolMode); + + BTIF_TRACE_DEBUG("addr = %02X:%02X:%02X:%02X:%02X:%02X", (*bda)[0], (*bda)[1], + (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); + + if (btif_hh_cb.status == BTIF_HH_DISABLED) { + BTIF_TRACE_ERROR("%s: Error, HH status = %d", __func__, btif_hh_cb.status); + return BT_STATUS_FAIL; + } + + p_dev = btif_hh_find_connected_dev_by_bda(bd_addr); + if (p_dev == NULL) { + BTIF_TRACE_WARNING( + " Error, device %02X:%02X:%02X:%02X:%02X:%02X not opened.", (*bda)[0], + (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); + return BT_STATUS_FAIL; + } else if (protocolMode != BTA_HH_PROTO_RPT_MODE && + protocolMode != BTA_HH_PROTO_BOOT_MODE) { + BTIF_TRACE_WARNING("%s: Error, device proto_mode = %d.", __func__, + proto_mode); + return BT_STATUS_FAIL; + } else { + BTA_HhSetProtoMode(p_dev->dev_handle, protocolMode); + } + + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function set_report -** -** Description Send a SET_REPORT to HID device. -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t set_report (bt_bdaddr_t *bd_addr, bthh_report_type_t reportType, char* report) -{ - CHECK_BTHH_INIT(); - btif_hh_device_t *p_dev; - BD_ADDR* bda = (BD_ADDR*) bd_addr; - - BTIF_TRACE_DEBUG("%s:reportType = %d", __func__,reportType); - - BTIF_TRACE_DEBUG("addr = %02X:%02X:%02X:%02X:%02X:%02X", - (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); - - if (btif_hh_cb.status == BTIF_HH_DISABLED) { - BTIF_TRACE_ERROR("%s: Error, HH status = %d", __func__, btif_hh_cb.status); - return BT_STATUS_FAIL; - } + * + * Function get_report + * + * Description Send a GET_REPORT to HID device. + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t get_report(bt_bdaddr_t* bd_addr, + bthh_report_type_t reportType, uint8_t reportId, + int bufferSize) { + CHECK_BTHH_INIT(); + btif_hh_device_t* p_dev; + BD_ADDR* bda = (BD_ADDR*)bd_addr; + + BTIF_TRACE_DEBUG("%s: r_type = %d, rpt_id = %d, buf_size = %d", __func__, + reportType, reportId, bufferSize); + + BTIF_TRACE_DEBUG("addr = %02X:%02X:%02X:%02X:%02X:%02X", (*bda)[0], (*bda)[1], + (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); + + if (btif_hh_cb.status == BTIF_HH_DISABLED) { + BTIF_TRACE_ERROR("%s: Error, HH status = %d", __func__, btif_hh_cb.status); + return BT_STATUS_FAIL; + } + + p_dev = btif_hh_find_connected_dev_by_bda(bd_addr); + if (p_dev == NULL) { + BTIF_TRACE_ERROR( + "%s: Error, device %02X:%02X:%02X:%02X:%02X:%02X not opened.", __func__, + (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); + return BT_STATUS_FAIL; + } else if (((int)reportType) <= BTA_HH_RPTT_RESRV || + ((int)reportType) > BTA_HH_RPTT_FEATURE) { + BTIF_TRACE_ERROR(" Error, device %02X:%02X:%02X:%02X:%02X:%02X not opened.", + (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], + (*bda)[5]); + return BT_STATUS_FAIL; + } else { + BTA_HhGetReport(p_dev->dev_handle, reportType, reportId, bufferSize); + } + + return BT_STATUS_SUCCESS; +} - p_dev = btif_hh_find_connected_dev_by_bda(bd_addr); - if (p_dev == NULL) { - BTIF_TRACE_ERROR("%s: Error, device %02X:%02X:%02X:%02X:%02X:%02X not opened.", __func__, - (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); - return BT_STATUS_FAIL; - } - else if ( ( (int) reportType) <= BTA_HH_RPTT_RESRV || ( (int) reportType) > BTA_HH_RPTT_FEATURE) { - BTIF_TRACE_ERROR(" Error, device %02X:%02X:%02X:%02X:%02X:%02X not opened.", - (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); - return BT_STATUS_FAIL; - } - else { - int hex_bytes_filled; - size_t len = (strlen(report) + 1) / 2; - uint8_t *hexbuf = (uint8_t *)osi_calloc(len); - - /* Build a SetReport data buffer */ - //TODO - hex_bytes_filled = ascii_2_hex(report, len, hexbuf); - LOG_INFO(LOG_TAG, "Hex bytes filled, hex value: %d", hex_bytes_filled); - if (hex_bytes_filled) { - BT_HDR* p_buf = create_pbuf(hex_bytes_filled, hexbuf); - if (p_buf == NULL) { - BTIF_TRACE_ERROR("%s: Error, failed to allocate RPT buffer, len = %d", - __func__, hex_bytes_filled); - osi_free(hexbuf); - return BT_STATUS_FAIL; - } - BTA_HhSetReport(p_dev->dev_handle, reportType, p_buf); - osi_free(hexbuf); - return BT_STATUS_SUCCESS; - } +/******************************************************************************* + * + * Function set_report + * + * Description Send a SET_REPORT to HID device. + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t set_report(bt_bdaddr_t* bd_addr, + bthh_report_type_t reportType, char* report) { + CHECK_BTHH_INIT(); + btif_hh_device_t* p_dev; + BD_ADDR* bda = (BD_ADDR*)bd_addr; + + BTIF_TRACE_DEBUG("%s:reportType = %d", __func__, reportType); + + BTIF_TRACE_DEBUG("addr = %02X:%02X:%02X:%02X:%02X:%02X", (*bda)[0], (*bda)[1], + (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); + + if (btif_hh_cb.status == BTIF_HH_DISABLED) { + BTIF_TRACE_ERROR("%s: Error, HH status = %d", __func__, btif_hh_cb.status); + return BT_STATUS_FAIL; + } + + p_dev = btif_hh_find_connected_dev_by_bda(bd_addr); + if (p_dev == NULL) { + BTIF_TRACE_ERROR( + "%s: Error, device %02X:%02X:%02X:%02X:%02X:%02X not opened.", __func__, + (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); + return BT_STATUS_FAIL; + } else if (((int)reportType) <= BTA_HH_RPTT_RESRV || + ((int)reportType) > BTA_HH_RPTT_FEATURE) { + BTIF_TRACE_ERROR(" Error, device %02X:%02X:%02X:%02X:%02X:%02X not opened.", + (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], + (*bda)[5]); + return BT_STATUS_FAIL; + } else { + int hex_bytes_filled; + size_t len = (strlen(report) + 1) / 2; + uint8_t* hexbuf = (uint8_t*)osi_calloc(len); + + /* Build a SetReport data buffer */ + // TODO + hex_bytes_filled = ascii_2_hex(report, len, hexbuf); + LOG_INFO(LOG_TAG, "Hex bytes filled, hex value: %d", hex_bytes_filled); + if (hex_bytes_filled) { + BT_HDR* p_buf = create_pbuf(hex_bytes_filled, hexbuf); + if (p_buf == NULL) { + BTIF_TRACE_ERROR("%s: Error, failed to allocate RPT buffer, len = %d", + __func__, hex_bytes_filled); osi_free(hexbuf); return BT_STATUS_FAIL; + } + BTA_HhSetReport(p_dev->dev_handle, reportType, p_buf); + osi_free(hexbuf); + return BT_STATUS_SUCCESS; } + osi_free(hexbuf); + return BT_STATUS_FAIL; + } } /******************************************************************************* -** -** Function send_data -** -** Description Send a SEND_DATA to HID device. -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t send_data (bt_bdaddr_t *bd_addr, char* data) -{ - CHECK_BTHH_INIT(); - btif_hh_device_t *p_dev; - BD_ADDR* bda = (BD_ADDR*) bd_addr; - - BTIF_TRACE_DEBUG("%s", __func__); - - BTIF_TRACE_DEBUG("addr = %02X:%02X:%02X:%02X:%02X:%02X", - (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); - - if (btif_hh_cb.status == BTIF_HH_DISABLED) { - BTIF_TRACE_ERROR("%s: Error, HH status = %d", __func__, btif_hh_cb.status); - return BT_STATUS_FAIL; - } - - p_dev = btif_hh_find_connected_dev_by_bda(bd_addr); - if (p_dev == NULL) { - BTIF_TRACE_ERROR("%s: Error, device %02X:%02X:%02X:%02X:%02X:%02X not opened.", __func__, - (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); - return BT_STATUS_FAIL; - } - - else { - int hex_bytes_filled; - size_t len = (strlen(data) + 1) / 2; - uint8_t *hexbuf = (uint8_t *)osi_calloc(len); - - /* Build a SendData data buffer */ - hex_bytes_filled = ascii_2_hex(data, len, hexbuf); - BTIF_TRACE_ERROR("Hex bytes filled, hex value: %d, %d", hex_bytes_filled, len); - - if (hex_bytes_filled) { - BT_HDR* p_buf = create_pbuf(hex_bytes_filled, hexbuf); - if (p_buf == NULL) { - BTIF_TRACE_ERROR("%s: Error, failed to allocate RPT buffer, len = %d", - __func__, hex_bytes_filled); - osi_free(hexbuf); - return BT_STATUS_FAIL; - } - p_buf->layer_specific = BTA_HH_RPTT_OUTPUT; - BTA_HhSendData(p_dev->dev_handle, *bda, p_buf); - osi_free(hexbuf); - return BT_STATUS_SUCCESS; - } + * + * Function send_data + * + * Description Send a SEND_DATA to HID device. + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t send_data(bt_bdaddr_t* bd_addr, char* data) { + CHECK_BTHH_INIT(); + btif_hh_device_t* p_dev; + BD_ADDR* bda = (BD_ADDR*)bd_addr; + + BTIF_TRACE_DEBUG("%s", __func__); + + BTIF_TRACE_DEBUG("addr = %02X:%02X:%02X:%02X:%02X:%02X", (*bda)[0], (*bda)[1], + (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); + + if (btif_hh_cb.status == BTIF_HH_DISABLED) { + BTIF_TRACE_ERROR("%s: Error, HH status = %d", __func__, btif_hh_cb.status); + return BT_STATUS_FAIL; + } + + p_dev = btif_hh_find_connected_dev_by_bda(bd_addr); + if (p_dev == NULL) { + BTIF_TRACE_ERROR( + "%s: Error, device %02X:%02X:%02X:%02X:%02X:%02X not opened.", __func__, + (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]); + return BT_STATUS_FAIL; + } + + else { + int hex_bytes_filled; + size_t len = (strlen(data) + 1) / 2; + uint8_t* hexbuf = (uint8_t*)osi_calloc(len); + + /* Build a SendData data buffer */ + hex_bytes_filled = ascii_2_hex(data, len, hexbuf); + BTIF_TRACE_ERROR("Hex bytes filled, hex value: %d, %d", hex_bytes_filled, + len); + + if (hex_bytes_filled) { + BT_HDR* p_buf = create_pbuf(hex_bytes_filled, hexbuf); + if (p_buf == NULL) { + BTIF_TRACE_ERROR("%s: Error, failed to allocate RPT buffer, len = %d", + __func__, hex_bytes_filled); osi_free(hexbuf); return BT_STATUS_FAIL; + } + p_buf->layer_specific = BTA_HH_RPTT_OUTPUT; + BTA_HhSendData(p_dev->dev_handle, *bda, p_buf); + osi_free(hexbuf); + return BT_STATUS_SUCCESS; } + osi_free(hexbuf); + return BT_STATUS_FAIL; + } } /******************************************************************************* -** -** Function cleanup -** -** Description Closes the HH interface -** -** Returns bt_status_t -** -*******************************************************************************/ -static void cleanup( void ) -{ - BTIF_TRACE_EVENT("%s", __func__); - btif_hh_device_t *p_dev; - int i; - if (btif_hh_cb.status == BTIF_HH_DISABLED) { - BTIF_TRACE_WARNING("%s: HH disabling or disabled already, status = %d", __func__, btif_hh_cb.status); - return; - } - btif_hh_cb.status = BTIF_HH_DISABLING; - for (i = 0; i < BTIF_HH_MAX_HID; i++) { - p_dev = &btif_hh_cb.devices[i]; - if (p_dev->dev_status != BTHH_CONN_STATE_UNKNOWN && p_dev->fd >= 0) { - BTIF_TRACE_DEBUG("%s: Closing uhid fd = %d", __func__, p_dev->fd); - if (p_dev->fd >= 0) { - bta_hh_co_destroy(p_dev->fd); - p_dev->fd = -1; - } - p_dev->hh_keep_polling = 0; - p_dev->hh_poll_thread_id = -1; - } - } - - if (bt_hh_callbacks) - { - btif_disable_service(BTA_HID_SERVICE_ID); - bt_hh_callbacks = NULL; + * + * Function cleanup + * + * Description Closes the HH interface + * + * Returns bt_status_t + * + ******************************************************************************/ +static void cleanup(void) { + BTIF_TRACE_EVENT("%s", __func__); + btif_hh_device_t* p_dev; + int i; + if (btif_hh_cb.status == BTIF_HH_DISABLED) { + BTIF_TRACE_WARNING("%s: HH disabling or disabled already, status = %d", + __func__, btif_hh_cb.status); + return; + } + btif_hh_cb.status = BTIF_HH_DISABLING; + for (i = 0; i < BTIF_HH_MAX_HID; i++) { + p_dev = &btif_hh_cb.devices[i]; + if (p_dev->dev_status != BTHH_CONN_STATE_UNKNOWN && p_dev->fd >= 0) { + BTIF_TRACE_DEBUG("%s: Closing uhid fd = %d", __func__, p_dev->fd); + if (p_dev->fd >= 0) { + bta_hh_co_destroy(p_dev->fd); + p_dev->fd = -1; + } + p_dev->hh_keep_polling = 0; + p_dev->hh_poll_thread_id = -1; } + } + if (bt_hh_callbacks) { + btif_disable_service(BTA_HID_SERVICE_ID); + bt_hh_callbacks = NULL; + } } static const bthh_interface_t bthhInterface = { - sizeof(bthhInterface), - init, - connect, - disconnect, - virtual_unplug, - set_info, - get_protocol, - set_protocol, -// get_idle_time, -// set_idle_time, - get_report, - set_report, - send_data, - cleanup, + sizeof(bthhInterface), init, connect, disconnect, virtual_unplug, set_info, + get_protocol, set_protocol, + // get_idle_time, + // set_idle_time, + get_report, set_report, send_data, cleanup, }; /******************************************************************************* -** -** Function btif_hh_execute_service -** -** Description Initializes/Shuts down the service -** -** Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_hh_execute_service(bool b_enable) -{ - if (b_enable) - { - /* Enable and register with BTA-HH */ - BTA_HhEnable(BTUI_HH_SECURITY, bte_hh_evt); - } - else { - /* Disable HH */ - BTA_HhDisable(); - } - return BT_STATUS_SUCCESS; + * + * Function btif_hh_execute_service + * + * Description Initializes/Shuts down the service + * + * Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_hh_execute_service(bool b_enable) { + if (b_enable) { + /* Enable and register with BTA-HH */ + BTA_HhEnable(BTUI_HH_SECURITY, bte_hh_evt); + } else { + /* Disable HH */ + BTA_HhDisable(); + } + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_hh_get_interface -** -** Description Get the hh callback interface -** -** Returns bthh_interface_t -** -*******************************************************************************/ -const bthh_interface_t *btif_hh_get_interface() -{ - BTIF_TRACE_EVENT("%s", __func__); - return &bthhInterface; + * + * Function btif_hh_get_interface + * + * Description Get the hh callback interface + * + * Returns bthh_interface_t + * + ******************************************************************************/ +const bthh_interface_t* btif_hh_get_interface() { + BTIF_TRACE_EVENT("%s", __func__); + return &bthhInterface; } diff --git a/btif/src/btif_hl.cc b/btif/src/btif_hl.cc index c724809e7..19963f114 100644 --- a/btif/src/btif_hl.cc +++ b/btif/src/btif_hl.cc @@ -59,27 +59,33 @@ #define MAX_DATATYPE_SUPPORTED 8 -extern int btif_hl_update_maxfd( int max_org_s); -extern void btif_hl_select_monitor_callback( fd_set *p_cur_set, fd_set *p_org_set ); -extern void btif_hl_select_wakeup_callback( fd_set *p_org_set , int wakeup_signal ); -extern int btif_hl_update_maxfd( int max_org_s); -extern void btif_hl_select_monitor_callback( fd_set *p_cur_set, fd_set *p_org_set ); -extern void btif_hl_select_wakeup_callback( fd_set *p_org_set , int wakeup_signal ); +extern int btif_hl_update_maxfd(int max_org_s); +extern void btif_hl_select_monitor_callback(fd_set* p_cur_set, + fd_set* p_org_set); +extern void btif_hl_select_wakeup_callback(fd_set* p_org_set, + int wakeup_signal); +extern int btif_hl_update_maxfd(int max_org_s); +extern void btif_hl_select_monitor_callback(fd_set* p_cur_set, + fd_set* p_org_set); +extern void btif_hl_select_wakeup_callback(fd_set* p_org_set, + int wakeup_signal); extern void btif_hl_soc_thread_init(void); extern void btif_hl_release_mcl_sockets(uint8_t app_idx, uint8_t mcl_idx); -extern bool btif_hl_create_socket(uint8_t app_idx, uint8_t mcl_idx, uint8_t mdl_idx); -extern void btif_hl_release_socket(uint8_t app_idx, uint8_t mcl_idx, uint8_t mdl_idx); +extern bool btif_hl_create_socket(uint8_t app_idx, uint8_t mcl_idx, + uint8_t mdl_idx); +extern void btif_hl_release_socket(uint8_t app_idx, uint8_t mcl_idx, + uint8_t mdl_idx); -extern fixed_queue_t *btu_general_alarm_queue; +extern fixed_queue_t* btu_general_alarm_queue; btif_hl_cb_t btif_hl_cb; -btif_hl_cb_t *p_btif_hl_cb = &btif_hl_cb; +btif_hl_cb_t* p_btif_hl_cb = &btif_hl_cb; /************************************************************************************ -** Static variables -************************************************************************************/ -static bthl_callbacks_t bt_hl_callbacks_cb; -static bthl_callbacks_t *bt_hl_callbacks=NULL; + * Static variables + ***********************************************************************************/ +static bthl_callbacks_t bt_hl_callbacks_cb; +static bthl_callbacks_t* bt_hl_callbacks = NULL; /* signal socketpair to wake up select loop */ @@ -90,8 +96,8 @@ const int btif_hl_signal_select_close_connected = 3; static int listen_s = -1; static int connected_s = -1; static int select_thread_id = -1; -static int signal_fds[2] = { -1, -1 }; -static list_t *soc_queue; +static int signal_fds[2] = {-1, -1}; +static list_t* soc_queue; static int reg_counter; static inline int btif_hl_select_wakeup(void); @@ -101,4227 +107,3972 @@ static uint8_t btif_hl_get_next_app_id(void); static int btif_hl_get_next_channel_id(uint8_t app_id); static void btif_hl_init_next_app_id(void); static void btif_hl_init_next_channel_id(void); -static void btif_hl_ctrl_cback(tBTA_HL_CTRL_EVT event, tBTA_HL_CTRL *p_data); +static void btif_hl_ctrl_cback(tBTA_HL_CTRL_EVT event, tBTA_HL_CTRL* p_data); static void btif_hl_set_state(btif_hl_state_t state); static btif_hl_state_t btif_hl_get_state(void); -static void btif_hl_cback(tBTA_HL_EVT event, tBTA_HL *p_data); +static void btif_hl_cback(tBTA_HL_EVT event, tBTA_HL* p_data); static void btif_hl_proc_cb_evt(uint16_t event, char* p_param); -#define CHECK_CALL_CBACK(P_CB, P_CBACK, ...)\ - if ((P_CB) && (P_CB)->P_CBACK) { \ - (P_CB)->P_CBACK(__VA_ARGS__); \ - } \ - else { \ - ASSERTC(0, "Callback is NULL", 0); \ - } - -#define BTIF_HL_CALL_CBACK(P_CB, P_CBACK, ...)\ - if((p_btif_hl_cb->state != BTIF_HL_STATE_DISABLING) &&\ - (p_btif_hl_cb->state != BTIF_HL_STATE_DISABLED)) \ - { \ - if ((P_CB) && (P_CB)->P_CBACK) { \ - (P_CB)->P_CBACK(__VA_ARGS__); \ - } \ - else { \ - ASSERTC(0, "Callback is NULL", 0); \ - } \ - } - -#define CHECK_BTHL_INIT() if (bt_hl_callbacks == NULL)\ - {\ - BTIF_TRACE_WARNING("BTHL: %s: BTHL not initialized", __func__);\ - return BT_STATUS_NOT_READY;\ - }\ - else\ - {\ - BTIF_TRACE_EVENT("BTHL: %s", __func__);\ - } +#define CHECK_CALL_CBACK(P_CB, P_CBACK, ...) \ + if ((P_CB) && (P_CB)->P_CBACK) { \ + (P_CB)->P_CBACK(__VA_ARGS__); \ + } else { \ + ASSERTC(0, "Callback is NULL", 0); \ + } + +#define BTIF_HL_CALL_CBACK(P_CB, P_CBACK, ...) \ + if ((p_btif_hl_cb->state != BTIF_HL_STATE_DISABLING) && \ + (p_btif_hl_cb->state != BTIF_HL_STATE_DISABLED)) { \ + if ((P_CB) && (P_CB)->P_CBACK) { \ + (P_CB)->P_CBACK(__VA_ARGS__); \ + } else { \ + ASSERTC(0, "Callback is NULL", 0); \ + } \ + } + +#define CHECK_BTHL_INIT() \ + if (bt_hl_callbacks == NULL) { \ + BTIF_TRACE_WARNING("BTHL: %s: BTHL not initialized", __func__); \ + return BT_STATUS_NOT_READY; \ + } else { \ + BTIF_TRACE_EVENT("BTHL: %s", __func__); \ + } static const btif_hl_data_type_cfg_t data_type_table[] = { - /* Data Specilization Ntx Nrx (from Bluetooth SIG's HDP whitepaper)*/ - {BTIF_HL_DATA_TYPE_PULSE_OXIMETER, 9216, 256}, - {BTIF_HL_DATA_TYPE_BLOOD_PRESSURE_MON, 896, 224}, - {BTIF_HL_DATA_TYPE_BODY_THERMOMETER, 896, 224}, - {BTIF_HL_DATA_TYPE_BODY_WEIGHT_SCALE, 896, 224}, - {BTIF_HL_DATA_TYPE_GLUCOSE_METER, 896, 224}, - {BTIF_HL_DATA_TYPE_STEP_COUNTER, 6624, 224}, - {BTIF_HL_DATA_TYPE_BCA, 7730, 1230}, - {BTIF_HL_DATA_TYPE_PEAK_FLOW , 2030, 224}, - {BTIF_HL_DATA_TYPE_ACTIVITY_HUB, 5120, 224}, - {BTIF_HL_DATA_TYPE_AMM, 1024, 64} -}; - -#define BTIF_HL_DATA_TABLE_SIZE (sizeof(data_type_table) / sizeof(btif_hl_data_type_cfg_t)) -#define BTIF_HL_DEFAULT_SRC_TX_APDU_SIZE 10240 /* use this size if the data type is not - defined in the table; for future proof */ -#define BTIF_HL_DEFAULT_SRC_RX_APDU_SIZE 512 /* use this size if the data type is not - defined in the table; for future proof */ + /* Data Specilization Ntx Nrx (from Bluetooth SIG's + HDP whitepaper)*/ + {BTIF_HL_DATA_TYPE_PULSE_OXIMETER, 9216, 256}, + {BTIF_HL_DATA_TYPE_BLOOD_PRESSURE_MON, 896, 224}, + {BTIF_HL_DATA_TYPE_BODY_THERMOMETER, 896, 224}, + {BTIF_HL_DATA_TYPE_BODY_WEIGHT_SCALE, 896, 224}, + {BTIF_HL_DATA_TYPE_GLUCOSE_METER, 896, 224}, + {BTIF_HL_DATA_TYPE_STEP_COUNTER, 6624, 224}, + {BTIF_HL_DATA_TYPE_BCA, 7730, 1230}, + {BTIF_HL_DATA_TYPE_PEAK_FLOW, 2030, 224}, + {BTIF_HL_DATA_TYPE_ACTIVITY_HUB, 5120, 224}, + {BTIF_HL_DATA_TYPE_AMM, 1024, 64}}; + +#define BTIF_HL_DATA_TABLE_SIZE \ + (sizeof(data_type_table) / sizeof(btif_hl_data_type_cfg_t)) +#define BTIF_HL_DEFAULT_SRC_TX_APDU_SIZE \ + 10240 /* use this size if the data type is not \ + defined in the table; for future proof */ +#define BTIF_HL_DEFAULT_SRC_RX_APDU_SIZE \ + 512 /* use this size if the data type is not \ + defined in the table; for future proof */ #define BTIF_HL_ECHO_MAX_TX_RX_APDU_SIZE 1024 /************************************************************************************ -** Static utility functions -************************************************************************************/ + * Static utility functions + ***********************************************************************************/ #define BTIF_IF_GET_NAME 16 -void btif_hl_display_calling_process_name(void) -{ - char name[16]; - prctl(BTIF_IF_GET_NAME, name, 0, 0, 0); - BTIF_TRACE_DEBUG("Process name (%s)", name); +void btif_hl_display_calling_process_name(void) { + char name[16]; + prctl(BTIF_IF_GET_NAME, name, 0, 0, 0); + BTIF_TRACE_DEBUG("Process name (%s)", name); } -#define BTIF_TIMEOUT_CCH_NO_DCH_MS (30 * 1000) +#define BTIF_TIMEOUT_CCH_NO_DCH_MS (30 * 1000) /******************************************************************************* -** -** Function btif_hl_if_channel_setup_pending -** -** Description check whether channel id is in setup pending state or not -** -** Returns bool -** -*******************************************************************************/ -bool btif_hl_if_channel_setup_pending(int channel_id, uint8_t *p_app_idx, uint8_t *p_mcl_idx) -{ - btif_hl_app_cb_t *p_acb; - btif_hl_mcl_cb_t *p_mcb; - uint8_t i, j; - bool found=false; - - *p_app_idx = 0; - *p_mcl_idx = 0; - for (i=0; i < BTA_HL_NUM_APPS ; i ++) - { - p_acb =BTIF_HL_GET_APP_CB_PTR(i); - if (p_acb->in_use) - { - for (j=0; j< BTA_HL_NUM_MCLS; j++) - { - p_mcb = BTIF_HL_GET_MCL_CB_PTR(i, j); - if (p_mcb->in_use && - p_mcb->is_connected && p_mcb->pcb.channel_id == channel_id ) - { - found = true; - *p_app_idx = i; - *p_mcl_idx = j; - break; - } - } + * + * Function btif_hl_if_channel_setup_pending + * + * Description check whether channel id is in setup pending state or not + * + * Returns bool + * + ******************************************************************************/ +bool btif_hl_if_channel_setup_pending(int channel_id, uint8_t* p_app_idx, + uint8_t* p_mcl_idx) { + btif_hl_app_cb_t* p_acb; + btif_hl_mcl_cb_t* p_mcb; + uint8_t i, j; + bool found = false; + + *p_app_idx = 0; + *p_mcl_idx = 0; + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + p_acb = BTIF_HL_GET_APP_CB_PTR(i); + if (p_acb->in_use) { + for (j = 0; j < BTA_HL_NUM_MCLS; j++) { + p_mcb = BTIF_HL_GET_MCL_CB_PTR(i, j); + if (p_mcb->in_use && p_mcb->is_connected && + p_mcb->pcb.channel_id == channel_id) { + found = true; + *p_app_idx = i; + *p_mcl_idx = j; + break; } - if (found) - break; + } } - BTIF_TRACE_DEBUG("%s found=%d channel_id=0x%08x", - __func__, found, channel_id, *p_app_idx, *p_mcl_idx); - return found; - + if (found) break; + } + BTIF_TRACE_DEBUG("%s found=%d channel_id=0x%08x", __func__, found, channel_id, + *p_app_idx, *p_mcl_idx); + return found; } /******************************************************************************* -** -** Function btif_hl_num_dchs_in_use -** -** Description find number of DCHs in use -** -** Returns uint8_t -*******************************************************************************/ -uint8_t btif_hl_num_dchs_in_use(uint8_t mcl_handle){ - - btif_hl_app_cb_t * p_acb; - btif_hl_mcl_cb_t *p_mcb; - uint8_t i,j,x; - uint8_t cnt=0; - - for (i=0; iin_use) - { - for (j=0; j < BTA_HL_NUM_MCLS ; j++) - { - if(p_acb->mcb[j].in_use) - BTIF_TRACE_DEBUG("btif_hl_num_dchs:mcb in use j=%d, mcl_handle=%d,mcb handle=%d", - j,mcl_handle, p_acb->mcb[j].mcl_handle); - if (p_acb->mcb[j].in_use && - (p_acb->mcb[j].mcl_handle == mcl_handle)) - { - p_mcb = &p_acb->mcb[j]; - BTIF_TRACE_DEBUG("btif_hl_num_dchs: mcl handle found j =%d",j); - for (x=0; x < BTA_HL_NUM_MDLS_PER_MCL ; x ++) - { - if (p_mcb->mdl[x].in_use) - { - BTIF_TRACE_DEBUG("btif_hl_num_dchs_in_use:found x =%d",x); - cnt++; - } - } - } + * + * Function btif_hl_num_dchs_in_use + * + * Description find number of DCHs in use + * + * Returns uint8_t + ******************************************************************************/ +uint8_t btif_hl_num_dchs_in_use(uint8_t mcl_handle) { + btif_hl_app_cb_t* p_acb; + btif_hl_mcl_cb_t* p_mcb; + uint8_t i, j, x; + uint8_t cnt = 0; + + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + BTIF_TRACE_DEBUG("btif_hl_num_dchs:i = %d", i); + p_acb = BTIF_HL_GET_APP_CB_PTR(i); + if (p_acb && p_acb->in_use) { + for (j = 0; j < BTA_HL_NUM_MCLS; j++) { + if (p_acb->mcb[j].in_use) + BTIF_TRACE_DEBUG( + "btif_hl_num_dchs:mcb in use j=%d, mcl_handle=%d,mcb handle=%d", + j, mcl_handle, p_acb->mcb[j].mcl_handle); + if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) { + p_mcb = &p_acb->mcb[j]; + BTIF_TRACE_DEBUG("btif_hl_num_dchs: mcl handle found j =%d", j); + for (x = 0; x < BTA_HL_NUM_MDLS_PER_MCL; x++) { + if (p_mcb->mdl[x].in_use) { + BTIF_TRACE_DEBUG("btif_hl_num_dchs_in_use:found x =%d", x); + cnt++; } + } } + } } + } - BTIF_TRACE_DEBUG("%s dch in use count=%d", __func__, cnt); - return cnt; + BTIF_TRACE_DEBUG("%s dch in use count=%d", __func__, cnt); + return cnt; } /******************************************************************************* -** -** Function btif_hl_timer_timeout -** -** Description Process timer timeout -** -** Returns void -*******************************************************************************/ -void btif_hl_timer_timeout(void *data) -{ - btif_hl_mcl_cb_t *p_mcb = (btif_hl_mcl_cb_t *)data; - - BTIF_TRACE_DEBUG("%s", __func__); - if (p_mcb->is_connected) { - BTIF_TRACE_DEBUG("Idle timeout Close CCH mcl_handle=%d", - p_mcb->mcl_handle); - BTA_HlCchClose(p_mcb->mcl_handle); - } else { - BTIF_TRACE_DEBUG("CCH idle timeout But CCH not connected"); - } + * + * Function btif_hl_timer_timeout + * + * Description Process timer timeout + * + * Returns void + ******************************************************************************/ +void btif_hl_timer_timeout(void* data) { + btif_hl_mcl_cb_t* p_mcb = (btif_hl_mcl_cb_t*)data; + + BTIF_TRACE_DEBUG("%s", __func__); + if (p_mcb->is_connected) { + BTIF_TRACE_DEBUG("Idle timeout Close CCH mcl_handle=%d", p_mcb->mcl_handle); + BTA_HlCchClose(p_mcb->mcl_handle); + } else { + BTIF_TRACE_DEBUG("CCH idle timeout But CCH not connected"); + } } /******************************************************************************* -** -** Function btif_hl_stop_cch_timer -** -** Description stop CCH timer -** -** Returns void -*******************************************************************************/ -void btif_hl_stop_cch_timer(uint8_t app_idx, uint8_t mcl_idx) -{ - btif_hl_mcl_cb_t *p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + * + * Function btif_hl_stop_cch_timer + * + * Description stop CCH timer + * + * Returns void + ******************************************************************************/ +void btif_hl_stop_cch_timer(uint8_t app_idx, uint8_t mcl_idx) { + btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - BTIF_TRACE_DEBUG("%s app_idx=%d, mcl_idx=%d", __func__, app_idx, mcl_idx); - alarm_cancel(p_mcb->cch_timer); + BTIF_TRACE_DEBUG("%s app_idx=%d, mcl_idx=%d", __func__, app_idx, mcl_idx); + alarm_cancel(p_mcb->cch_timer); } /******************************************************************************* -** -** Function btif_hl_start_cch_timer -** -** Description start CCH timer -** -** Returns void -*******************************************************************************/ -void btif_hl_start_cch_timer(uint8_t app_idx, uint8_t mcl_idx) -{ - btif_hl_mcl_cb_t *p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - BTIF_TRACE_DEBUG("%s app_idx=%d, mcl_idx=%d", __func__, app_idx, mcl_idx); - - alarm_free(p_mcb->cch_timer); - p_mcb->cch_timer = alarm_new("btif_hl.mcl_cch_timer"); - alarm_set_on_queue(p_mcb->cch_timer, BTIF_TIMEOUT_CCH_NO_DCH_MS, - btif_hl_timer_timeout, p_mcb, - btu_general_alarm_queue); + * + * Function btif_hl_start_cch_timer + * + * Description start CCH timer + * + * Returns void + ******************************************************************************/ +void btif_hl_start_cch_timer(uint8_t app_idx, uint8_t mcl_idx) { + btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + BTIF_TRACE_DEBUG("%s app_idx=%d, mcl_idx=%d", __func__, app_idx, mcl_idx); + + alarm_free(p_mcb->cch_timer); + p_mcb->cch_timer = alarm_new("btif_hl.mcl_cch_timer"); + alarm_set_on_queue(p_mcb->cch_timer, BTIF_TIMEOUT_CCH_NO_DCH_MS, + btif_hl_timer_timeout, p_mcb, btu_general_alarm_queue); } /******************************************************************************* -** -** Function btif_hl_find_mdl_idx -** -** Description Find the MDL index using MDL ID -** -** Returns bool -** -*******************************************************************************/ -static bool btif_hl_find_mdl_idx(uint8_t app_idx, uint8_t mcl_idx, uint16_t mdl_id, - uint8_t *p_mdl_idx) -{ - btif_hl_mcl_cb_t *p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - bool found=false; - uint8_t i; - - for (i=0; i < BTA_HL_NUM_MDLS_PER_MCL ; i ++) - { - if (p_mcb->mdl[i].in_use && - (mdl_id !=0) && - (p_mcb->mdl[i].mdl_id== mdl_id)) - { - found = true; - *p_mdl_idx = i; - break; - } + * + * Function btif_hl_find_mdl_idx + * + * Description Find the MDL index using MDL ID + * + * Returns bool + * + ******************************************************************************/ +static bool btif_hl_find_mdl_idx(uint8_t app_idx, uint8_t mcl_idx, + uint16_t mdl_id, uint8_t* p_mdl_idx) { + btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + bool found = false; + uint8_t i; + + for (i = 0; i < BTA_HL_NUM_MDLS_PER_MCL; i++) { + if (p_mcb->mdl[i].in_use && (mdl_id != 0) && + (p_mcb->mdl[i].mdl_id == mdl_id)) { + found = true; + *p_mdl_idx = i; + break; } + } - BTIF_TRACE_DEBUG("%s found=%d mdl_id=%d mdl_idx=%d ", - __func__,found, mdl_id, i); + BTIF_TRACE_DEBUG("%s found=%d mdl_id=%d mdl_idx=%d ", __func__, found, mdl_id, + i); - return found; + return found; } /******************************************************************************* -** -** Function btif_hl_is_the_first_reliable_existed -** -** Description This function checks whether the first reliable DCH channel -** has been setup on the MCL or not -** -** Returns bool - true exist -** false does not exist -** -*******************************************************************************/ -bool btif_hl_is_the_first_reliable_existed(uint8_t app_idx, uint8_t mcl_idx ) -{ - btif_hl_mcl_cb_t *p_mcb =BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - bool is_existed =false; - uint8_t i ; - - for (i=0; i< BTA_HL_NUM_MDLS_PER_MCL; i++) - { - if (p_mcb->mdl[i].in_use && p_mcb->mdl[i].is_the_first_reliable) - { - is_existed = true; - break; - } + * + * Function btif_hl_is_the_first_reliable_existed + * + * Description This function checks whether the first reliable DCH channel + * has been setup on the MCL or not + * + * Returns bool - true exist + * false does not exist + * + ******************************************************************************/ +bool btif_hl_is_the_first_reliable_existed(uint8_t app_idx, uint8_t mcl_idx) { + btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + bool is_existed = false; + uint8_t i; + + for (i = 0; i < BTA_HL_NUM_MDLS_PER_MCL; i++) { + if (p_mcb->mdl[i].in_use && p_mcb->mdl[i].is_the_first_reliable) { + is_existed = true; + break; } + } - BTIF_TRACE_DEBUG("bta_hl_is_the_first_reliable_existed is_existed=%d ",is_existed ); - return is_existed; + BTIF_TRACE_DEBUG("bta_hl_is_the_first_reliable_existed is_existed=%d ", + is_existed); + return is_existed; } /******************************************************************************* -** -** Function btif_hl_clean_delete_mdl -** -** Description Cleanup the delete mdl control block -** -** Returns Nothing -** -*******************************************************************************/ -static void btif_hl_clean_delete_mdl(btif_hl_delete_mdl_t *p_cb) -{ - BTIF_TRACE_DEBUG("%s", __func__ ); - memset(p_cb, 0 , sizeof(btif_hl_delete_mdl_t)); + * + * Function btif_hl_clean_delete_mdl + * + * Description Cleanup the delete mdl control block + * + * Returns Nothing + * + ******************************************************************************/ +static void btif_hl_clean_delete_mdl(btif_hl_delete_mdl_t* p_cb) { + BTIF_TRACE_DEBUG("%s", __func__); + memset(p_cb, 0, sizeof(btif_hl_delete_mdl_t)); } /******************************************************************************* -** -** Function btif_hl_clean_pcb -** -** Description Cleanup the pending chan control block -** -** Returns Nothing -** -*******************************************************************************/ -static void btif_hl_clean_pcb(btif_hl_pending_chan_cb_t *p_pcb) -{ - BTIF_TRACE_DEBUG("%s", __func__ ); - memset(p_pcb, 0 , sizeof(btif_hl_pending_chan_cb_t)); + * + * Function btif_hl_clean_pcb + * + * Description Cleanup the pending chan control block + * + * Returns Nothing + * + ******************************************************************************/ +static void btif_hl_clean_pcb(btif_hl_pending_chan_cb_t* p_pcb) { + BTIF_TRACE_DEBUG("%s", __func__); + memset(p_pcb, 0, sizeof(btif_hl_pending_chan_cb_t)); } /******************************************************************************* -** -** Function btif_hl_clean_mdl_cb -** -** Description Cleanup the MDL control block -** -** Returns Nothing -** -*******************************************************************************/ -static void btif_hl_clean_mdl_cb(btif_hl_mdl_cb_t *p_dcb) -{ - BTIF_TRACE_DEBUG("%s", __func__ ); - osi_free_and_reset((void **)&p_dcb->p_rx_pkt); - osi_free_and_reset((void **)&p_dcb->p_tx_pkt); - memset(p_dcb, 0 , sizeof(btif_hl_mdl_cb_t)); + * + * Function btif_hl_clean_mdl_cb + * + * Description Cleanup the MDL control block + * + * Returns Nothing + * + ******************************************************************************/ +static void btif_hl_clean_mdl_cb(btif_hl_mdl_cb_t* p_dcb) { + BTIF_TRACE_DEBUG("%s", __func__); + osi_free_and_reset((void**)&p_dcb->p_rx_pkt); + osi_free_and_reset((void**)&p_dcb->p_tx_pkt); + memset(p_dcb, 0, sizeof(btif_hl_mdl_cb_t)); } /******************************************************************************* -** -** Function btif_hl_reset_mcb -** -** Description Reset MCL control block -** -** Returns bool -** -*******************************************************************************/ -static void btif_hl_clean_mcl_cb(uint8_t app_idx, uint8_t mcl_idx) -{ - btif_hl_mcl_cb_t *p_mcb; - BTIF_TRACE_DEBUG("%s app_idx=%d, mcl_idx=%d", __func__,app_idx, mcl_idx); - p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - alarm_free(p_mcb->cch_timer); - memset(p_mcb, 0, sizeof(btif_hl_mcl_cb_t)); + * + * Function btif_hl_reset_mcb + * + * Description Reset MCL control block + * + * Returns bool + * + ******************************************************************************/ +static void btif_hl_clean_mcl_cb(uint8_t app_idx, uint8_t mcl_idx) { + btif_hl_mcl_cb_t* p_mcb; + BTIF_TRACE_DEBUG("%s app_idx=%d, mcl_idx=%d", __func__, app_idx, mcl_idx); + p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + alarm_free(p_mcb->cch_timer); + memset(p_mcb, 0, sizeof(btif_hl_mcl_cb_t)); } /******************************************************************************* -** -** Function btif_hl_find_sdp_idx_using_mdep_filter -** -** Description This function finds the SDP record index using MDEP filter parameters -** -** Returns bool -** -*******************************************************************************/ -static void btif_hl_reset_mdep_filter(uint8_t app_idx) -{ - btif_hl_app_cb_t *p_acb =BTIF_HL_GET_APP_CB_PTR(app_idx); - p_acb->filter.num_elems = 0; + * + * Function btif_hl_find_sdp_idx_using_mdep_filter + * + * Description This function finds the SDP record index using MDEP filter + *parameters + * + * Returns bool + * + ******************************************************************************/ +static void btif_hl_reset_mdep_filter(uint8_t app_idx) { + btif_hl_app_cb_t* p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + p_acb->filter.num_elems = 0; } /******************************************************************************* -** -** Function btif_hl_find_sdp_idx_using_mdep_filter -** -** Description This function finds the SDP record index using MDEP filter parameters -** -** Returns bool -** -*******************************************************************************/ -static bool btif_hl_find_sdp_idx_using_mdep_filter(uint8_t app_idx, uint8_t mcl_idx, uint8_t *p_sdp_idx) -{ - btif_hl_app_cb_t *p_acb =BTIF_HL_GET_APP_CB_PTR(app_idx); - btif_hl_mcl_cb_t *p_mcb =BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - uint8_t i, j, num_recs,num_elems, num_mdeps, mdep_idx; - tBTA_HL_MDEP_ROLE peer_mdep_role; - uint16_t data_type; - tBTA_HL_SDP_MDEP_CFG *p_mdep; - bool found = false; - bool elem_found; - - BTIF_TRACE_DEBUG("btif_hl_find_sdp_idx_using_mdep_filter"); - num_recs = p_mcb->sdp.num_recs; - num_elems = p_acb->filter.num_elems; - if (!num_elems) - { - BTIF_TRACE_DEBUG("btif_hl_find_sdp_idx_using_mdep_filter num_elem=0"); - *p_sdp_idx = 0; + * + * Function btif_hl_find_sdp_idx_using_mdep_filter + * + * Description This function finds the SDP record index using MDEP filter + *parameters + * + * Returns bool + * + ******************************************************************************/ +static bool btif_hl_find_sdp_idx_using_mdep_filter(uint8_t app_idx, + uint8_t mcl_idx, + uint8_t* p_sdp_idx) { + btif_hl_app_cb_t* p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + uint8_t i, j, num_recs, num_elems, num_mdeps, mdep_idx; + tBTA_HL_MDEP_ROLE peer_mdep_role; + uint16_t data_type; + tBTA_HL_SDP_MDEP_CFG* p_mdep; + bool found = false; + bool elem_found; + + BTIF_TRACE_DEBUG("btif_hl_find_sdp_idx_using_mdep_filter"); + num_recs = p_mcb->sdp.num_recs; + num_elems = p_acb->filter.num_elems; + if (!num_elems) { + BTIF_TRACE_DEBUG("btif_hl_find_sdp_idx_using_mdep_filter num_elem=0"); + *p_sdp_idx = 0; + found = true; + return found; + } + + for (i = 0; i < num_recs; i++) { + num_mdeps = p_mcb->sdp.sdp_rec[i].num_mdeps; + for (j = 0; j < num_elems; j++) { + data_type = p_acb->filter.elem[j].data_type; + peer_mdep_role = p_acb->filter.elem[j].peer_mdep_role; + elem_found = false; + mdep_idx = 0; + while (!elem_found && mdep_idx < num_mdeps) { + p_mdep = &(p_mcb->sdp.sdp_rec[i].mdep_cfg[mdep_idx]); + if ((p_mdep->data_type == data_type) && + (p_mdep->mdep_role == peer_mdep_role)) { + elem_found = true; + } else { + mdep_idx++; + } + } + + if (!elem_found) { + found = false; + break; + } else { found = true; - return found; + } } - for (i=0; isdp.sdp_rec[i].num_mdeps; - for (j=0; jfilter.elem[j].data_type; - peer_mdep_role = p_acb->filter.elem[j].peer_mdep_role; - elem_found = false; - mdep_idx=0; - while (!elem_found && mdep_idx < num_mdeps ) - { - p_mdep = &(p_mcb->sdp.sdp_rec[i].mdep_cfg[mdep_idx]); - if ( (p_mdep->data_type == data_type) && - (p_mdep->mdep_role == peer_mdep_role) ) - { - elem_found = true; - } - else - { - mdep_idx++; - } - } - - if (!elem_found) - { - found = false; - break; - } - else - { - found = true; - } - } - - if (found) - { - BTIF_TRACE_DEBUG("btif_hl_find_sdp_idx_using_mdep_filter found idx=%d",i); - *p_sdp_idx = i; - break; - } + if (found) { + BTIF_TRACE_DEBUG("btif_hl_find_sdp_idx_using_mdep_filter found idx=%d", + i); + *p_sdp_idx = i; + break; } + } - BTIF_TRACE_DEBUG("%s found=%d sdp_idx=%d",__func__ , found, *p_sdp_idx); + BTIF_TRACE_DEBUG("%s found=%d sdp_idx=%d", __func__, found, *p_sdp_idx); - btif_hl_reset_mdep_filter(app_idx); + btif_hl_reset_mdep_filter(app_idx); - return found; + return found; } /******************************************************************************* -** -** Function btif_hl_is_reconnect_possible -** -** Description check reconnect is possible or not -** -** Returns bool -** -*******************************************************************************/ -bool btif_hl_is_reconnect_possible(uint8_t app_idx, uint8_t mcl_idx, int mdep_cfg_idx, - tBTA_HL_DCH_OPEN_PARAM *p_dch_open_api, tBTA_HL_MDL_ID *p_mdl_id) -{ - btif_hl_app_cb_t *p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - btif_hl_mcl_cb_t *p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - tBTA_HL_DCH_CFG local_cfg = p_dch_open_api->local_cfg; - tBTA_HL_DCH_MODE dch_mode = BTA_HL_DCH_MODE_RELIABLE; - bool use_mdl_dch_mode=false; - btif_hl_mdl_cfg_t *p_mdl; - btif_hl_mdl_cfg_t *p_mdl1; - uint8_t i, j; - bool is_reconnect_ok=false; - bool stream_mode_avail=false; - uint16_t data_type = p_acb->sup_feature.mdep[mdep_cfg_idx].mdep_cfg.data_cfg[0].data_type; - tBTA_HL_MDEP_ID peer_mdep_id = p_dch_open_api->peer_mdep_id; - uint8_t mdl_idx; - - BTIF_TRACE_DEBUG("%s app_idx=%d mcl_idx=%d mdep_cfg_idx=%d", - __func__, app_idx, mcl_idx, mdep_cfg_idx ); - switch (local_cfg) - { - case BTA_HL_DCH_CFG_NO_PREF: - if (!btif_hl_is_the_first_reliable_existed(app_idx, mcl_idx)) - { - dch_mode = BTA_HL_DCH_MODE_RELIABLE; - } - else - { - use_mdl_dch_mode = true; - } - break; - case BTA_HL_DCH_CFG_RELIABLE: - dch_mode = BTA_HL_DCH_MODE_RELIABLE; - break; - case BTA_HL_DCH_CFG_STREAMING: - dch_mode = BTA_HL_DCH_MODE_STREAMING; - break; - default: - BTIF_TRACE_ERROR("Invalid local_cfg=%d",local_cfg ); - return is_reconnect_ok; + * + * Function btif_hl_is_reconnect_possible + * + * Description check reconnect is possible or not + * + * Returns bool + * + ******************************************************************************/ +bool btif_hl_is_reconnect_possible(uint8_t app_idx, uint8_t mcl_idx, + int mdep_cfg_idx, + tBTA_HL_DCH_OPEN_PARAM* p_dch_open_api, + tBTA_HL_MDL_ID* p_mdl_id) { + btif_hl_app_cb_t* p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + tBTA_HL_DCH_CFG local_cfg = p_dch_open_api->local_cfg; + tBTA_HL_DCH_MODE dch_mode = BTA_HL_DCH_MODE_RELIABLE; + bool use_mdl_dch_mode = false; + btif_hl_mdl_cfg_t* p_mdl; + btif_hl_mdl_cfg_t* p_mdl1; + uint8_t i, j; + bool is_reconnect_ok = false; + bool stream_mode_avail = false; + uint16_t data_type = + p_acb->sup_feature.mdep[mdep_cfg_idx].mdep_cfg.data_cfg[0].data_type; + tBTA_HL_MDEP_ID peer_mdep_id = p_dch_open_api->peer_mdep_id; + uint8_t mdl_idx; + + BTIF_TRACE_DEBUG("%s app_idx=%d mcl_idx=%d mdep_cfg_idx=%d", __func__, + app_idx, mcl_idx, mdep_cfg_idx); + switch (local_cfg) { + case BTA_HL_DCH_CFG_NO_PREF: + if (!btif_hl_is_the_first_reliable_existed(app_idx, mcl_idx)) { + dch_mode = BTA_HL_DCH_MODE_RELIABLE; + } else { + use_mdl_dch_mode = true; + } + break; + case BTA_HL_DCH_CFG_RELIABLE: + dch_mode = BTA_HL_DCH_MODE_RELIABLE; + break; + case BTA_HL_DCH_CFG_STREAMING: + dch_mode = BTA_HL_DCH_MODE_STREAMING; + break; + default: + BTIF_TRACE_ERROR("Invalid local_cfg=%d", local_cfg); + return is_reconnect_ok; + break; + } + + BTIF_TRACE_DEBUG("local_cfg=%d use_mdl_dch_mode=%d dch_mode=%d ", local_cfg, + use_mdl_dch_mode, dch_mode); + + for (i = 0, p_mdl = &p_acb->mdl_cfg[0]; i < BTA_HL_NUM_MDL_CFGS; + i++, p_mdl++) { + if (p_mdl->base.active && p_mdl->extra.data_type == data_type && + (p_mdl->extra.peer_mdep_id != BTA_HL_INVALID_MDEP_ID && + p_mdl->extra.peer_mdep_id == peer_mdep_id) && + memcpy(p_mdl->base.peer_bd_addr, p_mcb->bd_addr, sizeof(BD_ADDR)) && + p_mdl->base.mdl_id && + !btif_hl_find_mdl_idx(app_idx, mcl_idx, p_mdl->base.mdl_id, &mdl_idx)) { + BTIF_TRACE_DEBUG("i=%d Matched active=%d mdl_id =%d, mdl_dch_mode=%d", + i, p_mdl->base.active, p_mdl->base.mdl_id, + p_mdl->base.dch_mode); + if (!use_mdl_dch_mode) { + if (p_mdl->base.dch_mode == dch_mode) { + is_reconnect_ok = true; + *p_mdl_id = p_mdl->base.mdl_id; + BTIF_TRACE_DEBUG("reconnect is possible dch_mode=%d mdl_id=%d", + dch_mode, p_mdl->base.mdl_id); + break; + } + } else { + is_reconnect_ok = true; + for (j = i, p_mdl1 = &p_acb->mdl_cfg[i]; j < BTA_HL_NUM_MDL_CFGS; + j++, p_mdl1++) { + if (p_mdl1->base.active && p_mdl1->extra.data_type == data_type && + (p_mdl1->extra.peer_mdep_id != BTA_HL_INVALID_MDEP_ID && + p_mdl1->extra.peer_mdep_id == peer_mdep_id) && + memcpy(p_mdl1->base.peer_bd_addr, p_mcb->bd_addr, + sizeof(BD_ADDR)) && + p_mdl1->base.dch_mode == BTA_HL_DCH_MODE_STREAMING) { + stream_mode_avail = true; + BTIF_TRACE_DEBUG("found streaming mode mdl index=%d", j); break; - - } - - BTIF_TRACE_DEBUG("local_cfg=%d use_mdl_dch_mode=%d dch_mode=%d ", - local_cfg, use_mdl_dch_mode, dch_mode ); - - for (i=0, p_mdl=&p_acb->mdl_cfg[0] ; i< BTA_HL_NUM_MDL_CFGS; i++, p_mdl++ ) - { - if (p_mdl->base.active && - p_mdl->extra.data_type ==data_type && - (p_mdl->extra.peer_mdep_id != BTA_HL_INVALID_MDEP_ID && p_mdl->extra.peer_mdep_id == peer_mdep_id) && - memcpy(p_mdl->base.peer_bd_addr, p_mcb->bd_addr,sizeof(BD_ADDR) ) && - p_mdl->base.mdl_id && - !btif_hl_find_mdl_idx(app_idx, mcl_idx,p_mdl->base.mdl_id, &mdl_idx)) - { - BTIF_TRACE_DEBUG("i=%d Matched active=%d mdl_id =%d, mdl_dch_mode=%d", - i, p_mdl->base.active, p_mdl->base.mdl_id,p_mdl->base.dch_mode); - if (!use_mdl_dch_mode) - { - if (p_mdl->base.dch_mode == dch_mode) - { - is_reconnect_ok = true; - *p_mdl_id = p_mdl->base.mdl_id; - BTIF_TRACE_DEBUG("reconnect is possible dch_mode=%d mdl_id=%d", dch_mode, p_mdl->base.mdl_id ); - break; - } - } - else - { - is_reconnect_ok = true; - for (j=i, p_mdl1=&p_acb->mdl_cfg[i]; j< BTA_HL_NUM_MDL_CFGS; j++, p_mdl1++) - { - if (p_mdl1->base.active && - p_mdl1->extra.data_type == data_type && - (p_mdl1->extra.peer_mdep_id != BTA_HL_INVALID_MDEP_ID && p_mdl1->extra.peer_mdep_id == peer_mdep_id) && - memcpy(p_mdl1->base.peer_bd_addr, p_mcb->bd_addr,sizeof(BD_ADDR)) && - p_mdl1->base.dch_mode == BTA_HL_DCH_MODE_STREAMING) - { - stream_mode_avail = true; - BTIF_TRACE_DEBUG("found streaming mode mdl index=%d", j); - break; - } - } - - if (stream_mode_avail) - { - dch_mode = BTA_HL_DCH_MODE_STREAMING; - *p_mdl_id = p_mdl1->base.mdl_id; - BTIF_TRACE_DEBUG("reconnect is ok index=%d dch_mode=streaming mdl_id=%d", j, *p_mdl_id); - break; - } - else - { - dch_mode= p_mdl->base.dch_mode; - *p_mdl_id = p_mdl->base.mdl_id; - BTIF_TRACE_DEBUG("reconnect is ok index=%d dch_mode=%d mdl_id=%d", i, p_mdl->base.dch_mode, *p_mdl_id); - break; - - } - } - + } } + if (stream_mode_avail) { + dch_mode = BTA_HL_DCH_MODE_STREAMING; + *p_mdl_id = p_mdl1->base.mdl_id; + BTIF_TRACE_DEBUG( + "reconnect is ok index=%d dch_mode=streaming mdl_id=%d", j, + *p_mdl_id); + break; + } else { + dch_mode = p_mdl->base.dch_mode; + *p_mdl_id = p_mdl->base.mdl_id; + BTIF_TRACE_DEBUG("reconnect is ok index=%d dch_mode=%d mdl_id=%d", i, + p_mdl->base.dch_mode, *p_mdl_id); + break; + } + } } + } - BTIF_TRACE_DEBUG("is_reconnect_ok dch_mode=%d mdl_id=%d",is_reconnect_ok, dch_mode, *p_mdl_id); - return is_reconnect_ok; + BTIF_TRACE_DEBUG("is_reconnect_ok dch_mode=%d mdl_id=%d", is_reconnect_ok, + dch_mode, *p_mdl_id); + return is_reconnect_ok; } /******************************************************************************* -** -** Function btif_hl_dch_open -** -** Description Process DCH open request using the DCH Open API parameters -** -** Returns bool -** -*******************************************************************************/ + * + * Function btif_hl_dch_open + * + * Description Process DCH open request using the DCH Open API parameters + * + * Returns bool + * + ******************************************************************************/ bool btif_hl_dch_open(uint8_t app_id, BD_ADDR bd_addr, - tBTA_HL_DCH_OPEN_PARAM *p_dch_open_api, - int mdep_cfg_idx, - btif_hl_pend_dch_op_t op, int *channel_id){ - btif_hl_app_cb_t *p_acb; - btif_hl_mcl_cb_t *p_mcb; - btif_hl_pending_chan_cb_t *p_pcb; - uint8_t app_idx, mcl_idx; - bool status = false; - tBTA_HL_MDL_ID mdl_id; - tBTA_HL_DCH_RECONNECT_PARAM reconnect_param; - - BTIF_TRACE_DEBUG("%s app_id=%d ", - __func__, app_id ); - BTIF_TRACE_DEBUG("DB [%02x:%02x:%02x:%02x:%02x:%02x]", - bd_addr[0], bd_addr[1],bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]); - - if (btif_hl_find_app_idx(app_id, &app_idx)) - { - if (btif_hl_find_mcl_idx(app_idx, bd_addr , &mcl_idx)) - { - p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - - p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); - if (!p_pcb->in_use) - { - p_mcb->req_ctrl_psm = p_dch_open_api->ctrl_psm; - - p_pcb->in_use = true; - *channel_id = - p_pcb->channel_id = (int) btif_hl_get_next_channel_id(app_id); - p_pcb->cb_state = BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING; - p_pcb->mdep_cfg_idx = mdep_cfg_idx; - memcpy(p_pcb->bd_addr, bd_addr, sizeof(BD_ADDR)); - p_pcb->op = op; - - if (p_mcb->sdp.num_recs) - { - if (p_mcb->valid_sdp_idx) - { - p_dch_open_api->ctrl_psm = p_mcb->ctrl_psm; - } - - if (!btif_hl_is_reconnect_possible(app_idx, mcl_idx, mdep_cfg_idx, p_dch_open_api, &mdl_id )) - { - - BTIF_TRACE_DEBUG("Issue DCH open" ); - BTA_HlDchOpen(p_mcb->mcl_handle, p_dch_open_api); - } - else - { - reconnect_param.ctrl_psm = p_mcb->ctrl_psm; - reconnect_param.mdl_id = mdl_id;; - BTIF_TRACE_DEBUG("Issue Reconnect ctrl_psm=0x%x mdl_id=0x%x",reconnect_param.ctrl_psm, reconnect_param.mdl_id ); - BTA_HlDchReconnect(p_mcb->mcl_handle, &reconnect_param); - } - - status = true; - } - else - { - p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - p_mcb->cch_oper = BTIF_HL_CCH_OP_DCH_OPEN; - BTA_HlSdpQuery(app_id,p_acb->app_handle, bd_addr); - status = true; - } - } + tBTA_HL_DCH_OPEN_PARAM* p_dch_open_api, int mdep_cfg_idx, + btif_hl_pend_dch_op_t op, int* channel_id) { + btif_hl_app_cb_t* p_acb; + btif_hl_mcl_cb_t* p_mcb; + btif_hl_pending_chan_cb_t* p_pcb; + uint8_t app_idx, mcl_idx; + bool status = false; + tBTA_HL_MDL_ID mdl_id; + tBTA_HL_DCH_RECONNECT_PARAM reconnect_param; + + BTIF_TRACE_DEBUG("%s app_id=%d ", __func__, app_id); + BTIF_TRACE_DEBUG("DB [%02x:%02x:%02x:%02x:%02x:%02x]", bd_addr[0], bd_addr[1], + bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]); + + if (btif_hl_find_app_idx(app_id, &app_idx)) { + if (btif_hl_find_mcl_idx(app_idx, bd_addr, &mcl_idx)) { + p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + + p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); + if (!p_pcb->in_use) { + p_mcb->req_ctrl_psm = p_dch_open_api->ctrl_psm; + + p_pcb->in_use = true; + *channel_id = p_pcb->channel_id = + (int)btif_hl_get_next_channel_id(app_id); + p_pcb->cb_state = BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING; + p_pcb->mdep_cfg_idx = mdep_cfg_idx; + memcpy(p_pcb->bd_addr, bd_addr, sizeof(BD_ADDR)); + p_pcb->op = op; + + if (p_mcb->sdp.num_recs) { + if (p_mcb->valid_sdp_idx) { + p_dch_open_api->ctrl_psm = p_mcb->ctrl_psm; + } + + if (!btif_hl_is_reconnect_possible(app_idx, mcl_idx, mdep_cfg_idx, + p_dch_open_api, &mdl_id)) { + BTIF_TRACE_DEBUG("Issue DCH open"); + BTA_HlDchOpen(p_mcb->mcl_handle, p_dch_open_api); + } else { + reconnect_param.ctrl_psm = p_mcb->ctrl_psm; + reconnect_param.mdl_id = mdl_id; + ; + BTIF_TRACE_DEBUG("Issue Reconnect ctrl_psm=0x%x mdl_id=0x%x", + reconnect_param.ctrl_psm, reconnect_param.mdl_id); + BTA_HlDchReconnect(p_mcb->mcl_handle, &reconnect_param); + } + + status = true; + } else { + p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + p_mcb->cch_oper = BTIF_HL_CCH_OP_DCH_OPEN; + BTA_HlSdpQuery(app_id, p_acb->app_handle, bd_addr); + status = true; } + } } + } - BTIF_TRACE_DEBUG("status=%d ", status); - return status; + BTIF_TRACE_DEBUG("status=%d ", status); + return status; } /******************************************************************************* -** -** Function btif_hl_copy_bda -** -** Description copy bt_bdaddr_t to BD_ADDR format -** -** Returns void -** -*******************************************************************************/ -void btif_hl_copy_bda(bt_bdaddr_t *bd_addr, BD_ADDR bda){ - uint8_t i; - for (i=0; i<6; i++) - { - bd_addr->address[i] = bda[i] ; - } + * + * Function btif_hl_copy_bda + * + * Description copy bt_bdaddr_t to BD_ADDR format + * + * Returns void + * + ******************************************************************************/ +void btif_hl_copy_bda(bt_bdaddr_t* bd_addr, BD_ADDR bda) { + uint8_t i; + for (i = 0; i < 6; i++) { + bd_addr->address[i] = bda[i]; + } } /******************************************************************************* -** -** Function btif_hl_copy_bda -** -** Description display bt_bdaddr_t -** -** Returns bool -** -*******************************************************************************/ -void btif_hl_display_bt_bda(bt_bdaddr_t *bd_addr){ - BTIF_TRACE_DEBUG("DB [%02x:%02x:%02x:%02x:%02x:%02x]", - bd_addr->address[0], bd_addr->address[1], bd_addr->address[2], - bd_addr->address[3], bd_addr->address[4], bd_addr->address[5]); + * + * Function btif_hl_copy_bda + * + * Description display bt_bdaddr_t + * + * Returns bool + * + ******************************************************************************/ +void btif_hl_display_bt_bda(bt_bdaddr_t* bd_addr) { + BTIF_TRACE_DEBUG("DB [%02x:%02x:%02x:%02x:%02x:%02x]", bd_addr->address[0], + bd_addr->address[1], bd_addr->address[2], + bd_addr->address[3], bd_addr->address[4], + bd_addr->address[5]); } /******************************************************************************* -** -** Function btif_hl_dch_abort -** -** Description Process DCH abort request -** -** Returns Nothing -** -*******************************************************************************/ -void btif_hl_dch_abort(uint8_t app_idx, uint8_t mcl_idx){ - btif_hl_mcl_cb_t *p_mcb; - - BTIF_TRACE_DEBUG("%s app_idx=%d mcl_idx=%d",__func__, app_idx, mcl_idx ); - p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - if (p_mcb->is_connected) - { - BTA_HlDchAbort(p_mcb->mcl_handle); - } - else - { - p_mcb->pcb.abort_pending = true; - } - + * + * Function btif_hl_dch_abort + * + * Description Process DCH abort request + * + * Returns Nothing + * + ******************************************************************************/ +void btif_hl_dch_abort(uint8_t app_idx, uint8_t mcl_idx) { + btif_hl_mcl_cb_t* p_mcb; + + BTIF_TRACE_DEBUG("%s app_idx=%d mcl_idx=%d", __func__, app_idx, mcl_idx); + p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + if (p_mcb->is_connected) { + BTA_HlDchAbort(p_mcb->mcl_handle); + } else { + p_mcb->pcb.abort_pending = true; + } } /******************************************************************************* -** -** Function btif_hl_cch_open -** -** Description Process CCH open request -** -** Returns Nothing -** -*******************************************************************************/ + * + * Function btif_hl_cch_open + * + * Description Process CCH open request + * + * Returns Nothing + * + ******************************************************************************/ bool btif_hl_cch_open(uint8_t app_id, BD_ADDR bd_addr, uint16_t ctrl_psm, - int mdep_cfg_idx, - btif_hl_pend_dch_op_t op, int *channel_id){ - - btif_hl_app_cb_t *p_acb; - btif_hl_mcl_cb_t *p_mcb; - btif_hl_pending_chan_cb_t *p_pcb; - uint8_t app_idx, mcl_idx; - bool status = true; - - BTIF_TRACE_DEBUG("%s app_id=%d ctrl_psm=%d mdep_cfg_idx=%d op=%d", - __func__, app_id, ctrl_psm, mdep_cfg_idx, op); - BTIF_TRACE_DEBUG("DB [%02x:%02x:%02x:%02x:%02x:%02x]", - bd_addr[0], bd_addr[1],bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]); - - if (btif_hl_find_app_idx(app_id, &app_idx)) - { - p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - - if (!btif_hl_find_mcl_idx(app_idx, bd_addr, &mcl_idx)) - { - if (btif_hl_find_avail_mcl_idx(app_idx, &mcl_idx)) - { - p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - alarm_free(p_mcb->cch_timer); - memset(p_mcb, 0, sizeof(btif_hl_mcl_cb_t)); - p_mcb->in_use = true; - bdcpy(p_mcb->bd_addr, bd_addr); - - if (!ctrl_psm) - { - p_mcb->cch_oper = BTIF_HL_CCH_OP_MDEP_FILTERING; - } - else - { - p_mcb->cch_oper = BTIF_HL_CCH_OP_MATCHED_CTRL_PSM; - p_mcb->req_ctrl_psm = ctrl_psm; - } + int mdep_cfg_idx, btif_hl_pend_dch_op_t op, + int* channel_id) { + btif_hl_app_cb_t* p_acb; + btif_hl_mcl_cb_t* p_mcb; + btif_hl_pending_chan_cb_t* p_pcb; + uint8_t app_idx, mcl_idx; + bool status = true; + + BTIF_TRACE_DEBUG("%s app_id=%d ctrl_psm=%d mdep_cfg_idx=%d op=%d", __func__, + app_id, ctrl_psm, mdep_cfg_idx, op); + BTIF_TRACE_DEBUG("DB [%02x:%02x:%02x:%02x:%02x:%02x]", bd_addr[0], bd_addr[1], + bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]); + + if (btif_hl_find_app_idx(app_id, &app_idx)) { + p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); - p_pcb->in_use = true; - p_pcb->mdep_cfg_idx = mdep_cfg_idx; - memcpy(p_pcb->bd_addr, bd_addr, sizeof(BD_ADDR)); - p_pcb->op = op; + if (!btif_hl_find_mcl_idx(app_idx, bd_addr, &mcl_idx)) { + if (btif_hl_find_avail_mcl_idx(app_idx, &mcl_idx)) { + p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + alarm_free(p_mcb->cch_timer); + memset(p_mcb, 0, sizeof(btif_hl_mcl_cb_t)); + p_mcb->in_use = true; + bdcpy(p_mcb->bd_addr, bd_addr); - switch (op) - { - case BTIF_HL_PEND_DCH_OP_OPEN: - *channel_id = - p_pcb->channel_id = (int) btif_hl_get_next_channel_id(app_id); - p_pcb->cb_state = BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING; - break; - case BTIF_HL_PEND_DCH_OP_DELETE_MDL: - p_pcb->channel_id = p_acb->delete_mdl.channel_id; - p_pcb->cb_state = BTIF_HL_CHAN_CB_STATE_DESTROYED_PENDING; - break; - default: - break; - } - BTA_HlSdpQuery(app_id,p_acb->app_handle, bd_addr); - } - else - { - status = false; - BTIF_TRACE_ERROR("Open CCH request discarded- No mcl cb"); - } + if (!ctrl_psm) { + p_mcb->cch_oper = BTIF_HL_CCH_OP_MDEP_FILTERING; + } else { + p_mcb->cch_oper = BTIF_HL_CCH_OP_MATCHED_CTRL_PSM; + p_mcb->req_ctrl_psm = ctrl_psm; } - else - { - status = false; - BTIF_TRACE_ERROR("Open CCH request discarded- already in USE"); + + p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); + p_pcb->in_use = true; + p_pcb->mdep_cfg_idx = mdep_cfg_idx; + memcpy(p_pcb->bd_addr, bd_addr, sizeof(BD_ADDR)); + p_pcb->op = op; + + switch (op) { + case BTIF_HL_PEND_DCH_OP_OPEN: + *channel_id = p_pcb->channel_id = + (int)btif_hl_get_next_channel_id(app_id); + p_pcb->cb_state = BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING; + break; + case BTIF_HL_PEND_DCH_OP_DELETE_MDL: + p_pcb->channel_id = p_acb->delete_mdl.channel_id; + p_pcb->cb_state = BTIF_HL_CHAN_CB_STATE_DESTROYED_PENDING; + break; + default: + break; } - } - else - { + BTA_HlSdpQuery(app_id, p_acb->app_handle, bd_addr); + } else { status = false; - BTIF_TRACE_ERROR("Invalid app_id=%d", app_id); - } - - if (channel_id) - { - BTIF_TRACE_DEBUG("status=%d channel_id=0x%08x", status, *channel_id); - } - else - { - BTIF_TRACE_DEBUG("status=%d ", status); + BTIF_TRACE_ERROR("Open CCH request discarded- No mcl cb"); + } + } else { + status = false; + BTIF_TRACE_ERROR("Open CCH request discarded- already in USE"); } - return status; + } else { + status = false; + BTIF_TRACE_ERROR("Invalid app_id=%d", app_id); + } + + if (channel_id) { + BTIF_TRACE_DEBUG("status=%d channel_id=0x%08x", status, *channel_id); + } else { + BTIF_TRACE_DEBUG("status=%d ", status); + } + return status; } /******************************************************************************* -** -** Function btif_hl_find_mdl_idx_using_handle -** -** Description Find the MDL index using channel id -** -** Returns bool -** -*******************************************************************************/ + * + * Function btif_hl_find_mdl_idx_using_handle + * + * Description Find the MDL index using channel id + * + * Returns bool + * + ******************************************************************************/ bool btif_hl_find_mdl_cfg_idx_using_channel_id(int channel_id, - uint8_t *p_app_idx, - uint8_t *p_mdl_cfg_idx){ - btif_hl_app_cb_t *p_acb; - btif_hl_mdl_cfg_t *p_mdl; - bool found=false; - uint8_t i,j; - int mdl_cfg_channel_id; - - *p_app_idx = 0; - *p_mdl_cfg_idx =0; - for (i=0; i < BTA_HL_NUM_APPS ; i ++) - { - p_acb =BTIF_HL_GET_APP_CB_PTR(i); - for (j=0; j< BTA_HL_NUM_MDL_CFGS; j++) - { - p_mdl =BTIF_HL_GET_MDL_CFG_PTR(i,j); - mdl_cfg_channel_id = *(BTIF_HL_GET_MDL_CFG_CHANNEL_ID_PTR(i,j)); - if (p_acb->in_use && - p_mdl->base.active && - (mdl_cfg_channel_id == channel_id)) - { - found = true; - *p_app_idx = i; - *p_mdl_cfg_idx =j; - break; - } - } + uint8_t* p_app_idx, + uint8_t* p_mdl_cfg_idx) { + btif_hl_app_cb_t* p_acb; + btif_hl_mdl_cfg_t* p_mdl; + bool found = false; + uint8_t i, j; + int mdl_cfg_channel_id; + + *p_app_idx = 0; + *p_mdl_cfg_idx = 0; + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + p_acb = BTIF_HL_GET_APP_CB_PTR(i); + for (j = 0; j < BTA_HL_NUM_MDL_CFGS; j++) { + p_mdl = BTIF_HL_GET_MDL_CFG_PTR(i, j); + mdl_cfg_channel_id = *(BTIF_HL_GET_MDL_CFG_CHANNEL_ID_PTR(i, j)); + if (p_acb->in_use && p_mdl->base.active && + (mdl_cfg_channel_id == channel_id)) { + found = true; + *p_app_idx = i; + *p_mdl_cfg_idx = j; + break; + } } + } - BTIF_TRACE_EVENT("%s found=%d channel_id=0x%08x, app_idx=%d mdl_cfg_idx=%d ", - __func__,found,channel_id, i,j ); - return found; + BTIF_TRACE_EVENT("%s found=%d channel_id=0x%08x, app_idx=%d mdl_cfg_idx=%d ", + __func__, found, channel_id, i, j); + return found; } /******************************************************************************* -** -** Function btif_hl_find_mdl_idx_using_handle -** -** Description Find the MDL index using channel id -** -** Returns bool -** -*******************************************************************************/ -bool btif_hl_find_mdl_idx_using_channel_id(int channel_id, - uint8_t *p_app_idx,uint8_t *p_mcl_idx, - uint8_t *p_mdl_idx){ - btif_hl_app_cb_t *p_acb; - btif_hl_mcl_cb_t *p_mcb; - btif_hl_mdl_cb_t *p_dcb; - bool found=false; - uint8_t i,j,k; - - for (i=0; i < BTA_HL_NUM_APPS ; i ++) - { - p_acb =BTIF_HL_GET_APP_CB_PTR(i); - for (j=0; j< BTA_HL_NUM_MCLS; j++) - { - p_mcb =BTIF_HL_GET_MCL_CB_PTR(i,j); - for (k=0; k< BTA_HL_NUM_MDLS_PER_MCL; k++) - { - p_dcb =BTIF_HL_GET_MDL_CB_PTR(i,j,k); - if (p_acb->in_use && - p_mcb->in_use && - p_dcb->in_use && - (p_dcb->channel_id == channel_id)) - { - found = true; - *p_app_idx = i; - *p_mcl_idx =j; - *p_mdl_idx = k; - break; - } - } + * + * Function btif_hl_find_mdl_idx_using_handle + * + * Description Find the MDL index using channel id + * + * Returns bool + * + ******************************************************************************/ +bool btif_hl_find_mdl_idx_using_channel_id(int channel_id, uint8_t* p_app_idx, + uint8_t* p_mcl_idx, + uint8_t* p_mdl_idx) { + btif_hl_app_cb_t* p_acb; + btif_hl_mcl_cb_t* p_mcb; + btif_hl_mdl_cb_t* p_dcb; + bool found = false; + uint8_t i, j, k; + + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + p_acb = BTIF_HL_GET_APP_CB_PTR(i); + for (j = 0; j < BTA_HL_NUM_MCLS; j++) { + p_mcb = BTIF_HL_GET_MCL_CB_PTR(i, j); + for (k = 0; k < BTA_HL_NUM_MDLS_PER_MCL; k++) { + p_dcb = BTIF_HL_GET_MDL_CB_PTR(i, j, k); + if (p_acb->in_use && p_mcb->in_use && p_dcb->in_use && + (p_dcb->channel_id == channel_id)) { + found = true; + *p_app_idx = i; + *p_mcl_idx = j; + *p_mdl_idx = k; + break; } + } } - BTIF_TRACE_DEBUG("%s found=%d app_idx=%d mcl_idx=%d mdl_idx=%d ", - __func__,found,i,j,k ); - return found; + } + BTIF_TRACE_DEBUG("%s found=%d app_idx=%d mcl_idx=%d mdl_idx=%d ", __func__, + found, i, j, k); + return found; } /******************************************************************************* -** -** Function btif_hl_find_channel_id_using_mdl_id -** -** Description Find channel id using mdl_id' -** -** Returns bool -*********************************************************************************/ -bool btif_hl_find_channel_id_using_mdl_id(uint8_t app_idx, tBTA_HL_MDL_ID mdl_id, - int *p_channel_id){ - btif_hl_app_cb_t *p_acb; - btif_hl_mdl_cfg_t *p_mdl; - bool found=false; - uint8_t j=0; - int mdl_cfg_channel_id; - p_acb =BTIF_HL_GET_APP_CB_PTR(app_idx); - if (p_acb && p_acb->in_use) - { - for (j=0; j< BTA_HL_NUM_MDL_CFGS; j++) - { - p_mdl =BTIF_HL_GET_MDL_CFG_PTR(app_idx,j); - mdl_cfg_channel_id = *(BTIF_HL_GET_MDL_CFG_CHANNEL_ID_PTR(app_idx,j)); - if ( p_mdl->base.active && (p_mdl->base.mdl_id == mdl_id)) - { - found = true; - *p_channel_id = mdl_cfg_channel_id; - break; - } - } - } - BTIF_TRACE_EVENT("%s found=%d channel_id=0x%08x, mdl_id=0x%x app_idx=%d mdl_cfg_idx=%d ", - __func__,found,*p_channel_id,mdl_id, app_idx,j ); - return found; + * + * Function btif_hl_find_channel_id_using_mdl_id + * + * Description Find channel id using mdl_id' + * + * Returns bool + ********************************************************************************/ +bool btif_hl_find_channel_id_using_mdl_id(uint8_t app_idx, + tBTA_HL_MDL_ID mdl_id, + int* p_channel_id) { + btif_hl_app_cb_t* p_acb; + btif_hl_mdl_cfg_t* p_mdl; + bool found = false; + uint8_t j = 0; + int mdl_cfg_channel_id; + p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + if (p_acb && p_acb->in_use) { + for (j = 0; j < BTA_HL_NUM_MDL_CFGS; j++) { + p_mdl = BTIF_HL_GET_MDL_CFG_PTR(app_idx, j); + mdl_cfg_channel_id = *(BTIF_HL_GET_MDL_CFG_CHANNEL_ID_PTR(app_idx, j)); + if (p_mdl->base.active && (p_mdl->base.mdl_id == mdl_id)) { + found = true; + *p_channel_id = mdl_cfg_channel_id; + break; + } + } + } + BTIF_TRACE_EVENT( + "%s found=%d channel_id=0x%08x, mdl_id=0x%x app_idx=%d mdl_cfg_idx=%d ", + __func__, found, *p_channel_id, mdl_id, app_idx, j); + return found; } /******************************************************************************* -** -** Function btif_hl_find_mdl_idx_using_handle -** -** Description Find the MDL index using handle -** -** Returns bool -** -*******************************************************************************/ -bool btif_hl_find_mdl_idx_using_handle(tBTA_HL_MDL_HANDLE mdl_handle, - uint8_t *p_app_idx,uint8_t *p_mcl_idx, - uint8_t *p_mdl_idx){ - btif_hl_app_cb_t *p_acb; - btif_hl_mcl_cb_t *p_mcb; - btif_hl_mdl_cb_t *p_dcb; - bool found=false; - uint8_t i,j,k; - - *p_app_idx = 0; - *p_mcl_idx =0; - *p_mdl_idx = 0; - for (i=0; i < BTA_HL_NUM_APPS ; i ++) - { - p_acb =BTIF_HL_GET_APP_CB_PTR(i); - for (j=0; j< BTA_HL_NUM_MCLS; j++) - { - p_mcb =BTIF_HL_GET_MCL_CB_PTR(i,j); - for (k=0; k< BTA_HL_NUM_MDLS_PER_MCL; k++) - { - p_dcb =BTIF_HL_GET_MDL_CB_PTR(i,j,k); - if (p_acb->in_use && - p_mcb->in_use && - p_dcb->in_use && - (p_dcb->mdl_handle == mdl_handle)) - { - found = true; - *p_app_idx = i; - *p_mcl_idx =j; - *p_mdl_idx = k; - break; - } - } + * + * Function btif_hl_find_mdl_idx_using_handle + * + * Description Find the MDL index using handle + * + * Returns bool + * + ******************************************************************************/ +bool btif_hl_find_mdl_idx_using_handle(tBTA_HL_MDL_HANDLE mdl_handle, + uint8_t* p_app_idx, uint8_t* p_mcl_idx, + uint8_t* p_mdl_idx) { + btif_hl_app_cb_t* p_acb; + btif_hl_mcl_cb_t* p_mcb; + btif_hl_mdl_cb_t* p_dcb; + bool found = false; + uint8_t i, j, k; + + *p_app_idx = 0; + *p_mcl_idx = 0; + *p_mdl_idx = 0; + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + p_acb = BTIF_HL_GET_APP_CB_PTR(i); + for (j = 0; j < BTA_HL_NUM_MCLS; j++) { + p_mcb = BTIF_HL_GET_MCL_CB_PTR(i, j); + for (k = 0; k < BTA_HL_NUM_MDLS_PER_MCL; k++) { + p_dcb = BTIF_HL_GET_MDL_CB_PTR(i, j, k); + if (p_acb->in_use && p_mcb->in_use && p_dcb->in_use && + (p_dcb->mdl_handle == mdl_handle)) { + found = true; + *p_app_idx = i; + *p_mcl_idx = j; + *p_mdl_idx = k; + break; } + } } + } - BTIF_TRACE_EVENT("%s found=%d app_idx=%d mcl_idx=%d mdl_idx=%d ", - __func__,found,i,j,k ); - return found; + BTIF_TRACE_EVENT("%s found=%d app_idx=%d mcl_idx=%d mdl_idx=%d ", __func__, + found, i, j, k); + return found; } /******************************************************************************* -** -** Function btif_hl_find_peer_mdep_id -** -** Description Find the peer MDEP ID from the received SPD records -** -** Returns bool -** -*******************************************************************************/ + * + * Function btif_hl_find_peer_mdep_id + * + * Description Find the peer MDEP ID from the received SPD records + * + * Returns bool + * + ******************************************************************************/ static bool btif_hl_find_peer_mdep_id(uint8_t app_id, BD_ADDR bd_addr, - tBTA_HL_MDEP_ROLE local_mdep_role, - uint16_t data_type, - tBTA_HL_MDEP_ID *p_peer_mdep_id){ - uint8_t app_idx, mcl_idx; - btif_hl_mcl_cb_t *p_mcb; - tBTA_HL_SDP_REC *p_rec; - uint8_t i, num_mdeps; - bool found = false; - tBTA_HL_MDEP_ROLE peer_mdep_role; - - BTIF_TRACE_DEBUG("%s app_id=%d local_mdep_role=%d, data_type=%d", - __func__, app_id, local_mdep_role, data_type); - - BTIF_TRACE_DEBUG("DB [%02x:%02x:%02x:%02x:%02x:%02x]", - bd_addr[0], bd_addr[1], - bd_addr[2], bd_addr[3], - bd_addr[4], bd_addr[5]); - - BTIF_TRACE_DEBUG("local_mdep_role=%d", local_mdep_role); - BTIF_TRACE_DEBUG("data_type=%d", data_type); - - if (local_mdep_role == BTA_HL_MDEP_ROLE_SINK) - peer_mdep_role = BTA_HL_MDEP_ROLE_SOURCE; - else - peer_mdep_role = BTA_HL_MDEP_ROLE_SINK; - - if (btif_hl_find_app_idx(app_id, &app_idx) ) - { - if (btif_hl_find_mcl_idx(app_idx, bd_addr, &mcl_idx)) - { - p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - - BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=%d",app_idx, mcl_idx); - BTIF_TRACE_DEBUG("valid_spd_idx=%d sdp_idx=%d",p_mcb->valid_sdp_idx, p_mcb->sdp_idx); - if (p_mcb->valid_sdp_idx) - { - p_rec = &p_mcb->sdp.sdp_rec[p_mcb->sdp_idx]; - num_mdeps = p_rec->num_mdeps; - BTIF_TRACE_DEBUG("num_mdeps=%d", num_mdeps); - - for (i = 0; i < num_mdeps; i++) - { - BTIF_TRACE_DEBUG("p_rec->mdep_cfg[%d].mdep_role=%d",i, p_rec->mdep_cfg[i].mdep_role); - BTIF_TRACE_DEBUG("p_rec->mdep_cfg[%d].data_type =%d",i, p_rec->mdep_cfg[i].data_type ); - if ((p_rec->mdep_cfg[i].mdep_role == peer_mdep_role) && - (p_rec->mdep_cfg[i].data_type == data_type)) - { - found = true; - *p_peer_mdep_id = p_rec->mdep_cfg[i].mdep_id; - break; - } - } - } + tBTA_HL_MDEP_ROLE local_mdep_role, + uint16_t data_type, + tBTA_HL_MDEP_ID* p_peer_mdep_id) { + uint8_t app_idx, mcl_idx; + btif_hl_mcl_cb_t* p_mcb; + tBTA_HL_SDP_REC* p_rec; + uint8_t i, num_mdeps; + bool found = false; + tBTA_HL_MDEP_ROLE peer_mdep_role; + + BTIF_TRACE_DEBUG("%s app_id=%d local_mdep_role=%d, data_type=%d", __func__, + app_id, local_mdep_role, data_type); + + BTIF_TRACE_DEBUG("DB [%02x:%02x:%02x:%02x:%02x:%02x]", bd_addr[0], bd_addr[1], + bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]); + + BTIF_TRACE_DEBUG("local_mdep_role=%d", local_mdep_role); + BTIF_TRACE_DEBUG("data_type=%d", data_type); + + if (local_mdep_role == BTA_HL_MDEP_ROLE_SINK) + peer_mdep_role = BTA_HL_MDEP_ROLE_SOURCE; + else + peer_mdep_role = BTA_HL_MDEP_ROLE_SINK; + + if (btif_hl_find_app_idx(app_id, &app_idx)) { + if (btif_hl_find_mcl_idx(app_idx, bd_addr, &mcl_idx)) { + p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + + BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=%d", app_idx, mcl_idx); + BTIF_TRACE_DEBUG("valid_spd_idx=%d sdp_idx=%d", p_mcb->valid_sdp_idx, + p_mcb->sdp_idx); + if (p_mcb->valid_sdp_idx) { + p_rec = &p_mcb->sdp.sdp_rec[p_mcb->sdp_idx]; + num_mdeps = p_rec->num_mdeps; + BTIF_TRACE_DEBUG("num_mdeps=%d", num_mdeps); + + for (i = 0; i < num_mdeps; i++) { + BTIF_TRACE_DEBUG("p_rec->mdep_cfg[%d].mdep_role=%d", i, + p_rec->mdep_cfg[i].mdep_role); + BTIF_TRACE_DEBUG("p_rec->mdep_cfg[%d].data_type =%d", i, + p_rec->mdep_cfg[i].data_type); + if ((p_rec->mdep_cfg[i].mdep_role == peer_mdep_role) && + (p_rec->mdep_cfg[i].data_type == data_type)) { + found = true; + *p_peer_mdep_id = p_rec->mdep_cfg[i].mdep_id; + break; + } } + } } + } - BTIF_TRACE_DEBUG("found =%d *p_peer_mdep_id=%d", found, *p_peer_mdep_id); + BTIF_TRACE_DEBUG("found =%d *p_peer_mdep_id=%d", found, *p_peer_mdep_id); - return found; + return found; } /******************************************************************************* -** -** Function btif_hl_find_mdep_cfg_idx -** -** Description Find the MDEP configuration index using local MDEP_ID -** -** Returns bool -** -*******************************************************************************/ -static bool btif_hl_find_mdep_cfg_idx(uint8_t app_idx, tBTA_HL_MDEP_ID local_mdep_id, - uint8_t *p_mdep_cfg_idx){ - btif_hl_app_cb_t *p_acb =BTIF_HL_GET_APP_CB_PTR(app_idx); - tBTA_HL_SUP_FEATURE *p_sup_feature= &p_acb->sup_feature; - bool found =false; - uint8_t i; - - for (i=0; i< p_sup_feature->num_of_mdeps; i++) - { - BTIF_TRACE_DEBUG("btif_hl_find_mdep_cfg_idx: mdep_id=%d app_idx = %d", - p_sup_feature->mdep[i].mdep_id,app_idx); - if ( p_sup_feature->mdep[i].mdep_id == local_mdep_id) - { - found = true; - *p_mdep_cfg_idx = i; - break; - } + * + * Function btif_hl_find_mdep_cfg_idx + * + * Description Find the MDEP configuration index using local MDEP_ID + * + * Returns bool + * + ******************************************************************************/ +static bool btif_hl_find_mdep_cfg_idx(uint8_t app_idx, + tBTA_HL_MDEP_ID local_mdep_id, + uint8_t* p_mdep_cfg_idx) { + btif_hl_app_cb_t* p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + tBTA_HL_SUP_FEATURE* p_sup_feature = &p_acb->sup_feature; + bool found = false; + uint8_t i; + + for (i = 0; i < p_sup_feature->num_of_mdeps; i++) { + BTIF_TRACE_DEBUG("btif_hl_find_mdep_cfg_idx: mdep_id=%d app_idx = %d", + p_sup_feature->mdep[i].mdep_id, app_idx); + if (p_sup_feature->mdep[i].mdep_id == local_mdep_id) { + found = true; + *p_mdep_cfg_idx = i; + break; } + } - BTIF_TRACE_DEBUG("%s found=%d mdep_idx=%d local_mdep_id=%d app_idx=%d ", - __func__, found,i, local_mdep_id,app_idx); - return found; + BTIF_TRACE_DEBUG("%s found=%d mdep_idx=%d local_mdep_id=%d app_idx=%d ", + __func__, found, i, local_mdep_id, app_idx); + return found; } /******************************************************************************* -** -** Function btif_hl_find_mcl_idx -** -** Description Find the MCL index using BD address -** -** Returns bool -** -*******************************************************************************/ -bool btif_hl_find_mcl_idx(uint8_t app_idx, BD_ADDR p_bd_addr, uint8_t *p_mcl_idx){ - bool found=false; - uint8_t i; - btif_hl_mcl_cb_t *p_mcb; - - *p_mcl_idx = 0; - for (i=0; i < BTA_HL_NUM_MCLS ; i ++) - { - p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, i); - if (p_mcb->in_use && - (!memcmp (p_mcb->bd_addr, p_bd_addr, BD_ADDR_LEN))) - { - found = true; - *p_mcl_idx = i; - break; - } + * + * Function btif_hl_find_mcl_idx + * + * Description Find the MCL index using BD address + * + * Returns bool + * + ******************************************************************************/ +bool btif_hl_find_mcl_idx(uint8_t app_idx, BD_ADDR p_bd_addr, + uint8_t* p_mcl_idx) { + bool found = false; + uint8_t i; + btif_hl_mcl_cb_t* p_mcb; + + *p_mcl_idx = 0; + for (i = 0; i < BTA_HL_NUM_MCLS; i++) { + p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, i); + if (p_mcb->in_use && (!memcmp(p_mcb->bd_addr, p_bd_addr, BD_ADDR_LEN))) { + found = true; + *p_mcl_idx = i; + break; } + } - BTIF_TRACE_DEBUG("%s found=%d idx=%d",__func__, found, i); - return found; + BTIF_TRACE_DEBUG("%s found=%d idx=%d", __func__, found, i); + return found; } /******************************************************************************* -** -** Function btif_hl_init -** -** Description HL initialization function. -** -** Returns void -** -*******************************************************************************/ -static void btif_hl_init(void){ - BTIF_TRACE_DEBUG("%s", __func__); - memset(p_btif_hl_cb, 0, sizeof(btif_hl_cb_t)); - btif_hl_init_next_app_id(); - btif_hl_init_next_channel_id(); + * + * Function btif_hl_init + * + * Description HL initialization function. + * + * Returns void + * + ******************************************************************************/ +static void btif_hl_init(void) { + BTIF_TRACE_DEBUG("%s", __func__); + memset(p_btif_hl_cb, 0, sizeof(btif_hl_cb_t)); + btif_hl_init_next_app_id(); + btif_hl_init_next_channel_id(); } /******************************************************************************* -** -** Function btif_hl_disable -** -** Description Disable initialization function. -** -** Returns void -** -*******************************************************************************/ -static void btif_hl_disable(void){ - BTIF_TRACE_DEBUG("%s", __func__); - - if ((p_btif_hl_cb->state != BTIF_HL_STATE_DISABLING) && - (p_btif_hl_cb->state != BTIF_HL_STATE_DISABLED)) - { - btif_hl_set_state(BTIF_HL_STATE_DISABLING); - BTA_HlDisable(); - } + * + * Function btif_hl_disable + * + * Description Disable initialization function. + * + * Returns void + * + ******************************************************************************/ +static void btif_hl_disable(void) { + BTIF_TRACE_DEBUG("%s", __func__); + + if ((p_btif_hl_cb->state != BTIF_HL_STATE_DISABLING) && + (p_btif_hl_cb->state != BTIF_HL_STATE_DISABLED)) { + btif_hl_set_state(BTIF_HL_STATE_DISABLING); + BTA_HlDisable(); + } } /******************************************************************************* -** -** Function btif_hl_is_no_active_app -** -** Description Find whether or not any APP is still in use -** -** Returns bool -** -*******************************************************************************/ -static bool btif_hl_is_no_active_app(void){ - bool no_active_app = true; - uint8_t i; - - for (i=0; i < BTA_HL_NUM_APPS ; i ++) - { - if (btif_hl_cb.acb[i].in_use) - { - no_active_app = false; - break; - } + * + * Function btif_hl_is_no_active_app + * + * Description Find whether or not any APP is still in use + * + * Returns bool + * + ******************************************************************************/ +static bool btif_hl_is_no_active_app(void) { + bool no_active_app = true; + uint8_t i; + + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + if (btif_hl_cb.acb[i].in_use) { + no_active_app = false; + break; } + } - BTIF_TRACE_DEBUG("%s no_active_app=%d ", __func__, no_active_app ); - return no_active_app; + BTIF_TRACE_DEBUG("%s no_active_app=%d ", __func__, no_active_app); + return no_active_app; } /******************************************************************************* -** -** Function btif_hl_free_app_idx -** -** Description free an application control block -** -** Returns void -** -*******************************************************************************/ -static void btif_hl_free_app_idx(uint8_t app_idx){ - - if ((app_idx < BTA_HL_NUM_APPS) && btif_hl_cb.acb[app_idx].in_use ) - { - btif_hl_cb.acb[app_idx].in_use = false; - for (size_t i = 0; i < BTA_HL_NUM_MCLS; i++) - alarm_free(btif_hl_cb.acb[app_idx].mcb[i].cch_timer); - memset(&btif_hl_cb.acb[app_idx], 0, sizeof(btif_hl_app_cb_t)); - } + * + * Function btif_hl_free_app_idx + * + * Description free an application control block + * + * Returns void + * + ******************************************************************************/ +static void btif_hl_free_app_idx(uint8_t app_idx) { + if ((app_idx < BTA_HL_NUM_APPS) && btif_hl_cb.acb[app_idx].in_use) { + btif_hl_cb.acb[app_idx].in_use = false; + for (size_t i = 0; i < BTA_HL_NUM_MCLS; i++) + alarm_free(btif_hl_cb.acb[app_idx].mcb[i].cch_timer); + memset(&btif_hl_cb.acb[app_idx], 0, sizeof(btif_hl_app_cb_t)); + } } /******************************************************************************* -** -** Function btif_hl_set_state -** -** Description set HL state -** -** Returns void -** -*******************************************************************************/ -static void btif_hl_set_state(btif_hl_state_t state){ - BTIF_TRACE_DEBUG("btif_hl_set_state: %d ---> %d ", p_btif_hl_cb->state, state); - p_btif_hl_cb->state = state; + * + * Function btif_hl_set_state + * + * Description set HL state + * + * Returns void + * + ******************************************************************************/ +static void btif_hl_set_state(btif_hl_state_t state) { + BTIF_TRACE_DEBUG("btif_hl_set_state: %d ---> %d ", p_btif_hl_cb->state, + state); + p_btif_hl_cb->state = state; } /******************************************************************************* -** -** Function btif_hl_set_state -** -** Description get HL state -** -** Returns btif_hl_state_t -** -*******************************************************************************/ - -static btif_hl_state_t btif_hl_get_state(void){ - BTIF_TRACE_DEBUG("btif_hl_get_state: %d ", p_btif_hl_cb->state); - return p_btif_hl_cb->state; + * + * Function btif_hl_set_state + * + * Description get HL state + * + * Returns btif_hl_state_t + * + ******************************************************************************/ + +static btif_hl_state_t btif_hl_get_state(void) { + BTIF_TRACE_DEBUG("btif_hl_get_state: %d ", p_btif_hl_cb->state); + return p_btif_hl_cb->state; } /******************************************************************************* -** -** Function btif_hl_find_data_type_idx -** -** Description Find the index in the data type table -** -** Returns bool -** -*******************************************************************************/ -static bool btif_hl_find_data_type_idx(uint16_t data_type, uint8_t *p_idx){ - bool found = false; - uint8_t i; - - for (i=0; i< BTIF_HL_DATA_TABLE_SIZE; i++ ) - { - if (data_type_table[i].data_type == data_type) - { - found = true; - *p_idx= i; - break; - } + * + * Function btif_hl_find_data_type_idx + * + * Description Find the index in the data type table + * + * Returns bool + * + ******************************************************************************/ +static bool btif_hl_find_data_type_idx(uint16_t data_type, uint8_t* p_idx) { + bool found = false; + uint8_t i; + + for (i = 0; i < BTIF_HL_DATA_TABLE_SIZE; i++) { + if (data_type_table[i].data_type == data_type) { + found = true; + *p_idx = i; + break; } + } - BTIF_TRACE_DEBUG("%s found=%d, data_type=0x%x idx=%d", __func__, found, data_type, i); - return found; + BTIF_TRACE_DEBUG("%s found=%d, data_type=0x%x idx=%d", __func__, found, + data_type, i); + return found; } /******************************************************************************* -** -** Function btif_hl_get_max_tx_apdu_size -** -** Description Find the maximum TX APDU size for the specified data type and -** MDEP role -** -** Returns uint16_t -** -*******************************************************************************/ -uint16_t btif_hl_get_max_tx_apdu_size(tBTA_HL_MDEP_ROLE mdep_role, - uint16_t data_type ){ - uint8_t idx; - uint16_t max_tx_apdu_size =0; - - if (btif_hl_find_data_type_idx(data_type, &idx)) - { - if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE) - { - max_tx_apdu_size = data_type_table[idx].max_tx_apdu_size; - } - else - { - max_tx_apdu_size = data_type_table[idx].max_rx_apdu_size; - } + * + * Function btif_hl_get_max_tx_apdu_size + * + * Description Find the maximum TX APDU size for the specified data type and + * MDEP role + * + * Returns uint16_t + * + ******************************************************************************/ +uint16_t btif_hl_get_max_tx_apdu_size(tBTA_HL_MDEP_ROLE mdep_role, + uint16_t data_type) { + uint8_t idx; + uint16_t max_tx_apdu_size = 0; + + if (btif_hl_find_data_type_idx(data_type, &idx)) { + if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE) { + max_tx_apdu_size = data_type_table[idx].max_tx_apdu_size; + } else { + max_tx_apdu_size = data_type_table[idx].max_rx_apdu_size; } - else - { - if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE) - { - max_tx_apdu_size = BTIF_HL_DEFAULT_SRC_TX_APDU_SIZE; - } - else - { - max_tx_apdu_size = BTIF_HL_DEFAULT_SRC_RX_APDU_SIZE; - } - + } else { + if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE) { + max_tx_apdu_size = BTIF_HL_DEFAULT_SRC_TX_APDU_SIZE; + } else { + max_tx_apdu_size = BTIF_HL_DEFAULT_SRC_RX_APDU_SIZE; } + } - BTIF_TRACE_DEBUG("%s mdep_role=%d data_type=0x%4x size=%d", - __func__, mdep_role, data_type, max_tx_apdu_size); - return max_tx_apdu_size; + BTIF_TRACE_DEBUG("%s mdep_role=%d data_type=0x%4x size=%d", __func__, + mdep_role, data_type, max_tx_apdu_size); + return max_tx_apdu_size; } /******************************************************************************* -** -** Function btif_hl_get_max_rx_apdu_size -** -** Description Find the maximum RX APDU size for the specified data type and -** MDEP role -** -** Returns uint16_t -** -*******************************************************************************/ -uint16_t btif_hl_get_max_rx_apdu_size(tBTA_HL_MDEP_ROLE mdep_role, - uint16_t data_type ){ - uint8_t idx; - uint16_t max_rx_apdu_size =0; - - if (btif_hl_find_data_type_idx(data_type, &idx)) - { - if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE) - { - max_rx_apdu_size = data_type_table[idx].max_rx_apdu_size; - } - else - { - max_rx_apdu_size = data_type_table[idx].max_tx_apdu_size; - } + * + * Function btif_hl_get_max_rx_apdu_size + * + * Description Find the maximum RX APDU size for the specified data type and + * MDEP role + * + * Returns uint16_t + * + ******************************************************************************/ +uint16_t btif_hl_get_max_rx_apdu_size(tBTA_HL_MDEP_ROLE mdep_role, + uint16_t data_type) { + uint8_t idx; + uint16_t max_rx_apdu_size = 0; + + if (btif_hl_find_data_type_idx(data_type, &idx)) { + if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE) { + max_rx_apdu_size = data_type_table[idx].max_rx_apdu_size; + } else { + max_rx_apdu_size = data_type_table[idx].max_tx_apdu_size; } - else - { - if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE) - { - max_rx_apdu_size = BTIF_HL_DEFAULT_SRC_RX_APDU_SIZE; - } - else - { - max_rx_apdu_size = BTIF_HL_DEFAULT_SRC_TX_APDU_SIZE; - } + } else { + if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE) { + max_rx_apdu_size = BTIF_HL_DEFAULT_SRC_RX_APDU_SIZE; + } else { + max_rx_apdu_size = BTIF_HL_DEFAULT_SRC_TX_APDU_SIZE; } + } - BTIF_TRACE_DEBUG("%s mdep_role=%d data_type=0x%4x size=%d", - __func__, mdep_role, data_type, max_rx_apdu_size); + BTIF_TRACE_DEBUG("%s mdep_role=%d data_type=0x%4x size=%d", __func__, + mdep_role, data_type, max_rx_apdu_size); - return max_rx_apdu_size; + return max_rx_apdu_size; } /******************************************************************************* -** -** Function btif_hl_if_channel_setup_pending -** -** Description -** -** Returns bool -** -*******************************************************************************/ - -static bool btif_hl_get_bta_mdep_role(bthl_mdep_role_t mdep, tBTA_HL_MDEP_ROLE *p){ - bool status = true; - switch (mdep) - { - case BTHL_MDEP_ROLE_SOURCE: - *p = BTA_HL_MDEP_ROLE_SOURCE; - break; - case BTHL_MDEP_ROLE_SINK: - *p = BTA_HL_MDEP_ROLE_SINK; - break; - default: - *p = BTA_HL_MDEP_ROLE_SOURCE; - status = false; - break; - } + * + * Function btif_hl_if_channel_setup_pending + * + * Description + * + * Returns bool + * + ******************************************************************************/ - BTIF_TRACE_DEBUG("%s status=%d bta_mdep_role=%d (%d:btif)", - __func__, status, *p, mdep); - return status; +static bool btif_hl_get_bta_mdep_role(bthl_mdep_role_t mdep, + tBTA_HL_MDEP_ROLE* p) { + bool status = true; + switch (mdep) { + case BTHL_MDEP_ROLE_SOURCE: + *p = BTA_HL_MDEP_ROLE_SOURCE; + break; + case BTHL_MDEP_ROLE_SINK: + *p = BTA_HL_MDEP_ROLE_SINK; + break; + default: + *p = BTA_HL_MDEP_ROLE_SOURCE; + status = false; + break; + } + + BTIF_TRACE_DEBUG("%s status=%d bta_mdep_role=%d (%d:btif)", __func__, status, + *p, mdep); + return status; } /******************************************************************************* -** -** Function btif_hl_get_bta_channel_type -** -** Description convert bthl channel type to BTA DCH channel type -** -** Returns bool -** -*******************************************************************************/ - -static bool btif_hl_get_bta_channel_type(bthl_channel_type_t channel_type, tBTA_HL_DCH_CFG *p){ - bool status = true; - switch (channel_type) - { - case BTHL_CHANNEL_TYPE_RELIABLE: - *p = BTA_HL_DCH_CFG_RELIABLE; - break; - case BTHL_CHANNEL_TYPE_STREAMING: - *p = BTA_HL_DCH_CFG_STREAMING; - break; - case BTHL_CHANNEL_TYPE_ANY: - *p = BTA_HL_DCH_CFG_NO_PREF; - break; - default: - status = false; - break; - } - BTIF_TRACE_DEBUG("%s status = %d BTA DCH CFG=%d (1-rel 2-strm", - __func__, status, *p); - return status; + * + * Function btif_hl_get_bta_channel_type + * + * Description convert bthl channel type to BTA DCH channel type + * + * Returns bool + * + ******************************************************************************/ + +static bool btif_hl_get_bta_channel_type(bthl_channel_type_t channel_type, + tBTA_HL_DCH_CFG* p) { + bool status = true; + switch (channel_type) { + case BTHL_CHANNEL_TYPE_RELIABLE: + *p = BTA_HL_DCH_CFG_RELIABLE; + break; + case BTHL_CHANNEL_TYPE_STREAMING: + *p = BTA_HL_DCH_CFG_STREAMING; + break; + case BTHL_CHANNEL_TYPE_ANY: + *p = BTA_HL_DCH_CFG_NO_PREF; + break; + default: + status = false; + break; + } + BTIF_TRACE_DEBUG("%s status = %d BTA DCH CFG=%d (1-rel 2-strm", __func__, + status, *p); + return status; } /******************************************************************************* -** -** Function btif_hl_get_next_app_id -** -** Description get next applcation id -** -** Returns uint8_t -** -*******************************************************************************/ - -static uint8_t btif_hl_get_next_app_id(){ - uint8_t next_app_id = btif_hl_cb.next_app_id; - - btif_hl_cb.next_app_id++; - return next_app_id; + * + * Function btif_hl_get_next_app_id + * + * Description get next applcation id + * + * Returns uint8_t + * + ******************************************************************************/ + +static uint8_t btif_hl_get_next_app_id() { + uint8_t next_app_id = btif_hl_cb.next_app_id; + + btif_hl_cb.next_app_id++; + return next_app_id; } /******************************************************************************* -** -** Function btif_hl_get_next_channel_id -** -** Description get next channel id -** -** Returns int -** -*******************************************************************************/ -static int btif_hl_get_next_channel_id(uint8_t app_id){ - uint16_t next_channel_id = btif_hl_cb.next_channel_id; - int channel_id; - btif_hl_cb.next_channel_id++; - channel_id = (app_id << 16) + next_channel_id; - BTIF_TRACE_DEBUG("%s channel_id=0x%08x, app_id=0x%02x next_channel_id=0x%04x", __func__, - channel_id, app_id, next_channel_id); - return channel_id; + * + * Function btif_hl_get_next_channel_id + * + * Description get next channel id + * + * Returns int + * + ******************************************************************************/ +static int btif_hl_get_next_channel_id(uint8_t app_id) { + uint16_t next_channel_id = btif_hl_cb.next_channel_id; + int channel_id; + btif_hl_cb.next_channel_id++; + channel_id = (app_id << 16) + next_channel_id; + BTIF_TRACE_DEBUG("%s channel_id=0x%08x, app_id=0x%02x next_channel_id=0x%04x", + __func__, channel_id, app_id, next_channel_id); + return channel_id; } /******************************************************************************* -** -** Function btif_hl_get_app_id -** -** Description get the applicaiton id associated with the channel id -** -** Returns uint8_t -** -*******************************************************************************/ - -static uint8_t btif_hl_get_app_id(int channel_id){ - uint8_t app_id =(uint8_t) (channel_id >> 16); - BTIF_TRACE_DEBUG("%s channel_id=0x%08x, app_id=0x%02x ", __func__,channel_id, app_id); - return app_id; + * + * Function btif_hl_get_app_id + * + * Description get the applicaiton id associated with the channel id + * + * Returns uint8_t + * + ******************************************************************************/ + +static uint8_t btif_hl_get_app_id(int channel_id) { + uint8_t app_id = (uint8_t)(channel_id >> 16); + BTIF_TRACE_DEBUG("%s channel_id=0x%08x, app_id=0x%02x ", __func__, channel_id, + app_id); + return app_id; } /******************************************************************************* -** -** Function btif_hl_init_next_app_id -** -** Description initialize the application id -** -** Returns void -** -*******************************************************************************/ -static void btif_hl_init_next_app_id(void){ - btif_hl_cb.next_app_id = 1; -} + * + * Function btif_hl_init_next_app_id + * + * Description initialize the application id + * + * Returns void + * + ******************************************************************************/ +static void btif_hl_init_next_app_id(void) { btif_hl_cb.next_app_id = 1; } /******************************************************************************* -** -** Function btif_hl_init_next_channel_id -** -** Description initialize the channel id -** -** Returns void -** -*******************************************************************************/ -static void btif_hl_init_next_channel_id(void){ - btif_hl_cb.next_channel_id = 1; + * + * Function btif_hl_init_next_channel_id + * + * Description initialize the channel id + * + * Returns void + * + ******************************************************************************/ +static void btif_hl_init_next_channel_id(void) { + btif_hl_cb.next_channel_id = 1; } /******************************************************************************* -** -** Function btif_hl_find_app_idx_using_handle -** -** Description Find the applicaiton index using handle -** -** Returns bool -** -*******************************************************************************/ + * + * Function btif_hl_find_app_idx_using_handle + * + * Description Find the applicaiton index using handle + * + * Returns bool + * + ******************************************************************************/ bool btif_hl_find_app_idx_using_handle(tBTA_HL_APP_HANDLE app_handle, - uint8_t *p_app_idx){ - bool found=false; - uint8_t i; - - for (i=0; i < BTA_HL_NUM_APPS ; i ++) - { - if (btif_hl_cb.acb[i].in_use && - (btif_hl_cb.acb[i].app_handle == app_handle)) - { - found = true; - *p_app_idx = i; - break; - } + uint8_t* p_app_idx) { + bool found = false; + uint8_t i; + + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + if (btif_hl_cb.acb[i].in_use && + (btif_hl_cb.acb[i].app_handle == app_handle)) { + found = true; + *p_app_idx = i; + break; } + } - BTIF_TRACE_EVENT("%s status=%d handle=%d app_idx=%d ", - __func__, found, app_handle , i); + BTIF_TRACE_EVENT("%s status=%d handle=%d app_idx=%d ", __func__, found, + app_handle, i); - return found; + return found; } /******************************************************************************* -** -** Function btif_hl_find_app_idx_using_app_id -** -** Description Find the applicaiton index using app_id -** -** Returns bool -** -*******************************************************************************/ -bool btif_hl_find_app_idx_using_app_id(uint8_t app_id, - uint8_t *p_app_idx){ - bool found=false; - uint8_t i; - - *p_app_idx = 0; - for (i=0; i < BTA_HL_NUM_APPS ; i ++) - { - if (btif_hl_cb.acb[i].in_use && - (btif_hl_cb.acb[i].app_id == app_id)) - { - found = true; - *p_app_idx = i; - break; - } + * + * Function btif_hl_find_app_idx_using_app_id + * + * Description Find the applicaiton index using app_id + * + * Returns bool + * + ******************************************************************************/ +bool btif_hl_find_app_idx_using_app_id(uint8_t app_id, uint8_t* p_app_idx) { + bool found = false; + uint8_t i; + + *p_app_idx = 0; + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + if (btif_hl_cb.acb[i].in_use && (btif_hl_cb.acb[i].app_id == app_id)) { + found = true; + *p_app_idx = i; + break; } + } - BTIF_TRACE_EVENT("%s found=%d app_id=%d app_idx=%d ", - __func__, found, app_id , i); - - return found; -} + BTIF_TRACE_EVENT("%s found=%d app_id=%d app_idx=%d ", __func__, found, app_id, + i); -/******************************************************************************* -** -** Function btif_hl_find_mcl_idx_using_handle -** -** Description Find the MCL index using handle -** -** Returns bool -** -*******************************************************************************/ -bool btif_hl_find_mcl_idx_using_handle( tBTA_HL_MCL_HANDLE mcl_handle, - uint8_t *p_app_idx, uint8_t *p_mcl_idx){ - btif_hl_app_cb_t *p_acb; - bool found=false; - uint8_t i,j; - - for (i=0; imcb[j].in_use) - BTIF_TRACE_DEBUG("btif_hl_find_mcl_idx_using_handle:app_idx=%d," - "mcl_idx =%d mcl_handle=%d",i,j,p_acb->mcb[j].mcl_handle); - if (p_acb->mcb[j].in_use && - (p_acb->mcb[j].mcl_handle == mcl_handle)) - { - found = true; - *p_app_idx = i; - *p_mcl_idx = j; - break; - } - } - } - BTIF_TRACE_DEBUG("%s found=%d app_idx=%d mcl_idx=%d",__func__, - found, i, j); - return found; + return found; } /******************************************************************************* -** -** Function btif_hl_find_mdl_idx_using_mdl_id -** -** Description Find the mdl index using mdl_id -** -** Returns bool -** -*******************************************************************************/ -bool btif_hl_find_mcl_idx_using_mdl_id( uint8_t mdl_id,uint8_t mcl_handle, - uint8_t *p_app_idx, uint8_t *p_mcl_idx){ - btif_hl_app_cb_t *p_acb; - btif_hl_mcl_cb_t *p_mcb; - bool found=false; - uint8_t i,j,x; - - for (i=0; imcb[j].in_use && - (p_acb->mcb[j].mcl_handle == mcl_handle)) - { - p_mcb = &p_acb->mcb[j]; - BTIF_TRACE_DEBUG("btif_hl_find_mcl_idx_using_mdl_id: mcl handle found j =%d",j); - for (x=0; x < BTA_HL_NUM_MDLS_PER_MCL ; x ++) - { - if (p_mcb->mdl[x].in_use && p_mcb->mdl[x].mdl_id == mdl_id) - { - BTIF_TRACE_DEBUG("btif_hl_find_mcl_idx_using_mdl_id:found x =%d",x); - found = true; - *p_app_idx = i; - *p_mcl_idx = j; - break; - } - } - } - } + * + * Function btif_hl_find_mcl_idx_using_handle + * + * Description Find the MCL index using handle + * + * Returns bool + * + ******************************************************************************/ +bool btif_hl_find_mcl_idx_using_handle(tBTA_HL_MCL_HANDLE mcl_handle, + uint8_t* p_app_idx, uint8_t* p_mcl_idx) { + btif_hl_app_cb_t* p_acb; + bool found = false; + uint8_t i, j; + + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + p_acb = BTIF_HL_GET_APP_CB_PTR(i); + for (j = 0; j < BTA_HL_NUM_MCLS; j++) { + if (p_acb->mcb[j].in_use) + BTIF_TRACE_DEBUG( + "btif_hl_find_mcl_idx_using_handle:app_idx=%d," + "mcl_idx =%d mcl_handle=%d", + i, j, p_acb->mcb[j].mcl_handle); + if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) { + found = true; + *p_app_idx = i; + *p_mcl_idx = j; + break; + } } - BTIF_TRACE_DEBUG("%s found=%d app_idx=%d mcl_idx=%d",__func__, - found, i, j); - return found; + } + BTIF_TRACE_DEBUG("%s found=%d app_idx=%d mcl_idx=%d", __func__, found, i, j); + return found; } /******************************************************************************* -** -** Function btif_hl_find_mcl_idx_using_deleted_mdl_id -** -** Description Find the app index deleted_mdl_id -** -** Returns bool -** -*******************************************************************************/ -bool btif_hl_find_app_idx_using_deleted_mdl_id( uint8_t mdl_id, - uint8_t *p_app_idx){ - btif_hl_app_cb_t *p_acb; - bool found=false; - uint8_t i; - - for (i=0; idelete_mdl.active) { - BTIF_TRACE_DEBUG("%s: app_idx=%d, mdl_id=%d", - __func__,i,mdl_id); - } - if (p_acb->delete_mdl.active && - (p_acb->delete_mdl.mdl_id == mdl_id)) - { + * + * Function btif_hl_find_mdl_idx_using_mdl_id + * + * Description Find the mdl index using mdl_id + * + * Returns bool + * + ******************************************************************************/ +bool btif_hl_find_mcl_idx_using_mdl_id(uint8_t mdl_id, uint8_t mcl_handle, + uint8_t* p_app_idx, uint8_t* p_mcl_idx) { + btif_hl_app_cb_t* p_acb; + btif_hl_mcl_cb_t* p_mcb; + bool found = false; + uint8_t i, j, x; + + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + p_acb = BTIF_HL_GET_APP_CB_PTR(i); + for (j = 0; j < BTA_HL_NUM_MCLS; j++) { + if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) { + p_mcb = &p_acb->mcb[j]; + BTIF_TRACE_DEBUG( + "btif_hl_find_mcl_idx_using_mdl_id: mcl handle found j =%d", j); + for (x = 0; x < BTA_HL_NUM_MDLS_PER_MCL; x++) { + if (p_mcb->mdl[x].in_use && p_mcb->mdl[x].mdl_id == mdl_id) { + BTIF_TRACE_DEBUG("btif_hl_find_mcl_idx_using_mdl_id:found x =%d", + x); found = true; *p_app_idx = i; + *p_mcl_idx = j; break; + } } + } } - BTIF_TRACE_DEBUG("%s found=%d app_idx=%d",__func__, - found, i); - return found; + } + BTIF_TRACE_DEBUG("%s found=%d app_idx=%d mcl_idx=%d", __func__, found, i, j); + return found; } /******************************************************************************* -** -** Function btif_hl_stop_timer_using_handle -** -** Description clean control channel cb using handle -** -** Returns void -** -*******************************************************************************/ -static void btif_hl_stop_timer_using_handle( tBTA_HL_MCL_HANDLE mcl_handle){ - btif_hl_app_cb_t *p_acb; - uint8_t i,j; - - for (i=0; imcb[j].in_use && - (p_acb->mcb[j].mcl_handle == mcl_handle)) - { - btif_hl_stop_cch_timer(i, j); - } - } + * + * Function btif_hl_find_mcl_idx_using_deleted_mdl_id + * + * Description Find the app index deleted_mdl_id + * + * Returns bool + * + ******************************************************************************/ +bool btif_hl_find_app_idx_using_deleted_mdl_id(uint8_t mdl_id, + uint8_t* p_app_idx) { + btif_hl_app_cb_t* p_acb; + bool found = false; + uint8_t i; + + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + p_acb = BTIF_HL_GET_APP_CB_PTR(i); + if (p_acb->delete_mdl.active) { + BTIF_TRACE_DEBUG("%s: app_idx=%d, mdl_id=%d", __func__, i, mdl_id); } + if (p_acb->delete_mdl.active && (p_acb->delete_mdl.mdl_id == mdl_id)) { + found = true; + *p_app_idx = i; + break; + } + } + BTIF_TRACE_DEBUG("%s found=%d app_idx=%d", __func__, found, i); + return found; } /******************************************************************************* -** -** Function btif_hl_find_mcl_idx_using_app_idx -** -** Description Find the MCL index using handle -** -** Returns bool -** -*******************************************************************************/ -bool btif_hl_find_mcl_idx_using_app_idx( tBTA_HL_MCL_HANDLE mcl_handle, - uint8_t p_app_idx, uint8_t *p_mcl_idx){ - btif_hl_app_cb_t *p_acb; - bool found=false; - uint8_t j; - - p_acb =BTIF_HL_GET_APP_CB_PTR(p_app_idx); - for (j=0; j < BTA_HL_NUM_MCLS ; j++) - { - if (p_acb->mcb[j].in_use && - (p_acb->mcb[j].mcl_handle == mcl_handle)) - { - found = true; - *p_mcl_idx = j; - break; - } + * + * Function btif_hl_stop_timer_using_handle + * + * Description clean control channel cb using handle + * + * Returns void + * + ******************************************************************************/ +static void btif_hl_stop_timer_using_handle(tBTA_HL_MCL_HANDLE mcl_handle) { + btif_hl_app_cb_t* p_acb; + uint8_t i, j; + + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + p_acb = BTIF_HL_GET_APP_CB_PTR(i); + for (j = 0; j < BTA_HL_NUM_MCLS; j++) { + if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) { + btif_hl_stop_cch_timer(i, j); + } } - BTIF_TRACE_DEBUG("%s found=%dmcl_idx=%d",__func__, - found, j); - return found; + } } /******************************************************************************* -** -** Function btif_hl_clean_mdls_using_app_idx -** -** Description clean dch cpntrol bloack using app_idx -** -** Returns void -** -*******************************************************************************/ -void btif_hl_clean_mdls_using_app_idx( uint8_t app_idx){ - btif_hl_app_cb_t *p_acb; - btif_hl_mcl_cb_t *p_mcb; - btif_hl_mdl_cb_t *p_dcb; - uint8_t j,x,y; - bt_bdaddr_t bd_addr; - - p_acb =BTIF_HL_GET_APP_CB_PTR(app_idx); - for (j=0; j < BTA_HL_NUM_MCLS ; j++) - { - if (p_acb->mcb[j].in_use) - { - p_mcb = &p_acb->mcb[j]; - BTIF_TRACE_DEBUG("btif_hl_find_mcl_idx_using_mdl_id: mcl handle found j =%d",j); - for (x=0; x < BTA_HL_NUM_MDLS_PER_MCL ; x ++) - { - if (p_mcb->mdl[x].in_use) - { - p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, j,x); - btif_hl_release_socket(app_idx,j,x); - for (y=0; y<6; y++) - { - bd_addr.address[y] = p_mcb->bd_addr[y]; - } - BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, p_acb->app_id, - &bd_addr, p_dcb->local_mdep_cfg_idx, - p_dcb->channel_id, BTHL_CONN_STATE_DISCONNECTED, 0 ); - btif_hl_clean_mdl_cb(p_dcb); - if (!btif_hl_num_dchs_in_use(p_mcb->mcl_handle)) - BTA_HlCchClose(p_mcb->mcl_handle); - BTIF_TRACE_DEBUG("remote DCH close success mdl_idx=%d", x); - } - } - } - } + * + * Function btif_hl_find_mcl_idx_using_app_idx + * + * Description Find the MCL index using handle + * + * Returns bool + * + ******************************************************************************/ +bool btif_hl_find_mcl_idx_using_app_idx(tBTA_HL_MCL_HANDLE mcl_handle, + uint8_t p_app_idx, uint8_t* p_mcl_idx) { + btif_hl_app_cb_t* p_acb; + bool found = false; + uint8_t j; + + p_acb = BTIF_HL_GET_APP_CB_PTR(p_app_idx); + for (j = 0; j < BTA_HL_NUM_MCLS; j++) { + if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) { + found = true; + *p_mcl_idx = j; + break; + } + } + BTIF_TRACE_DEBUG("%s found=%dmcl_idx=%d", __func__, found, j); + return found; } /******************************************************************************* -** -** Function btif_hl_find_app_idx -** -** Description Find the application index using application ID -** -** Returns bool -** -*******************************************************************************/ -bool btif_hl_find_app_idx(uint8_t app_id, uint8_t *p_app_idx){ - bool found=false; - uint8_t i; - - for (i=0; i < BTA_HL_NUM_APPS ; i ++) - { - - if (btif_hl_cb.acb[i].in_use && - (btif_hl_cb.acb[i].app_id == app_id)) - { - found = true; - *p_app_idx = i; - break; + * + * Function btif_hl_clean_mdls_using_app_idx + * + * Description clean dch cpntrol bloack using app_idx + * + * Returns void + * + ******************************************************************************/ +void btif_hl_clean_mdls_using_app_idx(uint8_t app_idx) { + btif_hl_app_cb_t* p_acb; + btif_hl_mcl_cb_t* p_mcb; + btif_hl_mdl_cb_t* p_dcb; + uint8_t j, x, y; + bt_bdaddr_t bd_addr; + + p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + for (j = 0; j < BTA_HL_NUM_MCLS; j++) { + if (p_acb->mcb[j].in_use) { + p_mcb = &p_acb->mcb[j]; + BTIF_TRACE_DEBUG( + "btif_hl_find_mcl_idx_using_mdl_id: mcl handle found j =%d", j); + for (x = 0; x < BTA_HL_NUM_MDLS_PER_MCL; x++) { + if (p_mcb->mdl[x].in_use) { + p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, j, x); + btif_hl_release_socket(app_idx, j, x); + for (y = 0; y < 6; y++) { + bd_addr.address[y] = p_mcb->bd_addr[y]; + } + BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, p_acb->app_id, + &bd_addr, p_dcb->local_mdep_cfg_idx, + p_dcb->channel_id, BTHL_CONN_STATE_DISCONNECTED, + 0); + btif_hl_clean_mdl_cb(p_dcb); + if (!btif_hl_num_dchs_in_use(p_mcb->mcl_handle)) + BTA_HlCchClose(p_mcb->mcl_handle); + BTIF_TRACE_DEBUG("remote DCH close success mdl_idx=%d", x); } + } } - BTIF_TRACE_DEBUG("%s found=%d app_idx=%d", __func__, found, i ); + } +} - return found; +/******************************************************************************* + * + * Function btif_hl_find_app_idx + * + * Description Find the application index using application ID + * + * Returns bool + * + ******************************************************************************/ +bool btif_hl_find_app_idx(uint8_t app_id, uint8_t* p_app_idx) { + bool found = false; + uint8_t i; + + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + if (btif_hl_cb.acb[i].in_use && (btif_hl_cb.acb[i].app_id == app_id)) { + found = true; + *p_app_idx = i; + break; + } + } + BTIF_TRACE_DEBUG("%s found=%d app_idx=%d", __func__, found, i); + + return found; } /******************************************************************************* -** -** Function btif_hl_find_app_idx -** -** Description Find the application index using application ID -** -** Returns bool -** -*******************************************************************************/ -bool btif_hl_find_app_idx_using_mdepId(uint8_t mdep_id, uint8_t *p_app_idx){ - bool found=false; - uint8_t i; - - *p_app_idx = 0; - for (i=0; i < BTA_HL_NUM_APPS ; i ++) - { - BTIF_TRACE_DEBUG("btif_hl_find_app_idx_using_mdepId: MDEP-ID = %d", - btif_hl_cb.acb[i].sup_feature.mdep[0].mdep_id); - if (btif_hl_cb.acb[i].in_use && - (btif_hl_cb.acb[i].sup_feature.mdep[0].mdep_id == mdep_id)) - { - found = true; - *p_app_idx = i; - break; - } + * + * Function btif_hl_find_app_idx + * + * Description Find the application index using application ID + * + * Returns bool + * + ******************************************************************************/ +bool btif_hl_find_app_idx_using_mdepId(uint8_t mdep_id, uint8_t* p_app_idx) { + bool found = false; + uint8_t i; + + *p_app_idx = 0; + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + BTIF_TRACE_DEBUG("btif_hl_find_app_idx_using_mdepId: MDEP-ID = %d", + btif_hl_cb.acb[i].sup_feature.mdep[0].mdep_id); + if (btif_hl_cb.acb[i].in_use && + (btif_hl_cb.acb[i].sup_feature.mdep[0].mdep_id == mdep_id)) { + found = true; + *p_app_idx = i; + break; } - BTIF_TRACE_DEBUG("%s found=%d app_idx=%d", __func__, found, i ); + } + BTIF_TRACE_DEBUG("%s found=%d app_idx=%d", __func__, found, i); - return found; + return found; } /******************************************************************************* -** -** Function btif_hl_find_avail_mdl_idx -** -** Description Find a not in-use MDL index -** -** Returns bool -** -*******************************************************************************/ + * + * Function btif_hl_find_avail_mdl_idx + * + * Description Find a not in-use MDL index + * + * Returns bool + * + ******************************************************************************/ bool btif_hl_find_avail_mdl_idx(uint8_t app_idx, uint8_t mcl_idx, - uint8_t *p_mdl_idx){ - btif_hl_mcl_cb_t *p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - bool found=false; - uint8_t i; - - for (i=0; i < BTA_HL_NUM_MDLS_PER_MCL ; i ++) - { - if (!p_mcb->mdl[i].in_use) - { - btif_hl_clean_mdl_cb(&p_mcb->mdl[i]); - found = true; - *p_mdl_idx = i; - break; - } + uint8_t* p_mdl_idx) { + btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + bool found = false; + uint8_t i; + + for (i = 0; i < BTA_HL_NUM_MDLS_PER_MCL; i++) { + if (!p_mcb->mdl[i].in_use) { + btif_hl_clean_mdl_cb(&p_mcb->mdl[i]); + found = true; + *p_mdl_idx = i; + break; } + } - BTIF_TRACE_DEBUG("%s found=%d idx=%d",__func__, found, i); - return found; + BTIF_TRACE_DEBUG("%s found=%d idx=%d", __func__, found, i); + return found; } /******************************************************************************* -** -** Function btif_hl_find_avail_mcl_idx -** -** Description Find a not in-use MDL index -** -** Returns bool -** -*******************************************************************************/ -bool btif_hl_find_avail_mcl_idx(uint8_t app_idx, uint8_t *p_mcl_idx){ - bool found=false; - uint8_t i; - - for (i=0; i < BTA_HL_NUM_MCLS ; i ++) - { - if (!btif_hl_cb.acb[app_idx].mcb[i].in_use) - { - found = true; - *p_mcl_idx = i; - break; - } + * + * Function btif_hl_find_avail_mcl_idx + * + * Description Find a not in-use MDL index + * + * Returns bool + * + ******************************************************************************/ +bool btif_hl_find_avail_mcl_idx(uint8_t app_idx, uint8_t* p_mcl_idx) { + bool found = false; + uint8_t i; + + for (i = 0; i < BTA_HL_NUM_MCLS; i++) { + if (!btif_hl_cb.acb[app_idx].mcb[i].in_use) { + found = true; + *p_mcl_idx = i; + break; } - BTIF_TRACE_DEBUG("%s found=%d mcl_idx=%d", __func__, found, i); - return found; + } + BTIF_TRACE_DEBUG("%s found=%d mcl_idx=%d", __func__, found, i); + return found; } /******************************************************************************* -** -** Function btif_hl_find_avail_app_idx -** -** Description Find a not in-use APP index -** -** Returns bool -** -*******************************************************************************/ -static bool btif_hl_find_avail_app_idx(uint8_t *p_idx){ - bool found = false; - uint8_t i; - - for (i=0; i < BTA_HL_NUM_APPS ; i ++) - { - if (!btif_hl_cb.acb[i].in_use) - { - found = true; - *p_idx = i; - break; - } + * + * Function btif_hl_find_avail_app_idx + * + * Description Find a not in-use APP index + * + * Returns bool + * + ******************************************************************************/ +static bool btif_hl_find_avail_app_idx(uint8_t* p_idx) { + bool found = false; + uint8_t i; + + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + if (!btif_hl_cb.acb[i].in_use) { + found = true; + *p_idx = i; + break; } + } - BTIF_TRACE_DEBUG("%s found=%d app_idx=%d", __func__, found, i); - return found; + BTIF_TRACE_DEBUG("%s found=%d app_idx=%d", __func__, found, i); + return found; } /******************************************************************************* -** -** Function btif_hl_proc_dereg_cfm -** -** Description Process the de-registration confirmation -** -** Returns Nothing -** -*******************************************************************************/ -static void btif_hl_proc_dereg_cfm(tBTA_HL *p_data) + * + * Function btif_hl_proc_dereg_cfm + * + * Description Process the de-registration confirmation + * + * Returns Nothing + * + ******************************************************************************/ +static void btif_hl_proc_dereg_cfm(tBTA_HL* p_data) { - btif_hl_app_cb_t *p_acb; - uint8_t app_idx; - int app_id = 0; - bthl_app_reg_state_t state = BTHL_APP_REG_STATE_DEREG_SUCCESS; - - BTIF_TRACE_DEBUG("%s de-reg status=%d app_handle=%d", __func__, - p_data->dereg_cfm.status, p_data->dereg_cfm.app_handle); - - if (btif_hl_find_app_idx_using_app_id(p_data->dereg_cfm.app_id, &app_idx)) - { - p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - app_id = (int) p_acb->app_id; - if (p_data->dereg_cfm.status == BTA_HL_STATUS_OK) - { - btif_hl_clean_mdls_using_app_idx(app_idx); - for (size_t i = 0; i < BTA_HL_NUM_MCLS; i++) - alarm_free(p_acb->mcb[i].cch_timer); - memset(p_acb, 0, sizeof(btif_hl_app_cb_t)); - } - else - state = BTHL_APP_REG_STATE_DEREG_FAILED; + btif_hl_app_cb_t* p_acb; + uint8_t app_idx; + int app_id = 0; + bthl_app_reg_state_t state = BTHL_APP_REG_STATE_DEREG_SUCCESS; - BTIF_TRACE_DEBUG("call reg state callback app_id=%d state=%d", app_id, state); - BTIF_HL_CALL_CBACK(bt_hl_callbacks, app_reg_state_cb, app_id, state ); + BTIF_TRACE_DEBUG("%s de-reg status=%d app_handle=%d", __func__, + p_data->dereg_cfm.status, p_data->dereg_cfm.app_handle); - if (btif_hl_is_no_active_app()) - { - btif_hl_disable(); - } + if (btif_hl_find_app_idx_using_app_id(p_data->dereg_cfm.app_id, &app_idx)) { + p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + app_id = (int)p_acb->app_id; + if (p_data->dereg_cfm.status == BTA_HL_STATUS_OK) { + btif_hl_clean_mdls_using_app_idx(app_idx); + for (size_t i = 0; i < BTA_HL_NUM_MCLS; i++) + alarm_free(p_acb->mcb[i].cch_timer); + memset(p_acb, 0, sizeof(btif_hl_app_cb_t)); + } else + state = BTHL_APP_REG_STATE_DEREG_FAILED; + + BTIF_TRACE_DEBUG("call reg state callback app_id=%d state=%d", app_id, + state); + BTIF_HL_CALL_CBACK(bt_hl_callbacks, app_reg_state_cb, app_id, state); + + if (btif_hl_is_no_active_app()) { + btif_hl_disable(); } + } } /******************************************************************************* -** -** Function btif_hl_proc_reg_cfm -** -** Description Process the registration confirmation -** -** Returns Nothing -** -*******************************************************************************/ -static void btif_hl_proc_reg_cfm(tBTA_HL *p_data){ - btif_hl_app_cb_t *p_acb; - uint8_t app_idx; - bthl_app_reg_state_t state = BTHL_APP_REG_STATE_REG_SUCCESS; - - BTIF_TRACE_DEBUG("%s reg status=%d app_handle=%d", __func__, p_data->reg_cfm.status, p_data->reg_cfm.app_handle); - - if (btif_hl_find_app_idx(p_data->reg_cfm.app_id, &app_idx)) - { - p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - if (p_data->reg_cfm.status == BTA_HL_STATUS_OK) - { - p_acb->app_handle = p_data->reg_cfm.app_handle; - } - else - { - btif_hl_free_app_idx(app_idx); - reg_counter--; - state = BTHL_APP_REG_STATE_REG_FAILED; - } + * + * Function btif_hl_proc_reg_cfm + * + * Description Process the registration confirmation + * + * Returns Nothing + * + ******************************************************************************/ +static void btif_hl_proc_reg_cfm(tBTA_HL* p_data) { + btif_hl_app_cb_t* p_acb; + uint8_t app_idx; + bthl_app_reg_state_t state = BTHL_APP_REG_STATE_REG_SUCCESS; + + BTIF_TRACE_DEBUG("%s reg status=%d app_handle=%d", __func__, + p_data->reg_cfm.status, p_data->reg_cfm.app_handle); - BTIF_TRACE_DEBUG("%s call reg state callback app_id=%d reg state=%d", __func__, p_data->reg_cfm.app_id, state); - BTIF_HL_CALL_CBACK(bt_hl_callbacks, app_reg_state_cb, ((int) p_data->reg_cfm.app_id), state ); + if (btif_hl_find_app_idx(p_data->reg_cfm.app_id, &app_idx)) { + p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + if (p_data->reg_cfm.status == BTA_HL_STATUS_OK) { + p_acb->app_handle = p_data->reg_cfm.app_handle; + } else { + btif_hl_free_app_idx(app_idx); + reg_counter--; + state = BTHL_APP_REG_STATE_REG_FAILED; } + + BTIF_TRACE_DEBUG("%s call reg state callback app_id=%d reg state=%d", + __func__, p_data->reg_cfm.app_id, state); + BTIF_HL_CALL_CBACK(bt_hl_callbacks, app_reg_state_cb, + ((int)p_data->reg_cfm.app_id), state); + } } /******************************************************************************* -** -** Function btif_hl_set_chan_cb_state -** -** Description set the channel callback state -** -** Returns void -** -*******************************************************************************/ -void btif_hl_set_chan_cb_state(uint8_t app_idx, uint8_t mcl_idx, btif_hl_chan_cb_state_t state){ - btif_hl_pending_chan_cb_t *p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); - btif_hl_chan_cb_state_t cur_state = p_pcb->cb_state; - - if (cur_state != state) - { - p_pcb->cb_state = state; - BTIF_TRACE_DEBUG("%s state %d--->%d",__func__, cur_state, state); - } - + * + * Function btif_hl_set_chan_cb_state + * + * Description set the channel callback state + * + * Returns void + * + ******************************************************************************/ +void btif_hl_set_chan_cb_state(uint8_t app_idx, uint8_t mcl_idx, + btif_hl_chan_cb_state_t state) { + btif_hl_pending_chan_cb_t* p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); + btif_hl_chan_cb_state_t cur_state = p_pcb->cb_state; + + if (cur_state != state) { + p_pcb->cb_state = state; + BTIF_TRACE_DEBUG("%s state %d--->%d", __func__, cur_state, state); + } } /******************************************************************************* -** -** Function btif_hl_send_destroyed_cb -** -** Description send the channel destroyed callback -** -** Returns void -** -*******************************************************************************/ -void btif_hl_send_destroyed_cb(btif_hl_app_cb_t *p_acb ){ - bt_bdaddr_t bd_addr; - int app_id = (int) btif_hl_get_app_id(p_acb->delete_mdl.channel_id); - - btif_hl_copy_bda(&bd_addr, p_acb->delete_mdl.bd_addr); - BTIF_TRACE_DEBUG("%s",__func__); - BTIF_TRACE_DEBUG("call channel state callback channel_id=0x%08x mdep_cfg_idx=%d, state=%d fd=%d",p_acb->delete_mdl.channel_id, - p_acb->delete_mdl.mdep_cfg_idx, BTHL_CONN_STATE_DESTROYED, 0); - btif_hl_display_bt_bda(&bd_addr); - - BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, - &bd_addr, p_acb->delete_mdl.mdep_cfg_idx, - p_acb->delete_mdl.channel_id, BTHL_CONN_STATE_DESTROYED, 0 ); + * + * Function btif_hl_send_destroyed_cb + * + * Description send the channel destroyed callback + * + * Returns void + * + ******************************************************************************/ +void btif_hl_send_destroyed_cb(btif_hl_app_cb_t* p_acb) { + bt_bdaddr_t bd_addr; + int app_id = (int)btif_hl_get_app_id(p_acb->delete_mdl.channel_id); + + btif_hl_copy_bda(&bd_addr, p_acb->delete_mdl.bd_addr); + BTIF_TRACE_DEBUG("%s", __func__); + BTIF_TRACE_DEBUG( + "call channel state callback channel_id=0x%08x mdep_cfg_idx=%d, state=%d " + "fd=%d", + p_acb->delete_mdl.channel_id, p_acb->delete_mdl.mdep_cfg_idx, + BTHL_CONN_STATE_DESTROYED, 0); + btif_hl_display_bt_bda(&bd_addr); + + BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, &bd_addr, + p_acb->delete_mdl.mdep_cfg_idx, + p_acb->delete_mdl.channel_id, BTHL_CONN_STATE_DESTROYED, + 0); } /******************************************************************************* -** -** Function btif_hl_send_disconnecting_cb -** -** Description send a channel disconnecting callback -** -** Returns void -** -*******************************************************************************/ -void btif_hl_send_disconnecting_cb(uint8_t app_idx, uint8_t mcl_idx, uint8_t mdl_idx){ - btif_hl_mdl_cb_t *p_dcb = BTIF_HL_GET_MDL_CB_PTR( app_idx, mcl_idx, mdl_idx); - btif_hl_soc_cb_t *p_scb = p_dcb->p_scb; - bt_bdaddr_t bd_addr; - int app_id = (int) btif_hl_get_app_id(p_scb->channel_id); - - btif_hl_copy_bda(&bd_addr, p_scb->bd_addr); - - BTIF_TRACE_DEBUG("%s",__func__); - BTIF_TRACE_DEBUG("call channel state callback channel_id=0x%08x mdep_cfg_idx=%d, state=%d fd=%d",p_scb->channel_id, - p_scb->mdep_cfg_idx, BTHL_CONN_STATE_DISCONNECTING, p_scb->socket_id[0]); - btif_hl_display_bt_bda(&bd_addr); - BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, - &bd_addr, p_scb->mdep_cfg_idx, - p_scb->channel_id, BTHL_CONN_STATE_DISCONNECTING, p_scb->socket_id[0] ); + * + * Function btif_hl_send_disconnecting_cb + * + * Description send a channel disconnecting callback + * + * Returns void + * + ******************************************************************************/ +void btif_hl_send_disconnecting_cb(uint8_t app_idx, uint8_t mcl_idx, + uint8_t mdl_idx) { + btif_hl_mdl_cb_t* p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); + btif_hl_soc_cb_t* p_scb = p_dcb->p_scb; + bt_bdaddr_t bd_addr; + int app_id = (int)btif_hl_get_app_id(p_scb->channel_id); + + btif_hl_copy_bda(&bd_addr, p_scb->bd_addr); + + BTIF_TRACE_DEBUG("%s", __func__); + BTIF_TRACE_DEBUG( + "call channel state callback channel_id=0x%08x mdep_cfg_idx=%d, " + "state=%d fd=%d", + p_scb->channel_id, p_scb->mdep_cfg_idx, BTHL_CONN_STATE_DISCONNECTING, + p_scb->socket_id[0]); + btif_hl_display_bt_bda(&bd_addr); + BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, &bd_addr, + p_scb->mdep_cfg_idx, p_scb->channel_id, + BTHL_CONN_STATE_DISCONNECTING, p_scb->socket_id[0]); } /******************************************************************************* -** -** Function btif_hl_send_setup_connecting_cb -** -** Description send a channel connecting callback -** -** Returns void -** -*******************************************************************************/ -void btif_hl_send_setup_connecting_cb(uint8_t app_idx, uint8_t mcl_idx){ - btif_hl_pending_chan_cb_t *p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); - bt_bdaddr_t bd_addr; - int app_id = (int) btif_hl_get_app_id(p_pcb->channel_id); - - btif_hl_copy_bda(&bd_addr, p_pcb->bd_addr); - - if (p_pcb->in_use && p_pcb->cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING) - { - BTIF_TRACE_DEBUG("%s",__func__); - BTIF_TRACE_DEBUG("call channel state callback channel_id=0x%08x mdep_cfg_idx=%d state=%d fd=%d",p_pcb->channel_id, - p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_CONNECTING, 0); - btif_hl_display_bt_bda(&bd_addr); + * + * Function btif_hl_send_setup_connecting_cb + * + * Description send a channel connecting callback + * + * Returns void + * + ******************************************************************************/ +void btif_hl_send_setup_connecting_cb(uint8_t app_idx, uint8_t mcl_idx) { + btif_hl_pending_chan_cb_t* p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); + bt_bdaddr_t bd_addr; + int app_id = (int)btif_hl_get_app_id(p_pcb->channel_id); - BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, - &bd_addr, p_pcb->mdep_cfg_idx, - p_pcb->channel_id, BTHL_CONN_STATE_CONNECTING, 0 ); - btif_hl_set_chan_cb_state(app_idx, mcl_idx, BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING); - } + btif_hl_copy_bda(&bd_addr, p_pcb->bd_addr); + + if (p_pcb->in_use && + p_pcb->cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING) { + BTIF_TRACE_DEBUG("%s", __func__); + BTIF_TRACE_DEBUG( + "call channel state callback channel_id=0x%08x mdep_cfg_idx=%d " + "state=%d fd=%d", + p_pcb->channel_id, p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_CONNECTING, 0); + btif_hl_display_bt_bda(&bd_addr); + + BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, &bd_addr, + p_pcb->mdep_cfg_idx, p_pcb->channel_id, + BTHL_CONN_STATE_CONNECTING, 0); + btif_hl_set_chan_cb_state(app_idx, mcl_idx, + BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING); + } } /******************************************************************************* -** -** Function btif_hl_send_setup_disconnected_cb -** -** Description send a channel disconnected callback -** -** Returns void -** -*******************************************************************************/ -void btif_hl_send_setup_disconnected_cb(uint8_t app_idx, uint8_t mcl_idx){ - btif_hl_pending_chan_cb_t *p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); - bt_bdaddr_t bd_addr; - int app_id = (int) btif_hl_get_app_id(p_pcb->channel_id); - - btif_hl_copy_bda(&bd_addr, p_pcb->bd_addr); - - BTIF_TRACE_DEBUG("%s p_pcb->in_use=%d",__func__, p_pcb->in_use); - if (p_pcb->in_use) - { - BTIF_TRACE_DEBUG("%p_pcb->cb_state=%d",p_pcb->cb_state); - if (p_pcb->cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING) - { - BTIF_TRACE_DEBUG("call channel state callback channel_id=0x%08x mdep_cfg_idx=%d state=%d fd=%d",p_pcb->channel_id, - p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_CONNECTING, 0); - btif_hl_display_bt_bda(&bd_addr); - BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, - &bd_addr, p_pcb->mdep_cfg_idx, - p_pcb->channel_id, BTHL_CONN_STATE_CONNECTING, 0 ); - - BTIF_TRACE_DEBUG("call channel state callback channel_id=0x%08x mdep_cfg_idx=%d state=%d fd=%d",p_pcb->channel_id, - p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_DISCONNECTED, 0); - btif_hl_display_bt_bda(&bd_addr); - BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, - &bd_addr, p_pcb->mdep_cfg_idx, - p_pcb->channel_id, BTHL_CONN_STATE_DISCONNECTED, 0 ); - } - else if (p_pcb->cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING) - { - BTIF_TRACE_DEBUG("call channel state callback channel_id=0x%08x mdep_cfg_idx=%d state=%d fd=%d",p_pcb->channel_id, - p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_DISCONNECTED, 0); - btif_hl_display_bt_bda(&bd_addr); - BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, - &bd_addr, p_pcb->mdep_cfg_idx, - p_pcb->channel_id, BTHL_CONN_STATE_DISCONNECTED, 0 ); - } - btif_hl_clean_pcb(p_pcb); + * + * Function btif_hl_send_setup_disconnected_cb + * + * Description send a channel disconnected callback + * + * Returns void + * + ******************************************************************************/ +void btif_hl_send_setup_disconnected_cb(uint8_t app_idx, uint8_t mcl_idx) { + btif_hl_pending_chan_cb_t* p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); + bt_bdaddr_t bd_addr; + int app_id = (int)btif_hl_get_app_id(p_pcb->channel_id); + + btif_hl_copy_bda(&bd_addr, p_pcb->bd_addr); + + BTIF_TRACE_DEBUG("%s p_pcb->in_use=%d", __func__, p_pcb->in_use); + if (p_pcb->in_use) { + BTIF_TRACE_DEBUG("%p_pcb->cb_state=%d", p_pcb->cb_state); + if (p_pcb->cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING) { + BTIF_TRACE_DEBUG( + "call channel state callback channel_id=0x%08x mdep_cfg_idx=%d " + "state=%d fd=%d", + p_pcb->channel_id, p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_CONNECTING, + 0); + btif_hl_display_bt_bda(&bd_addr); + BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, &bd_addr, + p_pcb->mdep_cfg_idx, p_pcb->channel_id, + BTHL_CONN_STATE_CONNECTING, 0); + + BTIF_TRACE_DEBUG( + "call channel state callback channel_id=0x%08x mdep_cfg_idx=%d " + "state=%d fd=%d", + p_pcb->channel_id, p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_DISCONNECTED, + 0); + btif_hl_display_bt_bda(&bd_addr); + BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, &bd_addr, + p_pcb->mdep_cfg_idx, p_pcb->channel_id, + BTHL_CONN_STATE_DISCONNECTED, 0); + } else if (p_pcb->cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING) { + BTIF_TRACE_DEBUG( + "call channel state callback channel_id=0x%08x mdep_cfg_idx=%d " + "state=%d fd=%d", + p_pcb->channel_id, p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_DISCONNECTED, + 0); + btif_hl_display_bt_bda(&bd_addr); + BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id, &bd_addr, + p_pcb->mdep_cfg_idx, p_pcb->channel_id, + BTHL_CONN_STATE_DISCONNECTED, 0); } + btif_hl_clean_pcb(p_pcb); + } } /******************************************************************************* -** -** Function btif_hl_proc_sdp_query_cfm -** -** Description Process the SDP query confirmation -** -** Returns Nothing -** -*******************************************************************************/ -static bool btif_hl_proc_sdp_query_cfm(tBTA_HL *p_data){ - btif_hl_app_cb_t *p_acb; - btif_hl_mcl_cb_t *p_mcb; - tBTA_HL_SDP *p_sdp; - tBTA_HL_CCH_OPEN_PARAM open_param; - uint8_t app_idx, mcl_idx, sdp_idx = 0; - uint8_t num_recs, i, num_mdeps, j; - btif_hl_cch_op_t old_cch_oper; - bool status =false; - btif_hl_pending_chan_cb_t *p_pcb; - - BTIF_TRACE_DEBUG("%s", __func__); + * + * Function btif_hl_proc_sdp_query_cfm + * + * Description Process the SDP query confirmation + * + * Returns Nothing + * + ******************************************************************************/ +static bool btif_hl_proc_sdp_query_cfm(tBTA_HL* p_data) { + btif_hl_app_cb_t* p_acb; + btif_hl_mcl_cb_t* p_mcb; + tBTA_HL_SDP* p_sdp; + tBTA_HL_CCH_OPEN_PARAM open_param; + uint8_t app_idx, mcl_idx, sdp_idx = 0; + uint8_t num_recs, i, num_mdeps, j; + btif_hl_cch_op_t old_cch_oper; + bool status = false; + btif_hl_pending_chan_cb_t* p_pcb; + + BTIF_TRACE_DEBUG("%s", __func__); + + p_sdp = p_data->sdp_query_cfm.p_sdp; + num_recs = p_sdp->num_recs; + + BTIF_TRACE_DEBUG("num of SDP records=%d", num_recs); + for (i = 0; i < num_recs; i++) { + BTIF_TRACE_DEBUG("rec_idx=%d ctrl_psm=0x%x data_psm=0x%x", (i + 1), + p_sdp->sdp_rec[i].ctrl_psm, p_sdp->sdp_rec[i].data_psm); + BTIF_TRACE_DEBUG("MCAP supported procedures=0x%x", + p_sdp->sdp_rec[i].mcap_sup_proc); + num_mdeps = p_sdp->sdp_rec[i].num_mdeps; + BTIF_TRACE_DEBUG("num of mdeps =%d", num_mdeps); + for (j = 0; j < num_mdeps; j++) { + BTIF_TRACE_DEBUG("mdep_idx=%d mdep_id=0x%x data_type=0x%x mdep_role=0x%x", + (j + 1), p_sdp->sdp_rec[i].mdep_cfg[j].mdep_id, + p_sdp->sdp_rec[i].mdep_cfg[j].data_type, + p_sdp->sdp_rec[i].mdep_cfg[j].mdep_role); + } + } - p_sdp = p_data->sdp_query_cfm.p_sdp; - num_recs = p_sdp->num_recs; + if (btif_hl_find_app_idx_using_app_id(p_data->sdp_query_cfm.app_id, + &app_idx)) { + p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - BTIF_TRACE_DEBUG("num of SDP records=%d",num_recs); - for (i=0; isdp_rec[i].ctrl_psm, p_sdp->sdp_rec[i].data_psm); - BTIF_TRACE_DEBUG("MCAP supported procedures=0x%x",p_sdp->sdp_rec[i].mcap_sup_proc); - num_mdeps = p_sdp->sdp_rec[i].num_mdeps; - BTIF_TRACE_DEBUG("num of mdeps =%d",num_mdeps); - for (j=0; j< num_mdeps; j++) - { - BTIF_TRACE_DEBUG("mdep_idx=%d mdep_id=0x%x data_type=0x%x mdep_role=0x%x", - (j+1), - p_sdp->sdp_rec[i].mdep_cfg[j].mdep_id, - p_sdp->sdp_rec[i].mdep_cfg[j].data_type, - p_sdp->sdp_rec[i].mdep_cfg[j].mdep_role ); + if (btif_hl_find_mcl_idx(app_idx, p_data->sdp_query_cfm.bd_addr, + &mcl_idx)) { + p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + if (p_mcb->cch_oper != BTIF_HL_CCH_OP_NONE) { + memcpy(&p_mcb->sdp, p_sdp, sizeof(tBTA_HL_SDP)); + old_cch_oper = p_mcb->cch_oper; + p_mcb->cch_oper = BTIF_HL_CCH_OP_NONE; + + switch (old_cch_oper) { + case BTIF_HL_CCH_OP_MDEP_FILTERING: + status = btif_hl_find_sdp_idx_using_mdep_filter(app_idx, mcl_idx, + &sdp_idx); + break; + default: + break; } - } - if (btif_hl_find_app_idx_using_app_id(p_data->sdp_query_cfm.app_id, &app_idx)) - { - p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - - if (btif_hl_find_mcl_idx(app_idx, p_data->sdp_query_cfm.bd_addr, &mcl_idx)) - { - p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - if (p_mcb->cch_oper != BTIF_HL_CCH_OP_NONE) - { - memcpy(&p_mcb->sdp, p_sdp, sizeof(tBTA_HL_SDP)); - old_cch_oper = p_mcb->cch_oper; - p_mcb->cch_oper = BTIF_HL_CCH_OP_NONE; - - switch (old_cch_oper) - { - case BTIF_HL_CCH_OP_MDEP_FILTERING: - status = btif_hl_find_sdp_idx_using_mdep_filter(app_idx, - mcl_idx, &sdp_idx); - break; - default: - break; - } - - if (status) - { - p_mcb->sdp_idx = sdp_idx; - p_mcb->valid_sdp_idx = true; - p_mcb->ctrl_psm = p_mcb->sdp.sdp_rec[sdp_idx].ctrl_psm; - - switch (old_cch_oper) - { - case BTIF_HL_CCH_OP_MDEP_FILTERING: - p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); - if (p_pcb->in_use) - { - if (!p_pcb->abort_pending) - { - switch (p_pcb->op) - { - case BTIF_HL_PEND_DCH_OP_OPEN: - btif_hl_send_setup_connecting_cb(app_idx, mcl_idx); - break; - case BTIF_HL_PEND_DCH_OP_DELETE_MDL: - default: - break; - } - open_param.ctrl_psm = p_mcb->ctrl_psm; - bdcpy(open_param.bd_addr, p_mcb->bd_addr); - open_param.sec_mask = - (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT); - BTA_HlCchOpen(p_acb->app_id,p_acb->app_handle, &open_param); - } - else - { - BTIF_TRACE_DEBUG("channel abort pending"); - } - } - break; - - case BTIF_HL_CCH_OP_DCH_OPEN: - status = btif_hl_proc_pending_op(app_idx,mcl_idx); - break; - - default: - BTIF_TRACE_ERROR("Invalid CCH oper %d", old_cch_oper); - break; - } - } - else - { - BTIF_TRACE_ERROR("Can not find SDP idx discard CCH Open request"); - } + if (status) { + p_mcb->sdp_idx = sdp_idx; + p_mcb->valid_sdp_idx = true; + p_mcb->ctrl_psm = p_mcb->sdp.sdp_rec[sdp_idx].ctrl_psm; + + switch (old_cch_oper) { + case BTIF_HL_CCH_OP_MDEP_FILTERING: + p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); + if (p_pcb->in_use) { + if (!p_pcb->abort_pending) { + switch (p_pcb->op) { + case BTIF_HL_PEND_DCH_OP_OPEN: + btif_hl_send_setup_connecting_cb(app_idx, mcl_idx); + break; + case BTIF_HL_PEND_DCH_OP_DELETE_MDL: + default: + break; + } + open_param.ctrl_psm = p_mcb->ctrl_psm; + bdcpy(open_param.bd_addr, p_mcb->bd_addr); + open_param.sec_mask = + (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT); + BTA_HlCchOpen(p_acb->app_id, p_acb->app_handle, &open_param); + } else { + BTIF_TRACE_DEBUG("channel abort pending"); } - } + } + break; + + case BTIF_HL_CCH_OP_DCH_OPEN: + status = btif_hl_proc_pending_op(app_idx, mcl_idx); + break; + + default: + BTIF_TRACE_ERROR("Invalid CCH oper %d", old_cch_oper); + break; + } + } else { + BTIF_TRACE_ERROR("Can not find SDP idx discard CCH Open request"); } - return status; + } + } + } + return status; } /******************************************************************************* -** -** Function btif_hl_proc_cch_open_ind -** -** Description Process the CCH open indication -** -** Returns Nothing -** -*******************************************************************************/ -static void btif_hl_proc_cch_open_ind(tBTA_HL *p_data) + * + * Function btif_hl_proc_cch_open_ind + * + * Description Process the CCH open indication + * + * Returns Nothing + * + ******************************************************************************/ +static void btif_hl_proc_cch_open_ind(tBTA_HL* p_data) { - btif_hl_mcl_cb_t *p_mcb; - uint8_t mcl_idx; - int i; - - BTIF_TRACE_DEBUG("%s", __func__); - for(i=0; icch_open_ind.bd_addr, &mcl_idx)) - { - if (btif_hl_find_avail_mcl_idx(i, &mcl_idx)) - { - p_mcb = BTIF_HL_GET_MCL_CB_PTR(i, mcl_idx); - alarm_free(p_mcb->cch_timer); - memset(p_mcb, 0, sizeof(btif_hl_mcl_cb_t)); - p_mcb->in_use = true; - p_mcb->is_connected = true; - p_mcb->mcl_handle = p_data->cch_open_ind.mcl_handle; - bdcpy(p_mcb->bd_addr, p_data->cch_open_ind.bd_addr); - btif_hl_start_cch_timer(i, mcl_idx); - } - } - else - { - BTIF_TRACE_ERROR("The MCL already exist for cch_open_ind"); - } + btif_hl_mcl_cb_t* p_mcb; + uint8_t mcl_idx; + int i; + + BTIF_TRACE_DEBUG("%s", __func__); + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + if (btif_hl_cb.acb[i].in_use) { + if (!btif_hl_find_mcl_idx(i, p_data->cch_open_ind.bd_addr, &mcl_idx)) { + if (btif_hl_find_avail_mcl_idx(i, &mcl_idx)) { + p_mcb = BTIF_HL_GET_MCL_CB_PTR(i, mcl_idx); + alarm_free(p_mcb->cch_timer); + memset(p_mcb, 0, sizeof(btif_hl_mcl_cb_t)); + p_mcb->in_use = true; + p_mcb->is_connected = true; + p_mcb->mcl_handle = p_data->cch_open_ind.mcl_handle; + bdcpy(p_mcb->bd_addr, p_data->cch_open_ind.bd_addr); + btif_hl_start_cch_timer(i, mcl_idx); } + } else { + BTIF_TRACE_ERROR("The MCL already exist for cch_open_ind"); + } } + } } /******************************************************************************* -** -** Function btif_hl_proc_pending_op -** -** Description Process the pending dch operation. -** -** Returns Nothing -** -*******************************************************************************/ + * + * Function btif_hl_proc_pending_op + * + * Description Process the pending dch operation. + * + * Returns Nothing + * + ******************************************************************************/ bool btif_hl_proc_pending_op(uint8_t app_idx, uint8_t mcl_idx) { - - btif_hl_app_cb_t *p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - btif_hl_mcl_cb_t *p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - btif_hl_pending_chan_cb_t *p_pcb; - bool status = false; - tBTA_HL_DCH_OPEN_PARAM dch_open; - tBTA_HL_MDL_ID mdl_id; - tBTA_HL_DCH_RECONNECT_PARAM reconnect_param; - - p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); - if (p_pcb->in_use) - { - switch (p_pcb->op) - { - case BTIF_HL_PEND_DCH_OP_OPEN: - if (!p_pcb->abort_pending) - { - BTIF_TRACE_DEBUG("op BTIF_HL_PEND_DCH_OP_OPEN"); - dch_open.ctrl_psm = p_mcb->ctrl_psm; - dch_open.local_mdep_id = p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx].mdep_id; - if (btif_hl_find_peer_mdep_id(p_acb->app_id, p_mcb->bd_addr, - p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx].mdep_cfg.mdep_role, - p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx].mdep_cfg.data_cfg[0].data_type, &dch_open.peer_mdep_id )) - { - dch_open.local_cfg = p_acb->channel_type[p_pcb->mdep_cfg_idx]; - if ((p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx].mdep_cfg.mdep_role == BTA_HL_MDEP_ROLE_SOURCE) - && !btif_hl_is_the_first_reliable_existed(app_idx, mcl_idx)) - { - dch_open.local_cfg = BTA_HL_DCH_CFG_RELIABLE; - } - dch_open.sec_mask = (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT); - BTIF_TRACE_DEBUG("dch_open.local_cfg=%d ", dch_open.local_cfg); - btif_hl_send_setup_connecting_cb(app_idx,mcl_idx); - - if (!btif_hl_is_reconnect_possible(app_idx, mcl_idx, p_pcb->mdep_cfg_idx, &dch_open, &mdl_id )) - { - BTIF_TRACE_DEBUG("Issue DCH open, mcl_handle=%d",p_mcb->mcl_handle); - BTA_HlDchOpen(p_mcb->mcl_handle, &dch_open); - } - else - { - reconnect_param.ctrl_psm = p_mcb->ctrl_psm; - reconnect_param.mdl_id = mdl_id;; - BTIF_TRACE_DEBUG("Issue Reconnect ctrl_psm=0x%x mdl_id=0x%x",reconnect_param.ctrl_psm, reconnect_param.mdl_id); - BTA_HlDchReconnect(p_mcb->mcl_handle, &reconnect_param); - } - status = true; - } - } - else - { - btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx); - status = true; - } - break; - case BTIF_HL_PEND_DCH_OP_DELETE_MDL: - BTA_HlDeleteMdl(p_mcb->mcl_handle, p_acb->delete_mdl.mdl_id); - status = true; - break; - - default: - break; + btif_hl_app_cb_t* p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + btif_hl_pending_chan_cb_t* p_pcb; + bool status = false; + tBTA_HL_DCH_OPEN_PARAM dch_open; + tBTA_HL_MDL_ID mdl_id; + tBTA_HL_DCH_RECONNECT_PARAM reconnect_param; + + p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); + if (p_pcb->in_use) { + switch (p_pcb->op) { + case BTIF_HL_PEND_DCH_OP_OPEN: + if (!p_pcb->abort_pending) { + BTIF_TRACE_DEBUG("op BTIF_HL_PEND_DCH_OP_OPEN"); + dch_open.ctrl_psm = p_mcb->ctrl_psm; + dch_open.local_mdep_id = + p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx].mdep_id; + if (btif_hl_find_peer_mdep_id( + p_acb->app_id, p_mcb->bd_addr, + p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx] + .mdep_cfg.mdep_role, + p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx] + .mdep_cfg.data_cfg[0] + .data_type, + &dch_open.peer_mdep_id)) { + dch_open.local_cfg = p_acb->channel_type[p_pcb->mdep_cfg_idx]; + if ((p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx] + .mdep_cfg.mdep_role == BTA_HL_MDEP_ROLE_SOURCE) && + !btif_hl_is_the_first_reliable_existed(app_idx, mcl_idx)) { + dch_open.local_cfg = BTA_HL_DCH_CFG_RELIABLE; + } + dch_open.sec_mask = (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT); + BTIF_TRACE_DEBUG("dch_open.local_cfg=%d ", dch_open.local_cfg); + btif_hl_send_setup_connecting_cb(app_idx, mcl_idx); + + if (!btif_hl_is_reconnect_possible(app_idx, mcl_idx, + p_pcb->mdep_cfg_idx, &dch_open, + &mdl_id)) { + BTIF_TRACE_DEBUG("Issue DCH open, mcl_handle=%d", + p_mcb->mcl_handle); + BTA_HlDchOpen(p_mcb->mcl_handle, &dch_open); + } else { + reconnect_param.ctrl_psm = p_mcb->ctrl_psm; + reconnect_param.mdl_id = mdl_id; + ; + BTIF_TRACE_DEBUG("Issue Reconnect ctrl_psm=0x%x mdl_id=0x%x", + reconnect_param.ctrl_psm, + reconnect_param.mdl_id); + BTA_HlDchReconnect(p_mcb->mcl_handle, &reconnect_param); + } + status = true; + } + } else { + btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx); + status = true; } + break; + case BTIF_HL_PEND_DCH_OP_DELETE_MDL: + BTA_HlDeleteMdl(p_mcb->mcl_handle, p_acb->delete_mdl.mdl_id); + status = true; + break; + + default: + break; } - return status; + } + return status; } /******************************************************************************* -** -** Function btif_hl_proc_cch_open_cfm -** -** Description Process the CCH open confirmation -** -** Returns Nothing -** -*******************************************************************************/ -static bool btif_hl_proc_cch_open_cfm(tBTA_HL *p_data) + * + * Function btif_hl_proc_cch_open_cfm + * + * Description Process the CCH open confirmation + * + * Returns Nothing + * + ******************************************************************************/ +static bool btif_hl_proc_cch_open_cfm(tBTA_HL* p_data) { - btif_hl_mcl_cb_t *p_mcb; - uint8_t app_idx, mcl_idx; - bool status = false; - - BTIF_TRACE_DEBUG("%s", __func__); - - if (btif_hl_find_app_idx_using_app_id(p_data->cch_open_cfm.app_id, &app_idx)) - { - BTIF_TRACE_DEBUG("app_idx=%d", app_idx); - if (btif_hl_find_mcl_idx(app_idx, p_data->cch_open_cfm.bd_addr, &mcl_idx)) - { - p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - BTIF_TRACE_DEBUG("mcl_idx=%d, mcl_handle=%d", mcl_idx,p_data->cch_open_cfm.mcl_handle); - p_mcb->mcl_handle = p_data->cch_open_cfm.mcl_handle; - p_mcb->is_connected = true; - status = btif_hl_proc_pending_op(app_idx, mcl_idx); - if (status) - btif_hl_start_cch_timer(app_idx, mcl_idx); - } + btif_hl_mcl_cb_t* p_mcb; + uint8_t app_idx, mcl_idx; + bool status = false; + + BTIF_TRACE_DEBUG("%s", __func__); + + if (btif_hl_find_app_idx_using_app_id(p_data->cch_open_cfm.app_id, + &app_idx)) { + BTIF_TRACE_DEBUG("app_idx=%d", app_idx); + if (btif_hl_find_mcl_idx(app_idx, p_data->cch_open_cfm.bd_addr, &mcl_idx)) { + p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + BTIF_TRACE_DEBUG("mcl_idx=%d, mcl_handle=%d", mcl_idx, + p_data->cch_open_cfm.mcl_handle); + p_mcb->mcl_handle = p_data->cch_open_cfm.mcl_handle; + p_mcb->is_connected = true; + status = btif_hl_proc_pending_op(app_idx, mcl_idx); + if (status) btif_hl_start_cch_timer(app_idx, mcl_idx); } + } - return status; + return status; } /******************************************************************************* -** -** Function btif_hl_clean_mcb_using_handle -** -** Description clean control channel cb using handle -** -** Returns void -** -*******************************************************************************/ -static void btif_hl_clean_mcb_using_handle( tBTA_HL_MCL_HANDLE mcl_handle){ - btif_hl_app_cb_t *p_acb; - uint8_t i,j; - - for (i=0; imcb[j].in_use) - BTIF_TRACE_DEBUG("btif_hl_find_mcl_idx_using_handle: app_idx=%d," - "mcl_idx =%d mcl_handle=%d",i,j,p_acb->mcb[j].mcl_handle); - if (p_acb->mcb[j].in_use && - (p_acb->mcb[j].mcl_handle == mcl_handle)) - { - btif_hl_stop_cch_timer(i, j); - btif_hl_release_mcl_sockets(i, j); - btif_hl_send_setup_disconnected_cb(i, j); - btif_hl_clean_mcl_cb(i, j); - } - } + * + * Function btif_hl_clean_mcb_using_handle + * + * Description clean control channel cb using handle + * + * Returns void + * + ******************************************************************************/ +static void btif_hl_clean_mcb_using_handle(tBTA_HL_MCL_HANDLE mcl_handle) { + btif_hl_app_cb_t* p_acb; + uint8_t i, j; + + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + p_acb = BTIF_HL_GET_APP_CB_PTR(i); + for (j = 0; j < BTA_HL_NUM_MCLS; j++) { + if (p_acb->mcb[j].in_use) + BTIF_TRACE_DEBUG( + "btif_hl_find_mcl_idx_using_handle: app_idx=%d," + "mcl_idx =%d mcl_handle=%d", + i, j, p_acb->mcb[j].mcl_handle); + if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) { + btif_hl_stop_cch_timer(i, j); + btif_hl_release_mcl_sockets(i, j); + btif_hl_send_setup_disconnected_cb(i, j); + btif_hl_clean_mcl_cb(i, j); + } } + } } /******************************************************************************* -** -** Function btif_hl_proc_cch_close_ind -** -** Description Process the CCH close indication -** -** Returns Nothing -** -*******************************************************************************/ -static void btif_hl_proc_cch_close_ind(tBTA_HL *p_data) + * + * Function btif_hl_proc_cch_close_ind + * + * Description Process the CCH close indication + * + * Returns Nothing + * + ******************************************************************************/ +static void btif_hl_proc_cch_close_ind(tBTA_HL* p_data) { - BTIF_TRACE_DEBUG("%s", __func__); + BTIF_TRACE_DEBUG("%s", __func__); - btif_hl_clean_mcb_using_handle(p_data->cch_close_ind.mcl_handle); + btif_hl_clean_mcb_using_handle(p_data->cch_close_ind.mcl_handle); } /******************************************************************************* -** -** Function btif_hl_proc_cch_close_cfm -** -** Description Process the CCH close confirmation -** -** Returns Nothing -** -*******************************************************************************/ -static void btif_hl_proc_cch_close_cfm(tBTA_HL *p_data) -{ - BTIF_TRACE_DEBUG("%s", __func__); + * + * Function btif_hl_proc_cch_close_cfm + * + * Description Process the CCH close confirmation + * + * Returns Nothing + * + ******************************************************************************/ +static void btif_hl_proc_cch_close_cfm(tBTA_HL* p_data) { + BTIF_TRACE_DEBUG("%s", __func__); - btif_hl_clean_mcb_using_handle(p_data->cch_close_ind.mcl_handle); + btif_hl_clean_mcb_using_handle(p_data->cch_close_ind.mcl_handle); } /******************************************************************************* -** -** Function btif_hl_proc_create_ind -** -** Description Process the MDL create indication -** -** Returns Nothing -** -*******************************************************************************/ -static void btif_hl_proc_create_ind(tBTA_HL *p_data){ - btif_hl_app_cb_t *p_acb; - btif_hl_mcl_cb_t *p_mcb; - tBTA_HL_MDEP *p_mdep; - uint8_t orig_app_idx, mcl_idx, mdep_cfg_idx; - bool first_reliable_exist; - bool success = true; - tBTA_HL_DCH_CFG rsp_cfg = BTA_HL_DCH_CFG_UNKNOWN; - tBTA_HL_DCH_CREATE_RSP rsp_code = BTA_HL_DCH_CREATE_RSP_CFG_REJ; - tBTA_HL_DCH_CREATE_RSP_PARAM create_rsp_param; + * + * Function btif_hl_proc_create_ind + * + * Description Process the MDL create indication + * + * Returns Nothing + * + ******************************************************************************/ +static void btif_hl_proc_create_ind(tBTA_HL* p_data) { + btif_hl_app_cb_t* p_acb; + btif_hl_mcl_cb_t* p_mcb; + tBTA_HL_MDEP* p_mdep; + uint8_t orig_app_idx, mcl_idx, mdep_cfg_idx; + bool first_reliable_exist; + bool success = true; + tBTA_HL_DCH_CFG rsp_cfg = BTA_HL_DCH_CFG_UNKNOWN; + tBTA_HL_DCH_CREATE_RSP rsp_code = BTA_HL_DCH_CREATE_RSP_CFG_REJ; + tBTA_HL_DCH_CREATE_RSP_PARAM create_rsp_param; + + BTIF_TRACE_DEBUG("%s", __func__); + + // Find the correct app_idx based on the mdep_id; + btif_hl_find_app_idx_using_mdepId(p_data->dch_create_ind.local_mdep_id, + &orig_app_idx); + if (btif_hl_find_mcl_idx(orig_app_idx, p_data->dch_create_ind.bd_addr, + &mcl_idx)) { + p_acb = BTIF_HL_GET_APP_CB_PTR(orig_app_idx); + p_mcb = BTIF_HL_GET_MCL_CB_PTR(orig_app_idx, mcl_idx); + + if (btif_hl_find_mdep_cfg_idx(orig_app_idx, + p_data->dch_create_ind.local_mdep_id, + &mdep_cfg_idx)) { + p_mdep = &(p_acb->sup_feature.mdep[mdep_cfg_idx]); + first_reliable_exist = + btif_hl_is_the_first_reliable_existed(orig_app_idx, mcl_idx); + switch (p_mdep->mdep_cfg.mdep_role) { + case BTA_HL_MDEP_ROLE_SOURCE: + if (p_data->dch_create_ind.cfg == BTA_HL_DCH_CFG_NO_PREF) { + if (first_reliable_exist) { + rsp_cfg = p_acb->channel_type[mdep_cfg_idx]; + } else { + rsp_cfg = BTA_HL_DCH_CFG_RELIABLE; + } + rsp_code = BTA_HL_DCH_CREATE_RSP_SUCCESS; + } + + break; + case BTA_HL_MDEP_ROLE_SINK: + + BTIF_TRACE_DEBUG("btif_hl_proc_create_ind:BTA_HL_MDEP_ROLE_SINK"); + if ((p_data->dch_create_ind.cfg == BTA_HL_DCH_CFG_RELIABLE) || + (first_reliable_exist && + (p_data->dch_create_ind.cfg == BTA_HL_DCH_CFG_STREAMING))) { + rsp_code = BTA_HL_DCH_CREATE_RSP_SUCCESS; + rsp_cfg = p_data->dch_create_ind.cfg; + BTIF_TRACE_DEBUG( + "btif_hl_proc_create_ind:BTA_HL_MDEP_ROLE_SINK cfg = %d", + rsp_cfg); + } + break; + default: + break; + } + } + } else { + success = false; + } + + if (success) { + BTIF_TRACE_DEBUG("create response rsp_code=%d rsp_cfg=%d", rsp_code, + rsp_cfg); + create_rsp_param.local_mdep_id = p_data->dch_create_ind.local_mdep_id; + create_rsp_param.mdl_id = p_data->dch_create_ind.mdl_id; + create_rsp_param.rsp_code = rsp_code; + create_rsp_param.cfg_rsp = rsp_cfg; + BTA_HlDchCreateRsp(p_mcb->mcl_handle, &create_rsp_param); + } +} - BTIF_TRACE_DEBUG("%s", __func__); +/******************************************************************************* + * + * Function btif_hl_proc_dch_open_ind + * + * Description Process the DCH open indication + * + * Returns Nothing + * + ******************************************************************************/ +static void btif_hl_proc_dch_open_ind(tBTA_HL* p_data) -// Find the correct app_idx based on the mdep_id; - btif_hl_find_app_idx_using_mdepId(p_data->dch_create_ind.local_mdep_id,&orig_app_idx); - if (btif_hl_find_mcl_idx(orig_app_idx, p_data->dch_create_ind.bd_addr, &mcl_idx)) - { - p_acb =BTIF_HL_GET_APP_CB_PTR(orig_app_idx); - p_mcb =BTIF_HL_GET_MCL_CB_PTR(orig_app_idx, mcl_idx); - - if (btif_hl_find_mdep_cfg_idx(orig_app_idx, p_data->dch_create_ind.local_mdep_id, &mdep_cfg_idx)) - { - p_mdep = &(p_acb->sup_feature.mdep[mdep_cfg_idx]); - first_reliable_exist = btif_hl_is_the_first_reliable_existed(orig_app_idx, mcl_idx); - switch (p_mdep->mdep_cfg.mdep_role) - { - case BTA_HL_MDEP_ROLE_SOURCE: - if (p_data->dch_create_ind.cfg == BTA_HL_DCH_CFG_NO_PREF) - { - if (first_reliable_exist) - { - rsp_cfg = p_acb->channel_type[mdep_cfg_idx]; - } - else - { - rsp_cfg = BTA_HL_DCH_CFG_RELIABLE; - } - rsp_code = BTA_HL_DCH_CREATE_RSP_SUCCESS; - } - - break; - case BTA_HL_MDEP_ROLE_SINK: - - BTIF_TRACE_DEBUG("btif_hl_proc_create_ind:BTA_HL_MDEP_ROLE_SINK"); - if ((p_data->dch_create_ind.cfg == BTA_HL_DCH_CFG_RELIABLE) || - (first_reliable_exist && (p_data->dch_create_ind.cfg == BTA_HL_DCH_CFG_STREAMING))) - { - rsp_code = BTA_HL_DCH_CREATE_RSP_SUCCESS; - rsp_cfg = p_data->dch_create_ind.cfg; - BTIF_TRACE_DEBUG("btif_hl_proc_create_ind:BTA_HL_MDEP_ROLE_SINK cfg = %d",rsp_cfg); - } - break; - default: - break; - } +{ + btif_hl_mdl_cb_t* p_dcb; + uint8_t orig_app_idx, mcl_idx, mdl_idx, mdep_cfg_idx; + bool close_dch = false; + + BTIF_TRACE_DEBUG("%s", __func__); + + // Find the correct app_idx based on the mdep_id; + btif_hl_find_app_idx_using_mdepId(p_data->dch_open_ind.local_mdep_id, + &orig_app_idx); + + if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_open_ind.mcl_handle, + orig_app_idx, &mcl_idx)) { + if (btif_hl_find_avail_mdl_idx(orig_app_idx, mcl_idx, &mdl_idx)) { + p_dcb = BTIF_HL_GET_MDL_CB_PTR(orig_app_idx, mcl_idx, mdl_idx); + + if (btif_hl_find_mdep_cfg_idx(orig_app_idx, + p_data->dch_open_ind.local_mdep_id, + &mdep_cfg_idx)) { + p_dcb->in_use = true; + p_dcb->mdl_handle = p_data->dch_open_ind.mdl_handle; + p_dcb->local_mdep_cfg_idx = mdep_cfg_idx; + p_dcb->local_mdep_id = p_data->dch_open_ind.local_mdep_id; + p_dcb->mdl_id = p_data->dch_open_ind.mdl_id; + p_dcb->dch_mode = p_data->dch_open_ind.dch_mode; + p_dcb->dch_mode = p_data->dch_open_ind.dch_mode; + p_dcb->is_the_first_reliable = p_data->dch_open_ind.first_reliable; + p_dcb->mtu = p_data->dch_open_ind.mtu; + + if (btif_hl_find_channel_id_using_mdl_id(orig_app_idx, p_dcb->mdl_id, + &p_dcb->channel_id)) { + BTIF_TRACE_DEBUG(" app_idx=%d mcl_idx=%d mdl_idx=%d channel_id=%d", + orig_app_idx, mcl_idx, mdl_idx, p_dcb->channel_id); + if (!btif_hl_create_socket(orig_app_idx, mcl_idx, mdl_idx)) { + BTIF_TRACE_ERROR("Unable to create socket"); + close_dch = true; + } + } else { + BTIF_TRACE_ERROR("Unable find channel id for mdl_id=0x%x", + p_dcb->mdl_id); + close_dch = true; } - } - else - { - success = false; - } + } else { + BTIF_TRACE_ERROR("INVALID_LOCAL_MDEP_ID mdep_id=%d", + p_data->dch_open_cfm.local_mdep_id); + close_dch = true; + } - if (success) - { - BTIF_TRACE_DEBUG("create response rsp_code=%d rsp_cfg=%d", rsp_code, rsp_cfg ); - create_rsp_param.local_mdep_id = p_data->dch_create_ind.local_mdep_id; - create_rsp_param.mdl_id = p_data->dch_create_ind.mdl_id; - create_rsp_param.rsp_code = rsp_code; - create_rsp_param.cfg_rsp = rsp_cfg; - BTA_HlDchCreateRsp(p_mcb->mcl_handle, &create_rsp_param); - } + if (close_dch) btif_hl_clean_mdl_cb(p_dcb); + } else + close_dch = true; + } else + close_dch = true; + + if (close_dch) BTA_HlDchClose(p_data->dch_open_cfm.mdl_handle); } /******************************************************************************* -** -** Function btif_hl_proc_dch_open_ind -** -** Description Process the DCH open indication -** -** Returns Nothing -** -*******************************************************************************/ -static void btif_hl_proc_dch_open_ind(tBTA_HL *p_data) + * + * Function btif_hl_proc_dch_open_cfm + * + * Description Process the DCH close confirmation + * + * Returns Nothing + * + ******************************************************************************/ +static bool btif_hl_proc_dch_open_cfm(tBTA_HL* p_data) { - btif_hl_mdl_cb_t *p_dcb; - uint8_t orig_app_idx, mcl_idx, mdl_idx, mdep_cfg_idx; - bool close_dch = false; + btif_hl_mdl_cb_t* p_dcb; + btif_hl_pending_chan_cb_t* p_pcb; + uint8_t app_idx, mcl_idx, mdl_idx, mdep_cfg_idx; + bool status = false; + bool close_dch = false; - BTIF_TRACE_DEBUG("%s", __func__); + BTIF_TRACE_DEBUG("%s", __func__); - // Find the correct app_idx based on the mdep_id; - btif_hl_find_app_idx_using_mdepId(p_data->dch_open_ind.local_mdep_id,&orig_app_idx); + // Find the correct app_idx based on the mdep_id; + btif_hl_find_app_idx_using_mdepId(p_data->dch_open_cfm.local_mdep_id, + &app_idx); - if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_open_ind.mcl_handle, orig_app_idx, &mcl_idx )) - { - if (btif_hl_find_avail_mdl_idx(orig_app_idx, mcl_idx, &mdl_idx)) - { - p_dcb = BTIF_HL_GET_MDL_CB_PTR(orig_app_idx, mcl_idx, mdl_idx); - - if (btif_hl_find_mdep_cfg_idx(orig_app_idx, p_data->dch_open_ind.local_mdep_id, &mdep_cfg_idx)) - { - p_dcb->in_use = true; - p_dcb->mdl_handle = p_data->dch_open_ind.mdl_handle; - p_dcb->local_mdep_cfg_idx = mdep_cfg_idx; - p_dcb->local_mdep_id = p_data->dch_open_ind.local_mdep_id; - p_dcb->mdl_id = p_data->dch_open_ind.mdl_id; - p_dcb->dch_mode = p_data->dch_open_ind.dch_mode; - p_dcb->dch_mode = p_data->dch_open_ind.dch_mode; - p_dcb->is_the_first_reliable = p_data->dch_open_ind.first_reliable; - p_dcb->mtu = p_data->dch_open_ind.mtu; - - if(btif_hl_find_channel_id_using_mdl_id(orig_app_idx,p_dcb->mdl_id , &p_dcb->channel_id)) - { - BTIF_TRACE_DEBUG(" app_idx=%d mcl_idx=%d mdl_idx=%d channel_id=%d", - orig_app_idx, mcl_idx, mdl_idx, p_dcb->channel_id ); - if (!btif_hl_create_socket(orig_app_idx, mcl_idx, mdl_idx)) - { - BTIF_TRACE_ERROR("Unable to create socket"); - close_dch = true; - } - } - else - { - BTIF_TRACE_ERROR("Unable find channel id for mdl_id=0x%x", p_dcb->mdl_id ); - close_dch = true; - } - } - else - { - BTIF_TRACE_ERROR("INVALID_LOCAL_MDEP_ID mdep_id=%d",p_data->dch_open_cfm.local_mdep_id); - close_dch = true; - } + if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_open_cfm.mcl_handle, + app_idx, &mcl_idx)) { + p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); - if (close_dch) - btif_hl_clean_mdl_cb(p_dcb); + if (btif_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx)) { + p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); + + if (btif_hl_find_mdep_cfg_idx(app_idx, p_data->dch_open_cfm.local_mdep_id, + &mdep_cfg_idx)) { + p_dcb->in_use = true; + p_dcb->mdl_handle = p_data->dch_open_cfm.mdl_handle; + p_dcb->local_mdep_cfg_idx = mdep_cfg_idx; + p_dcb->local_mdep_id = p_data->dch_open_cfm.local_mdep_id; + p_dcb->mdl_id = p_data->dch_open_cfm.mdl_id; + p_dcb->dch_mode = p_data->dch_open_cfm.dch_mode; + p_dcb->is_the_first_reliable = p_data->dch_open_cfm.first_reliable; + p_dcb->mtu = p_data->dch_open_cfm.mtu; + p_dcb->channel_id = p_pcb->channel_id; + + BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=%d mdl_idx=%d", app_idx, mcl_idx, + mdl_idx); + btif_hl_send_setup_connecting_cb(app_idx, mcl_idx); + if (btif_hl_create_socket(app_idx, mcl_idx, mdl_idx)) { + status = true; + BTIF_TRACE_DEBUG( + "app_idx=%d mcl_idx=%d mdl_idx=%d p_dcb->channel_id=0x%08x", + app_idx, mcl_idx, mdl_idx, p_dcb->channel_id); + btif_hl_clean_pcb(p_pcb); + } else { + BTIF_TRACE_ERROR("Unable to create socket"); + close_dch = true; } - else - close_dch = true; - } - else + } else { + BTIF_TRACE_ERROR("INVALID_LOCAL_MDEP_ID mdep_id=%d", + p_data->dch_open_cfm.local_mdep_id); close_dch = true; + } - if (close_dch) + if (close_dch) { + btif_hl_clean_mdl_cb(p_dcb); BTA_HlDchClose(p_data->dch_open_cfm.mdl_handle); -} - -/******************************************************************************* -** -** Function btif_hl_proc_dch_open_cfm -** -** Description Process the DCH close confirmation -** -** Returns Nothing -** -*******************************************************************************/ -static bool btif_hl_proc_dch_open_cfm(tBTA_HL *p_data) - -{ - btif_hl_mdl_cb_t *p_dcb; - btif_hl_pending_chan_cb_t *p_pcb; - uint8_t app_idx, mcl_idx, mdl_idx, mdep_cfg_idx; - bool status = false; - bool close_dch = false; - - BTIF_TRACE_DEBUG("%s", __func__); - - // Find the correct app_idx based on the mdep_id; - btif_hl_find_app_idx_using_mdepId(p_data->dch_open_cfm.local_mdep_id,&app_idx); - - if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_open_cfm.mcl_handle, app_idx, &mcl_idx )) - { - p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); - - if (btif_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx)) - { - p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); - - if (btif_hl_find_mdep_cfg_idx(app_idx, p_data->dch_open_cfm.local_mdep_id, &mdep_cfg_idx)) - { - p_dcb->in_use = true; - p_dcb->mdl_handle = p_data->dch_open_cfm.mdl_handle; - p_dcb->local_mdep_cfg_idx = mdep_cfg_idx; - p_dcb->local_mdep_id = p_data->dch_open_cfm.local_mdep_id; - p_dcb->mdl_id = p_data->dch_open_cfm.mdl_id; - p_dcb->dch_mode = p_data->dch_open_cfm.dch_mode; - p_dcb->is_the_first_reliable= p_data->dch_open_cfm.first_reliable; - p_dcb->mtu = p_data->dch_open_cfm.mtu; - p_dcb->channel_id = p_pcb->channel_id; - - BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=%d mdl_idx=%d", app_idx, mcl_idx, mdl_idx ); - btif_hl_send_setup_connecting_cb(app_idx, mcl_idx); - if (btif_hl_create_socket(app_idx, mcl_idx, mdl_idx)) - { - status = true; - BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=%d mdl_idx=%d p_dcb->channel_id=0x%08x", - app_idx, mcl_idx, mdl_idx, p_dcb->channel_id); - btif_hl_clean_pcb(p_pcb); - } - else - { - BTIF_TRACE_ERROR("Unable to create socket"); - close_dch = true; - } - } - else - { - BTIF_TRACE_ERROR("INVALID_LOCAL_MDEP_ID mdep_id=%d",p_data->dch_open_cfm.local_mdep_id); - close_dch = true; - } - - if (close_dch) - { - btif_hl_clean_mdl_cb(p_dcb); - BTA_HlDchClose(p_data->dch_open_cfm.mdl_handle); - } - } + } } + } - return status; + return status; } /******************************************************************************* -** -** Function btif_hl_proc_dch_reconnect_cfm -** -** Description Process the DCH reconnect indication -** -** Returns Nothing -** -*******************************************************************************/ -static bool btif_hl_proc_dch_reconnect_cfm(tBTA_HL *p_data) -{ - btif_hl_mdl_cb_t *p_dcb; - btif_hl_pending_chan_cb_t *p_pcb; - uint8_t app_idx, mcl_idx, mdl_idx, mdep_cfg_idx; - bool status = false; - bool close_dch = false; - - BTIF_TRACE_DEBUG("%s", __func__); + * + * Function btif_hl_proc_dch_reconnect_cfm + * + * Description Process the DCH reconnect indication + * + * Returns Nothing + * + ******************************************************************************/ +static bool btif_hl_proc_dch_reconnect_cfm(tBTA_HL* p_data) { + btif_hl_mdl_cb_t* p_dcb; + btif_hl_pending_chan_cb_t* p_pcb; + uint8_t app_idx, mcl_idx, mdl_idx, mdep_cfg_idx; + bool status = false; + bool close_dch = false; - btif_hl_find_app_idx_using_mdepId(p_data->dch_reconnect_cfm.local_mdep_id,&app_idx); + BTIF_TRACE_DEBUG("%s", __func__); - if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_reconnect_cfm.mcl_handle, app_idx, &mcl_idx )) - { - p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); + btif_hl_find_app_idx_using_mdepId(p_data->dch_reconnect_cfm.local_mdep_id, + &app_idx); - if (btif_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx)) - { - p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); - - if (btif_hl_find_mdep_cfg_idx(app_idx, p_data->dch_reconnect_cfm.local_mdep_id, &mdep_cfg_idx)) - { - p_dcb->in_use = true; - p_dcb->mdl_handle = p_data->dch_reconnect_cfm.mdl_handle; - p_dcb->local_mdep_cfg_idx = mdep_cfg_idx; - p_dcb->local_mdep_id = p_data->dch_reconnect_cfm.local_mdep_id; - p_dcb->mdl_id = p_data->dch_reconnect_cfm.mdl_id; - p_dcb->dch_mode = p_data->dch_reconnect_cfm.dch_mode; - p_dcb->is_the_first_reliable= p_data->dch_reconnect_cfm.first_reliable; - p_dcb->mtu = p_data->dch_reconnect_cfm.mtu; - p_dcb->channel_id = p_pcb->channel_id; - - BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=%d mdl_idx=%d", app_idx, mcl_idx, mdl_idx ); - btif_hl_send_setup_connecting_cb(app_idx, mcl_idx); - if (btif_hl_create_socket(app_idx, mcl_idx, mdl_idx)) - { - status = true; - BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=%d mdl_idx=%d p_dcb->channel_id=0x%08x", - app_idx, mcl_idx, mdl_idx, p_dcb->channel_id); - btif_hl_clean_pcb(p_pcb); - } - else - { - BTIF_TRACE_ERROR("Unable to create socket"); - close_dch = true; - } - } - else - { - BTIF_TRACE_ERROR("INVALID_LOCAL_MDEP_ID mdep_id=%d",p_data->dch_open_cfm.local_mdep_id); - close_dch = true; - } + if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_reconnect_cfm.mcl_handle, + app_idx, &mcl_idx)) { + p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); - if (close_dch) - { - btif_hl_clean_mdl_cb(p_dcb); - BTA_HlDchClose(p_data->dch_reconnect_cfm.mdl_handle); - } + if (btif_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx)) { + p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); + + if (btif_hl_find_mdep_cfg_idx(app_idx, + p_data->dch_reconnect_cfm.local_mdep_id, + &mdep_cfg_idx)) { + p_dcb->in_use = true; + p_dcb->mdl_handle = p_data->dch_reconnect_cfm.mdl_handle; + p_dcb->local_mdep_cfg_idx = mdep_cfg_idx; + p_dcb->local_mdep_id = p_data->dch_reconnect_cfm.local_mdep_id; + p_dcb->mdl_id = p_data->dch_reconnect_cfm.mdl_id; + p_dcb->dch_mode = p_data->dch_reconnect_cfm.dch_mode; + p_dcb->is_the_first_reliable = p_data->dch_reconnect_cfm.first_reliable; + p_dcb->mtu = p_data->dch_reconnect_cfm.mtu; + p_dcb->channel_id = p_pcb->channel_id; + + BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=%d mdl_idx=%d", app_idx, mcl_idx, + mdl_idx); + btif_hl_send_setup_connecting_cb(app_idx, mcl_idx); + if (btif_hl_create_socket(app_idx, mcl_idx, mdl_idx)) { + status = true; + BTIF_TRACE_DEBUG( + "app_idx=%d mcl_idx=%d mdl_idx=%d p_dcb->channel_id=0x%08x", + app_idx, mcl_idx, mdl_idx, p_dcb->channel_id); + btif_hl_clean_pcb(p_pcb); + } else { + BTIF_TRACE_ERROR("Unable to create socket"); + close_dch = true; } - } + } else { + BTIF_TRACE_ERROR("INVALID_LOCAL_MDEP_ID mdep_id=%d", + p_data->dch_open_cfm.local_mdep_id); + close_dch = true; + } - return status; + if (close_dch) { + btif_hl_clean_mdl_cb(p_dcb); + BTA_HlDchClose(p_data->dch_reconnect_cfm.mdl_handle); + } + } + } + return status; } /******************************************************************************* -** -** Function btif_hl_proc_dch_reconnect_ind -** -** Description Process the DCH reconnect indication -** -** Returns Nothing -** -*******************************************************************************/ -static void btif_hl_proc_dch_reconnect_ind(tBTA_HL *p_data) + * + * Function btif_hl_proc_dch_reconnect_ind + * + * Description Process the DCH reconnect indication + * + * Returns Nothing + * + ******************************************************************************/ +static void btif_hl_proc_dch_reconnect_ind(tBTA_HL* p_data) { - btif_hl_app_cb_t *p_acb; - btif_hl_mdl_cb_t *p_dcb; - uint8_t app_idx, mcl_idx, mdl_idx, mdep_cfg_idx; - bool close_dch = false; - - BTIF_TRACE_DEBUG("%s", __func__); + btif_hl_app_cb_t* p_acb; + btif_hl_mdl_cb_t* p_dcb; + uint8_t app_idx, mcl_idx, mdl_idx, mdep_cfg_idx; + bool close_dch = false; - // Find the correct app_idx based on the mdep_id; - btif_hl_find_app_idx_using_mdepId(p_data->dch_reconnect_ind.local_mdep_id,&app_idx); + BTIF_TRACE_DEBUG("%s", __func__); - if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_reconnect_ind.mcl_handle, app_idx, &mcl_idx )) - { - p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - BTIF_TRACE_DEBUG("btif_hl_proc_dch_reconnect_ind: app_idx = %d, mcl_idx = %d", - app_idx, mcl_idx); - - if (btif_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx)) - { - p_dcb =BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); - - if (btif_hl_find_mdep_cfg_idx(app_idx, p_data->dch_reconnect_ind.local_mdep_id, &mdep_cfg_idx)) - { - p_dcb->in_use = true; - p_dcb->mdl_handle = p_data->dch_reconnect_ind.mdl_handle; - p_dcb->local_mdep_cfg_idx = mdep_cfg_idx; - p_dcb->local_mdep_id = p_data->dch_reconnect_ind.local_mdep_id; - p_dcb->mdl_id = p_data->dch_reconnect_ind.mdl_id; - p_dcb->dch_mode = p_data->dch_reconnect_ind.dch_mode; - p_dcb->dch_mode = p_data->dch_reconnect_ind.dch_mode; - p_dcb->is_the_first_reliable= p_data->dch_reconnect_ind.first_reliable; - p_dcb->mtu = p_data->dch_reconnect_ind.mtu; - p_dcb->channel_id = btif_hl_get_next_channel_id(p_acb->app_id); - - BTIF_TRACE_DEBUG(" app_idx=%d mcl_idx=%d mdl_idx=%d channel_id=%d", - app_idx, mcl_idx, mdl_idx, p_dcb->channel_id ); - if (!btif_hl_create_socket(app_idx, mcl_idx, mdl_idx)) - { - BTIF_TRACE_ERROR("Unable to create socket"); - close_dch = true; - } - } - else - { - BTIF_TRACE_ERROR("INVALID_LOCAL_MDEP_ID mdep_id=%d",p_data->dch_open_cfm.local_mdep_id); - close_dch = true; - } + // Find the correct app_idx based on the mdep_id; + btif_hl_find_app_idx_using_mdepId(p_data->dch_reconnect_ind.local_mdep_id, + &app_idx); - if (close_dch) - btif_hl_clean_mdl_cb(p_dcb); + if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_reconnect_ind.mcl_handle, + app_idx, &mcl_idx)) { + p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + BTIF_TRACE_DEBUG( + "btif_hl_proc_dch_reconnect_ind: app_idx = %d, mcl_idx = %d", app_idx, + mcl_idx); + + if (btif_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx)) { + p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); + + if (btif_hl_find_mdep_cfg_idx(app_idx, + p_data->dch_reconnect_ind.local_mdep_id, + &mdep_cfg_idx)) { + p_dcb->in_use = true; + p_dcb->mdl_handle = p_data->dch_reconnect_ind.mdl_handle; + p_dcb->local_mdep_cfg_idx = mdep_cfg_idx; + p_dcb->local_mdep_id = p_data->dch_reconnect_ind.local_mdep_id; + p_dcb->mdl_id = p_data->dch_reconnect_ind.mdl_id; + p_dcb->dch_mode = p_data->dch_reconnect_ind.dch_mode; + p_dcb->dch_mode = p_data->dch_reconnect_ind.dch_mode; + p_dcb->is_the_first_reliable = p_data->dch_reconnect_ind.first_reliable; + p_dcb->mtu = p_data->dch_reconnect_ind.mtu; + p_dcb->channel_id = btif_hl_get_next_channel_id(p_acb->app_id); + + BTIF_TRACE_DEBUG(" app_idx=%d mcl_idx=%d mdl_idx=%d channel_id=%d", + app_idx, mcl_idx, mdl_idx, p_dcb->channel_id); + if (!btif_hl_create_socket(app_idx, mcl_idx, mdl_idx)) { + BTIF_TRACE_ERROR("Unable to create socket"); + close_dch = true; } - else - close_dch = true; - } - else + } else { + BTIF_TRACE_ERROR("INVALID_LOCAL_MDEP_ID mdep_id=%d", + p_data->dch_open_cfm.local_mdep_id); close_dch = true; + } - if (close_dch) - BTA_HlDchClose(p_data->dch_reconnect_ind.mdl_handle); + if (close_dch) btif_hl_clean_mdl_cb(p_dcb); + } else + close_dch = true; + } else + close_dch = true; + if (close_dch) BTA_HlDchClose(p_data->dch_reconnect_ind.mdl_handle); } /******************************************************************************* -** -** Function btif_hl_proc_dch_close_ind -** -** Description Process the DCH close indication -** -** Returns Nothing -** -*******************************************************************************/ -static void btif_hl_proc_dch_close_ind(tBTA_HL *p_data) + * + * Function btif_hl_proc_dch_close_ind + * + * Description Process the DCH close indication + * + * Returns Nothing + * + ******************************************************************************/ +static void btif_hl_proc_dch_close_ind(tBTA_HL* p_data) { - btif_hl_mdl_cb_t *p_dcb; - btif_hl_mcl_cb_t *p_mcb; - uint8_t app_idx, mcl_idx, mdl_idx; - - BTIF_TRACE_DEBUG("%s", __func__); - if (btif_hl_find_mdl_idx_using_handle(p_data->dch_close_ind.mdl_handle, - &app_idx, &mcl_idx, &mdl_idx )) - { - p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); - btif_hl_release_socket(app_idx,mcl_idx, mdl_idx); - btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx); - p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx,mcl_idx); - btif_hl_clean_mdl_cb(p_dcb); - if (!btif_hl_num_dchs_in_use(p_mcb->mcl_handle)) - btif_hl_start_cch_timer(app_idx, mcl_idx); - BTIF_TRACE_DEBUG("remote DCH close success mdl_idx=%d", mdl_idx); - } + btif_hl_mdl_cb_t* p_dcb; + btif_hl_mcl_cb_t* p_mcb; + uint8_t app_idx, mcl_idx, mdl_idx; + + BTIF_TRACE_DEBUG("%s", __func__); + if (btif_hl_find_mdl_idx_using_handle(p_data->dch_close_ind.mdl_handle, + &app_idx, &mcl_idx, &mdl_idx)) { + p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); + btif_hl_release_socket(app_idx, mcl_idx, mdl_idx); + btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx); + p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + btif_hl_clean_mdl_cb(p_dcb); + if (!btif_hl_num_dchs_in_use(p_mcb->mcl_handle)) + btif_hl_start_cch_timer(app_idx, mcl_idx); + BTIF_TRACE_DEBUG("remote DCH close success mdl_idx=%d", mdl_idx); + } } /******************************************************************************* -** -** Function btif_hl_proc_dch_close_cfm -** -** Description Process the DCH reconnect confirmation -** -** Returns Nothing -** -*******************************************************************************/ -static void btif_hl_proc_dch_close_cfm(tBTA_HL *p_data) + * + * Function btif_hl_proc_dch_close_cfm + * + * Description Process the DCH reconnect confirmation + * + * Returns Nothing + * + ******************************************************************************/ +static void btif_hl_proc_dch_close_cfm(tBTA_HL* p_data) { - btif_hl_mdl_cb_t *p_dcb; - btif_hl_mcl_cb_t *p_mcb; - uint8_t app_idx, mcl_idx, mdl_idx; - - BTIF_TRACE_DEBUG("%s", __func__); - if (btif_hl_find_mdl_idx_using_handle(p_data->dch_close_cfm.mdl_handle, - &app_idx, &mcl_idx, &mdl_idx )) - { - p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); - btif_hl_release_socket(app_idx,mcl_idx,mdl_idx); - btif_hl_clean_mdl_cb(p_dcb); - p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx,mcl_idx); - if (!btif_hl_num_dchs_in_use(p_mcb->mcl_handle)) - btif_hl_start_cch_timer(app_idx, mcl_idx); - BTIF_TRACE_DEBUG(" local DCH close success mdl_idx=%d", mdl_idx); - } + btif_hl_mdl_cb_t* p_dcb; + btif_hl_mcl_cb_t* p_mcb; + uint8_t app_idx, mcl_idx, mdl_idx; + + BTIF_TRACE_DEBUG("%s", __func__); + if (btif_hl_find_mdl_idx_using_handle(p_data->dch_close_cfm.mdl_handle, + &app_idx, &mcl_idx, &mdl_idx)) { + p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); + btif_hl_release_socket(app_idx, mcl_idx, mdl_idx); + btif_hl_clean_mdl_cb(p_dcb); + p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + if (!btif_hl_num_dchs_in_use(p_mcb->mcl_handle)) + btif_hl_start_cch_timer(app_idx, mcl_idx); + BTIF_TRACE_DEBUG(" local DCH close success mdl_idx=%d", mdl_idx); + } } /******************************************************************************* -** -** Function btif_hl_proc_abort_ind -** -** Description Process the abort indicaiton -** -** Returns Nothing -** -*******************************************************************************/ -static void btif_hl_proc_abort_ind(tBTA_HL_MCL_HANDLE mcl_handle){ - - BTIF_TRACE_DEBUG("%s", __func__ ); - btif_hl_app_cb_t *p_acb; - uint8_t i,j; - - for (i=0; imcb[j].in_use) - BTIF_TRACE_DEBUG("btif_hl_find_mcl_idx_using_handle: app_idx=%d,mcl_idx =%d mcl_handle=%d",i,j,p_acb->mcb[j].mcl_handle); - if (p_acb->mcb[j].in_use && - (p_acb->mcb[j].mcl_handle == mcl_handle)) - { - btif_hl_stop_cch_timer(i, j); - btif_hl_send_setup_disconnected_cb(i, j); - btif_hl_clean_mcl_cb(i, j); - } - } + * + * Function btif_hl_proc_abort_ind + * + * Description Process the abort indicaiton + * + * Returns Nothing + * + ******************************************************************************/ +static void btif_hl_proc_abort_ind(tBTA_HL_MCL_HANDLE mcl_handle) { + BTIF_TRACE_DEBUG("%s", __func__); + btif_hl_app_cb_t* p_acb; + uint8_t i, j; + + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + p_acb = BTIF_HL_GET_APP_CB_PTR(i); + for (j = 0; j < BTA_HL_NUM_MCLS; j++) { + if (p_acb->mcb[j].in_use) + BTIF_TRACE_DEBUG( + "btif_hl_find_mcl_idx_using_handle: app_idx=%d,mcl_idx =%d " + "mcl_handle=%d", + i, j, p_acb->mcb[j].mcl_handle); + if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) { + btif_hl_stop_cch_timer(i, j); + btif_hl_send_setup_disconnected_cb(i, j); + btif_hl_clean_mcl_cb(i, j); + } } + } } /******************************************************************************* -** -** Function btif_hl_proc_abort_cfm -** -** Description Process the abort confirmation -** -** Returns Nothing -** -*******************************************************************************/ -static void btif_hl_proc_abort_cfm(tBTA_HL_MCL_HANDLE mcl_handle){ - BTIF_TRACE_DEBUG("%s", __func__ ); - btif_hl_app_cb_t *p_acb; - uint8_t i,j; - - for (i=0; imcb[j].in_use) - BTIF_TRACE_DEBUG("btif_hl_find_mcl_idx_using_handle: app_idx=%d,mcl_idx =%d mcl_handle=%d",i,j,p_acb->mcb[j].mcl_handle); - if (p_acb->mcb[j].in_use && - (p_acb->mcb[j].mcl_handle == mcl_handle)) - { - btif_hl_stop_cch_timer(i, j); - btif_hl_send_setup_disconnected_cb(i, j); - btif_hl_clean_mcl_cb(i, j); - } - } + * + * Function btif_hl_proc_abort_cfm + * + * Description Process the abort confirmation + * + * Returns Nothing + * + ******************************************************************************/ +static void btif_hl_proc_abort_cfm(tBTA_HL_MCL_HANDLE mcl_handle) { + BTIF_TRACE_DEBUG("%s", __func__); + btif_hl_app_cb_t* p_acb; + uint8_t i, j; + + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + p_acb = BTIF_HL_GET_APP_CB_PTR(i); + for (j = 0; j < BTA_HL_NUM_MCLS; j++) { + if (p_acb->mcb[j].in_use) + BTIF_TRACE_DEBUG( + "btif_hl_find_mcl_idx_using_handle: app_idx=%d,mcl_idx =%d " + "mcl_handle=%d", + i, j, p_acb->mcb[j].mcl_handle); + if (p_acb->mcb[j].in_use && (p_acb->mcb[j].mcl_handle == mcl_handle)) { + btif_hl_stop_cch_timer(i, j); + btif_hl_send_setup_disconnected_cb(i, j); + btif_hl_clean_mcl_cb(i, j); + } } - + } } /******************************************************************************* -** -** Function btif_hl_proc_send_data_cfm -** -** Description Process the send data confirmation -** -** Returns Nothing -** -*******************************************************************************/ + * + * Function btif_hl_proc_send_data_cfm + * + * Description Process the send data confirmation + * + * Returns Nothing + * + ******************************************************************************/ static void btif_hl_proc_send_data_cfm(tBTA_HL_MDL_HANDLE mdl_handle, - tBTA_HL_STATUS status){ - uint8_t app_idx,mcl_idx, mdl_idx; - btif_hl_mdl_cb_t *p_dcb; - UNUSED(status); - - BTIF_TRACE_DEBUG("%s", __func__); - if (btif_hl_find_mdl_idx_using_handle(mdl_handle, - &app_idx, &mcl_idx, &mdl_idx )) - { - p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); - osi_free_and_reset((void **)&p_dcb->p_tx_pkt); - BTIF_TRACE_DEBUG("send success free p_tx_pkt tx_size=%d", - p_dcb->tx_size); - p_dcb->tx_size = 0; - } + tBTA_HL_STATUS status) { + uint8_t app_idx, mcl_idx, mdl_idx; + btif_hl_mdl_cb_t* p_dcb; + UNUSED(status); + + BTIF_TRACE_DEBUG("%s", __func__); + if (btif_hl_find_mdl_idx_using_handle(mdl_handle, &app_idx, &mcl_idx, + &mdl_idx)) { + p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); + osi_free_and_reset((void**)&p_dcb->p_tx_pkt); + BTIF_TRACE_DEBUG("send success free p_tx_pkt tx_size=%d", p_dcb->tx_size); + p_dcb->tx_size = 0; + } } /******************************************************************************* -** -** Function btif_hl_proc_dch_cong_ind -** -** Description Process the DCH congestion change indication -** -** Returns Nothing -** -*******************************************************************************/ -static void btif_hl_proc_dch_cong_ind(tBTA_HL *p_data) + * + * Function btif_hl_proc_dch_cong_ind + * + * Description Process the DCH congestion change indication + * + * Returns Nothing + * + ******************************************************************************/ +static void btif_hl_proc_dch_cong_ind(tBTA_HL* p_data) { - btif_hl_mdl_cb_t *p_dcb; - uint8_t app_idx, mcl_idx, mdl_idx; + btif_hl_mdl_cb_t* p_dcb; + uint8_t app_idx, mcl_idx, mdl_idx; - BTIF_TRACE_DEBUG("btif_hl_proc_dch_cong_ind"); + BTIF_TRACE_DEBUG("btif_hl_proc_dch_cong_ind"); - if (btif_hl_find_mdl_idx_using_handle(p_data->dch_cong_ind.mdl_handle, &app_idx, &mcl_idx, &mdl_idx)) - { - p_dcb =BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); - p_dcb->cong = p_data->dch_cong_ind.cong; - } + if (btif_hl_find_mdl_idx_using_handle(p_data->dch_cong_ind.mdl_handle, + &app_idx, &mcl_idx, &mdl_idx)) { + p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); + p_dcb->cong = p_data->dch_cong_ind.cong; + } } /******************************************************************************* -** -** Function btif_hl_proc_reg_request -** -** Description Process registration request -** -** Returns void -** -*******************************************************************************/ -static void btif_hl_proc_reg_request(uint8_t app_idx, uint8_t app_id, - tBTA_HL_REG_PARAM *p_reg_param, - tBTA_HL_CBACK *p_cback){ - UNUSED(p_cback); - - BTIF_TRACE_DEBUG("%s app_idx=%d app_id=%d", __func__, app_idx, app_id); - - if(reg_counter >1) - { - BTIF_TRACE_DEBUG("btif_hl_proc_reg_request: calling uPDATE"); - BTA_HlUpdate(app_id, p_reg_param,true, btif_hl_cback); - } - else - BTA_HlRegister(app_id, p_reg_param, btif_hl_cback); + * + * Function btif_hl_proc_reg_request + * + * Description Process registration request + * + * Returns void + * + ******************************************************************************/ +static void btif_hl_proc_reg_request(uint8_t app_idx, uint8_t app_id, + tBTA_HL_REG_PARAM* p_reg_param, + tBTA_HL_CBACK* p_cback) { + UNUSED(p_cback); + + BTIF_TRACE_DEBUG("%s app_idx=%d app_id=%d", __func__, app_idx, app_id); + + if (reg_counter > 1) { + BTIF_TRACE_DEBUG("btif_hl_proc_reg_request: calling uPDATE"); + BTA_HlUpdate(app_id, p_reg_param, true, btif_hl_cback); + } else + BTA_HlRegister(app_id, p_reg_param, btif_hl_cback); } /******************************************************************************* -** -** Function btif_hl_proc_cb_evt -** -** Description Process HL callback events -** -** Returns void -** -*******************************************************************************/ -static void btif_hl_proc_cb_evt(uint16_t event, char* p_param){ - - btif_hl_evt_cb_t *p_data = (btif_hl_evt_cb_t *)p_param; - bt_bdaddr_t bd_addr; - bthl_channel_state_t state=BTHL_CONN_STATE_DISCONNECTED; - bool send_chan_cb=true; - tBTA_HL_REG_PARAM reg_param; - btif_hl_app_cb_t *p_acb; - - BTIF_TRACE_DEBUG("%s event %d", __func__, event); - btif_hl_display_calling_process_name(); - - switch (event) - { - case BTIF_HL_SEND_CONNECTED_CB: - case BTIF_HL_SEND_DISCONNECTED_CB: - if (p_data->chan_cb.cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING) - state = BTHL_CONN_STATE_CONNECTED; - else if (p_data->chan_cb.cb_state == BTIF_HL_CHAN_CB_STATE_DISCONNECTED_PENDING) - state = BTHL_CONN_STATE_DISCONNECTED; - else - send_chan_cb = false; - - if (send_chan_cb) - { - btif_hl_copy_bda(&bd_addr, p_data->chan_cb.bd_addr); - BTIF_TRACE_DEBUG("state callbk: ch_id=0x%08x cb_state=%d state=%d fd=%d", - p_data->chan_cb.channel_id, - p_data->chan_cb.cb_state, - state, p_data->chan_cb.fd); - btif_hl_display_bt_bda(&bd_addr); - BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, p_data->chan_cb.app_id, - &bd_addr, p_data->chan_cb.mdep_cfg_index, - p_data->chan_cb.channel_id, state, p_data->chan_cb.fd ); - } - - break; - case BTIF_HL_REG_APP: - p_acb = BTIF_HL_GET_APP_CB_PTR(p_data->reg.app_idx); - BTIF_TRACE_DEBUG("Rcv BTIF_HL_REG_APP app_idx=%d reg_pending=%d", p_data->reg.app_idx, p_acb->reg_pending); - if (btif_hl_get_state() == BTIF_HL_STATE_ENABLED && p_acb->reg_pending) - { - BTIF_TRACE_DEBUG("Rcv BTIF_HL_REG_APP reg_counter=%d",reg_counter); - p_acb->reg_pending = false; - reg_param.dev_type = p_acb->dev_type; - reg_param.sec_mask = BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT; - reg_param.p_srv_name = p_acb->srv_name; - reg_param.p_srv_desp = p_acb->srv_desp; - reg_param.p_provider_name = p_acb->provider_name; - btif_hl_proc_reg_request (p_data->reg.app_idx, p_acb->app_id, ®_param, btif_hl_cback); - } - else - { - BTIF_TRACE_DEBUG("reg request is processed state=%d reg_pending=%d", btif_hl_get_state(), p_acb->reg_pending); - } - - break; - - case BTIF_HL_UNREG_APP: - BTIF_TRACE_DEBUG("Rcv BTIF_HL_UNREG_APP app_idx=%d", p_data->unreg.app_idx ); - p_acb = BTIF_HL_GET_APP_CB_PTR(p_data->unreg.app_idx); - if (btif_hl_get_state() == BTIF_HL_STATE_ENABLED) - { - if(reg_counter >= 1) - BTA_HlUpdate(p_acb->app_id,NULL,false,NULL); - else - BTA_HlDeregister(p_acb->app_id, p_acb->app_handle); - } - break; - case BTIF_HL_UPDATE_MDL: - BTIF_TRACE_DEBUG("Rcv BTIF_HL_UPDATE_MDL app_idx=%d", p_data->update_mdl.app_idx ); - p_acb = BTIF_HL_GET_APP_CB_PTR(p_data->update_mdl.app_idx); - break; - - default: - BTIF_TRACE_ERROR("Unknown event %d", event); - break; - } + * + * Function btif_hl_proc_cb_evt + * + * Description Process HL callback events + * + * Returns void + * + ******************************************************************************/ +static void btif_hl_proc_cb_evt(uint16_t event, char* p_param) { + btif_hl_evt_cb_t* p_data = (btif_hl_evt_cb_t*)p_param; + bt_bdaddr_t bd_addr; + bthl_channel_state_t state = BTHL_CONN_STATE_DISCONNECTED; + bool send_chan_cb = true; + tBTA_HL_REG_PARAM reg_param; + btif_hl_app_cb_t* p_acb; + + BTIF_TRACE_DEBUG("%s event %d", __func__, event); + btif_hl_display_calling_process_name(); + + switch (event) { + case BTIF_HL_SEND_CONNECTED_CB: + case BTIF_HL_SEND_DISCONNECTED_CB: + if (p_data->chan_cb.cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING) + state = BTHL_CONN_STATE_CONNECTED; + else if (p_data->chan_cb.cb_state == + BTIF_HL_CHAN_CB_STATE_DISCONNECTED_PENDING) + state = BTHL_CONN_STATE_DISCONNECTED; + else + send_chan_cb = false; + + if (send_chan_cb) { + btif_hl_copy_bda(&bd_addr, p_data->chan_cb.bd_addr); + BTIF_TRACE_DEBUG( + "state callbk: ch_id=0x%08x cb_state=%d state=%d fd=%d", + p_data->chan_cb.channel_id, p_data->chan_cb.cb_state, state, + p_data->chan_cb.fd); + btif_hl_display_bt_bda(&bd_addr); + BTIF_HL_CALL_CBACK( + bt_hl_callbacks, channel_state_cb, p_data->chan_cb.app_id, &bd_addr, + p_data->chan_cb.mdep_cfg_index, p_data->chan_cb.channel_id, state, + p_data->chan_cb.fd); + } + + break; + case BTIF_HL_REG_APP: + p_acb = BTIF_HL_GET_APP_CB_PTR(p_data->reg.app_idx); + BTIF_TRACE_DEBUG("Rcv BTIF_HL_REG_APP app_idx=%d reg_pending=%d", + p_data->reg.app_idx, p_acb->reg_pending); + if (btif_hl_get_state() == BTIF_HL_STATE_ENABLED && p_acb->reg_pending) { + BTIF_TRACE_DEBUG("Rcv BTIF_HL_REG_APP reg_counter=%d", reg_counter); + p_acb->reg_pending = false; + reg_param.dev_type = p_acb->dev_type; + reg_param.sec_mask = BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT; + reg_param.p_srv_name = p_acb->srv_name; + reg_param.p_srv_desp = p_acb->srv_desp; + reg_param.p_provider_name = p_acb->provider_name; + btif_hl_proc_reg_request(p_data->reg.app_idx, p_acb->app_id, ®_param, + btif_hl_cback); + } else { + BTIF_TRACE_DEBUG("reg request is processed state=%d reg_pending=%d", + btif_hl_get_state(), p_acb->reg_pending); + } + + break; + + case BTIF_HL_UNREG_APP: + BTIF_TRACE_DEBUG("Rcv BTIF_HL_UNREG_APP app_idx=%d", + p_data->unreg.app_idx); + p_acb = BTIF_HL_GET_APP_CB_PTR(p_data->unreg.app_idx); + if (btif_hl_get_state() == BTIF_HL_STATE_ENABLED) { + if (reg_counter >= 1) + BTA_HlUpdate(p_acb->app_id, NULL, false, NULL); + else + BTA_HlDeregister(p_acb->app_id, p_acb->app_handle); + } + break; + case BTIF_HL_UPDATE_MDL: + BTIF_TRACE_DEBUG("Rcv BTIF_HL_UPDATE_MDL app_idx=%d", + p_data->update_mdl.app_idx); + p_acb = BTIF_HL_GET_APP_CB_PTR(p_data->update_mdl.app_idx); + break; + + default: + BTIF_TRACE_ERROR("Unknown event %d", event); + break; + } } /******************************************************************************* -** -** Function btif_hl_upstreams_evt -** -** Description Process HL events -** -** Returns void -** -*******************************************************************************/ -static void btif_hl_upstreams_evt(uint16_t event, char* p_param){ - tBTA_HL *p_data = (tBTA_HL *)p_param; - uint8_t app_idx, mcl_idx; - btif_hl_app_cb_t *p_acb; - btif_hl_mcl_cb_t *p_mcb = NULL; - btif_hl_pend_dch_op_t pending_op; - bool status; - - BTIF_TRACE_DEBUG("%s event %d", __func__, event); - btif_hl_display_calling_process_name(); - switch (event) - { - case BTA_HL_REGISTER_CFM_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_REGISTER_CFM_EVT"); - BTIF_TRACE_DEBUG("app_id=%d app_handle=%d status=%d ", - p_data->reg_cfm.app_id, - p_data->reg_cfm.app_handle, - p_data->reg_cfm.status ); - - btif_hl_proc_reg_cfm(p_data); - break; - case BTA_HL_SDP_INFO_IND_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_SDP_INFO_IND_EVT"); - BTIF_TRACE_DEBUG("app_handle=%d ctrl_psm=0x%04x data_psm=0x%04x x_spec=%d mcap_sup_procs=0x%02x", - p_data->sdp_info_ind.app_handle, - p_data->sdp_info_ind.ctrl_psm, - p_data->sdp_info_ind.data_psm, - p_data->sdp_info_ind.data_x_spec, - p_data->sdp_info_ind.mcap_sup_procs); - //btif_hl_proc_sdp_info_ind(p_data); - break; - - case BTA_HL_DEREGISTER_CFM_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_DEREGISTER_CFM_EVT"); - BTIF_TRACE_DEBUG("app_handle=%d status=%d ", - p_data->dereg_cfm.app_handle, - p_data->dereg_cfm.status ); - btif_hl_proc_dereg_cfm(p_data); - break; - - case BTA_HL_SDP_QUERY_CFM_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_SDP_QUERY_CFM_EVT"); - BTIF_TRACE_DEBUG("app_handle=%d app_id =%d,status =%d", - p_data->sdp_query_cfm.app_handle,p_data->sdp_query_cfm.app_id, - p_data->sdp_query_cfm.status); - - BTIF_TRACE_DEBUG("DB [%02x] [%02x] [%02x] [%02x] [%02x] [%02x]", - p_data->sdp_query_cfm.bd_addr[0], p_data->sdp_query_cfm.bd_addr[1], - p_data->sdp_query_cfm.bd_addr[2], p_data->sdp_query_cfm.bd_addr[3], - p_data->sdp_query_cfm.bd_addr[4], p_data->sdp_query_cfm.bd_addr[5]); - - if (p_data->sdp_query_cfm.status == BTA_HL_STATUS_OK) - status = btif_hl_proc_sdp_query_cfm(p_data); - else - status = false; - - if (!status) - { - BTIF_TRACE_DEBUG("BTA_HL_SDP_QUERY_CFM_EVT Status = %d", - p_data->sdp_query_cfm.status); - if (btif_hl_find_app_idx_using_app_id(p_data->sdp_query_cfm.app_id, &app_idx)) - { - p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - if (btif_hl_find_mcl_idx(app_idx, p_data->sdp_query_cfm.bd_addr, &mcl_idx)) - { - p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - if ( (p_mcb->cch_oper == BTIF_HL_CCH_OP_MDEP_FILTERING) || - (p_mcb->cch_oper == BTIF_HL_CCH_OP_DCH_OPEN) ) - { - pending_op = p_mcb->pcb.op; - switch (pending_op) - { - case BTIF_HL_PEND_DCH_OP_OPEN: - btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx); - break; - case BTIF_HL_PEND_DCH_OP_RECONNECT: - case BTIF_HL_PEND_DCH_OP_DELETE_MDL: - default: - break; - } - if (!p_mcb->is_connected) - btif_hl_clean_mcl_cb(app_idx, mcl_idx); - } - } - } - } - - break; - - case BTA_HL_CCH_OPEN_CFM_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_CCH_OPEN_CFM_EVT"); - BTIF_TRACE_DEBUG("app_id=%d,app_handle=%d mcl_handle=%d status =%d", - p_data->cch_open_cfm.app_id, - p_data->cch_open_cfm.app_handle, - p_data->cch_open_cfm.mcl_handle, - p_data->cch_open_cfm.status); - BTIF_TRACE_DEBUG("DB [%02x] [%02x] [%02x] [%02x] [%02x] [%02x]", - p_data->cch_open_cfm.bd_addr[0], p_data->cch_open_cfm.bd_addr[1], - p_data->cch_open_cfm.bd_addr[2], p_data->cch_open_cfm.bd_addr[3], - p_data->cch_open_cfm.bd_addr[4], p_data->cch_open_cfm.bd_addr[5]); - - if (p_data->cch_open_cfm.status == BTA_HL_STATUS_OK || - p_data->cch_open_cfm.status == BTA_HL_STATUS_DUPLICATE_CCH_OPEN) - { - status = btif_hl_proc_cch_open_cfm(p_data); - } - else - { - status = false; - } - - if (!status) - { - if (btif_hl_find_app_idx_using_app_id(p_data->cch_open_cfm.app_id, &app_idx)) - { - p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - if (btif_hl_find_mcl_idx(app_idx, p_data->cch_open_cfm.bd_addr, &mcl_idx)) - { - p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - pending_op = p_mcb->pcb.op; - switch (pending_op) - { - case BTIF_HL_PEND_DCH_OP_OPEN: - btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx); - break; - case BTIF_HL_PEND_DCH_OP_RECONNECT: - case BTIF_HL_PEND_DCH_OP_DELETE_MDL: - default: - break; - } - btif_hl_clean_mcl_cb(app_idx, mcl_idx); - } - } - } - break; - - case BTA_HL_DCH_OPEN_CFM_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_OPEN_CFM_EVT"); - BTIF_TRACE_DEBUG("mcl_handle=%d mdl_handle=0x%x status=%d ", - p_data->dch_open_cfm.mcl_handle, - p_data->dch_open_cfm.mdl_handle, - p_data->dch_open_cfm.status); - BTIF_TRACE_DEBUG("first_reliable =%d dch_mode=%d local_mdep_id=%d mdl_id=%d mtu=%d", - p_data->dch_open_cfm.first_reliable, - p_data->dch_open_cfm.dch_mode, - p_data->dch_open_cfm.local_mdep_id, - p_data->dch_open_cfm.mdl_id, - p_data->dch_open_cfm.mtu); - if (p_data->dch_open_cfm.status == BTA_HL_STATUS_OK) - { - status = btif_hl_proc_dch_open_cfm(p_data); - } - else - { - status = false; - } - - if (!status) - { - if (btif_hl_find_mcl_idx_using_handle(p_data->dch_open_cfm.mcl_handle,&app_idx, &mcl_idx)) - { - p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - pending_op = p_mcb->pcb.op; - switch (pending_op) - { - case BTIF_HL_PEND_DCH_OP_OPEN: - btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx); - break; - case BTIF_HL_PEND_DCH_OP_RECONNECT: - case BTIF_HL_PEND_DCH_OP_DELETE_MDL: - default: - break; - } - } - } - break; - - case BTA_HL_CCH_OPEN_IND_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_CCH_OPEN_IND_EVT"); - BTIF_TRACE_DEBUG("app_handle=%d mcl_handle=%d", - p_data->cch_open_ind.app_handle, - p_data->cch_open_ind.mcl_handle); - BTIF_TRACE_DEBUG("DB [%02x] [%02x] [%02x] [%02x] [%02x] [%02x]", - p_data->cch_open_ind.bd_addr[0], p_data->cch_open_ind.bd_addr[1], - p_data->cch_open_ind.bd_addr[2], p_data->cch_open_ind.bd_addr[3], - p_data->cch_open_ind.bd_addr[4], p_data->cch_open_ind.bd_addr[5]); - - btif_hl_proc_cch_open_ind(p_data); - break; - - case BTA_HL_DCH_CREATE_IND_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_CREATE_IND_EVT"); - BTIF_TRACE_DEBUG("mcl_handle=%d", - p_data->dch_create_ind.mcl_handle ); - BTIF_TRACE_DEBUG("local_mdep_id =%d mdl_id=%d cfg=%d", - p_data->dch_create_ind.local_mdep_id, - p_data->dch_create_ind.mdl_id, - p_data->dch_create_ind.cfg); - btif_hl_proc_create_ind(p_data); - break; - - case BTA_HL_DCH_OPEN_IND_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_OPEN_IND_EVT"); - BTIF_TRACE_DEBUG("mcl_handle=%d mdl_handle=0x%x", - p_data->dch_open_ind.mcl_handle, - p_data->dch_open_ind.mdl_handle ); - BTIF_TRACE_DEBUG("first_reliable =%d dch_mode=%d local_mdep_id=%d mdl_id=%d mtu=%d", - p_data->dch_open_ind.first_reliable, - p_data->dch_open_ind.dch_mode, - p_data->dch_open_ind.local_mdep_id, - p_data->dch_open_ind.mdl_id, - p_data->dch_open_ind.mtu); - - btif_hl_proc_dch_open_ind(p_data); - break; - - case BTA_HL_DELETE_MDL_IND_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_DELETE_MDL_IND_EVT"); - BTIF_TRACE_DEBUG("mcl_handle=%d mdl_id=0x%x", - p_data->delete_mdl_ind.mcl_handle, - p_data->delete_mdl_ind.mdl_id); - break; - - case BTA_HL_DELETE_MDL_CFM_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_DELETE_MDL_CFM_EVT"); - BTIF_TRACE_DEBUG("mcl_handle=%d mdl_id=0x%x status=%d", - p_data->delete_mdl_cfm.mcl_handle, - p_data->delete_mdl_cfm.mdl_id, - p_data->delete_mdl_cfm.status); - - if (btif_hl_find_app_idx_using_deleted_mdl_id( p_data->delete_mdl_cfm.mdl_id, - &app_idx)) - { - p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - btif_hl_send_destroyed_cb(p_acb); - btif_hl_clean_delete_mdl(&p_acb->delete_mdl); - } - break; - - case BTA_HL_DCH_RECONNECT_CFM_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_RECONNECT_CFM_EVT"); - BTIF_TRACE_DEBUG("mcl_handle=%d mdl_handle=%d status=%d ", - p_data->dch_reconnect_cfm.mcl_handle, - p_data->dch_reconnect_cfm.mdl_handle, - p_data->dch_reconnect_cfm.status); - BTIF_TRACE_DEBUG("first_reliable =%d dch_mode=%d mdl_id=%d mtu=%d", - p_data->dch_reconnect_cfm.first_reliable, - p_data->dch_reconnect_cfm.dch_mode, - p_data->dch_reconnect_cfm.mdl_id, - p_data->dch_reconnect_cfm.mtu); - - if (p_data->dch_reconnect_cfm.status == BTA_HL_STATUS_OK) - { - status = btif_hl_proc_dch_reconnect_cfm(p_data); - } - else - { - status = false; - } - - if (!status) - { - if (btif_hl_find_mcl_idx_using_handle(p_data->dch_open_cfm.mcl_handle,&app_idx, &mcl_idx)) - { - p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - pending_op = p_mcb->pcb.op; - switch (pending_op) - { - case BTIF_HL_PEND_DCH_OP_OPEN: - btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx); - break; - case BTIF_HL_PEND_DCH_OP_RECONNECT: - case BTIF_HL_PEND_DCH_OP_DELETE_MDL: - default: - break; - } - } - } - - break; - - case BTA_HL_CCH_CLOSE_CFM_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_CCH_CLOSE_CFM_EVT"); - BTIF_TRACE_DEBUG("mcl_handle=%d status =%d", - p_data->cch_close_cfm.mcl_handle, - p_data->cch_close_cfm.status); - if (p_data->cch_close_cfm.status == BTA_HL_STATUS_OK) - { - btif_hl_proc_cch_close_cfm(p_data); - } - break; - - case BTA_HL_CCH_CLOSE_IND_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_CCH_CLOSE_IND_EVT"); - BTIF_TRACE_DEBUG("mcl_handle =%d intentional_close=%s", - p_data->cch_close_ind.mcl_handle, - (p_data->cch_close_ind.intentional?"Yes":"No")); - - btif_hl_proc_cch_close_ind(p_data); - break; - - case BTA_HL_DCH_CLOSE_IND_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_CLOSE_IND_EVT"); - BTIF_TRACE_DEBUG("mdl_handle=%d intentional_close=%s", - p_data->dch_close_ind.mdl_handle, - (p_data->dch_close_ind.intentional?"Yes":"No") ); - - btif_hl_proc_dch_close_ind(p_data); - break; - - case BTA_HL_DCH_CLOSE_CFM_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_CLOSE_CFM_EVT"); - BTIF_TRACE_DEBUG("mdl_handle=%d status=%d ", - p_data->dch_close_cfm.mdl_handle, - p_data->dch_close_cfm.status); + * + * Function btif_hl_upstreams_evt + * + * Description Process HL events + * + * Returns void + * + ******************************************************************************/ +static void btif_hl_upstreams_evt(uint16_t event, char* p_param) { + tBTA_HL* p_data = (tBTA_HL*)p_param; + uint8_t app_idx, mcl_idx; + btif_hl_app_cb_t* p_acb; + btif_hl_mcl_cb_t* p_mcb = NULL; + btif_hl_pend_dch_op_t pending_op; + bool status; + + BTIF_TRACE_DEBUG("%s event %d", __func__, event); + btif_hl_display_calling_process_name(); + switch (event) { + case BTA_HL_REGISTER_CFM_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_REGISTER_CFM_EVT"); + BTIF_TRACE_DEBUG("app_id=%d app_handle=%d status=%d ", + p_data->reg_cfm.app_id, p_data->reg_cfm.app_handle, + p_data->reg_cfm.status); + + btif_hl_proc_reg_cfm(p_data); + break; + case BTA_HL_SDP_INFO_IND_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_SDP_INFO_IND_EVT"); + BTIF_TRACE_DEBUG( + "app_handle=%d ctrl_psm=0x%04x data_psm=0x%04x x_spec=%d " + "mcap_sup_procs=0x%02x", + p_data->sdp_info_ind.app_handle, p_data->sdp_info_ind.ctrl_psm, + p_data->sdp_info_ind.data_psm, p_data->sdp_info_ind.data_x_spec, + p_data->sdp_info_ind.mcap_sup_procs); + // btif_hl_proc_sdp_info_ind(p_data); + break; + + case BTA_HL_DEREGISTER_CFM_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_DEREGISTER_CFM_EVT"); + BTIF_TRACE_DEBUG("app_handle=%d status=%d ", p_data->dereg_cfm.app_handle, + p_data->dereg_cfm.status); + btif_hl_proc_dereg_cfm(p_data); + break; + + case BTA_HL_SDP_QUERY_CFM_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_SDP_QUERY_CFM_EVT"); + BTIF_TRACE_DEBUG("app_handle=%d app_id =%d,status =%d", + p_data->sdp_query_cfm.app_handle, + p_data->sdp_query_cfm.app_id, + p_data->sdp_query_cfm.status); + + BTIF_TRACE_DEBUG( + "DB [%02x] [%02x] [%02x] [%02x] [%02x] [%02x]", + p_data->sdp_query_cfm.bd_addr[0], p_data->sdp_query_cfm.bd_addr[1], + p_data->sdp_query_cfm.bd_addr[2], p_data->sdp_query_cfm.bd_addr[3], + p_data->sdp_query_cfm.bd_addr[4], p_data->sdp_query_cfm.bd_addr[5]); + + if (p_data->sdp_query_cfm.status == BTA_HL_STATUS_OK) + status = btif_hl_proc_sdp_query_cfm(p_data); + else + status = false; - if (p_data->dch_close_cfm.status == BTA_HL_STATUS_OK) - { - btif_hl_proc_dch_close_cfm(p_data); + if (!status) { + BTIF_TRACE_DEBUG("BTA_HL_SDP_QUERY_CFM_EVT Status = %d", + p_data->sdp_query_cfm.status); + if (btif_hl_find_app_idx_using_app_id(p_data->sdp_query_cfm.app_id, + &app_idx)) { + p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + if (btif_hl_find_mcl_idx(app_idx, p_data->sdp_query_cfm.bd_addr, + &mcl_idx)) { + p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + if ((p_mcb->cch_oper == BTIF_HL_CCH_OP_MDEP_FILTERING) || + (p_mcb->cch_oper == BTIF_HL_CCH_OP_DCH_OPEN)) { + pending_op = p_mcb->pcb.op; + switch (pending_op) { + case BTIF_HL_PEND_DCH_OP_OPEN: + btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx); + break; + case BTIF_HL_PEND_DCH_OP_RECONNECT: + case BTIF_HL_PEND_DCH_OP_DELETE_MDL: + default: + break; + } + if (!p_mcb->is_connected) btif_hl_clean_mcl_cb(app_idx, mcl_idx); } - break; - - case BTA_HL_DCH_ECHO_TEST_CFM_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_ECHO_TEST_CFM_EVT"); - BTIF_TRACE_DEBUG("mcl_handle=%d status=%d", - p_data->echo_test_cfm.mcl_handle, - p_data->echo_test_cfm.status ); - /* not supported */ - break; - - case BTA_HL_DCH_RECONNECT_IND_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_RECONNECT_IND_EVT"); - - BTIF_TRACE_DEBUG("mcl_handle=%d mdl_handle=5d", - p_data->dch_reconnect_ind.mcl_handle, - p_data->dch_reconnect_ind.mdl_handle ); - BTIF_TRACE_DEBUG("first_reliable =%d dch_mode=%d mdl_id=%d mtu=%d", - p_data->dch_reconnect_ind.first_reliable, - p_data->dch_reconnect_ind.dch_mode, - p_data->dch_reconnect_ind.mdl_id, - p_data->dch_reconnect_ind.mtu); - - btif_hl_proc_dch_reconnect_ind(p_data); - break; - - case BTA_HL_CONG_CHG_IND_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_CONG_CHG_IND_EVT"); - BTIF_TRACE_DEBUG("mdl_handle=%d cong =%d", - p_data->dch_cong_ind.mdl_handle, - p_data->dch_cong_ind.cong); - btif_hl_proc_dch_cong_ind(p_data); - break; - - case BTA_HL_DCH_ABORT_IND_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_ABORT_IND_EVT"); - BTIF_TRACE_DEBUG("mcl_handle=%d", - p_data->dch_abort_ind.mcl_handle ); - btif_hl_proc_abort_ind(p_data->dch_abort_ind.mcl_handle); - break; - case BTA_HL_DCH_ABORT_CFM_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_ABORT_CFM_EVT"); - BTIF_TRACE_DEBUG("mcl_handle=%d status =%d", - p_data->dch_abort_cfm.mcl_handle, - p_data->dch_abort_cfm.status); - if (p_data->dch_abort_cfm.status == BTA_HL_STATUS_OK) - { - btif_hl_proc_abort_cfm(p_data->dch_abort_ind.mcl_handle); + } + } + } + + break; + + case BTA_HL_CCH_OPEN_CFM_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_CCH_OPEN_CFM_EVT"); + BTIF_TRACE_DEBUG( + "app_id=%d,app_handle=%d mcl_handle=%d status =%d", + p_data->cch_open_cfm.app_id, p_data->cch_open_cfm.app_handle, + p_data->cch_open_cfm.mcl_handle, p_data->cch_open_cfm.status); + BTIF_TRACE_DEBUG( + "DB [%02x] [%02x] [%02x] [%02x] [%02x] [%02x]", + p_data->cch_open_cfm.bd_addr[0], p_data->cch_open_cfm.bd_addr[1], + p_data->cch_open_cfm.bd_addr[2], p_data->cch_open_cfm.bd_addr[3], + p_data->cch_open_cfm.bd_addr[4], p_data->cch_open_cfm.bd_addr[5]); + + if (p_data->cch_open_cfm.status == BTA_HL_STATUS_OK || + p_data->cch_open_cfm.status == BTA_HL_STATUS_DUPLICATE_CCH_OPEN) { + status = btif_hl_proc_cch_open_cfm(p_data); + } else { + status = false; + } + + if (!status) { + if (btif_hl_find_app_idx_using_app_id(p_data->cch_open_cfm.app_id, + &app_idx)) { + p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + if (btif_hl_find_mcl_idx(app_idx, p_data->cch_open_cfm.bd_addr, + &mcl_idx)) { + p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + pending_op = p_mcb->pcb.op; + switch (pending_op) { + case BTIF_HL_PEND_DCH_OP_OPEN: + btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx); + break; + case BTIF_HL_PEND_DCH_OP_RECONNECT: + case BTIF_HL_PEND_DCH_OP_DELETE_MDL: + default: + break; } - break; - - case BTA_HL_DCH_SEND_DATA_CFM_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_SEND_DATA_CFM_EVT"); - BTIF_TRACE_DEBUG("mdl_handle=0x%x status =%d", - p_data->dch_send_data_cfm.mdl_handle, - p_data->dch_send_data_cfm.status); - btif_hl_proc_send_data_cfm(p_data->dch_send_data_cfm.mdl_handle, - p_data->dch_send_data_cfm.status); - break; - - case BTA_HL_DCH_RCV_DATA_IND_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_RCV_DATA_IND_EVT"); - BTIF_TRACE_DEBUG("mdl_handle=0x%x ", - p_data->dch_rcv_data_ind.mdl_handle); - /* do nothing here */ - break; - - default: - BTIF_TRACE_DEBUG("Unknown Event (0x%02x)...", event); - break; - } -} - -/******************************************************************************* -** -** Function btif_hl_cback -** -** Description Callback function for HL events -** -** Returns void -** -*******************************************************************************/ -static void btif_hl_cback(tBTA_HL_EVT event, tBTA_HL *p_data){ - bt_status_t status; - int param_len = 0; - BTIF_TRACE_DEBUG("%s event %d", __func__, event); - btif_hl_display_calling_process_name(); - switch (event) - { - case BTA_HL_REGISTER_CFM_EVT: - param_len = sizeof(tBTA_HL_REGISTER_CFM); - break; - case BTA_HL_SDP_INFO_IND_EVT: - param_len = sizeof(tBTA_HL_SDP_INFO_IND); - break; - case BTA_HL_DEREGISTER_CFM_EVT: - param_len = sizeof(tBTA_HL_DEREGISTER_CFM); - break; - case BTA_HL_SDP_QUERY_CFM_EVT: - param_len = sizeof(tBTA_HL_SDP_QUERY_CFM); - break; - case BTA_HL_CCH_OPEN_CFM_EVT: - param_len = sizeof(tBTA_HL_CCH_OPEN_CFM); - break; - case BTA_HL_DCH_OPEN_CFM_EVT: - param_len = sizeof(tBTA_HL_DCH_OPEN_CFM); - break; - case BTA_HL_CCH_OPEN_IND_EVT: - param_len = sizeof(tBTA_HL_CCH_OPEN_IND); - break; - case BTA_HL_DCH_CREATE_IND_EVT: - param_len = sizeof(tBTA_HL_DCH_CREATE_IND); - break; - case BTA_HL_DCH_OPEN_IND_EVT: - param_len = sizeof(tBTA_HL_DCH_OPEN_IND); - break; - case BTA_HL_DELETE_MDL_IND_EVT: - param_len = sizeof(tBTA_HL_MDL_IND); - break; - case BTA_HL_DELETE_MDL_CFM_EVT: - param_len = sizeof(tBTA_HL_MDL_CFM); - break; - case BTA_HL_DCH_RECONNECT_CFM_EVT: - param_len = sizeof(tBTA_HL_DCH_OPEN_CFM); - break; - case BTA_HL_CCH_CLOSE_CFM_EVT: - param_len = sizeof(tBTA_HL_MCL_CFM); - break; - case BTA_HL_CCH_CLOSE_IND_EVT: - param_len = sizeof(tBTA_HL_CCH_CLOSE_IND); - break; - case BTA_HL_DCH_CLOSE_IND_EVT: - param_len = sizeof(tBTA_HL_DCH_CLOSE_IND); - break; - case BTA_HL_DCH_CLOSE_CFM_EVT: - param_len = sizeof(tBTA_HL_MDL_CFM); - break; - case BTA_HL_DCH_ECHO_TEST_CFM_EVT: - param_len = sizeof(tBTA_HL_MCL_CFM); - break; - case BTA_HL_DCH_RECONNECT_IND_EVT: - param_len = sizeof(tBTA_HL_DCH_OPEN_IND); - break; - case BTA_HL_CONG_CHG_IND_EVT: - param_len = sizeof(tBTA_HL_DCH_CONG_IND); - break; - case BTA_HL_DCH_ABORT_IND_EVT: - param_len = sizeof(tBTA_HL_MCL_IND); - break; - case BTA_HL_DCH_ABORT_CFM_EVT: - param_len = sizeof(tBTA_HL_MCL_CFM); - break; - case BTA_HL_DCH_SEND_DATA_CFM_EVT: - param_len = sizeof(tBTA_HL_MDL_CFM); - break; - case BTA_HL_DCH_RCV_DATA_IND_EVT: - param_len = sizeof(tBTA_HL_MDL_IND); - break; - default: - param_len = sizeof(tBTA_HL_MDL_IND); - break; - } - status = btif_transfer_context(btif_hl_upstreams_evt, (uint16_t)event, (char*)p_data, param_len, NULL); - - /* catch any failed context transfers */ - ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); + btif_hl_clean_mcl_cb(app_idx, mcl_idx); + } + } + } + break; + + case BTA_HL_DCH_OPEN_CFM_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_OPEN_CFM_EVT"); + BTIF_TRACE_DEBUG("mcl_handle=%d mdl_handle=0x%x status=%d ", + p_data->dch_open_cfm.mcl_handle, + p_data->dch_open_cfm.mdl_handle, + p_data->dch_open_cfm.status); + BTIF_TRACE_DEBUG( + "first_reliable =%d dch_mode=%d local_mdep_id=%d mdl_id=%d mtu=%d", + p_data->dch_open_cfm.first_reliable, p_data->dch_open_cfm.dch_mode, + p_data->dch_open_cfm.local_mdep_id, p_data->dch_open_cfm.mdl_id, + p_data->dch_open_cfm.mtu); + if (p_data->dch_open_cfm.status == BTA_HL_STATUS_OK) { + status = btif_hl_proc_dch_open_cfm(p_data); + } else { + status = false; + } + + if (!status) { + if (btif_hl_find_mcl_idx_using_handle(p_data->dch_open_cfm.mcl_handle, + &app_idx, &mcl_idx)) { + p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + pending_op = p_mcb->pcb.op; + switch (pending_op) { + case BTIF_HL_PEND_DCH_OP_OPEN: + btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx); + break; + case BTIF_HL_PEND_DCH_OP_RECONNECT: + case BTIF_HL_PEND_DCH_OP_DELETE_MDL: + default: + break; + } + } + } + break; + + case BTA_HL_CCH_OPEN_IND_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_CCH_OPEN_IND_EVT"); + BTIF_TRACE_DEBUG("app_handle=%d mcl_handle=%d", + p_data->cch_open_ind.app_handle, + p_data->cch_open_ind.mcl_handle); + BTIF_TRACE_DEBUG( + "DB [%02x] [%02x] [%02x] [%02x] [%02x] [%02x]", + p_data->cch_open_ind.bd_addr[0], p_data->cch_open_ind.bd_addr[1], + p_data->cch_open_ind.bd_addr[2], p_data->cch_open_ind.bd_addr[3], + p_data->cch_open_ind.bd_addr[4], p_data->cch_open_ind.bd_addr[5]); + + btif_hl_proc_cch_open_ind(p_data); + break; + + case BTA_HL_DCH_CREATE_IND_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_CREATE_IND_EVT"); + BTIF_TRACE_DEBUG("mcl_handle=%d", p_data->dch_create_ind.mcl_handle); + BTIF_TRACE_DEBUG("local_mdep_id =%d mdl_id=%d cfg=%d", + p_data->dch_create_ind.local_mdep_id, + p_data->dch_create_ind.mdl_id, + p_data->dch_create_ind.cfg); + btif_hl_proc_create_ind(p_data); + break; + + case BTA_HL_DCH_OPEN_IND_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_OPEN_IND_EVT"); + BTIF_TRACE_DEBUG("mcl_handle=%d mdl_handle=0x%x", + p_data->dch_open_ind.mcl_handle, + p_data->dch_open_ind.mdl_handle); + BTIF_TRACE_DEBUG( + "first_reliable =%d dch_mode=%d local_mdep_id=%d mdl_id=%d mtu=%d", + p_data->dch_open_ind.first_reliable, p_data->dch_open_ind.dch_mode, + p_data->dch_open_ind.local_mdep_id, p_data->dch_open_ind.mdl_id, + p_data->dch_open_ind.mtu); + + btif_hl_proc_dch_open_ind(p_data); + break; + + case BTA_HL_DELETE_MDL_IND_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_DELETE_MDL_IND_EVT"); + BTIF_TRACE_DEBUG("mcl_handle=%d mdl_id=0x%x", + p_data->delete_mdl_ind.mcl_handle, + p_data->delete_mdl_ind.mdl_id); + break; + + case BTA_HL_DELETE_MDL_CFM_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_DELETE_MDL_CFM_EVT"); + BTIF_TRACE_DEBUG("mcl_handle=%d mdl_id=0x%x status=%d", + p_data->delete_mdl_cfm.mcl_handle, + p_data->delete_mdl_cfm.mdl_id, + p_data->delete_mdl_cfm.status); + + if (btif_hl_find_app_idx_using_deleted_mdl_id( + p_data->delete_mdl_cfm.mdl_id, &app_idx)) { + p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + btif_hl_send_destroyed_cb(p_acb); + btif_hl_clean_delete_mdl(&p_acb->delete_mdl); + } + break; + + case BTA_HL_DCH_RECONNECT_CFM_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_RECONNECT_CFM_EVT"); + BTIF_TRACE_DEBUG("mcl_handle=%d mdl_handle=%d status=%d ", + p_data->dch_reconnect_cfm.mcl_handle, + p_data->dch_reconnect_cfm.mdl_handle, + p_data->dch_reconnect_cfm.status); + BTIF_TRACE_DEBUG("first_reliable =%d dch_mode=%d mdl_id=%d mtu=%d", + p_data->dch_reconnect_cfm.first_reliable, + p_data->dch_reconnect_cfm.dch_mode, + p_data->dch_reconnect_cfm.mdl_id, + p_data->dch_reconnect_cfm.mtu); + + if (p_data->dch_reconnect_cfm.status == BTA_HL_STATUS_OK) { + status = btif_hl_proc_dch_reconnect_cfm(p_data); + } else { + status = false; + } + + if (!status) { + if (btif_hl_find_mcl_idx_using_handle(p_data->dch_open_cfm.mcl_handle, + &app_idx, &mcl_idx)) { + p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + pending_op = p_mcb->pcb.op; + switch (pending_op) { + case BTIF_HL_PEND_DCH_OP_OPEN: + btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx); + break; + case BTIF_HL_PEND_DCH_OP_RECONNECT: + case BTIF_HL_PEND_DCH_OP_DELETE_MDL: + default: + break; + } + } + } + + break; + + case BTA_HL_CCH_CLOSE_CFM_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_CCH_CLOSE_CFM_EVT"); + BTIF_TRACE_DEBUG("mcl_handle=%d status =%d", + p_data->cch_close_cfm.mcl_handle, + p_data->cch_close_cfm.status); + if (p_data->cch_close_cfm.status == BTA_HL_STATUS_OK) { + btif_hl_proc_cch_close_cfm(p_data); + } + break; + + case BTA_HL_CCH_CLOSE_IND_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_CCH_CLOSE_IND_EVT"); + BTIF_TRACE_DEBUG("mcl_handle =%d intentional_close=%s", + p_data->cch_close_ind.mcl_handle, + (p_data->cch_close_ind.intentional ? "Yes" : "No")); + + btif_hl_proc_cch_close_ind(p_data); + break; + + case BTA_HL_DCH_CLOSE_IND_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_CLOSE_IND_EVT"); + BTIF_TRACE_DEBUG("mdl_handle=%d intentional_close=%s", + p_data->dch_close_ind.mdl_handle, + (p_data->dch_close_ind.intentional ? "Yes" : "No")); + + btif_hl_proc_dch_close_ind(p_data); + break; + + case BTA_HL_DCH_CLOSE_CFM_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_CLOSE_CFM_EVT"); + BTIF_TRACE_DEBUG("mdl_handle=%d status=%d ", + p_data->dch_close_cfm.mdl_handle, + p_data->dch_close_cfm.status); + + if (p_data->dch_close_cfm.status == BTA_HL_STATUS_OK) { + btif_hl_proc_dch_close_cfm(p_data); + } + break; + + case BTA_HL_DCH_ECHO_TEST_CFM_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_ECHO_TEST_CFM_EVT"); + BTIF_TRACE_DEBUG("mcl_handle=%d status=%d", + p_data->echo_test_cfm.mcl_handle, + p_data->echo_test_cfm.status); + /* not supported */ + break; + + case BTA_HL_DCH_RECONNECT_IND_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_RECONNECT_IND_EVT"); + + BTIF_TRACE_DEBUG("mcl_handle=%d mdl_handle=5d", + p_data->dch_reconnect_ind.mcl_handle, + p_data->dch_reconnect_ind.mdl_handle); + BTIF_TRACE_DEBUG("first_reliable =%d dch_mode=%d mdl_id=%d mtu=%d", + p_data->dch_reconnect_ind.first_reliable, + p_data->dch_reconnect_ind.dch_mode, + p_data->dch_reconnect_ind.mdl_id, + p_data->dch_reconnect_ind.mtu); + + btif_hl_proc_dch_reconnect_ind(p_data); + break; + + case BTA_HL_CONG_CHG_IND_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_CONG_CHG_IND_EVT"); + BTIF_TRACE_DEBUG("mdl_handle=%d cong =%d", + p_data->dch_cong_ind.mdl_handle, + p_data->dch_cong_ind.cong); + btif_hl_proc_dch_cong_ind(p_data); + break; + + case BTA_HL_DCH_ABORT_IND_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_ABORT_IND_EVT"); + BTIF_TRACE_DEBUG("mcl_handle=%d", p_data->dch_abort_ind.mcl_handle); + btif_hl_proc_abort_ind(p_data->dch_abort_ind.mcl_handle); + break; + case BTA_HL_DCH_ABORT_CFM_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_ABORT_CFM_EVT"); + BTIF_TRACE_DEBUG("mcl_handle=%d status =%d", + p_data->dch_abort_cfm.mcl_handle, + p_data->dch_abort_cfm.status); + if (p_data->dch_abort_cfm.status == BTA_HL_STATUS_OK) { + btif_hl_proc_abort_cfm(p_data->dch_abort_ind.mcl_handle); + } + break; + + case BTA_HL_DCH_SEND_DATA_CFM_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_SEND_DATA_CFM_EVT"); + BTIF_TRACE_DEBUG("mdl_handle=0x%x status =%d", + p_data->dch_send_data_cfm.mdl_handle, + p_data->dch_send_data_cfm.status); + btif_hl_proc_send_data_cfm(p_data->dch_send_data_cfm.mdl_handle, + p_data->dch_send_data_cfm.status); + break; + + case BTA_HL_DCH_RCV_DATA_IND_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_DCH_RCV_DATA_IND_EVT"); + BTIF_TRACE_DEBUG("mdl_handle=0x%x ", p_data->dch_rcv_data_ind.mdl_handle); + /* do nothing here */ + break; + + default: + BTIF_TRACE_DEBUG("Unknown Event (0x%02x)...", event); + break; + } } /******************************************************************************* -** -** Function btif_hl_upstreams_ctrl_evt -** -** Description Callback function for HL control events in the BTIF task context -** -** Returns void -** -*******************************************************************************/ -static void btif_hl_upstreams_ctrl_evt(uint16_t event, char* p_param){ - tBTA_HL_CTRL *p_data = (tBTA_HL_CTRL *) p_param; - uint8_t i; - tBTA_HL_REG_PARAM reg_param; - btif_hl_app_cb_t *p_acb; - - BTIF_TRACE_DEBUG("%s event %d", __func__, event); - btif_hl_display_calling_process_name(); - - switch ( event ) - { - case BTA_HL_CTRL_ENABLE_CFM_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_CTRL_ENABLE_CFM_EVT"); - BTIF_TRACE_DEBUG("status=%d", p_data->enable_cfm.status); - - if (p_data->enable_cfm.status == BTA_HL_STATUS_OK) - { - btif_hl_set_state(BTIF_HL_STATE_ENABLED); - - for (i=0; i < BTA_HL_NUM_APPS ; i ++) - { - p_acb = BTIF_HL_GET_APP_CB_PTR(i); - if (p_acb->in_use && p_acb->reg_pending) - { - p_acb->reg_pending = false; - reg_param.dev_type = p_acb->dev_type; - reg_param.sec_mask = BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT; - reg_param.p_srv_name = p_acb->srv_name; - reg_param.p_srv_desp = p_acb->srv_desp; - reg_param.p_provider_name = p_acb->provider_name; - - BTIF_TRACE_DEBUG("Register pending app_id=%d", p_acb->app_id); - btif_hl_proc_reg_request (i, p_acb->app_id, ®_param, btif_hl_cback); - } - } - } - - break; - case BTA_HL_CTRL_DISABLE_CFM_EVT: - BTIF_TRACE_DEBUG("Rcv BTA_HL_CTRL_DISABLE_CFM_EVT"); - BTIF_TRACE_DEBUG("status=%d", - p_data->disable_cfm.status); - - if (p_data->disable_cfm.status == BTA_HL_STATUS_OK) - { - for (size_t i = 0; i < BTA_HL_NUM_APPS; i++) { - for (size_t j = 0; j < BTA_HL_NUM_MCLS; j++) { - alarm_free(p_btif_hl_cb->acb[i].mcb[j].cch_timer); - } - } - memset(p_btif_hl_cb, 0, sizeof(btif_hl_cb_t)); - btif_hl_set_state(BTIF_HL_STATE_DISABLED); - } - - break; - default: - break; - } + * + * Function btif_hl_cback + * + * Description Callback function for HL events + * + * Returns void + * + ******************************************************************************/ +static void btif_hl_cback(tBTA_HL_EVT event, tBTA_HL* p_data) { + bt_status_t status; + int param_len = 0; + BTIF_TRACE_DEBUG("%s event %d", __func__, event); + btif_hl_display_calling_process_name(); + switch (event) { + case BTA_HL_REGISTER_CFM_EVT: + param_len = sizeof(tBTA_HL_REGISTER_CFM); + break; + case BTA_HL_SDP_INFO_IND_EVT: + param_len = sizeof(tBTA_HL_SDP_INFO_IND); + break; + case BTA_HL_DEREGISTER_CFM_EVT: + param_len = sizeof(tBTA_HL_DEREGISTER_CFM); + break; + case BTA_HL_SDP_QUERY_CFM_EVT: + param_len = sizeof(tBTA_HL_SDP_QUERY_CFM); + break; + case BTA_HL_CCH_OPEN_CFM_EVT: + param_len = sizeof(tBTA_HL_CCH_OPEN_CFM); + break; + case BTA_HL_DCH_OPEN_CFM_EVT: + param_len = sizeof(tBTA_HL_DCH_OPEN_CFM); + break; + case BTA_HL_CCH_OPEN_IND_EVT: + param_len = sizeof(tBTA_HL_CCH_OPEN_IND); + break; + case BTA_HL_DCH_CREATE_IND_EVT: + param_len = sizeof(tBTA_HL_DCH_CREATE_IND); + break; + case BTA_HL_DCH_OPEN_IND_EVT: + param_len = sizeof(tBTA_HL_DCH_OPEN_IND); + break; + case BTA_HL_DELETE_MDL_IND_EVT: + param_len = sizeof(tBTA_HL_MDL_IND); + break; + case BTA_HL_DELETE_MDL_CFM_EVT: + param_len = sizeof(tBTA_HL_MDL_CFM); + break; + case BTA_HL_DCH_RECONNECT_CFM_EVT: + param_len = sizeof(tBTA_HL_DCH_OPEN_CFM); + break; + case BTA_HL_CCH_CLOSE_CFM_EVT: + param_len = sizeof(tBTA_HL_MCL_CFM); + break; + case BTA_HL_CCH_CLOSE_IND_EVT: + param_len = sizeof(tBTA_HL_CCH_CLOSE_IND); + break; + case BTA_HL_DCH_CLOSE_IND_EVT: + param_len = sizeof(tBTA_HL_DCH_CLOSE_IND); + break; + case BTA_HL_DCH_CLOSE_CFM_EVT: + param_len = sizeof(tBTA_HL_MDL_CFM); + break; + case BTA_HL_DCH_ECHO_TEST_CFM_EVT: + param_len = sizeof(tBTA_HL_MCL_CFM); + break; + case BTA_HL_DCH_RECONNECT_IND_EVT: + param_len = sizeof(tBTA_HL_DCH_OPEN_IND); + break; + case BTA_HL_CONG_CHG_IND_EVT: + param_len = sizeof(tBTA_HL_DCH_CONG_IND); + break; + case BTA_HL_DCH_ABORT_IND_EVT: + param_len = sizeof(tBTA_HL_MCL_IND); + break; + case BTA_HL_DCH_ABORT_CFM_EVT: + param_len = sizeof(tBTA_HL_MCL_CFM); + break; + case BTA_HL_DCH_SEND_DATA_CFM_EVT: + param_len = sizeof(tBTA_HL_MDL_CFM); + break; + case BTA_HL_DCH_RCV_DATA_IND_EVT: + param_len = sizeof(tBTA_HL_MDL_IND); + break; + default: + param_len = sizeof(tBTA_HL_MDL_IND); + break; + } + status = btif_transfer_context(btif_hl_upstreams_evt, (uint16_t)event, + (char*)p_data, param_len, NULL); + + /* catch any failed context transfers */ + ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); } /******************************************************************************* -** -** Function btif_hl_ctrl_cback -** -** Description Callback function for HL control events -** -** Returns void -** -*******************************************************************************/ -static void btif_hl_ctrl_cback(tBTA_HL_CTRL_EVT event, tBTA_HL_CTRL *p_data){ - bt_status_t status; - int param_len = 0; - - BTIF_TRACE_DEBUG("%s event %d", __func__, event); - btif_hl_display_calling_process_name(); - - switch ( event ) - { - case BTA_HL_CTRL_ENABLE_CFM_EVT: - case BTA_HL_CTRL_DISABLE_CFM_EVT: - param_len = sizeof(tBTA_HL_CTRL_ENABLE_DISABLE); - break; - default: - break; - } - - status = btif_transfer_context(btif_hl_upstreams_ctrl_evt, (uint16_t)event, (char*)p_data, param_len, NULL); - ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); -} -/******************************************************************************* -** -** Function connect_channel -** -** Description connect a data channel -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t connect_channel(int app_id, bt_bdaddr_t *bd_addr, int mdep_cfg_index, int *channel_id){ - uint8_t app_idx, mcl_idx; - btif_hl_app_cb_t *p_acb = NULL; - btif_hl_pending_chan_cb_t *p_pcb = NULL; - btif_hl_mcl_cb_t *p_mcb=NULL; - bt_status_t status = BT_STATUS_SUCCESS; - tBTA_HL_DCH_OPEN_PARAM dch_open; - BD_ADDR bda; - uint8_t i; - - CHECK_BTHL_INIT(); - BTIF_TRACE_EVENT("%s", __func__); - btif_hl_display_calling_process_name(); - - for (i=0; i<6; i++) - { - bda[i] = (uint8_t) bd_addr->address[i]; - } - if (btif_hl_find_app_idx(((uint8_t)app_id), &app_idx)) - { - p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - if (btif_hl_find_mcl_idx(app_idx, bda , &mcl_idx)) - { - p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - if (p_mcb->is_connected) - { - dch_open.ctrl_psm = p_mcb->ctrl_psm; - dch_open.local_mdep_id = p_acb->sup_feature.mdep[mdep_cfg_index].mdep_id; - BTIF_TRACE_DEBUG("connect_channel: app_idx =%d, mdep_cfg_indx =%d, mdep_id =%d app_id= %d", app_idx, - mdep_cfg_index, dch_open.local_mdep_id, app_id); - if (btif_hl_find_peer_mdep_id(p_acb->app_id, p_mcb->bd_addr, - p_acb->sup_feature.mdep[mdep_cfg_index].mdep_cfg.mdep_role, - p_acb->sup_feature.mdep[mdep_cfg_index].mdep_cfg.data_cfg[0].data_type, &dch_open.peer_mdep_id )) - { - dch_open.local_cfg = p_acb->channel_type[mdep_cfg_index]; - if ((p_acb->sup_feature.mdep[mdep_cfg_index].mdep_cfg.mdep_role == BTA_HL_MDEP_ROLE_SOURCE) - && !btif_hl_is_the_first_reliable_existed(app_idx,mcl_idx)) - { - dch_open.local_cfg = BTA_HL_DCH_CFG_RELIABLE; - } - dch_open.sec_mask = (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT); - - if( !btif_hl_dch_open(p_acb->app_id, bda, &dch_open, - mdep_cfg_index, BTIF_HL_PEND_DCH_OP_OPEN, channel_id )) - { - status = BT_STATUS_FAIL; - BTIF_TRACE_EVENT("%s loc0 status = BT_STATUS_FAIL", __func__); - } - } - else - { - p_mcb->cch_oper = BTIF_HL_CCH_OP_MDEP_FILTERING; - - p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); - p_pcb->in_use = true; - p_pcb->mdep_cfg_idx = mdep_cfg_index; - memcpy(p_pcb->bd_addr, bda, sizeof(BD_ADDR)); - p_pcb->op = BTIF_HL_PEND_DCH_OP_OPEN; - BTA_HlSdpQuery(app_id,p_acb->app_handle, bda); - } - } - else - { - status = BT_STATUS_FAIL; - } + * + * Function btif_hl_upstreams_ctrl_evt + * + * Description Callback function for HL control events in the BTIF task + *context + * + * Returns void + * + ******************************************************************************/ +static void btif_hl_upstreams_ctrl_evt(uint16_t event, char* p_param) { + tBTA_HL_CTRL* p_data = (tBTA_HL_CTRL*)p_param; + uint8_t i; + tBTA_HL_REG_PARAM reg_param; + btif_hl_app_cb_t* p_acb; + + BTIF_TRACE_DEBUG("%s event %d", __func__, event); + btif_hl_display_calling_process_name(); + + switch (event) { + case BTA_HL_CTRL_ENABLE_CFM_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_CTRL_ENABLE_CFM_EVT"); + BTIF_TRACE_DEBUG("status=%d", p_data->enable_cfm.status); + + if (p_data->enable_cfm.status == BTA_HL_STATUS_OK) { + btif_hl_set_state(BTIF_HL_STATE_ENABLED); + + for (i = 0; i < BTA_HL_NUM_APPS; i++) { + p_acb = BTIF_HL_GET_APP_CB_PTR(i); + if (p_acb->in_use && p_acb->reg_pending) { + p_acb->reg_pending = false; + reg_param.dev_type = p_acb->dev_type; + reg_param.sec_mask = BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT; + reg_param.p_srv_name = p_acb->srv_name; + reg_param.p_srv_desp = p_acb->srv_desp; + reg_param.p_provider_name = p_acb->provider_name; + + BTIF_TRACE_DEBUG("Register pending app_id=%d", p_acb->app_id); + btif_hl_proc_reg_request(i, p_acb->app_id, ®_param, + btif_hl_cback); + } } - else - { - p_acb->filter.num_elems =1; - p_acb->filter.elem[0].data_type = p_acb->sup_feature.mdep[mdep_cfg_index].mdep_cfg.data_cfg[mdep_cfg_index].data_type; - if (p_acb->sup_feature.mdep[mdep_cfg_index].mdep_cfg.mdep_role == BTA_HL_MDEP_ROLE_SINK) - p_acb->filter.elem[0].peer_mdep_role = BTA_HL_MDEP_ROLE_SOURCE; - else - p_acb->filter.elem[0].peer_mdep_role = BTA_HL_MDEP_ROLE_SINK; - - if ( !btif_hl_cch_open(p_acb->app_id, bda, 0, mdep_cfg_index, - BTIF_HL_PEND_DCH_OP_OPEN, - channel_id)) - { - status = BT_STATUS_FAIL; - } + } + + break; + case BTA_HL_CTRL_DISABLE_CFM_EVT: + BTIF_TRACE_DEBUG("Rcv BTA_HL_CTRL_DISABLE_CFM_EVT"); + BTIF_TRACE_DEBUG("status=%d", p_data->disable_cfm.status); + + if (p_data->disable_cfm.status == BTA_HL_STATUS_OK) { + for (size_t i = 0; i < BTA_HL_NUM_APPS; i++) { + for (size_t j = 0; j < BTA_HL_NUM_MCLS; j++) { + alarm_free(p_btif_hl_cb->acb[i].mcb[j].cch_timer); + } } - } - else - { - status = BT_STATUS_FAIL; - } - - BTIF_TRACE_DEBUG("%s status=%d channel_id=0x%08x", __func__, status, *channel_id); - - return status; + memset(p_btif_hl_cb, 0, sizeof(btif_hl_cb_t)); + btif_hl_set_state(BTIF_HL_STATE_DISABLED); + } + + break; + default: + break; + } } -/******************************************************************************* -** -** Function destroy_channel -** -** Description destroy a data channel -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t destroy_channel(int channel_id){ - uint8_t app_idx, mcl_idx, mdl_cfg_idx, mdep_cfg_idx = 0; - bt_status_t status = BT_STATUS_SUCCESS; - btif_hl_mdl_cfg_t *p_mdl; - btif_hl_mcl_cb_t *p_mcb; - btif_hl_app_cb_t *p_acb; - - CHECK_BTHL_INIT(); - BTIF_TRACE_EVENT("%s channel_id=0x%08x", __func__, channel_id); - btif_hl_display_calling_process_name(); - - if (btif_hl_if_channel_setup_pending(channel_id, &app_idx, &mcl_idx)) - { - btif_hl_dch_abort(app_idx, mcl_idx); - } - else - { - if (btif_hl_find_mdl_cfg_idx_using_channel_id(channel_id, &app_idx, &mdl_cfg_idx)) - // if(btif_hl_find_mdl_idx_using_channel_id(channel_id, &app_idx,&mcl_idx, &mdl_idx)) - { - p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - if (!p_acb->delete_mdl.active) - { - p_mdl =BTIF_HL_GET_MDL_CFG_PTR(app_idx, mdl_cfg_idx); - p_acb->delete_mdl.active = true; - p_acb->delete_mdl.mdl_id = p_mdl->base.mdl_id; - p_acb->delete_mdl.channel_id = channel_id; - p_acb->delete_mdl.mdep_cfg_idx = p_mdl->extra.mdep_cfg_idx; - memcpy(p_acb->delete_mdl.bd_addr, p_mdl->base.peer_bd_addr,sizeof(BD_ADDR)); - - if (btif_hl_find_mcl_idx(app_idx, p_mdl->base.peer_bd_addr, &mcl_idx)) - { - p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - if (p_mcb->is_connected) - { - BTIF_TRACE_DEBUG("calling BTA_HlDeleteMdl mdl_id=%d",p_acb->delete_mdl.mdl_id ); - BTA_HlDeleteMdl(p_mcb->mcl_handle, p_acb->delete_mdl.mdl_id); - } - else - { - status = BT_STATUS_FAIL; - } - } - else - { - BTIF_TRACE_DEBUG("btif_hl_delete_mdl calling btif_hl_cch_open" ); - mdep_cfg_idx = p_mdl->extra.mdep_cfg_idx; - p_acb->filter.num_elems =1; - p_acb->filter.elem[0].data_type = p_acb->sup_feature.mdep[mdep_cfg_idx].mdep_cfg.data_cfg[mdep_cfg_idx].data_type; - if (p_acb->sup_feature.mdep[mdep_cfg_idx].mdep_cfg.mdep_role == BTA_HL_MDEP_ROLE_SINK) - p_acb->filter.elem[0].peer_mdep_role = BTA_HL_MDEP_ROLE_SOURCE; - else - p_acb->filter.elem[0].peer_mdep_role = BTA_HL_MDEP_ROLE_SINK; - if (btif_hl_cch_open(p_acb->app_id, p_acb->delete_mdl.bd_addr, 0, - mdep_cfg_idx, - BTIF_HL_PEND_DCH_OP_DELETE_MDL, NULL)) - { - status = BT_STATUS_FAIL; - } - } - - if ( status == BT_STATUS_FAIL) - { - /* fail for now */ - btif_hl_clean_delete_mdl(&p_acb->delete_mdl); - } - } - else - { - status = BT_STATUS_BUSY; - } - } - else - { - status = BT_STATUS_FAIL; - } - } - return status; -} /******************************************************************************* -** -** Function unregister_application -** -** Description unregister an HDP application -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t unregister_application(int app_id){ - uint8_t app_idx; - int len; - bt_status_t status = BT_STATUS_SUCCESS; - btif_hl_evt_cb_t evt_param; - - CHECK_BTHL_INIT(); - BTIF_TRACE_EVENT("%s app_id=%d", __func__, app_id); - btif_hl_display_calling_process_name(); - - if (btif_hl_find_app_idx(((uint8_t)app_id), &app_idx)) - { - evt_param.unreg.app_idx = app_idx; - reg_counter --; - len = sizeof(btif_hl_unreg_t); - status = btif_transfer_context (btif_hl_proc_cb_evt, BTIF_HL_UNREG_APP, - (char*) &evt_param, len, NULL); - ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); - } - else - { - status = BT_STATUS_FAIL; - } - - BTIF_TRACE_DEBUG("de-reg return status=%d", status); - return status; + * + * Function btif_hl_ctrl_cback + * + * Description Callback function for HL control events + * + * Returns void + * + ******************************************************************************/ +static void btif_hl_ctrl_cback(tBTA_HL_CTRL_EVT event, tBTA_HL_CTRL* p_data) { + bt_status_t status; + int param_len = 0; + + BTIF_TRACE_DEBUG("%s event %d", __func__, event); + btif_hl_display_calling_process_name(); + + switch (event) { + case BTA_HL_CTRL_ENABLE_CFM_EVT: + case BTA_HL_CTRL_DISABLE_CFM_EVT: + param_len = sizeof(tBTA_HL_CTRL_ENABLE_DISABLE); + break; + default: + break; + } + + status = btif_transfer_context(btif_hl_upstreams_ctrl_evt, (uint16_t)event, + (char*)p_data, param_len, NULL); + ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); } /******************************************************************************* -** -** Function register_application -** -** Description register an HDP application -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t register_application(bthl_reg_param_t *p_reg_param, int *app_id){ - btif_hl_app_cb_t *p_acb; - tBTA_HL_SUP_FEATURE *p_sup; - tBTA_HL_MDEP_CFG *p_cfg; - tBTA_HL_MDEP_DATA_TYPE_CFG *p_data; - uint8_t app_idx=0, i=0; - bthl_mdep_cfg_t *p_mdep_cfg; - bt_status_t status = BT_STATUS_SUCCESS; - btif_hl_evt_cb_t evt_param; - int len; - - CHECK_BTHL_INIT(); - BTIF_TRACE_EVENT("%s", __func__); - btif_hl_display_calling_process_name(); - - if (btif_hl_get_state() == BTIF_HL_STATE_DISABLED) - { - btif_hl_init(); - btif_hl_set_state(BTIF_HL_STATE_ENABLING); - BTA_HlEnable(btif_hl_ctrl_cback); - } - - if (!btif_hl_find_avail_app_idx(&app_idx)) - { - BTIF_TRACE_ERROR("Unable to allocate a new application control block"); - return BT_STATUS_FAIL; - } - + * + * Function connect_channel + * + * Description connect a data channel + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t connect_channel(int app_id, bt_bdaddr_t* bd_addr, + int mdep_cfg_index, int* channel_id) { + uint8_t app_idx, mcl_idx; + btif_hl_app_cb_t* p_acb = NULL; + btif_hl_pending_chan_cb_t* p_pcb = NULL; + btif_hl_mcl_cb_t* p_mcb = NULL; + bt_status_t status = BT_STATUS_SUCCESS; + tBTA_HL_DCH_OPEN_PARAM dch_open; + BD_ADDR bda; + uint8_t i; + + CHECK_BTHL_INIT(); + BTIF_TRACE_EVENT("%s", __func__); + btif_hl_display_calling_process_name(); + + for (i = 0; i < 6; i++) { + bda[i] = (uint8_t)bd_addr->address[i]; + } + if (btif_hl_find_app_idx(((uint8_t)app_id), &app_idx)) { p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - p_acb->in_use = true; - - p_acb->app_id = btif_hl_get_next_app_id(); - - if (p_reg_param->application_name != NULL ) - strncpy(p_acb->application_name, p_reg_param->application_name, BTIF_HL_APPLICATION_NAME_LEN); - - if (p_reg_param->provider_name != NULL ) - strncpy(p_acb->provider_name, p_reg_param->provider_name, BTA_PROVIDER_NAME_LEN); - - if (p_reg_param->srv_name != NULL ) - strncpy(p_acb->srv_name, p_reg_param->srv_name, BTA_SERVICE_NAME_LEN); - - if (p_reg_param->srv_desp != NULL ) - strncpy(p_acb->srv_desp, p_reg_param->srv_desp, BTA_SERVICE_DESP_LEN); + if (btif_hl_find_mcl_idx(app_idx, bda, &mcl_idx)) { + p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + if (p_mcb->is_connected) { + dch_open.ctrl_psm = p_mcb->ctrl_psm; + dch_open.local_mdep_id = + p_acb->sup_feature.mdep[mdep_cfg_index].mdep_id; + BTIF_TRACE_DEBUG( + "connect_channel: app_idx =%d, mdep_cfg_indx =%d, mdep_id =%d " + "app_id= %d", + app_idx, mdep_cfg_index, dch_open.local_mdep_id, app_id); + if (btif_hl_find_peer_mdep_id( + p_acb->app_id, p_mcb->bd_addr, + p_acb->sup_feature.mdep[mdep_cfg_index].mdep_cfg.mdep_role, + p_acb->sup_feature.mdep[mdep_cfg_index] + .mdep_cfg.data_cfg[0] + .data_type, + &dch_open.peer_mdep_id)) { + dch_open.local_cfg = p_acb->channel_type[mdep_cfg_index]; + if ((p_acb->sup_feature.mdep[mdep_cfg_index].mdep_cfg.mdep_role == + BTA_HL_MDEP_ROLE_SOURCE) && + !btif_hl_is_the_first_reliable_existed(app_idx, mcl_idx)) { + dch_open.local_cfg = BTA_HL_DCH_CFG_RELIABLE; + } + dch_open.sec_mask = (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT); + + if (!btif_hl_dch_open(p_acb->app_id, bda, &dch_open, mdep_cfg_index, + BTIF_HL_PEND_DCH_OP_OPEN, channel_id)) { + status = BT_STATUS_FAIL; + BTIF_TRACE_EVENT("%s loc0 status = BT_STATUS_FAIL", __func__); + } + } else { + p_mcb->cch_oper = BTIF_HL_CCH_OP_MDEP_FILTERING; + + p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx); + p_pcb->in_use = true; + p_pcb->mdep_cfg_idx = mdep_cfg_index; + memcpy(p_pcb->bd_addr, bda, sizeof(BD_ADDR)); + p_pcb->op = BTIF_HL_PEND_DCH_OP_OPEN; + BTA_HlSdpQuery(app_id, p_acb->app_handle, bda); + } + } else { + status = BT_STATUS_FAIL; + } + } else { + p_acb->filter.num_elems = 1; + p_acb->filter.elem[0].data_type = p_acb->sup_feature.mdep[mdep_cfg_index] + .mdep_cfg.data_cfg[mdep_cfg_index] + .data_type; + if (p_acb->sup_feature.mdep[mdep_cfg_index].mdep_cfg.mdep_role == + BTA_HL_MDEP_ROLE_SINK) + p_acb->filter.elem[0].peer_mdep_role = BTA_HL_MDEP_ROLE_SOURCE; + else + p_acb->filter.elem[0].peer_mdep_role = BTA_HL_MDEP_ROLE_SINK; + + if (!btif_hl_cch_open(p_acb->app_id, bda, 0, mdep_cfg_index, + BTIF_HL_PEND_DCH_OP_OPEN, channel_id)) { + status = BT_STATUS_FAIL; + } + } + } else { + status = BT_STATUS_FAIL; + } - p_sup = &p_acb->sup_feature; - p_sup->advertize_source_sdp = true; - p_sup->echo_cfg.max_rx_apdu_size = BTIF_HL_ECHO_MAX_TX_RX_APDU_SIZE; - p_sup->echo_cfg.max_tx_apdu_size = BTIF_HL_ECHO_MAX_TX_RX_APDU_SIZE; - p_sup->num_of_mdeps = p_reg_param->number_of_mdeps; + BTIF_TRACE_DEBUG("%s status=%d channel_id=0x%08x", __func__, status, + *channel_id); - for (i=0, p_mdep_cfg = p_reg_param->mdep_cfg ; i< p_sup->num_of_mdeps; i++, p_mdep_cfg++ ) + return status; +} +/******************************************************************************* + * + * Function destroy_channel + * + * Description destroy a data channel + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t destroy_channel(int channel_id) { + uint8_t app_idx, mcl_idx, mdl_cfg_idx, mdep_cfg_idx = 0; + bt_status_t status = BT_STATUS_SUCCESS; + btif_hl_mdl_cfg_t* p_mdl; + btif_hl_mcl_cb_t* p_mcb; + btif_hl_app_cb_t* p_acb; + + CHECK_BTHL_INIT(); + BTIF_TRACE_EVENT("%s channel_id=0x%08x", __func__, channel_id); + btif_hl_display_calling_process_name(); + + if (btif_hl_if_channel_setup_pending(channel_id, &app_idx, &mcl_idx)) { + btif_hl_dch_abort(app_idx, mcl_idx); + } else { + if (btif_hl_find_mdl_cfg_idx_using_channel_id(channel_id, &app_idx, + &mdl_cfg_idx)) + // if(btif_hl_find_mdl_idx_using_channel_id(channel_id, + // &app_idx,&mcl_idx, &mdl_idx)) { - p_cfg = &p_sup->mdep[i].mdep_cfg; - p_cfg->num_of_mdep_data_types = 1; - p_data = &p_cfg->data_cfg[0]; - - if ( !btif_hl_get_bta_mdep_role(p_mdep_cfg->mdep_role, &(p_cfg->mdep_role))) - { - BTIF_TRACE_ERROR("Invalid mdep_role=%d", p_mdep_cfg->mdep_role); + p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + if (!p_acb->delete_mdl.active) { + p_mdl = BTIF_HL_GET_MDL_CFG_PTR(app_idx, mdl_cfg_idx); + p_acb->delete_mdl.active = true; + p_acb->delete_mdl.mdl_id = p_mdl->base.mdl_id; + p_acb->delete_mdl.channel_id = channel_id; + p_acb->delete_mdl.mdep_cfg_idx = p_mdl->extra.mdep_cfg_idx; + memcpy(p_acb->delete_mdl.bd_addr, p_mdl->base.peer_bd_addr, + sizeof(BD_ADDR)); + + if (btif_hl_find_mcl_idx(app_idx, p_mdl->base.peer_bd_addr, &mcl_idx)) { + p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + if (p_mcb->is_connected) { + BTIF_TRACE_DEBUG("calling BTA_HlDeleteMdl mdl_id=%d", + p_acb->delete_mdl.mdl_id); + BTA_HlDeleteMdl(p_mcb->mcl_handle, p_acb->delete_mdl.mdl_id); + } else { status = BT_STATUS_FAIL; - break; + } + } else { + BTIF_TRACE_DEBUG("btif_hl_delete_mdl calling btif_hl_cch_open"); + mdep_cfg_idx = p_mdl->extra.mdep_cfg_idx; + p_acb->filter.num_elems = 1; + p_acb->filter.elem[0].data_type = + p_acb->sup_feature.mdep[mdep_cfg_idx] + .mdep_cfg.data_cfg[mdep_cfg_idx] + .data_type; + if (p_acb->sup_feature.mdep[mdep_cfg_idx].mdep_cfg.mdep_role == + BTA_HL_MDEP_ROLE_SINK) + p_acb->filter.elem[0].peer_mdep_role = BTA_HL_MDEP_ROLE_SOURCE; + else + p_acb->filter.elem[0].peer_mdep_role = BTA_HL_MDEP_ROLE_SINK; + if (btif_hl_cch_open(p_acb->app_id, p_acb->delete_mdl.bd_addr, 0, + mdep_cfg_idx, BTIF_HL_PEND_DCH_OP_DELETE_MDL, + NULL)) { + status = BT_STATUS_FAIL; + } } - else - { - if (p_cfg->mdep_role == BTA_HL_MDEP_ROLE_SINK ) - p_sup->app_role_mask |= BTA_HL_MDEP_ROLE_MASK_SINK; - else - p_sup->app_role_mask |= BTA_HL_MDEP_ROLE_MASK_SOURCE; - - if ( (p_sup->app_role_mask & BTA_HL_MDEP_ROLE_MASK_SINK) && - (p_sup->app_role_mask & BTA_HL_MDEP_ROLE_MASK_SOURCE) ) - { - p_acb->dev_type = BTA_HL_DEVICE_TYPE_DUAL; - } else if (p_sup->app_role_mask & BTA_HL_MDEP_ROLE_MASK_SINK) { - p_acb->dev_type = BTA_HL_DEVICE_TYPE_SINK; - } else { - p_acb->dev_type = BTA_HL_DEVICE_TYPE_SOURCE; - } - p_data->data_type = (uint16_t) p_mdep_cfg->data_type; - p_data->max_rx_apdu_size = btif_hl_get_max_rx_apdu_size(p_cfg->mdep_role, p_data->data_type); - p_data->max_tx_apdu_size = btif_hl_get_max_tx_apdu_size(p_cfg->mdep_role, p_data->data_type); - - if (p_mdep_cfg->mdep_description != NULL ) - strncpy(p_data->desp, p_mdep_cfg->mdep_description, BTA_SERVICE_DESP_LEN); - - if ( !btif_hl_get_bta_channel_type(p_mdep_cfg->channel_type, &(p_acb->channel_type[i]))) - { - BTIF_TRACE_ERROR("Invalid channel_type=%d", p_mdep_cfg->channel_type); - status = BT_STATUS_FAIL; - break; - } + if (status == BT_STATUS_FAIL) { + /* fail for now */ + btif_hl_clean_delete_mdl(&p_acb->delete_mdl); } + } else { + status = BT_STATUS_BUSY; + } + } else { + status = BT_STATUS_FAIL; } + } + return status; +} +/******************************************************************************* + * + * Function unregister_application + * + * Description unregister an HDP application + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t unregister_application(int app_id) { + uint8_t app_idx; + int len; + bt_status_t status = BT_STATUS_SUCCESS; + btif_hl_evt_cb_t evt_param; + + CHECK_BTHL_INIT(); + BTIF_TRACE_EVENT("%s app_id=%d", __func__, app_id); + btif_hl_display_calling_process_name(); + + if (btif_hl_find_app_idx(((uint8_t)app_id), &app_idx)) { + evt_param.unreg.app_idx = app_idx; + reg_counter--; + len = sizeof(btif_hl_unreg_t); + status = btif_transfer_context(btif_hl_proc_cb_evt, BTIF_HL_UNREG_APP, + (char*)&evt_param, len, NULL); + ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); + } else { + status = BT_STATUS_FAIL; + } - if (status == BT_STATUS_SUCCESS) - { - *app_id = (int) p_acb->app_id; - evt_param.reg.app_idx = app_idx; - len = sizeof(btif_hl_reg_t); - p_acb->reg_pending = true; - reg_counter++; - BTIF_TRACE_DEBUG("calling btif_transfer_context status=%d app_id=%d", status, *app_id); - status = btif_transfer_context (btif_hl_proc_cb_evt, BTIF_HL_REG_APP, - (char*) &evt_param, len, NULL); - ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); - - } - else - { - btif_hl_free_app_idx(app_idx); + BTIF_TRACE_DEBUG("de-reg return status=%d", status); + return status; +} +/******************************************************************************* + * + * Function register_application + * + * Description register an HDP application + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t register_application(bthl_reg_param_t* p_reg_param, + int* app_id) { + btif_hl_app_cb_t* p_acb; + tBTA_HL_SUP_FEATURE* p_sup; + tBTA_HL_MDEP_CFG* p_cfg; + tBTA_HL_MDEP_DATA_TYPE_CFG* p_data; + uint8_t app_idx = 0, i = 0; + bthl_mdep_cfg_t* p_mdep_cfg; + bt_status_t status = BT_STATUS_SUCCESS; + btif_hl_evt_cb_t evt_param; + int len; + + CHECK_BTHL_INIT(); + BTIF_TRACE_EVENT("%s", __func__); + btif_hl_display_calling_process_name(); + + if (btif_hl_get_state() == BTIF_HL_STATE_DISABLED) { + btif_hl_init(); + btif_hl_set_state(BTIF_HL_STATE_ENABLING); + BTA_HlEnable(btif_hl_ctrl_cback); + } + + if (!btif_hl_find_avail_app_idx(&app_idx)) { + BTIF_TRACE_ERROR("Unable to allocate a new application control block"); + return BT_STATUS_FAIL; + } + + p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + p_acb->in_use = true; + + p_acb->app_id = btif_hl_get_next_app_id(); + + if (p_reg_param->application_name != NULL) + strncpy(p_acb->application_name, p_reg_param->application_name, + BTIF_HL_APPLICATION_NAME_LEN); + + if (p_reg_param->provider_name != NULL) + strncpy(p_acb->provider_name, p_reg_param->provider_name, + BTA_PROVIDER_NAME_LEN); + + if (p_reg_param->srv_name != NULL) + strncpy(p_acb->srv_name, p_reg_param->srv_name, BTA_SERVICE_NAME_LEN); + + if (p_reg_param->srv_desp != NULL) + strncpy(p_acb->srv_desp, p_reg_param->srv_desp, BTA_SERVICE_DESP_LEN); + + p_sup = &p_acb->sup_feature; + p_sup->advertize_source_sdp = true; + p_sup->echo_cfg.max_rx_apdu_size = BTIF_HL_ECHO_MAX_TX_RX_APDU_SIZE; + p_sup->echo_cfg.max_tx_apdu_size = BTIF_HL_ECHO_MAX_TX_RX_APDU_SIZE; + p_sup->num_of_mdeps = p_reg_param->number_of_mdeps; + + for (i = 0, p_mdep_cfg = p_reg_param->mdep_cfg; i < p_sup->num_of_mdeps; + i++, p_mdep_cfg++) { + p_cfg = &p_sup->mdep[i].mdep_cfg; + p_cfg->num_of_mdep_data_types = 1; + p_data = &p_cfg->data_cfg[0]; + + if (!btif_hl_get_bta_mdep_role(p_mdep_cfg->mdep_role, + &(p_cfg->mdep_role))) { + BTIF_TRACE_ERROR("Invalid mdep_role=%d", p_mdep_cfg->mdep_role); + status = BT_STATUS_FAIL; + break; + } else { + if (p_cfg->mdep_role == BTA_HL_MDEP_ROLE_SINK) + p_sup->app_role_mask |= BTA_HL_MDEP_ROLE_MASK_SINK; + else + p_sup->app_role_mask |= BTA_HL_MDEP_ROLE_MASK_SOURCE; + + if ((p_sup->app_role_mask & BTA_HL_MDEP_ROLE_MASK_SINK) && + (p_sup->app_role_mask & BTA_HL_MDEP_ROLE_MASK_SOURCE)) { + p_acb->dev_type = BTA_HL_DEVICE_TYPE_DUAL; + } else if (p_sup->app_role_mask & BTA_HL_MDEP_ROLE_MASK_SINK) { + p_acb->dev_type = BTA_HL_DEVICE_TYPE_SINK; + } else { + p_acb->dev_type = BTA_HL_DEVICE_TYPE_SOURCE; + } + + p_data->data_type = (uint16_t)p_mdep_cfg->data_type; + p_data->max_rx_apdu_size = + btif_hl_get_max_rx_apdu_size(p_cfg->mdep_role, p_data->data_type); + p_data->max_tx_apdu_size = + btif_hl_get_max_tx_apdu_size(p_cfg->mdep_role, p_data->data_type); + + if (p_mdep_cfg->mdep_description != NULL) + strncpy(p_data->desp, p_mdep_cfg->mdep_description, + BTA_SERVICE_DESP_LEN); + + if (!btif_hl_get_bta_channel_type(p_mdep_cfg->channel_type, + &(p_acb->channel_type[i]))) { + BTIF_TRACE_ERROR("Invalid channel_type=%d", p_mdep_cfg->channel_type); + status = BT_STATUS_FAIL; + break; + } } + } + + if (status == BT_STATUS_SUCCESS) { + *app_id = (int)p_acb->app_id; + evt_param.reg.app_idx = app_idx; + len = sizeof(btif_hl_reg_t); + p_acb->reg_pending = true; + reg_counter++; + BTIF_TRACE_DEBUG("calling btif_transfer_context status=%d app_id=%d", + status, *app_id); + status = btif_transfer_context(btif_hl_proc_cb_evt, BTIF_HL_REG_APP, + (char*)&evt_param, len, NULL); + ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); - BTIF_TRACE_DEBUG("register_application status=%d app_id=%d", status, *app_id); - return status; + } else { + btif_hl_free_app_idx(app_idx); + } + + BTIF_TRACE_DEBUG("register_application status=%d app_id=%d", status, *app_id); + return status; } /******************************************************************************* -** -** Function btif_hl_save_mdl_cfg -** -** Description Save the MDL configuration -** -** Returns bool -** -*******************************************************************************/ -bool btif_hl_save_mdl_cfg(uint8_t mdep_id, uint8_t item_idx, - tBTA_HL_MDL_CFG *p_mdl_cfg){ - btif_hl_mdl_cfg_t *p_mdl=NULL; - bool success = false; - btif_hl_app_cb_t *p_acb; - btif_hl_mcl_cb_t *p_mcb; - uint8_t app_idx, mcl_idx, len; - bt_status_t bt_status; - btif_hl_evt_cb_t evt_param; - int *p_channel_id; - - BTIF_TRACE_DEBUG("%s mdep_id=%d item_idx=%d, local_mdep_id=%d mdl_id=0x%x dch_mode=%d", - __func__, mdep_id, item_idx, p_mdl_cfg->local_mdep_id, - p_mdl_cfg->mdl_id, p_mdl_cfg->dch_mode ); - - if(btif_hl_find_app_idx_using_mdepId(mdep_id,&app_idx)) - { - p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - p_mdl = BTIF_HL_GET_MDL_CFG_PTR(app_idx, item_idx); - p_channel_id = BTIF_HL_GET_MDL_CFG_CHANNEL_ID_PTR(app_idx, item_idx); - if (p_mdl) - { - memcpy(&p_mdl->base, p_mdl_cfg, sizeof(tBTA_HL_MDL_CFG)); - if (btif_hl_find_mcl_idx(app_idx, p_mdl->base.peer_bd_addr , &mcl_idx)) - { - p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - if (p_mcb->pcb.in_use) - *p_channel_id = p_mcb->pcb.channel_id; - else - *p_channel_id = btif_hl_get_next_channel_id(p_acb->app_id); - p_mdl->extra.mdep_cfg_idx = p_mcb->pcb.mdep_cfg_idx; - p_mdl->extra.data_type = p_acb->sup_feature.mdep[p_mcb->pcb.mdep_cfg_idx].mdep_cfg.data_cfg[0].data_type; - - if (!btif_hl_find_peer_mdep_id(p_acb->app_id, p_mcb->bd_addr, - p_acb->sup_feature.mdep[p_mcb->pcb.mdep_cfg_idx].mdep_cfg.mdep_role, - p_acb->sup_feature.mdep[p_mcb->pcb.mdep_cfg_idx].mdep_cfg.data_cfg[0].data_type, - &p_mdl->extra.peer_mdep_id)) - { - p_mdl->extra.peer_mdep_id = BTA_HL_INVALID_MDEP_ID; - } - BTIF_TRACE_DEBUG("%s app_idx=%d item_idx=%d mld_id=0x%x", - __func__, app_idx, item_idx, p_mdl->base.mdl_id); - evt_param.update_mdl.app_idx = app_idx; - len = sizeof(btif_hl_update_mdl_t); - BTIF_TRACE_DEBUG("send BTIF_HL_UPDATE_MDL event app_idx=%d ",app_idx); - if ((bt_status = btif_transfer_context (btif_hl_proc_cb_evt, BTIF_HL_UPDATE_MDL, - (char*) &evt_param, len, NULL)) == BT_STATUS_SUCCESS) - { - success = true; - } - ASSERTC(bt_status == BT_STATUS_SUCCESS, "context transfer failed", bt_status); - } + * + * Function btif_hl_save_mdl_cfg + * + * Description Save the MDL configuration + * + * Returns bool + * + ******************************************************************************/ +bool btif_hl_save_mdl_cfg(uint8_t mdep_id, uint8_t item_idx, + tBTA_HL_MDL_CFG* p_mdl_cfg) { + btif_hl_mdl_cfg_t* p_mdl = NULL; + bool success = false; + btif_hl_app_cb_t* p_acb; + btif_hl_mcl_cb_t* p_mcb; + uint8_t app_idx, mcl_idx, len; + bt_status_t bt_status; + btif_hl_evt_cb_t evt_param; + int* p_channel_id; + + BTIF_TRACE_DEBUG( + "%s mdep_id=%d item_idx=%d, local_mdep_id=%d mdl_id=0x%x dch_mode=%d", + __func__, mdep_id, item_idx, p_mdl_cfg->local_mdep_id, p_mdl_cfg->mdl_id, + p_mdl_cfg->dch_mode); + + if (btif_hl_find_app_idx_using_mdepId(mdep_id, &app_idx)) { + p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + p_mdl = BTIF_HL_GET_MDL_CFG_PTR(app_idx, item_idx); + p_channel_id = BTIF_HL_GET_MDL_CFG_CHANNEL_ID_PTR(app_idx, item_idx); + if (p_mdl) { + memcpy(&p_mdl->base, p_mdl_cfg, sizeof(tBTA_HL_MDL_CFG)); + if (btif_hl_find_mcl_idx(app_idx, p_mdl->base.peer_bd_addr, &mcl_idx)) { + p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + if (p_mcb->pcb.in_use) + *p_channel_id = p_mcb->pcb.channel_id; + else + *p_channel_id = btif_hl_get_next_channel_id(p_acb->app_id); + p_mdl->extra.mdep_cfg_idx = p_mcb->pcb.mdep_cfg_idx; + p_mdl->extra.data_type = + p_acb->sup_feature.mdep[p_mcb->pcb.mdep_cfg_idx] + .mdep_cfg.data_cfg[0] + .data_type; + + if (!btif_hl_find_peer_mdep_id( + p_acb->app_id, p_mcb->bd_addr, + p_acb->sup_feature.mdep[p_mcb->pcb.mdep_cfg_idx] + .mdep_cfg.mdep_role, + p_acb->sup_feature.mdep[p_mcb->pcb.mdep_cfg_idx] + .mdep_cfg.data_cfg[0] + .data_type, + &p_mdl->extra.peer_mdep_id)) { + p_mdl->extra.peer_mdep_id = BTA_HL_INVALID_MDEP_ID; + } + BTIF_TRACE_DEBUG("%s app_idx=%d item_idx=%d mld_id=0x%x", __func__, + app_idx, item_idx, p_mdl->base.mdl_id); + evt_param.update_mdl.app_idx = app_idx; + len = sizeof(btif_hl_update_mdl_t); + BTIF_TRACE_DEBUG("send BTIF_HL_UPDATE_MDL event app_idx=%d ", app_idx); + if ((bt_status = btif_transfer_context( + btif_hl_proc_cb_evt, BTIF_HL_UPDATE_MDL, (char*)&evt_param, + len, NULL)) == BT_STATUS_SUCCESS) { + success = true; } + ASSERTC(bt_status == BT_STATUS_SUCCESS, "context transfer failed", + bt_status); + } } - BTIF_TRACE_DEBUG("%s success=%d ",__func__, success ); + } + BTIF_TRACE_DEBUG("%s success=%d ", __func__, success); - return success; + return success; } /******************************************************************************* -** -** Function btif_hl_delete_mdl_cfg -** -** Description Delete the MDL configuration -** -** Returns bool -** -*******************************************************************************/ -bool btif_hl_delete_mdl_cfg(uint8_t mdep_id, uint8_t item_idx){ - btif_hl_mdl_cfg_t *p_mdl=NULL; - bool success = false; - uint8_t app_idx, len; - bt_status_t bt_status; - btif_hl_evt_cb_t evt_param; - - if(btif_hl_find_app_idx_using_mdepId(mdep_id,&app_idx)) - { - p_mdl = BTIF_HL_GET_MDL_CFG_PTR(app_idx, item_idx); - if (p_mdl) - { - memset(p_mdl, 0, sizeof(btif_hl_mdl_cfg_t)); - evt_param.update_mdl.app_idx = app_idx; - len = sizeof(btif_hl_update_mdl_t); - BTIF_TRACE_DEBUG("send BTIF_HL_UPDATE_MDL event app_idx=%d ",app_idx); - if ((bt_status = btif_transfer_context (btif_hl_proc_cb_evt, BTIF_HL_UPDATE_MDL, - (char*) &evt_param, len, NULL)) == BT_STATUS_SUCCESS) - { - success = true; - } - ASSERTC(bt_status == BT_STATUS_SUCCESS, "context transfer failed", bt_status); - } + * + * Function btif_hl_delete_mdl_cfg + * + * Description Delete the MDL configuration + * + * Returns bool + * + ******************************************************************************/ +bool btif_hl_delete_mdl_cfg(uint8_t mdep_id, uint8_t item_idx) { + btif_hl_mdl_cfg_t* p_mdl = NULL; + bool success = false; + uint8_t app_idx, len; + bt_status_t bt_status; + btif_hl_evt_cb_t evt_param; + + if (btif_hl_find_app_idx_using_mdepId(mdep_id, &app_idx)) { + p_mdl = BTIF_HL_GET_MDL_CFG_PTR(app_idx, item_idx); + if (p_mdl) { + memset(p_mdl, 0, sizeof(btif_hl_mdl_cfg_t)); + evt_param.update_mdl.app_idx = app_idx; + len = sizeof(btif_hl_update_mdl_t); + BTIF_TRACE_DEBUG("send BTIF_HL_UPDATE_MDL event app_idx=%d ", app_idx); + if ((bt_status = btif_transfer_context( + btif_hl_proc_cb_evt, BTIF_HL_UPDATE_MDL, (char*)&evt_param, len, + NULL)) == BT_STATUS_SUCCESS) { + success = true; + } + ASSERTC(bt_status == BT_STATUS_SUCCESS, "context transfer failed", + bt_status); } + } - BTIF_TRACE_DEBUG("%s success=%d ",__func__, success ); - return success; + BTIF_TRACE_DEBUG("%s success=%d ", __func__, success); + return success; } /******************************************************************************* -** -** Function init -** -** Description initializes the hl interface -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t init( bthl_callbacks_t* callbacks ){ - bt_status_t status = BT_STATUS_SUCCESS; - - BTIF_TRACE_EVENT("%s", __func__); - btif_hl_display_calling_process_name(); - bt_hl_callbacks_cb = *callbacks; - bt_hl_callbacks = &bt_hl_callbacks_cb; - btif_hl_soc_thread_init(); - reg_counter = 0; - return status; + * + * Function init + * + * Description initializes the hl interface + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t init(bthl_callbacks_t* callbacks) { + bt_status_t status = BT_STATUS_SUCCESS; + + BTIF_TRACE_EVENT("%s", __func__); + btif_hl_display_calling_process_name(); + bt_hl_callbacks_cb = *callbacks; + bt_hl_callbacks = &bt_hl_callbacks_cb; + btif_hl_soc_thread_init(); + reg_counter = 0; + return status; } /******************************************************************************* -** -** Function cleanup -** -** Description Closes the HL interface -** -** Returns void -** -*******************************************************************************/ -static void cleanup( void ){ - BTIF_TRACE_EVENT("%s", __func__); - btif_hl_display_calling_process_name(); - if (bt_hl_callbacks) - { - btif_disable_service(BTA_HDP_SERVICE_ID); - bt_hl_callbacks = NULL; - reg_counter = 0; - } + * + * Function cleanup + * + * Description Closes the HL interface + * + * Returns void + * + ******************************************************************************/ +static void cleanup(void) { + BTIF_TRACE_EVENT("%s", __func__); + btif_hl_display_calling_process_name(); + if (bt_hl_callbacks) { + btif_disable_service(BTA_HDP_SERVICE_ID); + bt_hl_callbacks = NULL; + reg_counter = 0; + } - btif_hl_disable(); - btif_hl_close_select_thread(); + btif_hl_disable(); + btif_hl_close_select_thread(); } static const bthl_interface_t bthlInterface = { @@ -4335,630 +4086,627 @@ static const bthl_interface_t bthlInterface = { }; /******************************************************************************* -** -** Function btif_hl_get_interface -** -** Description Get the hl callback interface -** -** Returns bthf_interface_t -** -*******************************************************************************/ -const bthl_interface_t *btif_hl_get_interface(){ - BTIF_TRACE_EVENT("%s", __func__); - return &bthlInterface; + * + * Function btif_hl_get_interface + * + * Description Get the hl callback interface + * + * Returns bthf_interface_t + * + ******************************************************************************/ +const bthl_interface_t* btif_hl_get_interface() { + BTIF_TRACE_EVENT("%s", __func__); + return &bthlInterface; } /******************************************************************************* -** -** Function btif_hl_update_maxfd -** -** Description Update the max fd if the input fd is greater than the current max fd -** -** Returns int -** -*******************************************************************************/ + * + * Function btif_hl_update_maxfd + * + * Description Update the max fd if the input fd is greater than the current max + *fd + * + * Returns int + * + ******************************************************************************/ int btif_hl_update_maxfd(int max_org_s) { - int maxfd = max_org_s; - - BTIF_TRACE_DEBUG("btif_hl_update_maxfd max_org_s= %d", max_org_s); - for (const list_node_t *node = list_begin(soc_queue); - node != list_end(soc_queue); node = list_next(node)) { - btif_hl_soc_cb_t *p_scb = (btif_hl_soc_cb_t *)list_node(node); - if (maxfd < p_scb->max_s) { - maxfd = p_scb->max_s; - BTIF_TRACE_DEBUG("btif_hl_update_maxfd maxfd=%d", maxfd); - } + int maxfd = max_org_s; + + BTIF_TRACE_DEBUG("btif_hl_update_maxfd max_org_s= %d", max_org_s); + for (const list_node_t* node = list_begin(soc_queue); + node != list_end(soc_queue); node = list_next(node)) { + btif_hl_soc_cb_t* p_scb = (btif_hl_soc_cb_t*)list_node(node); + if (maxfd < p_scb->max_s) { + maxfd = p_scb->max_s; + BTIF_TRACE_DEBUG("btif_hl_update_maxfd maxfd=%d", maxfd); } + } - BTIF_TRACE_DEBUG("btif_hl_update_maxfd final *p_max_s=%d", maxfd); - return maxfd; + BTIF_TRACE_DEBUG("btif_hl_update_maxfd final *p_max_s=%d", maxfd); + return maxfd; } /******************************************************************************* -** -** Function btif_hl_get_socket_state -** -** Description get socket state -** -** Returns btif_hl_soc_state_t -** -*******************************************************************************/ -btif_hl_soc_state_t btif_hl_get_socket_state(btif_hl_soc_cb_t *p_scb){ - BTIF_TRACE_DEBUG("btif_hl_get_socket_state state=%d", p_scb->state); - return p_scb->state; + * + * Function btif_hl_get_socket_state + * + * Description get socket state + * + * Returns btif_hl_soc_state_t + * + ******************************************************************************/ +btif_hl_soc_state_t btif_hl_get_socket_state(btif_hl_soc_cb_t* p_scb) { + BTIF_TRACE_DEBUG("btif_hl_get_socket_state state=%d", p_scb->state); + return p_scb->state; } /******************************************************************************* -** -** Function btif_hl_set_socket_state -** -** Description set socket state -** -** Returns void -** -*******************************************************************************/ -void btif_hl_set_socket_state(btif_hl_soc_cb_t *p_scb, btif_hl_soc_state_t new_state){ - BTIF_TRACE_DEBUG("btif_hl_set_socket_state %d---->%d", p_scb->state, new_state); - p_scb->state = new_state; + * + * Function btif_hl_set_socket_state + * + * Description set socket state + * + * Returns void + * + ******************************************************************************/ +void btif_hl_set_socket_state(btif_hl_soc_cb_t* p_scb, + btif_hl_soc_state_t new_state) { + BTIF_TRACE_DEBUG("btif_hl_set_socket_state %d---->%d", p_scb->state, + new_state); + p_scb->state = new_state; } /******************************************************************************* -** -** Function btif_hl_release_mcl_sockets -** -** Description Release all sockets on the MCL -** -** Returns void -** -*******************************************************************************/ -void btif_hl_release_mcl_sockets(uint8_t app_idx, uint8_t mcl_idx){ - uint8_t i; - btif_hl_mdl_cb_t *p_dcb; - bool found= false; - BTIF_TRACE_DEBUG("%s", __func__); - for (i=0; i < BTA_HL_NUM_MDLS_PER_MCL ; i ++) - { - p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, i); - if (p_dcb && p_dcb->in_use && p_dcb->p_scb) - { - BTIF_TRACE_DEBUG("found socket for app_idx=%d mcl_id=%d, mdl_idx=%d", app_idx, mcl_idx, i); - btif_hl_set_socket_state (p_dcb->p_scb, BTIF_HL_SOC_STATE_W4_REL); - p_dcb->p_scb = NULL; - found = true; - } + * + * Function btif_hl_release_mcl_sockets + * + * Description Release all sockets on the MCL + * + * Returns void + * + ******************************************************************************/ +void btif_hl_release_mcl_sockets(uint8_t app_idx, uint8_t mcl_idx) { + uint8_t i; + btif_hl_mdl_cb_t* p_dcb; + bool found = false; + BTIF_TRACE_DEBUG("%s", __func__); + for (i = 0; i < BTA_HL_NUM_MDLS_PER_MCL; i++) { + p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, i); + if (p_dcb && p_dcb->in_use && p_dcb->p_scb) { + BTIF_TRACE_DEBUG("found socket for app_idx=%d mcl_id=%d, mdl_idx=%d", + app_idx, mcl_idx, i); + btif_hl_set_socket_state(p_dcb->p_scb, BTIF_HL_SOC_STATE_W4_REL); + p_dcb->p_scb = NULL; + found = true; } - if (found) - btif_hl_select_close_connected(); + } + if (found) btif_hl_select_close_connected(); } /******************************************************************************* -** -** Function btif_hl_release_socket -** -** Description release a specified socket -** -** Returns void -** -*******************************************************************************/ -void btif_hl_release_socket(uint8_t app_idx, uint8_t mcl_idx, uint8_t mdl_idx){ - btif_hl_soc_cb_t *p_scb = NULL; - btif_hl_mdl_cb_t *p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); - - BTIF_TRACE_DEBUG("%s", __func__); - BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=%d mdl_idx=%d", app_idx, mcl_idx, mdl_idx ); - - if (p_dcb && p_dcb->p_scb) - { - p_scb = p_dcb->p_scb; - btif_hl_set_socket_state(p_scb, BTIF_HL_SOC_STATE_W4_REL); - p_dcb->p_scb = NULL; - btif_hl_select_close_connected(); - } + * + * Function btif_hl_release_socket + * + * Description release a specified socket + * + * Returns void + * + ******************************************************************************/ +void btif_hl_release_socket(uint8_t app_idx, uint8_t mcl_idx, uint8_t mdl_idx) { + btif_hl_soc_cb_t* p_scb = NULL; + btif_hl_mdl_cb_t* p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); + + BTIF_TRACE_DEBUG("%s", __func__); + BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=%d mdl_idx=%d", app_idx, mcl_idx, + mdl_idx); + + if (p_dcb && p_dcb->p_scb) { + p_scb = p_dcb->p_scb; + btif_hl_set_socket_state(p_scb, BTIF_HL_SOC_STATE_W4_REL); + p_dcb->p_scb = NULL; + btif_hl_select_close_connected(); + } } /******************************************************************************* -** -** Function btif_hl_create_socket -** -** Description create a socket -** -** Returns bool -** -*******************************************************************************/ -bool btif_hl_create_socket(uint8_t app_idx, uint8_t mcl_idx, uint8_t mdl_idx){ - btif_hl_mcl_cb_t *p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); - btif_hl_mdl_cb_t *p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); - bool status = false; - - BTIF_TRACE_DEBUG("%s", __func__); - - if (p_dcb) { - btif_hl_soc_cb_t *p_scb = - (btif_hl_soc_cb_t *)osi_malloc(sizeof(btif_hl_soc_cb_t)); - if (socketpair(AF_UNIX, SOCK_STREAM, 0, p_scb->socket_id) >= 0) { - BTIF_TRACE_DEBUG("socket id[0]=%d id[1]=%d",p_scb->socket_id[0], p_scb->socket_id[1] ); - p_dcb->p_scb = p_scb; - p_scb->app_idx = app_idx; - p_scb->mcl_idx = mcl_idx; - p_scb->mdl_idx = mdl_idx; - p_scb->channel_id = p_dcb->channel_id; - p_scb->mdep_cfg_idx = p_dcb->local_mdep_cfg_idx; - memcpy(p_scb->bd_addr, p_mcb->bd_addr,sizeof(BD_ADDR)); - btif_hl_set_socket_state(p_scb, BTIF_HL_SOC_STATE_W4_ADD); - p_scb->max_s = p_scb->socket_id[1]; - list_append(soc_queue, (void *)p_scb); - btif_hl_select_wakeup(); - status = true; - } else { - osi_free_and_reset((void **)&p_scb); - } + * + * Function btif_hl_create_socket + * + * Description create a socket + * + * Returns bool + * + ******************************************************************************/ +bool btif_hl_create_socket(uint8_t app_idx, uint8_t mcl_idx, uint8_t mdl_idx) { + btif_hl_mcl_cb_t* p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx); + btif_hl_mdl_cb_t* p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx); + bool status = false; + + BTIF_TRACE_DEBUG("%s", __func__); + + if (p_dcb) { + btif_hl_soc_cb_t* p_scb = + (btif_hl_soc_cb_t*)osi_malloc(sizeof(btif_hl_soc_cb_t)); + if (socketpair(AF_UNIX, SOCK_STREAM, 0, p_scb->socket_id) >= 0) { + BTIF_TRACE_DEBUG("socket id[0]=%d id[1]=%d", p_scb->socket_id[0], + p_scb->socket_id[1]); + p_dcb->p_scb = p_scb; + p_scb->app_idx = app_idx; + p_scb->mcl_idx = mcl_idx; + p_scb->mdl_idx = mdl_idx; + p_scb->channel_id = p_dcb->channel_id; + p_scb->mdep_cfg_idx = p_dcb->local_mdep_cfg_idx; + memcpy(p_scb->bd_addr, p_mcb->bd_addr, sizeof(BD_ADDR)); + btif_hl_set_socket_state(p_scb, BTIF_HL_SOC_STATE_W4_ADD); + p_scb->max_s = p_scb->socket_id[1]; + list_append(soc_queue, (void*)p_scb); + btif_hl_select_wakeup(); + status = true; + } else { + osi_free_and_reset((void**)&p_scb); } + } - BTIF_TRACE_DEBUG("%s status=%d", __func__, status); - return status; + BTIF_TRACE_DEBUG("%s status=%d", __func__, status); + return status; } /******************************************************************************* -** -** Function btif_hl_add_socket_to_set -** -** Description Add a socket -** -** Returns void -** -*******************************************************************************/ -void btif_hl_add_socket_to_set(fd_set *p_org_set) { - btif_hl_mdl_cb_t *p_dcb = NULL; - btif_hl_mcl_cb_t *p_mcb = NULL; - btif_hl_app_cb_t *p_acb = NULL; - btif_hl_evt_cb_t evt_param; - bt_status_t status; - int len; - - BTIF_TRACE_DEBUG("entering %s",__func__); - - for (const list_node_t *node = list_begin(soc_queue); - node != list_end(soc_queue); node = list_next(node)) { - btif_hl_soc_cb_t *p_scb = (btif_hl_soc_cb_t *)list_node(node); - - BTIF_TRACE_DEBUG("btif_hl_add_socket_to_set first p_scb=0x%x", p_scb); - if (btif_hl_get_socket_state(p_scb) == BTIF_HL_SOC_STATE_W4_ADD) { - btif_hl_set_socket_state(p_scb, BTIF_HL_SOC_STATE_W4_READ); - FD_SET(p_scb->socket_id[1], p_org_set); - BTIF_TRACE_DEBUG("found and set socket_id=%d is_set=%d", - p_scb->socket_id[1], FD_ISSET(p_scb->socket_id[1], p_org_set)); - p_mcb = BTIF_HL_GET_MCL_CB_PTR(p_scb->app_idx, p_scb->mcl_idx); - p_dcb = BTIF_HL_GET_MDL_CB_PTR(p_scb->app_idx, p_scb->mcl_idx, p_scb->mdl_idx); - p_acb = BTIF_HL_GET_APP_CB_PTR(p_scb->app_idx); - if (p_mcb && p_dcb) { - btif_hl_stop_timer_using_handle(p_mcb->mcl_handle); - evt_param.chan_cb.app_id = p_acb->app_id; - memcpy(evt_param.chan_cb.bd_addr, p_mcb->bd_addr, sizeof(BD_ADDR)); - evt_param.chan_cb.channel_id = p_dcb->channel_id; - evt_param.chan_cb.fd = p_scb->socket_id[0]; - evt_param.chan_cb.mdep_cfg_index = (int ) p_dcb->local_mdep_cfg_idx; - evt_param.chan_cb.cb_state = BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING; - len = sizeof(btif_hl_send_chan_state_cb_t); - status = btif_transfer_context (btif_hl_proc_cb_evt, BTIF_HL_SEND_CONNECTED_CB, - (char*) &evt_param, len, NULL); - ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); - } - } + * + * Function btif_hl_add_socket_to_set + * + * Description Add a socket + * + * Returns void + * + ******************************************************************************/ +void btif_hl_add_socket_to_set(fd_set* p_org_set) { + btif_hl_mdl_cb_t* p_dcb = NULL; + btif_hl_mcl_cb_t* p_mcb = NULL; + btif_hl_app_cb_t* p_acb = NULL; + btif_hl_evt_cb_t evt_param; + bt_status_t status; + int len; + + BTIF_TRACE_DEBUG("entering %s", __func__); + + for (const list_node_t* node = list_begin(soc_queue); + node != list_end(soc_queue); node = list_next(node)) { + btif_hl_soc_cb_t* p_scb = (btif_hl_soc_cb_t*)list_node(node); + + BTIF_TRACE_DEBUG("btif_hl_add_socket_to_set first p_scb=0x%x", p_scb); + if (btif_hl_get_socket_state(p_scb) == BTIF_HL_SOC_STATE_W4_ADD) { + btif_hl_set_socket_state(p_scb, BTIF_HL_SOC_STATE_W4_READ); + FD_SET(p_scb->socket_id[1], p_org_set); + BTIF_TRACE_DEBUG("found and set socket_id=%d is_set=%d", + p_scb->socket_id[1], + FD_ISSET(p_scb->socket_id[1], p_org_set)); + p_mcb = BTIF_HL_GET_MCL_CB_PTR(p_scb->app_idx, p_scb->mcl_idx); + p_dcb = BTIF_HL_GET_MDL_CB_PTR(p_scb->app_idx, p_scb->mcl_idx, + p_scb->mdl_idx); + p_acb = BTIF_HL_GET_APP_CB_PTR(p_scb->app_idx); + if (p_mcb && p_dcb) { + btif_hl_stop_timer_using_handle(p_mcb->mcl_handle); + evt_param.chan_cb.app_id = p_acb->app_id; + memcpy(evt_param.chan_cb.bd_addr, p_mcb->bd_addr, sizeof(BD_ADDR)); + evt_param.chan_cb.channel_id = p_dcb->channel_id; + evt_param.chan_cb.fd = p_scb->socket_id[0]; + evt_param.chan_cb.mdep_cfg_index = (int)p_dcb->local_mdep_cfg_idx; + evt_param.chan_cb.cb_state = BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING; + len = sizeof(btif_hl_send_chan_state_cb_t); + status = btif_transfer_context(btif_hl_proc_cb_evt, + BTIF_HL_SEND_CONNECTED_CB, + (char*)&evt_param, len, NULL); + ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); + } } - BTIF_TRACE_DEBUG("leaving %s",__func__); + } + BTIF_TRACE_DEBUG("leaving %s", __func__); } /******************************************************************************* -** -** Function btif_hl_close_socket -** -** Description close a socket -** -** Returns void -** -*******************************************************************************/ -void btif_hl_close_socket( fd_set *p_org_set){ - BTIF_TRACE_DEBUG("entering %s",__func__); - for (const list_node_t *node = list_begin(soc_queue); - node != list_end(soc_queue); node = list_next(node)) { - btif_hl_soc_cb_t *p_scb = (btif_hl_soc_cb_t *)list_node(node); - if (btif_hl_get_socket_state(p_scb) == BTIF_HL_SOC_STATE_W4_REL) { - BTIF_TRACE_DEBUG("app_idx=%d mcl_id=%d, mdl_idx=%d", - p_scb->app_idx, p_scb->mcl_idx, p_scb->mdl_idx); - btif_hl_set_socket_state(p_scb, BTIF_HL_SOC_STATE_IDLE); - if (p_scb->socket_id[1] != -1) { - FD_CLR(p_scb->socket_id[1] , p_org_set); - shutdown(p_scb->socket_id[1], SHUT_RDWR); - close(p_scb->socket_id[1]); - - btif_hl_evt_cb_t evt_param; - evt_param.chan_cb.app_id = (int) btif_hl_get_app_id(p_scb->channel_id); - memcpy(evt_param.chan_cb.bd_addr, p_scb->bd_addr, sizeof(BD_ADDR)); - evt_param.chan_cb.channel_id = p_scb->channel_id; - evt_param.chan_cb.fd = p_scb->socket_id[0]; - evt_param.chan_cb.mdep_cfg_index = (int ) p_scb->mdep_cfg_idx; - evt_param.chan_cb.cb_state = BTIF_HL_CHAN_CB_STATE_DISCONNECTED_PENDING; - int len = sizeof(btif_hl_send_chan_state_cb_t); - bt_status_t status = btif_transfer_context (btif_hl_proc_cb_evt, - BTIF_HL_SEND_DISCONNECTED_CB, - (char*) &evt_param, len, NULL); - ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); - } - } + * + * Function btif_hl_close_socket + * + * Description close a socket + * + * Returns void + * + ******************************************************************************/ +void btif_hl_close_socket(fd_set* p_org_set) { + BTIF_TRACE_DEBUG("entering %s", __func__); + for (const list_node_t* node = list_begin(soc_queue); + node != list_end(soc_queue); node = list_next(node)) { + btif_hl_soc_cb_t* p_scb = (btif_hl_soc_cb_t*)list_node(node); + if (btif_hl_get_socket_state(p_scb) == BTIF_HL_SOC_STATE_W4_REL) { + BTIF_TRACE_DEBUG("app_idx=%d mcl_id=%d, mdl_idx=%d", p_scb->app_idx, + p_scb->mcl_idx, p_scb->mdl_idx); + btif_hl_set_socket_state(p_scb, BTIF_HL_SOC_STATE_IDLE); + if (p_scb->socket_id[1] != -1) { + FD_CLR(p_scb->socket_id[1], p_org_set); + shutdown(p_scb->socket_id[1], SHUT_RDWR); + close(p_scb->socket_id[1]); + + btif_hl_evt_cb_t evt_param; + evt_param.chan_cb.app_id = (int)btif_hl_get_app_id(p_scb->channel_id); + memcpy(evt_param.chan_cb.bd_addr, p_scb->bd_addr, sizeof(BD_ADDR)); + evt_param.chan_cb.channel_id = p_scb->channel_id; + evt_param.chan_cb.fd = p_scb->socket_id[0]; + evt_param.chan_cb.mdep_cfg_index = (int)p_scb->mdep_cfg_idx; + evt_param.chan_cb.cb_state = BTIF_HL_CHAN_CB_STATE_DISCONNECTED_PENDING; + int len = sizeof(btif_hl_send_chan_state_cb_t); + bt_status_t status = btif_transfer_context( + btif_hl_proc_cb_evt, BTIF_HL_SEND_DISCONNECTED_CB, + (char*)&evt_param, len, NULL); + ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); + } } - - for (const list_node_t *node = list_begin(soc_queue); - node != list_end(soc_queue); ) { - // We may mutate this list so we need keep track of - // the current node and only remove items behind. - btif_hl_soc_cb_t *p_scb = (btif_hl_soc_cb_t *)list_node(node); - BTIF_TRACE_DEBUG("p_scb=0x%x", p_scb); - node = list_next(node); - if (btif_hl_get_socket_state(p_scb) == BTIF_HL_SOC_STATE_IDLE) { - btif_hl_mdl_cb_t *p_dcb = BTIF_HL_GET_MDL_CB_PTR(p_scb->app_idx, - p_scb->mcl_idx, p_scb->mdl_idx); - BTIF_TRACE_DEBUG("idle socket app_idx=%d mcl_id=%d, mdl_idx=%d p_dcb->in_use=%d", - p_scb->app_idx, p_scb->mcl_idx, p_scb->mdl_idx, p_dcb->in_use); - list_remove(soc_queue, p_scb); - osi_free(p_scb); - p_dcb->p_scb = NULL; - } + } + + for (const list_node_t* node = list_begin(soc_queue); + node != list_end(soc_queue);) { + // We may mutate this list so we need keep track of + // the current node and only remove items behind. + btif_hl_soc_cb_t* p_scb = (btif_hl_soc_cb_t*)list_node(node); + BTIF_TRACE_DEBUG("p_scb=0x%x", p_scb); + node = list_next(node); + if (btif_hl_get_socket_state(p_scb) == BTIF_HL_SOC_STATE_IDLE) { + btif_hl_mdl_cb_t* p_dcb = BTIF_HL_GET_MDL_CB_PTR( + p_scb->app_idx, p_scb->mcl_idx, p_scb->mdl_idx); + BTIF_TRACE_DEBUG( + "idle socket app_idx=%d mcl_id=%d, mdl_idx=%d p_dcb->in_use=%d", + p_scb->app_idx, p_scb->mcl_idx, p_scb->mdl_idx, p_dcb->in_use); + list_remove(soc_queue, p_scb); + osi_free(p_scb); + p_dcb->p_scb = NULL; } - BTIF_TRACE_DEBUG("leaving %s",__func__); + } + BTIF_TRACE_DEBUG("leaving %s", __func__); } /******************************************************************************* -** -** Function btif_hl_select_wakeup_callback -** -** Description Select wakup callback to add or close a socket -** -** Returns void -** -*******************************************************************************/ - -void btif_hl_select_wakeup_callback( fd_set *p_org_set , int wakeup_signal){ - BTIF_TRACE_DEBUG("entering %s wakeup_signal=0x%04x",__func__, wakeup_signal); - - if (wakeup_signal == btif_hl_signal_select_wakeup ) - { - btif_hl_add_socket_to_set(p_org_set); - } - else if (wakeup_signal == btif_hl_signal_select_close_connected) - { - btif_hl_close_socket(p_org_set); - } - BTIF_TRACE_DEBUG("leaving %s",__func__); + * + * Function btif_hl_select_wakeup_callback + * + * Description Select wakup callback to add or close a socket + * + * Returns void + * + ******************************************************************************/ + +void btif_hl_select_wakeup_callback(fd_set* p_org_set, int wakeup_signal) { + BTIF_TRACE_DEBUG("entering %s wakeup_signal=0x%04x", __func__, wakeup_signal); + + if (wakeup_signal == btif_hl_signal_select_wakeup) { + btif_hl_add_socket_to_set(p_org_set); + } else if (wakeup_signal == btif_hl_signal_select_close_connected) { + btif_hl_close_socket(p_org_set); + } + BTIF_TRACE_DEBUG("leaving %s", __func__); } /******************************************************************************* -** -** Function btif_hl_select_monitor_callback -** -** Description Select monitor callback to check pending socket actions -** -** Returns void -** -*******************************************************************************/ -void btif_hl_select_monitor_callback(fd_set *p_cur_set ,fd_set *p_org_set) { - UNUSED(p_org_set); - - BTIF_TRACE_DEBUG("entering %s",__func__); - - for (const list_node_t *node = list_begin(soc_queue); - node != list_end(soc_queue); node = list_next(node)) { - btif_hl_soc_cb_t *p_scb = (btif_hl_soc_cb_t *)list_node(node); - if (btif_hl_get_socket_state(p_scb) == BTIF_HL_SOC_STATE_W4_READ) { - if (FD_ISSET(p_scb->socket_id[1], p_cur_set)) { - BTIF_TRACE_DEBUG("read data state= BTIF_HL_SOC_STATE_W4_READ"); - btif_hl_mdl_cb_t *p_dcb = BTIF_HL_GET_MDL_CB_PTR(p_scb->app_idx, - p_scb->mcl_idx, p_scb->mdl_idx); - assert(p_dcb != NULL); - if (p_dcb->p_tx_pkt) { - BTIF_TRACE_ERROR("Rcv new pkt but the last pkt is still not been" - " sent tx_size=%d", p_dcb->tx_size); - osi_free_and_reset((void **)&p_dcb->p_tx_pkt); - } - p_dcb->p_tx_pkt = (uint8_t*)osi_malloc(p_dcb->mtu); - ssize_t r; - OSI_NO_INTR(r = recv(p_scb->socket_id[1], p_dcb->p_tx_pkt, - p_dcb->mtu, MSG_DONTWAIT)); - if (r > 0) { - BTIF_TRACE_DEBUG("btif_hl_select_monitor_callback send data r =%d", r); - p_dcb->tx_size = r; - BTIF_TRACE_DEBUG("btif_hl_select_monitor_callback send data tx_size=%d", p_dcb->tx_size ); - BTA_HlSendData(p_dcb->mdl_handle, p_dcb->tx_size); - } else { - BTIF_TRACE_DEBUG("btif_hl_select_monitor_callback receive failed r=%d",r); - BTA_HlDchClose(p_dcb->mdl_handle); - } - } + * + * Function btif_hl_select_monitor_callback + * + * Description Select monitor callback to check pending socket actions + * + * Returns void + * + ******************************************************************************/ +void btif_hl_select_monitor_callback(fd_set* p_cur_set, fd_set* p_org_set) { + UNUSED(p_org_set); + + BTIF_TRACE_DEBUG("entering %s", __func__); + + for (const list_node_t* node = list_begin(soc_queue); + node != list_end(soc_queue); node = list_next(node)) { + btif_hl_soc_cb_t* p_scb = (btif_hl_soc_cb_t*)list_node(node); + if (btif_hl_get_socket_state(p_scb) == BTIF_HL_SOC_STATE_W4_READ) { + if (FD_ISSET(p_scb->socket_id[1], p_cur_set)) { + BTIF_TRACE_DEBUG("read data state= BTIF_HL_SOC_STATE_W4_READ"); + btif_hl_mdl_cb_t* p_dcb = BTIF_HL_GET_MDL_CB_PTR( + p_scb->app_idx, p_scb->mcl_idx, p_scb->mdl_idx); + assert(p_dcb != NULL); + if (p_dcb->p_tx_pkt) { + BTIF_TRACE_ERROR( + "Rcv new pkt but the last pkt is still not been" + " sent tx_size=%d", + p_dcb->tx_size); + osi_free_and_reset((void**)&p_dcb->p_tx_pkt); + } + p_dcb->p_tx_pkt = (uint8_t*)osi_malloc(p_dcb->mtu); + ssize_t r; + OSI_NO_INTR(r = recv(p_scb->socket_id[1], p_dcb->p_tx_pkt, p_dcb->mtu, + MSG_DONTWAIT)); + if (r > 0) { + BTIF_TRACE_DEBUG("btif_hl_select_monitor_callback send data r =%d", + r); + p_dcb->tx_size = r; + BTIF_TRACE_DEBUG( + "btif_hl_select_monitor_callback send data tx_size=%d", + p_dcb->tx_size); + BTA_HlSendData(p_dcb->mdl_handle, p_dcb->tx_size); + } else { + BTIF_TRACE_DEBUG( + "btif_hl_select_monitor_callback receive failed r=%d", r); + BTA_HlDchClose(p_dcb->mdl_handle); } + } } + } - if (list_is_empty(soc_queue)) - BTIF_TRACE_DEBUG("btif_hl_select_monitor_queue is empty"); + if (list_is_empty(soc_queue)) + BTIF_TRACE_DEBUG("btif_hl_select_monitor_queue is empty"); - BTIF_TRACE_DEBUG("leaving %s",__func__); + BTIF_TRACE_DEBUG("leaving %s", __func__); } /******************************************************************************* -** -** Function btif_hl_select_wakeup_init -** -** Description select loop wakup init -** -** Returns int -** -*******************************************************************************/ -static inline int btif_hl_select_wakeup_init(fd_set* set){ - BTIF_TRACE_DEBUG("%s", __func__); - if (signal_fds[0] == -1 && socketpair(AF_UNIX, SOCK_STREAM, 0, signal_fds) < 0) - { - BTIF_TRACE_ERROR("socketpair failed: %s", strerror(errno)); - return -1; - } - - BTIF_TRACE_DEBUG("btif_hl_select_wakeup_init signal_fds[0]=%d signal_fds[1]=%d",signal_fds[0], signal_fds[1] ); - FD_SET(signal_fds[0], set); - - return signal_fds[0]; + * + * Function btif_hl_select_wakeup_init + * + * Description select loop wakup init + * + * Returns int + * + ******************************************************************************/ +static inline int btif_hl_select_wakeup_init(fd_set* set) { + BTIF_TRACE_DEBUG("%s", __func__); + if (signal_fds[0] == -1 && + socketpair(AF_UNIX, SOCK_STREAM, 0, signal_fds) < 0) { + BTIF_TRACE_ERROR("socketpair failed: %s", strerror(errno)); + return -1; + } + + BTIF_TRACE_DEBUG( + "btif_hl_select_wakeup_init signal_fds[0]=%d signal_fds[1]=%d", + signal_fds[0], signal_fds[1]); + FD_SET(signal_fds[0], set); + + return signal_fds[0]; } /******************************************************************************* -** -** Function btif_hl_select_wakeup -** -** Description send a signal to wakupo the select loop -** -** Returns int -** -*******************************************************************************/ -static inline int btif_hl_select_wakeup(void){ - char sig_on = btif_hl_signal_select_wakeup; + * + * Function btif_hl_select_wakeup + * + * Description send a signal to wakupo the select loop + * + * Returns int + * + ******************************************************************************/ +static inline int btif_hl_select_wakeup(void) { + char sig_on = btif_hl_signal_select_wakeup; - BTIF_TRACE_DEBUG("%s", __func__); + BTIF_TRACE_DEBUG("%s", __func__); - ssize_t ret; - OSI_NO_INTR(ret = send(signal_fds[1], &sig_on, sizeof(sig_on), 0)); + ssize_t ret; + OSI_NO_INTR(ret = send(signal_fds[1], &sig_on, sizeof(sig_on), 0)); - return (int)ret; + return (int)ret; } /******************************************************************************* -** -** Function btif_hl_select_close_connected -** -** Description send a signal to close a socket -** -** Returns int -** -*******************************************************************************/ -static inline int btif_hl_select_close_connected(void){ - char sig_on = btif_hl_signal_select_close_connected; + * + * Function btif_hl_select_close_connected + * + * Description send a signal to close a socket + * + * Returns int + * + ******************************************************************************/ +static inline int btif_hl_select_close_connected(void) { + char sig_on = btif_hl_signal_select_close_connected; - BTIF_TRACE_DEBUG("%s", __func__); + BTIF_TRACE_DEBUG("%s", __func__); - ssize_t ret; - OSI_NO_INTR(ret = send(signal_fds[1], &sig_on, sizeof(sig_on), 0)); + ssize_t ret; + OSI_NO_INTR(ret = send(signal_fds[1], &sig_on, sizeof(sig_on), 0)); - return (int)ret; + return (int)ret; } /******************************************************************************* -** -** Function btif_hl_close_select_thread -** -** Description send signal to close the thread and then close all signal FDs -** -** Returns int -** -*******************************************************************************/ -static inline int btif_hl_close_select_thread(void) -{ - ssize_t result = 0; - char sig_on = btif_hl_signal_select_exit; + * + * Function btif_hl_close_select_thread + * + * Description send signal to close the thread and then close all signal FDs + * + * Returns int + * + ******************************************************************************/ +static inline int btif_hl_close_select_thread(void) { + ssize_t result = 0; + char sig_on = btif_hl_signal_select_exit; - BTIF_TRACE_DEBUG("%", __func__); + BTIF_TRACE_DEBUG("%", __func__); - OSI_NO_INTR(result = send(signal_fds[1], &sig_on, sizeof(sig_on), 0)); + OSI_NO_INTR(result = send(signal_fds[1], &sig_on, sizeof(sig_on), 0)); - if (btif_is_enabled()) - { - /* Wait for the select_thread_id to exit if BT is still enabled - and only this profile getting cleaned up*/ - if (select_thread_id != -1) { - pthread_join(select_thread_id, NULL); - select_thread_id = -1; - } + if (btif_is_enabled()) { + /* Wait for the select_thread_id to exit if BT is still enabled + and only this profile getting cleaned up*/ + if (select_thread_id != -1) { + pthread_join(select_thread_id, NULL); + select_thread_id = -1; } - list_free(soc_queue); - soc_queue = NULL; + } + list_free(soc_queue); + soc_queue = NULL; - return (int)result; + return (int)result; } /******************************************************************************* -** -** Function btif_hl_select_wake_reset -** -** Description clear the received signal for the select loop -** -** Returns int -** -*******************************************************************************/ -static inline int btif_hl_select_wake_reset(void){ - char sig_recv = 0; + * + * Function btif_hl_select_wake_reset + * + * Description clear the received signal for the select loop + * + * Returns int + * + ******************************************************************************/ +static inline int btif_hl_select_wake_reset(void) { + char sig_recv = 0; - BTIF_TRACE_DEBUG("%s", __func__); + BTIF_TRACE_DEBUG("%s", __func__); - ssize_t r; - OSI_NO_INTR(r = recv(signal_fds[0], &sig_recv, sizeof(sig_recv), - MSG_WAITALL)); + ssize_t r; + OSI_NO_INTR( + r = recv(signal_fds[0], &sig_recv, sizeof(sig_recv), MSG_WAITALL)); - return (int)sig_recv; + return (int)sig_recv; } /******************************************************************************* -** -** Function btif_hl_select_wake_signaled -** -** Description check whether a fd is set or not -** -** Returns int -** -*******************************************************************************/ -static inline int btif_hl_select_wake_signaled(fd_set* set){ - BTIF_TRACE_DEBUG("btif_hl_select_wake_signaled"); - return FD_ISSET(signal_fds[0], set); + * + * Function btif_hl_select_wake_signaled + * + * Description check whether a fd is set or not + * + * Returns int + * + ******************************************************************************/ +static inline int btif_hl_select_wake_signaled(fd_set* set) { + BTIF_TRACE_DEBUG("btif_hl_select_wake_signaled"); + return FD_ISSET(signal_fds[0], set); } /******************************************************************************* -** -** Function btif_hl_thread_cleanup -** -** Description shut down and clean up the select loop -** -** Returns void -** -*******************************************************************************/ -static void btif_hl_thread_cleanup(){ - if (listen_s != -1) - close(listen_s); - if (connected_s != -1) - { - shutdown(connected_s, SHUT_RDWR); - close(connected_s); - } - listen_s = connected_s = -1; - BTIF_TRACE_DEBUG("hl thread cleanup"); + * + * Function btif_hl_thread_cleanup + * + * Description shut down and clean up the select loop + * + * Returns void + * + ******************************************************************************/ +static void btif_hl_thread_cleanup() { + if (listen_s != -1) close(listen_s); + if (connected_s != -1) { + shutdown(connected_s, SHUT_RDWR); + close(connected_s); + } + listen_s = connected_s = -1; + BTIF_TRACE_DEBUG("hl thread cleanup"); } /******************************************************************************* -** -** Function btif_hl_select_thread -** -** Description the select loop -** -** Returns void -** -*******************************************************************************/ -static void *btif_hl_select_thread(void *arg){ - fd_set org_set, curr_set; - int r, max_curr_s, max_org_s; - UNUSED(arg); - - BTIF_TRACE_DEBUG("entered btif_hl_select_thread"); - FD_ZERO(&org_set); - max_org_s = btif_hl_select_wakeup_init(&org_set); - BTIF_TRACE_DEBUG("max_s=%d ", max_org_s); - - for (;;) - { - r = 0; - BTIF_TRACE_DEBUG("set curr_set = org_set "); - curr_set = org_set; - max_curr_s = max_org_s; - int ret = select((max_curr_s + 1), &curr_set, NULL, NULL, NULL); - BTIF_TRACE_DEBUG("select unblocked ret=%d", ret); - if (ret == -1) - { - if (errno == EINTR) - continue; - BTIF_TRACE_DEBUG("select() ret -1, exit the thread"); - btif_hl_thread_cleanup(); - select_thread_id = -1; - return 0; - } - else if (ret) - { - BTIF_TRACE_DEBUG("btif_hl_select_wake_signaled, signal ret=%d", ret); - if (btif_hl_select_wake_signaled(&curr_set)) - { - r = btif_hl_select_wake_reset(); - BTIF_TRACE_DEBUG("btif_hl_select_wake_signaled, signal:%d", r); - if (r == btif_hl_signal_select_wakeup || r == btif_hl_signal_select_close_connected ) - { - btif_hl_select_wakeup_callback(&org_set, r); - } - else if( r == btif_hl_signal_select_exit) - { - btif_hl_thread_cleanup(); - BTIF_TRACE_DEBUG("Exit hl_select_thread for btif_hl_signal_select_exit"); - return 0; - } - } - - btif_hl_select_monitor_callback(&curr_set, &org_set); - max_org_s = btif_hl_update_maxfd(max_org_s); + * + * Function btif_hl_select_thread + * + * Description the select loop + * + * Returns void + * + ******************************************************************************/ +static void* btif_hl_select_thread(void* arg) { + fd_set org_set, curr_set; + int r, max_curr_s, max_org_s; + UNUSED(arg); + + BTIF_TRACE_DEBUG("entered btif_hl_select_thread"); + FD_ZERO(&org_set); + max_org_s = btif_hl_select_wakeup_init(&org_set); + BTIF_TRACE_DEBUG("max_s=%d ", max_org_s); + + for (;;) { + r = 0; + BTIF_TRACE_DEBUG("set curr_set = org_set "); + curr_set = org_set; + max_curr_s = max_org_s; + int ret = select((max_curr_s + 1), &curr_set, NULL, NULL, NULL); + BTIF_TRACE_DEBUG("select unblocked ret=%d", ret); + if (ret == -1) { + if (errno == EINTR) continue; + BTIF_TRACE_DEBUG("select() ret -1, exit the thread"); + btif_hl_thread_cleanup(); + select_thread_id = -1; + return 0; + } else if (ret) { + BTIF_TRACE_DEBUG("btif_hl_select_wake_signaled, signal ret=%d", ret); + if (btif_hl_select_wake_signaled(&curr_set)) { + r = btif_hl_select_wake_reset(); + BTIF_TRACE_DEBUG("btif_hl_select_wake_signaled, signal:%d", r); + if (r == btif_hl_signal_select_wakeup || + r == btif_hl_signal_select_close_connected) { + btif_hl_select_wakeup_callback(&org_set, r); + } else if (r == btif_hl_signal_select_exit) { + btif_hl_thread_cleanup(); + BTIF_TRACE_DEBUG( + "Exit hl_select_thread for btif_hl_signal_select_exit"); + return 0; } - else - BTIF_TRACE_DEBUG("no data, select ret: %d\n", ret); - } - BTIF_TRACE_DEBUG("leaving hl_select_thread"); - return 0; + } + + btif_hl_select_monitor_callback(&curr_set, &org_set); + max_org_s = btif_hl_update_maxfd(max_org_s); + } else + BTIF_TRACE_DEBUG("no data, select ret: %d\n", ret); + } + BTIF_TRACE_DEBUG("leaving hl_select_thread"); + return 0; } /******************************************************************************* -** -** Function create_thread -** -** Description creat a select loop -** -** Returns pthread_t -** -*******************************************************************************/ -static inline pthread_t create_thread(void *(*start_routine)(void *), void * arg){ - BTIF_TRACE_DEBUG("create_thread: entered"); - pthread_attr_t thread_attr; - - pthread_attr_init(&thread_attr); - pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE); - pthread_t thread_id = -1; - if ( pthread_create(&thread_id, &thread_attr, start_routine, arg)!=0 ) - { - BTIF_TRACE_ERROR("pthread_create : %s", strerror(errno)); - return -1; - } - BTIF_TRACE_DEBUG("create_thread: thread created successfully"); - return thread_id; + * + * Function create_thread + * + * Description creat a select loop + * + * Returns pthread_t + * + ******************************************************************************/ +static inline pthread_t create_thread(void* (*start_routine)(void*), + void* arg) { + BTIF_TRACE_DEBUG("create_thread: entered"); + pthread_attr_t thread_attr; + + pthread_attr_init(&thread_attr); + pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE); + pthread_t thread_id = -1; + if (pthread_create(&thread_id, &thread_attr, start_routine, arg) != 0) { + BTIF_TRACE_ERROR("pthread_create : %s", strerror(errno)); + return -1; + } + BTIF_TRACE_DEBUG("create_thread: thread created successfully"); + return thread_id; } /******************************************************************************* -** -** Function btif_hl_soc_thread_init -** -** Description HL select loop init function. -** -** Returns void -** -*******************************************************************************/ -void btif_hl_soc_thread_init(void){ - BTIF_TRACE_DEBUG("%s", __func__); - soc_queue = list_new(NULL); - if (soc_queue == NULL) - LOG_ERROR(LOG_TAG, "%s unable to allocate resources for thread", __func__); - select_thread_id = create_thread(btif_hl_select_thread, NULL); + * + * Function btif_hl_soc_thread_init + * + * Description HL select loop init function. + * + * Returns void + * + ******************************************************************************/ +void btif_hl_soc_thread_init(void) { + BTIF_TRACE_DEBUG("%s", __func__); + soc_queue = list_new(NULL); + if (soc_queue == NULL) + LOG_ERROR(LOG_TAG, "%s unable to allocate resources for thread", __func__); + select_thread_id = create_thread(btif_hl_select_thread, NULL); } /******************************************************************************* -** -** Function btif_hl_load_mdl_config -** -** Description load the MDL configuation from the application control block -** -** Returns bool -** -*******************************************************************************/ -bool btif_hl_load_mdl_config (uint8_t app_id, uint8_t buffer_size, - tBTA_HL_MDL_CFG *p_mdl_buf ){ - uint8_t app_idx; - bool result = false; - btif_hl_app_cb_t *p_acb; - tBTA_HL_MDL_CFG *p; - int i; - BTIF_TRACE_DEBUG("%s", __func__); - - if (btif_hl_find_app_idx(app_id, &app_idx)) - { - p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); - for (i=0, p=p_mdl_buf; imdl_cfg[i].base, sizeof(tBTA_HL_MDL_CFG)); - } - result = true; + * + * Function btif_hl_load_mdl_config + * + * Description load the MDL configuation from the application control block + * + * Returns bool + * + ******************************************************************************/ +bool btif_hl_load_mdl_config(uint8_t app_id, uint8_t buffer_size, + tBTA_HL_MDL_CFG* p_mdl_buf) { + uint8_t app_idx; + bool result = false; + btif_hl_app_cb_t* p_acb; + tBTA_HL_MDL_CFG* p; + int i; + BTIF_TRACE_DEBUG("%s", __func__); + + if (btif_hl_find_app_idx(app_id, &app_idx)) { + p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx); + for (i = 0, p = p_mdl_buf; i < buffer_size; i++, p++) { + memcpy(p, &p_acb->mdl_cfg[i].base, sizeof(tBTA_HL_MDL_CFG)); } + result = true; + } - BTIF_TRACE_DEBUG("result=%d", result); - return result; + BTIF_TRACE_DEBUG("result=%d", result); + return result; } diff --git a/btif/src/btif_mce.cc b/btif/src/btif_mce.cc index e452c693d..8bb4af764 100644 --- a/btif/src/btif_mce.cc +++ b/btif/src/btif_mce.cc @@ -43,138 +43,126 @@ #include "btif_util.h" /***************************************************************************** -** Static variables -******************************************************************************/ + * Static variables + *****************************************************************************/ -static btmce_callbacks_t *bt_mce_callbacks = NULL; +static btmce_callbacks_t* bt_mce_callbacks = NULL; -static void btif_mce_mas_discovery_comp_evt(uint16_t event, char *p_param) -{ - tBTA_MCE_MAS_DISCOVERY_COMP *evt_data = (tBTA_MCE_MAS_DISCOVERY_COMP*) p_param; - btmce_mas_instance_t insts[BTA_MCE_MAX_MAS_INSTANCES]; - bt_bdaddr_t addr; - int i; +static void btif_mce_mas_discovery_comp_evt(uint16_t event, char* p_param) { + tBTA_MCE_MAS_DISCOVERY_COMP* evt_data = (tBTA_MCE_MAS_DISCOVERY_COMP*)p_param; + btmce_mas_instance_t insts[BTA_MCE_MAX_MAS_INSTANCES]; + bt_bdaddr_t addr; + int i; - BTIF_TRACE_EVENT("%s: event = %d", __func__, event); + BTIF_TRACE_EVENT("%s: event = %d", __func__, event); - if (event != BTA_MCE_MAS_DISCOVERY_COMP_EVT) - return; + if (event != BTA_MCE_MAS_DISCOVERY_COMP_EVT) return; - for (i = 0; i < evt_data->num_mas; i++) - { - insts[i].id = evt_data->mas[i].instance_id; - insts[i].scn = evt_data->mas[i].scn; - insts[i].msg_types = evt_data->mas[i].msg_type; - insts[i].p_name = evt_data->mas[i].p_srv_name; - } + for (i = 0; i < evt_data->num_mas; i++) { + insts[i].id = evt_data->mas[i].instance_id; + insts[i].scn = evt_data->mas[i].scn; + insts[i].msg_types = evt_data->mas[i].msg_type; + insts[i].p_name = evt_data->mas[i].p_srv_name; + } - bdcpy(addr.address, evt_data->remote_addr); + bdcpy(addr.address, evt_data->remote_addr); - HAL_CBACK(bt_mce_callbacks, remote_mas_instances_cb, (bt_status_t)evt_data->status, &addr, evt_data->num_mas, insts); + HAL_CBACK(bt_mce_callbacks, remote_mas_instances_cb, + (bt_status_t)evt_data->status, &addr, evt_data->num_mas, insts); } -static void mas_discovery_comp_copy_cb(uint16_t event, char *p_dest, char *p_src) -{ - tBTA_MCE_MAS_DISCOVERY_COMP *p_dest_data = (tBTA_MCE_MAS_DISCOVERY_COMP *) p_dest; - tBTA_MCE_MAS_DISCOVERY_COMP *p_src_data = (tBTA_MCE_MAS_DISCOVERY_COMP *) p_src; - char *p_dest_str; - int i; +static void mas_discovery_comp_copy_cb(uint16_t event, char* p_dest, + char* p_src) { + tBTA_MCE_MAS_DISCOVERY_COMP* p_dest_data = + (tBTA_MCE_MAS_DISCOVERY_COMP*)p_dest; + tBTA_MCE_MAS_DISCOVERY_COMP* p_src_data = (tBTA_MCE_MAS_DISCOVERY_COMP*)p_src; + char* p_dest_str; + int i; - if (!p_src) - return; + if (!p_src) return; - if (event != BTA_MCE_MAS_DISCOVERY_COMP_EVT) - return; + if (event != BTA_MCE_MAS_DISCOVERY_COMP_EVT) return; - maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data)); + maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data)); - p_dest_str = p_dest + sizeof(tBTA_MCE_MAS_DISCOVERY_COMP); + p_dest_str = p_dest + sizeof(tBTA_MCE_MAS_DISCOVERY_COMP); - for (i = 0; i < p_src_data->num_mas; i++) - { - p_dest_data->mas[i].p_srv_name = p_dest_str; - memcpy(p_dest_str, p_src_data->mas[i].p_srv_name, p_src_data->mas[i].srv_name_len); - p_dest_str += p_src_data->mas[i].srv_name_len; - *(p_dest_str++) = '\0'; - } + for (i = 0; i < p_src_data->num_mas; i++) { + p_dest_data->mas[i].p_srv_name = p_dest_str; + memcpy(p_dest_str, p_src_data->mas[i].p_srv_name, + p_src_data->mas[i].srv_name_len); + p_dest_str += p_src_data->mas[i].srv_name_len; + *(p_dest_str++) = '\0'; + } } -static void mce_dm_cback(tBTA_MCE_EVT event, tBTA_MCE *p_data, void *user_data) -{ - switch (event) - { - case BTA_MCE_MAS_DISCOVERY_COMP_EVT: - { - int i; - uint16_t param_len = sizeof(tBTA_MCE); - - /* include space for all p_srv_name copies including null-termination */ - for (i = 0; i < p_data->mas_disc_comp.num_mas; i++) - param_len += (p_data->mas_disc_comp.mas[i].srv_name_len + 1); - - /* need to deepy copy p_srv_name and null-terminate */ - btif_transfer_context(btif_mce_mas_discovery_comp_evt, event, - (char*)p_data, param_len, mas_discovery_comp_copy_cb); - - break; - } +static void mce_dm_cback(tBTA_MCE_EVT event, tBTA_MCE* p_data, + void* user_data) { + switch (event) { + case BTA_MCE_MAS_DISCOVERY_COMP_EVT: { + int i; + uint16_t param_len = sizeof(tBTA_MCE); + + /* include space for all p_srv_name copies including null-termination */ + for (i = 0; i < p_data->mas_disc_comp.num_mas; i++) + param_len += (p_data->mas_disc_comp.mas[i].srv_name_len + 1); + + /* need to deepy copy p_srv_name and null-terminate */ + btif_transfer_context(btif_mce_mas_discovery_comp_evt, event, + (char*)p_data, param_len, + mas_discovery_comp_copy_cb); + + break; } + } } -static bt_status_t init(btmce_callbacks_t* callbacks) -{ - BTIF_TRACE_EVENT("%s", __func__); +static bt_status_t init(btmce_callbacks_t* callbacks) { + BTIF_TRACE_EVENT("%s", __func__); - bt_mce_callbacks = callbacks; + bt_mce_callbacks = callbacks; - btif_enable_service(BTA_MAP_SERVICE_ID); + btif_enable_service(BTA_MAP_SERVICE_ID); - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } -static bt_status_t get_remote_mas_instances(bt_bdaddr_t *bd_addr) -{ - bdstr_t bdstr; +static bt_status_t get_remote_mas_instances(bt_bdaddr_t* bd_addr) { + bdstr_t bdstr; - BTIF_TRACE_EVENT("%s: remote_addr=%s", __func__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr))); + BTIF_TRACE_EVENT("%s: remote_addr=%s", __func__, + bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr))); - BTA_MceGetRemoteMasInstances(bd_addr->address); + BTA_MceGetRemoteMasInstances(bd_addr->address); - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } static const btmce_interface_t mce_if = { - sizeof(btmce_interface_t), - init, - get_remote_mas_instances, + sizeof(btmce_interface_t), init, get_remote_mas_instances, }; -const btmce_interface_t *btif_mce_get_interface(void) -{ - BTIF_TRACE_EVENT("%s", __func__); - return &mce_if; +const btmce_interface_t* btif_mce_get_interface(void) { + BTIF_TRACE_EVENT("%s", __func__); + return &mce_if; } /******************************************************************************* -** -** Function btif_mce_execute_service -** -** Description Initializes/Shuts down the service -** -** Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_mce_execute_service(bool b_enable) -{ - BTIF_TRACE_EVENT("%s enable:%d", __func__, b_enable); - - if (b_enable) - { - BTA_MceEnable(mce_dm_cback); - } - else - { - /* This is called on BT disable so no need to extra cleanup */ - } - return BT_STATUS_SUCCESS; + * + * Function btif_mce_execute_service + * + * Description Initializes/Shuts down the service + * + * Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_mce_execute_service(bool b_enable) { + BTIF_TRACE_EVENT("%s enable:%d", __func__, b_enable); + + if (b_enable) { + BTA_MceEnable(mce_dm_cback); + } else { + /* This is called on BT disable so no need to extra cleanup */ + } + return BT_STATUS_SUCCESS; } diff --git a/btif/src/btif_pan.cc b/btif/src/btif_pan.cc index 960edaa60..5322e2c58 100644 --- a/btif/src/btif_pan.cc +++ b/btif/src/btif_pan.cc @@ -40,7 +40,6 @@ #include #include #include -#include #include #include #include @@ -52,6 +51,7 @@ #include #include +#include "bt_common.h" #include "bta_api.h" #include "bta_pan_api.h" #include "btcore/include/bdaddr.h" @@ -62,14 +62,13 @@ #include "btif_util.h" #include "btm_api.h" #include "device/include/controller.h" -#include "bt_common.h" #include "osi/include/log.h" #include "osi/include/osi.h" -#define FORWARD_IGNORE 1 -#define FORWARD_SUCCESS 0 -#define FORWARD_FAILURE (-1) -#define FORWARD_CONGEST (-2) +#define FORWARD_IGNORE 1 +#define FORWARD_SUCCESS 0 +#define FORWARD_FAILURE (-1) +#define FORWARD_CONGEST (-2) #if (PAN_NAP_DISABLED == TRUE && PANU_DISABLED == TRUE) #define BTPAN_LOCAL_ROLE BTPAN_ROLE_NONE @@ -81,7 +80,10 @@ #define BTPAN_LOCAL_ROLE (BTPAN_ROLE_PANU | BTPAN_ROLE_PANNAP) #endif -#define asrt(s) if (!(s)) BTIF_TRACE_ERROR("btif_pan: ## %s assert %s failed at line:%d ##",__func__, #s, __LINE__) +#define asrt(s) \ + if (!(s)) \ + BTIF_TRACE_ERROR("btif_pan: ## %s assert %s failed at line:%d ##", __func__, \ + #s, __LINE__) #define MIN(x, y) (((x) < (y)) ? (x) : (y)) @@ -92,30 +94,23 @@ static bool stack_initialized; static bt_status_t btpan_jni_init(const btpan_callbacks_t* callbacks); static void btpan_jni_cleanup(); -static bt_status_t btpan_connect(const bt_bdaddr_t *bd_addr, int local_role, int remote_role); -static bt_status_t btpan_disconnect(const bt_bdaddr_t *bd_addr); +static bt_status_t btpan_connect(const bt_bdaddr_t* bd_addr, int local_role, + int remote_role); +static bt_status_t btpan_disconnect(const bt_bdaddr_t* bd_addr); static bt_status_t btpan_enable(int local_role); static int btpan_get_local_role(void); -static void btpan_tap_fd_signaled(int fd, int type, int flags, uint32_t user_id); +static void btpan_tap_fd_signaled(int fd, int type, int flags, + uint32_t user_id); static void btpan_cleanup_conn(btpan_conn_t* conn); -static void bta_pan_callback(tBTA_PAN_EVT event, tBTA_PAN *p_data); -static void btu_exec_tap_fd_read(void *p_param); +static void bta_pan_callback(tBTA_PAN_EVT event, tBTA_PAN* p_data); +static void btu_exec_tap_fd_read(void* p_param); static btpan_interface_t pan_if = { - sizeof(pan_if), - btpan_jni_init, - btpan_enable, - btpan_get_local_role, - btpan_connect, - btpan_disconnect, - btpan_jni_cleanup -}; + sizeof(pan_if), btpan_jni_init, btpan_enable, btpan_get_local_role, + btpan_connect, btpan_disconnect, btpan_jni_cleanup}; -btpan_interface_t *btif_pan_get_interface() -{ - return &pan_if; -} +btpan_interface_t* btif_pan_get_interface() { return &pan_if; } /******************************************************************************* ** @@ -126,703 +121,662 @@ btpan_interface_t *btif_pan_get_interface() ** Returns bt_status_t ** *******************************************************************************/ -void btif_pan_init() -{ - BTIF_TRACE_DEBUG("jni_initialized = %d, btpan_cb.enabled:%d", jni_initialized, btpan_cb.enabled); - stack_initialized = true; - - if (jni_initialized && !btpan_cb.enabled) - { - BTIF_TRACE_DEBUG("Enabling PAN...."); - memset(&btpan_cb, 0, sizeof(btpan_cb)); - btpan_cb.tap_fd = INVALID_FD; - btpan_cb.flow = 1; - for (int i = 0; i < MAX_PAN_CONNS; i++) - btpan_cleanup_conn(&btpan_cb.conns[i]); - BTA_PanEnable(bta_pan_callback); - btpan_cb.enabled = 1; - btpan_enable(BTPAN_LOCAL_ROLE); - } -} - -static void pan_disable() -{ - if (btpan_cb.enabled) - { - btpan_cb.enabled = 0; - BTA_PanDisable(); - if (btpan_cb.tap_fd != INVALID_FD) - { - btpan_tap_close(btpan_cb.tap_fd); - btpan_cb.tap_fd = INVALID_FD; - } +void btif_pan_init() { + BTIF_TRACE_DEBUG("jni_initialized = %d, btpan_cb.enabled:%d", jni_initialized, + btpan_cb.enabled); + stack_initialized = true; + + if (jni_initialized && !btpan_cb.enabled) { + BTIF_TRACE_DEBUG("Enabling PAN...."); + memset(&btpan_cb, 0, sizeof(btpan_cb)); + btpan_cb.tap_fd = INVALID_FD; + btpan_cb.flow = 1; + for (int i = 0; i < MAX_PAN_CONNS; i++) + btpan_cleanup_conn(&btpan_cb.conns[i]); + BTA_PanEnable(bta_pan_callback); + btpan_cb.enabled = 1; + btpan_enable(BTPAN_LOCAL_ROLE); + } +} + +static void pan_disable() { + if (btpan_cb.enabled) { + btpan_cb.enabled = 0; + BTA_PanDisable(); + if (btpan_cb.tap_fd != INVALID_FD) { + btpan_tap_close(btpan_cb.tap_fd); + btpan_cb.tap_fd = INVALID_FD; } + } } -void btif_pan_cleanup() -{ - if (!stack_initialized) - return; +void btif_pan_cleanup() { + if (!stack_initialized) return; - // Bluetooth is shuting down, invalidate all BTA PAN handles - for (int i = 0; i < MAX_PAN_CONNS; i++) - btpan_cleanup_conn(&btpan_cb.conns[i]); + // Bluetooth is shuting down, invalidate all BTA PAN handles + for (int i = 0; i < MAX_PAN_CONNS; i++) + btpan_cleanup_conn(&btpan_cb.conns[i]); - pan_disable(); - stack_initialized = false; + pan_disable(); + stack_initialized = false; } static btpan_callbacks_t callback; -static bt_status_t btpan_jni_init(const btpan_callbacks_t* callbacks) -{ - BTIF_TRACE_DEBUG("stack_initialized = %d, btpan_cb.enabled:%d", stack_initialized, btpan_cb.enabled); - callback = *callbacks; - jni_initialized = true; - if (stack_initialized && !btpan_cb.enabled) - btif_pan_init(); - return BT_STATUS_SUCCESS; +static bt_status_t btpan_jni_init(const btpan_callbacks_t* callbacks) { + BTIF_TRACE_DEBUG("stack_initialized = %d, btpan_cb.enabled:%d", + stack_initialized, btpan_cb.enabled); + callback = *callbacks; + jni_initialized = true; + if (stack_initialized && !btpan_cb.enabled) btif_pan_init(); + return BT_STATUS_SUCCESS; } -static void btpan_jni_cleanup() -{ - pan_disable(); - jni_initialized = false; +static void btpan_jni_cleanup() { + pan_disable(); + jni_initialized = false; } -static inline int bta_role_to_btpan(int bta_pan_role) -{ - int btpan_role = 0; - BTIF_TRACE_DEBUG("bta_pan_role:0x%x", bta_pan_role); - if (bta_pan_role & PAN_ROLE_NAP_SERVER) - btpan_role |= BTPAN_ROLE_PANNAP; - if (bta_pan_role & PAN_ROLE_CLIENT) - btpan_role |= BTPAN_ROLE_PANU; - return btpan_role; +static inline int bta_role_to_btpan(int bta_pan_role) { + int btpan_role = 0; + BTIF_TRACE_DEBUG("bta_pan_role:0x%x", bta_pan_role); + if (bta_pan_role & PAN_ROLE_NAP_SERVER) btpan_role |= BTPAN_ROLE_PANNAP; + if (bta_pan_role & PAN_ROLE_CLIENT) btpan_role |= BTPAN_ROLE_PANU; + return btpan_role; } -static inline int btpan_role_to_bta(int btpan_role) -{ - int bta_pan_role = PAN_ROLE_INACTIVE; - BTIF_TRACE_DEBUG("btpan_role:0x%x", btpan_role); - if (btpan_role & BTPAN_ROLE_PANNAP) - bta_pan_role |= PAN_ROLE_NAP_SERVER; - if (btpan_role & BTPAN_ROLE_PANU) - bta_pan_role |= PAN_ROLE_CLIENT; - return bta_pan_role; +static inline int btpan_role_to_bta(int btpan_role) { + int bta_pan_role = PAN_ROLE_INACTIVE; + BTIF_TRACE_DEBUG("btpan_role:0x%x", btpan_role); + if (btpan_role & BTPAN_ROLE_PANNAP) bta_pan_role |= PAN_ROLE_NAP_SERVER; + if (btpan_role & BTPAN_ROLE_PANU) bta_pan_role |= PAN_ROLE_CLIENT; + return bta_pan_role; } static volatile int btpan_dev_local_role; #if (BTA_PAN_INCLUDED == TRUE) static tBTA_PAN_ROLE_INFO bta_panu_info = {PANU_SERVICE_NAME, 0, PAN_SECURITY}; -static tBTA_PAN_ROLE_INFO bta_pan_nap_info = {PAN_NAP_SERVICE_NAME, 1, PAN_SECURITY}; +static tBTA_PAN_ROLE_INFO bta_pan_nap_info = {PAN_NAP_SERVICE_NAME, 1, + PAN_SECURITY}; #endif -static bt_status_t btpan_enable(int local_role) -{ +static bt_status_t btpan_enable(int local_role) { #if (BTA_PAN_INCLUDED == TRUE) - BTIF_TRACE_DEBUG("%s - local_role: %d", __func__, local_role); - int bta_pan_role = btpan_role_to_bta(local_role); - BTA_PanSetRole(bta_pan_role, &bta_panu_info, NULL, &bta_pan_nap_info); - btpan_dev_local_role = local_role; - return BT_STATUS_SUCCESS; + BTIF_TRACE_DEBUG("%s - local_role: %d", __func__, local_role); + int bta_pan_role = btpan_role_to_bta(local_role); + BTA_PanSetRole(bta_pan_role, &bta_panu_info, NULL, &bta_pan_nap_info); + btpan_dev_local_role = local_role; + return BT_STATUS_SUCCESS; #else - return BT_STATUS_FAIL; + return BT_STATUS_FAIL; #endif } -static int btpan_get_local_role() -{ - BTIF_TRACE_DEBUG("btpan_dev_local_role:%d", btpan_dev_local_role); - return btpan_dev_local_role; -} - -static bt_status_t btpan_connect(const bt_bdaddr_t *bd_addr, int local_role, int remote_role) -{ - BTIF_TRACE_DEBUG("local_role:%d, remote_role:%d", local_role, remote_role); - int bta_local_role = btpan_role_to_bta(local_role); - int bta_remote_role = btpan_role_to_bta(remote_role); - btpan_new_conn(-1, bd_addr->address, bta_local_role, bta_remote_role); - BTA_PanOpen((uint8_t*)bd_addr->address, bta_local_role, bta_remote_role); +static int btpan_get_local_role() { + BTIF_TRACE_DEBUG("btpan_dev_local_role:%d", btpan_dev_local_role); + return btpan_dev_local_role; +} + +static bt_status_t btpan_connect(const bt_bdaddr_t* bd_addr, int local_role, + int remote_role) { + BTIF_TRACE_DEBUG("local_role:%d, remote_role:%d", local_role, remote_role); + int bta_local_role = btpan_role_to_bta(local_role); + int bta_remote_role = btpan_role_to_bta(remote_role); + btpan_new_conn(-1, bd_addr->address, bta_local_role, bta_remote_role); + BTA_PanOpen((uint8_t*)bd_addr->address, bta_local_role, bta_remote_role); + return BT_STATUS_SUCCESS; +} + +static void btif_in_pan_generic_evt(uint16_t event, char* p_param) { + BTIF_TRACE_EVENT("%s: event=%d", __func__, event); + switch (event) { + case BTIF_PAN_CB_DISCONNECTING: { + bt_bdaddr_t* bd_addr = (bt_bdaddr_t*)p_param; + btpan_conn_t* conn = btpan_find_conn_addr(bd_addr->address); + int btpan_conn_local_role; + int btpan_remote_role; + asrt(conn != NULL); + if (conn) { + btpan_conn_local_role = bta_role_to_btpan(conn->local_role); + btpan_remote_role = bta_role_to_btpan(conn->remote_role); + callback.connection_state_cb(BTPAN_STATE_DISCONNECTING, + BT_STATUS_SUCCESS, + (const bt_bdaddr_t*)conn->peer, + btpan_conn_local_role, btpan_remote_role); + } + } break; + default: { + BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __func__, event); + } break; + } +} + +static bt_status_t btpan_disconnect(const bt_bdaddr_t* bd_addr) { + btpan_conn_t* conn = btpan_find_conn_addr(bd_addr->address); + if (conn && conn->handle >= 0) { + /* Inform the application that the disconnect has been initiated + * successfully */ + btif_transfer_context(btif_in_pan_generic_evt, BTIF_PAN_CB_DISCONNECTING, + (char*)bd_addr, sizeof(bt_bdaddr_t), NULL); + BTA_PanClose(conn->handle); return BT_STATUS_SUCCESS; -} - -static void btif_in_pan_generic_evt(uint16_t event, char *p_param) -{ - BTIF_TRACE_EVENT("%s: event=%d", __func__, event); - switch (event) { - case BTIF_PAN_CB_DISCONNECTING: - { - bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)p_param; - btpan_conn_t* conn = btpan_find_conn_addr(bd_addr->address); - int btpan_conn_local_role; - int btpan_remote_role; - asrt(conn != NULL); - if (conn) { - btpan_conn_local_role = bta_role_to_btpan(conn->local_role); - btpan_remote_role = bta_role_to_btpan(conn->remote_role); - callback.connection_state_cb(BTPAN_STATE_DISCONNECTING, BT_STATUS_SUCCESS, - (const bt_bdaddr_t*)conn->peer, btpan_conn_local_role, btpan_remote_role); - } - } break; - default: - { - BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __func__, event); - } - break; - } -} - -static bt_status_t btpan_disconnect(const bt_bdaddr_t *bd_addr) -{ - btpan_conn_t* conn = btpan_find_conn_addr(bd_addr->address); - if (conn && conn->handle >= 0) - { - /* Inform the application that the disconnect has been initiated successfully */ - btif_transfer_context(btif_in_pan_generic_evt, BTIF_PAN_CB_DISCONNECTING, - (char *)bd_addr, sizeof(bt_bdaddr_t), NULL); - BTA_PanClose(conn->handle); - return BT_STATUS_SUCCESS; - } - return BT_STATUS_FAIL; + } + return BT_STATUS_FAIL; } static int pan_pth = -1; -void create_tap_read_thread(int tap_fd) -{ - if (pan_pth < 0) - pan_pth = btsock_thread_create(btpan_tap_fd_signaled, NULL); - if (pan_pth >= 0) - btsock_thread_add_fd(pan_pth, tap_fd, 0, SOCK_THREAD_FD_RD, 0); -} - -void destroy_tap_read_thread(void) -{ - if (pan_pth >= 0) - { - btsock_thread_exit(pan_pth); - pan_pth = -1; - } -} - -static int tap_if_up(const char *devname, const bt_bdaddr_t *addr) -{ - struct ifreq ifr; - int sk, err; - - sk = socket(AF_INET, SOCK_DGRAM, 0); - if (sk < 0) - return -1; - - //set mac addr - memset(&ifr, 0, sizeof(ifr)); - strncpy(ifr.ifr_name, devname, IFNAMSIZ - 1); - err = ioctl(sk, SIOCGIFHWADDR, &ifr); - if (err < 0) - { - BTIF_TRACE_ERROR("Could not get network hardware for interface:%s, errno:%s", devname, strerror(errno)); - close(sk); - return -1; - } - - strncpy(ifr.ifr_name, devname, IFNAMSIZ - 1); - memcpy(ifr.ifr_hwaddr.sa_data, addr->address, 6); - - /* The IEEE has specified that the most significant bit of the most significant byte is used to - * determine a multicast address. If its a 1, that means multicast, 0 means unicast. - * Kernel returns an error if we try to set a multicast address for the tun-tap ethernet interface. - * Mask this bit to avoid any issue with auto generated address. - */ - if (ifr.ifr_hwaddr.sa_data[0] & 0x01) { - BTIF_TRACE_WARNING("Not a unicast MAC address, force multicast bit flipping"); - ifr.ifr_hwaddr.sa_data[0] &= ~0x01; - } - - err = ioctl(sk, SIOCSIFHWADDR, (caddr_t)&ifr); - - if (err < 0) { - BTIF_TRACE_ERROR("Could not set bt address for interface:%s, errno:%s", devname, strerror(errno)); - close(sk); - return -1; - } - - //bring it up - memset(&ifr, 0, sizeof(ifr)); - strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1); +void create_tap_read_thread(int tap_fd) { + if (pan_pth < 0) pan_pth = btsock_thread_create(btpan_tap_fd_signaled, NULL); + if (pan_pth >= 0) + btsock_thread_add_fd(pan_pth, tap_fd, 0, SOCK_THREAD_FD_RD, 0); +} + +void destroy_tap_read_thread(void) { + if (pan_pth >= 0) { + btsock_thread_exit(pan_pth); + pan_pth = -1; + } +} + +static int tap_if_up(const char* devname, const bt_bdaddr_t* addr) { + struct ifreq ifr; + int sk, err; + + sk = socket(AF_INET, SOCK_DGRAM, 0); + if (sk < 0) return -1; + + // set mac addr + memset(&ifr, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, devname, IFNAMSIZ - 1); + err = ioctl(sk, SIOCGIFHWADDR, &ifr); + if (err < 0) { + BTIF_TRACE_ERROR( + "Could not get network hardware for interface:%s, errno:%s", devname, + strerror(errno)); + close(sk); + return -1; + } + + strncpy(ifr.ifr_name, devname, IFNAMSIZ - 1); + memcpy(ifr.ifr_hwaddr.sa_data, addr->address, 6); + + /* The IEEE has specified that the most significant bit of the most + * significant byte is used to + * determine a multicast address. If its a 1, that means multicast, 0 means + * unicast. + * Kernel returns an error if we try to set a multicast address for the + * tun-tap ethernet interface. + * Mask this bit to avoid any issue with auto generated address. + */ + if (ifr.ifr_hwaddr.sa_data[0] & 0x01) { + BTIF_TRACE_WARNING( + "Not a unicast MAC address, force multicast bit flipping"); + ifr.ifr_hwaddr.sa_data[0] &= ~0x01; + } + + err = ioctl(sk, SIOCSIFHWADDR, (caddr_t)&ifr); + + if (err < 0) { + BTIF_TRACE_ERROR("Could not set bt address for interface:%s, errno:%s", + devname, strerror(errno)); + close(sk); + return -1; + } - ifr.ifr_flags |= IFF_UP; - ifr.ifr_flags |= IFF_MULTICAST; + // bring it up + memset(&ifr, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1); - err = ioctl(sk, SIOCSIFFLAGS, (caddr_t) &ifr); + ifr.ifr_flags |= IFF_UP; + ifr.ifr_flags |= IFF_MULTICAST; + err = ioctl(sk, SIOCSIFFLAGS, (caddr_t)&ifr); - if (err < 0) { - BTIF_TRACE_ERROR("Could not bring up network interface:%s, errno:%d", devname, errno); - close(sk); - return -1; - } + if (err < 0) { + BTIF_TRACE_ERROR("Could not bring up network interface:%s, errno:%d", + devname, errno); close(sk); - BTIF_TRACE_DEBUG("network interface: %s is up", devname); - return 0; + return -1; + } + close(sk); + BTIF_TRACE_DEBUG("network interface: %s is up", devname); + return 0; } -static int tap_if_down(const char *devname) -{ - struct ifreq ifr; - int sk; +static int tap_if_down(const char* devname) { + struct ifreq ifr; + int sk; - sk = socket(AF_INET, SOCK_DGRAM, 0); - if (sk < 0) - return -1; + sk = socket(AF_INET, SOCK_DGRAM, 0); + if (sk < 0) return -1; - memset(&ifr, 0, sizeof(ifr)); - strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1); + memset(&ifr, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1); - ifr.ifr_flags &= ~IFF_UP; + ifr.ifr_flags &= ~IFF_UP; - ioctl(sk, SIOCSIFFLAGS, (caddr_t) &ifr); + ioctl(sk, SIOCSIFFLAGS, (caddr_t)&ifr); - close(sk); + close(sk); - return 0; + return 0; } void btpan_set_flow_control(bool enable) { - if (btpan_cb.tap_fd == -1) - return; + if (btpan_cb.tap_fd == -1) return; - btpan_cb.flow = enable; - if (enable) { - btsock_thread_add_fd(pan_pth, btpan_cb.tap_fd, 0, SOCK_THREAD_FD_RD, 0); - bta_dmexecutecallback(btu_exec_tap_fd_read, INT_TO_PTR(btpan_cb.tap_fd)); - } + btpan_cb.flow = enable; + if (enable) { + btsock_thread_add_fd(pan_pth, btpan_cb.tap_fd, 0, SOCK_THREAD_FD_RD, 0); + bta_dmexecutecallback(btu_exec_tap_fd_read, INT_TO_PTR(btpan_cb.tap_fd)); + } } -int btpan_tap_open() -{ - struct ifreq ifr; - int fd, err; - const char *clonedev = "/dev/tun"; +int btpan_tap_open() { + struct ifreq ifr; + int fd, err; + const char* clonedev = "/dev/tun"; - /* open the clone device */ + /* open the clone device */ - if ((fd = open(clonedev, O_RDWR)) < 0) - { - BTIF_TRACE_DEBUG("could not open %s, err:%d", clonedev, errno); - return fd; - } + if ((fd = open(clonedev, O_RDWR)) < 0) { + BTIF_TRACE_DEBUG("could not open %s, err:%d", clonedev, errno); + return fd; + } - memset(&ifr, 0, sizeof(ifr)); - ifr.ifr_flags = IFF_TAP | IFF_NO_PI; + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_flags = IFF_TAP | IFF_NO_PI; - strncpy(ifr.ifr_name, TAP_IF_NAME, IFNAMSIZ); + strncpy(ifr.ifr_name, TAP_IF_NAME, IFNAMSIZ); - /* try to create the device */ - if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0) - { - BTIF_TRACE_DEBUG("ioctl error:%d, errno:%s", err, strerror(errno)); - close(fd); - return err; - } - if (tap_if_up(TAP_IF_NAME, controller_get_interface()->get_address()) == 0) - { - int flags = fcntl(fd, F_GETFL, 0); - fcntl(fd, F_SETFL, flags | O_NONBLOCK); - return fd; - } - BTIF_TRACE_ERROR("can not bring up tap interface:%s", TAP_IF_NAME); + /* try to create the device */ + if ((err = ioctl(fd, TUNSETIFF, (void*)&ifr)) < 0) { + BTIF_TRACE_DEBUG("ioctl error:%d, errno:%s", err, strerror(errno)); close(fd); - return INVALID_FD; -} - -int btpan_tap_send(int tap_fd, const BD_ADDR src, const BD_ADDR dst, uint16_t proto, const char* buf, - uint16_t len, bool ext, bool forward) -{ - UNUSED(ext); - UNUSED(forward); - if (tap_fd != INVALID_FD) - { - tETH_HDR eth_hdr; - memcpy(ð_hdr.h_dest, dst, ETH_ADDR_LEN); - memcpy(ð_hdr.h_src, src, ETH_ADDR_LEN); - eth_hdr.h_proto = htons(proto); - char packet[TAP_MAX_PKT_WRITE_LEN + sizeof(tETH_HDR)]; - memcpy(packet, ð_hdr, sizeof(tETH_HDR)); - if (len > TAP_MAX_PKT_WRITE_LEN) - { - LOG_ERROR(LOG_TAG, "btpan_tap_send eth packet size:%d is exceeded limit!", len); - return -1; - } - memcpy(packet + sizeof(tETH_HDR), buf, len); - - /* Send data to network interface */ - ssize_t ret; - OSI_NO_INTR(ret = write(tap_fd, packet, len + sizeof(tETH_HDR))); - BTIF_TRACE_DEBUG("ret:%d", ret); - return (int)ret; + return err; + } + if (tap_if_up(TAP_IF_NAME, controller_get_interface()->get_address()) == 0) { + int flags = fcntl(fd, F_GETFL, 0); + fcntl(fd, F_SETFL, flags | O_NONBLOCK); + return fd; + } + BTIF_TRACE_ERROR("can not bring up tap interface:%s", TAP_IF_NAME); + close(fd); + return INVALID_FD; +} + +int btpan_tap_send(int tap_fd, const BD_ADDR src, const BD_ADDR dst, + uint16_t proto, const char* buf, uint16_t len, bool ext, + bool forward) { + UNUSED(ext); + UNUSED(forward); + if (tap_fd != INVALID_FD) { + tETH_HDR eth_hdr; + memcpy(ð_hdr.h_dest, dst, ETH_ADDR_LEN); + memcpy(ð_hdr.h_src, src, ETH_ADDR_LEN); + eth_hdr.h_proto = htons(proto); + char packet[TAP_MAX_PKT_WRITE_LEN + sizeof(tETH_HDR)]; + memcpy(packet, ð_hdr, sizeof(tETH_HDR)); + if (len > TAP_MAX_PKT_WRITE_LEN) { + LOG_ERROR(LOG_TAG, "btpan_tap_send eth packet size:%d is exceeded limit!", + len); + return -1; } - return -1; - -} - -int btpan_tap_close(int fd) -{ - if (tap_if_down(TAP_IF_NAME) == 0) - close(fd); - if (pan_pth >= 0) - btsock_thread_wakeup(pan_pth); - return 0; -} - -btpan_conn_t * btpan_find_conn_handle(uint16_t handle) -{ - for (int i = 0; i < MAX_PAN_CONNS; i++) - { - if (btpan_cb.conns[i].handle == handle) - return &btpan_cb.conns[i]; + memcpy(packet + sizeof(tETH_HDR), buf, len); + + /* Send data to network interface */ + ssize_t ret; + OSI_NO_INTR(ret = write(tap_fd, packet, len + sizeof(tETH_HDR))); + BTIF_TRACE_DEBUG("ret:%d", ret); + return (int)ret; + } + return -1; +} + +int btpan_tap_close(int fd) { + if (tap_if_down(TAP_IF_NAME) == 0) close(fd); + if (pan_pth >= 0) btsock_thread_wakeup(pan_pth); + return 0; +} + +btpan_conn_t* btpan_find_conn_handle(uint16_t handle) { + for (int i = 0; i < MAX_PAN_CONNS; i++) { + if (btpan_cb.conns[i].handle == handle) return &btpan_cb.conns[i]; + } + return NULL; +} + +btpan_conn_t* btpan_find_conn_addr(const BD_ADDR addr) { + for (int i = 0; i < MAX_PAN_CONNS; i++) { + if (memcmp(btpan_cb.conns[i].peer, addr, sizeof(BD_ADDR)) == 0) + return &btpan_cb.conns[i]; + } + return NULL; +} + +static void btpan_open_conn(btpan_conn_t* conn, tBTA_PAN* p_data) { + BTIF_TRACE_API( + "btpan_open_conn: local_role:%d, peer_role: %d, handle:%d, conn: %p", + p_data->open.local_role, p_data->open.peer_role, p_data->open.handle, + conn); + + if (conn == NULL) + conn = btpan_new_conn(p_data->open.handle, p_data->open.bd_addr, + p_data->open.local_role, p_data->open.peer_role); + if (conn) { + BTIF_TRACE_DEBUG( + "btpan_open_conn:tap_fd:%d, open_count:%d, " + "conn->handle:%d should = handle:%d, local_role:%d, remote_role:%d", + btpan_cb.tap_fd, btpan_cb.open_count, conn->handle, p_data->open.handle, + conn->local_role, conn->remote_role); + + btpan_cb.open_count++; + conn->handle = p_data->open.handle; + if (btpan_cb.tap_fd < 0) { + btpan_cb.tap_fd = btpan_tap_open(); + if (btpan_cb.tap_fd >= 0) create_tap_read_thread(btpan_cb.tap_fd); } - return NULL; -} -btpan_conn_t* btpan_find_conn_addr(const BD_ADDR addr) -{ - for (int i = 0; i < MAX_PAN_CONNS; i++) - { - if (memcmp(btpan_cb.conns[i].peer, addr, sizeof(BD_ADDR)) == 0) - return &btpan_cb.conns[i]; - } - return NULL; -} - -static void btpan_open_conn(btpan_conn_t* conn, tBTA_PAN *p_data) -{ - BTIF_TRACE_API("btpan_open_conn: local_role:%d, peer_role: %d, handle:%d, conn: %p", - p_data->open.local_role, p_data->open.peer_role, p_data->open.handle, conn); - - if (conn == NULL) - conn = btpan_new_conn(p_data->open.handle, p_data->open.bd_addr, p_data->open.local_role, - p_data->open.peer_role); - if (conn) - { - BTIF_TRACE_DEBUG("btpan_open_conn:tap_fd:%d, open_count:%d, " - "conn->handle:%d should = handle:%d, local_role:%d, remote_role:%d", - btpan_cb.tap_fd, btpan_cb.open_count, conn->handle, p_data->open.handle, - conn->local_role, conn->remote_role); - - btpan_cb.open_count++; - conn->handle = p_data->open.handle; - if (btpan_cb.tap_fd < 0) - { - btpan_cb.tap_fd = btpan_tap_open(); - if(btpan_cb.tap_fd >= 0) - create_tap_read_thread(btpan_cb.tap_fd); - } - - if (btpan_cb.tap_fd >= 0) - { - btpan_cb.flow = 1; - conn->state = PAN_STATE_OPEN; - } + if (btpan_cb.tap_fd >= 0) { + btpan_cb.flow = 1; + conn->state = PAN_STATE_OPEN; } + } } -static void btpan_close_conn(btpan_conn_t* conn) -{ - BTIF_TRACE_API("btpan_close_conn: %p",conn); +static void btpan_close_conn(btpan_conn_t* conn) { + BTIF_TRACE_API("btpan_close_conn: %p", conn); - if (conn && conn->state == PAN_STATE_OPEN) - { - BTIF_TRACE_DEBUG("btpan_close_conn: PAN_STATE_OPEN"); + if (conn && conn->state == PAN_STATE_OPEN) { + BTIF_TRACE_DEBUG("btpan_close_conn: PAN_STATE_OPEN"); - conn->state = PAN_STATE_CLOSE; - btpan_cb.open_count--; + conn->state = PAN_STATE_CLOSE; + btpan_cb.open_count--; - if (btpan_cb.open_count == 0) - { - destroy_tap_read_thread(); - if (btpan_cb.tap_fd != INVALID_FD) - { - btpan_tap_close(btpan_cb.tap_fd); - btpan_cb.tap_fd = INVALID_FD; - } - } + if (btpan_cb.open_count == 0) { + destroy_tap_read_thread(); + if (btpan_cb.tap_fd != INVALID_FD) { + btpan_tap_close(btpan_cb.tap_fd); + btpan_cb.tap_fd = INVALID_FD; + } } -} - - -static void btpan_cleanup_conn(btpan_conn_t* conn) -{ - if (conn) - { - conn->handle = -1; - conn->state = -1; - memset(&conn->peer, 0, sizeof(conn->peer)); - memset(&conn->eth_addr, 0, sizeof(conn->eth_addr)); - conn->local_role = conn->remote_role = 0; + } +} + +static void btpan_cleanup_conn(btpan_conn_t* conn) { + if (conn) { + conn->handle = -1; + conn->state = -1; + memset(&conn->peer, 0, sizeof(conn->peer)); + memset(&conn->eth_addr, 0, sizeof(conn->eth_addr)); + conn->local_role = conn->remote_role = 0; + } +} + +btpan_conn_t* btpan_new_conn(int handle, const BD_ADDR addr, int local_role, + int remote_role) { + for (int i = 0; i < MAX_PAN_CONNS; i++) { + BTIF_TRACE_DEBUG("conns[%d]:%d", i, btpan_cb.conns[i].handle); + if (btpan_cb.conns[i].handle == -1) { + BTIF_TRACE_DEBUG("handle:%d, local_role:%d, remote_role:%d", handle, + local_role, remote_role); + + btpan_cb.conns[i].handle = handle; + bdcpy(btpan_cb.conns[i].peer, addr); + btpan_cb.conns[i].local_role = local_role; + btpan_cb.conns[i].remote_role = remote_role; + return &btpan_cb.conns[i]; } -} - -btpan_conn_t* btpan_new_conn(int handle, const BD_ADDR addr, int local_role, int remote_role) -{ - for (int i = 0; i < MAX_PAN_CONNS; i++) - { - BTIF_TRACE_DEBUG("conns[%d]:%d", i, btpan_cb.conns[i].handle); - if (btpan_cb.conns[i].handle == -1) - { - BTIF_TRACE_DEBUG("handle:%d, local_role:%d, remote_role:%d", handle, local_role, remote_role); - - btpan_cb.conns[i].handle = handle; - bdcpy(btpan_cb.conns[i].peer, addr); - btpan_cb.conns[i].local_role = local_role; - btpan_cb.conns[i].remote_role = remote_role; - return &btpan_cb.conns[i]; - } + } + BTIF_TRACE_DEBUG("MAX_PAN_CONNS:%d exceeded, return NULL as failed", + MAX_PAN_CONNS); + return NULL; +} + +void btpan_close_handle(btpan_conn_t* p) { + BTIF_TRACE_DEBUG("btpan_close_handle : close handle %d", p->handle); + p->handle = -1; + p->local_role = -1; + p->remote_role = -1; + memset(&p->peer, 0, 6); +} + +static inline bool should_forward(tETH_HDR* hdr) { + uint16_t proto = ntohs(hdr->h_proto); + if (proto == ETH_P_IP || proto == ETH_P_ARP || proto == ETH_P_IPV6) + return true; + BTIF_TRACE_DEBUG("unknown proto:%x", proto); + return false; +} + +static int forward_bnep(tETH_HDR* eth_hdr, BT_HDR* hdr) { + int broadcast = eth_hdr->h_dest[0] & 1; + + // Find the right connection to send this frame over. + for (int i = 0; i < MAX_PAN_CONNS; i++) { + uint16_t handle = btpan_cb.conns[i].handle; + if (handle != (uint16_t)-1 && + (broadcast || + memcmp(btpan_cb.conns[i].eth_addr, eth_hdr->h_dest, sizeof(BD_ADDR)) == + 0 || + memcmp(btpan_cb.conns[i].peer, eth_hdr->h_dest, sizeof(BD_ADDR)) == + 0)) { + int result = PAN_WriteBuf(handle, eth_hdr->h_dest, eth_hdr->h_src, + ntohs(eth_hdr->h_proto), hdr, 0); + switch (result) { + case PAN_Q_SIZE_EXCEEDED: + return FORWARD_CONGEST; + case PAN_SUCCESS: + return FORWARD_SUCCESS; + default: + return FORWARD_FAILURE; + } } - BTIF_TRACE_DEBUG("MAX_PAN_CONNS:%d exceeded, return NULL as failed", MAX_PAN_CONNS); - return NULL; -} - -void btpan_close_handle(btpan_conn_t *p) -{ - BTIF_TRACE_DEBUG("btpan_close_handle : close handle %d", p->handle); - p->handle = -1; - p->local_role = -1; - p->remote_role = -1; - memset(&p->peer, 0, 6); -} - -static inline bool should_forward(tETH_HDR* hdr) -{ - uint16_t proto = ntohs(hdr->h_proto); - if (proto == ETH_P_IP || proto == ETH_P_ARP || proto == ETH_P_IPV6) - return true; - BTIF_TRACE_DEBUG("unknown proto:%x", proto); - return false; -} - -static int forward_bnep(tETH_HDR* eth_hdr, BT_HDR *hdr) { - int broadcast = eth_hdr->h_dest[0] & 1; - - // Find the right connection to send this frame over. - for (int i = 0; i < MAX_PAN_CONNS; i++) - { - uint16_t handle = btpan_cb.conns[i].handle; - if (handle != (uint16_t)-1 && - (broadcast || memcmp(btpan_cb.conns[i].eth_addr, eth_hdr->h_dest, sizeof(BD_ADDR)) == 0 - || memcmp(btpan_cb.conns[i].peer, eth_hdr->h_dest, sizeof(BD_ADDR)) == 0)) { - int result = PAN_WriteBuf(handle, eth_hdr->h_dest, eth_hdr->h_src, ntohs(eth_hdr->h_proto), hdr, 0); - switch (result) { - case PAN_Q_SIZE_EXCEEDED: - return FORWARD_CONGEST; - case PAN_SUCCESS: - return FORWARD_SUCCESS; - default: - return FORWARD_FAILURE; - } - } + } + osi_free(hdr); + return FORWARD_IGNORE; +} + +static void bta_pan_callback_transfer(uint16_t event, char* p_param) { + tBTA_PAN* p_data = (tBTA_PAN*)p_param; + + switch (event) { + case BTA_PAN_ENABLE_EVT: + BTIF_TRACE_DEBUG("BTA_PAN_ENABLE_EVT"); + break; + case BTA_PAN_SET_ROLE_EVT: { + int btpan_role = bta_role_to_btpan(p_data->set_role.role); + bt_status_t status = p_data->set_role.status == BTA_PAN_SUCCESS + ? BT_STATUS_SUCCESS + : BT_STATUS_FAIL; + btpan_control_state_t state = + btpan_role == 0 ? BTPAN_STATE_DISABLED : BTPAN_STATE_ENABLED; + callback.control_state_cb(state, btpan_role, status, TAP_IF_NAME); + break; } - osi_free(hdr); - return FORWARD_IGNORE; -} - -static void bta_pan_callback_transfer(uint16_t event, char *p_param) -{ - tBTA_PAN *p_data = (tBTA_PAN *)p_param; - - switch(event) - { - case BTA_PAN_ENABLE_EVT: - BTIF_TRACE_DEBUG("BTA_PAN_ENABLE_EVT"); - break; - case BTA_PAN_SET_ROLE_EVT: - { - int btpan_role = bta_role_to_btpan(p_data->set_role.role); - bt_status_t status = p_data->set_role.status == BTA_PAN_SUCCESS ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; - btpan_control_state_t state = btpan_role == 0 ? BTPAN_STATE_DISABLED : BTPAN_STATE_ENABLED; - callback.control_state_cb(state, btpan_role, status, TAP_IF_NAME); - break; - } - case BTA_PAN_OPENING_EVT: - { - btpan_conn_t* conn; - bdstr_t bds; - bdaddr_to_string((bt_bdaddr_t *)p_data->opening.bd_addr, bds, sizeof(bds)); - BTIF_TRACE_DEBUG("BTA_PAN_OPENING_EVT handle %d, addr: %s", p_data->opening.handle, bds); - conn = btpan_find_conn_addr(p_data->opening.bd_addr); - - asrt(conn != NULL); - if (conn) - { - conn->handle = p_data->opening.handle; - int btpan_conn_local_role = bta_role_to_btpan(conn->local_role); - int btpan_remote_role = bta_role_to_btpan(conn->remote_role); - callback.connection_state_cb(BTPAN_STATE_CONNECTING, BT_STATUS_SUCCESS, - (const bt_bdaddr_t*)p_data->opening.bd_addr, btpan_conn_local_role, btpan_remote_role); - } - else - BTIF_TRACE_ERROR("connection not found"); - break; - } - case BTA_PAN_OPEN_EVT: - { - btpan_connection_state_t state; - bt_status_t status; - btpan_conn_t *conn = btpan_find_conn_handle(p_data->open.handle); - - LOG_VERBOSE(LOG_TAG, "%s pan connection open status: %d", __func__, p_data->open.status); - if (p_data->open.status == BTA_PAN_SUCCESS) - { - state = BTPAN_STATE_CONNECTED; - status = BT_STATUS_SUCCESS; - btpan_open_conn(conn, p_data); - } - else - { - state = BTPAN_STATE_DISCONNECTED; - status = BT_STATUS_FAIL; - btpan_cleanup_conn(conn); - } - /* debug("BTA_PAN_OPEN_EVT handle:%d, conn:%p", p_data->open.handle, conn); */ - /* debug("conn bta local_role:%d, bta remote role:%d", conn->local_role, conn->remote_role); */ - int btpan_conn_local_role = bta_role_to_btpan(p_data->open.local_role); - int btpan_remote_role = bta_role_to_btpan(p_data->open.peer_role); - callback.connection_state_cb(state, status, (const bt_bdaddr_t*)p_data->open.bd_addr, - btpan_conn_local_role, btpan_remote_role); - break; - } - case BTA_PAN_CLOSE_EVT: - { - LOG_INFO(LOG_TAG, "%s: event = BTA_PAN_CLOSE_EVT handle %d", __func__, p_data->close.handle); - btpan_conn_t* conn = btpan_find_conn_handle(p_data->close.handle); - btpan_close_conn(conn); - - if (conn && conn->handle >= 0) - { - int btpan_conn_local_role = bta_role_to_btpan(conn->local_role); - int btpan_remote_role = bta_role_to_btpan(conn->remote_role); - callback.connection_state_cb(BTPAN_STATE_DISCONNECTED, (bt_status_t)0, (const bt_bdaddr_t*)conn->peer, - btpan_conn_local_role, btpan_remote_role); - btpan_cleanup_conn(conn); - } - else - BTIF_TRACE_ERROR("pan handle not found (%d)", p_data->close.handle); - break; - } - default: - BTIF_TRACE_WARNING("Unknown pan event %d", event); - break; + case BTA_PAN_OPENING_EVT: { + btpan_conn_t* conn; + bdstr_t bds; + bdaddr_to_string((bt_bdaddr_t*)p_data->opening.bd_addr, bds, sizeof(bds)); + BTIF_TRACE_DEBUG("BTA_PAN_OPENING_EVT handle %d, addr: %s", + p_data->opening.handle, bds); + conn = btpan_find_conn_addr(p_data->opening.bd_addr); + + asrt(conn != NULL); + if (conn) { + conn->handle = p_data->opening.handle; + int btpan_conn_local_role = bta_role_to_btpan(conn->local_role); + int btpan_remote_role = bta_role_to_btpan(conn->remote_role); + callback.connection_state_cb( + BTPAN_STATE_CONNECTING, BT_STATUS_SUCCESS, + (const bt_bdaddr_t*)p_data->opening.bd_addr, btpan_conn_local_role, + btpan_remote_role); + } else + BTIF_TRACE_ERROR("connection not found"); + break; } + case BTA_PAN_OPEN_EVT: { + btpan_connection_state_t state; + bt_status_t status; + btpan_conn_t* conn = btpan_find_conn_handle(p_data->open.handle); + + LOG_VERBOSE(LOG_TAG, "%s pan connection open status: %d", __func__, + p_data->open.status); + if (p_data->open.status == BTA_PAN_SUCCESS) { + state = BTPAN_STATE_CONNECTED; + status = BT_STATUS_SUCCESS; + btpan_open_conn(conn, p_data); + } else { + state = BTPAN_STATE_DISCONNECTED; + status = BT_STATUS_FAIL; + btpan_cleanup_conn(conn); + } + /* debug("BTA_PAN_OPEN_EVT handle:%d, conn:%p", p_data->open.handle, + * conn); */ + /* debug("conn bta local_role:%d, bta remote role:%d", conn->local_role, + * conn->remote_role); */ + int btpan_conn_local_role = bta_role_to_btpan(p_data->open.local_role); + int btpan_remote_role = bta_role_to_btpan(p_data->open.peer_role); + callback.connection_state_cb(state, status, + (const bt_bdaddr_t*)p_data->open.bd_addr, + btpan_conn_local_role, btpan_remote_role); + break; + } + case BTA_PAN_CLOSE_EVT: { + LOG_INFO(LOG_TAG, "%s: event = BTA_PAN_CLOSE_EVT handle %d", __func__, + p_data->close.handle); + btpan_conn_t* conn = btpan_find_conn_handle(p_data->close.handle); + btpan_close_conn(conn); + + if (conn && conn->handle >= 0) { + int btpan_conn_local_role = bta_role_to_btpan(conn->local_role); + int btpan_remote_role = bta_role_to_btpan(conn->remote_role); + callback.connection_state_cb(BTPAN_STATE_DISCONNECTED, (bt_status_t)0, + (const bt_bdaddr_t*)conn->peer, + btpan_conn_local_role, btpan_remote_role); + btpan_cleanup_conn(conn); + } else + BTIF_TRACE_ERROR("pan handle not found (%d)", p_data->close.handle); + break; + } + default: + BTIF_TRACE_WARNING("Unknown pan event %d", event); + break; + } } -static void bta_pan_callback(tBTA_PAN_EVT event, tBTA_PAN *p_data) -{ - btif_transfer_context(bta_pan_callback_transfer, event, (char*)p_data, sizeof(tBTA_PAN), NULL); +static void bta_pan_callback(tBTA_PAN_EVT event, tBTA_PAN* p_data) { + btif_transfer_context(bta_pan_callback_transfer, event, (char*)p_data, + sizeof(tBTA_PAN), NULL); } #define IS_EXCEPTION(e) ((e) & (POLLHUP | POLLRDHUP | POLLERR | POLLNVAL)) -static void btu_exec_tap_fd_read(void *p_param) { - struct pollfd ufd; - int fd = PTR_TO_INT(p_param); - - if (fd == INVALID_FD || fd != btpan_cb.tap_fd) - return; - - // Don't occupy BTU context too long, avoid buffer overruns and - // give other profiles a chance to run by limiting the amount of memory - // PAN can use. - for (int i = 0; i < PAN_BUF_MAX && btif_is_enabled() && btpan_cb.flow; i++) { - BT_HDR *buffer = (BT_HDR *)osi_malloc(PAN_BUF_SIZE); - buffer->offset = PAN_MINIMUM_OFFSET; - buffer->len = PAN_BUF_SIZE - sizeof(BT_HDR) - buffer->offset; - - uint8_t *packet = (uint8_t *)buffer + sizeof(BT_HDR) + buffer->offset; - - // If we don't have an undelivered packet left over, pull one from the TAP driver. - // We save it in the congest_packet right away in case we can't deliver it in this - // attempt. - if (!btpan_cb.congest_packet_size) { - ssize_t ret; - OSI_NO_INTR(ret = read(fd, btpan_cb.congest_packet, - sizeof(btpan_cb.congest_packet))); - switch (ret) { - case -1: - BTIF_TRACE_ERROR("%s unable to read from driver: %s", __func__, strerror(errno)); - osi_free(buffer); - //add fd back to monitor thread to try it again later - btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0); - return; - case 0: - BTIF_TRACE_WARNING("%s end of file reached.", __func__); - osi_free(buffer); - //add fd back to monitor thread to process the exception - btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0); - return; - default: - btpan_cb.congest_packet_size = ret; - break; - } - } - - memcpy(packet, btpan_cb.congest_packet, MIN(btpan_cb.congest_packet_size, buffer->len)); - buffer->len = MIN(btpan_cb.congest_packet_size, buffer->len); - - if (buffer->len > sizeof(tETH_HDR) && should_forward((tETH_HDR *)packet)) { - // Extract the ethernet header from the buffer since the PAN_WriteBuf inside - // forward_bnep can't handle two pointers that point inside the same GKI buffer. - tETH_HDR hdr; - memcpy(&hdr, packet, sizeof(tETH_HDR)); - - // Skip the ethernet header. - buffer->len -= sizeof(tETH_HDR); - buffer->offset += sizeof(tETH_HDR); - if (forward_bnep(&hdr, buffer) != FORWARD_CONGEST) - btpan_cb.congest_packet_size = 0; - } else { - BTIF_TRACE_WARNING("%s dropping packet of length %d", __func__, buffer->len); - btpan_cb.congest_packet_size = 0; - osi_free(buffer); - } - - // Bail out of the loop if reading from the TAP fd would block. - ufd.fd = fd; - ufd.events = POLLIN; - ufd.revents = 0; - - int ret; - OSI_NO_INTR(ret = poll(&ufd, 1, 0)); - if (ret <= 0 || IS_EXCEPTION(ufd.revents)) - break; +static void btu_exec_tap_fd_read(void* p_param) { + struct pollfd ufd; + int fd = PTR_TO_INT(p_param); + + if (fd == INVALID_FD || fd != btpan_cb.tap_fd) return; + + // Don't occupy BTU context too long, avoid buffer overruns and + // give other profiles a chance to run by limiting the amount of memory + // PAN can use. + for (int i = 0; i < PAN_BUF_MAX && btif_is_enabled() && btpan_cb.flow; i++) { + BT_HDR* buffer = (BT_HDR*)osi_malloc(PAN_BUF_SIZE); + buffer->offset = PAN_MINIMUM_OFFSET; + buffer->len = PAN_BUF_SIZE - sizeof(BT_HDR) - buffer->offset; + + uint8_t* packet = (uint8_t*)buffer + sizeof(BT_HDR) + buffer->offset; + + // If we don't have an undelivered packet left over, pull one from the TAP + // driver. + // We save it in the congest_packet right away in case we can't deliver it + // in this + // attempt. + if (!btpan_cb.congest_packet_size) { + ssize_t ret; + OSI_NO_INTR(ret = read(fd, btpan_cb.congest_packet, + sizeof(btpan_cb.congest_packet))); + switch (ret) { + case -1: + BTIF_TRACE_ERROR("%s unable to read from driver: %s", __func__, + strerror(errno)); + osi_free(buffer); + // add fd back to monitor thread to try it again later + btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0); + return; + case 0: + BTIF_TRACE_WARNING("%s end of file reached.", __func__); + osi_free(buffer); + // add fd back to monitor thread to process the exception + btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0); + return; + default: + btpan_cb.congest_packet_size = ret; + break; + } } - if (btpan_cb.flow) { - //add fd back to monitor thread when the flow is on - btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0); + memcpy(packet, btpan_cb.congest_packet, + MIN(btpan_cb.congest_packet_size, buffer->len)); + buffer->len = MIN(btpan_cb.congest_packet_size, buffer->len); + + if (buffer->len > sizeof(tETH_HDR) && should_forward((tETH_HDR*)packet)) { + // Extract the ethernet header from the buffer since the PAN_WriteBuf + // inside + // forward_bnep can't handle two pointers that point inside the same GKI + // buffer. + tETH_HDR hdr; + memcpy(&hdr, packet, sizeof(tETH_HDR)); + + // Skip the ethernet header. + buffer->len -= sizeof(tETH_HDR); + buffer->offset += sizeof(tETH_HDR); + if (forward_bnep(&hdr, buffer) != FORWARD_CONGEST) + btpan_cb.congest_packet_size = 0; + } else { + BTIF_TRACE_WARNING("%s dropping packet of length %d", __func__, + buffer->len); + btpan_cb.congest_packet_size = 0; + osi_free(buffer); } + + // Bail out of the loop if reading from the TAP fd would block. + ufd.fd = fd; + ufd.events = POLLIN; + ufd.revents = 0; + + int ret; + OSI_NO_INTR(ret = poll(&ufd, 1, 0)); + if (ret <= 0 || IS_EXCEPTION(ufd.revents)) break; + } + + if (btpan_cb.flow) { + // add fd back to monitor thread when the flow is on + btsock_thread_add_fd(pan_pth, fd, 0, SOCK_THREAD_FD_RD, 0); + } } static void btif_pan_close_all_conns() { - if (!stack_initialized) - return; + if (!stack_initialized) return; - for (int i = 0; i < MAX_PAN_CONNS; ++i) - { - if (btpan_cb.conns[i].handle != -1) - BTA_PanClose(btpan_cb.conns[i].handle); - } + for (int i = 0; i < MAX_PAN_CONNS; ++i) { + if (btpan_cb.conns[i].handle != -1) BTA_PanClose(btpan_cb.conns[i].handle); + } } -static void btpan_tap_fd_signaled(int fd, int type, int flags, uint32_t user_id) { - assert(btpan_cb.tap_fd == INVALID_FD || btpan_cb.tap_fd == fd); +static void btpan_tap_fd_signaled(int fd, int type, int flags, + uint32_t user_id) { + assert(btpan_cb.tap_fd == INVALID_FD || btpan_cb.tap_fd == fd); - if (btpan_cb.tap_fd != fd) { - BTIF_TRACE_WARNING("%s Signaled on mismatched fds exp:%d act:%d\n", - __func__, btpan_cb.tap_fd, fd); - return; - } + if (btpan_cb.tap_fd != fd) { + BTIF_TRACE_WARNING("%s Signaled on mismatched fds exp:%d act:%d\n", + __func__, btpan_cb.tap_fd, fd); + return; + } - if (flags & SOCK_THREAD_FD_EXCEPTION) { - btpan_cb.tap_fd = INVALID_FD; - btpan_tap_close(fd); - btif_pan_close_all_conns(); - } else if (flags & SOCK_THREAD_FD_RD) - bta_dmexecutecallback(btu_exec_tap_fd_read, INT_TO_PTR(fd)); + if (flags & SOCK_THREAD_FD_EXCEPTION) { + btpan_cb.tap_fd = INVALID_FD; + btpan_tap_close(fd); + btif_pan_close_all_conns(); + } else if (flags & SOCK_THREAD_FD_RD) + bta_dmexecutecallback(btu_exec_tap_fd_read, INT_TO_PTR(fd)); } diff --git a/btif/src/btif_profile_queue.cc b/btif/src/btif_profile_queue.cc index 096833067..9e1d20a54 100644 --- a/btif/src/btif_profile_queue.cc +++ b/btif/src/btif_profile_queue.cc @@ -31,15 +31,15 @@ #include #include -#include "btif_common.h" #include "bt_common.h" +#include "btif_common.h" #include "osi/include/allocator.h" #include "osi/include/list.h" #include "stack_manager.h" /******************************************************************************* -** Local type definitions -*******************************************************************************/ + * Local type definitions + ******************************************************************************/ typedef enum { BTIF_QUEUE_CONNECT_EVT, @@ -47,128 +47,129 @@ typedef enum { } btif_queue_event_t; typedef struct { - bt_bdaddr_t bda; - uint16_t uuid; - bool busy; - btif_connect_cb_t connect_cb; + bt_bdaddr_t bda; + uint16_t uuid; + bool busy; + btif_connect_cb_t connect_cb; } connect_node_t; /******************************************************************************* -** Static variables -*******************************************************************************/ + * Static variables + ******************************************************************************/ -static list_t *connect_queue; +static list_t* connect_queue; static const size_t MAX_REASONABLE_REQUESTS = 10; /******************************************************************************* -** Queue helper functions -*******************************************************************************/ - -static void queue_int_add(connect_node_t *p_param) { - if (!connect_queue) { - connect_queue = list_new(osi_free); - assert(connect_queue != NULL); - } - - // Sanity check to make sure we're not leaking connection requests - assert(list_length(connect_queue) < MAX_REASONABLE_REQUESTS); + * Queue helper functions + ******************************************************************************/ - for (const list_node_t *node = list_begin(connect_queue); node != list_end(connect_queue); node = list_next(node)) { - if (((connect_node_t *)list_node(node))->uuid == p_param->uuid) { - LOG_INFO(LOG_TAG, "%s dropping duplicate connect request for uuid: %04x", __func__, p_param->uuid); - return; - } +static void queue_int_add(connect_node_t* p_param) { + if (!connect_queue) { + connect_queue = list_new(osi_free); + assert(connect_queue != NULL); + } + + // Sanity check to make sure we're not leaking connection requests + assert(list_length(connect_queue) < MAX_REASONABLE_REQUESTS); + + for (const list_node_t* node = list_begin(connect_queue); + node != list_end(connect_queue); node = list_next(node)) { + if (((connect_node_t*)list_node(node))->uuid == p_param->uuid) { + LOG_INFO(LOG_TAG, "%s dropping duplicate connect request for uuid: %04x", + __func__, p_param->uuid); + return; } + } - connect_node_t *p_node = (connect_node_t *)osi_malloc(sizeof(connect_node_t)); - memcpy(p_node, p_param, sizeof(connect_node_t)); - list_append(connect_queue, p_node); + connect_node_t* p_node = (connect_node_t*)osi_malloc(sizeof(connect_node_t)); + memcpy(p_node, p_param, sizeof(connect_node_t)); + list_append(connect_queue, p_node); } static void queue_int_advance() { - if (connect_queue && !list_is_empty(connect_queue)) - list_remove(connect_queue, list_front(connect_queue)); + if (connect_queue && !list_is_empty(connect_queue)) + list_remove(connect_queue, list_front(connect_queue)); } -static void queue_int_handle_evt(uint16_t event, char *p_param) { - switch(event) { - case BTIF_QUEUE_CONNECT_EVT: - queue_int_add((connect_node_t *)p_param); - break; +static void queue_int_handle_evt(uint16_t event, char* p_param) { + switch (event) { + case BTIF_QUEUE_CONNECT_EVT: + queue_int_add((connect_node_t*)p_param); + break; - case BTIF_QUEUE_ADVANCE_EVT: - queue_int_advance(); - break; - } + case BTIF_QUEUE_ADVANCE_EVT: + queue_int_advance(); + break; + } - if (stack_manager_get_interface()->get_stack_is_running()) - btif_queue_connect_next(); + if (stack_manager_get_interface()->get_stack_is_running()) + btif_queue_connect_next(); } /******************************************************************************* -** -** Function btif_queue_connect -** -** Description Add a new connection to the queue and trigger the next -** scheduled connection. -** -** Returns BT_STATUS_SUCCESS if successful -** -*******************************************************************************/ -bt_status_t btif_queue_connect(uint16_t uuid, const bt_bdaddr_t *bda, btif_connect_cb_t connect_cb) { - connect_node_t node; - memset(&node, 0, sizeof(connect_node_t)); - memcpy(&node.bda, bda, sizeof(bt_bdaddr_t)); - node.uuid = uuid; - node.connect_cb = connect_cb; - - return btif_transfer_context(queue_int_handle_evt, BTIF_QUEUE_CONNECT_EVT, - (char *)&node, sizeof(connect_node_t), NULL); + * + * Function btif_queue_connect + * + * Description Add a new connection to the queue and trigger the next + * scheduled connection. + * + * Returns BT_STATUS_SUCCESS if successful + * + ******************************************************************************/ +bt_status_t btif_queue_connect(uint16_t uuid, const bt_bdaddr_t* bda, + btif_connect_cb_t connect_cb) { + connect_node_t node; + memset(&node, 0, sizeof(connect_node_t)); + memcpy(&node.bda, bda, sizeof(bt_bdaddr_t)); + node.uuid = uuid; + node.connect_cb = connect_cb; + + return btif_transfer_context(queue_int_handle_evt, BTIF_QUEUE_CONNECT_EVT, + (char*)&node, sizeof(connect_node_t), NULL); } /******************************************************************************* -** -** Function btif_queue_advance -** -** Description Clear the queue's busy status and advance to the next -** scheduled connection. -** -** Returns void -** -*******************************************************************************/ + * + * Function btif_queue_advance + * + * Description Clear the queue's busy status and advance to the next + * scheduled connection. + * + * Returns void + * + ******************************************************************************/ void btif_queue_advance() { - btif_transfer_context(queue_int_handle_evt, BTIF_QUEUE_ADVANCE_EVT, - NULL, 0, NULL); + btif_transfer_context(queue_int_handle_evt, BTIF_QUEUE_ADVANCE_EVT, NULL, 0, + NULL); } // This function dispatches the next pending connect request. It is called from // stack_manager when the stack comes up. bt_status_t btif_queue_connect_next(void) { - if (!connect_queue || list_is_empty(connect_queue)) - return BT_STATUS_FAIL; + if (!connect_queue || list_is_empty(connect_queue)) return BT_STATUS_FAIL; - connect_node_t *p_head = (connect_node_t *)list_front(connect_queue); + connect_node_t* p_head = (connect_node_t*)list_front(connect_queue); - // If the queue is currently busy, we return success anyway, - // since the connection has been queued... - if (p_head->busy) - return BT_STATUS_SUCCESS; + // If the queue is currently busy, we return success anyway, + // since the connection has been queued... + if (p_head->busy) return BT_STATUS_SUCCESS; - p_head->busy = true; - return p_head->connect_cb(&p_head->bda, p_head->uuid); + p_head->busy = true; + return p_head->connect_cb(&p_head->bda, p_head->uuid); } /******************************************************************************* -** -** Function btif_queue_release -** -** Description Free up all the queue nodes and set the queue head to NULL -** -** Returns void -** -*******************************************************************************/ + * + * Function btif_queue_release + * + * Description Free up all the queue nodes and set the queue head to NULL + * + * Returns void + * + ******************************************************************************/ void btif_queue_release() { - list_free(connect_queue); - connect_queue = NULL; + list_free(connect_queue); + connect_queue = NULL; } diff --git a/btif/src/btif_rc.cc b/btif/src/btif_rc.cc index a271deb54..11091ffac 100644 --- a/btif/src/btif_rc.cc +++ b/btif/src/btif_rc.cc @@ -37,659 +37,638 @@ #include #include "avrc_defs.h" +#include "bdaddr.h" +#include "bt_common.h" #include "bta_api.h" #include "bta_av_api.h" #include "btif_av.h" #include "btif_common.h" #include "btif_util.h" -#include "bt_common.h" +#include "btu.h" #include "device/include/interop.h" -#include "uinput.h" -#include "bdaddr.h" #include "osi/include/list.h" #include "osi/include/properties.h" -#include "btu.h" +#include "uinput.h" #define RC_INVALID_TRACK_ID (0xFFFFFFFFFFFFFFFFULL) /***************************************************************************** -** Constants & Macros -******************************************************************************/ + * Constants & Macros + *****************************************************************************/ /* cod value for Headsets */ -#define COD_AV_HEADSETS 0x0404 +#define COD_AV_HEADSETS 0x0404 /* for AVRC 1.4 need to change this */ #define MAX_RC_NOTIFICATIONS AVRC_EVT_VOLUME_CHANGE -#define IDX_GET_PLAY_STATUS_RSP 0 -#define IDX_LIST_APP_ATTR_RSP 1 -#define IDX_LIST_APP_VALUE_RSP 2 -#define IDX_GET_CURR_APP_VAL_RSP 3 -#define IDX_SET_APP_VAL_RSP 4 -#define IDX_GET_APP_ATTR_TXT_RSP 5 -#define IDX_GET_APP_VAL_TXT_RSP 6 -#define IDX_GET_ELEMENT_ATTR_RSP 7 -#define IDX_SET_ADDR_PLAYER_RSP 8 -#define IDX_SET_BROWSED_PLAYER_RSP 9 -#define IDX_GET_FOLDER_ITEMS_RSP 10 -#define IDX_CHG_PATH_RSP 11 -#define IDX_GET_ITEM_ATTR_RSP 12 -#define IDX_PLAY_ITEM_RSP 13 -#define IDX_GET_TOTAL_NUM_OF_ITEMS_RSP 14 -#define IDX_SEARCH_RSP 15 -#define IDX_ADD_TO_NOW_PLAYING_RSP 16 +#define IDX_GET_PLAY_STATUS_RSP 0 +#define IDX_LIST_APP_ATTR_RSP 1 +#define IDX_LIST_APP_VALUE_RSP 2 +#define IDX_GET_CURR_APP_VAL_RSP 3 +#define IDX_SET_APP_VAL_RSP 4 +#define IDX_GET_APP_ATTR_TXT_RSP 5 +#define IDX_GET_APP_VAL_TXT_RSP 6 +#define IDX_GET_ELEMENT_ATTR_RSP 7 +#define IDX_SET_ADDR_PLAYER_RSP 8 +#define IDX_SET_BROWSED_PLAYER_RSP 9 +#define IDX_GET_FOLDER_ITEMS_RSP 10 +#define IDX_CHG_PATH_RSP 11 +#define IDX_GET_ITEM_ATTR_RSP 12 +#define IDX_PLAY_ITEM_RSP 13 +#define IDX_GET_TOTAL_NUM_OF_ITEMS_RSP 14 +#define IDX_SEARCH_RSP 15 +#define IDX_ADD_TO_NOW_PLAYING_RSP 16 /* Update MAX value whenever IDX will be changed */ -#define MAX_CMD_QUEUE_LEN 17 +#define MAX_CMD_QUEUE_LEN 17 #define MAX_VOLUME 128 #define MAX_LABEL 16 #define MAX_TRANSACTIONS_PER_SESSION 16 #define PLAY_STATUS_PLAYING 1 -#define BTIF_RC_NUM_CONN BT_RC_NUM_APP - -#define CHECK_RC_CONNECTED(p_dev) \ -do { \ - BTIF_TRACE_DEBUG("%s: ", __func__); \ - if (p_dev == NULL || p_dev->rc_connected == false) \ - { \ - BTIF_TRACE_WARNING("%s: called when RC is not connected", __func__); \ - return BT_STATUS_NOT_READY; \ - } \ -} while (0) - -#define CHECK_BR_CONNECTED(p_dev) \ -do { \ - BTIF_TRACE_DEBUG("## %s ##", __FUNCTION__); \ - if (p_dev == NULL || p_dev->br_connected == false) \ - { \ - BTIF_TRACE_WARNING("Function %s() called when BR is not connected", __FUNCTION__); \ - return BT_STATUS_NOT_READY; \ - } \ -} while (0) +#define BTIF_RC_NUM_CONN BT_RC_NUM_APP + +#define CHECK_RC_CONNECTED(p_dev) \ + do { \ + BTIF_TRACE_DEBUG("%s: ", __func__); \ + if (p_dev == NULL || p_dev->rc_connected == false) { \ + BTIF_TRACE_WARNING("%s: called when RC is not connected", __func__); \ + return BT_STATUS_NOT_READY; \ + } \ + } while (0) + +#define CHECK_BR_CONNECTED(p_dev) \ + do { \ + BTIF_TRACE_DEBUG("## %s ##", __FUNCTION__); \ + if (p_dev == NULL || p_dev->br_connected == false) { \ + BTIF_TRACE_WARNING("Function %s() called when BR is not connected", \ + __FUNCTION__); \ + return BT_STATUS_NOT_READY; \ + } \ + } while (0) /***************************************************************************** -** Local type definitions -******************************************************************************/ + * Local type definitions + *****************************************************************************/ typedef struct { - uint8_t bNotify; - uint8_t label; + uint8_t bNotify; + uint8_t label; } btif_rc_reg_notifications_t; -typedef struct -{ - uint8_t label; - uint8_t ctype; - bool is_rsp_pending; +typedef struct { + uint8_t label; + uint8_t ctype; + bool is_rsp_pending; } btif_rc_cmd_ctxt_t; /* 2 second timeout to get interim response */ -#define BTIF_TIMEOUT_RC_INTERIM_RSP_MS (2 * 1000) -#define BTIF_TIMEOUT_RC_STATUS_CMD_MS (2 * 1000) -#define BTIF_TIMEOUT_RC_CONTROL_CMD_MS (2 * 1000) - - -typedef enum -{ - eNOT_REGISTERED, - eREGISTERED, - eINTERIM +#define BTIF_TIMEOUT_RC_INTERIM_RSP_MS (2 * 1000) +#define BTIF_TIMEOUT_RC_STATUS_CMD_MS (2 * 1000) +#define BTIF_TIMEOUT_RC_CONTROL_CMD_MS (2 * 1000) + +typedef enum { + eNOT_REGISTERED, + eREGISTERED, + eINTERIM } btif_rc_nfn_reg_status_t; typedef struct { - uint8_t event_id; - uint8_t label; - btif_rc_nfn_reg_status_t status; + uint8_t event_id; + uint8_t label; + btif_rc_nfn_reg_status_t status; } btif_rc_supported_event_t; -#define BTIF_RC_STS_TIMEOUT 0xFE +#define BTIF_RC_STS_TIMEOUT 0xFE typedef struct { - uint8_t label; - uint8_t pdu_id; + uint8_t label; + uint8_t pdu_id; } btif_rc_status_cmd_timer_t; typedef struct { - uint8_t label; - uint8_t pdu_id; + uint8_t label; + uint8_t pdu_id; } btif_rc_control_cmd_timer_t; typedef struct { - union { - btif_rc_status_cmd_timer_t rc_status_cmd; - btif_rc_control_cmd_timer_t rc_control_cmd; - }; - BD_ADDR rc_addr; + union { + btif_rc_status_cmd_timer_t rc_status_cmd; + btif_rc_control_cmd_timer_t rc_control_cmd; + }; + BD_ADDR rc_addr; } btif_rc_timer_context_t; typedef struct { - bool query_started; - uint8_t num_attrs; - uint8_t num_ext_attrs; - - uint8_t attr_index; - uint8_t ext_attr_index; - uint8_t ext_val_index; - btrc_player_app_attr_t attrs[AVRC_MAX_APP_ATTR_SIZE]; - btrc_player_app_ext_attr_t ext_attrs[AVRC_MAX_APP_ATTR_SIZE]; + bool query_started; + uint8_t num_attrs; + uint8_t num_ext_attrs; + + uint8_t attr_index; + uint8_t ext_attr_index; + uint8_t ext_val_index; + btrc_player_app_attr_t attrs[AVRC_MAX_APP_ATTR_SIZE]; + btrc_player_app_ext_attr_t ext_attrs[AVRC_MAX_APP_ATTR_SIZE]; } btif_rc_player_app_settings_t; -/* TODO : Merge btif_rc_reg_notifications_t and btif_rc_cmd_ctxt_t to a single struct */ +/* TODO : Merge btif_rc_reg_notifications_t and btif_rc_cmd_ctxt_t to a single + * struct */ typedef struct { - bool rc_connected; - bool br_connected; // Browsing channel. - uint8_t rc_handle; - tBTA_AV_FEAT rc_features; - btrc_connection_state_t rc_state; - BD_ADDR rc_addr; - uint16_t rc_pending_play; - btif_rc_cmd_ctxt_t rc_pdu_info[MAX_CMD_QUEUE_LEN]; - btif_rc_reg_notifications_t rc_notif[MAX_RC_NOTIFICATIONS]; - unsigned int rc_volume; - uint8_t rc_vol_label; - list_t *rc_supported_event_list; - btif_rc_player_app_settings_t rc_app_settings; - alarm_t *rc_play_status_timer; - bool rc_features_processed; - uint64_t rc_playing_uid; - bool rc_procedure_complete; + bool rc_connected; + bool br_connected; // Browsing channel. + uint8_t rc_handle; + tBTA_AV_FEAT rc_features; + btrc_connection_state_t rc_state; + BD_ADDR rc_addr; + uint16_t rc_pending_play; + btif_rc_cmd_ctxt_t rc_pdu_info[MAX_CMD_QUEUE_LEN]; + btif_rc_reg_notifications_t rc_notif[MAX_RC_NOTIFICATIONS]; + unsigned int rc_volume; + uint8_t rc_vol_label; + list_t* rc_supported_event_list; + btif_rc_player_app_settings_t rc_app_settings; + alarm_t* rc_play_status_timer; + bool rc_features_processed; + uint64_t rc_playing_uid; + bool rc_procedure_complete; } btif_rc_device_cb_t; typedef struct { - std::mutex lock; - btif_rc_device_cb_t rc_multi_cb[BTIF_RC_NUM_CONN]; + std::mutex lock; + btif_rc_device_cb_t rc_multi_cb[BTIF_RC_NUM_CONN]; } rc_cb_t; typedef struct { - bool in_use; - uint8_t lbl; - uint8_t handle; - btif_rc_timer_context_t txn_timer_context; - alarm_t *txn_timer; + bool in_use; + uint8_t lbl; + uint8_t handle; + btif_rc_timer_context_t txn_timer_context; + alarm_t* txn_timer; } rc_transaction_t; -typedef struct -{ - std::recursive_mutex lbllock; - rc_transaction_t transaction[MAX_TRANSACTIONS_PER_SESSION]; +typedef struct { + std::recursive_mutex lbllock; + rc_transaction_t transaction[MAX_TRANSACTIONS_PER_SESSION]; } rc_device_t; -typedef struct -{ - uint8_t label; - BD_ADDR rc_addr; +typedef struct { + uint8_t label; + BD_ADDR rc_addr; } rc_context_t; rc_device_t device; #define MAX_UINPUT_PATHS 3 -static const char* uinput_dev_path[] = - {"/dev/uinput", "/dev/input/uinput", "/dev/misc/uinput" }; +static const char* uinput_dev_path[] = {"/dev/uinput", "/dev/input/uinput", + "/dev/misc/uinput"}; static int uinput_fd = -1; -static int uinput_driver_check(); -static int uinput_create(const char *name); -static int init_uinput (void); -static void close_uinput (void); +static int uinput_driver_check(); +static int uinput_create(const char* name); +static int init_uinput(void); +static void close_uinput(void); static void sleep_ms(period_ms_t timeout_ms); static const struct { - const char *name; - uint8_t avrcp; - uint16_t mapped_id; - uint8_t release_quirk; -} key_map[] = { - { "PLAY", AVRC_ID_PLAY, KEY_PLAYCD, 1 }, - { "STOP", AVRC_ID_STOP, KEY_STOPCD, 0 }, - { "PAUSE", AVRC_ID_PAUSE, KEY_PAUSECD, 1 }, - { "FORWARD", AVRC_ID_FORWARD, KEY_NEXTSONG, 0 }, - { "BACKWARD", AVRC_ID_BACKWARD, KEY_PREVIOUSSONG, 0 }, - { "REWIND", AVRC_ID_REWIND, KEY_REWIND, 0 }, - { "FAST FORWARD", AVRC_ID_FAST_FOR, KEY_FAST_FORWARD, 0 }, - { NULL, 0, 0, 0 } -}; + const char* name; + uint8_t avrcp; + uint16_t mapped_id; + uint8_t release_quirk; +} key_map[] = {{"PLAY", AVRC_ID_PLAY, KEY_PLAYCD, 1}, + {"STOP", AVRC_ID_STOP, KEY_STOPCD, 0}, + {"PAUSE", AVRC_ID_PAUSE, KEY_PAUSECD, 1}, + {"FORWARD", AVRC_ID_FORWARD, KEY_NEXTSONG, 0}, + {"BACKWARD", AVRC_ID_BACKWARD, KEY_PREVIOUSSONG, 0}, + {"REWIND", AVRC_ID_REWIND, KEY_REWIND, 0}, + {"FAST FORWARD", AVRC_ID_FAST_FOR, KEY_FAST_FORWARD, 0}, + {NULL, 0, 0, 0}}; /* Response status code - Unknown Error - this is changed to "reserved" */ -#define BTIF_STS_GEN_ERROR 0x06 +#define BTIF_STS_GEN_ERROR 0x06 -/* Utility table to map hal status codes to bta status codes for the response status */ -static const uint8_t status_code_map[] = -{ +/* Utility table to map hal status codes to bta status codes for the response + * status */ +static const uint8_t status_code_map[] = { /* BTA_Status codes HAL_Status codes */ - AVRC_STS_BAD_CMD, /* BTRC_STS_BAD_CMD */ - AVRC_STS_BAD_PARAM, /* BTRC_STS_BAD_PARAM */ - AVRC_STS_NOT_FOUND, /* BTRC_STS_NOT_FOUND */ - AVRC_STS_INTERNAL_ERR, /* BTRC_STS_INTERNAL_ERR */ - AVRC_STS_NO_ERROR, /* BTRC_STS_NO_ERROR */ - AVRC_STS_UID_CHANGED, /* BTRC_STS_UID_CHANGED */ - BTIF_STS_GEN_ERROR, /* BTRC_STS_RESERVED */ - AVRC_STS_BAD_DIR, /* BTRC_STS_INV_DIRN */ - AVRC_STS_NOT_DIR, /* BTRC_STS_INV_DIRECTORY */ - AVRC_STS_NOT_EXIST, /* BTRC_STS_INV_ITEM */ - AVRC_STS_BAD_SCOPE, /* BTRC_STS_INV_SCOPE */ - AVRC_STS_BAD_RANGE, /* BTRC_STS_INV_RANGE */ - AVRC_STS_UID_IS_DIR, /* BTRC_STS_DIRECTORY */ - AVRC_STS_IN_USE, /* BTRC_STS_MEDIA_IN_USE */ - AVRC_STS_NOW_LIST_FULL, /* BTRC_STS_PLAY_LIST_FULL */ - AVRC_STS_SEARCH_NOT_SUP, /* BTRC_STS_SRCH_NOT_SPRTD */ - AVRC_STS_SEARCH_BUSY, /* BTRC_STS_SRCH_IN_PROG */ - AVRC_STS_BAD_PLAYER_ID, /* BTRC_STS_INV_PLAYER */ - AVRC_STS_PLAYER_N_BR, /* BTRC_STS_PLAY_NOT_BROW */ - AVRC_STS_PLAYER_N_ADDR, /* BTRC_STS_PLAY_NOT_ADDR */ - AVRC_STS_BAD_SEARCH_RES, /* BTRC_STS_INV_RESULTS */ - AVRC_STS_NO_AVAL_PLAYER, /* BTRC_STS_NO_AVBL_PLAY */ - AVRC_STS_ADDR_PLAYER_CHG, /* BTRC_STS_ADDR_PLAY_CHGD */ + AVRC_STS_BAD_CMD, /* BTRC_STS_BAD_CMD */ + AVRC_STS_BAD_PARAM, /* BTRC_STS_BAD_PARAM */ + AVRC_STS_NOT_FOUND, /* BTRC_STS_NOT_FOUND */ + AVRC_STS_INTERNAL_ERR, /* BTRC_STS_INTERNAL_ERR */ + AVRC_STS_NO_ERROR, /* BTRC_STS_NO_ERROR */ + AVRC_STS_UID_CHANGED, /* BTRC_STS_UID_CHANGED */ + BTIF_STS_GEN_ERROR, /* BTRC_STS_RESERVED */ + AVRC_STS_BAD_DIR, /* BTRC_STS_INV_DIRN */ + AVRC_STS_NOT_DIR, /* BTRC_STS_INV_DIRECTORY */ + AVRC_STS_NOT_EXIST, /* BTRC_STS_INV_ITEM */ + AVRC_STS_BAD_SCOPE, /* BTRC_STS_INV_SCOPE */ + AVRC_STS_BAD_RANGE, /* BTRC_STS_INV_RANGE */ + AVRC_STS_UID_IS_DIR, /* BTRC_STS_DIRECTORY */ + AVRC_STS_IN_USE, /* BTRC_STS_MEDIA_IN_USE */ + AVRC_STS_NOW_LIST_FULL, /* BTRC_STS_PLAY_LIST_FULL */ + AVRC_STS_SEARCH_NOT_SUP, /* BTRC_STS_SRCH_NOT_SPRTD */ + AVRC_STS_SEARCH_BUSY, /* BTRC_STS_SRCH_IN_PROG */ + AVRC_STS_BAD_PLAYER_ID, /* BTRC_STS_INV_PLAYER */ + AVRC_STS_PLAYER_N_BR, /* BTRC_STS_PLAY_NOT_BROW */ + AVRC_STS_PLAYER_N_ADDR, /* BTRC_STS_PLAY_NOT_ADDR */ + AVRC_STS_BAD_SEARCH_RES, /* BTRC_STS_INV_RESULTS */ + AVRC_STS_NO_AVAL_PLAYER, /* BTRC_STS_NO_AVBL_PLAY */ + AVRC_STS_ADDR_PLAYER_CHG, /* BTRC_STS_ADDR_PLAY_CHGD */ }; -static void send_reject_response (uint8_t rc_handle, uint8_t label, - uint8_t pdu, uint8_t status, uint8_t opcode); +static void send_reject_response(uint8_t rc_handle, uint8_t label, uint8_t pdu, + uint8_t status, uint8_t opcode); static uint8_t opcode_from_pdu(uint8_t pdu); -static void send_metamsg_rsp (btif_rc_device_cb_t *p_dev, int index, uint8_t label, - tBTA_AV_CODE code, tAVRC_RESPONSE *pmetamsg_resp); +static void send_metamsg_rsp(btif_rc_device_cb_t* p_dev, int index, + uint8_t label, tBTA_AV_CODE code, + tAVRC_RESPONSE* pmetamsg_resp); #if (AVRC_ADV_CTRL_INCLUDED == TRUE) -static void register_volumechange(uint8_t label, btif_rc_device_cb_t *p_dev); +static void register_volumechange(uint8_t label, btif_rc_device_cb_t* p_dev); #endif static void lbl_init(); static void init_all_transactions(); -static bt_status_t get_transaction(rc_transaction_t **ptransaction); +static bt_status_t get_transaction(rc_transaction_t** ptransaction); static void release_transaction(uint8_t label); static rc_transaction_t* get_transaction_by_lbl(uint8_t label); #if (AVRC_ADV_CTRL_INCLUDED == TRUE) -static void handle_rc_metamsg_rsp(tBTA_AV_META_MSG *pmeta_msg, btif_rc_device_cb_t *p_dev); +static void handle_rc_metamsg_rsp(tBTA_AV_META_MSG* pmeta_msg, + btif_rc_device_cb_t* p_dev); #endif #if (AVRC_CTRL_INCLUDED == TRUE) -static void handle_avk_rc_metamsg_cmd(tBTA_AV_META_MSG *pmeta_msg); -static void handle_avk_rc_metamsg_rsp(tBTA_AV_META_MSG *pmeta_msg); -static void btif_rc_ctrl_upstreams_rsp_cmd( - uint8_t event, tAVRC_COMMAND *pavrc_cmd, uint8_t label, btif_rc_device_cb_t *p_dev); -static void rc_ctrl_procedure_complete(btif_rc_device_cb_t *p_dev); -static void rc_stop_play_status_timer(btif_rc_device_cb_t *p_dev); -static void register_for_event_notification (btif_rc_supported_event_t *p_event, - btif_rc_device_cb_t *p_dev); -static void handle_get_capability_response (tBTA_AV_META_MSG *pmeta_msg, - tAVRC_GET_CAPS_RSP *p_rsp); -static void handle_app_attr_response (tBTA_AV_META_MSG *pmeta_msg, tAVRC_LIST_APP_ATTR_RSP *p_rsp); -static void handle_app_val_response (tBTA_AV_META_MSG *pmeta_msg, - tAVRC_LIST_APP_VALUES_RSP *p_rsp); -static void handle_app_cur_val_response (tBTA_AV_META_MSG *pmeta_msg, - tAVRC_GET_CUR_APP_VALUE_RSP *p_rsp); -static void handle_app_attr_txt_response (tBTA_AV_META_MSG *pmeta_msg, - tAVRC_GET_APP_ATTR_TXT_RSP *p_rsp); -static void handle_app_attr_val_txt_response (tBTA_AV_META_MSG *pmeta_msg, - tAVRC_GET_APP_ATTR_TXT_RSP *p_rsp); -static void handle_get_playstatus_response (tBTA_AV_META_MSG *pmeta_msg, - tAVRC_GET_PLAY_STATUS_RSP *p_rsp); -static void handle_get_elem_attr_response (tBTA_AV_META_MSG *pmeta_msg, - tAVRC_GET_ELEM_ATTRS_RSP *p_rsp); -static void handle_set_app_attr_val_response (tBTA_AV_META_MSG *pmeta_msg, tAVRC_RSP *p_rsp); -static bt_status_t get_play_status_cmd(btif_rc_device_cb_t *p_dev); -static bt_status_t get_player_app_setting_attr_text_cmd (uint8_t *attrs, uint8_t num_attrs, - btif_rc_device_cb_t *p_dev); -static bt_status_t get_player_app_setting_value_text_cmd (uint8_t *vals, uint8_t num_vals, - btif_rc_device_cb_t *p_dev); -static bt_status_t register_notification_cmd (uint8_t label, uint8_t event_id, uint32_t event_value, - btif_rc_device_cb_t *p_dev); -static bt_status_t get_element_attribute_cmd (uint8_t num_attribute, uint32_t *p_attr_ids, - btif_rc_device_cb_t *p_dev); -static bt_status_t getcapabilities_cmd (uint8_t cap_id, btif_rc_device_cb_t *p_dev); -static bt_status_t list_player_app_setting_attrib_cmd(btif_rc_device_cb_t *p_dev); -static bt_status_t list_player_app_setting_value_cmd(uint8_t attrib_id, - btif_rc_device_cb_t *p_dev); -static bt_status_t get_player_app_setting_cmd(uint8_t num_attrib, uint8_t* attrib_ids, - btif_rc_device_cb_t *p_dev); -void get_folder_item_type_media(const tAVRC_ITEM *avrc_item, btrc_folder_items_t *btrc_item); -void get_folder_item_type_folder(const tAVRC_ITEM *avrc_item, btrc_folder_items_t *btrc_item); -void get_folder_item_type_player(const tAVRC_ITEM *avrc_item, btrc_folder_items_t *btrc_item); -static bt_status_t get_folder_items_cmd(bt_bdaddr_t *bd_addr, uint8_t scope, uint8_t start_item, - uint8_t num_items); +static void handle_avk_rc_metamsg_cmd(tBTA_AV_META_MSG* pmeta_msg); +static void handle_avk_rc_metamsg_rsp(tBTA_AV_META_MSG* pmeta_msg); +static void btif_rc_ctrl_upstreams_rsp_cmd(uint8_t event, + tAVRC_COMMAND* pavrc_cmd, + uint8_t label, + btif_rc_device_cb_t* p_dev); +static void rc_ctrl_procedure_complete(btif_rc_device_cb_t* p_dev); +static void rc_stop_play_status_timer(btif_rc_device_cb_t* p_dev); +static void register_for_event_notification(btif_rc_supported_event_t* p_event, + btif_rc_device_cb_t* p_dev); +static void handle_get_capability_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_GET_CAPS_RSP* p_rsp); +static void handle_app_attr_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_LIST_APP_ATTR_RSP* p_rsp); +static void handle_app_val_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_LIST_APP_VALUES_RSP* p_rsp); +static void handle_app_cur_val_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_GET_CUR_APP_VALUE_RSP* p_rsp); +static void handle_app_attr_txt_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_GET_APP_ATTR_TXT_RSP* p_rsp); +static void handle_app_attr_val_txt_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_GET_APP_ATTR_TXT_RSP* p_rsp); +static void handle_get_playstatus_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_GET_PLAY_STATUS_RSP* p_rsp); +static void handle_get_elem_attr_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_GET_ELEM_ATTRS_RSP* p_rsp); +static void handle_set_app_attr_val_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_RSP* p_rsp); +static bt_status_t get_play_status_cmd(btif_rc_device_cb_t* p_dev); +static bt_status_t get_player_app_setting_attr_text_cmd( + uint8_t* attrs, uint8_t num_attrs, btif_rc_device_cb_t* p_dev); +static bt_status_t get_player_app_setting_value_text_cmd( + uint8_t* vals, uint8_t num_vals, btif_rc_device_cb_t* p_dev); +static bt_status_t register_notification_cmd(uint8_t label, uint8_t event_id, + uint32_t event_value, + btif_rc_device_cb_t* p_dev); +static bt_status_t get_element_attribute_cmd(uint8_t num_attribute, + uint32_t* p_attr_ids, + btif_rc_device_cb_t* p_dev); +static bt_status_t getcapabilities_cmd(uint8_t cap_id, + btif_rc_device_cb_t* p_dev); +static bt_status_t list_player_app_setting_attrib_cmd( + btif_rc_device_cb_t* p_dev); +static bt_status_t list_player_app_setting_value_cmd( + uint8_t attrib_id, btif_rc_device_cb_t* p_dev); +static bt_status_t get_player_app_setting_cmd(uint8_t num_attrib, + uint8_t* attrib_ids, + btif_rc_device_cb_t* p_dev); +void get_folder_item_type_media(const tAVRC_ITEM* avrc_item, + btrc_folder_items_t* btrc_item); +void get_folder_item_type_folder(const tAVRC_ITEM* avrc_item, + btrc_folder_items_t* btrc_item); +void get_folder_item_type_player(const tAVRC_ITEM* avrc_item, + btrc_folder_items_t* btrc_item); +static bt_status_t get_folder_items_cmd(bt_bdaddr_t* bd_addr, uint8_t scope, + uint8_t start_item, uint8_t num_items); #endif -static void btif_rc_upstreams_evt(uint16_t event, tAVRC_COMMAND* p_param, uint8_t ctype, uint8_t label, - btif_rc_device_cb_t *p_dev); +static void btif_rc_upstreams_evt(uint16_t event, tAVRC_COMMAND* p_param, + uint8_t ctype, uint8_t label, + btif_rc_device_cb_t* p_dev); #if (AVRC_ADV_CTRL_INCLUDED == TRUE) -static void btif_rc_upstreams_rsp_evt(uint16_t event, tAVRC_RESPONSE *pavrc_resp, uint8_t ctype, - uint8_t label, btif_rc_device_cb_t *p_dev); +static void btif_rc_upstreams_rsp_evt(uint16_t event, + tAVRC_RESPONSE* pavrc_resp, uint8_t ctype, + uint8_t label, + btif_rc_device_cb_t* p_dev); #endif -static void rc_start_play_status_timer(btif_rc_device_cb_t *p_dev); +static void rc_start_play_status_timer(btif_rc_device_cb_t* p_dev); static bool absolute_volume_disabled(void); /***************************************************************************** -** Static variables -******************************************************************************/ -static rc_cb_t btif_rc_cb; -static btrc_callbacks_t *bt_rc_callbacks = NULL; -static btrc_ctrl_callbacks_t *bt_rc_ctrl_callbacks = NULL; + * Static variables + *****************************************************************************/ +static rc_cb_t btif_rc_cb; +static btrc_callbacks_t* bt_rc_callbacks = NULL; +static btrc_ctrl_callbacks_t* bt_rc_ctrl_callbacks = NULL; /***************************************************************************** -** Static functions -******************************************************************************/ + * Static functions + *****************************************************************************/ /***************************************************************************** -** Externs -******************************************************************************/ + * Externs + *****************************************************************************/ extern bool btif_hf_call_terminated_recently(); -extern bool check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod); +extern bool check_cod(const bt_bdaddr_t* remote_bdaddr, uint32_t cod); -extern fixed_queue_t *btu_general_alarm_queue; +extern fixed_queue_t* btu_general_alarm_queue; /***************************************************************************** -** Functions -******************************************************************************/ -static btif_rc_device_cb_t *alloc_device() -{ - for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) { - if (btif_rc_cb.rc_multi_cb[idx].rc_state == BTRC_CONNECTION_STATE_DISCONNECTED) { - return (&btif_rc_cb.rc_multi_cb[idx]); - } - } - return NULL; + * Functions + *****************************************************************************/ +static btif_rc_device_cb_t* alloc_device() { + for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) { + if (btif_rc_cb.rc_multi_cb[idx].rc_state == + BTRC_CONNECTION_STATE_DISCONNECTED) { + return (&btif_rc_cb.rc_multi_cb[idx]); + } + } + return NULL; } -static btif_rc_device_cb_t *get_connected_device(int index) -{ - BTIF_TRACE_DEBUG("%s: index: %d", __func__, index); - if (index > BTIF_RC_NUM_CONN) { - BTIF_TRACE_ERROR("%s: can't support more than %d connections", __func__, BTIF_RC_NUM_CONN); - return NULL; - } - if (btif_rc_cb.rc_multi_cb[index].rc_state != BTRC_CONNECTION_STATE_CONNECTED) { - BTIF_TRACE_ERROR("%s: returning NULL", __func__); - return NULL; - } - return (&btif_rc_cb.rc_multi_cb[index]); +static btif_rc_device_cb_t* get_connected_device(int index) { + BTIF_TRACE_DEBUG("%s: index: %d", __func__, index); + if (index > BTIF_RC_NUM_CONN) { + BTIF_TRACE_ERROR("%s: can't support more than %d connections", __func__, + BTIF_RC_NUM_CONN); + return NULL; + } + if (btif_rc_cb.rc_multi_cb[index].rc_state != + BTRC_CONNECTION_STATE_CONNECTED) { + BTIF_TRACE_ERROR("%s: returning NULL", __func__); + return NULL; + } + return (&btif_rc_cb.rc_multi_cb[index]); } -static int get_num_connected_devices() -{ - int connected_devices = 0; - for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) - { - if (btif_rc_cb.rc_multi_cb[idx].rc_state == BTRC_CONNECTION_STATE_CONNECTED) - { - connected_devices++; - } - } - BTIF_TRACE_DEBUG("%s: returning connected_devices: %d", __func__, connected_devices); - return connected_devices; +static int get_num_connected_devices() { + int connected_devices = 0; + for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) { + if (btif_rc_cb.rc_multi_cb[idx].rc_state == + BTRC_CONNECTION_STATE_CONNECTED) { + connected_devices++; + } + } + BTIF_TRACE_DEBUG("%s: returning connected_devices: %d", __func__, + connected_devices); + return connected_devices; } -btif_rc_device_cb_t *btif_rc_get_device_by_bda(bt_bdaddr_t *bd_addr) -{ - BTIF_TRACE_DEBUG("%s: bd_addr: %02x-%02x-%02x-%02x-%02x-%02x", __func__, - bd_addr[0], bd_addr[1],bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]); - - for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) - { - if ((btif_rc_cb.rc_multi_cb[idx].rc_state != BTRC_CONNECTION_STATE_DISCONNECTED) && - (bdcmp(btif_rc_cb.rc_multi_cb[idx].rc_addr, bd_addr->address) == 0)) - { - return (&btif_rc_cb.rc_multi_cb[idx]); - } - } - BTIF_TRACE_ERROR("%s: device not found, returning NULL!", __func__); - return NULL; +btif_rc_device_cb_t* btif_rc_get_device_by_bda(bt_bdaddr_t* bd_addr) { + BTIF_TRACE_DEBUG("%s: bd_addr: %02x-%02x-%02x-%02x-%02x-%02x", __func__, + bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], + bd_addr[5]); + + for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) { + if ((btif_rc_cb.rc_multi_cb[idx].rc_state != + BTRC_CONNECTION_STATE_DISCONNECTED) && + (bdcmp(btif_rc_cb.rc_multi_cb[idx].rc_addr, bd_addr->address) == 0)) { + return (&btif_rc_cb.rc_multi_cb[idx]); + } + } + BTIF_TRACE_ERROR("%s: device not found, returning NULL!", __func__); + return NULL; } -btif_rc_device_cb_t *btif_rc_get_device_by_handle(uint8_t handle) -{ - BTIF_TRACE_DEBUG("%s: handle: 0x%x", __func__, handle); - for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) - { - if ((btif_rc_cb.rc_multi_cb[idx].rc_state != BTRC_CONNECTION_STATE_DISCONNECTED) && - (btif_rc_cb.rc_multi_cb[idx].rc_handle == handle)) - { - BTIF_TRACE_DEBUG("%s: btif_rc_cb.rc_multi_cb[idx].rc_handle: 0x%x", - __func__, btif_rc_cb.rc_multi_cb[idx].rc_handle); - return (&btif_rc_cb.rc_multi_cb[idx]); - } - } - BTIF_TRACE_ERROR("%s: returning NULL", __func__); - return NULL; +btif_rc_device_cb_t* btif_rc_get_device_by_handle(uint8_t handle) { + BTIF_TRACE_DEBUG("%s: handle: 0x%x", __func__, handle); + for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) { + if ((btif_rc_cb.rc_multi_cb[idx].rc_state != + BTRC_CONNECTION_STATE_DISCONNECTED) && + (btif_rc_cb.rc_multi_cb[idx].rc_handle == handle)) { + BTIF_TRACE_DEBUG("%s: btif_rc_cb.rc_multi_cb[idx].rc_handle: 0x%x", + __func__, btif_rc_cb.rc_multi_cb[idx].rc_handle); + return (&btif_rc_cb.rc_multi_cb[idx]); + } + } + BTIF_TRACE_ERROR("%s: returning NULL", __func__); + return NULL; } void fill_pdu_queue(int index, uint8_t ctype, uint8_t label, bool pending, - btif_rc_device_cb_t *p_dev) -{ - p_dev->rc_pdu_info[index].ctype = ctype; - p_dev->rc_pdu_info[index].label = label; - p_dev->rc_pdu_info[index].is_rsp_pending = pending; + btif_rc_device_cb_t* p_dev) { + p_dev->rc_pdu_info[index].ctype = ctype; + p_dev->rc_pdu_info[index].label = label; + p_dev->rc_pdu_info[index].is_rsp_pending = pending; } -void fill_avrc_attr_entry(tAVRC_ATTR_ENTRY *attr_vals, int num_attrs, - btrc_element_attr_val_t* p_attrs) -{ - for (int attr_cnt = 0; attr_cnt < num_attrs; attr_cnt++) - { - attr_vals[attr_cnt].attr_id = p_attrs[attr_cnt].attr_id; - attr_vals[attr_cnt].name.charset_id = AVRC_CHARSET_ID_UTF8; - attr_vals[attr_cnt].name.str_len = - (uint16_t)strlen((char *)p_attrs[attr_cnt].text); - attr_vals[attr_cnt].name.p_str = p_attrs[attr_cnt].text; - BTIF_TRACE_DEBUG("%s: attr_id: 0x%x, charset_id: 0x%x, str_len: %d, str: %s", - __func__, (unsigned int)attr_vals[attr_cnt].attr_id, - attr_vals[attr_cnt].name.charset_id, attr_vals[attr_cnt].name.str_len, - attr_vals[attr_cnt].name.p_str); - } +void fill_avrc_attr_entry(tAVRC_ATTR_ENTRY* attr_vals, int num_attrs, + btrc_element_attr_val_t* p_attrs) { + for (int attr_cnt = 0; attr_cnt < num_attrs; attr_cnt++) { + attr_vals[attr_cnt].attr_id = p_attrs[attr_cnt].attr_id; + attr_vals[attr_cnt].name.charset_id = AVRC_CHARSET_ID_UTF8; + attr_vals[attr_cnt].name.str_len = + (uint16_t)strlen((char*)p_attrs[attr_cnt].text); + attr_vals[attr_cnt].name.p_str = p_attrs[attr_cnt].text; + BTIF_TRACE_DEBUG( + "%s: attr_id: 0x%x, charset_id: 0x%x, str_len: %d, str: %s", __func__, + (unsigned int)attr_vals[attr_cnt].attr_id, + attr_vals[attr_cnt].name.charset_id, attr_vals[attr_cnt].name.str_len, + attr_vals[attr_cnt].name.p_str); + } } /***************************************************************************** -** Local uinput helper functions -******************************************************************************/ + * Local uinput helper functions + *****************************************************************************/ /************** uinput related functions **************/ -int uinput_driver_check() -{ - uint32_t i; - for (i=0; i < MAX_UINPUT_PATHS; i++) - { - if (access(uinput_dev_path[i], O_RDWR) == 0) { - return 0; - } - } - BTIF_TRACE_ERROR("%s: ERROR: uinput device is not in the system", __func__); - return -1; +int uinput_driver_check() { + uint32_t i; + for (i = 0; i < MAX_UINPUT_PATHS; i++) { + if (access(uinput_dev_path[i], O_RDWR) == 0) { + return 0; + } + } + BTIF_TRACE_ERROR("%s: ERROR: uinput device is not in the system", __func__); + return -1; } -int uinput_create(const char *name) -{ - struct uinput_dev dev; - int fd, x = 0; - - for(x=0; x < MAX_UINPUT_PATHS; x++) - { - fd = open(uinput_dev_path[x], O_RDWR); - if (fd < 0) - continue; - break; - } - if (x == MAX_UINPUT_PATHS) { - BTIF_TRACE_ERROR("%s: ERROR: uinput device open failed", __func__); - return -1; - } - memset(&dev, 0, sizeof(dev)); - if (name) - strncpy(dev.name, name, UINPUT_MAX_NAME_SIZE-1); - - dev.id.bustype = BUS_BLUETOOTH; - dev.id.vendor = 0x0000; - dev.id.product = 0x0000; - dev.id.version = 0x0000; - - ssize_t ret; - OSI_NO_INTR(ret = write(fd, &dev, sizeof(dev))); - if (ret < 0) { - BTIF_TRACE_ERROR("%s: Unable to write device information", __func__); - close(fd); - return -1; - } +int uinput_create(const char* name) { + struct uinput_dev dev; + int fd, x = 0; + + for (x = 0; x < MAX_UINPUT_PATHS; x++) { + fd = open(uinput_dev_path[x], O_RDWR); + if (fd < 0) continue; + break; + } + if (x == MAX_UINPUT_PATHS) { + BTIF_TRACE_ERROR("%s: ERROR: uinput device open failed", __func__); + return -1; + } + memset(&dev, 0, sizeof(dev)); + if (name) strncpy(dev.name, name, UINPUT_MAX_NAME_SIZE - 1); + + dev.id.bustype = BUS_BLUETOOTH; + dev.id.vendor = 0x0000; + dev.id.product = 0x0000; + dev.id.version = 0x0000; + + ssize_t ret; + OSI_NO_INTR(ret = write(fd, &dev, sizeof(dev))); + if (ret < 0) { + BTIF_TRACE_ERROR("%s: Unable to write device information", __func__); + close(fd); + return -1; + } - ioctl(fd, UI_SET_EVBIT, EV_KEY); - ioctl(fd, UI_SET_EVBIT, EV_REL); - ioctl(fd, UI_SET_EVBIT, EV_SYN); + ioctl(fd, UI_SET_EVBIT, EV_KEY); + ioctl(fd, UI_SET_EVBIT, EV_REL); + ioctl(fd, UI_SET_EVBIT, EV_SYN); - for (x = 0; key_map[x].name != NULL; x++) - ioctl(fd, UI_SET_KEYBIT, key_map[x].mapped_id); + for (x = 0; key_map[x].name != NULL; x++) + ioctl(fd, UI_SET_KEYBIT, key_map[x].mapped_id); - if (ioctl(fd, UI_DEV_CREATE, NULL) < 0) { - BTIF_TRACE_ERROR("%s: Unable to create uinput device", __func__); - close(fd); - return -1; - } - return fd; + if (ioctl(fd, UI_DEV_CREATE, NULL) < 0) { + BTIF_TRACE_ERROR("%s: Unable to create uinput device", __func__); + close(fd); + return -1; + } + return fd; } -int init_uinput (void) -{ - const char *name = "AVRCP"; - - BTIF_TRACE_DEBUG("%s: ", __func__); - uinput_fd = uinput_create(name); - if (uinput_fd < 0) { - BTIF_TRACE_ERROR("%s: AVRCP: Failed to initialize uinput for %s (%d)", - __func__, name, uinput_fd); - } else { - BTIF_TRACE_DEBUG("%s: AVRCP: Initialized uinput for %s (fd: %d)", - __func__, name, uinput_fd); - } - return uinput_fd; +int init_uinput(void) { + const char* name = "AVRCP"; + + BTIF_TRACE_DEBUG("%s: ", __func__); + uinput_fd = uinput_create(name); + if (uinput_fd < 0) { + BTIF_TRACE_ERROR("%s: AVRCP: Failed to initialize uinput for %s (%d)", + __func__, name, uinput_fd); + } else { + BTIF_TRACE_DEBUG("%s: AVRCP: Initialized uinput for %s (fd: %d)", __func__, + name, uinput_fd); + } + return uinput_fd; } -void close_uinput (void) -{ - BTIF_TRACE_DEBUG("%s: ", __func__); - if (uinput_fd > 0) { - ioctl(uinput_fd, UI_DEV_DESTROY); +void close_uinput(void) { + BTIF_TRACE_DEBUG("%s: ", __func__); + if (uinput_fd > 0) { + ioctl(uinput_fd, UI_DEV_DESTROY); - close(uinput_fd); - uinput_fd = -1; - } + close(uinput_fd); + uinput_fd = -1; + } } #if (AVRC_CTRL_INCLUDED == TRUE) -void rc_cleanup_sent_cmd (void *p_data) -{ - BTIF_TRACE_DEBUG("%s: ", __func__); - -} - -void handle_rc_ctrl_features(btif_rc_device_cb_t *p_dev) -{ - if (!(p_dev->rc_features & BTA_AV_FEAT_RCTG) && - (!(p_dev->rc_features & BTA_AV_FEAT_RCCT) || - !(p_dev->rc_features & BTA_AV_FEAT_ADV_CTRL))) - { - return; - } - - bt_bdaddr_t rc_addr; - int rc_features = 0; - bdcpy(rc_addr.address, p_dev->rc_addr); - - if ((p_dev->rc_features & BTA_AV_FEAT_ADV_CTRL)&& - (p_dev->rc_features & BTA_AV_FEAT_RCCT)) - { - rc_features |= BTRC_FEAT_ABSOLUTE_VOLUME; - } - if ((p_dev->rc_features & BTA_AV_FEAT_METADATA)&& - (p_dev->rc_features & BTA_AV_FEAT_VENDOR)&& - (p_dev->rc_features_processed != true)) - { - rc_features |= BTRC_FEAT_METADATA; - /* Mark rc features processed to avoid repeating - * the AVRCP procedure every time on receiving this - * update. - */ - p_dev->rc_features_processed = true; - if (btif_av_is_sink_enabled()) - getcapabilities_cmd (AVRC_CAP_COMPANY_ID, p_dev); - } - - /* Add browsing feature capability */ - if (p_dev->rc_features & BTA_AV_FEAT_BROWSE) { - rc_features |= BTRC_FEAT_BROWSE; - } - - BTIF_TRACE_DEBUG("%s: Update rc features to CTRL: %d", __func__, rc_features); - HAL_CBACK(bt_rc_ctrl_callbacks, getrcfeatures_cb, &rc_addr, rc_features); +void rc_cleanup_sent_cmd(void* p_data) { BTIF_TRACE_DEBUG("%s: ", __func__); } + +void handle_rc_ctrl_features(btif_rc_device_cb_t* p_dev) { + if (!(p_dev->rc_features & BTA_AV_FEAT_RCTG) && + (!(p_dev->rc_features & BTA_AV_FEAT_RCCT) || + !(p_dev->rc_features & BTA_AV_FEAT_ADV_CTRL))) { + return; + } + + bt_bdaddr_t rc_addr; + int rc_features = 0; + bdcpy(rc_addr.address, p_dev->rc_addr); + + if ((p_dev->rc_features & BTA_AV_FEAT_ADV_CTRL) && + (p_dev->rc_features & BTA_AV_FEAT_RCCT)) { + rc_features |= BTRC_FEAT_ABSOLUTE_VOLUME; + } + if ((p_dev->rc_features & BTA_AV_FEAT_METADATA) && + (p_dev->rc_features & BTA_AV_FEAT_VENDOR) && + (p_dev->rc_features_processed != true)) { + rc_features |= BTRC_FEAT_METADATA; + /* Mark rc features processed to avoid repeating + * the AVRCP procedure every time on receiving this + * update. + */ + p_dev->rc_features_processed = true; + if (btif_av_is_sink_enabled()) + getcapabilities_cmd(AVRC_CAP_COMPANY_ID, p_dev); + } + + /* Add browsing feature capability */ + if (p_dev->rc_features & BTA_AV_FEAT_BROWSE) { + rc_features |= BTRC_FEAT_BROWSE; + } + + BTIF_TRACE_DEBUG("%s: Update rc features to CTRL: %d", __func__, rc_features); + HAL_CBACK(bt_rc_ctrl_callbacks, getrcfeatures_cb, &rc_addr, rc_features); } #endif -void handle_rc_features(btif_rc_device_cb_t *p_dev) -{ - if (bt_rc_callbacks == NULL) - { - BTIF_TRACE_ERROR("%s: bt_rc_callbacks NULL, returning", __func__); - return; - } +void handle_rc_features(btif_rc_device_cb_t* p_dev) { + if (bt_rc_callbacks == NULL) { + BTIF_TRACE_ERROR("%s: bt_rc_callbacks NULL, returning", __func__); + return; + } - btrc_remote_features_t rc_features = BTRC_FEAT_NONE; - bt_bdaddr_t rc_addr; + btrc_remote_features_t rc_features = BTRC_FEAT_NONE; + bt_bdaddr_t rc_addr; - bdcpy(rc_addr.address, p_dev->rc_addr); - bt_bdaddr_t avdtp_addr = btif_av_get_addr(); + bdcpy(rc_addr.address, p_dev->rc_addr); + bt_bdaddr_t avdtp_addr = btif_av_get_addr(); - bdstr_t addr1, addr2; - BTIF_TRACE_DEBUG("%s: AVDTP Address: %s AVCTP address: %s", __func__, - bdaddr_to_string(&avdtp_addr, addr1, sizeof(addr1)), - bdaddr_to_string(&rc_addr, addr2, sizeof(addr2))); + bdstr_t addr1, addr2; + BTIF_TRACE_DEBUG("%s: AVDTP Address: %s AVCTP address: %s", __func__, + bdaddr_to_string(&avdtp_addr, addr1, sizeof(addr1)), + bdaddr_to_string(&rc_addr, addr2, sizeof(addr2))); - if (interop_match_addr(INTEROP_DISABLE_ABSOLUTE_VOLUME, &rc_addr) - || absolute_volume_disabled() - || bdcmp(avdtp_addr.address, rc_addr.address)) - { - p_dev->rc_features &= ~BTA_AV_FEAT_ADV_CTRL; - } + if (interop_match_addr(INTEROP_DISABLE_ABSOLUTE_VOLUME, &rc_addr) || + absolute_volume_disabled() || + bdcmp(avdtp_addr.address, rc_addr.address)) { + p_dev->rc_features &= ~BTA_AV_FEAT_ADV_CTRL; + } - if (p_dev->rc_features & BTA_AV_FEAT_BROWSE) - { - rc_features = (btrc_remote_features_t)(rc_features | BTRC_FEAT_BROWSE); - } + if (p_dev->rc_features & BTA_AV_FEAT_BROWSE) { + rc_features = (btrc_remote_features_t)(rc_features | BTRC_FEAT_BROWSE); + } #if (AVRC_ADV_CTRL_INCLUDED == TRUE) - if ( (p_dev->rc_features & BTA_AV_FEAT_ADV_CTRL) && - (p_dev->rc_features & BTA_AV_FEAT_RCTG)) - { - rc_features = (btrc_remote_features_t)(rc_features | BTRC_FEAT_ABSOLUTE_VOLUME); - } + if ((p_dev->rc_features & BTA_AV_FEAT_ADV_CTRL) && + (p_dev->rc_features & BTA_AV_FEAT_RCTG)) { + rc_features = + (btrc_remote_features_t)(rc_features | BTRC_FEAT_ABSOLUTE_VOLUME); + } #endif - if (p_dev->rc_features & BTA_AV_FEAT_METADATA) - { - rc_features = (btrc_remote_features_t)(rc_features | BTRC_FEAT_METADATA); - } + if (p_dev->rc_features & BTA_AV_FEAT_METADATA) { + rc_features = (btrc_remote_features_t)(rc_features | BTRC_FEAT_METADATA); + } - BTIF_TRACE_DEBUG("%s: rc_features: 0x%x", __func__, rc_features); - HAL_CBACK(bt_rc_callbacks, remote_features_cb, &rc_addr, rc_features) + BTIF_TRACE_DEBUG("%s: rc_features: 0x%x", __func__, rc_features); + HAL_CBACK(bt_rc_callbacks, remote_features_cb, &rc_addr, rc_features) #if (AVRC_ADV_CTRL_INCLUDED == TRUE) - BTIF_TRACE_DEBUG("%s: Checking for feature flags in btif_rc_handler with label: %d", - __func__, p_dev->rc_vol_label); - // Register for volume change on connect - if (p_dev->rc_features & BTA_AV_FEAT_ADV_CTRL && - p_dev->rc_features & BTA_AV_FEAT_RCTG) - { - rc_transaction_t *p_transaction = NULL; - bt_status_t status = BT_STATUS_NOT_READY; - if (MAX_LABEL == p_dev->rc_vol_label) - { - status = get_transaction(&p_transaction); - } - else - { - p_transaction=get_transaction_by_lbl(p_dev->rc_vol_label); - if (NULL!=p_transaction) - { - BTIF_TRACE_DEBUG("%s: register_volumechange already in progress for label: %d", - __func__, p_dev->rc_vol_label); - return; - } - else - status = get_transaction(&p_transaction); - } - if (BT_STATUS_SUCCESS == status && NULL != p_transaction) - { - p_dev->rc_vol_label = p_transaction->lbl; - register_volumechange(p_dev->rc_vol_label, p_dev); - } + BTIF_TRACE_DEBUG( + "%s: Checking for feature flags in btif_rc_handler with label: %d", + __func__, p_dev->rc_vol_label); + // Register for volume change on connect + if (p_dev->rc_features & BTA_AV_FEAT_ADV_CTRL && + p_dev->rc_features & BTA_AV_FEAT_RCTG) { + rc_transaction_t* p_transaction = NULL; + bt_status_t status = BT_STATUS_NOT_READY; + if (MAX_LABEL == p_dev->rc_vol_label) { + status = get_transaction(&p_transaction); + } else { + p_transaction = get_transaction_by_lbl(p_dev->rc_vol_label); + if (NULL != p_transaction) { + BTIF_TRACE_DEBUG( + "%s: register_volumechange already in progress for label: %d", + __func__, p_dev->rc_vol_label); + return; + } else + status = get_transaction(&p_transaction); } + if (BT_STATUS_SUCCESS == status && NULL != p_transaction) { + p_dev->rc_vol_label = p_transaction->lbl; + register_volumechange(p_dev->rc_vol_label, p_dev); + } + } #endif } @@ -701,26 +680,27 @@ void handle_rc_features(btif_rc_device_cb_t *p_dev) * - Description: browse RC connection event handler * ***************************************************************************/ -void handle_rc_browse_connect(tBTA_AV_RC_OPEN *p_rc_open) { - BTIF_TRACE_DEBUG("%s rc_handle %d status %d", - __func__, p_rc_open->rc_handle, p_rc_open->status); +void handle_rc_browse_connect(tBTA_AV_RC_OPEN* p_rc_open) { + BTIF_TRACE_DEBUG("%s rc_handle %d status %d", __func__, p_rc_open->rc_handle, + p_rc_open->status); #if (AVRC_CTRL_INCLUDED == TRUE) - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_handle(p_rc_open->rc_handle); - - if (!p_dev) { - BTIF_TRACE_ERROR("%s p_dev is null", __func__); - return; - } - - /* check that we are already connected to this address since being connected - * to a browse when not connected to the control channel over AVRCP is - * probably not preferred anyways. */ - if (p_rc_open->status == BTA_AV_SUCCESS) { - bt_bdaddr_t rc_addr; - bdcpy(rc_addr.address, p_dev->rc_addr); - p_dev->br_connected = true; - HAL_CBACK(bt_rc_ctrl_callbacks, connection_state_cb, true, true, &rc_addr); - } + btif_rc_device_cb_t* p_dev = + btif_rc_get_device_by_handle(p_rc_open->rc_handle); + + if (!p_dev) { + BTIF_TRACE_ERROR("%s p_dev is null", __func__); + return; + } + + /* check that we are already connected to this address since being connected + * to a browse when not connected to the control channel over AVRCP is + * probably not preferred anyways. */ + if (p_rc_open->status == BTA_AV_SUCCESS) { + bt_bdaddr_t rc_addr; + bdcpy(rc_addr.address, p_dev->rc_addr); + p_dev->br_connected = true; + HAL_CBACK(bt_rc_ctrl_callbacks, connection_state_cb, true, true, &rc_addr); + } #endif } @@ -732,78 +712,71 @@ void handle_rc_browse_connect(tBTA_AV_RC_OPEN *p_rc_open) { * - Description: RC connection event handler * ***************************************************************************/ -void handle_rc_connect (tBTA_AV_RC_OPEN *p_rc_open) -{ - BTIF_TRACE_DEBUG("%s: rc_handle: %d", __func__, p_rc_open->rc_handle); - bt_status_t result = BT_STATUS_SUCCESS; +void handle_rc_connect(tBTA_AV_RC_OPEN* p_rc_open) { + BTIF_TRACE_DEBUG("%s: rc_handle: %d", __func__, p_rc_open->rc_handle); + bt_status_t result = BT_STATUS_SUCCESS; #if (AVRC_CTRL_INCLUDED == TRUE) - bt_bdaddr_t rc_addr; + bt_bdaddr_t rc_addr; #endif - btif_rc_device_cb_t *p_dev = alloc_device(); - if ( p_dev == NULL ) - { - BTIF_TRACE_ERROR("%s: p_dev is NULL", __func__); - return; - } - - if (!(p_rc_open->status == BTA_AV_SUCCESS)) - { - BTIF_TRACE_ERROR("%s: Connect failed with error code: %d", - __func__, p_rc_open->status); - p_dev->rc_connected = false; - } - - //check if already some RC is connected - if (p_dev->rc_connected) - { - BTIF_TRACE_ERROR("%s: Got RC OPEN in connected state, Connected RC: %d \ - and Current RC: %d", __func__, p_dev->rc_handle, p_rc_open->rc_handle ); - if ((p_dev->rc_handle != p_rc_open->rc_handle) - && (bdcmp(p_dev->rc_addr, p_rc_open->peer_addr))) - { - BTIF_TRACE_DEBUG("%s: Got RC connected for some other handle", __func__); - BTA_AvCloseRc(p_rc_open->rc_handle); - return; - } - } - memcpy(p_dev->rc_addr, p_rc_open->peer_addr, sizeof(BD_ADDR)); - p_dev->rc_features = p_rc_open->peer_features; - p_dev->rc_vol_label = MAX_LABEL; - p_dev->rc_volume = MAX_VOLUME; - - p_dev->rc_connected = true; - p_dev->rc_handle = p_rc_open->rc_handle; - p_dev->rc_state = BTRC_CONNECTION_STATE_CONNECTED; - /* on locally initiated connection we will get remote features as part of connect */ - if (p_dev->rc_features != 0) - { - handle_rc_features(p_dev); - } - - if (bt_rc_callbacks) - { - result = (bt_status_t)uinput_driver_check(); - if (result == BT_STATUS_SUCCESS) - { - init_uinput(); - } - } - else - { - BTIF_TRACE_WARNING("%s: Avrcp TG role not enabled, not initializing UInput", - __func__); - } - BTIF_TRACE_DEBUG("%s: handle_rc_connect features: %d ",__func__, p_dev->rc_features); + btif_rc_device_cb_t* p_dev = alloc_device(); + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: p_dev is NULL", __func__); + return; + } + + if (!(p_rc_open->status == BTA_AV_SUCCESS)) { + BTIF_TRACE_ERROR("%s: Connect failed with error code: %d", __func__, + p_rc_open->status); + p_dev->rc_connected = false; + } + + // check if already some RC is connected + if (p_dev->rc_connected) { + BTIF_TRACE_ERROR( + "%s: Got RC OPEN in connected state, Connected RC: %d \ + and Current RC: %d", + __func__, p_dev->rc_handle, p_rc_open->rc_handle); + if ((p_dev->rc_handle != p_rc_open->rc_handle) && + (bdcmp(p_dev->rc_addr, p_rc_open->peer_addr))) { + BTIF_TRACE_DEBUG("%s: Got RC connected for some other handle", __func__); + BTA_AvCloseRc(p_rc_open->rc_handle); + return; + } + } + memcpy(p_dev->rc_addr, p_rc_open->peer_addr, sizeof(BD_ADDR)); + p_dev->rc_features = p_rc_open->peer_features; + p_dev->rc_vol_label = MAX_LABEL; + p_dev->rc_volume = MAX_VOLUME; + + p_dev->rc_connected = true; + p_dev->rc_handle = p_rc_open->rc_handle; + p_dev->rc_state = BTRC_CONNECTION_STATE_CONNECTED; + /* on locally initiated connection we will get remote features as part of + * connect */ + if (p_dev->rc_features != 0) { + handle_rc_features(p_dev); + } + + if (bt_rc_callbacks) { + result = (bt_status_t)uinput_driver_check(); + if (result == BT_STATUS_SUCCESS) { + init_uinput(); + } + } else { + BTIF_TRACE_WARNING("%s: Avrcp TG role not enabled, not initializing UInput", + __func__); + } + BTIF_TRACE_DEBUG("%s: handle_rc_connect features: %d ", __func__, + p_dev->rc_features); #if (AVRC_CTRL_INCLUDED == TRUE) - p_dev->rc_playing_uid = RC_INVALID_TRACK_ID; - bdcpy(rc_addr.address, p_dev->rc_addr); - if (bt_rc_ctrl_callbacks != NULL) - { - HAL_CBACK(bt_rc_ctrl_callbacks, connection_state_cb, true, false, &rc_addr); - } - /* report connection state if remote device is AVRCP target */ - handle_rc_ctrl_features(p_dev); + p_dev->rc_playing_uid = RC_INVALID_TRACK_ID; + bdcpy(rc_addr.address, p_dev->rc_addr); + if (bt_rc_ctrl_callbacks != NULL) { + HAL_CBACK(bt_rc_ctrl_callbacks, connection_state_cb, true, false, &rc_addr); + } + /* report connection state if remote device is AVRCP target */ + handle_rc_ctrl_features(p_dev); #endif } @@ -815,74 +788,66 @@ void handle_rc_connect (tBTA_AV_RC_OPEN *p_rc_open) * - Description: RC disconnection event handler * ***************************************************************************/ -void handle_rc_disconnect (tBTA_AV_RC_CLOSE *p_rc_close) -{ - btif_rc_device_cb_t *p_dev = NULL; +void handle_rc_disconnect(tBTA_AV_RC_CLOSE* p_rc_close) { + btif_rc_device_cb_t* p_dev = NULL; #if (AVRC_CTRL_INCLUDED == TRUE) - bt_bdaddr_t rc_addr; - tBTA_AV_FEAT features = 0; + bt_bdaddr_t rc_addr; + tBTA_AV_FEAT features = 0; #endif - BTIF_TRACE_DEBUG("%s: rc_handle: %d", __func__, p_rc_close->rc_handle); - - p_dev = btif_rc_get_device_by_handle(p_rc_close->rc_handle); - if (p_dev == NULL) - { - BTIF_TRACE_ERROR("%s: Got disconnect from invalid rc handle", __func__); - return; - } - - if ((p_rc_close->rc_handle != p_dev->rc_handle) - && (bdcmp(p_dev->rc_addr, p_rc_close->peer_addr))) - { - BTIF_TRACE_ERROR("Got disconnect of unknown device"); - return; - } + BTIF_TRACE_DEBUG("%s: rc_handle: %d", __func__, p_rc_close->rc_handle); + + p_dev = btif_rc_get_device_by_handle(p_rc_close->rc_handle); + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: Got disconnect from invalid rc handle", __func__); + return; + } + + if ((p_rc_close->rc_handle != p_dev->rc_handle) && + (bdcmp(p_dev->rc_addr, p_rc_close->peer_addr))) { + BTIF_TRACE_ERROR("Got disconnect of unknown device"); + return; + } #if (AVRC_CTRL_INCLUDED == TRUE) - bdcpy(rc_addr.address, p_dev->rc_addr); - features = p_dev->rc_features; - /* Clean up AVRCP procedure flags */ - memset(&p_dev->rc_app_settings, 0, - sizeof(btif_rc_player_app_settings_t)); - p_dev->rc_features_processed = false; - p_dev->rc_procedure_complete = false; - rc_stop_play_status_timer(p_dev); - /* Check and clear the notification event list */ - if (p_dev->rc_supported_event_list != NULL) - { - list_clear(p_dev->rc_supported_event_list); - p_dev->rc_supported_event_list = NULL; - } + bdcpy(rc_addr.address, p_dev->rc_addr); + features = p_dev->rc_features; + /* Clean up AVRCP procedure flags */ + memset(&p_dev->rc_app_settings, 0, sizeof(btif_rc_player_app_settings_t)); + p_dev->rc_features_processed = false; + p_dev->rc_procedure_complete = false; + rc_stop_play_status_timer(p_dev); + /* Check and clear the notification event list */ + if (p_dev->rc_supported_event_list != NULL) { + list_clear(p_dev->rc_supported_event_list); + p_dev->rc_supported_event_list = NULL; + } #endif - /* check if there is another device connected */ - if (p_dev->rc_state == BTRC_CONNECTION_STATE_CONNECTED) - { - p_dev->rc_handle = 0; - p_dev->rc_connected = false; - p_dev->rc_state = BTRC_CONNECTION_STATE_DISCONNECTED; - - memset(p_dev->rc_notif, 0, sizeof(p_dev->rc_notif)); + /* check if there is another device connected */ + if (p_dev->rc_state == BTRC_CONNECTION_STATE_CONNECTED) { + p_dev->rc_handle = 0; + p_dev->rc_connected = false; + p_dev->rc_state = BTRC_CONNECTION_STATE_DISCONNECTED; - p_dev->rc_features = 0; - p_dev->rc_vol_label=MAX_LABEL; - p_dev->rc_volume=MAX_VOLUME; + memset(p_dev->rc_notif, 0, sizeof(p_dev->rc_notif)); - memset(p_dev->rc_addr, 0, sizeof(BD_ADDR)); - - } - if (get_num_connected_devices() == 0) - { - BTIF_TRACE_DEBUG("%s: Closing all handles", __func__); - init_all_transactions(); - } + p_dev->rc_features = 0; + p_dev->rc_vol_label = MAX_LABEL; + p_dev->rc_volume = MAX_VOLUME; memset(p_dev->rc_addr, 0, sizeof(BD_ADDR)); + } + if (get_num_connected_devices() == 0) { + BTIF_TRACE_DEBUG("%s: Closing all handles", __func__); + init_all_transactions(); + } + + memset(p_dev->rc_addr, 0, sizeof(BD_ADDR)); #if (AVRC_CTRL_INCLUDED == TRUE) - /* report connection state if device is AVRCP target */ - if (bt_rc_ctrl_callbacks != NULL) - { - HAL_CBACK(bt_rc_ctrl_callbacks, connection_state_cb, false, false, &rc_addr); - } + /* report connection state if device is AVRCP target */ + if (bt_rc_ctrl_callbacks != NULL) { + HAL_CBACK(bt_rc_ctrl_callbacks, connection_state_cb, false, false, + &rc_addr); + } #endif } @@ -895,71 +860,75 @@ void handle_rc_disconnect (tBTA_AV_RC_CLOSE *p_rc_close) * - Description: Remote control command handler * ***************************************************************************/ -void handle_rc_passthrough_cmd (tBTA_AV_REMOTE_CMD *p_remote_cmd) -{ - btif_rc_device_cb_t *p_dev = NULL; - const char *status; - int pressed; - bt_bdaddr_t rc_addr; - - p_dev = btif_rc_get_device_by_handle(p_remote_cmd->rc_handle); - if (p_dev == NULL) - { - BTIF_TRACE_ERROR("%s: Got passthrough command from invalid rc handle", __func__); - return; - } - - bdcpy(rc_addr.address, p_dev->rc_addr); - - BTIF_TRACE_DEBUG("%s: p_remote_cmd->rc_id: %d", __func__, p_remote_cmd->rc_id); - - /* If AVRC is open and peer sends PLAY but there is no AVDT, then we queue-up this PLAY */ - if (p_remote_cmd) - { - /* queue AVRC PLAY if GAVDTP Open notification to app is pending (2 second timer) */ - if ((p_remote_cmd->rc_id == BTA_AV_RC_PLAY) && (!btif_av_is_connected())) - { - if (p_remote_cmd->key_state == AVRC_STATE_PRESS) - { - APPL_TRACE_WARNING("%s: AVDT not open, queuing the PLAY command", __func__); - p_dev->rc_pending_play = true; - } - return; - } - if ((p_remote_cmd->rc_id == BTA_AV_RC_PAUSE) && (p_dev->rc_pending_play)) - { - APPL_TRACE_WARNING("%s: Clear the pending PLAY on PAUSE received", __func__); - p_dev->rc_pending_play = false; - return; - } - if ((p_remote_cmd->rc_id == BTA_AV_RC_VOL_UP)||(p_remote_cmd->rc_id == BTA_AV_RC_VOL_DOWN)) - return; // this command is not to be sent to UINPUT, only needed for PTS - } - - if ((p_remote_cmd->rc_id == BTA_AV_RC_STOP) && (!btif_av_stream_started_ready())) - { - APPL_TRACE_WARNING("%s: Stream suspended, ignore STOP cmd", __func__); - return; - } - - if (p_remote_cmd->key_state == AVRC_STATE_RELEASE) { - status = "released"; - pressed = 0; - } else { - status = "pressed"; - pressed = 1; - } - - if (p_remote_cmd->rc_id == BTA_AV_RC_FAST_FOR || p_remote_cmd->rc_id == BTA_AV_RC_REWIND) { - HAL_CBACK(bt_rc_callbacks, passthrough_cmd_cb, p_remote_cmd->rc_id, pressed, &rc_addr); - return; - } - - /* handle all pass through commands in upper layers through Android media apis */ - BTIF_TRACE_DEBUG("%s: rc_features: %d, cmd->rc_id: %d, pressed: %d", __func__, - p_dev->rc_features, p_remote_cmd->rc_id, pressed); - HAL_CBACK(bt_rc_callbacks, passthrough_cmd_cb, p_remote_cmd->rc_id, pressed, &rc_addr); - +void handle_rc_passthrough_cmd(tBTA_AV_REMOTE_CMD* p_remote_cmd) { + btif_rc_device_cb_t* p_dev = NULL; + const char* status; + int pressed; + bt_bdaddr_t rc_addr; + + p_dev = btif_rc_get_device_by_handle(p_remote_cmd->rc_handle); + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: Got passthrough command from invalid rc handle", + __func__); + return; + } + + bdcpy(rc_addr.address, p_dev->rc_addr); + + BTIF_TRACE_DEBUG("%s: p_remote_cmd->rc_id: %d", __func__, + p_remote_cmd->rc_id); + + /* If AVRC is open and peer sends PLAY but there is no AVDT, then we queue-up + * this PLAY */ + if (p_remote_cmd) { + /* queue AVRC PLAY if GAVDTP Open notification to app is pending (2 second + * timer) */ + if ((p_remote_cmd->rc_id == BTA_AV_RC_PLAY) && (!btif_av_is_connected())) { + if (p_remote_cmd->key_state == AVRC_STATE_PRESS) { + APPL_TRACE_WARNING("%s: AVDT not open, queuing the PLAY command", + __func__); + p_dev->rc_pending_play = true; + } + return; + } + if ((p_remote_cmd->rc_id == BTA_AV_RC_PAUSE) && (p_dev->rc_pending_play)) { + APPL_TRACE_WARNING("%s: Clear the pending PLAY on PAUSE received", + __func__); + p_dev->rc_pending_play = false; + return; + } + if ((p_remote_cmd->rc_id == BTA_AV_RC_VOL_UP) || + (p_remote_cmd->rc_id == BTA_AV_RC_VOL_DOWN)) + return; // this command is not to be sent to UINPUT, only needed for PTS + } + + if ((p_remote_cmd->rc_id == BTA_AV_RC_STOP) && + (!btif_av_stream_started_ready())) { + APPL_TRACE_WARNING("%s: Stream suspended, ignore STOP cmd", __func__); + return; + } + + if (p_remote_cmd->key_state == AVRC_STATE_RELEASE) { + status = "released"; + pressed = 0; + } else { + status = "pressed"; + pressed = 1; + } + + if (p_remote_cmd->rc_id == BTA_AV_RC_FAST_FOR || + p_remote_cmd->rc_id == BTA_AV_RC_REWIND) { + HAL_CBACK(bt_rc_callbacks, passthrough_cmd_cb, p_remote_cmd->rc_id, pressed, + &rc_addr); + return; + } + + /* handle all pass through commands in upper layers through Android media apis + */ + BTIF_TRACE_DEBUG("%s: rc_features: %d, cmd->rc_id: %d, pressed: %d", __func__, + p_dev->rc_features, p_remote_cmd->rc_id, pressed); + HAL_CBACK(bt_rc_callbacks, passthrough_cmd_cb, p_remote_cmd->rc_id, pressed, + &rc_addr); } /*************************************************************************** @@ -970,50 +939,45 @@ void handle_rc_passthrough_cmd (tBTA_AV_REMOTE_CMD *p_remote_cmd) * - Description: Remote control passthrough response handler * ***************************************************************************/ -void handle_rc_passthrough_rsp ( tBTA_AV_REMOTE_RSP *p_remote_rsp) -{ - btif_rc_device_cb_t *p_dev = NULL; - bt_bdaddr_t rc_addr; +void handle_rc_passthrough_rsp(tBTA_AV_REMOTE_RSP* p_remote_rsp) { + btif_rc_device_cb_t* p_dev = NULL; + bt_bdaddr_t rc_addr; - p_dev = btif_rc_get_device_by_handle(p_remote_rsp->rc_handle); - if (p_dev == NULL) - { - BTIF_TRACE_ERROR("%s: passthrough response for Invalid rc handle", __func__); - return; - } + p_dev = btif_rc_get_device_by_handle(p_remote_rsp->rc_handle); + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: passthrough response for Invalid rc handle", + __func__); + return; + } - bdcpy(rc_addr.address, p_dev->rc_addr); + bdcpy(rc_addr.address, p_dev->rc_addr); #if (AVRC_CTRL_INCLUDED == TRUE) - const char *status; - if (p_dev->rc_features & BTA_AV_FEAT_RCTG) - { - int key_state; - if (p_remote_rsp->key_state == AVRC_STATE_RELEASE) - { - status = "released"; - key_state = 1; - } - else - { - status = "pressed"; - key_state = 0; - } + const char* status; + if (p_dev->rc_features & BTA_AV_FEAT_RCTG) { + int key_state; + if (p_remote_rsp->key_state == AVRC_STATE_RELEASE) { + status = "released"; + key_state = 1; + } else { + status = "pressed"; + key_state = 0; + } - BTIF_TRACE_DEBUG("%s: rc_id: %d status: %s", __func__, p_remote_rsp->rc_id, status); + BTIF_TRACE_DEBUG("%s: rc_id: %d status: %s", __func__, p_remote_rsp->rc_id, + status); - release_transaction(p_remote_rsp->label); - if (bt_rc_ctrl_callbacks != NULL) { - HAL_CBACK(bt_rc_ctrl_callbacks, passthrough_rsp_cb, &rc_addr, - p_remote_rsp->rc_id, key_state); - } - } - else - { - BTIF_TRACE_ERROR("%s: DUT does not support AVRCP controller role", __func__); + release_transaction(p_remote_rsp->label); + if (bt_rc_ctrl_callbacks != NULL) { + HAL_CBACK(bt_rc_ctrl_callbacks, passthrough_rsp_cb, &rc_addr, + p_remote_rsp->rc_id, key_state); } + } else { + BTIF_TRACE_ERROR("%s: DUT does not support AVRCP controller role", + __func__); + } #else - BTIF_TRACE_ERROR("%s: AVRCP controller role is not enabled", __func__); + BTIF_TRACE_ERROR("%s: AVRCP controller role is not enabled", __func__); #endif } @@ -1025,67 +989,60 @@ void handle_rc_passthrough_rsp ( tBTA_AV_REMOTE_RSP *p_remote_rsp) * - Description: Remote control vendor unique response handler * ***************************************************************************/ -void handle_rc_vendorunique_rsp ( tBTA_AV_REMOTE_RSP *p_remote_rsp) -{ +void handle_rc_vendorunique_rsp(tBTA_AV_REMOTE_RSP* p_remote_rsp) { #if (AVRC_CTRL_INCLUDED == TRUE) - btif_rc_device_cb_t *p_dev = NULL; - const char *status; - uint8_t vendor_id = 0; - - p_dev = btif_rc_get_device_by_handle(p_remote_rsp->rc_handle); - if (p_dev == NULL) - { - BTIF_TRACE_ERROR("%s: Got vendorunique rsp from invalid rc handle", __func__); - return; + btif_rc_device_cb_t* p_dev = NULL; + const char* status; + uint8_t vendor_id = 0; + + p_dev = btif_rc_get_device_by_handle(p_remote_rsp->rc_handle); + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: Got vendorunique rsp from invalid rc handle", + __func__); + return; + } + + if (p_dev->rc_features & BTA_AV_FEAT_RCTG) { + int key_state; + if (p_remote_rsp->key_state == AVRC_STATE_RELEASE) { + status = "released"; + key_state = 1; + } else { + status = "pressed"; + key_state = 0; } - if (p_dev->rc_features & BTA_AV_FEAT_RCTG) - { - int key_state; - if (p_remote_rsp->key_state == AVRC_STATE_RELEASE) - { - status = "released"; - key_state = 1; - } - else - { - status = "pressed"; - key_state = 0; - } - - if (p_remote_rsp->len > 0) - { - if (p_remote_rsp->len >= AVRC_PASS_THRU_GROUP_LEN) - vendor_id = p_remote_rsp->p_data[AVRC_PASS_THRU_GROUP_LEN -1]; - osi_free_and_reset((void **)&p_remote_rsp->p_data); - } - BTIF_TRACE_DEBUG("%s: vendor_id: %d status: %s", __func__, vendor_id, status); - - release_transaction(p_remote_rsp->label); - HAL_CBACK(bt_rc_ctrl_callbacks, groupnavigation_rsp_cb, vendor_id, key_state); - } - else - { - BTIF_TRACE_ERROR("%s: Remote does not support AVRCP TG role", __func__); + if (p_remote_rsp->len > 0) { + if (p_remote_rsp->len >= AVRC_PASS_THRU_GROUP_LEN) + vendor_id = p_remote_rsp->p_data[AVRC_PASS_THRU_GROUP_LEN - 1]; + osi_free_and_reset((void**)&p_remote_rsp->p_data); } + BTIF_TRACE_DEBUG("%s: vendor_id: %d status: %s", __func__, vendor_id, + status); + + release_transaction(p_remote_rsp->label); + HAL_CBACK(bt_rc_ctrl_callbacks, groupnavigation_rsp_cb, vendor_id, + key_state); + } else { + BTIF_TRACE_ERROR("%s: Remote does not support AVRCP TG role", __func__); + } #else - BTIF_TRACE_ERROR("%s: AVRCP controller role is not enabled", __func__); + BTIF_TRACE_ERROR("%s: AVRCP controller role is not enabled", __func__); #endif } -void handle_uid_changed_notification(btif_rc_device_cb_t *p_dev, uint8_t label, tAVRC_COMMAND *pavrc_command) -{ - tAVRC_RESPONSE avrc_rsp = {0}; - avrc_rsp.rsp.pdu = pavrc_command->pdu; - avrc_rsp.rsp.status = AVRC_STS_NO_ERROR; - avrc_rsp.rsp.opcode = pavrc_command->cmd.opcode; +void handle_uid_changed_notification(btif_rc_device_cb_t* p_dev, uint8_t label, + tAVRC_COMMAND* pavrc_command) { + tAVRC_RESPONSE avrc_rsp = {0}; + avrc_rsp.rsp.pdu = pavrc_command->pdu; + avrc_rsp.rsp.status = AVRC_STS_NO_ERROR; + avrc_rsp.rsp.opcode = pavrc_command->cmd.opcode; - avrc_rsp.reg_notif.event_id = pavrc_command->reg_notif.event_id; - avrc_rsp.reg_notif.param.uid_counter = 0; - - send_metamsg_rsp(p_dev, -1, label, AVRC_RSP_INTERIM, &avrc_rsp); - send_metamsg_rsp(p_dev, -1, label, AVRC_RSP_CHANGED, &avrc_rsp); + avrc_rsp.reg_notif.event_id = pavrc_command->reg_notif.event_id; + avrc_rsp.reg_notif.param.uid_counter = 0; + send_metamsg_rsp(p_dev, -1, label, AVRC_RSP_INTERIM, &avrc_rsp); + send_metamsg_rsp(p_dev, -1, label, AVRC_RSP_CHANGED, &avrc_rsp); } /*************************************************************************** @@ -1097,118 +1054,112 @@ void handle_uid_changed_notification(btif_rc_device_cb_t *p_dev, uint8_t label, * - Description: Remote control metamsg command handler (AVRCP 1.3) * ***************************************************************************/ -void handle_rc_metamsg_cmd (tBTA_AV_META_MSG *pmeta_msg) -{ - /* Parse the metamsg command and pass it on to BTL-IFS */ - uint8_t scratch_buf[512] = {0}; - tAVRC_COMMAND avrc_command = {0}; - tAVRC_STS status; - btif_rc_device_cb_t *p_dev = NULL; - - if (NULL == pmeta_msg) - { - BTIF_TRACE_EVENT("%s: Exiting as pmeta_msg is NULL", __func__); - return; - } - - if (NULL == pmeta_msg->p_msg) - { - BTIF_TRACE_EVENT("%s: Exiting as pmeta_msg->p_msg is NULL", __func__); - return; - } - - BTIF_TRACE_EVENT("%s: pmeta_msg: opcode: %x, code: %x", __func__, - pmeta_msg->p_msg->hdr.opcode, pmeta_msg->code); - - p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle); - if (p_dev == NULL) - { - BTIF_TRACE_ERROR("%s: Meta msg event for Invalid rc handle", __func__); - return; - } - - if (pmeta_msg->p_msg->hdr.opcode != AVRC_OP_VENDOR && - pmeta_msg->p_msg->hdr.opcode != AVRC_OP_BROWSE) +void handle_rc_metamsg_cmd(tBTA_AV_META_MSG* pmeta_msg) { + /* Parse the metamsg command and pass it on to BTL-IFS */ + uint8_t scratch_buf[512] = {0}; + tAVRC_COMMAND avrc_command = {0}; + tAVRC_STS status; + btif_rc_device_cb_t* p_dev = NULL; + + if (NULL == pmeta_msg) { + BTIF_TRACE_EVENT("%s: Exiting as pmeta_msg is NULL", __func__); + return; + } + + if (NULL == pmeta_msg->p_msg) { + BTIF_TRACE_EVENT("%s: Exiting as pmeta_msg->p_msg is NULL", __func__); + return; + } + + BTIF_TRACE_EVENT("%s: pmeta_msg: opcode: %x, code: %x", __func__, + pmeta_msg->p_msg->hdr.opcode, pmeta_msg->code); + + p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle); + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: Meta msg event for Invalid rc handle", __func__); + return; + } + + if (pmeta_msg->p_msg->hdr.opcode != AVRC_OP_VENDOR && + pmeta_msg->p_msg->hdr.opcode != AVRC_OP_BROWSE) { + BTIF_TRACE_WARNING("Invalid opcode: %x", pmeta_msg->p_msg->hdr.opcode); + return; + } + + if (pmeta_msg->len < 3) { + BTIF_TRACE_WARNING("%s: Invalid length. opcode: 0x%x, len: 0x%x", __func__, + pmeta_msg->p_msg->hdr.opcode, pmeta_msg->len); + return; + } + + if (pmeta_msg->code >= AVRC_RSP_NOT_IMPL) { +#if (AVRC_ADV_CTRL_INCLUDED == TRUE) { - BTIF_TRACE_WARNING("Invalid opcode: %x", pmeta_msg->p_msg->hdr.opcode); - return; + rc_transaction_t* transaction = NULL; + transaction = get_transaction_by_lbl(pmeta_msg->label); + if (NULL != transaction) { + handle_rc_metamsg_rsp(pmeta_msg, p_dev); + } else { + BTIF_TRACE_DEBUG( + "%s: Discard vendor dependent rsp. code: %d label: %d.", __func__, + pmeta_msg->code, pmeta_msg->label); + } + return; } - - if (pmeta_msg->len < 3) +#else { - BTIF_TRACE_WARNING("%s: Invalid length. opcode: 0x%x, len: 0x%x", __func__, - pmeta_msg->p_msg->hdr.opcode, pmeta_msg->len); - return; + BTIF_TRACE_DEBUG( + "%s: Received vendor dependent rsp. code: %d len: %d. Not processing " + "it.", + __func__, pmeta_msg->code, pmeta_msg->len); + return; } - - if (pmeta_msg->code >= AVRC_RSP_NOT_IMPL) - { -#if (AVRC_ADV_CTRL_INCLUDED == TRUE) -{ - rc_transaction_t *transaction = NULL; - transaction = get_transaction_by_lbl(pmeta_msg->label); - if (NULL != transaction) - { - handle_rc_metamsg_rsp(pmeta_msg, p_dev); - } - else - { - BTIF_TRACE_DEBUG("%s: Discard vendor dependent rsp. code: %d label: %d.", - __func__, pmeta_msg->code, pmeta_msg->label); - } - return; -} -#else -{ - BTIF_TRACE_DEBUG("%s: Received vendor dependent rsp. code: %d len: %d. Not processing it.", - __func__, pmeta_msg->code, pmeta_msg->len); - return; -} #endif + } + + status = AVRC_ParsCommand(pmeta_msg->p_msg, &avrc_command, scratch_buf, + sizeof(scratch_buf)); + BTIF_TRACE_DEBUG("%s: Received vendor command.code,PDU and label: %d, %d, %d", + __func__, pmeta_msg->code, avrc_command.cmd.pdu, + pmeta_msg->label); + + if (status != AVRC_STS_NO_ERROR) { + /* return error */ + BTIF_TRACE_WARNING( + "%s: Error in parsing received metamsg command. status: 0x%02x", + __func__, status); + send_reject_response(pmeta_msg->rc_handle, pmeta_msg->label, + avrc_command.pdu, status, + pmeta_msg->p_msg->hdr.opcode); + } else { + /* if RegisterNotification, add it to our registered queue */ + + if (avrc_command.cmd.pdu == AVRC_PDU_REGISTER_NOTIFICATION) { + uint8_t event_id = avrc_command.reg_notif.event_id; + + BTIF_TRACE_EVENT( + "%s: New register notification received.event_id: %s, label: 0x%x, " + "code: %x", + __func__, dump_rc_notification_event_id(event_id), pmeta_msg->label, + pmeta_msg->code); + p_dev->rc_notif[event_id - 1].bNotify = true; + p_dev->rc_notif[event_id - 1].label = pmeta_msg->label; + + if (event_id == AVRC_EVT_UIDS_CHANGE) { + handle_uid_changed_notification(p_dev, pmeta_msg->label, &avrc_command); + return; } - - status=AVRC_ParsCommand(pmeta_msg->p_msg, &avrc_command, scratch_buf, sizeof(scratch_buf)); - BTIF_TRACE_DEBUG("%s: Received vendor command.code,PDU and label: %d, %d, %d", - __func__, pmeta_msg->code, avrc_command.cmd.pdu, pmeta_msg->label); - - if (status != AVRC_STS_NO_ERROR) - { - /* return error */ - BTIF_TRACE_WARNING("%s: Error in parsing received metamsg command. status: 0x%02x", - __func__, status); - send_reject_response(pmeta_msg->rc_handle, pmeta_msg->label, avrc_command.pdu, status, - pmeta_msg->p_msg->hdr.opcode); } - else - { - /* if RegisterNotification, add it to our registered queue */ - - if (avrc_command.cmd.pdu == AVRC_PDU_REGISTER_NOTIFICATION) - { - uint8_t event_id = avrc_command.reg_notif.event_id; - - BTIF_TRACE_EVENT("%s: New register notification received.event_id: %s, label: 0x%x, code: %x", - __func__,dump_rc_notification_event_id(event_id), pmeta_msg->label, pmeta_msg->code); - p_dev->rc_notif[event_id-1].bNotify = true; - p_dev->rc_notif[event_id-1].label = pmeta_msg->label; - - if (event_id == AVRC_EVT_UIDS_CHANGE) - { - handle_uid_changed_notification(p_dev, pmeta_msg->label, &avrc_command); - return; - } - - } - BTIF_TRACE_EVENT("%s: Passing received metamsg command to app. pdu: %s", - __func__, dump_rc_pdu(avrc_command.cmd.pdu)); + BTIF_TRACE_EVENT("%s: Passing received metamsg command to app. pdu: %s", + __func__, dump_rc_pdu(avrc_command.cmd.pdu)); - /* Since handle_rc_metamsg_cmd() itself is called from - *btif context, no context switching is required. Invoke - * btif_rc_upstreams_evt directly from here. */ - btif_rc_upstreams_evt((uint16_t)avrc_command.cmd.pdu, &avrc_command, pmeta_msg->code, - pmeta_msg->label, p_dev); - } + /* Since handle_rc_metamsg_cmd() itself is called from + *btif context, no context switching is required. Invoke + * btif_rc_upstreams_evt directly from here. */ + btif_rc_upstreams_evt((uint16_t)avrc_command.cmd.pdu, &avrc_command, + pmeta_msg->code, pmeta_msg->label, p_dev); + } } /*************************************************************************** @@ -1218,164 +1169,130 @@ void handle_rc_metamsg_cmd (tBTA_AV_META_MSG *pmeta_msg) ** Description RC event handler ** ***************************************************************************/ -void btif_rc_handler(tBTA_AV_EVT event, tBTA_AV *p_data) -{ - BTIF_TRACE_DEBUG ("%s: event: %s", __func__, dump_rc_event(event)); - btif_rc_device_cb_t *p_dev = NULL; - switch (event) - { - case BTA_AV_RC_OPEN_EVT: - { - BTIF_TRACE_DEBUG("%s: Peer_features: %x", __func__, p_data->rc_open.peer_features); - handle_rc_connect( &(p_data->rc_open) ); - }break; - - case BTA_AV_RC_BROWSE_OPEN_EVT: - { - /* tell the UL that we have connection to browse channel and that - * browse commands can be directed accordingly. */ - handle_rc_browse_connect(&(p_data->rc_open)); - }break; - - case BTA_AV_RC_CLOSE_EVT: - { - handle_rc_disconnect(&(p_data->rc_close)); - }break; - - case BTA_AV_RC_BROWSE_CLOSE_EVT: - { - BTIF_TRACE_DEBUG("%s: BTA_AV_RC_BROWSE_CLOSE_EVT", __func__); - }break; - - case BTA_AV_REMOTE_CMD_EVT: - { - if (bt_rc_callbacks != NULL) - { - BTIF_TRACE_DEBUG("%s: rc_id: 0x%x key_state: %d", __func__, p_data->remote_cmd.rc_id, - p_data->remote_cmd.key_state); - handle_rc_passthrough_cmd( (&p_data->remote_cmd) ); - } - else - { - BTIF_TRACE_ERROR("%s: AVRCP TG role not up, drop passthrough commands", __func__); - } - } - break; +void btif_rc_handler(tBTA_AV_EVT event, tBTA_AV* p_data) { + BTIF_TRACE_DEBUG("%s: event: %s", __func__, dump_rc_event(event)); + btif_rc_device_cb_t* p_dev = NULL; + switch (event) { + case BTA_AV_RC_OPEN_EVT: { + BTIF_TRACE_DEBUG("%s: Peer_features: %x", __func__, + p_data->rc_open.peer_features); + handle_rc_connect(&(p_data->rc_open)); + } break; + + case BTA_AV_RC_BROWSE_OPEN_EVT: { + /* tell the UL that we have connection to browse channel and that + * browse commands can be directed accordingly. */ + handle_rc_browse_connect(&(p_data->rc_open)); + } break; + + case BTA_AV_RC_CLOSE_EVT: { + handle_rc_disconnect(&(p_data->rc_close)); + } break; + + case BTA_AV_RC_BROWSE_CLOSE_EVT: { + BTIF_TRACE_DEBUG("%s: BTA_AV_RC_BROWSE_CLOSE_EVT", __func__); + } break; + + case BTA_AV_REMOTE_CMD_EVT: { + if (bt_rc_callbacks != NULL) { + BTIF_TRACE_DEBUG("%s: rc_id: 0x%x key_state: %d", __func__, + p_data->remote_cmd.rc_id, + p_data->remote_cmd.key_state); + handle_rc_passthrough_cmd((&p_data->remote_cmd)); + } else { + BTIF_TRACE_ERROR("%s: AVRCP TG role not up, drop passthrough commands", + __func__); + } + } break; #if (AVRC_CTRL_INCLUDED == TRUE) - case BTA_AV_REMOTE_RSP_EVT: - { - BTIF_TRACE_DEBUG("%s: RSP: rc_id: 0x%x key_state: %d", - __func__, p_data->remote_rsp.rc_id, p_data->remote_rsp.key_state); - - if (p_data->remote_rsp.rc_id == AVRC_ID_VENDOR) - { - handle_rc_vendorunique_rsp((&p_data->remote_rsp)); - } - else - { - handle_rc_passthrough_rsp((&p_data->remote_rsp)); - } - } - break; + case BTA_AV_REMOTE_RSP_EVT: { + BTIF_TRACE_DEBUG("%s: RSP: rc_id: 0x%x key_state: %d", __func__, + p_data->remote_rsp.rc_id, p_data->remote_rsp.key_state); + + if (p_data->remote_rsp.rc_id == AVRC_ID_VENDOR) { + handle_rc_vendorunique_rsp((&p_data->remote_rsp)); + } else { + handle_rc_passthrough_rsp((&p_data->remote_rsp)); + } + } break; #endif - case BTA_AV_RC_FEAT_EVT: - { - BTIF_TRACE_DEBUG("%s: Peer_features: %x", __func__, p_data->rc_feat.peer_features); - p_dev = btif_rc_get_device_by_handle(p_data->rc_feat.rc_handle); - if (p_dev == NULL) - { - BTIF_TRACE_ERROR("%s: RC Feature event for Invalid rc handle", __func__); - break; - } + case BTA_AV_RC_FEAT_EVT: { + BTIF_TRACE_DEBUG("%s: Peer_features: %x", __func__, + p_data->rc_feat.peer_features); + p_dev = btif_rc_get_device_by_handle(p_data->rc_feat.rc_handle); + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: RC Feature event for Invalid rc handle", + __func__); + break; + } - p_dev->rc_features = p_data->rc_feat.peer_features; - handle_rc_features(p_dev); + p_dev->rc_features = p_data->rc_feat.peer_features; + handle_rc_features(p_dev); #if (AVRC_CTRL_INCLUDED == TRUE) - if ((p_dev->rc_connected) && (bt_rc_ctrl_callbacks != NULL)) - { - handle_rc_ctrl_features(p_dev); - } + if ((p_dev->rc_connected) && (bt_rc_ctrl_callbacks != NULL)) { + handle_rc_ctrl_features(p_dev); + } #endif - } - break; + } break; - case BTA_AV_META_MSG_EVT: - { - if (bt_rc_callbacks != NULL) - { - BTIF_TRACE_DEBUG("%s: BTA_AV_META_MSG_EVT code: %d label: %d", - __func__, - p_data->meta_msg.code, - p_data->meta_msg.label); - BTIF_TRACE_DEBUG("%s: company_id: 0x%x len: %d handle: %d", - __func__, - p_data->meta_msg.company_id, - p_data->meta_msg.len, - p_data->meta_msg.rc_handle); - - /* handle the metamsg command */ - handle_rc_metamsg_cmd(&(p_data->meta_msg)); - - /* Free the Memory allocated for tAVRC_MSG */ - } -#if (AVRC_CTRL_INCLUDED == TRUE) - else if ((bt_rc_callbacks == NULL)&&(bt_rc_ctrl_callbacks != NULL)) - { - /* This is case of Sink + CT + TG(for abs vol)) */ - BTIF_TRACE_DEBUG( - "%s BTA_AV_META_MSG_EVT code:%d label:%d opcode %d ctype %d", - __FUNCTION__, - p_data->meta_msg.code, - p_data->meta_msg.label, - p_data->meta_msg.p_msg->hdr.opcode, - p_data->meta_msg.p_msg->hdr.ctype); - BTIF_TRACE_DEBUG("%s company_id:0x%x len:%d handle:%d", - __FUNCTION__, - p_data->meta_msg.company_id, - p_data->meta_msg.len, - p_data->meta_msg.rc_handle); - switch (p_data->meta_msg.p_msg->hdr.opcode) { - case AVRC_OP_VENDOR: - if ((p_data->meta_msg.code >= AVRC_RSP_NOT_IMPL)&& - (p_data->meta_msg.code <= AVRC_RSP_INTERIM)) - { - /* Its a response */ - handle_avk_rc_metamsg_rsp(&(p_data->meta_msg)); - } - else if (p_data->meta_msg.code <= AVRC_CMD_GEN_INQ) - { - /* Its a command */ - handle_avk_rc_metamsg_cmd(&(p_data->meta_msg)); - } - break; - - case AVRC_OP_BROWSE: - if (p_data->meta_msg.p_msg->hdr.ctype == AVRC_CMD) - { - handle_avk_rc_metamsg_cmd(&(p_data->meta_msg)); - } - else if (p_data->meta_msg.p_msg->hdr.ctype == AVRC_RSP) - { - handle_avk_rc_metamsg_rsp(&(p_data->meta_msg)); - } - break; - } + case BTA_AV_META_MSG_EVT: { + if (bt_rc_callbacks != NULL) { + BTIF_TRACE_DEBUG("%s: BTA_AV_META_MSG_EVT code: %d label: %d", __func__, + p_data->meta_msg.code, p_data->meta_msg.label); + BTIF_TRACE_DEBUG("%s: company_id: 0x%x len: %d handle: %d", __func__, + p_data->meta_msg.company_id, p_data->meta_msg.len, + p_data->meta_msg.rc_handle); + + /* handle the metamsg command */ + handle_rc_metamsg_cmd(&(p_data->meta_msg)); + /* Free the Memory allocated for tAVRC_MSG */ + } +#if (AVRC_CTRL_INCLUDED == TRUE) + else if ((bt_rc_callbacks == NULL) && (bt_rc_ctrl_callbacks != NULL)) { + /* This is case of Sink + CT + TG(for abs vol)) */ + BTIF_TRACE_DEBUG( + "%s BTA_AV_META_MSG_EVT code:%d label:%d opcode %d ctype %d", + __FUNCTION__, p_data->meta_msg.code, p_data->meta_msg.label, + p_data->meta_msg.p_msg->hdr.opcode, + p_data->meta_msg.p_msg->hdr.ctype); + BTIF_TRACE_DEBUG("%s company_id:0x%x len:%d handle:%d", __FUNCTION__, + p_data->meta_msg.company_id, p_data->meta_msg.len, + p_data->meta_msg.rc_handle); + switch (p_data->meta_msg.p_msg->hdr.opcode) { + case AVRC_OP_VENDOR: + if ((p_data->meta_msg.code >= AVRC_RSP_NOT_IMPL) && + (p_data->meta_msg.code <= AVRC_RSP_INTERIM)) { + /* Its a response */ + handle_avk_rc_metamsg_rsp(&(p_data->meta_msg)); + } else if (p_data->meta_msg.code <= AVRC_CMD_GEN_INQ) { + /* Its a command */ + handle_avk_rc_metamsg_cmd(&(p_data->meta_msg)); } -#endif - else - { - BTIF_TRACE_ERROR("Neither CTRL, nor TG is up, drop meta commands"); + break; + + case AVRC_OP_BROWSE: + if (p_data->meta_msg.p_msg->hdr.ctype == AVRC_CMD) { + handle_avk_rc_metamsg_cmd(&(p_data->meta_msg)); + } else if (p_data->meta_msg.p_msg->hdr.ctype == AVRC_RSP) { + handle_avk_rc_metamsg_rsp(&(p_data->meta_msg)); } + break; } - break; - default: - BTIF_TRACE_DEBUG("%s: Unhandled RC event : 0x%x", __func__, event); - } + } +#endif + else { + BTIF_TRACE_ERROR("Neither CTRL, nor TG is up, drop meta commands"); + } + } break; + + default: + BTIF_TRACE_DEBUG("%s: Unhandled RC event : 0x%x", __func__, event); + } } /*************************************************************************** @@ -1385,22 +1302,19 @@ void btif_rc_handler(tBTA_AV_EVT event, tBTA_AV *p_data) ** Description Fetches the connected headset's BD_ADDR if any ** ***************************************************************************/ -bool btif_rc_get_connected_peer(BD_ADDR peer_addr) -{ - bt_bdaddr_t rc_addr; - bdcpy(rc_addr.address, peer_addr); - btif_rc_device_cb_t *p_dev = NULL; - - for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) - { - p_dev = get_connected_device(idx); - if (p_dev != NULL && (p_dev->rc_connected == TRUE)) - { - bdcpy(peer_addr, p_dev->rc_addr); - return TRUE; - } - } - return false; +bool btif_rc_get_connected_peer(BD_ADDR peer_addr) { + bt_bdaddr_t rc_addr; + bdcpy(rc_addr.address, peer_addr); + btif_rc_device_cb_t* p_dev = NULL; + + for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) { + p_dev = get_connected_device(idx); + if (p_dev != NULL && (p_dev->rc_connected == TRUE)) { + bdcpy(peer_addr, p_dev->rc_addr); + return TRUE; + } + } + return false; } /*************************************************************************** @@ -1410,127 +1324,119 @@ bool btif_rc_get_connected_peer(BD_ADDR peer_addr) ** Description Fetches the connected headset's handle if any ** ***************************************************************************/ -uint8_t btif_rc_get_connected_peer_handle(BD_ADDR peer_addr) -{ - bt_bdaddr_t rc_addr; - bdcpy(rc_addr.address, peer_addr); - - btif_rc_device_cb_t *p_dev = NULL; - p_dev = btif_rc_get_device_by_bda(&rc_addr); - - if (p_dev == NULL){ - BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); - return BTRC_HANDLE_NONE; - } - return p_dev->rc_handle; +uint8_t btif_rc_get_connected_peer_handle(BD_ADDR peer_addr) { + bt_bdaddr_t rc_addr; + bdcpy(rc_addr.address, peer_addr); + + btif_rc_device_cb_t* p_dev = NULL; + p_dev = btif_rc_get_device_by_bda(&rc_addr); + + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); + return BTRC_HANDLE_NONE; + } + return p_dev->rc_handle; } /*************************************************************************** ** ** Function btif_rc_check_handle_pending_play ** - ** Description Clears the queued PLAY command. if |bSendToApp| is true, forwards to app + ** Description Clears the queued PLAY command. if |bSendToApp| is true, + *forwards to app ** ***************************************************************************/ /* clear the queued PLAY command. if |bSendToApp| is true, forward to app */ -void btif_rc_check_handle_pending_play (BD_ADDR peer_addr, bool bSendToApp) -{ - bt_bdaddr_t rc_addr; - bdcpy(rc_addr.address, peer_addr); - - btif_rc_device_cb_t *p_dev = NULL; - p_dev = btif_rc_get_device_by_bda(&rc_addr); - - if( p_dev == NULL ){ - BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); - return; - } - - BTIF_TRACE_DEBUG("%s: bSendToApp: %d", __func__, bSendToApp); - if ( p_dev->rc_pending_play ) - { - if (bSendToApp) - { - tBTA_AV_REMOTE_CMD remote_cmd; - APPL_TRACE_DEBUG("%s: Sending queued PLAYED event to app", __func__); - - memset (&remote_cmd, 0, sizeof(tBTA_AV_REMOTE_CMD)); - remote_cmd.rc_handle = p_dev->rc_handle; - remote_cmd.rc_id = AVRC_ID_PLAY; - remote_cmd.hdr.ctype = AVRC_CMD_CTRL; - remote_cmd.hdr.opcode = AVRC_OP_PASS_THRU; - - /* delay sending to app, else there is a timing issue in the framework, - ** which causes the audio to be on th device's speaker. Delay between - ** OPEN & RC_PLAYs - */ - sleep_ms(200); - /* send to app - both PRESSED & RELEASED */ - remote_cmd.key_state = AVRC_STATE_PRESS; - handle_rc_passthrough_cmd(&remote_cmd); - - sleep_ms(100); - - remote_cmd.key_state = AVRC_STATE_RELEASE; - handle_rc_passthrough_cmd(&remote_cmd); - } - p_dev->rc_pending_play = false; - } +void btif_rc_check_handle_pending_play(BD_ADDR peer_addr, bool bSendToApp) { + bt_bdaddr_t rc_addr; + bdcpy(rc_addr.address, peer_addr); + + btif_rc_device_cb_t* p_dev = NULL; + p_dev = btif_rc_get_device_by_bda(&rc_addr); + + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); + return; + } + + BTIF_TRACE_DEBUG("%s: bSendToApp: %d", __func__, bSendToApp); + if (p_dev->rc_pending_play) { + if (bSendToApp) { + tBTA_AV_REMOTE_CMD remote_cmd; + APPL_TRACE_DEBUG("%s: Sending queued PLAYED event to app", __func__); + + memset(&remote_cmd, 0, sizeof(tBTA_AV_REMOTE_CMD)); + remote_cmd.rc_handle = p_dev->rc_handle; + remote_cmd.rc_id = AVRC_ID_PLAY; + remote_cmd.hdr.ctype = AVRC_CMD_CTRL; + remote_cmd.hdr.opcode = AVRC_OP_PASS_THRU; + + /* delay sending to app, else there is a timing issue in the framework, + ** which causes the audio to be on th device's speaker. Delay between + ** OPEN & RC_PLAYs + */ + sleep_ms(200); + /* send to app - both PRESSED & RELEASED */ + remote_cmd.key_state = AVRC_STATE_PRESS; + handle_rc_passthrough_cmd(&remote_cmd); + + sleep_ms(100); + + remote_cmd.key_state = AVRC_STATE_RELEASE; + handle_rc_passthrough_cmd(&remote_cmd); + } + p_dev->rc_pending_play = false; + } } /* Generic reject response */ -static void send_reject_response (uint8_t rc_handle, uint8_t label, uint8_t pdu, uint8_t status, - uint8_t opcode) -{ - uint8_t ctype = AVRC_RSP_REJ; - tAVRC_RESPONSE avrc_rsp; - BT_HDR *p_msg = NULL; - memset (&avrc_rsp, 0, sizeof(tAVRC_RESPONSE)); - - avrc_rsp.rsp.opcode = opcode; - avrc_rsp.rsp.pdu = pdu; - avrc_rsp.rsp.status = status; - - status = AVRC_BldResponse(rc_handle, &avrc_rsp, &p_msg); - - if (status != AVRC_STS_NO_ERROR) - { - BTIF_TRACE_ERROR("%s: status not AVRC_STS_NO_ERROR", __func__); - return; - } - - BTIF_TRACE_DEBUG("%s: Sending error notification to handle: %d. pdu: %s,status: 0x%02x", - __func__, rc_handle, dump_rc_pdu(pdu), status); - BTA_AvMetaRsp(rc_handle, label, ctype, p_msg); +static void send_reject_response(uint8_t rc_handle, uint8_t label, uint8_t pdu, + uint8_t status, uint8_t opcode) { + uint8_t ctype = AVRC_RSP_REJ; + tAVRC_RESPONSE avrc_rsp; + BT_HDR* p_msg = NULL; + memset(&avrc_rsp, 0, sizeof(tAVRC_RESPONSE)); + + avrc_rsp.rsp.opcode = opcode; + avrc_rsp.rsp.pdu = pdu; + avrc_rsp.rsp.status = status; + + status = AVRC_BldResponse(rc_handle, &avrc_rsp, &p_msg); + + if (status != AVRC_STS_NO_ERROR) { + BTIF_TRACE_ERROR("%s: status not AVRC_STS_NO_ERROR", __func__); + return; + } + + BTIF_TRACE_DEBUG( + "%s: Sending error notification to handle: %d. pdu: %s,status: 0x%02x", + __func__, rc_handle, dump_rc_pdu(pdu), status); + BTA_AvMetaRsp(rc_handle, label, ctype, p_msg); } /*************************************************************************** * Function get_rsp_type_code * * - Argument: status - * - Description: Returns response type codes for particular command code and status. + * - Description: Returns response type codes for particular command code and + *status. * ***************************************************************************/ -static tBTA_AV_CODE get_rsp_type_code(tAVRC_STS status, tBTA_AV_CODE code) -{ - if (status != AVRC_STS_NO_ERROR) - { - return AVRC_RSP_REJ; - } +static tBTA_AV_CODE get_rsp_type_code(tAVRC_STS status, tBTA_AV_CODE code) { + if (status != AVRC_STS_NO_ERROR) { + return AVRC_RSP_REJ; + } - if (code < AVRC_RSP_NOT_IMPL) - { - if (code == AVRC_CMD_NOTIF) - return AVRC_RSP_INTERIM; + if (code < AVRC_RSP_NOT_IMPL) { + if (code == AVRC_CMD_NOTIF) return AVRC_RSP_INTERIM; - if (code == AVRC_CMD_STATUS) - return AVRC_RSP_IMPL_STBL; + if (code == AVRC_CMD_STATUS) return AVRC_RSP_IMPL_STBL; - return AVRC_RSP_ACCEPT; - } + return AVRC_RSP_ACCEPT; + } - return code; + return code; } /*************************************************************************** @@ -1546,109 +1452,99 @@ static tBTA_AV_CODE get_rsp_type_code(tAVRC_STS status, tBTA_AV_CODE code) * - Description: Remote control metamsg response handler * ***************************************************************************/ -static void send_metamsg_rsp (btif_rc_device_cb_t *p_dev, int index, uint8_t label, tBTA_AV_CODE code, - tAVRC_RESPONSE *pmetamsg_resp) -{ - uint8_t ctype; - - if (p_dev == NULL) - { - BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); - return; - } - - if (pmetamsg_resp == NULL) - { - BTIF_TRACE_WARNING("%s: Invalid response received from application", __func__); - return; - } - - BTIF_TRACE_EVENT("%s: rc_handle: %d, index: %d, label: %d, code: 0x%02x, pdu: %s", __func__, - p_dev->rc_handle, index, label, code, dump_rc_pdu(pmetamsg_resp->rsp.pdu)); - - if (index >= 0 && p_dev->rc_pdu_info[index].is_rsp_pending == false) - { - BTIF_TRACE_ERROR("%s: is_rsp_pending false, returning", __func__); - return; +static void send_metamsg_rsp(btif_rc_device_cb_t* p_dev, int index, + uint8_t label, tBTA_AV_CODE code, + tAVRC_RESPONSE* pmetamsg_resp) { + uint8_t ctype; + + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); + return; + } + + if (pmetamsg_resp == NULL) { + BTIF_TRACE_WARNING("%s: Invalid response received from application", + __func__); + return; + } + + BTIF_TRACE_EVENT( + "%s: rc_handle: %d, index: %d, label: %d, code: 0x%02x, pdu: %s", + __func__, p_dev->rc_handle, index, label, code, + dump_rc_pdu(pmetamsg_resp->rsp.pdu)); + + if (index >= 0 && p_dev->rc_pdu_info[index].is_rsp_pending == false) { + BTIF_TRACE_ERROR("%s: is_rsp_pending false, returning", __func__); + return; + } + + ctype = get_rsp_type_code(pmetamsg_resp->rsp.status, code); + + /* if response is for register_notification, make sure the rc has + actually registered for this */ + if ((pmetamsg_resp->rsp.pdu == AVRC_PDU_REGISTER_NOTIFICATION) && + ((code == AVRC_RSP_CHANGED) || (code == AVRC_RSP_INTERIM))) { + bool bSent = false; + uint8_t event_id = pmetamsg_resp->reg_notif.event_id; + bool bNotify = + (p_dev->rc_connected) && (p_dev->rc_notif[event_id - 1].bNotify); + + /* de-register this notification for a CHANGED response */ + p_dev->rc_notif[event_id - 1].bNotify = false; + BTIF_TRACE_DEBUG("%s: rc_handle: %d. event_id: 0x%02d bNotify: %u", + __func__, p_dev->rc_handle, event_id, bNotify); + if (bNotify) { + BT_HDR* p_msg = NULL; + tAVRC_STS status; + + if (AVRC_STS_NO_ERROR == (status = AVRC_BldResponse( + p_dev->rc_handle, pmetamsg_resp, &p_msg))) { + BTIF_TRACE_DEBUG( + "%s: Sending notification to rc_handle: %d. event_id: 0x%02d", + __func__, p_dev->rc_handle, event_id); + bSent = true; + BTA_AvMetaRsp(p_dev->rc_handle, p_dev->rc_notif[event_id - 1].label, + ctype, p_msg); + } else { + BTIF_TRACE_WARNING( + "%s: failed to build metamsg response. status: 0x%02x", __func__, + status); + } } - ctype = get_rsp_type_code(pmetamsg_resp->rsp.status, code); - - /* if response is for register_notification, make sure the rc has - actually registered for this */ - if ((pmetamsg_resp->rsp.pdu == AVRC_PDU_REGISTER_NOTIFICATION) && ((code == AVRC_RSP_CHANGED) - || (code == AVRC_RSP_INTERIM))) - { - bool bSent = false; - uint8_t event_id = pmetamsg_resp->reg_notif.event_id; - bool bNotify = (p_dev->rc_connected) && (p_dev->rc_notif[event_id-1].bNotify); - - /* de-register this notification for a CHANGED response */ - p_dev->rc_notif[event_id-1].bNotify = false; - BTIF_TRACE_DEBUG("%s: rc_handle: %d. event_id: 0x%02d bNotify: %u", __func__, - p_dev->rc_handle, event_id, bNotify); - if (bNotify) - { - BT_HDR *p_msg = NULL; - tAVRC_STS status; - - if (AVRC_STS_NO_ERROR == (status = AVRC_BldResponse(p_dev->rc_handle, - pmetamsg_resp, &p_msg)) ) - { - BTIF_TRACE_DEBUG("%s: Sending notification to rc_handle: %d. event_id: 0x%02d", - __func__, p_dev->rc_handle, event_id); - bSent = true; - BTA_AvMetaRsp(p_dev->rc_handle, p_dev->rc_notif[event_id-1].label, - ctype, p_msg); - } - else - { - BTIF_TRACE_WARNING("%s: failed to build metamsg response. status: 0x%02x", - __func__, status); - } - - } - - if (!bSent) - { - BTIF_TRACE_DEBUG("%s: Notification not sent, as there are no RC connections or the \ - CT has not subscribed for event_id: %s", __func__, dump_rc_notification_event_id(event_id)); - } + if (!bSent) { + BTIF_TRACE_DEBUG( + "%s: Notification not sent, as there are no RC connections or the \ + CT has not subscribed for event_id: %s", + __func__, dump_rc_notification_event_id(event_id)); } - else - { - /* All other commands go here */ + } else { + /* All other commands go here */ - BT_HDR *p_msg = NULL; - tAVRC_STS status; + BT_HDR* p_msg = NULL; + tAVRC_STS status; - status = AVRC_BldResponse(p_dev->rc_handle, pmetamsg_resp, &p_msg); + status = AVRC_BldResponse(p_dev->rc_handle, pmetamsg_resp, &p_msg); - if (status == AVRC_STS_NO_ERROR) - { - BTA_AvMetaRsp(p_dev->rc_handle, label, ctype, p_msg); - } - else - { - BTIF_TRACE_ERROR("%s: failed to build metamsg response. status: 0x%02x", - __func__, status); - } + if (status == AVRC_STS_NO_ERROR) { + BTA_AvMetaRsp(p_dev->rc_handle, label, ctype, p_msg); + } else { + BTIF_TRACE_ERROR("%s: failed to build metamsg response. status: 0x%02x", + __func__, status); } + } - if (index >= 0) - { - p_dev->rc_pdu_info[index].ctype = 0; - p_dev->rc_pdu_info[index].label = 0; - p_dev->rc_pdu_info[index].is_rsp_pending = false; - } + if (index >= 0) { + p_dev->rc_pdu_info[index].ctype = 0; + p_dev->rc_pdu_info[index].label = 0; + p_dev->rc_pdu_info[index].is_rsp_pending = false; + } } -static uint8_t opcode_from_pdu(uint8_t pdu) -{ - uint8_t opcode = 0; +static uint8_t opcode_from_pdu(uint8_t pdu) { + uint8_t opcode = 0; - switch (pdu) - { + switch (pdu) { case AVRC_PDU_SET_BROWSED_PLAYER: case AVRC_PDU_GET_FOLDER_ITEMS: case AVRC_PDU_CHANGE_PATH: @@ -1657,4377 +1553,4077 @@ static uint8_t opcode_from_pdu(uint8_t pdu) case AVRC_PDU_SEARCH: case AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS: case AVRC_PDU_GENERAL_REJECT: - opcode = AVRC_OP_BROWSE; - break; + opcode = AVRC_OP_BROWSE; + break; case AVRC_PDU_NEXT_GROUP: case AVRC_PDU_PREV_GROUP: /* pass thru */ - opcode = AVRC_OP_PASS_THRU; - break; + opcode = AVRC_OP_PASS_THRU; + break; default: /* vendor */ - opcode = AVRC_OP_VENDOR; - break; - } + opcode = AVRC_OP_VENDOR; + break; + } - return opcode; + return opcode; } /******************************************************************************* -** -** Function btif_rc_upstreams_evt -** -** Description Executes AVRC UPSTREAMS events in btif context. -** -** Returns void -** -*******************************************************************************/ -static void btif_rc_upstreams_evt(uint16_t event, tAVRC_COMMAND *pavrc_cmd, uint8_t ctype, - uint8_t label, btif_rc_device_cb_t *p_dev) -{ - BTIF_TRACE_EVENT("%s: pdu: %s handle: 0x%x ctype: %x label: %x event ID: %x", __func__, - dump_rc_pdu(pavrc_cmd->pdu), p_dev->rc_handle, ctype, label, - pavrc_cmd->reg_notif.event_id); - bt_bdaddr_t rc_addr; - - bdcpy(rc_addr.address, p_dev->rc_addr); - - switch (event) - { - case AVRC_PDU_GET_PLAY_STATUS: - { - fill_pdu_queue(IDX_GET_PLAY_STATUS_RSP, ctype, label, true, p_dev); - HAL_CBACK(bt_rc_callbacks, get_play_status_cb, &rc_addr); - } - break; - case AVRC_PDU_LIST_PLAYER_APP_ATTR: - case AVRC_PDU_LIST_PLAYER_APP_VALUES: - case AVRC_PDU_GET_CUR_PLAYER_APP_VALUE: - case AVRC_PDU_SET_PLAYER_APP_VALUE: - case AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT: - case AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT: - { - /* TODO: Add support for Application Settings */ - send_reject_response (p_dev->rc_handle, label, pavrc_cmd->pdu, - AVRC_STS_BAD_CMD, pavrc_cmd->cmd.opcode); + * + * Function btif_rc_upstreams_evt + * + * Description Executes AVRC UPSTREAMS events in btif context. + * + * Returns void + * + ******************************************************************************/ +static void btif_rc_upstreams_evt(uint16_t event, tAVRC_COMMAND* pavrc_cmd, + uint8_t ctype, uint8_t label, + btif_rc_device_cb_t* p_dev) { + BTIF_TRACE_EVENT("%s: pdu: %s handle: 0x%x ctype: %x label: %x event ID: %x", + __func__, dump_rc_pdu(pavrc_cmd->pdu), p_dev->rc_handle, + ctype, label, pavrc_cmd->reg_notif.event_id); + bt_bdaddr_t rc_addr; + + bdcpy(rc_addr.address, p_dev->rc_addr); + + switch (event) { + case AVRC_PDU_GET_PLAY_STATUS: { + fill_pdu_queue(IDX_GET_PLAY_STATUS_RSP, ctype, label, true, p_dev); + HAL_CBACK(bt_rc_callbacks, get_play_status_cb, &rc_addr); + } break; + case AVRC_PDU_LIST_PLAYER_APP_ATTR: + case AVRC_PDU_LIST_PLAYER_APP_VALUES: + case AVRC_PDU_GET_CUR_PLAYER_APP_VALUE: + case AVRC_PDU_SET_PLAYER_APP_VALUE: + case AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT: + case AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT: { + /* TODO: Add support for Application Settings */ + send_reject_response(p_dev->rc_handle, label, pavrc_cmd->pdu, + AVRC_STS_BAD_CMD, pavrc_cmd->cmd.opcode); + } break; + case AVRC_PDU_GET_ELEMENT_ATTR: { + btrc_media_attr_t element_attrs[BTRC_MAX_ELEM_ATTR_SIZE]; + uint8_t num_attr; + memset(&element_attrs, 0, sizeof(element_attrs)); + if (pavrc_cmd->get_elem_attrs.num_attr == 0) { + /* CT requests for all attributes */ + int attr_cnt; + num_attr = BTRC_MAX_ELEM_ATTR_SIZE; + for (attr_cnt = 0; attr_cnt < BTRC_MAX_ELEM_ATTR_SIZE; attr_cnt++) { + element_attrs[attr_cnt] = (btrc_media_attr_t)(attr_cnt + 1); } - break; - case AVRC_PDU_GET_ELEMENT_ATTR: - { - btrc_media_attr_t element_attrs[BTRC_MAX_ELEM_ATTR_SIZE]; - uint8_t num_attr; - memset(&element_attrs, 0, sizeof(element_attrs)); - if (pavrc_cmd->get_elem_attrs.num_attr == 0) - { - /* CT requests for all attributes */ - int attr_cnt; - num_attr = BTRC_MAX_ELEM_ATTR_SIZE; - for (attr_cnt = 0; attr_cnt < BTRC_MAX_ELEM_ATTR_SIZE; attr_cnt++) - { - element_attrs[attr_cnt] = (btrc_media_attr_t)(attr_cnt + 1); - } - } - else if (pavrc_cmd->get_elem_attrs.num_attr == 0xFF) - { - /* 0xff indicates, no attributes requested - reject */ - send_reject_response (p_dev->rc_handle, label, pavrc_cmd->pdu, - AVRC_STS_BAD_PARAM, pavrc_cmd->cmd.opcode); - return; + } else if (pavrc_cmd->get_elem_attrs.num_attr == 0xFF) { + /* 0xff indicates, no attributes requested - reject */ + send_reject_response(p_dev->rc_handle, label, pavrc_cmd->pdu, + AVRC_STS_BAD_PARAM, pavrc_cmd->cmd.opcode); + return; + } else { + int attr_cnt, filled_attr_count; + + num_attr = 0; + /* Attribute IDs from 1 to AVRC_MAX_NUM_MEDIA_ATTR_ID are only valid, + * hence HAL definition limits the attributes to + * AVRC_MAX_NUM_MEDIA_ATTR_ID. + * Fill only valid entries. + */ + for (attr_cnt = 0; (attr_cnt < pavrc_cmd->get_elem_attrs.num_attr) && + (num_attr < AVRC_MAX_NUM_MEDIA_ATTR_ID); + attr_cnt++) { + if ((pavrc_cmd->get_elem_attrs.attrs[attr_cnt] > 0) && + (pavrc_cmd->get_elem_attrs.attrs[attr_cnt] <= + AVRC_MAX_NUM_MEDIA_ATTR_ID)) { + /* Skip the duplicate entries : PTS sends duplicate entries for + * Fragment cases + */ + for (filled_attr_count = 0; filled_attr_count < num_attr; + filled_attr_count++) { + if (element_attrs[filled_attr_count] == + pavrc_cmd->get_elem_attrs.attrs[attr_cnt]) + break; } - else - { - int attr_cnt, filled_attr_count; - - num_attr = 0; - /* Attribute IDs from 1 to AVRC_MAX_NUM_MEDIA_ATTR_ID are only valid, - * hence HAL definition limits the attributes to AVRC_MAX_NUM_MEDIA_ATTR_ID. - * Fill only valid entries. - */ - for (attr_cnt = 0; (attr_cnt < pavrc_cmd->get_elem_attrs.num_attr) && - (num_attr < AVRC_MAX_NUM_MEDIA_ATTR_ID); attr_cnt++) - { - if ((pavrc_cmd->get_elem_attrs.attrs[attr_cnt] > 0) && - (pavrc_cmd->get_elem_attrs.attrs[attr_cnt] <= AVRC_MAX_NUM_MEDIA_ATTR_ID)) - { - /* Skip the duplicate entries : PTS sends duplicate entries for Fragment cases - */ - for (filled_attr_count = 0; filled_attr_count < num_attr; filled_attr_count++) - { - if (element_attrs[filled_attr_count] == pavrc_cmd->get_elem_attrs.attrs[attr_cnt]) - break; - } - if (filled_attr_count == num_attr) - { - element_attrs[num_attr] = (btrc_media_attr_t)pavrc_cmd->get_elem_attrs.attrs[attr_cnt]; - num_attr++; - } - } - } + if (filled_attr_count == num_attr) { + element_attrs[num_attr] = + (btrc_media_attr_t)pavrc_cmd->get_elem_attrs.attrs[attr_cnt]; + num_attr++; } - fill_pdu_queue(IDX_GET_ELEMENT_ATTR_RSP, ctype, label, true, p_dev); - HAL_CBACK(bt_rc_callbacks, get_element_attr_cb, num_attr, - element_attrs, &rc_addr); + } } - break; - case AVRC_PDU_REGISTER_NOTIFICATION: - { - if (pavrc_cmd->reg_notif.event_id == BTRC_EVT_PLAY_POS_CHANGED && - pavrc_cmd->reg_notif.param == 0) - { - BTIF_TRACE_WARNING("%s: Device registering position changed with illegal param 0.", - __func__); - send_reject_response (p_dev->rc_handle, label, pavrc_cmd->pdu, - AVRC_STS_BAD_PARAM, pavrc_cmd->cmd.opcode); - /* de-register this notification for a rejected response */ - p_dev->rc_notif[BTRC_EVT_PLAY_POS_CHANGED - 1].bNotify = false; - return; - } - HAL_CBACK(bt_rc_callbacks, register_notification_cb, (btrc_event_id_t)pavrc_cmd->reg_notif.event_id, + } + fill_pdu_queue(IDX_GET_ELEMENT_ATTR_RSP, ctype, label, true, p_dev); + HAL_CBACK(bt_rc_callbacks, get_element_attr_cb, num_attr, element_attrs, + &rc_addr); + } break; + case AVRC_PDU_REGISTER_NOTIFICATION: { + if (pavrc_cmd->reg_notif.event_id == BTRC_EVT_PLAY_POS_CHANGED && + pavrc_cmd->reg_notif.param == 0) { + BTIF_TRACE_WARNING( + "%s: Device registering position changed with illegal param 0.", + __func__); + send_reject_response(p_dev->rc_handle, label, pavrc_cmd->pdu, + AVRC_STS_BAD_PARAM, pavrc_cmd->cmd.opcode); + /* de-register this notification for a rejected response */ + p_dev->rc_notif[BTRC_EVT_PLAY_POS_CHANGED - 1].bNotify = false; + return; + } + HAL_CBACK(bt_rc_callbacks, register_notification_cb, + (btrc_event_id_t)pavrc_cmd->reg_notif.event_id, pavrc_cmd->reg_notif.param, &rc_addr); - } - break; - case AVRC_PDU_INFORM_DISPLAY_CHARSET: - { - tAVRC_RESPONSE avrc_rsp; - BTIF_TRACE_EVENT("%s: AVRC_PDU_INFORM_DISPLAY_CHARSET", __func__); - if (p_dev->rc_connected == true) - { - memset(&(avrc_rsp.inform_charset), 0, sizeof(tAVRC_RSP)); - avrc_rsp.inform_charset.opcode = opcode_from_pdu(AVRC_PDU_INFORM_DISPLAY_CHARSET); - avrc_rsp.inform_charset.pdu = AVRC_PDU_INFORM_DISPLAY_CHARSET; - avrc_rsp.inform_charset.status = AVRC_STS_NO_ERROR; - send_metamsg_rsp(p_dev, -1, label, ctype, &avrc_rsp); - } - } - break; - - case AVRC_PDU_GET_FOLDER_ITEMS: - { - uint32_t attr_ids[BTRC_MAX_ELEM_ATTR_SIZE]; - uint8_t num_attr; - num_attr = pavrc_cmd->get_items.attr_count; - - BTIF_TRACE_EVENT("%s: AVRC_PDU_GET_FOLDER_ITEMS num_attr: %d, start_item [%d] \ - end_item [%d]", __func__, num_attr, pavrc_cmd->get_items.start_item, - pavrc_cmd->get_items.end_item); + } break; + case AVRC_PDU_INFORM_DISPLAY_CHARSET: { + tAVRC_RESPONSE avrc_rsp; + BTIF_TRACE_EVENT("%s: AVRC_PDU_INFORM_DISPLAY_CHARSET", __func__); + if (p_dev->rc_connected == true) { + memset(&(avrc_rsp.inform_charset), 0, sizeof(tAVRC_RSP)); + avrc_rsp.inform_charset.opcode = + opcode_from_pdu(AVRC_PDU_INFORM_DISPLAY_CHARSET); + avrc_rsp.inform_charset.pdu = AVRC_PDU_INFORM_DISPLAY_CHARSET; + avrc_rsp.inform_charset.status = AVRC_STS_NO_ERROR; + send_metamsg_rsp(p_dev, -1, label, ctype, &avrc_rsp); + } + } break; + + case AVRC_PDU_GET_FOLDER_ITEMS: { + uint32_t attr_ids[BTRC_MAX_ELEM_ATTR_SIZE]; + uint8_t num_attr; + num_attr = pavrc_cmd->get_items.attr_count; + + BTIF_TRACE_EVENT( + "%s: AVRC_PDU_GET_FOLDER_ITEMS num_attr: %d, start_item [%d] \ + end_item [%d]", + __func__, num_attr, pavrc_cmd->get_items.start_item, + pavrc_cmd->get_items.end_item); + + /* num_attr requested: + * 0x00: All attributes requested + * 0xFF: No Attributes requested + * 0x01 to 0x07: Specified number of attributes + */ + if ((num_attr != 0xFF && num_attr > BTRC_MAX_ELEM_ATTR_SIZE)) { + send_reject_response(p_dev->rc_handle, label, pavrc_cmd->pdu, + AVRC_STS_BAD_PARAM, pavrc_cmd->cmd.opcode); + return; + } - /* num_attr requested: - * 0x00: All attributes requested - * 0xFF: No Attributes requested - * 0x01 to 0x07: Specified number of attributes - */ - if ((num_attr != 0xFF && num_attr > BTRC_MAX_ELEM_ATTR_SIZE)) { - send_reject_response (p_dev->rc_handle, label, pavrc_cmd->pdu, - AVRC_STS_BAD_PARAM, pavrc_cmd->cmd.opcode); - return; - } + /* Except num_attr is None(0xff) / All(0x00), request follows with an + * Attribute List */ + if ((num_attr != 0xFF) && (num_attr != 0x00)) { + memcpy(attr_ids, pavrc_cmd->get_items.p_attr_list, + sizeof(uint32_t) * num_attr); + } - /* Except num_attr is None(0xff) / All(0x00), request follows with an Attribute List */ - if ((num_attr != 0xFF) && (num_attr != 0x00)) { - memcpy(attr_ids, pavrc_cmd->get_items.p_attr_list, sizeof(uint32_t)*num_attr); - } + fill_pdu_queue(IDX_GET_FOLDER_ITEMS_RSP, ctype, label, true, p_dev); + HAL_CBACK(bt_rc_callbacks, get_folder_items_cb, + pavrc_cmd->get_items.scope, pavrc_cmd->get_items.start_item, + pavrc_cmd->get_items.end_item, num_attr, attr_ids, &rc_addr); + } break; + + case AVRC_PDU_SET_ADDRESSED_PLAYER: { + fill_pdu_queue(IDX_SET_ADDR_PLAYER_RSP, ctype, label, true, p_dev); + HAL_CBACK(bt_rc_callbacks, set_addressed_player_cb, + pavrc_cmd->addr_player.player_id, &rc_addr); + } break; + + case AVRC_PDU_SET_BROWSED_PLAYER: { + fill_pdu_queue(IDX_SET_BROWSED_PLAYER_RSP, ctype, label, true, p_dev); + HAL_CBACK(bt_rc_callbacks, set_browsed_player_cb, + pavrc_cmd->br_player.player_id, &rc_addr); + } break; + + case AVRC_PDU_REQUEST_CONTINUATION_RSP: { + BTIF_TRACE_EVENT("%s() REQUEST CONTINUATION: target_pdu: 0x%02d", + __func__, pavrc_cmd->continu.target_pdu); + tAVRC_RESPONSE avrc_rsp; + if (p_dev->rc_connected == TRUE) { + memset(&(avrc_rsp.continu), 0, sizeof(tAVRC_NEXT_RSP)); + avrc_rsp.continu.opcode = + opcode_from_pdu(AVRC_PDU_REQUEST_CONTINUATION_RSP); + avrc_rsp.continu.pdu = AVRC_PDU_REQUEST_CONTINUATION_RSP; + avrc_rsp.continu.status = AVRC_STS_NO_ERROR; + avrc_rsp.continu.target_pdu = pavrc_cmd->continu.target_pdu; + send_metamsg_rsp(p_dev, -1, label, ctype, &avrc_rsp); + } + } break; + + case AVRC_PDU_ABORT_CONTINUATION_RSP: { + BTIF_TRACE_EVENT("%s() ABORT CONTINUATION: target_pdu: 0x%02d", __func__, + pavrc_cmd->abort.target_pdu); + tAVRC_RESPONSE avrc_rsp; + if (p_dev->rc_connected == TRUE) { + memset(&(avrc_rsp.abort), 0, sizeof(tAVRC_NEXT_RSP)); + avrc_rsp.abort.opcode = + opcode_from_pdu(AVRC_PDU_ABORT_CONTINUATION_RSP); + avrc_rsp.abort.pdu = AVRC_PDU_ABORT_CONTINUATION_RSP; + avrc_rsp.abort.status = AVRC_STS_NO_ERROR; + avrc_rsp.abort.target_pdu = pavrc_cmd->continu.target_pdu; + send_metamsg_rsp(p_dev, -1, label, ctype, &avrc_rsp); + } + } break; + + case AVRC_PDU_CHANGE_PATH: { + fill_pdu_queue(IDX_CHG_PATH_RSP, ctype, label, true, p_dev); + HAL_CBACK(bt_rc_callbacks, change_path_cb, pavrc_cmd->chg_path.direction, + pavrc_cmd->chg_path.folder_uid, &rc_addr); + } break; + + case AVRC_PDU_SEARCH: { + fill_pdu_queue(IDX_SEARCH_RSP, ctype, label, true, p_dev); + HAL_CBACK(bt_rc_callbacks, search_cb, pavrc_cmd->search.string.charset_id, + pavrc_cmd->search.string.str_len, + pavrc_cmd->search.string.p_str, &rc_addr); + } break; + + case AVRC_PDU_GET_ITEM_ATTRIBUTES: { + btrc_media_attr_t item_attrs[BTRC_MAX_ELEM_ATTR_SIZE]; + uint8_t num_attr; + uint8_t scope; + uint16_t uid_counter; + + scope = pavrc_cmd->get_attrs.scope; + uid_counter = pavrc_cmd->get_attrs.uid_counter; + memset(&item_attrs, 0, sizeof(item_attrs)); + + if (pavrc_cmd->get_attrs.attr_count == 0xFF) { + BTIF_TRACE_ERROR( + "%s: No attributes are requested in GET_ITEM_ATTRIBUTES", __func__); + /* 0xff indicates, no attributes requested - reject this */ + send_reject_response(p_dev->rc_handle, label, pavrc_cmd->pdu, + AVRC_STS_BAD_PARAM, pavrc_cmd->cmd.opcode); + return; + } - fill_pdu_queue(IDX_GET_FOLDER_ITEMS_RSP, ctype, label, true, p_dev); - HAL_CBACK(bt_rc_callbacks, get_folder_items_cb, pavrc_cmd->get_items.scope, - pavrc_cmd->get_items.start_item, pavrc_cmd->get_items.end_item, - num_attr, attr_ids, &rc_addr); + if (pavrc_cmd->get_attrs.attr_count == 0) { + /* CT requests for all attributes */ + int attr_cnt; + num_attr = BTRC_MAX_ELEM_ATTR_SIZE; + for (attr_cnt = 0; attr_cnt < BTRC_MAX_ELEM_ATTR_SIZE; attr_cnt++) { + item_attrs[attr_cnt] = (btrc_media_attr_t)(attr_cnt + 1); } - break; - - case AVRC_PDU_SET_ADDRESSED_PLAYER: - { - fill_pdu_queue(IDX_SET_ADDR_PLAYER_RSP, ctype, label, true, p_dev); - HAL_CBACK(bt_rc_callbacks, set_addressed_player_cb, pavrc_cmd->addr_player.player_id, + } else { + num_attr = pavrc_cmd->get_attrs.attr_count; + memcpy(item_attrs, pavrc_cmd->get_attrs.p_attr_list, + sizeof(uint32_t) * pavrc_cmd->get_attrs.attr_count); + } + fill_pdu_queue(IDX_GET_ITEM_ATTR_RSP, ctype, label, true, p_dev); + BTIF_TRACE_DEBUG("%s: GET_ITEM_ATTRIBUTES: num_attr: %d", __func__, + num_attr); + HAL_CBACK(bt_rc_callbacks, get_item_attr_cb, scope, + pavrc_cmd->get_attrs.uid, uid_counter, num_attr, item_attrs, &rc_addr); - } - break; - - case AVRC_PDU_SET_BROWSED_PLAYER: - { - fill_pdu_queue(IDX_SET_BROWSED_PLAYER_RSP, ctype, label, true, p_dev); - HAL_CBACK(bt_rc_callbacks, set_browsed_player_cb, pavrc_cmd->br_player.player_id, + } break; + + case AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS: { + fill_pdu_queue(IDX_GET_TOTAL_NUM_OF_ITEMS_RSP, ctype, label, true, p_dev); + HAL_CBACK(bt_rc_callbacks, get_total_num_of_items_cb, + pavrc_cmd->get_num_of_items.scope, &rc_addr); + } break; + + case AVRC_PDU_ADD_TO_NOW_PLAYING: { + fill_pdu_queue(IDX_ADD_TO_NOW_PLAYING_RSP, ctype, label, true, p_dev); + HAL_CBACK(bt_rc_callbacks, add_to_now_playing_cb, + pavrc_cmd->add_to_play.scope, pavrc_cmd->add_to_play.uid, + pavrc_cmd->add_to_play.uid_counter, &rc_addr); + } break; + + case AVRC_PDU_PLAY_ITEM: { + fill_pdu_queue(IDX_PLAY_ITEM_RSP, ctype, label, true, p_dev); + HAL_CBACK(bt_rc_callbacks, play_item_cb, pavrc_cmd->play_item.scope, + pavrc_cmd->play_item.uid_counter, pavrc_cmd->play_item.uid, &rc_addr); - } - break; - - case AVRC_PDU_REQUEST_CONTINUATION_RSP: - { - BTIF_TRACE_EVENT("%s() REQUEST CONTINUATION: target_pdu: 0x%02d", - __func__, pavrc_cmd->continu.target_pdu); - tAVRC_RESPONSE avrc_rsp; - if (p_dev->rc_connected == TRUE) - { - memset(&(avrc_rsp.continu), 0, sizeof(tAVRC_NEXT_RSP)); - avrc_rsp.continu.opcode = opcode_from_pdu(AVRC_PDU_REQUEST_CONTINUATION_RSP); - avrc_rsp.continu.pdu = AVRC_PDU_REQUEST_CONTINUATION_RSP; - avrc_rsp.continu.status = AVRC_STS_NO_ERROR; - avrc_rsp.continu.target_pdu = pavrc_cmd->continu.target_pdu; - send_metamsg_rsp(p_dev, -1, label, ctype, &avrc_rsp); - } - } - break; - - case AVRC_PDU_ABORT_CONTINUATION_RSP: - { - BTIF_TRACE_EVENT("%s() ABORT CONTINUATION: target_pdu: 0x%02d", - __func__, pavrc_cmd->abort.target_pdu); - tAVRC_RESPONSE avrc_rsp; - if (p_dev->rc_connected == TRUE) - { - memset(&(avrc_rsp.abort), 0, sizeof(tAVRC_NEXT_RSP)); - avrc_rsp.abort.opcode = opcode_from_pdu(AVRC_PDU_ABORT_CONTINUATION_RSP); - avrc_rsp.abort.pdu = AVRC_PDU_ABORT_CONTINUATION_RSP; - avrc_rsp.abort.status = AVRC_STS_NO_ERROR; - avrc_rsp.abort.target_pdu = pavrc_cmd->continu.target_pdu; - send_metamsg_rsp(p_dev, -1, label, ctype, &avrc_rsp); - } - } - break; - - case AVRC_PDU_CHANGE_PATH: - { - fill_pdu_queue(IDX_CHG_PATH_RSP, ctype, label, true, p_dev); - HAL_CBACK(bt_rc_callbacks, change_path_cb, pavrc_cmd->chg_path.direction, - pavrc_cmd->chg_path.folder_uid, &rc_addr); - } - break; - - case AVRC_PDU_SEARCH: - { - fill_pdu_queue(IDX_SEARCH_RSP, ctype, label, true, p_dev); - HAL_CBACK(bt_rc_callbacks, search_cb, pavrc_cmd->search.string.charset_id, - pavrc_cmd->search.string.str_len, pavrc_cmd->search.string.p_str, &rc_addr); - } - break; - - case AVRC_PDU_GET_ITEM_ATTRIBUTES: - { - btrc_media_attr_t item_attrs[BTRC_MAX_ELEM_ATTR_SIZE]; - uint8_t num_attr; - uint8_t scope; - uint16_t uid_counter; - - scope = pavrc_cmd->get_attrs.scope; - uid_counter = pavrc_cmd->get_attrs.uid_counter; - memset(&item_attrs, 0, sizeof(item_attrs)); - - if (pavrc_cmd->get_attrs.attr_count == 0xFF) - { - BTIF_TRACE_ERROR("%s: No attributes are requested in GET_ITEM_ATTRIBUTES", __func__); - /* 0xff indicates, no attributes requested - reject this */ - send_reject_response (p_dev->rc_handle, label, pavrc_cmd->pdu, - AVRC_STS_BAD_PARAM, pavrc_cmd->cmd.opcode); - return; - } - - if (pavrc_cmd->get_attrs.attr_count == 0) - { - /* CT requests for all attributes */ - int attr_cnt; - num_attr = BTRC_MAX_ELEM_ATTR_SIZE; - for (attr_cnt = 0; attr_cnt < BTRC_MAX_ELEM_ATTR_SIZE; attr_cnt++) - { - item_attrs[attr_cnt] = (btrc_media_attr_t)(attr_cnt + 1); - } - } - else - { - num_attr = pavrc_cmd->get_attrs.attr_count; - memcpy(item_attrs, pavrc_cmd->get_attrs.p_attr_list, sizeof(uint32_t) - *pavrc_cmd->get_attrs.attr_count); - } - fill_pdu_queue(IDX_GET_ITEM_ATTR_RSP, ctype, label, true, p_dev); - BTIF_TRACE_DEBUG("%s: GET_ITEM_ATTRIBUTES: num_attr: %d", __func__, num_attr); - HAL_CBACK(bt_rc_callbacks, get_item_attr_cb, scope, pavrc_cmd->get_attrs.uid, - uid_counter, num_attr, item_attrs, &rc_addr); - } - break; - - case AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS: - { - fill_pdu_queue(IDX_GET_TOTAL_NUM_OF_ITEMS_RSP, ctype, label, true, p_dev); - HAL_CBACK(bt_rc_callbacks, get_total_num_of_items_cb, - pavrc_cmd->get_num_of_items.scope, &rc_addr); - } - break; - - case AVRC_PDU_ADD_TO_NOW_PLAYING: - { - fill_pdu_queue(IDX_ADD_TO_NOW_PLAYING_RSP, ctype, label, true, p_dev); - HAL_CBACK(bt_rc_callbacks, add_to_now_playing_cb, pavrc_cmd->add_to_play.scope, - pavrc_cmd->add_to_play.uid, pavrc_cmd->add_to_play.uid_counter, - &rc_addr); - } - break; - - case AVRC_PDU_PLAY_ITEM: - { - fill_pdu_queue(IDX_PLAY_ITEM_RSP, ctype, label, true, p_dev); - HAL_CBACK(bt_rc_callbacks, play_item_cb, pavrc_cmd->play_item.scope, - pavrc_cmd->play_item.uid_counter, pavrc_cmd->play_item.uid, &rc_addr); - } - break; - - default: - { - send_reject_response(p_dev->rc_handle, label, pavrc_cmd->pdu, AVRC_STS_BAD_CMD, - pavrc_cmd->cmd.opcode); - return; - } - break; - } + } break; + + default: { + send_reject_response(p_dev->rc_handle, label, pavrc_cmd->pdu, + AVRC_STS_BAD_CMD, pavrc_cmd->cmd.opcode); + return; + } break; + } } #if (AVRC_CTRL_INCLUDED == TRUE) /******************************************************************************* -** -** Function btif_rc_ctrl_upstreams_rsp_cmd -** -** Description Executes AVRC UPSTREAMS response events in btif context. -** -** Returns void -** -*******************************************************************************/ -static void btif_rc_ctrl_upstreams_rsp_cmd(uint8_t event, tAVRC_COMMAND *pavrc_cmd, - uint8_t label, btif_rc_device_cb_t *p_dev) -{ - BTIF_TRACE_DEBUG("%s: pdu: %s: handle: 0x%x", __func__, - dump_rc_pdu(pavrc_cmd->pdu), p_dev->rc_handle); - bt_bdaddr_t rc_addr; - bdcpy(rc_addr.address, p_dev->rc_addr); - switch (event) - { + * + * Function btif_rc_ctrl_upstreams_rsp_cmd + * + * Description Executes AVRC UPSTREAMS response events in btif context. + * + * Returns void + * + ******************************************************************************/ +static void btif_rc_ctrl_upstreams_rsp_cmd(uint8_t event, + tAVRC_COMMAND* pavrc_cmd, + uint8_t label, + btif_rc_device_cb_t* p_dev) { + BTIF_TRACE_DEBUG("%s: pdu: %s: handle: 0x%x", __func__, + dump_rc_pdu(pavrc_cmd->pdu), p_dev->rc_handle); + bt_bdaddr_t rc_addr; + bdcpy(rc_addr.address, p_dev->rc_addr); + switch (event) { case AVRC_PDU_SET_ABSOLUTE_VOLUME: - HAL_CBACK(bt_rc_ctrl_callbacks,setabsvol_cmd_cb, &rc_addr, - pavrc_cmd->volume.volume, label); - break; + HAL_CBACK(bt_rc_ctrl_callbacks, setabsvol_cmd_cb, &rc_addr, + pavrc_cmd->volume.volume, label); + break; case AVRC_PDU_REGISTER_NOTIFICATION: - if (pavrc_cmd->reg_notif.event_id == AVRC_EVT_VOLUME_CHANGE) - { - HAL_CBACK(bt_rc_ctrl_callbacks, registernotification_absvol_cb, - &rc_addr, label); - } - break; - } + if (pavrc_cmd->reg_notif.event_id == AVRC_EVT_VOLUME_CHANGE) { + HAL_CBACK(bt_rc_ctrl_callbacks, registernotification_absvol_cb, + &rc_addr, label); + } + break; + } } #endif /******************************************************************************* -** -** Function btif_rc_upstreams_rsp_evt -** -** Description Executes AVRC UPSTREAMS response events in btif context. -** -** Returns void -** -*******************************************************************************/ -static void btif_rc_upstreams_rsp_evt(uint16_t event, tAVRC_RESPONSE *pavrc_resp, uint8_t ctype, - uint8_t label, btif_rc_device_cb_t *p_dev) -{ - BTIF_TRACE_EVENT("%s: pdu: %s: handle: 0x%x ctype: %x label: %x", __func__, - dump_rc_pdu(pavrc_resp->pdu), p_dev->rc_handle, ctype, label); - bt_bdaddr_t rc_addr; - bdcpy(rc_addr.address, p_dev->rc_addr); + * + * Function btif_rc_upstreams_rsp_evt + * + * Description Executes AVRC UPSTREAMS response events in btif context. + * + * Returns void + * + ******************************************************************************/ +static void btif_rc_upstreams_rsp_evt(uint16_t event, + tAVRC_RESPONSE* pavrc_resp, uint8_t ctype, + uint8_t label, + btif_rc_device_cb_t* p_dev) { + BTIF_TRACE_EVENT("%s: pdu: %s: handle: 0x%x ctype: %x label: %x", __func__, + dump_rc_pdu(pavrc_resp->pdu), p_dev->rc_handle, ctype, + label); + bt_bdaddr_t rc_addr; + bdcpy(rc_addr.address, p_dev->rc_addr); #if (AVRC_ADV_CTRL_INCLUDED == TRUE) - switch (event) - { - case AVRC_PDU_REGISTER_NOTIFICATION: - { - if (AVRC_RSP_CHANGED == ctype) - p_dev->rc_volume = pavrc_resp->reg_notif.param.volume; - HAL_CBACK(bt_rc_callbacks, volume_change_cb, pavrc_resp->reg_notif.param.volume, - ctype, &rc_addr) - } - break; - - case AVRC_PDU_SET_ABSOLUTE_VOLUME: - { - BTIF_TRACE_DEBUG("%s: Set absolute volume change event received: volume: %d, ctype: %d", - __func__, pavrc_resp->volume.volume, ctype); - if (AVRC_RSP_ACCEPT == ctype) - p_dev->rc_volume = pavrc_resp->volume.volume; - HAL_CBACK(bt_rc_callbacks, volume_change_cb, pavrc_resp->volume.volume, ctype, - &rc_addr); - } - break; - - default: - return; - } + switch (event) { + case AVRC_PDU_REGISTER_NOTIFICATION: { + if (AVRC_RSP_CHANGED == ctype) + p_dev->rc_volume = pavrc_resp->reg_notif.param.volume; + HAL_CBACK(bt_rc_callbacks, volume_change_cb, + pavrc_resp->reg_notif.param.volume, ctype, &rc_addr) + } break; + + case AVRC_PDU_SET_ABSOLUTE_VOLUME: { + BTIF_TRACE_DEBUG( + "%s: Set absolute volume change event received: volume: %d, ctype: " + "%d", + __func__, pavrc_resp->volume.volume, ctype); + if (AVRC_RSP_ACCEPT == ctype) + p_dev->rc_volume = pavrc_resp->volume.volume; + HAL_CBACK(bt_rc_callbacks, volume_change_cb, pavrc_resp->volume.volume, + ctype, &rc_addr); + } break; + + default: + return; + } #endif } /************************************************************************************ -** AVRCP API Functions -************************************************************************************/ + * AVRCP API Functions + ***********************************************************************************/ /******************************************************************************* -** -** Function init -** -** Description Initializes the AVRC interface -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t init(btrc_callbacks_t* callbacks) -{ - BTIF_TRACE_EVENT("%s: ", __func__); - bt_status_t result = BT_STATUS_SUCCESS; - - if (bt_rc_callbacks) - return BT_STATUS_DONE; - - bt_rc_callbacks = callbacks; - for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) - { - memset (&btif_rc_cb.rc_multi_cb[idx], 0, sizeof(btif_rc_cb.rc_multi_cb[idx])); - btif_rc_cb.rc_multi_cb[idx].rc_vol_label = MAX_LABEL; - btif_rc_cb.rc_multi_cb[idx].rc_volume = MAX_VOLUME; - btif_rc_cb.rc_multi_cb[idx].rc_state = BTRC_CONNECTION_STATE_DISCONNECTED; - } - lbl_init(); - - return result; + * + * Function init + * + * Description Initializes the AVRC interface + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t init(btrc_callbacks_t* callbacks) { + BTIF_TRACE_EVENT("%s: ", __func__); + bt_status_t result = BT_STATUS_SUCCESS; + + if (bt_rc_callbacks) return BT_STATUS_DONE; + + bt_rc_callbacks = callbacks; + for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) { + memset(&btif_rc_cb.rc_multi_cb[idx], 0, + sizeof(btif_rc_cb.rc_multi_cb[idx])); + btif_rc_cb.rc_multi_cb[idx].rc_vol_label = MAX_LABEL; + btif_rc_cb.rc_multi_cb[idx].rc_volume = MAX_VOLUME; + btif_rc_cb.rc_multi_cb[idx].rc_state = BTRC_CONNECTION_STATE_DISCONNECTED; + } + lbl_init(); + + return result; } /******************************************************************************* -** -** Function init_ctrl -** -** Description Initializes the AVRC interface -** -** Returns bt_status_t -** -*******************************************************************************/ -static bt_status_t init_ctrl(btrc_ctrl_callbacks_t* callbacks) -{ - BTIF_TRACE_EVENT("%s: ", __func__); - bt_status_t result = BT_STATUS_SUCCESS; - - if (bt_rc_ctrl_callbacks) - return BT_STATUS_DONE; - - bt_rc_ctrl_callbacks = callbacks; - for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) - { - memset (&btif_rc_cb.rc_multi_cb[idx], 0, sizeof(btif_rc_cb.rc_multi_cb[idx])); - btif_rc_cb.rc_multi_cb[idx].rc_vol_label = MAX_LABEL; - btif_rc_cb.rc_multi_cb[idx].rc_volume = MAX_VOLUME; - } - lbl_init(); - - return result; + * + * Function init_ctrl + * + * Description Initializes the AVRC interface + * + * Returns bt_status_t + * + ******************************************************************************/ +static bt_status_t init_ctrl(btrc_ctrl_callbacks_t* callbacks) { + BTIF_TRACE_EVENT("%s: ", __func__); + bt_status_t result = BT_STATUS_SUCCESS; + + if (bt_rc_ctrl_callbacks) return BT_STATUS_DONE; + + bt_rc_ctrl_callbacks = callbacks; + for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) { + memset(&btif_rc_cb.rc_multi_cb[idx], 0, + sizeof(btif_rc_cb.rc_multi_cb[idx])); + btif_rc_cb.rc_multi_cb[idx].rc_vol_label = MAX_LABEL; + btif_rc_cb.rc_multi_cb[idx].rc_volume = MAX_VOLUME; + } + lbl_init(); + + return result; } -static void rc_ctrl_procedure_complete(btif_rc_device_cb_t *p_dev) -{ - if (p_dev == NULL) - { - BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); - return; - } - - if (p_dev->rc_procedure_complete == true) - { - return; - } - p_dev->rc_procedure_complete = true; - uint32_t attr_list[] = { - AVRC_MEDIA_ATTR_ID_TITLE, - AVRC_MEDIA_ATTR_ID_ARTIST, - AVRC_MEDIA_ATTR_ID_ALBUM, - AVRC_MEDIA_ATTR_ID_TRACK_NUM, - AVRC_MEDIA_ATTR_ID_NUM_TRACKS, - AVRC_MEDIA_ATTR_ID_GENRE, - AVRC_MEDIA_ATTR_ID_PLAYING_TIME - }; - get_element_attribute_cmd (AVRC_MAX_NUM_MEDIA_ATTR_ID, attr_list, p_dev); +static void rc_ctrl_procedure_complete(btif_rc_device_cb_t* p_dev) { + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); + return; + } + + if (p_dev->rc_procedure_complete == true) { + return; + } + p_dev->rc_procedure_complete = true; + uint32_t attr_list[] = { + AVRC_MEDIA_ATTR_ID_TITLE, AVRC_MEDIA_ATTR_ID_ARTIST, + AVRC_MEDIA_ATTR_ID_ALBUM, AVRC_MEDIA_ATTR_ID_TRACK_NUM, + AVRC_MEDIA_ATTR_ID_NUM_TRACKS, AVRC_MEDIA_ATTR_ID_GENRE, + AVRC_MEDIA_ATTR_ID_PLAYING_TIME}; + get_element_attribute_cmd(AVRC_MAX_NUM_MEDIA_ATTR_ID, attr_list, p_dev); } /*************************************************************************** -** -** Function get_play_status_rsp -** -** Description Returns the current play status. -** This method is called in response to -** GetPlayStatus request. -** -** Returns bt_status_t -** -***************************************************************************/ -static bt_status_t get_play_status_rsp(bt_bdaddr_t *bd_addr, btrc_play_status_t play_status, - uint32_t song_len, uint32_t song_pos) -{ - tAVRC_RESPONSE avrc_rsp; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); - - CHECK_RC_CONNECTED(p_dev); - - memset(&(avrc_rsp.get_play_status), 0, sizeof(tAVRC_GET_PLAY_STATUS_RSP)); - - BTIF_TRACE_DEBUG("%s song len %d song pos %d", __func__, song_len, song_pos); - avrc_rsp.get_play_status.song_len = song_len; - avrc_rsp.get_play_status.song_pos = song_pos; - avrc_rsp.get_play_status.play_status = play_status; - - avrc_rsp.get_play_status.pdu = AVRC_PDU_GET_PLAY_STATUS; - avrc_rsp.get_play_status.opcode = opcode_from_pdu(AVRC_PDU_GET_PLAY_STATUS); - avrc_rsp.get_play_status.status = ((play_status != BTRC_PLAYSTATE_ERROR)? - AVRC_STS_NO_ERROR:AVRC_STS_BAD_PARAM); - - /* Send the response */ - send_metamsg_rsp(p_dev, IDX_GET_PLAY_STATUS_RSP, - p_dev->rc_pdu_info[IDX_GET_PLAY_STATUS_RSP].label, p_dev->rc_pdu_info[IDX_GET_PLAY_STATUS_RSP].ctype, - &avrc_rsp); - - return BT_STATUS_SUCCESS; + * + * Function get_play_status_rsp + * + * Description Returns the current play status. + * This method is called in response to + * GetPlayStatus request. + * + * Returns bt_status_t + * + **************************************************************************/ +static bt_status_t get_play_status_rsp(bt_bdaddr_t* bd_addr, + btrc_play_status_t play_status, + uint32_t song_len, uint32_t song_pos) { + tAVRC_RESPONSE avrc_rsp; + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr); + + CHECK_RC_CONNECTED(p_dev); + + memset(&(avrc_rsp.get_play_status), 0, sizeof(tAVRC_GET_PLAY_STATUS_RSP)); + + BTIF_TRACE_DEBUG("%s song len %d song pos %d", __func__, song_len, song_pos); + avrc_rsp.get_play_status.song_len = song_len; + avrc_rsp.get_play_status.song_pos = song_pos; + avrc_rsp.get_play_status.play_status = play_status; + + avrc_rsp.get_play_status.pdu = AVRC_PDU_GET_PLAY_STATUS; + avrc_rsp.get_play_status.opcode = opcode_from_pdu(AVRC_PDU_GET_PLAY_STATUS); + avrc_rsp.get_play_status.status = + ((play_status != BTRC_PLAYSTATE_ERROR) ? AVRC_STS_NO_ERROR + : AVRC_STS_BAD_PARAM); + + /* Send the response */ + send_metamsg_rsp(p_dev, IDX_GET_PLAY_STATUS_RSP, + p_dev->rc_pdu_info[IDX_GET_PLAY_STATUS_RSP].label, + p_dev->rc_pdu_info[IDX_GET_PLAY_STATUS_RSP].ctype, + &avrc_rsp); + + return BT_STATUS_SUCCESS; } /*************************************************************************** -** -** Function get_element_attr_rsp -** -** Description Returns the current songs' element attributes -** in text. -** -** Returns bt_status_t -** -***************************************************************************/ -static bt_status_t get_element_attr_rsp(bt_bdaddr_t *bd_addr, uint8_t num_attr, - btrc_element_attr_val_t *p_attrs) -{ - tAVRC_RESPONSE avrc_rsp; - uint32_t i; - tAVRC_ATTR_ENTRY element_attrs[BTRC_MAX_ELEM_ATTR_SIZE]; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); - - CHECK_RC_CONNECTED(p_dev); - - memset(element_attrs, 0, sizeof(tAVRC_ATTR_ENTRY) * num_attr); - BTIF_TRACE_ERROR("%s: calling btif_rc_get_device_by_bda",__func__); - - if (num_attr == 0) - { - avrc_rsp.get_play_status.status = AVRC_STS_BAD_PARAM; - } - else - { - for (i=0; irc_pdu_info[IDX_GET_ELEMENT_ATTR_RSP].label, p_dev->rc_pdu_info[IDX_GET_ELEMENT_ATTR_RSP].ctype, - &avrc_rsp); - - return BT_STATUS_SUCCESS; + avrc_rsp.get_play_status.status = AVRC_STS_NO_ERROR; + } + avrc_rsp.get_elem_attrs.num_attr = num_attr; + avrc_rsp.get_elem_attrs.p_attrs = element_attrs; + avrc_rsp.get_elem_attrs.pdu = AVRC_PDU_GET_ELEMENT_ATTR; + avrc_rsp.get_elem_attrs.opcode = opcode_from_pdu(AVRC_PDU_GET_ELEMENT_ATTR); + + /* Send the response */ + send_metamsg_rsp(p_dev, IDX_GET_ELEMENT_ATTR_RSP, + p_dev->rc_pdu_info[IDX_GET_ELEMENT_ATTR_RSP].label, + p_dev->rc_pdu_info[IDX_GET_ELEMENT_ATTR_RSP].ctype, + &avrc_rsp); + + return BT_STATUS_SUCCESS; } /*************************************************************************** -** -** Function reject_pending_notification -** -** Description Utility function to reject a pending notification. When AddressedPlayer -** change is received, all pending notifications should be completed. -** -** Returns void -** -***************************************************************************/ -static void reject_pending_notification (btrc_event_id_t event_id, int idx) -{ - tAVRC_RESPONSE avrc_rsp; - memset(&(avrc_rsp.reg_notif), 0, sizeof(tAVRC_REG_NOTIF_RSP)); - - avrc_rsp.reg_notif.event_id = event_id; - avrc_rsp.reg_notif.pdu = AVRC_PDU_REGISTER_NOTIFICATION; - avrc_rsp.reg_notif.opcode = opcode_from_pdu(AVRC_PDU_REGISTER_NOTIFICATION); - avrc_rsp.reg_notif.status = AVRC_STS_ADDR_PLAYER_CHG; - BTIF_TRACE_WARNING("%s: Handling event ID: 0x%x", __func__, event_id); - - send_metamsg_rsp(&btif_rc_cb.rc_multi_cb[idx], -1, - btif_rc_cb.rc_multi_cb[idx].rc_notif[event_id-1].label, AVRC_RSP_REJ, - &avrc_rsp); + * + * Function reject_pending_notification + * + * Description Utility function to reject a pending notification. When + *AddressedPlayer + * change is received, all pending notifications should be + *completed. + * + * Returns void + * + **************************************************************************/ +static void reject_pending_notification(btrc_event_id_t event_id, int idx) { + tAVRC_RESPONSE avrc_rsp; + memset(&(avrc_rsp.reg_notif), 0, sizeof(tAVRC_REG_NOTIF_RSP)); + + avrc_rsp.reg_notif.event_id = event_id; + avrc_rsp.reg_notif.pdu = AVRC_PDU_REGISTER_NOTIFICATION; + avrc_rsp.reg_notif.opcode = opcode_from_pdu(AVRC_PDU_REGISTER_NOTIFICATION); + avrc_rsp.reg_notif.status = AVRC_STS_ADDR_PLAYER_CHG; + BTIF_TRACE_WARNING("%s: Handling event ID: 0x%x", __func__, event_id); + + send_metamsg_rsp(&btif_rc_cb.rc_multi_cb[idx], -1, + btif_rc_cb.rc_multi_cb[idx].rc_notif[event_id - 1].label, + AVRC_RSP_REJ, &avrc_rsp); } /*************************************************************************** -** -** Function register_notification_rsp -** -** Description Response to the register notification request. -** -** Returns bt_status_t -** -***************************************************************************/ -static bt_status_t register_notification_rsp(btrc_event_id_t event_id, - btrc_notification_type_t type, btrc_register_notification_t *p_param) -{ - tAVRC_RESPONSE avrc_rsp; - BTIF_TRACE_EVENT("%s: event_id: %s", __func__, dump_rc_notification_event_id(event_id)); - std::unique_lock lock(btif_rc_cb.lock); - - memset(&(avrc_rsp.reg_notif), 0, sizeof(tAVRC_REG_NOTIF_RSP)); - - avrc_rsp.reg_notif.event_id = event_id; - avrc_rsp.reg_notif.pdu = AVRC_PDU_REGISTER_NOTIFICATION; - avrc_rsp.reg_notif.opcode = opcode_from_pdu(AVRC_PDU_REGISTER_NOTIFICATION); - avrc_rsp.get_play_status.status = AVRC_STS_NO_ERROR; - - for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) - { - memset(&(avrc_rsp.reg_notif.param), 0, sizeof(tAVRC_NOTIF_RSP_PARAM)); - - if (!(btif_rc_cb.rc_multi_cb[idx].rc_connected)) - { - BTIF_TRACE_ERROR("%s: Avrcp device is not connected, handle: 0x%x", __func__, - btif_rc_cb.rc_multi_cb[idx].rc_handle); - continue; - } - - if (btif_rc_cb.rc_multi_cb[idx].rc_notif[event_id-1].bNotify == false) - { - BTIF_TRACE_WARNING("%s: Avrcp Event id is not registered: event_id: %x, handle: 0x%x", - __func__, event_id, btif_rc_cb.rc_multi_cb[idx].rc_handle); - continue; - } - - BTIF_TRACE_DEBUG("%s: Avrcp Event id is registered: event_id: %x handle: 0x%x", - __func__, event_id, btif_rc_cb.rc_multi_cb[idx].rc_handle); - - switch (event_id) - { - case BTRC_EVT_PLAY_STATUS_CHANGED: - avrc_rsp.reg_notif.param.play_status = p_param->play_status; - if (avrc_rsp.reg_notif.param.play_status == PLAY_STATUS_PLAYING) - btif_av_clear_remote_suspend_flag(); - break; - case BTRC_EVT_TRACK_CHANGE: - memcpy(&(avrc_rsp.reg_notif.param.track), &(p_param->track), - sizeof(btrc_uid_t)); - break; - case BTRC_EVT_PLAY_POS_CHANGED: - avrc_rsp.reg_notif.param.play_pos = p_param->song_pos; - break; - case BTRC_EVT_AVAL_PLAYER_CHANGE: - break; - case BTRC_EVT_ADDR_PLAYER_CHANGE: - avrc_rsp.reg_notif.param.addr_player.player_id = - p_param->addr_player_changed.player_id; - avrc_rsp.reg_notif.param.addr_player.uid_counter = - p_param->addr_player_changed.uid_counter; - break; - case BTRC_EVT_UIDS_CHANGED: - avrc_rsp.reg_notif.param.uid_counter = p_param->uids_changed.uid_counter; - break; - case BTRC_EVT_NOW_PLAYING_CONTENT_CHANGED: - break; - - default: - BTIF_TRACE_WARNING("%s: Unhandled event ID: 0x%x", __func__, event_id); - return BT_STATUS_UNHANDLED; - } - - /* Send the response. */ - send_metamsg_rsp(&btif_rc_cb.rc_multi_cb[idx], -1, - btif_rc_cb.rc_multi_cb[idx].rc_notif[event_id-1].label, - ((type == BTRC_NOTIFICATION_TYPE_INTERIM)?AVRC_CMD_NOTIF:AVRC_RSP_CHANGED), - &avrc_rsp); - - /* if notification type is address player changed, then complete all player specific - * notifications with AV/C C-Type REJECTED with error code Addressed Player Changed. */ - if(event_id == BTRC_EVT_ADDR_PLAYER_CHANGE && type == BTRC_NOTIFICATION_TYPE_CHANGED) - { - /* array includes notifications to be completed on addressed player change */ - btrc_event_id_t evt_id[] = {BTRC_EVT_PLAY_STATUS_CHANGED, - BTRC_EVT_TRACK_CHANGE, - BTRC_EVT_PLAY_POS_CHANGED, - BTRC_EVT_NOW_PLAYING_CONTENT_CHANGED}; - for (uint8_t id = 0; id < sizeof(evt_id) / sizeof((evt_id)[0]); id++) - { - reject_pending_notification(evt_id[id], idx); - } - } - } - return BT_STATUS_SUCCESS; -} + * + * Function register_notification_rsp + * + * Description Response to the register notification request. + * + * Returns bt_status_t + * + **************************************************************************/ +static bt_status_t register_notification_rsp( + btrc_event_id_t event_id, btrc_notification_type_t type, + btrc_register_notification_t* p_param) { + tAVRC_RESPONSE avrc_rsp; + BTIF_TRACE_EVENT("%s: event_id: %s", __func__, + dump_rc_notification_event_id(event_id)); + std::unique_lock lock(btif_rc_cb.lock); + + memset(&(avrc_rsp.reg_notif), 0, sizeof(tAVRC_REG_NOTIF_RSP)); + + avrc_rsp.reg_notif.event_id = event_id; + avrc_rsp.reg_notif.pdu = AVRC_PDU_REGISTER_NOTIFICATION; + avrc_rsp.reg_notif.opcode = opcode_from_pdu(AVRC_PDU_REGISTER_NOTIFICATION); + avrc_rsp.get_play_status.status = AVRC_STS_NO_ERROR; + + for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) { + memset(&(avrc_rsp.reg_notif.param), 0, sizeof(tAVRC_NOTIF_RSP_PARAM)); + + if (!(btif_rc_cb.rc_multi_cb[idx].rc_connected)) { + BTIF_TRACE_ERROR("%s: Avrcp device is not connected, handle: 0x%x", + __func__, btif_rc_cb.rc_multi_cb[idx].rc_handle); + continue; + } + + if (btif_rc_cb.rc_multi_cb[idx].rc_notif[event_id - 1].bNotify == false) { + BTIF_TRACE_WARNING( + "%s: Avrcp Event id is not registered: event_id: %x, handle: 0x%x", + __func__, event_id, btif_rc_cb.rc_multi_cb[idx].rc_handle); + continue; + } + + BTIF_TRACE_DEBUG( + "%s: Avrcp Event id is registered: event_id: %x handle: 0x%x", __func__, + event_id, btif_rc_cb.rc_multi_cb[idx].rc_handle); + + switch (event_id) { + case BTRC_EVT_PLAY_STATUS_CHANGED: + avrc_rsp.reg_notif.param.play_status = p_param->play_status; + if (avrc_rsp.reg_notif.param.play_status == PLAY_STATUS_PLAYING) + btif_av_clear_remote_suspend_flag(); + break; + case BTRC_EVT_TRACK_CHANGE: + memcpy(&(avrc_rsp.reg_notif.param.track), &(p_param->track), + sizeof(btrc_uid_t)); + break; + case BTRC_EVT_PLAY_POS_CHANGED: + avrc_rsp.reg_notif.param.play_pos = p_param->song_pos; + break; + case BTRC_EVT_AVAL_PLAYER_CHANGE: + break; + case BTRC_EVT_ADDR_PLAYER_CHANGE: + avrc_rsp.reg_notif.param.addr_player.player_id = + p_param->addr_player_changed.player_id; + avrc_rsp.reg_notif.param.addr_player.uid_counter = + p_param->addr_player_changed.uid_counter; + break; + case BTRC_EVT_UIDS_CHANGED: + avrc_rsp.reg_notif.param.uid_counter = + p_param->uids_changed.uid_counter; + break; + case BTRC_EVT_NOW_PLAYING_CONTENT_CHANGED: + break; -/*************************************************************************** -** -** Function get_folder_items_list_rsp -** -** Description Returns the list of media items in current folder along with requested -** attributes. This is called in response to GetFolderItems request. -** -** Returns bt_status_t -** BT_STATUS_NOT_READY - when RC is not connected. -** BT_STATUS_SUCCESS - always if RC is connected -** BT_STATUS_UNHANDLED - when rsp is not pending for get_folder_items_list PDU -** -***************************************************************************/ -static bt_status_t get_folder_items_list_rsp(bt_bdaddr_t *bd_addr, btrc_status_t rsp_status, - uint16_t uid_counter, uint8_t num_items, btrc_folder_items_t *p_items) -{ - tAVRC_RESPONSE avrc_rsp; - tAVRC_ITEM item; - tBTA_AV_CODE code = 0, ctype = 0; - BT_HDR *p_msg = NULL; - int item_cnt; - tAVRC_STS status = AVRC_STS_NO_ERROR; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); - btrc_folder_items_t* cur_item = NULL; - - CHECK_RC_CONNECTED(p_dev); - - /* check if rsp to previous cmd was completed */ - if (p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].is_rsp_pending == false) - { - BTIF_TRACE_WARNING("%s: Not sending response as no PDU was registered", __func__); \ + default: + BTIF_TRACE_WARNING("%s: Unhandled event ID: 0x%x", __func__, event_id); return BT_STATUS_UNHANDLED; } - memset(&avrc_rsp, 0, sizeof(tAVRC_RESPONSE)); - memset(&item, 0, sizeof(tAVRC_ITEM)); - - avrc_rsp.get_items.pdu = AVRC_PDU_GET_FOLDER_ITEMS; - avrc_rsp.get_items.opcode = opcode_from_pdu(AVRC_PDU_GET_FOLDER_ITEMS); - avrc_rsp.get_items.status = status_code_map[rsp_status]; + /* Send the response. */ + send_metamsg_rsp( + &btif_rc_cb.rc_multi_cb[idx], -1, + btif_rc_cb.rc_multi_cb[idx].rc_notif[event_id - 1].label, + ((type == BTRC_NOTIFICATION_TYPE_INTERIM) ? AVRC_CMD_NOTIF + : AVRC_RSP_CHANGED), + &avrc_rsp); - if (avrc_rsp.get_items.status != AVRC_STS_NO_ERROR) - { - BTIF_TRACE_WARNING("%s: Error in parsing the received getfolderitems cmd. status: 0x%02x", - __func__, avrc_rsp.get_items.status); - status = avrc_rsp.get_items.status; + /* if notification type is address player changed, then complete all player + * specific + * notifications with AV/C C-Type REJECTED with error code Addressed Player + * Changed. */ + if (event_id == BTRC_EVT_ADDR_PLAYER_CHANGE && + type == BTRC_NOTIFICATION_TYPE_CHANGED) { + /* array includes notifications to be completed on addressed player change + */ + btrc_event_id_t evt_id[] = { + BTRC_EVT_PLAY_STATUS_CHANGED, BTRC_EVT_TRACK_CHANGE, + BTRC_EVT_PLAY_POS_CHANGED, BTRC_EVT_NOW_PLAYING_CONTENT_CHANGED}; + for (uint8_t id = 0; id < sizeof(evt_id) / sizeof((evt_id)[0]); id++) { + reject_pending_notification(evt_id[id], idx); + } } - else - { - avrc_rsp.get_items.uid_counter = uid_counter; - avrc_rsp.get_items.item_count = 1; - - /* create single item and build response iteratively for all num_items */ - for (item_cnt=0; item_cnt < num_items; item_cnt++) - { - cur_item = &p_items[item_cnt]; - item.item_type = p_items->item_type; - /* build respective item based on item_type. All items should be of same type within - * a response */ - switch (p_items->item_type) - { - case AVRC_ITEM_PLAYER: - { - item.u.player.name.charset_id = cur_item->player.charset_id; - memcpy(&(item.u.player.features), \ - &(cur_item->player.features), \ - sizeof(cur_item->player.features)); - item.u.player.major_type = cur_item->player.major_type; - item.u.player.sub_type = cur_item->player.sub_type; - item.u.player.play_status = cur_item->player.play_status; - item.u.player.player_id = cur_item->player.player_id; - item.u.player.name.p_str = cur_item->player.name; - item.u.player.name.str_len = \ - (uint16_t)strlen((char*)(cur_item->player.name)); - } - break; - - case AVRC_ITEM_FOLDER: - { - memcpy(item.u.folder.uid, cur_item->folder.uid, - sizeof(tAVRC_UID)); - item.u.folder.type = cur_item->folder.type; - item.u.folder.playable = cur_item->folder.playable; - item.u.folder.name.charset_id = AVRC_CHARSET_ID_UTF8; - item.u.folder.name.str_len = - strlen((char*)cur_item->folder.name); - item.u.folder.name.p_str = cur_item->folder.name; - } - break; - - case AVRC_ITEM_MEDIA: - { - tAVRC_ATTR_ENTRY attr_vals[BTRC_MAX_ELEM_ATTR_SIZE]; - - memcpy(item.u.media.uid, cur_item->media.uid, sizeof(tAVRC_UID)); - item.u.media.type = cur_item->media.type; - item.u.media.name.charset_id = cur_item->media.charset_id; - item.u.media.name.str_len = strlen((char*)cur_item->media.name); - item.u.media.name.p_str = cur_item->media.name; - item.u.media.attr_count = cur_item->media.num_attrs; - - /* Handle attributes of given item */ - if(item.u.media.attr_count == 0) - { - item.u.media.p_attr_list = NULL; - } - else - { - memset(&attr_vals, 0, sizeof(tAVRC_ATTR_ENTRY)*BTRC_MAX_ELEM_ATTR_SIZE); - fill_avrc_attr_entry(attr_vals, item.u.media.attr_count, - cur_item->media.p_attrs); - item.u.media.p_attr_list = attr_vals; - } - } - break; - - default: - { - BTIF_TRACE_ERROR ("%s: Unknown item_type: %d. Internal Error", __func__, p_items->item_type); - status = AVRC_STS_INTERNAL_ERR; - } - break; - } + } + return BT_STATUS_SUCCESS; +} - avrc_rsp.get_items.p_item_list = &item; +/*************************************************************************** + * + * Function get_folder_items_list_rsp + * + * Description Returns the list of media items in current folder along with + *requested + * attributes. This is called in response to GetFolderItems + *request. + * + * Returns bt_status_t + * BT_STATUS_NOT_READY - when RC is not connected. + * BT_STATUS_SUCCESS - always if RC is connected + * BT_STATUS_UNHANDLED - when rsp is not pending for + *get_folder_items_list PDU + * + **************************************************************************/ +static bt_status_t get_folder_items_list_rsp(bt_bdaddr_t* bd_addr, + btrc_status_t rsp_status, + uint16_t uid_counter, + uint8_t num_items, + btrc_folder_items_t* p_items) { + tAVRC_RESPONSE avrc_rsp; + tAVRC_ITEM item; + tBTA_AV_CODE code = 0, ctype = 0; + BT_HDR* p_msg = NULL; + int item_cnt; + tAVRC_STS status = AVRC_STS_NO_ERROR; + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr); + btrc_folder_items_t* cur_item = NULL; + + CHECK_RC_CONNECTED(p_dev); + + /* check if rsp to previous cmd was completed */ + if (p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].is_rsp_pending == false) { + BTIF_TRACE_WARNING("%s: Not sending response as no PDU was registered", + __func__); + return BT_STATUS_UNHANDLED; + } + + memset(&avrc_rsp, 0, sizeof(tAVRC_RESPONSE)); + memset(&item, 0, sizeof(tAVRC_ITEM)); + + avrc_rsp.get_items.pdu = AVRC_PDU_GET_FOLDER_ITEMS; + avrc_rsp.get_items.opcode = opcode_from_pdu(AVRC_PDU_GET_FOLDER_ITEMS); + avrc_rsp.get_items.status = status_code_map[rsp_status]; + + if (avrc_rsp.get_items.status != AVRC_STS_NO_ERROR) { + BTIF_TRACE_WARNING( + "%s: Error in parsing the received getfolderitems cmd. status: 0x%02x", + __func__, avrc_rsp.get_items.status); + status = avrc_rsp.get_items.status; + } else { + avrc_rsp.get_items.uid_counter = uid_counter; + avrc_rsp.get_items.item_count = 1; + + /* create single item and build response iteratively for all num_items */ + for (item_cnt = 0; item_cnt < num_items; item_cnt++) { + cur_item = &p_items[item_cnt]; + item.item_type = p_items->item_type; + /* build respective item based on item_type. All items should be of same + * type within + * a response */ + switch (p_items->item_type) { + case AVRC_ITEM_PLAYER: { + item.u.player.name.charset_id = cur_item->player.charset_id; + memcpy(&(item.u.player.features), &(cur_item->player.features), + sizeof(cur_item->player.features)); + item.u.player.major_type = cur_item->player.major_type; + item.u.player.sub_type = cur_item->player.sub_type; + item.u.player.play_status = cur_item->player.play_status; + item.u.player.player_id = cur_item->player.player_id; + item.u.player.name.p_str = cur_item->player.name; + item.u.player.name.str_len = + (uint16_t)strlen((char*)(cur_item->player.name)); + } break; + + case AVRC_ITEM_FOLDER: { + memcpy(item.u.folder.uid, cur_item->folder.uid, sizeof(tAVRC_UID)); + item.u.folder.type = cur_item->folder.type; + item.u.folder.playable = cur_item->folder.playable; + item.u.folder.name.charset_id = AVRC_CHARSET_ID_UTF8; + item.u.folder.name.str_len = strlen((char*)cur_item->folder.name); + item.u.folder.name.p_str = cur_item->folder.name; + } break; + + case AVRC_ITEM_MEDIA: { + tAVRC_ATTR_ENTRY attr_vals[BTRC_MAX_ELEM_ATTR_SIZE]; + + memcpy(item.u.media.uid, cur_item->media.uid, sizeof(tAVRC_UID)); + item.u.media.type = cur_item->media.type; + item.u.media.name.charset_id = cur_item->media.charset_id; + item.u.media.name.str_len = strlen((char*)cur_item->media.name); + item.u.media.name.p_str = cur_item->media.name; + item.u.media.attr_count = cur_item->media.num_attrs; + + /* Handle attributes of given item */ + if (item.u.media.attr_count == 0) { + item.u.media.p_attr_list = NULL; + } else { + memset(&attr_vals, 0, + sizeof(tAVRC_ATTR_ENTRY) * BTRC_MAX_ELEM_ATTR_SIZE); + fill_avrc_attr_entry(attr_vals, item.u.media.attr_count, + cur_item->media.p_attrs); + item.u.media.p_attr_list = attr_vals; + } + } break; + + default: { + BTIF_TRACE_ERROR("%s: Unknown item_type: %d. Internal Error", + __func__, p_items->item_type); + status = AVRC_STS_INTERNAL_ERR; + } break; + } - /* Add current item to buffer and build response if no error in item type */ - if (status != AVRC_STS_NO_ERROR) - { - /* Reject response due to error occured for unknown item_type, break the loop */ - break; - } + avrc_rsp.get_items.p_item_list = &item; - int len_before = p_msg ? p_msg->len : 0; - BTIF_TRACE_DEBUG ("%s: item_cnt: %d len: %d", __func__, item_cnt, len_before); - status = AVRC_BldResponse(p_dev->rc_handle, &avrc_rsp, &p_msg); - BTIF_TRACE_DEBUG ("%s: Build rsp status: %d len: %d", __func__, status, - (p_msg ? p_msg->len : 0)); - int len_after = p_msg ? p_msg->len : 0; - if (status != AVRC_STS_NO_ERROR || len_before == len_after) - { - /* Error occured in build response or we ran out of buffer so break the loop */ - break; - } - } - - /* setting the error status */ - avrc_rsp.get_items.status = status; - } + /* Add current item to buffer and build response if no error in item type + */ + if (status != AVRC_STS_NO_ERROR) { + /* Reject response due to error occured for unknown item_type, break the + * loop */ + break; + } - /* if packet built successfully, send the built items to BTA layer */ - if (status == AVRC_STS_NO_ERROR) - { - code = p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].ctype; - ctype = get_rsp_type_code(avrc_rsp.get_items.status, code); - BTA_AvMetaRsp(p_dev->rc_handle, - p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].label, ctype, p_msg); - } - else /* Error occured, send reject response */ - { - BTIF_TRACE_ERROR("%s: Error status: 0x%02X. Sending reject rsp", - __func__, avrc_rsp.rsp.status); - send_reject_response(p_dev->rc_handle, - p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].label, - avrc_rsp.pdu, avrc_rsp.get_items.status, avrc_rsp.get_items.opcode); + int len_before = p_msg ? p_msg->len : 0; + BTIF_TRACE_DEBUG("%s: item_cnt: %d len: %d", __func__, item_cnt, + len_before); + status = AVRC_BldResponse(p_dev->rc_handle, &avrc_rsp, &p_msg); + BTIF_TRACE_DEBUG("%s: Build rsp status: %d len: %d", __func__, status, + (p_msg ? p_msg->len : 0)); + int len_after = p_msg ? p_msg->len : 0; + if (status != AVRC_STS_NO_ERROR || len_before == len_after) { + /* Error occured in build response or we ran out of buffer so break the + * loop */ + break; + } } - /* Reset values for current pdu. */ - p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].ctype = 0; - p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].label = 0; - p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].is_rsp_pending = false; - - return status == AVRC_STS_NO_ERROR ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; + /* setting the error status */ + avrc_rsp.get_items.status = status; + } + + /* if packet built successfully, send the built items to BTA layer */ + if (status == AVRC_STS_NO_ERROR) { + code = p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].ctype; + ctype = get_rsp_type_code(avrc_rsp.get_items.status, code); + BTA_AvMetaRsp(p_dev->rc_handle, + p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].label, ctype, + p_msg); + } else /* Error occured, send reject response */ + { + BTIF_TRACE_ERROR("%s: Error status: 0x%02X. Sending reject rsp", __func__, + avrc_rsp.rsp.status); + send_reject_response( + p_dev->rc_handle, p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].label, + avrc_rsp.pdu, avrc_rsp.get_items.status, avrc_rsp.get_items.opcode); + } + + /* Reset values for current pdu. */ + p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].ctype = 0; + p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].label = 0; + p_dev->rc_pdu_info[IDX_GET_FOLDER_ITEMS_RSP].is_rsp_pending = false; + + return status == AVRC_STS_NO_ERROR ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } /*************************************************************************** -** -** Function set_addressed_player_rsp -** -** Description Response to set the addressed player for specified media player based on -** id in the media player list. -** -** Returns bt_status_t -** BT_STATUS_NOT_READY - when RC is not connected. -** BT_STATUS_SUCCESS - always if RC is connected -** -***************************************************************************/ -static bt_status_t set_addressed_player_rsp(bt_bdaddr_t *bd_addr, btrc_status_t rsp_status) -{ - tAVRC_RESPONSE avrc_rsp; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); - - CHECK_RC_CONNECTED(p_dev); - - avrc_rsp.addr_player.pdu = AVRC_PDU_SET_ADDRESSED_PLAYER; - avrc_rsp.addr_player.opcode = opcode_from_pdu(AVRC_PDU_SET_ADDRESSED_PLAYER); - avrc_rsp.addr_player.status = status_code_map[rsp_status]; + * + * Function set_addressed_player_rsp + * + * Description Response to set the addressed player for specified media + *player based on + * id in the media player list. + * + * Returns bt_status_t + * BT_STATUS_NOT_READY - when RC is not connected. + * BT_STATUS_SUCCESS - always if RC is connected + * + **************************************************************************/ +static bt_status_t set_addressed_player_rsp(bt_bdaddr_t* bd_addr, + btrc_status_t rsp_status) { + tAVRC_RESPONSE avrc_rsp; + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr); - /* Send the response. */ - send_metamsg_rsp(p_dev, IDX_SET_ADDR_PLAYER_RSP, - p_dev->rc_pdu_info[IDX_SET_ADDR_PLAYER_RSP].label, p_dev->rc_pdu_info[IDX_SET_ADDR_PLAYER_RSP].ctype, - &avrc_rsp); + CHECK_RC_CONNECTED(p_dev); + + avrc_rsp.addr_player.pdu = AVRC_PDU_SET_ADDRESSED_PLAYER; + avrc_rsp.addr_player.opcode = opcode_from_pdu(AVRC_PDU_SET_ADDRESSED_PLAYER); + avrc_rsp.addr_player.status = status_code_map[rsp_status]; + + /* Send the response. */ + send_metamsg_rsp(p_dev, IDX_SET_ADDR_PLAYER_RSP, + p_dev->rc_pdu_info[IDX_SET_ADDR_PLAYER_RSP].label, + p_dev->rc_pdu_info[IDX_SET_ADDR_PLAYER_RSP].ctype, + &avrc_rsp); - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } /*************************************************************************** -** -** Function set_browsed_player_rsp -** -** Description Response to set the browsed player command which contains current browsed path -** of the media player. By default, current_path = root and folder_depth = 0 for -** every set_browsed_player request. -** -** Returns bt_status_t -** BT_STATUS_NOT_READY - when RC is not connected. -** BT_STATUS_SUCCESS - if RC is connected and reponse sent successfully -** BT_STATUS_UNHANDLED - when rsp is not pending for set_browsed_player PDU -** -***************************************************************************/ -static bt_status_t set_browsed_player_rsp(bt_bdaddr_t *bd_addr, btrc_status_t rsp_status, - uint32_t num_items, uint16_t charset_id, uint8_t folder_depth, btrc_br_folder_name_t *p_folders) -{ - tAVRC_RESPONSE avrc_rsp; - tAVRC_NAME item; - BT_HDR *p_msg = NULL; - tBTA_AV_CODE code = 0; - tBTA_AV_CODE ctype = 0; - unsigned int item_cnt; - tAVRC_STS status = AVRC_STS_NO_ERROR; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); - - CHECK_RC_CONNECTED(p_dev); - - memset(&avrc_rsp, 0, sizeof(tAVRC_RESPONSE)); - memset(&item, 0, sizeof(tAVRC_NAME)); - - avrc_rsp.br_player.status = status_code_map[rsp_status]; - avrc_rsp.br_player.pdu = AVRC_PDU_SET_BROWSED_PLAYER; - avrc_rsp.br_player.opcode = opcode_from_pdu(AVRC_PDU_SET_BROWSED_PLAYER); - - BTIF_TRACE_DEBUG("%s: rsp_status: 0x%02X avrc_rsp.br_player.status: 0x%02X", __func__, \ - rsp_status, avrc_rsp.br_player.status); - - /* check if rsp to previous cmd was completed */ - if (p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].is_rsp_pending == false) - { - BTIF_TRACE_WARNING("%s: Not sending response as no PDU was registered", __func__); - return BT_STATUS_UNHANDLED; - } - - if (AVRC_STS_NO_ERROR == avrc_rsp.get_items.status) - { - avrc_rsp.br_player.num_items = num_items; - avrc_rsp.br_player.charset_id = charset_id; - avrc_rsp.br_player.folder_depth = folder_depth; - avrc_rsp.br_player.p_folders = (tAVRC_NAME*) p_folders; - - BTIF_TRACE_DEBUG("%s: folder_depth: 0x%02X num_items: %d", __func__, - folder_depth, num_items); - - if (folder_depth > 0) - { - /* Iteratively build response for all folders across folder depth upto current path */ - avrc_rsp.br_player.folder_depth = 1; - for(item_cnt=0; item_cnt < folder_depth; item_cnt++) - { - BTIF_TRACE_DEBUG("%s: iteration: %d", __func__, item_cnt); - item.str_len = p_folders[item_cnt].str_len; - item.p_str = p_folders[item_cnt].p_str; - avrc_rsp.br_player.p_folders = &item; - - /* Add current item to buffer and build response */ - status = AVRC_BldResponse(p_dev->rc_handle, &avrc_rsp, &p_msg); - if (AVRC_STS_NO_ERROR != status) - { - BTIF_TRACE_WARNING("%s: Build rsp status: %d", __func__, status); - /* if the build fails, it is likely that we ran out of buffer. so if we have - * some items to send, reset this error to no error for sending what we have */ - if (item_cnt > 0) - status = AVRC_STS_NO_ERROR; - - /* Error occured in build response so break the loop */ - break; - } - } - } - else /* current path is root folder, no folders navigated yet */ - { - status = AVRC_BldResponse(p_dev->rc_handle, &avrc_rsp, &p_msg); + * + * Function set_browsed_player_rsp + * + * Description Response to set the browsed player command which contains + *current browsed path + * of the media player. By default, current_path = root and + *folder_depth = 0 for + * every set_browsed_player request. + * + * Returns bt_status_t + * BT_STATUS_NOT_READY - when RC is not connected. + * BT_STATUS_SUCCESS - if RC is connected and reponse + *sent successfully + * BT_STATUS_UNHANDLED - when rsp is not pending for + *set_browsed_player PDU + * + **************************************************************************/ +static bt_status_t set_browsed_player_rsp(bt_bdaddr_t* bd_addr, + btrc_status_t rsp_status, + uint32_t num_items, + uint16_t charset_id, + uint8_t folder_depth, + btrc_br_folder_name_t* p_folders) { + tAVRC_RESPONSE avrc_rsp; + tAVRC_NAME item; + BT_HDR* p_msg = NULL; + tBTA_AV_CODE code = 0; + tBTA_AV_CODE ctype = 0; + unsigned int item_cnt; + tAVRC_STS status = AVRC_STS_NO_ERROR; + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr); + + CHECK_RC_CONNECTED(p_dev); + + memset(&avrc_rsp, 0, sizeof(tAVRC_RESPONSE)); + memset(&item, 0, sizeof(tAVRC_NAME)); + + avrc_rsp.br_player.status = status_code_map[rsp_status]; + avrc_rsp.br_player.pdu = AVRC_PDU_SET_BROWSED_PLAYER; + avrc_rsp.br_player.opcode = opcode_from_pdu(AVRC_PDU_SET_BROWSED_PLAYER); + + BTIF_TRACE_DEBUG("%s: rsp_status: 0x%02X avrc_rsp.br_player.status: 0x%02X", + __func__, rsp_status, avrc_rsp.br_player.status); + + /* check if rsp to previous cmd was completed */ + if (p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].is_rsp_pending == false) { + BTIF_TRACE_WARNING("%s: Not sending response as no PDU was registered", + __func__); + return BT_STATUS_UNHANDLED; + } + + if (AVRC_STS_NO_ERROR == avrc_rsp.get_items.status) { + avrc_rsp.br_player.num_items = num_items; + avrc_rsp.br_player.charset_id = charset_id; + avrc_rsp.br_player.folder_depth = folder_depth; + avrc_rsp.br_player.p_folders = (tAVRC_NAME*)p_folders; + + BTIF_TRACE_DEBUG("%s: folder_depth: 0x%02X num_items: %d", __func__, + folder_depth, num_items); + + if (folder_depth > 0) { + /* Iteratively build response for all folders across folder depth upto + * current path */ + avrc_rsp.br_player.folder_depth = 1; + for (item_cnt = 0; item_cnt < folder_depth; item_cnt++) { + BTIF_TRACE_DEBUG("%s: iteration: %d", __func__, item_cnt); + item.str_len = p_folders[item_cnt].str_len; + item.p_str = p_folders[item_cnt].p_str; + avrc_rsp.br_player.p_folders = &item; + + /* Add current item to buffer and build response */ + status = AVRC_BldResponse(p_dev->rc_handle, &avrc_rsp, &p_msg); + if (AVRC_STS_NO_ERROR != status) { + BTIF_TRACE_WARNING("%s: Build rsp status: %d", __func__, status); + /* if the build fails, it is likely that we ran out of buffer. so if + * we have + * some items to send, reset this error to no error for sending what we + * have */ + if (item_cnt > 0) status = AVRC_STS_NO_ERROR; + + /* Error occured in build response so break the loop */ + break; } - - /* setting the error status */ - avrc_rsp.br_player.status = status; - } - else /* error received from above layer */ - { - BTIF_TRACE_WARNING("%s: Error in parsing the received setbrowsed command. status: 0x%02x", - __func__, avrc_rsp.br_player.status); - status = avrc_rsp.br_player.status; - } - - /* if packet built successfully, send the built items to BTA layer */ - if (status == AVRC_STS_NO_ERROR) - { - code = p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].ctype; - ctype = get_rsp_type_code(avrc_rsp.br_player.status, code); - BTA_AvMetaRsp(p_dev->rc_handle, - p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].label, ctype, p_msg); - } - else /* Error occured, send reject response */ - { - BTIF_TRACE_ERROR("%s: Error status: 0x%02X. Sending reject rsp", - __func__, avrc_rsp.br_player.status); - send_reject_response(p_dev->rc_handle, - p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].label, - avrc_rsp.pdu, avrc_rsp.br_player.status, avrc_rsp.get_items.opcode); - } - - /* Reset values for set_browsed_player pdu.*/ - p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].ctype = 0; - p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].label = 0; - p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].is_rsp_pending = false; - - return status == AVRC_STS_NO_ERROR ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; + } + } else /* current path is root folder, no folders navigated yet */ + { + status = AVRC_BldResponse(p_dev->rc_handle, &avrc_rsp, &p_msg); + } + + /* setting the error status */ + avrc_rsp.br_player.status = status; + } else /* error received from above layer */ + { + BTIF_TRACE_WARNING( + "%s: Error in parsing the received setbrowsed command. status: 0x%02x", + __func__, avrc_rsp.br_player.status); + status = avrc_rsp.br_player.status; + } + + /* if packet built successfully, send the built items to BTA layer */ + if (status == AVRC_STS_NO_ERROR) { + code = p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].ctype; + ctype = get_rsp_type_code(avrc_rsp.br_player.status, code); + BTA_AvMetaRsp(p_dev->rc_handle, + p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].label, ctype, + p_msg); + } else /* Error occured, send reject response */ + { + BTIF_TRACE_ERROR("%s: Error status: 0x%02X. Sending reject rsp", __func__, + avrc_rsp.br_player.status); + send_reject_response( + p_dev->rc_handle, p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].label, + avrc_rsp.pdu, avrc_rsp.br_player.status, avrc_rsp.get_items.opcode); + } + + /* Reset values for set_browsed_player pdu.*/ + p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].ctype = 0; + p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].label = 0; + p_dev->rc_pdu_info[IDX_SET_BROWSED_PLAYER_RSP].is_rsp_pending = false; + + return status == AVRC_STS_NO_ERROR ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } - #if (AVRC_ADV_CTRL_INCLUDED == TRUE) /******************************************************************************* -** -** Function change_path_rsp -** -** Description Response to the change path command which -** contains number of items in the changed path. -** -** Returns bt_status_t -** BT_STATUS_NOT_READY - when RC is not connected. -** BT_STATUS_SUCCESS - always if RC is connected -** -***************************************************************************/ -static bt_status_t change_path_rsp(bt_bdaddr_t *bd_addr, btrc_status_t rsp_status, - uint32_t num_items) -{ - tAVRC_RESPONSE avrc_rsp; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); - - CHECK_RC_CONNECTED(p_dev); - - avrc_rsp.chg_path.pdu = AVRC_PDU_CHANGE_PATH; - avrc_rsp.chg_path.opcode = opcode_from_pdu(AVRC_PDU_CHANGE_PATH); - avrc_rsp.chg_path.num_items = num_items; - avrc_rsp.chg_path.status = status_code_map[rsp_status]; - - /* Send the response. */ - send_metamsg_rsp(p_dev, IDX_CHG_PATH_RSP, - p_dev->rc_pdu_info[IDX_CHG_PATH_RSP].label, p_dev->rc_pdu_info[IDX_CHG_PATH_RSP].ctype, - &avrc_rsp); - - return BT_STATUS_SUCCESS; + * + * Function change_path_rsp + * + * Description Response to the change path command which + * contains number of items in the changed path. + * + * Returns bt_status_t + * BT_STATUS_NOT_READY - when RC is not connected. + * BT_STATUS_SUCCESS - always if RC is connected + * + **************************************************************************/ +static bt_status_t change_path_rsp(bt_bdaddr_t* bd_addr, + btrc_status_t rsp_status, + uint32_t num_items) { + tAVRC_RESPONSE avrc_rsp; + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr); + + CHECK_RC_CONNECTED(p_dev); + + avrc_rsp.chg_path.pdu = AVRC_PDU_CHANGE_PATH; + avrc_rsp.chg_path.opcode = opcode_from_pdu(AVRC_PDU_CHANGE_PATH); + avrc_rsp.chg_path.num_items = num_items; + avrc_rsp.chg_path.status = status_code_map[rsp_status]; + + /* Send the response. */ + send_metamsg_rsp(p_dev, IDX_CHG_PATH_RSP, + p_dev->rc_pdu_info[IDX_CHG_PATH_RSP].label, + p_dev->rc_pdu_info[IDX_CHG_PATH_RSP].ctype, &avrc_rsp); + + return BT_STATUS_SUCCESS; } #endif /*************************************************************************** -** -** Function search_rsp -** -** Description Response to search a string from media content command. -** -** Returns bt_status_t -** BT_STATUS_NOT_READY - when RC is not connected. -** BT_STATUS_SUCCESS - always if RC is connected -** -***************************************************************************/ -static bt_status_t search_rsp(bt_bdaddr_t *bd_addr, btrc_status_t rsp_status, - uint32_t uid_counter, uint32_t num_items) -{ - tAVRC_RESPONSE avrc_rsp; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); - - CHECK_RC_CONNECTED(p_dev); - - avrc_rsp.search.pdu = AVRC_PDU_SEARCH; - avrc_rsp.search.opcode = opcode_from_pdu(AVRC_PDU_SEARCH); - avrc_rsp.search.num_items = num_items; - avrc_rsp.search.uid_counter = uid_counter; - avrc_rsp.search.status = status_code_map[rsp_status]; - - /* Send the response. */ - send_metamsg_rsp(p_dev, IDX_SEARCH_RSP, - p_dev->rc_pdu_info[IDX_SEARCH_RSP].label, p_dev->rc_pdu_info[IDX_SEARCH_RSP].ctype, - &avrc_rsp); - - return BT_STATUS_SUCCESS; + * + * Function search_rsp + * + * Description Response to search a string from media content command. + * + * Returns bt_status_t + * BT_STATUS_NOT_READY - when RC is not connected. + * BT_STATUS_SUCCESS - always if RC is connected + * + **************************************************************************/ +static bt_status_t search_rsp(bt_bdaddr_t* bd_addr, btrc_status_t rsp_status, + uint32_t uid_counter, uint32_t num_items) { + tAVRC_RESPONSE avrc_rsp; + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr); + + CHECK_RC_CONNECTED(p_dev); + + avrc_rsp.search.pdu = AVRC_PDU_SEARCH; + avrc_rsp.search.opcode = opcode_from_pdu(AVRC_PDU_SEARCH); + avrc_rsp.search.num_items = num_items; + avrc_rsp.search.uid_counter = uid_counter; + avrc_rsp.search.status = status_code_map[rsp_status]; + + /* Send the response. */ + send_metamsg_rsp(p_dev, IDX_SEARCH_RSP, + p_dev->rc_pdu_info[IDX_SEARCH_RSP].label, + p_dev->rc_pdu_info[IDX_SEARCH_RSP].ctype, &avrc_rsp); + + return BT_STATUS_SUCCESS; } /*************************************************************************** -** -** Function get_item_attr_rsp -** -** Description Response to the get item's attributes command which -** contains number of attributes and values list in text. -** -** Returns bt_status_t -** BT_STATUS_NOT_READY - when RC is not connected. -** BT_STATUS_SUCCESS - always if RC is connected -** -***************************************************************************/ -static bt_status_t get_item_attr_rsp(bt_bdaddr_t *bd_addr, btrc_status_t rsp_status, - uint8_t num_attr, btrc_element_attr_val_t *p_attrs) -{ - tAVRC_RESPONSE avrc_rsp; - tAVRC_ATTR_ENTRY item_attrs[BTRC_MAX_ELEM_ATTR_SIZE]; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); - - CHECK_RC_CONNECTED(p_dev); - - memset(item_attrs, 0, sizeof(tAVRC_ATTR_ENTRY) * num_attr); - - if (num_attr == 0) - { - avrc_rsp.get_attrs.status = AVRC_STS_INTERNAL_ERR; - BTIF_TRACE_ERROR("%s: num_attr in rsp is 0, sending internal error: 0x%02X", - __func__, avrc_rsp.get_attrs.status); - } - else - { - avrc_rsp.get_attrs.status = status_code_map[rsp_status]; - if (rsp_status == BTRC_STS_NO_ERROR) - { - fill_avrc_attr_entry(item_attrs, num_attr, p_attrs); - } - } - avrc_rsp.get_attrs.attr_count = num_attr; - avrc_rsp.get_attrs.p_attr_list = item_attrs; - avrc_rsp.get_attrs.pdu = AVRC_PDU_GET_ITEM_ATTRIBUTES; - avrc_rsp.get_attrs.opcode = opcode_from_pdu(AVRC_PDU_GET_ITEM_ATTRIBUTES); - - /* Send the response. */ - send_metamsg_rsp(p_dev, IDX_GET_ITEM_ATTR_RSP, - p_dev->rc_pdu_info[IDX_GET_ITEM_ATTR_RSP].label, p_dev->rc_pdu_info[IDX_GET_ITEM_ATTR_RSP].ctype, - &avrc_rsp); - - return BT_STATUS_SUCCESS; + * + * Function get_item_attr_rsp + * + * Description Response to the get item's attributes command which + * contains number of attributes and values list in text. + * + * Returns bt_status_t + * BT_STATUS_NOT_READY - when RC is not connected. + * BT_STATUS_SUCCESS - always if RC is connected + * + **************************************************************************/ +static bt_status_t get_item_attr_rsp(bt_bdaddr_t* bd_addr, + btrc_status_t rsp_status, uint8_t num_attr, + btrc_element_attr_val_t* p_attrs) { + tAVRC_RESPONSE avrc_rsp; + tAVRC_ATTR_ENTRY item_attrs[BTRC_MAX_ELEM_ATTR_SIZE]; + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr); + + CHECK_RC_CONNECTED(p_dev); + + memset(item_attrs, 0, sizeof(tAVRC_ATTR_ENTRY) * num_attr); + + if (num_attr == 0) { + avrc_rsp.get_attrs.status = AVRC_STS_INTERNAL_ERR; + BTIF_TRACE_ERROR("%s: num_attr in rsp is 0, sending internal error: 0x%02X", + __func__, avrc_rsp.get_attrs.status); + } else { + avrc_rsp.get_attrs.status = status_code_map[rsp_status]; + if (rsp_status == BTRC_STS_NO_ERROR) { + fill_avrc_attr_entry(item_attrs, num_attr, p_attrs); + } + } + avrc_rsp.get_attrs.attr_count = num_attr; + avrc_rsp.get_attrs.p_attr_list = item_attrs; + avrc_rsp.get_attrs.pdu = AVRC_PDU_GET_ITEM_ATTRIBUTES; + avrc_rsp.get_attrs.opcode = opcode_from_pdu(AVRC_PDU_GET_ITEM_ATTRIBUTES); + + /* Send the response. */ + send_metamsg_rsp(p_dev, IDX_GET_ITEM_ATTR_RSP, + p_dev->rc_pdu_info[IDX_GET_ITEM_ATTR_RSP].label, + p_dev->rc_pdu_info[IDX_GET_ITEM_ATTR_RSP].ctype, &avrc_rsp); + + return BT_STATUS_SUCCESS; } /*************************************************************************** -** -** Function add_to_now_playing_rsp -** -** Description Response to command for adding speciafied media item -** to Now Playing queue. -** -** Returns bt_status_t -** BT_STATUS_NOT_READY - when RC is not connected. -** BT_STATUS_SUCCESS - always if RC is connected -** -***************************************************************************/ -static bt_status_t add_to_now_playing_rsp(bt_bdaddr_t *bd_addr, btrc_status_t rsp_status) -{ - tAVRC_RESPONSE avrc_rsp; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); - - CHECK_RC_CONNECTED(p_dev); - - avrc_rsp.add_to_play.pdu = AVRC_PDU_ADD_TO_NOW_PLAYING; - avrc_rsp.add_to_play.opcode = opcode_from_pdu(AVRC_PDU_ADD_TO_NOW_PLAYING); - avrc_rsp.add_to_play.status = status_code_map[rsp_status]; + * + * Function add_to_now_playing_rsp + * + * Description Response to command for adding speciafied media item + * to Now Playing queue. + * + * Returns bt_status_t + * BT_STATUS_NOT_READY - when RC is not connected. + * BT_STATUS_SUCCESS - always if RC is connected + * + **************************************************************************/ +static bt_status_t add_to_now_playing_rsp(bt_bdaddr_t* bd_addr, + btrc_status_t rsp_status) { + tAVRC_RESPONSE avrc_rsp; + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr); - /* Send the response. */ - send_metamsg_rsp(p_dev, IDX_ADD_TO_NOW_PLAYING_RSP, - p_dev->rc_pdu_info[IDX_ADD_TO_NOW_PLAYING_RSP].label, p_dev->rc_pdu_info[IDX_ADD_TO_NOW_PLAYING_RSP].ctype, - &avrc_rsp); + CHECK_RC_CONNECTED(p_dev); - return BT_STATUS_SUCCESS; + avrc_rsp.add_to_play.pdu = AVRC_PDU_ADD_TO_NOW_PLAYING; + avrc_rsp.add_to_play.opcode = opcode_from_pdu(AVRC_PDU_ADD_TO_NOW_PLAYING); + avrc_rsp.add_to_play.status = status_code_map[rsp_status]; + + /* Send the response. */ + send_metamsg_rsp(p_dev, IDX_ADD_TO_NOW_PLAYING_RSP, + p_dev->rc_pdu_info[IDX_ADD_TO_NOW_PLAYING_RSP].label, + p_dev->rc_pdu_info[IDX_ADD_TO_NOW_PLAYING_RSP].ctype, + &avrc_rsp); + + return BT_STATUS_SUCCESS; } /*************************************************************************** -** -** Function play_item_rsp -** -** Description Response to command for playing the specified media item. -** -** Returns bt_status_t -** BT_STATUS_NOT_READY - when RC is not connected. -** BT_STATUS_SUCCESS - always if RC is connected -** -***************************************************************************/ -static bt_status_t play_item_rsp(bt_bdaddr_t *bd_addr, btrc_status_t rsp_status) -{ - tAVRC_RESPONSE avrc_rsp; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); - - CHECK_RC_CONNECTED(p_dev); - - avrc_rsp.play_item.pdu = AVRC_PDU_PLAY_ITEM; - avrc_rsp.play_item.opcode = opcode_from_pdu(AVRC_PDU_PLAY_ITEM); - avrc_rsp.play_item.status = status_code_map[rsp_status]; + * + * Function play_item_rsp + * + * Description Response to command for playing the specified media item. + * + * Returns bt_status_t + * BT_STATUS_NOT_READY - when RC is not connected. + * BT_STATUS_SUCCESS - always if RC is connected + * + **************************************************************************/ +static bt_status_t play_item_rsp(bt_bdaddr_t* bd_addr, + btrc_status_t rsp_status) { + tAVRC_RESPONSE avrc_rsp; + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr); - /* Send the response. */ - send_metamsg_rsp(p_dev, IDX_PLAY_ITEM_RSP, - p_dev->rc_pdu_info[IDX_PLAY_ITEM_RSP].label, p_dev->rc_pdu_info[IDX_PLAY_ITEM_RSP].ctype, - &avrc_rsp); + CHECK_RC_CONNECTED(p_dev); - return BT_STATUS_SUCCESS; -} + avrc_rsp.play_item.pdu = AVRC_PDU_PLAY_ITEM; + avrc_rsp.play_item.opcode = opcode_from_pdu(AVRC_PDU_PLAY_ITEM); + avrc_rsp.play_item.status = status_code_map[rsp_status]; -/*************************************************************************** -** -** Function get_total_num_of_items_rsp -** -** Description response to command to get the Number of Items -** in the selected folder at the selected scope -** -** Returns bt_status_t -** BT_STATUS_NOT_READY - when RC is not connected. -** BT_STATUS_SUCCESS - always if RC is connected -** -***************************************************************************/ -static bt_status_t get_total_num_of_items_rsp(bt_bdaddr_t *bd_addr, btrc_status_t rsp_status, - uint32_t uid_counter, uint32_t num_items) -{ - tAVRC_RESPONSE avrc_rsp; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); - - CHECK_RC_CONNECTED(p_dev); - - avrc_rsp.get_num_of_items.pdu = AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS; - avrc_rsp.get_num_of_items.opcode = opcode_from_pdu(AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS); - avrc_rsp.get_num_of_items.num_items = num_items; - avrc_rsp.get_num_of_items.uid_counter = uid_counter; - avrc_rsp.get_num_of_items.status = status_code_map[rsp_status]; + /* Send the response. */ + send_metamsg_rsp(p_dev, IDX_PLAY_ITEM_RSP, + p_dev->rc_pdu_info[IDX_PLAY_ITEM_RSP].label, + p_dev->rc_pdu_info[IDX_PLAY_ITEM_RSP].ctype, &avrc_rsp); - /* Send the response. */ - send_metamsg_rsp(p_dev, IDX_GET_TOTAL_NUM_OF_ITEMS_RSP, - p_dev->rc_pdu_info[IDX_GET_TOTAL_NUM_OF_ITEMS_RSP].label, p_dev->rc_pdu_info[IDX_GET_TOTAL_NUM_OF_ITEMS_RSP].ctype, - &avrc_rsp); + return BT_STATUS_SUCCESS; +} - return BT_STATUS_SUCCESS; +/*************************************************************************** + * + * Function get_total_num_of_items_rsp + * + * Description response to command to get the Number of Items + * in the selected folder at the selected scope + * + * Returns bt_status_t + * BT_STATUS_NOT_READY - when RC is not connected. + * BT_STATUS_SUCCESS - always if RC is connected + * + **************************************************************************/ +static bt_status_t get_total_num_of_items_rsp(bt_bdaddr_t* bd_addr, + btrc_status_t rsp_status, + uint32_t uid_counter, + uint32_t num_items) { + tAVRC_RESPONSE avrc_rsp; + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr); + + CHECK_RC_CONNECTED(p_dev); + + avrc_rsp.get_num_of_items.pdu = AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS; + avrc_rsp.get_num_of_items.opcode = + opcode_from_pdu(AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS); + avrc_rsp.get_num_of_items.num_items = num_items; + avrc_rsp.get_num_of_items.uid_counter = uid_counter; + avrc_rsp.get_num_of_items.status = status_code_map[rsp_status]; + + /* Send the response. */ + send_metamsg_rsp(p_dev, IDX_GET_TOTAL_NUM_OF_ITEMS_RSP, + p_dev->rc_pdu_info[IDX_GET_TOTAL_NUM_OF_ITEMS_RSP].label, + p_dev->rc_pdu_info[IDX_GET_TOTAL_NUM_OF_ITEMS_RSP].ctype, + &avrc_rsp); + + return BT_STATUS_SUCCESS; } /*************************************************************************** -** -** Function set_volume -** -** Description Send current volume setting to remote side. -** Support limited to SetAbsoluteVolume -** This can be enhanced to support Relative Volume (AVRCP 1.0). -** With RelateVolume, we will send VOLUME_UP/VOLUME_DOWN -** as opposed to absolute volume level -** volume: Should be in the range 0-127. bit7 is reseved and cannot be set -** -** Returns bt_status_t -** -***************************************************************************/ -static bt_status_t set_volume(uint8_t volume) -{ - BTIF_TRACE_DEBUG("%s: volume: %d", __func__, volume); - tAVRC_STS status = BT_STATUS_UNSUPPORTED; - rc_transaction_t *p_transaction=NULL; - - for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) - { + * + * Function set_volume + * + * Description Send current volume setting to remote side. + * Support limited to SetAbsoluteVolume + * This can be enhanced to support Relative Volume (AVRCP 1.0). + * With RelateVolume, we will send VOLUME_UP/VOLUME_DOWN + * as opposed to absolute volume level + * volume: Should be in the range 0-127. bit7 is reseved and cannot be set + * + * Returns bt_status_t + * + **************************************************************************/ +static bt_status_t set_volume(uint8_t volume) { + BTIF_TRACE_DEBUG("%s: volume: %d", __func__, volume); + tAVRC_STS status = BT_STATUS_UNSUPPORTED; + rc_transaction_t* p_transaction = NULL; + + for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) { + if (!btif_rc_cb.rc_multi_cb[idx].rc_connected) { + status = BT_STATUS_NOT_READY; + BTIF_TRACE_ERROR("%s: RC is not connected for device: 0x%x", __func__, + btif_rc_cb.rc_multi_cb[idx].rc_addr); + continue; + } + + if (btif_rc_cb.rc_multi_cb[idx].rc_volume == volume) { + status = BT_STATUS_DONE; + BTIF_TRACE_ERROR("%s: volume value already set earlier: 0x%02x", __func__, + volume); + continue; + } + + if ((btif_rc_cb.rc_multi_cb[idx].rc_volume != volume) && + btif_rc_cb.rc_multi_cb[idx].rc_state == + BTRC_CONNECTION_STATE_CONNECTED) { + if ((btif_rc_cb.rc_multi_cb[idx].rc_features & BTA_AV_FEAT_RCTG) == 0) { + status = BT_STATUS_NOT_READY; + continue; + } else { + tAVRC_COMMAND avrc_cmd = {0}; + BT_HDR* p_msg = NULL; - if (!btif_rc_cb.rc_multi_cb[idx].rc_connected) { - status = BT_STATUS_NOT_READY; - BTIF_TRACE_ERROR("%s: RC is not connected for device: 0x%x",__func__, - btif_rc_cb.rc_multi_cb[idx].rc_addr); - continue; - } + if (btif_rc_cb.rc_multi_cb[idx].rc_features & BTA_AV_FEAT_ADV_CTRL) { + BTIF_TRACE_DEBUG("%s: Peer supports absolute volume. newVolume: %d", + __func__, volume); + avrc_cmd.volume.opcode = AVRC_OP_VENDOR; + avrc_cmd.volume.pdu = AVRC_PDU_SET_ABSOLUTE_VOLUME; + avrc_cmd.volume.status = AVRC_STS_NO_ERROR; + avrc_cmd.volume.volume = volume; - if (btif_rc_cb.rc_multi_cb[idx].rc_volume == volume) - { - status = BT_STATUS_DONE; - BTIF_TRACE_ERROR("%s: volume value already set earlier: 0x%02x",__func__, volume); - continue; - } + if (AVRC_BldCommand(&avrc_cmd, &p_msg) == AVRC_STS_NO_ERROR) { + bt_status_t tran_status = get_transaction(&p_transaction); - if ((btif_rc_cb.rc_multi_cb[idx].rc_volume != volume) && - btif_rc_cb.rc_multi_cb[idx].rc_state == BTRC_CONNECTION_STATE_CONNECTED) - { - if ((btif_rc_cb.rc_multi_cb[idx].rc_features & BTA_AV_FEAT_RCTG) == 0) - { - status = BT_STATUS_NOT_READY; - continue; - } - else - { - tAVRC_COMMAND avrc_cmd = {0}; - BT_HDR *p_msg = NULL; - - if (btif_rc_cb.rc_multi_cb[idx].rc_features & BTA_AV_FEAT_ADV_CTRL) - { - - BTIF_TRACE_DEBUG("%s: Peer supports absolute volume. newVolume: %d", - __func__, volume); - avrc_cmd.volume.opcode = AVRC_OP_VENDOR; - avrc_cmd.volume.pdu = AVRC_PDU_SET_ABSOLUTE_VOLUME; - avrc_cmd.volume.status = AVRC_STS_NO_ERROR; - avrc_cmd.volume.volume = volume; - - if (AVRC_BldCommand(&avrc_cmd, &p_msg) == AVRC_STS_NO_ERROR) - { - bt_status_t tran_status = get_transaction(&p_transaction); - - if(BT_STATUS_SUCCESS == tran_status && NULL!=p_transaction) - { - BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", - __func__, p_transaction->lbl); - BTA_AvMetaCmd(btif_rc_cb.rc_multi_cb[idx].rc_handle,p_transaction->lbl, - AVRC_CMD_CTRL, p_msg); - status = BT_STATUS_SUCCESS; - } - else - { - osi_free_and_reset((void **)&p_msg); - BTIF_TRACE_ERROR( - "%s: failed to obtain transaction details. status: 0x%02x", - __func__, tran_status); - status = BT_STATUS_FAIL; - } - } - else - { - BTIF_TRACE_ERROR( - "%s: failed to build absolute volume command. status: 0x%02x", - __func__, status); - status = BT_STATUS_FAIL; - } - } + if (BT_STATUS_SUCCESS == tran_status && NULL != p_transaction) { + BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", + __func__, p_transaction->lbl); + BTA_AvMetaCmd(btif_rc_cb.rc_multi_cb[idx].rc_handle, + p_transaction->lbl, AVRC_CMD_CTRL, p_msg); + status = BT_STATUS_SUCCESS; + } else { + osi_free_and_reset((void**)&p_msg); + BTIF_TRACE_ERROR( + "%s: failed to obtain transaction details. status: 0x%02x", + __func__, tran_status); + status = BT_STATUS_FAIL; } + } else { + BTIF_TRACE_ERROR( + "%s: failed to build absolute volume command. status: 0x%02x", + __func__, status); + status = BT_STATUS_FAIL; + } } + } } - return (bt_status_t)status; + } + return (bt_status_t)status; } #if (AVRC_ADV_CTRL_INCLUDED == TRUE) /*************************************************************************** -** -** Function register_volumechange -** -** Description Register for volume change notification from remote side. -** -** Returns void -** -***************************************************************************/ - -static void register_volumechange (uint8_t lbl, btif_rc_device_cb_t *p_dev) -{ - tAVRC_COMMAND avrc_cmd = {0}; - BT_HDR *p_msg = NULL; - tAVRC_STS BldResp = AVRC_STS_BAD_CMD; - rc_transaction_t *p_transaction = NULL; - - BTIF_TRACE_DEBUG("%s: label: %d", __func__, lbl); - - avrc_cmd.cmd.opcode=0x00; - avrc_cmd.pdu = AVRC_PDU_REGISTER_NOTIFICATION; - avrc_cmd.reg_notif.event_id = AVRC_EVT_VOLUME_CHANGE; - avrc_cmd.reg_notif.status = AVRC_STS_NO_ERROR; - avrc_cmd.reg_notif.param = 0; - - BldResp=AVRC_BldCommand(&avrc_cmd, &p_msg); - if (AVRC_STS_NO_ERROR == BldResp && p_msg) { - p_transaction = get_transaction_by_lbl(lbl); - if (p_transaction != NULL) { - BTA_AvMetaCmd(p_dev->rc_handle, p_transaction->lbl, - AVRC_CMD_NOTIF, p_msg); - BTIF_TRACE_DEBUG("%s: BTA_AvMetaCmd called", __func__); - } else { - osi_free(p_msg); - BTIF_TRACE_ERROR("%s: transaction not obtained with label: %d", - __func__, lbl); - } + * + * Function register_volumechange + * + * Description Register for volume change notification from remote side. + * + * Returns void + * + **************************************************************************/ + +static void register_volumechange(uint8_t lbl, btif_rc_device_cb_t* p_dev) { + tAVRC_COMMAND avrc_cmd = {0}; + BT_HDR* p_msg = NULL; + tAVRC_STS BldResp = AVRC_STS_BAD_CMD; + rc_transaction_t* p_transaction = NULL; + + BTIF_TRACE_DEBUG("%s: label: %d", __func__, lbl); + + avrc_cmd.cmd.opcode = 0x00; + avrc_cmd.pdu = AVRC_PDU_REGISTER_NOTIFICATION; + avrc_cmd.reg_notif.event_id = AVRC_EVT_VOLUME_CHANGE; + avrc_cmd.reg_notif.status = AVRC_STS_NO_ERROR; + avrc_cmd.reg_notif.param = 0; + + BldResp = AVRC_BldCommand(&avrc_cmd, &p_msg); + if (AVRC_STS_NO_ERROR == BldResp && p_msg) { + p_transaction = get_transaction_by_lbl(lbl); + if (p_transaction != NULL) { + BTA_AvMetaCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_NOTIF, + p_msg); + BTIF_TRACE_DEBUG("%s: BTA_AvMetaCmd called", __func__); } else { - BTIF_TRACE_ERROR("%s: failed to build command: %d", __func__, BldResp); + osi_free(p_msg); + BTIF_TRACE_ERROR("%s: transaction not obtained with label: %d", __func__, + lbl); } + } else { + BTIF_TRACE_ERROR("%s: failed to build command: %d", __func__, BldResp); + } } /*************************************************************************** -** -** Function handle_rc_metamsg_rsp -** -** Description Handle RC metamessage response -** -** Returns void -** -***************************************************************************/ -static void handle_rc_metamsg_rsp(tBTA_AV_META_MSG *pmeta_msg, btif_rc_device_cb_t *p_dev) -{ - tAVRC_RESPONSE avrc_response = {0}; - uint8_t scratch_buf[512] = {0}; - tAVRC_STS status = BT_STATUS_UNSUPPORTED; - - BTIF_TRACE_DEBUG("%s: ", __func__); - - if (AVRC_OP_VENDOR == pmeta_msg->p_msg->hdr.opcode && (AVRC_RSP_CHANGED == pmeta_msg->code - || AVRC_RSP_INTERIM == pmeta_msg->code || AVRC_RSP_ACCEPT == pmeta_msg->code - || AVRC_RSP_REJ == pmeta_msg->code || AVRC_RSP_NOT_IMPL == pmeta_msg->code)) - { - status = AVRC_ParsResponse(pmeta_msg->p_msg, &avrc_response, scratch_buf, - sizeof(scratch_buf)); - BTIF_TRACE_DEBUG("%s: code:%d, event ID: %d, PDU: %x, parsing status: %d, label: %d", - __func__, pmeta_msg->code, avrc_response.reg_notif.event_id, - avrc_response.reg_notif.pdu, status, pmeta_msg->label); - - if (status != AVRC_STS_NO_ERROR) - { - if (AVRC_PDU_REGISTER_NOTIFICATION == avrc_response.rsp.pdu - && AVRC_EVT_VOLUME_CHANGE == avrc_response.reg_notif.event_id - && p_dev->rc_vol_label == pmeta_msg->label) - { - p_dev->rc_vol_label = MAX_LABEL; - release_transaction(p_dev->rc_vol_label); - } - else if (AVRC_PDU_SET_ABSOLUTE_VOLUME == avrc_response.rsp.pdu) - { - release_transaction(pmeta_msg->label); - } - return; - } - - if (AVRC_PDU_REGISTER_NOTIFICATION == avrc_response.rsp.pdu - && AVRC_EVT_VOLUME_CHANGE == avrc_response.reg_notif.event_id - && p_dev->rc_vol_label != pmeta_msg->label) - { - // Just discard the message, if the device sends back with an incorrect label - BTIF_TRACE_DEBUG("%s: Discarding register notification in rsp.code: %d and label: %d", - __func__, pmeta_msg->code, pmeta_msg->label); - return; - } - } - else - { - BTIF_TRACE_DEBUG( - "%s: Received vendor dependent in adv ctrl rsp. code: %d len: %d. Not processing it.", - __func__, pmeta_msg->code, pmeta_msg->len); - return; - } + * + * Function handle_rc_metamsg_rsp + * + * Description Handle RC metamessage response + * + * Returns void + * + **************************************************************************/ +static void handle_rc_metamsg_rsp(tBTA_AV_META_MSG* pmeta_msg, + btif_rc_device_cb_t* p_dev) { + tAVRC_RESPONSE avrc_response = {0}; + uint8_t scratch_buf[512] = {0}; + tAVRC_STS status = BT_STATUS_UNSUPPORTED; + + BTIF_TRACE_DEBUG("%s: ", __func__); + + if (AVRC_OP_VENDOR == pmeta_msg->p_msg->hdr.opcode && + (AVRC_RSP_CHANGED == pmeta_msg->code || + AVRC_RSP_INTERIM == pmeta_msg->code || + AVRC_RSP_ACCEPT == pmeta_msg->code || AVRC_RSP_REJ == pmeta_msg->code || + AVRC_RSP_NOT_IMPL == pmeta_msg->code)) { + status = AVRC_ParsResponse(pmeta_msg->p_msg, &avrc_response, scratch_buf, + sizeof(scratch_buf)); + BTIF_TRACE_DEBUG( + "%s: code:%d, event ID: %d, PDU: %x, parsing status: %d, label: %d", + __func__, pmeta_msg->code, avrc_response.reg_notif.event_id, + avrc_response.reg_notif.pdu, status, pmeta_msg->label); + + if (status != AVRC_STS_NO_ERROR) { + if (AVRC_PDU_REGISTER_NOTIFICATION == avrc_response.rsp.pdu && + AVRC_EVT_VOLUME_CHANGE == avrc_response.reg_notif.event_id && + p_dev->rc_vol_label == pmeta_msg->label) { + p_dev->rc_vol_label = MAX_LABEL; + release_transaction(p_dev->rc_vol_label); + } else if (AVRC_PDU_SET_ABSOLUTE_VOLUME == avrc_response.rsp.pdu) { + release_transaction(pmeta_msg->label); + } + return; + } + + if (AVRC_PDU_REGISTER_NOTIFICATION == avrc_response.rsp.pdu && + AVRC_EVT_VOLUME_CHANGE == avrc_response.reg_notif.event_id && + p_dev->rc_vol_label != pmeta_msg->label) { + // Just discard the message, if the device sends back with an incorrect + // label + BTIF_TRACE_DEBUG( + "%s: Discarding register notification in rsp.code: %d and label: %d", + __func__, pmeta_msg->code, pmeta_msg->label); + return; + } + } else { + BTIF_TRACE_DEBUG( + "%s: Received vendor dependent in adv ctrl rsp. code: %d len: %d. Not " + "processing it.", + __func__, pmeta_msg->code, pmeta_msg->len); + return; + } + + if (AVRC_PDU_REGISTER_NOTIFICATION == avrc_response.rsp.pdu && + AVRC_EVT_VOLUME_CHANGE == avrc_response.reg_notif.event_id && + AVRC_RSP_CHANGED == pmeta_msg->code) { + /* re-register for volume change notification */ + // Do not re-register for rejected case, as it might get into endless loop + register_volumechange(p_dev->rc_vol_label, p_dev); + } else if (AVRC_PDU_SET_ABSOLUTE_VOLUME == avrc_response.rsp.pdu) { + /* free up the label here */ + release_transaction(pmeta_msg->label); + } - if (AVRC_PDU_REGISTER_NOTIFICATION == avrc_response.rsp.pdu - && AVRC_EVT_VOLUME_CHANGE == avrc_response.reg_notif.event_id - && AVRC_RSP_CHANGED == pmeta_msg->code) - { - /* re-register for volume change notification */ - // Do not re-register for rejected case, as it might get into endless loop - register_volumechange(p_dev->rc_vol_label, p_dev); - } - else if (AVRC_PDU_SET_ABSOLUTE_VOLUME == avrc_response.rsp.pdu) - { - /* free up the label here */ - release_transaction(pmeta_msg->label); - } - - BTIF_TRACE_EVENT("%s: Passing received metamsg response to app. pdu: %s", - __func__, dump_rc_pdu(avrc_response.pdu)); - btif_rc_upstreams_rsp_evt((uint16_t)avrc_response.rsp.pdu, &avrc_response, pmeta_msg->code, - pmeta_msg->label, p_dev); + BTIF_TRACE_EVENT("%s: Passing received metamsg response to app. pdu: %s", + __func__, dump_rc_pdu(avrc_response.pdu)); + btif_rc_upstreams_rsp_evt((uint16_t)avrc_response.rsp.pdu, &avrc_response, + pmeta_msg->code, pmeta_msg->label, p_dev); } #endif #if (AVRC_CTRL_INCLUDED == TRUE) /*************************************************************************** -** -** Function iterate_supported_event_list_for_interim_rsp -** -** Description iterator callback function to match the event and handle -** timer cleanup -** Returns true to continue iterating, false to stop -** -***************************************************************************/ -bool iterate_supported_event_list_for_interim_rsp(void *data, void *cb_data) -{ - uint8_t *p_event_id; - btif_rc_supported_event_t *p_event = (btif_rc_supported_event_t *)data; - - p_event_id = (uint8_t*)cb_data; - - if (p_event->event_id == *p_event_id) - { - p_event->status = eINTERIM; - return false; - } - return true; + * + * Function iterate_supported_event_list_for_interim_rsp + * + * Description iterator callback function to match the event and handle + * timer cleanup + * Returns true to continue iterating, false to stop + * + **************************************************************************/ +bool iterate_supported_event_list_for_interim_rsp(void* data, void* cb_data) { + uint8_t* p_event_id; + btif_rc_supported_event_t* p_event = (btif_rc_supported_event_t*)data; + + p_event_id = (uint8_t*)cb_data; + + if (p_event->event_id == *p_event_id) { + p_event->status = eINTERIM; + return false; + } + return true; } /*************************************************************************** -** -** Function iterate_supported_event_list_for_timeout -** -** Description Iterator callback function for timeout handling. -** As part of the failure handling, it releases the -** transaction label and removes the event from list, -** this event will not be requested again during -** the lifetime of the connection. -** Returns false to stop iterating, true to continue -** -***************************************************************************/ -bool iterate_supported_event_list_for_timeout(void *data, void *cb_data) -{ - bt_bdaddr_t bd_addr; - rc_context_t *cntxt = (rc_context_t*)cb_data; - uint8_t label = cntxt->label & 0xFF; - bdcpy(bd_addr.address, cntxt->rc_addr); - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(&bd_addr); - btif_rc_supported_event_t *p_event = (btif_rc_supported_event_t *)data; - - if (p_event->label == label) - { - list_remove(p_dev->rc_supported_event_list, p_event); - return false; - } - return true; + * + * Function iterate_supported_event_list_for_timeout + * + * Description Iterator callback function for timeout handling. + * As part of the failure handling, it releases the + * transaction label and removes the event from list, + * this event will not be requested again during + * the lifetime of the connection. + * Returns false to stop iterating, true to continue + * + **************************************************************************/ +bool iterate_supported_event_list_for_timeout(void* data, void* cb_data) { + bt_bdaddr_t bd_addr; + rc_context_t* cntxt = (rc_context_t*)cb_data; + uint8_t label = cntxt->label & 0xFF; + bdcpy(bd_addr.address, cntxt->rc_addr); + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(&bd_addr); + btif_rc_supported_event_t* p_event = (btif_rc_supported_event_t*)data; + + if (p_event->label == label) { + list_remove(p_dev->rc_supported_event_list, p_event); + return false; + } + return true; } /*************************************************************************** -** -** Function rc_notification_interim_timout -** -** Description Interim response timeout handler. -** Runs the iterator to check and clear the timed out event. -** Proceeds to register for the unregistered events. -** Returns None -** -***************************************************************************/ -static void rc_notification_interim_timout (uint8_t label, btif_rc_device_cb_t *p_dev) -{ - list_node_t *node; - rc_context_t cntxt; - memset(&cntxt, 0, sizeof(rc_context_t)); - cntxt.label = label; - bdcpy(cntxt.rc_addr, p_dev->rc_addr); - - list_foreach(p_dev->rc_supported_event_list, - iterate_supported_event_list_for_timeout, &cntxt); - /* Timeout happened for interim response for the registered event, - * check if there are any pending for registration - */ - node = list_begin(p_dev->rc_supported_event_list); - while (node != NULL) - { - btif_rc_supported_event_t *p_event; - - p_event = (btif_rc_supported_event_t *)list_node(node); - if ((p_event != NULL) && (p_event->status == eNOT_REGISTERED)) - { - register_for_event_notification(p_event, p_dev); - break; - } - node = list_next (node); - } - /* Todo. Need to initiate application settings query if this - * is the last event registration. - */ + * + * Function rc_notification_interim_timout + * + * Description Interim response timeout handler. + * Runs the iterator to check and clear the timed out event. + * Proceeds to register for the unregistered events. + * Returns None + * + **************************************************************************/ +static void rc_notification_interim_timout(uint8_t label, + btif_rc_device_cb_t* p_dev) { + list_node_t* node; + rc_context_t cntxt; + memset(&cntxt, 0, sizeof(rc_context_t)); + cntxt.label = label; + bdcpy(cntxt.rc_addr, p_dev->rc_addr); + + list_foreach(p_dev->rc_supported_event_list, + iterate_supported_event_list_for_timeout, &cntxt); + /* Timeout happened for interim response for the registered event, + * check if there are any pending for registration + */ + node = list_begin(p_dev->rc_supported_event_list); + while (node != NULL) { + btif_rc_supported_event_t* p_event; + + p_event = (btif_rc_supported_event_t*)list_node(node); + if ((p_event != NULL) && (p_event->status == eNOT_REGISTERED)) { + register_for_event_notification(p_event, p_dev); + break; + } + node = list_next(node); + } + /* Todo. Need to initiate application settings query if this + * is the last event registration. + */ } /*************************************************************************** -** -** Function btif_rc_status_cmd_timeout_handler -** -** Description RC status command timeout handler (Runs in BTIF context). -** Returns None -** -***************************************************************************/ + * + * Function btif_rc_status_cmd_timeout_handler + * + * Description RC status command timeout handler (Runs in BTIF context). + * Returns None + * + **************************************************************************/ static void btif_rc_status_cmd_timeout_handler(UNUSED_ATTR uint16_t event, - char *data) -{ - btif_rc_timer_context_t *p_context; - tAVRC_RESPONSE avrc_response = {0}; - tBTA_AV_META_MSG meta_msg; - btif_rc_device_cb_t *p_dev = NULL; - bt_bdaddr_t bd_addr; - - p_context = (btif_rc_timer_context_t *)data; - bdcpy(bd_addr.address, p_context->rc_addr); - memset(&meta_msg, 0, sizeof(tBTA_AV_META_MSG)); - p_dev = btif_rc_get_device_by_bda(&bd_addr); - if( p_dev == NULL ){ - BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); - return; - } - meta_msg.rc_handle = p_dev->rc_handle; - - switch (p_context->rc_status_cmd.pdu_id) { + char* data) { + btif_rc_timer_context_t* p_context; + tAVRC_RESPONSE avrc_response = {0}; + tBTA_AV_META_MSG meta_msg; + btif_rc_device_cb_t* p_dev = NULL; + bt_bdaddr_t bd_addr; + + p_context = (btif_rc_timer_context_t*)data; + bdcpy(bd_addr.address, p_context->rc_addr); + memset(&meta_msg, 0, sizeof(tBTA_AV_META_MSG)); + p_dev = btif_rc_get_device_by_bda(&bd_addr); + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); + return; + } + meta_msg.rc_handle = p_dev->rc_handle; + + switch (p_context->rc_status_cmd.pdu_id) { case AVRC_PDU_REGISTER_NOTIFICATION: - rc_notification_interim_timout(p_context->rc_status_cmd.label, p_dev); - break; + rc_notification_interim_timout(p_context->rc_status_cmd.label, p_dev); + break; case AVRC_PDU_GET_CAPABILITIES: - avrc_response.get_caps.status = BTIF_RC_STS_TIMEOUT; - handle_get_capability_response(&meta_msg, &avrc_response.get_caps); - break; + avrc_response.get_caps.status = BTIF_RC_STS_TIMEOUT; + handle_get_capability_response(&meta_msg, &avrc_response.get_caps); + break; case AVRC_PDU_LIST_PLAYER_APP_ATTR: - avrc_response.list_app_attr.status = BTIF_RC_STS_TIMEOUT; - handle_app_attr_response(&meta_msg, &avrc_response.list_app_attr); - break; + avrc_response.list_app_attr.status = BTIF_RC_STS_TIMEOUT; + handle_app_attr_response(&meta_msg, &avrc_response.list_app_attr); + break; case AVRC_PDU_LIST_PLAYER_APP_VALUES: - avrc_response.list_app_values.status = BTIF_RC_STS_TIMEOUT; - handle_app_val_response(&meta_msg, &avrc_response.list_app_values); - break; + avrc_response.list_app_values.status = BTIF_RC_STS_TIMEOUT; + handle_app_val_response(&meta_msg, &avrc_response.list_app_values); + break; case AVRC_PDU_GET_CUR_PLAYER_APP_VALUE: - avrc_response.get_cur_app_val.status = BTIF_RC_STS_TIMEOUT; - handle_app_cur_val_response(&meta_msg, &avrc_response.get_cur_app_val); - break; + avrc_response.get_cur_app_val.status = BTIF_RC_STS_TIMEOUT; + handle_app_cur_val_response(&meta_msg, &avrc_response.get_cur_app_val); + break; case AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT: - avrc_response.get_app_attr_txt.status = BTIF_RC_STS_TIMEOUT; - handle_app_attr_txt_response(&meta_msg, &avrc_response.get_app_attr_txt); - break; + avrc_response.get_app_attr_txt.status = BTIF_RC_STS_TIMEOUT; + handle_app_attr_txt_response(&meta_msg, &avrc_response.get_app_attr_txt); + break; case AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT: - avrc_response.get_app_val_txt.status = BTIF_RC_STS_TIMEOUT; - handle_app_attr_txt_response(&meta_msg, &avrc_response.get_app_val_txt); - break; + avrc_response.get_app_val_txt.status = BTIF_RC_STS_TIMEOUT; + handle_app_attr_txt_response(&meta_msg, &avrc_response.get_app_val_txt); + break; case AVRC_PDU_GET_ELEMENT_ATTR: - avrc_response.get_elem_attrs.status = BTIF_RC_STS_TIMEOUT; - handle_get_elem_attr_response(&meta_msg, &avrc_response.get_elem_attrs); - break; + avrc_response.get_elem_attrs.status = BTIF_RC_STS_TIMEOUT; + handle_get_elem_attr_response(&meta_msg, &avrc_response.get_elem_attrs); + break; case AVRC_PDU_GET_PLAY_STATUS: - avrc_response.get_play_status.status = BTIF_RC_STS_TIMEOUT; - handle_get_playstatus_response(&meta_msg, &avrc_response.get_play_status); - break; - } - release_transaction(p_context->rc_status_cmd.label); + avrc_response.get_play_status.status = BTIF_RC_STS_TIMEOUT; + handle_get_playstatus_response(&meta_msg, &avrc_response.get_play_status); + break; + } + release_transaction(p_context->rc_status_cmd.label); } /*************************************************************************** -** -** Function btif_rc_status_cmd_timer_timeout -** -** Description RC status command timeout callback. -** This is called from BTU context and switches to BTIF -** context to handle the timeout events -** Returns None -** -***************************************************************************/ -static void btif_rc_status_cmd_timer_timeout(void *data) -{ - btif_rc_timer_context_t *p_data = (btif_rc_timer_context_t *)data; - - btif_transfer_context(btif_rc_status_cmd_timeout_handler, 0, - (char *)p_data, sizeof(btif_rc_timer_context_t), - NULL); + * + * Function btif_rc_status_cmd_timer_timeout + * + * Description RC status command timeout callback. + * This is called from BTU context and switches to BTIF + * context to handle the timeout events + * Returns None + * + **************************************************************************/ +static void btif_rc_status_cmd_timer_timeout(void* data) { + btif_rc_timer_context_t* p_data = (btif_rc_timer_context_t*)data; + + btif_transfer_context(btif_rc_status_cmd_timeout_handler, 0, (char*)p_data, + sizeof(btif_rc_timer_context_t), NULL); } /*************************************************************************** -** -** Function btif_rc_control_cmd_timeout_handler -** -** Description RC control command timeout handler (Runs in BTIF context). -** Returns None -** -***************************************************************************/ + * + * Function btif_rc_control_cmd_timeout_handler + * + * Description RC control command timeout handler (Runs in BTIF context). + * Returns None + * + **************************************************************************/ static void btif_rc_control_cmd_timeout_handler(UNUSED_ATTR uint16_t event, - char *data) -{ - btif_rc_timer_context_t *p_context = (btif_rc_timer_context_t *)data; - tAVRC_RESPONSE avrc_response = {0}; - tBTA_AV_META_MSG meta_msg; - bt_bdaddr_t bd_addr; - bdcpy(bd_addr.address, p_context->rc_addr); - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(&bd_addr); - if( p_dev == NULL ){ - BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); - return; - } - - memset(&meta_msg, 0, sizeof(tBTA_AV_META_MSG)); - meta_msg.rc_handle = p_dev->rc_handle; - - switch (p_context->rc_control_cmd.pdu_id) { + char* data) { + btif_rc_timer_context_t* p_context = (btif_rc_timer_context_t*)data; + tAVRC_RESPONSE avrc_response = {0}; + tBTA_AV_META_MSG meta_msg; + bt_bdaddr_t bd_addr; + bdcpy(bd_addr.address, p_context->rc_addr); + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(&bd_addr); + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); + return; + } + + memset(&meta_msg, 0, sizeof(tBTA_AV_META_MSG)); + meta_msg.rc_handle = p_dev->rc_handle; + + switch (p_context->rc_control_cmd.pdu_id) { case AVRC_PDU_SET_PLAYER_APP_VALUE: - avrc_response.set_app_val.status = BTIF_RC_STS_TIMEOUT; - handle_set_app_attr_val_response(&meta_msg, - &avrc_response.set_app_val); - break; - } - release_transaction(p_context->rc_control_cmd.label); + avrc_response.set_app_val.status = BTIF_RC_STS_TIMEOUT; + handle_set_app_attr_val_response(&meta_msg, &avrc_response.set_app_val); + break; + } + release_transaction(p_context->rc_control_cmd.label); } /*************************************************************************** -** -** Function btif_rc_control_cmd_timer_timeout -** -** Description RC control command timeout callback. -** This is called from BTU context and switches to BTIF -** context to handle the timeout events -** Returns None -** -***************************************************************************/ -static void btif_rc_control_cmd_timer_timeout(void *data) -{ - btif_rc_timer_context_t *p_data = (btif_rc_timer_context_t *)data; - - btif_transfer_context(btif_rc_control_cmd_timeout_handler, 0, - (char *)p_data, sizeof(btif_rc_timer_context_t), - NULL); + * + * Function btif_rc_control_cmd_timer_timeout + * + * Description RC control command timeout callback. + * This is called from BTU context and switches to BTIF + * context to handle the timeout events + * Returns None + * + **************************************************************************/ +static void btif_rc_control_cmd_timer_timeout(void* data) { + btif_rc_timer_context_t* p_data = (btif_rc_timer_context_t*)data; + + btif_transfer_context(btif_rc_control_cmd_timeout_handler, 0, (char*)p_data, + sizeof(btif_rc_timer_context_t), NULL); } /*************************************************************************** -** -** Function btif_rc_play_status_timeout_handler -** -** Description RC play status timeout handler (Runs in BTIF context). -** Returns None -** -***************************************************************************/ + * + * Function btif_rc_play_status_timeout_handler + * + * Description RC play status timeout handler (Runs in BTIF context). + * Returns None + * + **************************************************************************/ static void btif_rc_play_status_timeout_handler(UNUSED_ATTR uint16_t event, - char *p_data) -{ - btif_rc_device_cb_t *p_dev = (btif_rc_device_cb_t*)p_data; - get_play_status_cmd(p_dev); - rc_start_play_status_timer(p_dev); + char* p_data) { + btif_rc_device_cb_t* p_dev = (btif_rc_device_cb_t*)p_data; + get_play_status_cmd(p_dev); + rc_start_play_status_timer(p_dev); } /*************************************************************************** -** -** Function btif_rc_play_status_timer_timeout -** -** Description RC play status timeout callback. -** This is called from BTU context and switches to BTIF -** context to handle the timeout events -** Returns None -** -***************************************************************************/ -static void btif_rc_play_status_timer_timeout(void *data) -{ - btif_rc_device_cb_t *p_dev = (btif_rc_device_cb_t*)data; - btif_transfer_context(btif_rc_play_status_timeout_handler, 0, (char*)p_dev, 0, NULL); + * + * Function btif_rc_play_status_timer_timeout + * + * Description RC play status timeout callback. + * This is called from BTU context and switches to BTIF + * context to handle the timeout events + * Returns None + * + **************************************************************************/ +static void btif_rc_play_status_timer_timeout(void* data) { + btif_rc_device_cb_t* p_dev = (btif_rc_device_cb_t*)data; + btif_transfer_context(btif_rc_play_status_timeout_handler, 0, (char*)p_dev, 0, + NULL); } /*************************************************************************** -** -** Function rc_start_play_status_timer -** -** Description Helper function to start the timer to fetch play status. -** Returns None -** -***************************************************************************/ -static void rc_start_play_status_timer(btif_rc_device_cb_t *p_dev) -{ - /* Start the Play status timer only if it is not started */ - if (!alarm_is_scheduled(p_dev->rc_play_status_timer)) { - if (p_dev->rc_play_status_timer == NULL) { - p_dev->rc_play_status_timer = - alarm_new("p_dev->rc_play_status_timer"); - } - alarm_set_on_queue(p_dev->rc_play_status_timer, - BTIF_TIMEOUT_RC_INTERIM_RSP_MS, - btif_rc_play_status_timer_timeout, p_dev, - btu_general_alarm_queue); - } + * + * Function rc_start_play_status_timer + * + * Description Helper function to start the timer to fetch play status. + * Returns None + * + **************************************************************************/ +static void rc_start_play_status_timer(btif_rc_device_cb_t* p_dev) { + /* Start the Play status timer only if it is not started */ + if (!alarm_is_scheduled(p_dev->rc_play_status_timer)) { + if (p_dev->rc_play_status_timer == NULL) { + p_dev->rc_play_status_timer = alarm_new("p_dev->rc_play_status_timer"); + } + alarm_set_on_queue( + p_dev->rc_play_status_timer, BTIF_TIMEOUT_RC_INTERIM_RSP_MS, + btif_rc_play_status_timer_timeout, p_dev, btu_general_alarm_queue); + } } /*************************************************************************** -** -** Function rc_stop_play_status_timer -** -** Description Helper function to stop the play status timer. -** Returns None -** -***************************************************************************/ -void rc_stop_play_status_timer(btif_rc_device_cb_t *p_dev) -{ - alarm_cancel(p_dev->rc_play_status_timer); + * + * Function rc_stop_play_status_timer + * + * Description Helper function to stop the play status timer. + * Returns None + * + **************************************************************************/ +void rc_stop_play_status_timer(btif_rc_device_cb_t* p_dev) { + alarm_cancel(p_dev->rc_play_status_timer); } /*************************************************************************** -** -** Function register_for_event_notification -** -** Description Helper function registering notification events -** sets an interim response timeout to handle if the remote -** does not respond. -** Returns None -** -***************************************************************************/ -static void register_for_event_notification(btif_rc_supported_event_t *p_event, - btif_rc_device_cb_t *p_dev) -{ - bt_status_t status; - rc_transaction_t *p_transaction; - - status = get_transaction(&p_transaction); - if (status == BT_STATUS_SUCCESS) - { - btif_rc_timer_context_t *p_context = &p_transaction->txn_timer_context; - - status = register_notification_cmd (p_transaction->lbl, p_event->event_id, 0, p_dev); - if (status != BT_STATUS_SUCCESS) - { - BTIF_TRACE_ERROR("%s: Error in Notification registration: %d", - __func__, status); - release_transaction (p_transaction->lbl); - return; - } - p_event->label = p_transaction->lbl; - p_event->status = eREGISTERED; - p_context->rc_status_cmd.label = p_transaction->lbl; - p_context->rc_status_cmd.pdu_id = AVRC_PDU_REGISTER_NOTIFICATION; - bdcpy(p_context->rc_addr, p_dev->rc_addr); - - alarm_free(p_transaction->txn_timer); - p_transaction->txn_timer = - alarm_new("btif_rc.status_command_txn_timer"); - alarm_set_on_queue(p_transaction->txn_timer, - BTIF_TIMEOUT_RC_INTERIM_RSP_MS, - btif_rc_status_cmd_timer_timeout, p_context, - btu_general_alarm_queue); - } - else - { - BTIF_TRACE_ERROR("%s: Error No more Transaction label: %d", - __func__, status); - } -} - -static void start_status_command_timer(uint8_t pdu_id, rc_transaction_t *p_txn, - btif_rc_device_cb_t *p_dev) -{ - btif_rc_timer_context_t *p_context = &p_txn->txn_timer_context; - p_context->rc_status_cmd.label = p_txn->lbl; - p_context->rc_status_cmd.pdu_id = pdu_id; + * + * Function register_for_event_notification + * + * Description Helper function registering notification events + * sets an interim response timeout to handle if the remote + * does not respond. + * Returns None + * + **************************************************************************/ +static void register_for_event_notification(btif_rc_supported_event_t* p_event, + btif_rc_device_cb_t* p_dev) { + bt_status_t status; + rc_transaction_t* p_transaction; + + status = get_transaction(&p_transaction); + if (status == BT_STATUS_SUCCESS) { + btif_rc_timer_context_t* p_context = &p_transaction->txn_timer_context; + + status = register_notification_cmd(p_transaction->lbl, p_event->event_id, 0, + p_dev); + if (status != BT_STATUS_SUCCESS) { + BTIF_TRACE_ERROR("%s: Error in Notification registration: %d", __func__, + status); + release_transaction(p_transaction->lbl); + return; + } + p_event->label = p_transaction->lbl; + p_event->status = eREGISTERED; + p_context->rc_status_cmd.label = p_transaction->lbl; + p_context->rc_status_cmd.pdu_id = AVRC_PDU_REGISTER_NOTIFICATION; bdcpy(p_context->rc_addr, p_dev->rc_addr); - alarm_free(p_txn->txn_timer); - p_txn->txn_timer = alarm_new("btif_rc.status_command_txn_timer"); - alarm_set_on_queue(p_txn->txn_timer, BTIF_TIMEOUT_RC_STATUS_CMD_MS, + alarm_free(p_transaction->txn_timer); + p_transaction->txn_timer = alarm_new("btif_rc.status_command_txn_timer"); + alarm_set_on_queue(p_transaction->txn_timer, BTIF_TIMEOUT_RC_INTERIM_RSP_MS, btif_rc_status_cmd_timer_timeout, p_context, btu_general_alarm_queue); + } else { + BTIF_TRACE_ERROR("%s: Error No more Transaction label: %d", __func__, + status); + } } -static void start_control_command_timer(uint8_t pdu_id, rc_transaction_t *p_txn, - btif_rc_device_cb_t *p_dev) -{ - btif_rc_timer_context_t *p_context = &p_txn->txn_timer_context; - p_context->rc_control_cmd.label = p_txn->lbl; - p_context->rc_control_cmd.pdu_id = pdu_id; - bdcpy(p_context->rc_addr, p_dev->rc_addr); +static void start_status_command_timer(uint8_t pdu_id, rc_transaction_t* p_txn, + btif_rc_device_cb_t* p_dev) { + btif_rc_timer_context_t* p_context = &p_txn->txn_timer_context; + p_context->rc_status_cmd.label = p_txn->lbl; + p_context->rc_status_cmd.pdu_id = pdu_id; + bdcpy(p_context->rc_addr, p_dev->rc_addr); + + alarm_free(p_txn->txn_timer); + p_txn->txn_timer = alarm_new("btif_rc.status_command_txn_timer"); + alarm_set_on_queue(p_txn->txn_timer, BTIF_TIMEOUT_RC_STATUS_CMD_MS, + btif_rc_status_cmd_timer_timeout, p_context, + btu_general_alarm_queue); +} - alarm_free(p_txn->txn_timer); - p_txn->txn_timer = alarm_new("btif_rc.control_command_txn_timer"); - alarm_set_on_queue(p_txn->txn_timer, - BTIF_TIMEOUT_RC_CONTROL_CMD_MS, - btif_rc_control_cmd_timer_timeout, p_context, - btu_general_alarm_queue); +static void start_control_command_timer(uint8_t pdu_id, rc_transaction_t* p_txn, + btif_rc_device_cb_t* p_dev) { + btif_rc_timer_context_t* p_context = &p_txn->txn_timer_context; + p_context->rc_control_cmd.label = p_txn->lbl; + p_context->rc_control_cmd.pdu_id = pdu_id; + bdcpy(p_context->rc_addr, p_dev->rc_addr); + + alarm_free(p_txn->txn_timer); + p_txn->txn_timer = alarm_new("btif_rc.control_command_txn_timer"); + alarm_set_on_queue(p_txn->txn_timer, BTIF_TIMEOUT_RC_CONTROL_CMD_MS, + btif_rc_control_cmd_timer_timeout, p_context, + btu_general_alarm_queue); } /*************************************************************************** -** -** Function handle_get_capability_response -** -** Description Handles the get_cap_response to populate company id info -** and query the supported events. -** Initiates Notification registration for events supported -** Returns None -** -***************************************************************************/ -static void handle_get_capability_response (tBTA_AV_META_MSG *pmeta_msg, tAVRC_GET_CAPS_RSP *p_rsp) -{ - int xx = 0; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle); - - /* Todo: Do we need to retry on command timeout */ - if (p_rsp->status != AVRC_STS_NO_ERROR) - { - BTIF_TRACE_ERROR("%s: Error capability response: 0x%02X", - __func__, p_rsp->status); - return; + * + * Function handle_get_capability_response + * + * Description Handles the get_cap_response to populate company id info + * and query the supported events. + * Initiates Notification registration for events supported + * Returns None + * + **************************************************************************/ +static void handle_get_capability_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_GET_CAPS_RSP* p_rsp) { + int xx = 0; + btif_rc_device_cb_t* p_dev = + btif_rc_get_device_by_handle(pmeta_msg->rc_handle); + + /* Todo: Do we need to retry on command timeout */ + if (p_rsp->status != AVRC_STS_NO_ERROR) { + BTIF_TRACE_ERROR("%s: Error capability response: 0x%02X", __func__, + p_rsp->status); + return; + } + + if (p_rsp->capability_id == AVRC_CAP_EVENTS_SUPPORTED) { + btif_rc_supported_event_t* p_event; + + /* Todo: Check if list can be active when we hit here */ + p_dev->rc_supported_event_list = list_new(osi_free); + for (xx = 0; xx < p_rsp->count; xx++) { + /* Skip registering for Play position change notification */ + if ((p_rsp->param.event_id[xx] == AVRC_EVT_PLAY_STATUS_CHANGE) || + (p_rsp->param.event_id[xx] == AVRC_EVT_TRACK_CHANGE) || + (p_rsp->param.event_id[xx] == AVRC_EVT_APP_SETTING_CHANGE)) { + p_event = (btif_rc_supported_event_t*)osi_malloc( + sizeof(btif_rc_supported_event_t)); + p_event->event_id = p_rsp->param.event_id[xx]; + p_event->status = eNOT_REGISTERED; + list_append(p_dev->rc_supported_event_list, p_event); + } } - - if (p_rsp->capability_id == AVRC_CAP_EVENTS_SUPPORTED) - { - btif_rc_supported_event_t *p_event; - - /* Todo: Check if list can be active when we hit here */ - p_dev->rc_supported_event_list = list_new(osi_free); - for (xx = 0; xx < p_rsp->count; xx++) - { - /* Skip registering for Play position change notification */ - if ((p_rsp->param.event_id[xx] == AVRC_EVT_PLAY_STATUS_CHANGE)|| - (p_rsp->param.event_id[xx] == AVRC_EVT_TRACK_CHANGE)|| - (p_rsp->param.event_id[xx] == AVRC_EVT_APP_SETTING_CHANGE)) - { - p_event = (btif_rc_supported_event_t *)osi_malloc(sizeof(btif_rc_supported_event_t)); - p_event->event_id = p_rsp->param.event_id[xx]; - p_event->status = eNOT_REGISTERED; - list_append(p_dev->rc_supported_event_list, p_event); - } - } - p_event = (btif_rc_supported_event_t*)list_front(p_dev->rc_supported_event_list); - if (p_event != NULL) - { - register_for_event_notification(p_event, p_dev); - } + p_event = + (btif_rc_supported_event_t*)list_front(p_dev->rc_supported_event_list); + if (p_event != NULL) { + register_for_event_notification(p_event, p_dev); } - else if (p_rsp->capability_id == AVRC_CAP_COMPANY_ID) - { - getcapabilities_cmd(AVRC_CAP_EVENTS_SUPPORTED, p_dev); - BTIF_TRACE_EVENT("%s: AVRC_CAP_COMPANY_ID: ", __func__); - for (xx = 0; xx < p_rsp->count; xx++) - { - BTIF_TRACE_EVENT("%s: company_id: %d", __func__, p_rsp->param.company_id[xx]); - } + } else if (p_rsp->capability_id == AVRC_CAP_COMPANY_ID) { + getcapabilities_cmd(AVRC_CAP_EVENTS_SUPPORTED, p_dev); + BTIF_TRACE_EVENT("%s: AVRC_CAP_COMPANY_ID: ", __func__); + for (xx = 0; xx < p_rsp->count; xx++) { + BTIF_TRACE_EVENT("%s: company_id: %d", __func__, + p_rsp->param.company_id[xx]); } + } } -bool rc_is_track_id_valid (tAVRC_UID uid) -{ - tAVRC_UID invalid_uid = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; +bool rc_is_track_id_valid(tAVRC_UID uid) { + tAVRC_UID invalid_uid = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; - if (memcmp(uid, invalid_uid, sizeof(tAVRC_UID)) == 0) - { - return false; - } - else - { - return true; - } + if (memcmp(uid, invalid_uid, sizeof(tAVRC_UID)) == 0) { + return false; + } else { + return true; + } } /*************************************************************************** -** -** Function handle_notification_response -** -** Description Main handler for notification responses to registered events -** 1. Register for unregistered event(in interim response path) -** 2. After registering for all supported events, start -** retrieving application settings and values -** 3. Reregister for events on getting changed response -** 4. Run play status timer for getting position when the -** status changes to playing -** 5. Get the Media details when the track change happens -** or track change interim response is received with -** valid track id -** 6. HAL callback for play status change and application -** setting change -** Returns None -** -***************************************************************************/ -static void handle_notification_response (tBTA_AV_META_MSG *pmeta_msg, tAVRC_REG_NOTIF_RSP *p_rsp) -{ - bt_bdaddr_t rc_addr; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle); - uint32_t attr_list[] = { - AVRC_MEDIA_ATTR_ID_TITLE, - AVRC_MEDIA_ATTR_ID_ARTIST, - AVRC_MEDIA_ATTR_ID_ALBUM, - AVRC_MEDIA_ATTR_ID_TRACK_NUM, - AVRC_MEDIA_ATTR_ID_NUM_TRACKS, - AVRC_MEDIA_ATTR_ID_GENRE, - AVRC_MEDIA_ATTR_ID_PLAYING_TIME - }; - - if (p_dev == NULL) - { - BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); - return; - } - - bdcpy(rc_addr.address, p_dev->rc_addr); - - if (pmeta_msg->code == AVRC_RSP_INTERIM) - { - btif_rc_supported_event_t *p_event; - list_node_t *node; - - BTIF_TRACE_DEBUG("%s: Interim response: 0x%2X ", __func__, p_rsp->event_id); - switch (p_rsp->event_id) - { - case AVRC_EVT_PLAY_STATUS_CHANGE: - /* Start timer to get play status periodically - * if the play state is playing. - */ - if (p_rsp->param.play_status == AVRC_PLAYSTATE_PLAYING) - { - rc_start_play_status_timer(p_dev); - } - HAL_CBACK(bt_rc_ctrl_callbacks, play_status_changed_cb, - &rc_addr, (btrc_play_status_t)p_rsp->param.play_status); - break; + * + * Function handle_notification_response + * + * Description Main handler for notification responses to registered events + * 1. Register for unregistered event(in interim response path) + * 2. After registering for all supported events, start + * retrieving application settings and values + * 3. Reregister for events on getting changed response + * 4. Run play status timer for getting position when the + * status changes to playing + * 5. Get the Media details when the track change happens + * or track change interim response is received with + * valid track id + * 6. HAL callback for play status change and application + * setting change + * Returns None + * + **************************************************************************/ +static void handle_notification_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_REG_NOTIF_RSP* p_rsp) { + bt_bdaddr_t rc_addr; + btif_rc_device_cb_t* p_dev = + btif_rc_get_device_by_handle(pmeta_msg->rc_handle); + uint32_t attr_list[] = { + AVRC_MEDIA_ATTR_ID_TITLE, AVRC_MEDIA_ATTR_ID_ARTIST, + AVRC_MEDIA_ATTR_ID_ALBUM, AVRC_MEDIA_ATTR_ID_TRACK_NUM, + AVRC_MEDIA_ATTR_ID_NUM_TRACKS, AVRC_MEDIA_ATTR_ID_GENRE, + AVRC_MEDIA_ATTR_ID_PLAYING_TIME}; + + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); + return; + } + + bdcpy(rc_addr.address, p_dev->rc_addr); + + if (pmeta_msg->code == AVRC_RSP_INTERIM) { + btif_rc_supported_event_t* p_event; + list_node_t* node; + + BTIF_TRACE_DEBUG("%s: Interim response: 0x%2X ", __func__, p_rsp->event_id); + switch (p_rsp->event_id) { + case AVRC_EVT_PLAY_STATUS_CHANGE: + /* Start timer to get play status periodically + * if the play state is playing. + */ + if (p_rsp->param.play_status == AVRC_PLAYSTATE_PLAYING) { + rc_start_play_status_timer(p_dev); + } + HAL_CBACK(bt_rc_ctrl_callbacks, play_status_changed_cb, &rc_addr, + (btrc_play_status_t)p_rsp->param.play_status); + break; - case AVRC_EVT_TRACK_CHANGE: - if (rc_is_track_id_valid (p_rsp->param.track) != true) - { - break; - } - else - { - uint8_t *p_data = p_rsp->param.track; - /* Update the UID for current track - * Attributes will be fetched after the AVRCP procedure - */ - BE_STREAM_TO_UINT64(p_dev->rc_playing_uid, p_data); - } - break; + case AVRC_EVT_TRACK_CHANGE: + if (rc_is_track_id_valid(p_rsp->param.track) != true) { + break; + } else { + uint8_t* p_data = p_rsp->param.track; + /* Update the UID for current track + * Attributes will be fetched after the AVRCP procedure + */ + BE_STREAM_TO_UINT64(p_dev->rc_playing_uid, p_data); + } + break; - case AVRC_EVT_APP_SETTING_CHANGE: - break; + case AVRC_EVT_APP_SETTING_CHANGE: + break; - case AVRC_EVT_NOW_PLAYING_CHANGE: - break; + case AVRC_EVT_NOW_PLAYING_CHANGE: + break; - case AVRC_EVT_AVAL_PLAYERS_CHANGE: - break; + case AVRC_EVT_AVAL_PLAYERS_CHANGE: + break; - case AVRC_EVT_ADDR_PLAYER_CHANGE: - break; + case AVRC_EVT_ADDR_PLAYER_CHANGE: + break; - case AVRC_EVT_UIDS_CHANGE: - break; + case AVRC_EVT_UIDS_CHANGE: + break; - case AVRC_EVT_TRACK_REACHED_END: - case AVRC_EVT_TRACK_REACHED_START: - case AVRC_EVT_PLAY_POS_CHANGED: - case AVRC_EVT_BATTERY_STATUS_CHANGE: - case AVRC_EVT_SYSTEM_STATUS_CHANGE: - default: - BTIF_TRACE_ERROR("%s: Unhandled interim response: 0x%2X", __func__, - p_rsp->event_id); - return; - } + case AVRC_EVT_TRACK_REACHED_END: + case AVRC_EVT_TRACK_REACHED_START: + case AVRC_EVT_PLAY_POS_CHANGED: + case AVRC_EVT_BATTERY_STATUS_CHANGE: + case AVRC_EVT_SYSTEM_STATUS_CHANGE: + default: + BTIF_TRACE_ERROR("%s: Unhandled interim response: 0x%2X", __func__, + p_rsp->event_id); + return; + } - list_foreach(p_dev->rc_supported_event_list, - iterate_supported_event_list_for_interim_rsp, - &p_rsp->event_id); + list_foreach(p_dev->rc_supported_event_list, + iterate_supported_event_list_for_interim_rsp, + &p_rsp->event_id); - node = list_begin(p_dev->rc_supported_event_list); + node = list_begin(p_dev->rc_supported_event_list); - while (node != NULL) - { - p_event = (btif_rc_supported_event_t *)list_node(node); - if ((p_event != NULL) && (p_event->status == eNOT_REGISTERED)) - { - register_for_event_notification(p_event, p_dev); - break; - } - node = list_next (node); - p_event = NULL; - } - /* Registered for all events, we can request application settings */ - if (p_event == NULL && p_dev->rc_app_settings.query_started == false) - { - /* we need to do this only if remote TG supports - * player application settings - */ - p_dev->rc_app_settings.query_started = true; - if (p_dev->rc_features & BTA_AV_FEAT_APP_SETTING) - { - list_player_app_setting_attrib_cmd(p_dev); - } - else - { - BTIF_TRACE_DEBUG("%s: App setting not supported, complete procedure", __func__); - rc_ctrl_procedure_complete(p_dev); - } - } + while (node != NULL) { + p_event = (btif_rc_supported_event_t*)list_node(node); + if ((p_event != NULL) && (p_event->status == eNOT_REGISTERED)) { + register_for_event_notification(p_event, p_dev); + break; + } + node = list_next(node); + p_event = NULL; + } + /* Registered for all events, we can request application settings */ + if (p_event == NULL && p_dev->rc_app_settings.query_started == false) { + /* we need to do this only if remote TG supports + * player application settings + */ + p_dev->rc_app_settings.query_started = true; + if (p_dev->rc_features & BTA_AV_FEAT_APP_SETTING) { + list_player_app_setting_attrib_cmd(p_dev); + } else { + BTIF_TRACE_DEBUG("%s: App setting not supported, complete procedure", + __func__); + rc_ctrl_procedure_complete(p_dev); + } } - else if (pmeta_msg->code == AVRC_RSP_CHANGED) - { - btif_rc_supported_event_t *p_event; - list_node_t *node; + } else if (pmeta_msg->code == AVRC_RSP_CHANGED) { + btif_rc_supported_event_t* p_event; + list_node_t* node; - BTIF_TRACE_DEBUG("%s: Notification completed: 0x%2X ", __func__, - p_rsp->event_id); + BTIF_TRACE_DEBUG("%s: Notification completed: 0x%2X ", __func__, + p_rsp->event_id); - node = list_begin(p_dev->rc_supported_event_list); + node = list_begin(p_dev->rc_supported_event_list); - while (node != NULL) - { - p_event = (btif_rc_supported_event_t *)list_node(node); - if (p_event != NULL && p_event->event_id == p_rsp->event_id) - { - p_event->status = eNOT_REGISTERED; - register_for_event_notification(p_event, p_dev); - break; - } - node = list_next (node); + while (node != NULL) { + p_event = (btif_rc_supported_event_t*)list_node(node); + if (p_event != NULL && p_event->event_id == p_rsp->event_id) { + p_event->status = eNOT_REGISTERED; + register_for_event_notification(p_event, p_dev); + break; + } + node = list_next(node); + } + + switch (p_rsp->event_id) { + case AVRC_EVT_PLAY_STATUS_CHANGE: + /* Start timer to get play status periodically + * if the play state is playing. + */ + if (p_rsp->param.play_status == AVRC_PLAYSTATE_PLAYING) { + rc_start_play_status_timer(p_dev); + } else { + rc_stop_play_status_timer(p_dev); } + HAL_CBACK(bt_rc_ctrl_callbacks, play_status_changed_cb, &rc_addr, + (btrc_play_status_t)p_rsp->param.play_status); - switch (p_rsp->event_id) - { - case AVRC_EVT_PLAY_STATUS_CHANGE: - /* Start timer to get play status periodically - * if the play state is playing. - */ - if (p_rsp->param.play_status == AVRC_PLAYSTATE_PLAYING) - { - rc_start_play_status_timer(p_dev); - } - else - { - rc_stop_play_status_timer(p_dev); - } - HAL_CBACK(bt_rc_ctrl_callbacks, play_status_changed_cb, - &rc_addr, (btrc_play_status_t)p_rsp->param.play_status); + break; - break; + case AVRC_EVT_TRACK_CHANGE: + if (rc_is_track_id_valid(p_rsp->param.track) != true) { + break; + } + get_element_attribute_cmd(AVRC_MAX_NUM_MEDIA_ATTR_ID, attr_list, p_dev); + break; - case AVRC_EVT_TRACK_CHANGE: - if (rc_is_track_id_valid (p_rsp->param.track) != true) - { - break; - } - get_element_attribute_cmd(AVRC_MAX_NUM_MEDIA_ATTR_ID, attr_list, p_dev); - break; + case AVRC_EVT_APP_SETTING_CHANGE: { + btrc_player_settings_t app_settings; + uint16_t xx; - case AVRC_EVT_APP_SETTING_CHANGE: - { - btrc_player_settings_t app_settings; - uint16_t xx; - - app_settings.num_attr = p_rsp->param.player_setting.num_attr; - for (xx = 0; xx < app_settings.num_attr; xx++) - { - app_settings.attr_ids[xx] = p_rsp->param.player_setting.attr_id[xx]; - app_settings.attr_values[xx] = p_rsp->param.player_setting.attr_value[xx]; - } - HAL_CBACK(bt_rc_ctrl_callbacks, playerapplicationsetting_changed_cb, - &rc_addr, &app_settings); - } - break; - - case AVRC_EVT_NOW_PLAYING_CHANGE: - break; + app_settings.num_attr = p_rsp->param.player_setting.num_attr; + for (xx = 0; xx < app_settings.num_attr; xx++) { + app_settings.attr_ids[xx] = p_rsp->param.player_setting.attr_id[xx]; + app_settings.attr_values[xx] = + p_rsp->param.player_setting.attr_value[xx]; + } + HAL_CBACK(bt_rc_ctrl_callbacks, playerapplicationsetting_changed_cb, + &rc_addr, &app_settings); + } break; - case AVRC_EVT_AVAL_PLAYERS_CHANGE: - break; + case AVRC_EVT_NOW_PLAYING_CHANGE: + break; - case AVRC_EVT_ADDR_PLAYER_CHANGE: - break; + case AVRC_EVT_AVAL_PLAYERS_CHANGE: + break; - case AVRC_EVT_UIDS_CHANGE: - break; + case AVRC_EVT_ADDR_PLAYER_CHANGE: + break; - case AVRC_EVT_TRACK_REACHED_END: - case AVRC_EVT_TRACK_REACHED_START: - case AVRC_EVT_PLAY_POS_CHANGED: - case AVRC_EVT_BATTERY_STATUS_CHANGE: - case AVRC_EVT_SYSTEM_STATUS_CHANGE: - default: - BTIF_TRACE_ERROR("%s: Unhandled completion response: 0x%2X", - __func__, p_rsp->event_id); - return; - } - } -} + case AVRC_EVT_UIDS_CHANGE: + break; -/*************************************************************************** -** -** Function handle_app_attr_response -** -** Description handles the the application attributes response and -** initiates procedure to fetch the attribute values -** Returns None -** -***************************************************************************/ -static void handle_app_attr_response (tBTA_AV_META_MSG *pmeta_msg, tAVRC_LIST_APP_ATTR_RSP *p_rsp) -{ - uint8_t xx; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle); - - if (p_dev == NULL || p_rsp->status != AVRC_STS_NO_ERROR) - { - BTIF_TRACE_ERROR("%s: Error getting Player application settings: 0x%2X", - __func__, p_rsp->status); - rc_ctrl_procedure_complete(p_dev); + case AVRC_EVT_TRACK_REACHED_END: + case AVRC_EVT_TRACK_REACHED_START: + case AVRC_EVT_PLAY_POS_CHANGED: + case AVRC_EVT_BATTERY_STATUS_CHANGE: + case AVRC_EVT_SYSTEM_STATUS_CHANGE: + default: + BTIF_TRACE_ERROR("%s: Unhandled completion response: 0x%2X", __func__, + p_rsp->event_id); return; } - - for (xx = 0; xx < p_rsp->num_attr; xx++) - { - uint8_t st_index; - - if (p_rsp->attrs[xx] > AVRC_PLAYER_SETTING_LOW_MENU_EXT) - { - st_index = p_dev->rc_app_settings.num_ext_attrs; - p_dev->rc_app_settings.ext_attrs[st_index].attr_id = p_rsp->attrs[xx]; - p_dev->rc_app_settings.num_ext_attrs++; - } - else - { - st_index = p_dev->rc_app_settings.num_attrs; - p_dev->rc_app_settings.attrs[st_index].attr_id = p_rsp->attrs[xx]; - p_dev->rc_app_settings.num_attrs++; - } - } - p_dev->rc_app_settings.attr_index = 0; - p_dev->rc_app_settings.ext_attr_index = 0; - p_dev->rc_app_settings.ext_val_index = 0; - if (p_rsp->num_attr) - { - list_player_app_setting_value_cmd (p_dev->rc_app_settings.attrs[0].attr_id, p_dev); - } - else - { - BTIF_TRACE_ERROR("%s: No Player application settings found", - __func__); - } + } } /*************************************************************************** -** -** Function handle_app_val_response -** -** Description handles the the attributes value response and if extended -** menu is available, it initiates query for the attribute -** text. If not, it initiates procedure to get the current -** attribute values and calls the HAL callback for provding -** application settings information. -** Returns None -** -***************************************************************************/ -static void handle_app_val_response (tBTA_AV_META_MSG *pmeta_msg, tAVRC_LIST_APP_VALUES_RSP *p_rsp) -{ - uint8_t xx, attr_index; - uint8_t attrs[AVRC_MAX_APP_ATTR_SIZE]; - btif_rc_player_app_settings_t *p_app_settings; - bt_bdaddr_t rc_addr; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle); - - /* Todo: Do we need to retry on command timeout */ - if (p_dev == NULL || p_rsp->status != AVRC_STS_NO_ERROR) - { - BTIF_TRACE_ERROR("%s: Error fetching attribute values: 0x%02X", - __func__, p_rsp->status); - return; - } + * + * Function handle_app_attr_response + * + * Description handles the the application attributes response and + * initiates procedure to fetch the attribute values + * Returns None + * + **************************************************************************/ +static void handle_app_attr_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_LIST_APP_ATTR_RSP* p_rsp) { + uint8_t xx; + btif_rc_device_cb_t* p_dev = + btif_rc_get_device_by_handle(pmeta_msg->rc_handle); + + if (p_dev == NULL || p_rsp->status != AVRC_STS_NO_ERROR) { + BTIF_TRACE_ERROR("%s: Error getting Player application settings: 0x%2X", + __func__, p_rsp->status); + rc_ctrl_procedure_complete(p_dev); + return; + } - p_app_settings = &p_dev->rc_app_settings; - bdcpy(rc_addr.address, p_dev->rc_addr); + for (xx = 0; xx < p_rsp->num_attr; xx++) { + uint8_t st_index; - if (p_app_settings->attr_index < p_app_settings->num_attrs) - { - attr_index = p_app_settings->attr_index; - p_app_settings->attrs[attr_index].num_val = p_rsp->num_val; - for (xx = 0; xx < p_rsp->num_val; xx++) - { - p_app_settings->attrs[attr_index].attr_val[xx] = p_rsp->vals[xx]; - } - attr_index++; - p_app_settings->attr_index++; - if (attr_index < p_app_settings->num_attrs) - { - list_player_app_setting_value_cmd( - p_app_settings->attrs[p_app_settings->attr_index].attr_id, - p_dev); - } - else if (p_app_settings->ext_attr_index < p_app_settings->num_ext_attrs) - { - attr_index = 0; - p_app_settings->ext_attr_index = 0; - list_player_app_setting_value_cmd( - p_app_settings->ext_attrs[attr_index].attr_id, - p_dev); - } - else - { - for (xx = 0; xx < p_app_settings->num_attrs; xx++) - { - attrs[xx] = p_app_settings->attrs[xx].attr_id; - } - get_player_app_setting_cmd (p_app_settings->num_attrs, attrs, p_dev); - HAL_CBACK (bt_rc_ctrl_callbacks, playerapplicationsetting_cb, &rc_addr, - p_app_settings->num_attrs, p_app_settings->attrs, 0, NULL); - } - } - else if (p_app_settings->ext_attr_index < p_app_settings->num_ext_attrs) - { - attr_index = p_app_settings->ext_attr_index; - p_app_settings->ext_attrs[attr_index].num_val = p_rsp->num_val; - for (xx = 0; xx < p_rsp->num_val; xx++) - { - p_app_settings->ext_attrs[attr_index].ext_attr_val[xx].val = p_rsp->vals[xx]; - } - attr_index++; - p_app_settings->ext_attr_index++; - if (attr_index < p_app_settings->num_ext_attrs) - { - list_player_app_setting_value_cmd( - p_app_settings->ext_attrs[p_app_settings->ext_attr_index].attr_id, p_dev); - } - else - { - uint8_t attr[AVRC_MAX_APP_ATTR_SIZE]; - - for (uint8_t xx = 0; xx < p_app_settings->num_ext_attrs; xx++) - { - attr[xx] = p_app_settings->ext_attrs[xx].attr_id; - } - get_player_app_setting_attr_text_cmd(attr, p_app_settings->num_ext_attrs, p_dev); - } - } + if (p_rsp->attrs[xx] > AVRC_PLAYER_SETTING_LOW_MENU_EXT) { + st_index = p_dev->rc_app_settings.num_ext_attrs; + p_dev->rc_app_settings.ext_attrs[st_index].attr_id = p_rsp->attrs[xx]; + p_dev->rc_app_settings.num_ext_attrs++; + } else { + st_index = p_dev->rc_app_settings.num_attrs; + p_dev->rc_app_settings.attrs[st_index].attr_id = p_rsp->attrs[xx]; + p_dev->rc_app_settings.num_attrs++; + } + } + p_dev->rc_app_settings.attr_index = 0; + p_dev->rc_app_settings.ext_attr_index = 0; + p_dev->rc_app_settings.ext_val_index = 0; + if (p_rsp->num_attr) { + list_player_app_setting_value_cmd(p_dev->rc_app_settings.attrs[0].attr_id, + p_dev); + } else { + BTIF_TRACE_ERROR("%s: No Player application settings found", __func__); + } } /*************************************************************************** -** -** Function handle_app_cur_val_response -** -** Description handles the the get attributes value response. -** -** Returns None -** -***************************************************************************/ -static void handle_app_cur_val_response (tBTA_AV_META_MSG *pmeta_msg, - tAVRC_GET_CUR_APP_VALUE_RSP *p_rsp) -{ - btrc_player_settings_t app_settings; - bt_bdaddr_t rc_addr; - uint16_t xx; - btif_rc_device_cb_t *p_dev = NULL; - - /* Todo: Do we need to retry on command timeout */ - if (p_rsp->status != AVRC_STS_NO_ERROR) - { - BTIF_TRACE_ERROR("%s: Error fetching current settings: 0x%02X", - __func__, p_rsp->status); - return; - } - p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle); - if(p_dev == NULL) - { - BTIF_TRACE_ERROR("%s: Error in getting Device Address", __func__); - osi_free_and_reset((void **)&p_rsp->p_vals); - return; - } - - bdcpy(rc_addr.address, p_dev->rc_addr); + * + * Function handle_app_val_response + * + * Description handles the the attributes value response and if extended + * menu is available, it initiates query for the attribute + * text. If not, it initiates procedure to get the current + * attribute values and calls the HAL callback for provding + * application settings information. + * Returns None + * + **************************************************************************/ +static void handle_app_val_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_LIST_APP_VALUES_RSP* p_rsp) { + uint8_t xx, attr_index; + uint8_t attrs[AVRC_MAX_APP_ATTR_SIZE]; + btif_rc_player_app_settings_t* p_app_settings; + bt_bdaddr_t rc_addr; + btif_rc_device_cb_t* p_dev = + btif_rc_get_device_by_handle(pmeta_msg->rc_handle); + + /* Todo: Do we need to retry on command timeout */ + if (p_dev == NULL || p_rsp->status != AVRC_STS_NO_ERROR) { + BTIF_TRACE_ERROR("%s: Error fetching attribute values: 0x%02X", __func__, + p_rsp->status); + return; + } + + p_app_settings = &p_dev->rc_app_settings; + bdcpy(rc_addr.address, p_dev->rc_addr); + + if (p_app_settings->attr_index < p_app_settings->num_attrs) { + attr_index = p_app_settings->attr_index; + p_app_settings->attrs[attr_index].num_val = p_rsp->num_val; + for (xx = 0; xx < p_rsp->num_val; xx++) { + p_app_settings->attrs[attr_index].attr_val[xx] = p_rsp->vals[xx]; + } + attr_index++; + p_app_settings->attr_index++; + if (attr_index < p_app_settings->num_attrs) { + list_player_app_setting_value_cmd( + p_app_settings->attrs[p_app_settings->attr_index].attr_id, p_dev); + } else if (p_app_settings->ext_attr_index < p_app_settings->num_ext_attrs) { + attr_index = 0; + p_app_settings->ext_attr_index = 0; + list_player_app_setting_value_cmd( + p_app_settings->ext_attrs[attr_index].attr_id, p_dev); + } else { + for (xx = 0; xx < p_app_settings->num_attrs; xx++) { + attrs[xx] = p_app_settings->attrs[xx].attr_id; + } + get_player_app_setting_cmd(p_app_settings->num_attrs, attrs, p_dev); + HAL_CBACK(bt_rc_ctrl_callbacks, playerapplicationsetting_cb, &rc_addr, + p_app_settings->num_attrs, p_app_settings->attrs, 0, NULL); + } + } else if (p_app_settings->ext_attr_index < p_app_settings->num_ext_attrs) { + attr_index = p_app_settings->ext_attr_index; + p_app_settings->ext_attrs[attr_index].num_val = p_rsp->num_val; + for (xx = 0; xx < p_rsp->num_val; xx++) { + p_app_settings->ext_attrs[attr_index].ext_attr_val[xx].val = + p_rsp->vals[xx]; + } + attr_index++; + p_app_settings->ext_attr_index++; + if (attr_index < p_app_settings->num_ext_attrs) { + list_player_app_setting_value_cmd( + p_app_settings->ext_attrs[p_app_settings->ext_attr_index].attr_id, + p_dev); + } else { + uint8_t attr[AVRC_MAX_APP_ATTR_SIZE]; - app_settings.num_attr = p_rsp->num_val; - for (xx = 0; xx < app_settings.num_attr; xx++) - { - app_settings.attr_ids[xx] = p_rsp->p_vals[xx].attr_id; - app_settings.attr_values[xx] = p_rsp->p_vals[xx].attr_val; + for (uint8_t xx = 0; xx < p_app_settings->num_ext_attrs; xx++) { + attr[xx] = p_app_settings->ext_attrs[xx].attr_id; + } + get_player_app_setting_attr_text_cmd(attr, p_app_settings->num_ext_attrs, + p_dev); } - - HAL_CBACK(bt_rc_ctrl_callbacks, playerapplicationsetting_changed_cb, - &rc_addr, &app_settings); - /* Application settings are fetched only once for initial values - * initiate anything that follows after RC procedure. - * Defer it if browsing is supported till players query - */ - rc_ctrl_procedure_complete(p_dev); - osi_free_and_reset((void **)&p_rsp->p_vals); + } } /*************************************************************************** -** -** Function handle_app_attr_txt_response -** -** Description handles the the get attributes text response, if fails -** calls HAL callback with just normal settings and initiates -** query for current settings else initiates query for value text -** Returns None -** -***************************************************************************/ -static void handle_app_attr_txt_response (tBTA_AV_META_MSG *pmeta_msg, - tAVRC_GET_APP_ATTR_TXT_RSP *p_rsp) -{ - uint8_t xx; - uint8_t vals[AVRC_MAX_APP_ATTR_SIZE]; - btif_rc_player_app_settings_t *p_app_settings; - bt_bdaddr_t rc_addr; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle); - - if (p_dev == NULL) - { - BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); - return; - } - - bdcpy(rc_addr.address, p_dev->rc_addr); - p_app_settings = &p_dev->rc_app_settings; - - /* Todo: Do we need to retry on command timeout */ - if (p_rsp->status != AVRC_STS_NO_ERROR) - { - uint8_t attrs[AVRC_MAX_APP_ATTR_SIZE]; - - BTIF_TRACE_ERROR("%s: Error fetching attribute text: 0x%02X", - __func__, p_rsp->status); - /* Not able to fetch Text for extended Menu, skip the process - * and cleanup used memory. Proceed to get the current settings - * for standard attributes. - */ - p_app_settings->num_ext_attrs = 0; - for (xx = 0; xx < p_app_settings->ext_attr_index; xx++) - { - osi_free_and_reset((void **)&p_app_settings->ext_attrs[xx].p_str); - } - p_app_settings->ext_attr_index = 0; - - if (p_dev) - { - for (xx = 0; xx < p_app_settings->num_attrs; xx++) - { - attrs[xx] = p_app_settings->attrs[xx].attr_id; - } - - HAL_CBACK(bt_rc_ctrl_callbacks, playerapplicationsetting_cb, &rc_addr, - p_app_settings->num_attrs, p_app_settings->attrs, 0, NULL); - get_player_app_setting_cmd (xx, attrs, p_dev); - } - return; - } - - for (xx = 0; xx < p_rsp->num_attr; xx++) - { - uint8_t x; - for (x = 0; x < p_app_settings->num_ext_attrs; x++) - { - if (p_app_settings->ext_attrs[x].attr_id == p_rsp->p_attrs[xx].attr_id) - { - p_app_settings->ext_attrs[x].charset_id = p_rsp->p_attrs[xx].charset_id; - p_app_settings->ext_attrs[x].str_len = p_rsp->p_attrs[xx].str_len; - p_app_settings->ext_attrs[x].p_str = p_rsp->p_attrs[xx].p_str; - break; - } - } - } - - for (xx = 0; xx < p_app_settings->ext_attrs[0].num_val; xx++) - { - vals[xx] = p_app_settings->ext_attrs[0].ext_attr_val[xx].val; - } - get_player_app_setting_value_text_cmd(vals, xx, p_dev); + * + * Function handle_app_cur_val_response + * + * Description handles the the get attributes value response. + * + * Returns None + * + **************************************************************************/ +static void handle_app_cur_val_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_GET_CUR_APP_VALUE_RSP* p_rsp) { + btrc_player_settings_t app_settings; + bt_bdaddr_t rc_addr; + uint16_t xx; + btif_rc_device_cb_t* p_dev = NULL; + + /* Todo: Do we need to retry on command timeout */ + if (p_rsp->status != AVRC_STS_NO_ERROR) { + BTIF_TRACE_ERROR("%s: Error fetching current settings: 0x%02X", __func__, + p_rsp->status); + return; + } + p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle); + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: Error in getting Device Address", __func__); + osi_free_and_reset((void**)&p_rsp->p_vals); + return; + } + + bdcpy(rc_addr.address, p_dev->rc_addr); + + app_settings.num_attr = p_rsp->num_val; + for (xx = 0; xx < app_settings.num_attr; xx++) { + app_settings.attr_ids[xx] = p_rsp->p_vals[xx].attr_id; + app_settings.attr_values[xx] = p_rsp->p_vals[xx].attr_val; + } + + HAL_CBACK(bt_rc_ctrl_callbacks, playerapplicationsetting_changed_cb, &rc_addr, + &app_settings); + /* Application settings are fetched only once for initial values + * initiate anything that follows after RC procedure. + * Defer it if browsing is supported till players query + */ + rc_ctrl_procedure_complete(p_dev); + osi_free_and_reset((void**)&p_rsp->p_vals); } - /*************************************************************************** -** -** Function handle_app_attr_val_txt_response -** -** Description handles the the get attributes value text response, if fails -** calls HAL callback with just normal settings and initiates -** query for current settings -** Returns None -** -***************************************************************************/ -static void handle_app_attr_val_txt_response (tBTA_AV_META_MSG *pmeta_msg, - tAVRC_GET_APP_ATTR_TXT_RSP *p_rsp) -{ - uint8_t xx, attr_index; - uint8_t vals[AVRC_MAX_APP_ATTR_SIZE]; + * + * Function handle_app_attr_txt_response + * + * Description handles the the get attributes text response, if fails + * calls HAL callback with just normal settings and initiates + * query for current settings else initiates query for value + *text + * Returns None + * + **************************************************************************/ +static void handle_app_attr_txt_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_GET_APP_ATTR_TXT_RSP* p_rsp) { + uint8_t xx; + uint8_t vals[AVRC_MAX_APP_ATTR_SIZE]; + btif_rc_player_app_settings_t* p_app_settings; + bt_bdaddr_t rc_addr; + btif_rc_device_cb_t* p_dev = + btif_rc_get_device_by_handle(pmeta_msg->rc_handle); + + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); + return; + } + + bdcpy(rc_addr.address, p_dev->rc_addr); + p_app_settings = &p_dev->rc_app_settings; + + /* Todo: Do we need to retry on command timeout */ + if (p_rsp->status != AVRC_STS_NO_ERROR) { uint8_t attrs[AVRC_MAX_APP_ATTR_SIZE]; - btif_rc_player_app_settings_t *p_app_settings; - bt_bdaddr_t rc_addr; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle); - - if (p_dev == NULL) - { - BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); - return; - } - - bdcpy(rc_addr.address, p_dev->rc_addr); - p_app_settings = &p_dev->rc_app_settings; - - /* Todo: Do we need to retry on command timeout */ - if (p_rsp->status != AVRC_STS_NO_ERROR) - { - uint8_t attrs[AVRC_MAX_APP_ATTR_SIZE]; - - BTIF_TRACE_ERROR("%s: Error fetching attribute value text: 0x%02X", - __func__, p_rsp->status); - - /* Not able to fetch Text for extended Menu, skip the process - * and cleanup used memory. Proceed to get the current settings - * for standard attributes. - */ - p_app_settings->num_ext_attrs = 0; - for (xx = 0; xx < p_app_settings->ext_attr_index; xx++) - { - int x; - btrc_player_app_ext_attr_t *p_ext_attr = &p_app_settings->ext_attrs[xx]; - - for (x = 0; x < p_ext_attr->num_val; x++) - osi_free_and_reset((void **)&p_ext_attr->ext_attr_val[x].p_str); - p_ext_attr->num_val = 0; - osi_free_and_reset((void **)&p_app_settings->ext_attrs[xx].p_str); - } - p_app_settings->ext_attr_index = 0; - - for (xx = 0; xx < p_app_settings->num_attrs; xx++) - { - attrs[xx] = p_app_settings->attrs[xx].attr_id; - } - HAL_CBACK (bt_rc_ctrl_callbacks, playerapplicationsetting_cb, &rc_addr, - p_app_settings->num_attrs, p_app_settings->attrs, 0, NULL); - get_player_app_setting_cmd (xx, attrs, p_dev); - return; - } - - for (xx = 0; xx < p_rsp->num_attr; xx++) - { - uint8_t x; - btrc_player_app_ext_attr_t *p_ext_attr; - p_ext_attr = &p_app_settings->ext_attrs[p_app_settings->ext_val_index]; - for (x = 0; x < p_rsp->num_attr; x++) - { - if (p_ext_attr->ext_attr_val[x].val == p_rsp->p_attrs[xx].attr_id) - { - p_ext_attr->ext_attr_val[x].charset_id = p_rsp->p_attrs[xx].charset_id; - p_ext_attr->ext_attr_val[x].str_len = p_rsp->p_attrs[xx].str_len; - p_ext_attr->ext_attr_val[x].p_str = p_rsp->p_attrs[xx].p_str; - break; - } - } - } - p_app_settings->ext_val_index++; - - if (p_app_settings->ext_val_index < p_app_settings->num_ext_attrs) - { - attr_index = p_app_settings->ext_val_index; - for (xx = 0; xx < p_app_settings->ext_attrs[attr_index].num_val; xx++) - { - vals[xx] = p_app_settings->ext_attrs[attr_index].ext_attr_val[xx].val; - } - get_player_app_setting_value_text_cmd(vals, xx, p_dev); + BTIF_TRACE_ERROR("%s: Error fetching attribute text: 0x%02X", __func__, + p_rsp->status); + /* Not able to fetch Text for extended Menu, skip the process + * and cleanup used memory. Proceed to get the current settings + * for standard attributes. + */ + p_app_settings->num_ext_attrs = 0; + for (xx = 0; xx < p_app_settings->ext_attr_index; xx++) { + osi_free_and_reset((void**)&p_app_settings->ext_attrs[xx].p_str); } - else - { - uint8_t x; + p_app_settings->ext_attr_index = 0; - for (xx = 0; xx < p_app_settings->num_attrs; xx++) - { - attrs[xx] = p_app_settings->attrs[xx].attr_id; - } - for (x = 0; x < p_app_settings->num_ext_attrs; x++) - { - attrs[xx+x] = p_app_settings->ext_attrs[x].attr_id; - } - HAL_CBACK (bt_rc_ctrl_callbacks, playerapplicationsetting_cb, &rc_addr, - p_app_settings->num_attrs, p_app_settings->attrs, - p_app_settings->num_ext_attrs, p_app_settings->ext_attrs); - get_player_app_setting_cmd (xx + x, attrs, p_dev); + if (p_dev) { + for (xx = 0; xx < p_app_settings->num_attrs; xx++) { + attrs[xx] = p_app_settings->attrs[xx].attr_id; + } - /* Free the application settings information after sending to - * application. - */ - for (xx = 0; xx < p_app_settings->ext_attr_index; xx++) - { - int x; - btrc_player_app_ext_attr_t *p_ext_attr = &p_app_settings->ext_attrs[xx]; - - for (x = 0; x < p_ext_attr->num_val; x++) - osi_free_and_reset((void **)&p_ext_attr->ext_attr_val[x].p_str); - p_ext_attr->num_val = 0; - osi_free_and_reset((void **)&p_app_settings->ext_attrs[xx].p_str); - } - p_app_settings->num_attrs = 0; + HAL_CBACK(bt_rc_ctrl_callbacks, playerapplicationsetting_cb, &rc_addr, + p_app_settings->num_attrs, p_app_settings->attrs, 0, NULL); + get_player_app_setting_cmd(xx, attrs, p_dev); + } + return; + } + + for (xx = 0; xx < p_rsp->num_attr; xx++) { + uint8_t x; + for (x = 0; x < p_app_settings->num_ext_attrs; x++) { + if (p_app_settings->ext_attrs[x].attr_id == p_rsp->p_attrs[xx].attr_id) { + p_app_settings->ext_attrs[x].charset_id = p_rsp->p_attrs[xx].charset_id; + p_app_settings->ext_attrs[x].str_len = p_rsp->p_attrs[xx].str_len; + p_app_settings->ext_attrs[x].p_str = p_rsp->p_attrs[xx].p_str; + break; + } } + } + + for (xx = 0; xx < p_app_settings->ext_attrs[0].num_val; xx++) { + vals[xx] = p_app_settings->ext_attrs[0].ext_attr_val[xx].val; + } + get_player_app_setting_value_text_cmd(vals, xx, p_dev); } /*************************************************************************** -** -** Function handle_set_app_attr_val_response -** -** Description handles the the set attributes value response, if fails -** calls HAL callback to indicate the failure -** Returns None -** -***************************************************************************/ -static void handle_set_app_attr_val_response (tBTA_AV_META_MSG *pmeta_msg, tAVRC_RSP *p_rsp) -{ - uint8_t accepted = 0; - bt_bdaddr_t rc_addr; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle); + * + * Function handle_app_attr_val_txt_response + * + * Description handles the the get attributes value text response, if fails + * calls HAL callback with just normal settings and initiates + * query for current settings + * Returns None + * + **************************************************************************/ +static void handle_app_attr_val_txt_response( + tBTA_AV_META_MSG* pmeta_msg, tAVRC_GET_APP_ATTR_TXT_RSP* p_rsp) { + uint8_t xx, attr_index; + uint8_t vals[AVRC_MAX_APP_ATTR_SIZE]; + uint8_t attrs[AVRC_MAX_APP_ATTR_SIZE]; + btif_rc_player_app_settings_t* p_app_settings; + bt_bdaddr_t rc_addr; + btif_rc_device_cb_t* p_dev = + btif_rc_get_device_by_handle(pmeta_msg->rc_handle); + + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); + return; + } + + bdcpy(rc_addr.address, p_dev->rc_addr); + p_app_settings = &p_dev->rc_app_settings; + + /* Todo: Do we need to retry on command timeout */ + if (p_rsp->status != AVRC_STS_NO_ERROR) { + uint8_t attrs[AVRC_MAX_APP_ATTR_SIZE]; - if (p_dev == NULL) - { - BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); - return; + BTIF_TRACE_ERROR("%s: Error fetching attribute value text: 0x%02X", + __func__, p_rsp->status); + + /* Not able to fetch Text for extended Menu, skip the process + * and cleanup used memory. Proceed to get the current settings + * for standard attributes. + */ + p_app_settings->num_ext_attrs = 0; + for (xx = 0; xx < p_app_settings->ext_attr_index; xx++) { + int x; + btrc_player_app_ext_attr_t* p_ext_attr = &p_app_settings->ext_attrs[xx]; + + for (x = 0; x < p_ext_attr->num_val; x++) + osi_free_and_reset((void**)&p_ext_attr->ext_attr_val[x].p_str); + p_ext_attr->num_val = 0; + osi_free_and_reset((void**)&p_app_settings->ext_attrs[xx].p_str); + } + p_app_settings->ext_attr_index = 0; + + for (xx = 0; xx < p_app_settings->num_attrs; xx++) { + attrs[xx] = p_app_settings->attrs[xx].attr_id; + } + HAL_CBACK(bt_rc_ctrl_callbacks, playerapplicationsetting_cb, &rc_addr, + p_app_settings->num_attrs, p_app_settings->attrs, 0, NULL); + + get_player_app_setting_cmd(xx, attrs, p_dev); + return; + } + + for (xx = 0; xx < p_rsp->num_attr; xx++) { + uint8_t x; + btrc_player_app_ext_attr_t* p_ext_attr; + p_ext_attr = &p_app_settings->ext_attrs[p_app_settings->ext_val_index]; + for (x = 0; x < p_rsp->num_attr; x++) { + if (p_ext_attr->ext_attr_val[x].val == p_rsp->p_attrs[xx].attr_id) { + p_ext_attr->ext_attr_val[x].charset_id = p_rsp->p_attrs[xx].charset_id; + p_ext_attr->ext_attr_val[x].str_len = p_rsp->p_attrs[xx].str_len; + p_ext_attr->ext_attr_val[x].p_str = p_rsp->p_attrs[xx].p_str; + break; + } } + } + p_app_settings->ext_val_index++; - bdcpy(rc_addr.address, p_dev->rc_addr); + if (p_app_settings->ext_val_index < p_app_settings->num_ext_attrs) { + attr_index = p_app_settings->ext_val_index; + for (xx = 0; xx < p_app_settings->ext_attrs[attr_index].num_val; xx++) { + vals[xx] = p_app_settings->ext_attrs[attr_index].ext_attr_val[xx].val; + } + get_player_app_setting_value_text_cmd(vals, xx, p_dev); + } else { + uint8_t x; + + for (xx = 0; xx < p_app_settings->num_attrs; xx++) { + attrs[xx] = p_app_settings->attrs[xx].attr_id; + } + for (x = 0; x < p_app_settings->num_ext_attrs; x++) { + attrs[xx + x] = p_app_settings->ext_attrs[x].attr_id; + } + HAL_CBACK(bt_rc_ctrl_callbacks, playerapplicationsetting_cb, &rc_addr, + p_app_settings->num_attrs, p_app_settings->attrs, + p_app_settings->num_ext_attrs, p_app_settings->ext_attrs); + get_player_app_setting_cmd(xx + x, attrs, p_dev); - /* For timeout pmeta_msg will be NULL, else we need to - * check if this is accepted by TG + /* Free the application settings information after sending to + * application. */ - if (pmeta_msg && (pmeta_msg->code == AVRC_RSP_ACCEPT)) - { - accepted = 1; + for (xx = 0; xx < p_app_settings->ext_attr_index; xx++) { + int x; + btrc_player_app_ext_attr_t* p_ext_attr = &p_app_settings->ext_attrs[xx]; + + for (x = 0; x < p_ext_attr->num_val; x++) + osi_free_and_reset((void**)&p_ext_attr->ext_attr_val[x].p_str); + p_ext_attr->num_val = 0; + osi_free_and_reset((void**)&p_app_settings->ext_attrs[xx].p_str); } - HAL_CBACK(bt_rc_ctrl_callbacks, setplayerappsetting_rsp_cb, &rc_addr, accepted); + p_app_settings->num_attrs = 0; + } } /*************************************************************************** -** -** Function handle_get_elem_attr_response -** -** Description handles the the element attributes response, calls -** HAL callback to update track change information. -** Returns None -** -***************************************************************************/ -static void handle_get_elem_attr_response (tBTA_AV_META_MSG *pmeta_msg, - tAVRC_GET_ELEM_ATTRS_RSP *p_rsp) -{ - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle); - - if (p_rsp->status == AVRC_STS_NO_ERROR) { - bt_bdaddr_t rc_addr; - size_t buf_size = p_rsp->num_attr * sizeof(btrc_element_attr_val_t); - btrc_element_attr_val_t *p_attr = - (btrc_element_attr_val_t *)osi_calloc(buf_size); - - if (p_dev == NULL) - { - BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); - return; - } - - bdcpy(rc_addr.address, p_dev->rc_addr); - - for (int i = 0; i < p_rsp->num_attr; i++) { - p_attr[i].attr_id = p_rsp->p_attrs[i].attr_id; - /* Todo. Legth limit check to include null */ - if (p_rsp->p_attrs[i].name.str_len && - p_rsp->p_attrs[i].name.p_str) { - memcpy(p_attr[i].text, p_rsp->p_attrs[i].name.p_str, - p_rsp->p_attrs[i].name.str_len); - osi_free_and_reset((void **)&p_rsp->p_attrs[i].name.p_str); - } - } - HAL_CBACK(bt_rc_ctrl_callbacks, track_changed_cb, - &rc_addr, p_rsp->num_attr, p_attr); - osi_free(p_attr); - } else if (p_rsp->status == BTIF_RC_STS_TIMEOUT) { - /* Retry for timeout case, this covers error handling - * for continuation failure also. - */ - uint32_t attr_list[] = { - AVRC_MEDIA_ATTR_ID_TITLE, - AVRC_MEDIA_ATTR_ID_ARTIST, - AVRC_MEDIA_ATTR_ID_ALBUM, - AVRC_MEDIA_ATTR_ID_TRACK_NUM, - AVRC_MEDIA_ATTR_ID_NUM_TRACKS, - AVRC_MEDIA_ATTR_ID_GENRE, - AVRC_MEDIA_ATTR_ID_PLAYING_TIME - }; - get_element_attribute_cmd (AVRC_MAX_NUM_MEDIA_ATTR_ID, attr_list, p_dev); - } else { - BTIF_TRACE_ERROR("%s: Error in get element attr procedure: %d", - __func__, p_rsp->status); - } + * + * Function handle_set_app_attr_val_response + * + * Description handles the the set attributes value response, if fails + * calls HAL callback to indicate the failure + * Returns None + * + **************************************************************************/ +static void handle_set_app_attr_val_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_RSP* p_rsp) { + uint8_t accepted = 0; + bt_bdaddr_t rc_addr; + btif_rc_device_cb_t* p_dev = + btif_rc_get_device_by_handle(pmeta_msg->rc_handle); + + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); + return; + } + + bdcpy(rc_addr.address, p_dev->rc_addr); + + /* For timeout pmeta_msg will be NULL, else we need to + * check if this is accepted by TG + */ + if (pmeta_msg && (pmeta_msg->code == AVRC_RSP_ACCEPT)) { + accepted = 1; + } + HAL_CBACK(bt_rc_ctrl_callbacks, setplayerappsetting_rsp_cb, &rc_addr, + accepted); } /*************************************************************************** -** -** Function handle_get_playstatus_response -** -** Description handles the the play status response, calls -** HAL callback to update play position. -** Returns None -** -***************************************************************************/ -static void handle_get_playstatus_response (tBTA_AV_META_MSG *pmeta_msg, - tAVRC_GET_PLAY_STATUS_RSP *p_rsp) -{ - bt_bdaddr_t rc_addr; + * + * Function handle_get_elem_attr_response + * + * Description handles the the element attributes response, calls + * HAL callback to update track change information. + * Returns None + * + **************************************************************************/ +static void handle_get_elem_attr_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_GET_ELEM_ATTRS_RSP* p_rsp) { + btif_rc_device_cb_t* p_dev = + btif_rc_get_device_by_handle(pmeta_msg->rc_handle); - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle); + if (p_rsp->status == AVRC_STS_NO_ERROR) { + bt_bdaddr_t rc_addr; + size_t buf_size = p_rsp->num_attr * sizeof(btrc_element_attr_val_t); + btrc_element_attr_val_t* p_attr = + (btrc_element_attr_val_t*)osi_calloc(buf_size); - if (p_dev == NULL) - { - BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); - return; + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); + return; } bdcpy(rc_addr.address, p_dev->rc_addr); - if (p_rsp->status == AVRC_STS_NO_ERROR) - { - HAL_CBACK(bt_rc_ctrl_callbacks, play_position_changed_cb, - &rc_addr, p_rsp->song_len, p_rsp->song_pos); - } - else - { - BTIF_TRACE_ERROR("%s: Error in get play status procedure: %d", - __func__, p_rsp->status); + for (int i = 0; i < p_rsp->num_attr; i++) { + p_attr[i].attr_id = p_rsp->p_attrs[i].attr_id; + /* Todo. Legth limit check to include null */ + if (p_rsp->p_attrs[i].name.str_len && p_rsp->p_attrs[i].name.p_str) { + memcpy(p_attr[i].text, p_rsp->p_attrs[i].name.p_str, + p_rsp->p_attrs[i].name.str_len); + osi_free_and_reset((void**)&p_rsp->p_attrs[i].name.p_str); + } } + HAL_CBACK(bt_rc_ctrl_callbacks, track_changed_cb, &rc_addr, p_rsp->num_attr, + p_attr); + osi_free(p_attr); + } else if (p_rsp->status == BTIF_RC_STS_TIMEOUT) { + /* Retry for timeout case, this covers error handling + * for continuation failure also. + */ + uint32_t attr_list[] = { + AVRC_MEDIA_ATTR_ID_TITLE, AVRC_MEDIA_ATTR_ID_ARTIST, + AVRC_MEDIA_ATTR_ID_ALBUM, AVRC_MEDIA_ATTR_ID_TRACK_NUM, + AVRC_MEDIA_ATTR_ID_NUM_TRACKS, AVRC_MEDIA_ATTR_ID_GENRE, + AVRC_MEDIA_ATTR_ID_PLAYING_TIME}; + get_element_attribute_cmd(AVRC_MAX_NUM_MEDIA_ATTR_ID, attr_list, p_dev); + } else { + BTIF_TRACE_ERROR("%s: Error in get element attr procedure: %d", __func__, + p_rsp->status); + } } /*************************************************************************** -** -** Function handle_get_folder_items_response -** -** Description handles the the get folder items response, calls -** HAL callback to send the folder items. -** Returns None -** -***************************************************************************/ -static void handle_get_folder_items_response ( - tBTA_AV_META_MSG *pmeta_msg, tAVRC_GET_ITEMS_RSP *p_rsp) -{ - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle); - bt_bdaddr_t rc_addr; - bdcpy(rc_addr.address, p_dev->rc_addr); - - if (p_rsp->status == AVRC_STS_NO_ERROR) - { - /* Convert the internal folder listing into a response that can - * be passed onto JNI via HAL_CBACK - */ - uint8_t item_count = p_rsp->item_count; - btrc_folder_items_t *btrc_items = - (btrc_folder_items_t *) osi_malloc(sizeof(btrc_folder_items_t) * item_count); - for (uint8_t i = 0; i < item_count; i++) { - const tAVRC_ITEM *avrc_item = &(p_rsp->p_item_list[i]); - btrc_folder_items_t *btrc_item = &(btrc_items[i]); - BTIF_TRACE_DEBUG("%s folder item type %d", __func__, avrc_item->item_type); - switch (avrc_item->item_type) { - case AVRC_ITEM_MEDIA: - BTIF_TRACE_DEBUG("%s setting type to %d", __func__, BTRC_ITEM_MEDIA); - get_folder_item_type_media(avrc_item, btrc_item); - break; - - case AVRC_ITEM_FOLDER: - BTIF_TRACE_DEBUG("%s setting type to BTRC_ITEM_FOLDER", __func__); - get_folder_item_type_folder(avrc_item, btrc_item); - break; - - case AVRC_ITEM_PLAYER: - BTIF_TRACE_DEBUG("%s setting type to BTRC_ITEM_PLAYER", __func__); - get_folder_item_type_player(avrc_item, btrc_item); - break; - - default: - BTIF_TRACE_ERROR("%s cannot understand folder item type %d", - __func__, avrc_item->item_type); - } - } - - HAL_CBACK(bt_rc_ctrl_callbacks, - get_folder_items_cb, - &rc_addr, - /* We want to make the ownership explicit in native */ - (const btrc_folder_items_t *) btrc_items, - item_count); - BTIF_TRACE_DEBUG("%s HAL CBACK get_folder_items_cb finished", __func__); - - /* Release the memory block for items since we OWN the object */ - osi_free(btrc_items); - } - else - { - BTIF_TRACE_ERROR("%s: Error %d", __func__, p_rsp->status); - } + * + * Function handle_get_playstatus_response + * + * Description handles the the play status response, calls + * HAL callback to update play position. + * Returns None + * + **************************************************************************/ +static void handle_get_playstatus_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_GET_PLAY_STATUS_RSP* p_rsp) { + bt_bdaddr_t rc_addr; + + btif_rc_device_cb_t* p_dev = + btif_rc_get_device_by_handle(pmeta_msg->rc_handle); + + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: p_dev NULL", __func__); + return; + } + + bdcpy(rc_addr.address, p_dev->rc_addr); + + if (p_rsp->status == AVRC_STS_NO_ERROR) { + HAL_CBACK(bt_rc_ctrl_callbacks, play_position_changed_cb, &rc_addr, + p_rsp->song_len, p_rsp->song_pos); + } else { + BTIF_TRACE_ERROR("%s: Error in get play status procedure: %d", __func__, + p_rsp->status); + } } /*************************************************************************** -** -** Function get_folder_item_type_media -** -** Description Converts the AVRC representation of a folder item with -** TYPE media to BTIF representation. -** Returns None -** -***************************************************************************/ -void get_folder_item_type_media( - const tAVRC_ITEM *avrc_item, btrc_folder_items_t *btrc_item) -{ - btrc_item->item_type = BTRC_ITEM_MEDIA; - const tAVRC_ITEM_MEDIA *avrc_item_media = &(avrc_item->u.media); - btrc_item_media_t *btrc_item_media = &(btrc_item->media); - /* UID */ - memset(btrc_item_media->uid, 0, BTRC_UID_SIZE * sizeof (uint8_t)); - memcpy(btrc_item_media->uid, - avrc_item_media->uid, - sizeof(uint8_t) * BTRC_UID_SIZE); - - /* Audio/Video type */ - switch (avrc_item_media->type) { - case AVRC_MEDIA_TYPE_AUDIO: - btrc_item_media->type = BTRC_MEDIA_TYPE_AUDIO; - break; - case AVRC_MEDIA_TYPE_VIDEO: - btrc_item_media->type = BTRC_MEDIA_TYPE_VIDEO; - break; - } + * + * Function handle_get_folder_items_response + * + * Description handles the the get folder items response, calls + * HAL callback to send the folder items. + * Returns None + * + **************************************************************************/ +static void handle_get_folder_items_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_GET_ITEMS_RSP* p_rsp) { + btif_rc_device_cb_t* p_dev = + btif_rc_get_device_by_handle(pmeta_msg->rc_handle); + bt_bdaddr_t rc_addr; + bdcpy(rc_addr.address, p_dev->rc_addr); + + if (p_rsp->status == AVRC_STS_NO_ERROR) { + /* Convert the internal folder listing into a response that can + * be passed onto JNI via HAL_CBACK + */ + uint8_t item_count = p_rsp->item_count; + btrc_folder_items_t* btrc_items = (btrc_folder_items_t*)osi_malloc( + sizeof(btrc_folder_items_t) * item_count); + for (uint8_t i = 0; i < item_count; i++) { + const tAVRC_ITEM* avrc_item = &(p_rsp->p_item_list[i]); + btrc_folder_items_t* btrc_item = &(btrc_items[i]); + BTIF_TRACE_DEBUG("%s folder item type %d", __func__, + avrc_item->item_type); + switch (avrc_item->item_type) { + case AVRC_ITEM_MEDIA: + BTIF_TRACE_DEBUG("%s setting type to %d", __func__, BTRC_ITEM_MEDIA); + get_folder_item_type_media(avrc_item, btrc_item); + break; + + case AVRC_ITEM_FOLDER: + BTIF_TRACE_DEBUG("%s setting type to BTRC_ITEM_FOLDER", __func__); + get_folder_item_type_folder(avrc_item, btrc_item); + break; + + case AVRC_ITEM_PLAYER: + BTIF_TRACE_DEBUG("%s setting type to BTRC_ITEM_PLAYER", __func__); + get_folder_item_type_player(avrc_item, btrc_item); + break; - /* Charset ID */ - btrc_item_media->charset_id = avrc_item_media->name.charset_id; - - /* Copy the name */ - BTIF_TRACE_DEBUG("%s max len %d str len %d", __func__, BTRC_MAX_ATTR_STR_LEN, - avrc_item_media->name.str_len); - memset(btrc_item_media->name, 0, BTRC_MAX_ATTR_STR_LEN * sizeof (uint8_t)); - memcpy(btrc_item_media->name, - avrc_item_media->name.p_str, - sizeof(uint8_t) * (avrc_item_media->name.str_len)); - - /* Copy the parameters */ - btrc_item_media->num_attrs = avrc_item_media->attr_count; - btrc_item_media->p_attrs = (btrc_element_attr_val_t *) osi_malloc ( - btrc_item_media->num_attrs * sizeof (btrc_element_attr_val_t)); - - /* Extract each attribute */ - for (int i = 0; i < avrc_item_media->attr_count; i++) { - btrc_element_attr_val_t *btrc_attr_pair = - &(btrc_item_media->p_attrs[i]); - tAVRC_ATTR_ENTRY *avrc_attr_pair = - &(avrc_item_media->p_attr_list[i]); - - BTIF_TRACE_DEBUG("%s media attr id 0x%x", __func__, - avrc_attr_pair->attr_id); + default: + BTIF_TRACE_ERROR("%s cannot understand folder item type %d", __func__, + avrc_item->item_type); + } + } - switch (avrc_attr_pair->attr_id) { - case AVRC_MEDIA_ATTR_ID_TITLE: - btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_TITLE; - break; - case AVRC_MEDIA_ATTR_ID_ARTIST: - btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_ARTIST; - break; - case AVRC_MEDIA_ATTR_ID_ALBUM: - btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_ALBUM; - break; - case AVRC_MEDIA_ATTR_ID_TRACK_NUM: - btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_TRACK_NUM; - break; - case AVRC_MEDIA_ATTR_ID_NUM_TRACKS: - btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_NUM_TRACKS; - break; - case AVRC_MEDIA_ATTR_ID_GENRE: - btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_GENRE; - break; - case AVRC_MEDIA_ATTR_ID_PLAYING_TIME: - btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_PLAYING_TIME; - break; - default: - BTIF_TRACE_ERROR("%s invalid media attr id: 0x%x", - __func__, avrc_attr_pair->attr_id); - btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_INVALID; - } + HAL_CBACK(bt_rc_ctrl_callbacks, get_folder_items_cb, &rc_addr, + /* We want to make the ownership explicit in native */ + (const btrc_folder_items_t*)btrc_items, item_count); + BTIF_TRACE_DEBUG("%s HAL CBACK get_folder_items_cb finished", __func__); - memset(btrc_attr_pair->text, 0, - BTRC_MAX_ATTR_STR_LEN * sizeof (uint8_t)); - memcpy(btrc_attr_pair->text, - avrc_attr_pair->name.p_str, - avrc_attr_pair->name.str_len); - } + /* Release the memory block for items since we OWN the object */ + osi_free(btrc_items); + } else { + BTIF_TRACE_ERROR("%s: Error %d", __func__, p_rsp->status); + } } /*************************************************************************** -** -** Function get_folder_item_type_folder -** -** Description Converts the AVRC representation of a folder item with -** TYPE folder to BTIF representation. -** Returns None -** -***************************************************************************/ -void get_folder_item_type_folder( - const tAVRC_ITEM *avrc_item, btrc_folder_items_t *btrc_item) -{ - btrc_item->item_type = BTRC_ITEM_FOLDER; - const tAVRC_ITEM_FOLDER *avrc_item_folder = &(avrc_item->u.folder); - btrc_item_folder_t *btrc_item_folder = &(btrc_item->folder); - /* Copy the UID */ - memset(btrc_item_folder->uid, 0, BTRC_UID_SIZE * sizeof (uint8_t)); - memcpy(btrc_item_folder->uid, avrc_item_folder->uid, sizeof(uint8_t) * BTRC_UID_SIZE); - - /* Copy the type */ - switch (avrc_item_folder->type) { - case AVRC_FOLDER_TYPE_MIXED: - btrc_item_folder->type = BTRC_FOLDER_TYPE_MIXED; - break; - case AVRC_FOLDER_TYPE_TITLES: - btrc_item_folder->type = BTRC_FOLDER_TYPE_TITLES; - break; - case AVRC_FOLDER_TYPE_ALNUMS: - btrc_item_folder->type = BTRC_FOLDER_TYPE_ALBUMS; - break; - case AVRC_FOLDER_TYPE_ARTISTS: - btrc_item_folder->type = BTRC_FOLDER_TYPE_ARTISTS; - break; - case AVRC_FOLDER_TYPE_GENRES: - btrc_item_folder->type = BTRC_FOLDER_TYPE_GENRES; - break; - case AVRC_FOLDER_TYPE_PLAYLISTS: - btrc_item_folder->type = BTRC_FOLDER_TYPE_PLAYLISTS; - break; - case AVRC_FOLDER_TYPE_YEARS: - btrc_item_folder->type = BTRC_FOLDER_TYPE_YEARS; - break; + * + * Function get_folder_item_type_media + * + * Description Converts the AVRC representation of a folder item with + * TYPE media to BTIF representation. + * Returns None + * + **************************************************************************/ +void get_folder_item_type_media(const tAVRC_ITEM* avrc_item, + btrc_folder_items_t* btrc_item) { + btrc_item->item_type = BTRC_ITEM_MEDIA; + const tAVRC_ITEM_MEDIA* avrc_item_media = &(avrc_item->u.media); + btrc_item_media_t* btrc_item_media = &(btrc_item->media); + /* UID */ + memset(btrc_item_media->uid, 0, BTRC_UID_SIZE * sizeof(uint8_t)); + memcpy(btrc_item_media->uid, avrc_item_media->uid, + sizeof(uint8_t) * BTRC_UID_SIZE); + + /* Audio/Video type */ + switch (avrc_item_media->type) { + case AVRC_MEDIA_TYPE_AUDIO: + btrc_item_media->type = BTRC_MEDIA_TYPE_AUDIO; + break; + case AVRC_MEDIA_TYPE_VIDEO: + btrc_item_media->type = BTRC_MEDIA_TYPE_VIDEO; + break; + } + + /* Charset ID */ + btrc_item_media->charset_id = avrc_item_media->name.charset_id; + + /* Copy the name */ + BTIF_TRACE_DEBUG("%s max len %d str len %d", __func__, BTRC_MAX_ATTR_STR_LEN, + avrc_item_media->name.str_len); + memset(btrc_item_media->name, 0, BTRC_MAX_ATTR_STR_LEN * sizeof(uint8_t)); + memcpy(btrc_item_media->name, avrc_item_media->name.p_str, + sizeof(uint8_t) * (avrc_item_media->name.str_len)); + + /* Copy the parameters */ + btrc_item_media->num_attrs = avrc_item_media->attr_count; + btrc_item_media->p_attrs = (btrc_element_attr_val_t*)osi_malloc( + btrc_item_media->num_attrs * sizeof(btrc_element_attr_val_t)); + + /* Extract each attribute */ + for (int i = 0; i < avrc_item_media->attr_count; i++) { + btrc_element_attr_val_t* btrc_attr_pair = &(btrc_item_media->p_attrs[i]); + tAVRC_ATTR_ENTRY* avrc_attr_pair = &(avrc_item_media->p_attr_list[i]); + + BTIF_TRACE_DEBUG("%s media attr id 0x%x", __func__, + avrc_attr_pair->attr_id); + + switch (avrc_attr_pair->attr_id) { + case AVRC_MEDIA_ATTR_ID_TITLE: + btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_TITLE; + break; + case AVRC_MEDIA_ATTR_ID_ARTIST: + btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_ARTIST; + break; + case AVRC_MEDIA_ATTR_ID_ALBUM: + btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_ALBUM; + break; + case AVRC_MEDIA_ATTR_ID_TRACK_NUM: + btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_TRACK_NUM; + break; + case AVRC_MEDIA_ATTR_ID_NUM_TRACKS: + btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_NUM_TRACKS; + break; + case AVRC_MEDIA_ATTR_ID_GENRE: + btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_GENRE; + break; + case AVRC_MEDIA_ATTR_ID_PLAYING_TIME: + btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_PLAYING_TIME; + break; + default: + BTIF_TRACE_ERROR("%s invalid media attr id: 0x%x", __func__, + avrc_attr_pair->attr_id); + btrc_attr_pair->attr_id = BTRC_MEDIA_ATTR_ID_INVALID; } - /* Copy if playable */ - btrc_item_folder->playable = avrc_item_folder->playable; - - /* Copy name */ - BTIF_TRACE_DEBUG("%s max len %d str len %d", __func__, BTRC_MAX_ATTR_STR_LEN, - avrc_item_folder->name.str_len); - memset(btrc_item_folder->name, 0, BTRC_MAX_ATTR_STR_LEN * sizeof (uint8_t)); - memcpy(btrc_item_folder->name, - avrc_item_folder->name.p_str, - avrc_item_folder->name.str_len * sizeof (uint8_t)); - - /* Copy charset */ - btrc_item_folder->charset_id = avrc_item_folder->name.charset_id; + memset(btrc_attr_pair->text, 0, BTRC_MAX_ATTR_STR_LEN * sizeof(uint8_t)); + memcpy(btrc_attr_pair->text, avrc_attr_pair->name.p_str, + avrc_attr_pair->name.str_len); + } } /*************************************************************************** -** -** Function get_folder_item_type_player -** -** Description Converts the AVRC representation of a folder item with -** TYPE player to BTIF representation. -** Returns None -** -***************************************************************************/ -void get_folder_item_type_player( - const tAVRC_ITEM *avrc_item, btrc_folder_items_t *btrc_item) -{ - btrc_item->item_type = BTRC_ITEM_PLAYER; - const tAVRC_ITEM_PLAYER *avrc_item_player = &(avrc_item->u.player); - btrc_item_player_t *btrc_item_player = &(btrc_item->player); - /* Player ID */ - btrc_item_player->player_id = avrc_item_player->player_id; - /* Major type */ - btrc_item_player->major_type = avrc_item_player->major_type; - /* Sub type */ - btrc_item_player->sub_type = avrc_item_player->sub_type; - /* Features */ - memcpy(btrc_item_player->features, avrc_item_player->features, - BTRC_FEATURE_BIT_MASK_SIZE); - - memset(btrc_item_player->name, 0, - BTRC_MAX_ATTR_STR_LEN * sizeof(uint8_t)); - memcpy(btrc_item_player->name, - avrc_item_player->name.p_str, - avrc_item_player->name.str_len); + * + * Function get_folder_item_type_folder + * + * Description Converts the AVRC representation of a folder item with + * TYPE folder to BTIF representation. + * Returns None + * + **************************************************************************/ +void get_folder_item_type_folder(const tAVRC_ITEM* avrc_item, + btrc_folder_items_t* btrc_item) { + btrc_item->item_type = BTRC_ITEM_FOLDER; + const tAVRC_ITEM_FOLDER* avrc_item_folder = &(avrc_item->u.folder); + btrc_item_folder_t* btrc_item_folder = &(btrc_item->folder); + /* Copy the UID */ + memset(btrc_item_folder->uid, 0, BTRC_UID_SIZE * sizeof(uint8_t)); + memcpy(btrc_item_folder->uid, avrc_item_folder->uid, + sizeof(uint8_t) * BTRC_UID_SIZE); + + /* Copy the type */ + switch (avrc_item_folder->type) { + case AVRC_FOLDER_TYPE_MIXED: + btrc_item_folder->type = BTRC_FOLDER_TYPE_MIXED; + break; + case AVRC_FOLDER_TYPE_TITLES: + btrc_item_folder->type = BTRC_FOLDER_TYPE_TITLES; + break; + case AVRC_FOLDER_TYPE_ALNUMS: + btrc_item_folder->type = BTRC_FOLDER_TYPE_ALBUMS; + break; + case AVRC_FOLDER_TYPE_ARTISTS: + btrc_item_folder->type = BTRC_FOLDER_TYPE_ARTISTS; + break; + case AVRC_FOLDER_TYPE_GENRES: + btrc_item_folder->type = BTRC_FOLDER_TYPE_GENRES; + break; + case AVRC_FOLDER_TYPE_PLAYLISTS: + btrc_item_folder->type = BTRC_FOLDER_TYPE_PLAYLISTS; + break; + case AVRC_FOLDER_TYPE_YEARS: + btrc_item_folder->type = BTRC_FOLDER_TYPE_YEARS; + break; + } + + /* Copy if playable */ + btrc_item_folder->playable = avrc_item_folder->playable; + + /* Copy name */ + BTIF_TRACE_DEBUG("%s max len %d str len %d", __func__, BTRC_MAX_ATTR_STR_LEN, + avrc_item_folder->name.str_len); + memset(btrc_item_folder->name, 0, BTRC_MAX_ATTR_STR_LEN * sizeof(uint8_t)); + memcpy(btrc_item_folder->name, avrc_item_folder->name.p_str, + avrc_item_folder->name.str_len * sizeof(uint8_t)); + + /* Copy charset */ + btrc_item_folder->charset_id = avrc_item_folder->name.charset_id; } /*************************************************************************** -** -** Function handle_change_path_response -** -** Description handles the the change path response, calls -** HAL callback to send the updated folder -** Returns None -** -***************************************************************************/ -static void handle_change_path_response ( - tBTA_AV_META_MSG *pmeta_msg, tAVRC_CHG_PATH_RSP *p_rsp) -{ - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle); - bt_bdaddr_t rc_addr; - bdcpy(rc_addr.address, p_dev->rc_addr); - - if (p_rsp->status == AVRC_STS_NO_ERROR) - { - HAL_CBACK(bt_rc_ctrl_callbacks, change_folder_path_cb, - &rc_addr, p_rsp->num_items); - } - else - { - BTIF_TRACE_ERROR("%s error in handle_change_path_response %d", - __func__, p_rsp->status); - } + * + * Function get_folder_item_type_player + * + * Description Converts the AVRC representation of a folder item with + * TYPE player to BTIF representation. + * Returns None + * + **************************************************************************/ +void get_folder_item_type_player(const tAVRC_ITEM* avrc_item, + btrc_folder_items_t* btrc_item) { + btrc_item->item_type = BTRC_ITEM_PLAYER; + const tAVRC_ITEM_PLAYER* avrc_item_player = &(avrc_item->u.player); + btrc_item_player_t* btrc_item_player = &(btrc_item->player); + /* Player ID */ + btrc_item_player->player_id = avrc_item_player->player_id; + /* Major type */ + btrc_item_player->major_type = avrc_item_player->major_type; + /* Sub type */ + btrc_item_player->sub_type = avrc_item_player->sub_type; + /* Features */ + memcpy(btrc_item_player->features, avrc_item_player->features, + BTRC_FEATURE_BIT_MASK_SIZE); + + memset(btrc_item_player->name, 0, BTRC_MAX_ATTR_STR_LEN * sizeof(uint8_t)); + memcpy(btrc_item_player->name, avrc_item_player->name.p_str, + avrc_item_player->name.str_len); } /*************************************************************************** -** -** Function handle_set_browsed_player_response -** -** Description handles the the change path response, calls -** HAL callback to send the updated folder -** Returns None -** -***************************************************************************/ -static void handle_set_browsed_player_response( - tBTA_AV_META_MSG *pmeta_msg, tAVRC_SET_BR_PLAYER_RSP *p_rsp) -{ - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle); - bt_bdaddr_t rc_addr; - bdcpy(rc_addr.address, p_dev->rc_addr); - - if (p_rsp->status == AVRC_STS_NO_ERROR) - { - HAL_CBACK(bt_rc_ctrl_callbacks, set_browsed_player_cb, - &rc_addr, p_rsp->num_items, p_rsp->folder_depth); - } - else - { - BTIF_TRACE_ERROR("%s error %d", __func__, p_rsp->status); - } + * + * Function handle_change_path_response + * + * Description handles the the change path response, calls + * HAL callback to send the updated folder + * Returns None + * + **************************************************************************/ +static void handle_change_path_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_CHG_PATH_RSP* p_rsp) { + btif_rc_device_cb_t* p_dev = + btif_rc_get_device_by_handle(pmeta_msg->rc_handle); + bt_bdaddr_t rc_addr; + bdcpy(rc_addr.address, p_dev->rc_addr); + + if (p_rsp->status == AVRC_STS_NO_ERROR) { + HAL_CBACK(bt_rc_ctrl_callbacks, change_folder_path_cb, &rc_addr, + p_rsp->num_items); + } else { + BTIF_TRACE_ERROR("%s error in handle_change_path_response %d", __func__, + p_rsp->status); + } } +/*************************************************************************** + * + * Function handle_set_browsed_player_response + * + * Description handles the the change path response, calls + * HAL callback to send the updated folder + * Returns None + * + **************************************************************************/ +static void handle_set_browsed_player_response(tBTA_AV_META_MSG* pmeta_msg, + tAVRC_SET_BR_PLAYER_RSP* p_rsp) { + btif_rc_device_cb_t* p_dev = + btif_rc_get_device_by_handle(pmeta_msg->rc_handle); + bt_bdaddr_t rc_addr; + bdcpy(rc_addr.address, p_dev->rc_addr); + + if (p_rsp->status == AVRC_STS_NO_ERROR) { + HAL_CBACK(bt_rc_ctrl_callbacks, set_browsed_player_cb, &rc_addr, + p_rsp->num_items, p_rsp->folder_depth); + } else { + BTIF_TRACE_ERROR("%s error %d", __func__, p_rsp->status); + } +} /*************************************************************************** -** -** Function clear_cmd_timeout -** -** Description helper function to stop the command timeout timer -** Returns None -** -***************************************************************************/ -static void clear_cmd_timeout (uint8_t label) -{ - rc_transaction_t *p_txn; - - p_txn = get_transaction_by_lbl (label); - if (p_txn == NULL) - { - BTIF_TRACE_ERROR("%s: Error in transaction label lookup", __func__); - return; - } + * + * Function clear_cmd_timeout + * + * Description helper function to stop the command timeout timer + * Returns None + * + **************************************************************************/ +static void clear_cmd_timeout(uint8_t label) { + rc_transaction_t* p_txn; + + p_txn = get_transaction_by_lbl(label); + if (p_txn == NULL) { + BTIF_TRACE_ERROR("%s: Error in transaction label lookup", __func__); + return; + } - if (p_txn->txn_timer != NULL) - alarm_cancel(p_txn->txn_timer); + if (p_txn->txn_timer != NULL) alarm_cancel(p_txn->txn_timer); } /*************************************************************************** -** -** Function handle_avk_rc_metamsg_rsp -** -** Description Handle RC metamessage response -** -** Returns void -** -***************************************************************************/ -static void handle_avk_rc_metamsg_rsp(tBTA_AV_META_MSG *pmeta_msg) -{ - tAVRC_RESPONSE avrc_response = {0}; - uint8_t scratch_buf[512] = {0};// this variable is unused - uint16_t buf_len; - tAVRC_STS status; - - BTIF_TRACE_DEBUG("%s: opcode: %d rsp_code: %d ", __func__, - pmeta_msg->p_msg->hdr.opcode, pmeta_msg->code); - - status = AVRC_Ctrl_ParsResponse(pmeta_msg->p_msg, &avrc_response, scratch_buf, &buf_len); - if ((AVRC_OP_VENDOR == pmeta_msg->p_msg->hdr.opcode)&& - (pmeta_msg->code >= AVRC_RSP_NOT_IMPL)&& - (pmeta_msg->code <= AVRC_RSP_INTERIM)) - { - BTIF_TRACE_DEBUG("%s parse status %d pdu = %d rsp_status = %d", - __FUNCTION__, status, avrc_response.pdu, - pmeta_msg->p_msg->vendor.hdr.ctype); - - switch (avrc_response.pdu) - { - case AVRC_PDU_REGISTER_NOTIFICATION: - handle_notification_response(pmeta_msg, &avrc_response.reg_notif); - if (pmeta_msg->code == AVRC_RSP_INTERIM) - { - /* Don't free the transaction Id */ - clear_cmd_timeout (pmeta_msg->label); - return; - } - break; + * + * Function handle_avk_rc_metamsg_rsp + * + * Description Handle RC metamessage response + * + * Returns void + * + **************************************************************************/ +static void handle_avk_rc_metamsg_rsp(tBTA_AV_META_MSG* pmeta_msg) { + tAVRC_RESPONSE avrc_response = {0}; + uint8_t scratch_buf[512] = {0}; // this variable is unused + uint16_t buf_len; + tAVRC_STS status; + + BTIF_TRACE_DEBUG("%s: opcode: %d rsp_code: %d ", __func__, + pmeta_msg->p_msg->hdr.opcode, pmeta_msg->code); + + status = AVRC_Ctrl_ParsResponse(pmeta_msg->p_msg, &avrc_response, scratch_buf, + &buf_len); + if ((AVRC_OP_VENDOR == pmeta_msg->p_msg->hdr.opcode) && + (pmeta_msg->code >= AVRC_RSP_NOT_IMPL) && + (pmeta_msg->code <= AVRC_RSP_INTERIM)) { + BTIF_TRACE_DEBUG("%s parse status %d pdu = %d rsp_status = %d", + __FUNCTION__, status, avrc_response.pdu, + pmeta_msg->p_msg->vendor.hdr.ctype); + + switch (avrc_response.pdu) { + case AVRC_PDU_REGISTER_NOTIFICATION: + handle_notification_response(pmeta_msg, &avrc_response.reg_notif); + if (pmeta_msg->code == AVRC_RSP_INTERIM) { + /* Don't free the transaction Id */ + clear_cmd_timeout(pmeta_msg->label); + return; + } + break; - case AVRC_PDU_GET_CAPABILITIES: - handle_get_capability_response(pmeta_msg, &avrc_response.get_caps); - break; + case AVRC_PDU_GET_CAPABILITIES: + handle_get_capability_response(pmeta_msg, &avrc_response.get_caps); + break; - case AVRC_PDU_LIST_PLAYER_APP_ATTR: - handle_app_attr_response(pmeta_msg, &avrc_response.list_app_attr); - break; + case AVRC_PDU_LIST_PLAYER_APP_ATTR: + handle_app_attr_response(pmeta_msg, &avrc_response.list_app_attr); + break; - case AVRC_PDU_LIST_PLAYER_APP_VALUES: - handle_app_val_response(pmeta_msg, &avrc_response.list_app_values); - break; + case AVRC_PDU_LIST_PLAYER_APP_VALUES: + handle_app_val_response(pmeta_msg, &avrc_response.list_app_values); + break; - case AVRC_PDU_GET_CUR_PLAYER_APP_VALUE: - handle_app_cur_val_response(pmeta_msg, &avrc_response.get_cur_app_val); - break; + case AVRC_PDU_GET_CUR_PLAYER_APP_VALUE: + handle_app_cur_val_response(pmeta_msg, &avrc_response.get_cur_app_val); + break; - case AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT: - handle_app_attr_txt_response(pmeta_msg, &avrc_response.get_app_attr_txt); - break; + case AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT: + handle_app_attr_txt_response(pmeta_msg, + &avrc_response.get_app_attr_txt); + break; - case AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT: - handle_app_attr_val_txt_response(pmeta_msg, &avrc_response.get_app_val_txt); - break; + case AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT: + handle_app_attr_val_txt_response(pmeta_msg, + &avrc_response.get_app_val_txt); + break; - case AVRC_PDU_SET_PLAYER_APP_VALUE: - handle_set_app_attr_val_response(pmeta_msg, &avrc_response.set_app_val); - break; + case AVRC_PDU_SET_PLAYER_APP_VALUE: + handle_set_app_attr_val_response(pmeta_msg, &avrc_response.set_app_val); + break; - case AVRC_PDU_GET_ELEMENT_ATTR: - handle_get_elem_attr_response(pmeta_msg, &avrc_response.get_elem_attrs); - break; + case AVRC_PDU_GET_ELEMENT_ATTR: + handle_get_elem_attr_response(pmeta_msg, &avrc_response.get_elem_attrs); + break; - case AVRC_PDU_GET_PLAY_STATUS: - handle_get_playstatus_response(pmeta_msg, &avrc_response.get_play_status); - break; - } - } - else if (AVRC_OP_BROWSE == pmeta_msg->p_msg->hdr.opcode) - { - BTIF_TRACE_DEBUG("%s AVRC_OP_BROWSE pdu %d", __func__, avrc_response.pdu); - /* check what kind of command it is for browsing */ - switch (avrc_response.pdu) { - case AVRC_PDU_GET_FOLDER_ITEMS: - handle_get_folder_items_response(pmeta_msg, &avrc_response.get_items); - break; - case AVRC_PDU_CHANGE_PATH: - handle_change_path_response(pmeta_msg, &avrc_response.chg_path); - break; - case AVRC_PDU_SET_BROWSED_PLAYER: - handle_set_browsed_player_response(pmeta_msg, &avrc_response.br_player); - break; - default: - BTIF_TRACE_ERROR("%s cannot handle browse pdu %d", __func__, - pmeta_msg->p_msg->hdr.opcode); - } - } - else - { - BTIF_TRACE_DEBUG("%s: Invalid Vendor Command code: %d len: %d. Not processing it.", - __func__, pmeta_msg->code, pmeta_msg->len); - return; + case AVRC_PDU_GET_PLAY_STATUS: + handle_get_playstatus_response(pmeta_msg, + &avrc_response.get_play_status); + break; } - BTIF_TRACE_DEBUG("XX __func__ release transaction %d", pmeta_msg->label); - release_transaction(pmeta_msg->label); + } else if (AVRC_OP_BROWSE == pmeta_msg->p_msg->hdr.opcode) { + BTIF_TRACE_DEBUG("%s AVRC_OP_BROWSE pdu %d", __func__, avrc_response.pdu); + /* check what kind of command it is for browsing */ + switch (avrc_response.pdu) { + case AVRC_PDU_GET_FOLDER_ITEMS: + handle_get_folder_items_response(pmeta_msg, &avrc_response.get_items); + break; + case AVRC_PDU_CHANGE_PATH: + handle_change_path_response(pmeta_msg, &avrc_response.chg_path); + break; + case AVRC_PDU_SET_BROWSED_PLAYER: + handle_set_browsed_player_response(pmeta_msg, &avrc_response.br_player); + break; + default: + BTIF_TRACE_ERROR("%s cannot handle browse pdu %d", __func__, + pmeta_msg->p_msg->hdr.opcode); + } + } else { + BTIF_TRACE_DEBUG( + "%s: Invalid Vendor Command code: %d len: %d. Not processing it.", + __func__, pmeta_msg->code, pmeta_msg->len); + return; + } + BTIF_TRACE_DEBUG("XX __func__ release transaction %d", pmeta_msg->label); + release_transaction(pmeta_msg->label); } /*************************************************************************** -** -** Function handle_avk_rc_metamsg_cmd -** -** Description Handle RC metamessage response -** -** Returns void -** -***************************************************************************/ -static void handle_avk_rc_metamsg_cmd(tBTA_AV_META_MSG *pmeta_msg) -{ - tAVRC_COMMAND avrc_cmd = {0}; - tAVRC_STS status = BT_STATUS_UNSUPPORTED; - btif_rc_device_cb_t *p_dev = NULL; - - BTIF_TRACE_DEBUG("%s: opcode: %d rsp_code: %d",__func__, - pmeta_msg->p_msg->hdr.opcode,pmeta_msg->code); - status = AVRC_Ctrl_ParsCommand(pmeta_msg->p_msg, &avrc_cmd); - if ((AVRC_OP_VENDOR==pmeta_msg->p_msg->hdr.opcode)&& - (pmeta_msg->code <= AVRC_CMD_GEN_INQ)) - { - BTIF_TRACE_DEBUG("%s Received vendor command.code %d, PDU %d label %d", - __FUNCTION__, pmeta_msg->code, avrc_cmd.pdu, pmeta_msg->label); - - if (status != AVRC_STS_NO_ERROR) - { - /* return error */ - BTIF_TRACE_WARNING("%s: Error in parsing received metamsg command. status: 0x%02x", - __func__, status); - send_reject_response(pmeta_msg->rc_handle, pmeta_msg->label, avrc_cmd.pdu, status, - pmeta_msg->p_msg->hdr.opcode); - } - else - { - p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle); - if (p_dev == NULL) - { - BTIF_TRACE_ERROR("%s: avk rc meta msg cmd for Invalid rc handle", __func__); - return; - } + * + * Function handle_avk_rc_metamsg_cmd + * + * Description Handle RC metamessage response + * + * Returns void + * + **************************************************************************/ +static void handle_avk_rc_metamsg_cmd(tBTA_AV_META_MSG* pmeta_msg) { + tAVRC_COMMAND avrc_cmd = {0}; + tAVRC_STS status = BT_STATUS_UNSUPPORTED; + btif_rc_device_cb_t* p_dev = NULL; + + BTIF_TRACE_DEBUG("%s: opcode: %d rsp_code: %d", __func__, + pmeta_msg->p_msg->hdr.opcode, pmeta_msg->code); + status = AVRC_Ctrl_ParsCommand(pmeta_msg->p_msg, &avrc_cmd); + if ((AVRC_OP_VENDOR == pmeta_msg->p_msg->hdr.opcode) && + (pmeta_msg->code <= AVRC_CMD_GEN_INQ)) { + BTIF_TRACE_DEBUG("%s Received vendor command.code %d, PDU %d label %d", + __FUNCTION__, pmeta_msg->code, avrc_cmd.pdu, + pmeta_msg->label); + + if (status != AVRC_STS_NO_ERROR) { + /* return error */ + BTIF_TRACE_WARNING( + "%s: Error in parsing received metamsg command. status: 0x%02x", + __func__, status); + send_reject_response(pmeta_msg->rc_handle, pmeta_msg->label, avrc_cmd.pdu, + status, pmeta_msg->p_msg->hdr.opcode); + } else { + p_dev = btif_rc_get_device_by_handle(pmeta_msg->rc_handle); + if (p_dev == NULL) { + BTIF_TRACE_ERROR("%s: avk rc meta msg cmd for Invalid rc handle", + __func__); + return; + } - if (avrc_cmd.pdu == AVRC_PDU_REGISTER_NOTIFICATION) - { - uint8_t event_id = avrc_cmd.reg_notif.event_id; - BTIF_TRACE_EVENT("%s: Register notification event_id: %s", - __func__, dump_rc_notification_event_id(event_id)); - } - else if (avrc_cmd.pdu == AVRC_PDU_SET_ABSOLUTE_VOLUME) - { - BTIF_TRACE_EVENT("%s: Abs Volume Cmd Recvd", __func__); - } + if (avrc_cmd.pdu == AVRC_PDU_REGISTER_NOTIFICATION) { + uint8_t event_id = avrc_cmd.reg_notif.event_id; + BTIF_TRACE_EVENT("%s: Register notification event_id: %s", __func__, + dump_rc_notification_event_id(event_id)); + } else if (avrc_cmd.pdu == AVRC_PDU_SET_ABSOLUTE_VOLUME) { + BTIF_TRACE_EVENT("%s: Abs Volume Cmd Recvd", __func__); + } - btif_rc_ctrl_upstreams_rsp_cmd(avrc_cmd.pdu, &avrc_cmd, pmeta_msg->label, p_dev); - } - } - else - { - BTIF_TRACE_DEBUG("%s: Invalid Vendor Command code: %d len: %d. Not processing it.", - __func__, pmeta_msg->code, pmeta_msg->len); - return; + btif_rc_ctrl_upstreams_rsp_cmd(avrc_cmd.pdu, &avrc_cmd, pmeta_msg->label, + p_dev); } + } else { + BTIF_TRACE_DEBUG( + "%s: Invalid Vendor Command code: %d len: %d. Not processing it.", + __func__, pmeta_msg->code, pmeta_msg->len); + return; + } } #endif /*************************************************************************** -** -** Function cleanup -** -** Description Closes the AVRC interface -** -** Returns void -** -***************************************************************************/ -static void cleanup() -{ - BTIF_TRACE_EVENT("%s: ", __func__); - close_uinput(); - if (bt_rc_callbacks) - { - bt_rc_callbacks = NULL; - } - - for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) { - alarm_free(btif_rc_cb.rc_multi_cb[idx].rc_play_status_timer); - memset(&btif_rc_cb.rc_multi_cb[idx], 0, sizeof(btif_rc_cb.rc_multi_cb[idx])); - } - - BTIF_TRACE_EVENT("%s: completed", __func__); + * + * Function cleanup + * + * Description Closes the AVRC interface + * + * Returns void + * + **************************************************************************/ +static void cleanup() { + BTIF_TRACE_EVENT("%s: ", __func__); + close_uinput(); + if (bt_rc_callbacks) { + bt_rc_callbacks = NULL; + } + + for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) { + alarm_free(btif_rc_cb.rc_multi_cb[idx].rc_play_status_timer); + memset(&btif_rc_cb.rc_multi_cb[idx], 0, + sizeof(btif_rc_cb.rc_multi_cb[idx])); + } + + BTIF_TRACE_EVENT("%s: completed", __func__); } /*************************************************************************** -** -** Function cleanup_ctrl -** -** Description Closes the AVRC Controller interface -** -** Returns void -** -***************************************************************************/ -static void cleanup_ctrl() -{ - BTIF_TRACE_EVENT("%s: ", __func__); - - if (bt_rc_ctrl_callbacks) - { - bt_rc_ctrl_callbacks = NULL; - } - - for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) { - alarm_free(btif_rc_cb.rc_multi_cb[idx].rc_play_status_timer); - memset(&btif_rc_cb.rc_multi_cb[idx], 0, sizeof(btif_rc_cb.rc_multi_cb[idx])); - } - - memset(&btif_rc_cb.rc_multi_cb, 0, sizeof(btif_rc_cb.rc_multi_cb)); - BTIF_TRACE_EVENT("%s: completed", __func__); + * + * Function cleanup_ctrl + * + * Description Closes the AVRC Controller interface + * + * Returns void + * + **************************************************************************/ +static void cleanup_ctrl() { + BTIF_TRACE_EVENT("%s: ", __func__); + + if (bt_rc_ctrl_callbacks) { + bt_rc_ctrl_callbacks = NULL; + } + + for (int idx = 0; idx < BTIF_RC_NUM_CONN; idx++) { + alarm_free(btif_rc_cb.rc_multi_cb[idx].rc_play_status_timer); + memset(&btif_rc_cb.rc_multi_cb[idx], 0, + sizeof(btif_rc_cb.rc_multi_cb[idx])); + } + + memset(&btif_rc_cb.rc_multi_cb, 0, sizeof(btif_rc_cb.rc_multi_cb)); + BTIF_TRACE_EVENT("%s: completed", __func__); } /*************************************************************************** -** -** Function getcapabilities_cmd -** -** Description GetCapabilties from Remote(Company_ID, Events_Supported) -** -** Returns void -** -***************************************************************************/ -static bt_status_t getcapabilities_cmd (uint8_t cap_id, btif_rc_device_cb_t *p_dev) -{ - tAVRC_STS status = BT_STATUS_UNSUPPORTED; - rc_transaction_t *p_transaction = NULL; + * + * Function getcapabilities_cmd + * + * Description GetCapabilties from Remote(Company_ID, Events_Supported) + * + * Returns void + * + **************************************************************************/ +static bt_status_t getcapabilities_cmd(uint8_t cap_id, + btif_rc_device_cb_t* p_dev) { + tAVRC_STS status = BT_STATUS_UNSUPPORTED; + rc_transaction_t* p_transaction = NULL; #if (AVRC_CTRL_INCLUDED == TRUE) - BTIF_TRACE_DEBUG("%s: cap_id: %d", __func__, cap_id); - - CHECK_RC_CONNECTED(p_dev); - - bt_status_t tran_status=get_transaction(&p_transaction); - if (BT_STATUS_SUCCESS != tran_status) - return BT_STATUS_FAIL; - - tAVRC_COMMAND avrc_cmd = {0}; - BT_HDR *p_msg = NULL; - avrc_cmd.get_caps.opcode = AVRC_OP_VENDOR; - avrc_cmd.get_caps.capability_id = cap_id; - avrc_cmd.get_caps.pdu = AVRC_PDU_GET_CAPABILITIES; - avrc_cmd.get_caps.status = AVRC_STS_NO_ERROR; - status = AVRC_BldCommand(&avrc_cmd, &p_msg); - if ((status == AVRC_STS_NO_ERROR)&&(p_msg != NULL)) - { - uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; - BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", - __func__,p_transaction->lbl); - BTA_AvVendorCmd(p_dev->rc_handle,p_transaction->lbl,AVRC_CMD_STATUS, - data_start, p_msg->len); - status = BT_STATUS_SUCCESS; - start_status_command_timer (AVRC_PDU_GET_CAPABILITIES, p_transaction, p_dev); - } - else - { - BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", - __func__, status); - } - osi_free(p_msg); + BTIF_TRACE_DEBUG("%s: cap_id: %d", __func__, cap_id); + + CHECK_RC_CONNECTED(p_dev); + + bt_status_t tran_status = get_transaction(&p_transaction); + if (BT_STATUS_SUCCESS != tran_status) return BT_STATUS_FAIL; + + tAVRC_COMMAND avrc_cmd = {0}; + BT_HDR* p_msg = NULL; + avrc_cmd.get_caps.opcode = AVRC_OP_VENDOR; + avrc_cmd.get_caps.capability_id = cap_id; + avrc_cmd.get_caps.pdu = AVRC_PDU_GET_CAPABILITIES; + avrc_cmd.get_caps.status = AVRC_STS_NO_ERROR; + status = AVRC_BldCommand(&avrc_cmd, &p_msg); + if ((status == AVRC_STS_NO_ERROR) && (p_msg != NULL)) { + uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; + BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", __func__, + p_transaction->lbl); + BTA_AvVendorCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_STATUS, + data_start, p_msg->len); + status = BT_STATUS_SUCCESS; + start_status_command_timer(AVRC_PDU_GET_CAPABILITIES, p_transaction, p_dev); + } else { + BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", __func__, + status); + } + osi_free(p_msg); #else - BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); + BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); #endif - return (bt_status_t)status; + return (bt_status_t)status; } /*************************************************************************** -** -** Function list_player_app_setting_attrib_cmd -** -** Description Get supported List Player Attributes -** -** Returns void -** -***************************************************************************/ -static bt_status_t list_player_app_setting_attrib_cmd(btif_rc_device_cb_t *p_dev) -{ - tAVRC_STS status = BT_STATUS_UNSUPPORTED; - rc_transaction_t *p_transaction = NULL; + * + * Function list_player_app_setting_attrib_cmd + * + * Description Get supported List Player Attributes + * + * Returns void + * + **************************************************************************/ +static bt_status_t list_player_app_setting_attrib_cmd( + btif_rc_device_cb_t* p_dev) { + tAVRC_STS status = BT_STATUS_UNSUPPORTED; + rc_transaction_t* p_transaction = NULL; #if (AVRC_CTRL_INCLUDED == TRUE) - BTIF_TRACE_DEBUG("%s: ", __func__); - - CHECK_RC_CONNECTED(p_dev); - - bt_status_t tran_status=get_transaction(&p_transaction); - if (BT_STATUS_SUCCESS != tran_status) - return BT_STATUS_FAIL; - - tAVRC_COMMAND avrc_cmd = {0}; - BT_HDR *p_msg = NULL; - avrc_cmd.list_app_attr.opcode = AVRC_OP_VENDOR; - avrc_cmd.list_app_attr.pdu = AVRC_PDU_LIST_PLAYER_APP_ATTR; - avrc_cmd.list_app_attr.status = AVRC_STS_NO_ERROR; - status = AVRC_BldCommand(&avrc_cmd, &p_msg); - if ((status == AVRC_STS_NO_ERROR)&&(p_msg != NULL)) - { - uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; - BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", - __func__, p_transaction->lbl); - BTA_AvVendorCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_STATUS, - data_start, p_msg->len); - status = BT_STATUS_SUCCESS; - start_status_command_timer (AVRC_PDU_LIST_PLAYER_APP_ATTR, p_transaction, p_dev); - } - else - { - - BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", - __func__, status); - } - osi_free(p_msg); + BTIF_TRACE_DEBUG("%s: ", __func__); + + CHECK_RC_CONNECTED(p_dev); + + bt_status_t tran_status = get_transaction(&p_transaction); + if (BT_STATUS_SUCCESS != tran_status) return BT_STATUS_FAIL; + + tAVRC_COMMAND avrc_cmd = {0}; + BT_HDR* p_msg = NULL; + avrc_cmd.list_app_attr.opcode = AVRC_OP_VENDOR; + avrc_cmd.list_app_attr.pdu = AVRC_PDU_LIST_PLAYER_APP_ATTR; + avrc_cmd.list_app_attr.status = AVRC_STS_NO_ERROR; + status = AVRC_BldCommand(&avrc_cmd, &p_msg); + if ((status == AVRC_STS_NO_ERROR) && (p_msg != NULL)) { + uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; + BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", __func__, + p_transaction->lbl); + BTA_AvVendorCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_STATUS, + data_start, p_msg->len); + status = BT_STATUS_SUCCESS; + start_status_command_timer(AVRC_PDU_LIST_PLAYER_APP_ATTR, p_transaction, + p_dev); + } else { + BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", __func__, + status); + } + osi_free(p_msg); #else - BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); + BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); #endif - return (bt_status_t)status; + return (bt_status_t)status; } /*************************************************************************** -** -** Function list_player_app_setting_value_cmd -** -** Description Get values of supported Player Attributes -** -** Returns void -** -***************************************************************************/ -static bt_status_t list_player_app_setting_value_cmd(uint8_t attrib_id, - btif_rc_device_cb_t *p_dev) -{ - tAVRC_STS status = BT_STATUS_UNSUPPORTED; - rc_transaction_t *p_transaction=NULL; + * + * Function list_player_app_setting_value_cmd + * + * Description Get values of supported Player Attributes + * + * Returns void + * + **************************************************************************/ +static bt_status_t list_player_app_setting_value_cmd( + uint8_t attrib_id, btif_rc_device_cb_t* p_dev) { + tAVRC_STS status = BT_STATUS_UNSUPPORTED; + rc_transaction_t* p_transaction = NULL; #if (AVRC_CTRL_INCLUDED == TRUE) - BTIF_TRACE_DEBUG("%s: attrib_id: %d", __func__, attrib_id); - - CHECK_RC_CONNECTED(p_dev); - - bt_status_t tran_status=get_transaction(&p_transaction); - if (BT_STATUS_SUCCESS != tran_status) - return BT_STATUS_FAIL; - - tAVRC_COMMAND avrc_cmd = {0}; - BT_HDR *p_msg = NULL; - avrc_cmd.list_app_values.attr_id = attrib_id; - avrc_cmd.list_app_values.opcode = AVRC_OP_VENDOR; - avrc_cmd.list_app_values.pdu = AVRC_PDU_LIST_PLAYER_APP_VALUES; - avrc_cmd.list_app_values.status = AVRC_STS_NO_ERROR; - status = AVRC_BldCommand(&avrc_cmd, &p_msg); - if ((status == AVRC_STS_NO_ERROR) && (p_msg != NULL)) - { - uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; - BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", - __func__,p_transaction->lbl); - BTA_AvVendorCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_STATUS, - data_start, p_msg->len); - status = BT_STATUS_SUCCESS; - start_status_command_timer (AVRC_PDU_LIST_PLAYER_APP_VALUES, p_transaction, p_dev); - } - else - { - BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", __func__, status); - } - osi_free(p_msg); + BTIF_TRACE_DEBUG("%s: attrib_id: %d", __func__, attrib_id); + + CHECK_RC_CONNECTED(p_dev); + + bt_status_t tran_status = get_transaction(&p_transaction); + if (BT_STATUS_SUCCESS != tran_status) return BT_STATUS_FAIL; + + tAVRC_COMMAND avrc_cmd = {0}; + BT_HDR* p_msg = NULL; + avrc_cmd.list_app_values.attr_id = attrib_id; + avrc_cmd.list_app_values.opcode = AVRC_OP_VENDOR; + avrc_cmd.list_app_values.pdu = AVRC_PDU_LIST_PLAYER_APP_VALUES; + avrc_cmd.list_app_values.status = AVRC_STS_NO_ERROR; + status = AVRC_BldCommand(&avrc_cmd, &p_msg); + if ((status == AVRC_STS_NO_ERROR) && (p_msg != NULL)) { + uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; + BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", __func__, + p_transaction->lbl); + BTA_AvVendorCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_STATUS, + data_start, p_msg->len); + status = BT_STATUS_SUCCESS; + start_status_command_timer(AVRC_PDU_LIST_PLAYER_APP_VALUES, p_transaction, + p_dev); + } else { + BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", __func__, + status); + } + osi_free(p_msg); #else - BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); + BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); #endif - return (bt_status_t)status; + return (bt_status_t)status; } /*************************************************************************** -** -** Function get_player_app_setting_cmd -** -** Description Get current values of Player Attributes -** -** Returns void -** -***************************************************************************/ -static bt_status_t get_player_app_setting_cmd(uint8_t num_attrib, uint8_t* attrib_ids, - btif_rc_device_cb_t *p_dev) -{ - tAVRC_STS status = BT_STATUS_UNSUPPORTED; - rc_transaction_t *p_transaction = NULL; - int count = 0; + * + * Function get_player_app_setting_cmd + * + * Description Get current values of Player Attributes + * + * Returns void + * + **************************************************************************/ +static bt_status_t get_player_app_setting_cmd(uint8_t num_attrib, + uint8_t* attrib_ids, + btif_rc_device_cb_t* p_dev) { + tAVRC_STS status = BT_STATUS_UNSUPPORTED; + rc_transaction_t* p_transaction = NULL; + int count = 0; #if (AVRC_CTRL_INCLUDED == TRUE) - BTIF_TRACE_DEBUG("%s: num attrib_id: %d", __func__, num_attrib); - - CHECK_RC_CONNECTED(p_dev); - - bt_status_t tran_status=get_transaction(&p_transaction); - if (BT_STATUS_SUCCESS != tran_status) - return BT_STATUS_FAIL; - - tAVRC_COMMAND avrc_cmd = {0}; - BT_HDR *p_msg = NULL; - avrc_cmd.get_cur_app_val.opcode = AVRC_OP_VENDOR; - avrc_cmd.get_cur_app_val.status = AVRC_STS_NO_ERROR; - avrc_cmd.get_cur_app_val.num_attr = num_attrib; - avrc_cmd.get_cur_app_val.pdu = AVRC_PDU_GET_CUR_PLAYER_APP_VALUE; - - for (count = 0; count < num_attrib; count++) - { - avrc_cmd.get_cur_app_val.attrs[count] = attrib_ids[count]; - } - status = AVRC_BldCommand(&avrc_cmd, &p_msg); - if ((status == AVRC_STS_NO_ERROR) && (p_msg != NULL)) - { - uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; - BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", - __func__, p_transaction->lbl); - BTA_AvVendorCmd(p_dev->rc_handle,p_transaction->lbl,AVRC_CMD_STATUS, - data_start, p_msg->len); - status = BT_STATUS_SUCCESS; - start_status_command_timer (AVRC_PDU_GET_CUR_PLAYER_APP_VALUE, p_transaction, p_dev); - } - else - { - BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", - __func__, status); - } - osi_free(p_msg); + BTIF_TRACE_DEBUG("%s: num attrib_id: %d", __func__, num_attrib); + + CHECK_RC_CONNECTED(p_dev); + + bt_status_t tran_status = get_transaction(&p_transaction); + if (BT_STATUS_SUCCESS != tran_status) return BT_STATUS_FAIL; + + tAVRC_COMMAND avrc_cmd = {0}; + BT_HDR* p_msg = NULL; + avrc_cmd.get_cur_app_val.opcode = AVRC_OP_VENDOR; + avrc_cmd.get_cur_app_val.status = AVRC_STS_NO_ERROR; + avrc_cmd.get_cur_app_val.num_attr = num_attrib; + avrc_cmd.get_cur_app_val.pdu = AVRC_PDU_GET_CUR_PLAYER_APP_VALUE; + + for (count = 0; count < num_attrib; count++) { + avrc_cmd.get_cur_app_val.attrs[count] = attrib_ids[count]; + } + status = AVRC_BldCommand(&avrc_cmd, &p_msg); + if ((status == AVRC_STS_NO_ERROR) && (p_msg != NULL)) { + uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; + BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", __func__, + p_transaction->lbl); + BTA_AvVendorCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_STATUS, + data_start, p_msg->len); + status = BT_STATUS_SUCCESS; + start_status_command_timer(AVRC_PDU_GET_CUR_PLAYER_APP_VALUE, p_transaction, + p_dev); + } else { + BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", __func__, + status); + } + osi_free(p_msg); #else - BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); + BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); #endif - return (bt_status_t)status; + return (bt_status_t)status; } /*************************************************************************** -** -** Function get_now_playing_list_cmd -** -** Description Fetch the now playing list -** -** Paramters start_item: First item to fetch (0 to fetch from beganning) -** end_item: Last item to fetch (0xff to fetch until end) -** -** Returns BT_STATUS_SUCCESS if command issued successfully otherwise -** BT_STATUS_FAIL. -** -***************************************************************************/ -static bt_status_t get_now_playing_list_cmd(bt_bdaddr_t *bd_addr, uint8_t start_item, + * + * Function get_now_playing_list_cmd + * + * Description Fetch the now playing list + * + * Paramters start_item: First item to fetch (0 to fetch from beganning) + * end_item: Last item to fetch (0xff to fetch until end) + * + * Returns BT_STATUS_SUCCESS if command issued successfully otherwise + * BT_STATUS_FAIL. + * + **************************************************************************/ +static bt_status_t get_now_playing_list_cmd(bt_bdaddr_t* bd_addr, + uint8_t start_item, uint8_t num_items) { - BTIF_TRACE_DEBUG("%s start, end: (%d, %d)", __func__, start_item, num_items); + BTIF_TRACE_DEBUG("%s start, end: (%d, %d)", __func__, start_item, num_items); #if (AVRC_CTRL_INCLUDED == TRUE) - return get_folder_items_cmd(bd_addr, AVRC_SCOPE_NOW_PLAYING, start_item, num_items); + return get_folder_items_cmd(bd_addr, AVRC_SCOPE_NOW_PLAYING, start_item, + num_items); #else - BTIF_TRACE_ERROR("%s AVRCP controller role is not enabled", __func__); - return BT_STATUS_FAIL; + BTIF_TRACE_ERROR("%s AVRCP controller role is not enabled", __func__); + return BT_STATUS_FAIL; #endif } /*************************************************************************** -** -** Function get_folder_list_cmd -** -** Description Fetch the currently selected folder list -** -** Paramters start_item: First item to fetch (0 to fetch from beganning) -** end_item: Last item to fetch (0xff to fetch until end) -** -** Returns BT_STATUS_SUCCESS if command issued successfully otherwise -** BT_STATUS_FAIL. -** -***************************************************************************/ -static bt_status_t get_folder_list_cmd(bt_bdaddr_t *bd_addr, uint8_t start_item, - uint8_t num_items) { - BTIF_TRACE_DEBUG("%s start, end: (%d, %d)", __func__, start_item, num_items); + * + * Function get_folder_list_cmd + * + * Description Fetch the currently selected folder list + * + * Paramters start_item: First item to fetch (0 to fetch from beganning) + * end_item: Last item to fetch (0xff to fetch until end) + * + * Returns BT_STATUS_SUCCESS if command issued successfully otherwise + * BT_STATUS_FAIL. + * + **************************************************************************/ +static bt_status_t get_folder_list_cmd(bt_bdaddr_t* bd_addr, uint8_t start_item, + uint8_t num_items) { + BTIF_TRACE_DEBUG("%s start, end: (%d, %d)", __func__, start_item, num_items); #if (AVRC_CTRL_INCLUDED == TRUE) - return get_folder_items_cmd(bd_addr, AVRC_SCOPE_FILE_SYSTEM, start_item, num_items); + return get_folder_items_cmd(bd_addr, AVRC_SCOPE_FILE_SYSTEM, start_item, + num_items); #else - BTIF_TRACE_ERROR("%s AVRCP controller role is not enabled", __func__); - return BT_STATUS_FAIL; + BTIF_TRACE_ERROR("%s AVRCP controller role is not enabled", __func__); + return BT_STATUS_FAIL; #endif } /*************************************************************************** -** -** Function get_player_list_cmd -** -** Description Fetch the player list -** -** Paramters start_item: First item to fetch (0 to fetch from beganning) -** end_item: Last item to fetch (0xff to fetch until end) -** -** Returns BT_STATUS_SUCCESS if command issued successfully otherwise -** BT_STATUS_FAIL. -** -***************************************************************************/ -static bt_status_t get_player_list_cmd(bt_bdaddr_t *bd_addr, uint8_t start_item, - uint8_t num_items) { - BTIF_TRACE_DEBUG("%s start, end: (%d, %d)", __func__, start_item, num_items); + * + * Function get_player_list_cmd + * + * Description Fetch the player list + * + * Paramters start_item: First item to fetch (0 to fetch from beganning) + * end_item: Last item to fetch (0xff to fetch until end) + * + * Returns BT_STATUS_SUCCESS if command issued successfully otherwise + * BT_STATUS_FAIL. + * + **************************************************************************/ +static bt_status_t get_player_list_cmd(bt_bdaddr_t* bd_addr, uint8_t start_item, + uint8_t num_items) { + BTIF_TRACE_DEBUG("%s start, end: (%d, %d)", __func__, start_item, num_items); #if (AVRC_CTRL_INCLUDED == TRUE) - return get_folder_items_cmd(bd_addr, AVRC_SCOPE_PLAYER_LIST, start_item, num_items); + return get_folder_items_cmd(bd_addr, AVRC_SCOPE_PLAYER_LIST, start_item, + num_items); #else - BTIF_TRACE_ERROR("%s AVRCP controller role is not enabled", __func__); - return BT_STATUS_FAIL; + BTIF_TRACE_ERROR("%s AVRCP controller role is not enabled", __func__); + return BT_STATUS_FAIL; #endif } /*************************************************************************** -** -** Function change_folder_path_cmd -** -** Description Change the folder. -** -** Paramters direction: Direction (Up/Down) to change folder -** uid: The UID of folder to move to -** start_item: First item to fetch (0 to fetch from beganning) -** end_item: Last item to fetch (0xff to fetch until end) -** -** Returns BT_STATUS_SUCCESS if command issued successfully otherwise -** BT_STATUS_FAIL. -** -***************************************************************************/ -static bt_status_t change_folder_path_cmd(bt_bdaddr_t *bd_addr, uint8_t direction, uint8_t *uid) -{ - BTIF_TRACE_DEBUG("%s direction (%d)", __func__, direction); + * + * Function change_folder_path_cmd + * + * Description Change the folder. + * + * Paramters direction: Direction (Up/Down) to change folder + * uid: The UID of folder to move to + * start_item: First item to fetch (0 to fetch from beganning) + * end_item: Last item to fetch (0xff to fetch until end) + * + * Returns BT_STATUS_SUCCESS if command issued successfully otherwise + * BT_STATUS_FAIL. + * + **************************************************************************/ +static bt_status_t change_folder_path_cmd(bt_bdaddr_t* bd_addr, + uint8_t direction, uint8_t* uid) { + BTIF_TRACE_DEBUG("%s direction (%d)", __func__, direction); #if (AVRC_CTRL_INCLUDED == TRUE) - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); - - CHECK_RC_CONNECTED(p_dev); - CHECK_BR_CONNECTED(p_dev); - - tAVRC_STS status = BT_STATUS_UNSUPPORTED; - rc_transaction_t *p_transaction = NULL; - - if (p_dev->br_connected) - { - tAVRC_COMMAND avrc_cmd = {0}; - BT_HDR *p_msg = NULL; + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr); - avrc_cmd.chg_path.pdu = AVRC_PDU_CHANGE_PATH; - avrc_cmd.chg_path.status = AVRC_STS_NO_ERROR; - // TODO(sanketa): Improve for database aware clients. - avrc_cmd.chg_path.uid_counter = 0; - avrc_cmd.chg_path.direction = direction; + CHECK_RC_CONNECTED(p_dev); + CHECK_BR_CONNECTED(p_dev); - memset(avrc_cmd.chg_path.folder_uid, 0, AVRC_UID_SIZE * sizeof (uint8_t)); - memcpy(avrc_cmd.chg_path.folder_uid, uid, AVRC_UID_SIZE * sizeof (uint8_t)); + tAVRC_STS status = BT_STATUS_UNSUPPORTED; + rc_transaction_t* p_transaction = NULL; - if (AVRC_BldCommand(&avrc_cmd, &p_msg) == AVRC_STS_NO_ERROR) - { - bt_status_t tran_status = get_transaction(&p_transaction); - if (BT_STATUS_SUCCESS == tran_status && p_transaction != NULL) - { - BTIF_TRACE_DEBUG("%s msgreq being sent out with label %d", - __func__, p_transaction->lbl); - BTA_AvMetaCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_CTRL, p_msg); - status = BT_STATUS_SUCCESS; - } - else - { - osi_free(p_msg); - BTIF_TRACE_ERROR("%s: failed to obtain transaction details. status: 0x%02x", - __func__, tran_status); - status = BT_STATUS_FAIL; - } - } - else - { - BTIF_TRACE_ERROR("%s failed to build command status %d", __func__, status); - status = BT_STATUS_FAIL; - } - } - else - { - BTIF_TRACE_ERROR("%s command not supported by peer features %d", - __func__, p_dev->rc_features); + if (p_dev->br_connected) { + tAVRC_COMMAND avrc_cmd = {0}; + BT_HDR* p_msg = NULL; + + avrc_cmd.chg_path.pdu = AVRC_PDU_CHANGE_PATH; + avrc_cmd.chg_path.status = AVRC_STS_NO_ERROR; + // TODO(sanketa): Improve for database aware clients. + avrc_cmd.chg_path.uid_counter = 0; + avrc_cmd.chg_path.direction = direction; + + memset(avrc_cmd.chg_path.folder_uid, 0, AVRC_UID_SIZE * sizeof(uint8_t)); + memcpy(avrc_cmd.chg_path.folder_uid, uid, AVRC_UID_SIZE * sizeof(uint8_t)); + + if (AVRC_BldCommand(&avrc_cmd, &p_msg) == AVRC_STS_NO_ERROR) { + bt_status_t tran_status = get_transaction(&p_transaction); + if (BT_STATUS_SUCCESS == tran_status && p_transaction != NULL) { + BTIF_TRACE_DEBUG("%s msgreq being sent out with label %d", __func__, + p_transaction->lbl); + BTA_AvMetaCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_CTRL, + p_msg); + status = BT_STATUS_SUCCESS; + } else { + osi_free(p_msg); + BTIF_TRACE_ERROR( + "%s: failed to obtain transaction details. status: 0x%02x", + __func__, tran_status); status = BT_STATUS_FAIL; - } - return (bt_status_t) status; + } + } else { + BTIF_TRACE_ERROR("%s failed to build command status %d", __func__, + status); + status = BT_STATUS_FAIL; + } + } else { + BTIF_TRACE_ERROR("%s command not supported by peer features %d", __func__, + p_dev->rc_features); + status = BT_STATUS_FAIL; + } + return (bt_status_t)status; #else - BTIF_TRACE_ERROR("%s AVRCP controller role is not enabled", __func__); - return BT_STATUS_FAIL; + BTIF_TRACE_ERROR("%s AVRCP controller role is not enabled", __func__); + return BT_STATUS_FAIL; #endif } /*************************************************************************** -** -** Function set_browsed_player_cmd -** -** Description Change the browsed player. -** -** Paramters id: The UID of player to move to -** -** Returns BT_STATUS_SUCCESS if command issued successfully otherwise -** BT_STATUS_FAIL. -** -***************************************************************************/ -static bt_status_t set_browsed_player_cmd(bt_bdaddr_t *bd_addr, uint16_t id) -{ - BTIF_TRACE_DEBUG("%s id (%d)", __func__, id); + * + * Function set_browsed_player_cmd + * + * Description Change the browsed player. + * + * Paramters id: The UID of player to move to + * + * Returns BT_STATUS_SUCCESS if command issued successfully otherwise + * BT_STATUS_FAIL. + * + **************************************************************************/ +static bt_status_t set_browsed_player_cmd(bt_bdaddr_t* bd_addr, uint16_t id) { + BTIF_TRACE_DEBUG("%s id (%d)", __func__, id); #if (AVRC_CTRL_INCLUDED == TRUE) - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); - CHECK_RC_CONNECTED(p_dev); - CHECK_BR_CONNECTED(p_dev); - - tAVRC_STS status = BT_STATUS_UNSUPPORTED; - rc_transaction_t *p_transaction = NULL; + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr); + CHECK_RC_CONNECTED(p_dev); + CHECK_BR_CONNECTED(p_dev); - if (p_dev->br_connected) - { - tAVRC_COMMAND avrc_cmd = {0}; - BT_HDR *p_msg = NULL; + tAVRC_STS status = BT_STATUS_UNSUPPORTED; + rc_transaction_t* p_transaction = NULL; - avrc_cmd.br_player.pdu = AVRC_PDU_SET_BROWSED_PLAYER; - avrc_cmd.br_player.status = AVRC_STS_NO_ERROR; - // TODO(sanketa): Improve for database aware clients. - avrc_cmd.br_player.player_id = id; - - if (AVRC_BldCommand(&avrc_cmd, &p_msg) == AVRC_STS_NO_ERROR) - { - bt_status_t tran_status = get_transaction(&p_transaction); - if (BT_STATUS_SUCCESS == tran_status && p_transaction != NULL) - { - BTIF_TRACE_DEBUG("%s msgreq being sent out with label %d", - __func__, p_transaction->lbl); - BTA_AvMetaCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_CTRL, p_msg); - status = BT_STATUS_SUCCESS; - } - else - { - osi_free(p_msg); - BTIF_TRACE_ERROR("%s: failed to obtain transaction details. status: 0x%02x", - __func__, tran_status); - status = BT_STATUS_FAIL; - } - } - else - { - BTIF_TRACE_ERROR("%s failed to build command status %d", __func__, status); - status = BT_STATUS_FAIL; - } - } - else - { - BTIF_TRACE_ERROR("%s command not supported by peer features %d", - __func__, p_dev->rc_features); + if (p_dev->br_connected) { + tAVRC_COMMAND avrc_cmd = {0}; + BT_HDR* p_msg = NULL; + + avrc_cmd.br_player.pdu = AVRC_PDU_SET_BROWSED_PLAYER; + avrc_cmd.br_player.status = AVRC_STS_NO_ERROR; + // TODO(sanketa): Improve for database aware clients. + avrc_cmd.br_player.player_id = id; + + if (AVRC_BldCommand(&avrc_cmd, &p_msg) == AVRC_STS_NO_ERROR) { + bt_status_t tran_status = get_transaction(&p_transaction); + if (BT_STATUS_SUCCESS == tran_status && p_transaction != NULL) { + BTIF_TRACE_DEBUG("%s msgreq being sent out with label %d", __func__, + p_transaction->lbl); + BTA_AvMetaCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_CTRL, + p_msg); + status = BT_STATUS_SUCCESS; + } else { + osi_free(p_msg); + BTIF_TRACE_ERROR( + "%s: failed to obtain transaction details. status: 0x%02x", + __func__, tran_status); status = BT_STATUS_FAIL; - } - return (bt_status_t) status; + } + } else { + BTIF_TRACE_ERROR("%s failed to build command status %d", __func__, + status); + status = BT_STATUS_FAIL; + } + } else { + BTIF_TRACE_ERROR("%s command not supported by peer features %d", __func__, + p_dev->rc_features); + status = BT_STATUS_FAIL; + } + return (bt_status_t)status; #else - BTIF_TRACE_ERROR("%s AVRCP controller role is not enabled", __func__); - return BT_STATUS_FAIL; + BTIF_TRACE_ERROR("%s AVRCP controller role is not enabled", __func__); + return BT_STATUS_FAIL; #endif } #if (AVRC_CTRL_INCLUDED == TRUE) /*************************************************************************** -** -** Function get_folder_items_cmd -** -** Description Helper function to browse the content hierarchy of the -** TG device. -** -** Paramters scope: AVRC_SCOPE_NOW_PLAYING (etc) for various browseable -** content -** start_item: First item to fetch (0 to fetch from beganning) -** end_item: Last item to fetch (0xff to fetch until end) -** -** Returns BT_STATUS_SUCCESS if command issued successfully otherwise -** BT_STATUS_FAIL. -** -***************************************************************************/ -static bt_status_t get_folder_items_cmd(bt_bdaddr_t *bd_addr, uint8_t scope, uint8_t start_item, - uint8_t num_items) -{ - BTIF_TRACE_DEBUG("%s", __func__); - /* Check that both avrcp and browse channel are connected. */ - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); - CHECK_RC_CONNECTED(p_dev); - CHECK_BR_CONNECTED(p_dev); - - tAVRC_STS status = BT_STATUS_UNSUPPORTED; - rc_transaction_t *p_transaction=NULL; - - if (p_dev->br_connected) { - tAVRC_COMMAND avrc_cmd = {0}; - BT_HDR *p_msg = NULL; + * + * Function get_folder_items_cmd + * + * Description Helper function to browse the content hierarchy of the + * TG device. + * + * Paramters scope: AVRC_SCOPE_NOW_PLAYING (etc) for various browseable + * content + * start_item: First item to fetch (0 to fetch from beganning) + * end_item: Last item to fetch (0xff to fetch until end) + * + * Returns BT_STATUS_SUCCESS if command issued successfully otherwise + * BT_STATUS_FAIL. + * + **************************************************************************/ +static bt_status_t get_folder_items_cmd(bt_bdaddr_t* bd_addr, uint8_t scope, + uint8_t start_item, uint8_t num_items) { + BTIF_TRACE_DEBUG("%s", __func__); + /* Check that both avrcp and browse channel are connected. */ + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr); + CHECK_RC_CONNECTED(p_dev); + CHECK_BR_CONNECTED(p_dev); + + tAVRC_STS status = BT_STATUS_UNSUPPORTED; + rc_transaction_t* p_transaction = NULL; + + if (p_dev->br_connected) { + tAVRC_COMMAND avrc_cmd = {0}; + BT_HDR* p_msg = NULL; - /* Set the layer specific to point to browse although this should really - * be done by lower layers and looking at the PDU - */ - avrc_cmd.get_items.pdu = AVRC_PDU_GET_FOLDER_ITEMS; - avrc_cmd.get_items.status = AVRC_STS_NO_ERROR; - avrc_cmd.get_items.scope = scope; - avrc_cmd.get_items.start_item = start_item; - avrc_cmd.get_items.end_item = (start_item + num_items - 1); - avrc_cmd.get_items.attr_count = 0; /* p_attr_list does not matter hence */ - - if (AVRC_BldCommand(&avrc_cmd, &p_msg) == AVRC_STS_NO_ERROR) - { - bt_status_t tran_status = get_transaction(&p_transaction); - if (BT_STATUS_SUCCESS == tran_status && p_transaction != NULL) - { - BTIF_TRACE_DEBUG("%s msgreq being sent out with label %d", - __func__, p_transaction->lbl); - BTA_AvMetaCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_CTRL, p_msg); - status = BT_STATUS_SUCCESS; - } - else - { - osi_free(p_msg); - BTIF_TRACE_ERROR("%s: failed to obtain transaction details. status: 0x%02x", - __FUNCTION__, tran_status); - status = BT_STATUS_FAIL; - } - } - else - { - BTIF_TRACE_ERROR("%s failed to build command status %d", __func__, status); - status = BT_STATUS_FAIL; - } - } else { - BTIF_TRACE_ERROR("%s command not supported by peer features %d", - __func__, p_dev->rc_features); + /* Set the layer specific to point to browse although this should really + * be done by lower layers and looking at the PDU + */ + avrc_cmd.get_items.pdu = AVRC_PDU_GET_FOLDER_ITEMS; + avrc_cmd.get_items.status = AVRC_STS_NO_ERROR; + avrc_cmd.get_items.scope = scope; + avrc_cmd.get_items.start_item = start_item; + avrc_cmd.get_items.end_item = (start_item + num_items - 1); + avrc_cmd.get_items.attr_count = 0; /* p_attr_list does not matter hence */ + + if (AVRC_BldCommand(&avrc_cmd, &p_msg) == AVRC_STS_NO_ERROR) { + bt_status_t tran_status = get_transaction(&p_transaction); + if (BT_STATUS_SUCCESS == tran_status && p_transaction != NULL) { + BTIF_TRACE_DEBUG("%s msgreq being sent out with label %d", __func__, + p_transaction->lbl); + BTA_AvMetaCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_CTRL, + p_msg); + status = BT_STATUS_SUCCESS; + } else { + osi_free(p_msg); + BTIF_TRACE_ERROR( + "%s: failed to obtain transaction details. status: 0x%02x", + __FUNCTION__, tran_status); status = BT_STATUS_FAIL; - } - return (bt_status_t) status; + } + } else { + BTIF_TRACE_ERROR("%s failed to build command status %d", __func__, + status); + status = BT_STATUS_FAIL; + } + } else { + BTIF_TRACE_ERROR("%s command not supported by peer features %d", __func__, + p_dev->rc_features); + status = BT_STATUS_FAIL; + } + return (bt_status_t)status; } #endif /*************************************************************************** -** -** Function change_player_app_setting -** -** Description Set current values of Player Attributes -** -** Returns void -** -***************************************************************************/ -static bt_status_t change_player_app_setting(bt_bdaddr_t *bd_addr, uint8_t num_attrib, - uint8_t* attrib_ids, uint8_t* attrib_vals) -{ - tAVRC_STS status = BT_STATUS_UNSUPPORTED; - rc_transaction_t *p_transaction = NULL; - int count = 0; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); + * + * Function change_player_app_setting + * + * Description Set current values of Player Attributes + * + * Returns void + * + **************************************************************************/ +static bt_status_t change_player_app_setting(bt_bdaddr_t* bd_addr, + uint8_t num_attrib, + uint8_t* attrib_ids, + uint8_t* attrib_vals) { + tAVRC_STS status = BT_STATUS_UNSUPPORTED; + rc_transaction_t* p_transaction = NULL; + int count = 0; + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr); #if (AVRC_CTRL_INCLUDED == TRUE) - BTIF_TRACE_DEBUG("%s: num attrib_id: %d", __func__, num_attrib); - - CHECK_RC_CONNECTED(p_dev); - - bt_status_t tran_status=get_transaction(&p_transaction); - if (BT_STATUS_SUCCESS != tran_status) - return BT_STATUS_FAIL; - - tAVRC_COMMAND avrc_cmd = {0}; - BT_HDR *p_msg = NULL; - avrc_cmd.set_app_val.opcode = AVRC_OP_VENDOR; - avrc_cmd.set_app_val.status = AVRC_STS_NO_ERROR; - avrc_cmd.set_app_val.num_val = num_attrib; - avrc_cmd.set_app_val.pdu = AVRC_PDU_SET_PLAYER_APP_VALUE; - avrc_cmd.set_app_val.p_vals = - (tAVRC_APP_SETTING *)osi_malloc(sizeof(tAVRC_APP_SETTING) * num_attrib); - for (count = 0; count < num_attrib; count++) - { - avrc_cmd.set_app_val.p_vals[count].attr_id = attrib_ids[count]; - avrc_cmd.set_app_val.p_vals[count].attr_val = attrib_vals[count]; - } - status = AVRC_BldCommand(&avrc_cmd, &p_msg); - if ((status == AVRC_STS_NO_ERROR) && (p_msg != NULL)) - { - uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; - BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", - __func__, p_transaction->lbl); - BTA_AvVendorCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_CTRL, - data_start, p_msg->len); - status = BT_STATUS_SUCCESS; - start_control_command_timer (AVRC_PDU_SET_PLAYER_APP_VALUE, p_transaction, p_dev); - } - else - { - BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", - __func__, status); - } - osi_free(p_msg); - osi_free_and_reset((void **)&avrc_cmd.set_app_val.p_vals); + BTIF_TRACE_DEBUG("%s: num attrib_id: %d", __func__, num_attrib); + + CHECK_RC_CONNECTED(p_dev); + + bt_status_t tran_status = get_transaction(&p_transaction); + if (BT_STATUS_SUCCESS != tran_status) return BT_STATUS_FAIL; + + tAVRC_COMMAND avrc_cmd = {0}; + BT_HDR* p_msg = NULL; + avrc_cmd.set_app_val.opcode = AVRC_OP_VENDOR; + avrc_cmd.set_app_val.status = AVRC_STS_NO_ERROR; + avrc_cmd.set_app_val.num_val = num_attrib; + avrc_cmd.set_app_val.pdu = AVRC_PDU_SET_PLAYER_APP_VALUE; + avrc_cmd.set_app_val.p_vals = + (tAVRC_APP_SETTING*)osi_malloc(sizeof(tAVRC_APP_SETTING) * num_attrib); + for (count = 0; count < num_attrib; count++) { + avrc_cmd.set_app_val.p_vals[count].attr_id = attrib_ids[count]; + avrc_cmd.set_app_val.p_vals[count].attr_val = attrib_vals[count]; + } + status = AVRC_BldCommand(&avrc_cmd, &p_msg); + if ((status == AVRC_STS_NO_ERROR) && (p_msg != NULL)) { + uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; + BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", __func__, + p_transaction->lbl); + BTA_AvVendorCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_CTRL, + data_start, p_msg->len); + status = BT_STATUS_SUCCESS; + start_control_command_timer(AVRC_PDU_SET_PLAYER_APP_VALUE, p_transaction, + p_dev); + } else { + BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", __func__, + status); + } + osi_free(p_msg); + osi_free_and_reset((void**)&avrc_cmd.set_app_val.p_vals); #else - BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); + BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); #endif - return (bt_status_t)status; + return (bt_status_t)status; } /*************************************************************************** -** -** Function play_item_cmd -** -** Description Play the item specified by UID & scope -** -** Returns void -** -***************************************************************************/ -static bt_status_t play_item_cmd( - bt_bdaddr_t *bd_addr, uint8_t scope, uint8_t *uid, uint16_t uid_counter) -{ - tAVRC_STS status = BT_STATUS_UNSUPPORTED; + * + * Function play_item_cmd + * + * Description Play the item specified by UID & scope + * + * Returns void + * + **************************************************************************/ +static bt_status_t play_item_cmd(bt_bdaddr_t* bd_addr, uint8_t scope, + uint8_t* uid, uint16_t uid_counter) { + tAVRC_STS status = BT_STATUS_UNSUPPORTED; #if (AVRC_CTRL_INCLUDED == TRUE) - rc_transaction_t *p_transaction = NULL; - BTIF_TRACE_DEBUG("%s: scope %d uid_counter %d", __FUNCTION__, scope, uid_counter); - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); - CHECK_RC_CONNECTED(p_dev); - CHECK_BR_CONNECTED(p_dev); - - bt_status_t tran_status = get_transaction(&p_transaction); - if (BT_STATUS_SUCCESS != tran_status) - return BT_STATUS_FAIL; - - tAVRC_COMMAND avrc_cmd = {0}; - BT_HDR *p_msg = NULL; - avrc_cmd.pdu = AVRC_PDU_PLAY_ITEM; - avrc_cmd.play_item.opcode = AVRC_OP_VENDOR; - avrc_cmd.play_item.status = AVRC_STS_NO_ERROR; - avrc_cmd.play_item.scope = scope; - memcpy(avrc_cmd.play_item.uid, uid, AVRC_UID_SIZE); - avrc_cmd.play_item.uid_counter = uid_counter; - - status = AVRC_BldCommand(&avrc_cmd, &p_msg); - if ((status == AVRC_STS_NO_ERROR) && (p_msg != NULL)) - { - uint8_t* data_start = (uint8_t *)(p_msg + 1) + p_msg->offset; - BTIF_TRACE_DEBUG("%s msgreq being sent out with label %d", - __FUNCTION__,p_transaction->lbl); - BTA_AvVendorCmd(p_dev->rc_handle,p_transaction->lbl,AVRC_CMD_CTRL, - data_start, p_msg->len); - status = BT_STATUS_SUCCESS; - // start_control_command_timer (AVRC_PDU_PLAY_ITEM, p_transaction); - } - else - { - BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", - __FUNCTION__, status); - } - osi_free(p_msg); + rc_transaction_t* p_transaction = NULL; + BTIF_TRACE_DEBUG("%s: scope %d uid_counter %d", __FUNCTION__, scope, + uid_counter); + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr); + CHECK_RC_CONNECTED(p_dev); + CHECK_BR_CONNECTED(p_dev); + + bt_status_t tran_status = get_transaction(&p_transaction); + if (BT_STATUS_SUCCESS != tran_status) return BT_STATUS_FAIL; + + tAVRC_COMMAND avrc_cmd = {0}; + BT_HDR* p_msg = NULL; + avrc_cmd.pdu = AVRC_PDU_PLAY_ITEM; + avrc_cmd.play_item.opcode = AVRC_OP_VENDOR; + avrc_cmd.play_item.status = AVRC_STS_NO_ERROR; + avrc_cmd.play_item.scope = scope; + memcpy(avrc_cmd.play_item.uid, uid, AVRC_UID_SIZE); + avrc_cmd.play_item.uid_counter = uid_counter; + + status = AVRC_BldCommand(&avrc_cmd, &p_msg); + if ((status == AVRC_STS_NO_ERROR) && (p_msg != NULL)) { + uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; + BTIF_TRACE_DEBUG("%s msgreq being sent out with label %d", __FUNCTION__, + p_transaction->lbl); + BTA_AvVendorCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_CTRL, + data_start, p_msg->len); + status = BT_STATUS_SUCCESS; + // start_control_command_timer (AVRC_PDU_PLAY_ITEM, p_transaction); + } else { + BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", + __FUNCTION__, status); + } + osi_free(p_msg); #else - BTIF_TRACE_DEBUG("%s: feature not enabled", __FUNCTION__); + BTIF_TRACE_DEBUG("%s: feature not enabled", __FUNCTION__); #endif - return (bt_status_t) status; + return (bt_status_t)status; } /*************************************************************************** -** -** Function get_player_app_setting_attr_text_cmd -** -** Description Get text description for app attribute -** -** Returns void -** -***************************************************************************/ -static bt_status_t get_player_app_setting_attr_text_cmd (uint8_t *attrs, uint8_t num_attrs, - btif_rc_device_cb_t *p_dev) -{ - tAVRC_STS status = BT_STATUS_UNSUPPORTED; - rc_transaction_t *p_transaction = NULL; - int count = 0; + * + * Function get_player_app_setting_attr_text_cmd + * + * Description Get text description for app attribute + * + * Returns void + * + **************************************************************************/ +static bt_status_t get_player_app_setting_attr_text_cmd( + uint8_t* attrs, uint8_t num_attrs, btif_rc_device_cb_t* p_dev) { + tAVRC_STS status = BT_STATUS_UNSUPPORTED; + rc_transaction_t* p_transaction = NULL; + int count = 0; #if (AVRC_CTRL_INCLUDED == TRUE) - tAVRC_COMMAND avrc_cmd = {0}; - BT_HDR *p_msg = NULL; - bt_status_t tran_status; - - CHECK_RC_CONNECTED(p_dev); - - BTIF_TRACE_DEBUG("%s: num attrs: %d", __func__, num_attrs); - - tran_status = get_transaction(&p_transaction); - if (BT_STATUS_SUCCESS != tran_status) - return BT_STATUS_FAIL; - - avrc_cmd.pdu = AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT; - avrc_cmd.get_app_attr_txt.opcode = AVRC_OP_VENDOR; - avrc_cmd.get_app_attr_txt.num_attr = num_attrs; - - for (count = 0; count < num_attrs; count++) - { - avrc_cmd.get_app_attr_txt.attrs[count] = attrs[count]; - } - status = AVRC_BldCommand(&avrc_cmd, &p_msg); - if (status == AVRC_STS_NO_ERROR) - { - uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; - BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", - __func__, p_transaction->lbl); - BTA_AvVendorCmd(p_dev->rc_handle, p_transaction->lbl, - AVRC_CMD_STATUS, data_start, p_msg->len); - osi_free(p_msg); - status = BT_STATUS_SUCCESS; - start_status_command_timer (AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT, p_transaction, p_dev); - } - else - { - BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", __func__, status); - } + tAVRC_COMMAND avrc_cmd = {0}; + BT_HDR* p_msg = NULL; + bt_status_t tran_status; + + CHECK_RC_CONNECTED(p_dev); + + BTIF_TRACE_DEBUG("%s: num attrs: %d", __func__, num_attrs); + + tran_status = get_transaction(&p_transaction); + if (BT_STATUS_SUCCESS != tran_status) return BT_STATUS_FAIL; + + avrc_cmd.pdu = AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT; + avrc_cmd.get_app_attr_txt.opcode = AVRC_OP_VENDOR; + avrc_cmd.get_app_attr_txt.num_attr = num_attrs; + + for (count = 0; count < num_attrs; count++) { + avrc_cmd.get_app_attr_txt.attrs[count] = attrs[count]; + } + status = AVRC_BldCommand(&avrc_cmd, &p_msg); + if (status == AVRC_STS_NO_ERROR) { + uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; + BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", __func__, + p_transaction->lbl); + BTA_AvVendorCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_STATUS, + data_start, p_msg->len); osi_free(p_msg); + status = BT_STATUS_SUCCESS; + start_status_command_timer(AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT, p_transaction, + p_dev); + } else { + BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", __func__, + status); + } + osi_free(p_msg); #else - BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); + BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); #endif - return (bt_status_t)status; + return (bt_status_t)status; } /*************************************************************************** -** -** Function get_player_app_setting_val_text_cmd -** -** Description Get text description for app attribute values -** -** Returns void -** -***************************************************************************/ -static bt_status_t get_player_app_setting_value_text_cmd (uint8_t *vals, uint8_t num_vals, - btif_rc_device_cb_t *p_dev) -{ - tAVRC_STS status = BT_STATUS_UNSUPPORTED; - rc_transaction_t *p_transaction = NULL; - int count = 0; + * + * Function get_player_app_setting_val_text_cmd + * + * Description Get text description for app attribute values + * + * Returns void + * + **************************************************************************/ +static bt_status_t get_player_app_setting_value_text_cmd( + uint8_t* vals, uint8_t num_vals, btif_rc_device_cb_t* p_dev) { + tAVRC_STS status = BT_STATUS_UNSUPPORTED; + rc_transaction_t* p_transaction = NULL; + int count = 0; #if (AVRC_CTRL_INCLUDED == TRUE) - tAVRC_COMMAND avrc_cmd = {0}; - BT_HDR *p_msg = NULL; - bt_status_t tran_status; - - CHECK_RC_CONNECTED(p_dev); - - BTIF_TRACE_DEBUG("%s: num_vals: %d", __func__, num_vals); - - tran_status = get_transaction(&p_transaction); - if (BT_STATUS_SUCCESS != tran_status) - return BT_STATUS_FAIL; - - avrc_cmd.pdu = AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT; - avrc_cmd.get_app_val_txt.opcode = AVRC_OP_VENDOR; - avrc_cmd.get_app_val_txt.num_val = num_vals; - - for (count = 0; count < num_vals; count++) - { - avrc_cmd.get_app_val_txt.vals[count] = vals[count]; - } - status = AVRC_BldCommand(&avrc_cmd, &p_msg); - if (status == AVRC_STS_NO_ERROR) - { - uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; - BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", - __func__, p_transaction->lbl); - if (p_msg != NULL) - { - BTA_AvVendorCmd(p_dev->rc_handle, p_transaction->lbl, - AVRC_CMD_STATUS, data_start, p_msg->len); - status = BT_STATUS_SUCCESS; - start_status_command_timer (AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT, p_transaction, p_dev); - } - } - else - { - BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", - __func__, status); - } - osi_free(p_msg); + tAVRC_COMMAND avrc_cmd = {0}; + BT_HDR* p_msg = NULL; + bt_status_t tran_status; + + CHECK_RC_CONNECTED(p_dev); + + BTIF_TRACE_DEBUG("%s: num_vals: %d", __func__, num_vals); + + tran_status = get_transaction(&p_transaction); + if (BT_STATUS_SUCCESS != tran_status) return BT_STATUS_FAIL; + + avrc_cmd.pdu = AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT; + avrc_cmd.get_app_val_txt.opcode = AVRC_OP_VENDOR; + avrc_cmd.get_app_val_txt.num_val = num_vals; + + for (count = 0; count < num_vals; count++) { + avrc_cmd.get_app_val_txt.vals[count] = vals[count]; + } + status = AVRC_BldCommand(&avrc_cmd, &p_msg); + if (status == AVRC_STS_NO_ERROR) { + uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; + BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", __func__, + p_transaction->lbl); + if (p_msg != NULL) { + BTA_AvVendorCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_STATUS, + data_start, p_msg->len); + status = BT_STATUS_SUCCESS; + start_status_command_timer(AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT, + p_transaction, p_dev); + } + } else { + BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", __func__, + status); + } + osi_free(p_msg); #else - BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); + BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); #endif - return (bt_status_t)status; + return (bt_status_t)status; } /*************************************************************************** -** -** Function register_notification_cmd -** -** Description Send Command to register for a Notification ID -** -** Returns void -** -***************************************************************************/ -static bt_status_t register_notification_cmd (uint8_t label, uint8_t event_id, uint32_t event_value, - btif_rc_device_cb_t *p_dev) -{ - - tAVRC_STS status = BT_STATUS_UNSUPPORTED; + * + * Function register_notification_cmd + * + * Description Send Command to register for a Notification ID + * + * Returns void + * + **************************************************************************/ +static bt_status_t register_notification_cmd(uint8_t label, uint8_t event_id, + uint32_t event_value, + btif_rc_device_cb_t* p_dev) { + tAVRC_STS status = BT_STATUS_UNSUPPORTED; #if (AVRC_CTRL_INCLUDED == TRUE) - tAVRC_COMMAND avrc_cmd = {0}; - BT_HDR *p_msg = NULL; - - CHECK_RC_CONNECTED(p_dev); - - BTIF_TRACE_DEBUG("%s: event_id: %d event_value", __func__, event_id, event_value); - - avrc_cmd.reg_notif.opcode = AVRC_OP_VENDOR; - avrc_cmd.reg_notif.status = AVRC_STS_NO_ERROR; - avrc_cmd.reg_notif.event_id = event_id; - avrc_cmd.reg_notif.pdu = AVRC_PDU_REGISTER_NOTIFICATION; - avrc_cmd.reg_notif.param = event_value; - status = AVRC_BldCommand(&avrc_cmd, &p_msg); - if (status == AVRC_STS_NO_ERROR) - { - uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; - BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", - __func__, label); - if (p_msg != NULL) - { - BTA_AvVendorCmd(p_dev->rc_handle, label, AVRC_CMD_NOTIF, - data_start, p_msg->len); - status = BT_STATUS_SUCCESS; - } - } - else - { - BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", - __func__, status); - } - osi_free(p_msg); + tAVRC_COMMAND avrc_cmd = {0}; + BT_HDR* p_msg = NULL; + + CHECK_RC_CONNECTED(p_dev); + + BTIF_TRACE_DEBUG("%s: event_id: %d event_value", __func__, event_id, + event_value); + + avrc_cmd.reg_notif.opcode = AVRC_OP_VENDOR; + avrc_cmd.reg_notif.status = AVRC_STS_NO_ERROR; + avrc_cmd.reg_notif.event_id = event_id; + avrc_cmd.reg_notif.pdu = AVRC_PDU_REGISTER_NOTIFICATION; + avrc_cmd.reg_notif.param = event_value; + status = AVRC_BldCommand(&avrc_cmd, &p_msg); + if (status == AVRC_STS_NO_ERROR) { + uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; + BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", __func__, + label); + if (p_msg != NULL) { + BTA_AvVendorCmd(p_dev->rc_handle, label, AVRC_CMD_NOTIF, data_start, + p_msg->len); + status = BT_STATUS_SUCCESS; + } + } else { + BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", __func__, + status); + } + osi_free(p_msg); #else - BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); + BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); #endif - return (bt_status_t)status; + return (bt_status_t)status; } /*************************************************************************** -** -** Function get_element_attribute_cmd -** -** Description Get Element Attribute for attributeIds -** -** Returns void -** -***************************************************************************/ -static bt_status_t get_element_attribute_cmd (uint8_t num_attribute, uint32_t *p_attr_ids, - btif_rc_device_cb_t *p_dev) -{ - tAVRC_STS status = BT_STATUS_UNSUPPORTED; - rc_transaction_t *p_transaction=NULL; - int count = 0; + * + * Function get_element_attribute_cmd + * + * Description Get Element Attribute for attributeIds + * + * Returns void + * + **************************************************************************/ +static bt_status_t get_element_attribute_cmd(uint8_t num_attribute, + uint32_t* p_attr_ids, + btif_rc_device_cb_t* p_dev) { + tAVRC_STS status = BT_STATUS_UNSUPPORTED; + rc_transaction_t* p_transaction = NULL; + int count = 0; #if (AVRC_CTRL_INCLUDED == TRUE) - tAVRC_COMMAND avrc_cmd = {0}; - BT_HDR *p_msg = NULL; - bt_status_t tran_status; - - CHECK_RC_CONNECTED(p_dev); - - BTIF_TRACE_DEBUG("%s: num_attribute: %d attribute_id: %d", - __func__, num_attribute, p_attr_ids[0]); - - tran_status = get_transaction(&p_transaction); - if (BT_STATUS_SUCCESS != tran_status) - return BT_STATUS_FAIL; - - avrc_cmd.get_elem_attrs.opcode = AVRC_OP_VENDOR; - avrc_cmd.get_elem_attrs.status = AVRC_STS_NO_ERROR; - avrc_cmd.get_elem_attrs.num_attr = num_attribute; - avrc_cmd.get_elem_attrs.pdu = AVRC_PDU_GET_ELEMENT_ATTR; - for (count = 0; count < num_attribute; count++) - { - avrc_cmd.get_elem_attrs.attrs[count] = p_attr_ids[count]; - } - - status = AVRC_BldCommand(&avrc_cmd, &p_msg); - if (status == AVRC_STS_NO_ERROR) - { - uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; - BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", - __func__, p_transaction->lbl); - if (p_msg != NULL) - { - BTA_AvVendorCmd(p_dev->rc_handle, p_transaction->lbl, - AVRC_CMD_STATUS, data_start, p_msg->len); - status = BT_STATUS_SUCCESS; - start_status_command_timer (AVRC_PDU_GET_ELEMENT_ATTR, - p_transaction, p_dev); - } - } - else - { - BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", - __func__, status); - } - osi_free(p_msg); + tAVRC_COMMAND avrc_cmd = {0}; + BT_HDR* p_msg = NULL; + bt_status_t tran_status; + + CHECK_RC_CONNECTED(p_dev); + + BTIF_TRACE_DEBUG("%s: num_attribute: %d attribute_id: %d", __func__, + num_attribute, p_attr_ids[0]); + + tran_status = get_transaction(&p_transaction); + if (BT_STATUS_SUCCESS != tran_status) return BT_STATUS_FAIL; + + avrc_cmd.get_elem_attrs.opcode = AVRC_OP_VENDOR; + avrc_cmd.get_elem_attrs.status = AVRC_STS_NO_ERROR; + avrc_cmd.get_elem_attrs.num_attr = num_attribute; + avrc_cmd.get_elem_attrs.pdu = AVRC_PDU_GET_ELEMENT_ATTR; + for (count = 0; count < num_attribute; count++) { + avrc_cmd.get_elem_attrs.attrs[count] = p_attr_ids[count]; + } + + status = AVRC_BldCommand(&avrc_cmd, &p_msg); + if (status == AVRC_STS_NO_ERROR) { + uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; + BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", __func__, + p_transaction->lbl); + if (p_msg != NULL) { + BTA_AvVendorCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_STATUS, + data_start, p_msg->len); + status = BT_STATUS_SUCCESS; + start_status_command_timer(AVRC_PDU_GET_ELEMENT_ATTR, p_transaction, + p_dev); + } + } else { + BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", __func__, + status); + } + osi_free(p_msg); #else - BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); + BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); #endif - return (bt_status_t)status; + return (bt_status_t)status; } /*************************************************************************** -** -** Function get_play_status_cmd -** -** Description Get Playing Status of a Device -** -** Returns bt_status_t -** -***************************************************************************/ -static bt_status_t get_play_status_cmd(btif_rc_device_cb_t *p_dev) -{ - tAVRC_STS status = BT_STATUS_UNSUPPORTED; - rc_transaction_t *p_transaction = NULL; + * + * Function get_play_status_cmd + * + * Description Get Playing Status of a Device + * + * Returns bt_status_t + * + **************************************************************************/ +static bt_status_t get_play_status_cmd(btif_rc_device_cb_t* p_dev) { + tAVRC_STS status = BT_STATUS_UNSUPPORTED; + rc_transaction_t* p_transaction = NULL; #if (AVRC_CTRL_INCLUDED == TRUE) - tAVRC_COMMAND avrc_cmd = {0}; - BT_HDR *p_msg = NULL; - bt_status_t tran_status; - - CHECK_RC_CONNECTED(p_dev); - - BTIF_TRACE_DEBUG("%s: ", __func__); - tran_status = get_transaction(&p_transaction); - if (BT_STATUS_SUCCESS != tran_status) - return BT_STATUS_FAIL; - - avrc_cmd.get_play_status.opcode = AVRC_OP_VENDOR; - avrc_cmd.get_play_status.pdu = AVRC_PDU_GET_PLAY_STATUS; - avrc_cmd.get_play_status.status = AVRC_STS_NO_ERROR; - status = AVRC_BldCommand(&avrc_cmd, &p_msg); - if (status == AVRC_STS_NO_ERROR) - { - uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; - BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", - __func__, p_transaction->lbl); - if (p_msg != NULL) - { - BTA_AvVendorCmd(p_dev->rc_handle,p_transaction->lbl, - AVRC_CMD_STATUS, data_start, p_msg->len); - status = BT_STATUS_SUCCESS; - start_status_command_timer (AVRC_PDU_GET_PLAY_STATUS, p_transaction, p_dev); - } - } - else - { - BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", - __func__, status); - } - osi_free(p_msg); + tAVRC_COMMAND avrc_cmd = {0}; + BT_HDR* p_msg = NULL; + bt_status_t tran_status; + + CHECK_RC_CONNECTED(p_dev); + + BTIF_TRACE_DEBUG("%s: ", __func__); + tran_status = get_transaction(&p_transaction); + if (BT_STATUS_SUCCESS != tran_status) return BT_STATUS_FAIL; + + avrc_cmd.get_play_status.opcode = AVRC_OP_VENDOR; + avrc_cmd.get_play_status.pdu = AVRC_PDU_GET_PLAY_STATUS; + avrc_cmd.get_play_status.status = AVRC_STS_NO_ERROR; + status = AVRC_BldCommand(&avrc_cmd, &p_msg); + if (status == AVRC_STS_NO_ERROR) { + uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; + BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", __func__, + p_transaction->lbl); + if (p_msg != NULL) { + BTA_AvVendorCmd(p_dev->rc_handle, p_transaction->lbl, AVRC_CMD_STATUS, + data_start, p_msg->len); + status = BT_STATUS_SUCCESS; + start_status_command_timer(AVRC_PDU_GET_PLAY_STATUS, p_transaction, + p_dev); + } + } else { + BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", __func__, + status); + } + osi_free(p_msg); #else - BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); + BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); #endif - return (bt_status_t)status; - + return (bt_status_t)status; } /*************************************************************************** -** -** Function set_volume_rsp -** -** Description Rsp for SetAbsoluteVolume Command -** -** Returns void -** -***************************************************************************/ -static bt_status_t set_volume_rsp(bt_bdaddr_t *bd_addr, uint8_t abs_vol, uint8_t label) -{ - tAVRC_STS status = BT_STATUS_UNSUPPORTED; + * + * Function set_volume_rsp + * + * Description Rsp for SetAbsoluteVolume Command + * + * Returns void + * + **************************************************************************/ +static bt_status_t set_volume_rsp(bt_bdaddr_t* bd_addr, uint8_t abs_vol, + uint8_t label) { + tAVRC_STS status = BT_STATUS_UNSUPPORTED; #if (AVRC_CTRL_INCLUDED == TRUE) - tAVRC_RESPONSE avrc_rsp; - BT_HDR *p_msg = NULL; - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); - - CHECK_RC_CONNECTED(p_dev); - - BTIF_TRACE_DEBUG("%s: abs_vol: %d", __func__, abs_vol); - - avrc_rsp.volume.opcode = AVRC_OP_VENDOR; - avrc_rsp.volume.pdu = AVRC_PDU_SET_ABSOLUTE_VOLUME; - avrc_rsp.volume.status = AVRC_STS_NO_ERROR; - avrc_rsp.volume.volume = abs_vol; - status = AVRC_BldResponse(p_dev->rc_handle, &avrc_rsp, &p_msg); - if (status == AVRC_STS_NO_ERROR) - { - uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; - BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", - __func__, p_dev->rc_vol_label); - if (p_msg != NULL) - { - BTA_AvVendorRsp(p_dev->rc_handle, label, - BTA_AV_RSP_ACCEPT, data_start, p_msg->len, 0); - status = BT_STATUS_SUCCESS; - } - } - else - { - BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", - __func__, status); - } - osi_free(p_msg); + tAVRC_RESPONSE avrc_rsp; + BT_HDR* p_msg = NULL; + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr); + + CHECK_RC_CONNECTED(p_dev); + + BTIF_TRACE_DEBUG("%s: abs_vol: %d", __func__, abs_vol); + + avrc_rsp.volume.opcode = AVRC_OP_VENDOR; + avrc_rsp.volume.pdu = AVRC_PDU_SET_ABSOLUTE_VOLUME; + avrc_rsp.volume.status = AVRC_STS_NO_ERROR; + avrc_rsp.volume.volume = abs_vol; + status = AVRC_BldResponse(p_dev->rc_handle, &avrc_rsp, &p_msg); + if (status == AVRC_STS_NO_ERROR) { + uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; + BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", __func__, + p_dev->rc_vol_label); + if (p_msg != NULL) { + BTA_AvVendorRsp(p_dev->rc_handle, label, BTA_AV_RSP_ACCEPT, data_start, + p_msg->len, 0); + status = BT_STATUS_SUCCESS; + } + } else { + BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", __func__, + status); + } + osi_free(p_msg); #else - BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); + BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); #endif - return (bt_status_t)status; + return (bt_status_t)status; } /*************************************************************************** -** -** Function send_register_abs_vol_rsp -** -** Description Rsp for Notification of Absolute Volume -** -** Returns void -** -***************************************************************************/ -static bt_status_t volume_change_notification_rsp(bt_bdaddr_t *bd_addr, - btrc_notification_type_t rsp_type, uint8_t abs_vol, uint8_t label) -{ - tAVRC_STS status = BT_STATUS_UNSUPPORTED; - tAVRC_RESPONSE avrc_rsp; - BT_HDR *p_msg = NULL; + * + * Function send_register_abs_vol_rsp + * + * Description Rsp for Notification of Absolute Volume + * + * Returns void + * + **************************************************************************/ +static bt_status_t volume_change_notification_rsp( + bt_bdaddr_t* bd_addr, btrc_notification_type_t rsp_type, uint8_t abs_vol, + uint8_t label) { + tAVRC_STS status = BT_STATUS_UNSUPPORTED; + tAVRC_RESPONSE avrc_rsp; + BT_HDR* p_msg = NULL; #if (AVRC_CTRL_INCLUDED == TRUE) - BTIF_TRACE_DEBUG("%s: rsp_type: %d abs_vol: %d", __func__, rsp_type, abs_vol); - - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); - - CHECK_RC_CONNECTED(p_dev); - - avrc_rsp.reg_notif.opcode = AVRC_OP_VENDOR; - avrc_rsp.reg_notif.pdu = AVRC_PDU_REGISTER_NOTIFICATION; - avrc_rsp.reg_notif.status = AVRC_STS_NO_ERROR; - avrc_rsp.reg_notif.param.volume = abs_vol; - avrc_rsp.reg_notif.event_id = AVRC_EVT_VOLUME_CHANGE; - - status = AVRC_BldResponse(p_dev->rc_handle, &avrc_rsp, &p_msg); - if (status == AVRC_STS_NO_ERROR) { - BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", - __func__, label); - uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; - BTA_AvVendorRsp(p_dev->rc_handle, label, - (rsp_type == BTRC_NOTIFICATION_TYPE_INTERIM) ? - AVRC_RSP_INTERIM : AVRC_RSP_CHANGED, - data_start, p_msg->len, 0); - status = BT_STATUS_SUCCESS; - } else { - BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", - __func__, status); - } - osi_free(p_msg); + BTIF_TRACE_DEBUG("%s: rsp_type: %d abs_vol: %d", __func__, rsp_type, abs_vol); + + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr); + + CHECK_RC_CONNECTED(p_dev); + + avrc_rsp.reg_notif.opcode = AVRC_OP_VENDOR; + avrc_rsp.reg_notif.pdu = AVRC_PDU_REGISTER_NOTIFICATION; + avrc_rsp.reg_notif.status = AVRC_STS_NO_ERROR; + avrc_rsp.reg_notif.param.volume = abs_vol; + avrc_rsp.reg_notif.event_id = AVRC_EVT_VOLUME_CHANGE; + + status = AVRC_BldResponse(p_dev->rc_handle, &avrc_rsp, &p_msg); + if (status == AVRC_STS_NO_ERROR) { + BTIF_TRACE_DEBUG("%s: msgreq being sent out with label: %d", __func__, + label); + uint8_t* data_start = (uint8_t*)(p_msg + 1) + p_msg->offset; + BTA_AvVendorRsp(p_dev->rc_handle, label, + (rsp_type == BTRC_NOTIFICATION_TYPE_INTERIM) + ? AVRC_RSP_INTERIM + : AVRC_RSP_CHANGED, + data_start, p_msg->len, 0); + status = BT_STATUS_SUCCESS; + } else { + BTIF_TRACE_ERROR("%s: failed to build command. status: 0x%02x", __func__, + status); + } + osi_free(p_msg); #else - BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); + BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); #endif - return (bt_status_t)status; + return (bt_status_t)status; } /*************************************************************************** -** -** Function send_groupnavigation_cmd -** -** Description Send Pass-Through command -** -** Returns void -** -***************************************************************************/ -static bt_status_t send_groupnavigation_cmd(bt_bdaddr_t *bd_addr, uint8_t key_code, - uint8_t key_state) -{ - tAVRC_STS status = BT_STATUS_UNSUPPORTED; + * + * Function send_groupnavigation_cmd + * + * Description Send Pass-Through command + * + * Returns void + * + **************************************************************************/ +static bt_status_t send_groupnavigation_cmd(bt_bdaddr_t* bd_addr, + uint8_t key_code, + uint8_t key_state) { + tAVRC_STS status = BT_STATUS_UNSUPPORTED; #if (AVRC_CTRL_INCLUDED == TRUE) - rc_transaction_t *p_transaction=NULL; - BTIF_TRACE_DEBUG("%s: key-code: %d, key-state: %d", __func__, - key_code, key_state); - btif_rc_device_cb_t *p_dev = btif_rc_get_device_by_bda(bd_addr); + rc_transaction_t* p_transaction = NULL; + BTIF_TRACE_DEBUG("%s: key-code: %d, key-state: %d", __func__, key_code, + key_state); + btif_rc_device_cb_t* p_dev = btif_rc_get_device_by_bda(bd_addr); - CHECK_RC_CONNECTED(p_dev); + CHECK_RC_CONNECTED(p_dev); - if (p_dev->rc_features & BTA_AV_FEAT_RCTG) - { - bt_status_t tran_status = get_transaction(&p_transaction); - if ((BT_STATUS_SUCCESS == tran_status) && (NULL != p_transaction)) { - uint8_t buffer[AVRC_PASS_THRU_GROUP_LEN] = {0}; - uint8_t* start = buffer; - UINT24_TO_BE_STREAM(start, AVRC_CO_METADATA); - *(start)++ = 0; - UINT8_TO_BE_STREAM(start, key_code); - BTA_AvRemoteVendorUniqueCmd(p_dev->rc_handle, - p_transaction->lbl, - (tBTA_AV_STATE)key_state, buffer, - AVRC_PASS_THRU_GROUP_LEN); - status = BT_STATUS_SUCCESS; - BTIF_TRACE_DEBUG("%s: succesfully sent group_navigation command to BTA", - __func__); - } - else - { - status = BT_STATUS_FAIL; - BTIF_TRACE_DEBUG("%s: error in fetching transaction", __func__); - } - } - else - { - status = BT_STATUS_FAIL; - BTIF_TRACE_DEBUG("%s: feature not supported", __func__); + if (p_dev->rc_features & BTA_AV_FEAT_RCTG) { + bt_status_t tran_status = get_transaction(&p_transaction); + if ((BT_STATUS_SUCCESS == tran_status) && (NULL != p_transaction)) { + uint8_t buffer[AVRC_PASS_THRU_GROUP_LEN] = {0}; + uint8_t* start = buffer; + UINT24_TO_BE_STREAM(start, AVRC_CO_METADATA); + *(start)++ = 0; + UINT8_TO_BE_STREAM(start, key_code); + BTA_AvRemoteVendorUniqueCmd(p_dev->rc_handle, p_transaction->lbl, + (tBTA_AV_STATE)key_state, buffer, + AVRC_PASS_THRU_GROUP_LEN); + status = BT_STATUS_SUCCESS; + BTIF_TRACE_DEBUG("%s: succesfully sent group_navigation command to BTA", + __func__); + } else { + status = BT_STATUS_FAIL; + BTIF_TRACE_DEBUG("%s: error in fetching transaction", __func__); } + } else { + status = BT_STATUS_FAIL; + BTIF_TRACE_DEBUG("%s: feature not supported", __func__); + } #else - BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); + BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); #endif - return (bt_status_t)status; + return (bt_status_t)status; } /*************************************************************************** -** -** Function send_passthrough_cmd -** -** Description Send Pass-Through command -** -** Returns void -** -***************************************************************************/ -static bt_status_t send_passthrough_cmd(bt_bdaddr_t *bd_addr, uint8_t key_code, - uint8_t key_state) -{ - tAVRC_STS status = BT_STATUS_UNSUPPORTED; - btif_rc_device_cb_t *p_dev = NULL; - BTIF_TRACE_ERROR("%s: calling btif_rc_get_device_by_bda", __func__); - p_dev = btif_rc_get_device_by_bda(bd_addr); + * + * Function send_passthrough_cmd + * + * Description Send Pass-Through command + * + * Returns void + * + **************************************************************************/ +static bt_status_t send_passthrough_cmd(bt_bdaddr_t* bd_addr, uint8_t key_code, + uint8_t key_state) { + tAVRC_STS status = BT_STATUS_UNSUPPORTED; + btif_rc_device_cb_t* p_dev = NULL; + BTIF_TRACE_ERROR("%s: calling btif_rc_get_device_by_bda", __func__); + p_dev = btif_rc_get_device_by_bda(bd_addr); #if (AVRC_CTRL_INCLUDED == TRUE) - CHECK_RC_CONNECTED(p_dev); + CHECK_RC_CONNECTED(p_dev); - rc_transaction_t *p_transaction=NULL; - BTIF_TRACE_DEBUG("%s: key-code: %d, key-state: %d", __func__, - key_code, key_state); - if (p_dev->rc_features & BTA_AV_FEAT_RCTG) - { - bt_status_t tran_status = get_transaction(&p_transaction); - if (BT_STATUS_SUCCESS == tran_status && NULL != p_transaction) - { - BTA_AvRemoteCmd(p_dev->rc_handle, p_transaction->lbl, - (tBTA_AV_RC)key_code, (tBTA_AV_STATE)key_state); - status = BT_STATUS_SUCCESS; - BTIF_TRACE_DEBUG("%s: succesfully sent passthrough command to BTA", __func__); - } - else - { - status = BT_STATUS_FAIL; - BTIF_TRACE_DEBUG("%s: error in fetching transaction", __func__); - } - } - else - { - status = BT_STATUS_FAIL; - BTIF_TRACE_DEBUG("%s: feature not supported", __func__); + rc_transaction_t* p_transaction = NULL; + BTIF_TRACE_DEBUG("%s: key-code: %d, key-state: %d", __func__, key_code, + key_state); + if (p_dev->rc_features & BTA_AV_FEAT_RCTG) { + bt_status_t tran_status = get_transaction(&p_transaction); + if (BT_STATUS_SUCCESS == tran_status && NULL != p_transaction) { + BTA_AvRemoteCmd(p_dev->rc_handle, p_transaction->lbl, + (tBTA_AV_RC)key_code, (tBTA_AV_STATE)key_state); + status = BT_STATUS_SUCCESS; + BTIF_TRACE_DEBUG("%s: succesfully sent passthrough command to BTA", + __func__); + } else { + status = BT_STATUS_FAIL; + BTIF_TRACE_DEBUG("%s: error in fetching transaction", __func__); } + } else { + status = BT_STATUS_FAIL; + BTIF_TRACE_DEBUG("%s: feature not supported", __func__); + } #else - BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); + BTIF_TRACE_DEBUG("%s: feature not enabled", __func__); #endif - return (bt_status_t)status; + return (bt_status_t)status; } static const btrc_interface_t bt_rc_interface = { @@ -6073,190 +5669,175 @@ static const btrc_ctrl_interface_t bt_rc_ctrl_interface = { }; /******************************************************************************* -** -** Function btif_rc_get_interface -** -** Description Get the AVRCP Target callback interface -** -** Returns btrc_interface_t -** -*******************************************************************************/ -const btrc_interface_t *btif_rc_get_interface(void) -{ - BTIF_TRACE_EVENT("%s: ", __func__); - return &bt_rc_interface; + * + * Function btif_rc_get_interface + * + * Description Get the AVRCP Target callback interface + * + * Returns btrc_interface_t + * + ******************************************************************************/ +const btrc_interface_t* btif_rc_get_interface(void) { + BTIF_TRACE_EVENT("%s: ", __func__); + return &bt_rc_interface; } /******************************************************************************* -** -** Function btif_rc_ctrl_get_interface -** -** Description Get the AVRCP Controller callback interface -** -** Returns btrc_ctrl_interface_t -** -*******************************************************************************/ -const btrc_ctrl_interface_t *btif_rc_ctrl_get_interface(void) -{ - BTIF_TRACE_EVENT("%s: ", __func__); - return &bt_rc_ctrl_interface; + * + * Function btif_rc_ctrl_get_interface + * + * Description Get the AVRCP Controller callback interface + * + * Returns btrc_ctrl_interface_t + * + ******************************************************************************/ +const btrc_ctrl_interface_t* btif_rc_ctrl_get_interface(void) { + BTIF_TRACE_EVENT("%s: ", __func__); + return &bt_rc_ctrl_interface; } /******************************************************************************* -** Function initialize_transaction -** -** Description Initializes fields of the transaction structure -** -** Returns void -*******************************************************************************/ -static void initialize_transaction(int lbl) -{ - std::unique_lock(device.lbllock); - if (lbl < MAX_TRANSACTIONS_PER_SESSION) { - if (alarm_is_scheduled(device.transaction[lbl].txn_timer)) { - clear_cmd_timeout(lbl); - } - device.transaction[lbl].lbl = lbl; - device.transaction[lbl].in_use=false; - device.transaction[lbl].handle=0; - } + * Function initialize_transaction + * + * Description Initializes fields of the transaction structure + * + * Returns void + ******************************************************************************/ +static void initialize_transaction(int lbl) { + std::unique_lock(device.lbllock); + if (lbl < MAX_TRANSACTIONS_PER_SESSION) { + if (alarm_is_scheduled(device.transaction[lbl].txn_timer)) { + clear_cmd_timeout(lbl); + } + device.transaction[lbl].lbl = lbl; + device.transaction[lbl].in_use = false; + device.transaction[lbl].handle = 0; + } } /******************************************************************************* -** Function lbl_init -** -** Description Initializes label structures and mutexes. -** -** Returns void -*******************************************************************************/ -void lbl_init() -{ - memset(&device.transaction, 0, sizeof(device.transaction)); - init_all_transactions(); + * Function lbl_init + * + * Description Initializes label structures and mutexes. + * + * Returns void + ******************************************************************************/ +void lbl_init() { + memset(&device.transaction, 0, sizeof(device.transaction)); + init_all_transactions(); } /******************************************************************************* -** -** Function init_all_transactions -** -** Description Initializes all transactions -** -** Returns void -*******************************************************************************/ -void init_all_transactions() -{ - uint8_t txn_indx=0; - for(txn_indx=0; txn_indx < MAX_TRANSACTIONS_PER_SESSION; txn_indx++) - { - initialize_transaction(txn_indx); - } + * + * Function init_all_transactions + * + * Description Initializes all transactions + * + * Returns void + ******************************************************************************/ +void init_all_transactions() { + uint8_t txn_indx = 0; + for (txn_indx = 0; txn_indx < MAX_TRANSACTIONS_PER_SESSION; txn_indx++) { + initialize_transaction(txn_indx); + } } /******************************************************************************* -** -** Function get_transaction_by_lbl -** -** Description Will return a transaction based on the label. If not inuse -** will return an error. -** -** Returns bt_status_t -*******************************************************************************/ -rc_transaction_t *get_transaction_by_lbl(uint8_t lbl) -{ - rc_transaction_t *transaction = NULL; - std::unique_lock lock(device.lbllock); - - /* Determine if this is a valid label */ - if (lbl < MAX_TRANSACTIONS_PER_SESSION) - { - if (false==device.transaction[lbl].in_use) - { - transaction = NULL; - } - else - { - transaction = &(device.transaction[lbl]); - BTIF_TRACE_DEBUG("%s: Got transaction.label: %d",__func__,lbl); - } + * + * Function get_transaction_by_lbl + * + * Description Will return a transaction based on the label. If not inuse + * will return an error. + * + * Returns bt_status_t + ******************************************************************************/ +rc_transaction_t* get_transaction_by_lbl(uint8_t lbl) { + rc_transaction_t* transaction = NULL; + std::unique_lock lock(device.lbllock); + + /* Determine if this is a valid label */ + if (lbl < MAX_TRANSACTIONS_PER_SESSION) { + if (false == device.transaction[lbl].in_use) { + transaction = NULL; + } else { + transaction = &(device.transaction[lbl]); + BTIF_TRACE_DEBUG("%s: Got transaction.label: %d", __func__, lbl); } + } - return transaction; + return transaction; } /******************************************************************************* -** -** Function get_transaction -** -** Description Obtains the transaction details. -** -** Returns bt_status_t -*******************************************************************************/ - -bt_status_t get_transaction(rc_transaction_t **ptransaction) -{ - bt_status_t result = BT_STATUS_NOMEM; - uint8_t i=0; - std::unique_lock lock(device.lbllock); - - // Check for unused transactions - for (i=0; i lock(device.lbllock); + + // Check for unused transactions + for (i = 0; i < MAX_TRANSACTIONS_PER_SESSION; i++) { + if (false == device.transaction[i].in_use) { + BTIF_TRACE_DEBUG("%s: Got transaction.label: %d", __func__, + device.transaction[i].lbl); + device.transaction[i].in_use = true; + *ptransaction = &(device.transaction[i]); + result = BT_STATUS_SUCCESS; + break; + } + } + + return result; } /******************************************************************************* -** -** Function release_transaction -** -** Description Will release a transaction for reuse -** -** Returns bt_status_t -*******************************************************************************/ -void release_transaction(uint8_t lbl) -{ - BTIF_TRACE_DEBUG("%s %d", __func__, lbl); - rc_transaction_t *transaction = get_transaction_by_lbl(lbl); - - /* If the transaction is in use... */ - if (transaction != NULL) - { - BTIF_TRACE_DEBUG("%s: lbl: %d", __func__, lbl); - initialize_transaction(lbl); - } + * + * Function release_transaction + * + * Description Will release a transaction for reuse + * + * Returns bt_status_t + ******************************************************************************/ +void release_transaction(uint8_t lbl) { + BTIF_TRACE_DEBUG("%s %d", __func__, lbl); + rc_transaction_t* transaction = get_transaction_by_lbl(lbl); + + /* If the transaction is in use... */ + if (transaction != NULL) { + BTIF_TRACE_DEBUG("%s: lbl: %d", __func__, lbl); + initialize_transaction(lbl); + } } /******************************************************************************* -** Function sleep_ms -** -** Description Sleep the calling thread unconditionally for -** |timeout_ms| milliseconds. -** -** Returns void -*******************************************************************************/ + * Function sleep_ms + * + * Description Sleep the calling thread unconditionally for + * |timeout_ms| milliseconds. + * + * Returns void + ******************************************************************************/ static void sleep_ms(period_ms_t timeout_ms) { - struct timespec delay; - delay.tv_sec = timeout_ms / 1000; - delay.tv_nsec = 1000 * 1000 * (timeout_ms % 1000); + struct timespec delay; + delay.tv_sec = timeout_ms / 1000; + delay.tv_nsec = 1000 * 1000 * (timeout_ms % 1000); - OSI_NO_INTR(nanosleep(&delay, &delay)); + OSI_NO_INTR(nanosleep(&delay, &delay)); } static bool absolute_volume_disabled() { - char volume_disabled[PROPERTY_VALUE_MAX] = {0}; - osi_property_get("persist.bluetooth.disableabsvol", volume_disabled, "false"); - if (strncmp(volume_disabled, "true", 4) == 0) { - BTIF_TRACE_WARNING("%s: Absolute volume disabled by property", __func__); - return true; - } - return false; + char volume_disabled[PROPERTY_VALUE_MAX] = {0}; + osi_property_get("persist.bluetooth.disableabsvol", volume_disabled, "false"); + if (strncmp(volume_disabled, "true", 4) == 0) { + BTIF_TRACE_WARNING("%s: Absolute volume disabled by property", __func__); + return true; + } + return false; } diff --git a/btif/src/btif_sdp.cc b/btif/src/btif_sdp.cc index 4549573d9..7e8009108 100644 --- a/btif/src/btif_sdp.cc +++ b/btif/src/btif_sdp.cc @@ -20,7 +20,8 @@ * * Filename: btif_sdp.c * Description: SDP Bluetooth Interface. - * Implements the generic message handling and search functionality. + * Implements the generic message handling and search + *functionality. * References btif_sdp_server.c for SDP record creation. * ***********************************************************************************/ @@ -40,11 +41,12 @@ #include "btif_util.h" /***************************************************************************** -** Functions implemented in sdp_server.c -******************************************************************************/ + * Functions implemented in sdp_server.c + *****************************************************************************/ bt_status_t sdp_server_init(); void sdp_server_cleanup(); -bt_status_t create_sdp_record(bluetooth_sdp_record *records, int* record_handles); +bt_status_t create_sdp_record(bluetooth_sdp_record* records, + int* record_handles); bt_status_t remove_sdp_record(int record_handle); void on_create_record_event(int handle); void on_remove_record_event(int handle); @@ -52,146 +54,124 @@ void on_remove_record_event(int handle); // Utility functions: int get_sdp_records_size(bluetooth_sdp_record* in_record, int count); void copy_sdp_records(bluetooth_sdp_record* in_records, - bluetooth_sdp_record* out_records, int count); + bluetooth_sdp_record* out_records, int count); /***************************************************************************** -** Static variables -******************************************************************************/ + * Static variables + *****************************************************************************/ -static btsdp_callbacks_t *bt_sdp_callbacks = NULL; +static btsdp_callbacks_t* bt_sdp_callbacks = NULL; -static void btif_sdp_search_comp_evt(uint16_t event, char *p_param) -{ - tBTA_SDP_SEARCH_COMP *evt_data = (tBTA_SDP_SEARCH_COMP*) p_param; - bt_bdaddr_t addr; - BTIF_TRACE_DEBUG("%s: event = %d", __func__, event); +static void btif_sdp_search_comp_evt(uint16_t event, char* p_param) { + tBTA_SDP_SEARCH_COMP* evt_data = (tBTA_SDP_SEARCH_COMP*)p_param; + bt_bdaddr_t addr; + BTIF_TRACE_DEBUG("%s: event = %d", __func__, event); - if (event != BTA_SDP_SEARCH_COMP_EVT) - return; + if (event != BTA_SDP_SEARCH_COMP_EVT) return; - bdcpy(addr.address, evt_data->remote_addr); + bdcpy(addr.address, evt_data->remote_addr); - HAL_CBACK(bt_sdp_callbacks, sdp_search_cb, (bt_status_t)evt_data->status, + HAL_CBACK(bt_sdp_callbacks, sdp_search_cb, (bt_status_t)evt_data->status, &addr, (uint8_t*)(evt_data->uuid.uu.uuid128), evt_data->record_count, evt_data->records); } -static void sdp_search_comp_copy_cb(uint16_t event, char *p_dest, char *p_src) -{ - tBTA_SDP_SEARCH_COMP *p_dest_data = (tBTA_SDP_SEARCH_COMP *) p_dest; - tBTA_SDP_SEARCH_COMP *p_src_data = (tBTA_SDP_SEARCH_COMP *) p_src; +static void sdp_search_comp_copy_cb(uint16_t event, char* p_dest, char* p_src) { + tBTA_SDP_SEARCH_COMP* p_dest_data = (tBTA_SDP_SEARCH_COMP*)p_dest; + tBTA_SDP_SEARCH_COMP* p_src_data = (tBTA_SDP_SEARCH_COMP*)p_src; - if (!p_src) - return; + if (!p_src) return; - if (event != BTA_SDP_SEARCH_COMP_EVT) - return; + if (event != BTA_SDP_SEARCH_COMP_EVT) return; - maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data)); + maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data)); - copy_sdp_records(p_src_data->records, p_dest_data->records, p_src_data->record_count); + copy_sdp_records(p_src_data->records, p_dest_data->records, + p_src_data->record_count); } -static void sdp_dm_cback(tBTA_SDP_EVT event, tBTA_SDP *p_data, void *user_data) -{ - switch (event) - { - case BTA_SDP_SEARCH_COMP_EVT: - { - int size = sizeof(tBTA_SDP); - size += get_sdp_records_size(p_data->sdp_search_comp.records, - p_data->sdp_search_comp.record_count); - - /* need to deep copy the record content */ - btif_transfer_context(btif_sdp_search_comp_evt, event, - (char*)p_data, size, sdp_search_comp_copy_cb); - break; - } - case BTA_SDP_CREATE_RECORD_USER_EVT: - { - on_create_record_event(PTR_TO_INT(user_data)); - break; - } - case BTA_SDP_REMOVE_RECORD_USER_EVT: - { - on_remove_record_event(PTR_TO_INT(user_data)); - break; - } - default: - break; +static void sdp_dm_cback(tBTA_SDP_EVT event, tBTA_SDP* p_data, + void* user_data) { + switch (event) { + case BTA_SDP_SEARCH_COMP_EVT: { + int size = sizeof(tBTA_SDP); + size += get_sdp_records_size(p_data->sdp_search_comp.records, + p_data->sdp_search_comp.record_count); + + /* need to deep copy the record content */ + btif_transfer_context(btif_sdp_search_comp_evt, event, (char*)p_data, + size, sdp_search_comp_copy_cb); + break; } + case BTA_SDP_CREATE_RECORD_USER_EVT: { + on_create_record_event(PTR_TO_INT(user_data)); + break; + } + case BTA_SDP_REMOVE_RECORD_USER_EVT: { + on_remove_record_event(PTR_TO_INT(user_data)); + break; + } + default: + break; + } } -static bt_status_t init(btsdp_callbacks_t* callbacks) -{ - BTIF_TRACE_DEBUG("Sdp Search %s", __func__); +static bt_status_t init(btsdp_callbacks_t* callbacks) { + BTIF_TRACE_DEBUG("Sdp Search %s", __func__); - bt_sdp_callbacks = callbacks; - sdp_server_init(); + bt_sdp_callbacks = callbacks; + sdp_server_init(); - btif_enable_service(BTA_SDP_SERVICE_ID); + btif_enable_service(BTA_SDP_SERVICE_ID); - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } -static bt_status_t deinit() -{ - BTIF_TRACE_DEBUG("Sdp Search %s", __func__); +static bt_status_t deinit() { + BTIF_TRACE_DEBUG("Sdp Search %s", __func__); - bt_sdp_callbacks = NULL; - sdp_server_cleanup(); - btif_disable_service(BTA_SDP_SERVICE_ID); + bt_sdp_callbacks = NULL; + sdp_server_cleanup(); + btif_disable_service(BTA_SDP_SERVICE_ID); - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } -static bt_status_t search(bt_bdaddr_t *bd_addr, const uint8_t *uuid) -{ - tSDP_UUID sdp_uuid; - sdp_uuid.len = 16; - memcpy(sdp_uuid.uu.uuid128, uuid, sizeof(sdp_uuid.uu.uuid128)); +static bt_status_t search(bt_bdaddr_t* bd_addr, const uint8_t* uuid) { + tSDP_UUID sdp_uuid; + sdp_uuid.len = 16; + memcpy(sdp_uuid.uu.uuid128, uuid, sizeof(sdp_uuid.uu.uuid128)); - BTA_SdpSearch(bd_addr->address, &sdp_uuid); + BTA_SdpSearch(bd_addr->address, &sdp_uuid); - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } static const btsdp_interface_t sdp_if = { - sizeof(btsdp_interface_t), - init, - deinit, - search, - create_sdp_record, - remove_sdp_record -}; - -const btsdp_interface_t *btif_sdp_get_interface(void) -{ - BTIF_TRACE_DEBUG("%s", __func__); - return &sdp_if; + sizeof(btsdp_interface_t), init, deinit, search, create_sdp_record, + remove_sdp_record}; + +const btsdp_interface_t* btif_sdp_get_interface(void) { + BTIF_TRACE_DEBUG("%s", __func__); + return &sdp_if; } /******************************************************************************* -** -** Function btif_sdp_execute_service -** -** Description Initializes/Shuts down the service -** -** Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_sdp_execute_service(bool b_enable) -{ - BTIF_TRACE_DEBUG("%s enable:%d", __func__, b_enable); - - if (b_enable) - { - BTA_SdpEnable(sdp_dm_cback); - } - else - { - /* This is called on BT disable so no need to extra cleanup */ - } - return BT_STATUS_SUCCESS; + * + * Function btif_sdp_execute_service + * + * Description Initializes/Shuts down the service + * + * Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_sdp_execute_service(bool b_enable) { + BTIF_TRACE_DEBUG("%s enable:%d", __func__, b_enable); + + if (b_enable) { + BTA_SdpEnable(sdp_dm_cback); + } else { + /* This is called on BT disable so no need to extra cleanup */ + } + return BT_STATUS_SUCCESS; } - diff --git a/btif/src/btif_sdp_server.cc b/btif/src/btif_sdp_server.cc index b33841199..c66f84df9 100644 --- a/btif/src/btif_sdp_server.cc +++ b/btif/src/btif_sdp_server.cc @@ -19,7 +19,8 @@ /************************************************************************************ * * Filename: btif_sdp_server.cc - * Description: SDP server Bluetooth Interface to create and remove SDP records. + * Description: SDP server Bluetooth Interface to create and remove SDP + *records. * To be used in combination with the RFCOMM/L2CAP(LE) sockets. * * @@ -52,8 +53,8 @@ static std::recursive_mutex sdp_lock; * The remaining state control is handled by program flow */ typedef enum { - SDP_RECORD_FREE = 0, - SDP_RECORD_ALLOCED, + SDP_RECORD_FREE = 0, + SDP_RECORD_ALLOCED, } sdp_state_t; typedef struct { @@ -88,95 +89,96 @@ static int free_sdp_slot(int id); * based on the ID beeing passed to BTA as user_data. *****************************************************************************/ -static void init_sdp_slots() -{ - int i; - memset(sdp_slots, 0, sizeof(sdp_slot_t)*MAX_SDP_SLOTS); - /* if SDP_RECORD_FREE is zero - no need to set the value */ - if(SDP_RECORD_FREE != 0) { - for(i = 0; i < MAX_SDP_SLOTS; i++) - { - sdp_slots[i].state = SDP_RECORD_FREE; - } +static void init_sdp_slots() { + int i; + memset(sdp_slots, 0, sizeof(sdp_slot_t) * MAX_SDP_SLOTS); + /* if SDP_RECORD_FREE is zero - no need to set the value */ + if (SDP_RECORD_FREE != 0) { + for (i = 0; i < MAX_SDP_SLOTS; i++) { + sdp_slots[i].state = SDP_RECORD_FREE; } + } } -bt_status_t sdp_server_init() -{ - BTIF_TRACE_DEBUG("Sdp Server %s", __func__); - init_sdp_slots(); - return BT_STATUS_SUCCESS; +bt_status_t sdp_server_init() { + BTIF_TRACE_DEBUG("Sdp Server %s", __func__); + init_sdp_slots(); + return BT_STATUS_SUCCESS; } -void sdp_server_cleanup() -{ - BTIF_TRACE_DEBUG("Sdp Server %s", __func__); - std::unique_lock lock(sdp_lock); - int i; - for(i = 0; i < MAX_SDP_SLOTS; i++) - { - /*remove_sdp_record(i); we cannot send messages to the other threads, since they might - * have been shut down already. Just do local cleanup. - */ - free_sdp_slot(i); - } +void sdp_server_cleanup() { + BTIF_TRACE_DEBUG("Sdp Server %s", __func__); + std::unique_lock lock(sdp_lock); + int i; + for (i = 0; i < MAX_SDP_SLOTS; i++) { + /*remove_sdp_record(i); we cannot send messages to the other threads, since + * they might + * have been shut down already. Just do local cleanup. + */ + free_sdp_slot(i); + } } int get_sdp_records_size(bluetooth_sdp_record* in_record, int count) { - bluetooth_sdp_record* record = in_record; - int records_size = 0; - int i; - for(i=0; ihdr.service_name_length; - if(record->hdr.service_name_length > 0){ - records_size++; /* + '\0' termination of string */ - } - records_size += record->hdr.user1_ptr_len; - records_size += record->hdr.user2_ptr_len; - } - return records_size; + bluetooth_sdp_record* record = in_record; + int records_size = 0; + int i; + for (i = 0; i < count; i++) { + record = &in_record[i]; + records_size += sizeof(bluetooth_sdp_record); + records_size += record->hdr.service_name_length; + if (record->hdr.service_name_length > 0) { + records_size++; /* + '\0' termination of string */ + } + records_size += record->hdr.user1_ptr_len; + records_size += record->hdr.user2_ptr_len; + } + return records_size; } /* Deep copy all content of in_records into out_records. * out_records must point to a chunk of memory large enough to contain all * the data. Use getSdpRecordsSize() to calculate the needed size. */ void copy_sdp_records(bluetooth_sdp_record* in_records, - bluetooth_sdp_record* out_records, int count) { - int i; - bluetooth_sdp_record* in_record; - bluetooth_sdp_record* out_record; - char* free_ptr = (char*)(&out_records[count]); /* set pointer to after the last entry */ - - for(i=0; ihdr.service_name == NULL || in_record->hdr.service_name_length == 0) { - out_record->hdr.service_name = NULL; - out_record->hdr.service_name_length = 0; - } else { - out_record->hdr.service_name = free_ptr; // Update service_name pointer - // Copy string - memcpy(free_ptr, in_record->hdr.service_name, in_record->hdr.service_name_length); - free_ptr += in_record->hdr.service_name_length; - *(free_ptr) = '\0'; // Set '\0' termination of string - free_ptr++; - } - if(in_record->hdr.user1_ptr != NULL) { - out_record->hdr.user1_ptr = (uint8_t*)free_ptr; // Update pointer - memcpy(free_ptr, in_record->hdr.user1_ptr, in_record->hdr.user1_ptr_len); // Copy content - free_ptr += in_record->hdr.user1_ptr_len; - } - if(in_record->hdr.user2_ptr != NULL) { - out_record->hdr.user2_ptr = (uint8_t*)free_ptr; // Update pointer - memcpy(free_ptr, in_record->hdr.user2_ptr, in_record->hdr.user2_ptr_len); // Copy content - free_ptr += in_record->hdr.user2_ptr_len; - } - } - return; + bluetooth_sdp_record* out_records, int count) { + int i; + bluetooth_sdp_record* in_record; + bluetooth_sdp_record* out_record; + char* free_ptr = + (char*)(&out_records[count]); /* set pointer to after the last entry */ + + for (i = 0; i < count; i++) { + in_record = &in_records[i]; + out_record = &out_records[i]; + *out_record = *in_record; + + if (in_record->hdr.service_name == NULL || + in_record->hdr.service_name_length == 0) { + out_record->hdr.service_name = NULL; + out_record->hdr.service_name_length = 0; + } else { + out_record->hdr.service_name = free_ptr; // Update service_name pointer + // Copy string + memcpy(free_ptr, in_record->hdr.service_name, + in_record->hdr.service_name_length); + free_ptr += in_record->hdr.service_name_length; + *(free_ptr) = '\0'; // Set '\0' termination of string + free_ptr++; + } + if (in_record->hdr.user1_ptr != NULL) { + out_record->hdr.user1_ptr = (uint8_t*)free_ptr; // Update pointer + memcpy(free_ptr, in_record->hdr.user1_ptr, + in_record->hdr.user1_ptr_len); // Copy content + free_ptr += in_record->hdr.user1_ptr_len; + } + if (in_record->hdr.user2_ptr != NULL) { + out_record->hdr.user2_ptr = (uint8_t*)free_ptr; // Update pointer + memcpy(free_ptr, in_record->hdr.user2_ptr, + in_record->hdr.user2_ptr_len); // Copy content + free_ptr += in_record->hdr.user2_ptr_len; + } + } + return; } /* Reserve a slot in sdp_slots, copy data and set a reference to the copy. @@ -187,54 +189,54 @@ void copy_sdp_records(bluetooth_sdp_record* in_records, * user1_ptr and * user2_ptr. */ static int alloc_sdp_slot(bluetooth_sdp_record* in_record) { - int record_size = get_sdp_records_size(in_record, 1); - /* We are optimists here, and preallocate the record. - * This is to reduce the time we hold the sdp_lock. */ - bluetooth_sdp_record* record = (bluetooth_sdp_record*)osi_malloc(record_size); - - copy_sdp_records(in_record, record, 1); - { - std::unique_lock lock(sdp_lock); - for (int i = 0; i < MAX_SDP_SLOTS; i++) { - if(sdp_slots[i].state == SDP_RECORD_FREE) { - sdp_slots[i].state = SDP_RECORD_ALLOCED; - sdp_slots[i].record_data = record; - return i; - } + int record_size = get_sdp_records_size(in_record, 1); + /* We are optimists here, and preallocate the record. + * This is to reduce the time we hold the sdp_lock. */ + bluetooth_sdp_record* record = (bluetooth_sdp_record*)osi_malloc(record_size); + + copy_sdp_records(in_record, record, 1); + { + std::unique_lock lock(sdp_lock); + for (int i = 0; i < MAX_SDP_SLOTS; i++) { + if (sdp_slots[i].state == SDP_RECORD_FREE) { + sdp_slots[i].state = SDP_RECORD_ALLOCED; + sdp_slots[i].record_data = record; + return i; } } - APPL_TRACE_ERROR("%s() failed - no more free slots!", __func__); - /* Rearly the optimist is too optimistic, and cleanup is needed...*/ - osi_free(record); - return -1; + } + APPL_TRACE_ERROR("%s() failed - no more free slots!", __func__); + /* Rearly the optimist is too optimistic, and cleanup is needed...*/ + osi_free(record); + return -1; } static int free_sdp_slot(int id) { - int handle = -1; - bluetooth_sdp_record* record = NULL; - if(id >= MAX_SDP_SLOTS) { - APPL_TRACE_ERROR("%s() failed - id %d is invalid", __func__, id); - return handle; - } + int handle = -1; + bluetooth_sdp_record* record = NULL; + if (id >= MAX_SDP_SLOTS) { + APPL_TRACE_ERROR("%s() failed - id %d is invalid", __func__, id); + return handle; + } - { - std::unique_lock lock(sdp_lock); - handle = sdp_slots[id].sdp_handle; - sdp_slots[id].sdp_handle = 0; - if (sdp_slots[id].state != SDP_RECORD_FREE) { - /* safe a copy of the pointer, and free after unlock() */ - record = sdp_slots[id].record_data; - } - sdp_slots[id].state = SDP_RECORD_FREE; + { + std::unique_lock lock(sdp_lock); + handle = sdp_slots[id].sdp_handle; + sdp_slots[id].sdp_handle = 0; + if (sdp_slots[id].state != SDP_RECORD_FREE) { + /* safe a copy of the pointer, and free after unlock() */ + record = sdp_slots[id].record_data; } + sdp_slots[id].state = SDP_RECORD_FREE; + } - if (record != NULL) { - osi_free(record); - } else { - // Record have already been freed - handle = -1; - } - return handle; + if (record != NULL) { + osi_free(record); + } else { + // Record have already been freed + handle = -1; + } + return handle; } /*** @@ -242,60 +244,62 @@ static int free_sdp_slot(int id) { * SDP_RECORD_CREATE_INITIATED. */ static const sdp_slot_t* start_create_sdp(int id) { - if (id >= MAX_SDP_SLOTS) { - APPL_TRACE_ERROR("%s() failed - id %d is invalid", __func__, id); - return NULL; - } - - std::unique_lock lock(sdp_lock); - if (sdp_slots[id].state != SDP_RECORD_ALLOCED) { - /* The record have been removed before this event occurred - e.g. deinit */ - APPL_TRACE_ERROR("%s() failed - state for id %d is " - "sdp_slots[id].state = %d expected %d", __func__, - id, sdp_slots[id].state, SDP_RECORD_ALLOCED); - return NULL; - } - - return &(sdp_slots[id]); + if (id >= MAX_SDP_SLOTS) { + APPL_TRACE_ERROR("%s() failed - id %d is invalid", __func__, id); + return NULL; + } + + std::unique_lock lock(sdp_lock); + if (sdp_slots[id].state != SDP_RECORD_ALLOCED) { + /* The record have been removed before this event occurred - e.g. deinit */ + APPL_TRACE_ERROR( + "%s() failed - state for id %d is " + "sdp_slots[id].state = %d expected %d", + __func__, id, sdp_slots[id].state, SDP_RECORD_ALLOCED); + return NULL; + } + + return &(sdp_slots[id]); } static void set_sdp_handle(int id, int handle) { - std::unique_lock lock(sdp_lock); - sdp_slots[id].sdp_handle = handle; - BTIF_TRACE_DEBUG("%s() id=%d to handle=0x%08x", __func__, id, handle); + std::unique_lock lock(sdp_lock); + sdp_slots[id].sdp_handle = handle; + BTIF_TRACE_DEBUG("%s() id=%d to handle=0x%08x", __func__, id, handle); } -bt_status_t create_sdp_record(bluetooth_sdp_record *record, int* record_handle) { - int handle; +bt_status_t create_sdp_record(bluetooth_sdp_record* record, + int* record_handle) { + int handle; - handle = alloc_sdp_slot(record); - BTIF_TRACE_DEBUG("%s() handle = 0x%08x", __func__, handle); + handle = alloc_sdp_slot(record); + BTIF_TRACE_DEBUG("%s() handle = 0x%08x", __func__, handle); - if(handle < 0) - return BT_STATUS_FAIL; + if (handle < 0) return BT_STATUS_FAIL; - BTA_SdpCreateRecordByUser(INT_TO_PTR(handle)); + BTA_SdpCreateRecordByUser(INT_TO_PTR(handle)); - *record_handle = handle; + *record_handle = handle; - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } bt_status_t remove_sdp_record(int record_id) { - int handle; + int handle; - /* Get the Record handle, and free the slot */ - handle = free_sdp_slot(record_id); - BTIF_TRACE_DEBUG("Sdp Server %s id=%d to handle=0x%08x", - __func__, record_id, handle); + /* Get the Record handle, and free the slot */ + handle = free_sdp_slot(record_id); + BTIF_TRACE_DEBUG("Sdp Server %s id=%d to handle=0x%08x", __func__, record_id, + handle); - /* Pass the actual record handle */ - if(handle > 0) { - BTA_SdpRemoveRecordByUser(INT_TO_PTR(handle)); - return BT_STATUS_SUCCESS; - } - BTIF_TRACE_DEBUG("Sdp Server %s - record already removed - or never created", __func__); - return BT_STATUS_FAIL; + /* Pass the actual record handle */ + if (handle > 0) { + BTA_SdpRemoveRecordByUser(INT_TO_PTR(handle)); + return BT_STATUS_SUCCESS; + } + BTIF_TRACE_DEBUG("Sdp Server %s - record already removed - or never created", + __func__); + return BT_STATUS_FAIL; } /****************************************************************************** @@ -304,468 +308,455 @@ bt_status_t remove_sdp_record(int record_id) { ******************************************************************************/ void on_create_record_event(int id) { - /* - * 1) Fetch the record pointer, and change its state? - * 2) switch on the type to create the correct record - * 3) Update state on completion - * 4) What to do at fail? - * */ - BTIF_TRACE_DEBUG("Sdp Server %s", __func__); - const sdp_slot_t* sdp_slot = start_create_sdp(id); - /* In the case we are shutting down, sdp_slot is NULL */ - if(sdp_slot != NULL) { - bluetooth_sdp_record* record = sdp_slot->record_data; - int handle = -1; - switch(record->hdr.type) { - case SDP_TYPE_MAP_MAS: - handle = add_maps_sdp(&record->mas); - break; - case SDP_TYPE_MAP_MNS: - handle = add_mapc_sdp(&record->mns); - break; - case SDP_TYPE_PBAP_PSE: - handle = add_pbaps_sdp(&record->pse); - break; - case SDP_TYPE_OPP_SERVER: - handle = add_opps_sdp(&record->ops); - break; - case SDP_TYPE_SAP_SERVER: - handle = add_saps_sdp(&record->sap); - break; - case SDP_TYPE_PBAP_PCE: - // break; not yet supported - default: - BTIF_TRACE_DEBUG("Record type %d is not supported",record->hdr.type); - break; - } - if(handle != -1) { - set_sdp_handle(id, handle); - } - } + /* + * 1) Fetch the record pointer, and change its state? + * 2) switch on the type to create the correct record + * 3) Update state on completion + * 4) What to do at fail? + * */ + BTIF_TRACE_DEBUG("Sdp Server %s", __func__); + const sdp_slot_t* sdp_slot = start_create_sdp(id); + /* In the case we are shutting down, sdp_slot is NULL */ + if (sdp_slot != NULL) { + bluetooth_sdp_record* record = sdp_slot->record_data; + int handle = -1; + switch (record->hdr.type) { + case SDP_TYPE_MAP_MAS: + handle = add_maps_sdp(&record->mas); + break; + case SDP_TYPE_MAP_MNS: + handle = add_mapc_sdp(&record->mns); + break; + case SDP_TYPE_PBAP_PSE: + handle = add_pbaps_sdp(&record->pse); + break; + case SDP_TYPE_OPP_SERVER: + handle = add_opps_sdp(&record->ops); + break; + case SDP_TYPE_SAP_SERVER: + handle = add_saps_sdp(&record->sap); + break; + case SDP_TYPE_PBAP_PCE: + // break; not yet supported + default: + BTIF_TRACE_DEBUG("Record type %d is not supported", record->hdr.type); + break; + } + if (handle != -1) { + set_sdp_handle(id, handle); + } + } } void on_remove_record_event(int handle) { - BTIF_TRACE_DEBUG("Sdp Server %s", __func__); - - // User data carries the actual SDP handle, not the ID. - if(handle != -1 && handle != 0) { - bool result; - result = SDP_DeleteRecord( handle ); - if(result == false) { - BTIF_TRACE_ERROR(" Unable to remove handle 0x%08x", handle); - } + BTIF_TRACE_DEBUG("Sdp Server %s", __func__); + + // User data carries the actual SDP handle, not the ID. + if (handle != -1 && handle != 0) { + bool result; + result = SDP_DeleteRecord(handle); + if (result == false) { + BTIF_TRACE_ERROR(" Unable to remove handle 0x%08x", handle); } + } } /**** - * Below the actual functions accessing BTA context data - hence only call from BTA context! + * Below the actual functions accessing BTA context data - hence only call from + * BTA context! */ -/* Create a MAP MAS SDP record based on information stored in a bluetooth_sdp_mas_record */ -static int add_maps_sdp(const bluetooth_sdp_mas_record* rec) -{ - - tSDP_PROTOCOL_ELEM protoList [3]; - uint16_t service = UUID_SERVCLASS_MESSAGE_ACCESS; - uint16_t browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP; - bool status = true; - uint32_t sdp_handle = 0; - uint8_t temp[4]; - uint8_t* p_temp = temp; - - APPL_TRACE_DEBUG("%s(): MASID = 0x%02x, scn 0x%02x, psm = 0x%04x\n service name %s", __func__, - rec->mas_instance_id, rec->hdr.rfcomm_channel_number, - rec->hdr.l2cap_psm, rec->hdr.service_name); - - APPL_TRACE_DEBUG(" msg_types: 0x%02x, feature_bits: 0x%08x", - rec->supported_message_types, rec->supported_features); - - if ((sdp_handle = SDP_CreateRecord()) == 0) - { - APPL_TRACE_ERROR("%s() - Unable to register MAPS Service", __func__); - return sdp_handle; - } - - /* add service class */ - status &= SDP_AddServiceClassIdList(sdp_handle, 1, &service); - memset( protoList, 0 , 3*sizeof(tSDP_PROTOCOL_ELEM) ); - - /* add protocol list, including RFCOMM scn */ - protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP; - protoList[0].num_params = 0; - protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM; - protoList[1].num_params = 1; - protoList[1].params[0] = rec->hdr.rfcomm_channel_number; - protoList[2].protocol_uuid = UUID_PROTOCOL_OBEX; - protoList[2].num_params = 0; - status &= SDP_AddProtocolList(sdp_handle, 3, protoList); - - /* Add a name entry */ - status &= SDP_AddAttribute(sdp_handle, - (uint16_t)ATTR_ID_SERVICE_NAME, - (uint8_t)TEXT_STR_DESC_TYPE, - (uint32_t)(rec->hdr.service_name_length + 1), - (uint8_t *)rec->hdr.service_name); - - /* Add in the Bluetooth Profile Descriptor List */ - status &= SDP_AddProfileDescriptorList(sdp_handle, - UUID_SERVCLASS_MAP_PROFILE, - rec->hdr.profile_version); - - /* Add MAS instance ID */ - status &= SDP_AddAttribute(sdp_handle, ATTR_ID_MAS_INSTANCE_ID, UINT_DESC_TYPE, - (uint32_t)1, (uint8_t*)&rec->mas_instance_id); - - /* Add supported message types */ - status &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_MSG_TYPE, UINT_DESC_TYPE, - (uint32_t)1, (uint8_t*)&rec->supported_message_types); - - /* Add supported feature */ - UINT32_TO_BE_STREAM(p_temp, rec->supported_features); - status &= SDP_AddAttribute(sdp_handle, ATTR_ID_MAP_SUPPORTED_FEATURES, - UINT_DESC_TYPE, (uint32_t)4, temp); - - /* Add the L2CAP PSM if present */ - if(rec->hdr.l2cap_psm != -1) { - p_temp = temp;// The macro modifies p_temp, hence rewind. - UINT16_TO_BE_STREAM(p_temp, rec->hdr.l2cap_psm); - status &= SDP_AddAttribute(sdp_handle, ATTR_ID_GOEP_L2CAP_PSM, - UINT_DESC_TYPE, (uint32_t)2, temp); - } - - /* Make the service browseable */ - status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse); - - if (!status) - { - SDP_DeleteRecord(sdp_handle); - sdp_handle = 0; - APPL_TRACE_ERROR("%s() FAILED", __func__); - } - else - { - bta_sys_add_uuid(service); /* UUID_SERVCLASS_MESSAGE_ACCESS */ - APPL_TRACE_DEBUG("%s(): SDP Registered (handle 0x%08x)", __func__, sdp_handle); - } +/* Create a MAP MAS SDP record based on information stored in a + * bluetooth_sdp_mas_record */ +static int add_maps_sdp(const bluetooth_sdp_mas_record* rec) { + tSDP_PROTOCOL_ELEM protoList[3]; + uint16_t service = UUID_SERVCLASS_MESSAGE_ACCESS; + uint16_t browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP; + bool status = true; + uint32_t sdp_handle = 0; + uint8_t temp[4]; + uint8_t* p_temp = temp; + + APPL_TRACE_DEBUG( + "%s(): MASID = 0x%02x, scn 0x%02x, psm = 0x%04x\n service name %s", + __func__, rec->mas_instance_id, rec->hdr.rfcomm_channel_number, + rec->hdr.l2cap_psm, rec->hdr.service_name); + + APPL_TRACE_DEBUG(" msg_types: 0x%02x, feature_bits: 0x%08x", + rec->supported_message_types, rec->supported_features); + + if ((sdp_handle = SDP_CreateRecord()) == 0) { + APPL_TRACE_ERROR("%s() - Unable to register MAPS Service", __func__); return sdp_handle; + } + + /* add service class */ + status &= SDP_AddServiceClassIdList(sdp_handle, 1, &service); + memset(protoList, 0, 3 * sizeof(tSDP_PROTOCOL_ELEM)); + + /* add protocol list, including RFCOMM scn */ + protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP; + protoList[0].num_params = 0; + protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM; + protoList[1].num_params = 1; + protoList[1].params[0] = rec->hdr.rfcomm_channel_number; + protoList[2].protocol_uuid = UUID_PROTOCOL_OBEX; + protoList[2].num_params = 0; + status &= SDP_AddProtocolList(sdp_handle, 3, protoList); + + /* Add a name entry */ + status &= SDP_AddAttribute(sdp_handle, (uint16_t)ATTR_ID_SERVICE_NAME, + (uint8_t)TEXT_STR_DESC_TYPE, + (uint32_t)(rec->hdr.service_name_length + 1), + (uint8_t*)rec->hdr.service_name); + + /* Add in the Bluetooth Profile Descriptor List */ + status &= SDP_AddProfileDescriptorList(sdp_handle, UUID_SERVCLASS_MAP_PROFILE, + rec->hdr.profile_version); + + /* Add MAS instance ID */ + status &= + SDP_AddAttribute(sdp_handle, ATTR_ID_MAS_INSTANCE_ID, UINT_DESC_TYPE, + (uint32_t)1, (uint8_t*)&rec->mas_instance_id); + + /* Add supported message types */ + status &= + SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_MSG_TYPE, UINT_DESC_TYPE, + (uint32_t)1, (uint8_t*)&rec->supported_message_types); + + /* Add supported feature */ + UINT32_TO_BE_STREAM(p_temp, rec->supported_features); + status &= SDP_AddAttribute(sdp_handle, ATTR_ID_MAP_SUPPORTED_FEATURES, + UINT_DESC_TYPE, (uint32_t)4, temp); + + /* Add the L2CAP PSM if present */ + if (rec->hdr.l2cap_psm != -1) { + p_temp = temp; // The macro modifies p_temp, hence rewind. + UINT16_TO_BE_STREAM(p_temp, rec->hdr.l2cap_psm); + status &= SDP_AddAttribute(sdp_handle, ATTR_ID_GOEP_L2CAP_PSM, + UINT_DESC_TYPE, (uint32_t)2, temp); + } + + /* Make the service browseable */ + status &= + SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse); + + if (!status) { + SDP_DeleteRecord(sdp_handle); + sdp_handle = 0; + APPL_TRACE_ERROR("%s() FAILED", __func__); + } else { + bta_sys_add_uuid(service); /* UUID_SERVCLASS_MESSAGE_ACCESS */ + APPL_TRACE_DEBUG("%s(): SDP Registered (handle 0x%08x)", __func__, + sdp_handle); + } + return sdp_handle; } -/* Create a MAP MNS SDP record based on information stored in a bluetooth_sdp_mns_record */ -static int add_mapc_sdp(const bluetooth_sdp_mns_record* rec) -{ - - tSDP_PROTOCOL_ELEM protoList [3]; - uint16_t service = UUID_SERVCLASS_MESSAGE_NOTIFICATION; - uint16_t browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP; - bool status = true; - uint32_t sdp_handle = 0; - uint8_t temp[4]; - uint8_t* p_temp = temp; - - APPL_TRACE_DEBUG("%s(): scn 0x%02x, psm = 0x%04x\n service name %s", __func__, - rec->hdr.rfcomm_channel_number, rec->hdr.l2cap_psm, rec->hdr.service_name); - - APPL_TRACE_DEBUG(" feature_bits: 0x%08x", rec->supported_features); - - if ((sdp_handle = SDP_CreateRecord()) == 0) - { - APPL_TRACE_ERROR("%s(): Unable to register MAP Notification Service", __func__); - return sdp_handle; - } - - /* add service class */ - status &= SDP_AddServiceClassIdList(sdp_handle, 1, &service); - memset( protoList, 0 , 3*sizeof(tSDP_PROTOCOL_ELEM) ); - - /* add protocol list, including RFCOMM scn */ - protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP; - protoList[0].num_params = 0; - protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM; - protoList[1].num_params = 1; - protoList[1].params[0] = rec->hdr.rfcomm_channel_number; - protoList[2].protocol_uuid = UUID_PROTOCOL_OBEX; - protoList[2].num_params = 0; - status &= SDP_AddProtocolList(sdp_handle, 3, protoList); - - /* Add a name entry */ - status &= SDP_AddAttribute(sdp_handle, - (uint16_t)ATTR_ID_SERVICE_NAME, - (uint8_t)TEXT_STR_DESC_TYPE, - (uint32_t)(rec->hdr.service_name_length + 1), - (uint8_t *)rec->hdr.service_name); - - /* Add in the Bluetooth Profile Descriptor List */ - status &= SDP_AddProfileDescriptorList(sdp_handle, - UUID_SERVCLASS_MAP_PROFILE, - rec->hdr.profile_version); - - /* Add supported feature */ - UINT32_TO_BE_STREAM(p_temp, rec->supported_features); - status &= SDP_AddAttribute(sdp_handle, ATTR_ID_MAP_SUPPORTED_FEATURES, - UINT_DESC_TYPE, (uint32_t)4, temp); - - /* Add the L2CAP PSM if present */ - if(rec->hdr.l2cap_psm != -1) { - p_temp = temp;// The macro modifies p_temp, hence rewind. - UINT16_TO_BE_STREAM(p_temp, rec->hdr.l2cap_psm); - status &= SDP_AddAttribute(sdp_handle, ATTR_ID_GOEP_L2CAP_PSM, - UINT_DESC_TYPE, (uint32_t)2, temp); - } - - /* Make the service browseable */ - status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse); - - if (!status) - { - SDP_DeleteRecord(sdp_handle); - sdp_handle = 0; - APPL_TRACE_ERROR("%s() FAILED", __func__); - } - else - { - bta_sys_add_uuid(service); /* UUID_SERVCLASS_MESSAGE_ACCESS */ - APPL_TRACE_DEBUG("%s(): SDP Registered (handle 0x%08x)", __func__, sdp_handle); - } +/* Create a MAP MNS SDP record based on information stored in a + * bluetooth_sdp_mns_record */ +static int add_mapc_sdp(const bluetooth_sdp_mns_record* rec) { + tSDP_PROTOCOL_ELEM protoList[3]; + uint16_t service = UUID_SERVCLASS_MESSAGE_NOTIFICATION; + uint16_t browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP; + bool status = true; + uint32_t sdp_handle = 0; + uint8_t temp[4]; + uint8_t* p_temp = temp; + + APPL_TRACE_DEBUG("%s(): scn 0x%02x, psm = 0x%04x\n service name %s", + __func__, rec->hdr.rfcomm_channel_number, rec->hdr.l2cap_psm, + rec->hdr.service_name); + + APPL_TRACE_DEBUG(" feature_bits: 0x%08x", rec->supported_features); + + if ((sdp_handle = SDP_CreateRecord()) == 0) { + APPL_TRACE_ERROR("%s(): Unable to register MAP Notification Service", + __func__); return sdp_handle; + } + + /* add service class */ + status &= SDP_AddServiceClassIdList(sdp_handle, 1, &service); + memset(protoList, 0, 3 * sizeof(tSDP_PROTOCOL_ELEM)); + + /* add protocol list, including RFCOMM scn */ + protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP; + protoList[0].num_params = 0; + protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM; + protoList[1].num_params = 1; + protoList[1].params[0] = rec->hdr.rfcomm_channel_number; + protoList[2].protocol_uuid = UUID_PROTOCOL_OBEX; + protoList[2].num_params = 0; + status &= SDP_AddProtocolList(sdp_handle, 3, protoList); + + /* Add a name entry */ + status &= SDP_AddAttribute(sdp_handle, (uint16_t)ATTR_ID_SERVICE_NAME, + (uint8_t)TEXT_STR_DESC_TYPE, + (uint32_t)(rec->hdr.service_name_length + 1), + (uint8_t*)rec->hdr.service_name); + + /* Add in the Bluetooth Profile Descriptor List */ + status &= SDP_AddProfileDescriptorList(sdp_handle, UUID_SERVCLASS_MAP_PROFILE, + rec->hdr.profile_version); + + /* Add supported feature */ + UINT32_TO_BE_STREAM(p_temp, rec->supported_features); + status &= SDP_AddAttribute(sdp_handle, ATTR_ID_MAP_SUPPORTED_FEATURES, + UINT_DESC_TYPE, (uint32_t)4, temp); + + /* Add the L2CAP PSM if present */ + if (rec->hdr.l2cap_psm != -1) { + p_temp = temp; // The macro modifies p_temp, hence rewind. + UINT16_TO_BE_STREAM(p_temp, rec->hdr.l2cap_psm); + status &= SDP_AddAttribute(sdp_handle, ATTR_ID_GOEP_L2CAP_PSM, + UINT_DESC_TYPE, (uint32_t)2, temp); + } + + /* Make the service browseable */ + status &= + SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse); + + if (!status) { + SDP_DeleteRecord(sdp_handle); + sdp_handle = 0; + APPL_TRACE_ERROR("%s() FAILED", __func__); + } else { + bta_sys_add_uuid(service); /* UUID_SERVCLASS_MESSAGE_ACCESS */ + APPL_TRACE_DEBUG("%s(): SDP Registered (handle 0x%08x)", __func__, + sdp_handle); + } + return sdp_handle; } -/* Create a PBAP Server SDP record based on information stored in a bluetooth_sdp_pse_record */ -static int add_pbaps_sdp(const bluetooth_sdp_pse_record* rec) -{ - - tSDP_PROTOCOL_ELEM protoList [3]; - uint16_t service = UUID_SERVCLASS_PBAP_PSE; - uint16_t browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP; - bool status = true; - uint32_t sdp_handle = 0; - uint8_t temp[4]; - uint8_t* p_temp = temp; - - APPL_TRACE_DEBUG("%s(): scn 0x%02x, psm = 0x%04x\n service name %s", __func__, - rec->hdr.rfcomm_channel_number, rec->hdr.l2cap_psm, rec->hdr.service_name); - - APPL_TRACE_DEBUG(" supported_repositories: 0x%08x, feature_bits: 0x%08x", - rec->supported_repositories, rec->supported_features); - - if ((sdp_handle = SDP_CreateRecord()) == 0) - { - APPL_TRACE_ERROR("%s(): Unable to register PBAP Server Service", __func__); - return sdp_handle; - } - - /* add service class */ - status &= SDP_AddServiceClassIdList(sdp_handle, 1, &service); - memset( protoList, 0 , 3*sizeof(tSDP_PROTOCOL_ELEM) ); - - /* add protocol list, including RFCOMM scn */ - protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP; - protoList[0].num_params = 0; - protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM; - protoList[1].num_params = 1; - protoList[1].params[0] = rec->hdr.rfcomm_channel_number; - protoList[2].protocol_uuid = UUID_PROTOCOL_OBEX; - protoList[2].num_params = 0; - status &= SDP_AddProtocolList(sdp_handle, 3, protoList); - - /* Add a name entry */ - status &= SDP_AddAttribute(sdp_handle, - (uint16_t)ATTR_ID_SERVICE_NAME, - (uint8_t)TEXT_STR_DESC_TYPE, - (uint32_t)(rec->hdr.service_name_length + 1), - (uint8_t *)rec->hdr.service_name); - - /* Add in the Bluetooth Profile Descriptor List */ - status &= SDP_AddProfileDescriptorList(sdp_handle, - UUID_SERVCLASS_PHONE_ACCESS, - rec->hdr.profile_version); - - /* Add supported repositories 1 byte */ - status &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_REPOSITORIES, - UINT_DESC_TYPE, (uint32_t)1, (uint8_t*)&rec->supported_repositories); - - /* Add supported feature 4 bytes*/ - UINT32_TO_BE_STREAM(p_temp, rec->supported_features); - status &= SDP_AddAttribute(sdp_handle, ATTR_ID_PBAP_SUPPORTED_FEATURES, - UINT_DESC_TYPE, (uint32_t)4, temp); - - /* Add the L2CAP PSM if present */ - if(rec->hdr.l2cap_psm != -1) { - p_temp = temp;// The macro modifies p_temp, hence rewind. - UINT16_TO_BE_STREAM(p_temp, rec->hdr.l2cap_psm); - status &= SDP_AddAttribute(sdp_handle, ATTR_ID_GOEP_L2CAP_PSM, - UINT_DESC_TYPE, (uint32_t)2, temp); - } - - /* Make the service browseable */ - status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse); - - if (!status) - { - SDP_DeleteRecord(sdp_handle); - sdp_handle = 0; - APPL_TRACE_ERROR("%s() FAILED", __func__); - } - else - { - bta_sys_add_uuid(service); /* UUID_SERVCLASS_MESSAGE_ACCESS */ - APPL_TRACE_DEBUG("%s(): SDP Registered (handle 0x%08x)", __func__, sdp_handle); - } +/* Create a PBAP Server SDP record based on information stored in a + * bluetooth_sdp_pse_record */ +static int add_pbaps_sdp(const bluetooth_sdp_pse_record* rec) { + tSDP_PROTOCOL_ELEM protoList[3]; + uint16_t service = UUID_SERVCLASS_PBAP_PSE; + uint16_t browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP; + bool status = true; + uint32_t sdp_handle = 0; + uint8_t temp[4]; + uint8_t* p_temp = temp; + + APPL_TRACE_DEBUG("%s(): scn 0x%02x, psm = 0x%04x\n service name %s", + __func__, rec->hdr.rfcomm_channel_number, rec->hdr.l2cap_psm, + rec->hdr.service_name); + + APPL_TRACE_DEBUG(" supported_repositories: 0x%08x, feature_bits: 0x%08x", + rec->supported_repositories, rec->supported_features); + + if ((sdp_handle = SDP_CreateRecord()) == 0) { + APPL_TRACE_ERROR("%s(): Unable to register PBAP Server Service", __func__); return sdp_handle; + } + + /* add service class */ + status &= SDP_AddServiceClassIdList(sdp_handle, 1, &service); + memset(protoList, 0, 3 * sizeof(tSDP_PROTOCOL_ELEM)); + + /* add protocol list, including RFCOMM scn */ + protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP; + protoList[0].num_params = 0; + protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM; + protoList[1].num_params = 1; + protoList[1].params[0] = rec->hdr.rfcomm_channel_number; + protoList[2].protocol_uuid = UUID_PROTOCOL_OBEX; + protoList[2].num_params = 0; + status &= SDP_AddProtocolList(sdp_handle, 3, protoList); + + /* Add a name entry */ + status &= SDP_AddAttribute(sdp_handle, (uint16_t)ATTR_ID_SERVICE_NAME, + (uint8_t)TEXT_STR_DESC_TYPE, + (uint32_t)(rec->hdr.service_name_length + 1), + (uint8_t*)rec->hdr.service_name); + + /* Add in the Bluetooth Profile Descriptor List */ + status &= SDP_AddProfileDescriptorList( + sdp_handle, UUID_SERVCLASS_PHONE_ACCESS, rec->hdr.profile_version); + + /* Add supported repositories 1 byte */ + status &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_REPOSITORIES, + UINT_DESC_TYPE, (uint32_t)1, + (uint8_t*)&rec->supported_repositories); + + /* Add supported feature 4 bytes*/ + UINT32_TO_BE_STREAM(p_temp, rec->supported_features); + status &= SDP_AddAttribute(sdp_handle, ATTR_ID_PBAP_SUPPORTED_FEATURES, + UINT_DESC_TYPE, (uint32_t)4, temp); + + /* Add the L2CAP PSM if present */ + if (rec->hdr.l2cap_psm != -1) { + p_temp = temp; // The macro modifies p_temp, hence rewind. + UINT16_TO_BE_STREAM(p_temp, rec->hdr.l2cap_psm); + status &= SDP_AddAttribute(sdp_handle, ATTR_ID_GOEP_L2CAP_PSM, + UINT_DESC_TYPE, (uint32_t)2, temp); + } + + /* Make the service browseable */ + status &= + SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse); + + if (!status) { + SDP_DeleteRecord(sdp_handle); + sdp_handle = 0; + APPL_TRACE_ERROR("%s() FAILED", __func__); + } else { + bta_sys_add_uuid(service); /* UUID_SERVCLASS_MESSAGE_ACCESS */ + APPL_TRACE_DEBUG("%s(): SDP Registered (handle 0x%08x)", __func__, + sdp_handle); + } + return sdp_handle; } -/* Create a OPP Server SDP record based on information stored in a bluetooth_sdp_ops_record */ -static int add_opps_sdp(const bluetooth_sdp_ops_record* rec) -{ - - tSDP_PROTOCOL_ELEM protoList [3]; - uint16_t service = UUID_SERVCLASS_OBEX_OBJECT_PUSH; - uint16_t browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP; - uint8_t type_len[rec->supported_formats_list_len]; - uint8_t desc_type[rec->supported_formats_list_len]; - uint8_t *type_value[rec->supported_formats_list_len]; - bool status = true; - uint32_t sdp_handle = 0; - uint8_t temp[4]; - uint8_t* p_temp = temp; - tBTA_UTL_COD cod; - int i,j; - - APPL_TRACE_DEBUG("%s(): scn 0x%02x, psm = 0x%04x\n service name %s", __func__, - rec->hdr.rfcomm_channel_number, rec->hdr.l2cap_psm, rec->hdr.service_name); - - APPL_TRACE_DEBUG(" supported formats count: %d", - rec->supported_formats_list_len); - - if ((sdp_handle = SDP_CreateRecord()) == 0) - { - APPL_TRACE_ERROR("%s(): Unable to register Object Push Server Service", __func__); - return sdp_handle; - } - - /* add service class */ - status &= SDP_AddServiceClassIdList(sdp_handle, 1, &service); - memset( protoList, 0 , 3*sizeof(tSDP_PROTOCOL_ELEM) ); - - /* add protocol list, including RFCOMM scn */ - protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP; - protoList[0].num_params = 0; - protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM; - protoList[1].num_params = 1; - protoList[1].params[0] = rec->hdr.rfcomm_channel_number; - protoList[2].protocol_uuid = UUID_PROTOCOL_OBEX; - protoList[2].num_params = 0; - status &= SDP_AddProtocolList(sdp_handle, 3, protoList); - - /* Add a name entry */ - status &= SDP_AddAttribute(sdp_handle, - (uint16_t)ATTR_ID_SERVICE_NAME, - (uint8_t)TEXT_STR_DESC_TYPE, - (uint32_t)(rec->hdr.service_name_length + 1), - (uint8_t *)rec->hdr.service_name); - - /* Add in the Bluetooth Profile Descriptor List */ - status &= SDP_AddProfileDescriptorList(sdp_handle, - UUID_SERVCLASS_OBEX_OBJECT_PUSH, - rec->hdr.profile_version); - - /* add sequence for supported types */ - for (i = 0, j = 0; i < rec->supported_formats_list_len; i++) - { - type_value[j] = (uint8_t *) &rec->supported_formats_list[i]; - desc_type[j] = UINT_DESC_TYPE; - type_len[j++] = 1; - } - - status &= SDP_AddSequence(sdp_handle, (uint16_t) ATTR_ID_SUPPORTED_FORMATS_LIST, - (uint8_t) rec->supported_formats_list_len, desc_type, type_len, type_value); - - /* Add the L2CAP PSM if present */ - if(rec->hdr.l2cap_psm != -1) { - p_temp = temp;// The macro modifies p_temp, hence rewind. - UINT16_TO_BE_STREAM(p_temp, rec->hdr.l2cap_psm); - status &= SDP_AddAttribute(sdp_handle, ATTR_ID_GOEP_L2CAP_PSM, - UINT_DESC_TYPE, (uint32_t)2, temp); - } - - /* Make the service browseable */ - status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse); - - if (!status) - { - SDP_DeleteRecord(sdp_handle); - sdp_handle = 0; - APPL_TRACE_ERROR("%s() FAILED", __func__); - } - else - { - /* set class of device */ - cod.service = BTM_COD_SERVICE_OBJ_TRANSFER; - utl_set_device_class(&cod, BTA_UTL_SET_COD_SERVICE_CLASS); - - bta_sys_add_uuid(service); /* UUID_SERVCLASS_OBEX_OBJECT_PUSH */ - APPL_TRACE_DEBUG("%s(): SDP Registered (handle 0x%08x)", __func__, sdp_handle); - } +/* Create a OPP Server SDP record based on information stored in a + * bluetooth_sdp_ops_record */ +static int add_opps_sdp(const bluetooth_sdp_ops_record* rec) { + tSDP_PROTOCOL_ELEM protoList[3]; + uint16_t service = UUID_SERVCLASS_OBEX_OBJECT_PUSH; + uint16_t browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP; + uint8_t type_len[rec->supported_formats_list_len]; + uint8_t desc_type[rec->supported_formats_list_len]; + uint8_t* type_value[rec->supported_formats_list_len]; + bool status = true; + uint32_t sdp_handle = 0; + uint8_t temp[4]; + uint8_t* p_temp = temp; + tBTA_UTL_COD cod; + int i, j; + + APPL_TRACE_DEBUG("%s(): scn 0x%02x, psm = 0x%04x\n service name %s", + __func__, rec->hdr.rfcomm_channel_number, rec->hdr.l2cap_psm, + rec->hdr.service_name); + + APPL_TRACE_DEBUG(" supported formats count: %d", + rec->supported_formats_list_len); + + if ((sdp_handle = SDP_CreateRecord()) == 0) { + APPL_TRACE_ERROR("%s(): Unable to register Object Push Server Service", + __func__); return sdp_handle; + } + + /* add service class */ + status &= SDP_AddServiceClassIdList(sdp_handle, 1, &service); + memset(protoList, 0, 3 * sizeof(tSDP_PROTOCOL_ELEM)); + + /* add protocol list, including RFCOMM scn */ + protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP; + protoList[0].num_params = 0; + protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM; + protoList[1].num_params = 1; + protoList[1].params[0] = rec->hdr.rfcomm_channel_number; + protoList[2].protocol_uuid = UUID_PROTOCOL_OBEX; + protoList[2].num_params = 0; + status &= SDP_AddProtocolList(sdp_handle, 3, protoList); + + /* Add a name entry */ + status &= SDP_AddAttribute(sdp_handle, (uint16_t)ATTR_ID_SERVICE_NAME, + (uint8_t)TEXT_STR_DESC_TYPE, + (uint32_t)(rec->hdr.service_name_length + 1), + (uint8_t*)rec->hdr.service_name); + + /* Add in the Bluetooth Profile Descriptor List */ + status &= SDP_AddProfileDescriptorList( + sdp_handle, UUID_SERVCLASS_OBEX_OBJECT_PUSH, rec->hdr.profile_version); + + /* add sequence for supported types */ + for (i = 0, j = 0; i < rec->supported_formats_list_len; i++) { + type_value[j] = (uint8_t*)&rec->supported_formats_list[i]; + desc_type[j] = UINT_DESC_TYPE; + type_len[j++] = 1; + } + + status &= + SDP_AddSequence(sdp_handle, (uint16_t)ATTR_ID_SUPPORTED_FORMATS_LIST, + (uint8_t)rec->supported_formats_list_len, desc_type, + type_len, type_value); + + /* Add the L2CAP PSM if present */ + if (rec->hdr.l2cap_psm != -1) { + p_temp = temp; // The macro modifies p_temp, hence rewind. + UINT16_TO_BE_STREAM(p_temp, rec->hdr.l2cap_psm); + status &= SDP_AddAttribute(sdp_handle, ATTR_ID_GOEP_L2CAP_PSM, + UINT_DESC_TYPE, (uint32_t)2, temp); + } + + /* Make the service browseable */ + status &= + SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse); + + if (!status) { + SDP_DeleteRecord(sdp_handle); + sdp_handle = 0; + APPL_TRACE_ERROR("%s() FAILED", __func__); + } else { + /* set class of device */ + cod.service = BTM_COD_SERVICE_OBJ_TRANSFER; + utl_set_device_class(&cod, BTA_UTL_SET_COD_SERVICE_CLASS); + + bta_sys_add_uuid(service); /* UUID_SERVCLASS_OBEX_OBJECT_PUSH */ + APPL_TRACE_DEBUG("%s(): SDP Registered (handle 0x%08x)", __func__, + sdp_handle); + } + return sdp_handle; } -// Create a Sim Access Profile SDP record based on information stored in a bluetooth_sdp_sap_record. -static int add_saps_sdp(const bluetooth_sdp_sap_record* rec) -{ - tSDP_PROTOCOL_ELEM protoList [2]; - uint16_t services[2]; - uint16_t browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP; - bool status = true; - uint32_t sdp_handle = 0; - - APPL_TRACE_DEBUG("%s(): scn 0x%02x, service name %s", __func__, - rec->hdr.rfcomm_channel_number, rec->hdr.service_name); - - if ((sdp_handle = SDP_CreateRecord()) == 0) - { - APPL_TRACE_ERROR("%s(): Unable to register SAPS Service", __func__); - return sdp_handle; - } +// Create a Sim Access Profile SDP record based on information stored in a +// bluetooth_sdp_sap_record. +static int add_saps_sdp(const bluetooth_sdp_sap_record* rec) { + tSDP_PROTOCOL_ELEM protoList[2]; + uint16_t services[2]; + uint16_t browse = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP; + bool status = true; + uint32_t sdp_handle = 0; - services[0] = UUID_SERVCLASS_SAP; - services[1] = UUID_SERVCLASS_GENERIC_TELEPHONY; - - // add service class - status &= SDP_AddServiceClassIdList(sdp_handle, 2, services); - memset(protoList, 0, 2 * sizeof(tSDP_PROTOCOL_ELEM)); - - // add protocol list, including RFCOMM scn - protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP; - protoList[0].num_params = 0; - protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM; - protoList[1].num_params = 1; - protoList[1].params[0] = rec->hdr.rfcomm_channel_number; - status &= SDP_AddProtocolList(sdp_handle, 2, protoList); - - // Add a name entry - status &= SDP_AddAttribute(sdp_handle, - (uint16_t)ATTR_ID_SERVICE_NAME, - (uint8_t)TEXT_STR_DESC_TYPE, - (uint32_t)(rec->hdr.service_name_length + 1), - (uint8_t *)rec->hdr.service_name); - - // Add in the Bluetooth Profile Descriptor List - status &= SDP_AddProfileDescriptorList(sdp_handle, - UUID_SERVCLASS_SAP, - rec->hdr.profile_version); - - // Make the service browseable - status &= SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse); - - if (!status) - { - SDP_DeleteRecord(sdp_handle); - sdp_handle = 0; - APPL_TRACE_ERROR("%s(): FAILED deleting record", __func__); - } - else - { - bta_sys_add_uuid(UUID_SERVCLASS_SAP); - APPL_TRACE_DEBUG("%s(): SDP Registered (handle 0x%08x)", __func__, sdp_handle); - } + APPL_TRACE_DEBUG("%s(): scn 0x%02x, service name %s", __func__, + rec->hdr.rfcomm_channel_number, rec->hdr.service_name); + + if ((sdp_handle = SDP_CreateRecord()) == 0) { + APPL_TRACE_ERROR("%s(): Unable to register SAPS Service", __func__); return sdp_handle; + } + + services[0] = UUID_SERVCLASS_SAP; + services[1] = UUID_SERVCLASS_GENERIC_TELEPHONY; + + // add service class + status &= SDP_AddServiceClassIdList(sdp_handle, 2, services); + memset(protoList, 0, 2 * sizeof(tSDP_PROTOCOL_ELEM)); + + // add protocol list, including RFCOMM scn + protoList[0].protocol_uuid = UUID_PROTOCOL_L2CAP; + protoList[0].num_params = 0; + protoList[1].protocol_uuid = UUID_PROTOCOL_RFCOMM; + protoList[1].num_params = 1; + protoList[1].params[0] = rec->hdr.rfcomm_channel_number; + status &= SDP_AddProtocolList(sdp_handle, 2, protoList); + + // Add a name entry + status &= SDP_AddAttribute(sdp_handle, (uint16_t)ATTR_ID_SERVICE_NAME, + (uint8_t)TEXT_STR_DESC_TYPE, + (uint32_t)(rec->hdr.service_name_length + 1), + (uint8_t*)rec->hdr.service_name); + + // Add in the Bluetooth Profile Descriptor List + status &= SDP_AddProfileDescriptorList(sdp_handle, UUID_SERVCLASS_SAP, + rec->hdr.profile_version); + + // Make the service browseable + status &= + SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &browse); + + if (!status) { + SDP_DeleteRecord(sdp_handle); + sdp_handle = 0; + APPL_TRACE_ERROR("%s(): FAILED deleting record", __func__); + } else { + bta_sys_add_uuid(UUID_SERVCLASS_SAP); + APPL_TRACE_DEBUG("%s(): SDP Registered (handle 0x%08x)", __func__, + sdp_handle); + } + return sdp_handle; } diff --git a/btif/src/btif_sm.cc b/btif/src/btif_sm.cc index 7635aadd3..9b2b033cd 100644 --- a/btif/src/btif_sm.cc +++ b/btif/src/btif_sm.cc @@ -28,159 +28,154 @@ #include "btif_sm.h" -#include "btif_common.h" #include "bt_common.h" +#include "btif_common.h" #include "osi/include/allocator.h" /***************************************************************************** -** Local type definitions -******************************************************************************/ + * Local type definitions + *****************************************************************************/ typedef struct { - btif_sm_state_t state; - btif_sm_handler_t *p_handlers; + btif_sm_state_t state; + btif_sm_handler_t* p_handlers; } btif_sm_cb_t; /***************************************************************************** -** Functions -******************************************************************************/ + * Functions + *****************************************************************************/ /***************************************************************************** -** -** Function btif_sm_init -** -** Description Initializes the state machine with the state handlers -** The caller should ensure that the table and the corresponding -** states match. The location that 'p_handlers' points to shall -** be available until the btif_sm_shutdown API is invoked. -** -** Returns Returns a pointer to the initialized state machine handle. -** -******************************************************************************/ - -btif_sm_handle_t btif_sm_init(const btif_sm_handler_t *p_handlers, btif_sm_state_t initial_state) -{ - if (p_handlers == NULL) { - BTIF_TRACE_ERROR("%s : p_handlers is NULL", __func__); - return NULL; - } - - btif_sm_cb_t *p_cb = (btif_sm_cb_t *)osi_malloc(sizeof(btif_sm_cb_t)); - p_cb->state = initial_state; - p_cb->p_handlers = (btif_sm_handler_t*)p_handlers; - - /* Send BTIF_SM_ENTER_EVT to the initial state */ - p_cb->p_handlers[initial_state](BTIF_SM_ENTER_EVT, NULL); - - return (btif_sm_handle_t)p_cb; + * + * Function btif_sm_init + * + * Description Initializes the state machine with the state handlers + * The caller should ensure that the table and the corresponding + * states match. The location that 'p_handlers' points to shall + * be available until the btif_sm_shutdown API is invoked. + * + * Returns Returns a pointer to the initialized state machine handle. + * + *****************************************************************************/ + +btif_sm_handle_t btif_sm_init(const btif_sm_handler_t* p_handlers, + btif_sm_state_t initial_state) { + if (p_handlers == NULL) { + BTIF_TRACE_ERROR("%s : p_handlers is NULL", __func__); + return NULL; + } + + btif_sm_cb_t* p_cb = (btif_sm_cb_t*)osi_malloc(sizeof(btif_sm_cb_t)); + p_cb->state = initial_state; + p_cb->p_handlers = (btif_sm_handler_t*)p_handlers; + + /* Send BTIF_SM_ENTER_EVT to the initial state */ + p_cb->p_handlers[initial_state](BTIF_SM_ENTER_EVT, NULL); + + return (btif_sm_handle_t)p_cb; } /***************************************************************************** -** -** Function btif_sm_shutdown -** -** Description Tears down the state machine -** -** Returns None -** -******************************************************************************/ -void btif_sm_shutdown(btif_sm_handle_t handle) -{ - btif_sm_cb_t *p_cb = (btif_sm_cb_t*)handle; - - if (p_cb == NULL) - { - BTIF_TRACE_ERROR("%s : Invalid handle", __func__); - return; - } - osi_free(p_cb); + * + * Function btif_sm_shutdown + * + * Description Tears down the state machine + * + * Returns None + * + *****************************************************************************/ +void btif_sm_shutdown(btif_sm_handle_t handle) { + btif_sm_cb_t* p_cb = (btif_sm_cb_t*)handle; + + if (p_cb == NULL) { + BTIF_TRACE_ERROR("%s : Invalid handle", __func__); + return; + } + osi_free(p_cb); } /***************************************************************************** -** -** Function btif_sm_get_state -** -** Description Fetches the current state of the state machine -** -** Returns Current state -** -******************************************************************************/ -btif_sm_state_t btif_sm_get_state(btif_sm_handle_t handle) -{ - btif_sm_cb_t *p_cb = (btif_sm_cb_t*)handle; - - if (p_cb == NULL) - { - BTIF_TRACE_ERROR("%s : Invalid handle", __func__); - return 0; - } - - return p_cb->state; + * + * Function btif_sm_get_state + * + * Description Fetches the current state of the state machine + * + * Returns Current state + * + *****************************************************************************/ +btif_sm_state_t btif_sm_get_state(btif_sm_handle_t handle) { + btif_sm_cb_t* p_cb = (btif_sm_cb_t*)handle; + + if (p_cb == NULL) { + BTIF_TRACE_ERROR("%s : Invalid handle", __func__); + return 0; + } + + return p_cb->state; } /***************************************************************************** -** -** Function btif_sm_dispatch -** -** Description Dispatches the 'event' along with 'data' to the current state handler -** -** Returns BT_STATUS_SUCCESS on success -** BT_STATUS_UNHANDLED if event was not processed -** BT_STATUS_FAIL otherwise -** -******************************************************************************/ + * + * Function btif_sm_dispatch + * + * Description Dispatches the 'event' along with 'data' to the current state + *handler + * + * Returns BT_STATUS_SUCCESS on success + * BT_STATUS_UNHANDLED if event was not processed + * BT_STATUS_FAIL otherwise + * + *****************************************************************************/ bt_status_t btif_sm_dispatch(btif_sm_handle_t handle, btif_sm_event_t event, - void *data) -{ - bt_status_t status = BT_STATUS_SUCCESS; + void* data) { + bt_status_t status = BT_STATUS_SUCCESS; - btif_sm_cb_t *p_cb = (btif_sm_cb_t*)handle; + btif_sm_cb_t* p_cb = (btif_sm_cb_t*)handle; - if (p_cb == NULL) - { - BTIF_TRACE_ERROR("%s : Invalid handle", __func__); - return BT_STATUS_FAIL; - } + if (p_cb == NULL) { + BTIF_TRACE_ERROR("%s : Invalid handle", __func__); + return BT_STATUS_FAIL; + } - if (p_cb->p_handlers[p_cb->state](event, data) == false) - return BT_STATUS_UNHANDLED; + if (p_cb->p_handlers[p_cb->state](event, data) == false) + return BT_STATUS_UNHANDLED; - return status; + return status; } /***************************************************************************** -** -** Function btif_sm_change_state -** -** Description Make a transition to the new 'state'. The 'BTIF_SM_EXIT_EVT' -** shall be invoked before exiting the current state. The -** 'BTIF_SM_ENTER_EVT' shall be invoked before entering the new state -** -** Returns BT_STATUS_SUCCESS on success -** BT_STATUS_UNHANDLED if event was not processed -** BT_STATUS_FAIL otherwise -** -******************************************************************************/ -bt_status_t btif_sm_change_state(btif_sm_handle_t handle, btif_sm_state_t state) -{ - bt_status_t status = BT_STATUS_SUCCESS; - btif_sm_cb_t *p_cb = (btif_sm_cb_t*)handle; - - if (p_cb == NULL) - { - BTIF_TRACE_ERROR("%s : Invalid handle", __func__); - return BT_STATUS_FAIL; - } - - /* Send exit event to the current state */ - if (p_cb->p_handlers[p_cb->state](BTIF_SM_EXIT_EVT, NULL) == false) - status = BT_STATUS_UNHANDLED; - - /* Change to the new state */ - p_cb->state = state; - - /* Send enter event to the new state */ - if (p_cb->p_handlers[p_cb->state](BTIF_SM_ENTER_EVT, NULL) == false) - status = BT_STATUS_UNHANDLED; - - return status; + * + * Function btif_sm_change_state + * + * Description Make a transition to the new 'state'. The 'BTIF_SM_EXIT_EVT' + * shall be invoked before exiting the current state. The + * 'BTIF_SM_ENTER_EVT' shall be invoked before entering the new + *state + * + * Returns BT_STATUS_SUCCESS on success + * BT_STATUS_UNHANDLED if event was not processed + * BT_STATUS_FAIL otherwise + * + *****************************************************************************/ +bt_status_t btif_sm_change_state(btif_sm_handle_t handle, + btif_sm_state_t state) { + bt_status_t status = BT_STATUS_SUCCESS; + btif_sm_cb_t* p_cb = (btif_sm_cb_t*)handle; + + if (p_cb == NULL) { + BTIF_TRACE_ERROR("%s : Invalid handle", __func__); + return BT_STATUS_FAIL; + } + + /* Send exit event to the current state */ + if (p_cb->p_handlers[p_cb->state](BTIF_SM_EXIT_EVT, NULL) == false) + status = BT_STATUS_UNHANDLED; + + /* Change to the new state */ + p_cb->state = state; + + /* Send enter event to the new state */ + if (p_cb->p_handlers[p_cb->state](BTIF_SM_ENTER_EVT, NULL) == false) + status = BT_STATUS_UNHANDLED; + + return status; } diff --git a/btif/src/btif_sock.cc b/btif/src/btif_sock.cc index 42747c455..deeddb78a 100644 --- a/btif/src/btif_sock.cc +++ b/btif/src/btif_sock.cc @@ -34,21 +34,22 @@ #include "btif_util.h" #include "osi/include/thread.h" -static bt_status_t btsock_listen(btsock_type_t type, const char *service_name, const uint8_t *uuid, int channel, int *sock_fd, int flags, int app_uid); -static bt_status_t btsock_connect(const bt_bdaddr_t *bd_addr, btsock_type_t type, const uint8_t *uuid, int channel, int *sock_fd, int flags, int app_uid); +static bt_status_t btsock_listen(btsock_type_t type, const char* service_name, + const uint8_t* uuid, int channel, int* sock_fd, + int flags, int app_uid); +static bt_status_t btsock_connect(const bt_bdaddr_t* bd_addr, + btsock_type_t type, const uint8_t* uuid, + int channel, int* sock_fd, int flags, + int app_uid); static void btsock_signaled(int fd, int type, int flags, uint32_t user_id); static int thread_handle = -1; -static thread_t *thread; - -btsock_interface_t *btif_sock_get_interface(void) { - static btsock_interface_t interface = { - sizeof(interface), - btsock_listen, - btsock_connect - }; +static thread_t* thread; +btsock_interface_t* btif_sock_get_interface(void) { + static btsock_interface_t interface = {sizeof(interface), btsock_listen, + btsock_connect}; return &interface; } @@ -67,13 +68,15 @@ bt_status_t btif_sock_init(uid_set_t* uid_set) { status = btsock_rfc_init(thread_handle, uid_set); if (status != BT_STATUS_SUCCESS) { - LOG_ERROR(LOG_TAG, "%s error initializing RFCOMM sockets: %d", __func__, status); + LOG_ERROR(LOG_TAG, "%s error initializing RFCOMM sockets: %d", __func__, + status); goto error; } status = btsock_l2cap_init(thread_handle, uid_set); if (status != BT_STATUS_SUCCESS) { - LOG_ERROR(LOG_TAG, "%s error initializing L2CAP sockets: %d", __func__, status); + LOG_ERROR(LOG_TAG, "%s error initializing L2CAP sockets: %d", __func__, + status); goto error; } @@ -86,7 +89,8 @@ bt_status_t btif_sock_init(uid_set_t* uid_set) { status = btsock_sco_init(thread); if (status != BT_STATUS_SUCCESS) { - LOG_ERROR(LOG_TAG, "%s error initializing SCO sockets: %d", __func__, status); + LOG_ERROR(LOG_TAG, "%s error initializing SCO sockets: %d", __func__, + status); btsock_rfc_cleanup(); goto error; } @@ -96,16 +100,14 @@ bt_status_t btif_sock_init(uid_set_t* uid_set) { error:; thread_free(thread); thread = NULL; - if (thread_handle != -1) - btsock_thread_exit(thread_handle); + if (thread_handle != -1) btsock_thread_exit(thread_handle); thread_handle = -1; uid_set = NULL; return BT_STATUS_FAIL; } void btif_sock_cleanup(void) { - if (thread_handle == -1) - return; + if (thread_handle == -1) return; thread_stop(thread); thread_join(thread); @@ -118,10 +120,12 @@ void btif_sock_cleanup(void) { thread = NULL; } -static bt_status_t btsock_listen(btsock_type_t type, const char *service_name, const uint8_t *service_uuid, int channel, int *sock_fd, int flags, int app_uid) { - if((flags & BTSOCK_FLAG_NO_SDP) == 0) { - assert(service_uuid != NULL || channel > 0); - assert(sock_fd != NULL); +static bt_status_t btsock_listen(btsock_type_t type, const char* service_name, + const uint8_t* service_uuid, int channel, + int* sock_fd, int flags, int app_uid) { + if ((flags & BTSOCK_FLAG_NO_SDP) == 0) { + assert(service_uuid != NULL || channel > 0); + assert(sock_fd != NULL); } *sock_fd = INVALID_FD; @@ -129,10 +133,12 @@ static bt_status_t btsock_listen(btsock_type_t type, const char *service_name, c switch (type) { case BTSOCK_RFCOMM: - status = btsock_rfc_listen(service_name, service_uuid, channel, sock_fd, flags, app_uid); + status = btsock_rfc_listen(service_name, service_uuid, channel, sock_fd, + flags, app_uid); break; case BTSOCK_L2CAP: - status = btsock_l2cap_listen(service_name, channel, sock_fd, flags, app_uid); + status = + btsock_l2cap_listen(service_name, channel, sock_fd, flags, app_uid); break; case BTSOCK_SCO: @@ -140,14 +146,18 @@ static bt_status_t btsock_listen(btsock_type_t type, const char *service_name, c break; default: - LOG_ERROR(LOG_TAG, "%s unknown/unsupported socket type: %d", __func__, type); + LOG_ERROR(LOG_TAG, "%s unknown/unsupported socket type: %d", __func__, + type); status = BT_STATUS_UNSUPPORTED; break; } return status; } -static bt_status_t btsock_connect(const bt_bdaddr_t *bd_addr, btsock_type_t type, const uint8_t *uuid, int channel, int *sock_fd, int flags, int app_uid) { +static bt_status_t btsock_connect(const bt_bdaddr_t* bd_addr, + btsock_type_t type, const uint8_t* uuid, + int channel, int* sock_fd, int flags, + int app_uid) { assert(uuid != NULL || channel > 0); assert(bd_addr != NULL); assert(sock_fd != NULL); @@ -157,7 +167,8 @@ static bt_status_t btsock_connect(const bt_bdaddr_t *bd_addr, btsock_type_t type switch (type) { case BTSOCK_RFCOMM: - status = btsock_rfc_connect(bd_addr, uuid, channel, sock_fd, flags, app_uid); + status = + btsock_rfc_connect(bd_addr, uuid, channel, sock_fd, flags, app_uid); break; case BTSOCK_L2CAP: @@ -169,7 +180,8 @@ static bt_status_t btsock_connect(const bt_bdaddr_t *bd_addr, btsock_type_t type break; default: - LOG_ERROR(LOG_TAG, "%s unknown/unsupported socket type: %d", __func__, type); + LOG_ERROR(LOG_TAG, "%s unknown/unsupported socket type: %d", __func__, + type); status = BT_STATUS_UNSUPPORTED; break; } diff --git a/btif/src/btif_sock_l2cap.cc b/btif/src/btif_sock_l2cap.cc index 73cc7e09a..2e4dd8f9c 100644 --- a/btif/src/btif_sock_l2cap.cc +++ b/btif/src/btif_sock_l2cap.cc @@ -33,6 +33,7 @@ #include "osi/include/allocator.h" #include "osi/include/log.h" +#include "bt_common.h" #include "bt_target.h" #include "bta_api.h" #include "bta_jv_api.h" @@ -46,1055 +47,1066 @@ #include "btm_api.h" #include "btm_int.h" #include "btu.h" -#include "bt_common.h" #include "hcimsgs.h" #include "l2c_api.h" #include "l2cdefs.h" #include "port_api.h" #include "sdp_api.h" -#define asrt(s) if (!(s)) APPL_TRACE_ERROR("## %s assert %s failed at line:%d ##",__func__, \ - #s, __LINE__) +#define asrt(s) \ + if (!(s)) \ + APPL_TRACE_ERROR("## %s assert %s failed at line:%d ##", __func__, #s, \ + __LINE__) struct packet { - struct packet *next, *prev; - uint32_t len; - uint8_t *data; + struct packet *next, *prev; + uint32_t len; + uint8_t* data; }; typedef struct l2cap_socket { - - struct l2cap_socket *prev; //link to prev list item - struct l2cap_socket *next; //link to next list item - bt_bdaddr_t addr; //other side's address - char name[256]; //user-friendly name of the service - uint32_t id; //just a tag to find this struct - int app_uid; // The UID of the app who requested this socket - int handle; //handle from lower layers - unsigned security; //security flags - int channel; //channel (fixed_chan) or PSM (!fixed_chan) - int our_fd; //fd from our side - int app_fd; //fd from app's side - - unsigned bytes_buffered; - struct packet *first_packet; //fist packet to be delivered to app - struct packet *last_packet; //last packet to be delivered to app - - fixed_queue_t *incoming_que; //data that came in but has not yet been read - unsigned fixed_chan :1; //fixed channel (or psm?) - unsigned server :1; //is a server? (or connecting?) - unsigned connected :1; //is connected? - unsigned outgoing_congest :1; //should we hold? - unsigned server_psm_sent :1; //The server shall only send PSM once. - bool is_le_coc; //is le connection oriented channel? + struct l2cap_socket* prev; // link to prev list item + struct l2cap_socket* next; // link to next list item + bt_bdaddr_t addr; // other side's address + char name[256]; // user-friendly name of the service + uint32_t id; // just a tag to find this struct + int app_uid; // The UID of the app who requested this socket + int handle; // handle from lower layers + unsigned security; // security flags + int channel; // channel (fixed_chan) or PSM (!fixed_chan) + int our_fd; // fd from our side + int app_fd; // fd from app's side + + unsigned bytes_buffered; + struct packet* first_packet; // fist packet to be delivered to app + struct packet* last_packet; // last packet to be delivered to app + + fixed_queue_t* incoming_que; // data that came in but has not yet been read + unsigned fixed_chan : 1; // fixed channel (or psm?) + unsigned server : 1; // is a server? (or connecting?) + unsigned connected : 1; // is connected? + unsigned outgoing_congest : 1; // should we hold? + unsigned server_psm_sent : 1; // The server shall only send PSM once. + bool is_le_coc; // is le connection oriented channel? } l2cap_socket; -static bt_status_t btSock_start_l2cap_server_l(l2cap_socket *sock); +static bt_status_t btSock_start_l2cap_server_l(l2cap_socket* sock); static std::mutex state_lock; -l2cap_socket *socks = NULL; +l2cap_socket* socks = NULL; static uid_set_t* uid_set = NULL; static int pth = -1; -static void btsock_l2cap_cbk(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data); +static void btsock_l2cap_cbk(tBTA_JV_EVT event, tBTA_JV* p_data, + void* user_data); -/* TODO: Consider to remove this buffer, as we have a buffer in l2cap as well, and we risk - * a buffer overflow with this implementation if the socket data is not read from - * JAVA for a while. In such a case we should use flow control to tell the sender to +/* TODO: Consider to remove this buffer, as we have a buffer in l2cap as well, + * and we risk + * a buffer overflow with this implementation if the socket data is not + * read from + * JAVA for a while. In such a case we should use flow control to tell the + * sender to * back off. - * BUT remember we need to avoid blocking the BTA task execution - hence we cannot + * BUT remember we need to avoid blocking the BTA task execution - hence + * we cannot * directly write to the socket. - * we should be able to change to store the data pointer here, and just wait + * we should be able to change to store the data pointer here, and just + * wait * confirming the l2cap_ind until we have more space in the buffer. */ /* returns false if none - caller must free "data" memory when done with it */ -static char packet_get_head_l(l2cap_socket *sock, uint8_t **data, uint32_t *len) -{ - struct packet *p = sock->first_packet; - - if (!p) - return false; - - if (data) - *data = sock->first_packet->data; - if (len) - *len = sock->first_packet->len; - sock->first_packet = p->next; - if (sock->first_packet) - sock->first_packet->prev = NULL; - else - sock->last_packet = NULL; +static char packet_get_head_l(l2cap_socket* sock, uint8_t** data, + uint32_t* len) { + struct packet* p = sock->first_packet; + + if (!p) return false; + + if (data) *data = sock->first_packet->data; + if (len) *len = sock->first_packet->len; + sock->first_packet = p->next; + if (sock->first_packet) + sock->first_packet->prev = NULL; + else + sock->last_packet = NULL; - if(len) - sock->bytes_buffered -= *len; + if (len) sock->bytes_buffered -= *len; - osi_free(p); + osi_free(p); - return true; + return true; } -static struct packet *packet_alloc(const uint8_t *data, uint32_t len) -{ - struct packet *p = (struct packet *)osi_calloc(sizeof(*p)); - uint8_t *buf = (uint8_t *)osi_malloc(len); +static struct packet* packet_alloc(const uint8_t* data, uint32_t len) { + struct packet* p = (struct packet*)osi_calloc(sizeof(*p)); + uint8_t* buf = (uint8_t*)osi_malloc(len); - p->data = buf; - p->len = len; - memcpy(p->data, data, len); - return p; + p->data = buf; + p->len = len; + memcpy(p->data, data, len); + return p; } /* makes a copy of the data, returns true on success */ -static char packet_put_head_l(l2cap_socket *sock, const void *data, uint32_t len) -{ - struct packet *p = packet_alloc((const uint8_t*)data, len); - - /* - * We do not check size limits here since this is used to undo "getting" a - * packet that the user read incompletely. That is to say the packet was - * already in the queue. We do check thos elimits in packet_put_tail_l() since - * that function is used to put new data into the queue. - */ - - if (!p) - return false; - - p->prev = NULL; - p->next = sock->first_packet; - sock->first_packet = p; - if (p->next) - p->next->prev = p; - else - sock->last_packet = p; +static char packet_put_head_l(l2cap_socket* sock, const void* data, + uint32_t len) { + struct packet* p = packet_alloc((const uint8_t*)data, len); + + /* + * We do not check size limits here since this is used to undo "getting" a + * packet that the user read incompletely. That is to say the packet was + * already in the queue. We do check thos elimits in packet_put_tail_l() since + * that function is used to put new data into the queue. + */ + + if (!p) return false; + + p->prev = NULL; + p->next = sock->first_packet; + sock->first_packet = p; + if (p->next) + p->next->prev = p; + else + sock->last_packet = p; - sock->bytes_buffered += len; + sock->bytes_buffered += len; - return true; + return true; } /* makes a copy of the data, returns true on success */ -static char packet_put_tail_l(l2cap_socket *sock, const void *data, uint32_t len) -{ - struct packet *p = packet_alloc((const uint8_t*)data, len); - - if (sock->bytes_buffered >= L2CAP_MAX_RX_BUFFER) { - LOG_ERROR(LOG_TAG, "packet_put_tail_l: buffer overflow"); - return false; - } +static char packet_put_tail_l(l2cap_socket* sock, const void* data, + uint32_t len) { + struct packet* p = packet_alloc((const uint8_t*)data, len); - if (!p) { - LOG_ERROR(LOG_TAG, "packet_put_tail_l: unable to allocate packet..."); - return false; - } + if (sock->bytes_buffered >= L2CAP_MAX_RX_BUFFER) { + LOG_ERROR(LOG_TAG, "packet_put_tail_l: buffer overflow"); + return false; + } - p->next = NULL; - p->prev = sock->last_packet; - sock->last_packet = p; - if (p->prev) - p->prev->next = p; - else - sock->first_packet = p; + if (!p) { + LOG_ERROR(LOG_TAG, "packet_put_tail_l: unable to allocate packet..."); + return false; + } + + p->next = NULL; + p->prev = sock->last_packet; + sock->last_packet = p; + if (p->prev) + p->prev->next = p; + else + sock->first_packet = p; - sock->bytes_buffered += len; + sock->bytes_buffered += len; - return true; + return true; } -static inline void bd_copy(uint8_t* dest, uint8_t* src, bool swap) -{ - if (swap) { - for (int i=0; i < 6; i++) - dest[i] = src[5-i]; - } else { - memcpy(dest, src, 6); - } +static inline void bd_copy(uint8_t* dest, uint8_t* src, bool swap) { + if (swap) { + for (int i = 0; i < 6; i++) dest[i] = src[5 - i]; + } else { + memcpy(dest, src, 6); + } } -static char is_inited(void) -{ - std::unique_lock lock(state_lock); - return pth != -1; +static char is_inited(void) { + std::unique_lock lock(state_lock); + return pth != -1; } /* only call with std::mutex taken */ -static l2cap_socket *btsock_l2cap_find_by_id_l(uint32_t id) -{ - l2cap_socket *sock = socks; +static l2cap_socket* btsock_l2cap_find_by_id_l(uint32_t id) { + l2cap_socket* sock = socks; - while (sock && sock->id != id) - sock = sock->next; + while (sock && sock->id != id) sock = sock->next; - return sock; + return sock; } -static void btsock_l2cap_free_l(l2cap_socket *sock) -{ - uint8_t *buf; - l2cap_socket *t = socks; +static void btsock_l2cap_free_l(l2cap_socket* sock) { + uint8_t* buf; + l2cap_socket* t = socks; - while(t && t != sock) - t = t->next; + while (t && t != sock) t = t->next; - if (!t) /* prever double-frees */ - return; + if (!t) /* prever double-frees */ + return; - if (sock->next) - sock->next->prev = sock->prev; + if (sock->next) sock->next->prev = sock->prev; - if (sock->prev) - sock->prev->next = sock->next; - else - socks = sock->next; + if (sock->prev) + sock->prev->next = sock->next; + else + socks = sock->next; - shutdown(sock->our_fd, SHUT_RDWR); - close(sock->our_fd); - if (sock->app_fd != -1) { - close(sock->app_fd); - } else { - APPL_TRACE_ERROR("SOCK_LIST: free(id = %d) - NO app_fd!", sock->id); - } + shutdown(sock->our_fd, SHUT_RDWR); + close(sock->our_fd); + if (sock->app_fd != -1) { + close(sock->app_fd); + } else { + APPL_TRACE_ERROR("SOCK_LIST: free(id = %d) - NO app_fd!", sock->id); + } - while (packet_get_head_l(sock, &buf, NULL)) - osi_free(buf); + while (packet_get_head_l(sock, &buf, NULL)) osi_free(buf); - //lower-level close() should be idempotent... so let's call it and see... - if (sock->is_le_coc) - { - // Only call if we are non server connections - if (sock->handle >= 0 && (sock->server == false)) { - BTA_JvL2capClose(sock->handle); - } - if ((sock->channel >= 0) && (sock->server == true)) { - BTA_JvFreeChannel(sock->channel, BTA_JV_CONN_TYPE_L2CAP); - } + // lower-level close() should be idempotent... so let's call it and see... + if (sock->is_le_coc) { + // Only call if we are non server connections + if (sock->handle >= 0 && (sock->server == false)) { + BTA_JvL2capClose(sock->handle); } - else - { - // Only call if we are non server connections - if ((sock->handle >= 0) && (sock->server == false)) { - if (sock->fixed_chan) - BTA_JvL2capCloseLE(sock->handle); - else - BTA_JvL2capClose(sock->handle); - } - if ((sock->channel >= 0) && (sock->server == true)) { - if (sock->fixed_chan) - BTA_JvFreeChannel(sock->channel, BTA_JV_CONN_TYPE_L2CAP_LE); - else - BTA_JvFreeChannel(sock->channel, BTA_JV_CONN_TYPE_L2CAP); - - if (!sock->fixed_chan) { - APPL_TRACE_DEBUG("%s stopping L2CAP server channel %d", __func__, sock->channel); - BTA_JvL2capStopServer(sock->channel, UINT_TO_PTR(sock->id)); - } - } + if ((sock->channel >= 0) && (sock->server == true)) { + BTA_JvFreeChannel(sock->channel, BTA_JV_CONN_TYPE_L2CAP); } + } else { + // Only call if we are non server connections + if ((sock->handle >= 0) && (sock->server == false)) { + if (sock->fixed_chan) + BTA_JvL2capCloseLE(sock->handle); + else + BTA_JvL2capClose(sock->handle); + } + if ((sock->channel >= 0) && (sock->server == true)) { + if (sock->fixed_chan) + BTA_JvFreeChannel(sock->channel, BTA_JV_CONN_TYPE_L2CAP_LE); + else + BTA_JvFreeChannel(sock->channel, BTA_JV_CONN_TYPE_L2CAP); - APPL_TRACE_DEBUG("%s: free(id = %d)", __func__, sock->id); - osi_free(sock); -} - -static l2cap_socket *btsock_l2cap_alloc_l(const char *name, const bt_bdaddr_t *addr, - char is_server, int flags) -{ - unsigned security = 0; - int fds[2]; - l2cap_socket *sock = (l2cap_socket*) osi_calloc(sizeof(*sock)); - - if (flags & BTSOCK_FLAG_ENCRYPT) - security |= is_server ? BTM_SEC_IN_ENCRYPT : BTM_SEC_OUT_ENCRYPT; - if (flags & BTSOCK_FLAG_AUTH) - security |= is_server ? BTM_SEC_IN_AUTHENTICATE : BTM_SEC_OUT_AUTHENTICATE; - if (flags & BTSOCK_FLAG_AUTH_MITM) - security |= is_server ? BTM_SEC_IN_MITM : BTM_SEC_OUT_MITM; - if (flags & BTSOCK_FLAG_AUTH_16_DIGIT) - security |= BTM_SEC_IN_MIN_16_DIGIT_PIN; - - if (socketpair(AF_LOCAL, SOCK_SEQPACKET, 0, fds)) { - APPL_TRACE_ERROR("socketpair failed, errno:%d", errno); - goto fail_sockpair; + if (!sock->fixed_chan) { + APPL_TRACE_DEBUG("%s stopping L2CAP server channel %d", __func__, + sock->channel); + BTA_JvL2capStopServer(sock->channel, UINT_TO_PTR(sock->id)); + } } + } - sock->our_fd = fds[0]; - sock->app_fd = fds[1]; - sock->security = security; - sock->server = is_server; - sock->connected = false; - sock->handle = 0; - sock->server_psm_sent = false; - sock->app_uid = -1; - - if (name) - strncpy(sock->name, name, sizeof(sock->name) - 1); - if (addr) - sock->addr = *addr; - - sock->first_packet = NULL; - sock->last_packet = NULL; + APPL_TRACE_DEBUG("%s: free(id = %d)", __func__, sock->id); + osi_free(sock); +} - sock->next = socks; - sock->prev = NULL; - if (socks) - socks->prev = sock; - sock->id = (socks ? socks->id : 0) + 1; - socks = sock; - /* paranoia cap on: verify no ID duplicates due to overflow and fix as needed */ - while (1) { - l2cap_socket *t; - t = socks->next; - while (t && t->id != sock->id) { - t = t->next; - } - if (!t && sock->id) /* non-zeor handle is unique -> we're done */ - break; - /* if we're here, we found a duplicate */ - if (!++sock->id) /* no zero IDs allowed */ - sock->id++; +static l2cap_socket* btsock_l2cap_alloc_l(const char* name, + const bt_bdaddr_t* addr, + char is_server, int flags) { + unsigned security = 0; + int fds[2]; + l2cap_socket* sock = (l2cap_socket*)osi_calloc(sizeof(*sock)); + + if (flags & BTSOCK_FLAG_ENCRYPT) + security |= is_server ? BTM_SEC_IN_ENCRYPT : BTM_SEC_OUT_ENCRYPT; + if (flags & BTSOCK_FLAG_AUTH) + security |= is_server ? BTM_SEC_IN_AUTHENTICATE : BTM_SEC_OUT_AUTHENTICATE; + if (flags & BTSOCK_FLAG_AUTH_MITM) + security |= is_server ? BTM_SEC_IN_MITM : BTM_SEC_OUT_MITM; + if (flags & BTSOCK_FLAG_AUTH_16_DIGIT) + security |= BTM_SEC_IN_MIN_16_DIGIT_PIN; + + if (socketpair(AF_LOCAL, SOCK_SEQPACKET, 0, fds)) { + APPL_TRACE_ERROR("socketpair failed, errno:%d", errno); + goto fail_sockpair; + } + + sock->our_fd = fds[0]; + sock->app_fd = fds[1]; + sock->security = security; + sock->server = is_server; + sock->connected = false; + sock->handle = 0; + sock->server_psm_sent = false; + sock->app_uid = -1; + + if (name) strncpy(sock->name, name, sizeof(sock->name) - 1); + if (addr) sock->addr = *addr; + + sock->first_packet = NULL; + sock->last_packet = NULL; + + sock->next = socks; + sock->prev = NULL; + if (socks) socks->prev = sock; + sock->id = (socks ? socks->id : 0) + 1; + socks = sock; + /* paranoia cap on: verify no ID duplicates due to overflow and fix as needed + */ + while (1) { + l2cap_socket* t; + t = socks->next; + while (t && t->id != sock->id) { + t = t->next; } - APPL_TRACE_DEBUG("SOCK_LIST: alloc(id = %d)", sock->id); - return sock; + if (!t && sock->id) /* non-zeor handle is unique -> we're done */ + break; + /* if we're here, we found a duplicate */ + if (!++sock->id) /* no zero IDs allowed */ + sock->id++; + } + APPL_TRACE_DEBUG("SOCK_LIST: alloc(id = %d)", sock->id); + return sock; fail_sockpair: - osi_free(sock); - return NULL; + osi_free(sock); + return NULL; } -bt_status_t btsock_l2cap_init(int handle, uid_set_t* set) -{ - APPL_TRACE_DEBUG("%s handle = %d", __func__); - std::unique_lock lock(state_lock); - pth = handle; - socks = NULL; - uid_set = set; - return BT_STATUS_SUCCESS; +bt_status_t btsock_l2cap_init(int handle, uid_set_t* set) { + APPL_TRACE_DEBUG("%s handle = %d", __func__); + std::unique_lock lock(state_lock); + pth = handle; + socks = NULL; + uid_set = set; + return BT_STATUS_SUCCESS; } -bt_status_t btsock_l2cap_cleanup() -{ - std::unique_lock lock(state_lock); - pth = -1; - while (socks) - btsock_l2cap_free_l(socks); - return BT_STATUS_SUCCESS; +bt_status_t btsock_l2cap_cleanup() { + std::unique_lock lock(state_lock); + pth = -1; + while (socks) btsock_l2cap_free_l(socks); + return BT_STATUS_SUCCESS; } -static inline bool send_app_psm_or_chan_l(l2cap_socket *sock) -{ - return sock_send_all(sock->our_fd, (const uint8_t*)&sock->channel, sizeof(sock->channel)) - == sizeof(sock->channel); +static inline bool send_app_psm_or_chan_l(l2cap_socket* sock) { + return sock_send_all(sock->our_fd, (const uint8_t*)&sock->channel, + sizeof(sock->channel)) == sizeof(sock->channel); } static bool send_app_connect_signal(int fd, const bt_bdaddr_t* addr, - int channel, int status, int send_fd, int tx_mtu) -{ - sock_connect_signal_t cs; - cs.size = sizeof(cs); - cs.bd_addr = *addr; - cs.channel = channel; - cs.status = status; - cs.max_rx_packet_size = L2CAP_MAX_SDU_LENGTH; - cs.max_tx_packet_size = tx_mtu; - if (send_fd != -1) { - if (sock_send_fd(fd, (const uint8_t*)&cs, sizeof(cs), send_fd) == sizeof(cs)) - return true; - else APPL_TRACE_ERROR("sock_send_fd failed, fd:%d, send_fd:%d", fd, send_fd); - } else if (sock_send_all(fd, (const uint8_t*)&cs, sizeof(cs)) == sizeof(cs)) { - return true; - } - return false; + int channel, int status, int send_fd, + int tx_mtu) { + sock_connect_signal_t cs; + cs.size = sizeof(cs); + cs.bd_addr = *addr; + cs.channel = channel; + cs.status = status; + cs.max_rx_packet_size = L2CAP_MAX_SDU_LENGTH; + cs.max_tx_packet_size = tx_mtu; + if (send_fd != -1) { + if (sock_send_fd(fd, (const uint8_t*)&cs, sizeof(cs), send_fd) == + sizeof(cs)) + return true; + else + APPL_TRACE_ERROR("sock_send_fd failed, fd:%d, send_fd:%d", fd, send_fd); + } else if (sock_send_all(fd, (const uint8_t*)&cs, sizeof(cs)) == sizeof(cs)) { + return true; + } + return false; } -static void on_srv_l2cap_listen_started(tBTA_JV_L2CAP_START *p_start, uint32_t id) -{ - l2cap_socket *sock; +static void on_srv_l2cap_listen_started(tBTA_JV_L2CAP_START* p_start, + uint32_t id) { + l2cap_socket* sock; - std::unique_lock lock(state_lock); - sock = btsock_l2cap_find_by_id_l(id); - if (!sock) - return; + std::unique_lock lock(state_lock); + sock = btsock_l2cap_find_by_id_l(id); + if (!sock) return; - if (p_start->status != BTA_JV_SUCCESS) { - APPL_TRACE_ERROR("Error starting l2cap_listen - status: 0x%04x", p_start->status); - btsock_l2cap_free_l(sock); - return; - } + if (p_start->status != BTA_JV_SUCCESS) { + APPL_TRACE_ERROR("Error starting l2cap_listen - status: 0x%04x", + p_start->status); + btsock_l2cap_free_l(sock); + return; + } - sock->handle = p_start->handle; - APPL_TRACE_DEBUG("on_srv_l2cap_listen_started() sock->handle =%d id:%d", - sock->handle, sock->id); + sock->handle = p_start->handle; + APPL_TRACE_DEBUG("on_srv_l2cap_listen_started() sock->handle =%d id:%d", + sock->handle, sock->id); - if(sock->server_psm_sent == false) { - if (!send_app_psm_or_chan_l(sock)) { - //closed - APPL_TRACE_DEBUG("send_app_psm() failed, close rs->id:%d", sock->id); - btsock_l2cap_free_l(sock); - } else { - sock->server_psm_sent = true; - } + if (sock->server_psm_sent == false) { + if (!send_app_psm_or_chan_l(sock)) { + // closed + APPL_TRACE_DEBUG("send_app_psm() failed, close rs->id:%d", sock->id); + btsock_l2cap_free_l(sock); + } else { + sock->server_psm_sent = true; } + } } -static void on_cl_l2cap_init(tBTA_JV_L2CAP_CL_INIT *p_init, uint32_t id) -{ - l2cap_socket *sock; +static void on_cl_l2cap_init(tBTA_JV_L2CAP_CL_INIT* p_init, uint32_t id) { + l2cap_socket* sock; - std::unique_lock lock(state_lock); - sock = btsock_l2cap_find_by_id_l(id); - if (!sock) - return; + std::unique_lock lock(state_lock); + sock = btsock_l2cap_find_by_id_l(id); + if (!sock) return; - if (p_init->status != BTA_JV_SUCCESS) { - btsock_l2cap_free_l(sock); - return; - } + if (p_init->status != BTA_JV_SUCCESS) { + btsock_l2cap_free_l(sock); + return; + } - sock->handle = p_init->handle; + sock->handle = p_init->handle; } /** - * Here we allocate a new sock instance to mimic the BluetoothSocket. The socket will be a clone + * Here we allocate a new sock instance to mimic the BluetoothSocket. The socket + * will be a clone * of the sock representing the BluetoothServerSocket. * */ -static void on_srv_l2cap_psm_connect_l(tBTA_JV_L2CAP_OPEN *p_open, l2cap_socket *sock) -{ - l2cap_socket *accept_rs; - uint32_t new_listen_id; +static void on_srv_l2cap_psm_connect_l(tBTA_JV_L2CAP_OPEN* p_open, + l2cap_socket* sock) { + l2cap_socket* accept_rs; + uint32_t new_listen_id; + + // std::mutex locked by caller + accept_rs = btsock_l2cap_alloc_l( + sock->name, (const bt_bdaddr_t*)p_open->rem_bda, false, 0); + accept_rs->connected = true; + accept_rs->security = sock->security; + accept_rs->fixed_chan = sock->fixed_chan; + accept_rs->channel = sock->channel; + accept_rs->handle = sock->handle; + accept_rs->app_uid = sock->app_uid; + sock->handle = + -1; /* We should no longer associate this handle with the server socket */ + accept_rs->is_le_coc = sock->is_le_coc; + + /* Swap IDs to hand over the GAP connection to the accepted socket, and start + a new server on + the newly create socket ID. */ + new_listen_id = accept_rs->id; + accept_rs->id = sock->id; + sock->id = new_listen_id; + + if (accept_rs) { + // start monitor the socket + btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, + SOCK_THREAD_FD_EXCEPTION, sock->id); + btsock_thread_add_fd(pth, accept_rs->our_fd, BTSOCK_L2CAP, + SOCK_THREAD_FD_RD, accept_rs->id); + APPL_TRACE_DEBUG( + "sending connect signal & app fd: %d to app server to accept() the" + " connection", + accept_rs->app_fd); + APPL_TRACE_DEBUG("server fd:%d, scn:%d", sock->our_fd, sock->channel); + send_app_connect_signal(sock->our_fd, &accept_rs->addr, sock->channel, 0, + accept_rs->app_fd, p_open->tx_mtu); + accept_rs->app_fd = + -1; // The fd is closed after sent to app in send_app_connect_signal() + // But for some reason we still leak a FD - either the server socket + // one or the accept socket one. + if (btSock_start_l2cap_server_l(sock) != BT_STATUS_SUCCESS) { + btsock_l2cap_free_l(sock); + } + } +} - // std::mutex locked by caller - accept_rs = btsock_l2cap_alloc_l(sock->name, (const bt_bdaddr_t*)p_open->rem_bda, false, 0); +static void on_srv_l2cap_le_connect_l(tBTA_JV_L2CAP_LE_OPEN* p_open, + l2cap_socket* sock) { + l2cap_socket* accept_rs; + uint32_t new_listen_id; + + // std::mutex locked by caller + accept_rs = btsock_l2cap_alloc_l( + sock->name, (const bt_bdaddr_t*)p_open->rem_bda, false, 0); + if (accept_rs) { + // swap IDs + new_listen_id = accept_rs->id; + accept_rs->id = sock->id; + sock->id = new_listen_id; + + accept_rs->handle = p_open->handle; accept_rs->connected = true; accept_rs->security = sock->security; accept_rs->fixed_chan = sock->fixed_chan; accept_rs->channel = sock->channel; - accept_rs->handle = sock->handle; accept_rs->app_uid = sock->app_uid; - sock->handle = -1; /* We should no longer associate this handle with the server socket */ - accept_rs->is_le_coc = sock->is_le_coc; - - /* Swap IDs to hand over the GAP connection to the accepted socket, and start a new server on - the newly create socket ID. */ - new_listen_id = accept_rs->id; - accept_rs->id = sock->id; - sock->id = new_listen_id; - if (accept_rs) { - //start monitor the socket - btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_EXCEPTION, sock->id); - btsock_thread_add_fd(pth, accept_rs->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, - accept_rs->id); - APPL_TRACE_DEBUG("sending connect signal & app fd: %d to app server to accept() the" - " connection", accept_rs->app_fd); - APPL_TRACE_DEBUG("server fd:%d, scn:%d", sock->our_fd, sock->channel); - send_app_connect_signal(sock->our_fd, &accept_rs->addr, sock->channel, 0, - accept_rs->app_fd, p_open->tx_mtu); - accept_rs->app_fd = -1; // The fd is closed after sent to app in send_app_connect_signal() - // But for some reason we still leak a FD - either the server socket - // one or the accept socket one. - if(btSock_start_l2cap_server_l(sock) != BT_STATUS_SUCCESS) { - btsock_l2cap_free_l(sock); - } - } + // if we do not set a callback, this socket will be dropped */ + *(p_open->p_p_cback) = (void*)btsock_l2cap_cbk; + *(p_open->p_user_data) = UINT_TO_PTR(accept_rs->id); + + // start monitor the socket + btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, + SOCK_THREAD_FD_EXCEPTION, sock->id); + btsock_thread_add_fd(pth, accept_rs->our_fd, BTSOCK_L2CAP, + SOCK_THREAD_FD_RD, accept_rs->id); + APPL_TRACE_DEBUG( + "sending connect signal & app fd:%dto app server to accept() the" + " connection", + accept_rs->app_fd); + APPL_TRACE_DEBUG("server fd:%d, scn:%d", sock->our_fd, sock->channel); + send_app_connect_signal(sock->our_fd, &accept_rs->addr, sock->channel, 0, + accept_rs->app_fd, p_open->tx_mtu); + accept_rs->app_fd = -1; // the fd is closed after sent to app + } } -static void on_srv_l2cap_le_connect_l(tBTA_JV_L2CAP_LE_OPEN *p_open, l2cap_socket *sock) -{ - l2cap_socket *accept_rs; - uint32_t new_listen_id; - - // std::mutex locked by caller - accept_rs = btsock_l2cap_alloc_l(sock->name, (const bt_bdaddr_t*)p_open->rem_bda, false, 0); - if (accept_rs) { - - //swap IDs - new_listen_id = accept_rs->id; - accept_rs->id = sock->id; - sock->id = new_listen_id; - - accept_rs->handle = p_open->handle; - accept_rs->connected = true; - accept_rs->security = sock->security; - accept_rs->fixed_chan = sock->fixed_chan; - accept_rs->channel = sock->channel; - accept_rs->app_uid = sock->app_uid; - - //if we do not set a callback, this socket will be dropped */ - *(p_open->p_p_cback) = (void*)btsock_l2cap_cbk; - *(p_open->p_user_data) = UINT_TO_PTR(accept_rs->id); - - //start monitor the socket - btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_EXCEPTION, sock->id); - btsock_thread_add_fd(pth, accept_rs->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, - accept_rs->id); - APPL_TRACE_DEBUG("sending connect signal & app fd:%dto app server to accept() the" - " connection", accept_rs->app_fd); - APPL_TRACE_DEBUG("server fd:%d, scn:%d", sock->our_fd, sock->channel); - send_app_connect_signal(sock->our_fd, &accept_rs->addr, sock->channel, 0, - accept_rs->app_fd, p_open->tx_mtu); - accept_rs->app_fd = -1; //the fd is closed after sent to app - } +static void on_cl_l2cap_psm_connect_l(tBTA_JV_L2CAP_OPEN* p_open, + l2cap_socket* sock) { + bd_copy(sock->addr.address, p_open->rem_bda, 0); + + if (!send_app_psm_or_chan_l(sock)) { + APPL_TRACE_ERROR("send_app_psm_or_chan_l failed"); + return; + } + + if (send_app_connect_signal(sock->our_fd, &sock->addr, sock->channel, 0, -1, + p_open->tx_mtu)) { + // start monitoring the socketpair to get call back when app writing data + APPL_TRACE_DEBUG( + "on_l2cap_connect_ind, connect signal sent, slot id:%d, psm:%d," + " server:%d", + sock->id, sock->channel, sock->server); + btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, + sock->id); + sock->connected = true; + } else + APPL_TRACE_ERROR("send_app_connect_signal failed"); } -static void on_cl_l2cap_psm_connect_l(tBTA_JV_L2CAP_OPEN *p_open, l2cap_socket *sock) -{ - bd_copy(sock->addr.address, p_open->rem_bda, 0); - - if (!send_app_psm_or_chan_l(sock)) { - APPL_TRACE_ERROR("send_app_psm_or_chan_l failed"); - return; - } - - if (send_app_connect_signal(sock->our_fd, &sock->addr, sock->channel, 0, -1, p_open->tx_mtu)) { - //start monitoring the socketpair to get call back when app writing data - APPL_TRACE_DEBUG("on_l2cap_connect_ind, connect signal sent, slot id:%d, psm:%d," - " server:%d", sock->id, sock->channel, sock->server); - btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, sock->id); - sock->connected = true; - } - else APPL_TRACE_ERROR("send_app_connect_signal failed"); +static void on_cl_l2cap_le_connect_l(tBTA_JV_L2CAP_LE_OPEN* p_open, + l2cap_socket* sock) { + bd_copy(sock->addr.address, p_open->rem_bda, 0); + + if (!send_app_psm_or_chan_l(sock)) { + APPL_TRACE_ERROR("send_app_psm_or_chan_l failed"); + return; + } + + if (send_app_connect_signal(sock->our_fd, &sock->addr, sock->channel, 0, -1, + p_open->tx_mtu)) { + // start monitoring the socketpair to get call back when app writing data + APPL_TRACE_DEBUG( + "on_l2cap_connect_ind, connect signal sent, slot id:%d, Chan:%d," + " server:%d", + sock->id, sock->channel, sock->server); + btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, + sock->id); + sock->connected = true; + } else + APPL_TRACE_ERROR("send_app_connect_signal failed"); } -static void on_cl_l2cap_le_connect_l(tBTA_JV_L2CAP_LE_OPEN *p_open, l2cap_socket *sock) -{ - bd_copy(sock->addr.address, p_open->rem_bda, 0); - - if (!send_app_psm_or_chan_l(sock)) { - APPL_TRACE_ERROR("send_app_psm_or_chan_l failed"); - return; - } - - if (send_app_connect_signal(sock->our_fd, &sock->addr, sock->channel, 0, -1, p_open->tx_mtu)) { - //start monitoring the socketpair to get call back when app writing data - APPL_TRACE_DEBUG("on_l2cap_connect_ind, connect signal sent, slot id:%d, Chan:%d," - " server:%d", sock->id, sock->channel, sock->server); - btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, sock->id); - sock->connected = true; - } - else APPL_TRACE_ERROR("send_app_connect_signal failed"); -} - -static void on_l2cap_connect(tBTA_JV *p_data, uint32_t id) -{ - l2cap_socket *sock; - tBTA_JV_L2CAP_OPEN *psm_open = &p_data->l2c_open; - tBTA_JV_L2CAP_LE_OPEN *le_open = &p_data->l2c_le_open; - - std::unique_lock lock(state_lock); - sock = btsock_l2cap_find_by_id_l(id); - if (!sock) { - APPL_TRACE_ERROR("on_l2cap_connect on unknown socket"); - return; - } - - if (sock->fixed_chan && le_open->status == BTA_JV_SUCCESS) { - if (!sock->server) - on_cl_l2cap_le_connect_l(le_open, sock); - else - on_srv_l2cap_le_connect_l(le_open, sock); - } else if (!sock->fixed_chan && psm_open->status == BTA_JV_SUCCESS) { - if (!sock->server) - on_cl_l2cap_psm_connect_l(psm_open, sock); - else - on_srv_l2cap_psm_connect_l(psm_open, sock); - } +static void on_l2cap_connect(tBTA_JV* p_data, uint32_t id) { + l2cap_socket* sock; + tBTA_JV_L2CAP_OPEN* psm_open = &p_data->l2c_open; + tBTA_JV_L2CAP_LE_OPEN* le_open = &p_data->l2c_le_open; + + std::unique_lock lock(state_lock); + sock = btsock_l2cap_find_by_id_l(id); + if (!sock) { + APPL_TRACE_ERROR("on_l2cap_connect on unknown socket"); + return; + } + + if (sock->fixed_chan && le_open->status == BTA_JV_SUCCESS) { + if (!sock->server) + on_cl_l2cap_le_connect_l(le_open, sock); else - btsock_l2cap_free_l(sock); -} - -static void on_l2cap_close(tBTA_JV_L2CAP_CLOSE * p_close, uint32_t id) -{ - l2cap_socket *sock; - - std::unique_lock lock(state_lock); - sock = btsock_l2cap_find_by_id_l(id); - if (!sock) - return; - - APPL_TRACE_DEBUG("on_l2cap_close, slot id:%d, fd:%d, %s:%d, server:%d", - sock->id, sock->our_fd, sock->fixed_chan ? "fixed_chan" : "PSM", - sock->channel, sock->server); - // TODO: This does not seem to be called... - // I'm not sure if this will be called for non-server sockets? - if(!sock->fixed_chan && (sock->server == true)) { - BTA_JvFreeChannel(sock->channel, BTA_JV_CONN_TYPE_L2CAP); - } + on_srv_l2cap_le_connect_l(le_open, sock); + } else if (!sock->fixed_chan && psm_open->status == BTA_JV_SUCCESS) { + if (!sock->server) + on_cl_l2cap_psm_connect_l(psm_open, sock); + else + on_srv_l2cap_psm_connect_l(psm_open, sock); + } else btsock_l2cap_free_l(sock); } -static void on_l2cap_outgoing_congest(tBTA_JV_L2CAP_CONG *p, uint32_t id) -{ - l2cap_socket *sock; - - std::unique_lock lock(state_lock); - sock = btsock_l2cap_find_by_id_l(id); - if (!sock) - return; - - sock->outgoing_congest = p->cong ? 1 : 0; - //mointer the fd for any outgoing data - if (!sock->outgoing_congest) { - APPL_TRACE_DEBUG("on_l2cap_outgoing_congest: adding fd to btsock_thread..."); - btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, sock->id); +static void on_l2cap_close(tBTA_JV_L2CAP_CLOSE* p_close, uint32_t id) { + l2cap_socket* sock; + + std::unique_lock lock(state_lock); + sock = btsock_l2cap_find_by_id_l(id); + if (!sock) return; + + APPL_TRACE_DEBUG("on_l2cap_close, slot id:%d, fd:%d, %s:%d, server:%d", + sock->id, sock->our_fd, + sock->fixed_chan ? "fixed_chan" : "PSM", sock->channel, + sock->server); + // TODO: This does not seem to be called... + // I'm not sure if this will be called for non-server sockets? + if (!sock->fixed_chan && (sock->server == true)) { + BTA_JvFreeChannel(sock->channel, BTA_JV_CONN_TYPE_L2CAP); + } + btsock_l2cap_free_l(sock); +} - } +static void on_l2cap_outgoing_congest(tBTA_JV_L2CAP_CONG* p, uint32_t id) { + l2cap_socket* sock; + + std::unique_lock lock(state_lock); + sock = btsock_l2cap_find_by_id_l(id); + if (!sock) return; + + sock->outgoing_congest = p->cong ? 1 : 0; + // mointer the fd for any outgoing data + if (!sock->outgoing_congest) { + APPL_TRACE_DEBUG( + "on_l2cap_outgoing_congest: adding fd to btsock_thread..."); + btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, + sock->id); + } } -static void on_l2cap_write_done(void* req_id, uint16_t len, uint32_t id) -{ - l2cap_socket *sock; +static void on_l2cap_write_done(void* req_id, uint16_t len, uint32_t id) { + l2cap_socket* sock; - if (req_id != NULL) { - osi_free(req_id); //free the buffer - } + if (req_id != NULL) { + osi_free(req_id); // free the buffer + } - int app_uid = -1; + int app_uid = -1; - std::unique_lock lock(state_lock); - sock = btsock_l2cap_find_by_id_l(id); - if (!sock) - return; + std::unique_lock lock(state_lock); + sock = btsock_l2cap_find_by_id_l(id); + if (!sock) return; - app_uid = sock->app_uid; - if (!sock->outgoing_congest) { - // monitor the fd for any outgoing data - APPL_TRACE_DEBUG("on_l2cap_write_done: adding fd to btsock_thread..."); - btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, sock->id); - } + app_uid = sock->app_uid; + if (!sock->outgoing_congest) { + // monitor the fd for any outgoing data + APPL_TRACE_DEBUG("on_l2cap_write_done: adding fd to btsock_thread..."); + btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, + sock->id); + } - uid_set_add_tx(uid_set, app_uid, len); + uid_set_add_tx(uid_set, app_uid, len); } -static void on_l2cap_write_fixed_done(void* req_id, uint16_t len, uint32_t id) -{ - l2cap_socket *sock; - - if (req_id != NULL) { - osi_free(req_id); //free the buffer - } - - int app_uid = -1; - std::unique_lock lock(state_lock); - sock = btsock_l2cap_find_by_id_l(id); - if (!sock) - return; - - app_uid = sock->app_uid; - if (!sock->outgoing_congest) { - //monitor the fd for any outgoing data - btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, sock->id); - } - uid_set_add_tx(uid_set, app_uid, len); +static void on_l2cap_write_fixed_done(void* req_id, uint16_t len, uint32_t id) { + l2cap_socket* sock; + + if (req_id != NULL) { + osi_free(req_id); // free the buffer + } + + int app_uid = -1; + std::unique_lock lock(state_lock); + sock = btsock_l2cap_find_by_id_l(id); + if (!sock) return; + + app_uid = sock->app_uid; + if (!sock->outgoing_congest) { + // monitor the fd for any outgoing data + btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_RD, + sock->id); + } + uid_set_add_tx(uid_set, app_uid, len); } -static void on_l2cap_data_ind(tBTA_JV *evt, uint32_t id) -{ - l2cap_socket *sock; +static void on_l2cap_data_ind(tBTA_JV* evt, uint32_t id) { + l2cap_socket* sock; - int app_uid = -1; - uint32_t bytes_read = 0; + int app_uid = -1; + uint32_t bytes_read = 0; - std::unique_lock lock(state_lock); - sock = btsock_l2cap_find_by_id_l(id); - if (!sock) - return; + std::unique_lock lock(state_lock); + sock = btsock_l2cap_find_by_id_l(id); + if (!sock) return; - app_uid = sock->app_uid; + app_uid = sock->app_uid; - if (sock->fixed_chan) { /* we do these differently */ + if (sock->fixed_chan) { /* we do these differently */ - tBTA_JV_LE_DATA_IND *p_le_data_ind = &evt->le_data_ind; - BT_HDR *p_buf = p_le_data_ind->p_buf; - uint8_t *data = (uint8_t*)(p_buf + 1) + p_buf->offset; + tBTA_JV_LE_DATA_IND* p_le_data_ind = &evt->le_data_ind; + BT_HDR* p_buf = p_le_data_ind->p_buf; + uint8_t* data = (uint8_t*)(p_buf + 1) + p_buf->offset; - if (packet_put_tail_l(sock, data, p_buf->len)) { - bytes_read = p_buf->len; - btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_WR, sock->id); - } else {//connection must be dropped - APPL_TRACE_DEBUG("on_l2cap_data_ind() unable to push data to socket - closing" - " fixed channel"); - BTA_JvL2capCloseLE(sock->handle); - btsock_l2cap_free_l(sock); - } + if (packet_put_tail_l(sock, data, p_buf->len)) { + bytes_read = p_buf->len; + btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_WR, + sock->id); + } else { // connection must be dropped + APPL_TRACE_DEBUG( + "on_l2cap_data_ind() unable to push data to socket - closing" + " fixed channel"); + BTA_JvL2capCloseLE(sock->handle); + btsock_l2cap_free_l(sock); + } - } else { - uint8_t buffer[L2CAP_MAX_SDU_LENGTH]; - uint32_t count; - - if (BTA_JvL2capReady(sock->handle, &count) == BTA_JV_SUCCESS) { - if (BTA_JvL2capRead(sock->handle, sock->id, buffer, count) == BTA_JV_SUCCESS) { - if (packet_put_tail_l(sock, buffer, count)) { - bytes_read = count; - btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_WR, - sock->id); - } else {//connection must be dropped - APPL_TRACE_DEBUG("on_l2cap_data_ind() unable to push data to socket" - " - closing channel"); - BTA_JvL2capClose(sock->handle); - btsock_l2cap_free_l(sock); - } - } + } else { + uint8_t buffer[L2CAP_MAX_SDU_LENGTH]; + uint32_t count; + + if (BTA_JvL2capReady(sock->handle, &count) == BTA_JV_SUCCESS) { + if (BTA_JvL2capRead(sock->handle, sock->id, buffer, count) == + BTA_JV_SUCCESS) { + if (packet_put_tail_l(sock, buffer, count)) { + bytes_read = count; + btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, + SOCK_THREAD_FD_WR, sock->id); + } else { // connection must be dropped + APPL_TRACE_DEBUG( + "on_l2cap_data_ind() unable to push data to socket" + " - closing channel"); + BTA_JvL2capClose(sock->handle); + btsock_l2cap_free_l(sock); } + } } + } - uid_set_add_rx(uid_set, app_uid, bytes_read); + uid_set_add_rx(uid_set, app_uid, bytes_read); } -static void btsock_l2cap_cbk(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data) -{ - uint32_t sock_id = PTR_TO_UINT(user_data); +static void btsock_l2cap_cbk(tBTA_JV_EVT event, tBTA_JV* p_data, + void* user_data) { + uint32_t sock_id = PTR_TO_UINT(user_data); - switch (event) { + switch (event) { case BTA_JV_L2CAP_START_EVT: - on_srv_l2cap_listen_started(&p_data->l2c_start, sock_id); - break; + on_srv_l2cap_listen_started(&p_data->l2c_start, sock_id); + break; case BTA_JV_L2CAP_CL_INIT_EVT: - on_cl_l2cap_init(&p_data->l2c_cl_init, sock_id); - break; + on_cl_l2cap_init(&p_data->l2c_cl_init, sock_id); + break; case BTA_JV_L2CAP_OPEN_EVT: - on_l2cap_connect(p_data, sock_id); - BTA_JvSetPmProfile(p_data->l2c_open.handle, BTA_JV_PM_ID_1,BTA_JV_CONN_OPEN); - break; + on_l2cap_connect(p_data, sock_id); + BTA_JvSetPmProfile(p_data->l2c_open.handle, BTA_JV_PM_ID_1, + BTA_JV_CONN_OPEN); + break; case BTA_JV_L2CAP_CLOSE_EVT: - APPL_TRACE_DEBUG("BTA_JV_L2CAP_CLOSE_EVT: id: %u", sock_id); - on_l2cap_close(&p_data->l2c_close, sock_id); - break; + APPL_TRACE_DEBUG("BTA_JV_L2CAP_CLOSE_EVT: id: %u", sock_id); + on_l2cap_close(&p_data->l2c_close, sock_id); + break; case BTA_JV_L2CAP_DATA_IND_EVT: - on_l2cap_data_ind(p_data, sock_id); - APPL_TRACE_DEBUG("BTA_JV_L2CAP_DATA_IND_EVT"); - break; + on_l2cap_data_ind(p_data, sock_id); + APPL_TRACE_DEBUG("BTA_JV_L2CAP_DATA_IND_EVT"); + break; case BTA_JV_L2CAP_READ_EVT: - APPL_TRACE_DEBUG("BTA_JV_L2CAP_READ_EVT not used"); - break; + APPL_TRACE_DEBUG("BTA_JV_L2CAP_READ_EVT not used"); + break; case BTA_JV_L2CAP_WRITE_EVT: - APPL_TRACE_DEBUG("BTA_JV_L2CAP_WRITE_EVT: id: %u", sock_id); - on_l2cap_write_done(UINT_TO_PTR(p_data->l2c_write.req_id), p_data->l2c_write.len, sock_id); - break; + APPL_TRACE_DEBUG("BTA_JV_L2CAP_WRITE_EVT: id: %u", sock_id); + on_l2cap_write_done(UINT_TO_PTR(p_data->l2c_write.req_id), + p_data->l2c_write.len, sock_id); + break; case BTA_JV_L2CAP_WRITE_FIXED_EVT: - APPL_TRACE_DEBUG("BTA_JV_L2CAP_WRITE_FIXED_EVT: id: %u", sock_id); - on_l2cap_write_fixed_done(UINT_TO_PTR(p_data->l2c_write_fixed.req_id), p_data->l2c_write.len, sock_id); - break; + APPL_TRACE_DEBUG("BTA_JV_L2CAP_WRITE_FIXED_EVT: id: %u", sock_id); + on_l2cap_write_fixed_done(UINT_TO_PTR(p_data->l2c_write_fixed.req_id), + p_data->l2c_write.len, sock_id); + break; case BTA_JV_L2CAP_CONG_EVT: - on_l2cap_outgoing_congest(&p_data->l2c_cong, sock_id); - break; + on_l2cap_outgoing_congest(&p_data->l2c_cong, sock_id); + break; default: - APPL_TRACE_ERROR("unhandled event %d, slot id: %u", event, sock_id); - break; - } + APPL_TRACE_ERROR("unhandled event %d, slot id: %u", event, sock_id); + break; + } } /* L2CAP default options for OBEX socket connections */ const tL2CAP_FCR_OPTS obex_l2c_fcr_opts_def = -{ - L2CAP_FCR_ERTM_MODE, /* Mandatory for OBEX over l2cap */ - OBX_FCR_OPT_TX_WINDOW_SIZE_BR_EDR,/* Tx window size */ - OBX_FCR_OPT_MAX_TX_B4_DISCNT, /* Maximum transmissions before disconnecting */ - OBX_FCR_OPT_RETX_TOUT, /* Retransmission timeout (2 secs) */ - OBX_FCR_OPT_MONITOR_TOUT, /* Monitor timeout (12 secs) */ - OBX_FCR_OPT_MAX_PDU_SIZE /* MPS segment size */ -}; -const tL2CAP_ERTM_INFO obex_l2c_etm_opt = -{ - L2CAP_FCR_ERTM_MODE, /* Mandatory for OBEX over l2cap */ - L2CAP_FCR_CHAN_OPT_ERTM, /* Mandatory for OBEX over l2cap */ - OBX_USER_RX_BUF_SIZE, - OBX_USER_TX_BUF_SIZE, - OBX_FCR_RX_BUF_SIZE, - OBX_FCR_TX_BUF_SIZE + { + L2CAP_FCR_ERTM_MODE, /* Mandatory for OBEX over l2cap */ + OBX_FCR_OPT_TX_WINDOW_SIZE_BR_EDR, /* Tx window size */ + OBX_FCR_OPT_MAX_TX_B4_DISCNT, /* Maximum transmissions before + disconnecting */ + OBX_FCR_OPT_RETX_TOUT, /* Retransmission timeout (2 secs) */ + OBX_FCR_OPT_MONITOR_TOUT, /* Monitor timeout (12 secs) */ + OBX_FCR_OPT_MAX_PDU_SIZE /* MPS segment size */ }; +const tL2CAP_ERTM_INFO obex_l2c_etm_opt = { + L2CAP_FCR_ERTM_MODE, /* Mandatory for OBEX over l2cap */ + L2CAP_FCR_CHAN_OPT_ERTM, /* Mandatory for OBEX over l2cap */ + OBX_USER_RX_BUF_SIZE, OBX_USER_TX_BUF_SIZE, + OBX_FCR_RX_BUF_SIZE, OBX_FCR_TX_BUF_SIZE}; /** - * When using a dynamic PSM, a PSM allocation is requested from btsock_l2cap_listen_or_connect(). - * The PSM allocation event is refeived in the JV-callback - currently located in RFC-code - + * When using a dynamic PSM, a PSM allocation is requested from + * btsock_l2cap_listen_or_connect(). + * The PSM allocation event is refeived in the JV-callback - currently located + * in RFC-code - * and this function is called with the newly allocated PSM. */ void on_l2cap_psm_assigned(int id, int psm) { - /* Setup ETM settings: - * mtu will be set below */ - std::unique_lock lock(state_lock); - l2cap_socket *sock = btsock_l2cap_find_by_id_l(id); - if (!sock) { - APPL_TRACE_ERROR("%s: Error: sock is null", __func__); - return; - } - - sock->channel = psm; - - if (btSock_start_l2cap_server_l(sock) != BT_STATUS_SUCCESS) - btsock_l2cap_free_l(sock); + /* Setup ETM settings: + * mtu will be set below */ + std::unique_lock lock(state_lock); + l2cap_socket* sock = btsock_l2cap_find_by_id_l(id); + if (!sock) { + APPL_TRACE_ERROR("%s: Error: sock is null", __func__); + return; + } + + sock->channel = psm; + + if (btSock_start_l2cap_server_l(sock) != BT_STATUS_SUCCESS) + btsock_l2cap_free_l(sock); } -static bt_status_t btSock_start_l2cap_server_l(l2cap_socket *sock) { - tL2CAP_CFG_INFO cfg; - bt_status_t stat = BT_STATUS_SUCCESS; - /* Setup ETM settings: - * mtu will be set below */ - memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO)); - - cfg.fcr_present = true; - cfg.fcr = obex_l2c_fcr_opts_def; - - if (sock->fixed_chan) { - - if (BTA_JvL2capStartServerLE(sock->security, 0, NULL, sock->channel, - L2CAP_DEFAULT_MTU, NULL, btsock_l2cap_cbk, UINT_TO_PTR(sock->id)) - != BTA_JV_SUCCESS) - stat = BT_STATUS_FAIL; - +static bt_status_t btSock_start_l2cap_server_l(l2cap_socket* sock) { + tL2CAP_CFG_INFO cfg; + bt_status_t stat = BT_STATUS_SUCCESS; + /* Setup ETM settings: + * mtu will be set below */ + memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO)); + + cfg.fcr_present = true; + cfg.fcr = obex_l2c_fcr_opts_def; + + if (sock->fixed_chan) { + if (BTA_JvL2capStartServerLE(sock->security, 0, NULL, sock->channel, + L2CAP_DEFAULT_MTU, NULL, btsock_l2cap_cbk, + UINT_TO_PTR(sock->id)) != BTA_JV_SUCCESS) + stat = BT_STATUS_FAIL; + + } else { + /* If we have a channel specified in the request, just start the server, + * else we request a PSM and start the server after we receive a PSM. */ + if (sock->channel < 0) { + if (sock->is_le_coc) { + if (BTA_JvGetChannelId(BTA_JV_CONN_TYPE_L2CAP_LE, UINT_TO_PTR(sock->id), + 0) != BTA_JV_SUCCESS) + stat = BT_STATUS_FAIL; + } else { + if (BTA_JvGetChannelId(BTA_JV_CONN_TYPE_L2CAP, UINT_TO_PTR(sock->id), + 0) != BTA_JV_SUCCESS) + stat = BT_STATUS_FAIL; + } } else { - /* If we have a channel specified in the request, just start the server, - * else we request a PSM and start the server after we receive a PSM. */ - if (sock->channel < 0) { - if (sock->is_le_coc) { - if (BTA_JvGetChannelId(BTA_JV_CONN_TYPE_L2CAP_LE, UINT_TO_PTR(sock->id), 0) - != BTA_JV_SUCCESS) - stat = BT_STATUS_FAIL; - } - else - { - if (BTA_JvGetChannelId(BTA_JV_CONN_TYPE_L2CAP, UINT_TO_PTR(sock->id), 0) - != BTA_JV_SUCCESS) - stat = BT_STATUS_FAIL; - } - } else { - if (sock->is_le_coc) { - if (BTA_JvL2capStartServer(BTA_JV_CONN_TYPE_L2CAP_LE, sock->security, 0, NULL, - sock->channel, L2CAP_MAX_SDU_LENGTH, &cfg, btsock_l2cap_cbk, UINT_TO_PTR(sock->id)) - != BTA_JV_SUCCESS) - stat = BT_STATUS_FAIL; - } - else - { - if (BTA_JvL2capStartServer(BTA_JV_CONN_TYPE_L2CAP, sock->security, 0, &obex_l2c_etm_opt, - sock->channel, L2CAP_MAX_SDU_LENGTH, &cfg, btsock_l2cap_cbk, UINT_TO_PTR(sock->id)) - != BTA_JV_SUCCESS) - stat = BT_STATUS_FAIL; - } - } + if (sock->is_le_coc) { + if (BTA_JvL2capStartServer(BTA_JV_CONN_TYPE_L2CAP_LE, sock->security, 0, + NULL, sock->channel, L2CAP_MAX_SDU_LENGTH, + &cfg, btsock_l2cap_cbk, + UINT_TO_PTR(sock->id)) != BTA_JV_SUCCESS) + stat = BT_STATUS_FAIL; + } else { + if (BTA_JvL2capStartServer(BTA_JV_CONN_TYPE_L2CAP, sock->security, 0, + &obex_l2c_etm_opt, sock->channel, + L2CAP_MAX_SDU_LENGTH, &cfg, btsock_l2cap_cbk, + UINT_TO_PTR(sock->id)) != BTA_JV_SUCCESS) + stat = BT_STATUS_FAIL; + } } - return stat; + } + return stat; } -static bt_status_t btsock_l2cap_listen_or_connect(const char *name, const bt_bdaddr_t *addr, - int channel, int* sock_fd, int flags, char listen, int app_uid) -{ - bt_status_t stat; - int fixed_chan = 1; - l2cap_socket *sock; - tL2CAP_CFG_INFO cfg; - bool is_le_coc = false; - - if (!sock_fd) - return BT_STATUS_PARM_INVALID; - - if(channel < 0) { - // We need to auto assign a PSM - fixed_chan = 0; - } else { - fixed_chan = (channel & L2CAP_MASK_FIXED_CHANNEL) != 0; - is_le_coc = (channel & L2CAP_MASK_LE_COC_CHANNEL) != 0; - channel &=~ (L2CAP_MASK_FIXED_CHANNEL | L2CAP_MASK_LE_COC_CHANNEL); - } - - if (!is_inited()) - return BT_STATUS_NOT_READY; - - // TODO: This is kind of bad to lock here, but it is needed for the current design. - std::unique_lock lock(state_lock); - sock = btsock_l2cap_alloc_l(name, addr, listen, flags); - if (!sock) { - return BT_STATUS_NOMEM; - } - - sock->fixed_chan = fixed_chan; - sock->channel = channel; - sock->app_uid = app_uid; - sock->is_le_coc = is_le_coc; - - stat = BT_STATUS_SUCCESS; - - /* Setup ETM settings: - * mtu will be set below */ - memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO)); - - cfg.fcr_present = true; - cfg.fcr = obex_l2c_fcr_opts_def; - - /* "role" is never initialized in rfcomm code */ - if (listen) { - stat = btSock_start_l2cap_server_l(sock); - } else { - if (fixed_chan) { - if (BTA_JvL2capConnectLE(sock->security, 0, NULL, channel, - L2CAP_DEFAULT_MTU, NULL, sock->addr.address, btsock_l2cap_cbk, - UINT_TO_PTR(sock->id)) != BTA_JV_SUCCESS) - stat = BT_STATUS_FAIL; - - } else { - if (sock->is_le_coc) { - if (BTA_JvL2capConnect(BTA_JV_CONN_TYPE_L2CAP_LE, sock->security, 0, NULL, - channel, L2CAP_MAX_SDU_LENGTH, &cfg, sock->addr.address, - btsock_l2cap_cbk, UINT_TO_PTR(sock->id)) != BTA_JV_SUCCESS) - stat = BT_STATUS_FAIL; - } - else - { - if (BTA_JvL2capConnect(BTA_JV_CONN_TYPE_L2CAP, sock->security, 0, &obex_l2c_etm_opt, - channel, L2CAP_MAX_SDU_LENGTH, &cfg, sock->addr.address, - btsock_l2cap_cbk, UINT_TO_PTR(sock->id)) != BTA_JV_SUCCESS) - stat = BT_STATUS_FAIL; - } - } - } +static bt_status_t btsock_l2cap_listen_or_connect(const char* name, + const bt_bdaddr_t* addr, + int channel, int* sock_fd, + int flags, char listen, + int app_uid) { + bt_status_t stat; + int fixed_chan = 1; + l2cap_socket* sock; + tL2CAP_CFG_INFO cfg; + bool is_le_coc = false; + + if (!sock_fd) return BT_STATUS_PARM_INVALID; + + if (channel < 0) { + // We need to auto assign a PSM + fixed_chan = 0; + } else { + fixed_chan = (channel & L2CAP_MASK_FIXED_CHANNEL) != 0; + is_le_coc = (channel & L2CAP_MASK_LE_COC_CHANNEL) != 0; + channel &= ~(L2CAP_MASK_FIXED_CHANNEL | L2CAP_MASK_LE_COC_CHANNEL); + } + + if (!is_inited()) return BT_STATUS_NOT_READY; + + // TODO: This is kind of bad to lock here, but it is needed for the current + // design. + std::unique_lock lock(state_lock); + sock = btsock_l2cap_alloc_l(name, addr, listen, flags); + if (!sock) { + return BT_STATUS_NOMEM; + } + + sock->fixed_chan = fixed_chan; + sock->channel = channel; + sock->app_uid = app_uid; + sock->is_le_coc = is_le_coc; + + stat = BT_STATUS_SUCCESS; + + /* Setup ETM settings: + * mtu will be set below */ + memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO)); + + cfg.fcr_present = true; + cfg.fcr = obex_l2c_fcr_opts_def; + + /* "role" is never initialized in rfcomm code */ + if (listen) { + stat = btSock_start_l2cap_server_l(sock); + } else { + if (fixed_chan) { + if (BTA_JvL2capConnectLE(sock->security, 0, NULL, channel, + L2CAP_DEFAULT_MTU, NULL, sock->addr.address, + btsock_l2cap_cbk, + UINT_TO_PTR(sock->id)) != BTA_JV_SUCCESS) + stat = BT_STATUS_FAIL; - if (stat == BT_STATUS_SUCCESS) { - *sock_fd = sock->app_fd; - /* We pass the FD to JAVA, but since it runs in another process, we need to also close - * it in native, either straight away, as done when accepting an incoming connection, - * or when doing cleanup after this socket */ - sock->app_fd = -1; /*This leaks the file descriptor. The FD should be closed in - JAVA but it apparently do not work */ - btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_EXCEPTION, - sock->id); } else { - btsock_l2cap_free_l(sock); + if (sock->is_le_coc) { + if (BTA_JvL2capConnect(BTA_JV_CONN_TYPE_L2CAP_LE, sock->security, 0, + NULL, channel, L2CAP_MAX_SDU_LENGTH, &cfg, + sock->addr.address, btsock_l2cap_cbk, + UINT_TO_PTR(sock->id)) != BTA_JV_SUCCESS) + stat = BT_STATUS_FAIL; + } else { + if (BTA_JvL2capConnect(BTA_JV_CONN_TYPE_L2CAP, sock->security, 0, + &obex_l2c_etm_opt, channel, L2CAP_MAX_SDU_LENGTH, + &cfg, sock->addr.address, btsock_l2cap_cbk, + UINT_TO_PTR(sock->id)) != BTA_JV_SUCCESS) + stat = BT_STATUS_FAIL; + } } + } + + if (stat == BT_STATUS_SUCCESS) { + *sock_fd = sock->app_fd; + /* We pass the FD to JAVA, but since it runs in another process, we need to + * also close + * it in native, either straight away, as done when accepting an incoming + * connection, + * or when doing cleanup after this socket */ + sock->app_fd = + -1; /*This leaks the file descriptor. The FD should be closed in + JAVA but it apparently do not work */ + btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, + SOCK_THREAD_FD_EXCEPTION, sock->id); + } else { + btsock_l2cap_free_l(sock); + } - return stat; + return stat; } -bt_status_t btsock_l2cap_listen(const char* name, int channel, int* sock_fd, int flags, int app_uid) -{ - return btsock_l2cap_listen_or_connect(name, NULL, channel, sock_fd, flags, 1, app_uid); +bt_status_t btsock_l2cap_listen(const char* name, int channel, int* sock_fd, + int flags, int app_uid) { + return btsock_l2cap_listen_or_connect(name, NULL, channel, sock_fd, flags, 1, + app_uid); } -bt_status_t btsock_l2cap_connect(const bt_bdaddr_t *bd_addr, int channel, int* sock_fd, int flags, int app_uid) -{ - return btsock_l2cap_listen_or_connect(NULL, bd_addr, channel, sock_fd, flags, 0, app_uid); +bt_status_t btsock_l2cap_connect(const bt_bdaddr_t* bd_addr, int channel, + int* sock_fd, int flags, int app_uid) { + return btsock_l2cap_listen_or_connect(NULL, bd_addr, channel, sock_fd, flags, + 0, app_uid); } -/* return true if we have more to send and should wait for user readiness, false else +/* return true if we have more to send and should wait for user readiness, false + * else * (for example: unrecoverable error or no data) */ -static bool flush_incoming_que_on_wr_signal_l(l2cap_socket *sock) -{ - uint8_t *buf; - uint32_t len; - - while (packet_get_head_l(sock, &buf, &len)) { - ssize_t sent; - OSI_NO_INTR(sent = send(sock->our_fd, buf, len, MSG_DONTWAIT)); - int saved_errno = errno; - - if (sent == (signed)len) - osi_free(buf); - else if (sent >= 0) { - packet_put_head_l(sock, buf + sent, len - sent); - osi_free(buf); - if (!sent) /* special case if other end not keeping up */ - return true; - } - else { - packet_put_head_l(sock, buf, len); - osi_free(buf); - return saved_errno == EWOULDBLOCK || saved_errno == EAGAIN; - } +static bool flush_incoming_que_on_wr_signal_l(l2cap_socket* sock) { + uint8_t* buf; + uint32_t len; + + while (packet_get_head_l(sock, &buf, &len)) { + ssize_t sent; + OSI_NO_INTR(sent = send(sock->our_fd, buf, len, MSG_DONTWAIT)); + int saved_errno = errno; + + if (sent == (signed)len) + osi_free(buf); + else if (sent >= 0) { + packet_put_head_l(sock, buf + sent, len - sent); + osi_free(buf); + if (!sent) /* special case if other end not keeping up */ + return true; + } else { + packet_put_head_l(sock, buf, len); + osi_free(buf); + return saved_errno == EWOULDBLOCK || saved_errno == EAGAIN; } + } - return false; + return false; } -void btsock_l2cap_signaled(int fd, int flags, uint32_t user_id) -{ - l2cap_socket *sock; - char drop_it = false; - - /* We use MSG_DONTWAIT when sending data to JAVA, hence it can be accepted to hold the lock. */ - std::unique_lock lock(state_lock); - sock = btsock_l2cap_find_by_id_l(user_id); - if (!sock) - return; - - if ((flags & SOCK_THREAD_FD_RD) && !sock->server) { - // app sending data - if (sock->connected) { - int size = 0; - - if (!(flags & SOCK_THREAD_FD_EXCEPTION) || (ioctl(sock->our_fd, FIONREAD, &size) - == 0 && size)) { - uint8_t *buffer = (uint8_t*) osi_malloc(L2CAP_MAX_SDU_LENGTH); - /* Apparently we hijack the req_id (uint32_t) to pass the pointer to the buffer to - * the write complete callback, which call a free... wonder if this works on a - * 64 bit platform? */ - /* The socket is created with SOCK_SEQPACKET, hence we read one message at - * the time. The maximum size of a message is allocated to ensure data is - * not lost. This is okay to do as Android uses virtual memory, hence even - * if we only use a fraction of the memory it should not block for others - * to use the memory. As the definition of ioctl(FIONREAD) do not clearly - * define what value will be returned if multiple messages are written to - * the socket before any message is read from the socket, we could - * potentially risk to allocate way more memory than needed. One of the use - * cases for this socket is obex where multiple 64kbyte messages are - * typically written to the socket in a tight loop, hence we risk the ioctl - * will return the total amount of data in the buffer, which could be - * multiple 64kbyte chunks. - * UPDATE: As the stack cannot handle 64kbyte buffers, the size is reduced - * to around 8kbyte - and using malloc for buffer allocation here seems to - * be wrong - * UPDATE: Since we are responsible for freeing the buffer in the - * write_complete_ind, it is OK to use malloc. */ - ssize_t count; - OSI_NO_INTR(count = recv(fd, buffer, L2CAP_MAX_SDU_LENGTH, - MSG_NOSIGNAL | MSG_DONTWAIT)); - APPL_TRACE_DEBUG("btsock_l2cap_signaled - %d bytes received from socket", - count); - - // TODO(armansito): |buffer|, which is created above via - // malloc, is being cast below to uint32_t to be used as - // the |req_id| parameter of BTA_JvL2capWriteFixed and - // BTA_JvL2capWrite. The "id" then gets freed in an - // obscure callback elsewhere. We need to watch out for - // this type of unsafe practice, as this is error prone - // and difficult to follow. - if (sock->fixed_chan) { - if (BTA_JvL2capWriteFixed(sock->channel, - (BD_ADDR*)&sock->addr, - PTR_TO_UINT(buffer), - btsock_l2cap_cbk, buffer, - count, - UINT_TO_PTR(user_id)) != BTA_JV_SUCCESS) { - // On fail, free the buffer - on_l2cap_write_fixed_done(buffer, count, user_id); - } - } else { - if (BTA_JvL2capWrite(sock->handle, PTR_TO_UINT(buffer), - buffer, count, - UINT_TO_PTR(user_id)) != BTA_JV_SUCCESS) { - // On fail, free the buffer - on_l2cap_write_done(buffer, count, user_id); - } - } - } - } else - drop_it = true; - } - if (flags & SOCK_THREAD_FD_WR) { - //app is ready to receive more data, tell stack to enable the data flow - if (flush_incoming_que_on_wr_signal_l(sock) && sock->connected) - btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_WR, sock->id); - } - if (drop_it || (flags & SOCK_THREAD_FD_EXCEPTION)) { - int size = 0; - if (drop_it || ioctl(sock->our_fd, FIONREAD, &size) != 0 || size == 0) - btsock_l2cap_free_l(sock); - } +void btsock_l2cap_signaled(int fd, int flags, uint32_t user_id) { + l2cap_socket* sock; + char drop_it = false; + + /* We use MSG_DONTWAIT when sending data to JAVA, hence it can be accepted to + * hold the lock. */ + std::unique_lock lock(state_lock); + sock = btsock_l2cap_find_by_id_l(user_id); + if (!sock) return; + + if ((flags & SOCK_THREAD_FD_RD) && !sock->server) { + // app sending data + if (sock->connected) { + int size = 0; + + if (!(flags & SOCK_THREAD_FD_EXCEPTION) || + (ioctl(sock->our_fd, FIONREAD, &size) == 0 && size)) { + uint8_t* buffer = (uint8_t*)osi_malloc(L2CAP_MAX_SDU_LENGTH); + /* Apparently we hijack the req_id (uint32_t) to pass the pointer to the + * buffer to + * the write complete callback, which call a free... wonder if this + * works on a + * 64 bit platform? */ + /* The socket is created with SOCK_SEQPACKET, hence we read one message + * at + * the time. The maximum size of a message is allocated to ensure data + * is + * not lost. This is okay to do as Android uses virtual memory, hence + * even + * if we only use a fraction of the memory it should not block for + * others + * to use the memory. As the definition of ioctl(FIONREAD) do not + * clearly + * define what value will be returned if multiple messages are written + * to + * the socket before any message is read from the socket, we could + * potentially risk to allocate way more memory than needed. One of the + * use + * cases for this socket is obex where multiple 64kbyte messages are + * typically written to the socket in a tight loop, hence we risk the + * ioctl + * will return the total amount of data in the buffer, which could be + * multiple 64kbyte chunks. + * UPDATE: As the stack cannot handle 64kbyte buffers, the size is + * reduced + * to around 8kbyte - and using malloc for buffer allocation here seems + * to + * be wrong + * UPDATE: Since we are responsible for freeing the buffer in the + * write_complete_ind, it is OK to use malloc. */ + ssize_t count; + OSI_NO_INTR(count = recv(fd, buffer, L2CAP_MAX_SDU_LENGTH, + MSG_NOSIGNAL | MSG_DONTWAIT)); + APPL_TRACE_DEBUG( + "btsock_l2cap_signaled - %d bytes received from socket", count); + + // TODO(armansito): |buffer|, which is created above via + // malloc, is being cast below to uint32_t to be used as + // the |req_id| parameter of BTA_JvL2capWriteFixed and + // BTA_JvL2capWrite. The "id" then gets freed in an + // obscure callback elsewhere. We need to watch out for + // this type of unsafe practice, as this is error prone + // and difficult to follow. + if (sock->fixed_chan) { + if (BTA_JvL2capWriteFixed(sock->channel, (BD_ADDR*)&sock->addr, + PTR_TO_UINT(buffer), btsock_l2cap_cbk, + buffer, count, + UINT_TO_PTR(user_id)) != BTA_JV_SUCCESS) { + // On fail, free the buffer + on_l2cap_write_fixed_done(buffer, count, user_id); + } + } else { + if (BTA_JvL2capWrite(sock->handle, PTR_TO_UINT(buffer), buffer, count, + UINT_TO_PTR(user_id)) != BTA_JV_SUCCESS) { + // On fail, free the buffer + on_l2cap_write_done(buffer, count, user_id); + } + } + } + } else + drop_it = true; + } + if (flags & SOCK_THREAD_FD_WR) { + // app is ready to receive more data, tell stack to enable the data flow + if (flush_incoming_que_on_wr_signal_l(sock) && sock->connected) + btsock_thread_add_fd(pth, sock->our_fd, BTSOCK_L2CAP, SOCK_THREAD_FD_WR, + sock->id); + } + if (drop_it || (flags & SOCK_THREAD_FD_EXCEPTION)) { + int size = 0; + if (drop_it || ioctl(sock->our_fd, FIONREAD, &size) != 0 || size == 0) + btsock_l2cap_free_l(sock); + } } - diff --git a/btif/src/btif_sock_rfc.cc b/btif/src/btif_sock_rfc.cc index b4fd65d8d..bc92de3c0 100644 --- a/btif/src/btif_sock_rfc.cc +++ b/btif/src/btif_sock_rfc.cc @@ -33,6 +33,7 @@ #include #include +#include "bt_common.h" #include "bt_target.h" #include "bta_api.h" #include "bta_jv_api.h" @@ -46,7 +47,6 @@ #include "btm_api.h" #include "btm_int.h" #include "btu.h" -#include "bt_common.h" #include "hcimsgs.h" #include "osi/include/compat.h" #include "osi/include/list.h" @@ -59,8 +59,10 @@ * L2CAP functions from this file. */ #include "btif_sock_l2cap.h" -#define MAX_RFC_CHANNEL 30 // Maximum number of RFCOMM channels (1-30 inclusive). -#define MAX_RFC_SESSION 7 // Maximum number of devices we can have an RFCOMM connection with. +#define MAX_RFC_CHANNEL \ + 30 // Maximum number of RFCOMM channels (1-30 inclusive). +#define MAX_RFC_SESSION \ + 7 // Maximum number of devices we can have an RFCOMM connection with. typedef struct { int outgoing_congest : 1; @@ -75,39 +77,38 @@ typedef struct { flags_t f; uint32_t id; // Non-zero indicates a valid (in-use) slot. int security; - int scn; // Server channel number + int scn; // Server channel number int scn_notified; bt_bdaddr_t addr; int is_service_uuid_valid; uint8_t service_uuid[16]; char service_name[256]; int fd; - int app_fd; // Temporary storage for the half of the socketpair that's sent back to upper layers. - int app_uid; // UID of the app for which this socket was created. + int app_fd; // Temporary storage for the half of the socketpair that's sent + // back to upper layers. + int app_uid; // UID of the app for which this socket was created. int mtu; - uint8_t *packet; + uint8_t* packet; int sdp_handle; int rfc_handle; int rfc_port_handle; int role; - list_t *incoming_queue; + list_t* incoming_queue; } rfc_slot_t; static rfc_slot_t rfc_slots[MAX_RFC_CHANNEL]; static uint32_t rfc_slot_id; -static volatile int pth = -1; // poll thread handle +static volatile int pth = -1; // poll thread handle static std::recursive_mutex slot_lock; static uid_set_t* uid_set = NULL; -static rfc_slot_t *find_free_slot(void); -static void cleanup_rfc_slot(rfc_slot_t *rs); -static void jv_dm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data); -static void *rfcomm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data); -static bool send_app_scn(rfc_slot_t *rs); +static rfc_slot_t* find_free_slot(void); +static void cleanup_rfc_slot(rfc_slot_t* rs); +static void jv_dm_cback(tBTA_JV_EVT event, tBTA_JV* p_data, void* user_data); +static void* rfcomm_cback(tBTA_JV_EVT event, tBTA_JV* p_data, void* user_data); +static bool send_app_scn(rfc_slot_t* rs); -static bool is_init_done(void) { - return pth != -1; -} +static bool is_init_done(void) { return pth != -1; } bt_status_t btsock_rfc_init(int poll_thread_handle, uid_set_t* set) { pth = poll_thread_handle; @@ -136,36 +137,34 @@ void btsock_rfc_cleanup(void) { std::unique_lock lock(slot_lock); for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i) { - if (rfc_slots[i].id) - cleanup_rfc_slot(&rfc_slots[i]); + if (rfc_slots[i].id) cleanup_rfc_slot(&rfc_slots[i]); list_free(rfc_slots[i].incoming_queue); rfc_slots[i].incoming_queue = NULL; } } -static rfc_slot_t *find_free_slot(void) { +static rfc_slot_t* find_free_slot(void) { for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i) - if (rfc_slots[i].fd == INVALID_FD) - return &rfc_slots[i]; + if (rfc_slots[i].fd == INVALID_FD) return &rfc_slots[i]; return NULL; } -static rfc_slot_t *find_rfc_slot_by_id(uint32_t id) { +static rfc_slot_t* find_rfc_slot_by_id(uint32_t id) { assert(id != 0); for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i) - if (rfc_slots[i].id == id) - return &rfc_slots[i]; + if (rfc_slots[i].id == id) return &rfc_slots[i]; LOG_ERROR(LOG_TAG, "%s unable to find RFCOMM slot id: %d", __func__, id); return NULL; } -static rfc_slot_t *find_rfc_slot_by_pending_sdp(void) { +static rfc_slot_t* find_rfc_slot_by_pending_sdp(void) { uint32_t min_id = UINT32_MAX; int slot = -1; for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i) - if (rfc_slots[i].id && rfc_slots[i].f.pending_sdp_request && rfc_slots[i].id < min_id) { + if (rfc_slots[i].id && rfc_slots[i].f.pending_sdp_request && + rfc_slots[i].id < min_id) { min_id = rfc_slots[i].id; slot = i; } @@ -175,37 +174,38 @@ static rfc_slot_t *find_rfc_slot_by_pending_sdp(void) { static bool is_requesting_sdp(void) { for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i) - if (rfc_slots[i].id && rfc_slots[i].f.doing_sdp_request) - return true; + if (rfc_slots[i].id && rfc_slots[i].f.doing_sdp_request) return true; return false; } -static rfc_slot_t *alloc_rfc_slot(const bt_bdaddr_t *addr, const char *name, const uint8_t *uuid, int channel, int flags, bool server) { +static rfc_slot_t* alloc_rfc_slot(const bt_bdaddr_t* addr, const char* name, + const uint8_t* uuid, int channel, int flags, + bool server) { int security = 0; - if(flags & BTSOCK_FLAG_ENCRYPT) - security |= server ? BTM_SEC_IN_ENCRYPT : BTM_SEC_OUT_ENCRYPT; - if(flags & BTSOCK_FLAG_AUTH) - security |= server ? BTM_SEC_IN_AUTHENTICATE : BTM_SEC_OUT_AUTHENTICATE; - if(flags & BTSOCK_FLAG_AUTH_MITM) - security |= server ? BTM_SEC_IN_MITM : BTM_SEC_OUT_MITM; - if(flags & BTSOCK_FLAG_AUTH_16_DIGIT) - security |= BTM_SEC_IN_MIN_16_DIGIT_PIN; - - rfc_slot_t *slot = find_free_slot(); + if (flags & BTSOCK_FLAG_ENCRYPT) + security |= server ? BTM_SEC_IN_ENCRYPT : BTM_SEC_OUT_ENCRYPT; + if (flags & BTSOCK_FLAG_AUTH) + security |= server ? BTM_SEC_IN_AUTHENTICATE : BTM_SEC_OUT_AUTHENTICATE; + if (flags & BTSOCK_FLAG_AUTH_MITM) + security |= server ? BTM_SEC_IN_MITM : BTM_SEC_OUT_MITM; + if (flags & BTSOCK_FLAG_AUTH_16_DIGIT) + security |= BTM_SEC_IN_MIN_16_DIGIT_PIN; + + rfc_slot_t* slot = find_free_slot(); if (!slot) { LOG_ERROR(LOG_TAG, "%s unable to find free RFCOMM slot.", __func__); return NULL; } - int fds[2] = { INVALID_FD, INVALID_FD }; + int fds[2] = {INVALID_FD, INVALID_FD}; if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) == -1) { - LOG_ERROR(LOG_TAG, "%s error creating socketpair: %s", __func__, strerror(errno)); + LOG_ERROR(LOG_TAG, "%s error creating socketpair: %s", __func__, + strerror(errno)); return NULL; } // Increment slot id and make sure we don't use id=0. - if (++rfc_slot_id == 0) - rfc_slot_id = 1; + if (++rfc_slot_id == 0) rfc_slot_id = 1; slot->fd = fds[0]; slot->app_fd = fds[1]; @@ -213,20 +213,19 @@ static rfc_slot_t *alloc_rfc_slot(const bt_bdaddr_t *addr, const char *name, con slot->scn = channel; slot->app_uid = -1; - if(!is_uuid_empty(uuid)) { + if (!is_uuid_empty(uuid)) { memcpy(slot->service_uuid, uuid, sizeof(slot->service_uuid)); slot->is_service_uuid_valid = true; } else { memset(slot->service_uuid, 0, sizeof(slot->service_uuid)); slot->is_service_uuid_valid = false; } - if(name && *name) { + if (name && *name) { strlcpy(slot->service_name, name, sizeof(slot->service_name)); } else { memset(slot->service_name, 0, sizeof(slot->service_name)); } - if (addr) - slot->addr = *addr; + if (addr) slot->addr = *addr; slot->id = rfc_slot_id; slot->f.server = server; @@ -234,8 +233,12 @@ static rfc_slot_t *alloc_rfc_slot(const bt_bdaddr_t *addr, const char *name, con return slot; } -static rfc_slot_t *create_srv_accept_rfc_slot(rfc_slot_t *srv_rs, const bt_bdaddr_t *addr, int open_handle, int new_listen_handle) { - rfc_slot_t *accept_rs = alloc_rfc_slot(addr, srv_rs->service_name, srv_rs->service_uuid, srv_rs->scn, 0, false); +static rfc_slot_t* create_srv_accept_rfc_slot(rfc_slot_t* srv_rs, + const bt_bdaddr_t* addr, + int open_handle, + int new_listen_handle) { + rfc_slot_t* accept_rs = alloc_rfc_slot( + addr, srv_rs->service_name, srv_rs->service_uuid, srv_rs->scn, 0, false); if (!accept_rs) { LOG_ERROR(LOG_TAG, "%s unable to allocate RFCOMM slot.", __func__); return NULL; @@ -263,80 +266,96 @@ static rfc_slot_t *create_srv_accept_rfc_slot(rfc_slot_t *srv_rs, const bt_bdadd return accept_rs; } -bt_status_t btsock_rfc_listen(const char *service_name, const uint8_t *service_uuid, int channel, int *sock_fd, int flags, int app_uid) { +bt_status_t btsock_rfc_listen(const char* service_name, + const uint8_t* service_uuid, int channel, + int* sock_fd, int flags, int app_uid) { assert(sock_fd != NULL); - assert((service_uuid != NULL) - || (channel >= 1 && channel <= MAX_RFC_CHANNEL) - || ((flags & BTSOCK_FLAG_NO_SDP) != 0)); + assert((service_uuid != NULL) || + (channel >= 1 && channel <= MAX_RFC_CHANNEL) || + ((flags & BTSOCK_FLAG_NO_SDP) != 0)); *sock_fd = INVALID_FD; - // TODO(sharvil): not sure that this check makes sense; seems like a logic error to call - // functions on RFCOMM sockets before initializing the module. Probably should be an assert. - if (!is_init_done()) - return BT_STATUS_NOT_READY; - - if((flags & BTSOCK_FLAG_NO_SDP) == 0) { - if(is_uuid_empty(service_uuid)) { - APPL_TRACE_DEBUG("BTA_JvGetChannelId: service_uuid not set AND " - "BTSOCK_FLAG_NO_SDP is not set - changing to SPP"); - service_uuid = UUID_SPP; // Use serial port profile to listen to specified channel + // TODO(sharvil): not sure that this check makes sense; seems like a logic + // error to call + // functions on RFCOMM sockets before initializing the module. Probably should + // be an assert. + if (!is_init_done()) return BT_STATUS_NOT_READY; + + if ((flags & BTSOCK_FLAG_NO_SDP) == 0) { + if (is_uuid_empty(service_uuid)) { + APPL_TRACE_DEBUG( + "BTA_JvGetChannelId: service_uuid not set AND " + "BTSOCK_FLAG_NO_SDP is not set - changing to SPP"); + service_uuid = + UUID_SPP; // Use serial port profile to listen to specified channel } else { - //Check the service_uuid. overwrite the channel # if reserved + // Check the service_uuid. overwrite the channel # if reserved int reserved_channel = get_reserved_rfc_channel(service_uuid); if (reserved_channel > 0) { - channel = reserved_channel; + channel = reserved_channel; } } } std::unique_lock lock(slot_lock); - rfc_slot_t *slot = alloc_rfc_slot(NULL, service_name, service_uuid, channel, flags, true); + rfc_slot_t* slot = + alloc_rfc_slot(NULL, service_name, service_uuid, channel, flags, true); if (!slot) { LOG_ERROR(LOG_TAG, "%s unable to allocate RFCOMM slot.", __func__); return BT_STATUS_FAIL; } - APPL_TRACE_DEBUG("BTA_JvGetChannelId: service_name: %s - channel: %d", service_name, channel); + APPL_TRACE_DEBUG("BTA_JvGetChannelId: service_name: %s - channel: %d", + service_name, channel); BTA_JvGetChannelId(BTA_JV_CONN_TYPE_RFCOMM, UINT_TO_PTR(slot->id), channel); - *sock_fd = slot->app_fd; // Transfer ownership of fd to caller. + *sock_fd = slot->app_fd; // Transfer ownership of fd to caller. /*TODO: - * We are leaking one of the app_fd's - either the listen socket, or the connection socket. + * We are leaking one of the app_fd's - either the listen socket, or the + connection socket. * WE need to close this in native, as the FD might belong to another process - This is the server socket FD - For accepted connections, we close the FD after passing it to JAVA. - Try to simply remove the = -1 to free the FD at rs cleanup.*/ -// close(rs->app_fd); + // close(rs->app_fd); slot->app_fd = INVALID_FD; // Drop our reference to the fd. slot->app_uid = app_uid; - btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_EXCEPTION, slot->id); + btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_EXCEPTION, + slot->id); return BT_STATUS_SUCCESS; } -bt_status_t btsock_rfc_connect(const bt_bdaddr_t *bd_addr, const uint8_t *service_uuid, int channel, int *sock_fd, int flags, int app_uid) { +bt_status_t btsock_rfc_connect(const bt_bdaddr_t* bd_addr, + const uint8_t* service_uuid, int channel, + int* sock_fd, int flags, int app_uid) { assert(sock_fd != NULL); assert(service_uuid != NULL || (channel >= 1 && channel <= MAX_RFC_CHANNEL)); *sock_fd = INVALID_FD; - // TODO(sharvil): not sure that this check makes sense; seems like a logic error to call - // functions on RFCOMM sockets before initializing the module. Probably should be an assert. - if (!is_init_done()) - return BT_STATUS_NOT_READY; + // TODO(sharvil): not sure that this check makes sense; seems like a logic + // error to call + // functions on RFCOMM sockets before initializing the module. Probably should + // be an assert. + if (!is_init_done()) return BT_STATUS_NOT_READY; std::unique_lock lock(slot_lock); - rfc_slot_t *slot = alloc_rfc_slot(bd_addr, NULL, service_uuid, channel, flags, false); + rfc_slot_t* slot = + alloc_rfc_slot(bd_addr, NULL, service_uuid, channel, flags, false); if (!slot) { LOG_ERROR(LOG_TAG, "%s unable to allocate RFCOMM slot.", __func__); return BT_STATUS_FAIL; } if (is_uuid_empty(service_uuid)) { - tBTA_JV_STATUS ret = BTA_JvRfcommConnect(slot->security, slot->role, slot->scn, slot->addr.address, rfcomm_cback, (void *)(uintptr_t)slot->id); + tBTA_JV_STATUS ret = BTA_JvRfcommConnect( + slot->security, slot->role, slot->scn, slot->addr.address, rfcomm_cback, + (void*)(uintptr_t)slot->id); if (ret != BTA_JV_SUCCESS) { - LOG_ERROR(LOG_TAG, "%s unable to initiate RFCOMM connection: %d", __func__, ret); + LOG_ERROR(LOG_TAG, "%s unable to initiate RFCOMM connection: %d", + __func__, ret); cleanup_rfc_slot(slot); return BT_STATUS_FAIL; } @@ -352,7 +371,8 @@ bt_status_t btsock_rfc_connect(const bt_bdaddr_t *bd_addr, const uint8_t *servic memcpy(sdp_uuid.uu.uuid128, service_uuid, sizeof(sdp_uuid.uu.uuid128)); if (!is_requesting_sdp()) { - BTA_JvStartDiscovery((uint8_t *)bd_addr->address, 1, &sdp_uuid, (void *)(uintptr_t)slot->id); + BTA_JvStartDiscovery((uint8_t*)bd_addr->address, 1, &sdp_uuid, + (void*)(uintptr_t)slot->id); slot->f.pending_sdp_request = false; slot->f.doing_sdp_request = true; } else { @@ -364,34 +384,34 @@ bt_status_t btsock_rfc_connect(const bt_bdaddr_t *bd_addr, const uint8_t *servic *sock_fd = slot->app_fd; // Transfer ownership of fd to caller. slot->app_fd = INVALID_FD; // Drop our reference to the fd. slot->app_uid = app_uid; - btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, slot->id); + btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, + slot->id); return BT_STATUS_SUCCESS; } -static int create_server_sdp_record(rfc_slot_t *slot) { - if(slot->scn == 0) { - return false; - } - slot->sdp_handle = add_rfc_sdp_rec(slot->service_name, slot->service_uuid, slot->scn); +static int create_server_sdp_record(rfc_slot_t* slot) { + if (slot->scn == 0) { + return false; + } + slot->sdp_handle = + add_rfc_sdp_rec(slot->service_name, slot->service_uuid, slot->scn); return (slot->sdp_handle > 0); } -static void free_rfc_slot_scn(rfc_slot_t *slot) { - if (slot->scn <= 0) - return; +static void free_rfc_slot_scn(rfc_slot_t* slot) { + if (slot->scn <= 0) return; - if(slot->f.server && !slot->f.closing && slot->rfc_handle) { - BTA_JvRfcommStopServer(slot->rfc_handle, (void *)(uintptr_t)slot->id); + if (slot->f.server && !slot->f.closing && slot->rfc_handle) { + BTA_JvRfcommStopServer(slot->rfc_handle, (void*)(uintptr_t)slot->id); slot->rfc_handle = 0; } - if (slot->f.server) - BTM_FreeSCN(slot->scn); + if (slot->f.server) BTM_FreeSCN(slot->scn); slot->scn = 0; } -static void cleanup_rfc_slot(rfc_slot_t *slot) { +static void cleanup_rfc_slot(rfc_slot_t* slot) { if (slot->fd != INVALID_FD) { shutdown(slot->fd, SHUT_RDWR); close(slot->fd); @@ -409,7 +429,7 @@ static void cleanup_rfc_slot(rfc_slot_t *slot) { } if (slot->rfc_handle && !slot->f.closing && !slot->f.server) { - BTA_JvRfcommClose(slot->rfc_handle, (void *)(uintptr_t)slot->id); + BTA_JvRfcommClose(slot->rfc_handle, (void*)(uintptr_t)slot->id); slot->rfc_handle = 0; } @@ -422,34 +442,36 @@ static void cleanup_rfc_slot(rfc_slot_t *slot) { slot->scn_notified = false; } -static bool send_app_scn(rfc_slot_t *slot) { - if(slot->scn_notified == true) { - //already send, just return success. +static bool send_app_scn(rfc_slot_t* slot) { + if (slot->scn_notified == true) { + // already send, just return success. return true; } slot->scn_notified = true; - return sock_send_all(slot->fd, (const uint8_t*)&slot->scn, sizeof(slot->scn)) == sizeof(slot->scn); + return sock_send_all(slot->fd, (const uint8_t*)&slot->scn, + sizeof(slot->scn)) == sizeof(slot->scn); } -static bool send_app_connect_signal(int fd, const bt_bdaddr_t* addr, int channel, int status, int send_fd) { +static bool send_app_connect_signal(int fd, const bt_bdaddr_t* addr, + int channel, int status, int send_fd) { sock_connect_signal_t cs; cs.size = sizeof(cs); cs.bd_addr = *addr; cs.channel = channel; cs.status = status; - cs.max_rx_packet_size = 0; // not used for RFCOMM - cs.max_tx_packet_size = 0; // not used for RFCOMM + cs.max_rx_packet_size = 0; // not used for RFCOMM + cs.max_tx_packet_size = 0; // not used for RFCOMM if (send_fd == INVALID_FD) - return sock_send_all(fd, (const uint8_t *)&cs, sizeof(cs)) == sizeof(cs); + return sock_send_all(fd, (const uint8_t*)&cs, sizeof(cs)) == sizeof(cs); - return sock_send_fd(fd, (const uint8_t *)&cs, sizeof(cs), send_fd) == sizeof(cs); + return sock_send_fd(fd, (const uint8_t*)&cs, sizeof(cs), send_fd) == + sizeof(cs); } -static void on_cl_rfc_init(tBTA_JV_RFCOMM_CL_INIT *p_init, uint32_t id) { +static void on_cl_rfc_init(tBTA_JV_RFCOMM_CL_INIT* p_init, uint32_t id) { std::unique_lock lock(slot_lock); - rfc_slot_t *slot = find_rfc_slot_by_id(id); - if (!slot) - return; + rfc_slot_t* slot = find_rfc_slot_by_id(id); + if (!slot) return; if (p_init->status == BTA_JV_SUCCESS) { slot->rfc_handle = p_init->handle; @@ -458,11 +480,11 @@ static void on_cl_rfc_init(tBTA_JV_RFCOMM_CL_INIT *p_init, uint32_t id) { } } -static void on_srv_rfc_listen_started(tBTA_JV_RFCOMM_START *p_start, uint32_t id) { +static void on_srv_rfc_listen_started(tBTA_JV_RFCOMM_START* p_start, + uint32_t id) { std::unique_lock lock(slot_lock); - rfc_slot_t *slot = find_rfc_slot_by_id(id); - if (!slot) - return; + rfc_slot_t* slot = find_rfc_slot_by_id(id); + if (!slot) return; if (p_start->status == BTA_JV_SUCCESS) { slot->rfc_handle = p_start->handle; @@ -471,30 +493,34 @@ static void on_srv_rfc_listen_started(tBTA_JV_RFCOMM_START *p_start, uint32_t id } } -static uint32_t on_srv_rfc_connect(tBTA_JV_RFCOMM_SRV_OPEN *p_open, uint32_t id) { +static uint32_t on_srv_rfc_connect(tBTA_JV_RFCOMM_SRV_OPEN* p_open, + uint32_t id) { std::unique_lock lock(slot_lock); - rfc_slot_t *accept_rs; - rfc_slot_t *srv_rs = find_rfc_slot_by_id(id); - if (!srv_rs) - return 0; + rfc_slot_t* accept_rs; + rfc_slot_t* srv_rs = find_rfc_slot_by_id(id); + if (!srv_rs) return 0; - accept_rs = create_srv_accept_rfc_slot(srv_rs, (const bt_bdaddr_t *)p_open->rem_bda, p_open->handle, p_open->new_listen_handle); - if (!accept_rs) - return 0; + accept_rs = + create_srv_accept_rfc_slot(srv_rs, (const bt_bdaddr_t*)p_open->rem_bda, + p_open->handle, p_open->new_listen_handle); + if (!accept_rs) return 0; // Start monitoring the socket. - btsock_thread_add_fd(pth, srv_rs->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_EXCEPTION, srv_rs->id); - btsock_thread_add_fd(pth, accept_rs->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, accept_rs->id); - send_app_connect_signal(srv_rs->fd, &accept_rs->addr, srv_rs->scn, 0, accept_rs->app_fd); - accept_rs->app_fd = INVALID_FD; // Ownership of the application fd has been transferred. + btsock_thread_add_fd(pth, srv_rs->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_EXCEPTION, + srv_rs->id); + btsock_thread_add_fd(pth, accept_rs->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, + accept_rs->id); + send_app_connect_signal(srv_rs->fd, &accept_rs->addr, srv_rs->scn, 0, + accept_rs->app_fd); + accept_rs->app_fd = + INVALID_FD; // Ownership of the application fd has been transferred. return srv_rs->id; } -static void on_cli_rfc_connect(tBTA_JV_RFCOMM_OPEN *p_open, uint32_t id) { +static void on_cli_rfc_connect(tBTA_JV_RFCOMM_OPEN* p_open, uint32_t id) { std::unique_lock lock(slot_lock); - rfc_slot_t *slot = find_rfc_slot_by_id(id); - if (!slot) - return; + rfc_slot_t* slot = find_rfc_slot_by_id(id); + if (!slot) return; if (p_open->status != BTA_JV_SUCCESS) { cleanup_rfc_slot(slot); @@ -507,52 +533,56 @@ static void on_cli_rfc_connect(tBTA_JV_RFCOMM_OPEN *p_open, uint32_t id) { if (send_app_connect_signal(slot->fd, &slot->addr, slot->scn, 0, -1)) { slot->f.connected = true; } else { - LOG_ERROR(LOG_TAG, "%s unable to send connect completion signal to caller.", __func__); + LOG_ERROR(LOG_TAG, "%s unable to send connect completion signal to caller.", + __func__); } } -static void on_rfc_close(UNUSED_ATTR tBTA_JV_RFCOMM_CLOSE *p_close, uint32_t id) { +static void on_rfc_close(UNUSED_ATTR tBTA_JV_RFCOMM_CLOSE* p_close, + uint32_t id) { std::unique_lock lock(slot_lock); // rfc_handle already closed when receiving rfcomm close event from stack. - rfc_slot_t *slot = find_rfc_slot_by_id(id); - if (slot) - cleanup_rfc_slot(slot); + rfc_slot_t* slot = find_rfc_slot_by_id(id); + if (slot) cleanup_rfc_slot(slot); } -static void on_rfc_write_done(tBTA_JV_RFCOMM_WRITE *p, uint32_t id) { +static void on_rfc_write_done(tBTA_JV_RFCOMM_WRITE* p, uint32_t id) { if (p->status != BTA_JV_SUCCESS) { - LOG_ERROR(LOG_TAG, "%s error writing to RFCOMM socket with slot %u.", __func__, p->req_id); + LOG_ERROR(LOG_TAG, "%s error writing to RFCOMM socket with slot %u.", + __func__, p->req_id); return; } int app_uid = -1; std::unique_lock lock(slot_lock); - rfc_slot_t *slot = find_rfc_slot_by_id(id); + rfc_slot_t* slot = find_rfc_slot_by_id(id); if (slot) { app_uid = slot->app_uid; if (!slot->f.outgoing_congest) { - btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, slot->id); + btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, + slot->id); } } uid_set_add_tx(uid_set, app_uid, p->len); } -static void on_rfc_outgoing_congest(tBTA_JV_RFCOMM_CONG *p, uint32_t id) { +static void on_rfc_outgoing_congest(tBTA_JV_RFCOMM_CONG* p, uint32_t id) { std::unique_lock lock(slot_lock); - rfc_slot_t *slot = find_rfc_slot_by_id(id); + rfc_slot_t* slot = find_rfc_slot_by_id(id); if (slot) { slot->f.outgoing_congest = p->cong ? 1 : 0; if (!slot->f.outgoing_congest) - btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, slot->id); + btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, + slot->id); } } -static void *rfcomm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data) { - void *new_user_data = NULL; +static void* rfcomm_cback(tBTA_JV_EVT event, tBTA_JV* p_data, void* user_data) { + void* new_user_data = NULL; switch (event) { case BTA_JV_RFCOMM_START_EVT: @@ -564,17 +594,21 @@ static void *rfcomm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data) { break; case BTA_JV_RFCOMM_OPEN_EVT: - BTA_JvSetPmProfile(p_data->rfc_open.handle,BTA_JV_PM_ID_1,BTA_JV_CONN_OPEN); + BTA_JvSetPmProfile(p_data->rfc_open.handle, BTA_JV_PM_ID_1, + BTA_JV_CONN_OPEN); on_cli_rfc_connect(&p_data->rfc_open, (uintptr_t)user_data); break; case BTA_JV_RFCOMM_SRV_OPEN_EVT: - BTA_JvSetPmProfile(p_data->rfc_srv_open.handle,BTA_JV_PM_ALL,BTA_JV_CONN_OPEN); - new_user_data = (void *)(uintptr_t)on_srv_rfc_connect(&p_data->rfc_srv_open, (uintptr_t)user_data); + BTA_JvSetPmProfile(p_data->rfc_srv_open.handle, BTA_JV_PM_ALL, + BTA_JV_CONN_OPEN); + new_user_data = (void*)(uintptr_t)on_srv_rfc_connect( + &p_data->rfc_srv_open, (uintptr_t)user_data); break; case BTA_JV_RFCOMM_CLOSE_EVT: - APPL_TRACE_DEBUG("BTA_JV_RFCOMM_CLOSE_EVT: user_data:%d", (uintptr_t)user_data); + APPL_TRACE_DEBUG("BTA_JV_RFCOMM_CLOSE_EVT: user_data:%d", + (uintptr_t)user_data); on_rfc_close(&p_data->rfc_close, (uintptr_t)user_data); break; @@ -591,92 +625,109 @@ static void *rfcomm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data) { break; default: - LOG_ERROR(LOG_TAG, "%s unhandled event %d, slot id: %zi", __func__, event, (uintptr_t)user_data); + LOG_ERROR(LOG_TAG, "%s unhandled event %d, slot id: %zi", __func__, event, + (uintptr_t)user_data); break; } return new_user_data; } -static void jv_dm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data) { +static void jv_dm_cback(tBTA_JV_EVT event, tBTA_JV* p_data, void* user_data) { uint32_t id = PTR_TO_UINT(user_data); - switch(event) { - case BTA_JV_GET_SCN_EVT: - { + switch (event) { + case BTA_JV_GET_SCN_EVT: { std::unique_lock lock(slot_lock); rfc_slot_t* rs = find_rfc_slot_by_id(id); int new_scn = p_data->scn; if (rs && (new_scn != 0)) { rs->scn = new_scn; - /* BTA_JvCreateRecordByUser will only create a record if a UUID is specified, - * else it just allocate a RFC channel and start the RFCOMM thread - needed + /* BTA_JvCreateRecordByUser will only create a record if a UUID is + * specified, + * else it just allocate a RFC channel and start the RFCOMM thread - + * needed * for the java * layer to get a RFCOMM channel. - * If uuid is null the create_sdp_record() will be called from Java when it - * has received the RFCOMM and L2CAP channel numbers through the sockets.*/ + * If uuid is null the create_sdp_record() will be called from Java when + * it + * has received the RFCOMM and L2CAP channel numbers through the + * sockets.*/ // Send channel ID to java layer if (!send_app_scn(rs)) { - //closed + // closed APPL_TRACE_DEBUG("send_app_scn() failed, close rs->id:%d", rs->id); cleanup_rfc_slot(rs); } else { if (rs->is_service_uuid_valid == true) { - // We already have data for SDP record, create it (RFC-only profiles) + // We already have data for SDP record, create it (RFC-only + // profiles) BTA_JvCreateRecordByUser(UINT_TO_PTR(rs->id)); } else { - APPL_TRACE_DEBUG("is_service_uuid_valid==false - don't set SDP-record, " - "just start the RFCOMM server", rs->id); - //now start the rfcomm server after sdp & channel # assigned - BTA_JvRfcommStartServer(rs->security, rs->role, rs->scn, MAX_RFC_SESSION, - rfcomm_cback, UINT_TO_PTR(rs->id)); + APPL_TRACE_DEBUG( + "is_service_uuid_valid==false - don't set SDP-record, " + "just start the RFCOMM server", + rs->id); + // now start the rfcomm server after sdp & channel # assigned + BTA_JvRfcommStartServer(rs->security, rs->role, rs->scn, + MAX_RFC_SESSION, rfcomm_cback, + UINT_TO_PTR(rs->id)); } } } else if (rs) { - APPL_TRACE_ERROR("jv_dm_cback: Error: allocate channel %d, slot found:%p", rs->scn, rs); + APPL_TRACE_ERROR( + "jv_dm_cback: Error: allocate channel %d, slot found:%p", rs->scn, + rs); cleanup_rfc_slot(rs); } break; } - case BTA_JV_GET_PSM_EVT: - { + case BTA_JV_GET_PSM_EVT: { APPL_TRACE_DEBUG("Received PSM: 0x%04x", p_data->psm); on_l2cap_psm_assigned(id, p_data->psm); break; } - case BTA_JV_CREATE_RECORD_EVT: - { + case BTA_JV_CREATE_RECORD_EVT: { std::unique_lock lock(slot_lock); - rfc_slot_t *slot = find_rfc_slot_by_id(id); + rfc_slot_t* slot = find_rfc_slot_by_id(id); if (slot && create_server_sdp_record(slot)) { // Start the rfcomm server after sdp & channel # assigned. - BTA_JvRfcommStartServer(slot->security, slot->role, slot->scn, MAX_RFC_SESSION, rfcomm_cback, (void *)(uintptr_t)slot->id); - } else if(slot) { - APPL_TRACE_ERROR("jv_dm_cback: cannot start server, slot found:%p", slot); + BTA_JvRfcommStartServer(slot->security, slot->role, slot->scn, + MAX_RFC_SESSION, rfcomm_cback, + (void*)(uintptr_t)slot->id); + } else if (slot) { + APPL_TRACE_ERROR("jv_dm_cback: cannot start server, slot found:%p", + slot); cleanup_rfc_slot(slot); } break; } - case BTA_JV_DISCOVERY_COMP_EVT: - { + case BTA_JV_DISCOVERY_COMP_EVT: { std::unique_lock lock(slot_lock); - rfc_slot_t *slot = find_rfc_slot_by_id(id); + rfc_slot_t* slot = find_rfc_slot_by_id(id); if (p_data->disc_comp.status == BTA_JV_SUCCESS && p_data->disc_comp.scn) { if (slot && slot->f.doing_sdp_request) { - // Establish the connection if we successfully looked up a channel number to connect to. - if (BTA_JvRfcommConnect(slot->security, slot->role, p_data->disc_comp.scn, slot->addr.address, rfcomm_cback, (void *)(uintptr_t)slot->id) == BTA_JV_SUCCESS) { + // Establish the connection if we successfully looked up a channel + // number to connect to. + if (BTA_JvRfcommConnect(slot->security, slot->role, + p_data->disc_comp.scn, slot->addr.address, + rfcomm_cback, (void*)(uintptr_t)slot->id) == + BTA_JV_SUCCESS) { slot->scn = p_data->disc_comp.scn; slot->f.doing_sdp_request = false; - if (!send_app_scn(slot)) - cleanup_rfc_slot(slot); + if (!send_app_scn(slot)) cleanup_rfc_slot(slot); } else { cleanup_rfc_slot(slot); } } else if (slot) { - // TODO(sharvil): this is really a logic error and we should probably assert. - LOG_ERROR(LOG_TAG, "%s SDP response returned but RFCOMM slot %d did not request SDP record.", __func__, id); + // TODO(sharvil): this is really a logic error and we should probably + // assert. + LOG_ERROR(LOG_TAG, + "%s SDP response returned but RFCOMM slot %d did not " + "request SDP record.", + __func__, id); } } else if (slot) { cleanup_rfc_slot(slot); @@ -687,8 +738,10 @@ static void jv_dm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data) { if (slot) { tSDP_UUID sdp_uuid; sdp_uuid.len = 16; - memcpy(sdp_uuid.uu.uuid128, slot->service_uuid, sizeof(sdp_uuid.uu.uuid128)); - BTA_JvStartDiscovery((uint8_t *)slot->addr.address, 1, &sdp_uuid, (void *)(uintptr_t)slot->id); + memcpy(sdp_uuid.uu.uuid128, slot->service_uuid, + sizeof(sdp_uuid.uu.uuid128)); + BTA_JvStartDiscovery((uint8_t*)slot->addr.address, 1, &sdp_uuid, + (void*)(uintptr_t)slot->id); slot->f.pending_sdp_request = false; slot->f.doing_sdp_request = true; } @@ -708,40 +761,38 @@ typedef enum { SENT_ALL, } sent_status_t; -static sent_status_t send_data_to_app(int fd, BT_HDR *p_buf) { - if (p_buf->len == 0) - return SENT_ALL; +static sent_status_t send_data_to_app(int fd, BT_HDR* p_buf) { + if (p_buf->len == 0) return SENT_ALL; ssize_t sent; - OSI_NO_INTR(sent = send(fd, p_buf->data + p_buf->offset, p_buf->len, - MSG_DONTWAIT)); + OSI_NO_INTR( + sent = send(fd, p_buf->data + p_buf->offset, p_buf->len, MSG_DONTWAIT)); if (sent == -1) { - if (errno == EAGAIN || errno == EWOULDBLOCK) - return SENT_NONE; - LOG_ERROR(LOG_TAG, "%s error writing RFCOMM data back to app: %s", __func__, strerror(errno)); + if (errno == EAGAIN || errno == EWOULDBLOCK) return SENT_NONE; + LOG_ERROR(LOG_TAG, "%s error writing RFCOMM data back to app: %s", __func__, + strerror(errno)); return SENT_FAILED; } - if (sent == 0) - return SENT_FAILED; + if (sent == 0) return SENT_FAILED; - if (sent == p_buf->len) - return SENT_ALL; + if (sent == p_buf->len) return SENT_ALL; p_buf->offset += sent; p_buf->len -= sent; return SENT_PARTIAL; } -static bool flush_incoming_que_on_wr_signal(rfc_slot_t *slot) { +static bool flush_incoming_que_on_wr_signal(rfc_slot_t* slot) { while (!list_is_empty(slot->incoming_queue)) { - BT_HDR *p_buf = (BT_HDR *)list_front(slot->incoming_queue); + BT_HDR* p_buf = (BT_HDR*)list_front(slot->incoming_queue); switch (send_data_to_app(slot->fd, p_buf)) { case SENT_NONE: case SENT_PARTIAL: - //monitor the fd to get callback when app is ready to receive data - btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_WR, slot->id); + // monitor the fd to get callback when app is ready to receive data + btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_WR, + slot->id); return true; case SENT_ALL: @@ -754,9 +805,10 @@ static bool flush_incoming_que_on_wr_signal(rfc_slot_t *slot) { } } - //app is ready to receive data, tell stack to start the data flow - //fix me: need a jv flow control api to serialize the call in stack - APPL_TRACE_DEBUG("enable data flow, rfc_handle:0x%x, rfc_port_handle:0x%x, user_id:%d", + // app is ready to receive data, tell stack to start the data flow + // fix me: need a jv flow control api to serialize the call in stack + APPL_TRACE_DEBUG( + "enable data flow, rfc_handle:0x%x, rfc_port_handle:0x%x, user_id:%d", slot->rfc_handle, slot->rfc_port_handle, slot->id); PORT_FlowControl_MaxCredit(slot->rfc_port_handle, true); return true; @@ -765,20 +817,23 @@ static bool flush_incoming_que_on_wr_signal(rfc_slot_t *slot) { void btsock_rfc_signaled(UNUSED_ATTR int fd, int flags, uint32_t user_id) { bool need_close = false; std::unique_lock lock(slot_lock); - rfc_slot_t *slot = find_rfc_slot_by_id(user_id); - if (!slot) - return; + rfc_slot_t* slot = find_rfc_slot_by_id(user_id); + if (!slot) return; // Data available from app, tell stack we have outgoing data. if (flags & SOCK_THREAD_FD_RD && !slot->f.server) { if (slot->f.connected) { // Make sure there's data pending in case the peer closed the socket. int size = 0; - if (!(flags & SOCK_THREAD_FD_EXCEPTION) || (ioctl(slot->fd, FIONREAD, &size) == 0 && size)) { + if (!(flags & SOCK_THREAD_FD_EXCEPTION) || + (ioctl(slot->fd, FIONREAD, &size) == 0 && size)) { BTA_JvRfcommWrite(slot->rfc_handle, slot->id); } } else { - LOG_ERROR(LOG_TAG, "%s socket signaled for read while disconnected, slot: %d, channel: %d", __func__, slot->id, slot->scn); + LOG_ERROR(LOG_TAG, + "%s socket signaled for read while disconnected, slot: %d, " + "channel: %d", + __func__, slot->id, slot->scn); need_close = true; } } @@ -786,7 +841,10 @@ void btsock_rfc_signaled(UNUSED_ATTR int fd, int flags, uint32_t user_id) { if (flags & SOCK_THREAD_FD_WR) { // App is ready to receive more data, tell stack to enable data flow. if (!slot->f.connected || !flush_incoming_que_on_wr_signal(slot)) { - LOG_ERROR(LOG_TAG, "%s socket signaled for write while disconnected (or write failure), slot: %d, channel: %d", __func__, slot->id, slot->scn); + LOG_ERROR(LOG_TAG, + "%s socket signaled for write while disconnected (or write " + "failure), slot: %d, channel: %d", + __func__, slot->id, slot->scn); need_close = true; } } @@ -799,15 +857,14 @@ void btsock_rfc_signaled(UNUSED_ATTR int fd, int flags, uint32_t user_id) { } } -int bta_co_rfc_data_incoming(void *user_data, BT_HDR *p_buf) { +int bta_co_rfc_data_incoming(void* user_data, BT_HDR* p_buf) { int app_uid = -1; uint64_t bytes_rx = 0; int ret = 0; uint32_t id = (uintptr_t)user_data; std::unique_lock lock(slot_lock); - rfc_slot_t *slot = find_rfc_slot_by_id(id); - if (!slot) - return 0; + rfc_slot_t* slot = find_rfc_slot_by_id(id); + if (!slot) return 0; app_uid = slot->app_uid; bytes_rx = p_buf->len; @@ -817,7 +874,8 @@ int bta_co_rfc_data_incoming(void *user_data, BT_HDR *p_buf) { case SENT_NONE: case SENT_PARTIAL: list_append(slot->incoming_queue, p_buf); - btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_WR, slot->id); + btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_WR, + slot->id); break; case SENT_ALL: @@ -839,16 +897,17 @@ int bta_co_rfc_data_incoming(void *user_data, BT_HDR *p_buf) { return ret; // Return 0 to disable data flow. } -int bta_co_rfc_data_outgoing_size(void *user_data, int *size) { +int bta_co_rfc_data_outgoing_size(void* user_data, int* size) { uint32_t id = (uintptr_t)user_data; *size = 0; std::unique_lock lock(slot_lock); - rfc_slot_t *slot = find_rfc_slot_by_id(id); - if (!slot) - return false; + rfc_slot_t* slot = find_rfc_slot_by_id(id); + if (!slot) return false; if (ioctl(slot->fd, FIONREAD, size) != 0) { - LOG_ERROR(LOG_TAG, "%s unable to determine bytes remaining to be read on fd %d: %s", __func__, slot->fd, strerror(errno)); + LOG_ERROR(LOG_TAG, + "%s unable to determine bytes remaining to be read on fd %d: %s", + __func__, slot->fd, strerror(errno)); cleanup_rfc_slot(slot); return false; } @@ -856,18 +915,18 @@ int bta_co_rfc_data_outgoing_size(void *user_data, int *size) { return true; } -int bta_co_rfc_data_outgoing(void *user_data, uint8_t *buf, uint16_t size) { +int bta_co_rfc_data_outgoing(void* user_data, uint8_t* buf, uint16_t size) { std::unique_lock lock(slot_lock); uint32_t id = (uintptr_t)user_data; - rfc_slot_t *slot = find_rfc_slot_by_id(id); - if (!slot) - return false; + rfc_slot_t* slot = find_rfc_slot_by_id(id); + if (!slot) return false; ssize_t received; OSI_NO_INTR(received = recv(slot->fd, buf, size, 0)); - if(received != size) { - LOG_ERROR(LOG_TAG, "%s error receiving RFCOMM data from app: %s", __func__, strerror(errno)); + if (received != size) { + LOG_ERROR(LOG_TAG, "%s error receiving RFCOMM data from app: %s", __func__, + strerror(errno)); cleanup_rfc_slot(slot); return false; } diff --git a/btif/src/btif_sock_sco.cc b/btif/src/btif_sock_sco.cc index 9c99f6f17..80c0794d8 100644 --- a/btif/src/btif_sock_sco.cc +++ b/btif/src/btif_sock_sco.cc @@ -59,44 +59,45 @@ typedef struct { uint16_t sco_handle; - socket_t *socket; + socket_t* socket; bool connect_completed; } sco_socket_t; // TODO: verify packet types that are being sent OTA. static tBTM_ESCO_PARAMS sco_parameters = { - BTM_64KBITS_RATE, /* TX Bandwidth (64 kbits/sec) */ - BTM_64KBITS_RATE, /* RX Bandwidth (64 kbits/sec) */ - 0x000a, /* 10 ms (HS/HF can use EV3, 2-EV3, 3-EV3) */ - 0x0060, /* Inp Linear, Air CVSD, 2s Comp, 16bit */ - (BTM_SCO_LINK_ALL_PKT_MASK | - BTM_SCO_PKT_TYPES_MASK_NO_2_EV5 | + BTM_64KBITS_RATE, /* TX Bandwidth (64 kbits/sec) */ + BTM_64KBITS_RATE, /* RX Bandwidth (64 kbits/sec) */ + 0x000a, /* 10 ms (HS/HF can use EV3, 2-EV3, 3-EV3) */ + 0x0060, /* Inp Linear, Air CVSD, 2s Comp, 16bit */ + (BTM_SCO_LINK_ALL_PKT_MASK | BTM_SCO_PKT_TYPES_MASK_NO_2_EV5 | BTM_SCO_PKT_TYPES_MASK_NO_3_EV5), - BTM_ESCO_RETRANS_POWER /* Retransmission effort */ + BTM_ESCO_RETRANS_POWER /* Retransmission effort */ }; -static sco_socket_t *sco_socket_establish_locked(bool is_listening, const bt_bdaddr_t *bd_addr, int *sock_fd); -static sco_socket_t *sco_socket_new(void); -static void sco_socket_free_locked(sco_socket_t *socket); -static sco_socket_t *sco_socket_find_locked(uint16_t sco_handle); -static void connection_request_cb(tBTM_ESCO_EVT event, tBTM_ESCO_EVT_DATA *data); +static sco_socket_t* sco_socket_establish_locked(bool is_listening, + const bt_bdaddr_t* bd_addr, + int* sock_fd); +static sco_socket_t* sco_socket_new(void); +static void sco_socket_free_locked(sco_socket_t* socket); +static sco_socket_t* sco_socket_find_locked(uint16_t sco_handle); +static void connection_request_cb(tBTM_ESCO_EVT event, + tBTM_ESCO_EVT_DATA* data); static void connect_completed_cb(uint16_t sco_handle); static void disconnect_completed_cb(uint16_t sco_handle); -static void socket_read_ready_cb(socket_t *socket, void *context); +static void socket_read_ready_cb(socket_t* socket, void* context); // |sco_lock| protects all of the static variables below and // calls into the BTM layer. static std::mutex sco_lock; -static list_t *sco_sockets; // Owns a collection of sco_socket_t objects. -static sco_socket_t *listen_sco_socket; // Not owned, do not free. -static thread_t *thread; // Not owned, do not free. +static list_t* sco_sockets; // Owns a collection of sco_socket_t objects. +static sco_socket_t* listen_sco_socket; // Not owned, do not free. +static thread_t* thread; // Not owned, do not free. -bt_status_t btsock_sco_init(thread_t *thread_) { +bt_status_t btsock_sco_init(thread_t* thread_) { assert(thread_ != NULL); sco_sockets = list_new((list_free_cb)sco_socket_free_locked); - if (!sco_sockets) - return BT_STATUS_FAIL; + if (!sco_sockets) return BT_STATUS_FAIL; thread = thread_; BTM_SetEScoMode(BTM_LINK_TYPE_ESCO, &sco_parameters); @@ -110,14 +111,13 @@ bt_status_t btsock_sco_cleanup(void) { return BT_STATUS_SUCCESS; } -bt_status_t btsock_sco_listen(int *sock_fd, UNUSED_ATTR int flags) { +bt_status_t btsock_sco_listen(int* sock_fd, UNUSED_ATTR int flags) { assert(sock_fd != NULL); std::unique_lock lock(sco_lock); - sco_socket_t *sco_socket = sco_socket_establish_locked(true, NULL, sock_fd); - if (!sco_socket) - return BT_STATUS_FAIL; + sco_socket_t* sco_socket = sco_socket_establish_locked(true, NULL, sock_fd); + if (!sco_socket) return BT_STATUS_FAIL; BTM_RegForEScoEvts(sco_socket->sco_handle, connection_request_cb); listen_sco_socket = sco_socket; @@ -125,25 +125,30 @@ bt_status_t btsock_sco_listen(int *sock_fd, UNUSED_ATTR int flags) { return BT_STATUS_SUCCESS; } -bt_status_t btsock_sco_connect(const bt_bdaddr_t *bd_addr, int *sock_fd, UNUSED_ATTR int flags) { +bt_status_t btsock_sco_connect(const bt_bdaddr_t* bd_addr, int* sock_fd, + UNUSED_ATTR int flags) { assert(bd_addr != NULL); assert(sock_fd != NULL); std::unique_lock lock(sco_lock); - sco_socket_t *sco_socket = sco_socket_establish_locked(false, bd_addr, sock_fd); + sco_socket_t* sco_socket = + sco_socket_establish_locked(false, bd_addr, sock_fd); return (sco_socket != NULL) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } // Must be called with |lock| held. -static sco_socket_t *sco_socket_establish_locked(bool is_listening, const bt_bdaddr_t *bd_addr, int *sock_fd) { - int pair[2] = { INVALID_FD, INVALID_FD }; - sco_socket_t *sco_socket = NULL; - socket_t *socket = NULL; +static sco_socket_t* sco_socket_establish_locked(bool is_listening, + const bt_bdaddr_t* bd_addr, + int* sock_fd) { + int pair[2] = {INVALID_FD, INVALID_FD}; + sco_socket_t* sco_socket = NULL; + socket_t* socket = NULL; tBTM_STATUS status; if (socketpair(AF_LOCAL, SOCK_STREAM, 0, pair) == -1) { - LOG_ERROR(LOG_TAG, "%s unable to allocate socket pair: %s", __func__, strerror(errno)); + LOG_ERROR(LOG_TAG, "%s unable to allocate socket pair: %s", __func__, + strerror(errno)); goto error; } @@ -153,7 +158,9 @@ static sco_socket_t *sco_socket_establish_locked(bool is_listening, const bt_bda goto error; } - status = BTM_CreateSco((uint8_t *)bd_addr, !is_listening, sco_parameters.packet_types, &sco_socket->sco_handle, connect_completed_cb, disconnect_completed_cb); + status = BTM_CreateSco((uint8_t*)bd_addr, !is_listening, + sco_parameters.packet_types, &sco_socket->sco_handle, + connect_completed_cb, disconnect_completed_cb); if (status != BTM_CMD_STARTED) { LOG_ERROR(LOG_TAG, "%s unable to create SCO socket: %d", __func__, status); goto error; @@ -161,7 +168,8 @@ static sco_socket_t *sco_socket_establish_locked(bool is_listening, const bt_bda socket = socket_new_from_fd(pair[1]); if (!socket) { - LOG_ERROR(LOG_TAG, "%s unable to allocate socket from file descriptor %d.", __func__, pair[1]); + LOG_ERROR(LOG_TAG, "%s unable to allocate socket from file descriptor %d.", + __func__, pair[1]); goto error; } @@ -169,30 +177,29 @@ static sco_socket_t *sco_socket_establish_locked(bool is_listening, const bt_bda sco_socket->socket = socket; // Hang on to the other end. list_append(sco_sockets, sco_socket); - socket_register(socket, thread_get_reactor(thread), sco_socket, socket_read_ready_cb, NULL); + socket_register(socket, thread_get_reactor(thread), sco_socket, + socket_read_ready_cb, NULL); return sco_socket; error:; - if (pair[0] != INVALID_FD) - close(pair[0]); - if (pair[1] != INVALID_FD) - close(pair[1]); + if (pair[0] != INVALID_FD) close(pair[0]); + if (pair[1] != INVALID_FD) close(pair[1]); sco_socket_free_locked(sco_socket); return NULL; } -static sco_socket_t *sco_socket_new(void) { - sco_socket_t *sco_socket = (sco_socket_t *)osi_calloc(sizeof(sco_socket_t)); +static sco_socket_t* sco_socket_new(void) { + sco_socket_t* sco_socket = (sco_socket_t*)osi_calloc(sizeof(sco_socket_t)); sco_socket->sco_handle = BTM_INVALID_SCO_INDEX; return sco_socket; } -// Must be called with |lock| held except during teardown when we know the socket thread +// Must be called with |lock| held except during teardown when we know the +// socket thread // is no longer alive. -static void sco_socket_free_locked(sco_socket_t *sco_socket) { - if (!sco_socket) - return; +static void sco_socket_free_locked(sco_socket_t* sco_socket) { + if (!sco_socket) return; if (sco_socket->sco_handle != BTM_INVALID_SCO_INDEX) BTM_RemoveSco(sco_socket->sco_handle); @@ -201,38 +208,42 @@ static void sco_socket_free_locked(sco_socket_t *sco_socket) { } // Must be called with |lock| held. -static sco_socket_t *sco_socket_find_locked(uint16_t sco_handle) { - for (const list_node_t *node = list_begin(sco_sockets); node != list_end(sco_sockets); node = list_next(node)) { - sco_socket_t *sco_socket = (sco_socket_t *)list_node(node); - if (sco_socket->sco_handle == sco_handle) - return sco_socket; +static sco_socket_t* sco_socket_find_locked(uint16_t sco_handle) { + for (const list_node_t* node = list_begin(sco_sockets); + node != list_end(sco_sockets); node = list_next(node)) { + sco_socket_t* sco_socket = (sco_socket_t*)list_node(node); + if (sco_socket->sco_handle == sco_handle) return sco_socket; } return NULL; } -static void connection_request_cb(tBTM_ESCO_EVT event, tBTM_ESCO_EVT_DATA *data) { +static void connection_request_cb(tBTM_ESCO_EVT event, + tBTM_ESCO_EVT_DATA* data) { assert(data != NULL); // Don't care about change of link parameters, only connection requests. - if (event != BTM_ESCO_CONN_REQ_EVT) - return; + if (event != BTM_ESCO_CONN_REQ_EVT) return; std::unique_lock lock(sco_lock); - const tBTM_ESCO_CONN_REQ_EVT_DATA *conn_data = &data->conn_evt; - sco_socket_t *sco_socket = sco_socket_find_locked(conn_data->sco_inx); + const tBTM_ESCO_CONN_REQ_EVT_DATA* conn_data = &data->conn_evt; + sco_socket_t* sco_socket = sco_socket_find_locked(conn_data->sco_inx); int client_fd = INVALID_FD; uint16_t temp; - sco_socket_t *new_sco_socket; + sco_socket_t* new_sco_socket; if (!sco_socket) { - LOG_ERROR(LOG_TAG, "%s unable to find sco_socket for handle: %hu", __func__, conn_data->sco_inx); + LOG_ERROR(LOG_TAG, "%s unable to find sco_socket for handle: %hu", __func__, + conn_data->sco_inx); goto error; } if (sco_socket != listen_sco_socket) { - LOG_ERROR(LOG_TAG, "%s received connection request on non-listening socket handle: %hu", __func__, conn_data->sco_inx); + LOG_ERROR( + LOG_TAG, + "%s received connection request on non-listening socket handle: %hu", + __func__, conn_data->sco_inx); goto error; } @@ -253,8 +264,12 @@ static void connection_request_cb(tBTM_ESCO_EVT event, tBTM_ESCO_EVT_DATA *data) connect_signal.channel = 0; connect_signal.status = 0; - if (socket_write_and_transfer_fd(sco_socket->socket, &connect_signal, sizeof(connect_signal), client_fd) != sizeof(connect_signal)) { - LOG_ERROR(LOG_TAG, "%s unable to send new file descriptor to listening socket.", __func__); + if (socket_write_and_transfer_fd(sco_socket->socket, &connect_signal, + sizeof(connect_signal), + client_fd) != sizeof(connect_signal)) { + LOG_ERROR(LOG_TAG, + "%s unable to send new file descriptor to listening socket.", + __func__); goto error; } @@ -264,21 +279,22 @@ static void connection_request_cb(tBTM_ESCO_EVT event, tBTM_ESCO_EVT_DATA *data) return; error:; - if (client_fd != INVALID_FD) - close(client_fd); + if (client_fd != INVALID_FD) close(client_fd); BTM_EScoConnRsp(conn_data->sco_inx, HCI_ERR_HOST_REJECT_RESOURCES, NULL); } static void connect_completed_cb(uint16_t sco_handle) { std::unique_lock lock(sco_lock); - sco_socket_t *sco_socket = sco_socket_find_locked(sco_handle); + sco_socket_t* sco_socket = sco_socket_find_locked(sco_handle); if (!sco_socket) { - LOG_ERROR(LOG_TAG, "%s SCO socket not found on connect for handle: %hu", __func__, sco_handle); + LOG_ERROR(LOG_TAG, "%s SCO socket not found on connect for handle: %hu", + __func__, sco_handle); return; } - // If sco_socket->socket was closed, we should tear down because there is no app-level + // If sco_socket->socket was closed, we should tear down because there is no + // app-level // interest in the SCO socket. if (!sco_socket->socket) { BTM_RemoveSco(sco_socket->sco_handle); @@ -292,19 +308,20 @@ static void connect_completed_cb(uint16_t sco_handle) { static void disconnect_completed_cb(uint16_t sco_handle) { std::unique_lock lock(sco_lock); - sco_socket_t *sco_socket = sco_socket_find_locked(sco_handle); + sco_socket_t* sco_socket = sco_socket_find_locked(sco_handle); if (!sco_socket) { - LOG_ERROR(LOG_TAG, "%s SCO socket not found on disconnect for handle: %hu", __func__, sco_handle); + LOG_ERROR(LOG_TAG, "%s SCO socket not found on disconnect for handle: %hu", + __func__, sco_handle); return; } list_remove(sco_sockets, sco_socket); } -static void socket_read_ready_cb(UNUSED_ATTR socket_t *socket, void *context) { +static void socket_read_ready_cb(UNUSED_ATTR socket_t* socket, void* context) { std::unique_lock lock(sco_lock); - sco_socket_t *sco_socket = (sco_socket_t *)context; + sco_socket_t* sco_socket = (sco_socket_t*)context; socket_free(sco_socket->socket); sco_socket->socket = NULL; @@ -317,7 +334,6 @@ static void socket_read_ready_cb(UNUSED_ATTR socket_t *socket, void *context) { if (sco_socket->connect_completed || sco_socket == listen_sco_socket) { if (BTM_RemoveSco(sco_socket->sco_handle) == BTM_SUCCESS) list_remove(sco_sockets, sco_socket); - if (sco_socket == listen_sco_socket) - listen_sco_socket = NULL; + if (sco_socket == listen_sco_socket) listen_sco_socket = NULL; } } diff --git a/btif/src/btif_sock_sdp.cc b/btif/src/btif_sock_sdp.cc index 1e637021b..21b2c0b3f 100644 --- a/btif/src/btif_sock_sdp.cc +++ b/btif/src/btif_sock_sdp.cc @@ -29,6 +29,7 @@ #include "../bta/pb/bta_pbs_int.h" #include "../include/bta_op_api.h" +#include "bt_common.h" #include "bt_target.h" #include "bta_api.h" #include "bta_jv_api.h" @@ -38,7 +39,6 @@ #include "btm_api.h" #include "btm_int.h" #include "btu.h" -#include "bt_common.h" #include "hcimsgs.h" #include "sdp_api.h" #include "utl.h" @@ -58,37 +58,27 @@ #define BTA_PBS_USERID_REQ FALSE static const tBTA_PBS_CFG bta_pbs_cfg = { - BTA_PBS_REALM_CHARSET, // realm_charset: Server only - BTA_PBS_USERID_REQ, // userid_req: Server only - (BTA_PBS_SUPF_DOWNLOAD | BTA_PBS_SURF_BROWSE), // supported_features - BTA_PBS_REPOSIT_LOCAL, // supported_repositories + BTA_PBS_REALM_CHARSET, // realm_charset: Server only + BTA_PBS_USERID_REQ, // userid_req: Server only + (BTA_PBS_SUPF_DOWNLOAD | BTA_PBS_SURF_BROWSE), // supported_features + BTA_PBS_REPOSIT_LOCAL, // supported_repositories }; // object format lookup table #define OBEX_PUSH_NUM_FORMATS 7 static const tBTA_OP_FMT bta_ops_obj_fmt[OBEX_PUSH_NUM_FORMATS] = { - BTA_OP_VCARD21_FMT, - BTA_OP_VCARD30_FMT, - BTA_OP_VCAL_FMT, - BTA_OP_ICAL_FMT, - BTA_OP_VNOTE_FMT, - BTA_OP_VMSG_FMT, - BTA_OP_OTHER_FMT -}; + BTA_OP_VCARD21_FMT, BTA_OP_VCARD30_FMT, BTA_OP_VCAL_FMT, BTA_OP_ICAL_FMT, + BTA_OP_VNOTE_FMT, BTA_OP_VMSG_FMT, BTA_OP_OTHER_FMT}; // TODO(jtgans): Figure out if we actually need this define. This is ifndef // defined in bt_target.h, but nowhere else, so right now, unless something // overrides this before bt_target.h sets it, it will always be bt_target.h's // version. #ifndef BTUI_OPS_FORMATS -#define BTUI_OPS_FORMATS (BTA_OP_VCARD21_MASK \ - | BTA_OP_VCARD30_MASK \ - | BTA_OP_VCAL_MASK \ - | BTA_OP_ICAL_MASK \ - | BTA_OP_VNOTE_MASK \ - | BTA_OP_VMSG_MASK \ - | BTA_OP_ANY_MASK) +#define BTUI_OPS_FORMATS \ + (BTA_OP_VCARD21_MASK | BTA_OP_VCARD30_MASK | BTA_OP_VCAL_MASK | \ + BTA_OP_ICAL_MASK | BTA_OP_VNOTE_MASK | BTA_OP_VMSG_MASK | BTA_OP_ANY_MASK) #endif #define RESERVED_SCN_PBS 19 @@ -104,7 +94,7 @@ static const tBTA_OP_FMT bta_ops_obj_fmt[OBEX_PUSH_NUM_FORMATS] = { // at the end of the protocol list. // // Returns true if successful, otherwise false. -static bool create_base_record(const uint32_t sdp_handle, const char *name, +static bool create_base_record(const uint32_t sdp_handle, const char* name, const uint16_t channel, const bool with_obex) { APPL_TRACE_DEBUG("create_base_record: scn: %d, name: %s, with_obex: %d", channel, name, with_obex); @@ -129,16 +119,15 @@ static bool create_base_record(const uint32_t sdp_handle, const char *name, // Mark the service as browseable. uint16_t list = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP; - const char *stage = "protocol_list"; + const char* stage = "protocol_list"; if (!SDP_AddProtocolList(sdp_handle, num_proto_elements, proto_list)) goto error; // Add the name to the SDP record. if (name[0] != '\0') { stage = "service_name"; - if (!SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_NAME, - TEXT_STR_DESC_TYPE, (uint32_t)(strlen(name) + 1), - (uint8_t *)name)) + if (!SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE, + (uint32_t)(strlen(name) + 1), (uint8_t*)name)) goto error; } @@ -146,29 +135,33 @@ static bool create_base_record(const uint32_t sdp_handle, const char *name, if (!SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &list)) goto error; - APPL_TRACE_DEBUG("create_base_record: successfully created base service " - "record, handle: 0x%08x, scn: %d, name: %s, with_obex: %d", - sdp_handle, channel, name, with_obex); + APPL_TRACE_DEBUG( + "create_base_record: successfully created base service " + "record, handle: 0x%08x, scn: %d, name: %s, with_obex: %d", + sdp_handle, channel, name, with_obex); return true; error: - APPL_TRACE_ERROR("create_base_record: failed to create base service " - "record, stage: %s, scn: %d, name: %s, with_obex: %d", - stage, channel, name, with_obex); + APPL_TRACE_ERROR( + "create_base_record: failed to create base service " + "record, stage: %s, scn: %d, name: %s, with_obex: %d", + stage, channel, name, with_obex); return false; } // Registers a service with the given |name|, |uuid|, and |channel| in the SDP // database as a generic L2CAP RFCOMM protocol, storing its |uuid| as a service // class sequence. -static int add_sdp_by_uuid(const char *name, const uint8_t *uuid, +static int add_sdp_by_uuid(const char* name, const uint8_t* uuid, const uint16_t channel) { APPL_TRACE_DEBUG("add_sdp_by_uuid: scn: %d, service_name: %s", channel, name); uint32_t handle = SDP_CreateRecord(); if (handle == 0) { - APPL_TRACE_ERROR("add_sdp_by_uuid: failed to create sdp record, " - "scn: %d, service_name: %s", channel, name); + APPL_TRACE_ERROR( + "add_sdp_by_uuid: failed to create sdp record, " + "scn: %d, service_name: %s", + channel, name); return 0; } @@ -179,66 +172,67 @@ static int add_sdp_by_uuid(const char *name, const uint8_t *uuid, uint8_t type_buf[48]; // Store the address of type buf in a pointer on the stack, so we can pass // a double pointer to SDP_AddSequence - uint8_t *type_buf_ptr = type_buf; - uint8_t *tmp = type_buf; + uint8_t* type_buf_ptr = type_buf; + uint8_t* tmp = type_buf; // Create the base SDP record. - const char *stage = "create_base_record"; + const char* stage = "create_base_record"; if (!create_base_record(handle, name, channel, false /* with_obex */)) goto error; - // Do the conversion to big-endian -- tmp is only used to iterate through the // UUID array in the macro and serves no other purpose as the conversion // macros are not hygenic. - { - ARRAY_TO_BE_STREAM(tmp, uuid, UUID_MAX_LENGTH); - } + { ARRAY_TO_BE_STREAM(tmp, uuid, UUID_MAX_LENGTH); } stage = "service_class_sequence"; - if (!SDP_AddSequence(handle, (uint16_t)ATTR_ID_SERVICE_CLASS_ID_LIST, - 1, &type, &type_len, &type_buf_ptr)) + if (!SDP_AddSequence(handle, (uint16_t)ATTR_ID_SERVICE_CLASS_ID_LIST, 1, + &type, &type_len, &type_buf_ptr)) goto error; - APPL_TRACE_DEBUG("add_sdp_by_uuid: service registered successfully, " - "service_name: %s, handle: 0x%08x", name, handle); + APPL_TRACE_DEBUG( + "add_sdp_by_uuid: service registered successfully, " + "service_name: %s, handle: 0x%08x", + name, handle); return handle; error: SDP_DeleteRecord(handle); - APPL_TRACE_ERROR("add_sdp_by_uuid: failed to register service " - "stage: %s, service_name: %s", stage, name); + APPL_TRACE_ERROR( + "add_sdp_by_uuid: failed to register service " + "stage: %s, service_name: %s", + stage, name); return 0; } // Registers a service with the given |name| and |channel| in the SDP // database as a PBAP protocol. -static int add_pbap_sdp(const char *name, const int channel) { +static int add_pbap_sdp(const char* name, const int channel) { APPL_TRACE_DEBUG("add_pbap_sdp: scn %d, service_name %s", channel, name); uint32_t handle = SDP_CreateRecord(); if (handle == 0) { - APPL_TRACE_ERROR("add_pbap_sdp: failed to create sdp record, " - "service_name: %s", name); + APPL_TRACE_ERROR( + "add_pbap_sdp: failed to create sdp record, " + "service_name: %s", + name); return 0; } uint16_t service = UUID_SERVCLASS_PBAP_PSE; // Create the base SDP record. - const char *stage = "create_base_record"; + const char* stage = "create_base_record"; if (!create_base_record(handle, name, channel, true /* with_obex */)) goto error; // Add service class stage = "service_class"; - if (!SDP_AddServiceClassIdList(handle, 1, &service)) - goto error; + if (!SDP_AddServiceClassIdList(handle, 1, &service)) goto error; // Add in the phone access descriptor stage = "profile_descriptor_list"; - if (!SDP_AddProfileDescriptorList(handle, - UUID_SERVCLASS_PHONE_ACCESS, + if (!SDP_AddProfileDescriptorList(handle, UUID_SERVCLASS_PHONE_ACCESS, BTA_PBS_DEFAULT_VERSION)) goto error; @@ -250,47 +244,52 @@ static int add_pbap_sdp(const char *name, const int channel) { // Notify the system that we've got a new service class UUID. bta_sys_add_uuid(UUID_SERVCLASS_PBAP_PSE); - APPL_TRACE_DEBUG("add_pbap_sdp: service registered successfully, " - "service_name: %s, handle: 0x%08x", name, handle); + APPL_TRACE_DEBUG( + "add_pbap_sdp: service registered successfully, " + "service_name: %s, handle: 0x%08x", + name, handle); return handle; error: SDP_DeleteRecord(handle); - APPL_TRACE_ERROR("add_pbap_sdp: failed to register PBAP service, stage: %s, " - "service_name: %s", stage, name); + APPL_TRACE_ERROR( + "add_pbap_sdp: failed to register PBAP service, stage: %s, " + "service_name: %s", + stage, name); return 0; } // Registers a service with the given |name| and |channel| as an OBEX Push // protocol. -static int add_ops_sdp(const char *name, const int channel) { +static int add_ops_sdp(const char* name, const int channel) { APPL_TRACE_DEBUG("add_ops_sdp: scn %d, service_name %s", channel, name); uint32_t handle = SDP_CreateRecord(); if (handle == 0) { - APPL_TRACE_ERROR("add_ops_sdp: failed to create sdp record, " - "service_name: %s", name); + APPL_TRACE_ERROR( + "add_ops_sdp: failed to create sdp record, " + "service_name: %s", + name); return 0; } // Add sequence for supported types. uint8_t desc_type[OBEX_PUSH_NUM_FORMATS]; uint8_t type_len[OBEX_PUSH_NUM_FORMATS]; - uint8_t *type_value[OBEX_PUSH_NUM_FORMATS]; + uint8_t* type_value[OBEX_PUSH_NUM_FORMATS]; uint8_t j = 0; uint16_t service = UUID_SERVCLASS_OBEX_OBJECT_PUSH; tBTA_UTL_COD cod; // Create the base SDP record. - const char *stage = "create_base_record"; + const char* stage = "create_base_record"; if (!create_base_record(handle, name, channel, true /* with_obex */)) goto error; // Add service class. stage = "service_class"; - if (!SDP_AddServiceClassIdList(handle, 1, &service)) - goto error; + if (!SDP_AddServiceClassIdList(handle, 1, &service)) goto error; // Add the OBEX push profile descriptor. stage = "profile_descriptor_list"; @@ -298,7 +297,6 @@ static int add_ops_sdp(const char *name, const int channel) { 0x0100)) goto error; - for (int i = 0; i < OBEX_PUSH_NUM_FORMATS; i++) { if ((BTUI_OPS_FORMATS >> i) & 1) { type_value[j] = (uint8_t*)(&bta_ops_obj_fmt[i]); @@ -308,62 +306,70 @@ static int add_ops_sdp(const char *name, const int channel) { } stage = "supported_types"; - if (!SDP_AddSequence(handle, (uint16_t)ATTR_ID_SUPPORTED_FORMATS_LIST, - j, desc_type, type_len, type_value)) + if (!SDP_AddSequence(handle, (uint16_t)ATTR_ID_SUPPORTED_FORMATS_LIST, j, + desc_type, type_len, type_value)) goto error; // Set class of device. cod.service = BTM_COD_SERVICE_OBJ_TRANSFER; stage = "class_of_device"; - if (!utl_set_device_class(&cod, BTA_UTL_SET_COD_SERVICE_CLASS)) - goto error; + if (!utl_set_device_class(&cod, BTA_UTL_SET_COD_SERVICE_CLASS)) goto error; // Notify the system that we've got a new service class UUID. bta_sys_add_uuid(UUID_SERVCLASS_OBEX_OBJECT_PUSH); - APPL_TRACE_DEBUG("ad_maps_sdp: service registered successfully, " - "service_name: %s, handle 0x%08x)", name, handle); + APPL_TRACE_DEBUG( + "ad_maps_sdp: service registered successfully, " + "service_name: %s, handle 0x%08x)", + name, handle); return handle; error: SDP_DeleteRecord(handle); - APPL_TRACE_ERROR("add_ops_sdp: failed to register OPS service, " - "stage: %s, service_name: %s", stage, name); + APPL_TRACE_ERROR( + "add_ops_sdp: failed to register OPS service, " + "stage: %s, service_name: %s", + stage, name); return 0; } // Registers a service with the given |name| and |channel| as a serial port // profile protocol. -static int add_spp_sdp(const char *name, const int channel) { +static int add_spp_sdp(const char* name, const int channel) { APPL_TRACE_DEBUG("add_spp_sdp: scn %d, service_name %s", channel, name); int handle = SDP_CreateRecord(); if (handle == 0) { - APPL_TRACE_ERROR("add_spp_sdp: failed to create sdp record, " - "service_name: %s", name); + APPL_TRACE_ERROR( + "add_spp_sdp: failed to create sdp record, " + "service_name: %s", + name); return 0; } // Create the base SDP record. - const char *stage = "create_base_record"; + const char* stage = "create_base_record"; uint16_t service = UUID_SERVCLASS_SERIAL_PORT; if (!create_base_record(handle, name, channel, false /* with_obex */)) goto error; stage = "service_class"; - if (!SDP_AddServiceClassIdList(handle, 1, &service)) - goto error; + if (!SDP_AddServiceClassIdList(handle, 1, &service)) goto error; - APPL_TRACE_DEBUG("add_spp_sdp: service registered successfully, " - "service_name: %s, handle 0x%08x)", name, handle); + APPL_TRACE_DEBUG( + "add_spp_sdp: service registered successfully, " + "service_name: %s, handle 0x%08x)", + name, handle); return handle; error: SDP_DeleteRecord(handle); - APPL_TRACE_ERROR("add_spp_sdp: failed to register SPP service, " - "stage: %s, service_name: %s", stage, name); + APPL_TRACE_ERROR( + "add_spp_sdp: failed to register SPP service, " + "stage: %s, service_name: %s", + stage, name); return 0; } @@ -371,7 +377,7 @@ error: // |channel|. This function attempts to identify the type of the service based // upon its |uuid|, and will override the |channel| with a reserved channel // number if the |uuid| matches one of the preregistered bluez SDP records. -static int add_rfc_sdp_by_uuid(const char *name, const uint8_t *uuid, +static int add_rfc_sdp_by_uuid(const char* name, const uint8_t* uuid, const int channel) { APPL_TRACE_DEBUG("add_rfc_sdp_by_uuid: service_name: %s, channel: %d", name, channel); @@ -398,12 +404,12 @@ static int add_rfc_sdp_by_uuid(const char *name, const uint8_t *uuid, if (UUID_MATCHES(UUID_OBEX_OBJECT_PUSH, uuid)) { handle = add_ops_sdp(name, final_channel); - } else if (UUID_MATCHES(UUID_PBAP_PSE,uuid)) { + } else if (UUID_MATCHES(UUID_PBAP_PSE, uuid)) { // PBAP Server is always channel 19 handle = add_pbap_sdp(name, final_channel); } else if (UUID_MATCHES(UUID_SPP, uuid)) { handle = add_spp_sdp(name, final_channel); - } else if (UUID_MATCHES(UUID_MAP_MAS,uuid)) { + } else if (UUID_MATCHES(UUID_MAP_MAS, uuid)) { // Record created by new SDP create record interface handle = 0xff; } else { @@ -414,7 +420,7 @@ static int add_rfc_sdp_by_uuid(const char *name, const uint8_t *uuid, } bool is_reserved_rfc_channel(const int channel) { - switch(channel) { + switch (channel) { case RESERVED_SCN_PBS: case RESERVED_SCN_OPS: return true; @@ -423,7 +429,7 @@ bool is_reserved_rfc_channel(const int channel) { return false; } -int get_reserved_rfc_channel(const uint8_t *uuid) { +int get_reserved_rfc_channel(const uint8_t* uuid) { if (UUID_MATCHES(UUID_PBAP_PSE, uuid)) { return RESERVED_SCN_PBS; } else if (UUID_MATCHES(UUID_OBEX_OBJECT_PUSH, uuid)) { @@ -436,10 +442,10 @@ int get_reserved_rfc_channel(const uint8_t *uuid) { // Adds an SDP record to the SDP database using the given |name|, |uuid|, and // |channel|. Note that if the |uuid| is empty, the |uuid| will be set based // upon the |channel| passed in. -int add_rfc_sdp_rec(const char *name, const uint8_t *uuid, const int channel) { +int add_rfc_sdp_rec(const char* name, const uint8_t* uuid, const int channel) { if (is_uuid_empty(uuid)) { - switch(channel) { - case RESERVED_SCN_PBS: // PBAP Reserved port + switch (channel) { + case RESERVED_SCN_PBS: // PBAP Reserved port uuid = UUID_PBAP_PSE; break; @@ -460,6 +466,5 @@ int add_rfc_sdp_rec(const char *name, const uint8_t *uuid, const int channel) { void del_rfc_sdp_rec(int handle) { APPL_TRACE_DEBUG("del_rfc_sdp_rec: handle:0x%x", handle); - if ((handle != -1) && (handle != 0)) - BTA_JvDeleteRecord(handle); + if ((handle != -1) && (handle != 0)) BTA_JvDeleteRecord(handle); } diff --git a/btif/src/btif_sock_thread.cc b/btif/src/btif_sock_thread.cc index e4dfb63df..3bc7f2480 100644 --- a/btif/src/btif_sock_thread.cc +++ b/btif/src/btif_sock_thread.cc @@ -55,546 +55,491 @@ #include "btif_util.h" #include "osi/include/socket_utils/sockets.h" -#define asrt(s) if(!(s)) APPL_TRACE_ERROR("## %s assert %s failed at line:%d ##",__func__, #s, __LINE__) -#define print_events(events) do { \ - APPL_TRACE_DEBUG("print poll event:%x", events); \ - if ((events) & POLLIN) APPL_TRACE_DEBUG( " POLLIN "); \ - if ((events) & POLLPRI) APPL_TRACE_DEBUG( " POLLPRI "); \ - if ((events) & POLLOUT) APPL_TRACE_DEBUG( " POLLOUT "); \ - if ((events) & POLLERR) APPL_TRACE_DEBUG( " POLLERR "); \ - if ((events) & POLLHUP) APPL_TRACE_DEBUG( " POLLHUP "); \ - if ((events) & POLLNVAL) APPL_TRACE_DEBUG(" POLLNVAL "); \ - if ((events) & POLLRDHUP) APPL_TRACE_DEBUG(" POLLRDHUP"); \ - } while(0) +#define asrt(s) \ + if (!(s)) \ + APPL_TRACE_ERROR("## %s assert %s failed at line:%d ##", __func__, #s, \ + __LINE__) +#define print_events(events) \ + do { \ + APPL_TRACE_DEBUG("print poll event:%x", events); \ + if ((events)&POLLIN) APPL_TRACE_DEBUG(" POLLIN "); \ + if ((events)&POLLPRI) APPL_TRACE_DEBUG(" POLLPRI "); \ + if ((events)&POLLOUT) APPL_TRACE_DEBUG(" POLLOUT "); \ + if ((events)&POLLERR) APPL_TRACE_DEBUG(" POLLERR "); \ + if ((events)&POLLHUP) APPL_TRACE_DEBUG(" POLLHUP "); \ + if ((events)&POLLNVAL) APPL_TRACE_DEBUG(" POLLNVAL "); \ + if ((events)&POLLRDHUP) APPL_TRACE_DEBUG(" POLLRDHUP"); \ + } while (0) #define MAX_THREAD 8 #define MAX_POLL 64 #define POLL_EXCEPTION_EVENTS (POLLHUP | POLLRDHUP | POLLERR | POLLNVAL) -#define IS_EXCEPTION(e) ((e) & POLL_EXCEPTION_EVENTS) -#define IS_READ(e) ((e) & POLLIN) -#define IS_WRITE(e) ((e) & POLLOUT) +#define IS_EXCEPTION(e) ((e)&POLL_EXCEPTION_EVENTS) +#define IS_READ(e) ((e)&POLLIN) +#define IS_WRITE(e) ((e)&POLLOUT) /*cmd executes in socket poll thread */ -#define CMD_WAKEUP 1 -#define CMD_EXIT 2 -#define CMD_ADD_FD 3 -#define CMD_REMOVE_FD 4 +#define CMD_WAKEUP 1 +#define CMD_EXIT 2 +#define CMD_ADD_FD 3 +#define CMD_REMOVE_FD 4 #define CMD_USER_PRIVATE 5 typedef struct { - struct pollfd pfd; - uint32_t user_id; - int type; - int flags; + struct pollfd pfd; + uint32_t user_id; + int type; + int flags; } poll_slot_t; typedef struct { - int cmd_fdr, cmd_fdw; - int poll_count; - poll_slot_t ps[MAX_POLL]; - int psi[MAX_POLL]; //index of poll slot - volatile pthread_t thread_id; - btsock_signaled_cb callback; - btsock_cmd_cb cmd_callback; - int used; + int cmd_fdr, cmd_fdw; + int poll_count; + poll_slot_t ps[MAX_POLL]; + int psi[MAX_POLL]; // index of poll slot + volatile pthread_t thread_id; + btsock_signaled_cb callback; + btsock_cmd_cb cmd_callback; + int used; } thread_slot_t; static thread_slot_t ts[MAX_THREAD]; -static void *sock_poll_thread(void *arg); +static void* sock_poll_thread(void* arg); static inline void close_cmd_fd(int h); -static inline void add_poll(int h, int fd, int type, int flags, uint32_t user_id); +static inline void add_poll(int h, int fd, int type, int flags, + uint32_t user_id); static std::recursive_mutex thread_slot_lock; -static inline int create_thread(void *(*start_routine)(void *), void * arg, - pthread_t * thread_id) -{ - pthread_attr_t thread_attr; - pthread_attr_init(&thread_attr); - pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE); - int policy; - int min_pri=0; - int ret = -1; - struct sched_param param; - - if ((ret = pthread_create(thread_id, &thread_attr, start_routine, arg))!=0 ) - { - APPL_TRACE_ERROR("pthread_create : %s", strerror(errno)); - return ret; - } - /* We need to lower the priority of this thread to ensure the stack gets - * priority over transfer to a socket */ - pthread_getschedparam(*thread_id, &policy, ¶m); - min_pri = sched_get_priority_min(policy); - if (param.sched_priority > min_pri) { - param.sched_priority -= 1; - } - pthread_setschedparam(*thread_id, policy, ¶m); +static inline int create_thread(void* (*start_routine)(void*), void* arg, + pthread_t* thread_id) { + pthread_attr_t thread_attr; + pthread_attr_init(&thread_attr); + pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE); + int policy; + int min_pri = 0; + int ret = -1; + struct sched_param param; + + if ((ret = pthread_create(thread_id, &thread_attr, start_routine, arg)) != + 0) { + APPL_TRACE_ERROR("pthread_create : %s", strerror(errno)); return ret; + } + /* We need to lower the priority of this thread to ensure the stack gets + * priority over transfer to a socket */ + pthread_getschedparam(*thread_id, &policy, ¶m); + min_pri = sched_get_priority_min(policy); + if (param.sched_priority > min_pri) { + param.sched_priority -= 1; + } + pthread_setschedparam(*thread_id, policy, ¶m); + return ret; } static void init_poll(int cmd_fd); -static int alloc_thread_slot() -{ - std::unique_lock lock(thread_slot_lock); - int i; - // reversed order to save guard uninitialized access to 0 index - for(i = MAX_THREAD - 1; i >=0; i--) - { - APPL_TRACE_DEBUG("ts[%d].used:%d", i, ts[i].used); - if(!ts[i].used) - { - ts[i].used = 1; - return i; - } - } - APPL_TRACE_ERROR("execeeded max thread count"); - return -1; +static int alloc_thread_slot() { + std::unique_lock lock(thread_slot_lock); + int i; + // reversed order to save guard uninitialized access to 0 index + for (i = MAX_THREAD - 1; i >= 0; i--) { + APPL_TRACE_DEBUG("ts[%d].used:%d", i, ts[i].used); + if (!ts[i].used) { + ts[i].used = 1; + return i; + } + } + APPL_TRACE_ERROR("execeeded max thread count"); + return -1; } -static void free_thread_slot(int h) -{ - if(0 <= h && h < MAX_THREAD) - { - close_cmd_fd(h); - ts[h].used = 0; - } - else APPL_TRACE_ERROR("invalid thread handle:%d", h); +static void free_thread_slot(int h) { + if (0 <= h && h < MAX_THREAD) { + close_cmd_fd(h); + ts[h].used = 0; + } else + APPL_TRACE_ERROR("invalid thread handle:%d", h); } -int btsock_thread_init() -{ - static int initialized; - APPL_TRACE_DEBUG("in initialized:%d", initialized); - if(!initialized) - { - initialized = 1; - int h; - for(h = 0; h < MAX_THREAD; h++) - { - ts[h].cmd_fdr = ts[h].cmd_fdw = -1; - ts[h].used = 0; - ts[h].thread_id = -1; - ts[h].poll_count = 0; - ts[h].callback = NULL; - ts[h].cmd_callback = NULL; - } - } - return true; +int btsock_thread_init() { + static int initialized; + APPL_TRACE_DEBUG("in initialized:%d", initialized); + if (!initialized) { + initialized = 1; + int h; + for (h = 0; h < MAX_THREAD; h++) { + ts[h].cmd_fdr = ts[h].cmd_fdw = -1; + ts[h].used = 0; + ts[h].thread_id = -1; + ts[h].poll_count = 0; + ts[h].callback = NULL; + ts[h].cmd_callback = NULL; + } + } + return true; } -int btsock_thread_create(btsock_signaled_cb callback, btsock_cmd_cb cmd_callback) -{ - asrt(callback || cmd_callback); - int h = alloc_thread_slot(); - APPL_TRACE_DEBUG("alloc_thread_slot ret:%d", h); - if(h >= 0) - { - init_poll(h); - pthread_t thread; - int status = create_thread(sock_poll_thread, (void*)(uintptr_t)h, &thread); - if (status) - { - APPL_TRACE_ERROR("create_thread failed: %s", strerror(status)); - free_thread_slot(h); - return -1; - } - - ts[h].thread_id = thread; - APPL_TRACE_DEBUG("h:%d, thread id:%d", h, ts[h].thread_id); - ts[h].callback = callback; - ts[h].cmd_callback = cmd_callback; - } - return h; +int btsock_thread_create(btsock_signaled_cb callback, + btsock_cmd_cb cmd_callback) { + asrt(callback || cmd_callback); + int h = alloc_thread_slot(); + APPL_TRACE_DEBUG("alloc_thread_slot ret:%d", h); + if (h >= 0) { + init_poll(h); + pthread_t thread; + int status = create_thread(sock_poll_thread, (void*)(uintptr_t)h, &thread); + if (status) { + APPL_TRACE_ERROR("create_thread failed: %s", strerror(status)); + free_thread_slot(h); + return -1; + } + + ts[h].thread_id = thread; + APPL_TRACE_DEBUG("h:%d, thread id:%d", h, ts[h].thread_id); + ts[h].callback = callback; + ts[h].cmd_callback = cmd_callback; + } + return h; } /* create dummy socket pair used to wake up select loop */ -static inline void init_cmd_fd(int h) -{ - asrt(ts[h].cmd_fdr == -1 && ts[h].cmd_fdw == -1); - if(socketpair(AF_UNIX, SOCK_STREAM, 0, &ts[h].cmd_fdr) < 0) - { - APPL_TRACE_ERROR("socketpair failed: %s", strerror(errno)); - return; - } - APPL_TRACE_DEBUG("h:%d, cmd_fdr:%d, cmd_fdw:%d", h, ts[h].cmd_fdr, ts[h].cmd_fdw); - //add the cmd fd for read & write - add_poll(h, ts[h].cmd_fdr, 0, SOCK_THREAD_FD_RD, 0); +static inline void init_cmd_fd(int h) { + asrt(ts[h].cmd_fdr == -1 && ts[h].cmd_fdw == -1); + if (socketpair(AF_UNIX, SOCK_STREAM, 0, &ts[h].cmd_fdr) < 0) { + APPL_TRACE_ERROR("socketpair failed: %s", strerror(errno)); + return; + } + APPL_TRACE_DEBUG("h:%d, cmd_fdr:%d, cmd_fdw:%d", h, ts[h].cmd_fdr, + ts[h].cmd_fdw); + // add the cmd fd for read & write + add_poll(h, ts[h].cmd_fdr, 0, SOCK_THREAD_FD_RD, 0); } -static inline void close_cmd_fd(int h) -{ - if(ts[h].cmd_fdr != -1) - { - close(ts[h].cmd_fdr); - ts[h].cmd_fdr = -1; - } - if(ts[h].cmd_fdw != -1) - { - close(ts[h].cmd_fdw); - ts[h].cmd_fdw = -1; - } +static inline void close_cmd_fd(int h) { + if (ts[h].cmd_fdr != -1) { + close(ts[h].cmd_fdr); + ts[h].cmd_fdr = -1; + } + if (ts[h].cmd_fdw != -1) { + close(ts[h].cmd_fdw); + ts[h].cmd_fdw = -1; + } } -typedef struct -{ - int id; - int fd; - int type; - int flags; - uint32_t user_id; +typedef struct { + int id; + int fd; + int type; + int flags; + uint32_t user_id; } sock_cmd_t; -int btsock_thread_add_fd(int h, int fd, int type, int flags, uint32_t user_id) -{ - if(h < 0 || h >= MAX_THREAD) - { - APPL_TRACE_ERROR("invalid bt thread handle:%d", h); - return false; - } - if(ts[h].cmd_fdw == -1) - { - APPL_TRACE_ERROR("cmd socket is not created. socket thread may not initialized"); - return false; - } - if(flags & SOCK_THREAD_ADD_FD_SYNC) - { - //must executed in socket poll thread - if(ts[h].thread_id == pthread_self()) - { - //cleanup one-time flags - flags &= ~SOCK_THREAD_ADD_FD_SYNC; - add_poll(h, fd, type, flags, user_id); - return true; - } - APPL_TRACE_DEBUG("THREAD_ADD_FD_SYNC is not called in poll thread, fallback to async"); - } - sock_cmd_t cmd = {CMD_ADD_FD, fd, type, flags, user_id}; - APPL_TRACE_DEBUG("adding fd:%d, flags:0x%x", fd, flags); - - ssize_t ret; - OSI_NO_INTR(ret = send(ts[h].cmd_fdw, &cmd, sizeof(cmd), 0)); - - return ret == sizeof(cmd); +int btsock_thread_add_fd(int h, int fd, int type, int flags, uint32_t user_id) { + if (h < 0 || h >= MAX_THREAD) { + APPL_TRACE_ERROR("invalid bt thread handle:%d", h); + return false; + } + if (ts[h].cmd_fdw == -1) { + APPL_TRACE_ERROR( + "cmd socket is not created. socket thread may not initialized"); + return false; + } + if (flags & SOCK_THREAD_ADD_FD_SYNC) { + // must executed in socket poll thread + if (ts[h].thread_id == pthread_self()) { + // cleanup one-time flags + flags &= ~SOCK_THREAD_ADD_FD_SYNC; + add_poll(h, fd, type, flags, user_id); + return true; + } + APPL_TRACE_DEBUG( + "THREAD_ADD_FD_SYNC is not called in poll thread, fallback to async"); + } + sock_cmd_t cmd = {CMD_ADD_FD, fd, type, flags, user_id}; + APPL_TRACE_DEBUG("adding fd:%d, flags:0x%x", fd, flags); + + ssize_t ret; + OSI_NO_INTR(ret = send(ts[h].cmd_fdw, &cmd, sizeof(cmd), 0)); + + return ret == sizeof(cmd); } -bool btsock_thread_remove_fd_and_close(int thread_handle, int fd) -{ - if (thread_handle < 0 || thread_handle >= MAX_THREAD) - { - APPL_TRACE_ERROR("%s invalid thread handle: %d", __func__, thread_handle); - return false; - } - if (fd == -1) - { - APPL_TRACE_ERROR("%s invalid file descriptor.", __func__); - return false; - } +bool btsock_thread_remove_fd_and_close(int thread_handle, int fd) { + if (thread_handle < 0 || thread_handle >= MAX_THREAD) { + APPL_TRACE_ERROR("%s invalid thread handle: %d", __func__, thread_handle); + return false; + } + if (fd == -1) { + APPL_TRACE_ERROR("%s invalid file descriptor.", __func__); + return false; + } - sock_cmd_t cmd = {CMD_REMOVE_FD, fd, 0, 0, 0}; + sock_cmd_t cmd = {CMD_REMOVE_FD, fd, 0, 0, 0}; - ssize_t ret; - OSI_NO_INTR(ret = send(ts[thread_handle].cmd_fdw, &cmd, sizeof(cmd), 0)); + ssize_t ret; + OSI_NO_INTR(ret = send(ts[thread_handle].cmd_fdw, &cmd, sizeof(cmd), 0)); - return ret == sizeof(cmd); + return ret == sizeof(cmd); } -int btsock_thread_post_cmd(int h, int type, const unsigned char* data, int size, uint32_t user_id) -{ - if(h < 0 || h >= MAX_THREAD) - { - APPL_TRACE_ERROR("invalid bt thread handle:%d", h); - return false; - } - if(ts[h].cmd_fdw == -1) - { - APPL_TRACE_ERROR("cmd socket is not created. socket thread may not initialized"); - return false; - } - sock_cmd_t cmd = {CMD_USER_PRIVATE, 0, type, size, user_id}; - APPL_TRACE_DEBUG("post cmd type:%d, size:%d, h:%d, ", type, size, h); - sock_cmd_t* cmd_send = &cmd; - int size_send = sizeof(cmd); - if(data && size) - { - size_send = sizeof(cmd) + size; - cmd_send = (sock_cmd_t*)alloca(size_send); - if(cmd_send) - { - *cmd_send = cmd; - memcpy(cmd_send + 1, data, size); - } - else - { - APPL_TRACE_ERROR("alloca failed at h:%d, cmd type:%d, size:%d", h, type, size_send); - return false; - } - } - - ssize_t ret; - OSI_NO_INTR(ret = send(ts[h].cmd_fdw, cmd_send, size_send, 0)); - - return ret == size_send; +int btsock_thread_post_cmd(int h, int type, const unsigned char* data, int size, + uint32_t user_id) { + if (h < 0 || h >= MAX_THREAD) { + APPL_TRACE_ERROR("invalid bt thread handle:%d", h); + return false; + } + if (ts[h].cmd_fdw == -1) { + APPL_TRACE_ERROR( + "cmd socket is not created. socket thread may not initialized"); + return false; + } + sock_cmd_t cmd = {CMD_USER_PRIVATE, 0, type, size, user_id}; + APPL_TRACE_DEBUG("post cmd type:%d, size:%d, h:%d, ", type, size, h); + sock_cmd_t* cmd_send = &cmd; + int size_send = sizeof(cmd); + if (data && size) { + size_send = sizeof(cmd) + size; + cmd_send = (sock_cmd_t*)alloca(size_send); + if (cmd_send) { + *cmd_send = cmd; + memcpy(cmd_send + 1, data, size); + } else { + APPL_TRACE_ERROR("alloca failed at h:%d, cmd type:%d, size:%d", h, type, + size_send); + return false; + } + } + + ssize_t ret; + OSI_NO_INTR(ret = send(ts[h].cmd_fdw, cmd_send, size_send, 0)); + + return ret == size_send; } -int btsock_thread_wakeup(int h) -{ - if(h < 0 || h >= MAX_THREAD) - { - APPL_TRACE_ERROR("invalid bt thread handle:%d", h); - return false; - } - if(ts[h].cmd_fdw == -1) - { - APPL_TRACE_ERROR("thread handle:%d, cmd socket is not created", h); - return false; - } - sock_cmd_t cmd = {CMD_WAKEUP, 0, 0, 0, 0}; +int btsock_thread_wakeup(int h) { + if (h < 0 || h >= MAX_THREAD) { + APPL_TRACE_ERROR("invalid bt thread handle:%d", h); + return false; + } + if (ts[h].cmd_fdw == -1) { + APPL_TRACE_ERROR("thread handle:%d, cmd socket is not created", h); + return false; + } + sock_cmd_t cmd = {CMD_WAKEUP, 0, 0, 0, 0}; - ssize_t ret; - OSI_NO_INTR(ret = send(ts[h].cmd_fdw, &cmd, sizeof(cmd), 0)); + ssize_t ret; + OSI_NO_INTR(ret = send(ts[h].cmd_fdw, &cmd, sizeof(cmd), 0)); - return ret == sizeof(cmd); + return ret == sizeof(cmd); } -int btsock_thread_exit(int h) -{ - if(h < 0 || h >= MAX_THREAD) - { - APPL_TRACE_ERROR("invalid bt thread handle:%d", h); - return false; - } - if(ts[h].cmd_fdw == -1) - { - APPL_TRACE_ERROR("cmd socket is not created"); - return false; - } - sock_cmd_t cmd = {CMD_EXIT, 0, 0, 0, 0}; +int btsock_thread_exit(int h) { + if (h < 0 || h >= MAX_THREAD) { + APPL_TRACE_ERROR("invalid bt thread handle:%d", h); + return false; + } + if (ts[h].cmd_fdw == -1) { + APPL_TRACE_ERROR("cmd socket is not created"); + return false; + } + sock_cmd_t cmd = {CMD_EXIT, 0, 0, 0, 0}; - ssize_t ret; - OSI_NO_INTR(ret = send(ts[h].cmd_fdw, &cmd, sizeof(cmd), 0)); + ssize_t ret; + OSI_NO_INTR(ret = send(ts[h].cmd_fdw, &cmd, sizeof(cmd), 0)); - if (ret == sizeof(cmd)) { - pthread_join(ts[h].thread_id, 0); - free_thread_slot(h); - return true; - } - return false; + if (ret == sizeof(cmd)) { + pthread_join(ts[h].thread_id, 0); + free_thread_slot(h); + return true; + } + return false; } -static void init_poll(int h) -{ - int i; - ts[h].poll_count = 0; - ts[h].thread_id = -1; - ts[h].callback = NULL; - ts[h].cmd_callback = NULL; - for(i = 0; i < MAX_POLL; i++) - { - ts[h].ps[i].pfd.fd = -1; - ts[h].psi[i] = -1; - } - init_cmd_fd(h); +static void init_poll(int h) { + int i; + ts[h].poll_count = 0; + ts[h].thread_id = -1; + ts[h].callback = NULL; + ts[h].cmd_callback = NULL; + for (i = 0; i < MAX_POLL; i++) { + ts[h].ps[i].pfd.fd = -1; + ts[h].psi[i] = -1; + } + init_cmd_fd(h); } -static inline unsigned int flags2pevents(int flags) -{ - unsigned int pevents = 0; - if(flags & SOCK_THREAD_FD_WR) - pevents |= POLLOUT; - if(flags & SOCK_THREAD_FD_RD) - pevents |= POLLIN; - pevents |= POLL_EXCEPTION_EVENTS; - return pevents; +static inline unsigned int flags2pevents(int flags) { + unsigned int pevents = 0; + if (flags & SOCK_THREAD_FD_WR) pevents |= POLLOUT; + if (flags & SOCK_THREAD_FD_RD) pevents |= POLLIN; + pevents |= POLL_EXCEPTION_EVENTS; + return pevents; } -static inline void set_poll(poll_slot_t* ps, int fd, int type, int flags, uint32_t user_id) -{ - ps->pfd.fd = fd; - ps->user_id = user_id; - if(ps->type != 0 && ps->type != type) - APPL_TRACE_ERROR("poll socket type should not changed! type was:%d, type now:%d", ps->type, type); - ps->type = type; - ps->flags = flags; - ps->pfd.events = flags2pevents(flags); - ps->pfd.revents = 0; -} -static inline void add_poll(int h, int fd, int type, int flags, uint32_t user_id) -{ - asrt(fd != -1); - int i; - int empty = -1; - poll_slot_t* ps = ts[h].ps; - - for(i = 0; i < MAX_POLL; i++) - { - if(ps[i].pfd.fd == fd) - { - asrt(ts[h].poll_count < MAX_POLL); - - set_poll(&ps[i], fd, type, flags | ps[i].flags, user_id); - return; - } - else if(empty < 0 && ps[i].pfd.fd == -1) - empty = i; - } - if(empty >= 0) - { - asrt(ts[h].poll_count < MAX_POLL); - set_poll(&ps[empty], fd, type, flags, user_id); - ++ts[h].poll_count; - return; - } - APPL_TRACE_ERROR("exceeded max poll slot:%d!", MAX_POLL); +static inline void set_poll(poll_slot_t* ps, int fd, int type, int flags, + uint32_t user_id) { + ps->pfd.fd = fd; + ps->user_id = user_id; + if (ps->type != 0 && ps->type != type) + APPL_TRACE_ERROR( + "poll socket type should not changed! type was:%d, type now:%d", + ps->type, type); + ps->type = type; + ps->flags = flags; + ps->pfd.events = flags2pevents(flags); + ps->pfd.revents = 0; } -static inline void remove_poll(int h, poll_slot_t* ps, int flags) -{ - if(flags == ps->flags) - { - //all monitored events signaled. To remove it, just clear the slot - --ts[h].poll_count; - memset(ps, 0, sizeof(*ps)); - ps->pfd.fd = -1; - } - else - { - //one read or one write monitor event signaled, removed the accordding bit - ps->flags &= ~flags; - //update the poll events mask - ps->pfd.events = flags2pevents(ps->flags); - } +static inline void add_poll(int h, int fd, int type, int flags, + uint32_t user_id) { + asrt(fd != -1); + int i; + int empty = -1; + poll_slot_t* ps = ts[h].ps; + + for (i = 0; i < MAX_POLL; i++) { + if (ps[i].pfd.fd == fd) { + asrt(ts[h].poll_count < MAX_POLL); + + set_poll(&ps[i], fd, type, flags | ps[i].flags, user_id); + return; + } else if (empty < 0 && ps[i].pfd.fd == -1) + empty = i; + } + if (empty >= 0) { + asrt(ts[h].poll_count < MAX_POLL); + set_poll(&ps[empty], fd, type, flags, user_id); + ++ts[h].poll_count; + return; + } + APPL_TRACE_ERROR("exceeded max poll slot:%d!", MAX_POLL); } -static int process_cmd_sock(int h) -{ - sock_cmd_t cmd = {-1, 0, 0, 0, 0}; - int fd = ts[h].cmd_fdr; - - ssize_t ret; - OSI_NO_INTR(ret = recv(fd, &cmd, sizeof(cmd), MSG_WAITALL)); - - if (ret != sizeof(cmd)) - { - APPL_TRACE_ERROR("recv cmd errno:%d", errno); - return false; - } - APPL_TRACE_DEBUG("cmd.id:%d", cmd.id); - switch(cmd.id) - { - case CMD_ADD_FD: - add_poll(h, cmd.fd, cmd.type, cmd.flags, cmd.user_id); - break; - case CMD_REMOVE_FD: - for (int i = 1; i < MAX_POLL; ++i) - { - poll_slot_t *poll_slot = &ts[h].ps[i]; - if (poll_slot->pfd.fd == cmd.fd) - { - remove_poll(h, poll_slot, poll_slot->flags); - break; - } - } - close(cmd.fd); - break; - case CMD_WAKEUP: - break; - case CMD_USER_PRIVATE: - asrt(ts[h].cmd_callback); - if(ts[h].cmd_callback) - ts[h].cmd_callback(fd, cmd.type, cmd.flags, cmd.user_id); - break; - case CMD_EXIT: - return false; - default: - APPL_TRACE_DEBUG("unknown cmd: %d", cmd.id); - break; - } - return true; +static inline void remove_poll(int h, poll_slot_t* ps, int flags) { + if (flags == ps->flags) { + // all monitored events signaled. To remove it, just clear the slot + --ts[h].poll_count; + memset(ps, 0, sizeof(*ps)); + ps->pfd.fd = -1; + } else { + // one read or one write monitor event signaled, removed the accordding bit + ps->flags &= ~flags; + // update the poll events mask + ps->pfd.events = flags2pevents(ps->flags); + } } -static void process_data_sock(int h, struct pollfd *pfds, int count) -{ - asrt(count <= ts[h].poll_count); - int i; - for( i= 1; i < ts[h].poll_count; i++) - { - if(pfds[i].revents) - { - int ps_i = ts[h].psi[i]; - asrt(pfds[i].fd == ts[h].ps[ps_i].pfd.fd); - uint32_t user_id = ts[h].ps[ps_i].user_id; - int type = ts[h].ps[ps_i].type; - int flags = 0; - print_events(pfds[i].revents); - if(IS_READ(pfds[i].revents)) - { - flags |= SOCK_THREAD_FD_RD; - } - if(IS_WRITE(pfds[i].revents)) - { - flags |= SOCK_THREAD_FD_WR; - } - if(IS_EXCEPTION(pfds[i].revents)) - { - flags |= SOCK_THREAD_FD_EXCEPTION; - //remove the whole slot not flags - remove_poll(h, &ts[h].ps[ps_i], ts[h].ps[ps_i].flags); - } - else if(flags) - remove_poll(h, &ts[h].ps[ps_i], flags); //remove the monitor flags that already processed - if(flags) - ts[h].callback(pfds[i].fd, type, flags, user_id); +static int process_cmd_sock(int h) { + sock_cmd_t cmd = {-1, 0, 0, 0, 0}; + int fd = ts[h].cmd_fdr; + + ssize_t ret; + OSI_NO_INTR(ret = recv(fd, &cmd, sizeof(cmd), MSG_WAITALL)); + + if (ret != sizeof(cmd)) { + APPL_TRACE_ERROR("recv cmd errno:%d", errno); + return false; + } + APPL_TRACE_DEBUG("cmd.id:%d", cmd.id); + switch (cmd.id) { + case CMD_ADD_FD: + add_poll(h, cmd.fd, cmd.type, cmd.flags, cmd.user_id); + break; + case CMD_REMOVE_FD: + for (int i = 1; i < MAX_POLL; ++i) { + poll_slot_t* poll_slot = &ts[h].ps[i]; + if (poll_slot->pfd.fd == cmd.fd) { + remove_poll(h, poll_slot, poll_slot->flags); + break; } - } + } + close(cmd.fd); + break; + case CMD_WAKEUP: + break; + case CMD_USER_PRIVATE: + asrt(ts[h].cmd_callback); + if (ts[h].cmd_callback) + ts[h].cmd_callback(fd, cmd.type, cmd.flags, cmd.user_id); + break; + case CMD_EXIT: + return false; + default: + APPL_TRACE_DEBUG("unknown cmd: %d", cmd.id); + break; + } + return true; +} +static void process_data_sock(int h, struct pollfd* pfds, int count) { + asrt(count <= ts[h].poll_count); + int i; + for (i = 1; i < ts[h].poll_count; i++) { + if (pfds[i].revents) { + int ps_i = ts[h].psi[i]; + asrt(pfds[i].fd == ts[h].ps[ps_i].pfd.fd); + uint32_t user_id = ts[h].ps[ps_i].user_id; + int type = ts[h].ps[ps_i].type; + int flags = 0; + print_events(pfds[i].revents); + if (IS_READ(pfds[i].revents)) { + flags |= SOCK_THREAD_FD_RD; + } + if (IS_WRITE(pfds[i].revents)) { + flags |= SOCK_THREAD_FD_WR; + } + if (IS_EXCEPTION(pfds[i].revents)) { + flags |= SOCK_THREAD_FD_EXCEPTION; + // remove the whole slot not flags + remove_poll(h, &ts[h].ps[ps_i], ts[h].ps[ps_i].flags); + } else if (flags) + remove_poll(h, &ts[h].ps[ps_i], + flags); // remove the monitor flags that already processed + if (flags) ts[h].callback(pfds[i].fd, type, flags, user_id); + } + } } -static void prepare_poll_fds(int h, struct pollfd* pfds) -{ - int count = 0; - int ps_i = 0; - int pfd_i = 0; - asrt(ts[h].poll_count <= MAX_POLL); - memset(pfds, 0, sizeof(pfds[0])*ts[h].poll_count); - while(count < ts[h].poll_count) - { - if(ps_i >= MAX_POLL) - { - APPL_TRACE_ERROR("exceed max poll range, ps_i:%d, MAX_POLL:%d, count:%d, ts[h].poll_count:%d", - ps_i, MAX_POLL, count, ts[h].poll_count); - return; - } - if(ts[h].ps[ps_i].pfd.fd >= 0) - { - pfds[pfd_i] = ts[h].ps[ps_i].pfd; - ts[h].psi[pfd_i] = ps_i; - count++; - pfd_i++; - } - ps_i++; - } +static void prepare_poll_fds(int h, struct pollfd* pfds) { + int count = 0; + int ps_i = 0; + int pfd_i = 0; + asrt(ts[h].poll_count <= MAX_POLL); + memset(pfds, 0, sizeof(pfds[0]) * ts[h].poll_count); + while (count < ts[h].poll_count) { + if (ps_i >= MAX_POLL) { + APPL_TRACE_ERROR( + "exceed max poll range, ps_i:%d, MAX_POLL:%d, count:%d, " + "ts[h].poll_count:%d", + ps_i, MAX_POLL, count, ts[h].poll_count); + return; + } + if (ts[h].ps[ps_i].pfd.fd >= 0) { + pfds[pfd_i] = ts[h].ps[ps_i].pfd; + ts[h].psi[pfd_i] = ps_i; + count++; + pfd_i++; + } + ps_i++; + } } -static void *sock_poll_thread(void *arg) -{ - struct pollfd pfds[MAX_POLL]; - memset(pfds, 0, sizeof(pfds)); - int h = (intptr_t)arg; - for(;;) - { - prepare_poll_fds(h, pfds); - int ret; - OSI_NO_INTR(ret = poll(pfds, ts[h].poll_count, -1)); - if(ret == -1) - { - APPL_TRACE_ERROR("poll ret -1, exit the thread, errno:%d, err:%s", errno, strerror(errno)); - break; - } - if(ret != 0) - { - int need_process_data_fd = true; - if(pfds[0].revents) //cmd fd always is the first one - { - asrt(pfds[0].fd == ts[h].cmd_fdr); - if(!process_cmd_sock(h)) - { - APPL_TRACE_DEBUG("h:%d, process_cmd_sock return false, exit...", h); - break; - } - if(ret == 1) - need_process_data_fd = false; - else ret--; //exclude the cmd fd - } - if(need_process_data_fd) - process_data_sock(h, pfds, ret); +static void* sock_poll_thread(void* arg) { + struct pollfd pfds[MAX_POLL]; + memset(pfds, 0, sizeof(pfds)); + int h = (intptr_t)arg; + for (;;) { + prepare_poll_fds(h, pfds); + int ret; + OSI_NO_INTR(ret = poll(pfds, ts[h].poll_count, -1)); + if (ret == -1) { + APPL_TRACE_ERROR("poll ret -1, exit the thread, errno:%d, err:%s", errno, + strerror(errno)); + break; + } + if (ret != 0) { + int need_process_data_fd = true; + if (pfds[0].revents) // cmd fd always is the first one + { + asrt(pfds[0].fd == ts[h].cmd_fdr); + if (!process_cmd_sock(h)) { + APPL_TRACE_DEBUG("h:%d, process_cmd_sock return false, exit...", h); + break; } - else {APPL_TRACE_DEBUG("no data, select ret: %d", ret)}; - } - ts[h].thread_id = -1; - APPL_TRACE_DEBUG("socket poll thread exiting, h:%d", h); - return 0; + if (ret == 1) + need_process_data_fd = false; + else + ret--; // exclude the cmd fd + } + if (need_process_data_fd) process_data_sock(h, pfds, ret); + } else { + APPL_TRACE_DEBUG("no data, select ret: %d", ret) + }; + } + ts[h].thread_id = -1; + APPL_TRACE_DEBUG("socket poll thread exiting, h:%d", h); + return 0; } diff --git a/btif/src/btif_sock_util.cc b/btif/src/btif_sock_util.cc index 234974ff1..784f00377 100644 --- a/btif/src/btif_sock_util.cc +++ b/btif/src/btif_sock_util.cc @@ -36,6 +36,7 @@ #include #include +#include "bt_common.h" #include "bt_target.h" #include "bta_api.h" #include "bta_jv_api.h" @@ -47,198 +48,184 @@ #include "btm_api.h" #include "btm_int.h" #include "btu.h" -#include "bt_common.h" #include "hcimsgs.h" #include "osi/include/log.h" #include "port_api.h" #include "sdp_api.h" -#define asrt(s) if(!(s)) BTIF_TRACE_ERROR("## %s assert %s failed at line:%d ##",__func__, #s, __LINE__) - -int sock_send_all(int sock_fd, const uint8_t* buf, int len) -{ - int s = len; - - while(s) - { - ssize_t ret; - OSI_NO_INTR(ret = send(sock_fd, buf, s, 0)); - if(ret <= 0) - { - BTIF_TRACE_ERROR("sock fd:%d send errno:%d, ret:%d", sock_fd, errno, ret); - return -1; - } - buf += ret; - s -= ret; +#define asrt(s) \ + if (!(s)) \ + BTIF_TRACE_ERROR("## %s assert %s failed at line:%d ##", __func__, #s, \ + __LINE__) + +int sock_send_all(int sock_fd, const uint8_t* buf, int len) { + int s = len; + + while (s) { + ssize_t ret; + OSI_NO_INTR(ret = send(sock_fd, buf, s, 0)); + if (ret <= 0) { + BTIF_TRACE_ERROR("sock fd:%d send errno:%d, ret:%d", sock_fd, errno, ret); + return -1; } - return len; + buf += ret; + s -= ret; + } + return len; } -int sock_recv_all(int sock_fd, uint8_t* buf, int len) -{ - int r = len; - - while(r) - { - ssize_t ret; - OSI_NO_INTR(ret = recv(sock_fd, buf, r, MSG_WAITALL)); - if(ret <= 0) - { - BTIF_TRACE_ERROR("sock fd:%d recv errno:%d, ret:%d", sock_fd, errno, ret); - return -1; - } - buf += ret; - r -= ret; +int sock_recv_all(int sock_fd, uint8_t* buf, int len) { + int r = len; + + while (r) { + ssize_t ret; + OSI_NO_INTR(ret = recv(sock_fd, buf, r, MSG_WAITALL)); + if (ret <= 0) { + BTIF_TRACE_ERROR("sock fd:%d recv errno:%d, ret:%d", sock_fd, errno, ret); + return -1; } - return len; + buf += ret; + r -= ret; + } + return len; } -int sock_send_fd(int sock_fd, const uint8_t* buf, int len, int send_fd) -{ - struct msghdr msg; - unsigned char *buffer = (unsigned char *)buf; - memset(&msg, 0, sizeof(msg)); - - struct cmsghdr *cmsg; - char msgbuf[CMSG_SPACE(1)]; - asrt(send_fd != -1); - if(sock_fd == -1 || send_fd == -1) - return -1; - // Add any pending outbound file descriptors to the message - // See "man cmsg" really - msg.msg_control = msgbuf; - msg.msg_controllen = sizeof msgbuf; - cmsg = CMSG_FIRSTHDR(&msg); - cmsg->cmsg_level = SOL_SOCKET; - cmsg->cmsg_type = SCM_RIGHTS; - cmsg->cmsg_len = CMSG_LEN(sizeof send_fd); - memcpy(CMSG_DATA(cmsg), &send_fd, sizeof send_fd); - - // We only write our msg_control during the first write - int ret_len = len; - while (len > 0) { - struct iovec iv; - memset(&iv, 0, sizeof(iv)); - - iv.iov_base = buffer; - iv.iov_len = len; - - msg.msg_iov = &iv; - msg.msg_iovlen = 1; - - ssize_t ret; - OSI_NO_INTR(ret = sendmsg(sock_fd, &msg, MSG_NOSIGNAL)); - if (ret < 0) { - BTIF_TRACE_ERROR("fd:%d, send_fd:%d, sendmsg ret:%d, errno:%d, %s", - sock_fd, send_fd, (int)ret, errno, strerror(errno)); - ret_len = -1; - break; - } - - buffer += ret; - len -= ret; - - // Wipes out any msg_control too - memset(&msg, 0, sizeof(msg)); +int sock_send_fd(int sock_fd, const uint8_t* buf, int len, int send_fd) { + struct msghdr msg; + unsigned char* buffer = (unsigned char*)buf; + memset(&msg, 0, sizeof(msg)); + + struct cmsghdr* cmsg; + char msgbuf[CMSG_SPACE(1)]; + asrt(send_fd != -1); + if (sock_fd == -1 || send_fd == -1) return -1; + // Add any pending outbound file descriptors to the message + // See "man cmsg" really + msg.msg_control = msgbuf; + msg.msg_controllen = sizeof msgbuf; + cmsg = CMSG_FIRSTHDR(&msg); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SCM_RIGHTS; + cmsg->cmsg_len = CMSG_LEN(sizeof send_fd); + memcpy(CMSG_DATA(cmsg), &send_fd, sizeof send_fd); + + // We only write our msg_control during the first write + int ret_len = len; + while (len > 0) { + struct iovec iv; + memset(&iv, 0, sizeof(iv)); + + iv.iov_base = buffer; + iv.iov_len = len; + + msg.msg_iov = &iv; + msg.msg_iovlen = 1; + + ssize_t ret; + OSI_NO_INTR(ret = sendmsg(sock_fd, &msg, MSG_NOSIGNAL)); + if (ret < 0) { + BTIF_TRACE_ERROR("fd:%d, send_fd:%d, sendmsg ret:%d, errno:%d, %s", + sock_fd, send_fd, (int)ret, errno, strerror(errno)); + ret_len = -1; + break; } - BTIF_TRACE_DEBUG("close fd:%d after sent", send_fd); - // TODO: This seems wrong - if the FD is not opened in JAVA before this is called - // we get a "socket closed" exception in java, when reading from the socket... - close(send_fd); - return ret_len; + + buffer += ret; + len -= ret; + + // Wipes out any msg_control too + memset(&msg, 0, sizeof(msg)); + } + BTIF_TRACE_DEBUG("close fd:%d after sent", send_fd); + // TODO: This seems wrong - if the FD is not opened in JAVA before this is + // called + // we get a "socket closed" exception in java, when reading from the + // socket... + close(send_fd); + return ret_len; } static const char* hex_table = "0123456789abcdef"; -static inline void byte2hex(const char* data, char** str) -{ - **str = hex_table[(*data >> 4) & 0xf]; - ++*str; - **str = hex_table[*data & 0xf]; - ++*str; +static inline void byte2hex(const char* data, char** str) { + **str = hex_table[(*data >> 4) & 0xf]; + ++*str; + **str = hex_table[*data & 0xf]; + ++*str; } -static inline void byte2char(const char* data, char** str) -{ - **str = *data < ' ' ? '.' : *data > '~' ? '.' : *data; - ++(*str); +static inline void byte2char(const char* data, char** str) { + **str = *data < ' ' ? '.' : *data > '~' ? '.' : *data; + ++(*str); } -static inline void word2hex(const char* data, char** hex) -{ - byte2hex(&data[1], hex); - byte2hex(&data[0], hex); +static inline void word2hex(const char* data, char** hex) { + byte2hex(&data[1], hex); + byte2hex(&data[0], hex); } -void dump_bin(const char* title, const char* data, int size) -{ - char line_buff[256]; - char *line; - int i, j, addr; - const int width = 16; - LOG_DEBUG(LOG_TAG, "%s, size:%d, dump started {", title, size); - if(size <= 0) - return; - //write offset - line = line_buff; - *line++ = ' '; - *line++ = ' '; - *line++ = ' '; - *line++ = ' '; +void dump_bin(const char* title, const char* data, int size) { + char line_buff[256]; + char* line; + int i, j, addr; + const int width = 16; + LOG_DEBUG(LOG_TAG, "%s, size:%d, dump started {", title, size); + if (size <= 0) return; + // write offset + line = line_buff; + *line++ = ' '; + *line++ = ' '; + *line++ = ' '; + *line++ = ' '; + *line++ = ' '; + *line++ = ' '; + for (j = 0; j < width; j++) { + byte2hex((const char*)&j, &line); *line++ = ' '; + } + *line = 0; + LOG_DEBUG(LOG_TAG, "%s", line_buff); + + for (i = 0; i < size / width; i++) { + line = line_buff; + // write address: + addr = i * width; + word2hex((const char*)&addr, &line); + *line++ = ':'; *line++ = ' '; - for(j = 0; j < width; j++) - { - byte2hex((const char*)&j, &line); - *line++ = ' '; + // write hex of data + for (j = 0; j < width; j++) { + byte2hex(&data[j], &line); + *line++ = ' '; } + // write char of data + for (j = 0; j < width; j++) byte2char(data++, &line); + // wirte the end of line *line = 0; + // output the line LOG_DEBUG(LOG_TAG, "%s", line_buff); - - for(i = 0; i < size / width; i++) - { - line = line_buff; - //write address: - addr = i*width; - word2hex((const char*)&addr, &line); - *line++ = ':'; *line++ = ' '; - //write hex of data - for(j = 0; j < width; j++) - { - byte2hex(&data[j], &line); - *line++ = ' '; - } - //write char of data - for(j = 0; j < width; j++) - byte2char(data++, &line); - //wirte the end of line - *line = 0; - //output the line - LOG_DEBUG(LOG_TAG, "%s", line_buff); + } + // last line of left over if any + int leftover = size % width; + if (leftover > 0) { + line = line_buff; + // write address: + addr = i * width; + word2hex((const char*)&addr, &line); + *line++ = ':'; + *line++ = ' '; + // write hex of data + for (j = 0; j < leftover; j++) { + byte2hex(&data[j], &line); + *line++ = ' '; } - //last line of left over if any - int leftover = size % width; - if(leftover > 0) - { - line = line_buff; - //write address: - addr = i*width; - word2hex((const char*)&addr, &line); - *line++ = ':'; *line++ = ' '; - //write hex of data - for(j = 0; j < leftover; j++) { - byte2hex(&data[j], &line); - *line++ = ' '; - } - //write hex padding - for(; j < width; j++) { - *line++ = ' '; - *line++ = ' '; - *line++ = ' '; - } - //write char of data - for(j = 0; j < leftover; j++) - byte2char(data++, &line); - //write the end of line - *line = 0; - //output the line - LOG_DEBUG(LOG_TAG, "%s", line_buff); + // write hex padding + for (; j < width; j++) { + *line++ = ' '; + *line++ = ' '; + *line++ = ' '; } - LOG_DEBUG(LOG_TAG, "%s, size:%d, dump ended }", title, size); + // write char of data + for (j = 0; j < leftover; j++) byte2char(data++, &line); + // write the end of line + *line = 0; + // output the line + LOG_DEBUG(LOG_TAG, "%s", line_buff); + } + LOG_DEBUG(LOG_TAG, "%s, size:%d, dump ended }", title, size); } - diff --git a/btif/src/btif_storage.cc b/btif/src/btif_storage.cc index 638c45fe5..21d6eb32e 100644 --- a/btif/src/btif_storage.cc +++ b/btif/src/btif_storage.cc @@ -39,13 +39,13 @@ #include #include +#include "bt_common.h" #include "bta_hh_api.h" #include "btcore/include/bdaddr.h" #include "btif_api.h" #include "btif_config.h" #include "btif_hh.h" #include "btif_util.h" -#include "bt_common.h" #include "osi/include/allocator.h" #include "osi/include/compat.h" #include "osi/include/config.h" @@ -53,8 +53,8 @@ #include "osi/include/osi.h" /************************************************************************************ -** Constants & Macros -************************************************************************************/ + * Constants & Macros + ***********************************************************************************/ // TODO(armansito): Find a better way than using a hardcoded path. #define BTIF_STORAGE_PATH_BLUEDROID "/data/misc/bluedroid" @@ -80,445 +80,436 @@ /* This is a local property to add a device found */ #define BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP 0xFF -#define BTIF_STORAGE_GET_ADAPTER_PROP(t,v,l,p) \ - {(p).type=t;(p).val=v;(p).len=l; btif_storage_get_adapter_property(&(p));} - -#define BTIF_STORAGE_GET_REMOTE_PROP(b,t,v,l,p) \ - {(p).type=t;(p).val=v;(p).len=l;btif_storage_get_remote_device_property(b,&(p));} - -#define STORAGE_BDADDR_STRING_SZ (18) /* 00:11:22:33:44:55 */ -#define STORAGE_UUID_STRING_SIZE (36+1) /* 00001200-0000-1000-8000-00805f9b34fb; */ -#define STORAGE_PINLEN_STRING_MAX_SIZE (2) /* ascii pinlen max chars */ -#define STORAGE_KEYTYPE_STRING_MAX_SIZE (1) /* ascii keytype max chars */ - -#define STORAGE_KEY_TYPE_MAX (10) - -#define STORAGE_HID_ATRR_MASK_SIZE (4) -#define STORAGE_HID_SUB_CLASS_SIZE (2) -#define STORAGE_HID_APP_ID_SIZE (2) -#define STORAGE_HID_VENDOR_ID_SIZE (4) -#define STORAGE_HID_PRODUCT_ID_SIZE (4) -#define STORAGE_HID_VERSION_SIZE (4) -#define STORAGE_HID_CTRY_CODE_SIZE (2) -#define STORAGE_HID_DESC_LEN_SIZE (4) -#define STORAGE_HID_DESC_MAX_SIZE (2*512) - -/* <18 char bd addr> LIST< <36 char uuid> <;> > */ -#define BTIF_REMOTE_SERVICES_ENTRY_SIZE_MAX (STORAGE_BDADDR_STRING_SZ + 1 +\ - STORAGE_UUID_STRING_SIZE*BT_MAX_NUM_UUIDS + \ - STORAGE_PINLEN_STRING_MAX_SIZE +\ - STORAGE_KEYTYPE_STRING_MAX_SIZE) - -#define STORAGE_REMOTE_LINKKEYS_ENTRY_SIZE (LINK_KEY_LEN*2 + 1 + 2 + 1 + 2) - -/* <18 char bd addr> LIST > - > - > */ -#define BTIF_HID_INFO_ENTRY_SIZE_MAX (STORAGE_BDADDR_STRING_SZ + 1 +\ - STORAGE_HID_ATRR_MASK_SIZE + 1 +\ - STORAGE_HID_SUB_CLASS_SIZE + 1 +\ - STORAGE_HID_APP_ID_SIZE+ 1 +\ - STORAGE_HID_VENDOR_ID_SIZE+ 1 +\ - STORAGE_HID_PRODUCT_ID_SIZE+ 1 +\ - STORAGE_HID_VERSION_SIZE+ 1 +\ - STORAGE_HID_CTRY_CODE_SIZE+ 1 +\ - STORAGE_HID_DESC_LEN_SIZE+ 1 +\ - STORAGE_HID_DESC_MAX_SIZE+ 1 ) +#define BTIF_STORAGE_GET_ADAPTER_PROP(t, v, l, p) \ + { \ + (p).type = t; \ + (p).val = v; \ + (p).len = l; \ + btif_storage_get_adapter_property(&(p)); \ + } + +#define BTIF_STORAGE_GET_REMOTE_PROP(b, t, v, l, p) \ + { \ + (p).type = t; \ + (p).val = v; \ + (p).len = l; \ + btif_storage_get_remote_device_property(b, &(p)); \ + } + +#define STORAGE_BDADDR_STRING_SZ (18) /* 00:11:22:33:44:55 */ +#define STORAGE_UUID_STRING_SIZE \ + (36 + 1) /* 00001200-0000-1000-8000-00805f9b34fb; */ +#define STORAGE_PINLEN_STRING_MAX_SIZE (2) /* ascii pinlen max chars */ +#define STORAGE_KEYTYPE_STRING_MAX_SIZE (1) /* ascii keytype max chars */ + +#define STORAGE_KEY_TYPE_MAX (10) + +#define STORAGE_HID_ATRR_MASK_SIZE (4) +#define STORAGE_HID_SUB_CLASS_SIZE (2) +#define STORAGE_HID_APP_ID_SIZE (2) +#define STORAGE_HID_VENDOR_ID_SIZE (4) +#define STORAGE_HID_PRODUCT_ID_SIZE (4) +#define STORAGE_HID_VERSION_SIZE (4) +#define STORAGE_HID_CTRY_CODE_SIZE (2) +#define STORAGE_HID_DESC_LEN_SIZE (4) +#define STORAGE_HID_DESC_MAX_SIZE (2 * 512) + +/* <18 char bd addr> LIST< <36 char uuid> <;> > + */ +#define BTIF_REMOTE_SERVICES_ENTRY_SIZE_MAX \ + (STORAGE_BDADDR_STRING_SZ + 1 + \ + STORAGE_UUID_STRING_SIZE * BT_MAX_NUM_UUIDS + \ + STORAGE_PINLEN_STRING_MAX_SIZE + STORAGE_KEYTYPE_STRING_MAX_SIZE) + +#define STORAGE_REMOTE_LINKKEYS_ENTRY_SIZE (LINK_KEY_LEN * 2 + 1 + 2 + 1 + 2) + +/* <18 char bd addr> LIST > + + > + + > + */ +#define BTIF_HID_INFO_ENTRY_SIZE_MAX \ + (STORAGE_BDADDR_STRING_SZ + 1 + STORAGE_HID_ATRR_MASK_SIZE + 1 + \ + STORAGE_HID_SUB_CLASS_SIZE + 1 + STORAGE_HID_APP_ID_SIZE + 1 + \ + STORAGE_HID_VENDOR_ID_SIZE + 1 + STORAGE_HID_PRODUCT_ID_SIZE + 1 + \ + STORAGE_HID_VERSION_SIZE + 1 + STORAGE_HID_CTRY_CODE_SIZE + 1 + \ + STORAGE_HID_DESC_LEN_SIZE + 1 + STORAGE_HID_DESC_MAX_SIZE + 1) /* currently remote services is the potentially largest entry */ #define BTIF_STORAGE_MAX_LINE_SZ BTIF_REMOTE_SERVICES_ENTRY_SIZE_MAX /* check against unv max entry size at compile time */ #if (BTIF_STORAGE_ENTRY_MAX_SIZE > UNV_MAXLINE_LENGTH) - #error "btif storage entry size exceeds unv max line size" +#error "btif storage entry size exceeds unv max line size" #endif /************************************************************************************ -** Local type definitions -************************************************************************************/ -typedef struct -{ - uint32_t num_devices; - bt_bdaddr_t devices[BTM_SEC_MAX_DEVICE_RECORDS]; + * Local type definitions + ***********************************************************************************/ +typedef struct { + uint32_t num_devices; + bt_bdaddr_t devices[BTM_SEC_MAX_DEVICE_RECORDS]; } btif_bonded_devices_t; /************************************************************************************ -** External variables -************************************************************************************/ + * External variables + ***********************************************************************************/ extern bt_bdaddr_t btif_local_bd_addr; /************************************************************************************ -** External functions -************************************************************************************/ + * External functions + ***********************************************************************************/ extern void btif_gatts_add_bonded_dev_from_nv(BD_ADDR bda); /************************************************************************************ -** Internal Functions -************************************************************************************/ + * Internal Functions + ***********************************************************************************/ -static bt_status_t btif_in_fetch_bonded_ble_device(const char *remote_bd_addr,int add, - btif_bonded_devices_t *p_bonded_devices); -static bt_status_t btif_in_fetch_bonded_device(const char *bdstr); +static bt_status_t btif_in_fetch_bonded_ble_device( + const char* remote_bd_addr, int add, + btif_bonded_devices_t* p_bonded_devices); +static bt_status_t btif_in_fetch_bonded_device(const char* bdstr); -static bool btif_has_ble_keys(const char *bdstr); +static bool btif_has_ble_keys(const char* bdstr); /************************************************************************************ -** Static functions -************************************************************************************/ - -static int prop2cfg(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop) -{ - bdstr_t bdstr = {0}; - if(remote_bd_addr) - bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); - BTIF_TRACE_DEBUG("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len); - char value[1024]; - if(prop->len <= 0 || prop->len > (int)sizeof(value) - 1) - { - BTIF_TRACE_ERROR("property type:%d, len:%d is invalid", prop->type, prop->len); - return false; - } - switch(prop->type) - { - case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP: - btif_config_set_int(bdstr, - BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int)time(NULL)); - break; - case BT_PROPERTY_BDNAME: - strncpy(value, (char*)prop->val, prop->len); - value[prop->len]='\0'; - if(remote_bd_addr) - btif_config_set_str(bdstr, - BTIF_STORAGE_PATH_REMOTE_NAME, value); - else btif_config_set_str("Adapter", - BTIF_STORAGE_KEY_ADAPTER_NAME, value); - break; - case BT_PROPERTY_REMOTE_FRIENDLY_NAME: - strncpy(value, (char*)prop->val, prop->len); - value[prop->len]='\0'; - btif_config_set_str(bdstr, BTIF_STORAGE_PATH_REMOTE_ALIASE, value); - break; - case BT_PROPERTY_ADAPTER_SCAN_MODE: - btif_config_set_int("Adapter", - BTIF_STORAGE_KEY_ADAPTER_SCANMODE, *(int*)prop->val); - break; - case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: - btif_config_set_int("Adapter", - BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, *(int*)prop->val); - break; - case BT_PROPERTY_CLASS_OF_DEVICE: - btif_config_set_int(bdstr, - BTIF_STORAGE_PATH_REMOTE_DEVCLASS, *(int*)prop->val); - break; - case BT_PROPERTY_TYPE_OF_DEVICE: - btif_config_set_int(bdstr, - BTIF_STORAGE_PATH_REMOTE_DEVTYPE, *(int*)prop->val); - break; - case BT_PROPERTY_UUIDS: - { - uint32_t i; - char buf[64]; - value[0] = 0; - for (i=0; i < (prop->len)/sizeof(bt_uuid_t); i++) - { - bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val + i; - memset(buf, 0, sizeof(buf)); - uuid_to_string_legacy(p_uuid, buf, sizeof(buf)); - strcat(value, buf); - //strcat(value, ";"); - strcat(value, " "); - } - btif_config_set_str(bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, value); - break; - } - case BT_PROPERTY_REMOTE_VERSION_INFO: - { - bt_remote_version_t *info = (bt_remote_version_t *)prop->val; - - if (!info) - return false; - - btif_config_set_int(bdstr, - BTIF_STORAGE_PATH_REMOTE_VER_MFCT, info->manufacturer); - btif_config_set_int(bdstr, - BTIF_STORAGE_PATH_REMOTE_VER_VER, info->version); - btif_config_set_int(bdstr, - BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, info->sub_ver); - } break; - - default: - BTIF_TRACE_ERROR("Unknown prop type:%d", prop->type); - return false; - } - - /* save changes if the device was bonded */ - if (btif_in_fetch_bonded_device(bdstr) == BT_STATUS_SUCCESS) { - btif_config_save(); + * Static functions + ***********************************************************************************/ + +static int prop2cfg(bt_bdaddr_t* remote_bd_addr, bt_property_t* prop) { + bdstr_t bdstr = {0}; + if (remote_bd_addr) bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + BTIF_TRACE_DEBUG("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, + prop->len); + char value[1024]; + if (prop->len <= 0 || prop->len > (int)sizeof(value) - 1) { + BTIF_TRACE_ERROR("property type:%d, len:%d is invalid", prop->type, + prop->len); + return false; + } + switch (prop->type) { + case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP: + btif_config_set_int(bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTIME, + (int)time(NULL)); + break; + case BT_PROPERTY_BDNAME: + strncpy(value, (char*)prop->val, prop->len); + value[prop->len] = '\0'; + if (remote_bd_addr) + btif_config_set_str(bdstr, BTIF_STORAGE_PATH_REMOTE_NAME, value); + else + btif_config_set_str("Adapter", BTIF_STORAGE_KEY_ADAPTER_NAME, value); + break; + case BT_PROPERTY_REMOTE_FRIENDLY_NAME: + strncpy(value, (char*)prop->val, prop->len); + value[prop->len] = '\0'; + btif_config_set_str(bdstr, BTIF_STORAGE_PATH_REMOTE_ALIASE, value); + break; + case BT_PROPERTY_ADAPTER_SCAN_MODE: + btif_config_set_int("Adapter", BTIF_STORAGE_KEY_ADAPTER_SCANMODE, + *(int*)prop->val); + break; + case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: + btif_config_set_int("Adapter", BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, + *(int*)prop->val); + break; + case BT_PROPERTY_CLASS_OF_DEVICE: + btif_config_set_int(bdstr, BTIF_STORAGE_PATH_REMOTE_DEVCLASS, + *(int*)prop->val); + break; + case BT_PROPERTY_TYPE_OF_DEVICE: + btif_config_set_int(bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTYPE, + *(int*)prop->val); + break; + case BT_PROPERTY_UUIDS: { + uint32_t i; + char buf[64]; + value[0] = 0; + for (i = 0; i < (prop->len) / sizeof(bt_uuid_t); i++) { + bt_uuid_t* p_uuid = (bt_uuid_t*)prop->val + i; + memset(buf, 0, sizeof(buf)); + uuid_to_string_legacy(p_uuid, buf, sizeof(buf)); + strcat(value, buf); + // strcat(value, ";"); + strcat(value, " "); + } + btif_config_set_str(bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, value); + break; } + case BT_PROPERTY_REMOTE_VERSION_INFO: { + bt_remote_version_t* info = (bt_remote_version_t*)prop->val; + + if (!info) return false; + + btif_config_set_int(bdstr, BTIF_STORAGE_PATH_REMOTE_VER_MFCT, + info->manufacturer); + btif_config_set_int(bdstr, BTIF_STORAGE_PATH_REMOTE_VER_VER, + info->version); + btif_config_set_int(bdstr, BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, + info->sub_ver); + } break; + + default: + BTIF_TRACE_ERROR("Unknown prop type:%d", prop->type); + return false; + } + + /* save changes if the device was bonded */ + if (btif_in_fetch_bonded_device(bdstr) == BT_STATUS_SUCCESS) { + btif_config_save(); + } - return true; + return true; } -static int cfg2prop(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop) -{ - bdstr_t bdstr = {0}; - if(remote_bd_addr) - bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); - BTIF_TRACE_DEBUG("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, prop->len); - if(prop->len <= 0) - { - BTIF_TRACE_ERROR("property type:%d, len:%d is invalid", prop->type, prop->len); - return false; +static int cfg2prop(bt_bdaddr_t* remote_bd_addr, bt_property_t* prop) { + bdstr_t bdstr = {0}; + if (remote_bd_addr) bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + BTIF_TRACE_DEBUG("in, bd addr:%s, prop type:%d, len:%d", bdstr, prop->type, + prop->len); + if (prop->len <= 0) { + BTIF_TRACE_ERROR("property type:%d, len:%d is invalid", prop->type, + prop->len); + return false; + } + int ret = false; + switch (prop->type) { + case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP: + if (prop->len >= (int)sizeof(int)) + ret = btif_config_get_int(bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTIME, + (int*)prop->val); + break; + case BT_PROPERTY_BDNAME: { + int len = prop->len; + if (remote_bd_addr) + ret = btif_config_get_str(bdstr, BTIF_STORAGE_PATH_REMOTE_NAME, + (char*)prop->val, &len); + else + ret = btif_config_get_str("Adapter", BTIF_STORAGE_KEY_ADAPTER_NAME, + (char*)prop->val, &len); + if (ret && len && len <= prop->len) + prop->len = len - 1; + else { + prop->len = 0; + ret = false; + } + break; } - int ret = false; - switch(prop->type) - { - case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP: - if(prop->len >= (int)sizeof(int)) - ret = btif_config_get_int(bdstr, - BTIF_STORAGE_PATH_REMOTE_DEVTIME, (int*)prop->val); - break; - case BT_PROPERTY_BDNAME: - { - int len = prop->len; - if(remote_bd_addr) - ret = btif_config_get_str(bdstr, - BTIF_STORAGE_PATH_REMOTE_NAME, (char*)prop->val, &len); - else ret = btif_config_get_str("Adapter", - BTIF_STORAGE_KEY_ADAPTER_NAME, (char*)prop->val, &len); - if(ret && len && len <= prop->len) - prop->len = len - 1; - else - { - prop->len = 0; - ret = false; - } - break; - } - case BT_PROPERTY_REMOTE_FRIENDLY_NAME: - { - int len = prop->len; - ret = btif_config_get_str(bdstr, - BTIF_STORAGE_PATH_REMOTE_ALIASE, (char*)prop->val, &len); - if(ret && len && len <= prop->len) - prop->len = len - 1; - else - { - prop->len = 0; - ret = false; - } - break; - } - case BT_PROPERTY_ADAPTER_SCAN_MODE: - if(prop->len >= (int)sizeof(int)) - ret = btif_config_get_int("Adapter", - BTIF_STORAGE_KEY_ADAPTER_SCANMODE, (int*)prop->val); - break; - case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: - if(prop->len >= (int)sizeof(int)) - ret = btif_config_get_int("Adapter", - BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, (int*)prop->val); - break; - case BT_PROPERTY_CLASS_OF_DEVICE: - if(prop->len >= (int)sizeof(int)) - ret = btif_config_get_int(bdstr, - BTIF_STORAGE_PATH_REMOTE_DEVCLASS, (int*)prop->val); - break; - case BT_PROPERTY_TYPE_OF_DEVICE: - if(prop->len >= (int)sizeof(int)) - ret = btif_config_get_int(bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTYPE, (int*)prop->val); - break; - case BT_PROPERTY_UUIDS: - { - char value[1280]; - int size = sizeof(value); - if(btif_config_get_str(bdstr, - BTIF_STORAGE_PATH_REMOTE_SERVICE, value, &size)) - { - bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val; - size_t num_uuids = btif_split_uuids_string(value, p_uuid, BT_MAX_NUM_UUIDS); - prop->len = num_uuids * sizeof(bt_uuid_t); - ret = true; - } - else - { - prop->val = NULL; - prop->len = 0; - } - } break; - - case BT_PROPERTY_REMOTE_VERSION_INFO: - { - bt_remote_version_t *info = (bt_remote_version_t *)prop->val; - - if(prop->len >= (int)sizeof(bt_remote_version_t)) - { - ret = btif_config_get_int(bdstr, - BTIF_STORAGE_PATH_REMOTE_VER_MFCT, &info->manufacturer); - - if (ret == true) - ret = btif_config_get_int(bdstr, - BTIF_STORAGE_PATH_REMOTE_VER_VER, &info->version); - - if (ret == true) - ret = btif_config_get_int(bdstr, - BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, &info->sub_ver); - } - } break; - - default: - BTIF_TRACE_ERROR("Unknow prop type:%d", prop->type); - return false; + case BT_PROPERTY_REMOTE_FRIENDLY_NAME: { + int len = prop->len; + ret = btif_config_get_str(bdstr, BTIF_STORAGE_PATH_REMOTE_ALIASE, + (char*)prop->val, &len); + if (ret && len && len <= prop->len) + prop->len = len - 1; + else { + prop->len = 0; + ret = false; + } + break; } - return ret; + case BT_PROPERTY_ADAPTER_SCAN_MODE: + if (prop->len >= (int)sizeof(int)) + ret = btif_config_get_int("Adapter", BTIF_STORAGE_KEY_ADAPTER_SCANMODE, + (int*)prop->val); + break; + case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: + if (prop->len >= (int)sizeof(int)) + ret = btif_config_get_int( + "Adapter", BTIF_STORAGE_KEY_ADAPTER_DISC_TIMEOUT, (int*)prop->val); + break; + case BT_PROPERTY_CLASS_OF_DEVICE: + if (prop->len >= (int)sizeof(int)) + ret = btif_config_get_int(bdstr, BTIF_STORAGE_PATH_REMOTE_DEVCLASS, + (int*)prop->val); + break; + case BT_PROPERTY_TYPE_OF_DEVICE: + if (prop->len >= (int)sizeof(int)) + ret = btif_config_get_int(bdstr, BTIF_STORAGE_PATH_REMOTE_DEVTYPE, + (int*)prop->val); + break; + case BT_PROPERTY_UUIDS: { + char value[1280]; + int size = sizeof(value); + if (btif_config_get_str(bdstr, BTIF_STORAGE_PATH_REMOTE_SERVICE, value, + &size)) { + bt_uuid_t* p_uuid = (bt_uuid_t*)prop->val; + size_t num_uuids = + btif_split_uuids_string(value, p_uuid, BT_MAX_NUM_UUIDS); + prop->len = num_uuids * sizeof(bt_uuid_t); + ret = true; + } else { + prop->val = NULL; + prop->len = 0; + } + } break; + + case BT_PROPERTY_REMOTE_VERSION_INFO: { + bt_remote_version_t* info = (bt_remote_version_t*)prop->val; + + if (prop->len >= (int)sizeof(bt_remote_version_t)) { + ret = btif_config_get_int(bdstr, BTIF_STORAGE_PATH_REMOTE_VER_MFCT, + &info->manufacturer); + + if (ret == true) + ret = btif_config_get_int(bdstr, BTIF_STORAGE_PATH_REMOTE_VER_VER, + &info->version); + + if (ret == true) + ret = btif_config_get_int(bdstr, BTIF_STORAGE_PATH_REMOTE_VER_SUBVER, + &info->sub_ver); + } + } break; + + default: + BTIF_TRACE_ERROR("Unknow prop type:%d", prop->type); + return false; + } + return ret; } /******************************************************************************* -** -** Function btif_in_fetch_bonded_devices -** -** Description Internal helper function to fetch the bonded devices -** from NVRAM -** -** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -static bt_status_t btif_in_fetch_bonded_device(const char *bdstr) -{ - bool bt_linkkey_file_found=false; - - LINK_KEY link_key; - size_t size = sizeof(link_key); - if(btif_config_get_bin(bdstr, "LinkKey", (uint8_t *)link_key, &size)) - { - int linkkey_type; - if(btif_config_get_int(bdstr, "LinkKeyType", &linkkey_type)) - { - bt_linkkey_file_found = true; - } - else - { - bt_linkkey_file_found = false; - } - } + * + * Function btif_in_fetch_bonded_devices + * + * Description Internal helper function to fetch the bonded devices + * from NVRAM + * + * Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +static bt_status_t btif_in_fetch_bonded_device(const char* bdstr) { + bool bt_linkkey_file_found = false; + + LINK_KEY link_key; + size_t size = sizeof(link_key); + if (btif_config_get_bin(bdstr, "LinkKey", (uint8_t*)link_key, &size)) { + int linkkey_type; + if (btif_config_get_int(bdstr, "LinkKeyType", &linkkey_type)) { + bt_linkkey_file_found = true; + } else { + bt_linkkey_file_found = false; + } + } #if (BLE_INCLUDED == TRUE) - if((btif_in_fetch_bonded_ble_device(bdstr, false, NULL) != BT_STATUS_SUCCESS) - && (!bt_linkkey_file_found)) - { - BTIF_TRACE_DEBUG("Remote device:%s, no link key or ble key found", bdstr); - return BT_STATUS_FAIL; - } + if ((btif_in_fetch_bonded_ble_device(bdstr, false, NULL) != + BT_STATUS_SUCCESS) && + (!bt_linkkey_file_found)) { + BTIF_TRACE_DEBUG("Remote device:%s, no link key or ble key found", bdstr); + return BT_STATUS_FAIL; + } #else - if((!bt_linkkey_file_found)) - { - BTIF_TRACE_DEBUG("Remote device:%s, no link key found", bdstr); - return BT_STATUS_FAIL; - } + if ((!bt_linkkey_file_found)) { + BTIF_TRACE_DEBUG("Remote device:%s, no link key found", bdstr); + return BT_STATUS_FAIL; + } #endif - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_in_fetch_bonded_devices -** -** Description Internal helper function to fetch the bonded devices -** from NVRAM -** -** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -static bt_status_t btif_in_fetch_bonded_devices(btif_bonded_devices_t *p_bonded_devices, int add) -{ - memset(p_bonded_devices, 0, sizeof(btif_bonded_devices_t)); - - bool bt_linkkey_file_found=false; - int device_type; - - for (const btif_config_section_iter_t *iter = btif_config_section_begin(); iter != btif_config_section_end(); iter = btif_config_section_next(iter)) { - const char *name = btif_config_section_name(iter); - if (!string_is_bdaddr(name)) - continue; - - BTIF_TRACE_DEBUG("Remote device:%s", name); - LINK_KEY link_key; - size_t size = sizeof(link_key); - if (btif_config_get_bin(name, "LinkKey", link_key, &size)) { - int linkkey_type; - if (btif_config_get_int(name, "LinkKeyType", &linkkey_type)) { - bt_bdaddr_t bd_addr; - string_to_bdaddr(name, &bd_addr); - if (add) { - DEV_CLASS dev_class = {0, 0, 0}; - int cod; - int pin_length = 0; - if (btif_config_get_int(name, "DevClass", &cod)) - uint2devclass((uint32_t)cod, dev_class); - btif_config_get_int(name, "PinLength", &pin_length); - BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0, - (uint8_t)linkkey_type, 0, pin_length); + * + * Function btif_in_fetch_bonded_devices + * + * Description Internal helper function to fetch the bonded devices + * from NVRAM + * + * Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +static bt_status_t btif_in_fetch_bonded_devices( + btif_bonded_devices_t* p_bonded_devices, int add) { + memset(p_bonded_devices, 0, sizeof(btif_bonded_devices_t)); + + bool bt_linkkey_file_found = false; + int device_type; + + for (const btif_config_section_iter_t* iter = btif_config_section_begin(); + iter != btif_config_section_end(); + iter = btif_config_section_next(iter)) { + const char* name = btif_config_section_name(iter); + if (!string_is_bdaddr(name)) continue; + + BTIF_TRACE_DEBUG("Remote device:%s", name); + LINK_KEY link_key; + size_t size = sizeof(link_key); + if (btif_config_get_bin(name, "LinkKey", link_key, &size)) { + int linkkey_type; + if (btif_config_get_int(name, "LinkKeyType", &linkkey_type)) { + bt_bdaddr_t bd_addr; + string_to_bdaddr(name, &bd_addr); + if (add) { + DEV_CLASS dev_class = {0, 0, 0}; + int cod; + int pin_length = 0; + if (btif_config_get_int(name, "DevClass", &cod)) + uint2devclass((uint32_t)cod, dev_class); + btif_config_get_int(name, "PinLength", &pin_length); + BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0, + (uint8_t)linkkey_type, 0, pin_length); #if (BLE_INCLUDED == TRUE) - if (btif_config_get_int(name, "DevType", &device_type) && - (device_type == BT_DEVICE_TYPE_DUMO) ) { - btif_gatts_add_bonded_dev_from_nv(bd_addr.address); - } + if (btif_config_get_int(name, "DevType", &device_type) && + (device_type == BT_DEVICE_TYPE_DUMO)) { + btif_gatts_add_bonded_dev_from_nv(bd_addr.address); + } #endif - } - bt_linkkey_file_found = true; - memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t)); - } else { + } + bt_linkkey_file_found = true; + memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], + &bd_addr, sizeof(bt_bdaddr_t)); + } else { #if (BLE_INCLUDED == TRUE) - bt_linkkey_file_found = false; + bt_linkkey_file_found = false; #else - BTIF_TRACE_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid", name); + BTIF_TRACE_ERROR( + "bounded device:%s, LinkKeyType or PinLength is invalid", name); #endif - } - } + } + } #if (BLE_INCLUDED == TRUE) - if (!btif_in_fetch_bonded_ble_device(name, add, p_bonded_devices) && - !bt_linkkey_file_found) { - BTIF_TRACE_DEBUG("Remote device:%s, no link key or ble key found", name); - } + if (!btif_in_fetch_bonded_ble_device(name, add, p_bonded_devices) && + !bt_linkkey_file_found) { + BTIF_TRACE_DEBUG("Remote device:%s, no link key or ble key found", name); + } #else - if(!bt_linkkey_file_found) - BTIF_TRACE_DEBUG("Remote device:%s, no link key", name); + if (!bt_linkkey_file_found) + BTIF_TRACE_DEBUG("Remote device:%s, no link key", name); #endif - } - return BT_STATUS_SUCCESS; + } + return BT_STATUS_SUCCESS; } -static void btif_read_le_key(const uint8_t key_type, const size_t key_len, bt_bdaddr_t bd_addr, - const uint8_t addr_type, const bool add_key, bool *device_added, bool *key_found) -{ - assert(device_added); - assert(key_found); - - char buffer[100]; - memset(buffer, 0, sizeof(buffer)); - - if (btif_storage_get_ble_bonding_key(&bd_addr, key_type, buffer, key_len) == BT_STATUS_SUCCESS) - { - if (add_key) - { - BD_ADDR bta_bd_addr; - bdcpy(bta_bd_addr, bd_addr.address); - - if (!*device_added) - { - BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE); - *device_added = true; - } - - char bd_str[20] = {0}; - BTIF_TRACE_DEBUG("%s() Adding key type %d for %s", __func__, - key_type, bdaddr_to_string(&bd_addr, bd_str, sizeof(bd_str))); - BTA_DmAddBleKey(bta_bd_addr, (tBTA_LE_KEY_VALUE *)buffer, key_type); - } - - *key_found = true; +static void btif_read_le_key(const uint8_t key_type, const size_t key_len, + bt_bdaddr_t bd_addr, const uint8_t addr_type, + const bool add_key, bool* device_added, + bool* key_found) { + assert(device_added); + assert(key_found); + + char buffer[100]; + memset(buffer, 0, sizeof(buffer)); + + if (btif_storage_get_ble_bonding_key(&bd_addr, key_type, buffer, key_len) == + BT_STATUS_SUCCESS) { + if (add_key) { + BD_ADDR bta_bd_addr; + bdcpy(bta_bd_addr, bd_addr.address); + + if (!*device_added) { + BTA_DmAddBleDevice(bta_bd_addr, addr_type, BT_DEVICE_TYPE_BLE); + *device_added = true; + } + + char bd_str[20] = {0}; + BTIF_TRACE_DEBUG("%s() Adding key type %d for %s", __func__, key_type, + bdaddr_to_string(&bd_addr, bd_str, sizeof(bd_str))); + BTA_DmAddBleKey(bta_bd_addr, (tBTA_LE_KEY_VALUE*)buffer, key_type); } + + *key_found = true; + } } /******************************************************************************* @@ -532,910 +523,854 @@ static void btif_read_le_key(const uint8_t key_type, const size_t key_len, bt_bd *******************************************************************************/ /******************************************************************************* -** -** Function btif_split_uuids_string -** -** Description Internal helper function to split the string of UUIDs -** read from the NVRAM to an array -** -** Returns Number of UUIDs parsed from the supplied string -** -*******************************************************************************/ -size_t btif_split_uuids_string(const char *str, bt_uuid_t *p_uuid, size_t max_uuids) -{ - assert(str); - assert(p_uuid); - - size_t num_uuids = 0; - while (str && num_uuids < max_uuids) - { - bool rc = string_to_uuid(str, p_uuid++); - if (!rc) break; - num_uuids++; - str = strchr(str, ' '); - if (str) str++; - } - - return num_uuids; + * + * Function btif_split_uuids_string + * + * Description Internal helper function to split the string of UUIDs + * read from the NVRAM to an array + * + * Returns Number of UUIDs parsed from the supplied string + * + ******************************************************************************/ +size_t btif_split_uuids_string(const char* str, bt_uuid_t* p_uuid, + size_t max_uuids) { + assert(str); + assert(p_uuid); + + size_t num_uuids = 0; + while (str && num_uuids < max_uuids) { + bool rc = string_to_uuid(str, p_uuid++); + if (!rc) break; + num_uuids++; + str = strchr(str, ' '); + if (str) str++; + } + + return num_uuids; } /******************************************************************************* -** -** Function btif_storage_get_adapter_property -** -** Description BTIF storage API - Fetches the adapter property->type -** from NVRAM and fills property->val. -** Caller should provide memory for property->val and -** set the property->val -** -** Returns BT_STATUS_SUCCESS if the fetch was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_get_adapter_property(bt_property_t *property) -{ - - /* Special handling for adapter BD_ADDR and BONDED_DEVICES */ - if (property->type == BT_PROPERTY_BDADDR) - { - bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)property->val; - /* This has been cached in btif. Just fetch it from there */ - memcpy(bd_addr, &btif_local_bd_addr, sizeof(bt_bdaddr_t)); - property->len = sizeof(bt_bdaddr_t); - return BT_STATUS_SUCCESS; - } - else if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES) - { - btif_bonded_devices_t bonded_devices; - - btif_in_fetch_bonded_devices(&bonded_devices, 0); + * + * Function btif_storage_get_adapter_property + * + * Description BTIF storage API - Fetches the adapter property->type + * from NVRAM and fills property->val. + * Caller should provide memory for property->val and + * set the property->val + * + * Returns BT_STATUS_SUCCESS if the fetch was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_get_adapter_property(bt_property_t* property) { + /* Special handling for adapter BD_ADDR and BONDED_DEVICES */ + if (property->type == BT_PROPERTY_BDADDR) { + bt_bdaddr_t* bd_addr = (bt_bdaddr_t*)property->val; + /* This has been cached in btif. Just fetch it from there */ + memcpy(bd_addr, &btif_local_bd_addr, sizeof(bt_bdaddr_t)); + property->len = sizeof(bt_bdaddr_t); + return BT_STATUS_SUCCESS; + } else if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES) { + btif_bonded_devices_t bonded_devices; - BTIF_TRACE_DEBUG("%s: Number of bonded devices: %d Property:BT_PROPERTY_ADAPTER_BONDED_DEVICES", __func__, bonded_devices.num_devices); + btif_in_fetch_bonded_devices(&bonded_devices, 0); - if (bonded_devices.num_devices > 0) - { - property->len = bonded_devices.num_devices * sizeof(bt_bdaddr_t); - memcpy(property->val, bonded_devices.devices, property->len); - } + BTIF_TRACE_DEBUG( + "%s: Number of bonded devices: %d " + "Property:BT_PROPERTY_ADAPTER_BONDED_DEVICES", + __func__, bonded_devices.num_devices); - /* if there are no bonded_devices, then length shall be 0 */ - return BT_STATUS_SUCCESS; - } - else if (property->type == BT_PROPERTY_UUIDS) - { - /* publish list of local supported services */ - bt_uuid_t *p_uuid = (bt_uuid_t*)property->val; - uint32_t num_uuids = 0; - uint32_t i; - - tBTA_SERVICE_MASK service_mask = btif_get_enabled_services_mask(); - LOG_INFO(LOG_TAG, "%s service_mask:0x%x", __func__, service_mask); - for (i=0; i < BTA_MAX_SERVICE_ID; i++) - { - /* This should eventually become a function when more services are enabled */ - if (service_mask - &(tBTA_SERVICE_MASK)(1 << i)) - { - switch (i) - { - case BTA_HFP_SERVICE_ID: - { - uuid16_to_uuid128(UUID_SERVCLASS_AG_HANDSFREE, - p_uuid+num_uuids); - num_uuids++; - } - /* intentional fall through: Send both BFP & HSP UUIDs if HFP is enabled */ - case BTA_HSP_SERVICE_ID: - { - uuid16_to_uuid128(UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY, - p_uuid+num_uuids); - num_uuids++; - }break; - case BTA_A2DP_SOURCE_SERVICE_ID: - { - uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SOURCE, - p_uuid+num_uuids); - num_uuids++; - }break; - case BTA_A2DP_SINK_SERVICE_ID: - { - uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SINK, - p_uuid+num_uuids); - num_uuids++; - }break; - case BTA_HFP_HS_SERVICE_ID: - { - uuid16_to_uuid128(UUID_SERVCLASS_HF_HANDSFREE, - p_uuid+num_uuids); - num_uuids++; - }break; - } - } - } - property->len = (num_uuids)*sizeof(bt_uuid_t); - return BT_STATUS_SUCCESS; + if (bonded_devices.num_devices > 0) { + property->len = bonded_devices.num_devices * sizeof(bt_bdaddr_t); + memcpy(property->val, bonded_devices.devices, property->len); } - /* fall through for other properties */ - if(!cfg2prop(NULL, property)) - { - return btif_dm_get_adapter_property(property); + /* if there are no bonded_devices, then length shall be 0 */ + return BT_STATUS_SUCCESS; + } else if (property->type == BT_PROPERTY_UUIDS) { + /* publish list of local supported services */ + bt_uuid_t* p_uuid = (bt_uuid_t*)property->val; + uint32_t num_uuids = 0; + uint32_t i; + + tBTA_SERVICE_MASK service_mask = btif_get_enabled_services_mask(); + LOG_INFO(LOG_TAG, "%s service_mask:0x%x", __func__, service_mask); + for (i = 0; i < BTA_MAX_SERVICE_ID; i++) { + /* This should eventually become a function when more services are enabled + */ + if (service_mask & (tBTA_SERVICE_MASK)(1 << i)) { + switch (i) { + case BTA_HFP_SERVICE_ID: { + uuid16_to_uuid128(UUID_SERVCLASS_AG_HANDSFREE, p_uuid + num_uuids); + num_uuids++; + } + /* intentional fall through: Send both BFP & HSP UUIDs if HFP is + * enabled */ + case BTA_HSP_SERVICE_ID: { + uuid16_to_uuid128(UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY, + p_uuid + num_uuids); + num_uuids++; + } break; + case BTA_A2DP_SOURCE_SERVICE_ID: { + uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SOURCE, p_uuid + num_uuids); + num_uuids++; + } break; + case BTA_A2DP_SINK_SERVICE_ID: { + uuid16_to_uuid128(UUID_SERVCLASS_AUDIO_SINK, p_uuid + num_uuids); + num_uuids++; + } break; + case BTA_HFP_HS_SERVICE_ID: { + uuid16_to_uuid128(UUID_SERVCLASS_HF_HANDSFREE, p_uuid + num_uuids); + num_uuids++; + } break; + } + } } + property->len = (num_uuids) * sizeof(bt_uuid_t); return BT_STATUS_SUCCESS; - } + } + + /* fall through for other properties */ + if (!cfg2prop(NULL, property)) { + return btif_dm_get_adapter_property(property); + } + return BT_STATUS_SUCCESS; +} /******************************************************************************* -** -** Function btif_storage_set_adapter_property -** -** Description BTIF storage API - Stores the adapter property -** to NVRAM -** -** Returns BT_STATUS_SUCCESS if the store was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_set_adapter_property(bt_property_t *property) -{ - return prop2cfg(NULL, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; + * + * Function btif_storage_set_adapter_property + * + * Description BTIF storage API - Stores the adapter property + * to NVRAM + * + * Returns BT_STATUS_SUCCESS if the store was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_set_adapter_property(bt_property_t* property) { + return prop2cfg(NULL, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } /******************************************************************************* -** -** Function btif_storage_get_remote_device_property -** -** Description BTIF storage API - Fetches the remote device property->type -** from NVRAM and fills property->val. -** Caller should provide memory for property->val and -** set the property->val -** -** Returns BT_STATUS_SUCCESS if the fetch was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_get_remote_device_property(bt_bdaddr_t *remote_bd_addr, - bt_property_t *property) -{ - return cfg2prop(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; + * + * Function btif_storage_get_remote_device_property + * + * Description BTIF storage API - Fetches the remote device property->type + * from NVRAM and fills property->val. + * Caller should provide memory for property->val and + * set the property->val + * + * Returns BT_STATUS_SUCCESS if the fetch was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_get_remote_device_property(bt_bdaddr_t* remote_bd_addr, + bt_property_t* property) { + return cfg2prop(remote_bd_addr, property) ? BT_STATUS_SUCCESS + : BT_STATUS_FAIL; } /******************************************************************************* -** -** Function btif_storage_set_remote_device_property -** -** Description BTIF storage API - Stores the remote device property -** to NVRAM -** -** Returns BT_STATUS_SUCCESS if the store was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_set_remote_device_property(bt_bdaddr_t *remote_bd_addr, - bt_property_t *property) -{ - return prop2cfg(remote_bd_addr, property) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; + * + * Function btif_storage_set_remote_device_property + * + * Description BTIF storage API - Stores the remote device property + * to NVRAM + * + * Returns BT_STATUS_SUCCESS if the store was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_set_remote_device_property(bt_bdaddr_t* remote_bd_addr, + bt_property_t* property) { + return prop2cfg(remote_bd_addr, property) ? BT_STATUS_SUCCESS + : BT_STATUS_FAIL; } /******************************************************************************* -** -** Function btif_storage_add_remote_device -** -** Description BTIF storage API - Adds a newly discovered device to NVRAM -** along with the timestamp. Also, stores the various -** properties - RSSI, BDADDR, NAME (if found in EIR) -** -** Returns BT_STATUS_SUCCESS if the store was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_add_remote_device(bt_bdaddr_t *remote_bd_addr, + * + * Function btif_storage_add_remote_device + * + * Description BTIF storage API - Adds a newly discovered device to NVRAM + * along with the timestamp. Also, stores the various + * properties - RSSI, BDADDR, NAME (if found in EIR) + * + * Returns BT_STATUS_SUCCESS if the store was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_add_remote_device(bt_bdaddr_t* remote_bd_addr, uint32_t num_properties, - bt_property_t *properties) -{ - uint32_t i = 0; - /* TODO: If writing a property, fails do we go back undo the earlier - * written properties? */ - for (i=0; i < num_properties; i++) - { - /* Ignore the RSSI as this is not stored in DB */ - if (properties[i].type == BT_PROPERTY_REMOTE_RSSI) - continue; - - /* BD_ADDR for remote device needs special handling as we also store timestamp */ - if (properties[i].type == BT_PROPERTY_BDADDR) - { - bt_property_t addr_prop; - memcpy(&addr_prop, &properties[i], sizeof(bt_property_t)); - addr_prop.type = (bt_property_type_t) BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP; - btif_storage_set_remote_device_property(remote_bd_addr, - &addr_prop); - } - else - { - btif_storage_set_remote_device_property(remote_bd_addr, - &properties[i]); - } + bt_property_t* properties) { + uint32_t i = 0; + /* TODO: If writing a property, fails do we go back undo the earlier + * written properties? */ + for (i = 0; i < num_properties; i++) { + /* Ignore the RSSI as this is not stored in DB */ + if (properties[i].type == BT_PROPERTY_REMOTE_RSSI) continue; + + /* BD_ADDR for remote device needs special handling as we also store + * timestamp */ + if (properties[i].type == BT_PROPERTY_BDADDR) { + bt_property_t addr_prop; + memcpy(&addr_prop, &properties[i], sizeof(bt_property_t)); + addr_prop.type = (bt_property_type_t)BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP; + btif_storage_set_remote_device_property(remote_bd_addr, &addr_prop); + } else { + btif_storage_set_remote_device_property(remote_bd_addr, &properties[i]); } - return BT_STATUS_SUCCESS; + } + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_storage_add_bonded_device -** -** Description BTIF storage API - Adds the newly bonded device to NVRAM -** along with the link-key, Key type and Pin key length -** -** Returns BT_STATUS_SUCCESS if the store was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ - -bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr, - LINK_KEY link_key, - uint8_t key_type, - uint8_t pin_length) -{ - bdstr_t bdstr; - bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); - int ret = btif_config_set_int(bdstr, "LinkKeyType", (int)key_type); - ret &= btif_config_set_int(bdstr, "PinLength", (int)pin_length); - ret &= btif_config_set_bin(bdstr, "LinkKey", link_key, sizeof(LINK_KEY)); - - if (is_restricted_mode()) { - BTIF_TRACE_WARNING("%s: '%s' pairing will be removed if unrestricted", - __func__, bdstr); - btif_config_set_int(bdstr, "Restricted", 1); - } + * + * Function btif_storage_add_bonded_device + * + * Description BTIF storage API - Adds the newly bonded device to NVRAM + * along with the link-key, Key type and Pin key length + * + * Returns BT_STATUS_SUCCESS if the store was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ - /* write bonded info immediately */ - btif_config_flush(); - return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; +bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t* remote_bd_addr, + LINK_KEY link_key, uint8_t key_type, + uint8_t pin_length) { + bdstr_t bdstr; + bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + int ret = btif_config_set_int(bdstr, "LinkKeyType", (int)key_type); + ret &= btif_config_set_int(bdstr, "PinLength", (int)pin_length); + ret &= btif_config_set_bin(bdstr, "LinkKey", link_key, sizeof(LINK_KEY)); + + if (is_restricted_mode()) { + BTIF_TRACE_WARNING("%s: '%s' pairing will be removed if unrestricted", + __func__, bdstr); + btif_config_set_int(bdstr, "Restricted", 1); + } + + /* write bonded info immediately */ + btif_config_flush(); + return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } /******************************************************************************* -** -** Function btif_storage_remove_bonded_device -** -** Description BTIF storage API - Deletes the bonded device from NVRAM -** -** Returns BT_STATUS_SUCCESS if the deletion was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr) -{ - bdstr_t bdstr; - bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); - BTIF_TRACE_DEBUG("in bd addr:%s", bdstr); + * + * Function btif_storage_remove_bonded_device + * + * Description BTIF storage API - Deletes the bonded device from NVRAM + * + * Returns BT_STATUS_SUCCESS if the deletion was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t* remote_bd_addr) { + bdstr_t bdstr; + bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + BTIF_TRACE_DEBUG("in bd addr:%s", bdstr); #if (BLE_INCLUDED == TRUE) - btif_storage_remove_ble_bonding_keys(remote_bd_addr); + btif_storage_remove_ble_bonding_keys(remote_bd_addr); #endif - int ret = 1; - if(btif_config_exist(bdstr, "LinkKeyType")) - ret &= btif_config_remove(bdstr, "LinkKeyType"); - if(btif_config_exist(bdstr, "PinLength")) - ret &= btif_config_remove(bdstr, "PinLength"); - if(btif_config_exist(bdstr, "LinkKey")) - ret &= btif_config_remove(bdstr, "LinkKey"); - /* write bonded info immediately */ - btif_config_flush(); - return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; - + int ret = 1; + if (btif_config_exist(bdstr, "LinkKeyType")) + ret &= btif_config_remove(bdstr, "LinkKeyType"); + if (btif_config_exist(bdstr, "PinLength")) + ret &= btif_config_remove(bdstr, "PinLength"); + if (btif_config_exist(bdstr, "LinkKey")) + ret &= btif_config_remove(bdstr, "LinkKey"); + /* write bonded info immediately */ + btif_config_flush(); + return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } /******************************************************************************* -** -** Function btif_storage_load_bonded_devices -** -** Description BTIF storage API - Loads all the bonded devices from NVRAM -** and adds to the BTA. -** Additionally, this API also invokes the adaper_properties_cb -** and remote_device_properties_cb for each of the bonded devices. -** -** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_load_bonded_devices(void) -{ - btif_bonded_devices_t bonded_devices; - uint32_t i = 0; - bt_property_t adapter_props[6]; - uint32_t num_props = 0; - bt_property_t remote_properties[8]; - bt_bdaddr_t addr; - bt_bdname_t name, alias; - bt_scan_mode_t mode; - uint32_t disc_timeout; - bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS]; - bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS]; - - btif_in_fetch_bonded_devices(&bonded_devices, 1); - - /* Now send the adapter_properties_cb with all adapter_properties */ - { - memset(adapter_props, 0, sizeof(adapter_props)); - - /* BD_ADDR */ - BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDADDR, &addr, sizeof(addr), - adapter_props[num_props]); - num_props++; - - /* BD_NAME */ - BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDNAME, &name, sizeof(name), - adapter_props[num_props]); - num_props++; - - /* SCAN_MODE */ - /* TODO: At the time of BT on, always report the scan mode as 0 irrespective - of the scan_mode during the previous enable cycle. - This needs to be re-visited as part of the app/stack enable sequence - synchronization */ - mode = BT_SCAN_MODE_NONE; - adapter_props[num_props].type = BT_PROPERTY_ADAPTER_SCAN_MODE; - adapter_props[num_props].len = sizeof(mode); - adapter_props[num_props].val = &mode; - num_props++; - - /* DISC_TIMEOUT */ - BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT, - &disc_timeout, sizeof(disc_timeout), - adapter_props[num_props]); - num_props++; - - /* BONDED_DEVICES */ - bt_bdaddr_t *devices_list = - (bt_bdaddr_t *)osi_malloc(sizeof(bt_bdaddr_t) * bonded_devices.num_devices); - adapter_props[num_props].type = BT_PROPERTY_ADAPTER_BONDED_DEVICES; - adapter_props[num_props].len = bonded_devices.num_devices * sizeof(bt_bdaddr_t); - adapter_props[num_props].val = devices_list; - for (i=0; i < bonded_devices.num_devices; i++) - { - memcpy(devices_list + i, &bonded_devices.devices[i], sizeof(bt_bdaddr_t)); - } - num_props++; - - /* LOCAL UUIDs */ - BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_UUIDS, - local_uuids, sizeof(local_uuids), - adapter_props[num_props]); - num_props++; - - btif_adapter_properties_evt(BT_STATUS_SUCCESS, num_props, adapter_props); - - osi_free(devices_list); + * + * Function btif_storage_load_bonded_devices + * + * Description BTIF storage API - Loads all the bonded devices from NVRAM + * and adds to the BTA. + * Additionally, this API also invokes the adaper_properties_cb + * and remote_device_properties_cb for each of the bonded + *devices. + * + * Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_load_bonded_devices(void) { + btif_bonded_devices_t bonded_devices; + uint32_t i = 0; + bt_property_t adapter_props[6]; + uint32_t num_props = 0; + bt_property_t remote_properties[8]; + bt_bdaddr_t addr; + bt_bdname_t name, alias; + bt_scan_mode_t mode; + uint32_t disc_timeout; + bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS]; + bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS]; + + btif_in_fetch_bonded_devices(&bonded_devices, 1); + + /* Now send the adapter_properties_cb with all adapter_properties */ + { + memset(adapter_props, 0, sizeof(adapter_props)); + + /* BD_ADDR */ + BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDADDR, &addr, sizeof(addr), + adapter_props[num_props]); + num_props++; + + /* BD_NAME */ + BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_BDNAME, &name, sizeof(name), + adapter_props[num_props]); + num_props++; + + /* SCAN_MODE */ + /* TODO: At the time of BT on, always report the scan mode as 0 irrespective + of the scan_mode during the previous enable cycle. + This needs to be re-visited as part of the app/stack enable sequence + synchronization */ + mode = BT_SCAN_MODE_NONE; + adapter_props[num_props].type = BT_PROPERTY_ADAPTER_SCAN_MODE; + adapter_props[num_props].len = sizeof(mode); + adapter_props[num_props].val = &mode; + num_props++; + + /* DISC_TIMEOUT */ + BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT, + &disc_timeout, sizeof(disc_timeout), + adapter_props[num_props]); + num_props++; + + /* BONDED_DEVICES */ + bt_bdaddr_t* devices_list = (bt_bdaddr_t*)osi_malloc( + sizeof(bt_bdaddr_t) * bonded_devices.num_devices); + adapter_props[num_props].type = BT_PROPERTY_ADAPTER_BONDED_DEVICES; + adapter_props[num_props].len = + bonded_devices.num_devices * sizeof(bt_bdaddr_t); + adapter_props[num_props].val = devices_list; + for (i = 0; i < bonded_devices.num_devices; i++) { + memcpy(devices_list + i, &bonded_devices.devices[i], sizeof(bt_bdaddr_t)); } - - BTIF_TRACE_EVENT("%s: %d bonded devices found", __func__, bonded_devices.num_devices); - - { - for (i = 0; i < bonded_devices.num_devices; i++) - { - bt_bdaddr_t *p_remote_addr; - - /* - * TODO: improve handling of missing fields in NVRAM. - */ - uint32_t cod = 0; - uint32_t devtype = 0; - - num_props = 0; - p_remote_addr = &bonded_devices.devices[i]; - memset(remote_properties, 0, sizeof(remote_properties)); - BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_BDNAME, - &name, sizeof(name), - remote_properties[num_props]); - num_props++; - - BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_REMOTE_FRIENDLY_NAME, - &alias, sizeof(alias), - remote_properties[num_props]); - num_props++; - - BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_CLASS_OF_DEVICE, - &cod, sizeof(cod), - remote_properties[num_props]); - num_props++; - - BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_TYPE_OF_DEVICE, - &devtype, sizeof(devtype), - remote_properties[num_props]); - num_props++; - - BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_UUIDS, - remote_uuids, sizeof(remote_uuids), - remote_properties[num_props]); - num_props++; - - btif_remote_properties_evt(BT_STATUS_SUCCESS, p_remote_addr, - num_props, remote_properties); - } + num_props++; + + /* LOCAL UUIDs */ + BTIF_STORAGE_GET_ADAPTER_PROP(BT_PROPERTY_UUIDS, local_uuids, + sizeof(local_uuids), + adapter_props[num_props]); + num_props++; + + btif_adapter_properties_evt(BT_STATUS_SUCCESS, num_props, adapter_props); + + osi_free(devices_list); + } + + BTIF_TRACE_EVENT("%s: %d bonded devices found", __func__, + bonded_devices.num_devices); + + { + for (i = 0; i < bonded_devices.num_devices; i++) { + bt_bdaddr_t* p_remote_addr; + + /* + * TODO: improve handling of missing fields in NVRAM. + */ + uint32_t cod = 0; + uint32_t devtype = 0; + + num_props = 0; + p_remote_addr = &bonded_devices.devices[i]; + memset(remote_properties, 0, sizeof(remote_properties)); + BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_BDNAME, &name, + sizeof(name), remote_properties[num_props]); + num_props++; + + BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, + BT_PROPERTY_REMOTE_FRIENDLY_NAME, &alias, + sizeof(alias), remote_properties[num_props]); + num_props++; + + BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_CLASS_OF_DEVICE, + &cod, sizeof(cod), + remote_properties[num_props]); + num_props++; + + BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_TYPE_OF_DEVICE, + &devtype, sizeof(devtype), + remote_properties[num_props]); + num_props++; + + BTIF_STORAGE_GET_REMOTE_PROP(p_remote_addr, BT_PROPERTY_UUIDS, + remote_uuids, sizeof(remote_uuids), + remote_properties[num_props]); + num_props++; + + btif_remote_properties_evt(BT_STATUS_SUCCESS, p_remote_addr, num_props, + remote_properties); } - return BT_STATUS_SUCCESS; + } + return BT_STATUS_SUCCESS; } #if (BLE_INCLUDED == TRUE) /******************************************************************************* -** -** Function btif_storage_add_ble_bonding_key -** -** Description BTIF storage API - Adds the newly bonded device to NVRAM -** along with the ble-key, Key type and Pin key length -** -** Returns BT_STATUS_SUCCESS if the store was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ - -bt_status_t btif_storage_add_ble_bonding_key(bt_bdaddr_t *remote_bd_addr, - char *key, - uint8_t key_type, - uint8_t key_length) -{ - bdstr_t bdstr; - bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); - const char* name; - switch(key_type) - { - case BTIF_DM_LE_KEY_PENC: - name = "LE_KEY_PENC"; - break; - case BTIF_DM_LE_KEY_PID: - name = "LE_KEY_PID"; - break; - case BTIF_DM_LE_KEY_PCSRK: - name = "LE_KEY_PCSRK"; - break; - case BTIF_DM_LE_KEY_LENC: - name = "LE_KEY_LENC"; - break; - case BTIF_DM_LE_KEY_LCSRK: - name = "LE_KEY_LCSRK"; - break; - case BTIF_DM_LE_KEY_LID: - name = "LE_KEY_LID"; - break; - default: - return BT_STATUS_FAIL; - } - int ret = btif_config_set_bin(bdstr, name, (const uint8_t *)key, key_length); - btif_config_save(); - return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; + * + * Function btif_storage_add_ble_bonding_key + * + * Description BTIF storage API - Adds the newly bonded device to NVRAM + * along with the ble-key, Key type and Pin key length + * + * Returns BT_STATUS_SUCCESS if the store was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ + +bt_status_t btif_storage_add_ble_bonding_key(bt_bdaddr_t* remote_bd_addr, + char* key, uint8_t key_type, + uint8_t key_length) { + bdstr_t bdstr; + bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + const char* name; + switch (key_type) { + case BTIF_DM_LE_KEY_PENC: + name = "LE_KEY_PENC"; + break; + case BTIF_DM_LE_KEY_PID: + name = "LE_KEY_PID"; + break; + case BTIF_DM_LE_KEY_PCSRK: + name = "LE_KEY_PCSRK"; + break; + case BTIF_DM_LE_KEY_LENC: + name = "LE_KEY_LENC"; + break; + case BTIF_DM_LE_KEY_LCSRK: + name = "LE_KEY_LCSRK"; + break; + case BTIF_DM_LE_KEY_LID: + name = "LE_KEY_LID"; + break; + default: + return BT_STATUS_FAIL; + } + int ret = btif_config_set_bin(bdstr, name, (const uint8_t*)key, key_length); + btif_config_save(); + return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } /******************************************************************************* -** -** Function btif_storage_get_ble_bonding_key -** -** Description -** -** Returns BT_STATUS_SUCCESS if the fetch was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr, - uint8_t key_type, - char *key_value, - int key_length) -{ - bdstr_t bdstr; - bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); - const char* name; - switch(key_type) - { - case BTIF_DM_LE_KEY_PENC: - name = "LE_KEY_PENC"; - break; - case BTIF_DM_LE_KEY_PID: - name = "LE_KEY_PID"; - break; - case BTIF_DM_LE_KEY_PCSRK: - name = "LE_KEY_PCSRK"; - break; - case BTIF_DM_LE_KEY_LENC: - name = "LE_KEY_LENC"; - break; - case BTIF_DM_LE_KEY_LCSRK: - name = "LE_KEY_LCSRK"; - break; - case BTIF_DM_LE_KEY_LID: - name = "LE_KEY_LID"; - default: - return BT_STATUS_FAIL; - } - size_t length = key_length; - int ret = btif_config_get_bin(bdstr, name, (uint8_t *)key_value, &length); - return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; - + * + * Function btif_storage_get_ble_bonding_key + * + * Description + * + * Returns BT_STATUS_SUCCESS if the fetch was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_get_ble_bonding_key(bt_bdaddr_t* remote_bd_addr, + uint8_t key_type, char* key_value, + int key_length) { + bdstr_t bdstr; + bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + const char* name; + switch (key_type) { + case BTIF_DM_LE_KEY_PENC: + name = "LE_KEY_PENC"; + break; + case BTIF_DM_LE_KEY_PID: + name = "LE_KEY_PID"; + break; + case BTIF_DM_LE_KEY_PCSRK: + name = "LE_KEY_PCSRK"; + break; + case BTIF_DM_LE_KEY_LENC: + name = "LE_KEY_LENC"; + break; + case BTIF_DM_LE_KEY_LCSRK: + name = "LE_KEY_LCSRK"; + break; + case BTIF_DM_LE_KEY_LID: + name = "LE_KEY_LID"; + default: + return BT_STATUS_FAIL; + } + size_t length = key_length; + int ret = btif_config_get_bin(bdstr, name, (uint8_t*)key_value, &length); + return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } /******************************************************************************* -** -** Function btif_storage_remove_ble_keys -** -** Description BTIF storage API - Deletes the bonded device from NVRAM -** -** Returns BT_STATUS_SUCCESS if the deletion was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr) -{ - bdstr_t bdstr; - bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); - BTIF_TRACE_DEBUG(" %s in bd addr:%s",__func__, bdstr); - int ret = 1; - if(btif_config_exist(bdstr, "LE_KEY_PENC")) - ret &= btif_config_remove(bdstr, "LE_KEY_PENC"); - if(btif_config_exist(bdstr, "LE_KEY_PID")) - ret &= btif_config_remove(bdstr, "LE_KEY_PID"); - if(btif_config_exist(bdstr, "LE_KEY_PCSRK")) - ret &= btif_config_remove(bdstr, "LE_KEY_PCSRK"); - if(btif_config_exist(bdstr, "LE_KEY_LENC")) - ret &= btif_config_remove(bdstr, "LE_KEY_LENC"); - if(btif_config_exist(bdstr, "LE_KEY_LCSRK")) - ret &= btif_config_remove(bdstr, "LE_KEY_LCSRK"); - btif_config_save(); - return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; + * + * Function btif_storage_remove_ble_keys + * + * Description BTIF storage API - Deletes the bonded device from NVRAM + * + * Returns BT_STATUS_SUCCESS if the deletion was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_remove_ble_bonding_keys(bt_bdaddr_t* remote_bd_addr) { + bdstr_t bdstr; + bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + BTIF_TRACE_DEBUG(" %s in bd addr:%s", __func__, bdstr); + int ret = 1; + if (btif_config_exist(bdstr, "LE_KEY_PENC")) + ret &= btif_config_remove(bdstr, "LE_KEY_PENC"); + if (btif_config_exist(bdstr, "LE_KEY_PID")) + ret &= btif_config_remove(bdstr, "LE_KEY_PID"); + if (btif_config_exist(bdstr, "LE_KEY_PCSRK")) + ret &= btif_config_remove(bdstr, "LE_KEY_PCSRK"); + if (btif_config_exist(bdstr, "LE_KEY_LENC")) + ret &= btif_config_remove(bdstr, "LE_KEY_LENC"); + if (btif_config_exist(bdstr, "LE_KEY_LCSRK")) + ret &= btif_config_remove(bdstr, "LE_KEY_LCSRK"); + btif_config_save(); + return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } /******************************************************************************* -** -** Function btif_storage_add_ble_local_key -** -** Description BTIF storage API - Adds the ble key to NVRAM -** -** Returns BT_STATUS_SUCCESS if the store was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_add_ble_local_key(char *key, - uint8_t key_type, - uint8_t key_length) -{ - const char* name; - switch(key_type) - { - case BTIF_DM_LE_LOCAL_KEY_IR: - name = "LE_LOCAL_KEY_IR"; - break; - case BTIF_DM_LE_LOCAL_KEY_IRK: - name = "LE_LOCAL_KEY_IRK"; - break; - case BTIF_DM_LE_LOCAL_KEY_DHK: - name = "LE_LOCAL_KEY_DHK"; - break; - case BTIF_DM_LE_LOCAL_KEY_ER: - name = "LE_LOCAL_KEY_ER"; - break; - default: - return BT_STATUS_FAIL; - } - int ret = btif_config_set_bin("Adapter", name, (const uint8_t *)key, key_length); - btif_config_save(); - return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; + * + * Function btif_storage_add_ble_local_key + * + * Description BTIF storage API - Adds the ble key to NVRAM + * + * Returns BT_STATUS_SUCCESS if the store was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_add_ble_local_key(char* key, uint8_t key_type, + uint8_t key_length) { + const char* name; + switch (key_type) { + case BTIF_DM_LE_LOCAL_KEY_IR: + name = "LE_LOCAL_KEY_IR"; + break; + case BTIF_DM_LE_LOCAL_KEY_IRK: + name = "LE_LOCAL_KEY_IRK"; + break; + case BTIF_DM_LE_LOCAL_KEY_DHK: + name = "LE_LOCAL_KEY_DHK"; + break; + case BTIF_DM_LE_LOCAL_KEY_ER: + name = "LE_LOCAL_KEY_ER"; + break; + default: + return BT_STATUS_FAIL; + } + int ret = + btif_config_set_bin("Adapter", name, (const uint8_t*)key, key_length); + btif_config_save(); + return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } /******************************************************************************* -** -** Function btif_storage_get_ble_local_key -** -** Description -** -** Returns BT_STATUS_SUCCESS if the fetch was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_get_ble_local_key(uint8_t key_type, - char *key_value, - int key_length) -{ - const char* name; - switch(key_type) - { - case BTIF_DM_LE_LOCAL_KEY_IR: - name = "LE_LOCAL_KEY_IR"; - break; - case BTIF_DM_LE_LOCAL_KEY_IRK: - name = "LE_LOCAL_KEY_IRK"; - break; - case BTIF_DM_LE_LOCAL_KEY_DHK: - name = "LE_LOCAL_KEY_DHK"; - break; - case BTIF_DM_LE_LOCAL_KEY_ER: - name = "LE_LOCAL_KEY_ER"; - break; - default: - return BT_STATUS_FAIL; - } - size_t length = key_length; - int ret = btif_config_get_bin("Adapter", name, (uint8_t *)key_value, &length); - return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; + * + * Function btif_storage_get_ble_local_key + * + * Description + * + * Returns BT_STATUS_SUCCESS if the fetch was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_get_ble_local_key(uint8_t key_type, char* key_value, + int key_length) { + const char* name; + switch (key_type) { + case BTIF_DM_LE_LOCAL_KEY_IR: + name = "LE_LOCAL_KEY_IR"; + break; + case BTIF_DM_LE_LOCAL_KEY_IRK: + name = "LE_LOCAL_KEY_IRK"; + break; + case BTIF_DM_LE_LOCAL_KEY_DHK: + name = "LE_LOCAL_KEY_DHK"; + break; + case BTIF_DM_LE_LOCAL_KEY_ER: + name = "LE_LOCAL_KEY_ER"; + break; + default: + return BT_STATUS_FAIL; + } + size_t length = key_length; + int ret = btif_config_get_bin("Adapter", name, (uint8_t*)key_value, &length); + return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } /******************************************************************************* -** -** Function btif_storage_remove_ble_local_keys -** -** Description BTIF storage API - Deletes the bonded device from NVRAM -** -** Returns BT_STATUS_SUCCESS if the deletion was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_remove_ble_local_keys(void) -{ - int ret = 1; - if(btif_config_exist("Adapter", "LE_LOCAL_KEY_IR")) - ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_IR"); - if(btif_config_exist("Adapter", "LE_LOCAL_KEY_IRK")) - ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_IRK"); - if(btif_config_exist("Adapter", "LE_LOCAL_KEY_DHK")) - ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_DHK"); - if(btif_config_exist("Adapter", "LE_LOCAL_KEY_ER")) - ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_ER"); - btif_config_save(); - return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; + * + * Function btif_storage_remove_ble_local_keys + * + * Description BTIF storage API - Deletes the bonded device from NVRAM + * + * Returns BT_STATUS_SUCCESS if the deletion was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_remove_ble_local_keys(void) { + int ret = 1; + if (btif_config_exist("Adapter", "LE_LOCAL_KEY_IR")) + ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_IR"); + if (btif_config_exist("Adapter", "LE_LOCAL_KEY_IRK")) + ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_IRK"); + if (btif_config_exist("Adapter", "LE_LOCAL_KEY_DHK")) + ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_DHK"); + if (btif_config_exist("Adapter", "LE_LOCAL_KEY_ER")) + ret &= btif_config_remove("Adapter", "LE_LOCAL_KEY_ER"); + btif_config_save(); + return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } -static bt_status_t btif_in_fetch_bonded_ble_device(const char *remote_bd_addr, int add, btif_bonded_devices_t *p_bonded_devices) -{ - int device_type; - int addr_type; - bt_bdaddr_t bd_addr; - BD_ADDR bta_bd_addr; - bool device_added = false; - bool key_found = false; - - if (!btif_config_get_int(remote_bd_addr, "DevType", &device_type)) - return BT_STATUS_FAIL; - - if ((device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE || - btif_has_ble_keys(remote_bd_addr)) - { - BTIF_TRACE_DEBUG("%s Found a LE device: %s", __func__, remote_bd_addr); - - string_to_bdaddr(remote_bd_addr, &bd_addr); - bdcpy(bta_bd_addr, bd_addr.address); - - if (btif_storage_get_remote_addr_type(&bd_addr, &addr_type) != BT_STATUS_SUCCESS) - { - addr_type = BLE_ADDR_PUBLIC; - btif_storage_set_remote_addr_type(&bd_addr, BLE_ADDR_PUBLIC); - } +static bt_status_t btif_in_fetch_bonded_ble_device( + const char* remote_bd_addr, int add, + btif_bonded_devices_t* p_bonded_devices) { + int device_type; + int addr_type; + bt_bdaddr_t bd_addr; + BD_ADDR bta_bd_addr; + bool device_added = false; + bool key_found = false; + + if (!btif_config_get_int(remote_bd_addr, "DevType", &device_type)) + return BT_STATUS_FAIL; - btif_read_le_key(BTIF_DM_LE_KEY_PENC, sizeof(tBTM_LE_PENC_KEYS), - bd_addr, addr_type, add, &device_added, &key_found); + if ((device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE || + btif_has_ble_keys(remote_bd_addr)) { + BTIF_TRACE_DEBUG("%s Found a LE device: %s", __func__, remote_bd_addr); - btif_read_le_key(BTIF_DM_LE_KEY_PID, sizeof(tBTM_LE_PID_KEYS), - bd_addr, addr_type, add, &device_added, &key_found); + string_to_bdaddr(remote_bd_addr, &bd_addr); + bdcpy(bta_bd_addr, bd_addr.address); - btif_read_le_key(BTIF_DM_LE_KEY_LID, sizeof(tBTM_LE_PID_KEYS), - bd_addr, addr_type, add, &device_added, &key_found); + if (btif_storage_get_remote_addr_type(&bd_addr, &addr_type) != + BT_STATUS_SUCCESS) { + addr_type = BLE_ADDR_PUBLIC; + btif_storage_set_remote_addr_type(&bd_addr, BLE_ADDR_PUBLIC); + } - btif_read_le_key(BTIF_DM_LE_KEY_PCSRK, sizeof(tBTM_LE_PCSRK_KEYS), - bd_addr, addr_type, add, &device_added, &key_found); + btif_read_le_key(BTIF_DM_LE_KEY_PENC, sizeof(tBTM_LE_PENC_KEYS), bd_addr, + addr_type, add, &device_added, &key_found); - btif_read_le_key(BTIF_DM_LE_KEY_LENC, sizeof(tBTM_LE_LENC_KEYS), - bd_addr, addr_type, add, &device_added, &key_found); + btif_read_le_key(BTIF_DM_LE_KEY_PID, sizeof(tBTM_LE_PID_KEYS), bd_addr, + addr_type, add, &device_added, &key_found); - btif_read_le_key(BTIF_DM_LE_KEY_LCSRK, sizeof(tBTM_LE_LCSRK_KEYS), - bd_addr, addr_type, add, &device_added, &key_found); + btif_read_le_key(BTIF_DM_LE_KEY_LID, sizeof(tBTM_LE_PID_KEYS), bd_addr, + addr_type, add, &device_added, &key_found); - // Fill in the bonded devices - if (device_added) - { - memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], &bd_addr, sizeof(bt_bdaddr_t)); - btif_gatts_add_bonded_dev_from_nv(bta_bd_addr); - } + btif_read_le_key(BTIF_DM_LE_KEY_PCSRK, sizeof(tBTM_LE_PCSRK_KEYS), bd_addr, + addr_type, add, &device_added, &key_found); + + btif_read_le_key(BTIF_DM_LE_KEY_LENC, sizeof(tBTM_LE_LENC_KEYS), bd_addr, + addr_type, add, &device_added, &key_found); - if (key_found) - return BT_STATUS_SUCCESS; + btif_read_le_key(BTIF_DM_LE_KEY_LCSRK, sizeof(tBTM_LE_LCSRK_KEYS), bd_addr, + addr_type, add, &device_added, &key_found); + + // Fill in the bonded devices + if (device_added) { + memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], + &bd_addr, sizeof(bt_bdaddr_t)); + btif_gatts_add_bonded_dev_from_nv(bta_bd_addr); } - return BT_STATUS_FAIL; + + if (key_found) return BT_STATUS_SUCCESS; + } + return BT_STATUS_FAIL; } -bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr, - uint8_t addr_type) -{ - bdstr_t bdstr; - bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); - int ret = btif_config_set_int(bdstr, "AddrType", (int)addr_type); - return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; +bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t* remote_bd_addr, + uint8_t addr_type) { + bdstr_t bdstr; + bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + int ret = btif_config_set_int(bdstr, "AddrType", (int)addr_type); + return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } -bool btif_has_ble_keys(const char *bdstr) -{ - return btif_config_exist(bdstr, "LE_KEY_PENC"); +bool btif_has_ble_keys(const char* bdstr) { + return btif_config_exist(bdstr, "LE_KEY_PENC"); } /******************************************************************************* -** -** Function btif_storage_get_remote_addr_type -** -** Description BTIF storage API - Fetches the remote addr type -** -** Returns BT_STATUS_SUCCESS if the fetch was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr, - int*addr_type) -{ - bdstr_t bdstr; - bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); - int ret = btif_config_get_int(bdstr, "AddrType", addr_type); - return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; + * + * Function btif_storage_get_remote_addr_type + * + * Description BTIF storage API - Fetches the remote addr type + * + * Returns BT_STATUS_SUCCESS if the fetch was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t* remote_bd_addr, + int* addr_type) { + bdstr_t bdstr; + bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + int ret = btif_config_get_int(bdstr, "AddrType", addr_type); + return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } #endif /******************************************************************************* -** -** Function btif_storage_add_hid_device_info -** -** Description BTIF storage API - Adds the hid information of bonded hid devices-to NVRAM -** -** Returns BT_STATUS_SUCCESS if the store was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ - -bt_status_t btif_storage_add_hid_device_info(bt_bdaddr_t *remote_bd_addr, - uint16_t attr_mask, uint8_t sub_class, - uint8_t app_id, uint16_t vendor_id, - uint16_t product_id, uint16_t version, - uint8_t ctry_code, uint16_t ssr_max_latency, - uint16_t ssr_min_tout, uint16_t dl_len, uint8_t *dsc_list) -{ - bdstr_t bdstr; - BTIF_TRACE_DEBUG("btif_storage_add_hid_device_info:"); - bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); - btif_config_set_int(bdstr, "HidAttrMask", attr_mask); - btif_config_set_int(bdstr, "HidSubClass", sub_class); - btif_config_set_int(bdstr, "HidAppId", app_id); - btif_config_set_int(bdstr, "HidVendorId", vendor_id); - btif_config_set_int(bdstr, "HidProductId", product_id); - btif_config_set_int(bdstr, "HidVersion", version); - btif_config_set_int(bdstr, "HidCountryCode", ctry_code); - btif_config_set_int(bdstr, "HidSSRMaxLatency", ssr_max_latency); - btif_config_set_int(bdstr, "HidSSRMinTimeout", ssr_min_tout); - if(dl_len > 0) - btif_config_set_bin(bdstr, "HidDescriptor", dsc_list, dl_len); - btif_config_save(); - return BT_STATUS_SUCCESS; + * + * Function btif_storage_add_hid_device_info + * + * Description BTIF storage API - Adds the hid information of bonded hid + *devices-to NVRAM + * + * Returns BT_STATUS_SUCCESS if the store was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ + +bt_status_t btif_storage_add_hid_device_info( + bt_bdaddr_t* remote_bd_addr, uint16_t attr_mask, uint8_t sub_class, + uint8_t app_id, uint16_t vendor_id, uint16_t product_id, uint16_t version, + uint8_t ctry_code, uint16_t ssr_max_latency, uint16_t ssr_min_tout, + uint16_t dl_len, uint8_t* dsc_list) { + bdstr_t bdstr; + BTIF_TRACE_DEBUG("btif_storage_add_hid_device_info:"); + bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + btif_config_set_int(bdstr, "HidAttrMask", attr_mask); + btif_config_set_int(bdstr, "HidSubClass", sub_class); + btif_config_set_int(bdstr, "HidAppId", app_id); + btif_config_set_int(bdstr, "HidVendorId", vendor_id); + btif_config_set_int(bdstr, "HidProductId", product_id); + btif_config_set_int(bdstr, "HidVersion", version); + btif_config_set_int(bdstr, "HidCountryCode", ctry_code); + btif_config_set_int(bdstr, "HidSSRMaxLatency", ssr_max_latency); + btif_config_set_int(bdstr, "HidSSRMinTimeout", ssr_min_tout); + if (dl_len > 0) btif_config_set_bin(bdstr, "HidDescriptor", dsc_list, dl_len); + btif_config_save(); + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_storage_load_bonded_hid_info -** -** Description BTIF storage API - Loads hid info for all the bonded devices from NVRAM -** and adds those devices to the BTA_HH. -** -** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_load_bonded_hid_info(void) -{ - bt_bdaddr_t bd_addr; - tBTA_HH_DEV_DSCP_INFO dscp_info; - uint16_t attr_mask; - uint8_t sub_class; - uint8_t app_id; - - memset(&dscp_info, 0, sizeof(dscp_info)); - for (const btif_config_section_iter_t *iter = btif_config_section_begin(); iter != btif_config_section_end(); iter = btif_config_section_next(iter)) { - const char *name = btif_config_section_name(iter); - if (!string_is_bdaddr(name)) - continue; - - BTIF_TRACE_DEBUG("Remote device:%s", name); - int value; - if(btif_in_fetch_bonded_device(name) == BT_STATUS_SUCCESS) - { - if(btif_config_get_int(name, "HidAttrMask", &value)) - { - attr_mask = (uint16_t)value; - - btif_config_get_int(name, "HidSubClass", &value); - sub_class = (uint8_t)value; - - btif_config_get_int(name, "HidAppId", &value); - app_id = (uint8_t)value; - - btif_config_get_int(name, "HidVendorId", &value); - dscp_info.vendor_id = (uint16_t) value; - - btif_config_get_int(name, "HidProductId", &value); - dscp_info.product_id = (uint16_t) value; - - btif_config_get_int(name, "HidVersion", &value); - dscp_info.version = (uint8_t) value; - - btif_config_get_int(name, "HidCountryCode", &value); - dscp_info.ctry_code = (uint8_t) value; - - value = 0; - btif_config_get_int(name, "HidSSRMaxLatency", &value); - dscp_info.ssr_max_latency = (uint16_t) value; - - value = 0; - btif_config_get_int(name, "HidSSRMinTimeout", &value); - dscp_info.ssr_min_tout = (uint16_t) value; - - size_t len = btif_config_get_bin_length(name, "HidDescriptor"); - if(len > 0) - { - dscp_info.descriptor.dl_len = (uint16_t)len; - dscp_info.descriptor.dsc_list = (uint8_t*)alloca(len); - btif_config_get_bin(name, "HidDescriptor", (uint8_t *)dscp_info.descriptor.dsc_list, &len); - } - string_to_bdaddr(name, &bd_addr); - // add extracted information to BTA HH - if (btif_hh_add_added_dev(bd_addr,attr_mask)) - { - BTA_HhAddDev(bd_addr.address, attr_mask, sub_class, - app_id, dscp_info); - } - } + * + * Function btif_storage_load_bonded_hid_info + * + * Description BTIF storage API - Loads hid info for all the bonded devices + *from NVRAM + * and adds those devices to the BTA_HH. + * + * Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_load_bonded_hid_info(void) { + bt_bdaddr_t bd_addr; + tBTA_HH_DEV_DSCP_INFO dscp_info; + uint16_t attr_mask; + uint8_t sub_class; + uint8_t app_id; + + memset(&dscp_info, 0, sizeof(dscp_info)); + for (const btif_config_section_iter_t* iter = btif_config_section_begin(); + iter != btif_config_section_end(); + iter = btif_config_section_next(iter)) { + const char* name = btif_config_section_name(iter); + if (!string_is_bdaddr(name)) continue; + + BTIF_TRACE_DEBUG("Remote device:%s", name); + int value; + if (btif_in_fetch_bonded_device(name) == BT_STATUS_SUCCESS) { + if (btif_config_get_int(name, "HidAttrMask", &value)) { + attr_mask = (uint16_t)value; + + btif_config_get_int(name, "HidSubClass", &value); + sub_class = (uint8_t)value; + + btif_config_get_int(name, "HidAppId", &value); + app_id = (uint8_t)value; + + btif_config_get_int(name, "HidVendorId", &value); + dscp_info.vendor_id = (uint16_t)value; + + btif_config_get_int(name, "HidProductId", &value); + dscp_info.product_id = (uint16_t)value; + + btif_config_get_int(name, "HidVersion", &value); + dscp_info.version = (uint8_t)value; + + btif_config_get_int(name, "HidCountryCode", &value); + dscp_info.ctry_code = (uint8_t)value; + + value = 0; + btif_config_get_int(name, "HidSSRMaxLatency", &value); + dscp_info.ssr_max_latency = (uint16_t)value; + + value = 0; + btif_config_get_int(name, "HidSSRMinTimeout", &value); + dscp_info.ssr_min_tout = (uint16_t)value; + + size_t len = btif_config_get_bin_length(name, "HidDescriptor"); + if (len > 0) { + dscp_info.descriptor.dl_len = (uint16_t)len; + dscp_info.descriptor.dsc_list = (uint8_t*)alloca(len); + btif_config_get_bin(name, "HidDescriptor", + (uint8_t*)dscp_info.descriptor.dsc_list, &len); } - else - { - if(btif_config_get_int(name, "HidAttrMask", &value)) - { - btif_storage_remove_hid_info(&bd_addr); - string_to_bdaddr(name, &bd_addr); - } + string_to_bdaddr(name, &bd_addr); + // add extracted information to BTA HH + if (btif_hh_add_added_dev(bd_addr, attr_mask)) { + BTA_HhAddDev(bd_addr.address, attr_mask, sub_class, app_id, + dscp_info); } + } + } else { + if (btif_config_get_int(name, "HidAttrMask", &value)) { + btif_storage_remove_hid_info(&bd_addr); + string_to_bdaddr(name, &bd_addr); + } } + } - return BT_STATUS_SUCCESS; + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_storage_remove_hid_info -** -** Description BTIF storage API - Deletes the bonded hid device info from NVRAM -** -** Returns BT_STATUS_SUCCESS if the deletion was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t *remote_bd_addr) -{ - bdstr_t bdstr; - bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); - - btif_config_remove(bdstr, "HidAttrMask"); - btif_config_remove(bdstr, "HidSubClass"); - btif_config_remove(bdstr, "HidAppId"); - btif_config_remove(bdstr, "HidVendorId"); - btif_config_remove(bdstr, "HidProductId"); - btif_config_remove(bdstr, "HidVersion"); - btif_config_remove(bdstr, "HidCountryCode"); - btif_config_remove(bdstr, "HidSSRMaxLatency"); - btif_config_remove(bdstr, "HidSSRMinTimeout"); - btif_config_remove(bdstr, "HidDescriptor"); - btif_config_save(); - return BT_STATUS_SUCCESS; + * + * Function btif_storage_remove_hid_info + * + * Description BTIF storage API - Deletes the bonded hid device info from + *NVRAM + * + * Returns BT_STATUS_SUCCESS if the deletion was successful, + * BT_STATUS_FAIL otherwise + * + ******************************************************************************/ +bt_status_t btif_storage_remove_hid_info(bt_bdaddr_t* remote_bd_addr) { + bdstr_t bdstr; + bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + + btif_config_remove(bdstr, "HidAttrMask"); + btif_config_remove(bdstr, "HidSubClass"); + btif_config_remove(bdstr, "HidAppId"); + btif_config_remove(bdstr, "HidVendorId"); + btif_config_remove(bdstr, "HidProductId"); + btif_config_remove(bdstr, "HidVersion"); + btif_config_remove(bdstr, "HidCountryCode"); + btif_config_remove(bdstr, "HidSSRMaxLatency"); + btif_config_remove(bdstr, "HidSSRMinTimeout"); + btif_config_remove(bdstr, "HidDescriptor"); + btif_config_save(); + return BT_STATUS_SUCCESS; } /******************************************************************************* -** -** Function btif_storage_is_restricted_device -** -** Description BTIF storage API - checks if this device is a restricted device -** -** Returns true if the device is labeled as restricted -** false otherwise -** -*******************************************************************************/ -bool btif_storage_is_restricted_device(const bt_bdaddr_t *remote_bd_addr) -{ - bdstr_t bdstr; - bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); - - return btif_config_exist(bdstr, "Restricted"); + * + * Function btif_storage_is_restricted_device + * + * Description BTIF storage API - checks if this device is a restricted + *device + * + * Returns true if the device is labeled as restricted + * false otherwise + * + ******************************************************************************/ +bool btif_storage_is_restricted_device(const bt_bdaddr_t* remote_bd_addr) { + bdstr_t bdstr; + bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + + return btif_config_exist(bdstr, "Restricted"); } diff --git a/btif/src/btif_uid.cc b/btif/src/btif_uid.cc index 8f99d5485..d30cdadc5 100644 --- a/btif/src/btif_uid.cc +++ b/btif/src/btif_uid.cc @@ -28,95 +28,96 @@ #include "btif_uid.h" typedef struct uid_set_node_t { - struct uid_set_node_t* next; - bt_uid_traffic_t data; + struct uid_set_node_t* next; + bt_uid_traffic_t data; } uid_set_node_t; typedef struct uid_set_t { - std::mutex lock; - uid_set_node_t* head; + std::mutex lock; + uid_set_node_t* head; } uid_set_t; uid_set_t* uid_set_create(void) { - uid_set_t* set = (uid_set_t*)osi_calloc(sizeof(uid_set_t)); - return set; + uid_set_t* set = (uid_set_t*)osi_calloc(sizeof(uid_set_t)); + return set; } void uid_set_destroy(uid_set_t* set) { - std::unique_lock lock(set->lock); - uid_set_node_t* node = set->head; - while (node) { - uid_set_node_t* temp = node; - node = node->next; - osi_free(temp); - } - set->head = NULL; - osi_free(set); + std::unique_lock lock(set->lock); + uid_set_node_t* node = set->head; + while (node) { + uid_set_node_t* temp = node; + node = node->next; + osi_free(temp); + } + set->head = NULL; + osi_free(set); } // Lock in uid_set_t must be held. -static uid_set_node_t* uid_set_find_or_create_node(uid_set_t* set, int32_t app_uid) { - uid_set_node_t* node = set->head; - while (node && node->data.app_uid != app_uid) { - node = node->next; - } - - if (!node) { - node = (uid_set_node_t*)osi_calloc(sizeof(uid_set_node_t)); - node->data.app_uid = app_uid; - node->next = set->head; - set->head = node; - } - return node; +static uid_set_node_t* uid_set_find_or_create_node(uid_set_t* set, + int32_t app_uid) { + uid_set_node_t* node = set->head; + while (node && node->data.app_uid != app_uid) { + node = node->next; + } + + if (!node) { + node = (uid_set_node_t*)osi_calloc(sizeof(uid_set_node_t)); + node->data.app_uid = app_uid; + node->next = set->head; + set->head = node; + } + return node; } void uid_set_add_tx(uid_set_t* set, int32_t app_uid, uint64_t bytes) { - if (app_uid == -1 || bytes == 0) - return; + if (app_uid == -1 || bytes == 0) return; - std::unique_lock lock(set->lock); - uid_set_node_t* node = uid_set_find_or_create_node(set, app_uid); - node->data.tx_bytes += bytes; + std::unique_lock lock(set->lock); + uid_set_node_t* node = uid_set_find_or_create_node(set, app_uid); + node->data.tx_bytes += bytes; } void uid_set_add_rx(uid_set_t* set, int32_t app_uid, uint64_t bytes) { - if (app_uid == -1 || bytes == 0) - return; + if (app_uid == -1 || bytes == 0) return; - std::unique_lock lock(set->lock); - uid_set_node_t* node = uid_set_find_or_create_node(set, app_uid); - node->data.rx_bytes += bytes; + std::unique_lock lock(set->lock); + uid_set_node_t* node = uid_set_find_or_create_node(set, app_uid); + node->data.rx_bytes += bytes; } bt_uid_traffic_t* uid_set_read_and_clear(uid_set_t* set) { - std::unique_lock lock(set->lock); - - // Find the length - size_t len = 0; - uid_set_node_t* node = set->head; - while (node) { - len++; - node = node->next; - } - - // Allocate an array of elements + 1, to signify the end with app_uid set to -1. - bt_uid_traffic_t* result = (bt_uid_traffic_t*)osi_calloc(sizeof(bt_uid_traffic_t) * (len + 1)); - - bt_uid_traffic_t* data = result; - node = set->head; - while (node) { - // Copy the data. - *data = node->data; - data++; - - // Clear the counters. - node->data.rx_bytes = 0; - node->data.tx_bytes = 0; - node = node->next; - } - - // Mark the last entry - data->app_uid = -1; - - return result; + std::unique_lock lock(set->lock); + + // Find the length + size_t len = 0; + uid_set_node_t* node = set->head; + while (node) { + len++; + node = node->next; + } + + // Allocate an array of elements + 1, to signify the end with app_uid set to + // -1. + bt_uid_traffic_t* result = + (bt_uid_traffic_t*)osi_calloc(sizeof(bt_uid_traffic_t) * (len + 1)); + + bt_uid_traffic_t* data = result; + node = set->head; + while (node) { + // Copy the data. + *data = node->data; + data++; + + // Clear the counters. + node->data.rx_bytes = 0; + node->data.tx_bytes = 0; + node = node->next; + } + + // Mark the last entry + data->app_uid = -1; + + return result; } diff --git a/btif/src/btif_util.cc b/btif/src/btif_util.cc index 0cdc055d1..5c60e6ee7 100644 --- a/btif/src/btif_util.cc +++ b/btif/src/btif_util.cc @@ -40,6 +40,7 @@ #include #include "avrc_defs.h" +#include "bt_common.h" #include "bta_ag_api.h" #include "bta_api.h" #include "bta_av_api.h" @@ -49,509 +50,466 @@ #include "btif_common.h" #include "btif_dm.h" #include "btu.h" -#include "bt_common.h" /************************************************************************************ -** Constants & Macros -************************************************************************************/ -#define ISDIGIT(a) (((a)>='0') && ((a)<='9')) -#define ISXDIGIT(a) ((((a)>='0') && ((a)<='9'))||(((a)>='A') && ((a)<='F'))||(((a)>='a') && ((a)<='f'))) + * Constants & Macros + ***********************************************************************************/ +#define ISDIGIT(a) (((a) >= '0') && ((a) <= '9')) +#define ISXDIGIT(a) \ + ((((a) >= '0') && ((a) <= '9')) || (((a) >= 'A') && ((a) <= 'F')) || \ + (((a) >= 'a') && ((a) <= 'f'))) /************************************************************************************ -** Local type definitions -************************************************************************************/ + * Local type definitions + ***********************************************************************************/ /************************************************************************************ -** Static variables -************************************************************************************/ + * Static variables + ***********************************************************************************/ /************************************************************************************ -** Static functions -************************************************************************************/ + * Static functions + ***********************************************************************************/ /************************************************************************************ -** Externs -************************************************************************************/ + * Externs + ***********************************************************************************/ /************************************************************************************ -** Functions -************************************************************************************/ + * Functions + ***********************************************************************************/ /***************************************************************************** -** Logging helper functions -*****************************************************************************/ - -uint32_t devclass2uint(DEV_CLASS dev_class) -{ - uint32_t cod = 0; - - if(dev_class != NULL) - { - /* if COD is 0, irrespective of the device type set it to Unclassified device */ - cod = (dev_class[2]) | (dev_class[1] << 8) | (dev_class[0] << 16); - } - return cod; + * Logging helper functions + ****************************************************************************/ + +uint32_t devclass2uint(DEV_CLASS dev_class) { + uint32_t cod = 0; + + if (dev_class != NULL) { + /* if COD is 0, irrespective of the device type set it to Unclassified + * device */ + cod = (dev_class[2]) | (dev_class[1] << 8) | (dev_class[0] << 16); + } + return cod; } -void uint2devclass(uint32_t cod, DEV_CLASS dev_class) -{ - dev_class[2] = (uint8_t)cod; - dev_class[1] = (uint8_t)(cod >> 8); - dev_class[0] = (uint8_t)(cod >> 16); +void uint2devclass(uint32_t cod, DEV_CLASS dev_class) { + dev_class[2] = (uint8_t)cod; + dev_class[1] = (uint8_t)(cod >> 8); + dev_class[0] = (uint8_t)(cod >> 16); } -static const uint8_t sdp_base_uuid[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; +static const uint8_t sdp_base_uuid[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, + 0x5F, 0x9B, 0x34, 0xFB}; -void uuid16_to_uuid128(uint16_t uuid16, bt_uuid_t* uuid128) -{ - uint16_t uuid16_bo; - memset(uuid128, 0, sizeof(bt_uuid_t)); +void uuid16_to_uuid128(uint16_t uuid16, bt_uuid_t* uuid128) { + uint16_t uuid16_bo; + memset(uuid128, 0, sizeof(bt_uuid_t)); - memcpy(uuid128->uu, sdp_base_uuid, MAX_UUID_SIZE); - uuid16_bo = ntohs(uuid16); - memcpy(uuid128->uu + 2, &uuid16_bo, sizeof(uint16_t)); + memcpy(uuid128->uu, sdp_base_uuid, MAX_UUID_SIZE); + uuid16_bo = ntohs(uuid16); + memcpy(uuid128->uu + 2, &uuid16_bo, sizeof(uint16_t)); } -bool string_to_uuid(const char *str, bt_uuid_t *p_uuid) -{ - assert(p_uuid); - if (str == NULL) - return false; - - uint32_t uuid0, uuid4; - uint16_t uuid1, uuid2, uuid3, uuid5; - - int rc = sscanf(str, "%08x-%04hx-%04hx-%04hx-%08x%04hx", - &uuid0, &uuid1, &uuid2, &uuid3, &uuid4, &uuid5); - if (rc != 6) - return false; - - uuid0 = htonl(uuid0); - uuid1 = htons(uuid1); - uuid2 = htons(uuid2); - uuid3 = htons(uuid3); - uuid4 = htonl(uuid4); - uuid5 = htons(uuid5); - - memcpy(&(p_uuid->uu[0]), &uuid0, 4); - memcpy(&(p_uuid->uu[4]), &uuid1, 2); - memcpy(&(p_uuid->uu[6]), &uuid2, 2); - memcpy(&(p_uuid->uu[8]), &uuid3, 2); - memcpy(&(p_uuid->uu[10]), &uuid4, 4); - memcpy(&(p_uuid->uu[14]), &uuid5, 2); - - return true; +bool string_to_uuid(const char* str, bt_uuid_t* p_uuid) { + assert(p_uuid); + if (str == NULL) return false; + + uint32_t uuid0, uuid4; + uint16_t uuid1, uuid2, uuid3, uuid5; + + int rc = sscanf(str, "%08x-%04hx-%04hx-%04hx-%08x%04hx", &uuid0, &uuid1, + &uuid2, &uuid3, &uuid4, &uuid5); + if (rc != 6) return false; + + uuid0 = htonl(uuid0); + uuid1 = htons(uuid1); + uuid2 = htons(uuid2); + uuid3 = htons(uuid3); + uuid4 = htonl(uuid4); + uuid5 = htons(uuid5); + + memcpy(&(p_uuid->uu[0]), &uuid0, 4); + memcpy(&(p_uuid->uu[4]), &uuid1, 2); + memcpy(&(p_uuid->uu[6]), &uuid2, 2); + memcpy(&(p_uuid->uu[8]), &uuid3, 2); + memcpy(&(p_uuid->uu[10]), &uuid4, 4); + memcpy(&(p_uuid->uu[14]), &uuid5, 2); + + return true; } -void uuid_to_string_legacy(bt_uuid_t *p_uuid, char *str, size_t str_len) -{ - uint32_t uuid0, uuid4; - uint16_t uuid1, uuid2, uuid3, uuid5; - - memcpy(&uuid0, &(p_uuid->uu[0]), 4); - memcpy(&uuid1, &(p_uuid->uu[4]), 2); - memcpy(&uuid2, &(p_uuid->uu[6]), 2); - memcpy(&uuid3, &(p_uuid->uu[8]), 2); - memcpy(&uuid4, &(p_uuid->uu[10]), 4); - memcpy(&uuid5, &(p_uuid->uu[14]), 2); - - snprintf(str, str_len, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x", - ntohl(uuid0), ntohs(uuid1), - ntohs(uuid2), ntohs(uuid3), - ntohl(uuid4), ntohs(uuid5)); - return; +void uuid_to_string_legacy(bt_uuid_t* p_uuid, char* str, size_t str_len) { + uint32_t uuid0, uuid4; + uint16_t uuid1, uuid2, uuid3, uuid5; + + memcpy(&uuid0, &(p_uuid->uu[0]), 4); + memcpy(&uuid1, &(p_uuid->uu[4]), 2); + memcpy(&uuid2, &(p_uuid->uu[6]), 2); + memcpy(&uuid3, &(p_uuid->uu[8]), 2); + memcpy(&uuid4, &(p_uuid->uu[10]), 4); + memcpy(&uuid5, &(p_uuid->uu[14]), 2); + + snprintf(str, str_len, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x", ntohl(uuid0), + ntohs(uuid1), ntohs(uuid2), ntohs(uuid3), ntohl(uuid4), + ntohs(uuid5)); + return; } /***************************************************************************** -** Function ascii_2_hex -** -** Description This function converts an ASCII string into HEX -** -** Returns the number of hex bytes filled. + * Function ascii_2_hex + * + * Description This function converts an ASCII string into HEX + * + * Returns the number of hex bytes filled. */ -int ascii_2_hex (const char *p_ascii, int len, uint8_t *p_hex) -{ - int x; - uint8_t c; - - for (x = 0; (x < len) && (*p_ascii); x++) - { - if (ISDIGIT (*p_ascii)) - c = (*p_ascii - '0') << 4; - else - c = (toupper(*p_ascii) - 'A' + 10) << 4; - - p_ascii++; - if (*p_ascii) - { - if (ISDIGIT (*p_ascii)) - c |= (*p_ascii - '0'); - else - c |= (toupper(*p_ascii) - 'A' + 10); - - p_ascii++; - } - *p_hex++ = c; +int ascii_2_hex(const char* p_ascii, int len, uint8_t* p_hex) { + int x; + uint8_t c; + + for (x = 0; (x < len) && (*p_ascii); x++) { + if (ISDIGIT(*p_ascii)) + c = (*p_ascii - '0') << 4; + else + c = (toupper(*p_ascii) - 'A' + 10) << 4; + + p_ascii++; + if (*p_ascii) { + if (ISDIGIT(*p_ascii)) + c |= (*p_ascii - '0'); + else + c |= (toupper(*p_ascii) - 'A' + 10); + + p_ascii++; } + *p_hex++ = c; + } - return (x); + return (x); } -const char* dump_dm_search_event(uint16_t event) -{ - switch(event) - { - CASE_RETURN_STR(BTA_DM_INQ_RES_EVT) - CASE_RETURN_STR(BTA_DM_INQ_CMPL_EVT) - CASE_RETURN_STR(BTA_DM_DISC_RES_EVT) - CASE_RETURN_STR(BTA_DM_DISC_BLE_RES_EVT) - CASE_RETURN_STR(BTA_DM_DISC_CMPL_EVT) - CASE_RETURN_STR(BTA_DM_DI_DISC_CMPL_EVT) - CASE_RETURN_STR(BTA_DM_SEARCH_CANCEL_CMPL_EVT) - - default: - return "UNKNOWN MSG ID"; - } +const char* dump_dm_search_event(uint16_t event) { + switch (event) { + CASE_RETURN_STR(BTA_DM_INQ_RES_EVT) + CASE_RETURN_STR(BTA_DM_INQ_CMPL_EVT) + CASE_RETURN_STR(BTA_DM_DISC_RES_EVT) + CASE_RETURN_STR(BTA_DM_DISC_BLE_RES_EVT) + CASE_RETURN_STR(BTA_DM_DISC_CMPL_EVT) + CASE_RETURN_STR(BTA_DM_DI_DISC_CMPL_EVT) + CASE_RETURN_STR(BTA_DM_SEARCH_CANCEL_CMPL_EVT) + + default: + return "UNKNOWN MSG ID"; + } } -const char* dump_property_type(bt_property_type_t type) -{ - switch(type) - { - CASE_RETURN_STR(BT_PROPERTY_BDNAME) - CASE_RETURN_STR(BT_PROPERTY_BDADDR) - CASE_RETURN_STR(BT_PROPERTY_UUIDS) - CASE_RETURN_STR(BT_PROPERTY_CLASS_OF_DEVICE) - CASE_RETURN_STR(BT_PROPERTY_TYPE_OF_DEVICE) - CASE_RETURN_STR(BT_PROPERTY_REMOTE_RSSI) - CASE_RETURN_STR(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT) - CASE_RETURN_STR(BT_PROPERTY_ADAPTER_BONDED_DEVICES) - CASE_RETURN_STR(BT_PROPERTY_ADAPTER_SCAN_MODE) - CASE_RETURN_STR(BT_PROPERTY_REMOTE_FRIENDLY_NAME) - - default: - return "UNKNOWN PROPERTY ID"; - } +const char* dump_property_type(bt_property_type_t type) { + switch (type) { + CASE_RETURN_STR(BT_PROPERTY_BDNAME) + CASE_RETURN_STR(BT_PROPERTY_BDADDR) + CASE_RETURN_STR(BT_PROPERTY_UUIDS) + CASE_RETURN_STR(BT_PROPERTY_CLASS_OF_DEVICE) + CASE_RETURN_STR(BT_PROPERTY_TYPE_OF_DEVICE) + CASE_RETURN_STR(BT_PROPERTY_REMOTE_RSSI) + CASE_RETURN_STR(BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT) + CASE_RETURN_STR(BT_PROPERTY_ADAPTER_BONDED_DEVICES) + CASE_RETURN_STR(BT_PROPERTY_ADAPTER_SCAN_MODE) + CASE_RETURN_STR(BT_PROPERTY_REMOTE_FRIENDLY_NAME) + + default: + return "UNKNOWN PROPERTY ID"; + } } -const char* dump_dm_event(uint16_t event) -{ - switch(event) - { - CASE_RETURN_STR(BTA_DM_ENABLE_EVT) - CASE_RETURN_STR(BTA_DM_DISABLE_EVT) - CASE_RETURN_STR(BTA_DM_PIN_REQ_EVT) - CASE_RETURN_STR(BTA_DM_AUTH_CMPL_EVT) - CASE_RETURN_STR(BTA_DM_AUTHORIZE_EVT) - CASE_RETURN_STR(BTA_DM_LINK_UP_EVT) - CASE_RETURN_STR(BTA_DM_LINK_DOWN_EVT) - CASE_RETURN_STR(BTA_DM_SIG_STRENGTH_EVT) - CASE_RETURN_STR(BTA_DM_BUSY_LEVEL_EVT) - CASE_RETURN_STR(BTA_DM_BOND_CANCEL_CMPL_EVT) - CASE_RETURN_STR(BTA_DM_SP_CFM_REQ_EVT) - CASE_RETURN_STR(BTA_DM_SP_KEY_NOTIF_EVT) - CASE_RETURN_STR(BTA_DM_SP_RMT_OOB_EVT) - CASE_RETURN_STR(BTA_DM_SP_KEYPRESS_EVT) - CASE_RETURN_STR(BTA_DM_ROLE_CHG_EVT) - CASE_RETURN_STR(BTA_DM_BLE_KEY_EVT) - CASE_RETURN_STR(BTA_DM_BLE_SEC_REQ_EVT) - CASE_RETURN_STR(BTA_DM_BLE_PASSKEY_NOTIF_EVT) - CASE_RETURN_STR(BTA_DM_BLE_PASSKEY_REQ_EVT) - CASE_RETURN_STR(BTA_DM_BLE_OOB_REQ_EVT) - CASE_RETURN_STR(BTA_DM_BLE_LOCAL_IR_EVT) - CASE_RETURN_STR(BTA_DM_BLE_LOCAL_ER_EVT) - CASE_RETURN_STR(BTA_DM_BLE_AUTH_CMPL_EVT) - CASE_RETURN_STR(BTA_DM_DEV_UNPAIRED_EVT) - CASE_RETURN_STR(BTA_DM_HW_ERROR_EVT) - CASE_RETURN_STR(BTA_DM_ENER_INFO_READ) - - default: - return "UNKNOWN DM EVENT"; - } +const char* dump_dm_event(uint16_t event) { + switch (event) { + CASE_RETURN_STR(BTA_DM_ENABLE_EVT) + CASE_RETURN_STR(BTA_DM_DISABLE_EVT) + CASE_RETURN_STR(BTA_DM_PIN_REQ_EVT) + CASE_RETURN_STR(BTA_DM_AUTH_CMPL_EVT) + CASE_RETURN_STR(BTA_DM_AUTHORIZE_EVT) + CASE_RETURN_STR(BTA_DM_LINK_UP_EVT) + CASE_RETURN_STR(BTA_DM_LINK_DOWN_EVT) + CASE_RETURN_STR(BTA_DM_SIG_STRENGTH_EVT) + CASE_RETURN_STR(BTA_DM_BUSY_LEVEL_EVT) + CASE_RETURN_STR(BTA_DM_BOND_CANCEL_CMPL_EVT) + CASE_RETURN_STR(BTA_DM_SP_CFM_REQ_EVT) + CASE_RETURN_STR(BTA_DM_SP_KEY_NOTIF_EVT) + CASE_RETURN_STR(BTA_DM_SP_RMT_OOB_EVT) + CASE_RETURN_STR(BTA_DM_SP_KEYPRESS_EVT) + CASE_RETURN_STR(BTA_DM_ROLE_CHG_EVT) + CASE_RETURN_STR(BTA_DM_BLE_KEY_EVT) + CASE_RETURN_STR(BTA_DM_BLE_SEC_REQ_EVT) + CASE_RETURN_STR(BTA_DM_BLE_PASSKEY_NOTIF_EVT) + CASE_RETURN_STR(BTA_DM_BLE_PASSKEY_REQ_EVT) + CASE_RETURN_STR(BTA_DM_BLE_OOB_REQ_EVT) + CASE_RETURN_STR(BTA_DM_BLE_LOCAL_IR_EVT) + CASE_RETURN_STR(BTA_DM_BLE_LOCAL_ER_EVT) + CASE_RETURN_STR(BTA_DM_BLE_AUTH_CMPL_EVT) + CASE_RETURN_STR(BTA_DM_DEV_UNPAIRED_EVT) + CASE_RETURN_STR(BTA_DM_HW_ERROR_EVT) + CASE_RETURN_STR(BTA_DM_ENER_INFO_READ) + + default: + return "UNKNOWN DM EVENT"; + } } -const char* dump_hf_event(uint16_t event) -{ - switch(event) - { - CASE_RETURN_STR(BTA_AG_ENABLE_EVT) - CASE_RETURN_STR(BTA_AG_REGISTER_EVT) - CASE_RETURN_STR(BTA_AG_OPEN_EVT) - CASE_RETURN_STR(BTA_AG_CLOSE_EVT) - CASE_RETURN_STR(BTA_AG_CONN_EVT) - CASE_RETURN_STR(BTA_AG_AUDIO_OPEN_EVT) - CASE_RETURN_STR(BTA_AG_AUDIO_CLOSE_EVT) - CASE_RETURN_STR(BTA_AG_SPK_EVT) - CASE_RETURN_STR(BTA_AG_MIC_EVT) - CASE_RETURN_STR(BTA_AG_AT_CKPD_EVT) - CASE_RETURN_STR(BTA_AG_DISABLE_EVT) +const char* dump_hf_event(uint16_t event) { + switch (event) { + CASE_RETURN_STR(BTA_AG_ENABLE_EVT) + CASE_RETURN_STR(BTA_AG_REGISTER_EVT) + CASE_RETURN_STR(BTA_AG_OPEN_EVT) + CASE_RETURN_STR(BTA_AG_CLOSE_EVT) + CASE_RETURN_STR(BTA_AG_CONN_EVT) + CASE_RETURN_STR(BTA_AG_AUDIO_OPEN_EVT) + CASE_RETURN_STR(BTA_AG_AUDIO_CLOSE_EVT) + CASE_RETURN_STR(BTA_AG_SPK_EVT) + CASE_RETURN_STR(BTA_AG_MIC_EVT) + CASE_RETURN_STR(BTA_AG_AT_CKPD_EVT) + CASE_RETURN_STR(BTA_AG_DISABLE_EVT) #if (BTM_WBS_INCLUDED == TRUE) - CASE_RETURN_STR(BTA_AG_WBS_EVT) + CASE_RETURN_STR(BTA_AG_WBS_EVT) #endif - CASE_RETURN_STR(BTA_AG_AT_A_EVT) - CASE_RETURN_STR(BTA_AG_AT_D_EVT) - CASE_RETURN_STR(BTA_AG_AT_CHLD_EVT) - CASE_RETURN_STR(BTA_AG_AT_CHUP_EVT) - CASE_RETURN_STR(BTA_AG_AT_CIND_EVT) - CASE_RETURN_STR(BTA_AG_AT_VTS_EVT) - CASE_RETURN_STR(BTA_AG_AT_BINP_EVT) - CASE_RETURN_STR(BTA_AG_AT_BLDN_EVT) - CASE_RETURN_STR(BTA_AG_AT_BVRA_EVT) - CASE_RETURN_STR(BTA_AG_AT_NREC_EVT) - CASE_RETURN_STR(BTA_AG_AT_CNUM_EVT) - CASE_RETURN_STR(BTA_AG_AT_BTRH_EVT) - CASE_RETURN_STR(BTA_AG_AT_CLCC_EVT) - CASE_RETURN_STR(BTA_AG_AT_COPS_EVT) - CASE_RETURN_STR(BTA_AG_AT_UNAT_EVT) - CASE_RETURN_STR(BTA_AG_AT_CBC_EVT) - CASE_RETURN_STR(BTA_AG_AT_BAC_EVT) - CASE_RETURN_STR(BTA_AG_AT_BCS_EVT) - CASE_RETURN_STR(BTA_AG_AT_BIND_EVT) - CASE_RETURN_STR(BTA_AG_AT_BIEV_EVT) - - default: - return "UNKNOWN MSG ID"; - } + CASE_RETURN_STR(BTA_AG_AT_A_EVT) + CASE_RETURN_STR(BTA_AG_AT_D_EVT) + CASE_RETURN_STR(BTA_AG_AT_CHLD_EVT) + CASE_RETURN_STR(BTA_AG_AT_CHUP_EVT) + CASE_RETURN_STR(BTA_AG_AT_CIND_EVT) + CASE_RETURN_STR(BTA_AG_AT_VTS_EVT) + CASE_RETURN_STR(BTA_AG_AT_BINP_EVT) + CASE_RETURN_STR(BTA_AG_AT_BLDN_EVT) + CASE_RETURN_STR(BTA_AG_AT_BVRA_EVT) + CASE_RETURN_STR(BTA_AG_AT_NREC_EVT) + CASE_RETURN_STR(BTA_AG_AT_CNUM_EVT) + CASE_RETURN_STR(BTA_AG_AT_BTRH_EVT) + CASE_RETURN_STR(BTA_AG_AT_CLCC_EVT) + CASE_RETURN_STR(BTA_AG_AT_COPS_EVT) + CASE_RETURN_STR(BTA_AG_AT_UNAT_EVT) + CASE_RETURN_STR(BTA_AG_AT_CBC_EVT) + CASE_RETURN_STR(BTA_AG_AT_BAC_EVT) + CASE_RETURN_STR(BTA_AG_AT_BCS_EVT) + CASE_RETURN_STR(BTA_AG_AT_BIND_EVT) + CASE_RETURN_STR(BTA_AG_AT_BIEV_EVT) + + default: + return "UNKNOWN MSG ID"; + } } -const char* dump_hf_client_event(uint16_t event) -{ - switch(event) - { - CASE_RETURN_STR(BTA_HF_CLIENT_ENABLE_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_REGISTER_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_OPEN_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_CLOSE_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_CONN_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_AUDIO_OPEN_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_AUDIO_MSBC_OPEN_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_AUDIO_CLOSE_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_SPK_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_MIC_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_DISABLE_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_IND_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_VOICE_REC_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_OPERATOR_NAME_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_CLIP_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_CCWA_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_AT_RESULT_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_CLCC_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_CNUM_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_BTRH_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_BSIR_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_BINP_EVT) - CASE_RETURN_STR(BTA_HF_CLIENT_RING_INDICATION) - default: - return "UNKNOWN MSG ID"; - } +const char* dump_hf_client_event(uint16_t event) { + switch (event) { + CASE_RETURN_STR(BTA_HF_CLIENT_ENABLE_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_REGISTER_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_OPEN_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_CLOSE_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_CONN_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_AUDIO_OPEN_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_AUDIO_MSBC_OPEN_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_AUDIO_CLOSE_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_SPK_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_MIC_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_DISABLE_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_IND_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_VOICE_REC_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_OPERATOR_NAME_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_CLIP_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_CCWA_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_AT_RESULT_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_CLCC_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_CNUM_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_BTRH_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_BSIR_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_BINP_EVT) + CASE_RETURN_STR(BTA_HF_CLIENT_RING_INDICATION) + default: + return "UNKNOWN MSG ID"; + } } -const char* dump_hh_event(uint16_t event) -{ - switch(event) - { - CASE_RETURN_STR(BTA_HH_ENABLE_EVT) - CASE_RETURN_STR(BTA_HH_DISABLE_EVT) - CASE_RETURN_STR(BTA_HH_OPEN_EVT) - CASE_RETURN_STR(BTA_HH_CLOSE_EVT) - CASE_RETURN_STR(BTA_HH_GET_DSCP_EVT) - CASE_RETURN_STR(BTA_HH_GET_PROTO_EVT) - CASE_RETURN_STR(BTA_HH_GET_RPT_EVT) - CASE_RETURN_STR(BTA_HH_GET_IDLE_EVT) - CASE_RETURN_STR(BTA_HH_SET_PROTO_EVT) - CASE_RETURN_STR(BTA_HH_SET_RPT_EVT) - CASE_RETURN_STR(BTA_HH_SET_IDLE_EVT) - CASE_RETURN_STR(BTA_HH_VC_UNPLUG_EVT) - CASE_RETURN_STR(BTA_HH_ADD_DEV_EVT) - CASE_RETURN_STR(BTA_HH_RMV_DEV_EVT) - CASE_RETURN_STR(BTA_HH_API_ERR_EVT) - default: - return "UNKNOWN MSG ID"; - } +const char* dump_hh_event(uint16_t event) { + switch (event) { + CASE_RETURN_STR(BTA_HH_ENABLE_EVT) + CASE_RETURN_STR(BTA_HH_DISABLE_EVT) + CASE_RETURN_STR(BTA_HH_OPEN_EVT) + CASE_RETURN_STR(BTA_HH_CLOSE_EVT) + CASE_RETURN_STR(BTA_HH_GET_DSCP_EVT) + CASE_RETURN_STR(BTA_HH_GET_PROTO_EVT) + CASE_RETURN_STR(BTA_HH_GET_RPT_EVT) + CASE_RETURN_STR(BTA_HH_GET_IDLE_EVT) + CASE_RETURN_STR(BTA_HH_SET_PROTO_EVT) + CASE_RETURN_STR(BTA_HH_SET_RPT_EVT) + CASE_RETURN_STR(BTA_HH_SET_IDLE_EVT) + CASE_RETURN_STR(BTA_HH_VC_UNPLUG_EVT) + CASE_RETURN_STR(BTA_HH_ADD_DEV_EVT) + CASE_RETURN_STR(BTA_HH_RMV_DEV_EVT) + CASE_RETURN_STR(BTA_HH_API_ERR_EVT) + default: + return "UNKNOWN MSG ID"; + } } -const char* dump_hf_conn_state(uint16_t event) -{ - switch(event) - { - CASE_RETURN_STR(BTHF_CONNECTION_STATE_DISCONNECTED) - CASE_RETURN_STR(BTHF_CONNECTION_STATE_CONNECTING) - CASE_RETURN_STR(BTHF_CONNECTION_STATE_CONNECTED) - CASE_RETURN_STR(BTHF_CONNECTION_STATE_SLC_CONNECTED) - CASE_RETURN_STR(BTHF_CONNECTION_STATE_DISCONNECTING) - default: - return "UNKNOWN MSG ID"; - } +const char* dump_hf_conn_state(uint16_t event) { + switch (event) { + CASE_RETURN_STR(BTHF_CONNECTION_STATE_DISCONNECTED) + CASE_RETURN_STR(BTHF_CONNECTION_STATE_CONNECTING) + CASE_RETURN_STR(BTHF_CONNECTION_STATE_CONNECTED) + CASE_RETURN_STR(BTHF_CONNECTION_STATE_SLC_CONNECTED) + CASE_RETURN_STR(BTHF_CONNECTION_STATE_DISCONNECTING) + default: + return "UNKNOWN MSG ID"; + } } -const char* dump_hf_call_state(bthf_call_state_t call_state) -{ - switch(call_state) - { - CASE_RETURN_STR(BTHF_CALL_STATE_IDLE) - CASE_RETURN_STR(BTHF_CALL_STATE_HELD) - CASE_RETURN_STR(BTHF_CALL_STATE_DIALING) - CASE_RETURN_STR(BTHF_CALL_STATE_ALERTING) - CASE_RETURN_STR(BTHF_CALL_STATE_INCOMING) - CASE_RETURN_STR(BTHF_CALL_STATE_WAITING) - CASE_RETURN_STR(BTHF_CALL_STATE_ACTIVE) - default: - return "UNKNOWN CALL STATE"; - } +const char* dump_hf_call_state(bthf_call_state_t call_state) { + switch (call_state) { + CASE_RETURN_STR(BTHF_CALL_STATE_IDLE) + CASE_RETURN_STR(BTHF_CALL_STATE_HELD) + CASE_RETURN_STR(BTHF_CALL_STATE_DIALING) + CASE_RETURN_STR(BTHF_CALL_STATE_ALERTING) + CASE_RETURN_STR(BTHF_CALL_STATE_INCOMING) + CASE_RETURN_STR(BTHF_CALL_STATE_WAITING) + CASE_RETURN_STR(BTHF_CALL_STATE_ACTIVE) + default: + return "UNKNOWN CALL STATE"; + } } -const char* dump_thread_evt(bt_cb_thread_evt evt) -{ - switch(evt) - { - CASE_RETURN_STR(ASSOCIATE_JVM) - CASE_RETURN_STR(DISASSOCIATE_JVM) +const char* dump_thread_evt(bt_cb_thread_evt evt) { + switch (evt) { + CASE_RETURN_STR(ASSOCIATE_JVM) + CASE_RETURN_STR(DISASSOCIATE_JVM) - default: - return "unknown thread evt"; - } + default: + return "unknown thread evt"; + } } -const char* dump_hf_audio_state(uint16_t event) -{ - switch(event) - { - CASE_RETURN_STR(BTHF_AUDIO_STATE_DISCONNECTED) - CASE_RETURN_STR(BTHF_AUDIO_STATE_CONNECTING) - CASE_RETURN_STR(BTHF_AUDIO_STATE_CONNECTED) - CASE_RETURN_STR(BTHF_AUDIO_STATE_DISCONNECTING) - default: - return "UNKNOWN MSG ID"; - - } +const char* dump_hf_audio_state(uint16_t event) { + switch (event) { + CASE_RETURN_STR(BTHF_AUDIO_STATE_DISCONNECTED) + CASE_RETURN_STR(BTHF_AUDIO_STATE_CONNECTING) + CASE_RETURN_STR(BTHF_AUDIO_STATE_CONNECTED) + CASE_RETURN_STR(BTHF_AUDIO_STATE_DISCONNECTING) + default: + return "UNKNOWN MSG ID"; + } } -const char* dump_av_conn_state(uint16_t event) -{ - switch(event) - { - CASE_RETURN_STR(BTAV_CONNECTION_STATE_DISCONNECTED) - CASE_RETURN_STR(BTAV_CONNECTION_STATE_CONNECTING) - CASE_RETURN_STR(BTAV_CONNECTION_STATE_CONNECTED) - CASE_RETURN_STR(BTAV_CONNECTION_STATE_DISCONNECTING) - default: - return "UNKNOWN MSG ID"; - } +const char* dump_av_conn_state(uint16_t event) { + switch (event) { + CASE_RETURN_STR(BTAV_CONNECTION_STATE_DISCONNECTED) + CASE_RETURN_STR(BTAV_CONNECTION_STATE_CONNECTING) + CASE_RETURN_STR(BTAV_CONNECTION_STATE_CONNECTED) + CASE_RETURN_STR(BTAV_CONNECTION_STATE_DISCONNECTING) + default: + return "UNKNOWN MSG ID"; + } } -const char* dump_av_audio_state(uint16_t event) -{ - switch(event) - { - CASE_RETURN_STR(BTAV_AUDIO_STATE_REMOTE_SUSPEND) - CASE_RETURN_STR(BTAV_AUDIO_STATE_STOPPED) - CASE_RETURN_STR(BTAV_AUDIO_STATE_STARTED) - default: - return "UNKNOWN MSG ID"; - } +const char* dump_av_audio_state(uint16_t event) { + switch (event) { + CASE_RETURN_STR(BTAV_AUDIO_STATE_REMOTE_SUSPEND) + CASE_RETURN_STR(BTAV_AUDIO_STATE_STOPPED) + CASE_RETURN_STR(BTAV_AUDIO_STATE_STARTED) + default: + return "UNKNOWN MSG ID"; + } } -const char* dump_adapter_scan_mode(bt_scan_mode_t mode) -{ - switch(mode) - { - CASE_RETURN_STR(BT_SCAN_MODE_NONE) - CASE_RETURN_STR(BT_SCAN_MODE_CONNECTABLE) - CASE_RETURN_STR(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE) +const char* dump_adapter_scan_mode(bt_scan_mode_t mode) { + switch (mode) { + CASE_RETURN_STR(BT_SCAN_MODE_NONE) + CASE_RETURN_STR(BT_SCAN_MODE_CONNECTABLE) + CASE_RETURN_STR(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE) - default: - return "unknown scan mode"; - } + default: + return "unknown scan mode"; + } } -const char* dump_bt_status(bt_status_t status) -{ - switch(status) - { - CASE_RETURN_STR(BT_STATUS_SUCCESS) - CASE_RETURN_STR(BT_STATUS_FAIL) - CASE_RETURN_STR(BT_STATUS_NOT_READY) - CASE_RETURN_STR(BT_STATUS_NOMEM) - CASE_RETURN_STR(BT_STATUS_BUSY) - CASE_RETURN_STR(BT_STATUS_UNSUPPORTED) - - default: - return "unknown scan mode"; - } +const char* dump_bt_status(bt_status_t status) { + switch (status) { + CASE_RETURN_STR(BT_STATUS_SUCCESS) + CASE_RETURN_STR(BT_STATUS_FAIL) + CASE_RETURN_STR(BT_STATUS_NOT_READY) + CASE_RETURN_STR(BT_STATUS_NOMEM) + CASE_RETURN_STR(BT_STATUS_BUSY) + CASE_RETURN_STR(BT_STATUS_UNSUPPORTED) + + default: + return "unknown scan mode"; + } } -const char *dump_rc_event(uint8_t event) -{ - switch(event) { - CASE_RETURN_STR(BTA_AV_RC_OPEN_EVT) - CASE_RETURN_STR(BTA_AV_RC_CLOSE_EVT) - CASE_RETURN_STR(BTA_AV_RC_BROWSE_OPEN_EVT) - CASE_RETURN_STR(BTA_AV_RC_BROWSE_CLOSE_EVT) - CASE_RETURN_STR(BTA_AV_REMOTE_CMD_EVT) - CASE_RETURN_STR(BTA_AV_REMOTE_RSP_EVT) - CASE_RETURN_STR(BTA_AV_VENDOR_CMD_EVT) - CASE_RETURN_STR(BTA_AV_VENDOR_RSP_EVT) - CASE_RETURN_STR(BTA_AV_META_MSG_EVT) - CASE_RETURN_STR(BTA_AV_RC_FEAT_EVT) - default: - return "UNKNOWN_EVENT"; - } +const char* dump_rc_event(uint8_t event) { + switch (event) { + CASE_RETURN_STR(BTA_AV_RC_OPEN_EVT) + CASE_RETURN_STR(BTA_AV_RC_CLOSE_EVT) + CASE_RETURN_STR(BTA_AV_RC_BROWSE_OPEN_EVT) + CASE_RETURN_STR(BTA_AV_RC_BROWSE_CLOSE_EVT) + CASE_RETURN_STR(BTA_AV_REMOTE_CMD_EVT) + CASE_RETURN_STR(BTA_AV_REMOTE_RSP_EVT) + CASE_RETURN_STR(BTA_AV_VENDOR_CMD_EVT) + CASE_RETURN_STR(BTA_AV_VENDOR_RSP_EVT) + CASE_RETURN_STR(BTA_AV_META_MSG_EVT) + CASE_RETURN_STR(BTA_AV_RC_FEAT_EVT) + default: + return "UNKNOWN_EVENT"; + } } -const char * dump_rc_notification_event_id(uint8_t event_id) -{ - switch(event_id) - { - CASE_RETURN_STR(AVRC_EVT_PLAY_STATUS_CHANGE) - CASE_RETURN_STR(AVRC_EVT_TRACK_CHANGE) - CASE_RETURN_STR(AVRC_EVT_TRACK_REACHED_END) - CASE_RETURN_STR(AVRC_EVT_TRACK_REACHED_START) - CASE_RETURN_STR(AVRC_EVT_PLAY_POS_CHANGED) - CASE_RETURN_STR(AVRC_EVT_BATTERY_STATUS_CHANGE) - CASE_RETURN_STR(AVRC_EVT_SYSTEM_STATUS_CHANGE) - CASE_RETURN_STR(AVRC_EVT_APP_SETTING_CHANGE) - CASE_RETURN_STR(AVRC_EVT_VOLUME_CHANGE) - CASE_RETURN_STR(AVRC_EVT_ADDR_PLAYER_CHANGE) - CASE_RETURN_STR(AVRC_EVT_AVAL_PLAYERS_CHANGE) - CASE_RETURN_STR(AVRC_EVT_NOW_PLAYING_CHANGE) - CASE_RETURN_STR(AVRC_EVT_UIDS_CHANGE) - - default: - return "Unhandled Event ID"; - } +const char* dump_rc_notification_event_id(uint8_t event_id) { + switch (event_id) { + CASE_RETURN_STR(AVRC_EVT_PLAY_STATUS_CHANGE) + CASE_RETURN_STR(AVRC_EVT_TRACK_CHANGE) + CASE_RETURN_STR(AVRC_EVT_TRACK_REACHED_END) + CASE_RETURN_STR(AVRC_EVT_TRACK_REACHED_START) + CASE_RETURN_STR(AVRC_EVT_PLAY_POS_CHANGED) + CASE_RETURN_STR(AVRC_EVT_BATTERY_STATUS_CHANGE) + CASE_RETURN_STR(AVRC_EVT_SYSTEM_STATUS_CHANGE) + CASE_RETURN_STR(AVRC_EVT_APP_SETTING_CHANGE) + CASE_RETURN_STR(AVRC_EVT_VOLUME_CHANGE) + CASE_RETURN_STR(AVRC_EVT_ADDR_PLAYER_CHANGE) + CASE_RETURN_STR(AVRC_EVT_AVAL_PLAYERS_CHANGE) + CASE_RETURN_STR(AVRC_EVT_NOW_PLAYING_CHANGE) + CASE_RETURN_STR(AVRC_EVT_UIDS_CHANGE) + + default: + return "Unhandled Event ID"; + } } -const char* dump_rc_pdu(uint8_t pdu) -{ - switch(pdu) - { - CASE_RETURN_STR(AVRC_PDU_LIST_PLAYER_APP_ATTR) - CASE_RETURN_STR(AVRC_PDU_LIST_PLAYER_APP_VALUES) - CASE_RETURN_STR(AVRC_PDU_GET_CUR_PLAYER_APP_VALUE) - CASE_RETURN_STR(AVRC_PDU_SET_PLAYER_APP_VALUE) - CASE_RETURN_STR(AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT) - CASE_RETURN_STR(AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT) - CASE_RETURN_STR(AVRC_PDU_INFORM_DISPLAY_CHARSET) - CASE_RETURN_STR(AVRC_PDU_INFORM_BATTERY_STAT_OF_CT) - CASE_RETURN_STR(AVRC_PDU_GET_ELEMENT_ATTR) - CASE_RETURN_STR(AVRC_PDU_GET_PLAY_STATUS) - CASE_RETURN_STR(AVRC_PDU_REGISTER_NOTIFICATION) - CASE_RETURN_STR(AVRC_PDU_REQUEST_CONTINUATION_RSP) - CASE_RETURN_STR(AVRC_PDU_ABORT_CONTINUATION_RSP) - CASE_RETURN_STR(AVRC_PDU_SET_ABSOLUTE_VOLUME) - CASE_RETURN_STR(AVRC_PDU_SET_ADDRESSED_PLAYER) - CASE_RETURN_STR(AVRC_PDU_CHANGE_PATH) - CASE_RETURN_STR(AVRC_PDU_GET_CAPABILITIES) - CASE_RETURN_STR(AVRC_PDU_SET_BROWSED_PLAYER) - CASE_RETURN_STR(AVRC_PDU_GET_FOLDER_ITEMS) - CASE_RETURN_STR(AVRC_PDU_GET_ITEM_ATTRIBUTES) - CASE_RETURN_STR(AVRC_PDU_PLAY_ITEM) - CASE_RETURN_STR(AVRC_PDU_SEARCH) - CASE_RETURN_STR(AVRC_PDU_ADD_TO_NOW_PLAYING) - CASE_RETURN_STR(AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS) - CASE_RETURN_STR(AVRC_PDU_GENERAL_REJECT) - - default: - return "Unknown PDU"; - } +const char* dump_rc_pdu(uint8_t pdu) { + switch (pdu) { + CASE_RETURN_STR(AVRC_PDU_LIST_PLAYER_APP_ATTR) + CASE_RETURN_STR(AVRC_PDU_LIST_PLAYER_APP_VALUES) + CASE_RETURN_STR(AVRC_PDU_GET_CUR_PLAYER_APP_VALUE) + CASE_RETURN_STR(AVRC_PDU_SET_PLAYER_APP_VALUE) + CASE_RETURN_STR(AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT) + CASE_RETURN_STR(AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT) + CASE_RETURN_STR(AVRC_PDU_INFORM_DISPLAY_CHARSET) + CASE_RETURN_STR(AVRC_PDU_INFORM_BATTERY_STAT_OF_CT) + CASE_RETURN_STR(AVRC_PDU_GET_ELEMENT_ATTR) + CASE_RETURN_STR(AVRC_PDU_GET_PLAY_STATUS) + CASE_RETURN_STR(AVRC_PDU_REGISTER_NOTIFICATION) + CASE_RETURN_STR(AVRC_PDU_REQUEST_CONTINUATION_RSP) + CASE_RETURN_STR(AVRC_PDU_ABORT_CONTINUATION_RSP) + CASE_RETURN_STR(AVRC_PDU_SET_ABSOLUTE_VOLUME) + CASE_RETURN_STR(AVRC_PDU_SET_ADDRESSED_PLAYER) + CASE_RETURN_STR(AVRC_PDU_CHANGE_PATH) + CASE_RETURN_STR(AVRC_PDU_GET_CAPABILITIES) + CASE_RETURN_STR(AVRC_PDU_SET_BROWSED_PLAYER) + CASE_RETURN_STR(AVRC_PDU_GET_FOLDER_ITEMS) + CASE_RETURN_STR(AVRC_PDU_GET_ITEM_ATTRIBUTES) + CASE_RETURN_STR(AVRC_PDU_PLAY_ITEM) + CASE_RETURN_STR(AVRC_PDU_SEARCH) + CASE_RETURN_STR(AVRC_PDU_ADD_TO_NOW_PLAYING) + CASE_RETURN_STR(AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS) + CASE_RETURN_STR(AVRC_PDU_GENERAL_REJECT) + + default: + return "Unknown PDU"; + } } diff --git a/btif/src/stack_manager.cc b/btif/src/stack_manager.cc index d8dfb2d7a..04da8fbdf 100644 --- a/btif/src/stack_manager.cc +++ b/btif/src/stack_manager.cc @@ -33,11 +33,11 @@ #include "osi/include/thread.h" // Temp includes +#include "bt_utils.h" #include "btif_config.h" #include "btif_profile_queue.h" -#include "bt_utils.h" -static thread_t *management_thread; +static thread_t* management_thread; // If initialized, any of the bluetooth API functions can be called. // (e.g. turning logging on and off, enabling/disabling the stack, etc) @@ -45,17 +45,18 @@ static bool stack_is_initialized; // If running, the stack is fully up and able to bluetooth. static bool stack_is_running; -static void event_init_stack(void *context); -static void event_start_up_stack(void *context); -static void event_shut_down_stack(void *context); -static void event_clean_up_stack(void *context); +static void event_init_stack(void* context); +static void event_start_up_stack(void* context); +static void event_shut_down_stack(void* context); +static void event_clean_up_stack(void* context); -static void event_signal_stack_up(void *context); -static void event_signal_stack_down(void *context); +static void event_signal_stack_up(void* context); +static void event_signal_stack_down(void* context); -// Unvetted includes/imports, etc which should be removed or vetted in the future -static future_t *hack_future; -void btif_thread_post(thread_fn func, void *context); +// Unvetted includes/imports, etc which should be removed or vetted in the +// future +static future_t* hack_future; +void btif_thread_post(thread_fn func, void* context); // End unvetted section // Interface functions @@ -65,7 +66,7 @@ static void init_stack(void) { // state modification only happens there. Using the thread to perform // all stack operations ensures that the operations are done serially // and do not overlap. - semaphore_t *semaphore = semaphore_new(0); + semaphore_t* semaphore = semaphore_new(0); thread_post(management_thread, event_init_stack, semaphore); semaphore_wait(semaphore); semaphore_free(semaphore); @@ -82,28 +83,26 @@ static void shut_down_stack_async(void) { static void clean_up_stack(void) { // This is a synchronous process. Post it to the thread though, so // state modification only happens there. - semaphore_t *semaphore = semaphore_new(0); + semaphore_t* semaphore = semaphore_new(0); thread_post(management_thread, event_clean_up_stack, semaphore); semaphore_wait(semaphore); semaphore_free(semaphore); } -static bool get_stack_is_running(void) { - return stack_is_running; -} +static bool get_stack_is_running(void) { return stack_is_running; } // Internal functions // Synchronous function to initialize the stack -static void event_init_stack(void *context) { - semaphore_t *semaphore = (semaphore_t *)context; +static void event_init_stack(void* context) { + semaphore_t* semaphore = (semaphore_t*)context; LOG_INFO(LOG_TAG, "%s is initializing the stack", __func__); - if (stack_is_initialized) { - LOG_INFO(LOG_TAG, "%s found the stack already in initialized state", - __func__); - } else { + if (stack_is_initialized) { + LOG_INFO(LOG_TAG, "%s found the stack already in initialized state", + __func__); + } else { module_management_start(); module_init(get_module(OSI_MODULE)); @@ -117,20 +116,20 @@ static void event_init_stack(void *context) { LOG_INFO(LOG_TAG, "%s finished", __func__); - if (semaphore) - semaphore_post(semaphore); + if (semaphore) semaphore_post(semaphore); } static void ensure_stack_is_initialized(void) { if (!stack_is_initialized) { - LOG_WARN(LOG_TAG, "%s found the stack was uninitialized. Initializing now.", __func__); + LOG_WARN(LOG_TAG, "%s found the stack was uninitialized. Initializing now.", + __func__); // No semaphore needed since we are calling it directly event_init_stack(NULL); } } // Synchronous function to start up the stack -static void event_start_up_stack(UNUSED_ATTR void *context) { +static void event_start_up_stack(UNUSED_ATTR void* context) { if (stack_is_running) { LOG_INFO(LOG_TAG, "%s stack already brought up", __func__); return; @@ -139,7 +138,7 @@ static void event_start_up_stack(UNUSED_ATTR void *context) { ensure_stack_is_initialized(); LOG_INFO(LOG_TAG, "%s is bringing up the stack", __func__); - future_t *local_hack_future = future_new(); + future_t* local_hack_future = future_new(); hack_future = local_hack_future; // Include this for now to put btif config into a shutdown-able state @@ -148,7 +147,7 @@ static void event_start_up_stack(UNUSED_ATTR void *context) { if (future_await(local_hack_future) != FUTURE_SUCCESS) { LOG_ERROR(LOG_TAG, "%s failed to start up the stack", __func__); - stack_is_running = true; // So stack shutdown actually happens + stack_is_running = true; // So stack shutdown actually happens event_shut_down_stack(NULL); return; } @@ -159,14 +158,14 @@ static void event_start_up_stack(UNUSED_ATTR void *context) { } // Synchronous function to shut down the stack -static void event_shut_down_stack(UNUSED_ATTR void *context) { +static void event_shut_down_stack(UNUSED_ATTR void* context) { if (!stack_is_running) { LOG_INFO(LOG_TAG, "%s stack is already brought down", __func__); return; } LOG_INFO(LOG_TAG, "%s is bringing down the stack", __func__); - future_t *local_hack_future = future_new(); + future_t* local_hack_future = future_new(); hack_future = local_hack_future; stack_is_running = false; @@ -174,7 +173,9 @@ static void event_shut_down_stack(UNUSED_ATTR void *context) { module_shut_down(get_module(BTIF_CONFIG_MODULE)); future_await(local_hack_future); - module_shut_down(get_module(CONTROLLER_MODULE)); // Doesn't do any work, just puts it in a restartable state + module_shut_down(get_module(CONTROLLER_MODULE)); // Doesn't do any work, just + // puts it in a restartable + // state LOG_INFO(LOG_TAG, "%s finished", __func__); btif_thread_post(event_signal_stack_down, NULL); @@ -182,15 +183,16 @@ static void event_shut_down_stack(UNUSED_ATTR void *context) { static void ensure_stack_is_not_running(void) { if (stack_is_running) { - LOG_WARN(LOG_TAG, "%s found the stack was still running. Bringing it down now.", __func__); + LOG_WARN(LOG_TAG, + "%s found the stack was still running. Bringing it down now.", + __func__); event_shut_down_stack(NULL); } } // Synchronous function to clean up the stack -static void event_clean_up_stack(void *context) { - - future_t *local_hack_future; +static void event_clean_up_stack(void* context) { + future_t* local_hack_future; if (!stack_is_initialized) { LOG_INFO(LOG_TAG, "%s found the stack already in a clean state", __func__); @@ -212,25 +214,23 @@ static void event_clean_up_stack(void *context) { LOG_INFO(LOG_TAG, "%s finished", __func__); cleanup:; - semaphore_t *semaphore = (semaphore_t *)context; - if (semaphore) - semaphore_post(semaphore); + semaphore_t* semaphore = (semaphore_t*)context; + if (semaphore) semaphore_post(semaphore); } -static void event_signal_stack_up(UNUSED_ATTR void *context) { +static void event_signal_stack_up(UNUSED_ATTR void* context) { // Notify BTIF connect queue that we've brought up the stack. It's // now time to dispatch all the pending profile connect requests. btif_queue_connect_next(); HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_ON); } -static void event_signal_stack_down(UNUSED_ATTR void *context) { +static void event_signal_stack_down(UNUSED_ATTR void* context) { HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_OFF); } static void ensure_manager_initialized(void) { - if (management_thread) - return; + if (management_thread) return; management_thread = thread_new("stack_manager"); if (!management_thread) { @@ -239,20 +239,14 @@ static void ensure_manager_initialized(void) { } } -static const stack_manager_t interface = { - init_stack, - start_up_stack_async, - shut_down_stack_async, - clean_up_stack, +static const stack_manager_t interface = {init_stack, start_up_stack_async, + shut_down_stack_async, clean_up_stack, - get_stack_is_running -}; + get_stack_is_running}; -const stack_manager_t *stack_manager_get_interface() { +const stack_manager_t* stack_manager_get_interface() { ensure_manager_initialized(); return &interface; } -future_t *stack_manager_get_hack_future() { - return hack_future; -} +future_t* stack_manager_get_hack_future() { return hack_future; } diff --git a/btif/test/btif_storage_test.cc b/btif/test/btif_storage_test.cc index ef70e56eb..988e23fd9 100644 --- a/btif/test/btif_storage_test.cc +++ b/btif/test/btif_storage_test.cc @@ -22,7 +22,7 @@ #include "btif/include/btif_util.h" TEST(BtifStorageTest, test_string_to_uuid) { - const char *s1 = "e39c6285-867f-4b1d-9db0-35fbd9aebf22"; + const char* s1 = "e39c6285-867f-4b1d-9db0-35fbd9aebf22"; const uint8_t u1[] = {0xe3, 0x9c, 0x62, 0x85, 0x86, 0x7f, 0x4b, 0x1d, 0x9d, 0xb0, 0x35, 0xfb, 0xd9, 0xae, 0xbf, 0x22}; @@ -42,7 +42,9 @@ TEST(BtifStorageTest, test_string_to_uuid_invalid) { } TEST(BtifStorageTest, test_uuid_split_multiple) { - const char *s1 = "e39c6285-867f-4b1d-9db0-35fbd9aebf22 e39c6285-867f-4b1d-9db0-35fbd9aebf23"; + const char* s1 = + "e39c6285-867f-4b1d-9db0-35fbd9aebf22 " + "e39c6285-867f-4b1d-9db0-35fbd9aebf23"; const uint8_t u1[] = {0xe3, 0x9c, 0x62, 0x85, 0x86, 0x7f, 0x4b, 0x1d, 0x9d, 0xb0, 0x35, 0xfb, 0xd9, 0xae, 0xbf, 0x22}; const uint8_t u2[] = {0xe3, 0x9c, 0x62, 0x85, 0x86, 0x7f, 0x4b, 0x1d, @@ -56,7 +58,9 @@ TEST(BtifStorageTest, test_uuid_split_multiple) { } TEST(BtifStorageTest, test_uuid_split_partial) { - const char *s1 = "e39c6285-867f-4b1d-9db0-35fbd9aebf22 e39c6285-867f-4b1d-9db0-35fbd9aebf23"; + const char* s1 = + "e39c6285-867f-4b1d-9db0-35fbd9aebf22 " + "e39c6285-867f-4b1d-9db0-35fbd9aebf23"; bt_uuid_t uuids[2]; size_t num_uuids = btif_split_uuids_string(s1, uuids, 1); -- 2.11.0