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