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