1 /******************************************************************************
3 * Copyright (C) 1999-2012 Broadcom Corporation
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
19 /******************************************************************************
21 * This module contains functions for port emulation entity and RFCOMM
24 ******************************************************************************/
25 #include <base/logging.h>
28 #include "osi/include/mutex.h"
29 #include "osi/include/osi.h"
31 #include "bt_common.h"
32 #include "bt_target.h"
42 * Local function definitions
44 uint32_t port_rfc_send_tx_data(tPORT* p_port);
45 void port_rfc_closed(tPORT* p_port, uint8_t res);
46 void port_get_credits(tPORT* p_port, uint8_t k);
48 /*******************************************************************************
50 * Function port_open_continue
52 * Description This function is called after security manager completes
53 * required security checks.
57 ******************************************************************************/
58 int port_open_continue(tPORT* p_port) {
61 RFCOMM_TRACE_EVENT("port_open_continue, p_port:%p", p_port);
63 /* Check if multiplexer channel has already been established */
64 p_mcb = rfc_alloc_multiplexer_channel(p_port->bd_addr, true);
66 RFCOMM_TRACE_WARNING("port_open_continue no mx channel");
67 port_release_port(p_port);
68 return (PORT_NO_RESOURCES);
71 p_port->rfc.p_mcb = p_mcb;
73 p_mcb->port_inx[p_port->dlci] = p_port->inx;
75 /* Connection is up and we know local and remote features, select MTU */
76 port_select_mtu(p_port);
78 if (p_mcb->state == RFC_MX_STATE_CONNECTED) {
79 RFCOMM_ParNegReq(p_mcb, p_port->dlci, p_port->mtu);
80 } else if ((p_mcb->state == RFC_MX_STATE_IDLE) ||
81 (p_mcb->state == RFC_MX_STATE_DISC_WAIT_UA)) {
82 /* In RFC_MX_STATE_IDLE state, MX state machine will create connection */
83 /* In RFC_MX_STATE_DISC_WAIT_UA state, MX state machine will recreate
85 /* after disconnecting is completed */
86 RFCOMM_StartReq(p_mcb);
88 /* MX state machine ignores RFC_MX_EVENT_START_REQ in these states */
89 /* When it enters RFC_MX_STATE_CONNECTED, it will check any openning ports
92 "port_open_continue: mx state(%d) mx channel is openning",
95 return (PORT_SUCCESS);
98 /*******************************************************************************
100 * Function port_start_control
102 * Description This function is called in the BTU_TASK context to
103 * send control information
107 ******************************************************************************/
108 void port_start_control(tPORT* p_port) {
109 tRFC_MCB* p_mcb = p_port->rfc.p_mcb;
111 if (p_mcb == NULL) return;
113 RFCOMM_ControlReq(p_mcb, p_port->dlci, &p_port->local_ctrl);
116 /*******************************************************************************
118 * Function port_start_par_neg
120 * Description This function is called in the BTU_TASK context to
121 * send configuration information
125 ******************************************************************************/
126 void port_start_par_neg(tPORT* p_port) {
127 tRFC_MCB* p_mcb = p_port->rfc.p_mcb;
129 if (p_mcb == NULL) return;
131 RFCOMM_PortNegReq(p_mcb, p_port->dlci, &p_port->user_port_pars);
134 /*******************************************************************************
136 * Function port_start_close
138 * Description This function is called in the BTU_TASK context to
143 ******************************************************************************/
144 void port_start_close(tPORT* p_port) {
145 tRFC_MCB* p_mcb = p_port->rfc.p_mcb;
149 /* At first indicate to the user that signals on the connection were dropped
151 p_port->line_status |= LINE_STATUS_FAILED;
152 old_signals = p_port->peer_ctrl.modem_signal;
154 p_port->peer_ctrl.modem_signal &=
155 ~(PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON);
157 events |= port_get_signal_changes(p_port, old_signals,
158 p_port->peer_ctrl.modem_signal);
160 if (p_port->ev_mask & PORT_EV_CONNECT_ERR) events |= PORT_EV_CONNECT_ERR;
162 if (p_port->ev_mask & PORT_EV_ERR) events |= PORT_EV_ERR;
164 if ((p_port->p_callback != NULL) && events)
165 p_port->p_callback(events, p_port->inx);
167 /* Check if RFCOMM side has been closed while the message was queued */
168 if ((p_mcb == NULL) || (p_port->rfc.state == RFC_STATE_CLOSED)) {
169 /* Call management callback function before calling port_release_port() to
171 if (p_port->p_mgmt_callback)
172 p_port->p_mgmt_callback(PORT_CLOSED, p_port->inx);
174 port_release_port(p_port);
176 RFCOMM_DlcReleaseReq(p_mcb, p_port->dlci);
180 /*******************************************************************************
182 * Function PORT_StartCnf
184 * Description This function is called from the RFCOMM layer when
185 * establishing of the multiplexer channel is completed.
186 * Continue establishing of the connection for all ports that
187 * are in the OPENING state
189 ******************************************************************************/
190 void PORT_StartCnf(tRFC_MCB* p_mcb, uint16_t result) {
193 bool no_ports_up = true;
195 RFCOMM_TRACE_EVENT("PORT_StartCnf result:%d", result);
197 p_port = &rfc_cb.port.port[0];
198 for (i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
199 if (p_port->rfc.p_mcb == p_mcb) {
202 if (result == RFCOMM_SUCCESS)
203 RFCOMM_ParNegReq(p_mcb, p_port->dlci, p_port->mtu);
205 RFCOMM_TRACE_WARNING("PORT_StartCnf failed result:%d", result);
207 /* Warning: result is also set to 4 when l2cap connection
208 fails due to l2cap connect cnf (no_resources) */
209 if (result == HCI_ERR_PAGE_TIMEOUT)
210 p_port->error = PORT_PAGE_TIMEOUT;
212 p_port->error = PORT_START_FAILED;
214 rfc_release_multiplexer_channel(p_mcb);
216 /* Send event to the application */
217 if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECT_ERR))
218 (p_port->p_callback)(PORT_EV_CONNECT_ERR, p_port->inx);
220 if (p_port->p_mgmt_callback)
221 p_port->p_mgmt_callback(PORT_START_FAILED, p_port->inx);
223 port_release_port(p_port);
228 /* There can be a situation when after starting connection, user closes the */
229 /* port, we can catch it here to close multiplexor channel */
231 rfc_check_mcb_active(p_mcb);
235 /*******************************************************************************
237 * Function PORT_StartInd
239 * Description This function is called from the RFCOMM layer when
240 * some peer device wants to establish a multiplexer
241 * connection. Check if there are any ports open with this
242 * or not assigned multiplexer.
244 ******************************************************************************/
245 void PORT_StartInd(tRFC_MCB* p_mcb) {
249 RFCOMM_TRACE_EVENT("PORT_StartInd");
251 p_port = &rfc_cb.port.port[0];
252 for (i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
253 if ((p_port->rfc.p_mcb == NULL) || (p_port->rfc.p_mcb == p_mcb)) {
255 "PORT_StartInd, RFCOMM_StartRsp RFCOMM_SUCCESS: p_mcb:%p", p_mcb);
256 RFCOMM_StartRsp(p_mcb, RFCOMM_SUCCESS);
260 RFCOMM_StartRsp(p_mcb, RFCOMM_ERROR);
263 /*******************************************************************************
265 * Function PORT_ParNegInd
267 * Description This function is called from the RFCOMM layer to change
268 * DLCI parameters (currently only MTU is negotiated).
269 * If can not find the port do not accept the request.
270 * Otherwise save the MTU size supported by the peer.
272 ******************************************************************************/
273 void PORT_ParNegInd(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu, uint8_t cl,
275 tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
279 RFCOMM_TRACE_EVENT("PORT_ParNegInd dlci:%d mtu:%d", dlci, mtu);
282 /* This can be a first request for this port */
283 p_port = port_find_dlci_port(dlci);
285 /* If the port cannot be opened, send a DM. Per Errata 1205 */
286 rfc_send_dm(p_mcb, dlci, false);
287 /* check if this is the last port open, some headsets have
288 problem, they don't disconnect if we send DM */
289 rfc_check_mcb_active(p_mcb);
290 RFCOMM_TRACE_EVENT("PORT_ParNegInd: port not found");
293 p_mcb->port_inx[dlci] = p_port->inx;
296 p_port->bd_addr = p_mcb->bd_addr;
298 /* Connection is up and we know local and remote features, select MTU */
299 port_select_mtu(p_port);
301 p_port->rfc.p_mcb = p_mcb;
302 p_port->mtu = (p_port->mtu < mtu) ? p_port->mtu : mtu;
303 p_port->peer_mtu = p_port->mtu;
305 /* Negotiate the flow control mechanism. If flow control mechanism for */
306 /* mux has not been set yet, set it now. If either we or peer wants TS 07.10,
308 /* use that. Otherwise both must want credit based, so use that. If flow is
310 /* already defined for this mux, we respond with that value. */
311 if (p_mcb->flow == PORT_FC_UNDEFINED) {
312 if ((PORT_FC_DEFAULT == PORT_FC_TS710) ||
313 (cl == RFCOMM_PN_CONV_LAYER_TYPE_1)) {
314 p_mcb->flow = PORT_FC_TS710;
316 p_mcb->flow = PORT_FC_CREDIT;
320 /* Regardless of our flow control mechanism, if the PN cl is zero, we must */
321 /* respond with zero. "A responding implementation must set this field to 14
323 /* if (and only if) the PN request was 15." This could happen if a PN is sent
325 /* after the DLCI is already established-- the PN in that case must have cl =
327 /* See RFCOMM spec 5.5.3 */
328 if (cl == RFCOMM_PN_CONV_LAYER_TYPE_1) {
329 our_cl = RFCOMM_PN_CONV_LAYER_TYPE_1;
331 } else if (p_mcb->flow == PORT_FC_CREDIT) {
333 port_get_credits(p_port, k);
335 /* Set convergence layer and number of credits (k) */
336 our_cl = RFCOMM_PN_CONV_LAYER_CBFC_R;
337 our_k = (p_port->credit_rx_max < RFCOMM_K_MAX) ? p_port->credit_rx_max
339 p_port->credit_rx = our_k;
341 /* must not be using credit based flow control; use TS 7.10 */
342 our_cl = RFCOMM_PN_CONV_LAYER_TYPE_1;
345 RFCOMM_ParNegRsp(p_mcb, dlci, p_port->mtu, our_cl, our_k);
348 /*******************************************************************************
350 * Function PORT_ParNegCnf
352 * Description This function is called from the RFCOMM layer to change
353 * DLCI parameters (currently only MTU is negotiated).
354 * Save the MTU size supported by the peer.
355 * If the confirmation is received during the port opening
356 * procedure send EstablishRequest to continue.
358 ******************************************************************************/
359 void PORT_ParNegCnf(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu, uint8_t cl,
361 tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
363 RFCOMM_TRACE_EVENT("PORT_ParNegCnf dlci:%d mtu:%d cl: %d k: %d", dlci, mtu,
368 /* Flow control mechanism not set yet. Negotiate flow control mechanism. */
369 if (p_mcb->flow == PORT_FC_UNDEFINED) {
370 /* Our stack is configured for TS07.10 and they responded with credit-based.
372 /* This is illegal-- negotiation fails. */
373 if ((PORT_FC_DEFAULT == PORT_FC_TS710) &&
374 (cl == RFCOMM_PN_CONV_LAYER_CBFC_R)) {
375 rfc_send_disc(p_mcb, p_port->dlci);
376 rfc_port_closed(p_port);
379 /* Our stack is configured for credit-based and they responded with
381 else if (cl == RFCOMM_PN_CONV_LAYER_CBFC_R) {
382 p_mcb->flow = PORT_FC_CREDIT;
384 /* They responded with any other value. Treat this as negotiation to
387 p_mcb->flow = PORT_FC_TS710;
390 /* If mux flow control mechanism set, we honor that setting regardless of */
391 /* the CL value in their response. This allows us to gracefully accept any */
392 /* illegal PN negotiation scenarios. */
394 p_port->mtu = (p_port->mtu < mtu) ? p_port->mtu : mtu;
395 p_port->peer_mtu = p_port->mtu;
397 if (p_mcb->flow == PORT_FC_CREDIT) {
398 port_get_credits(p_port, k);
401 if (p_port->state == PORT_STATE_OPENING)
402 RFCOMM_DlcEstablishReq(p_mcb, p_port->dlci, p_port->mtu);
405 /*******************************************************************************
407 * Function PORT_DlcEstablishInd
409 * Description This function is called from the RFCOMM layer when peer
410 * device wants to establish a new DLC. If this is not the
411 * first message in the establishment procedure port_handle
412 * has a handle to the port control block otherwise the control
413 * block should be found based on the muliplexer channel and
414 * dlci. The block should be allocated allocated before
415 * meaning that application already made open.
417 ******************************************************************************/
418 void PORT_DlcEstablishInd(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu) {
419 tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
422 "PORT_DlcEstablishInd p_mcb:%p, dlci:%d mtu:%di, p_port:%p", p_mcb, dlci,
424 VLOG(1) << __func__ << " p_mcb addr:" << p_mcb->bd_addr;
427 /* This can be a first request for this port */
428 p_port = port_find_dlci_port(dlci);
430 RFCOMM_DlcEstablishRsp(p_mcb, dlci, 0, RFCOMM_ERROR);
433 p_mcb->port_inx[dlci] = p_port->inx;
436 /* If L2CAP's mtu less then RFCOMM's take it */
437 if (mtu && (mtu < p_port->peer_mtu)) p_port->peer_mtu = mtu;
439 /* If there was an inactivity timer running for MCB stop it */
440 rfc_timer_stop(p_mcb);
442 RFCOMM_DlcEstablishRsp(p_mcb, dlci, p_port->mtu, RFCOMM_SUCCESS);
444 /* This is the server side. If application wants to know when connection */
445 /* is established, thats the place */
446 if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECTED))
447 (p_port->p_callback)(PORT_EV_CONNECTED, p_port->inx);
449 if (p_port->p_mgmt_callback)
450 p_port->p_mgmt_callback(PORT_SUCCESS, p_port->inx);
452 p_port->state = PORT_STATE_OPENED;
455 /*******************************************************************************
457 * Function PORT_DlcEstablishCnf
459 * Description This function is called from the RFCOMM layer when peer
460 * acknowledges establish procedure (SABME/UA). Send reply
461 * to the user and set state to OPENED if result was
464 ******************************************************************************/
465 void PORT_DlcEstablishCnf(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu,
467 tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
469 RFCOMM_TRACE_EVENT("PORT_DlcEstablishCnf dlci:%d mtu:%d result:%d", dlci, mtu,
474 if (result != RFCOMM_SUCCESS) {
475 p_port->error = PORT_START_FAILED;
476 port_rfc_closed(p_port, PORT_START_FAILED);
480 /* If L2CAP's mtu less then RFCOMM's take it */
481 if (mtu && (mtu < p_port->peer_mtu)) p_port->peer_mtu = mtu;
483 /* If there was an inactivity timer running for MCB stop it */
484 rfc_timer_stop(p_mcb);
486 if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECTED))
487 (p_port->p_callback)(PORT_EV_CONNECTED, p_port->inx);
489 if (p_port->p_mgmt_callback)
490 p_port->p_mgmt_callback(PORT_SUCCESS, p_port->inx);
492 p_port->state = PORT_STATE_OPENED;
494 /* RPN is required only if we want to tell DTE how the port should be opened
496 if ((p_port->uuid == UUID_SERVCLASS_DIALUP_NETWORKING) ||
497 (p_port->uuid == UUID_SERVCLASS_FAX))
498 RFCOMM_PortNegReq(p_port->rfc.p_mcb, p_port->dlci, NULL);
500 RFCOMM_ControlReq(p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
503 /*******************************************************************************
505 * Function PORT_PortNegInd
507 * Description This function is called from the RFCOMM layer when peer
508 * device wants to set parameters of the port. As per the spec
509 * this message has to be sent before the first data packet
510 * and can be sent before establish. The block should be
511 * allocated before meaning that application already made open.
513 ******************************************************************************/
514 void PORT_PortNegInd(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_STATE* p_pars,
515 uint16_t param_mask) {
516 tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
518 RFCOMM_TRACE_EVENT("PORT_PortNegInd");
521 /* This can be a first request for this port */
522 p_port = port_find_dlci_port(dlci);
524 RFCOMM_PortNegRsp(p_mcb, dlci, p_pars, 0);
527 p_mcb->port_inx[dlci] = p_port->inx;
530 /* Check if the flow control is acceptable on local side */
531 p_port->peer_port_pars = *p_pars;
532 RFCOMM_PortNegRsp(p_mcb, dlci, p_pars, param_mask);
535 /*******************************************************************************
537 * Function PORT_PortNegCnf
539 * Description This function is called from the RFCOMM layer to change
540 * state for the port. Propagate change to the user.
542 ******************************************************************************/
543 void PORT_PortNegCnf(tRFC_MCB* p_mcb, uint8_t dlci,
544 UNUSED_ATTR tPORT_STATE* p_pars, uint16_t result) {
545 tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
547 RFCOMM_TRACE_EVENT("PORT_PortNegCnf");
550 RFCOMM_TRACE_WARNING("PORT_PortNegCnf no port");
553 /* Port negotiation failed. Drop the connection */
554 if (result != RFCOMM_SUCCESS) {
555 p_port->error = PORT_PORT_NEG_FAILED;
557 RFCOMM_DlcReleaseReq(p_mcb, p_port->dlci);
559 port_rfc_closed(p_port, PORT_PORT_NEG_FAILED);
563 if (!(p_port->port_ctrl & PORT_CTRL_REQ_SENT)) {
564 RFCOMM_ControlReq(p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
566 RFCOMM_TRACE_WARNING("PORT_PortNegCnf Control Already sent");
570 /*******************************************************************************
572 * Function PORT_ControlInd
574 * Description This function is called from the RFCOMM layer on the modem
575 * signal change. Propagate change to the user.
577 ******************************************************************************/
578 void PORT_ControlInd(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_CTRL* p_pars) {
579 tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
583 RFCOMM_TRACE_EVENT("PORT_ControlInd");
587 old_signals = p_port->peer_ctrl.modem_signal;
589 event = port_get_signal_changes(p_port, old_signals, p_pars->modem_signal);
591 p_port->peer_ctrl = *p_pars;
593 if (!(p_port->port_ctrl & PORT_CTRL_REQ_SENT))
594 RFCOMM_ControlReq(p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
596 /* If this is the first time we received control RFCOMM is connected */
597 if (!(p_port->port_ctrl & PORT_CTRL_IND_RECEIVED)) {
598 event |= (PORT_EV_CONNECTED & p_port->ev_mask);
601 if (p_port->port_ctrl & PORT_CTRL_REQ_CONFIRMED) {
602 event |= port_rfc_send_tx_data(p_port);
606 p_port->port_ctrl |= (PORT_CTRL_IND_RECEIVED | PORT_CTRL_IND_RESPONDED);
608 if (p_pars->break_signal) event |= (PORT_EV_BREAK & p_port->ev_mask);
610 /* execute call back function only if the application is registered for events
612 if (event && p_port->p_callback) (p_port->p_callback)(event, p_port->inx);
615 "PORT_ControlInd DTR_DSR : %d, RTS_CTS : %d, RI : %d, DCD : %d",
616 ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_DTRDSR) ? 1 : 0),
617 ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_RTSCTS) ? 1 : 0),
618 ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_RI) ? 1 : 0),
619 ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_DCD) ? 1 : 0));
622 /*******************************************************************************
624 * Function PORT_ControlCnf
626 * Description This function is called from the RFCOMM layer when
627 * peer acknowleges change of the modem signals.
629 ******************************************************************************/
630 void PORT_ControlCnf(tRFC_MCB* p_mcb, uint8_t dlci,
631 UNUSED_ATTR tPORT_CTRL* p_pars) {
632 tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
635 RFCOMM_TRACE_EVENT("PORT_ControlCnf");
639 if (!(p_port->port_ctrl & PORT_CTRL_REQ_CONFIRMED)) {
640 p_port->port_ctrl |= PORT_CTRL_REQ_CONFIRMED;
642 if (p_port->port_ctrl & PORT_CTRL_IND_RECEIVED)
643 event = (p_port->ev_mask & PORT_EV_CONNECTED);
646 if (p_port->port_ctrl & PORT_CTRL_IND_RECEIVED) {
647 event |= port_rfc_send_tx_data(p_port);
650 /* execute call back function only if the application is registered for events
652 if (event && p_port->p_callback) (p_port->p_callback)(event, p_port->inx);
655 /*******************************************************************************
657 * Function PORT_LineStatusInd
659 * Description This function is called from the RFCOMM layer when
660 * peer indicates change in the line status
662 ******************************************************************************/
663 void PORT_LineStatusInd(tRFC_MCB* p_mcb, uint8_t dlci, uint8_t line_status) {
664 tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
667 RFCOMM_TRACE_EVENT("PORT_LineStatusInd");
671 p_port->line_status |= line_status;
673 if (line_status & PORT_ERR_OVERRUN) event |= PORT_EV_OVERRUN;
675 if (line_status & PORT_ERR_BREAK) event |= PORT_EV_BREAK;
677 if (line_status & ~(PORT_ERR_OVERRUN | PORT_ERR_BREAK)) event |= PORT_EV_ERR;
679 if ((p_port->p_callback != NULL) && (p_port->ev_mask & event))
680 p_port->p_callback((p_port->ev_mask & event), p_port->inx);
683 /*******************************************************************************
685 * Function PORT_DlcReleaseInd
687 * Description This function is called from the RFCOMM layer when
688 * DLC connection is released.
690 ******************************************************************************/
691 void PORT_DlcReleaseInd(tRFC_MCB* p_mcb, uint8_t dlci) {
692 tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
694 RFCOMM_TRACE_EVENT("PORT_DlcReleaseInd");
698 port_rfc_closed(p_port, PORT_CLOSED);
701 /*******************************************************************************
703 * Function PORT_CloseInd
705 * Description This function is called from the RFCOMM layer when
706 * multiplexer connection is released.
708 ******************************************************************************/
709 void PORT_CloseInd(tRFC_MCB* p_mcb) {
713 RFCOMM_TRACE_EVENT("PORT_CloseInd");
715 p_port = &rfc_cb.port.port[0];
716 for (i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
717 if (p_port->rfc.p_mcb == p_mcb) {
718 port_rfc_closed(p_port, PORT_PEER_CONNECTION_FAILED);
721 rfc_release_multiplexer_channel(p_mcb);
724 /*******************************************************************************
726 * Function Port_TimeOutCloseMux
728 * Description This function is called when RFCOMM timesout on a command
729 * as a result multiplexer connection is closed.
731 ******************************************************************************/
732 void Port_TimeOutCloseMux(tRFC_MCB* p_mcb) {
736 RFCOMM_TRACE_EVENT("Port_TimeOutCloseMux");
738 p_port = &rfc_cb.port.port[0];
739 for (i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
740 if (p_port->rfc.p_mcb == p_mcb) {
741 port_rfc_closed(p_port, PORT_PEER_TIMEOUT);
746 /*******************************************************************************
748 * Function PORT_DataInd
750 * Description This function is called from the RFCOMM layer when data
751 * buffer is received from the peer.
753 ******************************************************************************/
754 void PORT_DataInd(tRFC_MCB* p_mcb, uint8_t dlci, BT_HDR* p_buf) {
755 tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
762 "PORT_DataInd with data length %d, p_mcb:%p,p_port:%p,dlci:%d",
763 p_buf->len, p_mcb, p_port, dlci);
768 /* If client registered callout callback with flow control we can just deliver
770 if (p_port->p_data_co_callback) {
771 /* Another packet is delivered to user. Send credits to peer if required */
772 if (p_port->p_data_co_callback(p_port->inx, (uint8_t*)p_buf, -1,
773 DATA_CO_CALLBACK_TYPE_INCOMING)) {
774 port_flow_control_peer(p_port, true, 1);
776 port_flow_control_peer(p_port, false, 0);
781 /* If client registered callback we can just deliver receive data */
782 if (p_port->p_data_callback) {
783 /* Another packet is delivered to user. Send credits to peer if required */
784 port_flow_control_peer(p_port, true, 1);
785 p_port->p_data_callback(p_port->inx, (uint8_t*)(p_buf + 1) + p_buf->offset,
790 /* Check if rx queue exceeds the limit */
791 if ((p_port->rx.queue_size + p_buf->len > PORT_RX_CRITICAL_WM) ||
792 (fixed_queue_length(p_port->rx.queue) + 1 > p_port->rx_buf_critical)) {
793 RFCOMM_TRACE_EVENT("PORT_DataInd. Buffer over run. Dropping the buffer");
795 RFCOMM_LineStatusReq(p_mcb, dlci, LINE_STATUS_OVERRUN);
798 /* If user registered to receive notification when a particular byte is */
799 /* received we mast check all received bytes */
800 if (((rx_char1 = p_port->user_port_pars.rx_char1) != 0) &&
801 (p_port->ev_mask & PORT_EV_RXFLAG)) {
802 for (i = 0, p = (uint8_t*)(p_buf + 1) + p_buf->offset; i < p_buf->len;
804 if (*p++ == rx_char1) {
805 events |= PORT_EV_RXFLAG;
813 fixed_queue_enqueue(p_port->rx.queue, p_buf);
814 p_port->rx.queue_size += p_buf->len;
816 mutex_global_unlock();
818 /* perform flow control procedures if necessary */
819 port_flow_control_peer(p_port, false, 0);
821 /* If user indicated flow control can not deliver any notifications to him */
822 if (p_port->rx.user_fc) {
823 if (events & PORT_EV_RXFLAG) {
824 p_port->rx_flag_ev_pending = true;
829 events |= PORT_EV_RXCHAR;
831 /* Mask out all events that are not of interest to user */
832 events &= p_port->ev_mask;
834 if (p_port->p_callback && events) p_port->p_callback(events, p_port->inx);
837 /*******************************************************************************
839 * Function PORT_FlowInd
841 * Description This function is called from the RFCOMM layer on the flow
842 * control signal change. Propagate change to the user.
844 ******************************************************************************/
845 void PORT_FlowInd(tRFC_MCB* p_mcb, uint8_t dlci, bool enable_data) {
846 tPORT* p_port = (tPORT*)NULL;
850 RFCOMM_TRACE_EVENT("PORT_FlowInd fc:%d", enable_data);
853 p_mcb->peer_ready = enable_data;
855 p_port = port_find_mcb_dlci_port(p_mcb, dlci);
856 if (p_port == NULL) return;
858 p_port->tx.peer_fc = !enable_data;
861 for (i = 0; i < MAX_RFC_PORTS; i++) {
862 /* If DLCI is 0 event applies to all ports */
864 p_port = &rfc_cb.port.port[i];
865 if (!p_port->in_use || (p_port->rfc.p_mcb != p_mcb) ||
866 (p_port->rfc.state != RFC_STATE_OPENED))
871 /* Check if flow of data is still enabled */
872 events |= port_flow_control_user(p_port);
874 /* Check if data can be sent and send it */
875 events |= port_rfc_send_tx_data(p_port);
877 /* Mask out all events that are not of interest to user */
878 events &= p_port->ev_mask;
880 /* Send event to the application */
881 if (p_port->p_callback && events) (p_port->p_callback)(events, p_port->inx);
883 /* If DLCI is not 0 event applies to one port only */
884 if (dlci != 0) break;
888 /*******************************************************************************
890 * Function port_rfc_send_tx_data
892 * Description This function is when forward data can be sent to the peer
894 ******************************************************************************/
895 uint32_t port_rfc_send_tx_data(tPORT* p_port) {
899 /* if there is data to be sent */
900 if (p_port->tx.queue_size > 0) {
901 /* while the rfcomm peer is not flow controlling us, and peer is ready */
902 while (!p_port->tx.peer_fc && p_port->rfc.p_mcb &&
903 p_port->rfc.p_mcb->peer_ready) {
904 /* get data from tx queue and send it */
907 p_buf = (BT_HDR*)fixed_queue_try_dequeue(p_port->tx.queue);
909 p_port->tx.queue_size -= p_buf->len;
911 mutex_global_unlock();
913 RFCOMM_TRACE_DEBUG("Sending RFCOMM_DataReq tx.queue_size=%d",
914 p_port->tx.queue_size);
916 RFCOMM_DataReq(p_port->rfc.p_mcb, p_port->dlci, p_buf);
918 events |= PORT_EV_TXCHAR;
920 if (p_port->tx.queue_size == 0) {
921 events |= PORT_EV_TXEMPTY;
925 /* queue is empty-- all data sent */
927 mutex_global_unlock();
929 events |= PORT_EV_TXEMPTY;
933 /* If we flow controlled user based on the queue size enable data again */
934 events |= port_flow_control_user(p_port);
936 return (events & p_port->ev_mask);
939 /*******************************************************************************
941 * Function port_rfc_closed
943 * Description This function when RFCOMM side of port is closed
945 ******************************************************************************/
946 void port_rfc_closed(tPORT* p_port, uint8_t res) {
949 tRFC_MCB* p_mcb = p_port->rfc.p_mcb;
951 if ((p_port->state == PORT_STATE_OPENING) && (p_port->is_server)) {
952 /* The servr side has not been informed that connection is up, ignore */
953 RFCOMM_TRACE_EVENT("port_rfc_closed in OPENING state ignored");
955 rfc_port_timer_stop(p_port);
956 p_port->rfc.state = RFC_STATE_CLOSED;
959 p_mcb->port_inx[p_port->dlci] = 0;
961 /* If there are no more ports opened on this MCB release it */
962 rfc_check_mcb_active(p_mcb);
963 p_port->rfc.p_mcb = NULL;
966 /* Need to restore DLCI to listening state
967 * if the server was on the initiating RFC
969 p_port->dlci &= 0xfe;
974 if ((p_port->state != PORT_STATE_CLOSING) &&
975 (p_port->state != PORT_STATE_CLOSED)) {
976 p_port->line_status |= LINE_STATUS_FAILED;
978 old_signals = p_port->peer_ctrl.modem_signal;
980 p_port->peer_ctrl.modem_signal &=
981 ~(PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON);
983 events |= port_get_signal_changes(p_port, old_signals,
984 p_port->peer_ctrl.modem_signal);
986 if (p_port->ev_mask & PORT_EV_CONNECT_ERR) events |= PORT_EV_CONNECT_ERR;
988 RFCOMM_TRACE_EVENT("port_rfc_closed state:%d sending events:%x",
989 p_port->state, events);
991 if ((p_port->p_callback != NULL) && events)
992 p_port->p_callback(events, p_port->inx);
994 if (p_port->p_mgmt_callback) p_port->p_mgmt_callback(res, p_port->inx);
996 p_port->rfc.state = RFC_STATE_CLOSED;
998 RFCOMM_TRACE_WARNING("%s RFCOMM connection in state %d closed: %s (res: %d)",
999 __func__, p_port->state, PORT_GetResultString(res), res);
1001 port_release_port(p_port);
1004 /*******************************************************************************
1006 * Function port_get_credits
1008 * Description Set initial values for credits.
1009 * Adjust max number of rx credits based on negotiated MTU.
1010 * Check max allowed num of bytes, max allowed num buffers,
1011 * should be less then 255
1013 ******************************************************************************/
1014 void port_get_credits(tPORT* p_port, uint8_t k) {
1015 p_port->credit_tx = k;
1016 if (p_port->credit_tx == 0) p_port->tx.peer_fc = true;