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