1 /******************************************************************************
3 * Copyright (C) 1999-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * This file contains the main L2CAP entry points
23 ******************************************************************************/
31 #include "device/include/controller.h"
32 #include "btcore/include/counter.h"
33 #include "bt_target.h"
41 #include "osi/include/log.h"
43 /********************************************************************************/
44 /* L O C A L F U N C T I O N P R O T O T Y P E S */
45 /********************************************************************************/
46 static void process_l2cap_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len);
48 /********************************************************************************/
49 /* G L O B A L L 2 C A P D A T A */
50 /********************************************************************************/
51 #if L2C_DYNAMIC_MEMORY == FALSE
55 /*******************************************************************************
57 ** Function l2c_bcst_msg
63 *******************************************************************************/
64 void l2c_bcst_msg( BT_HDR *p_buf, UINT16 psm )
68 /* Ensure we have enough space in the buffer for the L2CAP and HCI headers */
69 if (p_buf->offset < L2CAP_BCST_MIN_OFFSET)
71 L2CAP_TRACE_ERROR ("L2CAP - cannot send buffer, offset: %d", p_buf->offset);
76 /* Step back some bytes to add the headers */
77 p_buf->offset -= (HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_BCST_OVERHEAD);
78 p_buf->len += L2CAP_PKT_OVERHEAD + L2CAP_BCST_OVERHEAD;
80 /* Set the pointer to the beginning of the data */
81 p = (UINT8 *)(p_buf + 1) + p_buf->offset;
83 /* First, the HCI transport header */
84 UINT16_TO_STREAM (p, 0x0050 | (L2CAP_PKT_START << 12) | (2 << 14));
86 uint16_t acl_data_size = controller_get_interface()->get_acl_data_size_classic();
87 /* The HCI transport will segment the buffers. */
88 if (p_buf->len > acl_data_size)
90 UINT16_TO_STREAM (p, acl_data_size);
94 UINT16_TO_STREAM (p, p_buf->len);
97 /* Now the L2CAP header */
98 UINT16_TO_STREAM (p, p_buf->len - L2CAP_PKT_OVERHEAD);
99 UINT16_TO_STREAM (p, L2CAP_CONNECTIONLESS_CID);
100 UINT16_TO_STREAM (p, psm);
102 p_buf->len += HCI_DATA_PREAMBLE_SIZE;
104 if (p_buf->len <= controller_get_interface()->get_acl_packet_size_classic())
106 counter_add("l2cap.ch2.tx.bytes", p_buf->len);
107 counter_add("l2cap.ch2.tx.pkts", 1);
109 bte_main_hci_send(p_buf, BT_EVT_TO_LM_HCI_ACL);
114 /*******************************************************************************
116 ** Function l2c_rcv_acl_data
118 ** Description This function is called from the HCI Interface when an ACL
119 ** data packet is received.
123 *******************************************************************************/
124 void l2c_rcv_acl_data (BT_HDR *p_msg)
126 UINT8 *p = (UINT8 *)(p_msg + 1) + p_msg->offset;
127 UINT16 handle, hci_len;
130 tL2C_CCB *p_ccb = NULL;
131 UINT16 l2cap_len, rcv_cid, psm;
133 /* Extract the handle */
134 STREAM_TO_UINT16 (handle, p);
135 pkt_type = HCID_GET_EVENT (handle);
136 handle = HCID_GET_HANDLE (handle);
138 /* Since the HCI Transport is putting segmented packets back together, we */
139 /* should never get a valid packet with the type set to "continuation" */
140 if (pkt_type != L2CAP_PKT_CONTINUE)
142 /* Find the LCB based on the handle */
143 if ((p_lcb = l2cu_find_lcb_by_handle (handle)) == NULL)
147 /* There is a slight possibility (specifically with USB) that we get an */
148 /* L2CAP connection request before we get the HCI connection complete. */
149 /* So for these types of messages, hold them for up to 2 seconds. */
150 STREAM_TO_UINT16 (hci_len, p);
151 STREAM_TO_UINT16 (l2cap_len, p);
152 STREAM_TO_UINT16 (rcv_cid, p);
153 STREAM_TO_UINT8 (cmd_code, p);
155 if ((p_msg->layer_specific == 0) && (rcv_cid == L2CAP_SIGNALLING_CID)
156 && (cmd_code == L2CAP_CMD_INFO_REQ || cmd_code == L2CAP_CMD_CONN_REQ)) {
157 L2CAP_TRACE_WARNING ("L2CAP - holding ACL for unknown handle:%d ls:%d"
158 " cid:%d opcode:%d cur count:%d", handle, p_msg->layer_specific,
159 rcv_cid, cmd_code, list_length(l2cb.rcv_pending_q));
160 p_msg->layer_specific = 2;
161 list_append(l2cb.rcv_pending_q, p_msg);
163 if (list_length(l2cb.rcv_pending_q) == 1)
164 btu_start_timer (&l2cb.rcv_hold_tle, BTU_TTYPE_L2CAP_HOLD, BT_1SEC_TIMEOUT);
168 L2CAP_TRACE_ERROR ("L2CAP - rcvd ACL for unknown handle:%d ls:%d cid:%d"
169 " opcode:%d cur count:%d", handle, p_msg->layer_specific, rcv_cid,
170 cmd_code, list_length(l2cb.rcv_pending_q));
178 L2CAP_TRACE_WARNING ("L2CAP - expected pkt start or complete, got: %d", pkt_type);
183 /* Extract the length and update the buffer header */
184 STREAM_TO_UINT16 (hci_len, p);
187 if (hci_len < L2CAP_PKT_OVERHEAD) {
188 /* Must receive at least the L2CAP length and CID */
189 L2CAP_TRACE_WARNING ("L2CAP - got incorrect hci header");
190 android_errorWriteLog(0x534e4554, "34946955");
195 /* Extract the length and CID */
196 STREAM_TO_UINT16 (l2cap_len, p);
197 STREAM_TO_UINT16 (rcv_cid, p);
199 #if BLE_INCLUDED == TRUE
200 /* for BLE channel, always notify connection when ACL data received on the link */
201 if (p_lcb && p_lcb->transport == BT_TRANSPORT_LE && p_lcb->link_state != LST_DISCONNECTING)
202 /* only process fixed channel data as channel open indication when link is not in disconnecting mode */
203 l2cble_notify_le_connection(p_lcb->remote_bd_addr);
206 /* Find the CCB for this CID */
207 if (rcv_cid >= L2CAP_BASE_APPL_CID)
209 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, rcv_cid)) == NULL)
211 L2CAP_TRACE_WARNING ("L2CAP - unknown CID: 0x%04x", rcv_cid);
217 p_msg->len = hci_len - L2CAP_PKT_OVERHEAD;
218 p_msg->offset += L2CAP_PKT_OVERHEAD;
220 if (l2cap_len != p_msg->len)
222 L2CAP_TRACE_WARNING ("L2CAP - bad length in pkt. Exp: %d Act: %d",
223 l2cap_len, p_msg->len);
229 /* Send the data through the channel state machine */
230 if (rcv_cid == L2CAP_SIGNALLING_CID)
232 counter_add("l2cap.sig.rx.bytes", l2cap_len);
233 counter_add("l2cap.sig.rx.pkts", 1);
234 process_l2cap_cmd (p_lcb, p, l2cap_len);
237 else if (rcv_cid == L2CAP_CONNECTIONLESS_CID)
239 counter_add("l2cap.ch2.rx.bytes", l2cap_len);
240 counter_add("l2cap.ch2.rx.pkts", 1);
241 /* process_connectionless_data (p_lcb); */
242 STREAM_TO_UINT16 (psm, p);
243 L2CAP_TRACE_DEBUG( "GOT CONNECTIONLESS DATA PSM:%d", psm ) ;
245 #if (L2CAP_UCD_INCLUDED == TRUE)
246 /* if it is not broadcast, check UCD registration */
247 if ( l2c_ucd_check_rx_pkts( p_lcb, p_msg ) )
255 #if (BLE_INCLUDED == TRUE)
256 else if (rcv_cid == L2CAP_BLE_SIGNALLING_CID)
258 counter_add("l2cap.ble.rx.bytes", l2cap_len);
259 counter_add("l2cap.ble.rx.pkts", 1);
260 l2cble_process_sig_cmd (p_lcb, p, l2cap_len);
264 #if (L2CAP_NUM_FIXED_CHNLS > 0)
265 else if ((rcv_cid >= L2CAP_FIRST_FIXED_CHNL) && (rcv_cid <= L2CAP_LAST_FIXED_CHNL) &&
266 (l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb != NULL) )
268 counter_add("l2cap.fix.rx.bytes", l2cap_len);
269 counter_add("l2cap.fix.rx.pkts", 1);
270 /* If no CCB for this channel, allocate one */
272 /* only process fixed channel data when link is open or wait for data indication */
273 (p_lcb->link_state != LST_DISCONNECTING) &&
274 l2cu_initialize_fixed_ccb (p_lcb, rcv_cid, &l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
276 p_ccb = p_lcb->p_fixed_ccbs[rcv_cid - L2CAP_FIRST_FIXED_CHNL];
278 if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE)
279 l2c_fcr_proc_pdu (p_ccb, p_msg);
281 (*l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb)
282 (rcv_cid, p_lcb->remote_bd_addr, p_msg);
291 counter_add("l2cap.dyn.rx.bytes", l2cap_len);
292 counter_add("l2cap.dyn.rx.pkts", 1);
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);
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);
312 /*******************************************************************************
314 ** Function process_l2cap_cmd
316 ** Description This function is called when a packet is received on the
317 ** L2CAP signalling CID
321 *******************************************************************************/
322 static void process_l2cap_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len)
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;
331 BOOLEAN cfg_rej, pkt_size_rej = FALSE;
332 UINT16 cfg_rej_len, cmd_len;
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)
342 /* Reject the packet if it exceeds the default Signalling Channel MTU */
343 if (pkt_len > L2CAP_DEFAULT_MTU)
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
350 L2CAP_TRACE_ERROR ("L2CAP SIG MTU Pkt Len Exceeded (672) -> pkt_len: %d", pkt_len);
354 p_pkt_end = p + pkt_len;
356 memset (&cfg_info, 0, sizeof(cfg_info));
358 /* An L2CAP packet may contain multiple commands */
361 /* Smallest command is 4 bytes */
362 if ((p = p_next_cmd) > (p_pkt_end - 4))
365 STREAM_TO_UINT8 (cmd_code, p);
366 STREAM_TO_UINT8 (id, p);
367 STREAM_TO_UINT16 (cmd_len, p);
369 /* Check command length does not exceed packet length */
370 if ((p_next_cmd = p + cmd_len) > p_pkt_end)
372 L2CAP_TRACE_WARNING ("Command len bad pkt_len: %d cmd_len: %d code: %d",
373 pkt_len, cmd_len, cmd_code);
377 L2CAP_TRACE_DEBUG ("cmd_code: %d, id:%d, cmd_len:%d", cmd_code, id, cmd_len);
379 /* Bad L2CAP packet length, look or cmd to reject */
382 /* If command found rejected it and we're done, otherwise keep looking */
383 if (l2c_is_cmd_rejected(cmd_code, id, p_lcb))
386 continue; /* Look for next cmd/response in current packet */
391 case L2CAP_CMD_REJECT:
392 if (p + 2 > p_next_cmd) {
393 android_errorWriteLog(0x534e4554, "74202041");
396 STREAM_TO_UINT16 (rej_reason, p);
397 if (rej_reason == L2CAP_CMD_REJ_MTU_EXCEEDED)
399 if (p + 2 > p_next_cmd) {
400 android_errorWriteLog(0x534e4554, "74202041");
403 STREAM_TO_UINT16 (rej_mtu, p);
404 /* What to do with the MTU reject ? We have negotiated an MTU. For now */
405 /* we will ignore it and let a higher protocol timeout take care of it */
407 L2CAP_TRACE_WARNING ("L2CAP - MTU rej Handle: %d MTU: %d", p_lcb->handle, rej_mtu);
409 if (rej_reason == L2CAP_CMD_REJ_INVALID_CID)
411 if (p + 4 > p_next_cmd) {
412 android_errorWriteLog(0x534e4554, "74202041");
415 STREAM_TO_UINT16 (rcid, p);
416 STREAM_TO_UINT16 (lcid, p);
418 L2CAP_TRACE_WARNING ("L2CAP - rej with CID invalid, LCID: 0x%04x RCID: 0x%04x", lcid, rcid);
420 /* Remote CID invalid. Treat as a disconnect */
421 if (((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
422 && (p_ccb->remote_cid == rcid))
424 /* Fake link disconnect - no reply is generated */
425 l2c_csm_execute (p_ccb, L2CEVT_LP_DISCONNECT_IND, NULL);
429 /* SonyEricsson Info request Bug workaround (Continue connection) */
430 else if (rej_reason == L2CAP_CMD_REJ_NOT_UNDERSTOOD && p_lcb->w4_info_rsp)
432 btu_stop_timer (&p_lcb->info_timer_entry);
434 p_lcb->w4_info_rsp = FALSE;
435 ci.status = HCI_SUCCESS;
436 memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
438 /* For all channels, send the event through their FSMs */
439 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
441 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
446 case L2CAP_CMD_CONN_REQ:
447 if (p + 4 > p_next_cmd) {
448 android_errorWriteLog(0x534e4554, "74202041");
451 STREAM_TO_UINT16 (con_info.psm, p);
452 STREAM_TO_UINT16 (rcid, p);
453 if ((p_rcb = l2cu_find_rcb_by_psm (con_info.psm)) == NULL)
455 L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for unknown PSM: %d", con_info.psm);
456 l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
461 if (!p_rcb->api.pL2CA_ConnectInd_Cb)
463 L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for outgoing-only connection PSM: %d", con_info.psm);
464 l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
468 if ((p_ccb = l2cu_allocate_ccb (p_lcb, 0)) == NULL)
470 L2CAP_TRACE_ERROR ("L2CAP - unable to allocate CCB");
471 l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_RESOURCES);
474 p_ccb->remote_id = id;
475 p_ccb->p_rcb = p_rcb;
476 p_ccb->remote_cid = rcid;
478 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_REQ, &con_info);
481 case L2CAP_CMD_CONN_RSP:
482 if (p + 8 > p_next_cmd) {
483 android_errorWriteLog(0x534e4554, "74202041");
486 STREAM_TO_UINT16 (con_info.remote_cid, p);
487 STREAM_TO_UINT16 (lcid, p);
488 STREAM_TO_UINT16 (con_info.l2cap_result, p);
489 STREAM_TO_UINT16 (con_info.l2cap_status, p);
491 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) == NULL)
493 L2CAP_TRACE_WARNING ("L2CAP - no CCB for conn rsp, LCID: %d RCID: %d",
494 lcid, con_info.remote_cid);
497 if (p_ccb->local_id != id)
499 L2CAP_TRACE_WARNING ("L2CAP - con rsp - bad ID. Exp: %d Got: %d",
500 p_ccb->local_id, id);
504 if (con_info.l2cap_result == L2CAP_CONN_OK)
505 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP, &con_info);
506 else if (con_info.l2cap_result == L2CAP_CONN_PENDING)
507 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_PND, &con_info);
509 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
513 case L2CAP_CMD_CONFIG_REQ:
514 p_cfg_end = p + cmd_len;
518 if (p + 4 > p_next_cmd) {
519 android_errorWriteLog(0x534e4554, "74202041");
522 STREAM_TO_UINT16 (lcid, p);
523 STREAM_TO_UINT16 (cfg_info.flags, p);
527 cfg_info.flush_to_present = cfg_info.mtu_present = cfg_info.qos_present =
528 cfg_info.fcr_present = cfg_info.fcs_present = FALSE;
530 while (p < p_cfg_end)
532 if (p + 2 > p_next_cmd) {
533 android_errorWriteLog(0x534e4554, "74202041");
536 STREAM_TO_UINT8 (cfg_code, p);
537 STREAM_TO_UINT8 (cfg_len, p);
539 switch (cfg_code & 0x7F)
541 case L2CAP_CFG_TYPE_MTU:
542 cfg_info.mtu_present = TRUE;
543 if (p + 2 > p_next_cmd) {
544 android_errorWriteLog(0x534e4554, "74202041");
547 STREAM_TO_UINT16 (cfg_info.mtu, p);
550 case L2CAP_CFG_TYPE_FLUSH_TOUT:
551 cfg_info.flush_to_present = TRUE;
552 if (p + 2 > p_next_cmd) {
553 android_errorWriteLog(0x534e4554, "74202041");
556 STREAM_TO_UINT16 (cfg_info.flush_to, p);
559 case L2CAP_CFG_TYPE_QOS:
560 cfg_info.qos_present = TRUE;
561 if (p + 2 + 5 * 4 > p_next_cmd) {
562 android_errorWriteLog(0x534e4554, "74202041");
565 STREAM_TO_UINT8 (cfg_info.qos.qos_flags, p);
566 STREAM_TO_UINT8 (cfg_info.qos.service_type, p);
567 STREAM_TO_UINT32 (cfg_info.qos.token_rate, p);
568 STREAM_TO_UINT32 (cfg_info.qos.token_bucket_size, p);
569 STREAM_TO_UINT32 (cfg_info.qos.peak_bandwidth, p);
570 STREAM_TO_UINT32 (cfg_info.qos.latency, p);
571 STREAM_TO_UINT32 (cfg_info.qos.delay_variation, p);
574 case L2CAP_CFG_TYPE_FCR:
575 cfg_info.fcr_present = TRUE;
576 if (p + 3 + 3 * 2 > p_next_cmd) {
577 android_errorWriteLog(0x534e4554, "74202041");
580 STREAM_TO_UINT8 (cfg_info.fcr.mode, p);
581 STREAM_TO_UINT8 (cfg_info.fcr.tx_win_sz, p);
582 STREAM_TO_UINT8 (cfg_info.fcr.max_transmit, p);
583 STREAM_TO_UINT16 (cfg_info.fcr.rtrans_tout, p);
584 STREAM_TO_UINT16 (cfg_info.fcr.mon_tout, p);
585 STREAM_TO_UINT16 (cfg_info.fcr.mps, p);
588 case L2CAP_CFG_TYPE_FCS:
589 cfg_info.fcs_present = TRUE;
590 if (p + 1 > p_next_cmd) {
591 android_errorWriteLog(0x534e4554, "74202041");
594 STREAM_TO_UINT8 (cfg_info.fcs, p);
597 case L2CAP_CFG_TYPE_EXT_FLOW:
598 cfg_info.ext_flow_spec_present = TRUE;
599 if (p + 2 + 2 + 3 * 4 > p_next_cmd) {
600 android_errorWriteLog(0x534e4554, "74202041");
603 STREAM_TO_UINT8 (cfg_info.ext_flow_spec.id, p);
604 STREAM_TO_UINT8 (cfg_info.ext_flow_spec.stype, p);
605 STREAM_TO_UINT16 (cfg_info.ext_flow_spec.max_sdu_size, p);
606 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.sdu_inter_time, p);
607 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.access_latency, p);
608 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.flush_timeout, p);
612 /* sanity check option length */
613 if ((cfg_len + L2CAP_CFG_OPTION_OVERHEAD) <= cmd_len)
616 if ((cfg_code & 0x80) == 0)
618 cfg_rej_len += cfg_len + L2CAP_CFG_OPTION_OVERHEAD;
622 /* bad length; force loop exit */
632 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
634 p_ccb->remote_id = id;
637 l2cu_send_peer_config_rej (p_ccb, p_cfg_start, (UINT16) (cmd_len - L2CAP_CONFIG_REQ_LEN), cfg_rej_len);
641 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_REQ, &cfg_info);
646 /* updated spec says send command reject on invalid cid */
647 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_INVALID_CID, id, 0, 0);
651 case L2CAP_CMD_CONFIG_RSP:
652 p_cfg_end = p + cmd_len;
653 if (p + 6 > p_next_cmd) {
654 android_errorWriteLog(0x534e4554, "74202041");
657 STREAM_TO_UINT16 (lcid, p);
658 STREAM_TO_UINT16 (cfg_info.flags, p);
659 STREAM_TO_UINT16 (cfg_info.result, p);
661 cfg_info.flush_to_present = cfg_info.mtu_present = cfg_info.qos_present =
662 cfg_info.fcr_present = cfg_info.fcs_present = FALSE;
664 while (p < p_cfg_end)
666 if (p + 2 > p_next_cmd) {
667 android_errorWriteLog(0x534e4554, "74202041");
670 STREAM_TO_UINT8 (cfg_code, p);
671 STREAM_TO_UINT8 (cfg_len, p);
673 switch (cfg_code & 0x7F)
675 case L2CAP_CFG_TYPE_MTU:
676 cfg_info.mtu_present = TRUE;
677 if (p + 2 > p_next_cmd) {
678 android_errorWriteLog(0x534e4554, "74202041");
681 STREAM_TO_UINT16 (cfg_info.mtu, p);
684 case L2CAP_CFG_TYPE_FLUSH_TOUT:
685 cfg_info.flush_to_present = TRUE;
686 if (p + 2 > p_next_cmd) {
687 android_errorWriteLog(0x534e4554, "74202041");
690 STREAM_TO_UINT16 (cfg_info.flush_to, p);
693 case L2CAP_CFG_TYPE_QOS:
694 cfg_info.qos_present = TRUE;
695 if (p + 2 + 5 * 4 > p_next_cmd) {
696 android_errorWriteLog(0x534e4554, "74202041");
699 STREAM_TO_UINT8 (cfg_info.qos.qos_flags, p);
700 STREAM_TO_UINT8 (cfg_info.qos.service_type, p);
701 STREAM_TO_UINT32 (cfg_info.qos.token_rate, p);
702 STREAM_TO_UINT32 (cfg_info.qos.token_bucket_size, p);
703 STREAM_TO_UINT32 (cfg_info.qos.peak_bandwidth, p);
704 STREAM_TO_UINT32 (cfg_info.qos.latency, p);
705 STREAM_TO_UINT32 (cfg_info.qos.delay_variation, p);
708 case L2CAP_CFG_TYPE_FCR:
709 cfg_info.fcr_present = TRUE;
710 if (p + 3 + 3 * 2 > p_next_cmd) {
711 android_errorWriteLog(0x534e4554, "74202041");
714 STREAM_TO_UINT8 (cfg_info.fcr.mode, p);
715 STREAM_TO_UINT8 (cfg_info.fcr.tx_win_sz, p);
716 STREAM_TO_UINT8 (cfg_info.fcr.max_transmit, p);
717 STREAM_TO_UINT16 (cfg_info.fcr.rtrans_tout, p);
718 STREAM_TO_UINT16 (cfg_info.fcr.mon_tout, p);
719 STREAM_TO_UINT16 (cfg_info.fcr.mps, p);
722 case L2CAP_CFG_TYPE_FCS:
723 cfg_info.fcs_present = TRUE;
724 if (p + 1 > p_next_cmd) {
725 android_errorWriteLog(0x534e4554, "74202041");
728 STREAM_TO_UINT8 (cfg_info.fcs, p);
731 case L2CAP_CFG_TYPE_EXT_FLOW:
732 cfg_info.ext_flow_spec_present = TRUE;
733 if (p + 2 + 2 + 3 * 4 > p_next_cmd) {
734 android_errorWriteLog(0x534e4554, "74202041");
737 STREAM_TO_UINT8 (cfg_info.ext_flow_spec.id, p);
738 STREAM_TO_UINT8 (cfg_info.ext_flow_spec.stype, p);
739 STREAM_TO_UINT16 (cfg_info.ext_flow_spec.max_sdu_size, p);
740 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.sdu_inter_time, p);
741 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.access_latency, p);
742 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.flush_timeout, p);
747 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
749 if (p_ccb->local_id != id)
751 L2CAP_TRACE_WARNING ("L2CAP - cfg rsp - bad ID. Exp: %d Got: %d",
752 p_ccb->local_id, id);
755 if ( (cfg_info.result == L2CAP_CFG_OK) || (cfg_info.result == L2CAP_CFG_PENDING) )
756 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_RSP, &cfg_info);
758 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_RSP_NEG, &cfg_info);
762 L2CAP_TRACE_WARNING ("L2CAP - rcvd cfg rsp for unknown CID: 0x%04x", lcid);
767 case L2CAP_CMD_DISC_REQ:
768 if (p + 4 > p_next_cmd) {
769 android_errorWriteLog(0x534e4554, "74202041");
772 STREAM_TO_UINT16 (lcid, p);
773 STREAM_TO_UINT16 (rcid, p);
775 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
777 if (p_ccb->remote_cid == rcid)
779 p_ccb->remote_id = id;
780 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_REQ, &con_info);
784 l2cu_send_peer_disc_rsp (p_lcb, id, lcid, rcid);
788 case L2CAP_CMD_DISC_RSP:
789 if (p + 4 > p_next_cmd) {
790 android_errorWriteLog(0x534e4554, "74202041");
793 STREAM_TO_UINT16 (rcid, p);
794 STREAM_TO_UINT16 (lcid, p);
796 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
798 if ((p_ccb->remote_cid == rcid) && (p_ccb->local_id == id))
800 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_RSP, &con_info);
805 case L2CAP_CMD_ECHO_REQ:
806 l2cu_send_peer_echo_rsp (p_lcb, id, NULL, 0);
809 case L2CAP_CMD_ECHO_RSP:
810 if (p_lcb->p_echo_rsp_cb)
812 tL2CA_ECHO_RSP_CB *p_cb = p_lcb->p_echo_rsp_cb;
814 /* Zero out the callback in case app immediately calls us again */
815 p_lcb->p_echo_rsp_cb = NULL;
817 (*p_cb) (L2CAP_PING_RESULT_OK);
821 case L2CAP_CMD_INFO_REQ:
822 if (p + 2 > p_next_cmd) {
823 android_errorWriteLog(0x534e4554, "74202041");
826 STREAM_TO_UINT16 (info_type, p);
827 l2cu_send_peer_info_rsp (p_lcb, id, info_type);
830 case L2CAP_CMD_INFO_RSP:
831 /* Stop the link connect timer if sent before L2CAP connection is up */
832 if (p_lcb->w4_info_rsp)
834 btu_stop_timer (&p_lcb->info_timer_entry);
835 p_lcb->w4_info_rsp = FALSE;
838 if (p + 4 > p_next_cmd) {
839 android_errorWriteLog(0x534e4554, "74202041");
842 STREAM_TO_UINT16 (info_type, p);
843 STREAM_TO_UINT16 (result, p);
845 p_lcb->info_rx_bits |= (1 << info_type);
847 if ( (info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE)
848 && (result == L2CAP_INFO_RESP_RESULT_SUCCESS) )
850 if (p + 4 > p_next_cmd) {
851 android_errorWriteLog(0x534e4554, "74202041");
854 STREAM_TO_UINT32( p_lcb->peer_ext_fea, p );
856 #if (L2CAP_NUM_FIXED_CHNLS > 0)
857 if (p_lcb->peer_ext_fea & L2CAP_EXTFEA_FIXED_CHNLS)
859 l2cu_send_peer_info_req (p_lcb, L2CAP_FIXED_CHANNELS_INFO_TYPE);
864 l2cu_process_fixed_chnl_resp (p_lcb);
870 #if (L2CAP_NUM_FIXED_CHNLS > 0)
871 if (info_type == L2CAP_FIXED_CHANNELS_INFO_TYPE)
873 if (result == L2CAP_INFO_RESP_RESULT_SUCCESS)
875 memcpy (p_lcb->peer_chnl_mask, p, L2CAP_FIXED_CHNL_ARRAY_SIZE);
878 l2cu_process_fixed_chnl_resp (p_lcb);
881 #if (L2CAP_UCD_INCLUDED == TRUE)
882 else if (info_type == L2CAP_CONNLESS_MTU_INFO_TYPE)
884 if (result == L2CAP_INFO_RESP_RESULT_SUCCESS)
886 if (p + 2 > p_next_cmd) {
887 android_errorWriteLog(0x534e4554, "74202041");
890 STREAM_TO_UINT16 (p_lcb->ucd_mtu, p);
895 ci.status = HCI_SUCCESS;
896 memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
897 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
899 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
904 L2CAP_TRACE_WARNING ("L2CAP - bad cmd code: %d", cmd_code);
905 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
911 /*******************************************************************************
913 ** Function l2c_process_held_packets
915 ** Description This function processes any L2CAP packets that arrived before
916 ** the HCI connection complete arrived. It is a work around for
917 ** badly behaved controllers.
921 *******************************************************************************/
922 void l2c_process_held_packets(BOOLEAN timed_out) {
923 if (list_is_empty(l2cb.rcv_pending_q))
927 btu_stop_timer(&l2cb.rcv_hold_tle);
928 L2CAP_TRACE_WARNING("L2CAP HOLD CONTINUE");
930 L2CAP_TRACE_WARNING("L2CAP HOLD TIMEOUT");
933 for (const list_node_t *node = list_begin(l2cb.rcv_pending_q);
934 node != list_end(l2cb.rcv_pending_q);) {
935 BT_HDR *p_buf = list_node(node);
936 node = list_next(node);
937 if (!timed_out || (!p_buf->layer_specific) || (--p_buf->layer_specific == 0)) {
938 list_remove(l2cb.rcv_pending_q, p_buf);
939 p_buf->layer_specific = 0xFFFF;
940 l2c_rcv_acl_data(p_buf);
944 /* If anyone still in the queue, restart the timeout */
945 if (!list_is_empty(l2cb.rcv_pending_q))
946 btu_start_timer (&l2cb.rcv_hold_tle, BTU_TTYPE_L2CAP_HOLD, BT_1SEC_TIMEOUT);
950 /*******************************************************************************
954 ** Description This function is called once at startup to initialize
955 ** all the L2CAP structures
959 *******************************************************************************/
964 memset (&l2cb, 0, sizeof (tL2C_CB));
965 /* the psm is increased by 2 before being used */
966 l2cb.dyn_psm = 0xFFF;
968 /* Put all the channel control blocks on the free queue */
969 for (xx = 0; xx < MAX_L2CAP_CHANNELS - 1; xx++)
971 l2cb.ccb_pool[xx].p_next_ccb = &l2cb.ccb_pool[xx + 1];
974 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
975 /* it will be set to L2CAP_PKT_START_NON_FLUSHABLE if controller supports */
976 l2cb.non_flushable_pbf = L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT;
980 l2cb.p_free_ccb_first = &l2cb.ccb_pool[0];
981 l2cb.p_free_ccb_last = &l2cb.ccb_pool[MAX_L2CAP_CHANNELS - 1];
983 #ifdef L2CAP_DESIRED_LINK_ROLE
984 l2cb.desire_role = L2CAP_DESIRED_LINK_ROLE;
986 l2cb.desire_role = HCI_ROLE_SLAVE;
989 /* Set the default idle timeout */
990 l2cb.idle_timeout = L2CAP_LINK_INACTIVITY_TOUT;
992 #if defined(L2CAP_INITIAL_TRACE_LEVEL)
993 l2cb.l2cap_trace_level = L2CAP_INITIAL_TRACE_LEVEL;
995 l2cb.l2cap_trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
998 #if L2CAP_CONFORMANCE_TESTING == TRUE
999 /* Conformance testing needs a dynamic response */
1000 l2cb.test_info_resp = L2CAP_EXTFEA_SUPPORTED_MASK;
1003 /* Number of ACL buffers to use for high priority channel */
1004 #if (defined(L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE) && (L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE == TRUE))
1005 l2cb.high_pri_min_xmit_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA;
1008 #if BLE_INCLUDED == TRUE
1009 l2cb.l2c_ble_fixed_chnls_mask =
1010 L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
1013 l2cb.rcv_pending_q = list_new(NULL);
1014 if (l2cb.rcv_pending_q == NULL)
1015 LOG_ERROR("%s unable to allocate memory for link layer control block", __func__);
1018 void l2c_free(void) {
1019 list_free(l2cb.rcv_pending_q);
1022 /*******************************************************************************
1024 ** Function l2c_process_timeout
1026 ** Description This function is called when an L2CAP-related timeout occurs
1030 *******************************************************************************/
1031 void l2c_process_timeout (TIMER_LIST_ENT *p_tle)
1033 /* What type of timeout ? */
1034 switch (p_tle->event)
1036 case BTU_TTYPE_L2CAP_LINK:
1037 l2c_link_timeout ((tL2C_LCB *)p_tle->param);
1040 case BTU_TTYPE_L2CAP_CHNL:
1041 l2c_csm_execute (((tL2C_CCB *)p_tle->param), L2CEVT_TIMEOUT, NULL);
1044 case BTU_TTYPE_L2CAP_FCR_ACK:
1045 l2c_csm_execute (((tL2C_CCB *)p_tle->param), L2CEVT_ACK_TIMEOUT, NULL);
1048 case BTU_TTYPE_L2CAP_HOLD:
1049 /* Update the timeouts in the hold queue */
1050 l2c_process_held_packets(TRUE);
1053 case BTU_TTYPE_L2CAP_INFO:
1054 l2c_info_timeout((tL2C_LCB *)p_tle->param);
1059 /*******************************************************************************
1061 ** Function l2c_data_write
1063 ** Description API functions call this function to write data.
1065 ** Returns L2CAP_DW_SUCCESS, if data accepted, else FALSE
1066 ** L2CAP_DW_CONGESTED, if data accepted and the channel is congested
1067 ** L2CAP_DW_FAILED, if error
1069 *******************************************************************************/
1070 UINT8 l2c_data_write (UINT16 cid, BT_HDR *p_data, UINT16 flags)
1074 /* Find the channel control block. We don't know the link it is on. */
1075 if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL)
1077 L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_DataWrite, CID: %d", cid);
1078 GKI_freebuf (p_data);
1079 return (L2CAP_DW_FAILED);
1082 #ifndef TESTER /* Tester may send any amount of data. otherwise sending message
1083 bigger than mtu size of peer is a violation of protocol */
1084 if (p_data->len > p_ccb->peer_cfg.mtu)
1086 L2CAP_TRACE_WARNING ("L2CAP - CID: 0x%04x cannot send message bigger than peer's mtu size", cid);
1087 GKI_freebuf (p_data);
1088 return (L2CAP_DW_FAILED);
1092 /* channel based, packet based flushable or non-flushable */
1093 p_data->layer_specific = flags;
1095 /* If already congested, do not accept any more packets */
1096 if (p_ccb->cong_sent)
1098 L2CAP_TRACE_ERROR ("L2CAP - CID: 0x%04x cannot send, already congested xmit_hold_q.count: %u buff_quota: %u",
1099 p_ccb->local_cid, GKI_queue_length(&p_ccb->xmit_hold_q), p_ccb->buff_quota);
1101 GKI_freebuf (p_data);
1102 return (L2CAP_DW_FAILED);
1105 counter_add("l2cap.dyn.tx.bytes", p_data->len);
1106 counter_add("l2cap.dyn.tx.pkts", 1);
1108 l2c_csm_execute (p_ccb, L2CEVT_L2CA_DATA_WRITE, p_data);
1110 if (p_ccb->cong_sent)
1111 return (L2CAP_DW_CONGESTED);
1113 return (L2CAP_DW_SUCCESS);