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 L2CAP interface functions
23 ******************************************************************************/
26 #include "bt_target.h"
28 #include "bt_common.h"
29 #include "osi/include/osi.h"
30 #include "osi/include/time.h"
41 * Define Callback functions to be called by L2CAP
43 static void RFCOMM_ConnectInd(const RawAddress& bd_addr, uint16_t lcid,
44 uint16_t psm, uint8_t id);
45 static void RFCOMM_ConnectCnf(uint16_t lcid, uint16_t err);
46 static void RFCOMM_ConfigInd(uint16_t lcid, tL2CAP_CFG_INFO* p_cfg);
47 static void RFCOMM_ConfigCnf(uint16_t lcid, tL2CAP_CFG_INFO* p_cfg);
48 static void RFCOMM_DisconnectInd(uint16_t lcid, bool is_clear);
49 static void RFCOMM_QoSViolationInd(UNUSED_ATTR const RawAddress& bd_addr);
50 static void RFCOMM_BufDataInd(uint16_t lcid, BT_HDR* p_buf);
51 static void RFCOMM_CongestionStatusInd(uint16_t lcid, bool is_congested);
53 /*******************************************************************************
55 * Function rfcomm_l2cap_if_init
57 * Description This function is called during the RFCOMM task startup
58 * to register interface functions with L2CAP.
60 ******************************************************************************/
61 void rfcomm_l2cap_if_init(void) {
62 tL2CAP_APPL_INFO* p_l2c = &rfc_cb.rfc.reg_info;
64 p_l2c->pL2CA_ConnectInd_Cb = RFCOMM_ConnectInd;
65 p_l2c->pL2CA_ConnectCfm_Cb = RFCOMM_ConnectCnf;
66 p_l2c->pL2CA_ConnectPnd_Cb = NULL;
67 p_l2c->pL2CA_ConfigInd_Cb = RFCOMM_ConfigInd;
68 p_l2c->pL2CA_ConfigCfm_Cb = RFCOMM_ConfigCnf;
69 p_l2c->pL2CA_DisconnectInd_Cb = RFCOMM_DisconnectInd;
70 p_l2c->pL2CA_DisconnectCfm_Cb = NULL;
71 p_l2c->pL2CA_QoSViolationInd_Cb = RFCOMM_QoSViolationInd;
72 p_l2c->pL2CA_DataInd_Cb = RFCOMM_BufDataInd;
73 p_l2c->pL2CA_CongestionStatus_Cb = RFCOMM_CongestionStatusInd;
74 p_l2c->pL2CA_TxComplete_Cb = NULL;
76 L2CA_Register(BT_PSM_RFCOMM, p_l2c);
79 /*******************************************************************************
81 * Function RFCOMM_ConnectInd
83 * Description This is a callback function called by L2CAP when
84 * L2CA_ConnectInd received. Allocate multiplexer control
85 * block and dispatch the event to it.
87 ******************************************************************************/
88 void RFCOMM_ConnectInd(const RawAddress& bd_addr, uint16_t lcid,
89 UNUSED_ATTR uint16_t psm, uint8_t id) {
90 tRFC_MCB* p_mcb = rfc_alloc_multiplexer_channel(bd_addr, false);
92 if ((p_mcb) && (p_mcb->state != RFC_MX_STATE_IDLE)) {
93 /* if this is collision case */
94 if ((p_mcb->is_initiator) && (p_mcb->state == RFC_MX_STATE_WAIT_CONN_CNF)) {
95 p_mcb->pending_lcid = lcid;
96 p_mcb->pending_id = id;
98 /* wait random timeout (2 - 12) to resolve collision */
99 /* if peer gives up then local device rejects incoming connection and
100 * continues as initiator */
101 /* if timeout, local device disconnects outgoing connection and continues
104 "RFCOMM_ConnectInd start timer for collision, initiator's "
105 "LCID(0x%x), acceptor's LCID(0x%x)",
106 p_mcb->lcid, p_mcb->pending_lcid);
108 rfc_timer_start(p_mcb, (uint16_t)(time_get_os_boottime_ms() % 10 + 2));
111 /* we cannot accept connection request from peer at this state */
112 /* don't update lcid */
116 /* store mcb even if null */
117 rfc_save_lcid_mcb(p_mcb, lcid);
121 L2CA_ConnectRsp(bd_addr, id, lcid, L2CAP_CONN_NO_RESOURCES, 0);
126 rfc_mx_sm_execute(p_mcb, RFC_MX_EVENT_CONN_IND, &id);
129 /*******************************************************************************
131 * Function RFCOMM_ConnectCnf
133 * Description This is a callback function called by L2CAP when
134 * L2CA_ConnectCnf received. Save L2CAP handle and dispatch
137 ******************************************************************************/
138 void RFCOMM_ConnectCnf(uint16_t lcid, uint16_t result) {
139 tRFC_MCB* p_mcb = rfc_find_lcid_mcb(lcid);
142 RFCOMM_TRACE_ERROR("RFCOMM_ConnectCnf LCID:0x%x", lcid);
146 if (p_mcb->pending_lcid) {
147 /* if peer rejects our connect request but peer's connect request is pending
149 if (result != L2CAP_CONN_OK) {
154 "RFCOMM_ConnectCnf retry as acceptor on pending LCID(0x%x)",
155 p_mcb->pending_lcid);
157 /* remove mcb from mapping table */
158 rfc_save_lcid_mcb(NULL, p_mcb->lcid);
160 p_mcb->lcid = p_mcb->pending_lcid;
161 p_mcb->is_initiator = false;
162 p_mcb->state = RFC_MX_STATE_IDLE;
164 /* store mcb into mapping table */
165 rfc_save_lcid_mcb(p_mcb, p_mcb->lcid);
167 /* update direction bit */
168 for (i = 0; i < RFCOMM_MAX_DLCI; i += 2) {
169 idx = p_mcb->port_inx[i];
171 p_mcb->port_inx[i] = 0;
172 p_mcb->port_inx[i + 1] = idx;
173 rfc_cb.port.port[idx - 1].dlci += 1;
174 RFCOMM_TRACE_DEBUG("RFCOMM MX - DLCI:%d -> %d", i,
175 rfc_cb.port.port[idx - 1].dlci);
179 rfc_mx_sm_execute(p_mcb, RFC_MX_EVENT_CONN_IND, &(p_mcb->pending_id));
182 RFCOMM_TRACE_DEBUG("RFCOMM_ConnectCnf peer gave up pending LCID(0x%x)",
183 p_mcb->pending_lcid);
185 /* Peer gave up his connection request, make sure cleaning up L2CAP
187 L2CA_ConnectRsp(p_mcb->bd_addr, p_mcb->pending_id, p_mcb->pending_lcid,
188 L2CAP_CONN_NO_RESOURCES, 0);
190 p_mcb->pending_lcid = 0;
194 /* Save LCID to be used in all consecutive calls to L2CAP */
197 rfc_mx_sm_execute(p_mcb, RFC_MX_EVENT_CONN_CNF, &result);
200 /*******************************************************************************
202 * Function RFCOMM_ConfigInd
204 * Description This is a callback function called by L2CAP when
205 * L2CA_ConfigInd received. Save parameters in the control
206 * block and dispatch event to the FSM.
208 ******************************************************************************/
209 void RFCOMM_ConfigInd(uint16_t lcid, tL2CAP_CFG_INFO* p_cfg) {
210 tRFC_MCB* p_mcb = rfc_find_lcid_mcb(lcid);
213 RFCOMM_TRACE_ERROR("RFCOMM_ConfigInd LCID:0x%x", lcid);
217 rfc_mx_sm_execute(p_mcb, RFC_MX_EVENT_CONF_IND, (void*)p_cfg);
220 /*******************************************************************************
222 * Function RFCOMM_ConfigCnf
224 * Description This is a callback function called by L2CAP when
225 * L2CA_ConfigCnf received. Save L2CAP handle and dispatch
228 ******************************************************************************/
229 void RFCOMM_ConfigCnf(uint16_t lcid, tL2CAP_CFG_INFO* p_cfg) {
230 tRFC_MCB* p_mcb = rfc_find_lcid_mcb(lcid);
233 RFCOMM_TRACE_ERROR("RFCOMM_ConfigCnf no MCB LCID:0x%x", lcid);
237 rfc_mx_sm_execute(p_mcb, RFC_MX_EVENT_CONF_CNF, (void*)p_cfg);
240 /*******************************************************************************
242 * Function RFCOMM_QoSViolationInd
244 * Description This is a callback function called by L2CAP when
245 * L2CA_QoSViolationIndInd received. Dispatch event to the
248 ******************************************************************************/
249 void RFCOMM_QoSViolationInd(UNUSED_ATTR const RawAddress& bd_addr) {}
251 /*******************************************************************************
253 * Function RFCOMM_DisconnectInd
255 * Description This is a callback function called by L2CAP when
256 * L2CA_DisconnectInd received. Dispatch event to the FSM.
258 ******************************************************************************/
259 void RFCOMM_DisconnectInd(uint16_t lcid, bool is_conf_needed) {
260 tRFC_MCB* p_mcb = rfc_find_lcid_mcb(lcid);
262 if (is_conf_needed) {
263 L2CA_DisconnectRsp(lcid);
267 RFCOMM_TRACE_WARNING("RFCOMM_DisconnectInd LCID:0x%x", lcid);
271 rfc_mx_sm_execute(p_mcb, RFC_MX_EVENT_DISC_IND, NULL);
274 /*******************************************************************************
276 * Function RFCOMM_BufDataInd
278 * Description This is a callback function called by L2CAP when
279 * data RFCOMM frame is received. Parse the frames, check
280 * the checksum and dispatch event to multiplexer or port
281 * state machine depending on the frame destination.
283 ******************************************************************************/
284 void RFCOMM_BufDataInd(uint16_t lcid, BT_HDR* p_buf) {
285 tRFC_MCB* p_mcb = rfc_find_lcid_mcb(lcid);
290 RFCOMM_TRACE_WARNING("RFCOMM_BufDataInd LCID:0x%x", lcid);
295 event = rfc_parse_data(p_mcb, &rfc_cb.rfc.rx_frame, p_buf);
297 /* If the frame did not pass validation just ignore it */
298 if (event == RFC_EVENT_BAD_FRAME) {
303 if (rfc_cb.rfc.rx_frame.dlci == RFCOMM_MX_DLCI) {
304 /* Take special care of the Multiplexer Control Messages */
305 if (event == RFC_EVENT_UIH) {
306 rfc_process_mx_message(p_mcb, p_buf);
310 /* Other multiplexer events go to state machine */
311 rfc_mx_sm_execute(p_mcb, event, NULL);
316 /* The frame was received on the data channel DLCI, verify that DLC exists */
317 if (((p_port = port_find_mcb_dlci_port(p_mcb, rfc_cb.rfc.rx_frame.dlci)) ==
319 (!p_port->rfc.p_mcb)) {
320 /* If this is a SABME on the new port, check if any appl is waiting for it
322 if (event != RFC_EVENT_SABME) {
323 if ((p_mcb->is_initiator && !rfc_cb.rfc.rx_frame.cr) ||
324 (!p_mcb->is_initiator && rfc_cb.rfc.rx_frame.cr))
325 rfc_send_dm(p_mcb, rfc_cb.rfc.rx_frame.dlci, rfc_cb.rfc.rx_frame.pf);
330 p_port = port_find_dlci_port(rfc_cb.rfc.rx_frame.dlci);
331 if (p_port == NULL) {
332 rfc_send_dm(p_mcb, rfc_cb.rfc.rx_frame.dlci, true);
336 p_mcb->port_inx[rfc_cb.rfc.rx_frame.dlci] = p_port->inx;
337 p_port->rfc.p_mcb = p_mcb;
340 if (event == RFC_EVENT_UIH) {
342 rfc_port_sm_execute(p_port, event, p_buf);
346 if (rfc_cb.rfc.rx_frame.credit != 0)
347 rfc_inc_credit(p_port, rfc_cb.rfc.rx_frame.credit);
351 rfc_port_sm_execute(p_port, event, NULL);
355 /*******************************************************************************
357 * Function RFCOMM_CongestionStatusInd
359 * Description This is a callback function called by L2CAP when
360 * data RFCOMM L2CAP congestion status changes
362 ******************************************************************************/
363 void RFCOMM_CongestionStatusInd(uint16_t lcid, bool is_congested) {
364 tRFC_MCB* p_mcb = rfc_find_lcid_mcb(lcid);
367 RFCOMM_TRACE_ERROR("RFCOMM_CongestionStatusInd dropped LCID:0x%x", lcid);
370 RFCOMM_TRACE_EVENT("RFCOMM_CongestionStatusInd LCID:0x%x", lcid);
372 rfc_process_l2cap_congestion(p_mcb, is_congested);
375 /*******************************************************************************
377 * Function rfc_find_lcid_mcb
379 * Description This function returns MCB block supporting local cid
381 ******************************************************************************/
382 tRFC_MCB* rfc_find_lcid_mcb(uint16_t lcid) {
385 if (lcid - L2CAP_BASE_APPL_CID >= MAX_L2CAP_CHANNELS) {
386 RFCOMM_TRACE_ERROR("rfc_find_lcid_mcb LCID:0x%x", lcid);
389 p_mcb = rfc_cb.rfc.p_rfc_lcid_mcb[lcid - L2CAP_BASE_APPL_CID];
391 if (p_mcb->lcid != lcid) {
392 RFCOMM_TRACE_WARNING(
393 "rfc_find_lcid_mcb LCID reused LCID:0x%x current:0x%x", lcid,
402 /*******************************************************************************
404 * Function rfc_save_lcid_mcb
406 * Description This function returns MCB block supporting local cid
408 ******************************************************************************/
409 void rfc_save_lcid_mcb(tRFC_MCB* p_mcb, uint16_t lcid) {
410 if (lcid < L2CAP_BASE_APPL_CID) return;
411 rfc_cb.rfc.p_rfc_lcid_mcb[lcid - L2CAP_BASE_APPL_CID] = p_mcb;