1 /****************************************************************************** 2 * 3 * Copyright (C) 2003-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 * This is the main implementation file for the BTA audio gateway. 22 * 23 ******************************************************************************/ 24 25 #include <string.h> 26 #include "bta_api.h" 27 #include "bta_sys.h" 28 #include "bta_ag_co.h" 29 #include "bta_ag_int.h" 30 #include "bd.h" 31 32 /***************************************************************************** 33 ** Constants and types 34 *****************************************************************************/ 35 #ifndef BTA_AG_DEBUG 36 #define BTA_AG_DEBUG FALSE 37 #endif 38 39 #if BTA_AG_DEBUG == TRUE 40 static char *bta_ag_evt_str(UINT16 event, tBTA_AG_RES result); 41 static char *bta_ag_state_str(UINT8 state); 42 #endif 43 44 /* state machine states */ 45 enum 46 { 47 BTA_AG_INIT_ST, 48 BTA_AG_OPENING_ST, 49 BTA_AG_OPEN_ST, 50 BTA_AG_CLOSING_ST 51 }; 52 53 /* state machine action enumeration list */ 54 enum 55 { 56 BTA_AG_REGISTER, 57 BTA_AG_DEREGISTER, 58 BTA_AG_START_OPEN, 59 BTA_AG_RFC_DO_OPEN, 60 BTA_AG_RFC_DO_CLOSE, 61 BTA_AG_START_DEREG, 62 BTA_AG_START_CLOSE, 63 BTA_AG_RFC_OPEN, 64 BTA_AG_OPEN_FAIL, 65 BTA_AG_RFC_ACP_OPEN, 66 BTA_AG_RFC_CLOSE, 67 BTA_AG_RFC_FAIL, 68 BTA_AG_RFC_DATA, 69 BTA_AG_DISC_INT_RES, 70 BTA_AG_DISC_FAIL, 71 BTA_AG_DISC_ACP_RES, 72 BTA_AG_FREE_DB, 73 BTA_AG_SCO_CONN_OPEN, 74 BTA_AG_SCO_CONN_CLOSE, 75 BTA_AG_SCO_LISTEN, 76 BTA_AG_SCO_OPEN, 77 BTA_AG_SCO_CLOSE, 78 BTA_AG_SCO_SHUTDOWN, 79 BTA_AG_POST_SCO_OPEN, 80 BTA_AG_POST_SCO_CLOSE, 81 BTA_AG_SVC_CONN_OPEN, 82 BTA_AG_RESULT, 83 BTA_AG_SETCODEC, 84 BTA_AG_SEND_RING, 85 BTA_AG_CI_SCO_DATA, 86 BTA_AG_CI_RX_DATA, 87 BTA_AG_RCVD_SLC_READY, 88 BTA_AG_NUM_ACTIONS 89 }; 90 91 #define BTA_AG_IGNORE BTA_AG_NUM_ACTIONS 92 93 /* type for action functions */ 94 typedef void (*tBTA_AG_ACTION)(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data); 95 96 /* action functions */ 97 const tBTA_AG_ACTION bta_ag_action[] = 98 { 99 bta_ag_register, 100 bta_ag_deregister, 101 bta_ag_start_open, 102 bta_ag_rfc_do_open, 103 bta_ag_rfc_do_close, 104 bta_ag_start_dereg, 105 bta_ag_start_close, 106 bta_ag_rfc_open, 107 bta_ag_open_fail, 108 bta_ag_rfc_acp_open, 109 bta_ag_rfc_close, 110 bta_ag_rfc_fail, 111 bta_ag_rfc_data, 112 bta_ag_disc_int_res, 113 bta_ag_disc_fail, 114 bta_ag_disc_acp_res, 115 bta_ag_free_db, 116 bta_ag_sco_conn_open, 117 bta_ag_sco_conn_close, 118 bta_ag_sco_listen, 119 bta_ag_sco_open, 120 bta_ag_sco_close, 121 bta_ag_sco_shutdown, 122 bta_ag_post_sco_open, 123 bta_ag_post_sco_close, 124 bta_ag_svc_conn_open, 125 bta_ag_result, 126 bta_ag_setcodec, 127 bta_ag_send_ring, 128 bta_ag_ci_sco_data, 129 bta_ag_ci_rx_data, 130 bta_ag_rcvd_slc_ready 131 }; 132 133 /* state table information */ 134 #define BTA_AG_ACTIONS 2 /* number of actions */ 135 #define BTA_AG_NEXT_STATE 2 /* position of next state */ 136 #define BTA_AG_NUM_COLS 3 /* number of columns in state tables */ 137 138 /* state table for init state */ 139 const UINT8 bta_ag_st_init[][BTA_AG_NUM_COLS] = 140 { 141 /* Event Action 1 Action 2 Next state */ 142 /* API_REGISTER_EVT */ {BTA_AG_REGISTER, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 143 /* API_DEREGISTER_EVT */ {BTA_AG_DEREGISTER, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 144 /* API_OPEN_EVT */ {BTA_AG_START_OPEN, BTA_AG_IGNORE, BTA_AG_OPENING_ST}, 145 /* API_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 146 /* API_AUDIO_OPEN_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 147 /* API_AUDIO_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 148 /* API_RESULT_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 149 /* API_SETCODEC_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 150 /* RFC_OPEN_EVT */ {BTA_AG_RFC_ACP_OPEN, BTA_AG_SCO_LISTEN, BTA_AG_OPEN_ST}, 151 /* RFC_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 152 /* RFC_SRV_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 153 /* RFC_DATA_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 154 /* SCO_OPEN_EVT */ {BTA_AG_SCO_CONN_OPEN, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 155 /* SCO_CLOSE_EVT */ {BTA_AG_SCO_CONN_CLOSE, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 156 /* DISC_ACP_RES_EVT */ {BTA_AG_FREE_DB, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 157 /* DISC_INT_RES_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 158 /* DISC_OK_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 159 /* DISC_FAIL_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 160 /* CI_RX_WRITE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 161 /* RING_TOUT_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 162 /* SVC_TOUT_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 163 /* CI_SCO_DATA_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 164 /* CI_SLC_READY_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_INIT_ST} 165 }; 166 167 /* state table for opening state */ 168 const UINT8 bta_ag_st_opening[][BTA_AG_NUM_COLS] = 169 { 170 /* Event Action 1 Action 2 Next state */ 171 /* API_REGISTER_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST}, 172 /* API_DEREGISTER_EVT */ {BTA_AG_RFC_DO_CLOSE, BTA_AG_START_DEREG, BTA_AG_CLOSING_ST}, 173 /* API_OPEN_EVT */ {BTA_AG_OPEN_FAIL, BTA_AG_IGNORE, BTA_AG_OPENING_ST}, 174 /* API_CLOSE_EVT */ {BTA_AG_RFC_DO_CLOSE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 175 /* API_AUDIO_OPEN_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST}, 176 /* API_AUDIO_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST}, 177 /* API_RESULT_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST}, 178 /* API_SETCODEC_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST}, 179 /* RFC_OPEN_EVT */ {BTA_AG_RFC_OPEN, BTA_AG_SCO_LISTEN, BTA_AG_OPEN_ST}, 180 /* RFC_CLOSE_EVT */ {BTA_AG_RFC_FAIL, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 181 /* RFC_SRV_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST}, 182 /* RFC_DATA_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST}, 183 /* SCO_OPEN_EVT */ {BTA_AG_SCO_CONN_OPEN, BTA_AG_IGNORE, BTA_AG_OPENING_ST}, 184 /* SCO_CLOSE_EVT */ {BTA_AG_SCO_CONN_CLOSE, BTA_AG_IGNORE, BTA_AG_OPENING_ST}, 185 /* DISC_ACP_RES_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST}, 186 /* DISC_INT_RES_EVT */ {BTA_AG_DISC_INT_RES, BTA_AG_IGNORE, BTA_AG_OPENING_ST}, 187 /* DISC_OK_EVT */ {BTA_AG_RFC_DO_OPEN, BTA_AG_IGNORE, BTA_AG_OPENING_ST}, 188 /* DISC_FAIL_EVT */ {BTA_AG_DISC_FAIL, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 189 /* CI_RX_WRITE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST}, 190 /* RING_TOUT_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST}, 191 /* SVC_TOUT_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST}, 192 /* CI_SCO_DATA_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST}, 193 /* CI_SLC_READY_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPENING_ST} 194 }; 195 196 /* state table for open state */ 197 const UINT8 bta_ag_st_open[][BTA_AG_NUM_COLS] = 198 { 199 /* Event Action 1 Action 2 Next state */ 200 /* API_REGISTER_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPEN_ST}, 201 /* API_DEREGISTER_EVT */ {BTA_AG_START_CLOSE, BTA_AG_START_DEREG, BTA_AG_CLOSING_ST}, 202 /* API_OPEN_EVT */ {BTA_AG_OPEN_FAIL, BTA_AG_IGNORE, BTA_AG_OPEN_ST}, 203 /* API_CLOSE_EVT */ {BTA_AG_START_CLOSE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 204 /* API_AUDIO_OPEN_EVT */ {BTA_AG_SCO_OPEN, BTA_AG_IGNORE, BTA_AG_OPEN_ST}, 205 /* API_AUDIO_CLOSE_EVT */ {BTA_AG_SCO_CLOSE, BTA_AG_IGNORE, BTA_AG_OPEN_ST}, 206 /* API_RESULT_EVT */ {BTA_AG_RESULT, BTA_AG_IGNORE, BTA_AG_OPEN_ST}, 207 /* API_SETCODEC_EVT */ {BTA_AG_SETCODEC, BTA_AG_IGNORE, BTA_AG_OPEN_ST}, 208 /* RFC_OPEN_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPEN_ST}, 209 /* RFC_CLOSE_EVT */ {BTA_AG_RFC_CLOSE, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 210 /* RFC_SRV_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPEN_ST}, 211 /* RFC_DATA_EVT */ {BTA_AG_RFC_DATA, BTA_AG_IGNORE, BTA_AG_OPEN_ST}, 212 /* SCO_OPEN_EVT */ {BTA_AG_SCO_CONN_OPEN, BTA_AG_POST_SCO_OPEN, BTA_AG_OPEN_ST}, 213 /* SCO_CLOSE_EVT */ {BTA_AG_SCO_CONN_CLOSE, BTA_AG_POST_SCO_CLOSE, BTA_AG_OPEN_ST}, 214 /* DISC_ACP_RES_EVT */ {BTA_AG_DISC_ACP_RES, BTA_AG_IGNORE, BTA_AG_OPEN_ST}, 215 /* DISC_INT_RES_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPEN_ST}, 216 /* DISC_OK_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPEN_ST}, 217 /* DISC_FAIL_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_OPEN_ST}, 218 /* CI_RX_WRITE_EVT */ {BTA_AG_CI_RX_DATA, BTA_AG_IGNORE, BTA_AG_OPEN_ST}, 219 /* RING_TOUT_EVT */ {BTA_AG_SEND_RING, BTA_AG_IGNORE, BTA_AG_OPEN_ST}, 220 /* SVC_TOUT_EVT */ {BTA_AG_START_CLOSE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 221 /* CI_SCO_DATA_EVT */ {BTA_AG_CI_SCO_DATA, BTA_AG_IGNORE, BTA_AG_OPEN_ST}, 222 /* CI_SLC_READY_EVT */ {BTA_AG_RCVD_SLC_READY, BTA_AG_IGNORE, BTA_AG_OPEN_ST} 223 }; 224 225 /* state table for closing state */ 226 const UINT8 bta_ag_st_closing[][BTA_AG_NUM_COLS] = 227 { 228 /* Event Action 1 Action 2 Next state */ 229 /* API_REGISTER_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 230 /* API_DEREGISTER_EVT */ {BTA_AG_START_DEREG, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 231 /* API_OPEN_EVT */ {BTA_AG_OPEN_FAIL, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 232 /* API_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 233 /* API_AUDIO_OPEN_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 234 /* API_AUDIO_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 235 /* API_RESULT_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 236 /* API_SETCODEC_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 237 /* RFC_OPEN_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 238 /* RFC_CLOSE_EVT */ {BTA_AG_RFC_CLOSE, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 239 /* RFC_SRV_CLOSE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 240 /* RFC_DATA_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 241 /* SCO_OPEN_EVT */ {BTA_AG_SCO_CONN_OPEN, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 242 /* SCO_CLOSE_EVT */ {BTA_AG_SCO_CONN_CLOSE, BTA_AG_POST_SCO_CLOSE, BTA_AG_CLOSING_ST}, 243 /* DISC_ACP_RES_EVT */ {BTA_AG_FREE_DB, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 244 /* DISC_INT_RES_EVT */ {BTA_AG_FREE_DB, BTA_AG_IGNORE, BTA_AG_INIT_ST}, 245 /* DISC_OK_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 246 /* DISC_FAIL_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 247 /* CI_RX_WRITE_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 248 /* RING_TOUT_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 249 /* SVC_TOUT_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 250 /* CI_SCO_DATA_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST}, 251 /* CI_SLC_READY_EVT */ {BTA_AG_IGNORE, BTA_AG_IGNORE, BTA_AG_CLOSING_ST} 252 }; 253 254 /* type for state table */ 255 typedef const UINT8 (*tBTA_AG_ST_TBL)[BTA_AG_NUM_COLS]; 256 257 /* state table */ 258 const tBTA_AG_ST_TBL bta_ag_st_tbl[] = 259 { 260 bta_ag_st_init, 261 bta_ag_st_opening, 262 bta_ag_st_open, 263 bta_ag_st_closing 264 }; 265 266 /***************************************************************************** 267 ** Global data 268 *****************************************************************************/ 269 270 /* AG control block */ 271 #if BTA_DYNAMIC_MEMORY == FALSE 272 tBTA_AG_CB bta_ag_cb; 273 #endif 274 275 /******************************************************************************* 276 ** 277 ** Function bta_ag_timer_cback 278 ** 279 ** Description AG timer callback. 280 ** 281 ** 282 ** Returns void 283 ** 284 *******************************************************************************/ 285 static void bta_ag_timer_cback(void *p) 286 { 287 BT_HDR *p_buf; 288 TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *) p; 289 290 if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL) 291 { 292 p_buf->event = p_tle->event; 293 p_buf->layer_specific = bta_ag_scb_to_idx((tBTA_AG_SCB *) p_tle->param); 294 bta_sys_sendmsg(p_buf); 295 } 296 } 297 298 /******************************************************************************* 299 ** 300 ** Function bta_ag_scb_alloc 301 ** 302 ** Description Allocate an AG service control block. 303 ** 304 ** 305 ** Returns pointer to the scb, or NULL if none could be allocated. 306 ** 307 *******************************************************************************/ 308 static tBTA_AG_SCB *bta_ag_scb_alloc(void) 309 { 310 tBTA_AG_SCB *p_scb = &bta_ag_cb.scb[0]; 311 int i; 312 313 for (i = 0; i < BTA_AG_NUM_SCB; i++, p_scb++) 314 { 315 if (!p_scb->in_use) 316 { 317 /* initialize variables */ 318 p_scb->in_use = TRUE; 319 p_scb->sco_idx = BTM_INVALID_SCO_INDEX; 320 321 /* set up timers */ 322 p_scb->act_timer.param = (UINT32) p_scb; 323 p_scb->act_timer.p_cback = bta_ag_timer_cback; 324 325 APPL_TRACE_DEBUG1("bta_ag_scb_alloc %d", bta_ag_scb_to_idx(p_scb)); 326 break; 327 } 328 } 329 330 if (i == BTA_AG_NUM_SCB) 331 { 332 /* out of scbs */ 333 p_scb = NULL; 334 APPL_TRACE_WARNING0("Out of ag scbs"); 335 } 336 return p_scb; 337 } 338 339 /******************************************************************************* 340 ** 341 ** Function bta_ag_scb_dealloc 342 ** 343 ** Description Deallocate a service control block. 344 ** 345 ** 346 ** Returns void 347 ** 348 *******************************************************************************/ 349 void bta_ag_scb_dealloc(tBTA_AG_SCB *p_scb) 350 { 351 UINT8 idx; 352 BOOLEAN allocated = FALSE; 353 354 APPL_TRACE_DEBUG1("bta_ag_scb_dealloc %d", bta_ag_scb_to_idx(p_scb)); 355 356 /* stop timers */ 357 bta_sys_stop_timer(&p_scb->act_timer); 358 #if (BTM_WBS_INCLUDED == TRUE) 359 bta_sys_stop_timer(&p_scb->cn_timer); 360 #endif 361 362 /* initialize control block */ 363 memset(p_scb, 0, sizeof(tBTA_AG_SCB)); 364 p_scb->sco_idx = BTM_INVALID_SCO_INDEX; 365 366 /* If all scbs are deallocated, callback with disable event */ 367 if (!bta_sys_is_register (BTA_ID_AG)) 368 { 369 for (idx = 0; idx < BTA_AG_NUM_SCB; idx++) 370 { 371 if (bta_ag_cb.scb[idx].in_use) 372 { 373 allocated = TRUE; 374 break; 375 } 376 } 377 378 if (!allocated) 379 { 380 (*bta_ag_cb.p_cback)(BTA_AG_DISABLE_EVT, NULL); 381 } 382 } 383 384 } 385 386 /******************************************************************************* 387 ** 388 ** Function bta_ag_scb_to_idx 389 ** 390 ** Description Given a pointer to an scb, return its index. 391 ** 392 ** 393 ** Returns Index of scb. 394 ** 395 *******************************************************************************/ 396 UINT16 bta_ag_scb_to_idx(tBTA_AG_SCB *p_scb) 397 { 398 /* use array arithmetic to determine index */ 399 return ((UINT16) (p_scb - bta_ag_cb.scb)) + 1; 400 } 401 402 /******************************************************************************* 403 ** 404 ** Function bta_ag_scb_by_idx 405 ** 406 ** Description Given an scb index return pointer to scb. 407 ** 408 ** 409 ** Returns Pointer to scb or NULL if not allocated. 410 ** 411 *******************************************************************************/ 412 tBTA_AG_SCB *bta_ag_scb_by_idx(UINT16 idx) 413 { 414 tBTA_AG_SCB *p_scb; 415 416 /* verify index */ 417 if (idx > 0 && idx <= BTA_AG_NUM_SCB) 418 { 419 p_scb = &bta_ag_cb.scb[idx - 1]; 420 if (!p_scb->in_use) 421 { 422 p_scb = NULL; 423 APPL_TRACE_WARNING1("ag scb idx %d not allocated", idx); 424 } 425 } 426 else 427 { 428 p_scb = NULL; 429 APPL_TRACE_DEBUG1("ag scb idx %d out of range", idx); 430 } 431 return p_scb; 432 } 433 434 /******************************************************************************* 435 ** 436 ** Function bta_ag_service_to_idx 437 ** 438 ** Description Given a BTA service mask convert to profile index. 439 ** 440 ** 441 ** Returns Profile ndex of scb. 442 ** 443 *******************************************************************************/ 444 UINT8 bta_ag_service_to_idx(tBTA_SERVICE_MASK services) 445 { 446 if (services & BTA_HFP_SERVICE_MASK) 447 { 448 return BTA_AG_HFP; 449 } 450 else 451 { 452 return BTA_AG_HSP; 453 } 454 } 455 456 /******************************************************************************* 457 ** 458 ** Function bta_ag_idx_by_bdaddr 459 ** 460 ** Description Find SCB associated with peer BD address. 461 ** 462 ** 463 ** Returns Index of SCB or zero if none found. 464 ** 465 *******************************************************************************/ 466 UINT16 bta_ag_idx_by_bdaddr(BD_ADDR peer_addr) 467 { 468 tBTA_AG_SCB *p_scb = &bta_ag_cb.scb[0]; 469 UINT16 i; 470 471 if (peer_addr != NULL) 472 { 473 for (i = 0; i < BTA_AG_NUM_SCB; i++, p_scb++) 474 { 475 if (p_scb->in_use && !bdcmp(peer_addr, p_scb->peer_addr)) 476 { 477 return (i + 1); 478 } 479 } 480 } 481 482 /* no scb found */ 483 APPL_TRACE_WARNING0("No ag scb for peer addr"); 484 return 0; 485 } 486 487 /******************************************************************************* 488 ** 489 ** Function bta_ag_other_scb_open 490 ** 491 ** Description Check whether any other scb is in open state. 492 ** 493 ** 494 ** Returns TRUE if another scb is in open state, FALSE otherwise. 495 ** 496 *******************************************************************************/ 497 BOOLEAN bta_ag_other_scb_open(tBTA_AG_SCB *p_curr_scb) 498 { 499 tBTA_AG_SCB *p_scb = &bta_ag_cb.scb[0]; 500 int i; 501 502 for (i = 0; i < BTA_AG_NUM_SCB; i++, p_scb++) 503 { 504 if (p_scb->in_use && p_scb != p_curr_scb && p_scb->state == BTA_AG_OPEN_ST) 505 { 506 return TRUE; 507 } 508 } 509 510 /* no other scb found */ 511 APPL_TRACE_DEBUG0("No other ag scb open"); 512 return FALSE; 513 } 514 515 /******************************************************************************* 516 ** 517 ** Function bta_ag_get_other_idle_scb 518 ** 519 ** Description Return other scb if it is in INIT st. 520 ** 521 ** 522 ** Returns Pointer to other scb if INIT st, NULL otherwise. 523 ** 524 *******************************************************************************/ 525 tBTA_AG_SCB *bta_ag_get_other_idle_scb (tBTA_AG_SCB *p_curr_scb) 526 { 527 tBTA_AG_SCB *p_scb = &bta_ag_cb.scb[0]; 528 UINT8 xx; 529 530 for (xx = 0; xx < BTA_AG_NUM_SCB; xx++, p_scb++) 531 { 532 if (p_scb->in_use && (p_scb != p_curr_scb) && (p_scb->state == BTA_AG_INIT_ST)) 533 { 534 return p_scb; 535 } 536 } 537 538 /* no other scb found */ 539 APPL_TRACE_DEBUG0("bta_ag_get_other_idle_scb: No idle AG scb"); 540 return NULL; 541 } 542 543 /******************************************************************************* 544 ** 545 ** Function bta_ag_colli_timer_cback 546 ** 547 ** Description AG connection collision timer callback 548 ** 549 ** 550 ** Returns void 551 ** 552 *******************************************************************************/ 553 static void bta_ag_colli_timer_cback (TIMER_LIST_ENT *p_tle) 554 { 555 tBTA_AG_SCB *p_scb; 556 557 APPL_TRACE_DEBUG0 ("bta_ag_colli_timer_cback"); 558 559 if (p_tle) 560 { 561 p_scb = (tBTA_AG_SCB *)p_tle->param; 562 563 if (p_scb) 564 { 565 p_scb->colli_tmr_on = FALSE; 566 567 /* If the peer haven't opened AG connection */ 568 /* we will restart opening process. */ 569 bta_ag_resume_open (p_scb); 570 } 571 } 572 } 573 574 /******************************************************************************* 575 ** 576 ** Function bta_ag_collision_cback 577 ** 578 ** Description Get notified about collision. 579 ** 580 ** 581 ** Returns void 582 ** 583 *******************************************************************************/ 584 void bta_ag_collision_cback (tBTA_SYS_CONN_STATUS status, UINT8 id, 585 UINT8 app_id, BD_ADDR peer_addr) 586 { 587 UINT16 handle; 588 tBTA_AG_SCB *p_scb; 589 590 /* Check if we have opening scb for the peer device. */ 591 handle = bta_ag_idx_by_bdaddr (peer_addr); 592 p_scb = bta_ag_scb_by_idx (handle); 593 594 if (p_scb && (p_scb->state == BTA_AG_OPENING_ST)) 595 { 596 if (id == BTA_ID_SYS) /* ACL collision */ 597 { 598 APPL_TRACE_WARNING0 ("AG found collision (ACL) ..."); 599 } 600 else if (id == BTA_ID_AG) /* RFCOMM collision */ 601 { 602 APPL_TRACE_WARNING0 ("AG found collision (RFCOMM) ..."); 603 } 604 else 605 { 606 APPL_TRACE_WARNING0 ("AG found collision (\?\?\?) ..."); 607 } 608 609 p_scb->state = BTA_AG_INIT_ST; 610 611 /* Cancel SDP if it had been started. */ 612 if(p_scb->p_disc_db) 613 { 614 (void)SDP_CancelServiceSearch (p_scb->p_disc_db); 615 bta_ag_free_db(p_scb, NULL); 616 } 617 618 /* reopen registered servers */ 619 /* Collision may be detected before or after we close servers. */ 620 if (bta_ag_is_server_closed (p_scb)) 621 bta_ag_start_servers(p_scb, p_scb->reg_services); 622 623 /* Start timer to han */ 624 p_scb->colli_timer.p_cback = (TIMER_CBACK*)&bta_ag_colli_timer_cback; 625 p_scb->colli_timer.param = (INT32)p_scb; 626 bta_sys_start_timer(&p_scb->colli_timer, 0, BTA_AG_COLLISION_TIMER); 627 p_scb->colli_tmr_on = TRUE; 628 } 629 630 } 631 632 /******************************************************************************* 633 ** 634 ** Function bta_ag_resume_open 635 ** 636 ** Description Resume opening process. 637 ** 638 ** 639 ** Returns void 640 ** 641 *******************************************************************************/ 642 void bta_ag_resume_open (tBTA_AG_SCB *p_scb) 643 { 644 if (p_scb) 645 { 646 APPL_TRACE_DEBUG1 ("bta_ag_resume_open, Handle(%d)", bta_ag_scb_to_idx(p_scb)); 647 648 /* resume opening process. */ 649 if (p_scb->state == BTA_AG_INIT_ST) 650 { 651 p_scb->state = BTA_AG_OPENING_ST; 652 bta_ag_start_open (p_scb, NULL); 653 } 654 } 655 else 656 { 657 APPL_TRACE_ERROR0 ("bta_ag_resume_open, Null p_scb"); 658 } 659 } 660 661 /******************************************************************************* 662 ** 663 ** Function bta_ag_api_enable 664 ** 665 ** Description Handle an API enable event. 666 ** 667 ** 668 ** Returns void 669 ** 670 *******************************************************************************/ 671 static void bta_ag_api_enable(tBTA_AG_DATA *p_data) 672 { 673 /* initialize control block */ 674 memset(&bta_ag_cb, 0, sizeof(tBTA_AG_CB)); 675 676 /* store callback function */ 677 bta_ag_cb.p_cback = p_data->api_enable.p_cback; 678 bta_ag_cb.parse_mode = p_data->api_enable.parse_mode; 679 680 /* call init call-out */ 681 bta_ag_co_init(); 682 683 bta_sys_collision_register (BTA_ID_AG, bta_ag_collision_cback); 684 685 /* call callback with enable event */ 686 (*bta_ag_cb.p_cback)(BTA_AG_ENABLE_EVT, NULL); 687 } 688 689 /******************************************************************************* 690 ** 691 ** Function bta_ag_api_disable 692 ** 693 ** Description Handle an API disable event. 694 ** 695 ** 696 ** Returns void 697 ** 698 *******************************************************************************/ 699 static void bta_ag_api_disable(tBTA_AG_DATA *p_data) 700 { 701 /* deregister all scbs in use */ 702 tBTA_AG_SCB *p_scb = &bta_ag_cb.scb[0]; 703 BOOLEAN do_dereg = FALSE; 704 int i; 705 706 if (!bta_sys_is_register (BTA_ID_AG)) 707 { 708 APPL_TRACE_ERROR0("BTA AG is already disabled, ignoring ..."); 709 return; 710 } 711 712 /* De-register with BTA system manager */ 713 GKI_sched_lock(); 714 bta_sys_deregister(BTA_ID_AG); 715 GKI_sched_unlock(); 716 717 for (i = 0; i < BTA_AG_NUM_SCB; i++, p_scb++) 718 { 719 if (p_scb->in_use) 720 { 721 bta_ag_sm_execute(p_scb, BTA_AG_API_DEREGISTER_EVT, p_data); 722 do_dereg = TRUE; 723 } 724 } 725 726 if (!do_dereg) 727 { 728 /* Done, send callback evt to app */ 729 (*bta_ag_cb.p_cback)(BTA_AG_DISABLE_EVT, NULL); 730 } 731 732 bta_sys_collision_register (BTA_ID_AG, NULL); 733 } 734 735 /******************************************************************************* 736 ** 737 ** Function bta_ag_api_register 738 ** 739 ** Description Handle an API event registers a new service. 740 ** 741 ** 742 ** Returns void 743 ** 744 *******************************************************************************/ 745 static void bta_ag_api_register(tBTA_AG_DATA *p_data) 746 { 747 tBTA_AG_SCB *p_scb; 748 tBTA_AG_REGISTER reg; 749 750 /* allocate an scb */ 751 if ((p_scb = bta_ag_scb_alloc()) != NULL) 752 { 753 bta_ag_sm_execute(p_scb, p_data->hdr.event, p_data); 754 } 755 else 756 { 757 reg.status = BTA_AG_FAIL_RESOURCES; 758 (*bta_ag_cb.p_cback)(BTA_AG_REGISTER_EVT, (tBTA_AG *) ®); 759 } 760 } 761 762 /******************************************************************************* 763 ** 764 ** Function bta_ag_api_result 765 ** 766 ** Description Handle an API result event. 767 ** 768 ** 769 ** Returns void 770 ** 771 *******************************************************************************/ 772 static void bta_ag_api_result(tBTA_AG_DATA *p_data) 773 { 774 tBTA_AG_SCB *p_scb; 775 int i; 776 777 if (p_data->hdr.layer_specific != BTA_AG_HANDLE_ALL) 778 { 779 if ((p_scb = bta_ag_scb_by_idx(p_data->hdr.layer_specific)) != NULL) 780 { 781 bta_ag_sm_execute(p_scb, BTA_AG_API_RESULT_EVT, p_data); 782 } 783 } 784 else 785 { 786 for (i = 0, p_scb = &bta_ag_cb.scb[0]; i < BTA_AG_NUM_SCB; i++, p_scb++) 787 { 788 if (p_scb->in_use) 789 { 790 bta_ag_sm_execute(p_scb, BTA_AG_API_RESULT_EVT, p_data); 791 } 792 } 793 } 794 } 795 796 /******************************************************************************* 797 ** 798 ** Function bta_ag_sm_execute 799 ** 800 ** Description State machine event handling function for AG 801 ** 802 ** 803 ** Returns void 804 ** 805 *******************************************************************************/ 806 void bta_ag_sm_execute(tBTA_AG_SCB *p_scb, UINT16 event, tBTA_AG_DATA *p_data) 807 { 808 tBTA_AG_ST_TBL state_table; 809 UINT8 action; 810 int i; 811 812 #if BTA_AG_DEBUG == TRUE 813 UINT16 in_event = event; 814 UINT8 in_state = p_scb->state; 815 816 /* Ignore displaying of AT results when not connected (Ignored in state machine) */ 817 if (in_event != BTA_AG_API_RESULT_EVT || p_scb->state == BTA_AG_OPEN_ST) 818 { 819 APPL_TRACE_EVENT5("AG evt (hdl 0x%04x): State %d (%s), Event 0x%04x (%s)", 820 bta_ag_scb_to_idx(p_scb), 821 p_scb->state, bta_ag_state_str(p_scb->state), 822 event, bta_ag_evt_str(event, p_data->api_result.result)); 823 } 824 #else 825 APPL_TRACE_EVENT3("AG evt (hdl 0x%04x): State %d, Event 0x%04x", 826 bta_ag_scb_to_idx(p_scb), p_scb->state, event); 827 #endif 828 829 event &= 0x00FF; 830 if (event >= (BTA_AG_MAX_EVT & 0x00FF)) 831 { 832 APPL_TRACE_ERROR0("AG evt out of range, ignoring..."); 833 return; 834 } 835 836 /* look up the state table for the current state */ 837 state_table = bta_ag_st_tbl[p_scb->state]; 838 839 /* set next state */ 840 p_scb->state = state_table[event][BTA_AG_NEXT_STATE]; 841 842 /* execute action functions */ 843 for (i = 0; i < BTA_AG_ACTIONS; i++) 844 { 845 if ((action = state_table[event][i]) != BTA_AG_IGNORE) 846 { 847 (*bta_ag_action[action])(p_scb, p_data); 848 } 849 else 850 { 851 break; 852 } 853 } 854 #if BTA_AG_DEBUG == TRUE 855 if (p_scb->state != in_state) 856 { 857 APPL_TRACE_EVENT3("BTA AG State Change: [%s] -> [%s] after Event [%s]", 858 bta_ag_state_str(in_state), 859 bta_ag_state_str(p_scb->state), 860 bta_ag_evt_str(in_event, p_data->api_result.result)); 861 } 862 #endif 863 } 864 865 /******************************************************************************* 866 ** 867 ** Function bta_ag_hdl_event 868 ** 869 ** Description Data gateway main event handling function. 870 ** 871 ** 872 ** Returns BOOLEAN 873 ** 874 *******************************************************************************/ 875 BOOLEAN bta_ag_hdl_event(BT_HDR *p_msg) 876 { 877 tBTA_AG_SCB *p_scb; 878 879 switch (p_msg->event) 880 { 881 /* handle enable event */ 882 case BTA_AG_API_ENABLE_EVT: 883 bta_ag_api_enable((tBTA_AG_DATA *) p_msg); 884 break; 885 886 /* handle disable event */ 887 case BTA_AG_API_DISABLE_EVT: 888 bta_ag_api_disable((tBTA_AG_DATA *) p_msg); 889 break; 890 891 /* handle register event */ 892 case BTA_AG_API_REGISTER_EVT: 893 bta_ag_api_register((tBTA_AG_DATA *) p_msg); 894 break; 895 896 /* handle result event */ 897 case BTA_AG_API_RESULT_EVT: 898 bta_ag_api_result((tBTA_AG_DATA *) p_msg); 899 break; 900 901 /* all others reference scb by handle */ 902 default: 903 if ((p_scb = bta_ag_scb_by_idx(p_msg->layer_specific)) != NULL) 904 { 905 bta_ag_sm_execute(p_scb, p_msg->event, (tBTA_AG_DATA *) p_msg); 906 } 907 break; 908 } 909 return TRUE; 910 } 911 912 #if BTA_AG_DEBUG == TRUE 913 static char *bta_ag_evt_str(UINT16 event, tBTA_AG_RES result) 914 { 915 switch (event) 916 { 917 case BTA_AG_API_REGISTER_EVT: 918 return "Register Request"; 919 case BTA_AG_API_DEREGISTER_EVT: 920 return "Deregister Request"; 921 case BTA_AG_API_OPEN_EVT: 922 return "Open SLC Request"; 923 case BTA_AG_API_CLOSE_EVT: 924 return "Close SLC Request"; 925 case BTA_AG_API_AUDIO_OPEN_EVT: 926 return "Open Audio Request"; 927 case BTA_AG_API_AUDIO_CLOSE_EVT: 928 return "Close Audio Request"; 929 case BTA_AG_API_RESULT_EVT: 930 switch (result) 931 { 932 case BTA_AG_SPK_RES: return ("AT Result BTA_AG_SPK_RES"); 933 case BTA_AG_MIC_RES: return ("AT Result BTA_AG_MIC_RES"); 934 case BTA_AG_INBAND_RING_RES: return ("AT Result BTA_AG_INBAND_RING_RES"); 935 case BTA_AG_CIND_RES: return ("AT Result BTA_AG_CIND_RES"); 936 case BTA_AG_BINP_RES: return ("AT Result BTA_AG_BINP_RES"); 937 case BTA_AG_IND_RES: return ("AT Result BTA_AG_IND_RES"); 938 case BTA_AG_BVRA_RES: return ("AT Result BTA_AG_BVRA_RES"); 939 case BTA_AG_CNUM_RES: return ("AT Result BTA_AG_CNUM_RES"); 940 case BTA_AG_BTRH_RES: return ("AT Result BTA_AG_BTRH_RES"); 941 case BTA_AG_CLCC_RES: return ("AT Result BTA_AG_CLCC_RES"); 942 case BTA_AG_COPS_RES: return ("AT Result BTA_AG_COPS_RES"); 943 case BTA_AG_IN_CALL_RES: return ("AT Result BTA_AG_IN_CALL_RES"); 944 case BTA_AG_IN_CALL_CONN_RES: return ("AT Result BTA_AG_IN_CALL_CONN_RES"); 945 case BTA_AG_CALL_WAIT_RES: return ("AT Result BTA_AG_CALL_WAIT_RES"); 946 case BTA_AG_OUT_CALL_ORIG_RES: return ("AT Result BTA_AG_OUT_CALL_ORIG_RES"); 947 case BTA_AG_OUT_CALL_ALERT_RES: return ("AT Result BTA_AG_OUT_CALL_ALERT_RES"); 948 case BTA_AG_OUT_CALL_CONN_RES: return ("AT Result BTA_AG_OUT_CALL_CONN_RES"); 949 case BTA_AG_CALL_CANCEL_RES: return ("AT Result BTA_AG_CALL_CANCEL_RES"); 950 case BTA_AG_END_CALL_RES: return ("AT Result BTA_AG_END_CALL_RES"); 951 case BTA_AG_UNAT_RES: return ("AT Result BTA_AG_UNAT_RES"); 952 default: return ("Unknown AG Result"); 953 } 954 case BTA_AG_API_SETCODEC_EVT: 955 return "Set Codec Request"; 956 case BTA_AG_RFC_OPEN_EVT: 957 return "RFC Opened"; 958 case BTA_AG_RFC_CLOSE_EVT: 959 return "RFC Closed"; 960 case BTA_AG_RFC_SRV_CLOSE_EVT: 961 return "RFC SRV Closed"; 962 case BTA_AG_RFC_DATA_EVT: 963 return "RFC Data"; 964 case BTA_AG_SCO_OPEN_EVT: 965 return "Audio Opened"; 966 case BTA_AG_SCO_CLOSE_EVT: 967 return "Audio Closed"; 968 case BTA_AG_DISC_ACP_RES_EVT: 969 return "Discovery ACP Result"; 970 case BTA_AG_DISC_INT_RES_EVT: 971 return "Discovery INT Result"; 972 case BTA_AG_DISC_OK_EVT: 973 return "Discovery OK"; 974 case BTA_AG_DISC_FAIL_EVT: 975 return "Discovery Failed"; 976 case BTA_AG_CI_RX_WRITE_EVT: 977 return "CI RX Write"; 978 case BTA_AG_RING_TOUT_EVT: 979 return "Ring Timeout"; 980 case BTA_AG_SVC_TOUT_EVT: 981 return "Service Timeout"; 982 case BTA_AG_API_ENABLE_EVT: 983 return "Enable AG"; 984 case BTA_AG_API_DISABLE_EVT: 985 return "Disable AG"; 986 case BTA_AG_CI_SCO_DATA_EVT: 987 return "SCO data Callin"; 988 case BTA_AG_CI_SLC_READY_EVT: 989 return "SLC Ready Callin"; 990 default: 991 return "Unknown AG Event"; 992 } 993 } 994 995 static char *bta_ag_state_str(UINT8 state) 996 { 997 switch (state) 998 { 999 case BTA_AG_INIT_ST: 1000 return "Initial"; 1001 case BTA_AG_OPENING_ST: 1002 return "Opening"; 1003 case BTA_AG_OPEN_ST: 1004 return "Open"; 1005 case BTA_AG_CLOSING_ST: 1006 return "Closing"; 1007 default: 1008 return "Unknown AG State"; 1009 } 1010 } 1011 1012 #endif 1013