1 /******************************************************************************
2  *
3  *  Copyright 2002-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 module contains the stream control block and functions which
22  *  operate on the stream control block.
23  *
24  ******************************************************************************/
25 
26 #define LOG_TAG "bluetooth-a2dp"
27 
28 #include <bluetooth/log.h>
29 #include <string.h>
30 
31 #include "avdt_api.h"
32 #include "avdt_int.h"
33 #include "avdtc_api.h"
34 #include "internal_include/bt_target.h"
35 #include "osi/include/osi.h"
36 
37 using namespace bluetooth;
38 
39 /*****************************************************************************
40  * state machine constants and types
41  ****************************************************************************/
42 #if (AVDT_DEBUG == TRUE)
43 
44 /* verbose state strings for trace */
45 const char* const avdt_scb_st_str[] = {"SCB_IDLE_ST",    "SCB_CONF_ST",
46                                        "SCB_OPENING_ST", "SCB_OPEN_ST",
47                                        "SCB_STREAM_ST",  "SCB_CLOSING_ST"};
48 
49 /* verbose event strings for trace */
50 const char* const avdt_scb_evt_str[] = {
51     "API_REMOVE_EVT",        "API_WRITE_REQ_EVT",     "API_GETCONFIG_REQ_EVT",
52     "API_DELAY_RPT_REQ",     "API_SETCONFIG_REQ_EVT", "API_OPEN_REQ_EVT",
53     "API_CLOSE_REQ_EVT",     "API_RECONFIG_REQ_EVT",  "API_SECURITY_REQ_EVT",
54     "API_ABORT_REQ_EVT",     "API_GETCONFIG_RSP_EVT", "API_SETCONFIG_RSP_EVT",
55     "API_SETCONFIG_REJ_EVT", "API_OPEN_RSP_EVT",      "API_CLOSE_RSP_EVT",
56     "API_RECONFIG_RSP_EVT",  "API_SECURITY_RSP_EVT",  "API_ABORT_RSP_EVT",
57     "MSG_SETCONFIG_CMD_EVT", "MSG_GETCONFIG_CMD_EVT", "MSG_OPEN_CMD_EVT",
58     "MSG_START_CMD_EVT",     "MSG_SUSPEND_CMD_EVT",   "MSG_CLOSE_CMD_EVT",
59     "MSG_ABORT_CMD_EVT",     "MSG_RECONFIG_CMD_EVT",  "MSG_SECURITY_CMD_EVT",
60     "MSG_DELAY_RPT_CMD_EVT", "MSG_DELAY_RPT_RSP_EVT", "MSG_SETCONFIG_RSP_EVT",
61     "MSG_GETCONFIG_RSP_EVT", "MSG_OPEN_RSP_EVT",      "MSG_START_RSP_EVT",
62     "MSG_SUSPEND_RSP_EVT",   "MSG_CLOSE_RSP_EVT",     "MSG_ABORT_RSP_EVT",
63     "MSG_RECONFIG_RSP_EVT",  "MSG_SECURITY_RSP_EVT",  "MSG_SETCONFIG_REJ_EVT",
64     "MSG_OPEN_REJ_EVT",      "MSG_START_REJ_EVT",     "MSG_SUSPEND_REJ_EVT",
65     "TC_TOUT_EVT",           "TC_OPEN_EVT",           "TC_CLOSE_EVT",
66     "TC_CONG_EVT",           "TC_DATA_EVT",           "CC_CLOSE_EVT"};
67 
68 #endif
69 
70 /* action function list */
71 const tAVDT_SCB_ACTION avdt_scb_action[] = {avdt_scb_hdl_abort_cmd,
72                                             avdt_scb_hdl_abort_rsp,
73                                             avdt_scb_hdl_close_cmd,
74                                             avdt_scb_hdl_close_rsp,
75                                             avdt_scb_hdl_getconfig_cmd,
76                                             avdt_scb_hdl_getconfig_rsp,
77                                             avdt_scb_hdl_open_cmd,
78                                             avdt_scb_hdl_open_rej,
79                                             avdt_scb_hdl_open_rsp,
80                                             avdt_scb_hdl_pkt,
81                                             avdt_scb_drop_pkt,
82                                             avdt_scb_hdl_reconfig_cmd,
83                                             avdt_scb_hdl_reconfig_rsp,
84                                             avdt_scb_hdl_security_cmd,
85                                             avdt_scb_hdl_security_rsp,
86                                             avdt_scb_hdl_setconfig_cmd,
87                                             avdt_scb_hdl_setconfig_rej,
88                                             avdt_scb_hdl_setconfig_rsp,
89                                             avdt_scb_hdl_start_cmd,
90                                             avdt_scb_hdl_start_rsp,
91                                             avdt_scb_hdl_suspend_cmd,
92                                             avdt_scb_hdl_suspend_rsp,
93                                             avdt_scb_hdl_tc_close,
94                                             avdt_scb_hdl_tc_close_sto,
95                                             avdt_scb_hdl_tc_open,
96                                             avdt_scb_hdl_tc_open_sto,
97                                             avdt_scb_snd_delay_rpt_req,
98                                             avdt_scb_hdl_delay_rpt_cmd,
99                                             avdt_scb_hdl_delay_rpt_rsp,
100                                             avdt_scb_hdl_write_req,
101                                             avdt_scb_snd_abort_req,
102                                             avdt_scb_snd_abort_rsp,
103                                             avdt_scb_snd_close_req,
104                                             avdt_scb_snd_stream_close,
105                                             avdt_scb_snd_close_rsp,
106                                             avdt_scb_snd_getconfig_req,
107                                             avdt_scb_snd_getconfig_rsp,
108                                             avdt_scb_snd_open_req,
109                                             avdt_scb_snd_open_rsp,
110                                             avdt_scb_snd_reconfig_req,
111                                             avdt_scb_snd_reconfig_rsp,
112                                             avdt_scb_snd_security_req,
113                                             avdt_scb_snd_security_rsp,
114                                             avdt_scb_snd_setconfig_req,
115                                             avdt_scb_snd_setconfig_rej,
116                                             avdt_scb_snd_setconfig_rsp,
117                                             avdt_scb_snd_snk_delay_rpt_req,
118                                             avdt_scb_snd_tc_close,
119                                             avdt_scb_cb_err,
120                                             avdt_scb_cong_state,
121                                             avdt_scb_rej_state,
122                                             avdt_scb_rej_in_use,
123                                             avdt_scb_rej_not_in_use,
124                                             avdt_scb_set_remove,
125                                             avdt_scb_free_pkt,
126                                             avdt_scb_clr_pkt,
127                                             avdt_scb_chk_snd_pkt,
128                                             avdt_scb_transport_channel_timer,
129                                             avdt_scb_clr_vars,
130                                             avdt_scb_dealloc};
131 
132 /* state table information */
133 #define AVDT_SCB_ACTIONS 2    /* number of actions */
134 #define AVDT_SCB_NEXT_STATE 2 /* position of next state */
135 #define AVDT_SCB_NUM_COLS 3   /* number of columns in state tables */
136 
137 /* state table for idle state */
138 const uint8_t avdt_scb_st_idle[][AVDT_SCB_NUM_COLS] = {
139     /* Event */
140     /* Action 1                       Action 2                    Next state */
141     /* API_REMOVE_EVT */
142     {AVDT_SCB_DEALLOC, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
143     /* API_WRITE_REQ_EVT */
144     {AVDT_SCB_FREE_PKT, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
145     /* API_GETCONFIG_REQ_EVT */
146     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
147     /* API_DELAY_RPT_REQ_EVT */
148     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
149     /* API_SETCONFIG_REQ_EVT */
150     {AVDT_SCB_SND_SETCONFIG_REQ, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
151     /* API_OPEN_REQ_EVT */
152     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
153     /* API_CLOSE_REQ_EVT */
154     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
155     /* API_RECONFIG_REQ_EVT */
156     {AVDT_SCB_CB_ERR, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
157     /* API_SECURITY_REQ_EVT */
158     {AVDT_SCB_CB_ERR, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
159     /* API_ABORT_REQ_EVT */
160     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
161     /* API_GETCONFIG_RSP_EVT */
162     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
163     /* API_SETCONFIG_RSP_EVT */
164     {AVDT_SCB_SND_SETCONFIG_RSP, AVDT_SCB_SND_SNK_DELAY_RPT_REQ, AVDT_SCB_CONF_ST},
165     /* API_SETCONFIG_REJ_EVT */
166     {AVDT_SCB_SND_SETCONFIG_REJ, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
167     /* API_OPEN_RSP_EVT */
168     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
169     /* API_CLOSE_RSP_EVT */
170     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
171     /* API_RECONFIG_RSP_EVT */
172     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
173     /* API_SECURITY_RSP_EVT */
174     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
175     /* API_ABORT_RSP_EVT */
176     {AVDT_SCB_SND_ABORT_RSP, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
177     /* MSG_SETCONFIG_CMD_EVT */
178     {AVDT_SCB_HDL_SETCONFIG_CMD, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
179     /* MSG_GETCONFIG_CMD_EVT */
180     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
181     /* MSG_OPEN_CMD_EVT */
182     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
183     /* MSG_START_CMD_EVT */
184     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
185     /* MSG_SUSPEND_CMD_EVT */
186     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
187     /* MSG_CLOSE_CMD_EVT */
188     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
189     /* MSG_ABORT_CMD_EVT */
190     {AVDT_SCB_HDL_ABORT_CMD, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
191     /* MSG_RECONFIG_CMD_EVT */
192     {AVDT_SCB_REJ_NOT_IN_USE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
193     /* MSG_SECURITY_CMD_EVT */
194     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
195     /* MSG_DELAY_RPT_CMD_EVT */
196     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
197     /* MSG_DELAY_RPT_RSP_EVT */
198     {AVDT_SCB_HDL_DELAY_RPT_RSP, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
199     /* MSG_SETCONFIG_RSP_EVT */
200     {AVDT_SCB_HDL_SETCONFIG_RSP, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
201     /* MSG_GETCONFIG_RSP_EVT */
202     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
203     /* MSG_OPEN_RSP_EVT */
204     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
205     /* MSG_START_RSP_EVT */
206     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
207     /* MSG_SUSPEND_RSP_EVT */
208     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
209     /* MSG_CLOSE_RSP_EVT */
210     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
211     /* MSG_ABORT_RSP_EVT */
212     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
213     /* MSG_RECONFIG_RSP_EVT */
214     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
215     /* MSG_SECURITY_RSP_EVT */
216     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
217     /* MSG_SETCONFIG_REJ_EVT */
218     {AVDT_SCB_HDL_SETCONFIG_REJ, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
219     /* MSG_OPEN_REJ_EVT */
220     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
221     /* MSG_START_REJ_EVT */
222     {AVDT_SCB_HDL_START_RSP, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
223     /* MSG_SUSPEND_REJ_EVT */
224     {AVDT_SCB_HDL_SUSPEND_RSP, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
225     /* TC_TOUT_EVT */
226     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
227     /* TC_OPEN_EVT */
228     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
229     /* TC_CLOSE_EVT */
230     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
231     /* TC_CONG_EVT */
232     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
233     /* TC_DATA_EVT */
234     {AVDT_SCB_DROP_PKT, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
235     /* CC_CLOSE_EVT */
236     {AVDT_SCB_CLR_VARS, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST}};
237 
238 /* state table for configured state */
239 const uint8_t avdt_scb_st_conf[][AVDT_SCB_NUM_COLS] = {
240     /* Event */
241     /* Action 1                       Action 2                    Next state */
242     /* API_REMOVE_EVT */
243     {AVDT_SCB_SND_ABORT_REQ, AVDT_SCB_SET_REMOVE, AVDT_SCB_CONF_ST},
244     /* API_WRITE_REQ_EVT */
245     {AVDT_SCB_FREE_PKT, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
246     /* API_GETCONFIG_REQ_EVT */
247     {AVDT_SCB_SND_GETCONFIG_REQ, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
248     /* API_DELAY_RPT_REQ_EVT */
249     {AVDT_SCB_SND_DELAY_RPT_REQ, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
250     /* API_SETCONFIG_REQ_EVT */
251     {AVDT_SCB_CB_ERR, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
252     /* API_OPEN_REQ_EVT */
253     {AVDT_SCB_SND_OPEN_REQ, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
254     /* API_CLOSE_REQ_EVT */
255     {AVDT_SCB_SND_ABORT_REQ, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
256     /* API_RECONFIG_REQ_EVT */
257     {AVDT_SCB_CB_ERR, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
258     /* API_SECURITY_REQ_EVT */
259     {AVDT_SCB_SND_SECURITY_REQ, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
260     /* API_ABORT_REQ_EVT */
261     {AVDT_SCB_SND_ABORT_REQ, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
262     /* API_GETCONFIG_RSP_EVT */
263     {AVDT_SCB_SND_GETCONFIG_RSP, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
264     /* API_SETCONFIG_RSP_EVT */
265     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
266     /* API_SETCONFIG_REJ_EVT */
267     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
268     /* API_OPEN_RSP_EVT */
269     {AVDT_SCB_SND_OPEN_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
270     /* API_CLOSE_RSP_EVT */
271     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
272     /* API_RECONFIG_RSP_EVT */
273     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
274     /* API_SECURITY_RSP_EVT */
275     {AVDT_SCB_SND_SECURITY_RSP, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
276     /* API_ABORT_RSP_EVT */
277     {AVDT_SCB_SND_ABORT_RSP, AVDT_SCB_HDL_TC_CLOSE, AVDT_SCB_IDLE_ST},
278     /* MSG_SETCONFIG_CMD_EVT */
279     {AVDT_SCB_REJ_IN_USE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
280     /* MSG_GETCONFIG_CMD_EVT */
281     {AVDT_SCB_HDL_GETCONFIG_CMD, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
282     /* MSG_OPEN_CMD_EVT */
283     {AVDT_SCB_HDL_OPEN_CMD, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
284     /* MSG_START_CMD_EVT */
285     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
286     /* MSG_SUSPEND_CMD_EVT */
287     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
288     /* MSG_CLOSE_CMD_EVT */
289     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
290     /* MSG_ABORT_CMD_EVT */
291     {AVDT_SCB_HDL_ABORT_CMD, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
292     /* MSG_RECONFIG_CMD_EVT */
293     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
294     /* MSG_SECURITY_CMD_EVT */
295     {AVDT_SCB_HDL_SECURITY_CMD, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
296     /* MSG_DELAY_RPT_CMD_EVT */
297     {AVDT_SCB_HDL_DELAY_RPT_CMD, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
298     /* MSG_DELAY_RPT_RSP_EVT */
299     {AVDT_SCB_HDL_DELAY_RPT_RSP, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
300     /* MSG_SETCONFIG_RSP_EVT */
301     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
302     /* MSG_GETCONFIG_RSP_EVT */
303     {AVDT_SCB_HDL_GETCONFIG_RSP, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
304     /* MSG_OPEN_RSP_EVT */
305     {AVDT_SCB_HDL_OPEN_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
306     /* MSG_START_RSP_EVT */
307     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
308     /* MSG_SUSPEND_RSP_EVT */
309     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
310     /* MSG_CLOSE_RSP_EVT */
311     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
312     /* MSG_ABORT_RSP_EVT */
313     {AVDT_SCB_HDL_ABORT_RSP, AVDT_SCB_HDL_TC_CLOSE, AVDT_SCB_IDLE_ST},
314     /* MSG_RECONFIG_RSP_EVT */
315     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
316     /* MSG_SECURITY_RSP_EVT */
317     {AVDT_SCB_HDL_SECURITY_RSP, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
318     /* MSG_SETCONFIG_REJ_EVT */
319     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
320     /* MSG_OPEN_REJ_EVT */
321     {AVDT_SCB_HDL_OPEN_REJ, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
322     /* MSG_START_REJ_EVT */
323     {AVDT_SCB_HDL_START_RSP, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
324     /* MSG_SUSPEND_REJ_EVT */
325     {AVDT_SCB_HDL_SUSPEND_RSP, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
326     /* TC_TOUT_EVT */
327     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
328     /* TC_OPEN_EVT */
329     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
330     /* TC_CLOSE_EVT */
331     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
332     /* TC_CONG_EVT */
333     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
334     /* TC_DATA_EVT */
335     {AVDT_SCB_DROP_PKT, AVDT_SCB_IGNORE, AVDT_SCB_CONF_ST},
336     /* CC_CLOSE_EVT */
337     {AVDT_SCB_HDL_TC_CLOSE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST}};
338 
339 /* state table for opening state */
340 const uint8_t avdt_scb_st_opening[][AVDT_SCB_NUM_COLS] = {
341     /* Event */
342     /* Action 1                       Action 2                    Next state */
343     /* API_REMOVE_EVT */
344     {AVDT_SCB_SND_CLOSE_REQ, AVDT_SCB_SET_REMOVE, AVDT_SCB_CLOSING_ST},
345     /* API_WRITE_REQ_EVT */
346     {AVDT_SCB_FREE_PKT, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
347     /* API_GETCONFIG_REQ_EVT */
348     {AVDT_SCB_SND_GETCONFIG_REQ, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
349     /* API_DELAY_RPT_REQ_EVT */
350     {AVDT_SCB_SND_DELAY_RPT_REQ, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
351     /* API_SETCONFIG_REQ_EVT */
352     {AVDT_SCB_CB_ERR, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
353     /* API_OPEN_REQ_EVT */
354     {AVDT_SCB_CB_ERR, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
355     /* API_CLOSE_REQ_EVT */
356     {AVDT_SCB_SND_CLOSE_REQ, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
357     /* API_RECONFIG_REQ_EVT */
358     {AVDT_SCB_CB_ERR, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
359     /* API_SECURITY_REQ_EVT */
360     {AVDT_SCB_SND_SECURITY_REQ, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
361     /* API_ABORT_REQ_EVT */
362     {AVDT_SCB_SND_ABORT_REQ, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
363     /* API_GETCONFIG_RSP_EVT */
364     {AVDT_SCB_SND_GETCONFIG_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
365     /* API_SETCONFIG_RSP_EVT */
366     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
367     /* API_SETCONFIG_REJ_EVT */
368     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
369     /* API_OPEN_RSP_EVT */
370     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
371     /* API_CLOSE_RSP_EVT */
372     {AVDT_SCB_SND_CLOSE_RSP, AVDT_SCB_SND_TC_CLOSE, AVDT_SCB_CLOSING_ST},
373     /* API_RECONFIG_RSP_EVT */
374     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
375     /* API_SECURITY_RSP_EVT */
376     {AVDT_SCB_SND_SECURITY_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
377     /* API_ABORT_RSP_EVT */
378     {AVDT_SCB_SND_ABORT_RSP, AVDT_SCB_SND_TC_CLOSE, AVDT_SCB_CLOSING_ST},
379     /* MSG_SETCONFIG_CMD_EVT */
380     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
381     /* MSG_GETCONFIG_CMD_EVT */
382     {AVDT_SCB_HDL_GETCONFIG_CMD, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
383     /* MSG_OPEN_CMD_EVT */
384     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
385     /* MSG_START_CMD_EVT */
386     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
387     /* MSG_SUSPEND_CMD_EVT */
388     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
389     /* MSG_CLOSE_CMD_EVT */
390     {AVDT_SCB_HDL_CLOSE_CMD, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
391     /* MSG_ABORT_CMD_EVT */
392     {AVDT_SCB_HDL_ABORT_CMD, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
393     /* MSG_RECONFIG_CMD_EVT */
394     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
395     /* MSG_SECURITY_CMD_EVT */
396     {AVDT_SCB_HDL_SECURITY_CMD, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
397     /* MSG_DELAY_RPT_CMD_EVT */
398     {AVDT_SCB_HDL_DELAY_RPT_CMD, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
399     /* MSG_DELAY_RPT_RSP_EVT */
400     {AVDT_SCB_HDL_DELAY_RPT_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
401     /* MSG_SETCONFIG_RSP_EVT */
402     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
403     /* MSG_GETCONFIG_RSP_EVT */
404     {AVDT_SCB_HDL_GETCONFIG_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
405     /* MSG_OPEN_RSP_EVT */
406     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
407     /* MSG_START_RSP_EVT */
408     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
409     /* MSG_SUSPEND_RSP_EVT */
410     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
411     /* MSG_CLOSE_RSP_EVT */
412     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
413     /* MSG_ABORT_RSP_EVT */
414     {AVDT_SCB_SND_TC_CLOSE, AVDT_SCB_HDL_ABORT_RSP, AVDT_SCB_CLOSING_ST},
415     /* MSG_RECONFIG_RSP_EVT */
416     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
417     /* MSG_SECURITY_RSP_EVT */
418     {AVDT_SCB_HDL_SECURITY_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
419     /* MSG_SETCONFIG_REJ_EVT */
420     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
421     /* MSG_OPEN_REJ_EVT */
422     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
423     /* MSG_START_REJ_EVT */
424     {AVDT_SCB_HDL_START_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
425     /* MSG_SUSPEND_REJ_EVT */
426     {AVDT_SCB_HDL_SUSPEND_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
427     /* TC_TOUT_EVT */
428     {AVDT_SCB_SND_ABORT_REQ, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
429     /* TC_OPEN_EVT */
430     {AVDT_SCB_HDL_TC_OPEN, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
431     /* TC_CLOSE_EVT */
432     {AVDT_SCB_HDL_TC_CLOSE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
433     /* TC_CONG_EVT */
434     {AVDT_SCB_CONG_STATE, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
435     /* TC_DATA_EVT */
436     {AVDT_SCB_DROP_PKT, AVDT_SCB_IGNORE, AVDT_SCB_OPENING_ST},
437     /* CC_CLOSE_EVT */
438     {AVDT_SCB_SND_TC_CLOSE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST}};
439 
440 /* state table for open state */
441 const uint8_t avdt_scb_st_open[][AVDT_SCB_NUM_COLS] = {
442     /* Event */
443     /* Action 1                       Action 2                    Next state */
444     /* API_REMOVE_EVT */
445     {AVDT_SCB_SND_CLOSE_REQ, AVDT_SCB_SET_REMOVE, AVDT_SCB_CLOSING_ST},
446     /* API_WRITE_REQ_EVT */
447     {AVDT_SCB_FREE_PKT, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
448     /* API_GETCONFIG_REQ_EVT */
449     {AVDT_SCB_SND_GETCONFIG_REQ, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
450     /* API_DELAY_RPT_REQ_EVT */
451     {AVDT_SCB_SND_DELAY_RPT_REQ, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
452     /* API_SETCONFIG_REQ_EVT */
453     {AVDT_SCB_CB_ERR, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
454     /* API_OPEN_REQ_EVT */
455     {AVDT_SCB_CB_ERR, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
456     /* API_CLOSE_REQ_EVT */
457     {AVDT_SCB_SND_CLOSE_REQ, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
458     /* API_RECONFIG_REQ_EVT */
459     {AVDT_SCB_SND_RECONFIG_REQ, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
460     /* API_SECURITY_REQ_EVT */
461     {AVDT_SCB_SND_SECURITY_REQ, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
462     /* API_ABORT_REQ_EVT */
463     {AVDT_SCB_SND_ABORT_REQ, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
464     /* API_GETCONFIG_RSP_EVT */
465     {AVDT_SCB_SND_GETCONFIG_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
466     /* API_SETCONFIG_RSP_EVT */
467     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
468     /* API_SETCONFIG_REJ_EVT */
469     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
470     /* API_OPEN_RSP_EVT */
471     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
472     /* API_CLOSE_RSP_EVT */
473     {AVDT_SCB_SND_CLOSE_RSP, AVDT_SCB_TC_TIMER, AVDT_SCB_CLOSING_ST},
474     /* API_RECONFIG_RSP_EVT */
475     {AVDT_SCB_SND_RECONFIG_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
476     /* API_SECURITY_RSP_EVT */
477     {AVDT_SCB_SND_SECURITY_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
478     /* API_ABORT_RSP_EVT */
479     {AVDT_SCB_SND_ABORT_RSP, AVDT_SCB_TC_TIMER, AVDT_SCB_CLOSING_ST},
480     /* MSG_SETCONFIG_CMD_EVT */
481     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
482     /* MSG_GETCONFIG_CMD_EVT */
483     {AVDT_SCB_HDL_GETCONFIG_CMD, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
484     /* MSG_OPEN_CMD_EVT */
485     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
486     /* MSG_START_CMD_EVT */
487     {AVDT_SCB_HDL_START_CMD, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
488     /* MSG_SUSPEND_CMD_EVT */
489     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
490     /* MSG_CLOSE_CMD_EVT */
491     {AVDT_SCB_HDL_CLOSE_CMD, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
492     /* MSG_ABORT_CMD_EVT */
493     {AVDT_SCB_HDL_ABORT_CMD, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
494     /* MSG_RECONFIG_CMD_EVT */
495     {AVDT_SCB_HDL_RECONFIG_CMD, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
496     /* MSG_SECURITY_CMD_EVT */
497     {AVDT_SCB_HDL_SECURITY_CMD, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
498     /* MSG_DELAY_RPT_CMD_EVT */
499     {AVDT_SCB_HDL_DELAY_RPT_CMD, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
500     /* MSG_DELAY_RPT_RSP_EVT */
501     {AVDT_SCB_HDL_DELAY_RPT_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
502     /* MSG_SETCONFIG_RSP_EVT */
503     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
504     /* MSG_GETCONFIG_RSP_EVT */
505     {AVDT_SCB_HDL_GETCONFIG_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
506     /* MSG_OPEN_RSP_EVT */
507     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
508     /* MSG_START_RSP_EVT */
509     {AVDT_SCB_HDL_START_RSP, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
510     /* MSG_SUSPEND_RSP_EVT */
511     {AVDT_SCB_HDL_SUSPEND_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
512     /* MSG_CLOSE_RSP_EVT */
513     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
514     /* MSG_ABORT_RSP_EVT */
515     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
516     /* MSG_RECONFIG_RSP_EVT */
517     {AVDT_SCB_HDL_RECONFIG_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
518     /* MSG_SECURITY_RSP_EVT */
519     {AVDT_SCB_HDL_SECURITY_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
520     /* MSG_SETCONFIG_REJ_EVT */
521     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
522     /* MSG_OPEN_REJ_EVT */
523     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
524     /* MSG_START_REJ_EVT */
525     {AVDT_SCB_HDL_START_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
526     /* MSG_SUSPEND_REJ_EVT */
527     {AVDT_SCB_HDL_SUSPEND_RSP, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
528     /* TC_TOUT_EVT */
529     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
530     /* TC_OPEN_EVT */
531     {AVDT_SCB_HDL_TC_OPEN_STO, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
532     /* TC_CLOSE_EVT */
533     {AVDT_SCB_HDL_TC_CLOSE_STO, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
534     /* TC_CONG_EVT */
535     {AVDT_SCB_CONG_STATE, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
536     /* TC_DATA_EVT */
537     {AVDT_SCB_DROP_PKT, AVDT_SCB_IGNORE, AVDT_SCB_OPEN_ST},
538     /* CC_CLOSE_EVT */
539     {AVDT_SCB_SND_TC_CLOSE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST}};
540 
541 /* state table for streaming state */
542 const uint8_t avdt_scb_st_stream[][AVDT_SCB_NUM_COLS] = {
543     /* Event */
544     /* Action 1                       Action 2                    Next state */
545     /* API_REMOVE_EVT */
546     {AVDT_SCB_SND_STREAM_CLOSE, AVDT_SCB_SET_REMOVE, AVDT_SCB_CLOSING_ST},
547     /* API_WRITE_REQ_EVT */
548     {AVDT_SCB_HDL_WRITE_REQ, AVDT_SCB_CHK_SND_PKT, AVDT_SCB_STREAM_ST},
549     /* API_GETCONFIG_REQ_EVT */
550     {AVDT_SCB_SND_GETCONFIG_REQ, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
551     /* API_DELAY_RPT_REQ_EVT */
552     {AVDT_SCB_SND_DELAY_RPT_REQ, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
553     /* API_SETCONFIG_REQ_EVT */
554     {AVDT_SCB_CB_ERR, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
555     /* API_OPEN_REQ_EVT */
556     {AVDT_SCB_CB_ERR, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
557     /* API_CLOSE_REQ_EVT */
558     {AVDT_SCB_SND_STREAM_CLOSE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
559     /* API_RECONFIG_REQ_EVT */
560     {AVDT_SCB_CB_ERR, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
561     /* API_SECURITY_REQ_EVT */
562     {AVDT_SCB_SND_SECURITY_REQ, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
563     /* API_ABORT_REQ_EVT */
564     {AVDT_SCB_SND_ABORT_REQ, AVDT_SCB_CLR_PKT, AVDT_SCB_CLOSING_ST},
565     /* API_GETCONFIG_RSP_EVT */
566     {AVDT_SCB_SND_GETCONFIG_RSP, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
567     /* API_SETCONFIG_RSP_EVT */
568     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
569     /* API_SETCONFIG_REJ_EVT */
570     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
571     /* API_OPEN_RSP_EVT */
572     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
573     /* API_CLOSE_RSP_EVT */
574     {AVDT_SCB_SND_CLOSE_RSP, AVDT_SCB_TC_TIMER, AVDT_SCB_CLOSING_ST},
575     /* API_RECONFIG_RSP_EVT */
576     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
577     /* API_SECURITY_RSP_EVT */
578     {AVDT_SCB_SND_SECURITY_RSP, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
579     /* API_ABORT_RSP_EVT */
580     {AVDT_SCB_SND_ABORT_RSP, AVDT_SCB_TC_TIMER, AVDT_SCB_CLOSING_ST},
581     /* MSG_SETCONFIG_CMD_EVT */
582     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
583     /* MSG_GETCONFIG_CMD_EVT */
584     {AVDT_SCB_HDL_GETCONFIG_CMD, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
585     /* MSG_OPEN_CMD_EVT */
586     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
587     /* MSG_START_CMD_EVT */
588     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
589     /* MSG_SUSPEND_CMD_EVT */
590     {AVDT_SCB_HDL_SUSPEND_CMD, AVDT_SCB_CLR_PKT, AVDT_SCB_OPEN_ST},
591     /* MSG_CLOSE_CMD_EVT */
592     {AVDT_SCB_HDL_CLOSE_CMD, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
593     /* MSG_ABORT_CMD_EVT */
594     {AVDT_SCB_HDL_ABORT_CMD, AVDT_SCB_CLR_PKT, AVDT_SCB_STREAM_ST},
595     /* MSG_RECONFIG_CMD_EVT */
596     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
597     /* MSG_SECURITY_CMD_EVT */
598     {AVDT_SCB_HDL_SECURITY_CMD, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
599     /* MSG_DELAY_RPT_CMD_EVT */
600     {AVDT_SCB_HDL_DELAY_RPT_CMD, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
601     /* MSG_DELAY_RPT_RSP_EVT */
602     {AVDT_SCB_HDL_DELAY_RPT_RSP, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
603     /* MSG_SETCONFIG_RSP_EVT */
604     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
605     /* MSG_GETCONFIG_RSP_EVT */
606     {AVDT_SCB_HDL_GETCONFIG_RSP, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
607     /* MSG_OPEN_RSP_EVT */
608     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
609     /* MSG_START_RSP_EVT */
610     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
611     /* MSG_SUSPEND_RSP_EVT */
612     {AVDT_SCB_HDL_SUSPEND_RSP, AVDT_SCB_CLR_PKT, AVDT_SCB_OPEN_ST},
613     /* MSG_CLOSE_RSP_EVT */
614     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
615     /* MSG_ABORT_RSP_EVT */
616     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
617     /* MSG_RECONFIG_RSP_EVT */
618     {AVDT_SCB_HDL_RECONFIG_RSP, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
619     /* MSG_SECURITY_RSP_EVT */
620     {AVDT_SCB_HDL_SECURITY_RSP, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
621     /* MSG_SETCONFIG_REJ_EVT */
622     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
623     /* MSG_OPEN_REJ_EVT */
624     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
625     /* MSG_START_REJ_EVT */
626     {AVDT_SCB_HDL_START_RSP, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
627     /* MSG_SUSPEND_REJ_EVT */
628     {AVDT_SCB_HDL_SUSPEND_RSP, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
629     /* TC_TOUT_EVT */
630     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
631     /* TC_OPEN_EVT */
632     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
633     /* TC_CLOSE_EVT */
634     {AVDT_SCB_HDL_TC_CLOSE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
635     /* TC_CONG_EVT */
636     {AVDT_SCB_CONG_STATE, AVDT_SCB_CHK_SND_PKT, AVDT_SCB_STREAM_ST},
637     /* TC_DATA_EVT */
638     {AVDT_SCB_HDL_PKT, AVDT_SCB_IGNORE, AVDT_SCB_STREAM_ST},
639     /* CC_CLOSE_EVT */
640     {AVDT_SCB_SND_TC_CLOSE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST}};
641 
642 /* state table for closing state */
643 const uint8_t avdt_scb_st_closing[][AVDT_SCB_NUM_COLS] = {
644     /* Event */
645     /* Action 1                       Action 2                    Next state */
646     /* API_REMOVE_EVT */
647     {AVDT_SCB_SET_REMOVE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
648     /* API_WRITE_REQ_EVT */
649     {AVDT_SCB_FREE_PKT, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
650     /* API_GETCONFIG_REQ_EVT */
651     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
652     /* API_DELAY_RPT_REQ_EVT */
653     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
654     /* API_SETCONFIG_REQ_EVT */
655     {AVDT_SCB_CB_ERR, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
656     /* API_OPEN_REQ_EVT */
657     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
658     /* API_CLOSE_REQ_EVT */
659     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
660     /* API_RECONFIG_REQ_EVT */
661     {AVDT_SCB_CB_ERR, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
662     /* API_SECURITY_REQ_EVT */
663     {AVDT_SCB_CB_ERR, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
664     /* API_ABORT_REQ_EVT */
665     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
666     /* API_GETCONFIG_RSP_EVT */
667     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
668     /* API_SETCONFIG_RSP_EVT */
669     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
670     /* API_SETCONFIG_REJ_EVT */
671     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
672     /* API_OPEN_RSP_EVT */
673     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
674     /* API_CLOSE_RSP_EVT */
675     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
676     /* API_RECONFIG_RSP_EVT */
677     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
678     /* API_SECURITY_RSP_EVT */
679     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
680     /* API_ABORT_RSP_EVT */
681     {AVDT_SCB_SND_ABORT_RSP, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
682     /* MSG_SETCONFIG_CMD_EVT */
683     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
684     /* MSG_GETCONFIG_CMD_EVT */
685     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
686     /* MSG_OPEN_CMD_EVT */
687     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
688     /* MSG_START_CMD_EVT */
689     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
690     /* MSG_SUSPEND_CMD_EVT */
691     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
692     /* MSG_CLOSE_CMD_EVT */
693     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
694     /* MSG_ABORT_CMD_EVT */
695     {AVDT_SCB_HDL_ABORT_CMD, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
696     /* MSG_RECONFIG_CMD_EVT */
697     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
698     /* MSG_SECURITY_CMD_EVT */
699     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
700     /* MSG_DELAY_RPT_CMD_EVT */
701     {AVDT_SCB_REJ_STATE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
702     /* MSG_DELAY_RPT_RSP_EVT */
703     {AVDT_SCB_HDL_DELAY_RPT_RSP, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
704     /* MSG_SETCONFIG_RSP_EVT */
705     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
706     /* MSG_GETCONFIG_RSP_EVT */
707     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
708     /* MSG_OPEN_RSP_EVT */
709     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
710     /* MSG_START_RSP_EVT */
711     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
712     /* MSG_SUSPEND_RSP_EVT */
713     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
714     /* MSG_CLOSE_RSP_EVT */
715     {AVDT_SCB_SND_TC_CLOSE, AVDT_SCB_HDL_CLOSE_RSP, AVDT_SCB_CLOSING_ST},
716     /* MSG_ABORT_RSP_EVT */
717     {AVDT_SCB_SND_TC_CLOSE, AVDT_SCB_HDL_ABORT_RSP, AVDT_SCB_CLOSING_ST},
718     /* MSG_RECONFIG_RSP_EVT */
719     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
720     /* MSG_SECURITY_RSP_EVT */
721     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
722     /* MSG_SETCONFIG_REJ_EVT */
723     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
724     /* MSG_OPEN_REJ_EVT */
725     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
726     /* MSG_START_REJ_EVT */
727     {AVDT_SCB_HDL_START_RSP, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
728     /* MSG_SUSPEND_REJ_EVT */
729     {AVDT_SCB_HDL_SUSPEND_RSP, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
730     /* TC_TOUT_EVT */
731     {AVDT_SCB_SND_TC_CLOSE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
732     /* TC_OPEN_EVT */
733     {AVDT_SCB_SND_TC_CLOSE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
734     /* TC_CLOSE_EVT */
735     {AVDT_SCB_HDL_TC_CLOSE, AVDT_SCB_IGNORE, AVDT_SCB_IDLE_ST},
736     /* TC_CONG_EVT */
737     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
738     /* TC_DATA_EVT */
739     {AVDT_SCB_DROP_PKT, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST},
740     /* CC_CLOSE_EVT */
741     {AVDT_SCB_IGNORE, AVDT_SCB_IGNORE, AVDT_SCB_CLOSING_ST}};
742 
743 /* type for state table */
744 typedef const uint8_t (*tAVDT_SCB_ST_TBL)[AVDT_SCB_NUM_COLS];
745 
746 /* state table */
747 const tAVDT_SCB_ST_TBL avdt_scb_st_tbl[] = {
748     avdt_scb_st_idle, avdt_scb_st_conf,   avdt_scb_st_opening,
749     avdt_scb_st_open, avdt_scb_st_stream, avdt_scb_st_closing};
750 
751 /*******************************************************************************
752  *
753  * Function         avdt_scb_event
754  *
755  * Description      State machine event handling function for scb
756  *
757  *
758  * Returns          Nothing.
759  *
760  ******************************************************************************/
avdt_scb_event(AvdtpScb * p_scb,uint8_t event,tAVDT_SCB_EVT * p_data)761 void avdt_scb_event(AvdtpScb* p_scb, uint8_t event, tAVDT_SCB_EVT* p_data) {
762   tAVDT_SCB_ST_TBL state_table;
763   uint8_t action;
764 
765 #if (AVDT_DEBUG == TRUE)
766   log::verbose("SCB hdl={} event={}/{} state={} p_avdt_scb={} scb_index={}",
767                avdt_scb_to_hdl(p_scb), event, avdt_scb_evt_str[event],
768                avdt_scb_st_str[p_scb->state], fmt::ptr(p_scb),
769                p_scb->stream_config.scb_index);
770 #endif
771 
772   /* Check that we only send AVDT_SCB_API_WRITE_REQ_EVT to the active stream
773    * device */
774   uint8_t num_st_streams = 0;
775   int ccb_index = -1;
776   int scb_index = -1;
777 
778   for (int i = 0; i < AVDT_NUM_LINKS; i++) {
779     for (int j = 0; j < AVDT_NUM_SEPS; j++) {
780       AvdtpScb* p_avdt_scb = &avdtp_cb.ccb[i].scb[j];
781       if (p_avdt_scb->allocated &&
782           avdt_scb_st_tbl[p_avdt_scb->state] == avdt_scb_st_stream) {
783         num_st_streams++;
784         ccb_index = i;
785         scb_index = j;
786       } else {
787         p_avdt_scb->curr_stream = false;
788       }
789     }
790   }
791 
792   if (num_st_streams == 1) {
793     avdtp_cb.ccb[ccb_index].scb[scb_index].curr_stream = true;
794   } else if (num_st_streams > 1 && !p_scb->curr_stream &&
795              event == AVDT_SCB_API_WRITE_REQ_EVT) {
796     log::error("ignore AVDT_SCB_API_WRITE_REQ_EVT");
797     avdt_scb_free_pkt(p_scb, p_data);
798     return;
799   }
800 
801   /* set current event */
802   p_scb->curr_evt = event;
803 
804   /* look up the state table for the current state */
805   state_table = avdt_scb_st_tbl[p_scb->state];
806 
807   /* set next state */
808   if (p_scb->state != state_table[event][AVDT_SCB_NEXT_STATE]) {
809     p_scb->state = state_table[event][AVDT_SCB_NEXT_STATE];
810   }
811 
812   /* execute action functions */
813   for (int i = 0; i < AVDT_SCB_ACTIONS; i++) {
814     action = state_table[event][i];
815     if (action != AVDT_SCB_IGNORE) {
816       (*avdtp_cb.p_scb_act[action])(p_scb, p_data);
817     } else {
818       break;
819     }
820   }
821 }
822 
823 /*******************************************************************************
824  *
825  * Function         avdt_scb_init
826  *
827  * Description      Initialize stream control block module.
828  *
829  *
830  * Returns          Nothing.
831  *
832  ******************************************************************************/
avdt_scb_init(void)833 void avdt_scb_init(void) {
834   for (size_t i = 0; i < AVDT_NUM_LINKS; i++) {
835     for (size_t j = 0; j < AVDT_NUM_SEPS; j++) {
836       avdtp_cb.ccb[i].scb[j].Reset(0);
837     }
838   }
839 
840   avdtp_cb.p_scb_act = avdt_scb_action;
841 }
842 
843 /*******************************************************************************
844  *
845  * Function         avdt_scb_alloc
846  *
847  * Description      Allocate a stream control block.
848  *
849  *
850  * Returns          pointer to the scb, or NULL if none could be allocated.
851  *
852  ******************************************************************************/
avdt_scb_alloc(uint8_t peer_id,const AvdtpStreamConfig & avdtp_stream_config)853 AvdtpScb* avdt_scb_alloc(uint8_t peer_id,
854                          const AvdtpStreamConfig& avdtp_stream_config) {
855   log::assert_that(peer_id < AVDT_NUM_LINKS,
856                    "assert failed: peer_id < AVDT_NUM_LINKS");
857 
858   // Find available entry
859   AvdtpScb* p_scb = &avdtp_cb.ccb[peer_id].scb[0];
860   for (int i = 0; i < AVDT_NUM_SEPS; i++, p_scb++) {
861     if (!p_scb->allocated) {
862       p_scb->Allocate(&avdtp_cb.ccb[peer_id], avdtp_stream_config);
863       log::verbose("allocated (handle={}, psc_mask:0x{:x})", p_scb->ScbHandle(),
864                    avdtp_stream_config.cfg.psc_mask);
865       return p_scb;
866     }
867   }
868 
869   log::warn("out of AvdtScb entries for peer_id {}", peer_id);
870   return nullptr;
871 }
872 
Allocate(AvdtpCcb * p_avdtp_ccb,const AvdtpStreamConfig & avdtp_stream_config)873 void AvdtpScb::Allocate(AvdtpCcb* p_avdtp_ccb,
874                         const AvdtpStreamConfig& avdtp_stream_config) {
875   uint8_t scb_handle = avdtp_cb.ComputeScbHandle(this);
876   Reset(scb_handle);
877   p_ccb = p_avdtp_ccb;
878   stream_config = avdtp_stream_config;
879   transport_channel_timer = alarm_new("avdtp_scb.transport_channel_timer");
880   allocated = true;
881 }
882 
883 /*******************************************************************************
884  *
885  * Function         avdt_scb_dealloc
886  *
887  * Description      Deallocate a stream control block.
888  *
889  *
890  * Returns          void.
891  *
892  ******************************************************************************/
avdt_scb_dealloc(AvdtpScb * p_scb,tAVDT_SCB_EVT *)893 void avdt_scb_dealloc(AvdtpScb* p_scb, tAVDT_SCB_EVT* /* p_data */) {
894   log::verbose("hdl={}", avdt_scb_to_hdl(p_scb));
895   p_scb->Recycle();
896 }
897 
898 /*******************************************************************************
899  *
900  * Function         avdt_scb_to_hdl
901  *
902  * Description      Given a pointer to an scb, return its handle (or seid).
903  *
904  *
905  * Returns          Index of scb.
906  *
907  ******************************************************************************/
avdt_scb_to_hdl(AvdtpScb * p_scb)908 uint8_t avdt_scb_to_hdl(AvdtpScb* p_scb) { return p_scb->ScbHandle(); }
909 
910 /*******************************************************************************
911  *
912  * Function         avdt_scb_by_hdl
913  *
914  * Description      Given an scb handle (or seid), return a pointer to the scb.
915  *
916  *
917  * Returns          Pointer to scb or NULL if index is out of range or scb
918  *                  is not allocated.
919  *
920  ******************************************************************************/
avdt_scb_by_hdl(uint8_t hdl)921 AvdtpScb* avdt_scb_by_hdl(uint8_t hdl) {
922   // Verify the index
923   if ((hdl < 1) || (hdl > AVDT_NUM_LINKS * AVDT_NUM_SEPS)) {
924     log::warn("SCB handle {} out of range", hdl);
925     return nullptr;
926   }
927 
928   uint8_t index = hdl - 1;
929   size_t i = index / AVDT_NUM_SEPS;
930   size_t j = index % AVDT_NUM_SEPS;
931 
932   AvdtpScb* p_scb = &avdtp_cb.ccb[i].scb[j];
933   // Verify the whether the scb is allocated
934   if (!p_scb->allocated) {
935     log::warn("SCB handle {} not allocated", hdl);
936     return nullptr;
937   }
938 
939   log::verbose("SCB for handle {} found: p_scb={} scb_index={}", hdl,
940                fmt::ptr(p_scb), p_scb->stream_config.scb_index);
941   return p_scb;
942 }
943 
944 /*******************************************************************************
945  *
946  * Function         avdt_scb_verify
947  *
948  * Description      Verify the condition of a list of scbs.
949  *
950  *
951  * Returns          SEID that failed, or 0 if success.
952  *
953  ******************************************************************************/
avdt_scb_verify(AvdtpCcb * p_ccb,uint8_t state,uint8_t * p_seid,uint16_t num_seid,uint8_t * p_err_code)954 uint8_t avdt_scb_verify(AvdtpCcb* p_ccb, uint8_t state, uint8_t* p_seid,
955                         uint16_t num_seid, uint8_t* p_err_code) {
956   log::verbose("avdt_scb_verify state {}", state);
957   /* set nonsupported command mask */
958   /* translate public state into private state */
959   uint8_t nsc_mask = 0;
960   if (state == AVDT_VERIFY_SUSPEND) {
961     nsc_mask = AvdtpStreamConfig::AVDT_NSC_SUSPEND;
962   }
963 
964   /* verify every scb */
965   *p_err_code = 0;
966   for (int i = 0; (i < num_seid) && (i < AVDT_NUM_SEPS); i++) {
967     AvdtpScb* p_scb = avdt_scb_by_hdl(p_seid[i]);
968     if (p_scb == NULL) {
969       *p_err_code = AVDT_ERR_BAD_STATE;
970       return p_seid[i];
971     }
972 
973     if (p_scb->p_ccb != p_ccb) {
974       *p_err_code = AVDT_ERR_BAD_STATE;
975       return p_seid[i];
976     }
977 
978     if (p_scb->stream_config.nsc_mask & nsc_mask) {
979       *p_err_code = AVDT_ERR_NSC;
980       return p_seid[i];
981     }
982 
983     switch (state) {
984       case AVDT_VERIFY_OPEN:
985       case AVDT_VERIFY_START:
986         if (p_scb->state != AVDT_SCB_OPEN_ST &&
987             p_scb->state != AVDT_SCB_STREAM_ST) {
988           *p_err_code = AVDT_ERR_BAD_STATE;
989           return p_seid[i];
990         }
991         break;
992 
993       case AVDT_VERIFY_SUSPEND:
994       case AVDT_VERIFY_STREAMING:
995         if (p_scb->state != AVDT_SCB_STREAM_ST) {
996           *p_err_code = AVDT_ERR_BAD_STATE;
997           return p_seid[i];
998         }
999         break;
1000     }
1001   }
1002 
1003   return 0;
1004 }
1005 
1006 /*******************************************************************************
1007  *
1008  * Function         avdt_scb_peer_seid_list
1009  *
1010  * Description      Given a list of SCB handles, return a list of peer SEIDs
1011  *                  for the handles, copied in place into the struct passed in.
1012  *
1013  *
1014  * Returns          Nothing.
1015  *
1016  ******************************************************************************/
avdt_scb_peer_seid_list(tAVDT_MULTI * p_multi)1017 void avdt_scb_peer_seid_list(tAVDT_MULTI* p_multi) {
1018   int i;
1019   AvdtpScb* p_scb;
1020 
1021   for (i = 0; i < p_multi->num_seps; i++) {
1022     p_scb = avdt_scb_by_hdl(p_multi->seid_list[i]);
1023     if (p_scb != NULL) {
1024       p_multi->seid_list[i] = p_scb->peer_seid;
1025     }
1026   }
1027 }
1028