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 file contains the Serial Port API code
     22  *
     23  ******************************************************************************/
     24 
     25 #include <string.h>
     26 #include "bt_target.h"
     27 #include "gki.h"
     28 #include "rfcdefs.h"
     29 #include "port_api.h"
     30 #include "port_int.h"
     31 #include "btm_int.h"
     32 #include "btm_api.h"
     33 #include "rfc_int.h"
     34 #include "l2c_api.h"
     35 #include "sdp_api.h"
     36 
     37 /* duration of break in 200ms units */
     38 #define PORT_BREAK_DURATION     1
     39 
     40 #include <cutils/log.h>
     41 #define info(fmt, ...)  ALOGI ("%s: " fmt,__FUNCTION__,  ## __VA_ARGS__)
     42 #define debug(fmt, ...) ALOGD ("%s: " fmt,__FUNCTION__,  ## __VA_ARGS__)
     43 #define error(fmt, ...) ALOGE ("## ERROR : %s: " fmt "##",__FUNCTION__,  ## __VA_ARGS__)
     44 #define asrt(s) if(!(s)) ALOGE ("## %s assert %s failed at line:%d ##",__FUNCTION__, #s, __LINE__)
     45 
     46 /*******************************************************************************
     47 **
     48 ** Function         RFCOMM_CreateConnection
     49 **
     50 ** Description      RFCOMM_CreateConnection function is used from the application
     51 **                  to establish serial port connection to the peer device,
     52 **                  or allow RFCOMM to accept a connection from the peer
     53 **                  application.
     54 **
     55 ** Parameters:      scn          - Service Channel Number as registered with
     56 **                                 the SDP (server) or obtained using SDP from
     57 **                                 the peer device (client).
     58 **                  is_server    - TRUE if requesting application is a server
     59 **                  mtu          - Maximum frame size the application can accept
     60 **                  bd_addr      - BD_ADDR of the peer (client)
     61 **                  mask         - specifies events to be enabled.  A value
     62 **                                 of zero disables all events.
     63 **                  p_handle     - OUT pointer to the handle.
     64 **                  p_mgmt_cb    - pointer to callback function to receive
     65 **                                 connection up/down events.
     66 ** Notes:
     67 **
     68 ** Server can call this function with the same scn parameter multiple times if
     69 ** it is ready to accept multiple simulteneous connections.
     70 **
     71 ** DLCI for the connection is (scn * 2 + 1) if client originates connection on
     72 ** existing none initiator multiplexer channel.  Otherwise it is (scn * 2).
     73 ** For the server DLCI can be changed later if client will be calling it using
     74 ** (scn * 2 + 1) dlci.
     75 **
     76 *******************************************************************************/
     77 int RFCOMM_CreateConnection (UINT16 uuid, UINT8 scn, BOOLEAN is_server,
     78                              UINT16 mtu, BD_ADDR bd_addr, UINT16 *p_handle,
     79                              tPORT_CALLBACK *p_mgmt_cb)
     80 {
     81     tPORT      *p_port;
     82     int        i;
     83     UINT8      dlci;
     84     tRFC_MCB   *p_mcb = port_find_mcb (bd_addr);
     85     UINT16     rfcomm_mtu;
     86 
     87     RFCOMM_TRACE_API ("RFCOMM_CreateConnection()  BDA: %02x-%02x-%02x-%02x-%02x-%02x",
     88                        bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
     89 
     90     *p_handle = 0;
     91 
     92     if (( scn == 0 )||(scn >= PORT_MAX_RFC_PORTS ))
     93     {
     94         /* Server Channel Number(SCN) should be in range 1...30 */
     95         RFCOMM_TRACE_ERROR ("RFCOMM_CreateConnection - invalid SCN");
     96         return (PORT_INVALID_SCN);
     97     }
     98 
     99     /* For client that originate connection on the existing none initiator */
    100     /* multiplexer channel DLCI should be odd */
    101     if (p_mcb && !p_mcb->is_initiator && !is_server)
    102         dlci = (scn << 1) + 1;
    103     else
    104         dlci = (scn << 1);
    105     RFCOMM_TRACE_API("RFCOMM_CreateConnection(): scn:%d, dlci:%d, is_server:%d mtu:%d, p_mcb:%p",
    106                        scn, dlci, is_server, mtu, p_mcb);
    107 
    108     /* For the server side always allocate a new port.  On the client side */
    109     /* do not allow the same (dlci, bd_addr) to be opened twice by application */
    110     if (!is_server && ((p_port = port_find_port (dlci, bd_addr)) != NULL))
    111     {
    112         /* if existing port is also a client port */
    113         if (p_port->is_server == FALSE)
    114         {
    115             RFCOMM_TRACE_ERROR ("RFCOMM_CreateConnection - already opened state:%d, RFC state:%d, MCB state:%d",
    116                 p_port->state, p_port->rfc.state, p_port->rfc.p_mcb ? p_port->rfc.p_mcb->state : 0);
    117             return (PORT_ALREADY_OPENED);
    118         }
    119     }
    120 
    121     if ((p_port = port_allocate_port (dlci, bd_addr)) == NULL)
    122     {
    123         RFCOMM_TRACE_WARNING ("RFCOMM_CreateConnection - no resources");
    124         return (PORT_NO_RESOURCES);
    125     }
    126    RFCOMM_TRACE_API("RFCOMM_CreateConnection(): scn:%d, dlci:%d, is_server:%d mtu:%d, p_mcb:%p, p_port:%p",
    127                        scn, dlci, is_server, mtu, p_mcb, p_port);
    128 
    129     p_port->default_signal_state = (PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON);
    130 
    131     switch (uuid)
    132     {
    133     case UUID_PROTOCOL_OBEX:
    134         p_port->default_signal_state = PORT_OBEX_DEFAULT_SIGNAL_STATE;
    135         break;
    136     case UUID_SERVCLASS_SERIAL_PORT:
    137         p_port->default_signal_state = PORT_SPP_DEFAULT_SIGNAL_STATE;
    138         break;
    139     case UUID_SERVCLASS_LAN_ACCESS_USING_PPP:
    140         p_port->default_signal_state = PORT_PPP_DEFAULT_SIGNAL_STATE;
    141         break;
    142     case UUID_SERVCLASS_DIALUP_NETWORKING:
    143     case UUID_SERVCLASS_FAX:
    144         p_port->default_signal_state = PORT_DUN_DEFAULT_SIGNAL_STATE;
    145         break;
    146     }
    147 
    148     RFCOMM_TRACE_EVENT ("RFCOMM_CreateConnection dlci:%d signal state:0x%x", dlci, p_port->default_signal_state);
    149 
    150     *p_handle = p_port->inx;
    151 
    152     p_port->state        = PORT_STATE_OPENING;
    153     p_port->uuid         = uuid;
    154     p_port->is_server    = is_server;
    155     p_port->scn          = scn;
    156     p_port->ev_mask      = 0;
    157 
    158     /* If the MTU is not specified (0), keep MTU decision until the
    159      * PN frame has to be send
    160      * at that time connection should be established and we
    161      * will know for sure our prefered MTU
    162      */
    163 
    164     rfcomm_mtu = L2CAP_MTU_SIZE - RFCOMM_DATA_OVERHEAD;
    165 
    166     if (mtu)
    167         p_port->mtu      = (mtu < rfcomm_mtu) ? mtu : rfcomm_mtu;
    168     else
    169         p_port->mtu      = rfcomm_mtu;
    170 
    171     /* server doesn't need to release port when closing */
    172     if( is_server )
    173     {
    174         p_port->keep_port_handle = TRUE;
    175 
    176         /* keep mtu that user asked, p_port->mtu could be updated during param negotiation */
    177         p_port->keep_mtu         = p_port->mtu;
    178     }
    179 
    180     p_port->local_ctrl.modem_signal = p_port->default_signal_state;
    181     p_port->local_ctrl.fc           = FALSE;
    182 
    183     p_port->p_mgmt_callback = p_mgmt_cb;
    184 
    185     for (i = 0; i < BD_ADDR_LEN; i++)
    186         p_port->bd_addr[i] = bd_addr[i];
    187 
    188     /* If this is not initiator of the connection need to just wait */
    189     if (p_port->is_server)
    190     {
    191         return (PORT_SUCCESS);
    192     }
    193 
    194     /* Open will be continued after security checks are passed */
    195     return port_open_continue (p_port);
    196 }
    197 
    198 
    199 /*******************************************************************************
    200 **
    201 ** Function         RFCOMM_RemoveConnection
    202 **
    203 ** Description      This function is called to close the specified connection.
    204 **
    205 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
    206 **
    207 *******************************************************************************/
    208 int RFCOMM_RemoveConnection (UINT16 handle)
    209 {
    210     tPORT      *p_port;
    211 
    212     RFCOMM_TRACE_API ("RFCOMM_RemoveConnection() handle:%d", handle);
    213 
    214     /* Check if handle is valid to avoid crashing */
    215     if ((handle == 0) || (handle > MAX_RFC_PORTS))
    216     {
    217         RFCOMM_TRACE_ERROR ("RFCOMM_RemoveConnection() BAD handle:%d", handle);
    218         return (PORT_BAD_HANDLE);
    219     }
    220     p_port = &rfc_cb.port.port[handle - 1];
    221 
    222     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
    223     {
    224         RFCOMM_TRACE_EVENT ("RFCOMM_RemoveConnection() Not opened:%d", handle);
    225         return (PORT_SUCCESS);
    226     }
    227 
    228     p_port->state = PORT_STATE_CLOSING;
    229 
    230     port_start_close (p_port);
    231 
    232     return (PORT_SUCCESS);
    233 }
    234 
    235 /*******************************************************************************
    236 **
    237 ** Function         RFCOMM_RemoveServer
    238 **
    239 ** Description      This function is called to close the server port.
    240 **
    241 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
    242 **
    243 *******************************************************************************/
    244 int RFCOMM_RemoveServer (UINT16 handle)
    245 {
    246     tPORT      *p_port;
    247 
    248     RFCOMM_TRACE_API ("RFCOMM_RemoveServer() handle:%d", handle);
    249 
    250     /* Check if handle is valid to avoid crashing */
    251     if ((handle == 0) || (handle > MAX_RFC_PORTS))
    252     {
    253         RFCOMM_TRACE_ERROR ("RFCOMM_RemoveServer() BAD handle:%d", handle);
    254         return (PORT_BAD_HANDLE);
    255     }
    256     p_port = &rfc_cb.port.port[handle - 1];
    257 
    258     /* Do not report any events to the client any more. */
    259     p_port->p_mgmt_callback = NULL;
    260 
    261     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
    262     {
    263         RFCOMM_TRACE_EVENT ("RFCOMM_RemoveServer() Not opened:%d", handle);
    264         return (PORT_SUCCESS);
    265     }
    266 
    267     /* this port will be deallocated after closing */
    268     p_port->keep_port_handle = FALSE;
    269     p_port->state = PORT_STATE_CLOSING;
    270 
    271     port_start_close (p_port);
    272 
    273     return (PORT_SUCCESS);
    274 }
    275 
    276 /*******************************************************************************
    277 **
    278 ** Function         PORT_SetEventCallback
    279 **
    280 ** Description      This function is called to provide an address of the
    281 **                  function which will be called when one of the events
    282 **                  specified in the mask occures.
    283 **
    284 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
    285 **                  p_callback - address of the callback function which should
    286 **                               be called from the RFCOMM when an event
    287 **                               specified in the mask occures.
    288 **
    289 **
    290 *******************************************************************************/
    291 int PORT_SetEventCallback (UINT16 port_handle, tPORT_CALLBACK *p_port_cb)
    292 {
    293     tPORT  *p_port;
    294 
    295     /* Check if handle is valid to avoid crashing */
    296     if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS))
    297     {
    298         return (PORT_BAD_HANDLE);
    299     }
    300 
    301     p_port = &rfc_cb.port.port[port_handle - 1];
    302 
    303     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
    304     {
    305         return (PORT_NOT_OPENED);
    306     }
    307 
    308     RFCOMM_TRACE_API ("PORT_SetEventCallback() handle:%d", port_handle);
    309 
    310     p_port->p_callback = p_port_cb;
    311 
    312     return (PORT_SUCCESS);
    313 }
    314 /*******************************************************************************
    315 **
    316 ** Function         PORT_ClearKeepHandleFlag
    317 **
    318 ** Description      This function is called to clear the keep handle flag
    319 **                  which will cause not to keep the port handle open when closed
    320 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
    321 **
    322 *******************************************************************************/
    323 
    324 int PORT_ClearKeepHandleFlag (UINT16 port_handle)
    325 {
    326     tPORT  *p_port;
    327 
    328     /* Check if handle is valid to avoid crashing */
    329     if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS))
    330     {
    331         return (PORT_BAD_HANDLE);
    332     }
    333 
    334     p_port = &rfc_cb.port.port[port_handle - 1];
    335     p_port->keep_port_handle = 0;
    336     return (PORT_SUCCESS);
    337 }
    338 
    339 /*******************************************************************************
    340 **
    341 ** Function         PORT_SetDataCallback
    342 **
    343 ** Description      This function is when a data packet is received
    344 **
    345 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
    346 **                  p_callback - address of the callback function which should
    347 **                               be called from the RFCOMM when data packet
    348 **                               is received.
    349 **
    350 **
    351 *******************************************************************************/
    352 int PORT_SetDataCallback (UINT16 port_handle, tPORT_DATA_CALLBACK *p_port_cb)
    353 {
    354     tPORT  *p_port;
    355 
    356     RFCOMM_TRACE_API ("PORT_SetDataCallback() handle:%d cb 0x%x", port_handle, p_port_cb);
    357 
    358     /* Check if handle is valid to avoid crashing */
    359     if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS))
    360     {
    361         return (PORT_BAD_HANDLE);
    362     }
    363 
    364     p_port = &rfc_cb.port.port[port_handle - 1];
    365 
    366     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
    367     {
    368         return (PORT_NOT_OPENED);
    369     }
    370 
    371     p_port->p_data_callback = p_port_cb;
    372 
    373     return (PORT_SUCCESS);
    374 }
    375 /*******************************************************************************
    376 **
    377 ** Function         PORT_SetCODataCallback
    378 **
    379 ** Description      This function is when a data packet is received
    380 **
    381 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
    382 **                  p_callback - address of the callback function which should
    383 **                               be called from the RFCOMM when data packet
    384 **                               is received.
    385 **
    386 **
    387 *******************************************************************************/
    388 int PORT_SetDataCOCallback (UINT16 port_handle, tPORT_DATA_CO_CALLBACK *p_port_cb)
    389 {
    390     tPORT  *p_port;
    391 
    392     RFCOMM_TRACE_API ("PORT_SetDataCOCallback() handle:%d cb 0x%x", port_handle, p_port_cb);
    393 
    394     /* Check if handle is valid to avoid crashing */
    395     if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS))
    396     {
    397         return (PORT_BAD_HANDLE);
    398     }
    399 
    400     p_port = &rfc_cb.port.port[port_handle - 1];
    401 
    402     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
    403     {
    404         return (PORT_NOT_OPENED);
    405     }
    406 
    407     p_port->p_data_co_callback = p_port_cb;
    408 
    409     return (PORT_SUCCESS);
    410 }
    411 
    412 
    413 
    414 /*******************************************************************************
    415 **
    416 ** Function         PORT_SetEventMask
    417 **
    418 ** Description      This function is called to close the specified connection.
    419 **
    420 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
    421 **                  mask   - Bitmask of the events the host is interested in
    422 **
    423 *******************************************************************************/
    424 int PORT_SetEventMask (UINT16 port_handle, UINT32 mask)
    425 {
    426     tPORT  *p_port;
    427 
    428     RFCOMM_TRACE_API ("PORT_SetEventMask() handle:%d mask:0x%x", port_handle, mask);
    429 
    430     /* Check if handle is valid to avoid crashing */
    431     if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS))
    432     {
    433         return (PORT_BAD_HANDLE);
    434     }
    435 
    436     p_port = &rfc_cb.port.port[port_handle - 1];
    437 
    438     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
    439     {
    440         return (PORT_NOT_OPENED);
    441     }
    442 
    443     p_port->ev_mask = mask;
    444 
    445     return (PORT_SUCCESS);
    446 }
    447 
    448 
    449 /*******************************************************************************
    450 **
    451 ** Function         PORT_CheckConnection
    452 **
    453 ** Description      This function returns PORT_SUCCESS if connection referenced
    454 **                  by handle is up and running
    455 **
    456 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
    457 **                  bd_addr    - OUT bd_addr of the peer
    458 **                  p_lcid     - OUT L2CAP's LCID
    459 **
    460 *******************************************************************************/
    461 int PORT_CheckConnection (UINT16 handle, BD_ADDR bd_addr, UINT16 *p_lcid)
    462 {
    463     tPORT      *p_port;
    464 
    465     RFCOMM_TRACE_API ("PORT_CheckConnection() handle:%d", handle);
    466 
    467     /* Check if handle is valid to avoid crashing */
    468     if ((handle == 0) || (handle > MAX_RFC_PORTS))
    469     {
    470         return (PORT_BAD_HANDLE);
    471     }
    472 
    473     p_port = &rfc_cb.port.port[handle - 1];
    474 
    475     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
    476     {
    477         return (PORT_NOT_OPENED);
    478     }
    479 
    480     if (!p_port->rfc.p_mcb
    481      || !p_port->rfc.p_mcb->peer_ready
    482      || (p_port->rfc.state != RFC_STATE_OPENED))
    483     {
    484         return (PORT_LINE_ERR);
    485     }
    486 
    487     memcpy (bd_addr, p_port->rfc.p_mcb->bd_addr, BD_ADDR_LEN);
    488     if (p_lcid)
    489         *p_lcid = p_port->rfc.p_mcb->lcid;
    490 
    491     return (PORT_SUCCESS);
    492 }
    493 
    494 /*******************************************************************************
    495 **
    496 ** Function         PORT_IsOpening
    497 **
    498 ** Description      This function returns TRUE if there is any RFCOMM connection
    499 **                  opening in process.
    500 **
    501 ** Parameters:      TRUE if any connection opening is found
    502 **                  bd_addr    - bd_addr of the peer
    503 **
    504 *******************************************************************************/
    505 BOOLEAN PORT_IsOpening (BD_ADDR bd_addr)
    506 {
    507     UINT8   xx, yy;
    508     tRFC_MCB *p_mcb = NULL;
    509     tPORT  *p_port;
    510     BOOLEAN found_port;
    511 
    512     /* Check for any rfc_mcb which is in the middle of opening. */
    513     for (xx = 0; xx < MAX_BD_CONNECTIONS; xx++)
    514     {
    515         if ((rfc_cb.port.rfc_mcb[xx].state > RFC_MX_STATE_IDLE) &&
    516             (rfc_cb.port.rfc_mcb[xx].state < RFC_MX_STATE_CONNECTED))
    517         {
    518             memcpy (bd_addr, rfc_cb.port.rfc_mcb[xx].bd_addr, BD_ADDR_LEN);
    519             return TRUE;
    520         }
    521 
    522         if (rfc_cb.port.rfc_mcb[xx].state == RFC_MX_STATE_CONNECTED)
    523         {
    524             found_port = FALSE;
    525             p_mcb = &rfc_cb.port.rfc_mcb[xx];
    526             p_port = &rfc_cb.port.port[0];
    527 
    528             for (yy = 0; yy < MAX_RFC_PORTS; yy++, p_port++)
    529             {
    530                 if (p_port->rfc.p_mcb == p_mcb)
    531                 {
    532                     found_port = TRUE;
    533                     break;
    534                 }
    535             }
    536 
    537             if ((!found_port) ||
    538                 (found_port && (p_port->rfc.state < RFC_STATE_OPENED)))
    539             {
    540                 /* Port is not established yet. */
    541                 memcpy (bd_addr, rfc_cb.port.rfc_mcb[xx].bd_addr, BD_ADDR_LEN);
    542                 return TRUE;
    543             }
    544         }
    545     }
    546 
    547     return FALSE;
    548 }
    549 
    550 /*******************************************************************************
    551 **
    552 ** Function         PORT_SetState
    553 **
    554 ** Description      This function configures connection according to the
    555 **                  specifications in the tPORT_STATE structure.
    556 **
    557 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
    558 **                  p_settings - Pointer to a tPORT_STATE structure containing
    559 **                               configuration information for the connection.
    560 **
    561 **
    562 *******************************************************************************/
    563 int PORT_SetState (UINT16 handle, tPORT_STATE *p_settings)
    564 {
    565     tPORT      *p_port;
    566     UINT8       baud_rate;
    567 
    568     RFCOMM_TRACE_API ("PORT_SetState() handle:%d", handle);
    569 
    570     /* Check if handle is valid to avoid crashing */
    571     if ((handle == 0) || (handle > MAX_RFC_PORTS))
    572     {
    573         return (PORT_BAD_HANDLE);
    574     }
    575 
    576     p_port = &rfc_cb.port.port[handle - 1];
    577 
    578     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
    579     {
    580         return (PORT_NOT_OPENED);
    581     }
    582 
    583     if (p_port->line_status)
    584     {
    585         return (PORT_LINE_ERR);
    586     }
    587 
    588     RFCOMM_TRACE_API ("PORT_SetState() handle:%d FC_TYPE:0x%x", handle,
    589                        p_settings->fc_type);
    590 
    591     baud_rate = p_port->user_port_pars.baud_rate;
    592     p_port->user_port_pars = *p_settings;
    593 
    594     /* for now we've been asked to pass only baud rate */
    595     if (baud_rate != p_settings->baud_rate)
    596     {
    597         port_start_par_neg (p_port);
    598     }
    599     return (PORT_SUCCESS);
    600 }
    601 
    602 /*******************************************************************************
    603 **
    604 ** Function         PORT_GetRxQueueCnt
    605 **
    606 ** Description      This function return number of buffers on the rx queue.
    607 **
    608 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
    609 **                  p_rx_queue_count - Pointer to return queue count in.
    610 **
    611 *******************************************************************************/
    612 int PORT_GetRxQueueCnt (UINT16 handle, UINT16 *p_rx_queue_count)
    613 {
    614     tPORT      *p_port;
    615 
    616     RFCOMM_TRACE_API ("PORT_GetRxQueueCnt() handle:%d", handle);
    617 
    618     /* Check if handle is valid to avoid crashing */
    619     if ((handle == 0) || (handle > MAX_RFC_PORTS))
    620     {
    621         return (PORT_BAD_HANDLE);
    622     }
    623 
    624     p_port = &rfc_cb.port.port[handle - 1];
    625 
    626     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
    627     {
    628         return (PORT_NOT_OPENED);
    629     }
    630 
    631     if (p_port->line_status)
    632     {
    633         return (PORT_LINE_ERR);
    634     }
    635 
    636     *p_rx_queue_count = p_port->rx.queue_size;
    637 
    638 	RFCOMM_TRACE_API ("PORT_GetRxQueueCnt() p_rx_queue_count:%d, p_port->rx.queue.count = %d",
    639 		                                     *p_rx_queue_count, p_port->rx.queue_size);
    640 
    641     return (PORT_SUCCESS);
    642 }
    643 
    644 /*******************************************************************************
    645 **
    646 ** Function         PORT_GetState
    647 **
    648 ** Description      This function is called to fill tPORT_STATE structure
    649 **                  with the curremt control settings for the port
    650 **
    651 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
    652 **                  p_settings - Pointer to a tPORT_STATE structure in which
    653 **                               configuration information is returned.
    654 **
    655 *******************************************************************************/
    656 int PORT_GetState (UINT16 handle, tPORT_STATE *p_settings)
    657 {
    658     tPORT      *p_port;
    659 
    660     RFCOMM_TRACE_API ("PORT_GetState() handle:%d", handle);
    661 
    662     /* Check if handle is valid to avoid crashing */
    663     if ((handle == 0) || (handle > MAX_RFC_PORTS))
    664     {
    665         return (PORT_BAD_HANDLE);
    666     }
    667 
    668     p_port = &rfc_cb.port.port[handle - 1];
    669 
    670     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
    671     {
    672         return (PORT_NOT_OPENED);
    673     }
    674 
    675     if (p_port->line_status)
    676     {
    677         return (PORT_LINE_ERR);
    678     }
    679 
    680     *p_settings = p_port->user_port_pars;
    681     return (PORT_SUCCESS);
    682 }
    683 
    684 
    685 /*******************************************************************************
    686 **
    687 ** Function         PORT_Control
    688 **
    689 ** Description      This function directs a specified connection to pass control
    690 **                  control information to the peer device.
    691 **
    692 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
    693 **                  signal     = specify the function to be passed
    694 **
    695 *******************************************************************************/
    696 int PORT_Control (UINT16 handle, UINT8 signal)
    697 {
    698     tPORT      *p_port;
    699     UINT8      old_modem_signal;
    700 
    701     RFCOMM_TRACE_API ("PORT_Control() handle:%d signal:0x%x", handle, signal);
    702 
    703     /* Check if handle is valid to avoid crashing */
    704     if ((handle == 0) || (handle > MAX_RFC_PORTS))
    705     {
    706         return (PORT_BAD_HANDLE);
    707     }
    708 
    709     p_port = &rfc_cb.port.port[handle - 1];
    710 
    711     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
    712     {
    713         return (PORT_NOT_OPENED);
    714     }
    715 
    716     old_modem_signal = p_port->local_ctrl.modem_signal;
    717     p_port->local_ctrl.break_signal = 0;
    718 
    719     switch (signal)
    720     {
    721     case PORT_SET_CTSRTS:
    722         p_port->local_ctrl.modem_signal |= PORT_CTSRTS_ON;
    723         break;
    724 
    725     case PORT_CLR_CTSRTS:
    726         p_port->local_ctrl.modem_signal &= ~PORT_CTSRTS_ON;
    727         break;
    728 
    729     case PORT_SET_DTRDSR:
    730         p_port->local_ctrl.modem_signal |= PORT_DTRDSR_ON;
    731         break;
    732 
    733     case PORT_CLR_DTRDSR:
    734         p_port->local_ctrl.modem_signal &= ~PORT_DTRDSR_ON;
    735         break;
    736 
    737     case PORT_SET_RI:
    738         p_port->local_ctrl.modem_signal |= PORT_RING_ON;
    739         break;
    740 
    741     case PORT_CLR_RI:
    742         p_port->local_ctrl.modem_signal &= ~PORT_RING_ON;
    743         break;
    744 
    745     case PORT_SET_DCD:
    746         p_port->local_ctrl.modem_signal |= PORT_DCD_ON;
    747         break;
    748 
    749     case PORT_CLR_DCD:
    750         p_port->local_ctrl.modem_signal &= ~PORT_DCD_ON;
    751         break;
    752     }
    753 
    754     if (signal == PORT_BREAK)
    755         p_port->local_ctrl.break_signal = PORT_BREAK_DURATION;
    756     else if (p_port->local_ctrl.modem_signal == old_modem_signal)
    757         return (PORT_SUCCESS);
    758 
    759     port_start_control (p_port);
    760 
    761     RFCOMM_TRACE_EVENT ("PORT_Control DTR_DSR : %d, RTS_CTS : %d, RI : %d, DCD : %d",
    762         ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_DTRDSR) ? 1 : 0),
    763         ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_RTSCTS) ? 1 : 0),
    764         ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_RI) ? 1 : 0),
    765         ((p_port->local_ctrl.modem_signal & MODEM_SIGNAL_DCD) ? 1 : 0));
    766 
    767     return (PORT_SUCCESS);
    768 }
    769 
    770 
    771 /*******************************************************************************
    772 **
    773 ** Function         PORT_FlowControl
    774 **
    775 ** Description      This function directs a specified connection to pass
    776 **                  flow control message to the peer device.  Enable flag passed
    777 **                  shows if port can accept more data.
    778 **
    779 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
    780 **                  enable     - enables data flow
    781 **
    782 *******************************************************************************/
    783 int PORT_FlowControl (UINT16 handle, BOOLEAN enable)
    784 {
    785     tPORT      *p_port;
    786     BOOLEAN    old_fc;
    787     UINT32     events;
    788 
    789     RFCOMM_TRACE_API ("PORT_FlowControl() handle:%d enable: %d", handle, enable);
    790 
    791     /* Check if handle is valid to avoid crashing */
    792     if ((handle == 0) || (handle > MAX_RFC_PORTS))
    793     {
    794         return (PORT_BAD_HANDLE);
    795     }
    796 
    797     p_port = &rfc_cb.port.port[handle - 1];
    798 
    799     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
    800     {
    801         return (PORT_NOT_OPENED);
    802     }
    803 
    804     if (!p_port->rfc.p_mcb)
    805     {
    806         return (PORT_NOT_OPENED);
    807     }
    808 
    809     p_port->rx.user_fc = !enable;
    810 
    811     if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT)
    812     {
    813         if (!p_port->rx.user_fc)
    814         {
    815             port_flow_control_peer(p_port, TRUE, 0);
    816         }
    817     }
    818     else
    819     {
    820         old_fc = p_port->local_ctrl.fc;
    821 
    822         /* FC is set if user is set or peer is set */
    823         p_port->local_ctrl.fc = (p_port->rx.user_fc | p_port->rx.peer_fc);
    824 
    825         if (p_port->local_ctrl.fc != old_fc)
    826             port_start_control (p_port);
    827     }
    828 
    829     /* Need to take care of the case when we could not deliver events */
    830     /* to the application because we were flow controlled */
    831     if (enable && (p_port->rx.queue_size != 0))
    832     {
    833         events = PORT_EV_RXCHAR;
    834         if (p_port->rx_flag_ev_pending)
    835         {
    836             p_port->rx_flag_ev_pending = FALSE;
    837             events |= PORT_EV_RXFLAG;
    838         }
    839 
    840         events &= p_port->ev_mask;
    841         if (p_port->p_callback && events)
    842         {
    843             p_port->p_callback (events, p_port->inx);
    844         }
    845     }
    846     return (PORT_SUCCESS);
    847 }
    848 /*******************************************************************************
    849 **
    850 ** Function         PORT_FlowControl_MaxCredit
    851 **
    852 ** Description      This function directs a specified connection to pass
    853 **                  flow control message to the peer device.  Enable flag passed
    854 **                  shows if port can accept more data. It also sends max credit
    855 **                  when data flow enabled
    856 **
    857 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
    858 **                  enable     - enables data flow
    859 **
    860 *******************************************************************************/
    861 
    862 int PORT_FlowControl_MaxCredit (UINT16 handle, BOOLEAN enable)
    863 {
    864     tPORT      *p_port;
    865     BOOLEAN    old_fc;
    866     UINT32     events;
    867 
    868     RFCOMM_TRACE_API ("PORT_FlowControl() handle:%d enable: %d", handle, enable);
    869 
    870     /* Check if handle is valid to avoid crashing */
    871     if ((handle == 0) || (handle > MAX_RFC_PORTS))
    872     {
    873         return (PORT_BAD_HANDLE);
    874     }
    875 
    876     p_port = &rfc_cb.port.port[handle - 1];
    877 
    878     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
    879     {
    880         return (PORT_NOT_OPENED);
    881     }
    882 
    883     if (!p_port->rfc.p_mcb)
    884     {
    885         return (PORT_NOT_OPENED);
    886     }
    887 
    888     p_port->rx.user_fc = !enable;
    889 
    890     if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT)
    891     {
    892         if (!p_port->rx.user_fc)
    893         {
    894             port_flow_control_peer(p_port, TRUE, p_port->credit_rx);
    895         }
    896     }
    897     else
    898     {
    899         old_fc = p_port->local_ctrl.fc;
    900 
    901         /* FC is set if user is set or peer is set */
    902         p_port->local_ctrl.fc = (p_port->rx.user_fc | p_port->rx.peer_fc);
    903 
    904         if (p_port->local_ctrl.fc != old_fc)
    905             port_start_control (p_port);
    906     }
    907 
    908     /* Need to take care of the case when we could not deliver events */
    909     /* to the application because we were flow controlled */
    910     if (enable && (p_port->rx.queue_size != 0))
    911     {
    912         events = PORT_EV_RXCHAR;
    913         if (p_port->rx_flag_ev_pending)
    914         {
    915             p_port->rx_flag_ev_pending = FALSE;
    916             events |= PORT_EV_RXFLAG;
    917         }
    918 
    919         events &= p_port->ev_mask;
    920         if (p_port->p_callback && events)
    921         {
    922             p_port->p_callback (events, p_port->inx);
    923         }
    924     }
    925     return (PORT_SUCCESS);
    926 }
    927 
    928 
    929 /*******************************************************************************
    930 **
    931 ** Function         PORT_GetModemStatus
    932 **
    933 ** Description      This function retrieves modem control signals.  Normally
    934 **                  application will call this function after a callback
    935 **                  function is called with notification that one of signals
    936 **                  has been changed.
    937 **
    938 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
    939 **                  p_signal   - specify the pointer to control signals info
    940 **
    941 *******************************************************************************/
    942 int PORT_GetModemStatus (UINT16 handle, UINT8 *p_signal)
    943 {
    944     tPORT      *p_port;
    945 
    946     if ((handle == 0) || (handle > MAX_RFC_PORTS))
    947     {
    948         return (PORT_BAD_HANDLE);
    949     }
    950 
    951     p_port = &rfc_cb.port.port[handle - 1];
    952 
    953     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
    954     {
    955         return (PORT_NOT_OPENED);
    956     }
    957 
    958     *p_signal = p_port->peer_ctrl.modem_signal;
    959 
    960     RFCOMM_TRACE_API ("PORT_GetModemStatus() handle:%d signal:%x", handle, *p_signal);
    961 
    962     return (PORT_SUCCESS);
    963 }
    964 
    965 
    966 /*******************************************************************************
    967 **
    968 ** Function         PORT_ClearError
    969 **
    970 ** Description      This function retreives information about a communications
    971 **                  error and reports current status of a connection.  The
    972 **                  function should be called when an error occures to clear
    973 **                  the connection error flag and to enable additional read
    974 **                  and write operations.
    975 **
    976 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
    977 **                  p_errors   - pointer of the variable to receive error codes
    978 **                  p_status   - pointer to the tPORT_STATUS structur to receive
    979 **                               connection status
    980 **
    981 *******************************************************************************/
    982 int PORT_ClearError (UINT16 handle, UINT16 *p_errors, tPORT_STATUS *p_status)
    983 {
    984     tPORT  *p_port;
    985 
    986     RFCOMM_TRACE_API ("PORT_ClearError() handle:%d", handle);
    987 
    988     if ((handle == 0) || (handle > MAX_RFC_PORTS))
    989     {
    990         return (PORT_BAD_HANDLE);
    991     }
    992 
    993     p_port = &rfc_cb.port.port[handle - 1];
    994 
    995     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
    996     {
    997         return (PORT_NOT_OPENED);
    998     }
    999 
   1000     *p_errors = p_port->line_status;
   1001 
   1002     /* This is the only call to clear error status.  We can not clear */
   1003     /* connection failed status.  To clean it port should be closed and reopened */
   1004     p_port->line_status = (p_port->line_status & LINE_STATUS_FAILED);
   1005 
   1006     PORT_GetQueueStatus (handle, p_status);
   1007     return (PORT_SUCCESS);
   1008 }
   1009 
   1010 
   1011 /*******************************************************************************
   1012 **
   1013 ** Function         PORT_SendError
   1014 **
   1015 ** Description      This function send a communications error to the peer device
   1016 **
   1017 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
   1018 **                  errors     - receive error codes
   1019 **
   1020 *******************************************************************************/
   1021 int PORT_SendError (UINT16 handle, UINT8 errors)
   1022 {
   1023     tPORT      *p_port;
   1024 
   1025     RFCOMM_TRACE_API ("PORT_SendError() handle:%d errors:0x%x", handle, errors);
   1026 
   1027     if ((handle == 0) || (handle > MAX_RFC_PORTS))
   1028     {
   1029         return (PORT_BAD_HANDLE);
   1030     }
   1031 
   1032     p_port = &rfc_cb.port.port[handle - 1];
   1033 
   1034     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
   1035     {
   1036         return (PORT_NOT_OPENED);
   1037     }
   1038 
   1039     if (!p_port->rfc.p_mcb)
   1040     {
   1041         return (PORT_NOT_OPENED);
   1042     }
   1043 
   1044     RFCOMM_LineStatusReq (p_port->rfc.p_mcb, p_port->dlci, errors);
   1045     return (PORT_SUCCESS);
   1046 }
   1047 
   1048 
   1049 /*******************************************************************************
   1050 **
   1051 ** Function         PORT_GetQueueStatus
   1052 **
   1053 ** Description      This function reports current status of a connection.
   1054 **
   1055 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
   1056 **                  p_status   - pointer to the tPORT_STATUS structur to receive
   1057 **                               connection status
   1058 **
   1059 *******************************************************************************/
   1060 int PORT_GetQueueStatus (UINT16 handle, tPORT_STATUS *p_status)
   1061 {
   1062     tPORT      *p_port;
   1063 
   1064     /* RFCOMM_TRACE_API ("PORT_GetQueueStatus() handle:%d", handle); */
   1065 
   1066     if ((handle == 0) || (handle > MAX_RFC_PORTS))
   1067     {
   1068         return (PORT_BAD_HANDLE);
   1069     }
   1070 
   1071     p_port = &rfc_cb.port.port[handle - 1];
   1072 
   1073     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
   1074     {
   1075         return (PORT_NOT_OPENED);
   1076     }
   1077 
   1078     p_status->in_queue_size  = (UINT16) p_port->rx.queue_size;
   1079     p_status->out_queue_size = (UINT16) p_port->tx.queue_size;
   1080 
   1081     p_status->mtu_size = (UINT16) p_port->peer_mtu;
   1082 
   1083     p_status->flags = 0;
   1084 
   1085     if (!(p_port->peer_ctrl.modem_signal & PORT_CTSRTS_ON))
   1086         p_status->flags |= PORT_FLAG_CTS_HOLD;
   1087 
   1088     if (!(p_port->peer_ctrl.modem_signal & PORT_DTRDSR_ON))
   1089         p_status->flags |= PORT_FLAG_DSR_HOLD;
   1090 
   1091     if (!(p_port->peer_ctrl.modem_signal & PORT_DCD_ON))
   1092         p_status->flags |= PORT_FLAG_RLSD_HOLD;
   1093 
   1094     return (PORT_SUCCESS);
   1095 }
   1096 
   1097 
   1098 /*******************************************************************************
   1099 **
   1100 ** Function         PORT_Purge
   1101 **
   1102 ** Description      This function discards all the data from the output or
   1103 **                  input queues of the specified connection.
   1104 **
   1105 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
   1106 **                  purge_flags - specify the action to take.
   1107 **
   1108 *******************************************************************************/
   1109 int PORT_Purge (UINT16 handle, UINT8 purge_flags)
   1110 {
   1111     tPORT      *p_port;
   1112     BT_HDR     *p_buf;
   1113     UINT16      count;
   1114     UINT32     events;
   1115 
   1116     RFCOMM_TRACE_API ("PORT_Purge() handle:%d flags:0x%x", handle, purge_flags);
   1117 
   1118     /* Check if handle is valid to avoid crashing */
   1119     if ((handle == 0) || (handle > MAX_RFC_PORTS))
   1120     {
   1121         return (PORT_BAD_HANDLE);
   1122     }
   1123 
   1124     p_port = &rfc_cb.port.port[handle - 1];
   1125 
   1126     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
   1127     {
   1128         return (PORT_NOT_OPENED);
   1129     }
   1130 
   1131     if (purge_flags & PORT_PURGE_RXCLEAR)
   1132     {
   1133         PORT_SCHEDULE_LOCK;  /* to prevent missing credit */
   1134 
   1135         count = p_port->rx.queue.count;
   1136 
   1137         while ((p_buf = (BT_HDR *)GKI_dequeue (&p_port->rx.queue)) != NULL)
   1138             GKI_freebuf (p_buf);
   1139 
   1140         p_port->rx.queue_size = 0;
   1141 
   1142         PORT_SCHEDULE_UNLOCK;
   1143 
   1144         /* If we flowed controlled peer based on rx_queue size enable data again */
   1145         if (count)
   1146             port_flow_control_peer (p_port, TRUE, count);
   1147     }
   1148 
   1149     if (purge_flags & PORT_PURGE_TXCLEAR)
   1150     {
   1151         PORT_SCHEDULE_LOCK;  /* to prevent tx.queue_size from being negative */
   1152 
   1153         while ((p_buf = (BT_HDR *)GKI_dequeue (&p_port->tx.queue)) != NULL)
   1154             GKI_freebuf (p_buf);
   1155 
   1156         p_port->tx.queue_size = 0;
   1157 
   1158         PORT_SCHEDULE_UNLOCK;
   1159 
   1160         events = PORT_EV_TXEMPTY;
   1161 
   1162         events |= port_flow_control_user (p_port);
   1163 
   1164         events &= p_port->ev_mask;
   1165 
   1166         if ((p_port->p_callback != NULL) && events)
   1167             (p_port->p_callback)(events, p_port->inx);
   1168     }
   1169 
   1170     return (PORT_SUCCESS);
   1171 }
   1172 
   1173 
   1174 /*******************************************************************************
   1175 **
   1176 ** Function         PORT_ReadData
   1177 **
   1178 ** Description      Normally not GKI aware application will call this function
   1179 **                  after receiving PORT_EV_RXCHAR event.
   1180 **
   1181 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
   1182 **                  p_data      - Data area
   1183 **                  max_len     - Byte count requested
   1184 **                  p_len       - Byte count received
   1185 **
   1186 *******************************************************************************/
   1187 int PORT_ReadData (UINT16 handle, char *p_data, UINT16 max_len, UINT16 *p_len)
   1188 {
   1189     tPORT      *p_port;
   1190     BT_HDR     *p_buf;
   1191     UINT16      count;
   1192 
   1193     RFCOMM_TRACE_API ("PORT_ReadData() handle:%d max_len:%d", handle, max_len);
   1194 
   1195     /* Initialize this in case of an error */
   1196     *p_len = 0;
   1197 
   1198     /* Check if handle is valid to avoid crashing */
   1199     if ((handle == 0) || (handle > MAX_RFC_PORTS))
   1200     {
   1201         return (PORT_BAD_HANDLE);
   1202     }
   1203 
   1204     p_port = &rfc_cb.port.port[handle - 1];
   1205 
   1206     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
   1207     {
   1208         return (PORT_NOT_OPENED);
   1209     }
   1210 
   1211     if (p_port->line_status)
   1212     {
   1213         return (PORT_LINE_ERR);
   1214     }
   1215 
   1216     p_buf = (BT_HDR *)GKI_getfirst (&p_port->rx.queue);
   1217     if (!p_buf)
   1218         return (PORT_SUCCESS);
   1219 
   1220     count = 0;
   1221 
   1222     while (max_len && p_buf)
   1223     {
   1224         if (p_buf->len > max_len)
   1225         {
   1226             memcpy (p_data, (UINT8 *)(p_buf + 1) + p_buf->offset, max_len);
   1227             p_buf->offset += max_len;
   1228             p_buf->len    -= max_len;
   1229 
   1230             *p_len += max_len;
   1231 
   1232             PORT_SCHEDULE_LOCK;
   1233 
   1234             p_port->rx.queue_size -= max_len;
   1235 
   1236             PORT_SCHEDULE_UNLOCK;
   1237 
   1238             break;
   1239         }
   1240         else
   1241         {
   1242             memcpy (p_data, (UINT8 *)(p_buf + 1) + p_buf->offset, p_buf->len);
   1243 
   1244             *p_len  += p_buf->len;
   1245             max_len -= p_buf->len;
   1246 
   1247             PORT_SCHEDULE_LOCK;
   1248 
   1249             p_port->rx.queue_size -= p_buf->len;
   1250 
   1251             if (max_len)
   1252             {
   1253                 p_data  += p_buf->len;
   1254                 p_buf = (BT_HDR *)GKI_getnext (p_buf);
   1255             }
   1256 
   1257             GKI_freebuf (GKI_dequeue (&p_port->rx.queue));
   1258 
   1259             PORT_SCHEDULE_UNLOCK;
   1260 
   1261             count++;
   1262         }
   1263     }
   1264 
   1265     if (*p_len == 1)
   1266     {
   1267         RFCOMM_TRACE_EVENT ("PORT_ReadData queue:%d returned:%d %x", p_port->rx.queue_size, *p_len, (p_data[0]));
   1268     }
   1269     else
   1270     {
   1271         RFCOMM_TRACE_EVENT ("PORT_ReadData queue:%d returned:%d", p_port->rx.queue_size, *p_len);
   1272     }
   1273 
   1274     /* If rfcomm suspended traffic from the peer based on the rx_queue_size */
   1275     /* check if it can be resumed now */
   1276     port_flow_control_peer (p_port, TRUE, count);
   1277 
   1278     return (PORT_SUCCESS);
   1279 }
   1280 
   1281 
   1282 /*******************************************************************************
   1283 **
   1284 ** Function         PORT_Read
   1285 **
   1286 ** Description      Normally application will call this function after receiving
   1287 **                  PORT_EV_RXCHAR event.
   1288 **
   1289 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
   1290 **                  pp_buf      - pointer to address of buffer with data,
   1291 **
   1292 *******************************************************************************/
   1293 int PORT_Read (UINT16 handle, BT_HDR **pp_buf)
   1294 {
   1295     tPORT      *p_port;
   1296     BT_HDR     *p_buf;
   1297 
   1298     RFCOMM_TRACE_API ("PORT_Read() handle:%d", handle);
   1299 
   1300     /* Check if handle is valid to avoid crashing */
   1301     if ((handle == 0) || (handle > MAX_RFC_PORTS))
   1302     {
   1303         return (PORT_BAD_HANDLE);
   1304     }
   1305     p_port = &rfc_cb.port.port[handle - 1];
   1306 
   1307     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
   1308     {
   1309         return (PORT_NOT_OPENED);
   1310     }
   1311 
   1312     if (p_port->line_status)
   1313     {
   1314         return (PORT_LINE_ERR);
   1315     }
   1316 
   1317     PORT_SCHEDULE_LOCK;
   1318 
   1319     p_buf = (BT_HDR *)GKI_dequeue (&p_port->rx.queue);
   1320     if (p_buf)
   1321     {
   1322         p_port->rx.queue_size -= p_buf->len;
   1323 
   1324         PORT_SCHEDULE_UNLOCK;
   1325 
   1326         /* If rfcomm suspended traffic from the peer based on the rx_queue_size */
   1327         /* check if it can be resumed now */
   1328         port_flow_control_peer (p_port, TRUE, 1);
   1329     }
   1330     else
   1331     {
   1332         PORT_SCHEDULE_UNLOCK;
   1333     }
   1334 
   1335     *pp_buf = p_buf;
   1336     return (PORT_SUCCESS);
   1337 }
   1338 
   1339 
   1340 /*******************************************************************************
   1341 **
   1342 ** Function         port_write
   1343 **
   1344 ** Description      This function when a data packet is received from the apper
   1345 **                  layer task.
   1346 **
   1347 ** Parameters:      p_port     - pointer to address of port control block
   1348 **                  p_buf      - pointer to address of buffer with data,
   1349 **
   1350 *******************************************************************************/
   1351 static int port_write (tPORT *p_port, BT_HDR *p_buf)
   1352 {
   1353     /* We should not allow to write data in to server port when connection is not opened */
   1354     if (p_port->is_server && (p_port->rfc.state != RFC_STATE_OPENED))
   1355     {
   1356         GKI_freebuf (p_buf);
   1357         return (PORT_CLOSED);
   1358     }
   1359 
   1360     /* Keep the data in pending queue if peer does not allow data, or */
   1361     /* Peer is not ready or Port is not yet opened or initial port control */
   1362     /* command has not been sent */
   1363     if (p_port->tx.peer_fc
   1364      || !p_port->rfc.p_mcb
   1365      || !p_port->rfc.p_mcb->peer_ready
   1366      || (p_port->rfc.state != RFC_STATE_OPENED)
   1367      || ((p_port->port_ctrl & (PORT_CTRL_REQ_SENT | PORT_CTRL_IND_RECEIVED)) !=
   1368                               (PORT_CTRL_REQ_SENT | PORT_CTRL_IND_RECEIVED)))
   1369     {
   1370         if ((p_port->tx.queue_size  > PORT_TX_CRITICAL_WM)
   1371          || (p_port->tx.queue.count > PORT_TX_BUF_CRITICAL_WM))
   1372         {
   1373             RFCOMM_TRACE_WARNING ("PORT_Write: Queue size: %d",
   1374                                    p_port->tx.queue_size);
   1375 
   1376             GKI_freebuf (p_buf);
   1377 
   1378             if ((p_port->p_callback != NULL) && (p_port->ev_mask & PORT_EV_ERR))
   1379                   p_port->p_callback (PORT_EV_ERR, p_port->inx);
   1380 
   1381             return (PORT_TX_FULL);
   1382         }
   1383 
   1384         RFCOMM_TRACE_EVENT ("PORT_Write : Data is enqued. flow disabled %d peer_ready %d state %d ctrl_state %x",
   1385                              p_port->tx.peer_fc,
   1386                              (p_port->rfc.p_mcb && p_port->rfc.p_mcb->peer_ready),
   1387                              p_port->rfc.state,
   1388                              p_port->port_ctrl);
   1389 
   1390         GKI_enqueue (&p_port->tx.queue, p_buf);
   1391         p_port->tx.queue_size += p_buf->len;
   1392 
   1393         return (PORT_CMD_PENDING);
   1394     }
   1395     else
   1396     {
   1397         RFCOMM_TRACE_EVENT ("PORT_Write : Data is being sent");
   1398 
   1399         RFCOMM_DataReq (p_port->rfc.p_mcb, p_port->dlci, p_buf);
   1400         return (PORT_SUCCESS);
   1401     }
   1402 }
   1403 
   1404 /*******************************************************************************
   1405 **
   1406 ** Function         PORT_Write
   1407 **
   1408 ** Description      This function when a data packet is received from the apper
   1409 **                  layer task.
   1410 **
   1411 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
   1412 **                  pp_buf      - pointer to address of buffer with data,
   1413 **
   1414 *******************************************************************************/
   1415 int PORT_Write (UINT16 handle, BT_HDR *p_buf)
   1416 {
   1417     tPORT  *p_port;
   1418     UINT32 event = 0;
   1419     int    rc;
   1420 
   1421     RFCOMM_TRACE_API ("PORT_Write() handle:%d", handle);
   1422 
   1423     /* Check if handle is valid to avoid crashing */
   1424     if ((handle == 0) || (handle > MAX_RFC_PORTS))
   1425     {
   1426         GKI_freebuf (p_buf);
   1427         return (PORT_BAD_HANDLE);
   1428     }
   1429 
   1430     p_port = &rfc_cb.port.port[handle - 1];
   1431 
   1432     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
   1433     {
   1434         GKI_freebuf (p_buf);
   1435         return (PORT_NOT_OPENED);
   1436     }
   1437 
   1438     if (p_port->line_status)
   1439     {
   1440         RFCOMM_TRACE_WARNING ("PORT_Write: Data dropped line_status:0x%x",
   1441                                p_port->line_status);
   1442         GKI_freebuf (p_buf);
   1443         return (PORT_LINE_ERR);
   1444     }
   1445 
   1446     rc = port_write (p_port, p_buf);
   1447     event |= port_flow_control_user (p_port);
   1448 
   1449     switch (rc)
   1450     {
   1451     case PORT_TX_FULL:
   1452         event |= PORT_EV_ERR;
   1453         break;
   1454 
   1455     case PORT_SUCCESS:
   1456         event |= (PORT_EV_TXCHAR | PORT_EV_TXEMPTY);
   1457         break;
   1458     }
   1459     /* Mask out all events that are not of interest to user */
   1460     event &= p_port->ev_mask;
   1461 
   1462     /* Send event to the application */
   1463     if (p_port->p_callback && event)
   1464         (p_port->p_callback)(event, p_port->inx);
   1465 
   1466     return (PORT_SUCCESS);
   1467 }
   1468 /*******************************************************************************
   1469 **
   1470 ** Function         PORT_WriteDataCO
   1471 **
   1472 ** Description      Normally not GKI aware application will call this function
   1473 **                  to send data to the port by callout functions
   1474 **
   1475 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
   1476 **                  fd         - socket fd
   1477 **                  p_len      - Byte count returned
   1478 **
   1479 *******************************************************************************/
   1480 int PORT_WriteDataCO (UINT16 handle, int* p_len)
   1481 {
   1482 
   1483     tPORT      *p_port;
   1484     BT_HDR     *p_buf;
   1485     UINT32     event = 0;
   1486     int        rc = 0;
   1487     UINT16     length;
   1488 
   1489     RFCOMM_TRACE_API ("PORT_WriteDataCO() handle:%d", handle);
   1490     int written;
   1491     *p_len = 0;
   1492 
   1493     /* Check if handle is valid to avoid crashing */
   1494     if ((handle == 0) || (handle > MAX_RFC_PORTS))
   1495     {
   1496         return (PORT_BAD_HANDLE);
   1497     }
   1498     p_port = &rfc_cb.port.port[handle - 1];
   1499 
   1500     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
   1501     {
   1502         RFCOMM_TRACE_WARNING ("PORT_WriteDataByFd() no port state:%d", p_port->state);
   1503         return (PORT_NOT_OPENED);
   1504     }
   1505 
   1506     if (!p_port->peer_mtu)
   1507     {
   1508         RFCOMM_TRACE_ERROR ("PORT_WriteDataByFd() peer_mtu:%d", p_port->peer_mtu);
   1509         return (PORT_UNKNOWN_ERROR);
   1510     }
   1511     int available = 0;
   1512     //if(ioctl(fd, FIONREAD, &available) < 0)
   1513     if(p_port->p_data_co_callback(handle, (UINT8*)&available, sizeof(available),
   1514                                 DATA_CO_CALLBACK_TYPE_OUTGOING_SIZE) == FALSE)
   1515     {
   1516         RFCOMM_TRACE_ERROR("p_data_co_callback DATA_CO_CALLBACK_TYPE_INCOMING_SIZE failed, available:%d", available);
   1517         return (PORT_UNKNOWN_ERROR);
   1518     }
   1519     if(available == 0)
   1520         return PORT_SUCCESS;
   1521     /* Length for each buffer is the smaller of GKI buffer, peer MTU, or max_len */
   1522     length = RFCOMM_DATA_POOL_BUF_SIZE -
   1523             (UINT16)(sizeof(BT_HDR) + L2CAP_MIN_OFFSET + RFCOMM_DATA_OVERHEAD);
   1524 
   1525     /* If there are buffers scheduled for transmission check if requested */
   1526     /* data fits into the end of the queue */
   1527     PORT_SCHEDULE_LOCK;
   1528 
   1529     if (((p_buf = (BT_HDR *)p_port->tx.queue.p_last) != NULL)
   1530      && (((int)p_buf->len + available) <= (int)p_port->peer_mtu)
   1531      && (((int)p_buf->len + available) <= (int)length))
   1532     {
   1533         //if(recv(fd, (UINT8 *)(p_buf + 1) + p_buf->offset + p_buf->len, available, 0) != available)
   1534         if(p_port->p_data_co_callback(handle, (UINT8 *)(p_buf + 1) + p_buf->offset + p_buf->len,
   1535                                     available, DATA_CO_CALLBACK_TYPE_OUTGOING) == FALSE)
   1536 
   1537         {
   1538             error("p_data_co_callback DATA_CO_CALLBACK_TYPE_OUTGOING failed, available:%d", available);
   1539             PORT_SCHEDULE_UNLOCK;
   1540             return (PORT_UNKNOWN_ERROR);
   1541         }
   1542         //memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset + p_buf->len, p_data, max_len);
   1543         p_port->tx.queue_size += (UINT16)available;
   1544 
   1545         *p_len = available;
   1546         p_buf->len += (UINT16)available;
   1547 
   1548         PORT_SCHEDULE_UNLOCK;
   1549 
   1550         return (PORT_SUCCESS);
   1551     }
   1552 
   1553     PORT_SCHEDULE_UNLOCK;
   1554 
   1555     //int max_read = length < p_port->peer_mtu ? length : p_port->peer_mtu;
   1556 
   1557     //max_read = available < max_read ? available : max_read;
   1558 
   1559     while (available)
   1560     {
   1561         /* if we're over buffer high water mark, we're done */
   1562         if ((p_port->tx.queue_size  > PORT_TX_HIGH_WM)
   1563          || (p_port->tx.queue.count > PORT_TX_BUF_HIGH_WM))
   1564         {
   1565             port_flow_control_user(p_port);
   1566             event |= PORT_EV_FC;
   1567             debug("tx queue is full,tx.queue_size:%d,tx.queue.count:%d,available:%d",
   1568                     p_port->tx.queue_size, p_port->tx.queue.count, available);
   1569             break;
   1570          }
   1571 
   1572         /* continue with rfcomm data write */
   1573         p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_DATA_POOL_ID);
   1574         if (!p_buf)
   1575             break;
   1576 
   1577         p_buf->offset         = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET;
   1578         p_buf->layer_specific = handle;
   1579 
   1580         if (p_port->peer_mtu < length)
   1581             length = p_port->peer_mtu;
   1582         if (available < (int)length)
   1583             length = (UINT16)available;
   1584         p_buf->len = length;
   1585         p_buf->event          = BT_EVT_TO_BTU_SP_DATA;
   1586 
   1587         //memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset, p_data, length);
   1588         //if(recv(fd, (UINT8 *)(p_buf + 1) + p_buf->offset, (int)length, 0) != (int)length)
   1589         if(p_port->p_data_co_callback(handle, (UINT8 *)(p_buf + 1) + p_buf->offset, length,
   1590                                       DATA_CO_CALLBACK_TYPE_OUTGOING) == FALSE)
   1591         {
   1592             error("p_data_co_callback DATA_CO_CALLBACK_TYPE_OUTGOING failed, length:%d", length);
   1593             return (PORT_UNKNOWN_ERROR);
   1594         }
   1595 
   1596 
   1597         RFCOMM_TRACE_EVENT ("PORT_WriteData %d bytes", length);
   1598 
   1599         rc = port_write (p_port, p_buf);
   1600 
   1601         /* If queue went below the threashold need to send flow control */
   1602         event |= port_flow_control_user (p_port);
   1603 
   1604         if (rc == PORT_SUCCESS)
   1605             event |= PORT_EV_TXCHAR;
   1606 
   1607         if ((rc != PORT_SUCCESS) && (rc != PORT_CMD_PENDING))
   1608             break;
   1609 
   1610         *p_len  += length;
   1611         available -= (int)length;
   1612     }
   1613     if (!available && (rc != PORT_CMD_PENDING) && (rc != PORT_TX_QUEUE_DISABLED))
   1614         event |= PORT_EV_TXEMPTY;
   1615 
   1616     /* Mask out all events that are not of interest to user */
   1617     event &= p_port->ev_mask;
   1618 
   1619     /* Send event to the application */
   1620     if (p_port->p_callback && event)
   1621         (p_port->p_callback)(event, p_port->inx);
   1622 
   1623     return (PORT_SUCCESS);
   1624 }
   1625 
   1626 
   1627 
   1628 /*******************************************************************************
   1629 **
   1630 ** Function         PORT_WriteData
   1631 **
   1632 ** Description      Normally not GKI aware application will call this function
   1633 **                  to send data to the port.
   1634 **
   1635 ** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
   1636 **                  p_data      - Data area
   1637 **                  max_len     - Byte count requested
   1638 **                  p_len       - Byte count received
   1639 **
   1640 *******************************************************************************/
   1641 int PORT_WriteData (UINT16 handle, char *p_data, UINT16 max_len, UINT16 *p_len)
   1642 {
   1643     tPORT      *p_port;
   1644     BT_HDR     *p_buf;
   1645     UINT32     event = 0;
   1646     int        rc = 0;
   1647     UINT16     length;
   1648 
   1649     RFCOMM_TRACE_API ("PORT_WriteData() max_len:%d", max_len);
   1650 
   1651     *p_len = 0;
   1652 
   1653     /* Check if handle is valid to avoid crashing */
   1654     if ((handle == 0) || (handle > MAX_RFC_PORTS))
   1655     {
   1656         return (PORT_BAD_HANDLE);
   1657     }
   1658     p_port = &rfc_cb.port.port[handle - 1];
   1659 
   1660     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
   1661     {
   1662         RFCOMM_TRACE_WARNING ("PORT_WriteData() no port state:%d", p_port->state);
   1663         return (PORT_NOT_OPENED);
   1664     }
   1665 
   1666     if (!max_len || !p_port->peer_mtu)
   1667     {
   1668         RFCOMM_TRACE_ERROR ("PORT_WriteData() peer_mtu:%d", p_port->peer_mtu);
   1669         return (PORT_UNKNOWN_ERROR);
   1670     }
   1671 
   1672     /* Length for each buffer is the smaller of GKI buffer, peer MTU, or max_len */
   1673     length = RFCOMM_DATA_POOL_BUF_SIZE -
   1674             (UINT16)(sizeof(BT_HDR) + L2CAP_MIN_OFFSET + RFCOMM_DATA_OVERHEAD);
   1675 
   1676     /* If there are buffers scheduled for transmission check if requested */
   1677     /* data fits into the end of the queue */
   1678     PORT_SCHEDULE_LOCK;
   1679 
   1680     if (((p_buf = (BT_HDR *)p_port->tx.queue.p_last) != NULL)
   1681      && ((p_buf->len + max_len) <= p_port->peer_mtu)
   1682      && ((p_buf->len + max_len) <= length))
   1683     {
   1684         memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset + p_buf->len, p_data, max_len);
   1685         p_port->tx.queue_size += max_len;
   1686 
   1687         *p_len = max_len;
   1688         p_buf->len += max_len;
   1689 
   1690         PORT_SCHEDULE_UNLOCK;
   1691 
   1692         return (PORT_SUCCESS);
   1693     }
   1694 
   1695     PORT_SCHEDULE_UNLOCK;
   1696 
   1697     while (max_len)
   1698     {
   1699         /* if we're over buffer high water mark, we're done */
   1700         if ((p_port->tx.queue_size  > PORT_TX_HIGH_WM)
   1701          || (p_port->tx.queue.count > PORT_TX_BUF_HIGH_WM))
   1702             break;
   1703 
   1704         /* continue with rfcomm data write */
   1705         p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_DATA_POOL_ID);
   1706         if (!p_buf)
   1707             break;
   1708 
   1709         p_buf->offset         = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET;
   1710         p_buf->layer_specific = handle;
   1711 
   1712         if (p_port->peer_mtu < length)
   1713             length = p_port->peer_mtu;
   1714         if (max_len < length)
   1715             length = max_len;
   1716         p_buf->len = length;
   1717         p_buf->event          = BT_EVT_TO_BTU_SP_DATA;
   1718 
   1719         memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset, p_data, length);
   1720 
   1721         RFCOMM_TRACE_EVENT ("PORT_WriteData %d bytes", length);
   1722 
   1723         rc = port_write (p_port, p_buf);
   1724 
   1725         /* If queue went below the threashold need to send flow control */
   1726         event |= port_flow_control_user (p_port);
   1727 
   1728         if (rc == PORT_SUCCESS)
   1729             event |= PORT_EV_TXCHAR;
   1730 
   1731         if ((rc != PORT_SUCCESS) && (rc != PORT_CMD_PENDING))
   1732             break;
   1733 
   1734         *p_len  += length;
   1735         max_len -= length;
   1736         p_data  += length;
   1737 
   1738     }
   1739     if (!max_len && (rc != PORT_CMD_PENDING) && (rc != PORT_TX_QUEUE_DISABLED))
   1740         event |= PORT_EV_TXEMPTY;
   1741 
   1742     /* Mask out all events that are not of interest to user */
   1743     event &= p_port->ev_mask;
   1744 
   1745     /* Send event to the application */
   1746     if (p_port->p_callback && event)
   1747         (p_port->p_callback)(event, p_port->inx);
   1748 
   1749     return (PORT_SUCCESS);
   1750 }
   1751 
   1752 
   1753 /*******************************************************************************
   1754 **
   1755 ** Function         PORT_Test
   1756 **
   1757 ** Description      Application can call this function to send RFCOMM Test frame
   1758 **
   1759 ** Parameters:      handle      - Handle returned in the RFCOMM_CreateConnection
   1760 **                  p_data      - Data area
   1761 **                  max_len     - Byte count requested
   1762 **
   1763 *******************************************************************************/
   1764 int PORT_Test (UINT16 handle, UINT8 *p_data, UINT16 len)
   1765 {
   1766     BT_HDR   *p_buf;
   1767     tPORT    *p_port;
   1768 
   1769     RFCOMM_TRACE_API ("PORT_Test() len:%d", len);
   1770 
   1771     if ((handle == 0) || (handle > MAX_RFC_PORTS))
   1772     {
   1773         return (PORT_BAD_HANDLE);
   1774     }
   1775     p_port = &rfc_cb.port.port[handle - 1];
   1776 
   1777     if (!p_port->in_use || (p_port->state == PORT_STATE_CLOSED))
   1778     {
   1779         return (PORT_NOT_OPENED);
   1780     }
   1781 
   1782     if (len > ((p_port->mtu == 0) ? RFCOMM_DEFAULT_MTU : p_port->mtu))
   1783     {
   1784         return (PORT_UNKNOWN_ERROR);
   1785     }
   1786 
   1787     if ((p_buf = (BT_HDR *)GKI_getpoolbuf (RFCOMM_CMD_POOL_ID)) != NULL)
   1788     {
   1789 
   1790         p_buf->offset  = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET + 2;
   1791         p_buf->len = len;
   1792 
   1793         memcpy ((UINT8 *)(p_buf + 1) + p_buf->offset, p_data, p_buf->len);
   1794 
   1795         rfc_send_test (p_port->rfc.p_mcb, TRUE, p_buf);
   1796         return (PORT_SUCCESS);
   1797     }
   1798     else
   1799     {
   1800         return (PORT_NO_MEM);
   1801     }
   1802 }
   1803 
   1804 /*******************************************************************************
   1805 **
   1806 ** Function         RFCOMM_Init
   1807 **
   1808 ** Description      This function is called to initialize RFCOMM layer
   1809 **
   1810 *******************************************************************************/
   1811 void RFCOMM_Init (void)
   1812 {
   1813     memset (&rfc_cb, 0, sizeof (tRFC_CB));  /* Init RFCOMM control block */
   1814 
   1815     rfc_cb.rfc.last_mux = MAX_BD_CONNECTIONS;
   1816 
   1817 #if defined(RFCOMM_INITIAL_TRACE_LEVEL)
   1818     rfc_cb.trace_level = RFCOMM_INITIAL_TRACE_LEVEL;
   1819 #else
   1820     rfc_cb.trace_level = BT_TRACE_LEVEL_NONE;    /* No traces */
   1821 #endif
   1822 
   1823     rfcomm_l2cap_if_init ();
   1824 }
   1825 
   1826 /*******************************************************************************
   1827 **
   1828 ** Function         PORT_SetTraceLevel
   1829 **
   1830 ** Description      This function sets the trace level for RFCOMM. If called with
   1831 **                  a value of 0xFF, it simply reads the current trace level.
   1832 **
   1833 ** Returns          the new (current) trace level
   1834 **
   1835 *******************************************************************************/
   1836 UINT8 PORT_SetTraceLevel (UINT8 new_level)
   1837 {
   1838     if (new_level != 0xFF)
   1839         rfc_cb.trace_level = new_level;
   1840 
   1841     return (rfc_cb.trace_level);
   1842 }
   1843 
   1844