OSDN Git Service

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