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