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