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