Home | History | Annotate | Download | only in mcap
      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 *****************************************************************************/
     36 enum
     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 */
     54 const 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 */
     75 const 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 */
     91 const 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 */
    107 const 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 */
    123 typedef const UINT8 (*tMCA_CCB_ST_TBL)[MCA_CCB_NUM_COLS];
    124 
    125 /* state table */
    126 const 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 */
    134 static 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 */
    148 static 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 *******************************************************************************/
    167 void mca_stop_timer(tMCA_CCB *p_ccb)
    168 {
    169     if (p_ccb->timer_entry.event == BTU_TTYPE_MCA_CCB_RSP)
    170     {
    171         btu_stop_timer(&p_ccb->timer_entry);
    172         p_ccb->timer_entry.event = 0;
    173     }
    174 }
    175 
    176 /*******************************************************************************
    177 **
    178 ** Function         mca_ccb_event
    179 **
    180 ** Description      This function is the CCB state machine main function.
    181 **                  It uses the state and action function tables to execute
    182 **                  action functions.
    183 **
    184 ** Returns          void.
    185 **
    186 *******************************************************************************/
    187 void mca_ccb_event(tMCA_CCB *p_ccb, UINT8 event, tMCA_CCB_EVT *p_data)
    188 {
    189     tMCA_CCB_ST_TBL    state_table;
    190     UINT8              action;
    191 
    192 #if (BT_TRACE_VERBOSE == TRUE)
    193     MCA_TRACE_EVENT3("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]);
    194 #else
    195     MCA_TRACE_EVENT3("CCB ccb=%d event=%d state=%d", mca_ccb_to_hdl(p_ccb), event, p_ccb->state);
    196 #endif
    197 
    198     /* look up the state table for the current state */
    199     state_table = mca_ccb_st_tbl[p_ccb->state - 1];
    200 
    201     /* set next state */
    202     p_ccb->state = state_table[event][MCA_CCB_NEXT_STATE];
    203 
    204     /* execute action functions */
    205     if ((action = state_table[event][MCA_CCB_ACT_COL]) != MCA_CCB_IGNORE)
    206     {
    207         (*mca_ccb_action[action])(p_ccb, p_data);
    208     }
    209 }
    210 
    211 /*******************************************************************************
    212 **
    213 ** Function         mca_ccb_by_bd
    214 **
    215 ** Description      This function looks up the CCB based on the BD address.
    216 **                  It returns a pointer to the CCB.
    217 **                  If no CCB is found it returns NULL.
    218 **
    219 ** Returns          void.
    220 **
    221 *******************************************************************************/
    222 tMCA_CCB *mca_ccb_by_bd(tMCA_HANDLE handle, BD_ADDR bd_addr)
    223 {
    224     tMCA_CCB *p_ccb = NULL;
    225     tMCA_RCB *p_rcb = mca_rcb_by_handle(handle);
    226     tMCA_CCB *p_ccb_tmp;
    227     int       i;
    228 
    229     if (p_rcb)
    230     {
    231         i = handle-1;
    232         p_ccb_tmp = &mca_cb.ccb[i*MCA_NUM_LINKS];
    233         for (i=0; i<MCA_NUM_LINKS; i++, p_ccb_tmp++)
    234         {
    235             if (p_ccb_tmp->state != MCA_CCB_NULL_ST && memcmp(p_ccb_tmp->peer_addr, bd_addr, BD_ADDR_LEN) == 0)
    236             {
    237                 p_ccb = p_ccb_tmp;
    238                 break;
    239             }
    240         }
    241     }
    242     return p_ccb;
    243 }
    244 
    245 /*******************************************************************************
    246 **
    247 ** Function         mca_ccb_alloc
    248 **
    249 ** Description      This function allocates a CCB and copies the BD address to
    250 **                  the CCB.  It returns a pointer to the CCB.  If no CCB can
    251 **                  be allocated it returns NULL.
    252 **
    253 ** Returns          void.
    254 **
    255 *******************************************************************************/
    256 tMCA_CCB *mca_ccb_alloc(tMCA_HANDLE handle, BD_ADDR bd_addr)
    257 {
    258     tMCA_CCB *p_ccb = NULL;
    259     tMCA_RCB *p_rcb = mca_rcb_by_handle(handle);
    260     tMCA_CCB *p_ccb_tmp;
    261     int       i;
    262 
    263     MCA_TRACE_DEBUG1("mca_ccb_alloc handle:0x%x", handle);
    264     if (p_rcb)
    265     {
    266         i = handle-1;
    267         p_ccb_tmp = &mca_cb.ccb[i*MCA_NUM_LINKS];
    268         for (i=0; i<MCA_NUM_LINKS; i++, p_ccb_tmp++)
    269         {
    270             if (p_ccb_tmp->state == MCA_CCB_NULL_ST)
    271             {
    272                 p_ccb_tmp->p_rcb = p_rcb;
    273                 p_ccb_tmp->state = MCA_CCB_OPENING_ST;
    274                 p_ccb_tmp->cong  = TRUE;
    275                 memcpy(p_ccb_tmp->peer_addr, bd_addr, BD_ADDR_LEN);
    276                 p_ccb = p_ccb_tmp;
    277                 break;
    278             }
    279         }
    280     }
    281     return p_ccb;
    282 }
    283 
    284 
    285 /*******************************************************************************
    286 **
    287 ** Function         mca_ccb_dealloc
    288 **
    289 ** Description      This function deallocates a CCB.
    290 **
    291 ** Returns          void.
    292 **
    293 *******************************************************************************/
    294 void mca_ccb_dealloc(tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data)
    295 {
    296     tMCA_CTRL   evt_data;
    297 
    298     MCA_TRACE_DEBUG1("mca_ccb_dealloc ctrl_vpsm:0x%x", p_ccb->ctrl_vpsm);
    299     mca_dcb_close_by_mdl_id (p_ccb, MCA_ALL_MDL_ID);
    300     if (p_ccb->ctrl_vpsm)
    301     {
    302         L2CA_Deregister (p_ccb->ctrl_vpsm);
    303     }
    304     if (p_ccb->data_vpsm)
    305     {
    306         L2CA_Deregister (p_ccb->data_vpsm);
    307     }
    308     mca_free_buf ((void **)&p_ccb->p_rx_msg);
    309     mca_free_buf ((void **)&p_ccb->p_tx_req);
    310     mca_stop_timer(p_ccb);
    311 
    312     if (p_data)
    313     {
    314         /* non-NULL -> an action function -> report disconnect event */
    315         memcpy (evt_data.disconnect_ind.bd_addr, p_ccb->peer_addr, BD_ADDR_LEN);
    316         evt_data.disconnect_ind.reason = p_data->close.reason;
    317         mca_ccb_report_event(p_ccb, MCA_DISCONNECT_IND_EVT, &evt_data);
    318     }
    319     mca_free_tc_tbl_by_lcid (p_ccb->lcid);
    320     memset (p_ccb, 0, sizeof (tMCA_CCB));
    321 }
    322 
    323 /*******************************************************************************
    324 **
    325 ** Function         mca_ccb_to_hdl
    326 **
    327 ** Description      This function converts a pointer to a CCB to a tMCA_CL
    328 **                  and returns the value.
    329 **
    330 ** Returns          void.
    331 **
    332 *******************************************************************************/
    333 tMCA_CL mca_ccb_to_hdl(tMCA_CCB *p_ccb)
    334 {
    335     return (UINT8) (p_ccb - mca_cb.ccb + 1);
    336 }
    337 
    338 /*******************************************************************************
    339 **
    340 ** Function         mca_ccb_by_hdl
    341 **
    342 ** Description      This function converts an index value to a CCB.  It returns
    343 **                  a pointer to the CCB.  If no valid CCB matches the index it
    344 **                  returns NULL.
    345 **
    346 ** Returns          void.
    347 **
    348 *******************************************************************************/
    349 tMCA_CCB *mca_ccb_by_hdl(tMCA_CL mcl)
    350 {
    351     tMCA_CCB * p_ccb = NULL;
    352     if (mcl && mcl <= MCA_NUM_CCBS && mca_cb.ccb[mcl-1].state)
    353         p_ccb = &mca_cb.ccb[mcl-1];
    354     return p_ccb;
    355 }
    356 
    357 
    358 /*******************************************************************************
    359 **
    360 ** Function         mca_ccb_uses_mdl_id
    361 **
    362 ** Description      This function checkes if a given mdl_id is in use.
    363 **
    364 ** Returns          TRUE, if the given mdl_id is currently used in the MCL.
    365 **
    366 *******************************************************************************/
    367 BOOLEAN mca_ccb_uses_mdl_id(tMCA_CCB *p_ccb, UINT16 mdl_id)
    368 {
    369     BOOLEAN uses = FALSE;
    370     tMCA_DCB *p_dcb;
    371     int       i;
    372 
    373     i = mca_ccb_to_hdl(p_ccb)-1;
    374     p_dcb = &mca_cb.dcb[i*MCA_NUM_MDLS];
    375     for (i=0; i<MCA_NUM_MDLS; i++, p_dcb++)
    376     {
    377         if (p_dcb->state != MCA_DCB_NULL_ST && p_dcb->mdl_id == mdl_id)
    378         {
    379             uses = TRUE;
    380             break;
    381         }
    382     }
    383 
    384     return uses;
    385 }
    386