1 /******************************************************************************
3 * Copyright (C) 2009-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * This is the implementation file for the MCAP Data chahnel state machine.
23 ******************************************************************************/
26 #include "bt_target.h"
31 /*****************************************************************************
32 ** data channel state machine constants and types
33 *****************************************************************************/
45 #define MCA_DCB_IGNORE MCA_DCB_NUM_ACTIONS
47 /* action function list */
48 const tMCA_DCB_ACTION mca_dcb_action[] = {
58 /* state table information */
59 #define MCA_DCB_ACTIONS 1 /* number of actions */
60 #define MCA_DCB_ACT_COL 0 /* position of action function */
61 #define MCA_DCB_NEXT_STATE 1 /* position of next state */
62 #define MCA_DCB_NUM_COLS 2 /* number of columns in state tables */
64 /* state table for opening state */
65 const uint8_t mca_dcb_st_opening[][MCA_DCB_NUM_COLS] = {
66 /* Event Action Next State */
67 /* MCA_DCB_API_CLOSE_EVT */ {MCA_DCB_DO_DISCONN, MCA_DCB_CLOSING_ST},
68 /* MCA_DCB_API_WRITE_EVT */ {MCA_DCB_IGNORE, MCA_DCB_OPENING_ST},
69 /* MCA_DCB_TC_OPEN_EVT */ {MCA_DCB_TC_OPEN, MCA_DCB_OPEN_ST},
70 /* MCA_DCB_TC_CLOSE_EVT */ {MCA_DCB_DEALLOC, MCA_DCB_NULL_ST},
71 /* MCA_DCB_TC_CONG_EVT */ {MCA_DCB_CONG, MCA_DCB_OPENING_ST},
72 /* MCA_DCB_TC_DATA_EVT */ {MCA_DCB_FREE_DATA, MCA_DCB_OPENING_ST}
75 /* state table for open state */
76 const uint8_t mca_dcb_st_open[][MCA_DCB_NUM_COLS] = {
77 /* Event Action Next State */
78 /* MCA_DCB_API_CLOSE_EVT */ {MCA_DCB_DO_DISCONN, MCA_DCB_CLOSING_ST},
79 /* MCA_DCB_API_WRITE_EVT */ {MCA_DCB_SND_DATA, MCA_DCB_OPEN_ST},
80 /* MCA_DCB_TC_OPEN_EVT */ {MCA_DCB_IGNORE, MCA_DCB_OPEN_ST},
81 /* MCA_DCB_TC_CLOSE_EVT */ {MCA_DCB_DEALLOC, MCA_DCB_NULL_ST},
82 /* MCA_DCB_TC_CONG_EVT */ {MCA_DCB_CONG, MCA_DCB_OPEN_ST},
83 /* MCA_DCB_TC_DATA_EVT */ {MCA_DCB_HDL_DATA, MCA_DCB_OPEN_ST}
86 /* state table for closing state */
87 const uint8_t mca_dcb_st_closing[][MCA_DCB_NUM_COLS] = {
88 /* Event Action Next State */
89 /* MCA_DCB_API_CLOSE_EVT */ {MCA_DCB_IGNORE, MCA_DCB_CLOSING_ST},
90 /* MCA_DCB_API_WRITE_EVT */ {MCA_DCB_IGNORE, MCA_DCB_CLOSING_ST},
91 /* MCA_DCB_TC_OPEN_EVT */ {MCA_DCB_TC_OPEN, MCA_DCB_OPEN_ST},
92 /* MCA_DCB_TC_CLOSE_EVT */ {MCA_DCB_DEALLOC, MCA_DCB_NULL_ST},
93 /* MCA_DCB_TC_CONG_EVT */ {MCA_DCB_IGNORE, MCA_DCB_CLOSING_ST},
94 /* MCA_DCB_TC_DATA_EVT */ {MCA_DCB_FREE_DATA, MCA_DCB_CLOSING_ST}
97 /* type for state table */
98 typedef const uint8_t (*tMCA_DCB_ST_TBL)[MCA_DCB_NUM_COLS];
101 const tMCA_DCB_ST_TBL mca_dcb_st_tbl[] = {
107 #if (BT_TRACE_VERBOSE == TRUE)
108 /* verbose event strings for trace */
109 const char * const mca_dcb_evt_str[] = {
117 /* verbose state strings for trace */
118 const char * const mca_dcb_st_str[] = {
126 /*******************************************************************************
128 ** Function mca_dcb_event
130 ** Description This function is the DCB state machine main function.
131 ** It uses the state and action function tables to execute
136 *******************************************************************************/
137 void mca_dcb_event(tMCA_DCB *p_dcb, uint8_t event, tMCA_DCB_EVT *p_data)
139 tMCA_DCB_ST_TBL state_table;
144 #if (BT_TRACE_VERBOSE == TRUE)
145 MCA_TRACE_EVENT("DCB dcb=%d event=%s state=%s", mca_dcb_to_hdl(p_dcb), mca_dcb_evt_str[event], mca_dcb_st_str[p_dcb->state]);
147 MCA_TRACE_EVENT("DCB dcb=%d event=%d state=%d", mca_dcb_to_hdl(p_dcb), event, p_dcb->state);
150 /* look up the state table for the current state */
151 state_table = mca_dcb_st_tbl[p_dcb->state - 1];
154 p_dcb->state = state_table[event][MCA_DCB_NEXT_STATE];
156 /* execute action functions */
157 if ((action = state_table[event][MCA_DCB_ACT_COL]) != MCA_DCB_IGNORE)
159 (*mca_dcb_action[action])(p_dcb, p_data);
163 /*******************************************************************************
165 ** Function mca_dcb_alloc
167 ** Description This function is called to allocate an DCB.
168 ** It initializes the DCB with the data passed to the function.
170 ** Returns tMCA_DCB *
172 *******************************************************************************/
173 tMCA_DCB *mca_dcb_alloc(tMCA_CCB*p_ccb, tMCA_DEP dep)
175 tMCA_DCB *p_dcb = NULL, *p_dcb_tmp;
176 tMCA_RCB *p_rcb = p_ccb->p_rcb;
180 if (dep < MCA_NUM_DEPS)
182 p_cs = &p_rcb->dep[dep];
183 i = mca_ccb_to_hdl(p_ccb)-1;
184 p_dcb_tmp = &mca_cb.dcb[i*MCA_NUM_MDLS];
185 /* make sure p_cs->max_mdl is smaller than MCA_NUM_MDLS at MCA_CreateDep */
187 for (i=0; i<max; i++, p_dcb_tmp++)
189 if (p_dcb_tmp->state == MCA_DCB_NULL_ST)
191 p_dcb_tmp->p_ccb = p_ccb;
192 p_dcb_tmp->state = MCA_DCB_OPENING_ST;
193 p_dcb_tmp->cong = true;
194 p_dcb_tmp->p_cs = p_cs;
203 /*******************************************************************************
205 ** Function mca_dep_free_mdl
207 ** Description This function is called to check the number of free mdl for
210 ** Returns the number of free mdl for the given dep
212 *******************************************************************************/
213 uint8_t mca_dep_free_mdl(tMCA_CCB *p_ccb, tMCA_DEP dep)
216 tMCA_RCB *p_rcb = p_ccb->p_rcb;
222 if (dep < MCA_NUM_DEPS)
224 p_cs = &p_rcb->dep[dep];
225 i = mca_ccb_to_hdl(p_ccb)-1;
226 p_dcb = &mca_cb.dcb[i * MCA_NUM_MDLS];
227 /* make sure p_cs->max_mdl is smaller than MCA_NUM_MDLS at MCA_CreateDep */
229 for (i=0; i<max; i++, p_dcb++)
231 if ((p_dcb->state != MCA_DCB_NULL_ST) && (p_dcb->p_cs == p_cs))
241 MCA_TRACE_WARNING("Invalid Dep ID");
247 /*******************************************************************************
249 ** Function mca_dcb_dealloc
251 ** Description This function deallocates an DCB.
255 *******************************************************************************/
256 void mca_dcb_dealloc(tMCA_DCB *p_dcb, tMCA_DCB_EVT *p_data)
258 tMCA_CCB *p_ccb = p_dcb->p_ccb;
259 uint8_t event = MCA_CLOSE_IND_EVT;
262 MCA_TRACE_DEBUG("mca_dcb_dealloc");
263 osi_free_and_reset((void **)&p_dcb->p_data);
266 /* non-NULL -> an action function -> report disconnect event */
267 evt_data.close_cfm.mdl = mca_dcb_to_hdl(p_dcb);
268 evt_data.close_cfm.reason = p_data->close.reason;
269 evt_data.close_cfm.mdl_id = p_dcb->mdl_id;
270 if (p_data->close.param == MCA_INT)
271 event = MCA_CLOSE_CFM_EVT;
272 if (p_data->close.lcid)
273 mca_ccb_report_event(p_ccb, event, &evt_data);
275 mca_free_tc_tbl_by_lcid (p_dcb->lcid);
276 memset (p_dcb, 0, sizeof (tMCA_DCB));
279 /*******************************************************************************
281 ** Function mca_dcb_to_hdl
283 ** Description This function converts a pointer to an DCB to a handle (tMCA_DL).
284 ** It returns the handle.
288 *******************************************************************************/
289 tMCA_DL mca_dcb_to_hdl(tMCA_DCB *p_dcb)
291 return (uint8_t) (p_dcb - mca_cb.dcb + 1);
294 /*******************************************************************************
296 ** Function mca_dcb_by_hdl
298 ** Description This function finds the DCB for a handle (tMCA_DL).
299 ** It returns a pointer to the DCB.
300 ** If no DCB matches the handle it returns NULL.
302 ** Returns tMCA_DCB *
304 *******************************************************************************/
305 tMCA_DCB *mca_dcb_by_hdl(tMCA_DL hdl)
307 tMCA_DCB * p_dcb = NULL;
308 if (hdl && hdl <= MCA_NUM_DCBS && mca_cb.dcb[hdl-1].state)
309 p_dcb = &mca_cb.dcb[hdl-1];
313 /*******************************************************************************
315 ** Function mca_dcb_close_by_mdl_id
317 ** Description This function finds the DCB for a mdl_id and
318 ** disconnect the mdl
322 *******************************************************************************/
323 void mca_dcb_close_by_mdl_id(tMCA_CCB*p_ccb, uint16_t mdl_id)
328 MCA_TRACE_DEBUG("mca_dcb_close_by_mdl_id mdl_id=%d", mdl_id);
329 i = mca_ccb_to_hdl(p_ccb)-1;
330 p_dcb = &mca_cb.dcb[i*MCA_NUM_MDLS];
331 for (i=0; i<MCA_NUM_MDLS; i++, p_dcb++)
335 if (p_dcb->mdl_id == mdl_id)
337 mca_dcb_event(p_dcb, MCA_DCB_API_CLOSE_EVT, NULL);
340 else if (mdl_id == MCA_ALL_MDL_ID)
342 mca_dcb_event(p_dcb, MCA_DCB_API_CLOSE_EVT, NULL);