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_CHK_2ND_START, 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 APPL_TRACE_VERBOSE(
490 "%s: peer %s AV event(0x%x)=0x%x(%s) state=%d(%s) p_scb=%p", __func__,
491 p_scb->PeerAddress().ToString().c_str(), p_scb->hndl, event,
492 bta_av_evt_code(event), p_scb->state, bta_av_sst_code(p_scb->state),
493 p_scb);
494
495 /* look up the state table for the current state */
496 tBTA_AV_SST_TBL state_table = bta_av_sst_tbl[p_scb->state];
497
498 /* set next state */
499 event -= BTA_AV_FIRST_SSM_EVT;
500 p_scb->state = state_table[event][BTA_AV_SNEXT_STATE];
501
502 APPL_TRACE_VERBOSE("%s: peer %s AV next state=%d(%s) p_scb=%p", __func__,
503 p_scb->PeerAddress().ToString().c_str(), p_scb->state,
504 bta_av_sst_code(p_scb->state), p_scb);
505
506 /* execute action functions */
507 for (int i = 0; i < BTA_AV_SACTIONS; i++) {
508 uint8_t action = state_table[event][i];
509 if (action != BTA_AV_SIGNORE) {
510 (*p_scb->p_act_tbl[action])(p_scb, p_data);
511 } else
512 break;
513 }
514 }
515
516 /*******************************************************************************
517 *
518 * Function bta_av_is_scb_opening
519 *
520 * Description Returns true is scb is in opening state.
521 *
522 *
523 * Returns true if scb is in opening state.
524 *
525 ******************************************************************************/
bta_av_is_scb_opening(tBTA_AV_SCB * p_scb)526 bool bta_av_is_scb_opening(tBTA_AV_SCB* p_scb) {
527 bool is_opening = false;
528
529 if (p_scb) {
530 if (p_scb->state == BTA_AV_OPENING_SST) is_opening = true;
531 }
532
533 return is_opening;
534 }
535
536 /*******************************************************************************
537 *
538 * Function bta_av_is_scb_incoming
539 *
540 * Description Returns true is scb is in incoming state.
541 *
542 *
543 * Returns true if scb is in incoming state.
544 *
545 ******************************************************************************/
bta_av_is_scb_incoming(tBTA_AV_SCB * p_scb)546 bool bta_av_is_scb_incoming(tBTA_AV_SCB* p_scb) {
547 bool is_incoming = false;
548
549 if (p_scb) {
550 if (p_scb->state == BTA_AV_INCOMING_SST) is_incoming = true;
551 }
552
553 return is_incoming;
554 }
555
556 /*******************************************************************************
557 *
558 * Function bta_av_set_scb_sst_init
559 *
560 * Description Set SST state to INIT.
561 * Use this function to change SST outside of state machine.
562 *
563 * Returns None
564 *
565 ******************************************************************************/
bta_av_set_scb_sst_init(tBTA_AV_SCB * p_scb)566 void bta_av_set_scb_sst_init(tBTA_AV_SCB* p_scb) {
567 if (p_scb == nullptr) {
568 return;
569 }
570
571 uint8_t next_state = BTA_AV_INIT_SST;
572
573 APPL_TRACE_VERBOSE(
574 "%s: peer %s AV (hndl=0x%x) state=%d(%s) next state=%d(%s) p_scb=%p",
575 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
576 p_scb->state, bta_av_sst_code(p_scb->state), next_state,
577 bta_av_sst_code(next_state), p_scb);
578
579 p_scb->state = next_state;
580 }
581
582 /*******************************************************************************
583 *
584 * Function bta_av_is_scb_init
585 *
586 * Description Returns true is scb is in init state.
587 *
588 *
589 * Returns true if scb is in incoming state.
590 *
591 ******************************************************************************/
bta_av_is_scb_init(tBTA_AV_SCB * p_scb)592 bool bta_av_is_scb_init(tBTA_AV_SCB* p_scb) {
593 bool is_init = false;
594
595 if (p_scb) {
596 if (p_scb->state == BTA_AV_INIT_SST) is_init = true;
597 }
598
599 return is_init;
600 }
601
602 /*******************************************************************************
603 *
604 * Function bta_av_set_scb_sst_incoming
605 *
606 * Description Set SST state to incoming.
607 * Use this function to change SST outside of state machine.
608 *
609 * Returns None
610 *
611 ******************************************************************************/
bta_av_set_scb_sst_incoming(tBTA_AV_SCB * p_scb)612 void bta_av_set_scb_sst_incoming(tBTA_AV_SCB* p_scb) {
613 if (p_scb) {
614 p_scb->state = BTA_AV_INCOMING_SST;
615 }
616 }
617
618 /*****************************************************************************
619 * Debug Functions
620 ****************************************************************************/
621 /*******************************************************************************
622 *
623 * Function bta_av_sst_code
624 *
625 * Description
626 *
627 * Returns char *
628 *
629 ******************************************************************************/
bta_av_sst_code(uint8_t state)630 const char* bta_av_sst_code(uint8_t state) {
631 switch (state) {
632 case BTA_AV_INIT_SST:
633 return "INIT";
634 case BTA_AV_INCOMING_SST:
635 return "INCOMING";
636 case BTA_AV_OPENING_SST:
637 return "OPENING";
638 case BTA_AV_OPEN_SST:
639 return "OPEN";
640 case BTA_AV_RCFG_SST:
641 return "RCFG";
642 case BTA_AV_CLOSING_SST:
643 return "CLOSING";
644 default:
645 return "unknown";
646 }
647 }
648