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