1/******************************************************************************
2 *
3 *  Copyright (C) 2009-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 is the implementation file for the MCAP Control channel state
22 *  machine.
23 *
24 ******************************************************************************/
25#include <string.h>
26
27#include "bt_target.h"
28#include "mca_api.h"
29#include "mca_defs.h"
30#include "mca_int.h"
31#include  "btu.h"
32
33/*****************************************************************************
34** data channel state machine constants and types
35*****************************************************************************/
36enum
37{
38    MCA_CCB_FREE_MSG,
39    MCA_CCB_SND_REQ,
40    MCA_CCB_SND_RSP,
41    MCA_CCB_DO_DISCONN,
42    MCA_CCB_CONG,
43    MCA_CCB_HDL_REQ,
44    MCA_CCB_HDL_RSP,
45    MCA_CCB_LL_OPEN,
46    MCA_CCB_DL_OPEN,
47    MCA_CCB_DEALLOC,
48    MCA_CCB_RSP_TOUT,
49    MCA_CCB_NUM_ACTIONS
50};
51#define MCA_CCB_IGNORE     MCA_CCB_NUM_ACTIONS
52
53/* action function list */
54const tMCA_CCB_ACTION mca_ccb_action[] = {
55    mca_ccb_free_msg,
56    mca_ccb_snd_req,
57    mca_ccb_snd_rsp,
58    mca_ccb_do_disconn,
59    mca_ccb_cong,
60    mca_ccb_hdl_req,
61    mca_ccb_hdl_rsp,
62    mca_ccb_ll_open,
63    mca_ccb_dl_open,
64    mca_ccb_dealloc,
65    mca_ccb_rsp_tout,
66};
67
68/* state table information */
69#define MCA_CCB_ACTIONS            1       /* number of actions */
70#define MCA_CCB_ACT_COL            0       /* position of action function */
71#define MCA_CCB_NEXT_STATE         1       /* position of next state */
72#define MCA_CCB_NUM_COLS           2       /* number of columns in state tables */
73
74/* state table for opening state */
75const UINT8 mca_ccb_st_opening[][MCA_CCB_NUM_COLS] = {
76/* Event                            Action              Next State */
77/* MCA_CCB_API_CONNECT_EVT    */   {MCA_CCB_IGNORE,     MCA_CCB_OPENING_ST},
78/* MCA_CCB_API_DISCONNECT_EVT */   {MCA_CCB_DO_DISCONN, MCA_CCB_CLOSING_ST},
79/* MCA_CCB_API_REQ_EVT        */   {MCA_CCB_FREE_MSG,   MCA_CCB_OPENING_ST},
80/* MCA_CCB_API_RSP_EVT        */   {MCA_CCB_IGNORE,     MCA_CCB_OPENING_ST},
81/* MCA_CCB_MSG_REQ_EVT        */   {MCA_CCB_FREE_MSG,   MCA_CCB_OPENING_ST},
82/* MCA_CCB_MSG_RSP_EVT        */   {MCA_CCB_FREE_MSG,   MCA_CCB_OPENING_ST},
83/* MCA_CCB_DL_OPEN_EVT        */   {MCA_CCB_IGNORE,     MCA_CCB_OPENING_ST},
84/* MCA_CCB_LL_OPEN_EVT        */   {MCA_CCB_LL_OPEN,    MCA_CCB_OPEN_ST},
85/* MCA_CCB_LL_CLOSE_EVT       */   {MCA_CCB_DEALLOC,    MCA_CCB_NULL_ST},
86/* MCA_CCB_LL_CONG_EVT        */   {MCA_CCB_CONG,       MCA_CCB_OPENING_ST},
87/* MCA_CCB_RSP_TOUT_EVT       */   {MCA_CCB_IGNORE,     MCA_CCB_OPENING_ST}
88};
89
90/* state table for open state */
91const UINT8 mca_ccb_st_open[][MCA_CCB_NUM_COLS] = {
92/* Event                            Action              Next State */
93/* MCA_CCB_API_CONNECT_EVT    */   {MCA_CCB_IGNORE,     MCA_CCB_OPEN_ST},
94/* MCA_CCB_API_DISCONNECT_EVT */   {MCA_CCB_DO_DISCONN, MCA_CCB_CLOSING_ST},
95/* MCA_CCB_API_REQ_EVT        */   {MCA_CCB_SND_REQ,    MCA_CCB_OPEN_ST},
96/* MCA_CCB_API_RSP_EVT        */   {MCA_CCB_SND_RSP,    MCA_CCB_OPEN_ST},
97/* MCA_CCB_MSG_REQ_EVT        */   {MCA_CCB_HDL_REQ,    MCA_CCB_OPEN_ST},
98/* MCA_CCB_MSG_RSP_EVT        */   {MCA_CCB_HDL_RSP,    MCA_CCB_OPEN_ST},
99/* MCA_CCB_DL_OPEN_EVT        */   {MCA_CCB_DL_OPEN,    MCA_CCB_OPEN_ST},
100/* MCA_CCB_LL_OPEN_EVT        */   {MCA_CCB_IGNORE,     MCA_CCB_OPEN_ST},
101/* MCA_CCB_LL_CLOSE_EVT       */   {MCA_CCB_DEALLOC,    MCA_CCB_NULL_ST},
102/* MCA_CCB_LL_CONG_EVT        */   {MCA_CCB_CONG,       MCA_CCB_OPEN_ST},
103/* MCA_CCB_RSP_TOUT_EVT       */   {MCA_CCB_RSP_TOUT,   MCA_CCB_OPEN_ST}
104};
105
106/* state table for closing state */
107const UINT8 mca_ccb_st_closing[][MCA_CCB_NUM_COLS] = {
108/* Event                            Action              Next State */
109/* MCA_CCB_API_CONNECT_EVT    */   {MCA_CCB_IGNORE,     MCA_CCB_CLOSING_ST},
110/* MCA_CCB_API_DISCONNECT_EVT */   {MCA_CCB_IGNORE,     MCA_CCB_CLOSING_ST},
111/* MCA_CCB_API_REQ_EVT        */   {MCA_CCB_FREE_MSG,   MCA_CCB_CLOSING_ST},
112/* MCA_CCB_API_RSP_EVT        */   {MCA_CCB_IGNORE,     MCA_CCB_CLOSING_ST},
113/* MCA_CCB_MSG_REQ_EVT        */   {MCA_CCB_FREE_MSG,   MCA_CCB_CLOSING_ST},
114/* MCA_CCB_MSG_RSP_EVT        */   {MCA_CCB_FREE_MSG,   MCA_CCB_CLOSING_ST},
115/* MCA_CCB_DL_OPEN_EVT        */   {MCA_CCB_IGNORE,     MCA_CCB_CLOSING_ST},
116/* MCA_CCB_LL_OPEN_EVT        */   {MCA_CCB_IGNORE,     MCA_CCB_CLOSING_ST},
117/* MCA_CCB_LL_CLOSE_EVT       */   {MCA_CCB_DEALLOC,    MCA_CCB_NULL_ST},
118/* MCA_CCB_LL_CONG_EVT        */   {MCA_CCB_IGNORE,     MCA_CCB_CLOSING_ST},
119/* MCA_CCB_RSP_TOUT_EVT       */   {MCA_CCB_IGNORE,     MCA_CCB_CLOSING_ST}
120};
121
122/* type for state table */
123typedef const UINT8 (*tMCA_CCB_ST_TBL)[MCA_CCB_NUM_COLS];
124
125/* state table */
126const tMCA_CCB_ST_TBL mca_ccb_st_tbl[] = {
127    mca_ccb_st_opening,
128    mca_ccb_st_open,
129    mca_ccb_st_closing
130};
131
132#if (BT_TRACE_VERBOSE == TRUE)
133/* verbose event strings for trace */
134static const char * const mca_ccb_evt_str[] = {
135    "API_CONNECT_EVT",
136    "API_DISCONNECT_EVT",
137    "API_REQ_EVT",
138    "API_RSP_EVT",
139    "MSG_REQ_EVT",
140    "MSG_RSP_EVT",
141    "DL_OPEN_EVT",
142    "LL_OPEN_EVT",
143    "LL_CLOSE_EVT",
144    "LL_CONG_EVT",
145    "RSP_TOUT_EVT"
146};
147/* verbose state strings for trace */
148static const char * const mca_ccb_st_str[] = {
149    "NULL_ST",
150    "OPENING_ST",
151    "OPEN_ST",
152    "CLOSING_ST"
153};
154#endif
155
156/*******************************************************************************
157**
158** Function         mca_stop_timer
159**
160** Description      This function is stop a MCAP timer
161**
162**                  This function is for use internal to MCAP only.
163**
164** Returns          void
165**
166*******************************************************************************/
167void mca_stop_timer(tMCA_CCB *p_ccb)
168{
169    alarm_cancel(p_ccb->mca_ccb_timer);
170}
171
172/*******************************************************************************
173**
174** Function         mca_ccb_event
175**
176** Description      This function is the CCB state machine main function.
177**                  It uses the state and action function tables to execute
178**                  action functions.
179**
180** Returns          void.
181**
182*******************************************************************************/
183void mca_ccb_event(tMCA_CCB *p_ccb, UINT8 event, tMCA_CCB_EVT *p_data)
184{
185    tMCA_CCB_ST_TBL    state_table;
186    UINT8              action;
187
188#if (BT_TRACE_VERBOSE == TRUE)
189    MCA_TRACE_EVENT("CCB ccb=%d event=%s state=%s", mca_ccb_to_hdl(p_ccb), mca_ccb_evt_str[event], mca_ccb_st_str[p_ccb->state]);
190#else
191    MCA_TRACE_EVENT("CCB ccb=%d event=%d state=%d", mca_ccb_to_hdl(p_ccb), event, p_ccb->state);
192#endif
193
194    /* look up the state table for the current state */
195    state_table = mca_ccb_st_tbl[p_ccb->state - 1];
196
197    /* set next state */
198    p_ccb->state = state_table[event][MCA_CCB_NEXT_STATE];
199
200    /* execute action functions */
201    if ((action = state_table[event][MCA_CCB_ACT_COL]) != MCA_CCB_IGNORE)
202    {
203        (*mca_ccb_action[action])(p_ccb, p_data);
204    }
205}
206
207/*******************************************************************************
208**
209** Function         mca_ccb_by_bd
210**
211** Description      This function looks up the CCB based on the BD address.
212**                  It returns a pointer to the CCB.
213**                  If no CCB is found it returns NULL.
214**
215** Returns          void.
216**
217*******************************************************************************/
218tMCA_CCB *mca_ccb_by_bd(tMCA_HANDLE handle, BD_ADDR bd_addr)
219{
220    tMCA_CCB *p_ccb = NULL;
221    tMCA_RCB *p_rcb = mca_rcb_by_handle(handle);
222    tMCA_CCB *p_ccb_tmp;
223    int       i;
224
225    if (p_rcb)
226    {
227        i = handle-1;
228        p_ccb_tmp = &mca_cb.ccb[i*MCA_NUM_LINKS];
229        for (i=0; i<MCA_NUM_LINKS; i++, p_ccb_tmp++)
230        {
231            if (p_ccb_tmp->state != MCA_CCB_NULL_ST && memcmp(p_ccb_tmp->peer_addr, bd_addr, BD_ADDR_LEN) == 0)
232            {
233                p_ccb = p_ccb_tmp;
234                break;
235            }
236        }
237    }
238    return p_ccb;
239}
240
241/*******************************************************************************
242**
243** Function         mca_ccb_alloc
244**
245** Description      This function allocates a CCB and copies the BD address to
246**                  the CCB.  It returns a pointer to the CCB.  If no CCB can
247**                  be allocated it returns NULL.
248**
249** Returns          void.
250**
251*******************************************************************************/
252tMCA_CCB *mca_ccb_alloc(tMCA_HANDLE handle, BD_ADDR bd_addr)
253{
254    tMCA_CCB *p_ccb = NULL;
255    tMCA_RCB *p_rcb = mca_rcb_by_handle(handle);
256    tMCA_CCB *p_ccb_tmp;
257    int       i;
258
259    MCA_TRACE_DEBUG("mca_ccb_alloc handle:0x%x", handle);
260    if (p_rcb)
261    {
262        i = handle-1;
263        p_ccb_tmp = &mca_cb.ccb[i*MCA_NUM_LINKS];
264        for (i=0; i<MCA_NUM_LINKS; i++, p_ccb_tmp++)
265        {
266            if (p_ccb_tmp->state == MCA_CCB_NULL_ST)
267            {
268                p_ccb_tmp->p_rcb = p_rcb;
269                p_ccb_tmp->mca_ccb_timer = alarm_new("mca.mca_ccb_timer");
270                p_ccb_tmp->state = MCA_CCB_OPENING_ST;
271                p_ccb_tmp->cong  = TRUE;
272                memcpy(p_ccb_tmp->peer_addr, bd_addr, BD_ADDR_LEN);
273                p_ccb = p_ccb_tmp;
274                break;
275            }
276        }
277    }
278    return p_ccb;
279}
280
281
282/*******************************************************************************
283**
284** Function         mca_ccb_dealloc
285**
286** Description      This function deallocates a CCB.
287**
288** Returns          void.
289**
290*******************************************************************************/
291void mca_ccb_dealloc(tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data)
292{
293    tMCA_CTRL   evt_data;
294
295    MCA_TRACE_DEBUG("mca_ccb_dealloc ctrl_vpsm:0x%x", p_ccb->ctrl_vpsm);
296    mca_dcb_close_by_mdl_id (p_ccb, MCA_ALL_MDL_ID);
297    if (p_ccb->ctrl_vpsm)
298    {
299        L2CA_Deregister (p_ccb->ctrl_vpsm);
300    }
301    if (p_ccb->data_vpsm)
302    {
303        L2CA_Deregister (p_ccb->data_vpsm);
304    }
305    osi_free_and_reset((void **)&p_ccb->p_rx_msg);
306    osi_free_and_reset((void **)&p_ccb->p_tx_req);
307    mca_stop_timer(p_ccb);
308
309    if (p_data)
310    {
311        /* non-NULL -> an action function -> report disconnect event */
312        memcpy (evt_data.disconnect_ind.bd_addr, p_ccb->peer_addr, BD_ADDR_LEN);
313        evt_data.disconnect_ind.reason = p_data->close.reason;
314        mca_ccb_report_event(p_ccb, MCA_DISCONNECT_IND_EVT, &evt_data);
315    }
316    mca_free_tc_tbl_by_lcid (p_ccb->lcid);
317    alarm_free(p_ccb->mca_ccb_timer);
318    memset(p_ccb, 0, sizeof(tMCA_CCB));
319}
320
321/*******************************************************************************
322**
323** Function         mca_ccb_to_hdl
324**
325** Description      This function converts a pointer to a CCB to a tMCA_CL
326**                  and returns the value.
327**
328** Returns          void.
329**
330*******************************************************************************/
331tMCA_CL mca_ccb_to_hdl(tMCA_CCB *p_ccb)
332{
333    return (UINT8) (p_ccb - mca_cb.ccb + 1);
334}
335
336/*******************************************************************************
337**
338** Function         mca_ccb_by_hdl
339**
340** Description      This function converts an index value to a CCB.  It returns
341**                  a pointer to the CCB.  If no valid CCB matches the index it
342**                  returns NULL.
343**
344** Returns          void.
345**
346*******************************************************************************/
347tMCA_CCB *mca_ccb_by_hdl(tMCA_CL mcl)
348{
349    tMCA_CCB * p_ccb = NULL;
350    if (mcl && mcl <= MCA_NUM_CCBS && mca_cb.ccb[mcl-1].state)
351        p_ccb = &mca_cb.ccb[mcl-1];
352    return p_ccb;
353}
354
355
356/*******************************************************************************
357**
358** Function         mca_ccb_uses_mdl_id
359**
360** Description      This function checkes if a given mdl_id is in use.
361**
362** Returns          TRUE, if the given mdl_id is currently used in the MCL.
363**
364*******************************************************************************/
365BOOLEAN mca_ccb_uses_mdl_id(tMCA_CCB *p_ccb, UINT16 mdl_id)
366{
367    BOOLEAN uses = FALSE;
368    tMCA_DCB *p_dcb;
369    int       i;
370
371    i = mca_ccb_to_hdl(p_ccb)-1;
372    p_dcb = &mca_cb.dcb[i*MCA_NUM_MDLS];
373    for (i=0; i<MCA_NUM_MDLS; i++, p_dcb++)
374    {
375        if (p_dcb->state != MCA_DCB_NULL_ST && p_dcb->mdl_id == mdl_id)
376        {
377            uses = TRUE;
378            break;
379        }
380    }
381
382    return uses;
383}
384