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 "osi/include/time.h"
29 #include "bt_common.h"
41 ** Define Callback functions to be called by L2CAP
43 static void RFCOMM_ConnectInd (BD_ADDR bd_addr, UINT16 lcid, UINT16 psm, UINT8 id);
44 static void RFCOMM_ConnectCnf (UINT16 lcid, UINT16 err);
45 static void RFCOMM_ConfigInd (UINT16 lcid, tL2CAP_CFG_INFO *p_cfg);
46 static void RFCOMM_ConfigCnf (UINT16 lcid, tL2CAP_CFG_INFO *p_cfg);
47 static void RFCOMM_DisconnectInd (UINT16 lcid, BOOLEAN is_clear);
48 static void RFCOMM_QoSViolationInd (BD_ADDR bd_addr);
49 static void RFCOMM_BufDataInd (UINT16 lcid, BT_HDR *p_buf);
50 static void RFCOMM_CongestionStatusInd (UINT16 lcid, BOOLEAN 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)
63 tL2CAP_APPL_INFO *p_l2c = &rfc_cb.rfc.reg_info;
65 p_l2c->pL2CA_ConnectInd_Cb = RFCOMM_ConnectInd;
66 p_l2c->pL2CA_ConnectCfm_Cb = RFCOMM_ConnectCnf;
67 p_l2c->pL2CA_ConnectPnd_Cb = NULL;
68 p_l2c->pL2CA_ConfigInd_Cb = RFCOMM_ConfigInd;
69 p_l2c->pL2CA_ConfigCfm_Cb = RFCOMM_ConfigCnf;
70 p_l2c->pL2CA_DisconnectInd_Cb = RFCOMM_DisconnectInd;
71 p_l2c->pL2CA_DisconnectCfm_Cb = NULL;
72 p_l2c->pL2CA_QoSViolationInd_Cb = RFCOMM_QoSViolationInd;
73 p_l2c->pL2CA_DataInd_Cb = RFCOMM_BufDataInd;
74 p_l2c->pL2CA_CongestionStatus_Cb = RFCOMM_CongestionStatusInd;
75 p_l2c->pL2CA_TxComplete_Cb = NULL;
78 L2CA_Register (BT_PSM_RFCOMM, p_l2c);
82 /*******************************************************************************
84 ** Function RFCOMM_ConnectInd
86 ** Description This is a callback function called by L2CAP when
87 ** L2CA_ConnectInd received. Allocate multiplexer control block
88 ** and dispatch the event to it.
90 *******************************************************************************/
91 void RFCOMM_ConnectInd (BD_ADDR bd_addr, UINT16 lcid, UINT16 psm, UINT8 id)
93 tRFC_MCB *p_mcb = rfc_alloc_multiplexer_channel(bd_addr, FALSE);
96 if ((p_mcb)&&(p_mcb->state != RFC_MX_STATE_IDLE))
98 /* if this is collision case */
99 if ((p_mcb->is_initiator)&&(p_mcb->state == RFC_MX_STATE_WAIT_CONN_CNF))
101 p_mcb->pending_lcid = lcid;
102 p_mcb->pending_id = id;
104 /* wait random timeout (2 - 12) to resolve collision */
105 /* if peer gives up then local device rejects incoming connection and continues as initiator */
106 /* if timeout, local device disconnects outgoing connection and continues as acceptor */
107 RFCOMM_TRACE_DEBUG ("RFCOMM_ConnectInd start timer for collision, initiator's LCID(0x%x), acceptor's LCID(0x%x)",
108 p_mcb->lcid, p_mcb->pending_lcid);
110 rfc_timer_start(p_mcb, (UINT16)(time_get_os_boottime_ms() % 10 + 2));
115 /* we cannot accept connection request from peer at this state */
116 /* don't update lcid */
122 /* store mcb even if null */
123 rfc_save_lcid_mcb (p_mcb, lcid);
128 L2CA_ConnectRsp (bd_addr, id, lcid, L2CAP_CONN_NO_RESOURCES, 0);
133 rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONN_IND, &id);
137 /*******************************************************************************
139 ** Function RFCOMM_ConnectCnf
141 ** Description This is a callback function called by L2CAP when
142 ** L2CA_ConnectCnf received. Save L2CAP handle and dispatch
145 *******************************************************************************/
146 void RFCOMM_ConnectCnf (UINT16 lcid, UINT16 result)
148 tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
152 RFCOMM_TRACE_ERROR ("RFCOMM_ConnectCnf LCID:0x%x", lcid);
156 if (p_mcb->pending_lcid)
158 /* if peer rejects our connect request but peer's connect request is pending */
159 if (result != L2CAP_CONN_OK )
164 RFCOMM_TRACE_DEBUG ("RFCOMM_ConnectCnf retry as acceptor on pending LCID(0x%x)", p_mcb->pending_lcid);
166 /* remove mcb from mapping table */
167 rfc_save_lcid_mcb (NULL, p_mcb->lcid);
169 p_mcb->lcid = p_mcb->pending_lcid;
170 p_mcb->is_initiator = FALSE;
171 p_mcb->state = RFC_MX_STATE_IDLE;
173 /* store mcb into mapping table */
174 rfc_save_lcid_mcb (p_mcb, p_mcb->lcid);
176 /* update direction bit */
177 for (i = 0; i < RFCOMM_MAX_DLCI; i += 2)
179 if ((idx = p_mcb->port_inx[i]) != 0)
181 p_mcb->port_inx[i] = 0;
182 p_mcb->port_inx[i+1] = idx;
183 rfc_cb.port.port[idx - 1].dlci += 1;
184 RFCOMM_TRACE_DEBUG ("RFCOMM MX - DLCI:%d -> %d", i, rfc_cb.port.port[idx - 1].dlci);
188 rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONN_IND, &(p_mcb->pending_id));
193 RFCOMM_TRACE_DEBUG ("RFCOMM_ConnectCnf peer gave up pending LCID(0x%x)", p_mcb->pending_lcid);
195 /* Peer gave up his connection request, make sure cleaning up L2CAP channel */
196 L2CA_ConnectRsp (p_mcb->bd_addr, p_mcb->pending_id, p_mcb->pending_lcid, L2CAP_CONN_NO_RESOURCES, 0);
198 p_mcb->pending_lcid = 0;
202 /* Save LCID to be used in all consecutive calls to L2CAP */
205 rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONN_CNF, &result);
209 /*******************************************************************************
211 ** Function RFCOMM_ConfigInd
213 ** Description This is a callback function called by L2CAP when
214 ** L2CA_ConfigInd received. Save parameters in the control
215 ** block and dispatch event to the FSM.
217 *******************************************************************************/
218 void RFCOMM_ConfigInd (UINT16 lcid, tL2CAP_CFG_INFO *p_cfg)
220 tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
224 RFCOMM_TRACE_ERROR ("RFCOMM_ConfigInd LCID:0x%x", lcid);
228 rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONF_IND, (void *)p_cfg);
232 /*******************************************************************************
234 ** Function RFCOMM_ConfigCnf
236 ** Description This is a callback function called by L2CAP when
237 ** L2CA_ConfigCnf received. Save L2CAP handle and dispatch
240 *******************************************************************************/
241 void RFCOMM_ConfigCnf (UINT16 lcid, tL2CAP_CFG_INFO *p_cfg)
243 tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
247 RFCOMM_TRACE_ERROR ("RFCOMM_ConfigCnf no MCB LCID:0x%x", lcid);
251 rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONF_CNF, (void *)p_cfg);
255 /*******************************************************************************
257 ** Function RFCOMM_QoSViolationInd
259 ** Description This is a callback function called by L2CAP when
260 ** L2CA_QoSViolationIndInd received. Dispatch event to the FSM.
262 *******************************************************************************/
263 void RFCOMM_QoSViolationInd (BD_ADDR bd_addr)
269 /*******************************************************************************
271 ** Function RFCOMM_DisconnectInd
273 ** Description This is a callback function called by L2CAP when
274 ** L2CA_DisconnectInd received. Dispatch event to the FSM.
276 *******************************************************************************/
277 void RFCOMM_DisconnectInd (UINT16 lcid, BOOLEAN is_conf_needed)
279 tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
283 L2CA_DisconnectRsp (lcid);
288 RFCOMM_TRACE_WARNING ("RFCOMM_DisconnectInd LCID:0x%x", lcid);
292 rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_DISC_IND, NULL);
296 /*******************************************************************************
298 ** Function RFCOMM_BufDataInd
300 ** Description This is a callback function called by L2CAP when
301 ** data RFCOMM frame is received. Parse the frames, check
302 ** the checksum and dispatch event to multiplexer or port
303 ** state machine depending on the frame destination.
305 *******************************************************************************/
306 void RFCOMM_BufDataInd (UINT16 lcid, BT_HDR *p_buf)
308 tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
315 RFCOMM_TRACE_WARNING ("RFCOMM_BufDataInd LCID:0x%x", lcid);
320 event = rfc_parse_data (p_mcb, &rfc_cb.rfc.rx_frame, p_buf);
322 /* If the frame did not pass validation just ignore it */
323 if (event == RFC_EVENT_BAD_FRAME)
329 if (rfc_cb.rfc.rx_frame.dlci == RFCOMM_MX_DLCI)
331 /* Take special care of the Multiplexer Control Messages */
332 if (event == RFC_EVENT_UIH)
334 rfc_process_mx_message (p_mcb, p_buf);
338 /* Other multiplexer events go to state machine */
339 rfc_mx_sm_execute (p_mcb, event, NULL);
344 /* The frame was received on the data channel DLCI, verify that DLC exists */
345 if (((p_port = port_find_mcb_dlci_port (p_mcb, rfc_cb.rfc.rx_frame.dlci)) == NULL)
346 || (!p_port->rfc.p_mcb))
348 /* If this is a SABME on the new port, check if any appl is waiting for it */
349 if (event != RFC_EVENT_SABME)
351 if (( p_mcb->is_initiator && !rfc_cb.rfc.rx_frame.cr)
352 || (!p_mcb->is_initiator && rfc_cb.rfc.rx_frame.cr))
353 rfc_send_dm (p_mcb, rfc_cb.rfc.rx_frame.dlci, rfc_cb.rfc.rx_frame.pf);
358 if ((p_port = port_find_dlci_port (rfc_cb.rfc.rx_frame.dlci)) == NULL)
360 rfc_send_dm (p_mcb, rfc_cb.rfc.rx_frame.dlci, TRUE);
364 p_mcb->port_inx[rfc_cb.rfc.rx_frame.dlci] = p_port->inx;
365 p_port->rfc.p_mcb = p_mcb;
368 if (event == RFC_EVENT_UIH)
372 rfc_port_sm_execute (p_port, event, p_buf);
376 if (rfc_cb.rfc.rx_frame.credit != 0)
377 rfc_inc_credit (p_port, rfc_cb.rfc.rx_frame.credit);
381 rfc_port_sm_execute (p_port, event, NULL);
385 /*******************************************************************************
387 ** Function RFCOMM_CongestionStatusInd
389 ** Description This is a callback function called by L2CAP when
390 ** data RFCOMM L2CAP congestion status changes
392 *******************************************************************************/
393 void RFCOMM_CongestionStatusInd (UINT16 lcid, BOOLEAN is_congested)
395 tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
399 RFCOMM_TRACE_ERROR ("RFCOMM_CongestionStatusInd dropped LCID:0x%x", lcid);
404 RFCOMM_TRACE_EVENT ("RFCOMM_CongestionStatusInd LCID:0x%x", lcid);
406 rfc_process_l2cap_congestion (p_mcb, is_congested);
409 /*******************************************************************************
411 ** Function rfc_find_lcid_mcb
413 ** Description This function returns MCB block supporting local cid
415 *******************************************************************************/
416 tRFC_MCB *rfc_find_lcid_mcb (UINT16 lcid)
420 if (lcid - L2CAP_BASE_APPL_CID >= MAX_L2CAP_CHANNELS)
422 RFCOMM_TRACE_ERROR ("rfc_find_lcid_mcb LCID:0x%x", lcid);
427 if ((p_mcb = rfc_cb.rfc.p_rfc_lcid_mcb[lcid - L2CAP_BASE_APPL_CID]) != NULL)
429 if (p_mcb->lcid != lcid)
431 RFCOMM_TRACE_WARNING ("rfc_find_lcid_mcb LCID reused LCID:0x%x current:0x%x", lcid, p_mcb->lcid);
440 /*******************************************************************************
442 ** Function rfc_save_lcid_mcb
444 ** Description This function returns MCB block supporting local cid
446 *******************************************************************************/
447 void rfc_save_lcid_mcb (tRFC_MCB *p_mcb, UINT16 lcid)
449 rfc_cb.rfc.p_rfc_lcid_mcb[lcid - L2CAP_BASE_APPL_CID] = p_mcb;