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
dump_av_sm_state_name(btif_av_state_t state)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
dump_av_sm_event_name(btif_av_sm_event_t event)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 ******************************************************************************/
btif_initiate_av_open_timer_timeout(UNUSED_ATTR void * data)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 ******************************************************************************/
btif_report_connection_state(btav_connection_state_t state,RawAddress * bd_addr)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 ******************************************************************************/
btif_report_audio_state(btav_audio_state_t state,RawAddress * bd_addr)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
btif_update_source_codec(void * p_data)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
btif_report_source_codec_state(UNUSED_ATTR void * p_data)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
btif_av_state_idle_handler(btif_sm_event_t event,void * p_data)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
btif_av_state_opening_handler(btif_sm_event_t event,void * p_data)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
btif_av_state_closing_handler(btif_sm_event_t event,void * p_data)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
btif_av_state_opened_handler(btif_sm_event_t event,void * p_data)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
btif_av_state_started_handler(btif_sm_event_t event,void * p_data)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
btif_av_handle_event(uint16_t event,char * p_param)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
btif_av_event_deep_copy(uint16_t event,char * p_dest,char * p_src)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 if ((p_msg_src->hdr.opcode == AVRC_OP_BROWSE) &&
1184 p_msg_src->browse.p_browse_data && p_msg_src->browse.browse_len) {
1185 p_msg_dest->browse.p_browse_data =
1186 (uint8_t*)osi_calloc(p_msg_src->browse.browse_len);
1187 memcpy(p_msg_dest->browse.p_browse_data,
1188 p_msg_src->browse.p_browse_data, p_msg_src->browse.browse_len);
1189 android_errorWriteLog(0x534e4554, "109699112");
1190 }
1191 }
1192 break;
1193
1194 default:
1195 break;
1196 }
1197 }
1198
btif_av_event_free_data(btif_sm_event_t event,void * p_data)1199 static void btif_av_event_free_data(btif_sm_event_t event, void* p_data) {
1200 switch (event) {
1201 case BTA_AV_META_MSG_EVT: {
1202 tBTA_AV* av = (tBTA_AV*)p_data;
1203 osi_free_and_reset((void**)&av->meta_msg.p_data);
1204
1205 if (av->meta_msg.p_msg) {
1206 if (av->meta_msg.p_msg->hdr.opcode == AVRC_OP_VENDOR) {
1207 osi_free(av->meta_msg.p_msg->vendor.p_vendor_data);
1208 }
1209 if (av->meta_msg.p_msg->hdr.opcode == AVRC_OP_BROWSE) {
1210 osi_free(av->meta_msg.p_msg->browse.p_browse_data);
1211 }
1212 osi_free_and_reset((void**)&av->meta_msg.p_msg);
1213 }
1214 } break;
1215
1216 default:
1217 break;
1218 }
1219 }
1220
bte_av_callback(tBTA_AV_EVT event,tBTA_AV * p_data)1221 static void bte_av_callback(tBTA_AV_EVT event, tBTA_AV* p_data) {
1222 btif_transfer_context(btif_av_handle_event, event, (char*)p_data,
1223 sizeof(tBTA_AV), btif_av_event_deep_copy);
1224 }
1225
bte_av_sink_media_callback(tBTA_AV_EVT event,tBTA_AV_MEDIA * p_data)1226 static void bte_av_sink_media_callback(tBTA_AV_EVT event,
1227 tBTA_AV_MEDIA* p_data) {
1228 switch (event) {
1229 case BTA_AV_SINK_MEDIA_DATA_EVT: {
1230 btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle);
1231 if ((state == BTIF_AV_STATE_STARTED) || (state == BTIF_AV_STATE_OPENED)) {
1232 uint8_t queue_len = btif_a2dp_sink_enqueue_buf((BT_HDR*)p_data);
1233 BTIF_TRACE_DEBUG("%s: packets in sink queue %d", __func__, queue_len);
1234 }
1235 break;
1236 }
1237 case BTA_AV_SINK_MEDIA_CFG_EVT: {
1238 btif_av_sink_config_req_t config_req;
1239
1240 /* send a command to BT Media Task */
1241 btif_a2dp_sink_update_decoder((uint8_t*)(p_data->avk_config.codec_info));
1242 /* Switch to BTIF context */
1243 config_req.sample_rate =
1244 A2DP_GetTrackSampleRate(p_data->avk_config.codec_info);
1245 if (config_req.sample_rate == -1) {
1246 APPL_TRACE_ERROR("%s: cannot get the track frequency", __func__);
1247 break;
1248 }
1249 config_req.channel_count =
1250 A2DP_GetTrackChannelCount(p_data->avk_config.codec_info);
1251 if (config_req.channel_count == -1) {
1252 APPL_TRACE_ERROR("%s: cannot get the channel count", __func__);
1253 break;
1254 }
1255
1256 config_req.peer_bd = p_data->avk_config.bd_addr;
1257 btif_transfer_context(btif_av_handle_event, BTIF_AV_SINK_CONFIG_REQ_EVT,
1258 (char*)&config_req, sizeof(config_req), NULL);
1259 break;
1260 }
1261 default:
1262 break;
1263 }
1264 }
1265
1266 /*******************************************************************************
1267 *
1268 * Function btif_av_init
1269 *
1270 * Description Initializes btif AV if not already done
1271 *
1272 * Returns bt_status_t
1273 *
1274 ******************************************************************************/
1275
btif_av_init(int service_id)1276 bt_status_t btif_av_init(int service_id) {
1277 if (btif_av_cb.sm_handle == NULL) {
1278 alarm_free(av_open_on_rc_timer);
1279 av_open_on_rc_timer = alarm_new("btif_av.av_open_on_rc_timer");
1280
1281 switch (service_id) {
1282 case BTA_A2DP_SOURCE_SERVICE_ID:
1283 if (!btif_a2dp_source_startup())
1284 return BT_STATUS_FAIL; // Already running
1285 break;
1286 case BTA_A2DP_SINK_SERVICE_ID:
1287 if (!btif_a2dp_sink_startup())
1288 return BT_STATUS_FAIL; // Already running
1289 break;
1290 default:
1291 break;
1292 }
1293
1294 btif_enable_service(service_id);
1295
1296 /* Also initialize the AV state machine */
1297 btif_av_cb.sm_handle = btif_sm_init(
1298 (const btif_sm_handler_t*)btif_av_state_handlers, BTIF_AV_STATE_IDLE);
1299 }
1300
1301 return BT_STATUS_SUCCESS;
1302 }
1303
1304 /*******************************************************************************
1305 *
1306 * Function init_src
1307 *
1308 * Description Initializes the AV interface for source mode
1309 *
1310 * Returns bt_status_t
1311 *
1312 ******************************************************************************/
1313
init_src(btav_source_callbacks_t * callbacks,std::vector<btav_a2dp_codec_config_t> codec_priorities)1314 static bt_status_t init_src(
1315 btav_source_callbacks_t* callbacks,
1316 std::vector<btav_a2dp_codec_config_t> codec_priorities) {
1317 BTIF_TRACE_EVENT("%s", __func__);
1318
1319 btif_av_cb.codec_priorities = codec_priorities;
1320 bt_status_t status = btif_av_init(BTA_A2DP_SOURCE_SERVICE_ID);
1321 if (status == BT_STATUS_SUCCESS) bt_av_src_callbacks = callbacks;
1322
1323 return status;
1324 }
1325
1326 /*******************************************************************************
1327 *
1328 * Function init_sink
1329 *
1330 * Description Initializes the AV interface for sink mode
1331 *
1332 * Returns bt_status_t
1333 *
1334 ******************************************************************************/
1335
init_sink(btav_sink_callbacks_t * callbacks)1336 static bt_status_t init_sink(btav_sink_callbacks_t* callbacks) {
1337 BTIF_TRACE_EVENT("%s", __func__);
1338
1339 bt_status_t status = btif_av_init(BTA_A2DP_SINK_SERVICE_ID);
1340 if (status == BT_STATUS_SUCCESS) bt_av_sink_callbacks = callbacks;
1341
1342 return status;
1343 }
1344
1345 /*******************************************************************************
1346 *
1347 * Function update_audio_focus_state
1348 *
1349 * Description Updates the final focus state reported by components calling
1350 * this module.
1351 *
1352 * Returns None
1353 *
1354 ******************************************************************************/
update_audio_focus_state(int state)1355 static void update_audio_focus_state(int state) {
1356 BTIF_TRACE_DEBUG("%s: state=%d", __func__, state);
1357 btif_a2dp_sink_set_focus_state_req((btif_a2dp_sink_focus_state_t)state);
1358 }
1359
1360 /*******************************************************************************
1361 *
1362 * Function update_audio_track_gain
1363 *
1364 * Description Updates the track gain (used for ducking).
1365 *
1366 * Returns None
1367 *
1368 ******************************************************************************/
update_audio_track_gain(float gain)1369 static void update_audio_track_gain(float gain) {
1370 BTIF_TRACE_DEBUG("%s: gain=%f", __func__, gain);
1371 btif_a2dp_sink_set_audio_track_gain(gain);
1372 }
1373
1374 /*******************************************************************************
1375 *
1376 * Function connect
1377 *
1378 * Description Establishes the AV signalling channel with the remote
1379 * headset
1380 *
1381 * Returns bt_status_t
1382 *
1383 ******************************************************************************/
1384
connect_int(RawAddress * bd_addr,uint16_t uuid)1385 static bt_status_t connect_int(RawAddress* bd_addr, uint16_t uuid) {
1386 btif_av_connect_req_t connect_req;
1387 connect_req.target_bda = bd_addr;
1388 connect_req.uuid = uuid;
1389 BTIF_TRACE_EVENT("%s", __func__);
1390
1391 btif_sm_dispatch(btif_av_cb.sm_handle, BTIF_AV_CONNECT_REQ_EVT,
1392 (char*)&connect_req);
1393
1394 return BT_STATUS_SUCCESS;
1395 }
1396
src_connect_sink(RawAddress * bd_addr)1397 static bt_status_t src_connect_sink(RawAddress* bd_addr) {
1398 BTIF_TRACE_EVENT("%s", __func__);
1399 CHECK_BTAV_INIT();
1400
1401 return btif_queue_connect(UUID_SERVCLASS_AUDIO_SOURCE, bd_addr, connect_int);
1402 }
1403
sink_connect_src(RawAddress * bd_addr)1404 static bt_status_t sink_connect_src(RawAddress* bd_addr) {
1405 BTIF_TRACE_EVENT("%s", __func__);
1406 CHECK_BTAV_INIT();
1407
1408 return btif_queue_connect(UUID_SERVCLASS_AUDIO_SINK, bd_addr, connect_int);
1409 }
1410
1411 /*******************************************************************************
1412 *
1413 * Function disconnect
1414 *
1415 * Description Tears down the AV signalling channel with the remote headset
1416 *
1417 * Returns bt_status_t
1418 *
1419 ******************************************************************************/
disconnect(RawAddress * bd_addr)1420 static bt_status_t disconnect(RawAddress* bd_addr) {
1421 BTIF_TRACE_EVENT("%s", __func__);
1422 CHECK_BTAV_INIT();
1423
1424 /* Switch to BTIF context */
1425 return btif_transfer_context(btif_av_handle_event, BTIF_AV_DISCONNECT_REQ_EVT,
1426 (char*)bd_addr, sizeof(RawAddress), NULL);
1427 }
1428
codec_config_src(std::vector<btav_a2dp_codec_config_t> codec_preferences)1429 static bt_status_t codec_config_src(
1430 std::vector<btav_a2dp_codec_config_t> codec_preferences) {
1431 BTIF_TRACE_EVENT("%s", __func__);
1432 CHECK_BTAV_INIT();
1433
1434 for (auto cp : codec_preferences) {
1435 BTIF_TRACE_DEBUG(
1436 "%s: codec_type=%d codec_priority=%d "
1437 "sample_rate=0x%x bits_per_sample=0x%x "
1438 "channel_mode=0x%x codec_specific_1=%d "
1439 "codec_specific_2=%d codec_specific_3=%d "
1440 "codec_specific_4=%d",
1441 __func__, cp.codec_type, cp.codec_priority, cp.sample_rate,
1442 cp.bits_per_sample, cp.channel_mode, cp.codec_specific_1,
1443 cp.codec_specific_2, cp.codec_specific_3, cp.codec_specific_4);
1444 btif_transfer_context(btif_av_handle_event, BTIF_AV_SOURCE_CONFIG_REQ_EVT,
1445 reinterpret_cast<char*>(&cp), sizeof(cp), NULL);
1446 }
1447
1448 return BT_STATUS_SUCCESS;
1449 }
1450
1451 /*******************************************************************************
1452 *
1453 * Function cleanup
1454 *
1455 * Description Shuts down the AV interface and does the cleanup
1456 *
1457 * Returns None
1458 *
1459 ******************************************************************************/
cleanup(int service_uuid)1460 static void cleanup(int service_uuid) {
1461 BTIF_TRACE_EVENT("%s", __func__);
1462
1463 btif_transfer_context(btif_av_handle_event, BTIF_AV_CLEANUP_REQ_EVT, NULL, 0,
1464 NULL);
1465
1466 btif_disable_service(service_uuid);
1467
1468 alarm_free(av_open_on_rc_timer);
1469 av_open_on_rc_timer = NULL;
1470
1471 /* Also shut down the AV state machine */
1472 btif_sm_shutdown(btif_av_cb.sm_handle);
1473 btif_av_cb.sm_handle = NULL;
1474 }
1475
cleanup_src(void)1476 static void cleanup_src(void) {
1477 BTIF_TRACE_EVENT("%s", __func__);
1478
1479 btif_queue_cleanup(UUID_SERVCLASS_AUDIO_SOURCE);
1480 if (bt_av_src_callbacks) {
1481 bt_av_src_callbacks = NULL;
1482 if (bt_av_sink_callbacks == NULL) cleanup(BTA_A2DP_SOURCE_SERVICE_ID);
1483 }
1484 }
1485
cleanup_sink(void)1486 static void cleanup_sink(void) {
1487 BTIF_TRACE_EVENT("%s", __func__);
1488
1489 btif_queue_cleanup(UUID_SERVCLASS_AUDIO_SINK);
1490 if (bt_av_sink_callbacks) {
1491 bt_av_sink_callbacks = NULL;
1492 if (bt_av_src_callbacks == NULL) cleanup(BTA_A2DP_SINK_SERVICE_ID);
1493 }
1494 }
1495
1496 static const btav_source_interface_t bt_av_src_interface = {
1497 sizeof(btav_source_interface_t),
1498 init_src,
1499 src_connect_sink,
1500 disconnect,
1501 codec_config_src,
1502 cleanup_src,
1503 };
1504
1505 static const btav_sink_interface_t bt_av_sink_interface = {
1506 sizeof(btav_sink_interface_t),
1507 init_sink,
1508 sink_connect_src,
1509 disconnect,
1510 cleanup_sink,
1511 update_audio_focus_state,
1512 update_audio_track_gain,
1513 };
1514
1515 /*******************************************************************************
1516 *
1517 * Function btif_av_get_addr
1518 *
1519 * Description Fetches current AV BD address
1520 *
1521 * Returns BD address
1522 *
1523 ******************************************************************************/
1524
btif_av_get_addr(void)1525 RawAddress btif_av_get_addr(void) { return btif_av_cb.peer_bda; }
1526
1527 /*******************************************************************************
1528 * Function btif_av_is_sink_enabled
1529 *
1530 * Description Checks if A2DP Sink is enabled or not
1531 *
1532 * Returns true if A2DP Sink is enabled, false otherwise
1533 *
1534 ******************************************************************************/
1535
btif_av_is_sink_enabled(void)1536 bool btif_av_is_sink_enabled(void) {
1537 return (bt_av_sink_callbacks != NULL) ? true : false;
1538 }
1539
1540 /*******************************************************************************
1541 *
1542 * Function btif_av_stream_ready
1543 *
1544 * Description Checks whether AV is ready for starting a stream
1545 *
1546 * Returns None
1547 *
1548 ******************************************************************************/
1549
btif_av_stream_ready(void)1550 bool btif_av_stream_ready(void) {
1551 btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle);
1552
1553 BTIF_TRACE_DEBUG("%s: sm_handle=%d, state=%d, flags=0x%x", __func__,
1554 btif_av_cb.sm_handle, state, btif_av_cb.flags);
1555
1556 /* also make sure main adapter is enabled */
1557 if (btif_is_enabled() == 0) {
1558 BTIF_TRACE_EVENT("%s: main adapter not enabled", __func__);
1559 return false;
1560 }
1561
1562 /* check if we are remotely suspended or stop is pending */
1563 if (btif_av_cb.flags &
1564 (BTIF_AV_FLAG_REMOTE_SUSPEND | BTIF_AV_FLAG_PENDING_STOP))
1565 return false;
1566
1567 return (state == BTIF_AV_STATE_OPENED);
1568 }
1569
1570 /*******************************************************************************
1571 *
1572 * Function btif_av_stream_started_ready
1573 *
1574 * Description Checks whether AV ready for media start in streaming state
1575 *
1576 * Returns None
1577 *
1578 ******************************************************************************/
1579
btif_av_stream_started_ready(void)1580 bool btif_av_stream_started_ready(void) {
1581 btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle);
1582 bool ready = false;
1583
1584 /* disallow media task to start if we have pending actions */
1585 if (btif_av_cb.flags &
1586 (BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING | BTIF_AV_FLAG_REMOTE_SUSPEND |
1587 BTIF_AV_FLAG_PENDING_STOP)) {
1588 ready = false;
1589 } else {
1590 ready = (state == BTIF_AV_STATE_STARTED);
1591 }
1592
1593 BTIF_TRACE_WARNING("%s: sm_handle=%d state=%d flags=0x%x ready=%d", __func__,
1594 btif_av_cb.sm_handle, state, btif_av_cb.flags, ready);
1595
1596 return ready;
1597 }
1598
1599 /*******************************************************************************
1600 *
1601 * Function btif_dispatch_sm_event
1602 *
1603 * Description Send event to AV statemachine
1604 *
1605 * Returns None
1606 *
1607 ******************************************************************************/
1608
1609 /* 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)1610 void btif_dispatch_sm_event(btif_av_sm_event_t event, void* p_data, int len) {
1611 /* Switch to BTIF context */
1612 btif_transfer_context(btif_av_handle_event, event, (char*)p_data, len, NULL);
1613 }
1614
1615 /*******************************************************************************
1616 *
1617 * Function btif_av_execute_service
1618 *
1619 * Description Initializes/Shuts down the service
1620 *
1621 * Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
1622 *
1623 ******************************************************************************/
btif_av_execute_service(bool b_enable)1624 bt_status_t btif_av_execute_service(bool b_enable) {
1625 if (b_enable) {
1626 /* TODO: Removed BTA_SEC_AUTHORIZE since the Java/App does not
1627 * handle this request in order to allow incoming connections to succeed.
1628 * We need to put this back once support for this is added */
1629
1630 /* Added BTA_AV_FEAT_NO_SCO_SSPD - this ensures that the BTA does not
1631 * auto-suspend av streaming on AG events(SCO or Call). The suspend shall
1632 * be initiated by the app/audioflinger layers */
1633 /* Support for browsing for SDP record should work only if we enable BROWSE
1634 * while registering. */
1635 #if (AVRC_METADATA_INCLUDED == TRUE)
1636 BTA_AvEnable(BTA_SEC_AUTHENTICATE,
1637 BTA_AV_FEAT_RCTG | BTA_AV_FEAT_METADATA | BTA_AV_FEAT_VENDOR |
1638 BTA_AV_FEAT_NO_SCO_SSPD
1639 #if (AVRC_ADV_CTRL_INCLUDED == TRUE)
1640 | BTA_AV_FEAT_RCCT | BTA_AV_FEAT_ADV_CTRL |
1641 BTA_AV_FEAT_BROWSE
1642 #endif
1643 ,
1644 bte_av_callback);
1645 #else
1646 BTA_AvEnable(BTA_SEC_AUTHENTICATE,
1647 (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_NO_SCO_SSPD), bte_av_callback);
1648 #endif
1649 BTA_AvRegister(BTA_AV_CHNL_AUDIO, BTIF_AV_SERVICE_NAME, 0, NULL,
1650 UUID_SERVCLASS_AUDIO_SOURCE);
1651 } else {
1652 BTA_AvDeregister(btif_av_cb.bta_handle);
1653 BTA_AvDisable();
1654 }
1655 return BT_STATUS_SUCCESS;
1656 }
1657
1658 /*******************************************************************************
1659 *
1660 * Function btif_av_sink_execute_service
1661 *
1662 * Description Initializes/Shuts down the service
1663 *
1664 * Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
1665 *
1666 ******************************************************************************/
btif_av_sink_execute_service(bool b_enable)1667 bt_status_t btif_av_sink_execute_service(bool b_enable) {
1668 if (b_enable) {
1669 /* Added BTA_AV_FEAT_NO_SCO_SSPD - this ensures that the BTA does not
1670 * auto-suspend av streaming on AG events(SCO or Call). The suspend shall
1671 * be initiated by the app/audioflinger layers */
1672 BTA_AvEnable(BTA_SEC_AUTHENTICATE,
1673 BTA_AV_FEAT_NO_SCO_SSPD | BTA_AV_FEAT_RCCT |
1674 BTA_AV_FEAT_METADATA | BTA_AV_FEAT_VENDOR |
1675 BTA_AV_FEAT_ADV_CTRL | BTA_AV_FEAT_RCTG |
1676 BTA_AV_FEAT_BROWSE,
1677 bte_av_callback);
1678 BTA_AvRegister(BTA_AV_CHNL_AUDIO, BTIF_AVK_SERVICE_NAME, 0,
1679 bte_av_sink_media_callback, UUID_SERVCLASS_AUDIO_SINK);
1680 } else {
1681 BTA_AvDeregister(btif_av_cb.bta_handle);
1682 BTA_AvDisable();
1683 }
1684 return BT_STATUS_SUCCESS;
1685 }
1686
1687 /*******************************************************************************
1688 *
1689 * Function btif_av_get_src_interface
1690 *
1691 * Description Get the AV callback interface for A2DP source profile
1692 *
1693 * Returns btav_source_interface_t
1694 *
1695 ******************************************************************************/
btif_av_get_src_interface(void)1696 const btav_source_interface_t* btif_av_get_src_interface(void) {
1697 BTIF_TRACE_EVENT("%s", __func__);
1698 return &bt_av_src_interface;
1699 }
1700
1701 /*******************************************************************************
1702 *
1703 * Function btif_av_get_sink_interface
1704 *
1705 * Description Get the AV callback interface for A2DP sink profile
1706 *
1707 * Returns btav_sink_interface_t
1708 *
1709 ******************************************************************************/
btif_av_get_sink_interface(void)1710 const btav_sink_interface_t* btif_av_get_sink_interface(void) {
1711 BTIF_TRACE_EVENT("%s", __func__);
1712 return &bt_av_sink_interface;
1713 }
1714
1715 /*******************************************************************************
1716 *
1717 * Function btif_av_is_connected
1718 *
1719 * Description Checks if av has a connected sink
1720 *
1721 * Returns bool
1722 *
1723 ******************************************************************************/
btif_av_is_connected(void)1724 bool btif_av_is_connected(void) {
1725 btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle);
1726 return ((state == BTIF_AV_STATE_OPENED) || (state == BTIF_AV_STATE_STARTED));
1727 }
1728
btif_av_get_peer_sep(void)1729 uint8_t btif_av_get_peer_sep(void) { return btif_av_cb.peer_sep; }
1730
1731 /*******************************************************************************
1732 *
1733 * Function btif_av_is_peer_edr
1734 *
1735 * Description Check if the connected a2dp device supports
1736 * EDR or not. Only when connected this function
1737 * will accurately provide a true capability of
1738 * remote peer. If not connected it will always be false.
1739 *
1740 * Returns true if remote device is capable of EDR
1741 *
1742 ******************************************************************************/
btif_av_is_peer_edr(void)1743 bool btif_av_is_peer_edr(void) {
1744 ASSERTC(btif_av_is_connected(), "No active a2dp connection", 0);
1745
1746 if (btif_av_cb.edr)
1747 return true;
1748 else
1749 return false;
1750 }
1751
1752 /******************************************************************************
1753 *
1754 * Function btif_av_clear_remote_suspend_flag
1755 *
1756 * Description Clears btif_av_cd.flags if BTIF_AV_FLAG_REMOTE_SUSPEND is set
1757 *
1758 * Returns void
1759 *****************************************************************************/
btif_av_clear_remote_suspend_flag(void)1760 void btif_av_clear_remote_suspend_flag(void) {
1761 BTIF_TRACE_DEBUG("%s: flags=0x%x", __func__, btif_av_cb.flags);
1762 btif_av_cb.flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;
1763 }
1764
1765 /*******************************************************************************
1766 *
1767 * Function btif_av_peer_supports_3mbps
1768 *
1769 * Description Check if the connected A2DP device supports
1770 * 3 Mbps EDR. This function only works if connected.
1771 * If not connected it will always be false.
1772 *
1773 * Returns true if remote device is EDR and supports 3 Mbps
1774 *
1775 ******************************************************************************/
btif_av_peer_supports_3mbps(void)1776 bool btif_av_peer_supports_3mbps(void) {
1777 bool is3mbps = ((btif_av_cb.edr & BTA_AV_EDR_3MBPS) != 0);
1778 BTIF_TRACE_DEBUG("%s: connected %d, edr_3mbps %d", __func__,
1779 btif_av_is_connected(), is3mbps);
1780 return (btif_av_is_connected() && is3mbps);
1781 }
1782
1783 /*******************************************************************************
1784 *
1785 * Function btif_av_move_idle
1786 *
1787 * Description Opening state is intermediate state. It cannot handle
1788 * incoming/outgoing connect/disconnect requests.When ACL
1789 * is disconnected and we are in opening state then move back
1790 * to idle state which is proper to handle connections.
1791 *
1792 * Returns Void
1793 *
1794 ******************************************************************************/
btif_av_move_idle(RawAddress bd_addr)1795 void btif_av_move_idle(RawAddress bd_addr) {
1796 /* inform the application that ACL is disconnected and move to idle state */
1797 btif_sm_state_t state = btif_sm_get_state(btif_av_cb.sm_handle);
1798 BTIF_TRACE_WARNING("%s: ACL Disconnected state %d bd_addr=%s peer_bda=%s",
1799 __func__, state, bd_addr.ToString().c_str(),
1800 btif_av_cb.peer_bda.ToString().c_str());
1801
1802 if (state == BTIF_AV_STATE_OPENING && (bd_addr == btif_av_cb.peer_bda)) {
1803 BTIF_TRACE_DEBUG(
1804 "%s: Moving State from Opening to Idle due to ACL disconnect",
1805 __func__);
1806 btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
1807 &(btif_av_cb.peer_bda));
1808 btif_sm_change_state(btif_av_cb.sm_handle, BTIF_AV_STATE_IDLE);
1809 }
1810 }
1811