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