• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 module contains functions for port emulation entity and RFCOMM
22  *  communications
23  *
24  ******************************************************************************/
25 #include <string.h>
26 
27 #include "common/bt_target.h"
28 #include "stack/rfcdefs.h"
29 #include "stack/port_api.h"
30 #include "btm_int.h"
31 #include "stack/btm_api.h"
32 #include "port_int.h"
33 #include "rfc_int.h"
34 #include "common/bt_defs.h"
35 #include "osi/mutex.h"
36 #include "osi/allocator.h"
37 #if (defined RFCOMM_INCLUDED && RFCOMM_INCLUDED == 1)
38 /*
39 ** Local function definitions
40 */
41 UINT32 port_rfc_send_tx_data (tPORT *p_port);
42 void   port_rfc_closed (tPORT *p_port, UINT8 res);
43 void   port_get_credits (tPORT *p_port, UINT8 k);
44 
45 
46 /*******************************************************************************
47 **
48 ** Function         port_open_continue
49 **
50 ** Description      This function is called after security manager completes
51 **                  required security checks.
52 **
53 ** Returns          void
54 **
55 *******************************************************************************/
port_open_continue(tPORT * p_port)56 int port_open_continue (tPORT *p_port)
57 {
58     tRFC_MCB *p_mcb;
59 
60     RFCOMM_TRACE_EVENT ("port_open_continue, p_port:%p", p_port);
61 
62     /* Check if multiplexer channel has already been established */
63     if ((p_mcb = rfc_alloc_multiplexer_channel (p_port->bd_addr, 1)) == NULL) {
64         RFCOMM_TRACE_WARNING ("port_open_continue no mx channel");
65         port_release_port (p_port);
66         return (PORT_NO_RESOURCES);
67     }
68 
69     p_port->rfc.p_mcb = p_mcb;
70 
71     p_mcb->port_inx[p_port->dlci] = p_port->inx;
72 
73     /* Connection is up and we know local and remote features, select MTU */
74     port_select_mtu (p_port);
75 
76     if (p_mcb->state == RFC_MX_STATE_CONNECTED) {
77         RFCOMM_ParNegReq (p_mcb, p_port->dlci, p_port->mtu);
78     } else if ((p_mcb->state == RFC_MX_STATE_IDLE)
79                || (p_mcb->state == RFC_MX_STATE_DISC_WAIT_UA)) {
80         /* In RFC_MX_STATE_IDLE state, MX state machine will create connection           */
81         /* In RFC_MX_STATE_DISC_WAIT_UA state, MX state machine will recreate connection */
82         /*    after disconnecting is completed                                           */
83         RFCOMM_StartReq (p_mcb);
84     } else {
85         /* MX state machine ignores RFC_MX_EVENT_START_REQ in these states */
86         /* When it enters RFC_MX_STATE_CONNECTED, it will check any openning ports */
87         RFCOMM_TRACE_DEBUG ("port_open_continue: mx state(%d) mx channel is openning", p_mcb->state);
88     }
89     return (PORT_SUCCESS);
90 }
91 
92 
93 /*******************************************************************************
94 **
95 ** Function         port_start_control
96 **
97 ** Description      This function is called in the BTU_TASK context to
98 **                  send control information
99 **
100 ** Returns          void
101 **
102 *******************************************************************************/
port_start_control(tPORT * p_port)103 void port_start_control (tPORT *p_port)
104 {
105     tRFC_MCB *p_mcb = p_port->rfc.p_mcb;
106 
107     if (p_mcb == NULL) {
108         return;
109     }
110 
111     RFCOMM_ControlReq (p_mcb, p_port->dlci, &p_port->local_ctrl);
112 }
113 
114 
115 /*******************************************************************************
116 **
117 ** Function         port_start_par_neg
118 **
119 ** Description      This function is called in the BTU_TASK context to
120 **                  send configuration information
121 **
122 ** Returns          void
123 **
124 *******************************************************************************/
port_start_par_neg(tPORT * p_port)125 void port_start_par_neg (tPORT *p_port)
126 {
127     tRFC_MCB *p_mcb = p_port->rfc.p_mcb;
128 
129     if (p_mcb == NULL) {
130         return;
131     }
132 
133     RFCOMM_PortNegReq (p_mcb, p_port->dlci, &p_port->user_port_pars);
134 }
135 
136 
137 /*******************************************************************************
138 **
139 ** Function         port_start_close
140 **
141 ** Description      This function is called in the BTU_TASK context to
142 **                  release DLC
143 **
144 ** Returns          void
145 **
146 *******************************************************************************/
port_start_close(tPORT * p_port)147 void port_start_close (tPORT *p_port)
148 {
149     tRFC_MCB *p_mcb = p_port->rfc.p_mcb;
150     UINT8  old_signals;
151     UINT32 events = 0;
152 
153     /* At first indicate to the user that signals on the connection were dropped */
154     p_port->line_status |= LINE_STATUS_FAILED;
155     old_signals = p_port->peer_ctrl.modem_signal;
156 
157     p_port->peer_ctrl.modem_signal &= ~(PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON);
158 
159     events |= port_get_signal_changes (p_port, old_signals, p_port->peer_ctrl.modem_signal);
160 
161     if (p_port->ev_mask & PORT_EV_CONNECT_ERR) {
162         events |= PORT_EV_CONNECT_ERR;
163     }
164 
165     if (p_port->ev_mask & PORT_EV_ERR) {
166         events |= PORT_EV_ERR;
167     }
168 
169     if ((p_port->p_callback != NULL) && events) {
170         p_port->p_callback (events, p_port->inx);
171     }
172 
173 
174     /* Check if RFCOMM side has been closed while the message was queued */
175     if ((p_mcb == NULL) || (p_port->rfc.state == RFC_STATE_CLOSED)) {
176         /* Call management callback function before calling port_release_port() to clear tPort */
177         if (p_port->p_mgmt_callback) {
178             p_port->p_mgmt_callback (PORT_CLOSED, p_port->inx);
179         }
180 
181         port_release_port (p_port);
182     } else {
183         RFCOMM_DlcReleaseReq (p_mcb, p_port->dlci);
184     }
185 }
186 
187 
188 /*******************************************************************************
189 **
190 ** Function         PORT_StartCnf
191 **
192 ** Description      This function is called from the RFCOMM layer when
193 **                  establishing of the multiplexer channel is completed.
194 **                  Continue establishing of the connection for all ports that
195 **                  are in the OPENING state
196 **
197 *******************************************************************************/
PORT_StartCnf(tRFC_MCB * p_mcb,UINT16 result)198 void PORT_StartCnf (tRFC_MCB *p_mcb, UINT16 result)
199 {
200     tPORT   *p_port;
201     int     i;
202     BOOLEAN no_ports_up = 1;
203 
204     RFCOMM_TRACE_EVENT ("PORT_StartCnf result:%d", result);
205 
206     p_port = &rfc_cb.port.port[0];
207     for (i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
208         if (p_port->rfc.p_mcb == p_mcb) {
209             no_ports_up = 0;
210 
211             if (result == RFCOMM_SUCCESS) {
212                 RFCOMM_ParNegReq (p_mcb, p_port->dlci, p_port->mtu);
213             } else {
214                 RFCOMM_TRACE_WARNING ("PORT_StartCnf failed result:%d", result);
215 
216                 /* Warning: result is also set to 4 when l2cap connection
217                    fails due to l2cap connect cnf (no_resources) */
218                 if ( result == HCI_ERR_PAGE_TIMEOUT ) {
219                     p_port->error = PORT_PAGE_TIMEOUT;
220                 } else {
221                     p_port->error = PORT_START_FAILED;
222                 }
223 
224                 rfc_release_multiplexer_channel (p_mcb);
225                 p_port->rfc.p_mcb = NULL;
226 
227                 /* Send event to the application */
228                 if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECT_ERR)) {
229                     (p_port->p_callback)(PORT_EV_CONNECT_ERR, p_port->inx);
230                 }
231 
232                 if (p_port->p_mgmt_callback) {
233                     p_port->p_mgmt_callback (PORT_START_FAILED, p_port->inx);
234                 }
235 
236                 port_release_port (p_port);
237             }
238         }
239     }
240 
241     /* There can be a situation when after starting connection, user closes the */
242     /* port, we can catch it here to close multiplexor channel */
243     if (no_ports_up) {
244         rfc_check_mcb_active (p_mcb);
245     }
246 }
247 
248 
249 /*******************************************************************************
250 **
251 ** Function         PORT_StartInd
252 **
253 ** Description      This function is called from the RFCOMM layer when
254 **                  some peer device wants to establish a multiplexer
255 **                  connection.  Check if there are any ports open with this
256 **                  or not assigned multiplexer.
257 **
258 *******************************************************************************/
PORT_StartInd(tRFC_MCB * p_mcb)259 void PORT_StartInd (tRFC_MCB *p_mcb)
260 {
261     tPORT *p_port;
262     int   i;
263 
264     RFCOMM_TRACE_EVENT ("PORT_StartInd");
265 
266     p_port = &rfc_cb.port.port[0];
267     for (i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
268         if ((p_port->rfc.p_mcb == NULL)
269                 || (p_port->rfc.p_mcb == p_mcb)) {
270             RFCOMM_TRACE_DEBUG("PORT_StartInd, RFCOMM_StartRsp RFCOMM_SUCCESS: p_mcb:%p", p_mcb);
271             RFCOMM_StartRsp (p_mcb, RFCOMM_SUCCESS);
272             return;
273         }
274     }
275     RFCOMM_StartRsp (p_mcb, RFCOMM_ERROR);
276 }
277 
278 
279 /*******************************************************************************
280 **
281 ** Function         PORT_ParNegInd
282 **
283 ** Description      This function is called from the RFCOMM layer to change
284 **                  DLCI parameters (currently only MTU is negotiated).
285 **                  If can not find the port do not accept the request.
286 **                  Otherwise save the MTU size supported by the peer.
287 **
288 *******************************************************************************/
PORT_ParNegInd(tRFC_MCB * p_mcb,UINT8 dlci,UINT16 mtu,UINT8 cl,UINT8 k)289 void PORT_ParNegInd (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu, UINT8 cl, UINT8 k)
290 {
291     tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
292     UINT8 our_cl;
293     UINT8 our_k;
294 
295     RFCOMM_TRACE_EVENT ("PORT_ParNegInd dlci:%d mtu:%d", dlci, mtu);
296 
297     if (!p_port) {
298         /* This can be a first request for this port */
299         p_port = port_find_dlci_port (dlci);
300         if (!p_port) {
301             /* If the port cannot be opened, send a DM.  Per Errata 1205 */
302             rfc_send_dm(p_mcb, dlci, 0);
303             /* check if this is the last port open, some headsets have
304             problem, they don't disconnect if we send DM */
305             rfc_check_mcb_active( p_mcb );
306             RFCOMM_TRACE_EVENT( "PORT_ParNegInd: port not found" );
307             return;
308         }
309         p_mcb->port_inx[dlci] = p_port->inx;
310     }
311 
312     memcpy (p_port->bd_addr, p_mcb->bd_addr, BD_ADDR_LEN);
313 
314     /* Connection is up and we know local and remote features, select MTU */
315     port_select_mtu (p_port);
316 
317     p_port->rfc.p_mcb   = p_mcb;
318     p_port->mtu         = (p_port->mtu < mtu) ? p_port->mtu : mtu;
319     p_port->peer_mtu    = p_port->mtu;
320 
321     /* Negotiate the flow control mechanism.  If flow control mechanism for */
322     /* mux has not been set yet, set it now.  If either we or peer wants TS 07.10, */
323     /* use that.  Otherwise both must want credit based, so use that. If flow is */
324     /* already defined for this mux, we respond with that value. */
325     if (p_mcb->flow == PORT_FC_UNDEFINED) {
326         if ((PORT_FC_DEFAULT == PORT_FC_TS710) || (cl == RFCOMM_PN_CONV_LAYER_TYPE_1)) {
327             p_mcb->flow = PORT_FC_TS710;
328         } else {
329             p_mcb->flow = PORT_FC_CREDIT;
330         }
331     }
332 
333     /* Regardless of our flow control mechanism, if the PN cl is zero, we must */
334     /* respond with zero.  "A responding implementation must set this field to 14 */
335     /* if (and only if) the PN request was 15."  This could happen if a PN is sent */
336     /* after the DLCI is already established-- the PN in that case must have cl = 0. */
337     /* See RFCOMM spec 5.5.3 */
338     if (cl == RFCOMM_PN_CONV_LAYER_TYPE_1) {
339         our_cl = RFCOMM_PN_CONV_LAYER_TYPE_1;
340         our_k = 0;
341     } else if (p_mcb->flow == PORT_FC_CREDIT) {
342         /* get credits */
343         port_get_credits (p_port, k);
344 
345         /* Set convergence layer and number of credits (k) */
346         our_cl = RFCOMM_PN_CONV_LAYER_CBFC_R;
347         our_k = (p_port->credit_rx_max < RFCOMM_K_MAX) ? p_port->credit_rx_max : RFCOMM_K_MAX;
348         p_port->credit_rx = our_k;
349     } else {
350         /* must not be using credit based flow control; use TS 7.10 */
351         our_cl = RFCOMM_PN_CONV_LAYER_TYPE_1;
352         our_k = 0;
353     }
354     RFCOMM_ParNegRsp (p_mcb, dlci, p_port->mtu, our_cl, our_k);
355 }
356 
357 
358 /*******************************************************************************
359 **
360 ** Function         PORT_ParNegCnf
361 **
362 ** Description      This function is called from the RFCOMM layer to change
363 **                  DLCI parameters (currently only MTU is negotiated).
364 **                  Save the MTU size supported by the peer.
365 **                  If the confirmation is received during the port opening
366 **                  procedure send EstablishRequest to continue.
367 **
368 *******************************************************************************/
PORT_ParNegCnf(tRFC_MCB * p_mcb,UINT8 dlci,UINT16 mtu,UINT8 cl,UINT8 k)369 void PORT_ParNegCnf (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu, UINT8 cl, UINT8 k)
370 {
371     tPORT   *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
372 
373     RFCOMM_TRACE_EVENT ("PORT_ParNegCnf dlci:%d mtu:%d cl: %d k: %d", dlci, mtu, cl, k);
374 
375     if (!p_port) {
376         return;
377     }
378 
379     /* Flow control mechanism not set yet.  Negotiate flow control mechanism. */
380     if (p_mcb->flow == PORT_FC_UNDEFINED) {
381         /* Our stack is configured for TS07.10 and they responded with credit-based. */
382         /* This is illegal-- negotiation fails. */
383         if ((PORT_FC_DEFAULT == PORT_FC_TS710) && (cl == RFCOMM_PN_CONV_LAYER_CBFC_R)) {
384             rfc_send_disc (p_mcb, p_port->dlci);
385             rfc_port_closed (p_port);
386             return;
387         }
388         /* Our stack is configured for credit-based and they responded with credit-based. */
389         else if (cl == RFCOMM_PN_CONV_LAYER_CBFC_R) {
390             p_mcb->flow = PORT_FC_CREDIT;
391         }
392         /* They responded with any other value.  Treat this as negotiation to TS07.10. */
393         else {
394             p_mcb->flow = PORT_FC_TS710;
395         }
396     }
397     /* If mux flow control mechanism set, we honor that setting regardless of */
398     /* the CL value in their response.  This allows us to gracefully accept any */
399     /* illegal PN negotiation scenarios. */
400 
401     p_port->mtu         = (p_port->mtu < mtu) ? p_port->mtu : mtu;
402     p_port->peer_mtu    = p_port->mtu;
403 
404     if (p_mcb->flow == PORT_FC_CREDIT) {
405         port_get_credits (p_port, k);
406     }
407 
408     if (p_port->state == PORT_STATE_OPENING) {
409         RFCOMM_DlcEstablishReq (p_mcb, p_port->dlci, p_port->mtu);
410     }
411 }
412 
413 
414 /*******************************************************************************
415 **
416 ** Function         PORT_DlcEstablishInd
417 **
418 ** Description      This function is called from the RFCOMM layer when peer
419 **                  device wants to establish a new DLC.  If this is not the
420 **                  first message in the establishment procedure port_handle
421 **                  has a handle to the port control block otherwise the control
422 **                  block should be found based on the muliplexer channel and
423 **                  dlci.  The block should be allocated allocated before
424 **                  meaning that application already made open.
425 **
426 *******************************************************************************/
PORT_DlcEstablishInd(tRFC_MCB * p_mcb,UINT8 dlci,UINT16 mtu)427 void PORT_DlcEstablishInd (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu)
428 {
429     tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
430 
431     RFCOMM_TRACE_DEBUG ("PORT_DlcEstablishInd p_mcb:%p, dlci:%d mtu:%di, p_port:%p", p_mcb, dlci, mtu, p_port);
432     RFCOMM_TRACE_DEBUG ("PORT_DlcEstablishInd p_mcb addr:%02x:%02x:%02x:%02x:%02x:%02x",
433                         p_mcb->bd_addr[0], p_mcb->bd_addr[1], p_mcb->bd_addr[2],
434                         p_mcb->bd_addr[3], p_mcb->bd_addr[4], p_mcb->bd_addr[5]);
435 
436     if (!p_port) {
437         /* This can be a first request for this port */
438         p_port = port_find_dlci_port (dlci);
439         if (!p_port) {
440             RFCOMM_DlcEstablishRsp (p_mcb, dlci, 0, RFCOMM_ERROR);
441             return;
442         }
443         p_mcb->port_inx[dlci] = p_port->inx;
444     }
445 
446     /* If L2CAP's mtu less then RFCOMM's take it */
447     if (mtu && (mtu < p_port->peer_mtu)) {
448         p_port->peer_mtu = mtu;
449     }
450 
451     /* If there was an inactivity timer running for MCB stop it */
452     rfc_timer_stop (p_mcb);
453 
454     RFCOMM_DlcEstablishRsp (p_mcb, dlci, p_port->mtu, RFCOMM_SUCCESS);
455 
456     /* This is the server side.  If application wants to know when connection */
457     /* is established, thats the place */
458     if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECTED)) {
459         (p_port->p_callback)(PORT_EV_CONNECTED, p_port->inx);
460     }
461 
462     if (p_port->p_mgmt_callback) {
463         p_port->p_mgmt_callback (PORT_SUCCESS, p_port->inx);
464     }
465 
466     p_port->state = PORT_STATE_OPENED;
467 }
468 
469 
470 /*******************************************************************************
471 **
472 ** Function         PORT_DlcEstablishCnf
473 **
474 ** Description      This function is called from the RFCOMM layer when peer
475 **                  acknowledges establish procedure (SABME/UA).  Send reply
476 **                  to the user and set state to OPENED if result was
477 **                  successfull.
478 **
479 *******************************************************************************/
PORT_DlcEstablishCnf(tRFC_MCB * p_mcb,UINT8 dlci,UINT16 mtu,UINT16 result)480 void PORT_DlcEstablishCnf (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu, UINT16 result)
481 {
482     tPORT  *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
483 
484     RFCOMM_TRACE_EVENT ("PORT_DlcEstablishCnf dlci:%d mtu:%d result:%d", dlci, mtu, result);
485 
486     if (!p_port) {
487         return;
488     }
489 
490     if (result != RFCOMM_SUCCESS) {
491         p_port->error = PORT_START_FAILED;
492         port_rfc_closed (p_port, PORT_START_FAILED);
493         return;
494     }
495 
496     /* If L2CAP's mtu less then RFCOMM's take it */
497     if (mtu && (mtu < p_port->peer_mtu)) {
498         p_port->peer_mtu = mtu;
499     }
500 
501     /* If there was an inactivity timer running for MCB stop it */
502     rfc_timer_stop (p_mcb);
503 
504     if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECTED)) {
505         (p_port->p_callback)(PORT_EV_CONNECTED, p_port->inx);
506     }
507 
508     if (p_port->p_mgmt_callback) {
509         p_port->p_mgmt_callback (PORT_SUCCESS, p_port->inx);
510     }
511 
512     p_port->state = PORT_STATE_OPENED;
513 
514     /* RPN is required only if we want to tell DTE how the port should be opened */
515     if ((p_port->uuid == UUID_SERVCLASS_DIALUP_NETWORKING)
516             || (p_port->uuid == UUID_SERVCLASS_FAX)) {
517         RFCOMM_PortNegReq (p_port->rfc.p_mcb, p_port->dlci, NULL);
518     } else {
519         RFCOMM_ControlReq (p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
520     }
521 }
522 
523 
524 /*******************************************************************************
525 **
526 ** Function         PORT_PortNegInd
527 **
528 ** Description      This function is called from the RFCOMM layer when peer
529 **                  device wants to set parameters of the port.  As per the spec
530 **                  this message has to be sent before the first data packet
531 **                  and can be sent before establish.  The block should be
532 **                  allocated before meaning that application already made open.
533 **
534 *******************************************************************************/
PORT_PortNegInd(tRFC_MCB * p_mcb,UINT8 dlci,tPORT_STATE * p_pars,UINT16 param_mask)535 void PORT_PortNegInd (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_STATE *p_pars,
536                       UINT16 param_mask)
537 {
538     tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
539 
540     RFCOMM_TRACE_EVENT ("PORT_PortNegInd");
541 
542     if (!p_port) {
543         /* This can be a first request for this port */
544         p_port = port_find_dlci_port (dlci);
545         if (!p_port) {
546             RFCOMM_PortNegRsp (p_mcb, dlci, p_pars, 0);
547             return;
548         }
549         p_mcb->port_inx[dlci] = p_port->inx;
550     }
551 
552     /* Check if the flow control is acceptable on local side */
553     p_port->peer_port_pars = *p_pars;
554     RFCOMM_PortNegRsp (p_mcb, dlci, p_pars, param_mask);
555 }
556 
557 
558 /*******************************************************************************
559 **
560 ** Function         PORT_PortNegCnf
561 **
562 ** Description      This function is called from the RFCOMM layer to change
563 **                  state for the port.  Propagate change to the user.
564 **
565 *******************************************************************************/
PORT_PortNegCnf(tRFC_MCB * p_mcb,UINT8 dlci,tPORT_STATE * p_pars,UINT16 result)566 void PORT_PortNegCnf (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_STATE *p_pars, UINT16 result)
567 {
568     tPORT  *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
569     UNUSED(p_pars);
570 
571     RFCOMM_TRACE_EVENT ("PORT_PortNegCnf");
572 
573     if (!p_port) {
574         RFCOMM_TRACE_WARNING ("PORT_PortNegCnf no port");
575         return;
576     }
577     /* Port negotiation failed. Drop the connection */
578     if (result != RFCOMM_SUCCESS) {
579         p_port->error = PORT_PORT_NEG_FAILED;
580 
581         RFCOMM_DlcReleaseReq (p_mcb, p_port->dlci);
582 
583         port_rfc_closed (p_port, PORT_PORT_NEG_FAILED);
584         return;
585     }
586 
587     if (!(p_port->port_ctrl & PORT_CTRL_REQ_SENT)) {
588         RFCOMM_ControlReq (p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
589     } else {
590         RFCOMM_TRACE_WARNING ("PORT_PortNegCnf Control Already sent");
591     }
592 }
593 
594 
595 /*******************************************************************************
596 **
597 ** Function         PORT_ControlInd
598 **
599 ** Description      This function is called from the RFCOMM layer on the modem
600 **                  signal change.  Propagate change to the user.
601 **
602 *******************************************************************************/
PORT_ControlInd(tRFC_MCB * p_mcb,UINT8 dlci,tPORT_CTRL * p_pars)603 void PORT_ControlInd (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_CTRL *p_pars)
604 {
605     tPORT  *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
606     UINT32 event;
607     UINT8  old_signals;
608 
609     RFCOMM_TRACE_EVENT ("PORT_ControlInd");
610 
611     if (!p_port) {
612         return;
613     }
614 
615     old_signals = p_port->peer_ctrl.modem_signal;
616 
617     event = port_get_signal_changes (p_port, old_signals, p_pars->modem_signal);
618 
619     p_port->peer_ctrl = *p_pars;
620 
621     if (!(p_port->port_ctrl & PORT_CTRL_REQ_SENT)) {
622         RFCOMM_ControlReq (p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
623     } else {
624         /* If this is the first time we received control RFCOMM is connected */
625         if (!(p_port->port_ctrl & PORT_CTRL_IND_RECEIVED)) {
626             event |= (PORT_EV_CONNECTED & p_port->ev_mask);
627         }
628 
629         if (p_port->port_ctrl & PORT_CTRL_REQ_CONFIRMED) {
630             event |= port_rfc_send_tx_data(p_port);
631         }
632     }
633 
634     p_port->port_ctrl |= (PORT_CTRL_IND_RECEIVED | PORT_CTRL_IND_RESPONDED);
635 
636     if (p_pars->break_signal) {
637         event |= (PORT_EV_BREAK & p_port->ev_mask);
638     }
639 
640     /* execute call back function only if the application is registered for events */
641     if (event && p_port->p_callback) {
642         (p_port->p_callback)(event, p_port->inx);
643     }
644 
645     RFCOMM_TRACE_EVENT ("PORT_ControlInd DTR_DSR : %d, RTS_CTS : %d, RI : %d, DCD : %d",
646                         ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_DTRDSR) ? 1 : 0),
647                         ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_RTSCTS) ? 1 : 0),
648                         ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_RI) ? 1 : 0),
649                         ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_DCD) ? 1 : 0));
650 
651 }
652 
653 
654 /*******************************************************************************
655 **
656 ** Function         PORT_ControlCnf
657 **
658 ** Description      This function is called from the RFCOMM layer when
659 **                  peer acknowleges change of the modem signals.
660 **
661 *******************************************************************************/
PORT_ControlCnf(tRFC_MCB * p_mcb,UINT8 dlci,tPORT_CTRL * p_pars)662 void PORT_ControlCnf (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_CTRL *p_pars)
663 {
664     tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
665     UINT32 event = 0;
666     UNUSED(p_pars);
667 
668     RFCOMM_TRACE_EVENT ("PORT_ControlCnf");
669 
670     if (!p_port) {
671         return;
672     }
673 
674     if (!(p_port->port_ctrl & PORT_CTRL_REQ_CONFIRMED)) {
675         p_port->port_ctrl |= PORT_CTRL_REQ_CONFIRMED;
676 
677         if (p_port->port_ctrl & PORT_CTRL_IND_RECEIVED) {
678             event = (p_port->ev_mask & PORT_EV_CONNECTED);
679         }
680     }
681 
682     if (p_port->port_ctrl & PORT_CTRL_IND_RECEIVED) {
683         event |= port_rfc_send_tx_data(p_port);
684     }
685 
686     /* execute call back function only if the application is registered for events */
687     if (event && p_port->p_callback) {
688         (p_port->p_callback)(event, p_port->inx);
689     }
690 }
691 
692 
693 /*******************************************************************************
694 **
695 ** Function         PORT_LineStatusInd
696 **
697 ** Description      This function is called from the RFCOMM layer when
698 **                  peer indicates change in the line status
699 **
700 *******************************************************************************/
PORT_LineStatusInd(tRFC_MCB * p_mcb,UINT8 dlci,UINT8 line_status)701 void PORT_LineStatusInd (tRFC_MCB *p_mcb, UINT8 dlci, UINT8 line_status)
702 {
703     tPORT  *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
704     UINT32 event = 0;
705 
706     RFCOMM_TRACE_EVENT ("PORT_LineStatusInd");
707 
708     if (!p_port) {
709         return;
710     }
711 
712     p_port->line_status |= line_status;
713 
714     if (line_status & PORT_ERR_OVERRUN) {
715         event |= PORT_EV_OVERRUN;
716     }
717 
718     if (line_status & PORT_ERR_BREAK) {
719         event |= PORT_EV_BREAK;
720     }
721 
722     if (line_status & ~(PORT_ERR_OVERRUN | PORT_ERR_BREAK)) {
723         event |= PORT_EV_ERR;
724     }
725 
726     if ((p_port->p_callback != NULL) && (p_port->ev_mask & event)) {
727         p_port->p_callback ((p_port->ev_mask & event), p_port->inx);
728     }
729 }
730 
731 
732 /*******************************************************************************
733 **
734 ** Function         PORT_DlcReleaseInd
735 **
736 ** Description      This function is called from the RFCOMM layer when
737 **                  DLC connection is released.
738 **
739 *******************************************************************************/
PORT_DlcReleaseInd(tRFC_MCB * p_mcb,UINT8 dlci)740 void PORT_DlcReleaseInd (tRFC_MCB *p_mcb, UINT8 dlci)
741 {
742     tPORT  *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
743 
744     RFCOMM_TRACE_EVENT ("PORT_DlcReleaseInd");
745 
746     if (!p_port) {
747         return;
748     }
749 
750     port_rfc_closed (p_port, PORT_CLOSED);
751 }
752 
753 
754 /*******************************************************************************
755 **
756 ** Function         PORT_CloseInd
757 **
758 ** Description      This function is called from the RFCOMM layer when
759 **                  multiplexer connection is released.
760 **
761 *******************************************************************************/
PORT_CloseInd(tRFC_MCB * p_mcb)762 void PORT_CloseInd (tRFC_MCB *p_mcb)
763 {
764     tPORT  *p_port;
765     int    i;
766 
767     RFCOMM_TRACE_EVENT ("PORT_CloseInd");
768 
769     p_port = &rfc_cb.port.port[0];
770     for (i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
771         if (p_port->rfc.p_mcb == p_mcb) {
772             port_rfc_closed (p_port, PORT_PEER_CONNECTION_FAILED);
773         }
774     }
775     rfc_release_multiplexer_channel (p_mcb);
776 }
777 
778 /*******************************************************************************
779 **
780 ** Function         Port_TimeOutCloseMux
781 **
782 ** Description      This function is called when RFCOMM timesout on a command
783 **                  as a result multiplexer connection is closed.
784 **
785 *******************************************************************************/
Port_TimeOutCloseMux(tRFC_MCB * p_mcb)786 void Port_TimeOutCloseMux (tRFC_MCB *p_mcb)
787 {
788     tPORT  *p_port;
789     int    i;
790 
791     RFCOMM_TRACE_EVENT ("Port_TimeOutCloseMux");
792 
793     p_port = &rfc_cb.port.port[0];
794     for (i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
795         if (p_port->rfc.p_mcb == p_mcb) {
796             port_rfc_closed (p_port, PORT_PEER_TIMEOUT);
797         }
798     }
799 }
800 
801 
802 /*******************************************************************************
803 **
804 ** Function         PORT_DataInd
805 **
806 ** Description      This function is called from the RFCOMM layer when data
807 **                  buffer is received from the peer.
808 **
809 *******************************************************************************/
PORT_DataInd(tRFC_MCB * p_mcb,UINT8 dlci,BT_HDR * p_buf)810 void PORT_DataInd (tRFC_MCB *p_mcb, UINT8 dlci, BT_HDR *p_buf)
811 {
812     tPORT  *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
813     UINT8  rx_char1;
814     UINT32 events = 0;
815     UINT8  *p;
816     int    i;
817 
818     RFCOMM_TRACE_EVENT("PORT_DataInd with data length %d, p_mcb:%p,p_port:%p,dlci:%d",
819                        p_buf->len, p_mcb, p_port, dlci);
820     if (!p_port) {
821         osi_free (p_buf);
822         return;
823     }
824     /* If client registered callout callback with flow control we can just deliver receive data */
825     if (p_port->p_data_co_callback) {
826         /* Another packet is delivered to user.  Send credits to peer if required */
827 
828         if (p_port->p_data_co_callback(p_port->inx, (UINT8 *)p_buf, -1, DATA_CO_CALLBACK_TYPE_INCOMING)) {
829             // do nothing, flow control credits will be given upon upper-layer request;
830             // port_flow_control_peer(p_port, 1, 1);
831         } else {
832             port_flow_control_peer(p_port, 0, 0);
833         }
834         //osi_free (p_buf);
835         return;
836     } else {
837         RFCOMM_TRACE_DEBUG("PORT_DataInd, p_port:%p, p_data_co_callback is null", p_port);
838     }
839     /* If client registered callback we can just deliver receive data */
840     if (p_port->p_data_callback) {
841         /* Another packet is delivered to user.  Send credits to peer if required */
842         port_flow_control_peer(p_port, 1, 1);
843 
844         p_port->p_data_callback (p_port->inx, (UINT8 *)(p_buf + 1) + p_buf->offset, p_buf->len);
845         osi_free (p_buf);
846         return;
847     }
848 
849     /* Check if rx queue exceeds the limit */
850     if ((p_port->rx.queue_size + p_buf->len > PORT_RX_CRITICAL_WM)
851      || (fixed_queue_length(p_port->rx.queue) + 1 > p_port->rx_buf_critical)) {
852         RFCOMM_TRACE_EVENT ("PORT_DataInd. Buffer over run. Dropping the buffer");
853         osi_free (p_buf);
854 
855         RFCOMM_LineStatusReq (p_mcb, dlci, LINE_STATUS_OVERRUN);
856         return;
857     }
858 
859     /* If user registered to receive notification when a particular byte is */
860     /* received we mast check all received bytes */
861     if (((rx_char1 = p_port->user_port_pars.rx_char1) != 0)
862             && (p_port->ev_mask & PORT_EV_RXFLAG)) {
863         for (i = 0, p = (UINT8 *)(p_buf + 1) + p_buf->offset; i < p_buf->len; i++) {
864             if (*p++ == rx_char1) {
865                 events |= PORT_EV_RXFLAG;
866                 break;
867             }
868         }
869     }
870 
871     osi_mutex_global_lock();
872 
873     fixed_queue_enqueue(p_port->rx.queue, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
874     p_port->rx.queue_size += p_buf->len;
875 
876     osi_mutex_global_unlock();
877 
878     /* perform flow control procedures if necessary */
879     port_flow_control_peer(p_port, 0, 0);
880 
881     /* If user indicated flow control can not deliver any notifications to him */
882     if (p_port->rx.user_fc) {
883         if (events & PORT_EV_RXFLAG) {
884             p_port->rx_flag_ev_pending = 1;
885         }
886 
887         return;
888     }
889 
890     events |= PORT_EV_RXCHAR;
891 
892     /* Mask out all events that are not of interest to user */
893     events &= p_port->ev_mask;
894 
895     if (p_port->p_callback && events) {
896         p_port->p_callback (events, p_port->inx);
897     }
898 }
899 
900 
901 /*******************************************************************************
902 **
903 ** Function         PORT_FlowInd
904 **
905 ** Description      This function is called from the RFCOMM layer on the flow
906 **                  control signal change.  Propagate change to the user.
907 **
908 *******************************************************************************/
PORT_FlowInd(tRFC_MCB * p_mcb,UINT8 dlci,BOOLEAN enable_data)909 void PORT_FlowInd (tRFC_MCB *p_mcb, UINT8 dlci, BOOLEAN enable_data)
910 {
911     tPORT  *p_port = (tPORT *)NULL;
912     UINT32 events = 0;
913     int    i;
914 
915     RFCOMM_TRACE_EVENT ("PORT_FlowInd fc:%d", enable_data);
916 
917     if (dlci == 0) {
918         p_mcb->peer_ready = enable_data;
919     } else {
920         if ((p_port = port_find_mcb_dlci_port (p_mcb, dlci)) == NULL) {
921             return;
922         }
923 
924         p_port->tx.peer_fc = !enable_data;
925     }
926 
927     for (i = 0; i < MAX_RFC_PORTS; i++) {
928         /* If DLCI is 0 event applies to all ports */
929         if (dlci == 0) {
930             p_port = &rfc_cb.port.port[i];
931             if (!p_port->in_use
932                     || (p_port->rfc.p_mcb != p_mcb)
933                     || (p_port->rfc.state != RFC_STATE_OPENED)) {
934                 continue;
935             }
936         }
937         events = 0;
938 
939         /* Check if flow of data is still enabled */
940         events |= port_flow_control_user (p_port);
941 
942         /* Check if data can be sent and send it */
943         events |= port_rfc_send_tx_data (p_port);
944 
945         /* Mask out all events that are not of interest to user */
946         events &= p_port->ev_mask;
947 
948         /* Send event to the application */
949         if (p_port->p_callback && events) {
950             (p_port->p_callback)(events, p_port->inx);
951         }
952 
953         /* If DLCI is not 0 event applies to one port only */
954         if (dlci != 0) {
955             break;
956         }
957     }
958 }
959 
960 
961 /*******************************************************************************
962 **
963 ** Function         port_rfc_send_tx_data
964 **
965 ** Description      This function is when forward data can be sent to the peer
966 **
967 *******************************************************************************/
port_rfc_send_tx_data(tPORT * p_port)968 UINT32 port_rfc_send_tx_data (tPORT *p_port)
969 {
970     UINT32 events = 0;
971     BT_HDR *p_buf;
972 
973     /* if there is data to be sent */
974     if (p_port->tx.queue_size > 0) {
975         /* while the rfcomm peer is not flow controlling us, and peer is ready */
976         while (!p_port->tx.peer_fc && p_port->rfc.p_mcb && p_port->rfc.p_mcb->peer_ready) {
977             /* get data from tx queue and send it */
978             osi_mutex_global_lock();
979 
980             if ((p_buf = (BT_HDR *)fixed_queue_dequeue(p_port->tx.queue, 0)) != NULL) {
981                 p_port->tx.queue_size -= p_buf->len;
982 
983                 osi_mutex_global_unlock();
984 
985                 RFCOMM_TRACE_DEBUG ("Sending RFCOMM_DataReq tx.queue_size=%d", p_port->tx.queue_size);
986 
987                 RFCOMM_DataReq (p_port->rfc.p_mcb, p_port->dlci, p_buf);
988 
989                 events |= PORT_EV_TXCHAR;
990 
991                 if (p_port->tx.queue_size == 0) {
992                     events |= PORT_EV_TXEMPTY;
993                     break;
994                 }
995             }
996             /* queue is empty-- all data sent */
997             else {
998                 osi_mutex_global_unlock();
999 
1000                 events |= PORT_EV_TXEMPTY;
1001                 break;
1002             }
1003         }
1004         /* If we flow controlled user based on the queue size enable data again */
1005         events |= port_flow_control_user (p_port);
1006     }
1007     return (events & p_port->ev_mask);
1008 }
1009 
1010 
1011 /*******************************************************************************
1012 **
1013 ** Function         port_rfc_closed
1014 **
1015 ** Description      This function when RFCOMM side of port is closed
1016 **
1017 *******************************************************************************/
port_rfc_closed(tPORT * p_port,UINT8 res)1018 void port_rfc_closed (tPORT *p_port, UINT8 res)
1019 {
1020     UINT8     old_signals;
1021     UINT32    events = 0;
1022     tRFC_MCB *p_mcb = p_port->rfc.p_mcb;
1023 
1024     if ((p_port->state == PORT_STATE_OPENING) && (p_port->is_server)) {
1025         /* The servr side has not been informed that connection is up, ignore */
1026         RFCOMM_TRACE_EVENT ("port_rfc_closed in OPENING state ignored");
1027 
1028         rfc_port_timer_stop (p_port);
1029         p_port->rfc.state = RFC_STATE_CLOSED;
1030 
1031         if (p_mcb) {
1032             p_mcb->port_inx[p_port->dlci] = 0;
1033 
1034             /* If there are no more ports opened on this MCB release it */
1035             rfc_check_mcb_active (p_mcb);
1036             p_port->rfc.p_mcb = NULL;
1037         }
1038 
1039         /* Need to restore DLCI to listening state
1040          * if the server was on the initiating RFC
1041          */
1042         p_port->dlci &= 0xfe;
1043 
1044         return;
1045     }
1046 
1047     if ((p_port->state != PORT_STATE_CLOSING) && (p_port->state != PORT_STATE_CLOSED)) {
1048         p_port->line_status |= LINE_STATUS_FAILED;
1049 
1050         old_signals = p_port->peer_ctrl.modem_signal;
1051 
1052         p_port->peer_ctrl.modem_signal &= ~(PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON);
1053 
1054         events |= port_get_signal_changes (p_port, old_signals, p_port->peer_ctrl.modem_signal);
1055 
1056         if (p_port->ev_mask & PORT_EV_CONNECT_ERR) {
1057             events |= PORT_EV_CONNECT_ERR;
1058         }
1059     }
1060     RFCOMM_TRACE_EVENT ("port_rfc_closed state:%d sending events:%x", p_port->state, events);
1061 
1062     if ((p_port->p_callback != NULL) && events) {
1063         p_port->p_callback (events, p_port->inx);
1064     }
1065 
1066     if (p_port->p_mgmt_callback) {
1067         p_port->p_mgmt_callback (res, p_port->inx);
1068     }
1069 
1070     p_port->rfc.state = RFC_STATE_CLOSED;
1071 
1072     RFCOMM_TRACE_WARNING ("%s RFCOMM connection in state %d closed: %s (res: %d)",
1073                           __func__, p_port->state, PORT_GetResultString(res), res);
1074 
1075     port_release_port (p_port);
1076 }
1077 
1078 
1079 /*******************************************************************************
1080 **
1081 ** Function         port_get_credits
1082 **
1083 ** Description      Set initial values for credits.
1084 **                  Adjust max number of rx credits based on negotiated MTU.
1085 **                  Check max allowed num of bytes, max allowed num buffers,
1086 **                  should be less then 255
1087 **
1088 *******************************************************************************/
port_get_credits(tPORT * p_port,UINT8 k)1089 void port_get_credits (tPORT *p_port, UINT8 k)
1090 {
1091     p_port->credit_tx = k;
1092     if (p_port->credit_tx == 0) {
1093         p_port->tx.peer_fc = 1;
1094     }
1095 }
1096 
1097 
1098 #endif ///(defined RFCOMM_INCLUDED && RFCOMM_INCLUDED == 1)
1099