1 /******************************************************************************
2  *
3  *  Copyright (C) 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 #include <string.h>
25 
26 #include "bt_target.h"
27 #include "bta_av_co.h"
28 #include "bta_av_int.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 
44 /* state machine action enumeration list */
45 enum {
46   BTA_AV_DO_DISC,
47   BTA_AV_CLEANUP,
48   BTA_AV_FREE_SDB,
49   BTA_AV_CONFIG_IND,
50   BTA_AV_DISCONNECT_REQ,
51   BTA_AV_SECURITY_REQ,
52   BTA_AV_SECURITY_RSP,
53   BTA_AV_SETCONFIG_RSP,
54   BTA_AV_ST_RC_TIMER,
55   BTA_AV_STR_OPENED,
56   BTA_AV_SECURITY_IND,
57   BTA_AV_SECURITY_CFM,
58   BTA_AV_DO_CLOSE,
59   BTA_AV_CONNECT_REQ,
60   BTA_AV_SDP_FAILED,
61   BTA_AV_DISC_RESULTS,
62   BTA_AV_DISC_RES_AS_ACP,
63   BTA_AV_OPEN_FAILED,
64   BTA_AV_GETCAP_RESULTS,
65   BTA_AV_SETCONFIG_REJ,
66   BTA_AV_DISCOVER_REQ,
67   BTA_AV_CONN_FAILED,
68   BTA_AV_DO_START,
69   BTA_AV_STR_STOPPED,
70   BTA_AV_RECONFIG,
71   BTA_AV_DATA_PATH,
72   BTA_AV_START_OK,
73   BTA_AV_START_FAILED,
74   BTA_AV_STR_CLOSED,
75   BTA_AV_CLR_CONG,
76   BTA_AV_SUSPEND_CFM,
77   BTA_AV_RCFG_STR_OK,
78   BTA_AV_RCFG_FAILED,
79   BTA_AV_RCFG_CONNECT,
80   BTA_AV_RCFG_DISCNTD,
81   BTA_AV_SUSPEND_CONT,
82   BTA_AV_RCFG_CFM,
83   BTA_AV_RCFG_OPEN,
84   BTA_AV_SECURITY_REJ,
85   BTA_AV_OPEN_RC,
86   BTA_AV_CHK_2ND_START,
87   BTA_AV_SAVE_CAPS,
88   BTA_AV_SET_USE_RC,
89   BTA_AV_CCO_CLOSE,
90   BTA_AV_SWITCH_ROLE,
91   BTA_AV_ROLE_RES,
92   BTA_AV_DELAY_CO,
93   BTA_AV_OPEN_AT_INC,
94   BTA_AV_OFFLOAD_REQ,
95   BTA_AV_OFFLOAD_RSP,
96   BTA_AV_NUM_SACTIONS
97 };
98 
99 #define BTA_AV_SIGNORE BTA_AV_NUM_SACTIONS
100 
101 /* state table information */
102 /* #define BTA_AV_SACTION_COL           0       position of actions */
103 #define BTA_AV_SACTIONS 2    /* number of actions */
104 #define BTA_AV_SNEXT_STATE 2 /* position of next state */
105 #define BTA_AV_NUM_COLS 3    /* number of columns in state tables */
106 
107 /* state table for init state */
108 static const uint8_t bta_av_sst_init[][BTA_AV_NUM_COLS] = {
109     /* Event                     Action 1               Action 2 Next state */
110     /* AP_OPEN_EVT */ {BTA_AV_DO_DISC, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
111     /* AP_CLOSE_EVT */ {BTA_AV_CLEANUP, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
112     /* AP_START_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
113     /* AP_STOP_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
114     /* API_RECONFIG_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
115     /* API_PROTECT_REQ_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
116     /* API_PROTECT_RSP_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
117     /* API_RC_OPEN_EVT  */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
118     /* SRC_DATA_READY_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
119     /* CI_SETCONFIG_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
120     /* CI_SETCONFIG_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
121                                  BTA_AV_INIT_SST},
122     /* SDP_DISC_OK_EVT */ {BTA_AV_FREE_SDB, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
123     /* SDP_DISC_FAIL_EVT */ {BTA_AV_FREE_SDB, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
124     /* STR_DISC_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
125     /* STR_DISC_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
126     /* STR_GETCAP_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
127     /* STR_GETCAP_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
128     /* STR_OPEN_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
129     /* STR_OPEN_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
130     /* STR_START_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
131     /* STR_START_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
132     /* STR_CLOSE_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
133     /* STR_CONFIG_IND_EVT */ {BTA_AV_SETCONFIG_REJ, BTA_AV_SIGNORE,
134                               BTA_AV_INIT_SST},
135     /* STR_SECURITY_IND_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
136                                 BTA_AV_INIT_SST},
137     /* STR_SECURITY_CFM_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
138                                 BTA_AV_INIT_SST},
139     /* STR_WRITE_CFM_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
140     /* STR_SUSPEND_CFM_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
141     /* STR_RECONFIG_CFM_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
142                                 BTA_AV_INIT_SST},
143     /* AVRC_TIMER_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
144     /* AVDT_CONNECT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
145     /* AVDT_DISCONNECT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
146     /* ROLE_CHANGE_EVT*/ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
147     /* AVDT_DELAY_RPT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
148     /* ACP_CONNECT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
149     /* API_OFFLOAD_START_EVT */ {BTA_AV_OFFLOAD_REQ, BTA_AV_SIGNORE,
150                                  BTA_AV_INIT_SST},
151     /* API_OFFLOAD_START_RSP_EVT */ {BTA_AV_OFFLOAD_RSP, BTA_AV_SIGNORE,
152                                      BTA_AV_INIT_SST}};
153 
154 /* state table for incoming state */
155 static const uint8_t bta_av_sst_incoming[][BTA_AV_NUM_COLS] = {
156     /* Event                     Action 1               Action 2 Next state */
157     /* AP_OPEN_EVT */ {BTA_AV_OPEN_AT_INC, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
158     /* AP_CLOSE_EVT */ {BTA_AV_CCO_CLOSE, BTA_AV_DISCONNECT_REQ,
159                         BTA_AV_CLOSING_SST},
160     /* AP_START_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
161     /* AP_STOP_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
162     /* API_RECONFIG_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
163                             BTA_AV_INCOMING_SST},
164     /* API_PROTECT_REQ_EVT */ {BTA_AV_SECURITY_REQ, BTA_AV_SIGNORE,
165                                BTA_AV_INCOMING_SST},
166     /* API_PROTECT_RSP_EVT */ {BTA_AV_SECURITY_RSP, BTA_AV_SIGNORE,
167                                BTA_AV_INCOMING_SST},
168     /* API_RC_OPEN_EVT  */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
169                             BTA_AV_INCOMING_SST},
170     /* SRC_DATA_READY_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
171                               BTA_AV_INCOMING_SST},
172     /* CI_SETCONFIG_OK_EVT */ {BTA_AV_SETCONFIG_RSP, BTA_AV_ST_RC_TIMER,
173                                BTA_AV_INCOMING_SST},
174     /* CI_SETCONFIG_FAIL_EVT */ {BTA_AV_SETCONFIG_REJ, BTA_AV_CLEANUP,
175                                  BTA_AV_INIT_SST},
176     /* SDP_DISC_OK_EVT */ {BTA_AV_FREE_SDB, BTA_AV_SIGNORE,
177                            BTA_AV_INCOMING_SST},
178     /* SDP_DISC_FAIL_EVT */ {BTA_AV_FREE_SDB, BTA_AV_SIGNORE,
179                              BTA_AV_INCOMING_SST},
180     /* STR_DISC_OK_EVT */ {BTA_AV_DISC_RES_AS_ACP, BTA_AV_SIGNORE,
181                            BTA_AV_INCOMING_SST},
182     /* STR_DISC_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
183                              BTA_AV_INCOMING_SST},
184     /* STR_GETCAP_OK_EVT */ {BTA_AV_SAVE_CAPS, BTA_AV_SIGNORE,
185                              BTA_AV_INCOMING_SST},
186     /* STR_GETCAP_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
187                                BTA_AV_INCOMING_SST},
188     /* STR_OPEN_OK_EVT */ {BTA_AV_STR_OPENED, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
189     /* STR_OPEN_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
190                              BTA_AV_INCOMING_SST},
191     /* STR_START_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
192                             BTA_AV_INCOMING_SST},
193     /* STR_START_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
194                               BTA_AV_INCOMING_SST},
195     /* STR_CLOSE_EVT */ {BTA_AV_CCO_CLOSE, BTA_AV_CLEANUP, BTA_AV_INIT_SST},
196     /* STR_CONFIG_IND_EVT */ {BTA_AV_CONFIG_IND, BTA_AV_SIGNORE,
197                               BTA_AV_INCOMING_SST},
198     /* STR_SECURITY_IND_EVT */ {BTA_AV_SECURITY_IND, BTA_AV_SIGNORE,
199                                 BTA_AV_INCOMING_SST},
200     /* STR_SECURITY_CFM_EVT */ {BTA_AV_SECURITY_CFM, BTA_AV_SIGNORE,
201                                 BTA_AV_INCOMING_SST},
202     /* STR_WRITE_CFM_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
203                              BTA_AV_INCOMING_SST},
204     /* STR_SUSPEND_CFM_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
205                                BTA_AV_INCOMING_SST},
206     /* STR_RECONFIG_CFM_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
207                                 BTA_AV_INCOMING_SST},
208     /* AVRC_TIMER_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
209     /* AVDT_CONNECT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
210                             BTA_AV_INCOMING_SST},
211     /* AVDT_DISCONNECT_EVT */ {BTA_AV_CCO_CLOSE, BTA_AV_DISCONNECT_REQ,
212                                BTA_AV_CLOSING_SST},
213     /* ROLE_CHANGE_EVT*/ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
214     /* AVDT_DELAY_RPT_EVT */ {BTA_AV_DELAY_CO, BTA_AV_SIGNORE,
215                               BTA_AV_INCOMING_SST},
216     /* ACP_CONNECT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
217     /* API_OFFLOAD_START_EVT */ {BTA_AV_OFFLOAD_REQ, BTA_AV_SIGNORE,
218                                  BTA_AV_INCOMING_SST},
219     /* API_OFFLOAD_START_RSP_EVT */ {BTA_AV_OFFLOAD_RSP, BTA_AV_SIGNORE,
220                                      BTA_AV_INCOMING_SST}};
221 
222 /* state table for opening state */
223 static const uint8_t bta_av_sst_opening[][BTA_AV_NUM_COLS] = {
224     /* Event                     Action 1               Action 2 Next state */
225     /* AP_OPEN_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
226     /* AP_CLOSE_EVT */ {BTA_AV_DO_CLOSE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
227     /* AP_START_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
228     /* AP_STOP_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
229     /* API_RECONFIG_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
230     /* API_PROTECT_REQ_EVT */ {BTA_AV_SECURITY_REQ, BTA_AV_SIGNORE,
231                                BTA_AV_OPENING_SST},
232     /* API_PROTECT_RSP_EVT */ {BTA_AV_SECURITY_RSP, BTA_AV_SIGNORE,
233                                BTA_AV_OPENING_SST},
234     /* API_RC_OPEN_EVT  */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
235     /* SRC_DATA_READY_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
236                               BTA_AV_OPENING_SST},
237     /* CI_SETCONFIG_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
238                                BTA_AV_OPENING_SST},
239     /* CI_SETCONFIG_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
240                                  BTA_AV_OPENING_SST},
241     /* SDP_DISC_OK_EVT */ {BTA_AV_CONNECT_REQ, BTA_AV_SIGNORE,
242                            BTA_AV_OPENING_SST},
243     /* SDP_DISC_FAIL_EVT */ {BTA_AV_CONNECT_REQ, BTA_AV_SIGNORE,
244                              BTA_AV_OPENING_SST},
245     /* STR_DISC_OK_EVT */ {BTA_AV_DISC_RESULTS, BTA_AV_SIGNORE,
246                            BTA_AV_OPENING_SST},
247     /* STR_DISC_FAIL_EVT */ {BTA_AV_OPEN_FAILED, BTA_AV_SIGNORE,
248                              BTA_AV_CLOSING_SST},
249     /* STR_GETCAP_OK_EVT */ {BTA_AV_GETCAP_RESULTS, BTA_AV_SIGNORE,
250                              BTA_AV_OPENING_SST},
251     /* STR_GETCAP_FAIL_EVT */ {BTA_AV_OPEN_FAILED, BTA_AV_SIGNORE,
252                                BTA_AV_CLOSING_SST},
253     /* STR_OPEN_OK_EVT */ {BTA_AV_ST_RC_TIMER, BTA_AV_STR_OPENED,
254                            BTA_AV_OPEN_SST},
255     /* STR_OPEN_FAIL_EVT */ {BTA_AV_OPEN_FAILED, BTA_AV_SIGNORE,
256                              BTA_AV_CLOSING_SST},
257     /* STR_START_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
258     /* STR_START_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
259                               BTA_AV_OPENING_SST},
260     /* STR_CLOSE_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
261     /* STR_CONFIG_IND_EVT */ {BTA_AV_CONFIG_IND, BTA_AV_SIGNORE,
262                               BTA_AV_INCOMING_SST},
263     /* STR_SECURITY_IND_EVT */ {BTA_AV_SECURITY_IND, BTA_AV_SIGNORE,
264                                 BTA_AV_OPENING_SST},
265     /* STR_SECURITY_CFM_EVT */ {BTA_AV_SECURITY_CFM, BTA_AV_SIGNORE,
266                                 BTA_AV_OPENING_SST},
267     /* STR_WRITE_CFM_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
268                              BTA_AV_OPENING_SST},
269     /* STR_SUSPEND_CFM_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
270                                BTA_AV_OPENING_SST},
271     /* STR_RECONFIG_CFM_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
272                                 BTA_AV_OPENING_SST},
273     /* AVRC_TIMER_EVT */ {BTA_AV_SWITCH_ROLE, BTA_AV_SIGNORE,
274                           BTA_AV_OPENING_SST},
275     /* AVDT_CONNECT_EVT */ {BTA_AV_DISCOVER_REQ, BTA_AV_SIGNORE,
276                             BTA_AV_OPENING_SST},
277     /* AVDT_DISCONNECT_EVT */ {BTA_AV_CONN_FAILED, BTA_AV_SIGNORE,
278                                BTA_AV_INIT_SST},
279     /* ROLE_CHANGE_EVT*/ {BTA_AV_ROLE_RES, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
280     /* AVDT_DELAY_RPT_EVT */ {BTA_AV_DELAY_CO, BTA_AV_SIGNORE,
281                               BTA_AV_OPENING_SST},
282     /* ACP_CONNECT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
283     /* API_OFFLOAD_START_EVT */ {BTA_AV_OFFLOAD_REQ, BTA_AV_SIGNORE,
284                                  BTA_AV_OPENING_SST},
285     /* API_OFFLOAD_START_RSP_EVT */ {BTA_AV_OFFLOAD_RSP, BTA_AV_SIGNORE,
286                                      BTA_AV_OPENING_SST}};
287 
288 /* state table for open state */
289 static const uint8_t bta_av_sst_open[][BTA_AV_NUM_COLS] = {
290     /* Event                     Action 1               Action 2 Next state */
291     /* AP_OPEN_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
292     /* AP_CLOSE_EVT */ {BTA_AV_DO_CLOSE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
293     /* AP_START_EVT */ {BTA_AV_DO_START, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
294     /* AP_STOP_EVT */ {BTA_AV_STR_STOPPED, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
295     /* API_RECONFIG_EVT */ {BTA_AV_RECONFIG, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
296     /* API_PROTECT_REQ_EVT */ {BTA_AV_SECURITY_REQ, BTA_AV_SIGNORE,
297                                BTA_AV_OPEN_SST},
298     /* API_PROTECT_RSP_EVT */ {BTA_AV_SECURITY_RSP, BTA_AV_SIGNORE,
299                                BTA_AV_OPEN_SST},
300     /* API_RC_OPEN_EVT  */ {BTA_AV_SET_USE_RC, BTA_AV_OPEN_RC, BTA_AV_OPEN_SST},
301     /* SRC_DATA_READY_EVT */ {BTA_AV_DATA_PATH, BTA_AV_SIGNORE,
302                               BTA_AV_OPEN_SST},
303     /* CI_SETCONFIG_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
304     /* CI_SETCONFIG_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
305                                  BTA_AV_OPEN_SST},
306     /* SDP_DISC_OK_EVT */ {BTA_AV_FREE_SDB, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
307     /* SDP_DISC_FAIL_EVT */ {BTA_AV_FREE_SDB, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
308     /* STR_DISC_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
309     /* STR_DISC_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
310     /* STR_GETCAP_OK_EVT */ {BTA_AV_SAVE_CAPS, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
311     /* STR_GETCAP_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
312     /* STR_OPEN_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
313     /* STR_OPEN_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
314     /* STR_START_OK_EVT */ {BTA_AV_START_OK, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
315     /* STR_START_FAIL_EVT */ {BTA_AV_START_FAILED, BTA_AV_SIGNORE,
316                               BTA_AV_OPEN_SST},
317     /* STR_CLOSE_EVT */ {BTA_AV_STR_CLOSED, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
318     /* STR_CONFIG_IND_EVT */ {BTA_AV_SETCONFIG_REJ, BTA_AV_SIGNORE,
319                               BTA_AV_OPEN_SST},
320     /* STR_SECURITY_IND_EVT */ {BTA_AV_SECURITY_IND, BTA_AV_SIGNORE,
321                                 BTA_AV_OPEN_SST},
322     /* STR_SECURITY_CFM_EVT */ {BTA_AV_SECURITY_CFM, BTA_AV_SIGNORE,
323                                 BTA_AV_OPEN_SST},
324     /* STR_WRITE_CFM_EVT */ {BTA_AV_CLR_CONG, BTA_AV_DATA_PATH,
325                              BTA_AV_OPEN_SST},
326     /* STR_SUSPEND_CFM_EVT */ {BTA_AV_SUSPEND_CFM, BTA_AV_SIGNORE,
327                                BTA_AV_OPEN_SST},
328     /* STR_RECONFIG_CFM_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
329                                 BTA_AV_OPEN_SST},
330     /* AVRC_TIMER_EVT */ {BTA_AV_OPEN_RC, BTA_AV_CHK_2ND_START,
331                           BTA_AV_OPEN_SST},
332     /* AVDT_CONNECT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
333     /* AVDT_DISCONNECT_EVT */ {BTA_AV_STR_CLOSED, BTA_AV_SIGNORE,
334                                BTA_AV_INIT_SST},
335     /* ROLE_CHANGE_EVT*/ {BTA_AV_ROLE_RES, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
336     /* AVDT_DELAY_RPT_EVT */ {BTA_AV_DELAY_CO, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
337     /* ACP_CONNECT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
338     /* API_OFFLOAD_START_EVT */ {BTA_AV_OFFLOAD_REQ, BTA_AV_SIGNORE,
339                                  BTA_AV_OPEN_SST},
340     /* API_OFFLOAD_START_RSP_EVT */ {BTA_AV_OFFLOAD_RSP, BTA_AV_SIGNORE,
341                                      BTA_AV_OPEN_SST}};
342 
343 /* state table for reconfig state */
344 static const uint8_t bta_av_sst_rcfg[][BTA_AV_NUM_COLS] = {
345     /* Event                     Action 1               Action 2 Next state */
346     /* AP_OPEN_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
347     /* AP_CLOSE_EVT */ {BTA_AV_DISCONNECT_REQ, BTA_AV_SIGNORE,
348                         BTA_AV_CLOSING_SST},
349     /* AP_START_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
350     /* AP_STOP_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
351     /* API_RECONFIG_EVT */ {BTA_AV_RECONFIG, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
352     /* API_PROTECT_REQ_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
353     /* API_PROTECT_RSP_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
354     /* API_RC_OPEN_EVT  */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
355     /* SRC_DATA_READY_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
356     /* CI_SETCONFIG_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
357     /* CI_SETCONFIG_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
358                                  BTA_AV_RCFG_SST},
359     /* SDP_DISC_OK_EVT */ {BTA_AV_FREE_SDB, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
360     /* SDP_DISC_FAIL_EVT */ {BTA_AV_FREE_SDB, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
361     /* STR_DISC_OK_EVT */ {BTA_AV_DISC_RESULTS, BTA_AV_SIGNORE,
362                            BTA_AV_RCFG_SST},
363     /* STR_DISC_FAIL_EVT */ {BTA_AV_STR_CLOSED, BTA_AV_SIGNORE,
364                              BTA_AV_INIT_SST},
365     /* STR_GETCAP_OK_EVT */ {BTA_AV_GETCAP_RESULTS, BTA_AV_SIGNORE,
366                              BTA_AV_RCFG_SST},
367     /* STR_GETCAP_FAIL_EVT */ {BTA_AV_STR_CLOSED, BTA_AV_SIGNORE,
368                                BTA_AV_INIT_SST},
369     /* STR_OPEN_OK_EVT */ {BTA_AV_RCFG_STR_OK, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
370     /* STR_OPEN_FAIL_EVT */ {BTA_AV_RCFG_FAILED, BTA_AV_SIGNORE,
371                              BTA_AV_RCFG_SST},
372     /* STR_START_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
373     /* STR_START_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
374     /* STR_CLOSE_EVT */ {BTA_AV_RCFG_CONNECT, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
375     /* STR_CONFIG_IND_EVT */ {BTA_AV_SETCONFIG_REJ, BTA_AV_SIGNORE,
376                               BTA_AV_RCFG_SST},
377     /* STR_SECURITY_IND_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
378                                 BTA_AV_RCFG_SST},
379     /* STR_SECURITY_CFM_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
380                                 BTA_AV_RCFG_SST},
381     /* STR_WRITE_CFM_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
382     /* STR_SUSPEND_CFM_EVT */ {BTA_AV_SUSPEND_CFM, BTA_AV_SUSPEND_CONT,
383                                BTA_AV_RCFG_SST},
384     /* STR_RECONFIG_CFM_EVT */ {BTA_AV_RCFG_CFM, BTA_AV_SIGNORE,
385                                 BTA_AV_RCFG_SST},
386     /* AVRC_TIMER_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
387     /* AVDT_CONNECT_EVT */ {BTA_AV_RCFG_OPEN, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
388     /* AVDT_DISCONNECT_EVT */ {BTA_AV_RCFG_DISCNTD, BTA_AV_SIGNORE,
389                                BTA_AV_RCFG_SST},
390     /* ROLE_CHANGE_EVT*/ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
391     /* AVDT_DELAY_RPT_EVT */ {BTA_AV_DELAY_CO, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
392     /* ACP_CONNECT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
393     /* API_OFFLOAD_START_EVT */ {BTA_AV_OFFLOAD_REQ, BTA_AV_SIGNORE,
394                                  BTA_AV_RCFG_SST},
395     /* API_OFFLOAD_START_RSP_EVT */ {BTA_AV_OFFLOAD_RSP, BTA_AV_SIGNORE,
396                                      BTA_AV_RCFG_SST}};
397 
398 /* state table for closing state */
399 static const uint8_t bta_av_sst_closing[][BTA_AV_NUM_COLS] = {
400     /* Event                     Action 1               Action 2 Next state */
401     /* AP_OPEN_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
402     /* AP_CLOSE_EVT */ {BTA_AV_DISCONNECT_REQ, BTA_AV_SIGNORE,
403                         BTA_AV_CLOSING_SST},
404     /* AP_START_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
405     /* AP_STOP_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
406     /* API_RECONFIG_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
407     /* API_PROTECT_REQ_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
408                                BTA_AV_CLOSING_SST},
409     /* API_PROTECT_RSP_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
410                                BTA_AV_CLOSING_SST},
411     /* API_RC_OPEN_EVT  */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
412     /* SRC_DATA_READY_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
413                               BTA_AV_CLOSING_SST},
414     /* CI_SETCONFIG_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
415                                BTA_AV_CLOSING_SST},
416     /* CI_SETCONFIG_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
417                                  BTA_AV_CLOSING_SST},
418     /* SDP_DISC_OK_EVT */ {BTA_AV_SDP_FAILED, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
419     /* SDP_DISC_FAIL_EVT */ {BTA_AV_SDP_FAILED, BTA_AV_SIGNORE,
420                              BTA_AV_INIT_SST},
421     /* STR_DISC_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
422     /* STR_DISC_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
423                              BTA_AV_CLOSING_SST},
424     /* STR_GETCAP_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
425                              BTA_AV_CLOSING_SST},
426     /* STR_GETCAP_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
427                                BTA_AV_CLOSING_SST},
428     /* STR_OPEN_OK_EVT */ {BTA_AV_DO_CLOSE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
429     /* STR_OPEN_FAIL_EVT */ {BTA_AV_DISCONNECT_REQ, BTA_AV_SIGNORE,
430                              BTA_AV_CLOSING_SST},
431     /* STR_START_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
432     /* STR_START_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
433                               BTA_AV_CLOSING_SST},
434     /* STR_CLOSE_EVT */ {BTA_AV_DISCONNECT_REQ, BTA_AV_SIGNORE,
435                          BTA_AV_CLOSING_SST},
436     /* STR_CONFIG_IND_EVT */ {BTA_AV_SETCONFIG_REJ, BTA_AV_SIGNORE,
437                               BTA_AV_CLOSING_SST},
438     /* STR_SECURITY_IND_EVT */ {BTA_AV_SECURITY_REJ, BTA_AV_SIGNORE,
439                                 BTA_AV_CLOSING_SST},
440     /* STR_SECURITY_CFM_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
441                                 BTA_AV_CLOSING_SST},
442     /* STR_WRITE_CFM_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
443                              BTA_AV_CLOSING_SST},
444     /* STR_SUSPEND_CFM_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
445                                BTA_AV_CLOSING_SST},
446     /* STR_RECONFIG_CFM_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
447                                 BTA_AV_CLOSING_SST},
448     /* AVRC_TIMER_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
449     /* AVDT_CONNECT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
450     /* AVDT_DISCONNECT_EVT */ {BTA_AV_STR_CLOSED, BTA_AV_SIGNORE,
451                                BTA_AV_INIT_SST},
452     /* ROLE_CHANGE_EVT*/ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
453     /* AVDT_DELAY_RPT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE,
454                               BTA_AV_CLOSING_SST},
455     /* ACP_CONNECT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
456     /* API_OFFLOAD_START_EVT */ {BTA_AV_OFFLOAD_REQ, BTA_AV_SIGNORE,
457                                  BTA_AV_CLOSING_SST},
458     /* API_OFFLOAD_START_RSP_EVT */ {BTA_AV_OFFLOAD_RSP, BTA_AV_SIGNORE,
459                                      BTA_AV_CLOSING_SST}};
460 
461 /* type for state table */
462 typedef const uint8_t (*tBTA_AV_SST_TBL)[BTA_AV_NUM_COLS];
463 
464 /* state table */
465 static const tBTA_AV_SST_TBL bta_av_sst_tbl[] = {
466     bta_av_sst_init, bta_av_sst_incoming, bta_av_sst_opening,
467     bta_av_sst_open, bta_av_sst_rcfg,     bta_av_sst_closing};
468 
469 static const char* bta_av_sst_code(uint8_t state);
470 
471 /*******************************************************************************
472  *
473  * Function         bta_av_is_rcfg_sst
474  *
475  * Description      Check if stream state machine is in reconfig state.
476  *
477  *
478  * Returns          true if stream state machine is in reconfig state.
479  *
480  ******************************************************************************/
bta_av_is_rcfg_sst(tBTA_AV_SCB * p_scb)481 bool bta_av_is_rcfg_sst(tBTA_AV_SCB* p_scb) {
482   bool is_rcfg_sst = false;
483 
484   if (p_scb != NULL) {
485     if (p_scb->state == BTA_AV_RCFG_SST) is_rcfg_sst = true;
486   }
487 
488   return is_rcfg_sst;
489 }
490 
491 /*******************************************************************************
492  *
493  * Function         bta_av_ssm_execute
494  *
495  * Description      Stream state machine event handling function for AV
496  *
497  *
498  * Returns          void
499  *
500  ******************************************************************************/
bta_av_ssm_execute(tBTA_AV_SCB * p_scb,uint16_t event,tBTA_AV_DATA * p_data)501 void bta_av_ssm_execute(tBTA_AV_SCB* p_scb, uint16_t event,
502                         tBTA_AV_DATA* p_data) {
503   tBTA_AV_SST_TBL state_table;
504   uint8_t action;
505   int i, xx;
506 
507   if (p_scb == NULL) {
508     /* this stream is not registered */
509     APPL_TRACE_EVENT("AV channel not registered");
510     return;
511   }
512 
513   /* In case incoming connection is for VDP, we need to swap scb.        */
514   /* When ACP_CONNECT_EVT was received, we put first available scb to    */
515   /* to Incoming state. Later, when STR_CONFIG_IND_EVT is coming, we     */
516   /* know if it is A2DP or VDP.                                          */
517   if ((p_scb->state == BTA_AV_INIT_SST) &&
518       (event == BTA_AV_STR_CONFIG_IND_EVT)) {
519     for (xx = 0; xx < BTA_AV_NUM_STRS; xx++) {
520       if (bta_av_cb.p_scb[xx]) {
521         if (bta_av_cb.p_scb[xx]->state == BTA_AV_INCOMING_SST) {
522           bta_av_cb.p_scb[xx]->state = BTA_AV_INIT_SST;
523           bta_av_cb.p_scb[xx]->coll_mask = 0;
524           p_scb->state = BTA_AV_INCOMING_SST;
525           break;
526         }
527       }
528     }
529   }
530 
531   APPL_TRACE_VERBOSE("%s: AV Sevent(0x%x)=0x%x(%s) state=%d(%s)", __func__,
532                      p_scb->hndl, event, bta_av_evt_code(event), p_scb->state,
533                      bta_av_sst_code(p_scb->state));
534 
535   /* look up the state table for the current state */
536   state_table = bta_av_sst_tbl[p_scb->state];
537 
538   event -= BTA_AV_FIRST_SSM_EVT;
539 
540   /* set next state */
541   p_scb->state = state_table[event][BTA_AV_SNEXT_STATE];
542 
543   /* execute action functions */
544   for (i = 0; i < BTA_AV_SACTIONS; i++) {
545     action = state_table[event][i];
546     if (action != BTA_AV_SIGNORE) {
547       (*p_scb->p_act_tbl[action])(p_scb, p_data);
548     } else
549       break;
550   }
551 }
552 
553 /*******************************************************************************
554  *
555  * Function         bta_av_is_scb_opening
556  *
557  * Description      Returns true is scb is in opening state.
558  *
559  *
560  * Returns          true if scb is in opening state.
561  *
562  ******************************************************************************/
bta_av_is_scb_opening(tBTA_AV_SCB * p_scb)563 bool bta_av_is_scb_opening(tBTA_AV_SCB* p_scb) {
564   bool is_opening = false;
565 
566   if (p_scb) {
567     if (p_scb->state == BTA_AV_OPENING_SST) is_opening = true;
568   }
569 
570   return is_opening;
571 }
572 
573 /*******************************************************************************
574  *
575  * Function         bta_av_is_scb_incoming
576  *
577  * Description      Returns true is scb is in incoming state.
578  *
579  *
580  * Returns          true if scb is in incoming state.
581  *
582  ******************************************************************************/
bta_av_is_scb_incoming(tBTA_AV_SCB * p_scb)583 bool bta_av_is_scb_incoming(tBTA_AV_SCB* p_scb) {
584   bool is_incoming = false;
585 
586   if (p_scb) {
587     if (p_scb->state == BTA_AV_INCOMING_SST) is_incoming = true;
588   }
589 
590   return is_incoming;
591 }
592 
593 /*******************************************************************************
594  *
595  * Function         bta_av_set_scb_sst_init
596  *
597  * Description      Set SST state to INIT.
598  *                  Use this function to change SST outside of state machine.
599  *
600  * Returns          None
601  *
602  ******************************************************************************/
bta_av_set_scb_sst_init(tBTA_AV_SCB * p_scb)603 void bta_av_set_scb_sst_init(tBTA_AV_SCB* p_scb) {
604   if (p_scb) {
605     p_scb->state = BTA_AV_INIT_SST;
606   }
607 }
608 
609 /*******************************************************************************
610  *
611  * Function         bta_av_is_scb_init
612  *
613  * Description      Returns true is scb is in init state.
614  *
615  *
616  * Returns          true if scb is in incoming state.
617  *
618  ******************************************************************************/
bta_av_is_scb_init(tBTA_AV_SCB * p_scb)619 bool bta_av_is_scb_init(tBTA_AV_SCB* p_scb) {
620   bool is_init = false;
621 
622   if (p_scb) {
623     if (p_scb->state == BTA_AV_INIT_SST) is_init = true;
624   }
625 
626   return is_init;
627 }
628 
629 /*******************************************************************************
630  *
631  * Function         bta_av_set_scb_sst_incoming
632  *
633  * Description      Set SST state to incoming.
634  *                  Use this function to change SST outside of state machine.
635  *
636  * Returns          None
637  *
638  ******************************************************************************/
bta_av_set_scb_sst_incoming(tBTA_AV_SCB * p_scb)639 void bta_av_set_scb_sst_incoming(tBTA_AV_SCB* p_scb) {
640   if (p_scb) {
641     p_scb->state = BTA_AV_INCOMING_SST;
642   }
643 }
644 
645 /*****************************************************************************
646  *  Debug Functions
647  ****************************************************************************/
648 /*******************************************************************************
649  *
650  * Function         bta_av_sst_code
651  *
652  * Description
653  *
654  * Returns          char *
655  *
656  ******************************************************************************/
bta_av_sst_code(uint8_t state)657 static const char* bta_av_sst_code(uint8_t state) {
658   switch (state) {
659     case BTA_AV_INIT_SST:
660       return "INIT";
661     case BTA_AV_INCOMING_SST:
662       return "INCOMING";
663     case BTA_AV_OPENING_SST:
664       return "OPENING";
665     case BTA_AV_OPEN_SST:
666       return "OPEN";
667     case BTA_AV_RCFG_SST:
668       return "RCFG";
669     case BTA_AV_CLOSING_SST:
670       return "CLOSING";
671     default:
672       return "unknown";
673   }
674 }
675