Home | History | Annotate | Download | only in src
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2009-2016 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 #define LOG_TAG "btif_av"
     20 
     21 #include "btif_av.h"
     22 
     23 #include <base/logging.h>
     24 #include <string.h>
     25 
     26 #include <hardware/bluetooth.h>
     27 #include <hardware/bt_av.h>
     28 #include <hardware/bt_rc.h>
     29 
     30 #include "audio_a2dp_hw/include/audio_a2dp_hw.h"
     31 #include "bt_common.h"
     32 #include "bt_utils.h"
     33 #include "bta_api.h"
     34 #include "btif_a2dp.h"
     35 #include "btif_a2dp_control.h"
     36 #include "btif_a2dp_sink.h"
     37 #include "btif_av_co.h"
     38 #include "btif_profile_queue.h"
     39 #include "btif_util.h"
     40 #include "btu.h"
     41 #include "osi/include/allocator.h"
     42 #include "osi/include/osi.h"
     43 
     44 /*****************************************************************************
     45  *  Constants & Macros
     46  *****************************************************************************/
     47 #define BTIF_AV_SERVICE_NAME "Advanced Audio"
     48 #define BTIF_AVK_SERVICE_NAME "Advanced Audio Sink"
     49 
     50 #define BTIF_TIMEOUT_AV_OPEN_ON_RC_MS (2 * 1000)
     51 
     52 typedef enum {
     53   BTIF_AV_STATE_IDLE = 0x0,
     54   BTIF_AV_STATE_OPENING,
     55   BTIF_AV_STATE_OPENED,
     56   BTIF_AV_STATE_STARTED,
     57   BTIF_AV_STATE_CLOSING
     58 } btif_av_state_t;
     59 
     60 /* Should not need dedicated suspend state as actual actions are no
     61    different than open state. Suspend flags are needed however to prevent
     62    media task from trying to restart stream during remote suspend or while
     63    we are in the process of a local suspend */
     64 
     65 #define BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING 0x1
     66 #define BTIF_AV_FLAG_REMOTE_SUSPEND 0x2
     67 #define BTIF_AV_FLAG_PENDING_START 0x4
     68 #define BTIF_AV_FLAG_PENDING_STOP 0x8
     69 
     70 /*****************************************************************************
     71  *  Local type definitions
     72  *****************************************************************************/
     73 
     74 typedef struct {
     75   tBTA_AV_HNDL bta_handle;
     76   RawAddress peer_bda;
     77   bool self_initiated_connection;
     78   btif_sm_handle_t sm_handle;
     79   uint8_t flags;
     80   tBTA_AV_EDR edr;
     81   uint8_t peer_sep; /* sep type of peer device */
     82   std::vector<btav_a2dp_codec_config_t> codec_priorities;
     83 } btif_av_cb_t;
     84 
     85 typedef struct {
     86   RawAddress* target_bda;
     87   uint16_t uuid;
     88 } btif_av_connect_req_t;
     89 
     90 typedef struct {
     91   int sample_rate;
     92   int channel_count;
     93   RawAddress peer_bd;
     94 } btif_av_sink_config_req_t;
     95 
     96 /*****************************************************************************
     97  *  Static variables
     98  *****************************************************************************/
     99 static btav_source_callbacks_t* bt_av_src_callbacks = NULL;
    100 static btav_sink_callbacks_t* bt_av_sink_callbacks = NULL;
    101 static btif_av_cb_t btif_av_cb = {
    102     0, {{0}}, false, 0, 0, 0, 0, std::vector<btav_a2dp_codec_config_t>()};
    103 static alarm_t* av_open_on_rc_timer = NULL;
    104 
    105 /* both interface and media task needs to be ready to alloc incoming request */
    106 #define CHECK_BTAV_INIT()                                                    \
    107   do {                                                                       \
    108     if (((bt_av_src_callbacks == NULL) && (bt_av_sink_callbacks == NULL)) || \
    109         (btif_av_cb.sm_handle == NULL)) {                                    \
    110       BTIF_TRACE_WARNING("%s: BTAV not initialized", __func__);              \
    111       return BT_STATUS_NOT_READY;                                            \
    112     }                                                                        \
    113   } while (0)
    114 
    115 /* Helper macro to avoid code duplication in the state machine handlers */
    116 #define CHECK_RC_EVENT(e, d)       \
    117   case BTA_AV_RC_OPEN_EVT:         \
    118   case BTA_AV_RC_BROWSE_OPEN_EVT:  \
    119   case BTA_AV_RC_CLOSE_EVT:        \
    120   case BTA_AV_RC_BROWSE_CLOSE_EVT: \
    121   case BTA_AV_REMOTE_CMD_EVT:      \
    122   case BTA_AV_VENDOR_CMD_EVT:      \
    123   case BTA_AV_META_MSG_EVT:        \
    124   case BTA_AV_RC_FEAT_EVT:         \
    125   case BTA_AV_REMOTE_RSP_EVT: {    \
    126     btif_rc_handler(e, d);         \
    127   } break;
    128 
    129 static bool btif_av_state_idle_handler(btif_sm_event_t event, void* data);
    130 static bool btif_av_state_opening_handler(btif_sm_event_t event, void* data);
    131 static bool btif_av_state_opened_handler(btif_sm_event_t event, void* data);
    132 static bool btif_av_state_started_handler(btif_sm_event_t event, void* data);
    133 static bool btif_av_state_closing_handler(btif_sm_event_t event, void* data);
    134 
    135 static const btif_sm_handler_t btif_av_state_handlers[] = {
    136     btif_av_state_idle_handler, btif_av_state_opening_handler,
    137     btif_av_state_opened_handler, btif_av_state_started_handler,
    138     btif_av_state_closing_handler};
    139 
    140 static void btif_av_event_free_data(btif_sm_event_t event, void* p_data);
    141 
    142 /*************************************************************************
    143  * Extern functions
    144  ************************************************************************/
    145 extern void btif_rc_handler(tBTA_AV_EVT event, tBTA_AV* p_data);
    146 extern bool btif_rc_get_connected_peer(RawAddress* peer_addr);
    147 extern uint8_t btif_rc_get_connected_peer_handle(const RawAddress& peer_addr);
    148 extern void btif_rc_check_handle_pending_play(const RawAddress& peer_addr,
    149                                               bool bSendToApp);
    150 
    151 /*****************************************************************************
    152  * Local helper functions
    153  *****************************************************************************/
    154 
    155 const char* dump_av_sm_state_name(btif_av_state_t state) {
    156   switch (state) {
    157     CASE_RETURN_STR(BTIF_AV_STATE_IDLE)
    158     CASE_RETURN_STR(BTIF_AV_STATE_OPENING)
    159     CASE_RETURN_STR(BTIF_AV_STATE_OPENED)
    160     CASE_RETURN_STR(BTIF_AV_STATE_STARTED)
    161     CASE_RETURN_STR(BTIF_AV_STATE_CLOSING)
    162     default:
    163       return "UNKNOWN_STATE";
    164   }
    165 }
    166 
    167 const char* dump_av_sm_event_name(btif_av_sm_event_t event) {
    168   switch ((int)event) {
    169     CASE_RETURN_STR(BTA_AV_ENABLE_EVT)
    170     CASE_RETURN_STR(BTA_AV_REGISTER_EVT)
    171     CASE_RETURN_STR(BTA_AV_OPEN_EVT)
    172     CASE_RETURN_STR(BTA_AV_CLOSE_EVT)
    173     CASE_RETURN_STR(BTA_AV_START_EVT)
    174     CASE_RETURN_STR(BTA_AV_STOP_EVT)
    175     CASE_RETURN_STR(BTA_AV_PROTECT_REQ_EVT)
    176     CASE_RETURN_STR(BTA_AV_PROTECT_RSP_EVT)
    177     CASE_RETURN_STR(BTA_AV_RC_OPEN_EVT)
    178     CASE_RETURN_STR(BTA_AV_RC_CLOSE_EVT)
    179     CASE_RETURN_STR(BTA_AV_RC_BROWSE_OPEN_EVT)
    180     CASE_RETURN_STR(BTA_AV_RC_BROWSE_CLOSE_EVT)
    181     CASE_RETURN_STR(BTA_AV_REMOTE_CMD_EVT)
    182     CASE_RETURN_STR(BTA_AV_REMOTE_RSP_EVT)
    183     CASE_RETURN_STR(BTA_AV_VENDOR_CMD_EVT)
    184     CASE_RETURN_STR(BTA_AV_VENDOR_RSP_EVT)
    185     CASE_RETURN_STR(BTA_AV_RECONFIG_EVT)
    186     CASE_RETURN_STR(BTA_AV_SUSPEND_EVT)
    187     CASE_RETURN_STR(BTA_AV_PENDING_EVT)
    188     CASE_RETURN_STR(BTA_AV_META_MSG_EVT)
    189     CASE_RETURN_STR(BTA_AV_REJECT_EVT)
    190     CASE_RETURN_STR(BTA_AV_RC_FEAT_EVT)
    191     CASE_RETURN_STR(BTA_AV_OFFLOAD_START_RSP_EVT)
    192     CASE_RETURN_STR(BTIF_SM_ENTER_EVT)
    193     CASE_RETURN_STR(BTIF_SM_EXIT_EVT)
    194     CASE_RETURN_STR(BTIF_AV_CONNECT_REQ_EVT)
    195     CASE_RETURN_STR(BTIF_AV_DISCONNECT_REQ_EVT)
    196     CASE_RETURN_STR(BTIF_AV_START_STREAM_REQ_EVT)
    197     CASE_RETURN_STR(BTIF_AV_STOP_STREAM_REQ_EVT)
    198     CASE_RETURN_STR(BTIF_AV_SUSPEND_STREAM_REQ_EVT)
    199     CASE_RETURN_STR(BTIF_AV_SOURCE_CONFIG_REQ_EVT)
    200     CASE_RETURN_STR(BTIF_AV_SOURCE_CONFIG_UPDATED_EVT)
    201     CASE_RETURN_STR(BTIF_AV_SINK_CONFIG_REQ_EVT)
    202     CASE_RETURN_STR(BTIF_AV_OFFLOAD_START_REQ_EVT)
    203     default:
    204       return "UNKNOWN_EVENT";
    205   }
    206 }
    207 
    208 /****************************************************************************
    209  *  Local helper functions
    210  ****************************************************************************/
    211 /*******************************************************************************
    212  *
    213  * Function         btif_initiate_av_open_timer_timeout
    214  *
    215  * Description      Timer to trigger AV open if the remote headset establishes
    216  *                  RC connection w/o AV connection. The timer is needed to IOP
    217  *                  with headsets that do establish AV after RC connection.
    218  *
    219  * Returns          void
    220  *
    221  ******************************************************************************/
    222 static void btif_initiate_av_open_timer_timeout(UNUSED_ATTR void* data) {
    223   RawAddress peer_addr;
    224   btif_av_connect_req_t connect_req;
    225 
    226   /* is there at least one RC connection - There should be */
    227   if (btif_rc_get_connected_peer(&peer_addr)) {
    228     BTIF_TRACE_DEBUG("%s: Issuing connect to the remote RC peer", __func__);
    229     /* In case of AVRCP connection request, we will initiate SRC connection */
    230     connect_req.target_bda = &peer_addr;
    231     if (bt_av_sink_callbacks != NULL)
    232       connect_req.uuid = UUID_SERVCLASS_AUDIO_SINK;
    233     else if (bt_av_src_callbacks != NULL)
    234       connect_req.uuid = UUID_SERVCLASS_AUDIO_SOURCE;
    235     btif_dispatch_sm_event(BTIF_AV_CONNECT_REQ_EVT, (char*)&connect_req,
    236                            sizeof(connect_req));
    237   } else {
    238     BTIF_TRACE_ERROR("%s: No connected RC peers", __func__);
    239   }
    240 }
    241 
    242 /*****************************************************************************
    243  *  Static functions
    244  *****************************************************************************/
    245 
    246 /*******************************************************************************
    247  *
    248  * Function         btif_report_connection_state
    249  *
    250  * Description      Updates the components via the callbacks about the
    251  *                  connection state of a2dp connection.
    252  *
    253  * Returns          None
    254  *
    255  ******************************************************************************/
    256 static void btif_report_connection_state(btav_connection_state_t state,
    257                                          RawAddress* bd_addr) {
    258   if (bt_av_sink_callbacks != NULL) {
    259     HAL_CBACK(bt_av_sink_callbacks, connection_state_cb, state, bd_addr);
    260   } else if (bt_av_src_callbacks != NULL) {
    261     HAL_CBACK(bt_av_src_callbacks, connection_state_cb, state, bd_addr);
    262   }
    263 }
    264 
    265 /*******************************************************************************
    266  *
    267  * Function         btif_report_audio_state
    268  *
    269  * Description      Updates the components via the callbacks about the audio
    270  *                  state of a2dp connection. The state is updated when either
    271  *                  the remote ends starts streaming (started state) or whenever
    272  *                  it transitions out of started state (to opened or streaming)
    273  *                  state.
    274  *
    275  * Returns          None
    276  *
    277  ******************************************************************************/
    278 static void btif_report_audio_state(btav_audio_state_t state,
    279                                     RawAddress* bd_addr) {
    280   if (bt_av_sink_callbacks != NULL) {
    281     HAL_CBACK(bt_av_sink_callbacks, audio_state_cb, state, bd_addr);
    282   } else if (bt_av_src_callbacks != NULL) {
    283     HAL_CBACK(bt_av_src_callbacks, audio_state_cb, state, bd_addr);
    284   }
    285 }
    286 
    287 static void btif_update_source_codec(void* p_data) {
    288   BTIF_TRACE_DEBUG("%s", __func__);
    289 
    290   // copy to avoid alignment problems
    291   btav_a2dp_codec_config_t req;
    292   memcpy(&req, p_data, sizeof(req));
    293 
    294   btif_a2dp_source_encoder_user_config_update_req(req);
    295 }
    296 
    297 static void btif_report_source_codec_state(UNUSED_ATTR void* p_data) {
    298   btav_a2dp_codec_config_t codec_config;
    299   std::vector<btav_a2dp_codec_config_t> codecs_local_capabilities;
    300   std::vector<btav_a2dp_codec_config_t> codecs_selectable_capabilities;
    301 
    302   A2dpCodecs* a2dp_codecs = bta_av_get_a2dp_codecs();
    303   if (a2dp_codecs == nullptr) return;
    304   if (!a2dp_codecs->getCodecConfigAndCapabilities(
    305           &codec_config, &codecs_local_capabilities,
    306           &codecs_selectable_capabilities)) {
    307     BTIF_TRACE_WARNING(
    308         "%s: error reporting audio source codec state: "
    309         "cannot get codec config and capabilities",
    310         __func__);
    311     return;
    312   }
    313   if (bt_av_src_callbacks != NULL) {
    314     HAL_CBACK(bt_av_src_callbacks, audio_config_cb, codec_config,
    315               codecs_local_capabilities, codecs_selectable_capabilities);
    316   }
    317 }
    318 
    319 /*****************************************************************************
    320  *
    321  * Function     btif_av_state_idle_handler
    322  *
    323  * Description  State managing disconnected AV link
    324  *
    325  * Returns      true if event was processed, false otherwise
    326  *
    327  ******************************************************************************/
    328 
    329 static bool btif_av_state_idle_handler(btif_sm_event_t event, void* p_data) {
    330   BTIF_TRACE_DEBUG("%s: event=%s flags=0x%x", __func__,
    331                    dump_av_sm_event_name((btif_av_sm_event_t)event),
    332                    btif_av_cb.flags);
    333 
    334   switch (event) {
    335     case BTIF_SM_ENTER_EVT:
    336       /* clear the peer_bda */
    337       btif_av_cb.peer_bda = RawAddress::kEmpty;
    338       btif_av_cb.flags = 0;
    339       btif_av_cb.edr = 0;
    340       bta_av_co_init(btif_av_cb.codec_priorities);
    341       btif_a2dp_on_idle();
    342       break;
    343 
    344     case BTIF_SM_EXIT_EVT:
    345       break;
    346 
    347     case BTA_AV_ENABLE_EVT:
    348       break;
    349 
    350     case BTA_AV_REGISTER_EVT:
    351       btif_av_cb.bta_handle = ((tBTA_AV*)p_data)->registr.hndl;
    352       break;
    353 
    354     case BTA_AV_PENDING_EVT:
    355     case BTIF_AV_CONNECT_REQ_EVT: {
    356       if (event == BTIF_AV_CONNECT_REQ_EVT) {
    357         btif_av_connect_req_t* connect_req_p = (btif_av_connect_req_t*)p_data;
    358         btif_av_cb.peer_bda = *connect_req_p->target_bda;
    359         btif_av_cb.self_initiated_connection = true;
    360         BTA_AvOpen(btif_av_cb.peer_bda, btif_av_cb.bta_handle, true,
    361                    BTA_SEC_AUTHENTICATE, connect_req_p->uuid);
    362       } else if (event == BTA_AV_PENDING_EVT) {
    363         btif_av_cb.peer_bda = ((tBTA_AV*)p_data)->pend.bd_addr;
    364         btif_av_cb.self_initiated_connection = false;
    365         if (bt_av_src_callbacks != NULL) {
    366           BTA_AvOpen(btif_av_cb.peer_bda, btif_av_cb.bta_handle, true,
    367                      BTA_SEC_AUTHENTICATE, UUID_SERVCLASS_AUDIO_SOURCE);
    368         }
    369         if (bt_av_sink_callbacks != NULL) {
    370           BTA_AvOpen(btif_av_cb.peer_bda, btif_av_cb.bta_handle, true,
    371                      BTA_SEC_AUTHENTICATE, UUID_SERVCLASS_AUDIO_SINK);
    372         }
    373       }
    374       btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_OPENING);
    375     } break;
    376 
    377     case BTA_AV_RC_OPEN_EVT:
    378       /* IOP_FIX: Jabra 620 only does RC open without AV open whenever it
    379        * connects. So
    380        * as per the AV WP, an AVRC connection cannot exist without an AV
    381        * connection. Therefore,
    382        * we initiate an AV connection if an RC_OPEN_EVT is received when we are
    383        * in AV_CLOSED state.
    384        * We initiate the AV connection after a small 3s timeout to avoid any
    385        * collisions from the
    386        * headsets, as some headsets initiate the AVRC connection first and then
    387        * immediately initiate the AV connection
    388        *
    389        * TODO: We may need to do this only on an AVRCP Play. FixMe
    390        */
    391 
    392       BTIF_TRACE_WARNING("%s: BTA_AV_RC_OPEN_EVT received w/o AV", __func__);
    393       alarm_set_on_mloop(av_open_on_rc_timer, BTIF_TIMEOUT_AV_OPEN_ON_RC_MS,
    394                          btif_initiate_av_open_timer_timeout, NULL);
    395       btif_rc_handler(event, (tBTA_AV*)p_data);
    396       break;
    397 
    398     case BTA_AV_RC_BROWSE_OPEN_EVT:
    399       BTIF_TRACE_DEBUG("%s: BTA_AV_RC_BROWSE_OPEN_EVT received", __func__);
    400       btif_rc_handler(event, (tBTA_AV*)p_data);
    401       break;
    402 
    403     case BTIF_AV_SOURCE_CONFIG_REQ_EVT:
    404       btif_update_source_codec(p_data);
    405       break;
    406 
    407     case BTIF_AV_SOURCE_CONFIG_UPDATED_EVT:
    408       btif_report_source_codec_state(p_data);
    409       break;
    410 
    411     /*
    412      * In case Signalling channel is not down
    413      * and remote started Streaming Procedure
    414      * we have to handle config and open event in
    415      * idle_state. We hit these scenarios while running
    416      * PTS test case for AVRCP Controller
    417      */
    418     case BTIF_AV_SINK_CONFIG_REQ_EVT: {
    419       btif_av_sink_config_req_t req;
    420       // copy to avoid alignment problems
    421       memcpy(&req, p_data, sizeof(req));
    422 
    423       BTIF_TRACE_WARNING(
    424           "%s: BTIF_AV_SINK_CONFIG_REQ_EVT sample_rate=%d "
    425           "channel_count=%d",
    426           __func__, req.sample_rate, req.channel_count);
    427       if (bt_av_sink_callbacks != NULL) {
    428         HAL_CBACK(bt_av_sink_callbacks, audio_config_cb, &(req.peer_bd),
    429                   req.sample_rate, req.channel_count);
    430       }
    431     } break;
    432 
    433     case BTA_AV_OPEN_EVT: {
    434       tBTA_AV* p_bta_data = (tBTA_AV*)p_data;
    435       btav_connection_state_t state;
    436       btif_sm_state_t av_state;
    437       BTIF_TRACE_WARNING("%s: BTA_AV_OPEN_EVT status=%d, edr=0x%x", __func__,
    438                          p_bta_data->open.status, p_bta_data->open.edr);
    439 
    440       if (p_bta_data->open.status == BTA_AV_SUCCESS) {
    441         state = BTAV_CONNECTION_STATE_CONNECTED;
    442         av_state = BTIF_AV_STATE_OPENED;
    443         btif_av_cb.edr = p_bta_data->open.edr;
    444 
    445         btif_av_cb.peer_sep = p_bta_data->open.sep;
    446       } else {
    447         BTIF_TRACE_WARNING("%s: BTA_AV_OPEN_EVT::FAILED status=%d", __func__,
    448                            p_bta_data->open.status);
    449         state = BTAV_CONNECTION_STATE_DISCONNECTED;
    450         av_state = BTIF_AV_STATE_IDLE;
    451       }
    452 
    453       /* inform the application of the event */
    454       btif_report_connection_state(state, &(btif_av_cb.peer_bda));
    455       /* change state to open/idle based on the status */
    456       btif_sm_change_state(btif_av_cb.sm_handle, av_state);
    457       if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) {
    458         /* if queued PLAY command,  send it now */
    459         btif_rc_check_handle_pending_play(
    460             p_bta_data->open.bd_addr,
    461             (p_bta_data->open.status == BTA_AV_SUCCESS));
    462       } else if ((btif_av_cb.peer_sep == AVDT_TSEP_SRC) &&
    463                  (p_bta_data->open.status == BTA_AV_SUCCESS)) {
    464         /* Bring up AVRCP connection too */
    465         BTA_AvOpenRc(btif_av_cb.bta_handle);
    466       }
    467       btif_queue_advance();
    468     } break;
    469 
    470     case BTA_AV_REMOTE_CMD_EVT:
    471     case BTA_AV_VENDOR_CMD_EVT:
    472     case BTA_AV_META_MSG_EVT:
    473     case BTA_AV_RC_FEAT_EVT:
    474     case BTA_AV_REMOTE_RSP_EVT:
    475       btif_rc_handler(event, (tBTA_AV*)p_data);
    476       break;
    477 
    478     case BTA_AV_RC_CLOSE_EVT:
    479       BTIF_TRACE_DEBUG("%s: BTA_AV_RC_CLOSE_EVT: Stopping AV timer.", __func__);
    480       alarm_cancel(av_open_on_rc_timer);
    481       btif_rc_handler(event, (tBTA_AV*)p_data);
    482       break;
    483 
    484     case BTIF_AV_OFFLOAD_START_REQ_EVT:
    485       BTIF_TRACE_ERROR(
    486           "%s: BTIF_AV_OFFLOAD_START_REQ_EVT: Stream not Started IDLE",
    487           __func__);
    488       btif_a2dp_on_offload_started(BTA_AV_FAIL);
    489       break;
    490 
    491     default:
    492       BTIF_TRACE_WARNING("%s: unhandled event=%s", __func__,
    493                          dump_av_sm_event_name((btif_av_sm_event_t)event));
    494       return false;
    495   }
    496 
    497   return true;
    498 }
    499 /*****************************************************************************
    500  *
    501  * Function        btif_av_state_opening_handler
    502  *
    503  * Description     Intermediate state managing events during establishment
    504  *                 of avdtp channel
    505  *
    506  * Returns         true if event was processed, false otherwise
    507  *
    508  ******************************************************************************/
    509 
    510 static bool btif_av_state_opening_handler(btif_sm_event_t event, void* p_data) {
    511   BTIF_TRACE_DEBUG("%s: event=%s flags=0x%x", __func__,
    512                    dump_av_sm_event_name((btif_av_sm_event_t)event),
    513                    btif_av_cb.flags);
    514 
    515   switch (event) {
    516     case BTIF_SM_ENTER_EVT:
    517       /* inform the application that we are entering connecting state */
    518       btif_report_connection_state(BTAV_CONNECTION_STATE_CONNECTING,
    519                                    &(btif_av_cb.peer_bda));
    520       break;
    521 
    522     case BTIF_SM_EXIT_EVT:
    523       break;
    524 
    525     case BTA_AV_REJECT_EVT:
    526       BTIF_TRACE_WARNING("%s: Received BTA_AV_REJECT_EVT", __func__);
    527       btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
    528                                    &(btif_av_cb.peer_bda));
    529       btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE);
    530       if (btif_av_cb.self_initiated_connection) {
    531         btif_queue_advance();
    532       }
    533       break;
    534 
    535     case BTA_AV_OPEN_EVT: {
    536       tBTA_AV* p_bta_data = (tBTA_AV*)p_data;
    537       btav_connection_state_t state;
    538       btif_sm_state_t av_state;
    539       BTIF_TRACE_WARNING("%s: BTA_AV_OPEN_EVT status=%d, edr=0x%x", __func__,
    540                          p_bta_data->open.status, p_bta_data->open.edr);
    541 
    542       if (p_bta_data->open.status == BTA_AV_SUCCESS) {
    543         state = BTAV_CONNECTION_STATE_CONNECTED;
    544         av_state = BTIF_AV_STATE_OPENED;
    545         btif_av_cb.edr = p_bta_data->open.edr;
    546 
    547         btif_av_cb.peer_sep = p_bta_data->open.sep;
    548       } else {
    549         BTIF_TRACE_WARNING("%s: BTA_AV_OPEN_EVT::FAILED status: %d", __func__,
    550                            p_bta_data->open.status);
    551         RawAddress peer_addr;
    552         uint8_t peer_handle = BTRC_HANDLE_NONE;
    553         if (btif_rc_get_connected_peer(&peer_addr) &&
    554             btif_av_cb.peer_bda == peer_addr) {
    555           /*
    556            * Disconnect AVRCP connection, if
    557            * A2DP conneciton failed, for any reason
    558            */
    559           BTIF_TRACE_WARNING("%s: Disconnecting AVRCP: peer_addr=%s", __func__,
    560                              peer_addr.ToString().c_str());
    561           peer_handle = btif_rc_get_connected_peer_handle(peer_addr);
    562           if (peer_handle != BTRC_HANDLE_NONE) {
    563             BTA_AvCloseRc(peer_handle);
    564           }
    565         }
    566         state = BTAV_CONNECTION_STATE_DISCONNECTED;
    567         av_state = BTIF_AV_STATE_IDLE;
    568       }
    569 
    570       /* inform the application of the event */
    571       btif_report_connection_state(state, &(btif_av_cb.peer_bda));
    572       /* change state to open/idle based on the status */
    573       btif_sm_change_state(btif_av_cb.sm_handle, av_state);
    574       if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) {
    575         /* if queued PLAY command,  send it now */
    576         btif_rc_check_handle_pending_play(
    577             p_bta_data->open.bd_addr,
    578             (p_bta_data->open.status == BTA_AV_SUCCESS));
    579       } else if ((btif_av_cb.peer_sep == AVDT_TSEP_SRC) &&
    580                  (p_bta_data->open.status == BTA_AV_SUCCESS)) {
    581         /* Bring up AVRCP connection too */
    582         BTA_AvOpenRc(btif_av_cb.bta_handle);
    583       }
    584       if (btif_av_cb.self_initiated_connection) {
    585         btif_queue_advance();
    586       }
    587     } break;
    588 
    589     case BTIF_AV_SOURCE_CONFIG_REQ_EVT:
    590       btif_update_source_codec(p_data);
    591       break;
    592 
    593     case BTIF_AV_SOURCE_CONFIG_UPDATED_EVT:
    594       btif_report_source_codec_state(p_data);
    595       break;
    596 
    597     case BTIF_AV_SINK_CONFIG_REQ_EVT: {
    598       btif_av_sink_config_req_t req;
    599       // copy to avoid alignment problems
    600       memcpy(&req, p_data, sizeof(req));
    601 
    602       BTIF_TRACE_WARNING(
    603           "%s: BTIF_AV_SINK_CONFIG_REQ_EVT sample_rate=%d "
    604           "channel_count=%d",
    605           __func__, req.sample_rate, req.channel_count);
    606       if (btif_av_cb.peer_sep == AVDT_TSEP_SRC &&
    607           bt_av_sink_callbacks != NULL) {
    608         HAL_CBACK(bt_av_sink_callbacks, audio_config_cb, &(btif_av_cb.peer_bda),
    609                   req.sample_rate, req.channel_count);
    610       }
    611     } break;
    612 
    613     case BTIF_AV_CONNECT_REQ_EVT: {
    614       // Check for device, if same device which moved to opening then ignore
    615       // callback
    616       btif_av_connect_req_t* connect_req_p = (btif_av_connect_req_t*)p_data;
    617       RawAddress& target_bda = *connect_req_p->target_bda;
    618       if (btif_av_cb.peer_bda == target_bda) {
    619         BTIF_TRACE_WARNING(
    620             "%s: device %s is already connecting, ignore Connect request",
    621             __func__, btif_av_cb.peer_bda.ToString().c_str());
    622       } else {
    623         BTIF_TRACE_WARNING(
    624             "%s: device %s is already connecting, reject Connect request to %s",
    625             __func__, btif_av_cb.peer_bda.ToString().c_str(),
    626             target_bda.ToString().c_str());
    627         btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
    628                                      &target_bda);
    629       }
    630       // Ignore all connection request if we are already opening
    631       btif_queue_advance();
    632     } break;
    633 
    634     case BTA_AV_PENDING_EVT: {
    635       // Check for device, if same device which moved to opening then ignore
    636       // callback
    637       const RawAddress& bd_addr = ((tBTA_AV*)p_data)->pend.bd_addr;
    638       if (bd_addr == btif_av_cb.peer_bda) {
    639         BTIF_TRACE_WARNING(
    640             "%s: device %s is already connecting, ignore incoming request",
    641             __func__, btif_av_cb.peer_bda.ToString().c_str());
    642       } else {
    643         BTIF_TRACE_WARNING(
    644             "%s: device %s is already connecting, reject incoming request "
    645             "from %s",
    646             __func__, btif_av_cb.peer_bda.ToString().c_str(),
    647             bd_addr.ToString().c_str());
    648         BTA_AvDisconnect(bd_addr);
    649       }
    650     } break;
    651 
    652     case BTIF_AV_OFFLOAD_START_REQ_EVT:
    653       BTIF_TRACE_ERROR(
    654           "%s: BTIF_AV_OFFLOAD_START_REQ_EVT: Stream not Started OPENING",
    655           __func__);
    656       btif_a2dp_on_offload_started(BTA_AV_FAIL);
    657       break;
    658 
    659     case BTA_AV_CLOSE_EVT:
    660       btif_a2dp_on_stopped(NULL);
    661       btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
    662                                    &(btif_av_cb.peer_bda));
    663       btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE);
    664       if (btif_av_cb.self_initiated_connection) {
    665         btif_queue_advance();
    666       }
    667       break;
    668 
    669     case BTIF_AV_DISCONNECT_REQ_EVT:
    670       btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
    671                                    &(btif_av_cb.peer_bda));
    672       BTA_AvClose(btif_av_cb.bta_handle);
    673       btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE);
    674       if (btif_av_cb.self_initiated_connection) {
    675         btif_queue_advance();
    676       }
    677       break;
    678 
    679       CHECK_RC_EVENT(event, (tBTA_AV*)p_data);
    680 
    681     default:
    682       BTIF_TRACE_WARNING("%s: unhandled event=%s", __func__,
    683                          dump_av_sm_event_name((btif_av_sm_event_t)event));
    684       return false;
    685   }
    686   return true;
    687 }
    688 
    689 /*****************************************************************************
    690  *
    691  * Function        btif_av_state_closing_handler
    692  *
    693  * Description     Intermediate state managing events during closing
    694  *                 of avdtp channel
    695  *
    696  * Returns         true if event was processed, false otherwise
    697  *
    698  ******************************************************************************/
    699 
    700 static bool btif_av_state_closing_handler(btif_sm_event_t event, void* p_data) {
    701   BTIF_TRACE_DEBUG("%s: event=%s flags=0x%x", __func__,
    702                    dump_av_sm_event_name((btif_av_sm_event_t)event),
    703                    btif_av_cb.flags);
    704 
    705   switch (event) {
    706     case BTIF_SM_ENTER_EVT:
    707       if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) {
    708         /* immediately stop transmission of frames */
    709         btif_a2dp_source_set_tx_flush(true);
    710         /* wait for audioflinger to stop a2dp */
    711       }
    712       if (btif_av_cb.peer_sep == AVDT_TSEP_SRC) {
    713         btif_a2dp_sink_set_rx_flush(true);
    714       }
    715       break;
    716 
    717     case BTA_AV_STOP_EVT:
    718     case BTIF_AV_STOP_STREAM_REQ_EVT:
    719       btif_a2dp_on_stopped(NULL);
    720       break;
    721 
    722     case BTIF_SM_EXIT_EVT:
    723       break;
    724 
    725     case BTIF_AV_SOURCE_CONFIG_REQ_EVT:
    726       btif_update_source_codec(p_data);
    727       break;
    728 
    729     case BTIF_AV_SOURCE_CONFIG_UPDATED_EVT:
    730       btif_report_source_codec_state(p_data);
    731       break;
    732 
    733     case BTA_AV_CLOSE_EVT:
    734 
    735       /* inform the application that we are disconnecting */
    736       btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
    737                                    &(btif_av_cb.peer_bda));
    738 
    739       btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE);
    740       break;
    741 
    742     /* Handle the RC_CLOSE event for the cleanup */
    743     case BTA_AV_RC_CLOSE_EVT:
    744       btif_rc_handler(event, (tBTA_AV*)p_data);
    745       break;
    746 
    747     /* Handle the RC_BROWSE_CLOSE event for tetsing*/
    748     case BTA_AV_RC_BROWSE_CLOSE_EVT:
    749       btif_rc_handler(event, (tBTA_AV*)p_data);
    750       break;
    751 
    752     case BTIF_AV_OFFLOAD_START_REQ_EVT:
    753       BTIF_TRACE_ERROR(
    754           "%s: BTIF_AV_OFFLOAD_START_REQ_EVT: Stream not Started Closing",
    755           __func__);
    756       btif_a2dp_on_offload_started(BTA_AV_FAIL);
    757       break;
    758 
    759     default:
    760       BTIF_TRACE_WARNING("%s: unhandled event=%s", __func__,
    761                          dump_av_sm_event_name((btif_av_sm_event_t)event));
    762       return false;
    763   }
    764   return true;
    765 }
    766 
    767 /*****************************************************************************
    768  *
    769  * Function     btif_av_state_opened_handler
    770  *
    771  * Description  Handles AV events while AVDTP is in OPEN state
    772  *
    773  * Returns      true if event was processed, false otherwise
    774  *
    775  ******************************************************************************/
    776 
    777 static bool btif_av_state_opened_handler(btif_sm_event_t event, void* p_data) {
    778   tBTA_AV* p_av = (tBTA_AV*)p_data;
    779 
    780   BTIF_TRACE_DEBUG("%s: event=%s flags=0x%x", __func__,
    781                    dump_av_sm_event_name((btif_av_sm_event_t)event),
    782                    btif_av_cb.flags);
    783 
    784   if ((event == BTA_AV_REMOTE_CMD_EVT) &&
    785       (btif_av_cb.flags & BTIF_AV_FLAG_REMOTE_SUSPEND) &&
    786       (p_av->remote_cmd.rc_id == BTA_AV_RC_PLAY)) {
    787     BTIF_TRACE_EVENT("%s: Resetting remote suspend flag on RC PLAY", __func__);
    788     btif_av_cb.flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;
    789   }
    790 
    791   switch (event) {
    792     case BTIF_SM_ENTER_EVT:
    793       btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_STOP;
    794       btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_START;
    795       break;
    796 
    797     case BTIF_SM_EXIT_EVT:
    798       btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_START;
    799       break;
    800 
    801     case BTIF_AV_START_STREAM_REQ_EVT:
    802       if (btif_av_cb.peer_sep != AVDT_TSEP_SRC) btif_a2dp_source_setup_codec();
    803       BTA_AvStart();
    804       btif_av_cb.flags |= BTIF_AV_FLAG_PENDING_START;
    805       break;
    806 
    807     case BTA_AV_START_EVT: {
    808       BTIF_TRACE_WARNING(
    809           "%s: BTA_AV_START_EVT status=%d suspending=%d initiator=%d "
    810           "flags=0x%x",
    811           __func__, p_av->start.status, p_av->start.suspending,
    812           p_av->start.initiator, btif_av_cb.flags);
    813 
    814       if ((p_av->start.status == BTA_SUCCESS) &&
    815           (p_av->start.suspending == true))
    816         return true;
    817 
    818       /* if remote tries to start a2dp when DUT is a2dp source
    819        * then suspend. In case a2dp is sink and call is active
    820        * then disconnect the AVDTP channel
    821        */
    822       if (!(btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START)) {
    823         if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) {
    824           BTIF_TRACE_WARNING("%s: trigger suspend as remote initiated!!",
    825                              __func__);
    826           btif_dispatch_sm_event(BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL, 0);
    827         }
    828       }
    829 
    830       /*  In case peer is A2DP SRC we do not want to ack commands on UIPC*/
    831       if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) {
    832         if (btif_a2dp_on_started(
    833                 &p_av->start,
    834                 ((btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) != 0))) {
    835           /* only clear pending flag after acknowledgement */
    836           btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_START;
    837         }
    838       }
    839 
    840       /* remain in open state if status failed */
    841       if (p_av->start.status != BTA_AV_SUCCESS) return false;
    842 
    843       if (btif_av_cb.peer_sep == AVDT_TSEP_SRC) {
    844         btif_a2dp_sink_set_rx_flush(
    845             false); /*  remove flush state, ready for streaming*/
    846       }
    847 
    848       /* change state to started, send acknowledgement if start is pending */
    849       if (btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) {
    850         if (btif_av_cb.peer_sep == AVDT_TSEP_SNK)
    851           btif_a2dp_on_started(NULL, true);
    852         /* pending start flag will be cleared when exit current state */
    853       }
    854       btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_STARTED);
    855 
    856     } break;
    857 
    858     case BTIF_AV_SOURCE_CONFIG_REQ_EVT:
    859       btif_update_source_codec(p_data);
    860       break;
    861 
    862     case BTIF_AV_SOURCE_CONFIG_UPDATED_EVT:
    863       btif_report_source_codec_state(p_data);
    864       break;
    865 
    866     case BTIF_AV_DISCONNECT_REQ_EVT:
    867       BTA_AvClose(btif_av_cb.bta_handle);
    868       if (btif_av_cb.peer_sep == AVDT_TSEP_SRC) {
    869         BTA_AvCloseRc(btif_av_cb.bta_handle);
    870       }
    871 
    872       /* inform the application that we are disconnecting */
    873       btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTING,
    874                                    &(btif_av_cb.peer_bda));
    875       break;
    876 
    877     case BTA_AV_CLOSE_EVT:
    878       /* avdtp link is closed */
    879       btif_a2dp_on_stopped(NULL);
    880 
    881       /* inform the application that we are disconnected */
    882       btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
    883                                    &(btif_av_cb.peer_bda));
    884 
    885       /* change state to idle, send acknowledgement if start is pending */
    886       if (btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) {
    887         btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
    888         /* pending start flag will be cleared when exit current state */
    889       }
    890       btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE);
    891       break;
    892 
    893     case BTA_AV_RECONFIG_EVT:
    894       if ((btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) &&
    895           (p_av->reconfig.status == BTA_AV_SUCCESS)) {
    896         APPL_TRACE_WARNING("reconfig done BTA_AVstart()");
    897         BTA_AvStart();
    898       } else if (btif_av_cb.flags & BTIF_AV_FLAG_PENDING_START) {
    899         btif_av_cb.flags &= ~BTIF_AV_FLAG_PENDING_START;
    900         btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
    901       }
    902       break;
    903 
    904     case BTIF_AV_CONNECT_REQ_EVT: {
    905       btif_av_connect_req_t* connect_req_p = (btif_av_connect_req_t*)p_data;
    906       RawAddress& target_bda = *connect_req_p->target_bda;
    907       if (btif_av_cb.peer_bda == target_bda) {
    908         BTIF_TRACE_WARNING(
    909             "%s: Ignore BTIF_AV_CONNECT_REQ_EVT for same device: target_bda=%s",
    910             __func__, target_bda.ToString().c_str());
    911       } else {
    912         BTIF_TRACE_WARNING(
    913             "%s: Moved to opened by Other incoming Connect request: "
    914             "target_bda=%s",
    915             __func__, target_bda.ToString().c_str());
    916         btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
    917                                      &target_bda);
    918       }
    919       btif_queue_advance();
    920     } break;
    921 
    922     case BTIF_AV_OFFLOAD_START_REQ_EVT:
    923       BTIF_TRACE_ERROR(
    924           "%s: BTIF_AV_OFFLOAD_START_REQ_EVT: Stream not Started Opened",
    925           __func__);
    926       btif_a2dp_on_offload_started(BTA_AV_FAIL);
    927       break;
    928 
    929       CHECK_RC_EVENT(event, (tBTA_AV*)p_data);
    930 
    931     default:
    932       BTIF_TRACE_WARNING("%s: unhandled event=%s", __func__,
    933                          dump_av_sm_event_name((btif_av_sm_event_t)event));
    934       return false;
    935   }
    936   return true;
    937 }
    938 
    939 /*****************************************************************************
    940  *
    941  * Function     btif_av_state_started_handler
    942  *
    943  * Description  Handles AV events while A2DP stream is started
    944  *
    945  * Returns      true if event was processed, false otherwise
    946  *
    947  ******************************************************************************/
    948 
    949 static bool btif_av_state_started_handler(btif_sm_event_t event, void* p_data) {
    950   tBTA_AV* p_av = (tBTA_AV*)p_data;
    951 
    952   BTIF_TRACE_DEBUG("%s: event=%s flags=0x%x", __func__,
    953                    dump_av_sm_event_name((btif_av_sm_event_t)event),
    954                    btif_av_cb.flags);
    955 
    956   switch (event) {
    957     case BTIF_SM_ENTER_EVT:
    958 
    959       /* we are again in started state, clear any remote suspend flags */
    960       btif_av_cb.flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;
    961 
    962       /**
    963        * Report to components above that we have entered the streaming
    964        * stage, this should usually be followed by focus grant.
    965        * see update_audio_focus_state()
    966        */
    967       btif_report_audio_state(BTAV_AUDIO_STATE_STARTED, &(btif_av_cb.peer_bda));
    968       break;
    969 
    970     case BTIF_SM_EXIT_EVT:
    971       break;
    972 
    973     case BTIF_AV_START_STREAM_REQ_EVT:
    974       /* we were remotely started, just ack back the local request */
    975       if (btif_av_cb.peer_sep == AVDT_TSEP_SNK)
    976         btif_a2dp_on_started(NULL, true);
    977       break;
    978 
    979     case BTIF_AV_SOURCE_CONFIG_REQ_EVT:
    980       btif_update_source_codec(p_data);
    981       break;
    982 
    983     case BTIF_AV_SOURCE_CONFIG_UPDATED_EVT:
    984       btif_report_source_codec_state(p_data);
    985       break;
    986 
    987     /* fixme -- use suspend = true always to work around issue with BTA AV */
    988     case BTIF_AV_STOP_STREAM_REQ_EVT:
    989     case BTIF_AV_SUSPEND_STREAM_REQ_EVT:
    990       BTIF_TRACE_WARNING("%s: event=%s flags=0x%x", __func__,
    991                          dump_av_sm_event_name((btif_av_sm_event_t)event),
    992                          btif_av_cb.flags);
    993       /* set pending flag to ensure btif task is not trying to restart
    994          stream while suspend is in progress */
    995       btif_av_cb.flags |= BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING;
    996 
    997       /* if we were remotely suspended but suspend locally, local suspend
    998          always overrides */
    999       btif_av_cb.flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;
   1000 
   1001       if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) {
   1002         /*
   1003          * Immediately stop transmission of frames while suspend is
   1004          * pending.
   1005          */
   1006         btif_a2dp_source_set_tx_flush(true);
   1007       }
   1008 
   1009       if (btif_av_cb.peer_sep == AVDT_TSEP_SRC) {
   1010         btif_a2dp_on_stopped(NULL);
   1011       }
   1012 
   1013       BTA_AvStop(true);
   1014       break;
   1015 
   1016     case BTIF_AV_DISCONNECT_REQ_EVT:
   1017       BTIF_TRACE_WARNING("%s: event=%s flags=0x%x", __func__,
   1018                          dump_av_sm_event_name((btif_av_sm_event_t)event),
   1019                          btif_av_cb.flags);
   1020 
   1021       /* request avdtp to close */
   1022       BTA_AvClose(btif_av_cb.bta_handle);
   1023       if (btif_av_cb.peer_sep == AVDT_TSEP_SRC) {
   1024         BTA_AvCloseRc(btif_av_cb.bta_handle);
   1025       }
   1026 
   1027       /* inform the application that we are disconnecting */
   1028       btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTING,
   1029                                    &(btif_av_cb.peer_bda));
   1030 
   1031       /* wait in closing state until fully closed */
   1032       btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_CLOSING);
   1033       break;
   1034 
   1035     case BTA_AV_SUSPEND_EVT:
   1036       BTIF_TRACE_WARNING(
   1037           "%s: BTA_AV_SUSPEND_EVT status=%d initiator=%d flags=0x%x", __func__,
   1038           p_av->suspend.status, p_av->suspend.initiator, btif_av_cb.flags);
   1039 
   1040       /* a2dp suspended, stop media task until resumed */
   1041       btif_a2dp_on_suspended(&p_av->suspend);
   1042 
   1043       /* if not successful, remain in current state */
   1044       if (p_av->suspend.status != BTA_AV_SUCCESS) {
   1045         btif_av_cb.flags &= ~BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING;
   1046 
   1047         if (btif_av_cb.peer_sep == AVDT_TSEP_SNK) {
   1048           /* suspend failed, reset back tx flush state */
   1049           btif_a2dp_source_set_tx_flush(false);
   1050         }
   1051         return false;
   1052       }
   1053 
   1054       if (p_av->suspend.initiator != true) {
   1055         /* remote suspend, notify HAL and await audioflinger to
   1056            suspend/stop stream */
   1057 
   1058         /* set remote suspend flag to block media task from restarting
   1059            stream only if we did not already initiate a local suspend */
   1060         if ((btif_av_cb.flags & BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING) == 0)
   1061           btif_av_cb.flags |= BTIF_AV_FLAG_REMOTE_SUSPEND;
   1062 
   1063         btif_report_audio_state(BTAV_AUDIO_STATE_REMOTE_SUSPEND,
   1064                                 &(btif_av_cb.peer_bda));
   1065       } else {
   1066         btif_report_audio_state(BTAV_AUDIO_STATE_STOPPED,
   1067                                 &(btif_av_cb.peer_bda));
   1068       }
   1069 
   1070       btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_OPENED);
   1071 
   1072       /* suspend completed and state changed, clear pending status */
   1073       btif_av_cb.flags &= ~BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING;
   1074       break;
   1075 
   1076     case BTA_AV_STOP_EVT:
   1077       BTIF_TRACE_WARNING("%s: event=%s flags=0x%x", __func__,
   1078                          dump_av_sm_event_name((btif_av_sm_event_t)event),
   1079                          btif_av_cb.flags);
   1080 
   1081       btif_av_cb.flags |= BTIF_AV_FLAG_PENDING_STOP;
   1082       btif_a2dp_on_stopped(&p_av->suspend);
   1083 
   1084       btif_report_audio_state(BTAV_AUDIO_STATE_STOPPED, &(btif_av_cb.peer_bda));
   1085 
   1086       /* if stop was successful, change state to open */
   1087       if (p_av->suspend.status == BTA_AV_SUCCESS)
   1088         btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_OPENED);
   1089 
   1090       break;
   1091 
   1092     case BTA_AV_CLOSE_EVT:
   1093       BTIF_TRACE_WARNING("%s: event=%s flags=0x%x", __func__,
   1094                          dump_av_sm_event_name((btif_av_sm_event_t)event),
   1095                          btif_av_cb.flags);
   1096 
   1097       btif_av_cb.flags |= BTIF_AV_FLAG_PENDING_STOP;
   1098 
   1099       /* avdtp link is closed */
   1100       btif_a2dp_on_stopped(NULL);
   1101 
   1102       /* inform the application that we are disconnected */
   1103       btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
   1104                                    &(btif_av_cb.peer_bda));
   1105 
   1106       btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE);
   1107       break;
   1108 
   1109     case BTIF_AV_OFFLOAD_START_REQ_EVT:
   1110       BTA_AvOffloadStart(btif_av_cb.bta_handle);
   1111       break;
   1112 
   1113     case BTA_AV_OFFLOAD_START_RSP_EVT:
   1114       btif_a2dp_on_offload_started(p_av->status);
   1115       break;
   1116 
   1117       CHECK_RC_EVENT(event, (tBTA_AV*)p_data);
   1118 
   1119     default:
   1120       BTIF_TRACE_WARNING("%s: unhandled event=%s", __func__,
   1121                          dump_av_sm_event_name((btif_av_sm_event_t)event));
   1122       return false;
   1123   }
   1124 
   1125   return true;
   1126 }
   1127 
   1128 /*****************************************************************************
   1129  *  Local event handlers
   1130  *****************************************************************************/
   1131 
   1132 static void btif_av_handle_event(uint16_t event, char* p_param) {
   1133   BTIF_TRACE_EVENT("%s: event=%s", __func__,
   1134                    dump_av_sm_event_name((btif_av_sm_event_t)event));
   1135   switch (event) {
   1136     case BTIF_AV_CLEANUP_REQ_EVT:
   1137       btif_a2dp_source_shutdown();
   1138       btif_a2dp_sink_shutdown();
   1139       break;
   1140 
   1141     case BTA_AV_REGISTER_EVT:
   1142       if (btif_av_cb.sm_handle == NULL) {
   1143         btif_av_cb.bta_handle = ((tBTA_AV*)p_param)->registr.hndl;
   1144         BTIF_TRACE_DEBUG("%s: BTA AV Handle updated", __func__);
   1145       }
   1146     /* FALLTHROUGH */
   1147     default:
   1148       btif_sm_dispatch(btif_av_cb.sm_handle, event, (void*)p_param);
   1149       btif_av_event_free_data(event, p_param);
   1150   }
   1151 }
   1152 
   1153 void btif_av_event_deep_copy(uint16_t event, char* p_dest, char* p_src) {
   1154   BTIF_TRACE_DEBUG("%s", __func__);
   1155   tBTA_AV* av_src = (tBTA_AV*)p_src;
   1156   tBTA_AV* av_dest = (tBTA_AV*)p_dest;
   1157 
   1158   // First copy the structure
   1159   maybe_non_aligned_memcpy(av_dest, av_src, sizeof(*av_src));
   1160   switch (event) {
   1161     case BTA_AV_META_MSG_EVT:
   1162       if (av_src->meta_msg.p_data && av_src->meta_msg.len) {
   1163         av_dest->meta_msg.p_data = (uint8_t*)osi_calloc(av_src->meta_msg.len);
   1164         memcpy(av_dest->meta_msg.p_data, av_src->meta_msg.p_data,
   1165                av_src->meta_msg.len);
   1166       }
   1167 
   1168       if (av_src->meta_msg.p_msg) {
   1169         av_dest->meta_msg.p_msg = (tAVRC_MSG*)osi_calloc(sizeof(tAVRC_MSG));
   1170         memcpy(av_dest->meta_msg.p_msg, av_src->meta_msg.p_msg,
   1171                sizeof(tAVRC_MSG));
   1172 
   1173         tAVRC_MSG* p_msg_src = av_src->meta_msg.p_msg;
   1174         tAVRC_MSG* p_msg_dest = av_dest->meta_msg.p_msg;
   1175 
   1176         if ((p_msg_src->hdr.opcode == AVRC_OP_VENDOR) &&
   1177             (p_msg_src->vendor.p_vendor_data && p_msg_src->vendor.vendor_len)) {
   1178           p_msg_dest->vendor.p_vendor_data =
   1179               (uint8_t*)osi_calloc(p_msg_src->vendor.vendor_len);
   1180           memcpy(p_msg_dest->vendor.p_vendor_data,
   1181                  p_msg_src->vendor.p_vendor_data, p_msg_src->vendor.vendor_len);
   1182         }
   1183       }
   1184       break;
   1185 
   1186     default:
   1187       break;
   1188   }
   1189 }
   1190 
   1191 static void btif_av_event_free_data(btif_sm_event_t event, void* p_data) {
   1192   switch (event) {
   1193     case BTA_AV_META_MSG_EVT: {
   1194       tBTA_AV* av = (tBTA_AV*)p_data;
   1195       osi_free_and_reset((void**)&av->meta_msg.p_data);
   1196 
   1197       if (av->meta_msg.p_msg) {
   1198         if (av->meta_msg.p_msg->hdr.opcode == AVRC_OP_VENDOR) {
   1199           osi_free(av->meta_msg.p_msg->vendor.p_vendor_data);
   1200         }
   1201         osi_free_and_reset((void**)&av->meta_msg.p_msg);
   1202       }
   1203     } break;
   1204 
   1205     default:
   1206       break;
   1207   }
   1208 }
   1209 
   1210 static void bte_av_callback(tBTA_AV_EVT event, tBTA_AV* p_data) {
   1211   btif_transfer_context(btif_av_handle_event, event, (char*)p_data,
   1212                         sizeof(tBTA_AV), btif_av_event_deep_copy);
   1213 }
   1214 
   1215 static void bte_av_sink_media_callback(tBTA_AV_EVT event,
   1216                                        tBTA_AV_MEDIA* p_data) {
   1217   switch (event) {
   1218     case BTA_AV_SINK_MEDIA_DATA_EVT: {
   1219       btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle);
   1220       if ((state == BTIF_AV_STATE_STARTED) || (state == BTIF_AV_STATE_OPENED)) {
   1221         uint8_t queue_len = btif_a2dp_sink_enqueue_buf((BT_HDR*)p_data);
   1222         BTIF_TRACE_DEBUG("%s: packets in sink queue %d", __func__, queue_len);
   1223       }
   1224       break;
   1225     }
   1226     case BTA_AV_SINK_MEDIA_CFG_EVT: {
   1227       btif_av_sink_config_req_t config_req;
   1228 
   1229       /* send a command to BT Media Task */
   1230       btif_a2dp_sink_update_decoder((uint8_t*)(p_data->avk_config.codec_info));
   1231       /* Switch to BTIF context */
   1232       config_req.sample_rate =
   1233           A2DP_GetTrackSampleRate(p_data->avk_config.codec_info);
   1234       if (config_req.sample_rate == -1) {
   1235         APPL_TRACE_ERROR("%s: cannot get the track frequency", __func__);
   1236         break;
   1237       }
   1238       config_req.channel_count =
   1239           A2DP_GetTrackChannelCount(p_data->avk_config.codec_info);
   1240       if (config_req.channel_count == -1) {
   1241         APPL_TRACE_ERROR("%s: cannot get the channel count", __func__);
   1242         break;
   1243       }
   1244 
   1245       config_req.peer_bd = p_data->avk_config.bd_addr;
   1246       btif_transfer_context(btif_av_handle_event, BTIF_AV_SINK_CONFIG_REQ_EVT,
   1247                             (char*)&config_req, sizeof(config_req), NULL);
   1248       break;
   1249     }
   1250     default:
   1251       break;
   1252   }
   1253 }
   1254 
   1255 /*******************************************************************************
   1256  *
   1257  * Function         btif_av_init
   1258  *
   1259  * Description      Initializes btif AV if not already done
   1260  *
   1261  * Returns          bt_status_t
   1262  *
   1263  ******************************************************************************/
   1264 
   1265 bt_status_t btif_av_init(int service_id) {
   1266   if (btif_av_cb.sm_handle == NULL) {
   1267     alarm_free(av_open_on_rc_timer);
   1268     av_open_on_rc_timer = alarm_new("btif_av.av_open_on_rc_timer");
   1269 
   1270     switch (service_id) {
   1271       case BTA_A2DP_SOURCE_SERVICE_ID:
   1272         if (!btif_a2dp_source_startup())
   1273           return BT_STATUS_FAIL;  // Already running
   1274         break;
   1275       case BTA_A2DP_SINK_SERVICE_ID:
   1276         if (!btif_a2dp_sink_startup())
   1277           return BT_STATUS_FAIL;  // Already running
   1278         break;
   1279       default:
   1280         break;
   1281     }
   1282 
   1283     btif_enable_service(service_id);
   1284 
   1285     /* Also initialize the AV state machine */
   1286     btif_av_cb.sm_handle = btif_sm_init(
   1287         (const btif_sm_handler_t*)btif_av_state_handlers, BTIF_AV_STATE_IDLE);
   1288   }
   1289 
   1290   return BT_STATUS_SUCCESS;
   1291 }
   1292 
   1293 /*******************************************************************************
   1294  *
   1295  * Function         init_src
   1296  *
   1297  * Description      Initializes the AV interface for source mode
   1298  *
   1299  * Returns          bt_status_t
   1300  *
   1301  ******************************************************************************/
   1302 
   1303 static bt_status_t init_src(
   1304     btav_source_callbacks_t* callbacks,
   1305     std::vector<btav_a2dp_codec_config_t> codec_priorities) {
   1306   BTIF_TRACE_EVENT("%s", __func__);
   1307 
   1308   btif_av_cb.codec_priorities = codec_priorities;
   1309   bt_status_t status = btif_av_init(BTA_A2DP_SOURCE_SERVICE_ID);
   1310   if (status == BT_STATUS_SUCCESS) bt_av_src_callbacks = callbacks;
   1311 
   1312   return status;
   1313 }
   1314 
   1315 /*******************************************************************************
   1316  *
   1317  * Function         init_sink
   1318  *
   1319  * Description      Initializes the AV interface for sink mode
   1320  *
   1321  * Returns          bt_status_t
   1322  *
   1323  ******************************************************************************/
   1324 
   1325 static bt_status_t init_sink(btav_sink_callbacks_t* callbacks) {
   1326   BTIF_TRACE_EVENT("%s", __func__);
   1327 
   1328   bt_status_t status = btif_av_init(BTA_A2DP_SINK_SERVICE_ID);
   1329   if (status == BT_STATUS_SUCCESS) bt_av_sink_callbacks = callbacks;
   1330 
   1331   return status;
   1332 }
   1333 
   1334 /*******************************************************************************
   1335  *
   1336  * Function         update_audio_focus_state
   1337  *
   1338  * Description      Updates the final focus state reported by components calling
   1339  *                  this module.
   1340  *
   1341  * Returns          None
   1342  *
   1343  ******************************************************************************/
   1344 static void update_audio_focus_state(int state) {
   1345   BTIF_TRACE_DEBUG("%s: state=%d", __func__, state);
   1346   btif_a2dp_sink_set_focus_state_req((btif_a2dp_sink_focus_state_t)state);
   1347 }
   1348 
   1349 /*******************************************************************************
   1350  *
   1351  * Function         update_audio_track_gain
   1352  *
   1353  * Description      Updates the track gain (used for ducking).
   1354  *
   1355  * Returns          None
   1356  *
   1357  ******************************************************************************/
   1358 static void update_audio_track_gain(float gain) {
   1359   BTIF_TRACE_DEBUG("%s: gain=%f", __func__, gain);
   1360   btif_a2dp_sink_set_audio_track_gain(gain);
   1361 }
   1362 
   1363 /*******************************************************************************
   1364  *
   1365  * Function         connect
   1366  *
   1367  * Description      Establishes the AV signalling channel with the remote
   1368  *                  headset
   1369  *
   1370  * Returns          bt_status_t
   1371  *
   1372  ******************************************************************************/
   1373 
   1374 static bt_status_t connect_int(RawAddress* bd_addr, uint16_t uuid) {
   1375   btif_av_connect_req_t connect_req;
   1376   connect_req.target_bda = bd_addr;
   1377   connect_req.uuid = uuid;
   1378   BTIF_TRACE_EVENT("%s", __func__);
   1379 
   1380   btif_sm_dispatch(btif_av_cb.sm_handle, BTIF_AV_CONNECT_REQ_EVT,
   1381                    (char*)&connect_req);
   1382 
   1383   return BT_STATUS_SUCCESS;
   1384 }
   1385 
   1386 static bt_status_t src_connect_sink(RawAddress* bd_addr) {
   1387   BTIF_TRACE_EVENT("%s", __func__);
   1388   CHECK_BTAV_INIT();
   1389 
   1390   return btif_queue_connect(UUID_SERVCLASS_AUDIO_SOURCE, bd_addr, connect_int);
   1391 }
   1392 
   1393 static bt_status_t sink_connect_src(RawAddress* bd_addr) {
   1394   BTIF_TRACE_EVENT("%s", __func__);
   1395   CHECK_BTAV_INIT();
   1396 
   1397   return btif_queue_connect(UUID_SERVCLASS_AUDIO_SINK, bd_addr, connect_int);
   1398 }
   1399 
   1400 /*******************************************************************************
   1401  *
   1402  * Function         disconnect
   1403  *
   1404  * Description      Tears down the AV signalling channel with the remote headset
   1405  *
   1406  * Returns          bt_status_t
   1407  *
   1408  ******************************************************************************/
   1409 static bt_status_t disconnect(RawAddress* bd_addr) {
   1410   BTIF_TRACE_EVENT("%s", __func__);
   1411   CHECK_BTAV_INIT();
   1412 
   1413   /* Switch to BTIF context */
   1414   return btif_transfer_context(btif_av_handle_event, BTIF_AV_DISCONNECT_REQ_EVT,
   1415                                (char*)bd_addr, sizeof(RawAddress), NULL);
   1416 }
   1417 
   1418 static bt_status_t codec_config_src(
   1419     std::vector<btav_a2dp_codec_config_t> codec_preferences) {
   1420   BTIF_TRACE_EVENT("%s", __func__);
   1421   CHECK_BTAV_INIT();
   1422 
   1423   for (auto cp : codec_preferences) {
   1424     BTIF_TRACE_DEBUG(
   1425         "%s: codec_type=%d codec_priority=%d "
   1426         "sample_rate=0x%x bits_per_sample=0x%x "
   1427         "channel_mode=0x%x codec_specific_1=%d "
   1428         "codec_specific_2=%d codec_specific_3=%d "
   1429         "codec_specific_4=%d",
   1430         __func__, cp.codec_type, cp.codec_priority, cp.sample_rate,
   1431         cp.bits_per_sample, cp.channel_mode, cp.codec_specific_1,
   1432         cp.codec_specific_2, cp.codec_specific_3, cp.codec_specific_4);
   1433     btif_transfer_context(btif_av_handle_event, BTIF_AV_SOURCE_CONFIG_REQ_EVT,
   1434                           reinterpret_cast<char*>(&cp), sizeof(cp), NULL);
   1435   }
   1436 
   1437   return BT_STATUS_SUCCESS;
   1438 }
   1439 
   1440 /*******************************************************************************
   1441  *
   1442  * Function         cleanup
   1443  *
   1444  * Description      Shuts down the AV interface and does the cleanup
   1445  *
   1446  * Returns          None
   1447  *
   1448  ******************************************************************************/
   1449 static void cleanup(int service_uuid) {
   1450   BTIF_TRACE_EVENT("%s", __func__);
   1451 
   1452   btif_transfer_context(btif_av_handle_event, BTIF_AV_CLEANUP_REQ_EVT, NULL, 0,
   1453                         NULL);
   1454 
   1455   btif_disable_service(service_uuid);
   1456 
   1457   alarm_free(av_open_on_rc_timer);
   1458   av_open_on_rc_timer = NULL;
   1459 
   1460   /* Also shut down the AV state machine */
   1461   btif_sm_shutdown(btif_av_cb.sm_handle);
   1462   btif_av_cb.sm_handle = NULL;
   1463 }
   1464 
   1465 static void cleanup_src(void) {
   1466   BTIF_TRACE_EVENT("%s", __func__);
   1467 
   1468   btif_queue_cleanup(UUID_SERVCLASS_AUDIO_SOURCE);
   1469   if (bt_av_src_callbacks) {
   1470     bt_av_src_callbacks = NULL;
   1471     if (bt_av_sink_callbacks == NULL) cleanup(BTA_A2DP_SOURCE_SERVICE_ID);
   1472   }
   1473 }
   1474 
   1475 static void cleanup_sink(void) {
   1476   BTIF_TRACE_EVENT("%s", __func__);
   1477 
   1478   btif_queue_cleanup(UUID_SERVCLASS_AUDIO_SINK);
   1479   if (bt_av_sink_callbacks) {
   1480     bt_av_sink_callbacks = NULL;
   1481     if (bt_av_src_callbacks == NULL) cleanup(BTA_A2DP_SINK_SERVICE_ID);
   1482   }
   1483 }
   1484 
   1485 static const btav_source_interface_t bt_av_src_interface = {
   1486     sizeof(btav_source_interface_t),
   1487     init_src,
   1488     src_connect_sink,
   1489     disconnect,
   1490     codec_config_src,
   1491     cleanup_src,
   1492 };
   1493 
   1494 static const btav_sink_interface_t bt_av_sink_interface = {
   1495     sizeof(btav_sink_interface_t),
   1496     init_sink,
   1497     sink_connect_src,
   1498     disconnect,
   1499     cleanup_sink,
   1500     update_audio_focus_state,
   1501     update_audio_track_gain,
   1502 };
   1503 
   1504 /*******************************************************************************
   1505  *
   1506  * Function         btif_av_get_addr
   1507  *
   1508  * Description      Fetches current AV BD address
   1509  *
   1510  * Returns          BD address
   1511  *
   1512  ******************************************************************************/
   1513 
   1514 RawAddress btif_av_get_addr(void) { return btif_av_cb.peer_bda; }
   1515 
   1516 /*******************************************************************************
   1517  * Function         btif_av_is_sink_enabled
   1518  *
   1519  * Description      Checks if A2DP Sink is enabled or not
   1520  *
   1521  * Returns          true if A2DP Sink is enabled, false otherwise
   1522  *
   1523  ******************************************************************************/
   1524 
   1525 bool btif_av_is_sink_enabled(void) {
   1526   return (bt_av_sink_callbacks != NULL) ? true : false;
   1527 }
   1528 
   1529 /*******************************************************************************
   1530  *
   1531  * Function         btif_av_stream_ready
   1532  *
   1533  * Description      Checks whether AV is ready for starting a stream
   1534  *
   1535  * Returns          None
   1536  *
   1537  ******************************************************************************/
   1538 
   1539 bool btif_av_stream_ready(void) {
   1540   btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle);
   1541 
   1542   BTIF_TRACE_DEBUG("%s: sm_handle=%d, state=%d, flags=0x%x", __func__,
   1543                    btif_av_cb.sm_handle, state, btif_av_cb.flags);
   1544 
   1545   /* also make sure main adapter is enabled */
   1546   if (btif_is_enabled() == 0) {
   1547     BTIF_TRACE_EVENT("%s: main adapter not enabled", __func__);
   1548     return false;
   1549   }
   1550 
   1551   /* check if we are remotely suspended or stop is pending */
   1552   if (btif_av_cb.flags &
   1553       (BTIF_AV_FLAG_REMOTE_SUSPEND | BTIF_AV_FLAG_PENDING_STOP))
   1554     return false;
   1555 
   1556   return (state == BTIF_AV_STATE_OPENED);
   1557 }
   1558 
   1559 /*******************************************************************************
   1560  *
   1561  * Function         btif_av_stream_started_ready
   1562  *
   1563  * Description      Checks whether AV ready for media start in streaming state
   1564  *
   1565  * Returns          None
   1566  *
   1567  ******************************************************************************/
   1568 
   1569 bool btif_av_stream_started_ready(void) {
   1570   btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle);
   1571   bool ready = false;
   1572 
   1573   /* disallow media task to start if we have pending actions */
   1574   if (btif_av_cb.flags &
   1575       (BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING | BTIF_AV_FLAG_REMOTE_SUSPEND |
   1576        BTIF_AV_FLAG_PENDING_STOP)) {
   1577     ready = false;
   1578   } else {
   1579     ready = (state == BTIF_AV_STATE_STARTED);
   1580   }
   1581 
   1582   BTIF_TRACE_WARNING("%s: sm_handle=%d state=%d flags=0x%x ready=%d", __func__,
   1583                      btif_av_cb.sm_handle, state, btif_av_cb.flags, ready);
   1584 
   1585   return ready;
   1586 }
   1587 
   1588 /*******************************************************************************
   1589  *
   1590  * Function         btif_dispatch_sm_event
   1591  *
   1592  * Description      Send event to AV statemachine
   1593  *
   1594  * Returns          None
   1595  *
   1596  ******************************************************************************/
   1597 
   1598 /* used to pass events to AV statemachine from other tasks */
   1599 void btif_dispatch_sm_event(btif_av_sm_event_t event, void* p_data, int len) {
   1600   /* Switch to BTIF context */
   1601   btif_transfer_context(btif_av_handle_event, event, (char*)p_data, len, NULL);
   1602 }
   1603 
   1604 /*******************************************************************************
   1605  *
   1606  * Function         btif_av_execute_service
   1607  *
   1608  * Description      Initializes/Shuts down the service
   1609  *
   1610  * Returns          BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
   1611  *
   1612  ******************************************************************************/
   1613 bt_status_t btif_av_execute_service(bool b_enable) {
   1614   if (b_enable) {
   1615 /* TODO: Removed BTA_SEC_AUTHORIZE since the Java/App does not
   1616  * handle this request in order to allow incoming connections to succeed.
   1617  * We need to put this back once support for this is added */
   1618 
   1619 /* Added BTA_AV_FEAT_NO_SCO_SSPD - this ensures that the BTA does not
   1620  * auto-suspend av streaming on AG events(SCO or Call). The suspend shall
   1621  * be initiated by the app/audioflinger layers */
   1622 /* Support for browsing for SDP record should work only if we enable BROWSE
   1623  * while registering. */
   1624 #if (AVRC_METADATA_INCLUDED == TRUE)
   1625     BTA_AvEnable(BTA_SEC_AUTHENTICATE,
   1626                  BTA_AV_FEAT_RCTG | BTA_AV_FEAT_METADATA | BTA_AV_FEAT_VENDOR |
   1627                      BTA_AV_FEAT_NO_SCO_SSPD
   1628 #if (AVRC_ADV_CTRL_INCLUDED == TRUE)
   1629                      | BTA_AV_FEAT_RCCT | BTA_AV_FEAT_ADV_CTRL |
   1630                      BTA_AV_FEAT_BROWSE
   1631 #endif
   1632                  ,
   1633                  bte_av_callback);
   1634 #else
   1635     BTA_AvEnable(BTA_SEC_AUTHENTICATE,
   1636                  (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_NO_SCO_SSPD), bte_av_callback);
   1637 #endif
   1638     BTA_AvRegister(BTA_AV_CHNL_AUDIO, BTIF_AV_SERVICE_NAME, 0, NULL,
   1639                    UUID_SERVCLASS_AUDIO_SOURCE);
   1640   } else {
   1641     BTA_AvDeregister(btif_av_cb.bta_handle);
   1642     BTA_AvDisable();
   1643   }
   1644   return BT_STATUS_SUCCESS;
   1645 }
   1646 
   1647 /*******************************************************************************
   1648  *
   1649  * Function         btif_av_sink_execute_service
   1650  *
   1651  * Description      Initializes/Shuts down the service
   1652  *
   1653  * Returns          BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
   1654  *
   1655  ******************************************************************************/
   1656 bt_status_t btif_av_sink_execute_service(bool b_enable) {
   1657   if (b_enable) {
   1658     /* Added BTA_AV_FEAT_NO_SCO_SSPD - this ensures that the BTA does not
   1659      * auto-suspend av streaming on AG events(SCO or Call). The suspend shall
   1660      * be initiated by the app/audioflinger layers */
   1661     BTA_AvEnable(BTA_SEC_AUTHENTICATE,
   1662                  BTA_AV_FEAT_NO_SCO_SSPD | BTA_AV_FEAT_RCCT |
   1663                      BTA_AV_FEAT_METADATA | BTA_AV_FEAT_VENDOR |
   1664                      BTA_AV_FEAT_ADV_CTRL | BTA_AV_FEAT_RCTG |
   1665                      BTA_AV_FEAT_BROWSE,
   1666                  bte_av_callback);
   1667     BTA_AvRegister(BTA_AV_CHNL_AUDIO, BTIF_AVK_SERVICE_NAME, 0,
   1668                    bte_av_sink_media_callback, UUID_SERVCLASS_AUDIO_SINK);
   1669   } else {
   1670     BTA_AvDeregister(btif_av_cb.bta_handle);
   1671     BTA_AvDisable();
   1672   }
   1673   return BT_STATUS_SUCCESS;
   1674 }
   1675 
   1676 /*******************************************************************************
   1677  *
   1678  * Function         btif_av_get_src_interface
   1679  *
   1680  * Description      Get the AV callback interface for A2DP source profile
   1681  *
   1682  * Returns          btav_source_interface_t
   1683  *
   1684  ******************************************************************************/
   1685 const btav_source_interface_t* btif_av_get_src_interface(void) {
   1686   BTIF_TRACE_EVENT("%s", __func__);
   1687   return &bt_av_src_interface;
   1688 }
   1689 
   1690 /*******************************************************************************
   1691  *
   1692  * Function         btif_av_get_sink_interface
   1693  *
   1694  * Description      Get the AV callback interface for A2DP sink profile
   1695  *
   1696  * Returns          btav_sink_interface_t
   1697  *
   1698  ******************************************************************************/
   1699 const btav_sink_interface_t* btif_av_get_sink_interface(void) {
   1700   BTIF_TRACE_EVENT("%s", __func__);
   1701   return &bt_av_sink_interface;
   1702 }
   1703 
   1704 /*******************************************************************************
   1705  *
   1706  * Function         btif_av_is_connected
   1707  *
   1708  * Description      Checks if av has a connected sink
   1709  *
   1710  * Returns          bool
   1711  *
   1712  ******************************************************************************/
   1713 bool btif_av_is_connected(void) {
   1714   btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle);
   1715   return ((state == BTIF_AV_STATE_OPENED) || (state == BTIF_AV_STATE_STARTED));
   1716 }
   1717 
   1718 uint8_t btif_av_get_peer_sep(void) { return btif_av_cb.peer_sep; }
   1719 
   1720 /*******************************************************************************
   1721  *
   1722  * Function         btif_av_is_peer_edr
   1723  *
   1724  * Description      Check if the connected a2dp device supports
   1725  *                  EDR or not. Only when connected this function
   1726  *                  will accurately provide a true capability of
   1727  *                  remote peer. If not connected it will always be false.
   1728  *
   1729  * Returns          true if remote device is capable of EDR
   1730  *
   1731  ******************************************************************************/
   1732 bool btif_av_is_peer_edr(void) {
   1733   ASSERTC(btif_av_is_connected(), "No active a2dp connection", 0);
   1734 
   1735   if (btif_av_cb.edr)
   1736     return true;
   1737   else
   1738     return false;
   1739 }
   1740 
   1741 /******************************************************************************
   1742  *
   1743  * Function        btif_av_clear_remote_suspend_flag
   1744  *
   1745  * Description     Clears btif_av_cd.flags if BTIF_AV_FLAG_REMOTE_SUSPEND is set
   1746  *
   1747  * Returns          void
   1748  *****************************************************************************/
   1749 void btif_av_clear_remote_suspend_flag(void) {
   1750   BTIF_TRACE_DEBUG("%s: flags=0x%x", __func__, btif_av_cb.flags);
   1751   btif_av_cb.flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;
   1752 }
   1753 
   1754 /*******************************************************************************
   1755  *
   1756  * Function         btif_av_peer_supports_3mbps
   1757  *
   1758  * Description      Check if the connected A2DP device supports
   1759  *                  3 Mbps EDR. This function only works if connected.
   1760  *                  If not connected it will always be false.
   1761  *
   1762  * Returns          true if remote device is EDR and supports 3 Mbps
   1763  *
   1764  ******************************************************************************/
   1765 bool btif_av_peer_supports_3mbps(void) {
   1766   bool is3mbps = ((btif_av_cb.edr & BTA_AV_EDR_3MBPS) != 0);
   1767   BTIF_TRACE_DEBUG("%s: connected %d, edr_3mbps %d", __func__,
   1768                    btif_av_is_connected(), is3mbps);
   1769   return (btif_av_is_connected() && is3mbps);
   1770 }
   1771 
   1772 /*******************************************************************************
   1773  *
   1774  * Function         btif_av_move_idle
   1775  *
   1776  * Description      Opening state is intermediate state. It cannot handle
   1777  *                  incoming/outgoing connect/disconnect requests.When ACL
   1778  *                  is disconnected and we are in opening state then move back
   1779  *                  to idle state which is proper to handle connections.
   1780  *
   1781  * Returns          Void
   1782  *
   1783  ******************************************************************************/
   1784 void btif_av_move_idle(RawAddress bd_addr) {
   1785   /* inform the application that ACL is disconnected and move to idle state */
   1786   btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle);
   1787   BTIF_TRACE_WARNING("%s: ACL Disconnected state %d bd_addr=%s peer_bda=%s",
   1788                      __func__, state, bd_addr.ToString().c_str(),
   1789                      btif_av_cb.peer_bda.ToString().c_str());
   1790 
   1791   if (state == BTIF_AV_STATE_OPENING && (bd_addr == btif_av_cb.peer_bda)) {
   1792     BTIF_TRACE_DEBUG(
   1793         "%s: Moving State from Opening to Idle due to ACL disconnect",
   1794         __func__);
   1795     btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
   1796                                  &(btif_av_cb.peer_bda));
   1797     btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE);
   1798   }
   1799 }
   1800