Home | History | Annotate | Download | only in rfcomm
      1 /******************************************************************************
      2  *
      3  *  Copyright 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 definitions internal to the PORT unit
     22  *
     23  *****************************************************************************/
     24 
     25 #ifndef PORT_INT_H
     26 #define PORT_INT_H
     27 
     28 #include "bt_common.h"
     29 #include "bt_target.h"
     30 #include "osi/include/alarm.h"
     31 #include "osi/include/fixed_queue.h"
     32 #include "port_api.h"
     33 #include "rfcdefs.h"
     34 
     35 /* Local events passed when application event is sent from the api to PORT */
     36 /* ???*/
     37 #define PORT_EVENT_OPEN (1 | BT_EVT_TO_BTU_SP_EVT)
     38 #define PORT_EVENT_CONTROL (2 | BT_EVT_TO_BTU_SP_EVT)
     39 #define PORT_EVENT_SET_STATE (3 | BT_EVT_TO_BTU_SP_EVT)
     40 #define PORT_EVENT_SET_CALLBACK (5 | BT_EVT_TO_BTU_SP_EVT)
     41 #define PORT_EVENT_WRITE (6 | BT_EVT_TO_BTU_SP_EVT)
     42 #define PORT_EVENT_PURGE (7 | BT_EVT_TO_BTU_SP_EVT)
     43 #define PORT_EVENT_SEND_ERROR (8 | BT_EVT_TO_BTU_SP_EVT)
     44 #define PORT_EVENT_FLOW_CONTROL (9 | BT_EVT_TO_BTU_SP_EVT)
     45 
     46 /*
     47  * Flow control configuration values for the mux
     48 */
     49 #define PORT_FC_UNDEFINED 0 /* mux flow control mechanism not defined yet */
     50 #define PORT_FC_TS710 1     /* use TS 07.10 flow control  */
     51 #define PORT_FC_CREDIT 2    /* use RFCOMM credit based flow control */
     52 
     53 /*
     54  * Define Port Data Transfere control block
     55 */
     56 typedef struct {
     57   fixed_queue_t* queue; /* Queue of buffers waiting to be sent */
     58   bool peer_fc; /* true if flow control is set based on peer's request */
     59   bool user_fc; /* true if flow control is set based on user's request  */
     60   uint32_t queue_size;        /* Number of data bytes in the queue */
     61   tPORT_CALLBACK* p_callback; /* Address of the callback function */
     62 } tPORT_DATA;
     63 
     64 /*
     65  * Port control structure used to pass modem info
     66 */
     67 typedef struct {
     68 #define MODEM_SIGNAL_DTRDSR 0x01
     69 #define MODEM_SIGNAL_RTSCTS 0x02
     70 #define MODEM_SIGNAL_RI 0x04
     71 #define MODEM_SIGNAL_DCD 0x08
     72 
     73   uint8_t modem_signal; /* [DTR/DSR | RTS/CTS | RI | DCD ] */
     74 
     75   uint8_t break_signal; /* 0-3 s in steps of 200 ms */
     76 
     77   uint8_t discard_buffers; /* 0 - do not discard, 1 - discard */
     78 
     79 #define RFCOMM_CTRL_BREAK_ASAP 0
     80 #define RFCOMM_CTRL_BREAK_IN_SEQ 1
     81 
     82   uint8_t break_signal_seq; /* as soon as possible | in sequence (default) */
     83 
     84   bool fc; /* true when the device is unable to accept frames */
     85 } tPORT_CTRL;
     86 
     87 /*
     88  * RFCOMM multiplexer Control Block
     89 */
     90 typedef struct {
     91   alarm_t* mcb_timer;   /* MCB timer */
     92   fixed_queue_t* cmd_q; /* Queue for command messages on this mux */
     93   uint8_t port_inx[RFCOMM_MAX_DLCI + 1]; /* Array for quick access to  */
     94                                          /* tPORT based on dlci        */
     95   RawAddress bd_addr;                    /* BD ADDR of the peer if initiator */
     96   uint16_t lcid;                         /* Local cid used for this channel */
     97   uint16_t peer_l2cap_mtu; /* Max frame that can be sent to peer L2CAP */
     98   uint8_t state;           /* Current multiplexer channel state */
     99   uint8_t is_initiator;    /* true if this side sends SABME (dlci=0) */
    100   bool local_cfg_sent;
    101   bool peer_cfg_rcvd;
    102   bool restart_required;  /* true if has to restart channel after disc */
    103   bool peer_ready;        /* True if other side can accept frames */
    104   uint8_t flow;           /* flow control mechanism for this mux */
    105   bool l2cap_congested;   /* true if L2CAP is congested */
    106   bool is_disc_initiator; /* true if initiated disc of port */
    107   uint16_t
    108       pending_lcid; /* store LCID for incoming connection while connecting */
    109   uint8_t
    110       pending_id; /* store l2cap ID for incoming connection while connecting */
    111 } tRFC_MCB;
    112 
    113 /*
    114  * RFCOMM Port Connection Control Block
    115 */
    116 typedef struct {
    117 #define RFC_PORT_STATE_IDLE 0
    118 #define RFC_PORT_STATE_WAIT_START 1
    119 #define RFC_PORT_STATE_OPENING 2
    120 #define RFC_PORT_STATE_OPENED 3
    121 #define RFC_PORT_STATE_CLOSING 4
    122 
    123   uint8_t state; /* Current state of the connection */
    124 
    125 #define RFC_RSP_PN 0x01
    126 #define RFC_RSP_RPN_REPLY 0x02
    127 #define RFC_RSP_RPN 0x04
    128 #define RFC_RSP_MSC 0x08
    129 #define RFC_RSP_RLS 0x10
    130 
    131   uint8_t expected_rsp;
    132 
    133   tRFC_MCB* p_mcb;
    134 
    135   alarm_t* port_timer;
    136 } tRFC_PORT;
    137 
    138 /*
    139  * Define control block containing information about PORT connection
    140 */
    141 typedef struct {
    142   uint8_t inx; /* Index of this control block in the port_info array */
    143   bool in_use; /* True when structure is allocated */
    144 
    145 #define PORT_STATE_CLOSED 0
    146 #define PORT_STATE_OPENING 1
    147 #define PORT_STATE_OPENED 2
    148 #define PORT_STATE_CLOSING 3
    149 
    150   uint8_t state; /* State of the application */
    151 
    152   uint8_t scn;   /* Service channel number */
    153   uint16_t uuid; /* Service UUID */
    154 
    155   RawAddress bd_addr; /* BD ADDR of the device for the multiplexer channel */
    156   bool is_server;  /* true if the server application */
    157   uint8_t dlci;    /* DLCI of the connection */
    158 
    159   uint8_t error; /* Last error detected */
    160 
    161   uint8_t line_status; /* Line status as reported by peer */
    162 
    163   uint8_t default_signal_state; /* Initial signal state depending on uuid */
    164 
    165   uint16_t mtu;      /* Max MTU that port can receive */
    166   uint16_t peer_mtu; /* Max MTU that port can send */
    167 
    168   tPORT_DATA tx; /* Control block for data from app to peer */
    169   tPORT_DATA rx; /* Control block for data from peer to app */
    170 
    171   tPORT_STATE user_port_pars; /* Port parameters for user connection */
    172   tPORT_STATE peer_port_pars; /* Port parameters for user connection */
    173 
    174   tPORT_CTRL local_ctrl;
    175   tPORT_CTRL peer_ctrl;
    176 
    177 #define PORT_CTRL_REQ_SENT 0x01
    178 #define PORT_CTRL_REQ_CONFIRMED 0x02
    179 #define PORT_CTRL_IND_RECEIVED 0x04
    180 #define PORT_CTRL_IND_RESPONDED 0x08
    181 
    182   uint8_t port_ctrl; /* Modem Status Command  */
    183 
    184   bool rx_flag_ev_pending; /* RXFLAG Character is received */
    185 
    186   tRFC_PORT rfc; /* RFCOMM port control block */
    187 
    188   uint32_t ev_mask;           /* Event mask for the callback */
    189   tPORT_CALLBACK* p_callback; /* Pointer to users callback function */
    190   tPORT_CALLBACK*
    191       p_mgmt_callback; /* Callback function to receive connection up/down */
    192   tPORT_DATA_CALLBACK*
    193       p_data_callback; /* Callback function to receive data indications */
    194   tPORT_DATA_CO_CALLBACK*
    195       p_data_co_callback; /* Callback function with callouts and flowctrl */
    196   uint16_t credit_tx;     /* Flow control credits for tx path */
    197   uint16_t credit_rx;     /* Flow control credits for rx path, this is */
    198                           /* number of buffers peer is allowed to sent */
    199   uint16_t
    200       credit_rx_max; /* Max number of credits we will allow this guy to sent */
    201   uint16_t credit_rx_low;   /* Number of credits when we send credit update */
    202   uint16_t rx_buf_critical; /* port receive queue critical watermark level */
    203   bool keep_port_handle;    /* true if port is not deallocated when closing */
    204   /* it is set to true for server when allocating port */
    205   uint16_t keep_mtu; /* Max MTU that port can receive by server */
    206 } tPORT;
    207 
    208 /* Define the PORT/RFCOMM control structure
    209 */
    210 typedef struct {
    211   tPORT port[MAX_RFC_PORTS];            /* Port info pool */
    212   tRFC_MCB rfc_mcb[MAX_BD_CONNECTIONS]; /* RFCOMM bd_connections pool */
    213 } tPORT_CB;
    214 
    215 /*
    216  * Functions provided by the port_utils.cc
    217 */
    218 extern tPORT* port_allocate_port(uint8_t dlci, const RawAddress& bd_addr);
    219 extern void port_set_defaults(tPORT* p_port);
    220 extern void port_select_mtu(tPORT* p_port);
    221 extern void port_release_port(tPORT* p_port);
    222 extern tPORT* port_find_mcb_dlci_port(tRFC_MCB* p_mcb, uint8_t dlci);
    223 extern tRFC_MCB* port_find_mcb(const RawAddress& bd_addr);
    224 extern tPORT* port_find_dlci_port(uint8_t dlci);
    225 extern tPORT* port_find_port(uint8_t dlci, const RawAddress& bd_addr);
    226 extern uint32_t port_get_signal_changes(tPORT* p_port, uint8_t old_signals,
    227                                         uint8_t signal);
    228 extern uint32_t port_flow_control_user(tPORT* p_port);
    229 extern void port_flow_control_peer(tPORT* p_port, bool enable, uint16_t count);
    230 
    231 /*
    232  * Functions provided by the port_rfc.cc
    233 */
    234 extern int port_open_continue(tPORT* p_port);
    235 extern void port_start_port_open(tPORT* p_port);
    236 extern void port_start_par_neg(tPORT* p_port);
    237 extern void port_start_control(tPORT* p_port);
    238 extern void port_start_close(tPORT* p_port);
    239 extern void port_rfc_closed(tPORT* p_port, uint8_t res);
    240 
    241 #endif
    242