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