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