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 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 for (size_t i = 0; i < AVDT_NUM_LINKS; i++) {
352 avdtp_cb.ccb[i].Reset(i);
353 }
354 avdtp_cb.p_ccb_act = avdt_ccb_action;
355 }
356
357 /*******************************************************************************
358 *
359 * Function avdt_ccb_event
360 *
361 * Description State machine event handling function for ccb
362 *
363 *
364 * Returns Nothing.
365 *
366 ******************************************************************************/
avdt_ccb_event(AvdtpCcb * p_ccb,uint8_t event,tAVDT_CCB_EVT * p_data)367 void avdt_ccb_event(AvdtpCcb* p_ccb, uint8_t event, tAVDT_CCB_EVT* p_data) {
368 tAVDT_CCB_ST_TBL state_table;
369 uint8_t action;
370 int i;
371
372 #if (AVDT_DEBUG == TRUE)
373 AVDT_TRACE_EVENT("%s: CCB ccb=%d event=%s state=%s p_ccb=%p", __func__,
374 avdt_ccb_to_idx(p_ccb), avdt_ccb_evt_str[event],
375 avdt_ccb_st_str[p_ccb->state], p_ccb);
376 #endif
377
378 /* look up the state table for the current state */
379 state_table = avdt_ccb_st_tbl[p_ccb->state];
380
381 /* set next state */
382 if (p_ccb->state != state_table[event][AVDT_CCB_NEXT_STATE]) {
383 p_ccb->state = state_table[event][AVDT_CCB_NEXT_STATE];
384 }
385
386 /* execute action functions */
387 for (i = 0; i < AVDT_CCB_ACTIONS; i++) {
388 action = state_table[event][i];
389 AVDT_TRACE_DEBUG("%s: event=%s state=%s action=%d", __func__,
390 avdt_ccb_evt_str[event], avdt_ccb_st_str[p_ccb->state],
391 action);
392 if (action != AVDT_CCB_IGNORE) {
393 (*avdtp_cb.p_ccb_act[action])(p_ccb, p_data);
394 } else {
395 break;
396 }
397 }
398 }
399
400 /*******************************************************************************
401 *
402 * Function avdt_ccb_by_bd
403 *
404 * Description This lookup function finds the ccb for a BD address.
405 *
406 *
407 * Returns pointer to the ccb, or NULL if none found.
408 *
409 ******************************************************************************/
avdt_ccb_by_bd(const RawAddress & bd_addr)410 AvdtpCcb* avdt_ccb_by_bd(const RawAddress& bd_addr) {
411 AvdtpCcb* p_ccb = &avdtp_cb.ccb[0];
412 int i;
413
414 for (i = 0; i < AVDT_NUM_LINKS; i++, p_ccb++) {
415 /* if allocated ccb has matching ccb */
416 if (p_ccb->allocated && p_ccb->peer_addr == bd_addr) {
417 break;
418 }
419 }
420
421 if (i == AVDT_NUM_LINKS) {
422 /* if no ccb found */
423 p_ccb = NULL;
424
425 VLOG(1) << "No ccb for addr " << bd_addr;
426 }
427 return p_ccb;
428 }
429
430 /*******************************************************************************
431 *
432 * Function avdt_ccb_alloc
433 *
434 * Description Allocate a channel control block.
435 *
436 *
437 * Returns pointer to the ccb, or NULL if none could be allocated.
438 *
439 ******************************************************************************/
avdt_ccb_alloc(const RawAddress & bd_addr)440 AvdtpCcb* avdt_ccb_alloc(const RawAddress& bd_addr) {
441 // Find available entry
442 AvdtpCcb* p_ccb = &avdtp_cb.ccb[0];
443 for (int i = 0; i < AVDT_NUM_LINKS; i++, p_ccb++) {
444 if (!p_ccb->allocated) {
445 p_ccb->Allocate(bd_addr);
446 AVDT_TRACE_DEBUG("%s: allocated (index %d) for peer %s", __func__, i,
447 bd_addr.ToString().c_str());
448 return p_ccb;
449 }
450 }
451
452 AVDT_TRACE_WARNING("%s: out of AvdtpCcb entries", __func__);
453 return nullptr;
454 }
455
avdt_ccb_alloc_by_channel_index(const RawAddress & bd_addr,uint8_t channel_index)456 AvdtpCcb* avdt_ccb_alloc_by_channel_index(const RawAddress& bd_addr,
457 uint8_t channel_index) {
458 // Allocate the entry for the specified channel index
459 if (channel_index >= AVDT_NUM_LINKS) {
460 AVDT_TRACE_ERROR("%s: peer %s invalid channel index %d (max %d)", __func__,
461 bd_addr.ToString().c_str(), channel_index, AVDT_NUM_LINKS);
462 return nullptr;
463 }
464 AvdtpCcb* p_ccb = &avdtp_cb.ccb[channel_index];
465 if (p_ccb->allocated) {
466 AVDT_TRACE_ERROR("%s: peer %s channel index %d already allocated", __func__,
467 bd_addr.ToString().c_str(), channel_index);
468 return nullptr;
469 }
470 p_ccb->Allocate(bd_addr);
471 AVDT_TRACE_DEBUG("%s: allocated (index %d) peer=%s p_ccb=%p", __func__,
472 channel_index, p_ccb->peer_addr.ToString().c_str(), p_ccb);
473 return p_ccb;
474 }
475
Allocate(const RawAddress & peer_address)476 void AvdtpCcb::Allocate(const RawAddress& peer_address) {
477 ResetCcb();
478 peer_addr = peer_address;
479 cmd_q = fixed_queue_new(SIZE_MAX);
480 rsp_q = fixed_queue_new(SIZE_MAX);
481 idle_ccb_timer = alarm_new("avdtp_ccb.idle_ccb_timer");
482 ret_ccb_timer = alarm_new("avdtp_ccb.ret_ccb_timer");
483 rsp_ccb_timer = alarm_new("avdtp_ccb.rsp_ccb_timer");
484 allocated = true;
485 }
486
487 /*******************************************************************************
488 *
489 * Function avdt_ccb_dealloc
490 *
491 * Description Deallocate a stream control block.
492 *
493 *
494 * Returns void.
495 *
496 ******************************************************************************/
avdt_ccb_dealloc(AvdtpCcb * p_ccb,UNUSED_ATTR tAVDT_CCB_EVT * p_data)497 void avdt_ccb_dealloc(AvdtpCcb* p_ccb, UNUSED_ATTR tAVDT_CCB_EVT* p_data) {
498 AVDT_TRACE_DEBUG("%s: deallocated (index %d) peer=%s p_ccb=%p", __func__,
499 avdt_ccb_to_idx(p_ccb), p_ccb->peer_addr.ToString().c_str(),
500 p_ccb);
501 p_ccb->ResetCcb();
502 }
503
504 /*******************************************************************************
505 *
506 * Function avdt_ccb_to_idx
507 *
508 * Description Given a pointer to an ccb, return its index.
509 *
510 *
511 * Returns Index of ccb.
512 *
513 ******************************************************************************/
avdt_ccb_to_idx(AvdtpCcb * p_ccb)514 uint8_t avdt_ccb_to_idx(AvdtpCcb* p_ccb) {
515 /* use array arithmetic to determine index */
516 return (uint8_t)(p_ccb - avdtp_cb.ccb);
517 }
518
519 /*******************************************************************************
520 *
521 * Function avdt_ccb_by_idx
522 *
523 * Description Return ccb pointer based on ccb index.
524 *
525 *
526 * Returns pointer to the ccb, or NULL if none found.
527 *
528 ******************************************************************************/
avdt_ccb_by_idx(uint8_t idx)529 AvdtpCcb* avdt_ccb_by_idx(uint8_t idx) {
530 AvdtpCcb* p_ccb;
531
532 /* verify index */
533 if (idx < AVDT_NUM_LINKS) {
534 p_ccb = &avdtp_cb.ccb[idx];
535 } else {
536 p_ccb = NULL;
537 AVDT_TRACE_WARNING("No ccb for idx %d", idx);
538 }
539 return p_ccb;
540 }
541