Home | History | Annotate | Download | only in l2cap
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 1999-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 file contains the main L2CAP entry points
     22  *
     23  ******************************************************************************/
     24 
     25 #include "bt_target.h"
     26 #include <stdlib.h>
     27 #include <string.h>
     28 #include <stdio.h>
     29 
     30 #include "gki.h"
     31 #include "hcimsgs.h"
     32 #include "l2cdefs.h"
     33 #include "l2c_int.h"
     34 #include "l2c_api.h"
     35 #include "btu.h"
     36 #include "btm_int.h"
     37 
     38 /********************************************************************************/
     39 /*              L O C A L    F U N C T I O N     P R O T O T Y P E S            */
     40 /********************************************************************************/
     41 static void process_l2cap_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len);
     42 
     43 /********************************************************************************/
     44 /*                 G L O B A L      L 2 C A P       D A T A                     */
     45 /********************************************************************************/
     46 #if L2C_DYNAMIC_MEMORY == FALSE
     47 tL2C_CB l2cb;
     48 #endif
     49 
     50 /* Temporary - until l2cap implements group management */
     51 #if (TCS_BCST_SETUP_INCLUDED == TRUE && TCS_INCLUDED == TRUE)
     52 extern void tcs_proc_bcst_msg( BD_ADDR addr, BT_HDR *p_msg ) ;
     53 
     54 /*******************************************************************************
     55 **
     56 ** Function         l2c_bcst_msg
     57 **
     58 ** Description
     59 **
     60 ** Returns          void
     61 **
     62 *******************************************************************************/
     63 void l2c_bcst_msg( BT_HDR *p_buf, UINT16 psm )
     64 {
     65     UINT8       *p;
     66 
     67     /* Ensure we have enough space in the buffer for the L2CAP and HCI headers */
     68     if (p_buf->offset < L2CAP_BCST_MIN_OFFSET)
     69     {
     70         L2CAP_TRACE_ERROR1 ("L2CAP - cannot send buffer, offset: %d", p_buf->offset);
     71         GKI_freebuf (p_buf);
     72         return;
     73     }
     74 
     75     /* Step back some bytes to add the headers */
     76     p_buf->offset -= (HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_BCST_OVERHEAD);
     77     p_buf->len    += L2CAP_PKT_OVERHEAD + L2CAP_BCST_OVERHEAD;
     78 
     79     /* Set the pointer to the beginning of the data */
     80     p = (UINT8 *)(p_buf + 1) + p_buf->offset;
     81 
     82     /* First, the HCI transport header */
     83     UINT16_TO_STREAM (p, 0x0050 | (L2CAP_PKT_START << 12) | (2 << 14));
     84 
     85     /* The HCI transport will segment the buffers. */
     86     if (p_buf->len > btu_cb.hcit_acl_data_size)
     87     {
     88         UINT16_TO_STREAM (p, btu_cb.hcit_acl_data_size);
     89     }
     90     else
     91     {
     92         UINT16_TO_STREAM (p, p_buf->len);
     93     }
     94 
     95     /* Now the L2CAP header */
     96     UINT16_TO_STREAM (p, p_buf->len - L2CAP_PKT_OVERHEAD);
     97     UINT16_TO_STREAM (p, L2CAP_CONNECTIONLESS_CID);
     98     UINT16_TO_STREAM (p, psm);
     99 
    100     p_buf->len += HCI_DATA_PREAMBLE_SIZE;
    101 
    102     if (p_buf->len <= btu_cb.hcit_acl_pkt_size)
    103     {
    104         HCI_ACL_DATA_TO_LOWER (p_buf);
    105     }
    106 }
    107 #endif
    108 
    109 /*******************************************************************************
    110 **
    111 ** Function         l2c_rcv_acl_data
    112 **
    113 ** Description      This function is called from the HCI Interface when an ACL
    114 **                  data packet is received.
    115 **
    116 ** Returns          void
    117 **
    118 *******************************************************************************/
    119 void l2c_rcv_acl_data (BT_HDR *p_msg)
    120 {
    121     UINT8       *p = (UINT8 *)(p_msg + 1) + p_msg->offset;
    122     UINT16      handle, hci_len;
    123     UINT8       pkt_type;
    124     tL2C_LCB    *p_lcb;
    125     tL2C_CCB    *p_ccb = NULL;
    126     UINT16      l2cap_len, rcv_cid, psm;
    127 
    128     /* Extract the handle */
    129     STREAM_TO_UINT16 (handle, p);
    130     pkt_type = HCID_GET_EVENT (handle);
    131     handle   = HCID_GET_HANDLE (handle);
    132 
    133     /* Since the HCI Transport is putting segmented packets back together, we */
    134     /* should never get a valid packet with the type set to "continuation"    */
    135     if (pkt_type != L2CAP_PKT_CONTINUE)
    136     {
    137         /* Find the LCB based on the handle */
    138         if ((p_lcb = l2cu_find_lcb_by_handle (handle)) == NULL)
    139         {
    140             UINT8       cmd_code;
    141 
    142             /* There is a slight possibility (specifically with USB) that we get an */
    143             /* L2CAP connection request before we get the HCI connection complete.  */
    144             /* So for these types of messages, hold them for up to 2 seconds.       */
    145             STREAM_TO_UINT16 (hci_len, p);
    146             STREAM_TO_UINT16 (l2cap_len, p);
    147             STREAM_TO_UINT16 (rcv_cid, p);
    148             STREAM_TO_UINT8  (cmd_code, p);
    149 
    150             if ((p_msg->layer_specific == 0) && (rcv_cid == L2CAP_SIGNALLING_CID)
    151                 && (cmd_code == L2CAP_CMD_INFO_REQ || cmd_code == L2CAP_CMD_CONN_REQ))
    152             {
    153                 L2CAP_TRACE_WARNING5 ("L2CAP - holding ACL for unknown handle:%d ls:%d cid:%d opcode:%d cur count:%d",
    154                                     handle, p_msg->layer_specific, rcv_cid, cmd_code,
    155                                     l2cb.rcv_hold_q.count);
    156                 p_msg->layer_specific = 2;
    157                 GKI_enqueue (&l2cb.rcv_hold_q, p_msg);
    158 
    159                 if (l2cb.rcv_hold_q.count == 1)
    160                     btu_start_timer (&l2cb.rcv_hold_tle, BTU_TTYPE_L2CAP_HOLD, BT_1SEC_TIMEOUT);
    161 
    162                 return;
    163             }
    164             else
    165             {
    166                 L2CAP_TRACE_ERROR5 ("L2CAP - rcvd ACL for unknown handle:%d ls:%d cid:%d opcode:%d cur count:%d",
    167                                     handle, p_msg->layer_specific, rcv_cid, cmd_code, l2cb.rcv_hold_q.count);
    168             }
    169             GKI_freebuf (p_msg);
    170             return;
    171         }
    172     }
    173     else
    174     {
    175         L2CAP_TRACE_WARNING1 ("L2CAP - expected pkt start or complete, got: %d", pkt_type);
    176         GKI_freebuf (p_msg);
    177         return;
    178     }
    179 
    180     /* Extract the length and update the buffer header */
    181     STREAM_TO_UINT16 (hci_len, p);
    182     p_msg->offset += 4;
    183 
    184 #if (L2CAP_HOST_FLOW_CTRL == TRUE)
    185     /* Send ack if we hit the threshold */
    186     if (++p_lcb->link_pkts_unacked >= p_lcb->link_ack_thresh)
    187         btu_hcif_send_host_rdy_for_data();
    188 #endif
    189 
    190     /* Extract the length and CID */
    191     STREAM_TO_UINT16 (l2cap_len, p);
    192     STREAM_TO_UINT16 (rcv_cid, p);
    193 
    194     /* Find the CCB for this CID */
    195     if (rcv_cid >= L2CAP_BASE_APPL_CID)
    196     {
    197         if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, rcv_cid)) == NULL)
    198         {
    199             L2CAP_TRACE_WARNING1 ("L2CAP - unknown CID: 0x%04x", rcv_cid);
    200             GKI_freebuf (p_msg);
    201             return;
    202         }
    203     }
    204 
    205     if (hci_len >= L2CAP_PKT_OVERHEAD)  /* Must receive at least the L2CAP length and CID.*/
    206     {
    207         p_msg->len    = hci_len - L2CAP_PKT_OVERHEAD;
    208         p_msg->offset += L2CAP_PKT_OVERHEAD;
    209     }
    210     else
    211     {
    212         L2CAP_TRACE_WARNING0 ("L2CAP - got incorrect hci header" );
    213         GKI_freebuf (p_msg);
    214         return;
    215     }
    216 
    217     if (l2cap_len != p_msg->len)
    218     {
    219         L2CAP_TRACE_WARNING2 ("L2CAP - bad length in pkt. Exp: %d  Act: %d",
    220                               l2cap_len, p_msg->len);
    221 
    222         GKI_freebuf (p_msg);
    223         return;
    224     }
    225 
    226     /* Send the data through the channel state machine */
    227     if (rcv_cid == L2CAP_SIGNALLING_CID)
    228     {
    229         process_l2cap_cmd (p_lcb, p, l2cap_len);
    230         GKI_freebuf (p_msg);
    231     }
    232     else if (rcv_cid == L2CAP_CONNECTIONLESS_CID)
    233     {
    234         /* process_connectionless_data (p_lcb); */
    235         STREAM_TO_UINT16 (psm, p);
    236         L2CAP_TRACE_DEBUG1( "GOT CONNECTIONLESS DATA PSM:%d", psm ) ;
    237 #if (TCS_BCST_SETUP_INCLUDED == TRUE && TCS_INCLUDED == TRUE)
    238         if (psm == TCS_PSM_INTERCOM || psm == TCS_PSM_CORDLESS)
    239         {
    240             p_msg->offset += L2CAP_BCST_OVERHEAD;
    241             p_msg->len -= L2CAP_BCST_OVERHEAD;
    242             tcs_proc_bcst_msg( p_lcb->remote_bd_addr, p_msg ) ;
    243             GKI_freebuf (p_msg);
    244         }
    245         else
    246 #endif
    247 
    248 #if (L2CAP_UCD_INCLUDED == TRUE)
    249         /* if it is not broadcast, check UCD registration */
    250         if ( l2c_ucd_check_rx_pkts( p_lcb, p_msg ) )
    251         {
    252             /* nothing to do */
    253         }
    254         else
    255 #endif
    256             GKI_freebuf (p_msg);
    257     }
    258 #if (BLE_INCLUDED == TRUE)
    259     else if (rcv_cid == L2CAP_BLE_SIGNALLING_CID)
    260     {
    261         l2cble_process_sig_cmd (p_lcb, p, l2cap_len);
    262         GKI_freebuf (p_msg);
    263     }
    264 #endif
    265 #if (L2CAP_NUM_FIXED_CHNLS > 0)
    266     else if ((rcv_cid >= L2CAP_FIRST_FIXED_CHNL) && (rcv_cid <= L2CAP_LAST_FIXED_CHNL) &&
    267              (l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb != NULL) )
    268     {
    269         /* If no CCB for this channel, allocate one */
    270         if (l2cu_initialize_fixed_ccb (p_lcb, rcv_cid, &l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
    271         {
    272             p_ccb = p_lcb->p_fixed_ccbs[rcv_cid - L2CAP_FIRST_FIXED_CHNL];
    273 
    274             if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE)
    275                 l2c_fcr_proc_pdu (p_ccb, p_msg);
    276             else
    277                 (*l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb)(p_lcb->remote_bd_addr, p_msg);
    278         }
    279         else
    280             GKI_freebuf (p_msg);
    281     }
    282 #endif
    283 
    284     else
    285     {
    286         if (p_ccb == NULL)
    287             GKI_freebuf (p_msg);
    288         else
    289         {
    290             /* Basic mode packets go straight to the state machine */
    291             if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_BASIC_MODE)
    292                 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DATA, p_msg);
    293             else
    294             {
    295                 /* eRTM or streaming mode, so we need to validate states first */
    296                 if ((p_ccb->chnl_state == CST_OPEN) || (p_ccb->chnl_state == CST_CONFIG))
    297                     l2c_fcr_proc_pdu (p_ccb, p_msg);
    298                 else
    299                     GKI_freebuf (p_msg);
    300             }
    301         }
    302     }
    303 }
    304 
    305 /*******************************************************************************
    306 **
    307 ** Function         process_l2cap_cmd
    308 **
    309 ** Description      This function is called when a packet is received on the
    310 **                  L2CAP signalling CID
    311 **
    312 ** Returns          void
    313 **
    314 *******************************************************************************/
    315 static void process_l2cap_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len)
    316 {
    317     UINT8           *p_pkt_end, *p_next_cmd, *p_cfg_end, *p_cfg_start;
    318     UINT8           cmd_code, cfg_code, cfg_len, id;
    319     tL2C_CONN_INFO  con_info;
    320     tL2CAP_CFG_INFO cfg_info;
    321     UINT16          rej_reason, rej_mtu, lcid, rcid, info_type;
    322     tL2C_CCB        *p_ccb;
    323     tL2C_RCB        *p_rcb;
    324     BOOLEAN         cfg_rej;
    325     UINT16          cfg_rej_len, cmd_len;
    326     UINT16          result;
    327     tL2C_CONN_INFO  ci;
    328 
    329 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
    330     /* if l2cap command received in CID 1 on top of an LE link, ignore this command */
    331     if (p_lcb->is_ble_link)
    332         return;
    333 #endif
    334     p_next_cmd = p;
    335     p_pkt_end  = p + pkt_len;
    336 
    337     memset (&cfg_info, 0, sizeof(cfg_info));
    338 
    339     /* An L2CAP packet may contain multiple commands */
    340     while (TRUE)
    341     {
    342         /* Smallest command is 4 bytes */
    343         if ((p = p_next_cmd) > (p_pkt_end - 4))
    344             break;
    345 
    346         STREAM_TO_UINT8  (cmd_code, p);
    347         STREAM_TO_UINT8  (id, p);
    348         STREAM_TO_UINT16 (cmd_len, p);
    349 
    350         /* Check command length does not exceed packet length */
    351         if ((p_next_cmd = p + cmd_len) > p_pkt_end)
    352         {
    353             L2CAP_TRACE_WARNING3 ("Command len bad  pkt_len: %d  cmd_len: %d  code: %d",
    354                                   pkt_len, cmd_len, cmd_code);
    355             break;
    356         }
    357 
    358         switch (cmd_code)
    359         {
    360         case L2CAP_CMD_REJECT:
    361             STREAM_TO_UINT16 (rej_reason, p);
    362             if (rej_reason == L2CAP_CMD_REJ_MTU_EXCEEDED)
    363             {
    364                 STREAM_TO_UINT16 (rej_mtu, p);
    365                 /* What to do with the MTU reject ? We have negotiated an MTU. For now */
    366                 /* we will ignore it and let a higher protocol timeout take care of it */
    367 
    368                 L2CAP_TRACE_WARNING2 ("L2CAP - MTU rej Handle: %d MTU: %d", p_lcb->handle, rej_mtu);
    369             }
    370             if (rej_reason == L2CAP_CMD_REJ_INVALID_CID)
    371             {
    372                 STREAM_TO_UINT16 (rcid, p);
    373                 STREAM_TO_UINT16 (lcid, p);
    374 
    375                 L2CAP_TRACE_WARNING2 ("L2CAP - rej with CID invalid, LCID: 0x%04x RCID: 0x%04x", lcid, rcid);
    376 
    377                 /* Remote CID invalid. Treat as a disconnect */
    378                 if (((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
    379                  && (p_ccb->remote_cid == rcid))
    380                 {
    381                     /* Fake link disconnect - no reply is generated */
    382                     l2c_csm_execute (p_ccb, L2CEVT_LP_DISCONNECT_IND, NULL);
    383                 }
    384             }
    385 
    386             /* SonyEricsson Info request Bug workaround (Continue connection) */
    387             else if (rej_reason == L2CAP_CMD_REJ_NOT_UNDERSTOOD && p_lcb->w4_info_rsp)
    388             {
    389                 btu_stop_timer (&p_lcb->info_timer_entry);
    390 
    391                 p_lcb->w4_info_rsp = FALSE;
    392                 ci.status = HCI_SUCCESS;
    393                 memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
    394 
    395                 /* For all channels, send the event through their FSMs */
    396                 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
    397                 {
    398                     l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
    399                 }
    400             }
    401             break;
    402 
    403         case L2CAP_CMD_CONN_REQ:
    404             STREAM_TO_UINT16 (con_info.psm, p);
    405             STREAM_TO_UINT16 (rcid, p);
    406             if ((p_rcb = l2cu_find_rcb_by_psm (con_info.psm)) == NULL)
    407             {
    408                 L2CAP_TRACE_WARNING1 ("L2CAP - rcvd conn req for unknown PSM: %d", con_info.psm);
    409                 l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
    410                 break;
    411             }
    412             else
    413             {
    414                 if (!p_rcb->api.pL2CA_ConnectInd_Cb)
    415                 {
    416                     L2CAP_TRACE_WARNING1 ("L2CAP - rcvd conn req for outgoing-only connection PSM: %d", con_info.psm);
    417                     l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
    418                     break;
    419                 }
    420             }
    421             if ((p_ccb = l2cu_allocate_ccb (p_lcb, 0)) == NULL)
    422             {
    423                 L2CAP_TRACE_ERROR0 ("L2CAP - unable to allocate CCB");
    424                 l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_RESOURCES);
    425                 break;
    426             }
    427             p_ccb->remote_id = id;
    428             p_ccb->p_rcb = p_rcb;
    429             p_ccb->remote_cid = rcid;
    430 
    431             l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_REQ, &con_info);
    432             break;
    433 
    434         case L2CAP_CMD_CONN_RSP:
    435             STREAM_TO_UINT16 (con_info.remote_cid, p);
    436             STREAM_TO_UINT16 (lcid, p);
    437             STREAM_TO_UINT16 (con_info.l2cap_result, p);
    438             STREAM_TO_UINT16 (con_info.l2cap_status, p);
    439 
    440             if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) == NULL)
    441             {
    442                 L2CAP_TRACE_WARNING2 ("L2CAP - no CCB for conn rsp, LCID: %d RCID: %d",
    443                                       lcid, con_info.remote_cid);
    444                 break;
    445             }
    446             if (p_ccb->local_id != id)
    447             {
    448                 L2CAP_TRACE_WARNING2 ("L2CAP - con rsp - bad ID. Exp: %d Got: %d",
    449                                       p_ccb->local_id, id);
    450                 break;
    451             }
    452 
    453             if (con_info.l2cap_result == L2CAP_CONN_OK)
    454                 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP, &con_info);
    455             else if (con_info.l2cap_result == L2CAP_CONN_PENDING)
    456                 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_PND, &con_info);
    457             else
    458                 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
    459 
    460             break;
    461 
    462         case L2CAP_CMD_CONFIG_REQ:
    463             p_cfg_end = p + cmd_len;
    464             cfg_rej = FALSE;
    465             cfg_rej_len = 0;
    466 
    467             STREAM_TO_UINT16 (lcid, p);
    468             STREAM_TO_UINT16 (cfg_info.flags, p);
    469 
    470             p_cfg_start = p;
    471 
    472             cfg_info.flush_to_present = cfg_info.mtu_present = cfg_info.qos_present =
    473                 cfg_info.fcr_present = cfg_info.fcs_present = FALSE;
    474 
    475             while (p < p_cfg_end)
    476             {
    477                 STREAM_TO_UINT8 (cfg_code, p);
    478                 STREAM_TO_UINT8 (cfg_len, p);
    479 
    480                 switch (cfg_code & 0x7F)
    481                 {
    482                 case L2CAP_CFG_TYPE_MTU:
    483                     cfg_info.mtu_present = TRUE;
    484                     STREAM_TO_UINT16 (cfg_info.mtu, p);
    485                     break;
    486 
    487                 case L2CAP_CFG_TYPE_FLUSH_TOUT:
    488                     cfg_info.flush_to_present = TRUE;
    489                     STREAM_TO_UINT16 (cfg_info.flush_to, p);
    490                     break;
    491 
    492                 case L2CAP_CFG_TYPE_QOS:
    493                     cfg_info.qos_present = TRUE;
    494                     STREAM_TO_UINT8  (cfg_info.qos.qos_flags, p);
    495                     STREAM_TO_UINT8  (cfg_info.qos.service_type, p);
    496                     STREAM_TO_UINT32 (cfg_info.qos.token_rate, p);
    497                     STREAM_TO_UINT32 (cfg_info.qos.token_bucket_size, p);
    498                     STREAM_TO_UINT32 (cfg_info.qos.peak_bandwidth, p);
    499                     STREAM_TO_UINT32 (cfg_info.qos.latency, p);
    500                     STREAM_TO_UINT32 (cfg_info.qos.delay_variation, p);
    501                     break;
    502 
    503                 case L2CAP_CFG_TYPE_FCR:
    504                     cfg_info.fcr_present = TRUE;
    505                     STREAM_TO_UINT8 (cfg_info.fcr.mode, p);
    506                     STREAM_TO_UINT8 (cfg_info.fcr.tx_win_sz, p);
    507                     STREAM_TO_UINT8 (cfg_info.fcr.max_transmit, p);
    508                     STREAM_TO_UINT16 (cfg_info.fcr.rtrans_tout, p);
    509                     STREAM_TO_UINT16 (cfg_info.fcr.mon_tout, p);
    510                     STREAM_TO_UINT16 (cfg_info.fcr.mps, p);
    511                     break;
    512 
    513                 case L2CAP_CFG_TYPE_FCS:
    514                     cfg_info.fcs_present = TRUE;
    515                     STREAM_TO_UINT8 (cfg_info.fcs, p);
    516                     break;
    517 
    518                 case L2CAP_CFG_TYPE_EXT_FLOW:
    519                     cfg_info.ext_flow_spec_present = TRUE;
    520                     STREAM_TO_UINT8  (cfg_info.ext_flow_spec.id, p);
    521                     STREAM_TO_UINT8  (cfg_info.ext_flow_spec.stype, p);
    522                     STREAM_TO_UINT16 (cfg_info.ext_flow_spec.max_sdu_size, p);
    523                     STREAM_TO_UINT32 (cfg_info.ext_flow_spec.sdu_inter_time, p);
    524                     STREAM_TO_UINT32 (cfg_info.ext_flow_spec.access_latency, p);
    525                     STREAM_TO_UINT32 (cfg_info.ext_flow_spec.flush_timeout, p);
    526                     break;
    527 
    528                 default:
    529                     /* sanity check option length */
    530                     if ((cfg_len + L2CAP_CFG_OPTION_OVERHEAD) <= cmd_len)
    531                     {
    532                         p += cfg_len;
    533                         if ((cfg_code & 0x80) == 0)
    534                         {
    535                             cfg_rej_len += cfg_len + L2CAP_CFG_OPTION_OVERHEAD;
    536                             cfg_rej = TRUE;
    537                         }
    538                     }
    539                     /* bad length; force loop exit */
    540                     else
    541                     {
    542                         p = p_cfg_end;
    543                         cfg_rej = TRUE;
    544                     }
    545                     break;
    546                 }
    547             }
    548 
    549             if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
    550             {
    551                 p_ccb->remote_id = id;
    552                 if (cfg_rej)
    553                 {
    554                     l2cu_send_peer_config_rej (p_ccb, p_cfg_start, (UINT16) (cmd_len - L2CAP_CONFIG_REQ_LEN), cfg_rej_len);
    555                 }
    556                 else
    557                 {
    558                     l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_REQ, &cfg_info);
    559                 }
    560             }
    561             else
    562             {
    563                 /* updated spec says send command reject on invalid cid */
    564                 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_INVALID_CID, id, 0, 0);
    565             }
    566             break;
    567 
    568         case L2CAP_CMD_CONFIG_RSP:
    569             p_cfg_end = p + cmd_len;
    570             STREAM_TO_UINT16 (lcid, p);
    571             STREAM_TO_UINT16 (cfg_info.flags, p);
    572             STREAM_TO_UINT16 (cfg_info.result, p);
    573 
    574             cfg_info.flush_to_present = cfg_info.mtu_present = cfg_info.qos_present =
    575                 cfg_info.fcr_present = cfg_info.fcs_present = FALSE;
    576 
    577             while (p < p_cfg_end)
    578             {
    579                 STREAM_TO_UINT8 (cfg_code, p);
    580                 STREAM_TO_UINT8 (cfg_len, p);
    581 
    582                 switch (cfg_code & 0x7F)
    583                 {
    584                 case L2CAP_CFG_TYPE_MTU:
    585                     cfg_info.mtu_present = TRUE;
    586                     STREAM_TO_UINT16 (cfg_info.mtu, p);
    587                     break;
    588 
    589                 case L2CAP_CFG_TYPE_FLUSH_TOUT:
    590                     cfg_info.flush_to_present = TRUE;
    591                     STREAM_TO_UINT16 (cfg_info.flush_to, p);
    592                     break;
    593 
    594                 case L2CAP_CFG_TYPE_QOS:
    595                     cfg_info.qos_present = TRUE;
    596                     STREAM_TO_UINT8  (cfg_info.qos.qos_flags, p);
    597                     STREAM_TO_UINT8  (cfg_info.qos.service_type, p);
    598                     STREAM_TO_UINT32 (cfg_info.qos.token_rate, p);
    599                     STREAM_TO_UINT32 (cfg_info.qos.token_bucket_size, p);
    600                     STREAM_TO_UINT32 (cfg_info.qos.peak_bandwidth, p);
    601                     STREAM_TO_UINT32 (cfg_info.qos.latency, p);
    602                     STREAM_TO_UINT32 (cfg_info.qos.delay_variation, p);
    603                     break;
    604 
    605                 case L2CAP_CFG_TYPE_FCR:
    606                     cfg_info.fcr_present = TRUE;
    607                     STREAM_TO_UINT8 (cfg_info.fcr.mode, p);
    608                     STREAM_TO_UINT8 (cfg_info.fcr.tx_win_sz, p);
    609                     STREAM_TO_UINT8 (cfg_info.fcr.max_transmit, p);
    610                     STREAM_TO_UINT16 (cfg_info.fcr.rtrans_tout, p);
    611                     STREAM_TO_UINT16 (cfg_info.fcr.mon_tout, p);
    612                     STREAM_TO_UINT16 (cfg_info.fcr.mps, p);
    613                     break;
    614 
    615                 case L2CAP_CFG_TYPE_FCS:
    616                     cfg_info.fcs_present = TRUE;
    617                     STREAM_TO_UINT8 (cfg_info.fcs, p);
    618                     break;
    619 
    620                 case L2CAP_CFG_TYPE_EXT_FLOW:
    621                     cfg_info.ext_flow_spec_present = TRUE;
    622                     STREAM_TO_UINT8  (cfg_info.ext_flow_spec.id, p);
    623                     STREAM_TO_UINT8  (cfg_info.ext_flow_spec.stype, p);
    624                     STREAM_TO_UINT16 (cfg_info.ext_flow_spec.max_sdu_size, p);
    625                     STREAM_TO_UINT32 (cfg_info.ext_flow_spec.sdu_inter_time, p);
    626                     STREAM_TO_UINT32 (cfg_info.ext_flow_spec.access_latency, p);
    627                     STREAM_TO_UINT32 (cfg_info.ext_flow_spec.flush_timeout, p);
    628                     break;
    629                 }
    630             }
    631 
    632             if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
    633             {
    634                 if (p_ccb->local_id != id)
    635                 {
    636                     L2CAP_TRACE_WARNING2 ("L2CAP - cfg rsp - bad ID. Exp: %d Got: %d",
    637                                           p_ccb->local_id, id);
    638                     break;
    639                 }
    640                 if ( (cfg_info.result == L2CAP_CFG_OK) || (cfg_info.result == L2CAP_CFG_PENDING) )
    641                     l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_RSP, &cfg_info);
    642                 else
    643                     l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_RSP_NEG, &cfg_info);
    644             }
    645             else
    646             {
    647                 L2CAP_TRACE_WARNING1 ("L2CAP - rcvd cfg rsp for unknown CID: 0x%04x", lcid);
    648             }
    649             break;
    650 
    651 
    652         case L2CAP_CMD_DISC_REQ:
    653             STREAM_TO_UINT16 (lcid, p);
    654             STREAM_TO_UINT16 (rcid, p);
    655 
    656             if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
    657             {
    658                 if (p_ccb->remote_cid == rcid)
    659                 {
    660                     p_ccb->remote_id = id;
    661                     l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_REQ, &con_info);
    662                 }
    663             }
    664             else
    665                 l2cu_send_peer_disc_rsp (p_lcb, id, lcid, rcid);
    666 
    667             break;
    668 
    669         case L2CAP_CMD_DISC_RSP:
    670             STREAM_TO_UINT16 (rcid, p);
    671             STREAM_TO_UINT16 (lcid, p);
    672 
    673             if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
    674             {
    675                 if ((p_ccb->remote_cid == rcid) && (p_ccb->local_id == id))
    676                 {
    677                     l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_RSP, &con_info);
    678                 }
    679             }
    680             break;
    681 
    682         case L2CAP_CMD_ECHO_REQ:
    683 
    684 #if (L2CAP_ENHANCED_FEATURES != 0)
    685             if (!l2cu_check_feature_req (p_lcb, id, p, cmd_len))
    686             {
    687                 if (cmd_len < (btu_cb.hcit_acl_pkt_size - L2CAP_PKT_OVERHEAD
    688                                                         - L2CAP_CMD_OVERHEAD
    689                                                         - L2CAP_ECHO_RSP_LEN
    690                                                         - HCI_DATA_PREAMBLE_SIZE))
    691                 {
    692                     l2cu_send_peer_echo_rsp (p_lcb, id, p, cmd_len);
    693                 }
    694                 else
    695                 {
    696                     l2cu_send_peer_echo_rsp (p_lcb, id, NULL, 0);
    697                 }
    698             }
    699 #else
    700             l2cu_send_peer_echo_rsp (p_lcb, id, NULL, 0);
    701 #endif
    702             break;
    703 
    704         case L2CAP_CMD_ECHO_RSP:
    705 #if (L2CAP_ENHANCED_FEATURES != 0)
    706             l2cu_check_feature_rsp (p_lcb, id, p, cmd_len);
    707 #endif
    708             if (p_lcb->p_echo_rsp_cb)
    709             {
    710                 tL2CA_ECHO_RSP_CB *p_cb = p_lcb->p_echo_rsp_cb;
    711 
    712                 /* Zero out the callback in case app immediately calls us again */
    713                 p_lcb->p_echo_rsp_cb = NULL;
    714 
    715                 (*p_cb) (L2CAP_PING_RESULT_OK);
    716             }
    717             break;
    718 
    719         case L2CAP_CMD_INFO_REQ:
    720             STREAM_TO_UINT16 (info_type, p);
    721             l2cu_send_peer_info_rsp (p_lcb, id, info_type);
    722             break;
    723 
    724         case L2CAP_CMD_INFO_RSP:
    725             /* Stop the link connect timer if sent before L2CAP connection is up */
    726             if (p_lcb->w4_info_rsp)
    727             {
    728                 btu_stop_timer (&p_lcb->info_timer_entry);
    729                 p_lcb->w4_info_rsp = FALSE;
    730             }
    731 
    732             STREAM_TO_UINT16 (info_type, p);
    733             STREAM_TO_UINT16 (result, p);
    734 
    735             p_lcb->info_rx_bits |= (1 << info_type);
    736 
    737             if ( (info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE)
    738               && (result == L2CAP_INFO_RESP_RESULT_SUCCESS) )
    739             {
    740                 STREAM_TO_UINT32( p_lcb->peer_ext_fea, p );
    741 
    742 #if (L2CAP_NUM_FIXED_CHNLS > 0)
    743                 if (p_lcb->peer_ext_fea & L2CAP_EXTFEA_FIXED_CHNLS)
    744                 {
    745                     l2cu_send_peer_info_req (p_lcb, L2CAP_FIXED_CHANNELS_INFO_TYPE);
    746                     break;
    747                 }
    748                 else
    749                 {
    750                     l2cu_process_fixed_chnl_resp (p_lcb);
    751                 }
    752 #endif
    753             }
    754 
    755 
    756 #if (L2CAP_NUM_FIXED_CHNLS > 0)
    757             if (info_type == L2CAP_FIXED_CHANNELS_INFO_TYPE)
    758             {
    759                 if (result == L2CAP_INFO_RESP_RESULT_SUCCESS)
    760                 {
    761                     memcpy (p_lcb->peer_chnl_mask, p, L2CAP_FIXED_CHNL_ARRAY_SIZE);
    762                 }
    763 
    764                 l2cu_process_fixed_chnl_resp (p_lcb);
    765             }
    766 #endif
    767 #if (L2CAP_UCD_INCLUDED == TRUE)
    768             else if (info_type == L2CAP_CONNLESS_MTU_INFO_TYPE)
    769             {
    770                 if (result == L2CAP_INFO_RESP_RESULT_SUCCESS)
    771                 {
    772                     STREAM_TO_UINT16 (p_lcb->ucd_mtu, p);
    773                 }
    774             }
    775 #endif
    776 
    777             ci.status = HCI_SUCCESS;
    778             memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
    779             for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
    780             {
    781                 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
    782             }
    783             break;
    784 
    785         default:
    786             L2CAP_TRACE_WARNING1 ("L2CAP - bad cmd code: %d", cmd_code);
    787             l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
    788             return;
    789         }
    790     }
    791 }
    792 
    793 /*******************************************************************************
    794 **
    795 ** Function         l2c_process_held_packets
    796 **
    797 ** Description      This function processes any L2CAP packets that arrived before
    798 **                  the HCI connection complete arrived. It is a work around for
    799 **                  badly behaved controllers.
    800 **
    801 ** Returns          void
    802 **
    803 *******************************************************************************/
    804 void l2c_process_held_packets (BOOLEAN timed_out)
    805 {
    806     BT_HDR      *p_buf, *p_buf1;
    807     BUFFER_Q    *p_rcv_hold_q = &l2cb.rcv_hold_q;
    808 
    809     if (!p_rcv_hold_q->count)
    810         return;
    811 
    812     if (!timed_out)
    813     {
    814         btu_stop_timer(&l2cb.rcv_hold_tle);
    815         L2CAP_TRACE_WARNING0("L2CAP HOLD CONTINUE");
    816     }
    817     else
    818     {
    819         L2CAP_TRACE_WARNING0("L2CAP HOLD TIMEOUT");
    820     }
    821 
    822     /* Update the timeouts in the hold queue */
    823     for (p_buf = (BT_HDR *)GKI_getfirst (p_rcv_hold_q); p_buf; p_buf = p_buf1)
    824     {
    825         p_buf1 = (BT_HDR *)GKI_getnext (p_buf);
    826         if (!timed_out || (!p_buf->layer_specific) || (--p_buf->layer_specific == 0))
    827         {
    828             GKI_remove_from_queue (p_rcv_hold_q, p_buf);
    829             p_buf->layer_specific = 0xFFFF;
    830             l2c_rcv_acl_data (p_buf);
    831         }
    832     }
    833 
    834     /* If anyone still in the queue, restart the timeout */
    835     if (p_rcv_hold_q->count)
    836         btu_start_timer (&l2cb.rcv_hold_tle, BTU_TTYPE_L2CAP_HOLD, BT_1SEC_TIMEOUT);
    837 }
    838 
    839 
    840 /*******************************************************************************
    841 **
    842 ** Function         l2c_init
    843 **
    844 ** Description      This function is called once at startup to initialize
    845 **                  all the L2CAP structures
    846 **
    847 ** Returns          void
    848 **
    849 *******************************************************************************/
    850 void l2c_init (void)
    851 {
    852     INT16  xx;
    853 
    854     memset (&l2cb, 0, sizeof (tL2C_CB));
    855     /* the psm is increased by 2 before being used */
    856     l2cb.dyn_psm = 0xFFF;
    857 
    858     /* Put all the channel control blocks on the free queue */
    859     for (xx = 0; xx < MAX_L2CAP_CHANNELS - 1; xx++)
    860     {
    861         l2cb.ccb_pool[xx].p_next_ccb = &l2cb.ccb_pool[xx + 1];
    862     }
    863 
    864 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
    865     /* it will be set to L2CAP_PKT_START_NON_FLUSHABLE if controller supports */
    866     l2cb.non_flushable_pbf = L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT;
    867 #endif
    868 
    869 
    870     l2cb.p_free_ccb_first = &l2cb.ccb_pool[0];
    871     l2cb.p_free_ccb_last  = &l2cb.ccb_pool[MAX_L2CAP_CHANNELS - 1];
    872 
    873 #ifdef L2CAP_DESIRED_LINK_ROLE
    874     l2cb.desire_role      = L2CAP_DESIRED_LINK_ROLE;
    875 #else
    876     l2cb.desire_role      = HCI_ROLE_SLAVE;
    877 #endif
    878 
    879     /* Set the default idle timeout */
    880     l2cb.idle_timeout = L2CAP_LINK_INACTIVITY_TOUT;
    881 
    882 #if defined(L2CAP_INITIAL_TRACE_LEVEL)
    883     l2cb.l2cap_trace_level = L2CAP_INITIAL_TRACE_LEVEL;
    884 #else
    885     l2cb.l2cap_trace_level = BT_TRACE_LEVEL_NONE;    /* No traces */
    886 #endif
    887 
    888 #if L2CAP_CONFORMANCE_TESTING == TRUE
    889      /* Conformance testing needs a dynamic response */
    890     l2cb.test_info_resp = L2CAP_EXTFEA_SUPPORTED_MASK;
    891 #endif
    892 
    893     /* Number of ACL buffers to use for high priority channel */
    894 #if (defined(L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE) && (L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE == TRUE))
    895     l2cb.high_pri_min_xmit_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA;
    896 #endif
    897 
    898 }
    899 
    900 /*******************************************************************************
    901 **
    902 ** Function         l2c_process_timeout
    903 **
    904 ** Description      This function is called when an L2CAP-related timeout occurs
    905 **
    906 ** Returns          void
    907 **
    908 *******************************************************************************/
    909 void l2c_process_timeout (TIMER_LIST_ENT *p_tle)
    910 {
    911     /* What type of timeout ? */
    912     switch (p_tle->event)
    913     {
    914     case BTU_TTYPE_L2CAP_LINK:
    915         l2c_link_timeout ((tL2C_LCB *)p_tle->param);
    916         break;
    917 
    918     case BTU_TTYPE_L2CAP_CHNL:
    919         l2c_csm_execute (((tL2C_CCB *)p_tle->param), L2CEVT_TIMEOUT, NULL);
    920         break;
    921 
    922     case BTU_TTYPE_L2CAP_FCR_ACK:
    923         l2c_csm_execute (((tL2C_CCB *)p_tle->param), L2CEVT_ACK_TIMEOUT, NULL);
    924         break;
    925 
    926     case BTU_TTYPE_L2CAP_HOLD:
    927         /* Update the timeouts in the hold queue */
    928         l2c_process_held_packets(TRUE);
    929         break;
    930 
    931     case BTU_TTYPE_L2CAP_INFO:
    932         l2c_info_timeout((tL2C_LCB *)p_tle->param);
    933         break;
    934 
    935     }
    936 }
    937 
    938 /*******************************************************************************
    939 **
    940 ** Function         l2c_data_write
    941 **
    942 ** Description      API functions call this function to write data.
    943 **
    944 ** Returns          L2CAP_DW_SUCCESS, if data accepted, else FALSE
    945 **                  L2CAP_DW_CONGESTED, if data accepted and the channel is congested
    946 **                  L2CAP_DW_FAILED, if error
    947 **
    948 *******************************************************************************/
    949 UINT8 l2c_data_write (UINT16 cid, BT_HDR *p_data, UINT16 flags)
    950 {
    951     tL2C_CCB        *p_ccb;
    952 
    953     /* Find the channel control block. We don't know the link it is on. */
    954     if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL)
    955     {
    956         L2CAP_TRACE_WARNING1 ("L2CAP - no CCB for L2CA_DataWrite, CID: %d", cid);
    957         GKI_freebuf (p_data);
    958         return (L2CAP_DW_FAILED);
    959     }
    960 
    961 #ifndef TESTER /* Tester may send any amount of data. otherwise sending message
    962                   bigger than mtu size of peer is a violation of protocol */
    963     if (p_data->len > p_ccb->peer_cfg.mtu)
    964     {
    965         L2CAP_TRACE_WARNING1 ("L2CAP - CID: 0x%04x  cannot send message bigger than peer's mtu size", cid);
    966         GKI_freebuf (p_data);
    967         return (L2CAP_DW_FAILED);
    968     }
    969 #endif
    970 
    971     /* channel based, packet based flushable or non-flushable */
    972     p_data->layer_specific = flags;
    973 
    974     /* If already congested, do not accept any more packets */
    975     if (p_ccb->cong_sent)
    976     {
    977         L2CAP_TRACE_ERROR3 ("L2CAP - CID: 0x%04x cannot send, already congested  xmit_hold_q.count: %u  buff_quota: %u",
    978                             p_ccb->local_cid, p_ccb->xmit_hold_q.count, p_ccb->buff_quota);
    979 
    980         GKI_freebuf (p_data);
    981         return (L2CAP_DW_FAILED);
    982     }
    983 
    984     l2c_csm_execute (p_ccb, L2CEVT_L2CA_DATA_WRITE, p_data);
    985 
    986     if (p_ccb->cong_sent)
    987         return (L2CAP_DW_CONGESTED);
    988 
    989     return (L2CAP_DW_SUCCESS);
    990 }
    991 
    992