1 /******************************************************************************
2 *
3 * Copyright (C) 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 channel control block state machine and
22 * functions which operate on the channel control block.
23 *
24 ******************************************************************************/
25
26 #include <string.h>
27 #include "stack/bt_types.h"
28 #include "common/bt_target.h"
29 #include "common/bt_defs.h"
30 #include "stack/avdt_api.h"
31 #include "stack/avdtc_api.h"
32 #include "avdt_int.h"
33 #include "stack/btu.h"
34
35 #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
36
37 /*****************************************************************************
38 ** state machine constants and types
39 *****************************************************************************/
40 #if AVDT_DEBUG == TRUE
41
42 /* verbose state strings for trace */
43 const char *const avdt_ccb_st_str[] = {
44 "CCB_IDLE_ST",
45 "CCB_OPENING_ST",
46 "CCB_OPEN_ST",
47 "CCB_CLOSING_ST"
48 };
49
50 /* verbose event strings for trace */
51 const char *const avdt_ccb_evt_str[] = {
52 "API_DISCOVER_REQ_EVT",
53 "API_GETCAP_REQ_EVT",
54 "API_START_REQ_EVT",
55 "API_SUSPEND_REQ_EVT",
56 "API_DISCOVER_RSP_EVT",
57 "API_GETCAP_RSP_EVT",
58 "API_START_RSP_EVT",
59 "API_SUSPEND_RSP_EVT",
60 "API_CONNECT_REQ_EVT",
61 "API_DISCONNECT_REQ_EVT",
62 "MSG_DISCOVER_CMD_EVT",
63 "MSG_GETCAP_CMD_EVT",
64 "MSG_START_CMD_EVT",
65 "MSG_SUSPEND_CMD_EVT",
66 "MSG_DISCOVER_RSP_EVT",
67 "MSG_GETCAP_RSP_EVT",
68 "MSG_START_RSP_EVT",
69 "MSG_SUSPEND_RSP_EVT",
70 "RCVRSP_EVT",
71 "SENDMSG_EVT",
72 "RET_TOUT_EVT",
73 "RSP_TOUT_EVT",
74 "IDLE_TOUT_EVT",
75 "UL_OPEN_EVT",
76 "UL_CLOSE_EVT",
77 "LL_OPEN_EVT",
78 "LL_CLOSE_EVT",
79 "LL_CONG_EVT"
80 };
81
82 #endif
83
84
85 /* action function list */
86 const tAVDT_CCB_ACTION avdt_ccb_action[] = {
87 avdt_ccb_chan_open,
88 avdt_ccb_chan_close,
89 avdt_ccb_chk_close,
90 avdt_ccb_hdl_discover_cmd,
91 avdt_ccb_hdl_discover_rsp,
92 avdt_ccb_hdl_getcap_cmd,
93 avdt_ccb_hdl_getcap_rsp,
94 avdt_ccb_hdl_start_cmd,
95 avdt_ccb_hdl_start_rsp,
96 avdt_ccb_hdl_suspend_cmd,
97 avdt_ccb_hdl_suspend_rsp,
98 avdt_ccb_snd_discover_cmd,
99 avdt_ccb_snd_discover_rsp,
100 avdt_ccb_snd_getcap_cmd,
101 avdt_ccb_snd_getcap_rsp,
102 avdt_ccb_snd_start_cmd,
103 avdt_ccb_snd_start_rsp,
104 avdt_ccb_snd_suspend_cmd,
105 avdt_ccb_snd_suspend_rsp,
106 avdt_ccb_clear_cmds,
107 avdt_ccb_cmd_fail,
108 avdt_ccb_free_cmd,
109 avdt_ccb_cong_state,
110 avdt_ccb_ret_cmd,
111 avdt_ccb_snd_cmd,
112 avdt_ccb_snd_msg,
113 avdt_ccb_set_reconn,
114 avdt_ccb_clr_reconn,
115 avdt_ccb_chk_reconn,
116 avdt_ccb_chk_timer,
117 avdt_ccb_set_conn,
118 avdt_ccb_set_disconn,
119 avdt_ccb_do_disconn,
120 avdt_ccb_ll_closed,
121 avdt_ccb_ll_opened,
122 avdt_ccb_dealloc
123 };
124
125 /* state table information */
126 #define AVDT_CCB_ACTIONS 2 /* number of actions */
127 #define AVDT_CCB_NEXT_STATE 2 /* position of next state */
128 #define AVDT_CCB_NUM_COLS 3 /* number of columns in state tables */
129
130 /* state table for idle state */
131 const UINT8 avdt_ccb_st_idle[][AVDT_CCB_NUM_COLS] = {
132 /* Event Action 1 Action 2 Next state */
133 /* API_DISCOVER_REQ_EVT */ {AVDT_CCB_SND_DISCOVER_CMD, AVDT_CCB_CHAN_OPEN, AVDT_CCB_OPENING_ST},
134 /* API_GETCAP_REQ_EVT */ {AVDT_CCB_SND_GETCAP_CMD, AVDT_CCB_CHAN_OPEN, AVDT_CCB_OPENING_ST},
135 /* API_START_REQ_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
136 /* API_SUSPEND_REQ_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
137 /* API_DISCOVER_RSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
138 /* API_GETCAP_RSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
139 /* API_START_RSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
140 /* API_SUSPEND_RSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
141 /* API_CONNECT_REQ_EVT */ {AVDT_CCB_SET_CONN, AVDT_CCB_CHAN_OPEN, AVDT_CCB_OPENING_ST},
142 /* API_DISCONNECT_REQ_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
143 /* MSG_DISCOVER_CMD_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
144 /* MSG_GETCAP_CMD_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
145 /* MSG_START_CMD_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
146 /* MSG_SUSPEND_CMD_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
147 /* MSG_DISCOVER_RSP_EVT */ {AVDT_CCB_HDL_DISCOVER_RSP, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
148 /* MSG_GETCAP_RSP_EVT */ {AVDT_CCB_HDL_GETCAP_RSP, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
149 /* MSG_START_RSP_EVT */ {AVDT_CCB_HDL_START_RSP, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
150 /* MSG_SUSPEND_RSP_EVT */ {AVDT_CCB_HDL_SUSPEND_RSP, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
151 /* RCVRSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
152 /* SENDMSG_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
153 /* RET_TOUT_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
154 /* RSP_TOUT_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
155 /* IDLE_TOUT_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
156 /* UL_OPEN_EVT */ {AVDT_CCB_CHAN_OPEN, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
157 /* UL_CLOSE_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
158 /* LL_OPEN_EVT */ {AVDT_CCB_LL_OPENED, AVDT_CCB_IGNORE, AVDT_CCB_OPEN_ST},
159 /* LL_CLOSE_EVT */ {AVDT_CCB_LL_CLOSED, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
160 /* LL_CONG_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST}
161 };
162
163 /* state table for opening state */
164 const UINT8 avdt_ccb_st_opening[][AVDT_CCB_NUM_COLS] = {
165 /* Event Action 1 Action 2 Next state */
166 /* API_DISCOVER_REQ_EVT */ {AVDT_CCB_SND_DISCOVER_CMD, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
167 /* API_GETCAP_REQ_EVT */ {AVDT_CCB_SND_GETCAP_CMD, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
168 /* API_START_REQ_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
169 /* API_SUSPEND_REQ_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
170 /* API_DISCOVER_RSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
171 /* API_GETCAP_RSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
172 /* API_START_RSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
173 /* API_SUSPEND_RSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
174 /* API_CONNECT_REQ_EVT */ {AVDT_CCB_SET_CONN, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
175 /* API_DISCONNECT_REQ_EVT */ {AVDT_CCB_SET_DISCONN, AVDT_CCB_DO_DISCONN, AVDT_CCB_CLOSING_ST},
176 /* MSG_DISCOVER_CMD_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
177 /* MSG_GETCAP_CMD_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
178 /* MSG_START_CMD_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
179 /* MSG_SUSPEND_CMD_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
180 /* MSG_DISCOVER_RSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
181 /* MSG_GETCAP_RSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
182 /* MSG_START_RSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
183 /* MSG_SUSPEND_RSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
184 /* RCVRSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
185 /* SENDMSG_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
186 /* RET_TOUT_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
187 /* RSP_TOUT_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
188 /* IDLE_TOUT_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
189 /* UL_OPEN_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST},
190 /* UL_CLOSE_EVT */ {AVDT_CCB_CLEAR_CMDS, AVDT_CCB_CHAN_CLOSE, AVDT_CCB_CLOSING_ST},
191 /* LL_OPEN_EVT */ {AVDT_CCB_SND_CMD, AVDT_CCB_LL_OPENED, AVDT_CCB_OPEN_ST},
192 /* LL_CLOSE_EVT */ {AVDT_CCB_LL_CLOSED, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
193 /* LL_CONG_EVT */ {AVDT_CCB_CONG_STATE, AVDT_CCB_IGNORE, AVDT_CCB_OPENING_ST}
194 };
195
196 /* state table for open state */
197 const UINT8 avdt_ccb_st_open[][AVDT_CCB_NUM_COLS] = {
198 /* Event Action 1 Action 2 Next state */
199 /* API_DISCOVER_REQ_EVT */ {AVDT_CCB_SND_DISCOVER_CMD, AVDT_CCB_SND_CMD, AVDT_CCB_OPEN_ST},
200 /* API_GETCAP_REQ_EVT */ {AVDT_CCB_SND_GETCAP_CMD, AVDT_CCB_SND_CMD, AVDT_CCB_OPEN_ST},
201 /* API_START_REQ_EVT */ {AVDT_CCB_SND_START_CMD, AVDT_CCB_SND_CMD, AVDT_CCB_OPEN_ST},
202 /* API_SUSPEND_REQ_EVT */ {AVDT_CCB_SND_SUSPEND_CMD, AVDT_CCB_SND_CMD, AVDT_CCB_OPEN_ST},
203 /* API_DISCOVER_RSP_EVT */ {AVDT_CCB_SND_DISCOVER_RSP, AVDT_CCB_SND_CMD, AVDT_CCB_OPEN_ST},
204 /* API_GETCAP_RSP_EVT */ {AVDT_CCB_SND_GETCAP_RSP, AVDT_CCB_SND_CMD, AVDT_CCB_OPEN_ST},
205 /* API_START_RSP_EVT */ {AVDT_CCB_SND_START_RSP, AVDT_CCB_SND_CMD, AVDT_CCB_OPEN_ST},
206 /* API_SUSPEND_RSP_EVT */ {AVDT_CCB_SND_SUSPEND_RSP, AVDT_CCB_SND_CMD, AVDT_CCB_OPEN_ST},
207 /* API_CONNECT_REQ_EVT */ {AVDT_CCB_SET_CONN, AVDT_CCB_LL_OPENED, AVDT_CCB_OPEN_ST},
208 /* API_DISCONNECT_REQ_EVT */ {AVDT_CCB_SET_DISCONN, AVDT_CCB_DO_DISCONN, AVDT_CCB_CLOSING_ST},
209 /* MSG_DISCOVER_CMD_EVT */ {AVDT_CCB_HDL_DISCOVER_CMD, AVDT_CCB_IGNORE, AVDT_CCB_OPEN_ST},
210 /* MSG_GETCAP_CMD_EVT */ {AVDT_CCB_HDL_GETCAP_CMD, AVDT_CCB_IGNORE, AVDT_CCB_OPEN_ST},
211 /* MSG_START_CMD_EVT */ {AVDT_CCB_HDL_START_CMD, AVDT_CCB_IGNORE, AVDT_CCB_OPEN_ST},
212 /* MSG_SUSPEND_CMD_EVT */ {AVDT_CCB_HDL_SUSPEND_CMD, AVDT_CCB_IGNORE, AVDT_CCB_OPEN_ST},
213 /* MSG_DISCOVER_RSP_EVT */ {AVDT_CCB_CHK_CLOSE, AVDT_CCB_HDL_DISCOVER_RSP, AVDT_CCB_OPEN_ST},
214 /* MSG_GETCAP_RSP_EVT */ {AVDT_CCB_CHK_CLOSE, AVDT_CCB_HDL_GETCAP_RSP, AVDT_CCB_OPEN_ST},
215 /* MSG_START_RSP_EVT */ {AVDT_CCB_HDL_START_RSP, AVDT_CCB_IGNORE, AVDT_CCB_OPEN_ST},
216 /* MSG_SUSPEND_RSP_EVT */ {AVDT_CCB_HDL_SUSPEND_RSP, AVDT_CCB_IGNORE, AVDT_CCB_OPEN_ST},
217 /* RCVRSP_EVT */ {AVDT_CCB_FREE_CMD, AVDT_CCB_SND_CMD, AVDT_CCB_OPEN_ST},
218 /* SENDMSG_EVT */ {AVDT_CCB_SND_MSG, AVDT_CCB_IGNORE, AVDT_CCB_OPEN_ST},
219 /* RET_TOUT_EVT */ {AVDT_CCB_RET_CMD, AVDT_CCB_IGNORE, AVDT_CCB_OPEN_ST},
220 /* RSP_TOUT_EVT */ {AVDT_CCB_CMD_FAIL, AVDT_CCB_SND_CMD, AVDT_CCB_OPEN_ST},
221 /* IDLE_TOUT_EVT */ {AVDT_CCB_CLEAR_CMDS, AVDT_CCB_CHAN_CLOSE, AVDT_CCB_CLOSING_ST},
222 /* UL_OPEN_EVT */ {AVDT_CCB_CHK_TIMER, AVDT_CCB_IGNORE, AVDT_CCB_OPEN_ST},
223 /* UL_CLOSE_EVT */ {AVDT_CCB_CHK_CLOSE, AVDT_CCB_IGNORE, AVDT_CCB_OPEN_ST},
224 /* LL_OPEN_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_OPEN_ST},
225 /* LL_CLOSE_EVT */ {AVDT_CCB_LL_CLOSED, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
226 /* LL_CONG_EVT */ {AVDT_CCB_CONG_STATE, AVDT_CCB_SND_MSG, AVDT_CCB_OPEN_ST}
227 };
228
229 /* state table for closing state */
230 const UINT8 avdt_ccb_st_closing[][AVDT_CCB_NUM_COLS] = {
231 /* Event Action 1 Action 2 Next state */
232 /* API_DISCOVER_REQ_EVT */ {AVDT_CCB_SET_RECONN, AVDT_CCB_SND_DISCOVER_CMD, AVDT_CCB_CLOSING_ST},
233 /* API_GETCAP_REQ_EVT */ {AVDT_CCB_SET_RECONN, AVDT_CCB_SND_GETCAP_CMD, AVDT_CCB_CLOSING_ST},
234 /* API_START_REQ_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
235 /* API_SUSPEND_REQ_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
236 /* API_DISCOVER_RSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
237 /* API_GETCAP_RSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
238 /* API_START_RSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
239 /* API_SUSPEND_RSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
240 /* API_CONNECT_REQ_EVT */ {AVDT_CCB_SET_RECONN, AVDT_CCB_SET_CONN, AVDT_CCB_CLOSING_ST},
241 /* API_DISCONNECT_REQ_EVT */ {AVDT_CCB_CLR_RECONN, AVDT_CCB_SET_DISCONN, AVDT_CCB_CLOSING_ST},
242 /* MSG_DISCOVER_CMD_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
243 /* MSG_GETCAP_CMD_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
244 /* MSG_START_CMD_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
245 /* MSG_SUSPEND_CMD_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
246 /* MSG_DISCOVER_RSP_EVT */ {AVDT_CCB_HDL_DISCOVER_RSP, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
247 /* MSG_GETCAP_RSP_EVT */ {AVDT_CCB_HDL_GETCAP_RSP, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
248 /* MSG_START_RSP_EVT */ {AVDT_CCB_HDL_START_RSP, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
249 /* MSG_SUSPEND_RSP_EVT */ {AVDT_CCB_HDL_SUSPEND_RSP, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
250 /* RCVRSP_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
251 /* SENDMSG_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
252 /* RET_TOUT_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
253 /* RSP_TOUT_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
254 /* IDLE_TOUT_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
255 /* UL_OPEN_EVT */ {AVDT_CCB_SET_RECONN, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
256 /* UL_CLOSE_EVT */ {AVDT_CCB_CLR_RECONN, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
257 /* LL_OPEN_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST},
258 /* LL_CLOSE_EVT */ {AVDT_CCB_CHK_RECONN, AVDT_CCB_IGNORE, AVDT_CCB_IDLE_ST},
259 /* LL_CONG_EVT */ {AVDT_CCB_IGNORE, AVDT_CCB_IGNORE, AVDT_CCB_CLOSING_ST}
260 };
261
262 /* type for state table */
263 typedef const UINT8 (*tAVDT_CCB_ST_TBL)[AVDT_CCB_NUM_COLS];
264
265 /* state table */
266 const tAVDT_CCB_ST_TBL avdt_ccb_st_tbl[] = {
267 avdt_ccb_st_idle,
268 avdt_ccb_st_opening,
269 avdt_ccb_st_open,
270 avdt_ccb_st_closing
271 };
272
273 /*******************************************************************************
274 **
275 ** Function avdt_ccb_init
276 **
277 ** Description Initialize channel control block module.
278 **
279 **
280 ** Returns Nothing.
281 **
282 *******************************************************************************/
avdt_ccb_init(void)283 void avdt_ccb_init(void)
284 {
285 memset(&avdt_cb.ccb[0], 0, sizeof(tAVDT_CCB) * AVDT_NUM_LINKS);
286 avdt_cb.p_ccb_act = (tAVDT_CCB_ACTION *) avdt_ccb_action;
287 }
288
289 /*******************************************************************************
290 **
291 ** Function avdt_ccb_event
292 **
293 ** Description State machine event handling function for ccb
294 **
295 **
296 ** Returns Nothing.
297 **
298 *******************************************************************************/
avdt_ccb_event(tAVDT_CCB * p_ccb,UINT8 event,tAVDT_CCB_EVT * p_data)299 void avdt_ccb_event(tAVDT_CCB *p_ccb, UINT8 event, tAVDT_CCB_EVT *p_data)
300 {
301 tAVDT_CCB_ST_TBL state_table;
302 UINT8 action;
303 int i;
304
305 #if AVDT_DEBUG == TRUE
306 AVDT_TRACE_EVENT("CCB ccb=%d event=%s state=%s\n", avdt_ccb_to_idx(p_ccb), avdt_ccb_evt_str[event], avdt_ccb_st_str[p_ccb->state]);
307 #endif
308
309 /* look up the state table for the current state */
310 state_table = avdt_ccb_st_tbl[p_ccb->state];
311
312 /* set next state */
313 if (p_ccb->state != state_table[event][AVDT_CCB_NEXT_STATE]) {
314 p_ccb->state = state_table[event][AVDT_CCB_NEXT_STATE];
315 }
316
317 /* execute action functions */
318 for (i = 0; i < AVDT_CCB_ACTIONS; i++) {
319 if ((action = state_table[event][i]) != AVDT_CCB_IGNORE) {
320 (*avdt_cb.p_ccb_act[action])(p_ccb, p_data);
321 } else {
322 break;
323 }
324 }
325 }
326
327
328 /*******************************************************************************
329 **
330 ** Function avdt_ccb_by_bd
331 **
332 ** Description This lookup function finds the ccb for a BD address.
333 **
334 **
335 ** Returns pointer to the ccb, or NULL if none found.
336 **
337 *******************************************************************************/
avdt_ccb_by_bd(BD_ADDR bd_addr)338 tAVDT_CCB *avdt_ccb_by_bd(BD_ADDR bd_addr)
339 {
340 tAVDT_CCB *p_ccb = &avdt_cb.ccb[0];
341 int i;
342
343 for (i = 0; i < AVDT_NUM_LINKS; i++, p_ccb++) {
344 /* if allocated ccb has matching ccb */
345 if (p_ccb->allocated && (!memcmp(p_ccb->peer_addr, bd_addr, BD_ADDR_LEN))) {
346 break;
347 }
348 }
349
350 if (i == AVDT_NUM_LINKS) {
351 /* if no ccb found */
352 p_ccb = NULL;
353
354 AVDT_TRACE_DEBUG("No ccb for addr %02x-%02x-%02x-%02x-%02x-%02x\n",
355 bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
356 }
357 return p_ccb;
358 }
359
360 /*******************************************************************************
361 **
362 ** Function avdt_ccb_alloc
363 **
364 ** Description Allocate a channel control block.
365 **
366 **
367 ** Returns pointer to the ccb, or NULL if none could be allocated.
368 **
369 *******************************************************************************/
avdt_ccb_alloc(BD_ADDR bd_addr)370 tAVDT_CCB *avdt_ccb_alloc(BD_ADDR bd_addr)
371 {
372 tAVDT_CCB *p_ccb = &avdt_cb.ccb[0];
373 int i;
374
375 for (i = 0; i < AVDT_NUM_LINKS; i++, p_ccb++) {
376 if (!p_ccb->allocated) {
377 p_ccb->allocated = TRUE;
378 memcpy(p_ccb->peer_addr, bd_addr, BD_ADDR_LEN);
379 p_ccb->cmd_q = fixed_queue_new(QUEUE_SIZE_MAX);
380 p_ccb->rsp_q = fixed_queue_new(QUEUE_SIZE_MAX);
381 p_ccb->timer_entry.param = (UINT32) p_ccb;
382 AVDT_TRACE_DEBUG("avdt_ccb_alloc %d\n", i);
383 break;
384 }
385 }
386
387 if (i == AVDT_NUM_LINKS) {
388 /* out of ccbs */
389 p_ccb = NULL;
390 AVDT_TRACE_WARNING("Out of ccbs");
391 }
392 return p_ccb;
393 }
394
395 /*******************************************************************************
396 **
397 ** Function avdt_ccb_dealloc
398 **
399 ** Description Deallocate a stream control block.
400 **
401 **
402 ** Returns void.
403 **
404 *******************************************************************************/
avdt_ccb_dealloc(tAVDT_CCB * p_ccb,tAVDT_CCB_EVT * p_data)405 void avdt_ccb_dealloc(tAVDT_CCB *p_ccb, tAVDT_CCB_EVT *p_data)
406 {
407 UNUSED(p_data);
408
409 AVDT_TRACE_DEBUG("avdt_ccb_dealloc %d\n", avdt_ccb_to_idx(p_ccb));
410 btu_free_timer(&p_ccb->timer_entry);
411 fixed_queue_free(p_ccb->cmd_q, NULL);
412 fixed_queue_free(p_ccb->rsp_q, NULL);
413 memset(p_ccb, 0, sizeof(tAVDT_CCB));
414 }
415
416 /*******************************************************************************
417 **
418 ** Function avdt_ccb_to_idx
419 **
420 ** Description Given a pointer to an ccb, return its index.
421 **
422 **
423 ** Returns Index of ccb.
424 **
425 *******************************************************************************/
avdt_ccb_to_idx(tAVDT_CCB * p_ccb)426 UINT8 avdt_ccb_to_idx(tAVDT_CCB *p_ccb)
427 {
428 /* use array arithmetic to determine index */
429 return (UINT8) (p_ccb - avdt_cb.ccb);
430 }
431
432 /*******************************************************************************
433 **
434 ** Function avdt_ccb_by_idx
435 **
436 ** Description Return ccb pointer based on ccb index.
437 **
438 **
439 ** Returns pointer to the ccb, or NULL if none found.
440 **
441 *******************************************************************************/
avdt_ccb_by_idx(UINT8 idx)442 tAVDT_CCB *avdt_ccb_by_idx(UINT8 idx)
443 {
444 tAVDT_CCB *p_ccb;
445
446 /* verify index */
447 if (idx < AVDT_NUM_LINKS) {
448 p_ccb = &avdt_cb.ccb[idx];
449 } else {
450 p_ccb = NULL;
451 AVDT_TRACE_WARNING("No ccb for idx %d\n", idx);
452 }
453 return p_ccb;
454 }
455
456 #endif /* #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE) */
457