Home | History | Annotate | Download | only in src
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2009-2012 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 
     20 /*****************************************************************************
     21  *
     22  *  Filename:      btif_av.c
     23  *
     24  *  Description:   Bluedroid AV implementation
     25  *
     26  *****************************************************************************/
     27 
     28 #include <hardware/bluetooth.h>
     29 #include "hardware/bt_av.h"
     30 
     31 #define LOG_TAG "BTIF_AV"
     32 
     33 #include "btif_av.h"
     34 #include "btif_util.h"
     35 #include "btif_profile_queue.h"
     36 #include "bta_api.h"
     37 #include "btif_media.h"
     38 #include "bta_av_api.h"
     39 #include "gki.h"
     40 #include "bd.h"
     41 #include "btu.h"
     42 
     43 /*****************************************************************************
     44 **  Constants & Macros
     45 ******************************************************************************/
     46 #define BTIF_AV_SERVICE_NAME "Advanced Audio"
     47 
     48 #define BTIF_TIMEOUT_AV_OPEN_ON_RC_SECS  2
     49 
     50 typedef enum {
     51     BTIF_AV_STATE_IDLE = 0x0,
     52     BTIF_AV_STATE_OPENING,
     53     BTIF_AV_STATE_OPENED,
     54     BTIF_AV_STATE_STARTED,
     55     BTIF_AV_STATE_CLOSING
     56 } btif_av_state_t;
     57 
     58 /* Should not need dedicated suspend state as actual actions are no
     59    different than open state. Suspend flags are needed however to prevent
     60    media task from trying to restart stream during remote suspend or while
     61    we are in the process of a local suspend */
     62 
     63 #define BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING 0x1
     64 #define BTIF_AV_FLAG_REMOTE_SUSPEND        0x2
     65 #define BTIF_AV_FLAG_PENDING_START         0x4
     66 #define BTIF_AV_FLAG_PENDING_STOP          0x8
     67 
     68 /*****************************************************************************
     69 **  Local type definitions
     70 ******************************************************************************/
     71 
     72 typedef struct
     73 {
     74     tBTA_AV_HNDL bta_handle;
     75     bt_bdaddr_t peer_bda;
     76     btif_sm_handle_t sm_handle;
     77     UINT8 flags;
     78     tBTA_AV_EDR edr;
     79 } btif_av_cb_t;
     80 
     81 /*****************************************************************************
     82 **  Static variables
     83 ******************************************************************************/
     84 static btav_callbacks_t *bt_av_callbacks = NULL;
     85 static btif_av_cb_t btif_av_cb;
     86 static TIMER_LIST_ENT tle_av_open_on_rc;
     87 
     88 /* both interface and media task needs to be ready to alloc incoming request */
     89 #define CHECK_BTAV_INIT() if ((bt_av_callbacks == NULL) || (btif_av_cb.sm_handle == NULL))\
     90 {\
     91      BTIF_TRACE_WARNING1("%s: BTAV not initialized", __FUNCTION__);\
     92      return BT_STATUS_NOT_READY;\
     93 }\
     94 else\
     95 {\
     96      BTIF_TRACE_EVENT1("%s", __FUNCTION__);\
     97 }
     98 
     99 /* Helper macro to avoid code duplication in the state machine handlers */
    100 #define CHECK_RC_EVENT(e, d) \
    101     case BTA_AV_RC_OPEN_EVT: \
    102     case BTA_AV_RC_CLOSE_EVT: \
    103     case BTA_AV_REMOTE_CMD_EVT: \
    104     case BTA_AV_VENDOR_CMD_EVT: \
    105     case BTA_AV_META_MSG_EVT: \
    106     case BTA_AV_RC_FEAT_EVT: \
    107     { \
    108          btif_rc_handler(e, d);\
    109     }break; \
    110 
    111 static BOOLEAN btif_av_state_idle_handler(btif_sm_event_t event, void *data);
    112 static BOOLEAN btif_av_state_opening_handler(btif_sm_event_t event, void *data);
    113 static BOOLEAN btif_av_state_opened_handler(btif_sm_event_t event, void *data);
    114 static BOOLEAN btif_av_state_started_handler(btif_sm_event_t event, void *data);
    115 static BOOLEAN btif_av_state_closing_handler(btif_sm_event_t event, void *data);
    116 
    117 static const btif_sm_handler_t btif_av_state_handlers[] =
    118 {
    119     btif_av_state_idle_handler,
    120     btif_av_state_opening_handler,
    121     btif_av_state_opened_handler,
    122     btif_av_state_started_handler,
    123     btif_av_state_closing_handler
    124 };
    125 
    126 /*************************************************************************
    127 ** Extern functions
    128 *************************************************************************/
    129 extern void btif_rc_handler(tBTA_AV_EVT event, tBTA_AV *p_data);
    130 extern BOOLEAN btif_rc_get_connected_peer(BD_ADDR peer_addr);
    131 extern void btif_rc_check_handle_pending_play (BD_ADDR peer_addr, BOOLEAN bSendToApp);
    132 
    133 /*****************************************************************************
    134 ** Local helper functions
    135 ******************************************************************************/
    136 
    137 const char *dump_av_sm_state_name(btif_av_state_t state)
    138 {
    139     switch (state)
    140     {
    141         CASE_RETURN_STR(BTIF_AV_STATE_IDLE)
    142         CASE_RETURN_STR(BTIF_AV_STATE_OPENING)
    143         CASE_RETURN_STR(BTIF_AV_STATE_OPENED)
    144         CASE_RETURN_STR(BTIF_AV_STATE_STARTED)
    145         CASE_RETURN_STR(BTIF_AV_STATE_CLOSING)
    146         default: return "UNKNOWN_STATE";
    147     }
    148 }
    149 
    150 const char *dump_av_sm_event_name(btif_av_sm_event_t event)
    151 {
    152     switch((int)event)
    153     {
    154         CASE_RETURN_STR(BTA_AV_ENABLE_EVT)
    155         CASE_RETURN_STR(BTA_AV_REGISTER_EVT)
    156         CASE_RETURN_STR(BTA_AV_OPEN_EVT)
    157         CASE_RETURN_STR(BTA_AV_CLOSE_EVT)
    158         CASE_RETURN_STR(BTA_AV_START_EVT)
    159         CASE_RETURN_STR(BTA_AV_STOP_EVT)
    160         CASE_RETURN_STR(BTA_AV_PROTECT_REQ_EVT)
    161         CASE_RETURN_STR(BTA_AV_PROTECT_RSP_EVT)
    162         CASE_RETURN_STR(BTA_AV_RC_OPEN_EVT)
    163         CASE_RETURN_STR(BTA_AV_RC_CLOSE_EVT)
    164         CASE_RETURN_STR(BTA_AV_REMOTE_CMD_EVT)
    165         CASE_RETURN_STR(BTA_AV_REMOTE_RSP_EVT)
    166         CASE_RETURN_STR(BTA_AV_VENDOR_CMD_EVT)
    167         CASE_RETURN_STR(BTA_AV_VENDOR_RSP_EVT)
    168         CASE_RETURN_STR(BTA_AV_RECONFIG_EVT)
    169         CASE_RETURN_STR(BTA_AV_SUSPEND_EVT)
    170         CASE_RETURN_STR(BTA_AV_PENDING_EVT)
    171         CASE_RETURN_STR(BTA_AV_META_MSG_EVT)
    172         CASE_RETURN_STR(BTA_AV_REJECT_EVT)
    173         CASE_RETURN_STR(BTA_AV_RC_FEAT_EVT)
    174         CASE_RETURN_STR(BTIF_SM_ENTER_EVT)
    175         CASE_RETURN_STR(BTIF_SM_EXIT_EVT)
    176         CASE_RETURN_STR(BTIF_AV_CONNECT_REQ_EVT)
    177         CASE_RETURN_STR(BTIF_AV_DISCONNECT_REQ_EVT)
    178         CASE_RETURN_STR(BTIF_AV_START_STREAM_REQ_EVT)
    179         CASE_RETURN_STR(BTIF_AV_STOP_STREAM_REQ_EVT)
    180         CASE_RETURN_STR(BTIF_AV_SUSPEND_STREAM_REQ_EVT)
    181         CASE_RETURN_STR(BTIF_AV_RECONFIGURE_REQ_EVT)
    182 
    183         default: return "UNKNOWN_EVENT";
    184    }
    185 }
    186 
    187 /****************************************************************************
    188 **  Local helper functions
    189 *****************************************************************************/
    190 /*******************************************************************************
    191 **
    192 ** Function         btif_initiate_av_open_tmr_hdlr
    193 **
    194 ** Description      Timer to trigger AV open if the remote headset establishes
    195 **                  RC connection w/o AV connection. The timer is needed to IOP
    196 **                  with headsets that do establish AV after RC connection.
    197 **
    198 ** Returns          void
    199 **
    200 *******************************************************************************/
    201 static void btif_initiate_av_open_tmr_hdlr(TIMER_LIST_ENT *tle)
    202 {
    203     BD_ADDR peer_addr;
    204 
    205     /* is there at least one RC connection - There should be */
    206     if (btif_rc_get_connected_peer(peer_addr)) {
    207        BTIF_TRACE_DEBUG1("%s Issuing connect to the remote RC peer", __FUNCTION__);
    208        btif_sm_dispatch(btif_av_cb.sm_handle, BTIF_AV_CONNECT_REQ_EVT, (void*)&peer_addr);
    209     }
    210     else
    211     {
    212         BTIF_TRACE_ERROR1("%s No connected RC peers", __FUNCTION__);
    213     }
    214 }
    215 
    216 /*****************************************************************************
    217 **  Static functions
    218 ******************************************************************************/
    219 
    220 /*****************************************************************************
    221 **
    222 ** Function		btif_av_state_idle_handler
    223 **
    224 ** Description  State managing disconnected AV link
    225 **
    226 ** Returns      TRUE if event was processed, FALSE otherwise
    227 **
    228 *******************************************************************************/
    229 
    230 static BOOLEAN btif_av_state_idle_handler(btif_sm_event_t event, void *p_data)
    231 {
    232     BTIF_TRACE_DEBUG3("%s event:%s flags %x", __FUNCTION__,
    233                      dump_av_sm_event_name(event), btif_av_cb.flags);
    234 
    235     switch (event)
    236     {
    237         case BTIF_SM_ENTER_EVT:
    238             /* clear the peer_bda */
    239             memset(&btif_av_cb.peer_bda, 0, sizeof(bt_bdaddr_t));
    240             btif_av_cb.flags = 0;
    241             btif_av_cb.edr = 0;
    242             btif_a2dp_on_idle();
    243             break;
    244 
    245         case BTIF_SM_EXIT_EVT:
    246             break;
    247 
    248         case BTA_AV_ENABLE_EVT:
    249             break;
    250 
    251         case BTA_AV_REGISTER_EVT:
    252             btif_av_cb.bta_handle = ((tBTA_AV*)p_data)->registr.hndl;
    253             break;
    254 
    255         case BTA_AV_PENDING_EVT:
    256         case BTIF_AV_CONNECT_REQ_EVT:
    257         {
    258              if (event == BTIF_AV_CONNECT_REQ_EVT)
    259              {
    260                  memcpy(&btif_av_cb.peer_bda, (bt_bdaddr_t*)p_data, sizeof(bt_bdaddr_t));
    261              }
    262              else if (event == BTA_AV_PENDING_EVT)
    263              {
    264                   bdcpy(btif_av_cb.peer_bda.address, ((tBTA_AV*)p_data)->pend.bd_addr);
    265              }
    266              BTA_AvOpen(btif_av_cb.peer_bda.address, btif_av_cb.bta_handle,
    267                     TRUE, BTA_SEC_NONE);
    268              btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_OPENING);
    269         } break;
    270 
    271         case BTA_AV_RC_OPEN_EVT:
    272             /* IOP_FIX: Jabra 620 only does RC open without AV open whenever it connects. So
    273              * as per the AV WP, an AVRC connection cannot exist without an AV connection. Therefore,
    274              * we initiate an AV connection if an RC_OPEN_EVT is received when we are in AV_CLOSED state.
    275              * We initiate the AV connection after a small 3s timeout to avoid any collisions from the
    276              * headsets, as some headsets initiate the AVRC connection first and then
    277              * immediately initiate the AV connection
    278              *
    279              * TODO: We may need to do this only on an AVRCP Play. FixMe
    280              */
    281 
    282             BTIF_TRACE_DEBUG0("BTA_AV_RC_OPEN_EVT received w/o AV");
    283             memset(&tle_av_open_on_rc, 0, sizeof(tle_av_open_on_rc));
    284             tle_av_open_on_rc.param = (UINT32)btif_initiate_av_open_tmr_hdlr;
    285             btu_start_timer(&tle_av_open_on_rc, BTU_TTYPE_USER_FUNC,
    286                             BTIF_TIMEOUT_AV_OPEN_ON_RC_SECS);
    287             btif_rc_handler(event, p_data);
    288             break;
    289 
    290         case BTA_AV_REMOTE_CMD_EVT:
    291         case BTA_AV_VENDOR_CMD_EVT:
    292         case BTA_AV_META_MSG_EVT:
    293         case BTA_AV_RC_FEAT_EVT:
    294             btif_rc_handler(event, (tBTA_AV*)p_data);
    295             break;
    296 
    297         case BTA_AV_RC_CLOSE_EVT:
    298             if (tle_av_open_on_rc.in_use) {
    299                 BTIF_TRACE_DEBUG0("BTA_AV_RC_CLOSE_EVT: Stopping AV timer.");
    300                 btu_stop_timer(&tle_av_open_on_rc);
    301             }
    302             btif_rc_handler(event, p_data);
    303             break;
    304 
    305         default:
    306             BTIF_TRACE_WARNING2("%s : unhandled event:%s", __FUNCTION__,
    307                                 dump_av_sm_event_name(event));
    308             return FALSE;
    309 
    310     }
    311     return TRUE;
    312 }
    313 /*****************************************************************************
    314 **
    315 ** Function        btif_av_state_opening_handler
    316 **
    317 ** Description     Intermediate state managing events during establishment
    318 **                 of avdtp channel
    319 **
    320 ** Returns         TRUE if event was processed, FALSE otherwise
    321 **
    322 *******************************************************************************/
    323 
    324 static BOOLEAN btif_av_state_opening_handler(btif_sm_event_t event, void *p_data)
    325 {
    326     BTIF_TRACE_DEBUG3("%s event:%s flags %x", __FUNCTION__,
    327                      dump_av_sm_event_name(event), btif_av_cb.flags);
    328 
    329     switch (event)
    330     {
    331         case BTIF_SM_ENTER_EVT:
    332             /* inform the application that we are entering connecting state */
    333             HAL_CBACK(bt_av_callbacks, connection_state_cb,
    334                       BTAV_CONNECTION_STATE_CONNECTING, &(btif_av_cb.peer_bda));
    335             break;
    336 
    337         case BTIF_SM_EXIT_EVT:
    338             break;
    339 
    340         case BTA_AV_OPEN_EVT:
    341         {
    342             tBTA_AV *p_bta_data = (tBTA_AV*)p_data;
    343             btav_connection_state_t state;
    344             btif_sm_state_t av_state;
    345             BTIF_TRACE_DEBUG2("status:%d, edr 0x%x",p_bta_data->open.status,
    346                                p_bta_data->open.edr);
    347 
    348             if (p_bta_data->open.status == BTA_AV_SUCCESS)
    349             {
    350                  state = BTAV_CONNECTION_STATE_CONNECTED;
    351                  av_state = BTIF_AV_STATE_OPENED;
    352                  btif_av_cb.edr = p_bta_data->open.edr;
    353             }
    354             else
    355             {
    356                 BTIF_TRACE_WARNING1("BTA_AV_OPEN_EVT::FAILED status: %d",
    357                                      p_bta_data->open.status );
    358                 state = BTAV_CONNECTION_STATE_DISCONNECTED;
    359                 av_state  = BTIF_AV_STATE_IDLE;
    360             }
    361 
    362             /* inform the application of the event */
    363             HAL_CBACK(bt_av_callbacks, connection_state_cb,
    364                              state, &(btif_av_cb.peer_bda));
    365             /* change state to open/idle based on the status */
    366             btif_sm_change_state(btif_av_cb.sm_handle, av_state);
    367             /* if queued PLAY command,  send it now */
    368             btif_rc_check_handle_pending_play(p_bta_data->open.bd_addr,
    369                                              (p_bta_data->open.status == BTA_AV_SUCCESS));
    370             btif_queue_advance();
    371         } break;
    372 
    373         CHECK_RC_EVENT(event, p_data);
    374 
    375         default:
    376             BTIF_TRACE_WARNING2("%s : unhandled event:%s", __FUNCTION__,
    377                                 dump_av_sm_event_name(event));
    378             return FALSE;
    379 
    380    }
    381    return TRUE;
    382 }
    383 
    384 
    385 /*****************************************************************************
    386 **
    387 ** Function        btif_av_state_closing_handler
    388 **
    389 ** Description     Intermediate state managing events during closing
    390 **                 of avdtp channel
    391 **
    392 ** Returns         TRUE if event was processed, FALSE otherwise
    393 **
    394 *******************************************************************************/
    395 
    396 static BOOLEAN btif_av_state_closing_handler(btif_sm_event_t event, void *p_data)
    397 {
    398     BTIF_TRACE_DEBUG3("%s event:%s flags %x", __FUNCTION__,
    399                      dump_av_sm_event_name(event), btif_av_cb.flags);
    400 
    401     switch (event)
    402     {
    403         case BTIF_SM_ENTER_EVT:
    404 
    405             /* immediately stop transmission of frames */
    406             btif_a2dp_set_tx_flush(TRUE);
    407             /* wait for audioflinger to stop a2dp */
    408             break;
    409 
    410         case BTA_AV_STOP_EVT:
    411         case BTIF_AV_STOP_STREAM_REQ_EVT:
    412               /* immediately flush any pending tx frames while suspend is pending */
    413               btif_a2dp_set_tx_flush(TRUE);
    414 
    415               btif_a2dp_on_stopped(NULL);
    416 
    417               break;
    418 
    419         case BTIF_SM_EXIT_EVT:
    420             break;
    421 
    422         case BTA_AV_CLOSE_EVT:
    423 
    424             /* inform the application that we are disconnecting */
    425             HAL_CBACK(bt_av_callbacks, connection_state_cb,
    426                 BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb.peer_bda));
    427 
    428             btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE);
    429             break;
    430 
    431         /* Handle the RC_CLOSE event for the cleanup */
    432         case BTA_AV_RC_CLOSE_EVT:
    433             btif_rc_handler(event, (tBTA_AV*)p_data);
    434             break;
    435 
    436         default:
    437             BTIF_TRACE_WARNING2("%s : unhandled event:%s", __FUNCTION__,
    438                                 dump_av_sm_event_name(event));
    439             return FALSE;
    440    }
    441    return TRUE;
    442 }
    443 
    444 
    445 /*****************************************************************************
    446 **
    447 ** Function     btif_av_state_opened_handler
    448 **
    449 ** Description  Handles AV events while AVDTP is in OPEN state
    450 **
    451 ** Returns      TRUE if event was processed, FALSE otherwise
    452 **
    453 *******************************************************************************/
    454 
    455 static BOOLEAN btif_av_state_opened_handler(btif_sm_event_t event, void *p_data)
    456 {
    457     tBTA_AV *p_av = (tBTA_AV*)p_data;
    458 
    459     BTIF_TRACE_DEBUG3("%s event:%s flags %x", __FUNCTION__,
    460                      dump_av_sm_event_name(event), btif_av_cb.flags);
    461 
    462     if ( (event == BTA_AV_REMOTE_CMD_EVT) && (btif_av_cb.flags & BTIF_AV_FLAG_REMOTE_SUSPEND) &&
    463          (p_av->remote_cmd.rc_id == BTA_AV_RC_PLAY) )
    464     {
    465         BTIF_TRACE_EVENT1("%s: Resetting remote suspend flag on RC PLAY", __FUNCTION__);
    466         btif_av_cb.flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;
    467     }
    468 
    469     switch (event)
    470     {
    471         case BTIF_SM_ENTER_EVT:
    472             btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_STOP;
    473             btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_START;
    474             break;
    475 
    476         case BTIF_SM_EXIT_EVT:
    477             btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_START;
    478             break;
    479 
    480         case BTIF_AV_START_STREAM_REQ_EVT:
    481             btif_a2dp_setup_codec();
    482             BTA_AvStart();
    483             btif_av_cb.flags |= BTIF_AV_FLAG_PENDING_START;
    484             break;
    485 
    486         case BTA_AV_START_EVT:
    487         {
    488             BTIF_TRACE_EVENT3("BTA_AV_START_EVT status %d, suspending %d, init %d",
    489                 p_av->start.status, p_av->start.suspending, p_av->start.initiator);
    490 
    491             if ((p_av->start.status == BTA_SUCCESS) && (p_av->start.suspending == TRUE))
    492                 return TRUE;
    493 
    494             if (btif_a2dp_on_started(&p_av->start,
    495                 ((btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) != 0))) {
    496                 /* only clear pending flag after acknowledgement */
    497                 btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_START;
    498             }
    499 
    500             /* remain in open state if status failed */
    501             if (p_av->start.status != BTA_AV_SUCCESS)
    502                 return FALSE;
    503 
    504             /* change state to started, send acknowledgement if start is pending */
    505             if (btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) {
    506                 btif_a2dp_on_started(NULL, TRUE);
    507                 /* pending start flag will be cleared when exit current state */
    508             }
    509             btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_STARTED);
    510 
    511         } break;
    512 
    513         case BTIF_AV_DISCONNECT_REQ_EVT:
    514             BTA_AvClose(btif_av_cb.bta_handle);
    515 
    516             /* inform the application that we are disconnecting */
    517             HAL_CBACK(bt_av_callbacks, connection_state_cb,
    518                BTAV_CONNECTION_STATE_DISCONNECTING, &(btif_av_cb.peer_bda));
    519             break;
    520 
    521         case BTA_AV_CLOSE_EVT:
    522 
    523             /* avdtp link is closed */
    524             btif_a2dp_on_stopped(NULL);
    525 
    526             /* inform the application that we are disconnected */
    527             HAL_CBACK(bt_av_callbacks, connection_state_cb,
    528                 BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb.peer_bda));
    529 
    530             /* change state to idle, send acknowledgement if start is pending */
    531             if (btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) {
    532                 btif_a2dp_ack_fail();
    533                 /* pending start flag will be cleared when exit current state */
    534             }
    535             btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE);
    536             break;
    537 
    538         case BTA_AV_RECONFIG_EVT:
    539             if((btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) &&
    540                 (p_av->reconfig.status == BTA_AV_SUCCESS))
    541             {
    542                APPL_TRACE_WARNING0("reconfig done BTA_AVstart()");
    543                BTA_AvStart();
    544             }
    545             else if(btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START)
    546             {
    547                btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_START;
    548                btif_a2dp_ack_fail();
    549             }
    550             break;
    551 
    552         CHECK_RC_EVENT(event, p_data);
    553 
    554         default:
    555             BTIF_TRACE_WARNING2("%s : unhandled event:%s", __FUNCTION__,
    556                                dump_av_sm_event_name(event));
    557             return FALSE;
    558 
    559     }
    560     return TRUE;
    561 }
    562 
    563 /*****************************************************************************
    564 **
    565 ** Function     btif_av_state_started_handler
    566 **
    567 ** Description  Handles AV events while A2DP stream is started
    568 **
    569 ** Returns      TRUE if event was processed, FALSE otherwise
    570 **
    571 *******************************************************************************/
    572 
    573 static BOOLEAN btif_av_state_started_handler(btif_sm_event_t event, void *p_data)
    574 {
    575     tBTA_AV *p_av = (tBTA_AV*)p_data;
    576 
    577     BTIF_TRACE_DEBUG3("%s event:%s flags %x", __FUNCTION__,
    578                      dump_av_sm_event_name(event), btif_av_cb.flags);
    579 
    580     switch (event)
    581     {
    582         case BTIF_SM_ENTER_EVT:
    583 
    584             /* we are again in started state, clear any remote suspend flags */
    585             btif_av_cb.flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;
    586 
    587             HAL_CBACK(bt_av_callbacks, audio_state_cb,
    588                 BTAV_AUDIO_STATE_STARTED, &(btif_av_cb.peer_bda));
    589             break;
    590 
    591         case BTIF_SM_EXIT_EVT:
    592             break;
    593 
    594         case BTIF_AV_START_STREAM_REQ_EVT:
    595             /* we were remotely started, just ack back the local request */
    596             btif_a2dp_on_started(NULL, TRUE);
    597             break;
    598 
    599         /* fixme -- use suspend = true always to work around issue with BTA AV */
    600         case BTIF_AV_STOP_STREAM_REQ_EVT:
    601         case BTIF_AV_SUSPEND_STREAM_REQ_EVT:
    602 
    603             /* set pending flag to ensure btif task is not trying to restart
    604                stream while suspend is in progress */
    605             btif_av_cb.flags |= BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING;
    606 
    607             /* if we were remotely suspended but suspend locally, local suspend
    608                always overrides */
    609             btif_av_cb.flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;
    610 
    611             /* immediately stop transmission of frames while suspend is pending */
    612             btif_a2dp_set_tx_flush(TRUE);
    613 
    614             BTA_AvStop(TRUE);
    615             break;
    616 
    617         case BTIF_AV_DISCONNECT_REQ_EVT:
    618 
    619             /* request avdtp to close */
    620             BTA_AvClose(btif_av_cb.bta_handle);
    621 
    622             /* inform the application that we are disconnecting */
    623             HAL_CBACK(bt_av_callbacks, connection_state_cb,
    624                 BTAV_CONNECTION_STATE_DISCONNECTING, &(btif_av_cb.peer_bda));
    625 
    626             /* wait in closing state until fully closed */
    627             btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_CLOSING);
    628             break;
    629 
    630         case BTA_AV_SUSPEND_EVT:
    631 
    632             BTIF_TRACE_EVENT2("BTA_AV_SUSPEND_EVT status %d, init %d",
    633                  p_av->suspend.status, p_av->suspend.initiator);
    634 
    635             /* a2dp suspended, stop media task until resumed */
    636             btif_a2dp_on_suspended(&p_av->suspend);
    637 
    638             /* if not successful, remain in current state */
    639             if (p_av->suspend.status != BTA_AV_SUCCESS)
    640             {
    641                 btif_av_cb.flags &= ~BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING;
    642 
    643                 /* suspend failed, reset back tx flush state */
    644                 btif_a2dp_set_tx_flush(FALSE);
    645                 return FALSE;
    646             }
    647 
    648             if (p_av->suspend.initiator != TRUE)
    649             {
    650                 /* remote suspend, notify HAL and await audioflinger to
    651                    suspend/stop stream */
    652 
    653                 /* set remote suspend flag to block media task from restarting
    654                    stream only if we did not already initiate a local suspend */
    655                 if ((btif_av_cb.flags & BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING) == 0)
    656                     btif_av_cb.flags |= BTIF_AV_FLAG_REMOTE_SUSPEND;
    657 
    658                 HAL_CBACK(bt_av_callbacks, audio_state_cb,
    659                         BTAV_AUDIO_STATE_REMOTE_SUSPEND, &(btif_av_cb.peer_bda));
    660             }
    661             else
    662             {
    663                 HAL_CBACK(bt_av_callbacks, audio_state_cb,
    664                         BTAV_AUDIO_STATE_STOPPED, &(btif_av_cb.peer_bda));
    665             }
    666 
    667             btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_OPENED);
    668 
    669             /* suspend completed and state changed, clear pending status */
    670             btif_av_cb.flags &= ~BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING;
    671             break;
    672 
    673         case BTA_AV_STOP_EVT:
    674 
    675             btif_av_cb.flags |= BTIF_AV_FLAG_PENDING_STOP;
    676 
    677             btif_a2dp_on_stopped(&p_av->suspend);
    678 
    679             HAL_CBACK(bt_av_callbacks, audio_state_cb,
    680                       BTAV_AUDIO_STATE_STOPPED, &(btif_av_cb.peer_bda));
    681 
    682             /* if stop was successful, change state to open */
    683             if (p_av->suspend.status == BTA_AV_SUCCESS)
    684                 btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_OPENED);
    685 
    686             break;
    687 
    688         case BTA_AV_CLOSE_EVT:
    689 
    690              btif_av_cb.flags |= BTIF_AV_FLAG_PENDING_STOP;
    691 
    692             /* avdtp link is closed */
    693 
    694             btif_a2dp_on_stopped(NULL);
    695 
    696             /* inform the application that we are disconnected */
    697             HAL_CBACK(bt_av_callbacks, connection_state_cb,
    698                 BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb.peer_bda));
    699 
    700             btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE);
    701             break;
    702 
    703         CHECK_RC_EVENT(event, p_data);
    704 
    705         default:
    706             BTIF_TRACE_WARNING2("%s : unhandled event:%s", __FUNCTION__,
    707                                  dump_av_sm_event_name(event));
    708             return FALSE;
    709 
    710     }
    711     return TRUE;
    712 }
    713 
    714 /*****************************************************************************
    715 **  Local event handlers
    716 ******************************************************************************/
    717 
    718 static void btif_av_handle_event(UINT16 event, char* p_param)
    719 {
    720     btif_sm_dispatch(btif_av_cb.sm_handle, event, (void*)p_param);
    721 }
    722 
    723 static void bte_av_callback(tBTA_AV_EVT event, tBTA_AV *p_data)
    724 {
    725     /* Switch to BTIF context */
    726     btif_transfer_context(btif_av_handle_event, event,
    727                           (char*)p_data, sizeof(tBTA_AV), NULL);
    728 }
    729 
    730 /*******************************************************************************
    731 **
    732 ** Function         btif_av_init
    733 **
    734 ** Description      Initializes btif AV if not already done
    735 **
    736 ** Returns          bt_status_t
    737 **
    738 *******************************************************************************/
    739 
    740 bt_status_t btif_av_init(void)
    741 {
    742     if (btif_av_cb.sm_handle == NULL)
    743     {
    744         if (btif_a2dp_start_media_task() != GKI_SUCCESS)
    745             return BT_STATUS_FAIL;
    746 
    747         btif_enable_service(BTA_A2DP_SERVICE_ID);
    748 
    749         /* Also initialize the AV state machine */
    750         btif_av_cb.sm_handle = btif_sm_init((const btif_sm_handler_t*)btif_av_state_handlers, BTIF_AV_STATE_IDLE);
    751 
    752         btif_a2dp_on_init();
    753 
    754         return BT_STATUS_SUCCESS;
    755     }
    756 
    757     return BT_STATUS_DONE;
    758 }
    759 
    760 /*******************************************************************************
    761 **
    762 ** Function         init
    763 **
    764 ** Description      Initializes the AV interface
    765 **
    766 ** Returns          bt_status_t
    767 **
    768 *******************************************************************************/
    769 
    770 static bt_status_t init(btav_callbacks_t* callbacks )
    771 {
    772     int status;
    773 
    774     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
    775 
    776     if (bt_av_callbacks)
    777         return BT_STATUS_DONE;
    778 
    779     bt_av_callbacks = callbacks;
    780     btif_av_cb.sm_handle = NULL;
    781 
    782     return btif_av_init();
    783 }
    784 
    785 /*******************************************************************************
    786 **
    787 ** Function         connect
    788 **
    789 ** Description      Establishes the AV signalling channel with the remote headset
    790 **
    791 ** Returns          bt_status_t
    792 **
    793 *******************************************************************************/
    794 
    795 static bt_status_t connect_int(bt_bdaddr_t *bd_addr)
    796 {
    797     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
    798 
    799     btif_sm_dispatch(btif_av_cb.sm_handle, BTIF_AV_CONNECT_REQ_EVT, (char*)bd_addr);
    800 
    801     return BT_STATUS_SUCCESS;
    802 }
    803 
    804 static bt_status_t connect(bt_bdaddr_t *bd_addr)
    805 {
    806     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
    807     CHECK_BTAV_INIT();
    808 
    809     return btif_queue_connect(UUID_SERVCLASS_AUDIO_SOURCE, bd_addr, connect_int);
    810 }
    811 
    812 /*******************************************************************************
    813 **
    814 ** Function         disconnect
    815 **
    816 ** Description      Tears down the AV signalling channel with the remote headset
    817 **
    818 ** Returns          bt_status_t
    819 **
    820 *******************************************************************************/
    821 static bt_status_t disconnect(bt_bdaddr_t *bd_addr)
    822 {
    823     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
    824 
    825     CHECK_BTAV_INIT();
    826 
    827     /* Switch to BTIF context */
    828     return btif_transfer_context(btif_av_handle_event, BTIF_AV_DISCONNECT_REQ_EVT,
    829                                  (char*)bd_addr, sizeof(bt_bdaddr_t), NULL);
    830 }
    831 
    832 /*******************************************************************************
    833 **
    834 ** Function         cleanup
    835 **
    836 ** Description      Shuts down the AV interface and does the cleanup
    837 **
    838 ** Returns          None
    839 **
    840 *******************************************************************************/
    841 static void cleanup(void)
    842 {
    843     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
    844 
    845     if (bt_av_callbacks)
    846     {
    847         btif_a2dp_stop_media_task();
    848 
    849         btif_disable_service(BTA_A2DP_SERVICE_ID);
    850         bt_av_callbacks = NULL;
    851 
    852         /* Also shut down the AV state machine */
    853         btif_sm_shutdown(btif_av_cb.sm_handle);
    854         btif_av_cb.sm_handle = NULL;
    855     }
    856     return;
    857 }
    858 
    859 static const btav_interface_t bt_av_interface = {
    860     sizeof(btav_interface_t),
    861     init,
    862     connect,
    863     disconnect,
    864     cleanup,
    865 };
    866 
    867 /*******************************************************************************
    868 **
    869 ** Function         btif_av_get_sm_handle
    870 **
    871 ** Description      Fetches current av SM handle
    872 **
    873 ** Returns          None
    874 **
    875 *******************************************************************************/
    876 
    877 btif_sm_handle_t btif_av_get_sm_handle(void)
    878 {
    879     return btif_av_cb.sm_handle;
    880 }
    881 
    882 /*******************************************************************************
    883 **
    884 ** Function         btif_av_stream_ready
    885 **
    886 ** Description      Checks whether AV is ready for starting a stream
    887 **
    888 ** Returns          None
    889 **
    890 *******************************************************************************/
    891 
    892 BOOLEAN btif_av_stream_ready(void)
    893 {
    894     btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle);
    895 
    896     BTIF_TRACE_DEBUG3("btif_av_stream_ready : sm hdl %d, state %d, flags %x",
    897                 btif_av_cb.sm_handle, state, btif_av_cb.flags);
    898 
    899     /* also make sure main adapter is enabled */
    900     if (btif_is_enabled() == 0)
    901     {
    902         BTIF_TRACE_EVENT0("main adapter not enabled");
    903         return FALSE;
    904     }
    905 
    906     /* check if we are remotely suspended or stop is pending */
    907     if (btif_av_cb.flags & (BTIF_AV_FLAG_REMOTE_SUSPEND|BTIF_AV_FLAG_PENDING_STOP))
    908         return FALSE;
    909 
    910     return (state == BTIF_AV_STATE_OPENED);
    911 }
    912 
    913 /*******************************************************************************
    914 **
    915 ** Function         btif_av_stream_started_ready
    916 **
    917 ** Description      Checks whether AV ready for media start in streaming state
    918 **
    919 ** Returns          None
    920 **
    921 *******************************************************************************/
    922 
    923 BOOLEAN btif_av_stream_started_ready(void)
    924 {
    925     btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle);
    926 
    927     BTIF_TRACE_DEBUG3("btif_av_stream_started : sm hdl %d, state %d, flags %x",
    928                 btif_av_cb.sm_handle, state, btif_av_cb.flags);
    929 
    930     /* disallow media task to start if we have pending actions */
    931     if (btif_av_cb.flags & (BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING | BTIF_AV_FLAG_REMOTE_SUSPEND
    932         | BTIF_AV_FLAG_PENDING_STOP))
    933         return FALSE;
    934 
    935     return (state == BTIF_AV_STATE_STARTED);
    936 }
    937 
    938 /*******************************************************************************
    939 **
    940 ** Function         btif_dispatch_sm_event
    941 **
    942 ** Description      Send event to AV statemachine
    943 **
    944 ** Returns          None
    945 **
    946 *******************************************************************************/
    947 
    948 /* used to pass events to AV statemachine from other tasks */
    949 void btif_dispatch_sm_event(btif_av_sm_event_t event, void *p_data, int len)
    950 {
    951     /* Switch to BTIF context */
    952     btif_transfer_context(btif_av_handle_event, event,
    953                           (char*)p_data, len, NULL);
    954 }
    955 
    956 /*******************************************************************************
    957 **
    958 ** Function         btif_av_execute_service
    959 **
    960 ** Description      Initializes/Shuts down the service
    961 **
    962 ** Returns          BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
    963 **
    964 *******************************************************************************/
    965 bt_status_t btif_av_execute_service(BOOLEAN b_enable)
    966 {
    967      if (b_enable)
    968      {
    969          /* TODO: Removed BTA_SEC_AUTHORIZE since the Java/App does not
    970           * handle this request in order to allow incoming connections to succeed.
    971           * We need to put this back once support for this is added */
    972 
    973          /* Added BTA_AV_FEAT_NO_SCO_SSPD - this ensures that the BTA does not
    974           * auto-suspend av streaming on AG events(SCO or Call). The suspend shall
    975           * be initiated by the app/audioflinger layers */
    976 #if (AVRC_METADATA_INCLUDED == TRUE)
    977          BTA_AvEnable(BTA_SEC_AUTHENTICATE,
    978              BTA_AV_FEAT_RCTG|BTA_AV_FEAT_METADATA|BTA_AV_FEAT_VENDOR|BTA_AV_FEAT_NO_SCO_SSPD
    979 #if (AVRC_ADV_CTRL_INCLUDED == TRUE)
    980              |BTA_AV_FEAT_RCCT
    981              |BTA_AV_FEAT_ADV_CTRL
    982 #endif
    983              ,bte_av_callback);
    984 #else
    985          BTA_AvEnable(BTA_SEC_AUTHENTICATE, (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_NO_SCO_SSPD),
    986                       bte_av_callback);
    987 #endif
    988          BTA_AvRegister(BTA_AV_CHNL_AUDIO, BTIF_AV_SERVICE_NAME, 0);
    989      }
    990      else {
    991          BTA_AvDeregister(btif_av_cb.bta_handle);
    992          BTA_AvDisable();
    993      }
    994      return BT_STATUS_SUCCESS;
    995 }
    996 
    997 /*******************************************************************************
    998 **
    999 ** Function         btif_av_get_interface
   1000 **
   1001 ** Description      Get the AV callback interface
   1002 **
   1003 ** Returns          btav_interface_t
   1004 **
   1005 *******************************************************************************/
   1006 const btav_interface_t *btif_av_get_interface(void)
   1007 {
   1008     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
   1009     return &bt_av_interface;
   1010 }
   1011 
   1012 /*******************************************************************************
   1013 **
   1014 ** Function         btif_av_is_connected
   1015 **
   1016 ** Description      Checks if av has a connected sink
   1017 **
   1018 ** Returns          BOOLEAN
   1019 **
   1020 *******************************************************************************/
   1021 BOOLEAN btif_av_is_connected(void)
   1022 {
   1023     btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle);
   1024     return ((state == BTIF_AV_STATE_OPENED) || (state ==  BTIF_AV_STATE_STARTED));
   1025 }
   1026 
   1027 /*******************************************************************************
   1028 **
   1029 ** Function         btif_av_is_peer_edr
   1030 **
   1031 ** Description      Check if the connected a2dp device supports
   1032 **                  EDR or not. Only when connected this function
   1033 **                  will accurately provide a true capability of
   1034 **                  remote peer. If not connected it will always be false.
   1035 **
   1036 ** Returns          TRUE if remote device is capable of EDR
   1037 **
   1038 *******************************************************************************/
   1039 BOOLEAN btif_av_is_peer_edr(void)
   1040 {
   1041     ASSERTC(btif_av_is_connected(), "No active a2dp connection", 0);
   1042 
   1043     if (btif_av_cb.edr)
   1044         return TRUE;
   1045     else
   1046         return FALSE;
   1047 }
   1048 
   1049