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