1 /******************************************************************************
2 *
3 * Copyright 2004-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 stream state machine for the BTA advanced audio/video.
22 *
23 ******************************************************************************/
24
25 #include "bt_target.h" // Must be first to define build configuration
26
27 #define LOG_TAG "bt_bta_av"
28
29 #include "bta/av/bta_av_int.h"
30 #include "osi/include/log.h"
31
32 /*****************************************************************************
33 * Constants and types
34 ****************************************************************************/
35
36 /* state machine states */
37 enum {
38 BTA_AV_INIT_SST,
39 BTA_AV_INCOMING_SST,
40 BTA_AV_OPENING_SST,
41 BTA_AV_OPEN_SST,
42 BTA_AV_RCFG_SST,
43 BTA_AV_CLOSING_SST
44 };
45
bta_av_better_stream_state_machine(tBTA_AV_SCB * p_scb,uint16_t event,tBTA_AV_DATA * p_data)46 static void bta_av_better_stream_state_machine(tBTA_AV_SCB* p_scb,
47 uint16_t event,
48 tBTA_AV_DATA* p_data) {
49 uint8_t previous_state = p_scb->state;
50 tBTA_AV_ACT event_handler1 = nullptr;
51 tBTA_AV_ACT event_handler2 = nullptr;
52 switch (p_scb->state) {
53 case BTA_AV_INIT_SST:
54 switch (event) {
55 case BTA_AV_API_OPEN_EVT:
56 p_scb->state = BTA_AV_OPENING_SST;
57 event_handler1 = &bta_av_do_disc_a2dp;
58 break;
59 case BTA_AV_API_CLOSE_EVT:
60 event_handler1 = &bta_av_cleanup;
61 break;
62 case BTA_AV_SDP_DISC_OK_EVT:
63 event_handler1 = &bta_av_free_sdb;
64 break;
65 case BTA_AV_SDP_DISC_FAIL_EVT:
66 event_handler1 = &bta_av_free_sdb;
67 break;
68 case BTA_AV_STR_CONFIG_IND_EVT:
69 p_scb->state = BTA_AV_INCOMING_SST;
70 event_handler1 = &bta_av_config_ind;
71 break;
72 case BTA_AV_ACP_CONNECT_EVT:
73 p_scb->state = BTA_AV_INCOMING_SST;
74 break;
75 case BTA_AV_API_OFFLOAD_START_EVT:
76 event_handler1 = &bta_av_offload_req;
77 break;
78 case BTA_AV_API_OFFLOAD_START_RSP_EVT:
79 event_handler1 = &bta_av_offload_rsp;
80 break;
81 }
82 break;
83 case BTA_AV_INCOMING_SST:
84 switch (event) {
85 case BTA_AV_API_OPEN_EVT:
86 event_handler1 = &bta_av_open_at_inc;
87 break;
88 case BTA_AV_API_CLOSE_EVT:
89 p_scb->state = BTA_AV_CLOSING_SST;
90 event_handler1 = &bta_av_cco_close;
91 event_handler2 = &bta_av_disconnect_req;
92 break;
93 case BTA_AV_API_PROTECT_REQ_EVT:
94 event_handler1 = &bta_av_security_req;
95 break;
96 case BTA_AV_API_PROTECT_RSP_EVT:
97 event_handler1 = &bta_av_security_rsp;
98 break;
99 case BTA_AV_CI_SETCONFIG_OK_EVT:
100 event_handler1 = &bta_av_setconfig_rsp;
101 event_handler2 = &bta_av_st_rc_timer;
102 break;
103 case BTA_AV_CI_SETCONFIG_FAIL_EVT:
104 p_scb->state = BTA_AV_INIT_SST;
105 event_handler1 = &bta_av_setconfig_rej;
106 event_handler2 = &bta_av_cleanup;
107 break;
108 case BTA_AV_SDP_DISC_OK_EVT:
109 event_handler1 = &bta_av_free_sdb;
110 break;
111 case BTA_AV_SDP_DISC_FAIL_EVT:
112 event_handler1 = &bta_av_free_sdb;
113 break;
114 case BTA_AV_STR_DISC_OK_EVT:
115 event_handler1 = &bta_av_disc_res_as_acp;
116 break;
117 case BTA_AV_STR_GETCAP_OK_EVT:
118 event_handler1 = &bta_av_save_caps;
119 break;
120 case BTA_AV_STR_OPEN_OK_EVT:
121 p_scb->state = BTA_AV_OPEN_SST;
122 event_handler1 = &bta_av_str_opened;
123 break;
124 case BTA_AV_STR_CLOSE_EVT:
125 p_scb->state = BTA_AV_INIT_SST;
126 event_handler1 = &bta_av_cco_close;
127 event_handler2 = &bta_av_cleanup;
128 break;
129 case BTA_AV_STR_CONFIG_IND_EVT:
130 event_handler1 = &bta_av_config_ind;
131 break;
132 case BTA_AV_STR_SECURITY_IND_EVT:
133 event_handler1 = &bta_av_security_ind;
134 break;
135 case BTA_AV_STR_SECURITY_CFM_EVT:
136 event_handler1 = &bta_av_security_cfm;
137 break;
138 case BTA_AV_AVDT_DISCONNECT_EVT:
139 p_scb->state = BTA_AV_CLOSING_SST;
140 event_handler1 = &bta_av_cco_close;
141 event_handler2 = &bta_av_disconnect_req;
142 break;
143 case BTA_AV_AVDT_DELAY_RPT_EVT:
144 event_handler1 = &bta_av_delay_co;
145 break;
146 case BTA_AV_API_OFFLOAD_START_EVT:
147 event_handler1 = &bta_av_offload_req;
148 break;
149 case BTA_AV_API_OFFLOAD_START_RSP_EVT:
150 event_handler1 = &bta_av_offload_rsp;
151 break;
152 }
153 break;
154 case BTA_AV_OPENING_SST:
155 switch (event) {
156 case BTA_AV_API_CLOSE_EVT:
157 p_scb->state = BTA_AV_CLOSING_SST;
158 event_handler1 = &bta_av_do_close;
159 break;
160 case BTA_AV_API_PROTECT_REQ_EVT:
161 event_handler1 = &bta_av_security_req;
162 break;
163 case BTA_AV_API_PROTECT_RSP_EVT:
164 event_handler1 = &bta_av_security_rsp;
165 break;
166 case BTA_AV_SDP_DISC_OK_EVT:
167 event_handler1 = &bta_av_connect_req;
168 break;
169 case BTA_AV_SDP_DISC_FAIL_EVT:
170 event_handler1 = &bta_av_connect_req;
171 break;
172 case BTA_AV_STR_DISC_OK_EVT:
173 event_handler1 = &bta_av_disc_results;
174 break;
175 case BTA_AV_STR_DISC_FAIL_EVT:
176 p_scb->state = BTA_AV_CLOSING_SST;
177 event_handler1 = &bta_av_open_failed;
178 break;
179 case BTA_AV_STR_GETCAP_OK_EVT:
180 event_handler1 = &bta_av_getcap_results;
181 break;
182 case BTA_AV_STR_GETCAP_FAIL_EVT:
183 p_scb->state = BTA_AV_CLOSING_SST;
184 event_handler1 = &bta_av_open_failed;
185 break;
186 case BTA_AV_STR_OPEN_OK_EVT:
187 p_scb->state = BTA_AV_OPEN_SST;
188 event_handler1 = &bta_av_st_rc_timer;
189 event_handler2 = &bta_av_str_opened;
190 break;
191 case BTA_AV_STR_OPEN_FAIL_EVT:
192 p_scb->state = BTA_AV_CLOSING_SST;
193 event_handler1 = &bta_av_open_failed;
194 break;
195 case BTA_AV_STR_CONFIG_IND_EVT:
196 p_scb->state = BTA_AV_INCOMING_SST;
197 event_handler1 = &bta_av_config_ind;
198 break;
199 case BTA_AV_STR_SECURITY_IND_EVT:
200 event_handler1 = &bta_av_security_ind;
201 break;
202 case BTA_AV_STR_SECURITY_CFM_EVT:
203 event_handler1 = &bta_av_security_cfm;
204 break;
205 case BTA_AV_AVRC_TIMER_EVT:
206 event_handler1 = &bta_av_switch_role;
207 break;
208 case BTA_AV_AVDT_CONNECT_EVT:
209 event_handler1 = &bta_av_discover_req;
210 break;
211 case BTA_AV_AVDT_DISCONNECT_EVT:
212 p_scb->state = BTA_AV_INIT_SST;
213 event_handler1 = &bta_av_conn_failed;
214 break;
215 case BTA_AV_ROLE_CHANGE_EVT:
216 event_handler1 = &bta_av_role_res;
217 break;
218 case BTA_AV_AVDT_DELAY_RPT_EVT:
219 event_handler1 = &bta_av_delay_co;
220 break;
221 case BTA_AV_API_OFFLOAD_START_EVT:
222 event_handler1 = &bta_av_offload_req;
223 break;
224 case BTA_AV_API_OFFLOAD_START_RSP_EVT:
225 event_handler1 = &bta_av_offload_rsp;
226 break;
227 }
228 break;
229 case BTA_AV_OPEN_SST:
230 switch (event) {
231 case BTA_AV_API_CLOSE_EVT:
232 p_scb->state = BTA_AV_CLOSING_SST;
233 event_handler1 = &bta_av_do_close;
234 break;
235 case BTA_AV_AP_START_EVT:
236 event_handler1 = &bta_av_do_start;
237 break;
238 case BTA_AV_AP_STOP_EVT:
239 event_handler1 = &bta_av_str_stopped;
240 break;
241 case BTA_AV_API_RECONFIG_EVT:
242 p_scb->state = BTA_AV_RCFG_SST;
243 event_handler1 = &bta_av_reconfig;
244 break;
245 case BTA_AV_API_PROTECT_REQ_EVT:
246 event_handler1 = &bta_av_security_req;
247 break;
248 case BTA_AV_API_PROTECT_RSP_EVT:
249 event_handler1 = &bta_av_security_rsp;
250 break;
251 case BTA_AV_API_RC_OPEN_EVT:
252 event_handler1 = &bta_av_set_use_rc;
253 break;
254 case BTA_AV_SRC_DATA_READY_EVT:
255 event_handler1 = &bta_av_data_path;
256 break;
257 case BTA_AV_SDP_DISC_OK_EVT:
258 event_handler1 = &bta_av_free_sdb;
259 break;
260 case BTA_AV_SDP_DISC_FAIL_EVT:
261 event_handler1 = &bta_av_free_sdb;
262 break;
263 case BTA_AV_STR_GETCAP_OK_EVT:
264 event_handler1 = &bta_av_save_caps;
265 break;
266 case BTA_AV_STR_START_OK_EVT:
267 event_handler1 = &bta_av_start_ok;
268 break;
269 case BTA_AV_STR_START_FAIL_EVT:
270 event_handler1 = &bta_av_start_failed;
271 break;
272 case BTA_AV_STR_CLOSE_EVT:
273 p_scb->state = BTA_AV_INIT_SST;
274 event_handler1 = &bta_av_str_closed;
275 break;
276 case BTA_AV_STR_CONFIG_IND_EVT:
277 event_handler1 = &bta_av_setconfig_rej;
278 break;
279 case BTA_AV_STR_SECURITY_IND_EVT:
280 event_handler1 = &bta_av_security_ind;
281 break;
282 case BTA_AV_STR_SECURITY_CFM_EVT:
283 event_handler1 = &bta_av_security_cfm;
284 break;
285 case BTA_AV_STR_WRITE_CFM_EVT:
286 event_handler1 = &bta_av_clr_cong;
287 event_handler2 = &bta_av_data_path;
288 break;
289 case BTA_AV_STR_SUSPEND_CFM_EVT:
290 event_handler1 = &bta_av_suspend_cfm;
291 break;
292 case BTA_AV_AVRC_TIMER_EVT:
293 event_handler1 = &bta_av_open_rc;
294 break;
295 case BTA_AV_AVDT_DISCONNECT_EVT:
296 p_scb->state = BTA_AV_INIT_SST;
297 event_handler1 = &bta_av_str_closed;
298 break;
299 case BTA_AV_ROLE_CHANGE_EVT:
300 event_handler1 = &bta_av_role_res;
301 break;
302 case BTA_AV_AVDT_DELAY_RPT_EVT:
303 event_handler1 = &bta_av_delay_co;
304 break;
305 case BTA_AV_API_OFFLOAD_START_EVT:
306 event_handler1 = &bta_av_offload_req;
307 break;
308 case BTA_AV_API_OFFLOAD_START_RSP_EVT:
309 event_handler1 = &bta_av_offload_rsp;
310 break;
311 }
312 break;
313 case BTA_AV_RCFG_SST:
314 switch (event) {
315 case BTA_AV_API_CLOSE_EVT:
316 p_scb->state = BTA_AV_CLOSING_SST;
317 event_handler1 = &bta_av_disconnect_req;
318 break;
319 case BTA_AV_API_RECONFIG_EVT:
320 event_handler1 = &bta_av_reconfig;
321 break;
322 case BTA_AV_SDP_DISC_OK_EVT:
323 event_handler1 = &bta_av_free_sdb;
324 break;
325 case BTA_AV_SDP_DISC_FAIL_EVT:
326 event_handler1 = &bta_av_free_sdb;
327 break;
328 case BTA_AV_STR_DISC_OK_EVT:
329 event_handler1 = &bta_av_disc_results;
330 break;
331 case BTA_AV_STR_DISC_FAIL_EVT:
332 p_scb->state = BTA_AV_INIT_SST;
333 event_handler1 = &bta_av_str_closed;
334 break;
335 case BTA_AV_STR_GETCAP_OK_EVT:
336 event_handler1 = &bta_av_getcap_results;
337 break;
338 case BTA_AV_STR_GETCAP_FAIL_EVT:
339 p_scb->state = BTA_AV_INIT_SST;
340 event_handler1 = &bta_av_str_closed;
341 break;
342 case BTA_AV_STR_OPEN_OK_EVT:
343 p_scb->state = BTA_AV_OPEN_SST;
344 event_handler1 = &bta_av_rcfg_str_ok;
345 break;
346 case BTA_AV_STR_OPEN_FAIL_EVT:
347 event_handler1 = &bta_av_rcfg_failed;
348 break;
349 case BTA_AV_STR_CLOSE_EVT:
350 event_handler1 = &bta_av_rcfg_connect;
351 break;
352 case BTA_AV_STR_CONFIG_IND_EVT:
353 event_handler1 = &bta_av_setconfig_rej;
354 break;
355 case BTA_AV_STR_SUSPEND_CFM_EVT:
356 event_handler1 = &bta_av_suspend_cfm;
357 event_handler2 = &bta_av_suspend_cont;
358 break;
359 case BTA_AV_STR_RECONFIG_CFM_EVT:
360 event_handler1 = &bta_av_rcfg_cfm;
361 break;
362 case BTA_AV_AVDT_CONNECT_EVT:
363 event_handler1 = &bta_av_rcfg_open;
364 break;
365 case BTA_AV_AVDT_DISCONNECT_EVT:
366 event_handler1 = &bta_av_rcfg_discntd;
367 break;
368 case BTA_AV_AVDT_DELAY_RPT_EVT:
369 event_handler1 = &bta_av_delay_co;
370 break;
371 case BTA_AV_API_OFFLOAD_START_EVT:
372 event_handler1 = &bta_av_offload_req;
373 break;
374 case BTA_AV_API_OFFLOAD_START_RSP_EVT:
375 event_handler1 = &bta_av_offload_rsp;
376 break;
377 }
378 break;
379 case BTA_AV_CLOSING_SST:
380 switch (event) {
381 case BTA_AV_API_CLOSE_EVT:
382 event_handler1 = &bta_av_disconnect_req;
383 break;
384 case BTA_AV_SDP_DISC_OK_EVT:
385 p_scb->state = BTA_AV_INIT_SST;
386 event_handler1 = &bta_av_sdp_failed;
387 break;
388 case BTA_AV_SDP_DISC_FAIL_EVT:
389 p_scb->state = BTA_AV_INIT_SST;
390 event_handler1 = &bta_av_sdp_failed;
391 break;
392 case BTA_AV_STR_OPEN_OK_EVT:
393 event_handler1 = &bta_av_do_close;
394 break;
395 case BTA_AV_STR_OPEN_FAIL_EVT:
396 event_handler1 = &bta_av_disconnect_req;
397 break;
398 case BTA_AV_STR_CLOSE_EVT:
399 event_handler1 = &bta_av_disconnect_req;
400 break;
401 case BTA_AV_STR_CONFIG_IND_EVT:
402 event_handler1 = &bta_av_setconfig_rej;
403 break;
404 case BTA_AV_STR_SECURITY_IND_EVT:
405 event_handler1 = &bta_av_security_rej;
406 break;
407 case BTA_AV_AVDT_DISCONNECT_EVT:
408 p_scb->state = BTA_AV_INIT_SST;
409 event_handler1 = &bta_av_str_closed;
410 break;
411 case BTA_AV_API_OFFLOAD_START_EVT:
412 event_handler1 = &bta_av_offload_req;
413 break;
414 case BTA_AV_API_OFFLOAD_START_RSP_EVT:
415 event_handler1 = &bta_av_offload_rsp;
416 break;
417 }
418 break;
419 }
420
421 if (previous_state != p_scb->state) {
422 LOG_INFO("peer %s p_scb=%#x(%p) AV event=0x%x(%s) state=%d(%s) -> %d(%s)",
423 ADDRESS_TO_LOGGABLE_CSTR(p_scb->PeerAddress()), p_scb->hndl, p_scb,
424 event, bta_av_evt_code(event), previous_state,
425 bta_av_sst_code(previous_state), p_scb->state,
426 bta_av_sst_code(p_scb->state));
427
428 } else {
429 LOG_VERBOSE("peer %s p_scb=%#x(%p) AV event=0x%x(%s) state=%d(%s)",
430 ADDRESS_TO_LOGGABLE_CSTR(p_scb->PeerAddress()), p_scb->hndl,
431 p_scb, event, bta_av_evt_code(event), p_scb->state,
432 bta_av_sst_code(p_scb->state));
433 }
434
435 if (event_handler1 != nullptr) {
436 event_handler1(p_scb, p_data);
437 }
438 if (event_handler2 != nullptr) {
439 event_handler2(p_scb, p_data);
440 }
441 }
442
443 /*******************************************************************************
444 *
445 * Function bta_av_ssm_execute
446 *
447 * Description Stream state machine event handling function for AV
448 *
449 *
450 * Returns void
451 *
452 ******************************************************************************/
bta_av_ssm_execute(tBTA_AV_SCB * p_scb,uint16_t event,tBTA_AV_DATA * p_data)453 void bta_av_ssm_execute(tBTA_AV_SCB* p_scb, uint16_t event,
454 tBTA_AV_DATA* p_data) {
455 if (p_scb == NULL) {
456 /* this stream is not registered */
457 APPL_TRACE_EVENT("%s: AV channel not registered", __func__);
458 return;
459 }
460
461 bta_av_better_stream_state_machine(p_scb, event, p_data);
462 }
463
464 /*******************************************************************************
465 *
466 * Function bta_av_is_scb_opening
467 *
468 * Description Returns true is scb is in opening state.
469 *
470 *
471 * Returns true if scb is in opening state.
472 *
473 ******************************************************************************/
bta_av_is_scb_opening(tBTA_AV_SCB * p_scb)474 bool bta_av_is_scb_opening(tBTA_AV_SCB* p_scb) {
475 bool is_opening = false;
476
477 if (p_scb) {
478 if (p_scb->state == BTA_AV_OPENING_SST) is_opening = true;
479 }
480
481 return is_opening;
482 }
483
484 /*******************************************************************************
485 *
486 * Function bta_av_is_scb_incoming
487 *
488 * Description Returns true is scb is in incoming state.
489 *
490 *
491 * Returns true if scb is in incoming state.
492 *
493 ******************************************************************************/
bta_av_is_scb_incoming(tBTA_AV_SCB * p_scb)494 bool bta_av_is_scb_incoming(tBTA_AV_SCB* p_scb) {
495 bool is_incoming = false;
496
497 if (p_scb) {
498 if (p_scb->state == BTA_AV_INCOMING_SST) is_incoming = true;
499 }
500
501 return is_incoming;
502 }
503
504 /*******************************************************************************
505 *
506 * Function bta_av_set_scb_sst_init
507 *
508 * Description Set SST state to INIT.
509 * Use this function to change SST outside of state machine.
510 *
511 * Returns None
512 *
513 ******************************************************************************/
bta_av_set_scb_sst_init(tBTA_AV_SCB * p_scb)514 void bta_av_set_scb_sst_init(tBTA_AV_SCB* p_scb) {
515 if (p_scb == nullptr) {
516 return;
517 }
518
519 uint8_t next_state = BTA_AV_INIT_SST;
520
521 APPL_TRACE_VERBOSE(
522 "%s: peer %s AV (hndl=0x%x) state=%d(%s) next state=%d(%s) p_scb=%p",
523 __func__, ADDRESS_TO_LOGGABLE_CSTR(p_scb->PeerAddress()), p_scb->hndl,
524 p_scb->state, bta_av_sst_code(p_scb->state), next_state,
525 bta_av_sst_code(next_state), p_scb);
526
527 p_scb->state = next_state;
528 }
529
530 /*******************************************************************************
531 *
532 * Function bta_av_is_scb_init
533 *
534 * Description Returns true is scb is in init state.
535 *
536 *
537 * Returns true if scb is in incoming state.
538 *
539 ******************************************************************************/
bta_av_is_scb_init(tBTA_AV_SCB * p_scb)540 bool bta_av_is_scb_init(tBTA_AV_SCB* p_scb) {
541 bool is_init = false;
542
543 if (p_scb) {
544 if (p_scb->state == BTA_AV_INIT_SST) is_init = true;
545 }
546
547 return is_init;
548 }
549
550 /*******************************************************************************
551 *
552 * Function bta_av_set_scb_sst_incoming
553 *
554 * Description Set SST state to incoming.
555 * Use this function to change SST outside of state machine.
556 *
557 * Returns None
558 *
559 ******************************************************************************/
bta_av_set_scb_sst_incoming(tBTA_AV_SCB * p_scb)560 void bta_av_set_scb_sst_incoming(tBTA_AV_SCB* p_scb) {
561 if (p_scb) {
562 p_scb->state = BTA_AV_INCOMING_SST;
563 }
564 }
565
566 /*****************************************************************************
567 * Debug Functions
568 ****************************************************************************/
569 /*******************************************************************************
570 *
571 * Function bta_av_sst_code
572 *
573 * Description
574 *
575 * Returns char *
576 *
577 ******************************************************************************/
bta_av_sst_code(uint8_t state)578 const char* bta_av_sst_code(uint8_t state) {
579 switch (state) {
580 case BTA_AV_INIT_SST:
581 return "INIT";
582 case BTA_AV_INCOMING_SST:
583 return "INCOMING";
584 case BTA_AV_OPENING_SST:
585 return "OPENING";
586 case BTA_AV_OPEN_SST:
587 return "OPEN";
588 case BTA_AV_RCFG_SST:
589 return "RCFG";
590 case BTA_AV_CLOSING_SST:
591 return "CLOSING";
592 default:
593 return "unknown";
594 }
595 }
596