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 L2CAP interface functions
22 *
23 ******************************************************************************/
24
25 #include <stddef.h>
26 #include "common/bt_target.h"
27
28 #include "stack/rfcdefs.h"
29 #include "stack/port_api.h"
30 #include "port_int.h"
31 #include "stack/l2c_api.h"
32 #include "stack/l2cdefs.h"
33 #include "rfc_int.h"
34 #include "common/bt_defs.h"
35 #include "osi/allocator.h"
36 #include "osi/mutex.h"
37 #include "osi/alarm.h"
38 #if (defined RFCOMM_INCLUDED && RFCOMM_INCLUDED == TRUE)
39
40 static tL2CAP_ERTM_INFO rfc_l2c_etm_opt =
41 {
42 L2CAP_FCR_ERTM_MODE,
43 L2CAP_FCR_CHAN_OPT_ERTM|L2CAP_FCR_CHAN_OPT_BASIC, /* Some devices do not support ERTM */
44 L2CAP_USER_RX_BUF_SIZE,
45 L2CAP_USER_TX_BUF_SIZE,
46 L2CAP_FCR_RX_BUF_SIZE,
47 L2CAP_FCR_TX_BUF_SIZE
48 };
49
50 /*
51 ** Define Callback functions to be called by L2CAP
52 */
53 static void RFCOMM_ConnectInd (BD_ADDR bd_addr, UINT16 lcid, UINT16 psm, UINT8 id);
54 static void RFCOMM_ConnectCnf (UINT16 lcid, UINT16 err);
55 static void RFCOMM_ConfigInd (UINT16 lcid, tL2CAP_CFG_INFO *p_cfg);
56 static void RFCOMM_ConfigCnf (UINT16 lcid, tL2CAP_CFG_INFO *p_cfg);
57 static void RFCOMM_DisconnectInd (UINT16 lcid, BOOLEAN is_clear);
58 static void RFCOMM_QoSViolationInd (BD_ADDR bd_addr);
59 static void RFCOMM_BufDataInd (UINT16 lcid, BT_HDR *p_buf);
60 static void RFCOMM_CongestionStatusInd (UINT16 lcid, BOOLEAN is_congested);
61
62
63 /*******************************************************************************
64 **
65 ** Function rfcomm_l2cap_if_init
66 **
67 ** Description This function is called during the RFCOMM task startup
68 ** to register interface functions with L2CAP.
69 **
70 *******************************************************************************/
rfcomm_l2cap_if_init(void)71 void rfcomm_l2cap_if_init (void)
72 {
73 tL2CAP_APPL_INFO *p_l2c = &rfc_cb.rfc.reg_info;
74
75 p_l2c->pL2CA_ConnectInd_Cb = RFCOMM_ConnectInd;
76 p_l2c->pL2CA_ConnectCfm_Cb = RFCOMM_ConnectCnf;
77 p_l2c->pL2CA_ConnectPnd_Cb = NULL;
78 p_l2c->pL2CA_ConfigInd_Cb = RFCOMM_ConfigInd;
79 p_l2c->pL2CA_ConfigCfm_Cb = RFCOMM_ConfigCnf;
80 p_l2c->pL2CA_DisconnectInd_Cb = RFCOMM_DisconnectInd;
81 p_l2c->pL2CA_DisconnectCfm_Cb = NULL;
82 p_l2c->pL2CA_QoSViolationInd_Cb = RFCOMM_QoSViolationInd;
83 p_l2c->pL2CA_DataInd_Cb = RFCOMM_BufDataInd;
84 p_l2c->pL2CA_CongestionStatus_Cb = RFCOMM_CongestionStatusInd;
85 p_l2c->pL2CA_TxComplete_Cb = NULL;
86
87
88 L2CA_Register (BT_PSM_RFCOMM, p_l2c);
89 }
90
91
92 /*******************************************************************************
93 **
94 ** Function RFCOMM_ConnectInd
95 **
96 ** Description This is a callback function called by L2CAP when
97 ** L2CA_ConnectInd received. Allocate multiplexer control block
98 ** and dispatch the event to it.
99 **
100 *******************************************************************************/
RFCOMM_ConnectInd(BD_ADDR bd_addr,UINT16 lcid,UINT16 psm,UINT8 id)101 void RFCOMM_ConnectInd (BD_ADDR bd_addr, UINT16 lcid, UINT16 psm, UINT8 id)
102 {
103 tRFC_MCB *p_mcb = rfc_alloc_multiplexer_channel(bd_addr, FALSE);
104 UNUSED(psm);
105
106 if ((p_mcb) && (p_mcb->state != RFC_MX_STATE_IDLE)) {
107 /* if this is collision case */
108 if ((p_mcb->is_initiator) && (p_mcb->state == RFC_MX_STATE_WAIT_CONN_CNF)) {
109 p_mcb->pending_lcid = lcid;
110 p_mcb->pending_id = id;
111
112 /* wait random timeout (2 - 12) to resolve collision */
113 /* if peer gives up then local device rejects incoming connection and continues as initiator */
114 /* if timeout, local device disconnects outgoing connection and continues as acceptor */
115 RFCOMM_TRACE_DEBUG ("RFCOMM_ConnectInd start timer for collision, initiator's LCID(0x%x), acceptor's LCID(0x%x)",
116 p_mcb->lcid, p_mcb->pending_lcid);
117
118 rfc_timer_start(p_mcb, (UINT16)(osi_time_get_os_boottime_ms() % 10 + 2));
119 return;
120 } else {
121 /* we cannot accept connection request from peer at this state */
122 /* don't update lcid */
123 p_mcb = NULL;
124 }
125 } else {
126 /* store mcb even if null */
127 rfc_save_lcid_mcb (p_mcb, lcid);
128 }
129
130 if (p_mcb == NULL) {
131 tL2CAP_ERTM_INFO *ertm_opt = rfc_cb.port.enable_l2cap_ertm ? &rfc_l2c_etm_opt : NULL;
132 L2CA_ErtmConnectRsp (bd_addr, id, lcid, L2CAP_CONN_NO_RESOURCES, 0, ertm_opt);
133 return;
134 }
135 p_mcb->lcid = lcid;
136
137 rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONN_IND, &id);
138 }
139
140
141 /*******************************************************************************
142 **
143 ** Function RFCOMM_ConnectCnf
144 **
145 ** Description This is a callback function called by L2CAP when
146 ** L2CA_ConnectCnf received. Save L2CAP handle and dispatch
147 ** event to the FSM.
148 **
149 *******************************************************************************/
RFCOMM_ConnectCnf(UINT16 lcid,UINT16 result)150 void RFCOMM_ConnectCnf (UINT16 lcid, UINT16 result)
151 {
152 tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
153
154 if (!p_mcb) {
155 RFCOMM_TRACE_ERROR ("RFCOMM_ConnectCnf LCID:0x%x", lcid);
156 return;
157 }
158
159 if (p_mcb->pending_lcid) {
160 /* if peer rejects our connect request but peer's connect request is pending */
161 if (result != L2CAP_CONN_OK ) {
162 UINT16 i;
163 UINT8 idx;
164
165 RFCOMM_TRACE_DEBUG ("RFCOMM_ConnectCnf retry as acceptor on pending LCID(0x%x)", p_mcb->pending_lcid);
166
167 /* remove mcb from mapping table */
168 rfc_save_lcid_mcb (NULL, p_mcb->lcid);
169
170 p_mcb->lcid = p_mcb->pending_lcid;
171 p_mcb->is_initiator = FALSE;
172 p_mcb->state = RFC_MX_STATE_IDLE;
173
174 /* store mcb into mapping table */
175 rfc_save_lcid_mcb (p_mcb, p_mcb->lcid);
176
177 /* update direction bit */
178 for (i = 0; i < RFCOMM_MAX_DLCI; i += 2) {
179 if ((idx = p_mcb->port_inx[i]) != 0) {
180 p_mcb->port_inx[i] = 0;
181 p_mcb->port_inx[i + 1] = idx;
182 rfc_cb.port.port[idx - 1].dlci += 1;
183 RFCOMM_TRACE_DEBUG ("RFCOMM MX - DLCI:%d -> %d", i, rfc_cb.port.port[idx - 1].dlci);
184 }
185 }
186
187 rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONN_IND, &(p_mcb->pending_id));
188 return;
189 } else {
190 RFCOMM_TRACE_DEBUG ("RFCOMM_ConnectCnf peer gave up pending LCID(0x%x)", p_mcb->pending_lcid);
191
192 tL2CAP_ERTM_INFO *ertm_opt = rfc_cb.port.enable_l2cap_ertm ? &rfc_l2c_etm_opt : NULL;
193 /* Peer gave up his connection request, make sure cleaning up L2CAP channel */
194 L2CA_ErtmConnectRsp (p_mcb->bd_addr, p_mcb->pending_id, p_mcb->pending_lcid, L2CAP_CONN_NO_RESOURCES, 0, ertm_opt);
195
196 p_mcb->pending_lcid = 0;
197 }
198 }
199
200 /* Save LCID to be used in all consecutive calls to L2CAP */
201 p_mcb->lcid = lcid;
202
203 rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONN_CNF, &result);
204 }
205
206
207 /*******************************************************************************
208 **
209 ** Function RFCOMM_ConfigInd
210 **
211 ** Description This is a callback function called by L2CAP when
212 ** L2CA_ConfigInd received. Save parameters in the control
213 ** block and dispatch event to the FSM.
214 **
215 *******************************************************************************/
RFCOMM_ConfigInd(UINT16 lcid,tL2CAP_CFG_INFO * p_cfg)216 void RFCOMM_ConfigInd (UINT16 lcid, tL2CAP_CFG_INFO *p_cfg)
217 {
218 tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
219
220 if (!p_mcb) {
221 RFCOMM_TRACE_ERROR ("RFCOMM_ConfigInd LCID:0x%x", lcid);
222 return;
223 }
224
225 rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONF_IND, (void *)p_cfg);
226 }
227
228
229 /*******************************************************************************
230 **
231 ** Function RFCOMM_ConfigCnf
232 **
233 ** Description This is a callback function called by L2CAP when
234 ** L2CA_ConfigCnf received. Save L2CAP handle and dispatch
235 ** event to the FSM.
236 **
237 *******************************************************************************/
RFCOMM_ConfigCnf(UINT16 lcid,tL2CAP_CFG_INFO * p_cfg)238 void RFCOMM_ConfigCnf (UINT16 lcid, tL2CAP_CFG_INFO *p_cfg)
239 {
240 tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
241
242 if (!p_mcb) {
243 RFCOMM_TRACE_ERROR ("RFCOMM_ConfigCnf no MCB LCID:0x%x", lcid);
244 return;
245 }
246
247 rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONF_CNF, (void *)p_cfg);
248 }
249
250
251 /*******************************************************************************
252 **
253 ** Function RFCOMM_QoSViolationInd
254 **
255 ** Description This is a callback function called by L2CAP when
256 ** L2CA_QoSViolationIndInd received. Dispatch event to the FSM.
257 **
258 *******************************************************************************/
RFCOMM_QoSViolationInd(BD_ADDR bd_addr)259 void RFCOMM_QoSViolationInd (BD_ADDR bd_addr)
260 {
261 UNUSED(bd_addr);
262 }
263
264
265 /*******************************************************************************
266 **
267 ** Function RFCOMM_DisconnectInd
268 **
269 ** Description This is a callback function called by L2CAP when
270 ** L2CA_DisconnectInd received. Dispatch event to the FSM.
271 **
272 *******************************************************************************/
RFCOMM_DisconnectInd(UINT16 lcid,BOOLEAN is_conf_needed)273 void RFCOMM_DisconnectInd (UINT16 lcid, BOOLEAN is_conf_needed)
274 {
275 tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
276
277 if (is_conf_needed) {
278 L2CA_DisconnectRsp (lcid);
279 }
280
281 if (!p_mcb) {
282 RFCOMM_TRACE_WARNING ("RFCOMM_DisconnectInd LCID:0x%x", lcid);
283 return;
284 }
285
286 rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_DISC_IND, NULL);
287 }
288
289
290 /*******************************************************************************
291 **
292 ** Function RFCOMM_BufDataInd
293 **
294 ** Description This is a callback function called by L2CAP when
295 ** data RFCOMM frame is received. Parse the frames, check
296 ** the checksum and dispatch event to multiplexer or port
297 ** state machine depending on the frame destination.
298 **
299 *******************************************************************************/
RFCOMM_BufDataInd(UINT16 lcid,BT_HDR * p_buf)300 void RFCOMM_BufDataInd (UINT16 lcid, BT_HDR *p_buf)
301 {
302 tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
303 tPORT *p_port;
304 UINT8 event;
305
306
307 if (!p_mcb) {
308 RFCOMM_TRACE_WARNING ("RFCOMM_BufDataInd LCID:0x%x", lcid);
309 osi_free (p_buf);
310 return;
311 }
312
313 event = rfc_parse_data (p_mcb, &rfc_cb.rfc.rx_frame, p_buf);
314
315 /* If the frame did not pass validation just ignore it */
316 if (event == RFC_EVENT_BAD_FRAME) {
317 osi_free (p_buf);
318 return;
319 }
320
321 if (rfc_cb.rfc.rx_frame.dlci == RFCOMM_MX_DLCI) {
322 /* Take special care of the Multiplexer Control Messages */
323 if (event == RFC_EVENT_UIH) {
324 rfc_process_mx_message (p_mcb, p_buf);
325 return;
326 }
327
328 /* Other multiplexer events go to state machine */
329 rfc_mx_sm_execute (p_mcb, event, NULL);
330 osi_free (p_buf);
331 return;
332 }
333
334 /* The frame was received on the data channel DLCI, verify that DLC exists */
335 if (((p_port = port_find_mcb_dlci_port (p_mcb, rfc_cb.rfc.rx_frame.dlci)) == NULL)
336 || (!p_port->rfc.p_mcb)) {
337 /* If this is a SABME on the new port, check if any appl is waiting for it */
338 if (event != RFC_EVENT_SABME) {
339 if (( p_mcb->is_initiator && !rfc_cb.rfc.rx_frame.cr)
340 || (!p_mcb->is_initiator && rfc_cb.rfc.rx_frame.cr)) {
341 rfc_send_dm (p_mcb, rfc_cb.rfc.rx_frame.dlci, rfc_cb.rfc.rx_frame.pf);
342 }
343 osi_free (p_buf);
344 return;
345 }
346
347 if ((p_port = port_find_dlci_port (rfc_cb.rfc.rx_frame.dlci)) == NULL) {
348 rfc_send_dm (p_mcb, rfc_cb.rfc.rx_frame.dlci, TRUE);
349 osi_free (p_buf);
350 return;
351 }
352 p_mcb->port_inx[rfc_cb.rfc.rx_frame.dlci] = p_port->inx;
353 p_port->rfc.p_mcb = p_mcb;
354 }
355
356 if (event == RFC_EVENT_UIH) {
357 if (p_buf->len > 0) {
358 rfc_port_sm_execute (p_port, event, p_buf);
359 } else {
360 osi_free (p_buf);
361 }
362
363 if (rfc_cb.rfc.rx_frame.credit != 0) {
364 rfc_inc_credit (p_port, rfc_cb.rfc.rx_frame.credit);
365 }
366
367 return;
368 }
369 rfc_port_sm_execute (p_port, event, NULL);
370 osi_free (p_buf);
371 }
372
373 /*******************************************************************************
374 **
375 ** Function RFCOMM_CongestionStatusInd
376 **
377 ** Description This is a callback function called by L2CAP when
378 ** data RFCOMM L2CAP congestion status changes
379 **
380 *******************************************************************************/
RFCOMM_CongestionStatusInd(UINT16 lcid,BOOLEAN is_congested)381 void RFCOMM_CongestionStatusInd (UINT16 lcid, BOOLEAN is_congested)
382 {
383 tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid);
384
385 if (!p_mcb) {
386 RFCOMM_TRACE_ERROR ("RFCOMM_CongestionStatusInd dropped LCID:0x%x", lcid);
387 return;
388 } else {
389 RFCOMM_TRACE_EVENT ("RFCOMM_CongestionStatusInd LCID:0x%x", lcid);
390 }
391 rfc_process_l2cap_congestion (p_mcb, is_congested);
392 }
393
394 /*******************************************************************************
395 **
396 ** Function rfc_find_lcid_mcb
397 **
398 ** Description This function returns MCB block supporting local cid
399 **
400 *******************************************************************************/
rfc_find_lcid_mcb(UINT16 lcid)401 tRFC_MCB *rfc_find_lcid_mcb (UINT16 lcid)
402 {
403 tRFC_MCB *p_mcb;
404
405 if (lcid - L2CAP_BASE_APPL_CID >= MAX_L2CAP_CHANNELS) {
406 RFCOMM_TRACE_ERROR ("rfc_find_lcid_mcb LCID:0x%x", lcid);
407 return (NULL);
408 } else {
409 if ((p_mcb = rfc_cb.rfc.p_rfc_lcid_mcb[lcid - L2CAP_BASE_APPL_CID]) != NULL) {
410 if (p_mcb->lcid != lcid) {
411 RFCOMM_TRACE_WARNING ("rfc_find_lcid_mcb LCID reused LCID:0x%x current:0x%x", lcid, p_mcb->lcid);
412 return (NULL);
413 }
414 }
415 }
416 return (p_mcb);
417 }
418
419
420 /*******************************************************************************
421 **
422 ** Function rfc_save_lcid_mcb
423 **
424 ** Description This function returns MCB block supporting local cid
425 **
426 *******************************************************************************/
rfc_save_lcid_mcb(tRFC_MCB * p_mcb,UINT16 lcid)427 void rfc_save_lcid_mcb (tRFC_MCB *p_mcb, UINT16 lcid)
428 {
429 rfc_cb.rfc.p_rfc_lcid_mcb[lcid - L2CAP_BASE_APPL_CID] = p_mcb;
430 }
431
432 #endif ///(defined RFCOMM_INCLUDED && RFCOMM_INCLUDED == TRUE)
433