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