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