l2c_main.c revision 8fe58875ce67c6e1099e7ba2339dcd2b979491b0
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/********************************************************************************/
41static 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
47tL2C_CB l2cb;
48#endif
49
50/* Temporary - until l2cap implements group management */
51#if (TCS_BCST_SETUP_INCLUDED == TRUE && TCS_INCLUDED == TRUE)
52extern void tcs_proc_bcst_msg( BD_ADDR addr, BT_HDR *p_msg ) ;
53#endif
54/*******************************************************************************
55**
56** Function         l2c_bcst_msg
57**
58** Description
59**
60** Returns          void
61**
62*******************************************************************************/
63void 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
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*******************************************************************************/
119void 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*******************************************************************************/
315static 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, *p_rcb2;
324    BOOLEAN         cfg_rej, pkt_size_rej = FALSE;
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->transport == BT_TRANSPORT_LE)
332        return;
333#endif
334
335    /* Reject the packet if it exceeds the default Signalling Channel MTU */
336    if (pkt_len > L2CAP_DEFAULT_MTU)
337    {
338        /* Core Spec requires a single response to the first command found in a multi-command
339        ** L2cap packet.  If only responses in the packet, then it will be ignored.
340        ** Here we simply mark the bad packet and decide which cmd ID to reject later
341        */
342        pkt_size_rej = TRUE;
343        L2CAP_TRACE_ERROR1 ("L2CAP SIG MTU Pkt Len Exceeded (672) -> pkt_len: %d", pkt_len);
344    }
345
346    p_next_cmd = p;
347    p_pkt_end  = p + pkt_len;
348
349    memset (&cfg_info, 0, sizeof(cfg_info));
350
351    /* An L2CAP packet may contain multiple commands */
352    while (TRUE)
353    {
354        /* Smallest command is 4 bytes */
355        if ((p = p_next_cmd) > (p_pkt_end - 4))
356            break;
357
358        STREAM_TO_UINT8  (cmd_code, p);
359        STREAM_TO_UINT8  (id, p);
360        STREAM_TO_UINT16 (cmd_len, p);
361
362        /* Check command length does not exceed packet length */
363        if ((p_next_cmd = p + cmd_len) > p_pkt_end)
364        {
365            L2CAP_TRACE_WARNING3 ("Command len bad  pkt_len: %d  cmd_len: %d  code: %d",
366                                  pkt_len, cmd_len, cmd_code);
367            break;
368        }
369
370        L2CAP_TRACE_DEBUG3 ("cmd_code: %d, id:%d, cmd_len:%d", cmd_code, id, cmd_len);
371
372        /* Bad L2CAP packet length, look or cmd to reject */
373        if (pkt_size_rej)
374        {
375            /* If command found rejected it and we're done, otherwise keep looking */
376            if (l2c_is_cmd_rejected(cmd_code, id, p_lcb))
377                return;
378            else
379                continue; /* Look for next cmd/response in current packet */
380        }
381
382        switch (cmd_code)
383        {
384        case L2CAP_CMD_REJECT:
385            STREAM_TO_UINT16 (rej_reason, p);
386            if (rej_reason == L2CAP_CMD_REJ_MTU_EXCEEDED)
387            {
388                STREAM_TO_UINT16 (rej_mtu, p);
389                /* What to do with the MTU reject ? We have negotiated an MTU. For now */
390                /* we will ignore it and let a higher protocol timeout take care of it */
391
392                L2CAP_TRACE_WARNING2 ("L2CAP - MTU rej Handle: %d MTU: %d", p_lcb->handle, rej_mtu);
393            }
394            if (rej_reason == L2CAP_CMD_REJ_INVALID_CID)
395            {
396                STREAM_TO_UINT16 (rcid, p);
397                STREAM_TO_UINT16 (lcid, p);
398
399                L2CAP_TRACE_WARNING2 ("L2CAP - rej with CID invalid, LCID: 0x%04x RCID: 0x%04x", lcid, rcid);
400
401                /* Remote CID invalid. Treat as a disconnect */
402                if (((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
403                 && (p_ccb->remote_cid == rcid))
404                {
405                    /* Fake link disconnect - no reply is generated */
406                    l2c_csm_execute (p_ccb, L2CEVT_LP_DISCONNECT_IND, NULL);
407                }
408            }
409
410            /* SonyEricsson Info request Bug workaround (Continue connection) */
411            else if (rej_reason == L2CAP_CMD_REJ_NOT_UNDERSTOOD && p_lcb->w4_info_rsp)
412            {
413                btu_stop_timer (&p_lcb->info_timer_entry);
414
415                p_lcb->w4_info_rsp = FALSE;
416                ci.status = HCI_SUCCESS;
417                memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
418
419                /* For all channels, send the event through their FSMs */
420                for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
421                {
422                    l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
423                }
424            }
425            break;
426
427        case L2CAP_CMD_CONN_REQ:
428            STREAM_TO_UINT16 (con_info.psm, p);
429            STREAM_TO_UINT16 (rcid, p);
430            if ((p_rcb = l2cu_find_rcb_by_psm (con_info.psm)) == NULL)
431            {
432                L2CAP_TRACE_WARNING1 ("L2CAP - rcvd conn req for unknown PSM: %d", con_info.psm);
433                l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
434                break;
435            }
436            else
437            {
438                if (!p_rcb->api.pL2CA_ConnectInd_Cb)
439                {
440                    L2CAP_TRACE_WARNING1 ("L2CAP - rcvd conn req for outgoing-only connection PSM: %d", con_info.psm);
441                    l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
442                    break;
443                }
444            }
445            if ((p_ccb = l2cu_allocate_ccb (p_lcb, 0)) == NULL)
446            {
447                L2CAP_TRACE_ERROR0 ("L2CAP - unable to allocate CCB");
448                l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_RESOURCES);
449                break;
450            }
451            p_ccb->remote_id = id;
452            p_ccb->p_rcb = p_rcb;
453            p_ccb->remote_cid = rcid;
454
455            l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_REQ, &con_info);
456            break;
457
458        case L2CAP_CMD_CONN_RSP:
459            STREAM_TO_UINT16 (con_info.remote_cid, p);
460            STREAM_TO_UINT16 (lcid, p);
461            STREAM_TO_UINT16 (con_info.l2cap_result, p);
462            STREAM_TO_UINT16 (con_info.l2cap_status, p);
463
464            if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) == NULL)
465            {
466                L2CAP_TRACE_WARNING2 ("L2CAP - no CCB for conn rsp, LCID: %d RCID: %d",
467                                      lcid, con_info.remote_cid);
468                break;
469            }
470            if (p_ccb->local_id != id)
471            {
472                L2CAP_TRACE_WARNING2 ("L2CAP - con rsp - bad ID. Exp: %d Got: %d",
473                                      p_ccb->local_id, id);
474                break;
475            }
476
477            if (con_info.l2cap_result == L2CAP_CONN_OK)
478                l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP, &con_info);
479            else if (con_info.l2cap_result == L2CAP_CONN_PENDING)
480                l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_PND, &con_info);
481            else
482                l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
483
484            break;
485
486        case L2CAP_CMD_CONFIG_REQ:
487            p_cfg_end = p + cmd_len;
488            cfg_rej = FALSE;
489            cfg_rej_len = 0;
490
491            STREAM_TO_UINT16 (lcid, p);
492            STREAM_TO_UINT16 (cfg_info.flags, p);
493
494            p_cfg_start = p;
495
496            cfg_info.flush_to_present = cfg_info.mtu_present = cfg_info.qos_present =
497                cfg_info.fcr_present = cfg_info.fcs_present = FALSE;
498
499            while (p < p_cfg_end)
500            {
501                STREAM_TO_UINT8 (cfg_code, p);
502                STREAM_TO_UINT8 (cfg_len, p);
503
504                switch (cfg_code & 0x7F)
505                {
506                case L2CAP_CFG_TYPE_MTU:
507                    cfg_info.mtu_present = TRUE;
508                    STREAM_TO_UINT16 (cfg_info.mtu, p);
509                    break;
510
511                case L2CAP_CFG_TYPE_FLUSH_TOUT:
512                    cfg_info.flush_to_present = TRUE;
513                    STREAM_TO_UINT16 (cfg_info.flush_to, p);
514                    break;
515
516                case L2CAP_CFG_TYPE_QOS:
517                    cfg_info.qos_present = TRUE;
518                    STREAM_TO_UINT8  (cfg_info.qos.qos_flags, p);
519                    STREAM_TO_UINT8  (cfg_info.qos.service_type, p);
520                    STREAM_TO_UINT32 (cfg_info.qos.token_rate, p);
521                    STREAM_TO_UINT32 (cfg_info.qos.token_bucket_size, p);
522                    STREAM_TO_UINT32 (cfg_info.qos.peak_bandwidth, p);
523                    STREAM_TO_UINT32 (cfg_info.qos.latency, p);
524                    STREAM_TO_UINT32 (cfg_info.qos.delay_variation, p);
525                    break;
526
527                case L2CAP_CFG_TYPE_FCR:
528                    cfg_info.fcr_present = TRUE;
529                    STREAM_TO_UINT8 (cfg_info.fcr.mode, p);
530                    STREAM_TO_UINT8 (cfg_info.fcr.tx_win_sz, p);
531                    STREAM_TO_UINT8 (cfg_info.fcr.max_transmit, p);
532                    STREAM_TO_UINT16 (cfg_info.fcr.rtrans_tout, p);
533                    STREAM_TO_UINT16 (cfg_info.fcr.mon_tout, p);
534                    STREAM_TO_UINT16 (cfg_info.fcr.mps, p);
535                    break;
536
537                case L2CAP_CFG_TYPE_FCS:
538                    cfg_info.fcs_present = TRUE;
539                    STREAM_TO_UINT8 (cfg_info.fcs, p);
540                    break;
541
542                case L2CAP_CFG_TYPE_EXT_FLOW:
543                    cfg_info.ext_flow_spec_present = TRUE;
544                    STREAM_TO_UINT8  (cfg_info.ext_flow_spec.id, p);
545                    STREAM_TO_UINT8  (cfg_info.ext_flow_spec.stype, p);
546                    STREAM_TO_UINT16 (cfg_info.ext_flow_spec.max_sdu_size, p);
547                    STREAM_TO_UINT32 (cfg_info.ext_flow_spec.sdu_inter_time, p);
548                    STREAM_TO_UINT32 (cfg_info.ext_flow_spec.access_latency, p);
549                    STREAM_TO_UINT32 (cfg_info.ext_flow_spec.flush_timeout, p);
550                    break;
551
552                default:
553                    /* sanity check option length */
554                    if ((cfg_len + L2CAP_CFG_OPTION_OVERHEAD) <= cmd_len)
555                    {
556                        p += cfg_len;
557                        if ((cfg_code & 0x80) == 0)
558                        {
559                            cfg_rej_len += cfg_len + L2CAP_CFG_OPTION_OVERHEAD;
560                            cfg_rej = TRUE;
561                        }
562                    }
563                    /* bad length; force loop exit */
564                    else
565                    {
566                        p = p_cfg_end;
567                        cfg_rej = TRUE;
568                    }
569                    break;
570                }
571            }
572
573            if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
574            {
575                p_ccb->remote_id = id;
576                if (cfg_rej)
577                {
578                    l2cu_send_peer_config_rej (p_ccb, p_cfg_start, (UINT16) (cmd_len - L2CAP_CONFIG_REQ_LEN), cfg_rej_len);
579                }
580                else
581                {
582                    l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_REQ, &cfg_info);
583                }
584            }
585            else
586            {
587                /* updated spec says send command reject on invalid cid */
588                l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_INVALID_CID, id, 0, 0);
589            }
590            break;
591
592        case L2CAP_CMD_CONFIG_RSP:
593            p_cfg_end = p + cmd_len;
594            STREAM_TO_UINT16 (lcid, p);
595            STREAM_TO_UINT16 (cfg_info.flags, p);
596            STREAM_TO_UINT16 (cfg_info.result, p);
597
598            cfg_info.flush_to_present = cfg_info.mtu_present = cfg_info.qos_present =
599                cfg_info.fcr_present = cfg_info.fcs_present = FALSE;
600
601            while (p < p_cfg_end)
602            {
603                STREAM_TO_UINT8 (cfg_code, p);
604                STREAM_TO_UINT8 (cfg_len, p);
605
606                switch (cfg_code & 0x7F)
607                {
608                case L2CAP_CFG_TYPE_MTU:
609                    cfg_info.mtu_present = TRUE;
610                    STREAM_TO_UINT16 (cfg_info.mtu, p);
611                    break;
612
613                case L2CAP_CFG_TYPE_FLUSH_TOUT:
614                    cfg_info.flush_to_present = TRUE;
615                    STREAM_TO_UINT16 (cfg_info.flush_to, p);
616                    break;
617
618                case L2CAP_CFG_TYPE_QOS:
619                    cfg_info.qos_present = TRUE;
620                    STREAM_TO_UINT8  (cfg_info.qos.qos_flags, p);
621                    STREAM_TO_UINT8  (cfg_info.qos.service_type, p);
622                    STREAM_TO_UINT32 (cfg_info.qos.token_rate, p);
623                    STREAM_TO_UINT32 (cfg_info.qos.token_bucket_size, p);
624                    STREAM_TO_UINT32 (cfg_info.qos.peak_bandwidth, p);
625                    STREAM_TO_UINT32 (cfg_info.qos.latency, p);
626                    STREAM_TO_UINT32 (cfg_info.qos.delay_variation, p);
627                    break;
628
629                case L2CAP_CFG_TYPE_FCR:
630                    cfg_info.fcr_present = TRUE;
631                    STREAM_TO_UINT8 (cfg_info.fcr.mode, p);
632                    STREAM_TO_UINT8 (cfg_info.fcr.tx_win_sz, p);
633                    STREAM_TO_UINT8 (cfg_info.fcr.max_transmit, p);
634                    STREAM_TO_UINT16 (cfg_info.fcr.rtrans_tout, p);
635                    STREAM_TO_UINT16 (cfg_info.fcr.mon_tout, p);
636                    STREAM_TO_UINT16 (cfg_info.fcr.mps, p);
637                    break;
638
639                case L2CAP_CFG_TYPE_FCS:
640                    cfg_info.fcs_present = TRUE;
641                    STREAM_TO_UINT8 (cfg_info.fcs, p);
642                    break;
643
644                case L2CAP_CFG_TYPE_EXT_FLOW:
645                    cfg_info.ext_flow_spec_present = TRUE;
646                    STREAM_TO_UINT8  (cfg_info.ext_flow_spec.id, p);
647                    STREAM_TO_UINT8  (cfg_info.ext_flow_spec.stype, p);
648                    STREAM_TO_UINT16 (cfg_info.ext_flow_spec.max_sdu_size, p);
649                    STREAM_TO_UINT32 (cfg_info.ext_flow_spec.sdu_inter_time, p);
650                    STREAM_TO_UINT32 (cfg_info.ext_flow_spec.access_latency, p);
651                    STREAM_TO_UINT32 (cfg_info.ext_flow_spec.flush_timeout, p);
652                    break;
653                }
654            }
655
656            if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
657            {
658                if (p_ccb->local_id != id)
659                {
660                    L2CAP_TRACE_WARNING2 ("L2CAP - cfg rsp - bad ID. Exp: %d Got: %d",
661                                          p_ccb->local_id, id);
662                    break;
663                }
664                if ( (cfg_info.result == L2CAP_CFG_OK) || (cfg_info.result == L2CAP_CFG_PENDING) )
665                    l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_RSP, &cfg_info);
666                else
667                    l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_RSP_NEG, &cfg_info);
668            }
669            else
670            {
671                L2CAP_TRACE_WARNING1 ("L2CAP - rcvd cfg rsp for unknown CID: 0x%04x", lcid);
672            }
673            break;
674
675
676        case L2CAP_CMD_DISC_REQ:
677            STREAM_TO_UINT16 (lcid, p);
678            STREAM_TO_UINT16 (rcid, p);
679
680            if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
681            {
682                if (p_ccb->remote_cid == rcid)
683                {
684                    p_ccb->remote_id = id;
685                    l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_REQ, &con_info);
686                }
687            }
688            else
689                l2cu_send_peer_disc_rsp (p_lcb, id, lcid, rcid);
690
691            break;
692
693        case L2CAP_CMD_DISC_RSP:
694            STREAM_TO_UINT16 (rcid, p);
695            STREAM_TO_UINT16 (lcid, p);
696
697            if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
698            {
699                if ((p_ccb->remote_cid == rcid) && (p_ccb->local_id == id))
700                {
701                    l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_RSP, &con_info);
702                }
703            }
704            break;
705
706        case L2CAP_CMD_ECHO_REQ:
707            l2cu_send_peer_echo_rsp (p_lcb, id, NULL, 0);
708            break;
709
710        case L2CAP_CMD_ECHO_RSP:
711            if (p_lcb->p_echo_rsp_cb)
712            {
713                tL2CA_ECHO_RSP_CB *p_cb = p_lcb->p_echo_rsp_cb;
714
715                /* Zero out the callback in case app immediately calls us again */
716                p_lcb->p_echo_rsp_cb = NULL;
717
718                (*p_cb) (L2CAP_PING_RESULT_OK);
719            }
720            break;
721
722        case L2CAP_CMD_INFO_REQ:
723            STREAM_TO_UINT16 (info_type, p);
724            l2cu_send_peer_info_rsp (p_lcb, id, info_type);
725            break;
726
727        case L2CAP_CMD_INFO_RSP:
728            /* Stop the link connect timer if sent before L2CAP connection is up */
729            if (p_lcb->w4_info_rsp)
730            {
731                btu_stop_timer (&p_lcb->info_timer_entry);
732                p_lcb->w4_info_rsp = FALSE;
733            }
734
735            STREAM_TO_UINT16 (info_type, p);
736            STREAM_TO_UINT16 (result, p);
737
738            p_lcb->info_rx_bits |= (1 << info_type);
739
740            if ( (info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE)
741              && (result == L2CAP_INFO_RESP_RESULT_SUCCESS) )
742            {
743                STREAM_TO_UINT32( p_lcb->peer_ext_fea, p );
744
745#if (L2CAP_NUM_FIXED_CHNLS > 0)
746                if (p_lcb->peer_ext_fea & L2CAP_EXTFEA_FIXED_CHNLS)
747                {
748                    l2cu_send_peer_info_req (p_lcb, L2CAP_FIXED_CHANNELS_INFO_TYPE);
749                    break;
750                }
751                else
752                {
753                    l2cu_process_fixed_chnl_resp (p_lcb);
754                }
755#endif
756            }
757
758
759#if (L2CAP_NUM_FIXED_CHNLS > 0)
760            if (info_type == L2CAP_FIXED_CHANNELS_INFO_TYPE)
761            {
762                if (result == L2CAP_INFO_RESP_RESULT_SUCCESS)
763                {
764                    memcpy (p_lcb->peer_chnl_mask, p, L2CAP_FIXED_CHNL_ARRAY_SIZE);
765                }
766
767                l2cu_process_fixed_chnl_resp (p_lcb);
768            }
769#endif
770#if (L2CAP_UCD_INCLUDED == TRUE)
771            else if (info_type == L2CAP_CONNLESS_MTU_INFO_TYPE)
772            {
773                if (result == L2CAP_INFO_RESP_RESULT_SUCCESS)
774                {
775                    STREAM_TO_UINT16 (p_lcb->ucd_mtu, p);
776                }
777            }
778#endif
779
780            ci.status = HCI_SUCCESS;
781            memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
782            for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
783            {
784                l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
785            }
786            break;
787
788        default:
789            L2CAP_TRACE_WARNING1 ("L2CAP - bad cmd code: %d", cmd_code);
790            l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
791            return;
792        }
793    }
794}
795
796/*******************************************************************************
797**
798** Function         l2c_process_held_packets
799**
800** Description      This function processes any L2CAP packets that arrived before
801**                  the HCI connection complete arrived. It is a work around for
802**                  badly behaved controllers.
803**
804** Returns          void
805**
806*******************************************************************************/
807void l2c_process_held_packets (BOOLEAN timed_out)
808{
809    BT_HDR      *p_buf, *p_buf1;
810    BUFFER_Q    *p_rcv_hold_q = &l2cb.rcv_hold_q;
811
812    if (!p_rcv_hold_q->count)
813        return;
814
815    if (!timed_out)
816    {
817        btu_stop_timer(&l2cb.rcv_hold_tle);
818        L2CAP_TRACE_WARNING0("L2CAP HOLD CONTINUE");
819    }
820    else
821    {
822        L2CAP_TRACE_WARNING0("L2CAP HOLD TIMEOUT");
823    }
824
825    /* Update the timeouts in the hold queue */
826    for (p_buf = (BT_HDR *)GKI_getfirst (p_rcv_hold_q); p_buf; p_buf = p_buf1)
827    {
828        p_buf1 = (BT_HDR *)GKI_getnext (p_buf);
829        if (!timed_out || (!p_buf->layer_specific) || (--p_buf->layer_specific == 0))
830        {
831            GKI_remove_from_queue (p_rcv_hold_q, p_buf);
832            p_buf->layer_specific = 0xFFFF;
833            l2c_rcv_acl_data (p_buf);
834        }
835    }
836
837    /* If anyone still in the queue, restart the timeout */
838    if (p_rcv_hold_q->count)
839        btu_start_timer (&l2cb.rcv_hold_tle, BTU_TTYPE_L2CAP_HOLD, BT_1SEC_TIMEOUT);
840}
841
842
843/*******************************************************************************
844**
845** Function         l2c_init
846**
847** Description      This function is called once at startup to initialize
848**                  all the L2CAP structures
849**
850** Returns          void
851**
852*******************************************************************************/
853void l2c_init (void)
854{
855    INT16  xx;
856
857    memset (&l2cb, 0, sizeof (tL2C_CB));
858    /* the psm is increased by 2 before being used */
859    l2cb.dyn_psm = 0xFFF;
860
861    /* Put all the channel control blocks on the free queue */
862    for (xx = 0; xx < MAX_L2CAP_CHANNELS - 1; xx++)
863    {
864        l2cb.ccb_pool[xx].p_next_ccb = &l2cb.ccb_pool[xx + 1];
865    }
866
867#if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
868    /* it will be set to L2CAP_PKT_START_NON_FLUSHABLE if controller supports */
869    l2cb.non_flushable_pbf = L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT;
870#endif
871
872
873    l2cb.p_free_ccb_first = &l2cb.ccb_pool[0];
874    l2cb.p_free_ccb_last  = &l2cb.ccb_pool[MAX_L2CAP_CHANNELS - 1];
875
876#ifdef L2CAP_DESIRED_LINK_ROLE
877    l2cb.desire_role      = L2CAP_DESIRED_LINK_ROLE;
878#else
879    l2cb.desire_role      = HCI_ROLE_SLAVE;
880#endif
881
882    /* Set the default idle timeout */
883    l2cb.idle_timeout = L2CAP_LINK_INACTIVITY_TOUT;
884
885#if defined(L2CAP_INITIAL_TRACE_LEVEL)
886    l2cb.l2cap_trace_level = L2CAP_INITIAL_TRACE_LEVEL;
887#else
888    l2cb.l2cap_trace_level = BT_TRACE_LEVEL_NONE;    /* No traces */
889#endif
890
891#if L2CAP_CONFORMANCE_TESTING == TRUE
892     /* Conformance testing needs a dynamic response */
893    l2cb.test_info_resp = L2CAP_EXTFEA_SUPPORTED_MASK;
894#endif
895
896    /* Number of ACL buffers to use for high priority channel */
897#if (defined(L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE) && (L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE == TRUE))
898    l2cb.high_pri_min_xmit_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA;
899#endif
900
901}
902
903/*******************************************************************************
904**
905** Function         l2c_process_timeout
906**
907** Description      This function is called when an L2CAP-related timeout occurs
908**
909** Returns          void
910**
911*******************************************************************************/
912void l2c_process_timeout (TIMER_LIST_ENT *p_tle)
913{
914    /* What type of timeout ? */
915    switch (p_tle->event)
916    {
917    case BTU_TTYPE_L2CAP_LINK:
918        l2c_link_timeout ((tL2C_LCB *)p_tle->param);
919        break;
920
921    case BTU_TTYPE_L2CAP_CHNL:
922        l2c_csm_execute (((tL2C_CCB *)p_tle->param), L2CEVT_TIMEOUT, NULL);
923        break;
924
925    case BTU_TTYPE_L2CAP_FCR_ACK:
926        l2c_csm_execute (((tL2C_CCB *)p_tle->param), L2CEVT_ACK_TIMEOUT, NULL);
927        break;
928
929    case BTU_TTYPE_L2CAP_HOLD:
930        /* Update the timeouts in the hold queue */
931        l2c_process_held_packets(TRUE);
932        break;
933
934    case BTU_TTYPE_L2CAP_INFO:
935        l2c_info_timeout((tL2C_LCB *)p_tle->param);
936        break;
937
938#if (BLE_INCLUDED == TRUE)
939    case BTU_TTYPE_L2CAP_END_CONN_UPD:
940        l2c_enable_conn_param_timeout((tL2C_LCB *)p_tle->param);
941        break;
942#endif
943    }
944}
945
946/*******************************************************************************
947**
948** Function         l2c_data_write
949**
950** Description      API functions call this function to write data.
951**
952** Returns          L2CAP_DW_SUCCESS, if data accepted, else FALSE
953**                  L2CAP_DW_CONGESTED, if data accepted and the channel is congested
954**                  L2CAP_DW_FAILED, if error
955**
956*******************************************************************************/
957UINT8 l2c_data_write (UINT16 cid, BT_HDR *p_data, UINT16 flags)
958{
959    tL2C_CCB        *p_ccb;
960
961    /* Find the channel control block. We don't know the link it is on. */
962    if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL)
963    {
964        L2CAP_TRACE_WARNING1 ("L2CAP - no CCB for L2CA_DataWrite, CID: %d", cid);
965        GKI_freebuf (p_data);
966        return (L2CAP_DW_FAILED);
967    }
968
969#ifndef TESTER /* Tester may send any amount of data. otherwise sending message
970                  bigger than mtu size of peer is a violation of protocol */
971    if (p_data->len > p_ccb->peer_cfg.mtu)
972    {
973        L2CAP_TRACE_WARNING1 ("L2CAP - CID: 0x%04x  cannot send message bigger than peer's mtu size", cid);
974        GKI_freebuf (p_data);
975        return (L2CAP_DW_FAILED);
976    }
977#endif
978
979    /* channel based, packet based flushable or non-flushable */
980    p_data->layer_specific = flags;
981
982    /* If already congested, do not accept any more packets */
983    if (p_ccb->cong_sent)
984    {
985        L2CAP_TRACE_ERROR3 ("L2CAP - CID: 0x%04x cannot send, already congested  xmit_hold_q.count: %u  buff_quota: %u",
986                            p_ccb->local_cid, p_ccb->xmit_hold_q.count, p_ccb->buff_quota);
987
988        GKI_freebuf (p_data);
989        return (L2CAP_DW_FAILED);
990    }
991
992    l2c_csm_execute (p_ccb, L2CEVT_L2CA_DATA_WRITE, p_data);
993
994    if (p_ccb->cong_sent)
995        return (L2CAP_DW_CONGESTED);
996
997    return (L2CAP_DW_SUCCESS);
998}
999
1000