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