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 #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     /* API_OPEN_EVT */ {BTA_AV_DO_DISC, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
111     /* API_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 */
121     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, 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 */
134     {BTA_AV_CONFIG_IND, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
135     /* STR_SECURITY_IND_EVT */
136     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
137     /* STR_SECURITY_CFM_EVT */
138     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, 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 */
142     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, 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 */
150     {BTA_AV_OFFLOAD_REQ, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
151     /* API_OFFLOAD_START_RSP_EVT */
152     {BTA_AV_OFFLOAD_RSP, BTA_AV_SIGNORE, 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     /* API_OPEN_EVT */ {BTA_AV_OPEN_AT_INC, BTA_AV_SIGNORE,
158                         BTA_AV_INCOMING_SST},
159     /* API_CLOSE_EVT */
160     {BTA_AV_CCO_CLOSE, BTA_AV_DISCONNECT_REQ, BTA_AV_CLOSING_SST},
161     /* AP_START_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
162     /* AP_STOP_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
163     /* API_RECONFIG_EVT */
164     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
165     /* API_PROTECT_REQ_EVT */
166     {BTA_AV_SECURITY_REQ, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
167     /* API_PROTECT_RSP_EVT */
168     {BTA_AV_SECURITY_RSP, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
169     /* API_RC_OPEN_EVT  */
170     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
171     /* SRC_DATA_READY_EVT */
172     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
173     /* CI_SETCONFIG_OK_EVT */
174     {BTA_AV_SETCONFIG_RSP, BTA_AV_ST_RC_TIMER, BTA_AV_INCOMING_SST},
175     /* CI_SETCONFIG_FAIL_EVT */
176     {BTA_AV_SETCONFIG_REJ, BTA_AV_CLEANUP, BTA_AV_INIT_SST},
177     /* SDP_DISC_OK_EVT */
178     {BTA_AV_FREE_SDB, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
179     /* SDP_DISC_FAIL_EVT */
180     {BTA_AV_FREE_SDB, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
181     /* STR_DISC_OK_EVT */
182     {BTA_AV_DISC_RES_AS_ACP, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
183     /* STR_DISC_FAIL_EVT */
184     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
185     /* STR_GETCAP_OK_EVT */
186     {BTA_AV_SAVE_CAPS, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
187     /* STR_GETCAP_FAIL_EVT */
188     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
189     /* STR_OPEN_OK_EVT */ {BTA_AV_STR_OPENED, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
190     /* STR_OPEN_FAIL_EVT */
191     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
192     /* STR_START_OK_EVT */
193     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
194     /* STR_START_FAIL_EVT */
195     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
196     /* STR_CLOSE_EVT */ {BTA_AV_CCO_CLOSE, BTA_AV_CLEANUP, BTA_AV_INIT_SST},
197     /* STR_CONFIG_IND_EVT */
198     {BTA_AV_CONFIG_IND, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
199     /* STR_SECURITY_IND_EVT */
200     {BTA_AV_SECURITY_IND, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
201     /* STR_SECURITY_CFM_EVT */
202     {BTA_AV_SECURITY_CFM, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
203     /* STR_WRITE_CFM_EVT */
204     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
205     /* STR_SUSPEND_CFM_EVT */
206     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
207     /* STR_RECONFIG_CFM_EVT */
208     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
209     /* AVRC_TIMER_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
210     /* AVDT_CONNECT_EVT */
211     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
212     /* AVDT_DISCONNECT_EVT */
213     {BTA_AV_CCO_CLOSE, BTA_AV_DISCONNECT_REQ, BTA_AV_CLOSING_SST},
214     /* ROLE_CHANGE_EVT*/ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
215     /* AVDT_DELAY_RPT_EVT */
216     {BTA_AV_DELAY_CO, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
217     /* ACP_CONNECT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
218     /* API_OFFLOAD_START_EVT */
219     {BTA_AV_OFFLOAD_REQ, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
220     /* API_OFFLOAD_START_RSP_EVT */
221     {BTA_AV_OFFLOAD_RSP, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST}};
222 
223 /* state table for opening state */
224 static const uint8_t bta_av_sst_opening[][BTA_AV_NUM_COLS] = {
225     /* Event                     Action 1               Action 2 Next state */
226     /* API_OPEN_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
227     /* API_CLOSE_EVT */ {BTA_AV_DO_CLOSE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
228     /* AP_START_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
229     /* AP_STOP_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
230     /* API_RECONFIG_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
231     /* API_PROTECT_REQ_EVT */
232     {BTA_AV_SECURITY_REQ, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
233     /* API_PROTECT_RSP_EVT */
234     {BTA_AV_SECURITY_RSP, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
235     /* API_RC_OPEN_EVT  */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
236     /* SRC_DATA_READY_EVT */
237     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
238     /* CI_SETCONFIG_OK_EVT */
239     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
240     /* CI_SETCONFIG_FAIL_EVT */
241     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
242     /* SDP_DISC_OK_EVT */
243     {BTA_AV_CONNECT_REQ, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
244     /* SDP_DISC_FAIL_EVT */
245     {BTA_AV_CONNECT_REQ, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
246     /* STR_DISC_OK_EVT */
247     {BTA_AV_DISC_RESULTS, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
248     /* STR_DISC_FAIL_EVT */
249     {BTA_AV_OPEN_FAILED, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
250     /* STR_GETCAP_OK_EVT */
251     {BTA_AV_GETCAP_RESULTS, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
252     /* STR_GETCAP_FAIL_EVT */
253     {BTA_AV_OPEN_FAILED, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
254     /* STR_OPEN_OK_EVT */
255     {BTA_AV_ST_RC_TIMER, BTA_AV_STR_OPENED, BTA_AV_OPEN_SST},
256     /* STR_OPEN_FAIL_EVT */
257     {BTA_AV_OPEN_FAILED, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
258     /* STR_START_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
259     /* STR_START_FAIL_EVT */
260     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
261     /* STR_CLOSE_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
262     /* STR_CONFIG_IND_EVT */
263     {BTA_AV_CONFIG_IND, BTA_AV_SIGNORE, BTA_AV_INCOMING_SST},
264     /* STR_SECURITY_IND_EVT */
265     {BTA_AV_SECURITY_IND, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
266     /* STR_SECURITY_CFM_EVT */
267     {BTA_AV_SECURITY_CFM, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
268     /* STR_WRITE_CFM_EVT */
269     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
270     /* STR_SUSPEND_CFM_EVT */
271     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
272     /* STR_RECONFIG_CFM_EVT */
273     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
274     /* AVRC_TIMER_EVT */
275     {BTA_AV_SWITCH_ROLE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
276     /* AVDT_CONNECT_EVT */
277     {BTA_AV_DISCOVER_REQ, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
278     /* AVDT_DISCONNECT_EVT */
279     {BTA_AV_CONN_FAILED, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
280     /* ROLE_CHANGE_EVT*/ {BTA_AV_ROLE_RES, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
281     /* AVDT_DELAY_RPT_EVT */
282     {BTA_AV_DELAY_CO, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
283     /* ACP_CONNECT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
284     /* API_OFFLOAD_START_EVT */
285     {BTA_AV_OFFLOAD_REQ, BTA_AV_SIGNORE, BTA_AV_OPENING_SST},
286     /* API_OFFLOAD_START_RSP_EVT */
287     {BTA_AV_OFFLOAD_RSP, BTA_AV_SIGNORE, BTA_AV_OPENING_SST}};
288 
289 /* state table for open state */
290 static const uint8_t bta_av_sst_open[][BTA_AV_NUM_COLS] = {
291     /* Event                     Action 1               Action 2 Next state */
292     /* API_OPEN_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
293     /* API_CLOSE_EVT */ {BTA_AV_DO_CLOSE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
294     /* AP_START_EVT */ {BTA_AV_DO_START, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
295     /* AP_STOP_EVT */ {BTA_AV_STR_STOPPED, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
296     /* API_RECONFIG_EVT */ {BTA_AV_RECONFIG, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
297     /* API_PROTECT_REQ_EVT */
298     {BTA_AV_SECURITY_REQ, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
299     /* API_PROTECT_RSP_EVT */
300     {BTA_AV_SECURITY_RSP, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
301     /* API_RC_OPEN_EVT  */ {BTA_AV_SET_USE_RC, BTA_AV_OPEN_RC, BTA_AV_OPEN_SST},
302     /* SRC_DATA_READY_EVT */
303     {BTA_AV_DATA_PATH, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
304     /* CI_SETCONFIG_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
305     /* CI_SETCONFIG_FAIL_EVT */
306     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
307     /* SDP_DISC_OK_EVT */ {BTA_AV_FREE_SDB, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
308     /* SDP_DISC_FAIL_EVT */ {BTA_AV_FREE_SDB, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
309     /* STR_DISC_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
310     /* STR_DISC_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
311     /* STR_GETCAP_OK_EVT */ {BTA_AV_SAVE_CAPS, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
312     /* STR_GETCAP_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
313     /* STR_OPEN_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
314     /* STR_OPEN_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
315     /* STR_START_OK_EVT */ {BTA_AV_START_OK, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
316     /* STR_START_FAIL_EVT */
317     {BTA_AV_START_FAILED, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
318     /* STR_CLOSE_EVT */ {BTA_AV_STR_CLOSED, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
319     /* STR_CONFIG_IND_EVT */
320     {BTA_AV_SETCONFIG_REJ, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
321     /* STR_SECURITY_IND_EVT */
322     {BTA_AV_SECURITY_IND, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
323     /* STR_SECURITY_CFM_EVT */
324     {BTA_AV_SECURITY_CFM, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
325     /* STR_WRITE_CFM_EVT */
326     {BTA_AV_CLR_CONG, BTA_AV_DATA_PATH, BTA_AV_OPEN_SST},
327     /* STR_SUSPEND_CFM_EVT */
328     {BTA_AV_SUSPEND_CFM, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
329     /* STR_RECONFIG_CFM_EVT */
330     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
331     /* AVRC_TIMER_EVT */
332     {BTA_AV_OPEN_RC, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
333     /* AVDT_CONNECT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
334     /* AVDT_DISCONNECT_EVT */
335     {BTA_AV_STR_CLOSED, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
336     /* ROLE_CHANGE_EVT*/ {BTA_AV_ROLE_RES, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
337     /* AVDT_DELAY_RPT_EVT */ {BTA_AV_DELAY_CO, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
338     /* ACP_CONNECT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
339     /* API_OFFLOAD_START_EVT */
340     {BTA_AV_OFFLOAD_REQ, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
341     /* API_OFFLOAD_START_RSP_EVT */
342     {BTA_AV_OFFLOAD_RSP, BTA_AV_SIGNORE, BTA_AV_OPEN_SST}};
343 
344 /* state table for reconfig state */
345 static const uint8_t bta_av_sst_rcfg[][BTA_AV_NUM_COLS] = {
346     /* Event                     Action 1               Action 2 Next state */
347     /* API_OPEN_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
348     /* API_CLOSE_EVT */
349     {BTA_AV_DISCONNECT_REQ, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
350     /* AP_START_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
351     /* AP_STOP_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
352     /* API_RECONFIG_EVT */ {BTA_AV_RECONFIG, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
353     /* API_PROTECT_REQ_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
354     /* API_PROTECT_RSP_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
355     /* API_RC_OPEN_EVT  */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
356     /* SRC_DATA_READY_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
357     /* CI_SETCONFIG_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
358     /* CI_SETCONFIG_FAIL_EVT */
359     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
360     /* SDP_DISC_OK_EVT */ {BTA_AV_FREE_SDB, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
361     /* SDP_DISC_FAIL_EVT */ {BTA_AV_FREE_SDB, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
362     /* STR_DISC_OK_EVT */
363     {BTA_AV_DISC_RESULTS, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
364     /* STR_DISC_FAIL_EVT */
365     {BTA_AV_STR_CLOSED, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
366     /* STR_GETCAP_OK_EVT */
367     {BTA_AV_GETCAP_RESULTS, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
368     /* STR_GETCAP_FAIL_EVT */
369     {BTA_AV_STR_CLOSED, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
370     /* STR_OPEN_OK_EVT */ {BTA_AV_RCFG_STR_OK, BTA_AV_SIGNORE, BTA_AV_OPEN_SST},
371     /* STR_OPEN_FAIL_EVT */
372     {BTA_AV_RCFG_FAILED, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
373     /* STR_START_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
374     /* STR_START_FAIL_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
375     /* STR_CLOSE_EVT */ {BTA_AV_RCFG_CONNECT, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
376     /* STR_CONFIG_IND_EVT */
377     {BTA_AV_SETCONFIG_REJ, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
378     /* STR_SECURITY_IND_EVT */
379     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
380     /* STR_SECURITY_CFM_EVT */
381     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
382     /* STR_WRITE_CFM_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
383     /* STR_SUSPEND_CFM_EVT */
384     {BTA_AV_SUSPEND_CFM, BTA_AV_SUSPEND_CONT, BTA_AV_RCFG_SST},
385     /* STR_RECONFIG_CFM_EVT */
386     {BTA_AV_RCFG_CFM, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
387     /* AVRC_TIMER_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
388     /* AVDT_CONNECT_EVT */ {BTA_AV_RCFG_OPEN, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
389     /* AVDT_DISCONNECT_EVT */
390     {BTA_AV_RCFG_DISCNTD, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
391     /* ROLE_CHANGE_EVT*/ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
392     /* AVDT_DELAY_RPT_EVT */ {BTA_AV_DELAY_CO, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
393     /* ACP_CONNECT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
394     /* API_OFFLOAD_START_EVT */
395     {BTA_AV_OFFLOAD_REQ, BTA_AV_SIGNORE, BTA_AV_RCFG_SST},
396     /* API_OFFLOAD_START_RSP_EVT */
397     {BTA_AV_OFFLOAD_RSP, BTA_AV_SIGNORE, BTA_AV_RCFG_SST}};
398 
399 /* state table for closing state */
400 static const uint8_t bta_av_sst_closing[][BTA_AV_NUM_COLS] = {
401     /* Event                     Action 1               Action 2 Next state */
402     /* API_OPEN_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
403     /* API_CLOSE_EVT */
404     {BTA_AV_DISCONNECT_REQ, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
405     /* AP_START_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
406     /* AP_STOP_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
407     /* API_RECONFIG_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
408     /* API_PROTECT_REQ_EVT */
409     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
410     /* API_PROTECT_RSP_EVT */
411     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
412     /* API_RC_OPEN_EVT  */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
413     /* SRC_DATA_READY_EVT */
414     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
415     /* CI_SETCONFIG_OK_EVT */
416     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
417     /* CI_SETCONFIG_FAIL_EVT */
418     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
419     /* SDP_DISC_OK_EVT */ {BTA_AV_SDP_FAILED, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
420     /* SDP_DISC_FAIL_EVT */
421     {BTA_AV_SDP_FAILED, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
422     /* STR_DISC_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
423     /* STR_DISC_FAIL_EVT */
424     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
425     /* STR_GETCAP_OK_EVT */
426     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
427     /* STR_GETCAP_FAIL_EVT */
428     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
429     /* STR_OPEN_OK_EVT */ {BTA_AV_DO_CLOSE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
430     /* STR_OPEN_FAIL_EVT */
431     {BTA_AV_DISCONNECT_REQ, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
432     /* STR_START_OK_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
433     /* STR_START_FAIL_EVT */
434     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
435     /* STR_CLOSE_EVT */
436     {BTA_AV_DISCONNECT_REQ, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
437     /* STR_CONFIG_IND_EVT */
438     {BTA_AV_SETCONFIG_REJ, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
439     /* STR_SECURITY_IND_EVT */
440     {BTA_AV_SECURITY_REJ, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
441     /* STR_SECURITY_CFM_EVT */
442     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
443     /* STR_WRITE_CFM_EVT */
444     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
445     /* STR_SUSPEND_CFM_EVT */
446     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
447     /* STR_RECONFIG_CFM_EVT */
448     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
449     /* AVRC_TIMER_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
450     /* AVDT_CONNECT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
451     /* AVDT_DISCONNECT_EVT */
452     {BTA_AV_STR_CLOSED, BTA_AV_SIGNORE, BTA_AV_INIT_SST},
453     /* ROLE_CHANGE_EVT*/ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
454     /* AVDT_DELAY_RPT_EVT */
455     {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
456     /* ACP_CONNECT_EVT */ {BTA_AV_SIGNORE, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
457     /* API_OFFLOAD_START_EVT */
458     {BTA_AV_OFFLOAD_REQ, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST},
459     /* API_OFFLOAD_START_RSP_EVT */
460     {BTA_AV_OFFLOAD_RSP, BTA_AV_SIGNORE, BTA_AV_CLOSING_SST}};
461 
462 /* type for state table */
463 typedef const uint8_t (*tBTA_AV_SST_TBL)[BTA_AV_NUM_COLS];
464 
465 /* state table */
466 static const tBTA_AV_SST_TBL bta_av_sst_tbl[] = {
467     bta_av_sst_init, bta_av_sst_incoming, bta_av_sst_opening,
468     bta_av_sst_open, bta_av_sst_rcfg,     bta_av_sst_closing};
469 
470 
471 /*******************************************************************************
472  *
473  * Function         bta_av_ssm_execute
474  *
475  * Description      Stream state machine event handling function for AV
476  *
477  *
478  * Returns          void
479  *
480  ******************************************************************************/
bta_av_ssm_execute(tBTA_AV_SCB * p_scb,uint16_t event,tBTA_AV_DATA * p_data)481 void bta_av_ssm_execute(tBTA_AV_SCB* p_scb, uint16_t event,
482                         tBTA_AV_DATA* p_data) {
483   if (p_scb == NULL) {
484     /* this stream is not registered */
485     APPL_TRACE_EVENT("%s: AV channel not registered", __func__);
486     return;
487   }
488 
489   /* look up the state table for the current state */
490   tBTA_AV_SST_TBL state_table = bta_av_sst_tbl[p_scb->state];
491 
492   /* set next state */
493   auto new_state =
494       state_table[event - BTA_AV_FIRST_SSM_EVT][BTA_AV_SNEXT_STATE];
495   if (p_scb->state != new_state) {
496     APPL_TRACE_WARNING(
497         "%s: peer %s AV event(0x%x)=0x%x(%s) state=%d(%s) -> %d(%s) p_scb=%p",
498         __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl, event,
499         bta_av_evt_code(event), p_scb->state, bta_av_sst_code(p_scb->state),
500         new_state, bta_av_sst_code(new_state), p_scb);
501   } else {
502     APPL_TRACE_VERBOSE(
503         "%s: peer %s AV event(0x%x)=0x%x(%s) state=%d(%s) p_scb=%p", __func__,
504         p_scb->PeerAddress().ToString().c_str(), p_scb->hndl, event,
505         bta_av_evt_code(event), p_scb->state, bta_av_sst_code(p_scb->state),
506         p_scb);
507   }
508   event -= BTA_AV_FIRST_SSM_EVT;
509   p_scb->state = state_table[event][BTA_AV_SNEXT_STATE];
510 
511   APPL_TRACE_VERBOSE("%s: peer %s AV next state=%d(%s) p_scb=%p(0x%x)",
512                      __func__, p_scb->PeerAddress().ToString().c_str(),
513                      p_scb->state, bta_av_sst_code(p_scb->state), p_scb,
514                      p_scb->hndl);
515 
516   /* execute action functions */
517   for (int i = 0; i < BTA_AV_SACTIONS; i++) {
518     uint8_t action = state_table[event][i];
519     if (action != BTA_AV_SIGNORE) {
520       (*p_scb->p_act_tbl[action])(p_scb, p_data);
521     } else
522       break;
523   }
524 }
525 
526 /*******************************************************************************
527  *
528  * Function         bta_av_is_scb_opening
529  *
530  * Description      Returns true is scb is in opening state.
531  *
532  *
533  * Returns          true if scb is in opening state.
534  *
535  ******************************************************************************/
bta_av_is_scb_opening(tBTA_AV_SCB * p_scb)536 bool bta_av_is_scb_opening(tBTA_AV_SCB* p_scb) {
537   bool is_opening = false;
538 
539   if (p_scb) {
540     if (p_scb->state == BTA_AV_OPENING_SST) is_opening = true;
541   }
542 
543   return is_opening;
544 }
545 
546 /*******************************************************************************
547  *
548  * Function         bta_av_is_scb_incoming
549  *
550  * Description      Returns true is scb is in incoming state.
551  *
552  *
553  * Returns          true if scb is in incoming state.
554  *
555  ******************************************************************************/
bta_av_is_scb_incoming(tBTA_AV_SCB * p_scb)556 bool bta_av_is_scb_incoming(tBTA_AV_SCB* p_scb) {
557   bool is_incoming = false;
558 
559   if (p_scb) {
560     if (p_scb->state == BTA_AV_INCOMING_SST) is_incoming = true;
561   }
562 
563   return is_incoming;
564 }
565 
566 /*******************************************************************************
567  *
568  * Function         bta_av_set_scb_sst_init
569  *
570  * Description      Set SST state to INIT.
571  *                  Use this function to change SST outside of state machine.
572  *
573  * Returns          None
574  *
575  ******************************************************************************/
bta_av_set_scb_sst_init(tBTA_AV_SCB * p_scb)576 void bta_av_set_scb_sst_init(tBTA_AV_SCB* p_scb) {
577   if (p_scb == nullptr) {
578     return;
579   }
580 
581   uint8_t next_state = BTA_AV_INIT_SST;
582 
583   APPL_TRACE_VERBOSE(
584       "%s: peer %s AV (hndl=0x%x) state=%d(%s) next state=%d(%s) p_scb=%p",
585       __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
586       p_scb->state, bta_av_sst_code(p_scb->state), next_state,
587       bta_av_sst_code(next_state), p_scb);
588 
589   p_scb->state = next_state;
590 }
591 
592 /*******************************************************************************
593  *
594  * Function         bta_av_is_scb_init
595  *
596  * Description      Returns true is scb is in init state.
597  *
598  *
599  * Returns          true if scb is in incoming state.
600  *
601  ******************************************************************************/
bta_av_is_scb_init(tBTA_AV_SCB * p_scb)602 bool bta_av_is_scb_init(tBTA_AV_SCB* p_scb) {
603   bool is_init = false;
604 
605   if (p_scb) {
606     if (p_scb->state == BTA_AV_INIT_SST) is_init = true;
607   }
608 
609   return is_init;
610 }
611 
612 /*******************************************************************************
613  *
614  * Function         bta_av_set_scb_sst_incoming
615  *
616  * Description      Set SST state to incoming.
617  *                  Use this function to change SST outside of state machine.
618  *
619  * Returns          None
620  *
621  ******************************************************************************/
bta_av_set_scb_sst_incoming(tBTA_AV_SCB * p_scb)622 void bta_av_set_scb_sst_incoming(tBTA_AV_SCB* p_scb) {
623   if (p_scb) {
624     p_scb->state = BTA_AV_INCOMING_SST;
625   }
626 }
627 
628 /*****************************************************************************
629  *  Debug Functions
630  ****************************************************************************/
631 /*******************************************************************************
632  *
633  * Function         bta_av_sst_code
634  *
635  * Description
636  *
637  * Returns          char *
638  *
639  ******************************************************************************/
bta_av_sst_code(uint8_t state)640 const char* bta_av_sst_code(uint8_t state) {
641   switch (state) {
642     case BTA_AV_INIT_SST:
643       return "INIT";
644     case BTA_AV_INCOMING_SST:
645       return "INCOMING";
646     case BTA_AV_OPENING_SST:
647       return "OPENING";
648     case BTA_AV_OPEN_SST:
649       return "OPEN";
650     case BTA_AV_RCFG_SST:
651       return "RCFG";
652     case BTA_AV_CLOSING_SST:
653       return "CLOSING";
654     default:
655       return "unknown";
656   }
657 }
658