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 ******************************************************************************/
25 #define LOG_TAG "bt_l2c_main"
33 #include "bt_target.h"
36 #include "device/include/controller.h"
37 #include "bt_common.h"
42 #include "osi/include/log.h"
45 extern fixed_queue_t *btu_general_alarm_queue;
47 /********************************************************************************/
48 /* L O C A L F U N C T I O N P R O T O T Y P E S */
49 /********************************************************************************/
50 static void process_l2cap_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len);
52 /********************************************************************************/
53 /* G L O B A L L 2 C A P D A T A */
54 /********************************************************************************/
55 #if L2C_DYNAMIC_MEMORY == FALSE
59 /*******************************************************************************
61 ** Function l2c_rcv_acl_data
63 ** Description This function is called from the HCI Interface when an ACL
64 ** data packet is received.
68 *******************************************************************************/
69 void l2c_rcv_acl_data (BT_HDR *p_msg)
71 UINT8 *p = (UINT8 *)(p_msg + 1) + p_msg->offset;
72 UINT16 handle, hci_len;
75 tL2C_CCB *p_ccb = NULL;
76 UINT16 l2cap_len, rcv_cid, psm;
79 /* Extract the handle */
80 STREAM_TO_UINT16 (handle, p);
81 pkt_type = HCID_GET_EVENT (handle);
82 handle = HCID_GET_HANDLE (handle);
84 /* Since the HCI Transport is putting segmented packets back together, we */
85 /* should never get a valid packet with the type set to "continuation" */
86 if (pkt_type != L2CAP_PKT_CONTINUE)
88 /* Find the LCB based on the handle */
89 if ((p_lcb = l2cu_find_lcb_by_handle (handle)) == NULL)
93 /* There is a slight possibility (specifically with USB) that we get an */
94 /* L2CAP connection request before we get the HCI connection complete. */
95 /* So for these types of messages, hold them for up to 2 seconds. */
96 STREAM_TO_UINT16 (hci_len, p);
97 STREAM_TO_UINT16 (l2cap_len, p);
98 STREAM_TO_UINT16 (rcv_cid, p);
99 STREAM_TO_UINT8 (cmd_code, p);
101 if ((p_msg->layer_specific == 0) && (rcv_cid == L2CAP_SIGNALLING_CID)
102 && (cmd_code == L2CAP_CMD_INFO_REQ || cmd_code == L2CAP_CMD_CONN_REQ)) {
103 L2CAP_TRACE_WARNING ("L2CAP - holding ACL for unknown handle:%d ls:%d"
104 " cid:%d opcode:%d cur count:%d", handle, p_msg->layer_specific,
105 rcv_cid, cmd_code, list_length(l2cb.rcv_pending_q));
106 p_msg->layer_specific = 2;
107 list_append(l2cb.rcv_pending_q, p_msg);
109 if (list_length(l2cb.rcv_pending_q) == 1) {
110 alarm_set_on_queue(l2cb.receive_hold_timer,
112 l2c_receive_hold_timer_timeout, NULL,
113 btu_general_alarm_queue);
118 L2CAP_TRACE_ERROR ("L2CAP - rcvd ACL for unknown handle:%d ls:%d cid:%d"
119 " opcode:%d cur count:%d", handle, p_msg->layer_specific, rcv_cid,
120 cmd_code, list_length(l2cb.rcv_pending_q));
128 L2CAP_TRACE_WARNING ("L2CAP - expected pkt start or complete, got: %d", pkt_type);
133 /* Extract the length and update the buffer header */
134 STREAM_TO_UINT16 (hci_len, p);
137 if (hci_len < L2CAP_PKT_OVERHEAD) {
138 /* Must receive at least the L2CAP length and CID */
139 L2CAP_TRACE_WARNING ("L2CAP - got incorrect hci header");
140 android_errorWriteLog(0x534e4554, "34946955");
145 /* Extract the length and CID */
146 STREAM_TO_UINT16 (l2cap_len, p);
147 STREAM_TO_UINT16 (rcv_cid, p);
149 #if BLE_INCLUDED == TRUE
150 /* for BLE channel, always notify connection when ACL data received on the link */
151 if (p_lcb && p_lcb->transport == BT_TRANSPORT_LE && p_lcb->link_state != LST_DISCONNECTING)
152 /* only process fixed channel data as channel open indication when link is not in disconnecting mode */
153 l2cble_notify_le_connection(p_lcb->remote_bd_addr);
156 /* Find the CCB for this CID */
157 if (rcv_cid >= L2CAP_BASE_APPL_CID)
159 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, rcv_cid)) == NULL)
161 L2CAP_TRACE_WARNING ("L2CAP - unknown CID: 0x%04x", rcv_cid);
167 p_msg->len = hci_len - L2CAP_PKT_OVERHEAD;
168 p_msg->offset += L2CAP_PKT_OVERHEAD;
170 if (l2cap_len != p_msg->len)
172 L2CAP_TRACE_WARNING ("L2CAP - bad length in pkt. Exp: %d Act: %d",
173 l2cap_len, p_msg->len);
179 /* Send the data through the channel state machine */
180 if (rcv_cid == L2CAP_SIGNALLING_CID)
182 process_l2cap_cmd (p_lcb, p, l2cap_len);
185 else if (rcv_cid == L2CAP_CONNECTIONLESS_CID)
187 /* process_connectionless_data (p_lcb); */
188 STREAM_TO_UINT16 (psm, p);
189 L2CAP_TRACE_DEBUG( "GOT CONNECTIONLESS DATA PSM:%d", psm ) ;
191 #if (L2CAP_UCD_INCLUDED == TRUE)
192 /* if it is not broadcast, check UCD registration */
193 if ( l2c_ucd_check_rx_pkts( p_lcb, p_msg ) )
201 #if (BLE_INCLUDED == TRUE)
202 else if (rcv_cid == L2CAP_BLE_SIGNALLING_CID)
204 l2cble_process_sig_cmd (p_lcb, p, l2cap_len);
208 #if (L2CAP_NUM_FIXED_CHNLS > 0)
209 else if ((rcv_cid >= L2CAP_FIRST_FIXED_CHNL) && (rcv_cid <= L2CAP_LAST_FIXED_CHNL) &&
210 (l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb != NULL) )
212 /* If no CCB for this channel, allocate one */
214 /* only process fixed channel data when link is open or wait for data indication */
215 (p_lcb->link_state != LST_DISCONNECTING) &&
216 l2cu_initialize_fixed_ccb (p_lcb, rcv_cid, &l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
218 p_ccb = p_lcb->p_fixed_ccbs[rcv_cid - L2CAP_FIRST_FIXED_CHNL];
220 if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE)
221 l2c_fcr_proc_pdu (p_ccb, p_msg);
223 (*l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb)
224 (rcv_cid, p_lcb->remote_bd_addr, p_msg);
237 if (p_lcb->transport == BT_TRANSPORT_LE)
239 l2c_lcc_proc_pdu(p_ccb,p_msg);
240 // Got a pkt, valid send out credits to the peer device
241 credit = L2CAP_LE_DEFAULT_CREDIT;
242 l2c_csm_execute(p_ccb, L2CEVT_L2CA_SEND_FLOW_CONTROL_CREDIT, &credit);
246 /* Basic mode packets go straight to the state machine */
247 if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_BASIC_MODE)
248 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DATA, p_msg);
251 /* eRTM or streaming mode, so we need to validate states first */
252 if ((p_ccb->chnl_state == CST_OPEN) || (p_ccb->chnl_state == CST_CONFIG))
253 l2c_fcr_proc_pdu (p_ccb, p_msg);
262 /*******************************************************************************
264 ** Function process_l2cap_cmd
266 ** Description This function is called when a packet is received on the
267 ** L2CAP signalling CID
271 *******************************************************************************/
272 static void process_l2cap_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len)
274 UINT8 *p_pkt_end, *p_next_cmd, *p_cfg_end, *p_cfg_start;
275 UINT8 cmd_code, cfg_code, cfg_len, id;
276 tL2C_CONN_INFO con_info;
277 tL2CAP_CFG_INFO cfg_info;
278 UINT16 rej_reason, rej_mtu, lcid, rcid, info_type;
281 BOOLEAN cfg_rej, pkt_size_rej = FALSE;
282 UINT16 cfg_rej_len, cmd_len;
286 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
287 /* if l2cap command received in CID 1 on top of an LE link, ignore this command */
288 if (p_lcb->transport == BT_TRANSPORT_LE)
292 /* Reject the packet if it exceeds the default Signalling Channel MTU */
293 if (pkt_len > L2CAP_DEFAULT_MTU)
295 /* Core Spec requires a single response to the first command found in a multi-command
296 ** L2cap packet. If only responses in the packet, then it will be ignored.
297 ** Here we simply mark the bad packet and decide which cmd ID to reject later
300 L2CAP_TRACE_ERROR ("L2CAP SIG MTU Pkt Len Exceeded (672) -> pkt_len: %d", pkt_len);
304 p_pkt_end = p + pkt_len;
306 memset (&cfg_info, 0, sizeof(cfg_info));
308 /* An L2CAP packet may contain multiple commands */
311 /* Smallest command is 4 bytes */
312 if ((p = p_next_cmd) > (p_pkt_end - 4))
315 STREAM_TO_UINT8 (cmd_code, p);
316 STREAM_TO_UINT8 (id, p);
317 STREAM_TO_UINT16 (cmd_len, p);
319 /* Check command length does not exceed packet length */
320 if ((p_next_cmd = p + cmd_len) > p_pkt_end)
322 L2CAP_TRACE_WARNING ("Command len bad pkt_len: %d cmd_len: %d code: %d",
323 pkt_len, cmd_len, cmd_code);
327 L2CAP_TRACE_DEBUG ("cmd_code: %d, id:%d, cmd_len:%d", cmd_code, id, cmd_len);
329 /* Bad L2CAP packet length, look or cmd to reject */
332 /* If command found rejected it and we're done, otherwise keep looking */
333 if (l2c_is_cmd_rejected(cmd_code, id, p_lcb))
336 continue; /* Look for next cmd/response in current packet */
341 case L2CAP_CMD_REJECT:
342 STREAM_TO_UINT16 (rej_reason, p);
343 if (rej_reason == L2CAP_CMD_REJ_MTU_EXCEEDED)
345 STREAM_TO_UINT16 (rej_mtu, p);
346 /* What to do with the MTU reject ? We have negotiated an MTU. For now */
347 /* we will ignore it and let a higher protocol timeout take care of it */
349 L2CAP_TRACE_WARNING ("L2CAP - MTU rej Handle: %d MTU: %d", p_lcb->handle, rej_mtu);
351 if (rej_reason == L2CAP_CMD_REJ_INVALID_CID)
353 STREAM_TO_UINT16 (rcid, p);
354 STREAM_TO_UINT16 (lcid, p);
356 L2CAP_TRACE_WARNING ("L2CAP - rej with CID invalid, LCID: 0x%04x RCID: 0x%04x", lcid, rcid);
358 /* Remote CID invalid. Treat as a disconnect */
359 if (((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
360 && (p_ccb->remote_cid == rcid))
362 /* Fake link disconnect - no reply is generated */
363 l2c_csm_execute (p_ccb, L2CEVT_LP_DISCONNECT_IND, NULL);
367 /* SonyEricsson Info request Bug workaround (Continue connection) */
368 else if (rej_reason == L2CAP_CMD_REJ_NOT_UNDERSTOOD && p_lcb->w4_info_rsp)
370 alarm_cancel(p_lcb->info_resp_timer);
372 p_lcb->w4_info_rsp = FALSE;
373 ci.status = HCI_SUCCESS;
374 memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
376 /* For all channels, send the event through their FSMs */
377 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
379 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
384 case L2CAP_CMD_CONN_REQ:
385 STREAM_TO_UINT16 (con_info.psm, p);
386 STREAM_TO_UINT16 (rcid, p);
387 if ((p_rcb = l2cu_find_rcb_by_psm (con_info.psm)) == NULL)
389 L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for unknown PSM: %d", con_info.psm);
390 l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
395 if (!p_rcb->api.pL2CA_ConnectInd_Cb)
397 L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for outgoing-only connection PSM: %d", con_info.psm);
398 l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
402 if ((p_ccb = l2cu_allocate_ccb (p_lcb, 0)) == NULL)
404 L2CAP_TRACE_ERROR ("L2CAP - unable to allocate CCB");
405 l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_RESOURCES);
408 p_ccb->remote_id = id;
409 p_ccb->p_rcb = p_rcb;
410 p_ccb->remote_cid = rcid;
412 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_REQ, &con_info);
415 case L2CAP_CMD_CONN_RSP:
416 STREAM_TO_UINT16 (con_info.remote_cid, p);
417 STREAM_TO_UINT16 (lcid, p);
418 STREAM_TO_UINT16 (con_info.l2cap_result, p);
419 STREAM_TO_UINT16 (con_info.l2cap_status, p);
421 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) == NULL)
423 L2CAP_TRACE_WARNING ("L2CAP - no CCB for conn rsp, LCID: %d RCID: %d",
424 lcid, con_info.remote_cid);
427 if (p_ccb->local_id != id)
429 L2CAP_TRACE_WARNING ("L2CAP - con rsp - bad ID. Exp: %d Got: %d",
430 p_ccb->local_id, id);
434 if (con_info.l2cap_result == L2CAP_CONN_OK)
435 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP, &con_info);
436 else if (con_info.l2cap_result == L2CAP_CONN_PENDING)
437 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_PND, &con_info);
439 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
443 case L2CAP_CMD_CONFIG_REQ:
444 p_cfg_end = p + cmd_len;
448 STREAM_TO_UINT16 (lcid, p);
449 STREAM_TO_UINT16 (cfg_info.flags, p);
453 cfg_info.flush_to_present = cfg_info.mtu_present = cfg_info.qos_present =
454 cfg_info.fcr_present = cfg_info.fcs_present = FALSE;
456 while (p < p_cfg_end)
458 STREAM_TO_UINT8 (cfg_code, p);
459 STREAM_TO_UINT8 (cfg_len, p);
461 switch (cfg_code & 0x7F)
463 case L2CAP_CFG_TYPE_MTU:
464 cfg_info.mtu_present = TRUE;
465 STREAM_TO_UINT16 (cfg_info.mtu, p);
468 case L2CAP_CFG_TYPE_FLUSH_TOUT:
469 cfg_info.flush_to_present = TRUE;
470 STREAM_TO_UINT16 (cfg_info.flush_to, p);
473 case L2CAP_CFG_TYPE_QOS:
474 cfg_info.qos_present = TRUE;
475 STREAM_TO_UINT8 (cfg_info.qos.qos_flags, p);
476 STREAM_TO_UINT8 (cfg_info.qos.service_type, p);
477 STREAM_TO_UINT32 (cfg_info.qos.token_rate, p);
478 STREAM_TO_UINT32 (cfg_info.qos.token_bucket_size, p);
479 STREAM_TO_UINT32 (cfg_info.qos.peak_bandwidth, p);
480 STREAM_TO_UINT32 (cfg_info.qos.latency, p);
481 STREAM_TO_UINT32 (cfg_info.qos.delay_variation, p);
484 case L2CAP_CFG_TYPE_FCR:
485 cfg_info.fcr_present = TRUE;
486 STREAM_TO_UINT8 (cfg_info.fcr.mode, p);
487 STREAM_TO_UINT8 (cfg_info.fcr.tx_win_sz, p);
488 STREAM_TO_UINT8 (cfg_info.fcr.max_transmit, p);
489 STREAM_TO_UINT16 (cfg_info.fcr.rtrans_tout, p);
490 STREAM_TO_UINT16 (cfg_info.fcr.mon_tout, p);
491 STREAM_TO_UINT16 (cfg_info.fcr.mps, p);
494 case L2CAP_CFG_TYPE_FCS:
495 cfg_info.fcs_present = TRUE;
496 STREAM_TO_UINT8 (cfg_info.fcs, p);
499 case L2CAP_CFG_TYPE_EXT_FLOW:
500 cfg_info.ext_flow_spec_present = TRUE;
501 STREAM_TO_UINT8 (cfg_info.ext_flow_spec.id, p);
502 STREAM_TO_UINT8 (cfg_info.ext_flow_spec.stype, p);
503 STREAM_TO_UINT16 (cfg_info.ext_flow_spec.max_sdu_size, p);
504 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.sdu_inter_time, p);
505 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.access_latency, p);
506 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.flush_timeout, p);
510 /* sanity check option length */
511 if ((cfg_len + L2CAP_CFG_OPTION_OVERHEAD) <= cmd_len)
514 if ((cfg_code & 0x80) == 0)
516 cfg_rej_len += cfg_len + L2CAP_CFG_OPTION_OVERHEAD;
520 /* bad length; force loop exit */
530 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
532 p_ccb->remote_id = id;
535 l2cu_send_peer_config_rej (p_ccb, p_cfg_start, (UINT16) (cmd_len - L2CAP_CONFIG_REQ_LEN), cfg_rej_len);
539 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_REQ, &cfg_info);
544 /* updated spec says send command reject on invalid cid */
545 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_INVALID_CID, id, 0, 0);
549 case L2CAP_CMD_CONFIG_RSP:
550 p_cfg_end = p + cmd_len;
551 STREAM_TO_UINT16 (lcid, p);
552 STREAM_TO_UINT16 (cfg_info.flags, p);
553 STREAM_TO_UINT16 (cfg_info.result, p);
555 cfg_info.flush_to_present = cfg_info.mtu_present = cfg_info.qos_present =
556 cfg_info.fcr_present = cfg_info.fcs_present = FALSE;
558 while (p < p_cfg_end)
560 STREAM_TO_UINT8 (cfg_code, p);
561 STREAM_TO_UINT8 (cfg_len, p);
563 switch (cfg_code & 0x7F)
565 case L2CAP_CFG_TYPE_MTU:
566 cfg_info.mtu_present = TRUE;
567 STREAM_TO_UINT16 (cfg_info.mtu, p);
570 case L2CAP_CFG_TYPE_FLUSH_TOUT:
571 cfg_info.flush_to_present = TRUE;
572 STREAM_TO_UINT16 (cfg_info.flush_to, p);
575 case L2CAP_CFG_TYPE_QOS:
576 cfg_info.qos_present = TRUE;
577 STREAM_TO_UINT8 (cfg_info.qos.qos_flags, p);
578 STREAM_TO_UINT8 (cfg_info.qos.service_type, p);
579 STREAM_TO_UINT32 (cfg_info.qos.token_rate, p);
580 STREAM_TO_UINT32 (cfg_info.qos.token_bucket_size, p);
581 STREAM_TO_UINT32 (cfg_info.qos.peak_bandwidth, p);
582 STREAM_TO_UINT32 (cfg_info.qos.latency, p);
583 STREAM_TO_UINT32 (cfg_info.qos.delay_variation, p);
586 case L2CAP_CFG_TYPE_FCR:
587 cfg_info.fcr_present = TRUE;
588 STREAM_TO_UINT8 (cfg_info.fcr.mode, p);
589 STREAM_TO_UINT8 (cfg_info.fcr.tx_win_sz, p);
590 STREAM_TO_UINT8 (cfg_info.fcr.max_transmit, p);
591 STREAM_TO_UINT16 (cfg_info.fcr.rtrans_tout, p);
592 STREAM_TO_UINT16 (cfg_info.fcr.mon_tout, p);
593 STREAM_TO_UINT16 (cfg_info.fcr.mps, p);
596 case L2CAP_CFG_TYPE_FCS:
597 cfg_info.fcs_present = TRUE;
598 STREAM_TO_UINT8 (cfg_info.fcs, p);
601 case L2CAP_CFG_TYPE_EXT_FLOW:
602 cfg_info.ext_flow_spec_present = TRUE;
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);
613 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
615 if (p_ccb->local_id != id)
617 L2CAP_TRACE_WARNING ("L2CAP - cfg rsp - bad ID. Exp: %d Got: %d",
618 p_ccb->local_id, id);
621 if ( (cfg_info.result == L2CAP_CFG_OK) || (cfg_info.result == L2CAP_CFG_PENDING) )
622 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_RSP, &cfg_info);
624 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_RSP_NEG, &cfg_info);
628 L2CAP_TRACE_WARNING ("L2CAP - rcvd cfg rsp for unknown CID: 0x%04x", lcid);
632 case L2CAP_CMD_DISC_REQ:
633 STREAM_TO_UINT16 (lcid, p);
634 STREAM_TO_UINT16 (rcid, p);
636 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
638 if (p_ccb->remote_cid == rcid)
640 p_ccb->remote_id = id;
641 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_REQ, &con_info);
645 l2cu_send_peer_disc_rsp (p_lcb, id, lcid, rcid);
649 case L2CAP_CMD_DISC_RSP:
650 STREAM_TO_UINT16 (rcid, p);
651 STREAM_TO_UINT16 (lcid, p);
653 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
655 if ((p_ccb->remote_cid == rcid) && (p_ccb->local_id == id))
657 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_RSP, &con_info);
662 case L2CAP_CMD_ECHO_REQ:
663 l2cu_send_peer_echo_rsp (p_lcb, id, NULL, 0);
666 case L2CAP_CMD_ECHO_RSP:
667 if (p_lcb->p_echo_rsp_cb)
669 tL2CA_ECHO_RSP_CB *p_cb = p_lcb->p_echo_rsp_cb;
671 /* Zero out the callback in case app immediately calls us again */
672 p_lcb->p_echo_rsp_cb = NULL;
674 (*p_cb) (L2CAP_PING_RESULT_OK);
678 case L2CAP_CMD_INFO_REQ:
679 STREAM_TO_UINT16 (info_type, p);
680 l2cu_send_peer_info_rsp (p_lcb, id, info_type);
683 case L2CAP_CMD_INFO_RSP:
684 /* Stop the link connect timer if sent before L2CAP connection is up */
685 if (p_lcb->w4_info_rsp)
687 alarm_cancel(p_lcb->info_resp_timer);
688 p_lcb->w4_info_rsp = FALSE;
691 STREAM_TO_UINT16 (info_type, p);
692 STREAM_TO_UINT16 (result, p);
694 p_lcb->info_rx_bits |= (1 << info_type);
696 if ( (info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE)
697 && (result == L2CAP_INFO_RESP_RESULT_SUCCESS) )
699 STREAM_TO_UINT32( p_lcb->peer_ext_fea, p );
701 #if (L2CAP_NUM_FIXED_CHNLS > 0)
702 if (p_lcb->peer_ext_fea & L2CAP_EXTFEA_FIXED_CHNLS)
704 l2cu_send_peer_info_req (p_lcb, L2CAP_FIXED_CHANNELS_INFO_TYPE);
709 l2cu_process_fixed_chnl_resp (p_lcb);
714 #if (L2CAP_NUM_FIXED_CHNLS > 0)
715 if (info_type == L2CAP_FIXED_CHANNELS_INFO_TYPE)
717 if (result == L2CAP_INFO_RESP_RESULT_SUCCESS)
719 memcpy (p_lcb->peer_chnl_mask, p, L2CAP_FIXED_CHNL_ARRAY_SIZE);
722 l2cu_process_fixed_chnl_resp (p_lcb);
725 #if (L2CAP_UCD_INCLUDED == TRUE)
726 else if (info_type == L2CAP_CONNLESS_MTU_INFO_TYPE)
728 if (result == L2CAP_INFO_RESP_RESULT_SUCCESS)
730 STREAM_TO_UINT16 (p_lcb->ucd_mtu, p);
735 ci.status = HCI_SUCCESS;
736 memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
737 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
739 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
744 L2CAP_TRACE_WARNING ("L2CAP - bad cmd code: %d", cmd_code);
745 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
751 /*******************************************************************************
753 ** Function l2c_process_held_packets
755 ** Description This function processes any L2CAP packets that arrived before
756 ** the HCI connection complete arrived. It is a work around for
757 ** badly behaved controllers.
761 *******************************************************************************/
762 void l2c_process_held_packets(BOOLEAN timed_out) {
763 if (list_is_empty(l2cb.rcv_pending_q))
767 alarm_cancel(l2cb.receive_hold_timer);
768 L2CAP_TRACE_WARNING("L2CAP HOLD CONTINUE");
770 L2CAP_TRACE_WARNING("L2CAP HOLD TIMEOUT");
773 for (const list_node_t *node = list_begin(l2cb.rcv_pending_q);
774 node != list_end(l2cb.rcv_pending_q);) {
775 BT_HDR *p_buf = list_node(node);
776 node = list_next(node);
777 if (!timed_out || (!p_buf->layer_specific) || (--p_buf->layer_specific == 0)) {
778 list_remove(l2cb.rcv_pending_q, p_buf);
779 p_buf->layer_specific = 0xFFFF;
780 l2c_rcv_acl_data(p_buf);
784 /* If anyone still in the queue, restart the timeout */
785 if (!list_is_empty(l2cb.rcv_pending_q)) {
786 alarm_set_on_queue(l2cb.receive_hold_timer, BT_1SEC_TIMEOUT_MS,
787 l2c_receive_hold_timer_timeout, NULL,
788 btu_general_alarm_queue);
792 /*******************************************************************************
796 ** Description This function is called once at startup to initialize
797 ** all the L2CAP structures
801 *******************************************************************************/
806 memset (&l2cb, 0, sizeof (tL2C_CB));
807 /* the psm is increased by 2 before being used */
808 l2cb.dyn_psm = 0xFFF;
810 /* Put all the channel control blocks on the free queue */
811 for (xx = 0; xx < MAX_L2CAP_CHANNELS - 1; xx++)
813 l2cb.ccb_pool[xx].p_next_ccb = &l2cb.ccb_pool[xx + 1];
816 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
817 /* it will be set to L2CAP_PKT_START_NON_FLUSHABLE if controller supports */
818 l2cb.non_flushable_pbf = L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT;
821 l2cb.p_free_ccb_first = &l2cb.ccb_pool[0];
822 l2cb.p_free_ccb_last = &l2cb.ccb_pool[MAX_L2CAP_CHANNELS - 1];
824 #ifdef L2CAP_DESIRED_LINK_ROLE
825 l2cb.desire_role = L2CAP_DESIRED_LINK_ROLE;
827 l2cb.desire_role = HCI_ROLE_SLAVE;
830 /* Set the default idle timeout */
831 l2cb.idle_timeout = L2CAP_LINK_INACTIVITY_TOUT;
833 #if defined(L2CAP_INITIAL_TRACE_LEVEL)
834 l2cb.l2cap_trace_level = L2CAP_INITIAL_TRACE_LEVEL;
836 l2cb.l2cap_trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
839 #if L2CAP_CONFORMANCE_TESTING == TRUE
840 /* Conformance testing needs a dynamic response */
841 l2cb.test_info_resp = L2CAP_EXTFEA_SUPPORTED_MASK;
844 /* Number of ACL buffers to use for high priority channel */
845 #if (defined(L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE) && (L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE == TRUE))
846 l2cb.high_pri_min_xmit_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA;
849 #if BLE_INCLUDED == TRUE
850 l2cb.l2c_ble_fixed_chnls_mask =
851 L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
854 l2cb.rcv_pending_q = list_new(NULL);
855 if (l2cb.rcv_pending_q == NULL)
856 LOG_ERROR(LOG_TAG, "%s unable to allocate memory for link layer control block", __func__);
857 l2cb.receive_hold_timer = alarm_new("l2c.receive_hold_timer");
860 void l2c_free(void) {
861 list_free(l2cb.rcv_pending_q);
862 l2cb.rcv_pending_q = NULL;
865 void l2c_receive_hold_timer_timeout(UNUSED_ATTR void *data)
867 /* Update the timeouts in the hold queue */
868 l2c_process_held_packets(TRUE);
871 void l2c_ccb_timer_timeout(void *data)
873 tL2C_CCB *p_ccb = (tL2C_CCB *)data;
875 l2c_csm_execute(p_ccb, L2CEVT_TIMEOUT, NULL);
878 void l2c_fcrb_ack_timer_timeout(void *data)
880 tL2C_CCB *p_ccb = (tL2C_CCB *)data;
882 l2c_csm_execute(p_ccb, L2CEVT_ACK_TIMEOUT, NULL);
885 void l2c_lcb_timer_timeout(void *data)
887 tL2C_LCB *p_lcb = (tL2C_LCB *)data;
889 l2c_link_timeout(p_lcb);
892 /*******************************************************************************
894 ** Function l2c_data_write
896 ** Description API functions call this function to write data.
898 ** Returns L2CAP_DW_SUCCESS, if data accepted, else FALSE
899 ** L2CAP_DW_CONGESTED, if data accepted and the channel is congested
900 ** L2CAP_DW_FAILED, if error
902 *******************************************************************************/
903 UINT8 l2c_data_write (UINT16 cid, BT_HDR *p_data, UINT16 flags)
907 /* Find the channel control block. We don't know the link it is on. */
908 if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL)
910 L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_DataWrite, CID: %d", cid);
912 return (L2CAP_DW_FAILED);
915 #ifndef TESTER /* Tester may send any amount of data. otherwise sending message
916 bigger than mtu size of peer is a violation of protocol */
919 if (p_ccb->p_lcb->transport == BT_TRANSPORT_LE)
920 mtu = p_ccb->peer_conn_cfg.mtu;
922 mtu = p_ccb->peer_cfg.mtu;
924 if (p_data->len > mtu)
926 L2CAP_TRACE_WARNING ("L2CAP - CID: 0x%04x cannot send message bigger than peer's mtu size", cid);
928 return (L2CAP_DW_FAILED);
932 /* channel based, packet based flushable or non-flushable */
933 p_data->layer_specific = flags;
935 /* If already congested, do not accept any more packets */
936 if (p_ccb->cong_sent)
938 L2CAP_TRACE_ERROR ("L2CAP - CID: 0x%04x cannot send, already congested xmit_hold_q.count: %u buff_quota: %u",
940 fixed_queue_length(p_ccb->xmit_hold_q),
944 return (L2CAP_DW_FAILED);
948 l2c_csm_execute (p_ccb, L2CEVT_L2CA_DATA_WRITE, p_data);
950 if (p_ccb->cong_sent)
951 return (L2CAP_DW_CONGESTED);
953 return (L2CAP_DW_SUCCESS);