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