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 == TRUE)
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, TRUE)) == 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, NULL);
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 = TRUE;
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 = FALSE;
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, NULL);
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, FALSE);
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     tPORT_MGMT_CL_CALLBACK_ARG cl_mgmt_cb_arg = {0};
431     tPORT_MGMT_SR_CALLBACK_ARG sr_mgmt_cb_arg = {
432         .accept = TRUE,
433         .ignore_rfc_state = FALSE,
434         .peer_mtu = 0,
435     };
436 
437     RFCOMM_TRACE_DEBUG ("PORT_DlcEstablishInd p_mcb:%p, dlci:%d mtu:%di, p_port:%p", p_mcb, dlci, mtu, p_port);
438     RFCOMM_TRACE_DEBUG ("PORT_DlcEstablishInd p_mcb addr:%02x:%02x:%02x:%02x:%02x:%02x",
439                         p_mcb->bd_addr[0], p_mcb->bd_addr[1], p_mcb->bd_addr[2],
440                         p_mcb->bd_addr[3], p_mcb->bd_addr[4], p_mcb->bd_addr[5]);
441 
442     if (!p_port) {
443         /* This can be a first request for this port */
444         p_port = port_find_dlci_port (dlci);
445         if (!p_port) {
446             RFCOMM_DlcEstablishRsp (p_mcb, dlci, 0, RFCOMM_ERROR);
447             return;
448         }
449         p_mcb->port_inx[dlci] = p_port->inx;
450     }
451 
452     /* If L2CAP's mtu less then RFCOMM's take it */
453     if (mtu && (mtu < p_port->peer_mtu)) {
454         p_port->peer_mtu = mtu;
455     }
456 
457     /* If there was an inactivity timer running for MCB stop it */
458     rfc_timer_stop (p_mcb);
459 
460     // RFCOMM_DlcEstablishRsp (p_mcb, dlci, p_port->mtu, RFCOMM_SUCCESS);
461 
462     /* This is the server side.  If application wants to know when connection */
463     /* is established, thats the place */
464     if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECTED)) {
465         (p_port->p_callback)(PORT_EV_CONNECTED, p_port->inx);
466     }
467 
468     if (p_port->p_mgmt_callback) {
469         if (p_port->is_server) {
470             sr_mgmt_cb_arg.peer_mtu = p_port->peer_mtu;
471             /**
472              * @note
473              * 1. The manage callback function may change the value of accept in mgmt_cb_arg.
474              * 2. Use mgmt_cb_arg.ignore_rfc_state to work around the issue caused by sending
475              * RFCOMM establish response after the manage callback function.
476              */
477             sr_mgmt_cb_arg.ignore_rfc_state = TRUE;
478             p_port->p_mgmt_callback (PORT_SUCCESS, p_port->inx, &sr_mgmt_cb_arg);
479 
480             if (!sr_mgmt_cb_arg.accept) {
481                 RFCOMM_DlcEstablishRsp(p_mcb, dlci, 0, RFCOMM_LOW_RESOURCES);
482                 return;
483             }
484         } else {
485             cl_mgmt_cb_arg.peer_mtu = p_port->peer_mtu;
486             p_port->p_mgmt_callback (PORT_SUCCESS, p_port->inx, &cl_mgmt_cb_arg);
487         }
488     }
489 
490     RFCOMM_DlcEstablishRsp(p_mcb, dlci, p_port->mtu, RFCOMM_SUCCESS);
491     p_port->state = PORT_STATE_OPENED;
492 }
493 
494 
495 /*******************************************************************************
496 **
497 ** Function         PORT_DlcEstablishCnf
498 **
499 ** Description      This function is called from the RFCOMM layer when peer
500 **                  acknowledges establish procedure (SABME/UA).  Send reply
501 **                  to the user and set state to OPENED if result was
502 **                  successfull.
503 **
504 *******************************************************************************/
PORT_DlcEstablishCnf(tRFC_MCB * p_mcb,UINT8 dlci,UINT16 mtu,UINT16 result)505 void PORT_DlcEstablishCnf (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu, UINT16 result)
506 {
507     tPORT  *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
508     tPORT_MGMT_SR_CALLBACK_ARG sr_mgmt_cb_arg = {0};
509     tPORT_MGMT_CL_CALLBACK_ARG cl_mgmt_cb_arg = {0};
510 
511     RFCOMM_TRACE_EVENT ("PORT_DlcEstablishCnf dlci:%d mtu:%d result:%d", dlci, mtu, result);
512 
513     if (!p_port) {
514         return;
515     }
516 
517     if (result != RFCOMM_SUCCESS) {
518         p_port->error = PORT_START_FAILED;
519         port_rfc_closed (p_port, PORT_START_FAILED);
520         return;
521     }
522 
523     /* If L2CAP's mtu less then RFCOMM's take it */
524     if (mtu && (mtu < p_port->peer_mtu)) {
525         p_port->peer_mtu = mtu;
526     }
527 
528     /* If there was an inactivity timer running for MCB stop it */
529     rfc_timer_stop (p_mcb);
530 
531     if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECTED)) {
532         (p_port->p_callback)(PORT_EV_CONNECTED, p_port->inx);
533     }
534 
535     if (p_port->p_mgmt_callback) {
536         if (p_port->is_server) {
537             sr_mgmt_cb_arg.peer_mtu = p_port->peer_mtu;
538             p_port->p_mgmt_callback (PORT_SUCCESS, p_port->inx, &sr_mgmt_cb_arg);
539         } else {
540             cl_mgmt_cb_arg.peer_mtu = p_port->peer_mtu;
541             p_port->p_mgmt_callback (PORT_SUCCESS, p_port->inx, &cl_mgmt_cb_arg);
542         }
543     }
544 
545     p_port->state = PORT_STATE_OPENED;
546 
547     /* RPN is required only if we want to tell DTE how the port should be opened */
548     if ((p_port->uuid == UUID_SERVCLASS_DIALUP_NETWORKING)
549             || (p_port->uuid == UUID_SERVCLASS_FAX)) {
550         RFCOMM_PortNegReq (p_port->rfc.p_mcb, p_port->dlci, NULL);
551     } else {
552         RFCOMM_ControlReq (p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
553     }
554 }
555 
556 
557 /*******************************************************************************
558 **
559 ** Function         PORT_PortNegInd
560 **
561 ** Description      This function is called from the RFCOMM layer when peer
562 **                  device wants to set parameters of the port.  As per the spec
563 **                  this message has to be sent before the first data packet
564 **                  and can be sent before establish.  The block should be
565 **                  allocated before meaning that application already made open.
566 **
567 *******************************************************************************/
PORT_PortNegInd(tRFC_MCB * p_mcb,UINT8 dlci,tPORT_STATE * p_pars,UINT16 param_mask)568 void PORT_PortNegInd (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_STATE *p_pars,
569                       UINT16 param_mask)
570 {
571     tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
572 
573     RFCOMM_TRACE_EVENT ("PORT_PortNegInd");
574 
575     if (!p_port) {
576         /* This can be a first request for this port */
577         p_port = port_find_dlci_port (dlci);
578         if (!p_port) {
579             RFCOMM_PortNegRsp (p_mcb, dlci, p_pars, 0);
580             return;
581         }
582         p_mcb->port_inx[dlci] = p_port->inx;
583     }
584 
585     /* Check if the flow control is acceptable on local side */
586     p_port->peer_port_pars = *p_pars;
587     RFCOMM_PortNegRsp (p_mcb, dlci, p_pars, param_mask);
588 }
589 
590 
591 /*******************************************************************************
592 **
593 ** Function         PORT_PortNegCnf
594 **
595 ** Description      This function is called from the RFCOMM layer to change
596 **                  state for the port.  Propagate change to the user.
597 **
598 *******************************************************************************/
PORT_PortNegCnf(tRFC_MCB * p_mcb,UINT8 dlci,tPORT_STATE * p_pars,UINT16 result)599 void PORT_PortNegCnf (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_STATE *p_pars, UINT16 result)
600 {
601     tPORT  *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
602     UNUSED(p_pars);
603 
604     RFCOMM_TRACE_EVENT ("PORT_PortNegCnf");
605 
606     if (!p_port) {
607         RFCOMM_TRACE_WARNING ("PORT_PortNegCnf no port");
608         return;
609     }
610     /* Port negotiation failed. Drop the connection */
611     if (result != RFCOMM_SUCCESS) {
612         p_port->error = PORT_PORT_NEG_FAILED;
613 
614         RFCOMM_DlcReleaseReq (p_mcb, p_port->dlci);
615 
616         port_rfc_closed (p_port, PORT_PORT_NEG_FAILED);
617         return;
618     }
619 
620     if (!(p_port->port_ctrl & PORT_CTRL_REQ_SENT)) {
621         RFCOMM_ControlReq (p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
622     } else {
623         RFCOMM_TRACE_WARNING ("PORT_PortNegCnf Control Already sent");
624     }
625 }
626 
627 
628 /*******************************************************************************
629 **
630 ** Function         PORT_ControlInd
631 **
632 ** Description      This function is called from the RFCOMM layer on the modem
633 **                  signal change.  Propagate change to the user.
634 **
635 *******************************************************************************/
PORT_ControlInd(tRFC_MCB * p_mcb,UINT8 dlci,tPORT_CTRL * p_pars)636 void PORT_ControlInd (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_CTRL *p_pars)
637 {
638     tPORT  *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
639     UINT32 event;
640     UINT8  old_signals;
641 
642     RFCOMM_TRACE_EVENT ("PORT_ControlInd");
643 
644     if (!p_port) {
645         return;
646     }
647 
648     old_signals = p_port->peer_ctrl.modem_signal;
649 
650     event = port_get_signal_changes (p_port, old_signals, p_pars->modem_signal);
651 
652     p_port->peer_ctrl = *p_pars;
653 
654     if (!(p_port->port_ctrl & PORT_CTRL_REQ_SENT)) {
655         RFCOMM_ControlReq (p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
656     } else {
657         /* If this is the first time we received control RFCOMM is connected */
658         if (!(p_port->port_ctrl & PORT_CTRL_IND_RECEIVED)) {
659             event |= (PORT_EV_CONNECTED & p_port->ev_mask);
660         }
661 
662         if (p_port->port_ctrl & PORT_CTRL_REQ_CONFIRMED) {
663             event |= port_rfc_send_tx_data(p_port);
664         }
665     }
666 
667     p_port->port_ctrl |= (PORT_CTRL_IND_RECEIVED | PORT_CTRL_IND_RESPONDED);
668 
669     if (p_pars->break_signal) {
670         event |= (PORT_EV_BREAK & p_port->ev_mask);
671     }
672 
673     /* execute call back function only if the application is registered for events */
674     if (event && p_port->p_callback) {
675         (p_port->p_callback)(event, p_port->inx);
676     }
677 
678     RFCOMM_TRACE_EVENT ("PORT_ControlInd DTR_DSR : %d, RTS_CTS : %d, RI : %d, DCD : %d",
679                         ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_DTRDSR) ? 1 : 0),
680                         ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_RTSCTS) ? 1 : 0),
681                         ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_RI) ? 1 : 0),
682                         ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_DCD) ? 1 : 0));
683 
684 }
685 
686 
687 /*******************************************************************************
688 **
689 ** Function         PORT_ControlCnf
690 **
691 ** Description      This function is called from the RFCOMM layer when
692 **                  peer acknowleges change of the modem signals.
693 **
694 *******************************************************************************/
PORT_ControlCnf(tRFC_MCB * p_mcb,UINT8 dlci,tPORT_CTRL * p_pars)695 void PORT_ControlCnf (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_CTRL *p_pars)
696 {
697     tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
698     UINT32 event = 0;
699     UNUSED(p_pars);
700 
701     RFCOMM_TRACE_EVENT ("PORT_ControlCnf");
702 
703     if (!p_port) {
704         return;
705     }
706 
707     if (!(p_port->port_ctrl & PORT_CTRL_REQ_CONFIRMED)) {
708         p_port->port_ctrl |= PORT_CTRL_REQ_CONFIRMED;
709 
710         if (p_port->port_ctrl & PORT_CTRL_IND_RECEIVED) {
711             event = (p_port->ev_mask & PORT_EV_CONNECTED);
712         }
713     }
714 
715     if (p_port->port_ctrl & PORT_CTRL_IND_RECEIVED) {
716         event |= port_rfc_send_tx_data(p_port);
717     }
718 
719     /* execute call back function only if the application is registered for events */
720     if (event && p_port->p_callback) {
721         (p_port->p_callback)(event, p_port->inx);
722     }
723 }
724 
725 
726 /*******************************************************************************
727 **
728 ** Function         PORT_LineStatusInd
729 **
730 ** Description      This function is called from the RFCOMM layer when
731 **                  peer indicates change in the line status
732 **
733 *******************************************************************************/
PORT_LineStatusInd(tRFC_MCB * p_mcb,UINT8 dlci,UINT8 line_status)734 void PORT_LineStatusInd (tRFC_MCB *p_mcb, UINT8 dlci, UINT8 line_status)
735 {
736     tPORT  *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
737     UINT32 event = 0;
738 
739     RFCOMM_TRACE_EVENT ("PORT_LineStatusInd");
740 
741     if (!p_port) {
742         return;
743     }
744 
745     p_port->line_status |= line_status;
746 
747     if (line_status & PORT_ERR_OVERRUN) {
748         event |= PORT_EV_OVERRUN;
749     }
750 
751     if (line_status & PORT_ERR_BREAK) {
752         event |= PORT_EV_BREAK;
753     }
754 
755     if (line_status & ~(PORT_ERR_OVERRUN | PORT_ERR_BREAK)) {
756         event |= PORT_EV_ERR;
757     }
758 
759     if ((p_port->p_callback != NULL) && (p_port->ev_mask & event)) {
760         p_port->p_callback ((p_port->ev_mask & event), p_port->inx);
761     }
762 }
763 
764 
765 /*******************************************************************************
766 **
767 ** Function         PORT_DlcReleaseInd
768 **
769 ** Description      This function is called from the RFCOMM layer when
770 **                  DLC connection is released.
771 **
772 *******************************************************************************/
PORT_DlcReleaseInd(tRFC_MCB * p_mcb,UINT8 dlci)773 void PORT_DlcReleaseInd (tRFC_MCB *p_mcb, UINT8 dlci)
774 {
775     tPORT  *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
776 
777     RFCOMM_TRACE_EVENT ("PORT_DlcReleaseInd");
778 
779     if (!p_port) {
780         return;
781     }
782 
783     port_rfc_closed (p_port, PORT_CLOSED);
784 }
785 
786 
787 /*******************************************************************************
788 **
789 ** Function         PORT_CloseInd
790 **
791 ** Description      This function is called from the RFCOMM layer when
792 **                  multiplexer connection is released.
793 **
794 *******************************************************************************/
PORT_CloseInd(tRFC_MCB * p_mcb)795 void PORT_CloseInd (tRFC_MCB *p_mcb)
796 {
797     tPORT  *p_port;
798     int    i;
799 
800     RFCOMM_TRACE_EVENT ("PORT_CloseInd");
801 
802     p_port = &rfc_cb.port.port[0];
803     for (i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
804         if (p_port->rfc.p_mcb == p_mcb) {
805             port_rfc_closed (p_port, PORT_PEER_CONNECTION_FAILED);
806         }
807     }
808     rfc_release_multiplexer_channel (p_mcb);
809 }
810 
811 /*******************************************************************************
812 **
813 ** Function         Port_TimeOutCloseMux
814 **
815 ** Description      This function is called when RFCOMM timesout on a command
816 **                  as a result multiplexer connection is closed.
817 **
818 *******************************************************************************/
Port_TimeOutCloseMux(tRFC_MCB * p_mcb)819 void Port_TimeOutCloseMux (tRFC_MCB *p_mcb)
820 {
821     tPORT  *p_port;
822     int    i;
823 
824     RFCOMM_TRACE_EVENT ("Port_TimeOutCloseMux");
825 
826     p_port = &rfc_cb.port.port[0];
827     for (i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
828         if (p_port->rfc.p_mcb == p_mcb) {
829             port_rfc_closed (p_port, PORT_PEER_TIMEOUT);
830         }
831     }
832 }
833 
834 
835 /*******************************************************************************
836 **
837 ** Function         PORT_DataInd
838 **
839 ** Description      This function is called from the RFCOMM layer when data
840 **                  buffer is received from the peer.
841 **
842 *******************************************************************************/
PORT_DataInd(tRFC_MCB * p_mcb,UINT8 dlci,BT_HDR * p_buf)843 void PORT_DataInd (tRFC_MCB *p_mcb, UINT8 dlci, BT_HDR *p_buf)
844 {
845     tPORT  *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
846     UINT8  rx_char1;
847     UINT32 events = 0;
848     UINT8  *p;
849     int    i;
850 
851     RFCOMM_TRACE_EVENT("PORT_DataInd with data length %d, p_mcb:%p,p_port:%p,dlci:%d",
852                        p_buf->len, p_mcb, p_port, dlci);
853     if (!p_port) {
854         osi_free (p_buf);
855         return;
856     }
857     /* If client registered callout callback with flow control we can just deliver receive data */
858     if (p_port->p_data_co_callback) {
859         /* Another packet is delivered to user.  Send credits to peer if required */
860 
861         if (p_port->p_data_co_callback(p_port->inx, (UINT8 *)p_buf, -1, DATA_CO_CALLBACK_TYPE_INCOMING)) {
862             // do nothing, flow control credits will be given upon upper-layer request;
863             // port_flow_control_peer(p_port, TRUE, 1);
864         } else {
865             port_flow_control_peer(p_port, FALSE, 0);
866         }
867         //osi_free (p_buf);
868         return;
869     } else {
870         RFCOMM_TRACE_DEBUG("PORT_DataInd, p_port:%p, p_data_co_callback is null", p_port);
871     }
872     /* If client registered callback we can just deliver receive data */
873     if (p_port->p_data_callback) {
874         /* Another packet is delivered to user.  Send credits to peer if required */
875         port_flow_control_peer(p_port, TRUE, 1);
876 
877         p_port->p_data_callback (p_port->inx, (UINT8 *)(p_buf + 1) + p_buf->offset, p_buf->len);
878         osi_free (p_buf);
879         return;
880     }
881 
882     /* Check if rx queue exceeds the limit */
883     if ((p_port->rx.queue_size + p_buf->len > PORT_RX_CRITICAL_WM)
884      || (fixed_queue_length(p_port->rx.queue) + 1 > p_port->rx_buf_critical)) {
885         RFCOMM_TRACE_EVENT ("PORT_DataInd. Buffer over run. Dropping the buffer");
886         osi_free (p_buf);
887 
888         RFCOMM_LineStatusReq (p_mcb, dlci, LINE_STATUS_OVERRUN);
889         return;
890     }
891 
892     /* If user registered to receive notification when a particular byte is */
893     /* received we mast check all received bytes */
894     if (((rx_char1 = p_port->user_port_pars.rx_char1) != 0)
895             && (p_port->ev_mask & PORT_EV_RXFLAG)) {
896         for (i = 0, p = (UINT8 *)(p_buf + 1) + p_buf->offset; i < p_buf->len; i++) {
897             if (*p++ == rx_char1) {
898                 events |= PORT_EV_RXFLAG;
899                 break;
900             }
901         }
902     }
903 
904     osi_mutex_global_lock();
905 
906     fixed_queue_enqueue(p_port->rx.queue, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
907     p_port->rx.queue_size += p_buf->len;
908 
909     osi_mutex_global_unlock();
910 
911     /* perform flow control procedures if necessary */
912     port_flow_control_peer(p_port, FALSE, 0);
913 
914     /* If user indicated flow control can not deliver any notifications to him */
915     if (p_port->rx.user_fc) {
916         if (events & PORT_EV_RXFLAG) {
917             p_port->rx_flag_ev_pending = TRUE;
918         }
919 
920         return;
921     }
922 
923     events |= PORT_EV_RXCHAR;
924 
925     /* Mask out all events that are not of interest to user */
926     events &= p_port->ev_mask;
927 
928     if (p_port->p_callback && events) {
929         p_port->p_callback (events, p_port->inx);
930     }
931 }
932 
933 
934 /*******************************************************************************
935 **
936 ** Function         PORT_FlowInd
937 **
938 ** Description      This function is called from the RFCOMM layer on the flow
939 **                  control signal change.  Propagate change to the user.
940 **
941 *******************************************************************************/
PORT_FlowInd(tRFC_MCB * p_mcb,UINT8 dlci,BOOLEAN enable_data)942 void PORT_FlowInd (tRFC_MCB *p_mcb, UINT8 dlci, BOOLEAN enable_data)
943 {
944     tPORT  *p_port = (tPORT *)NULL;
945     UINT32 events = 0;
946     int    i;
947 
948     RFCOMM_TRACE_EVENT ("PORT_FlowInd fc:%d", enable_data);
949 
950     if (dlci == 0) {
951         p_mcb->peer_ready = enable_data;
952     } else {
953         if ((p_port = port_find_mcb_dlci_port (p_mcb, dlci)) == NULL) {
954             return;
955         }
956 
957         p_port->tx.peer_fc = !enable_data;
958     }
959 
960     for (i = 0; i < MAX_RFC_PORTS; i++) {
961         /* If DLCI is 0 event applies to all ports */
962         if (dlci == 0) {
963             p_port = &rfc_cb.port.port[i];
964             if (!p_port->in_use
965                     || (p_port->rfc.p_mcb != p_mcb)
966                     || (p_port->rfc.state != RFC_STATE_OPENED)) {
967                 continue;
968             }
969         }
970         events = 0;
971 
972         /* Check if flow of data is still enabled */
973         events |= port_flow_control_user (p_port);
974 
975         /* Check if data can be sent and send it */
976         events |= port_rfc_send_tx_data (p_port);
977 
978         /* Mask out all events that are not of interest to user */
979         events &= p_port->ev_mask;
980 
981         /* Send event to the application */
982         if (p_port->p_callback && events) {
983             (p_port->p_callback)(events, p_port->inx);
984         }
985 
986         /* If DLCI is not 0 event applies to one port only */
987         if (dlci != 0) {
988             break;
989         }
990     }
991 }
992 
993 
994 /*******************************************************************************
995 **
996 ** Function         port_rfc_send_tx_data
997 **
998 ** Description      This function is when forward data can be sent to the peer
999 **
1000 *******************************************************************************/
port_rfc_send_tx_data(tPORT * p_port)1001 UINT32 port_rfc_send_tx_data (tPORT *p_port)
1002 {
1003     UINT32 events = 0;
1004     BT_HDR *p_buf;
1005 
1006     /* if there is data to be sent */
1007     if (p_port->tx.queue_size > 0) {
1008         /* while the rfcomm peer is not flow controlling us, and peer is ready */
1009         while (!p_port->tx.peer_fc && p_port->rfc.p_mcb && p_port->rfc.p_mcb->peer_ready) {
1010             /* get data from tx queue and send it */
1011             osi_mutex_global_lock();
1012 
1013             if ((p_buf = (BT_HDR *)fixed_queue_dequeue(p_port->tx.queue, 0)) != NULL) {
1014                 p_port->tx.queue_size -= p_buf->len;
1015 
1016                 osi_mutex_global_unlock();
1017 
1018                 RFCOMM_TRACE_DEBUG ("Sending RFCOMM_DataReq tx.queue_size=%d", p_port->tx.queue_size);
1019 
1020                 RFCOMM_DataReq (p_port->rfc.p_mcb, p_port->dlci, p_buf);
1021 
1022                 events |= PORT_EV_TXCHAR;
1023 
1024                 if (p_port->tx.queue_size == 0) {
1025                     events |= PORT_EV_TXEMPTY;
1026                     break;
1027                 }
1028             }
1029             /* queue is empty-- all data sent */
1030             else {
1031                 osi_mutex_global_unlock();
1032 
1033                 events |= PORT_EV_TXEMPTY;
1034                 break;
1035             }
1036         }
1037         /* If we flow controlled user based on the queue size enable data again */
1038         events |= port_flow_control_user (p_port);
1039     }
1040     return (events & p_port->ev_mask);
1041 }
1042 
1043 
1044 /*******************************************************************************
1045 **
1046 ** Function         port_rfc_closed
1047 **
1048 ** Description      This function when RFCOMM side of port is closed
1049 **
1050 *******************************************************************************/
port_rfc_closed(tPORT * p_port,UINT8 res)1051 void port_rfc_closed (tPORT *p_port, UINT8 res)
1052 {
1053     UINT8     old_signals;
1054     UINT32    events = 0;
1055     tRFC_MCB *p_mcb = p_port->rfc.p_mcb;
1056 
1057     if ((p_port->state == PORT_STATE_OPENING) && (p_port->is_server)) {
1058         /* The servr side has not been informed that connection is up, ignore */
1059         RFCOMM_TRACE_EVENT ("port_rfc_closed in OPENING state ignored");
1060 
1061         rfc_port_timer_stop (p_port);
1062         p_port->rfc.state = RFC_STATE_CLOSED;
1063 
1064         if (p_mcb) {
1065             p_mcb->port_inx[p_port->dlci] = 0;
1066 
1067             /* If there are no more ports opened on this MCB release it */
1068             rfc_check_mcb_active (p_mcb);
1069             p_port->rfc.p_mcb = NULL;
1070         }
1071 
1072         /* Need to restore DLCI to listening state
1073          * if the server was on the initiating RFC
1074          */
1075         p_port->dlci &= 0xfe;
1076 
1077         return;
1078     }
1079 
1080     if ((p_port->state != PORT_STATE_CLOSING) && (p_port->state != PORT_STATE_CLOSED)) {
1081         p_port->line_status |= LINE_STATUS_FAILED;
1082 
1083         old_signals = p_port->peer_ctrl.modem_signal;
1084 
1085         p_port->peer_ctrl.modem_signal &= ~(PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON);
1086 
1087         events |= port_get_signal_changes (p_port, old_signals, p_port->peer_ctrl.modem_signal);
1088 
1089         if (p_port->ev_mask & PORT_EV_CONNECT_ERR) {
1090             events |= PORT_EV_CONNECT_ERR;
1091         }
1092     }
1093     RFCOMM_TRACE_EVENT ("port_rfc_closed state:%d sending events:%x", p_port->state, events);
1094 
1095     if ((p_port->p_callback != NULL) && events) {
1096         p_port->p_callback (events, p_port->inx);
1097     }
1098 
1099     if (p_port->p_mgmt_callback && !(p_port->state == PORT_STATE_CLOSED && p_port->is_server)) {
1100         p_port->p_mgmt_callback(res, p_port->inx, NULL);
1101     }
1102 
1103     p_port->rfc.state = RFC_STATE_CLOSED;
1104 
1105     RFCOMM_TRACE_WARNING("%s RFCOMM connection in server:%d state %d closed: %s (res: %d)",
1106                          __func__, p_port->is_server, p_port->state, PORT_GetResultString(res),
1107                          res);
1108 
1109     port_release_port (p_port);
1110 }
1111 
1112 
1113 /*******************************************************************************
1114 **
1115 ** Function         port_get_credits
1116 **
1117 ** Description      Set initial values for credits.
1118 **                  Adjust max number of rx credits based on negotiated MTU.
1119 **                  Check max allowed num of bytes, max allowed num buffers,
1120 **                  should be less then 255
1121 **
1122 *******************************************************************************/
port_get_credits(tPORT * p_port,UINT8 k)1123 void port_get_credits (tPORT *p_port, UINT8 k)
1124 {
1125     p_port->credit_tx = k;
1126     if (p_port->credit_tx == 0) {
1127         p_port->tx.peer_fc = TRUE;
1128     }
1129 }
1130 
1131 
1132 #endif ///(defined RFCOMM_INCLUDED && RFCOMM_INCLUDED == TRUE)
1133