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/l2c_api.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 =
90       RawAddress::kEmpty;                /* BD ADDR of the peer if initiator */
91   uint16_t lcid;                         /* Local cid used for this channel */
92   uint16_t peer_l2cap_mtu; /* Max frame that can be sent to peer L2CAP */
93   tRFC_MX_STATE state;     /* Current multiplexer channel state */
94   uint8_t is_initiator;    /* true if this side sends SABME (dlci=0) */
95   bool restart_required;  /* true if has to restart channel after disc */
96   bool peer_ready;        /* True if other side can accept frames */
97   uint8_t flow;           /* flow control mechanism for this mux */
98   bool l2cap_congested;   /* true if L2CAP is congested */
99   bool is_disc_initiator; /* true if initiated disc of port */
100   uint16_t
101       pending_lcid; /* store LCID for incoming connection while connecting */
102   bool pending_configure_complete;       /* true if confiquration of the pending
103                                             connection was completed*/
104   tL2CAP_CFG_INFO pending_cfg_info = {}; /* store configure info for incoming
105                                        connection while connecting */
106 } tRFC_MCB;
107 
108 /*
109  * RFCOMM Port Connection Control Block
110 */
111 typedef struct {
112   uint8_t state; /* Current state of the connection */
113 
114 #define RFC_RSP_PN 0x01
115 #define RFC_RSP_RPN_REPLY 0x02
116 #define RFC_RSP_RPN 0x04
117 #define RFC_RSP_MSC 0x08
118 #define RFC_RSP_RLS 0x10
119 
120   uint8_t expected_rsp;
121 
122   tRFC_MCB* p_mcb;
123 
124   alarm_t* port_timer;
125 } tRFC_PORT;
126 
127 typedef enum : uint8_t {
128   PORT_CONNECTION_STATE_CLOSED = 0,
129   PORT_CONNECTION_STATE_OPENING = 1,
130   PORT_CONNECTION_STATE_OPENED = 2,
131   PORT_CONNECTION_STATE_CLOSING = 3,
132 } tPORT_CONNECTION_STATE;
133 
port_connection_state_text(const tPORT_CONNECTION_STATE & state)134 inline std::string port_connection_state_text(
135     const tPORT_CONNECTION_STATE& state) {
136   switch (state) {
137     CASE_RETURN_STRING(PORT_CONNECTION_STATE_CLOSED);
138     CASE_RETURN_STRING(PORT_CONNECTION_STATE_OPENING);
139     CASE_RETURN_STRING(PORT_CONNECTION_STATE_OPENED);
140     CASE_RETURN_STRING(PORT_CONNECTION_STATE_CLOSING);
141     default:
142       break;
143   }
144   RETURN_UNKNOWN_TYPE_STRING(tPORT_CONNECTION_STATE, state);
145 }
146 
147 namespace fmt {
148 template <>
149 struct formatter<tPORT_CONNECTION_STATE>
150     : enum_formatter<tPORT_CONNECTION_STATE> {};
151 }  // namespace fmt
152 
153 /*
154  * Define control block containing information about PORT connection
155 */
156 typedef struct {
157   uint8_t handle;  // Starting from 1, unique for this object
158   bool in_use; /* True when structure is allocated */
159 
160   tPORT_CONNECTION_STATE state; /* State of the application */
161 
162   uint8_t scn;   /* Service channel number */
163   uint16_t uuid; /* Service UUID */
164 
165   RawAddress bd_addr; /* BD ADDR of the device for the multiplexer channel */
166   bool is_server;  /* true if the server application */
167   uint8_t dlci;    /* DLCI of the connection */
168 
169   uint8_t line_status; /* Line status as reported by peer */
170 
171   uint8_t default_signal_state; /* Initial signal state depending on uuid */
172 
173   uint16_t mtu;      /* Max MTU that port can receive */
174   uint16_t peer_mtu; /* Max MTU that port can send */
175 
176   tPORT_DATA tx; /* Control block for data from app to peer */
177   tPORT_DATA rx; /* Control block for data from peer to app */
178 
179   tPORT_STATE user_port_pars; /* Port parameters for user connection */
180   tPORT_STATE peer_port_pars; /* Port parameters for user connection */
181 
182   tPORT_CTRL local_ctrl;
183   tPORT_CTRL peer_ctrl;
184 
185 #define PORT_CTRL_REQ_SENT 0x01
186 #define PORT_CTRL_REQ_CONFIRMED 0x02
187 #define PORT_CTRL_IND_RECEIVED 0x04
188 #define PORT_CTRL_IND_RESPONDED 0x08
189 
190   uint8_t port_ctrl; /* Modem Status Command  */
191 
192   bool rx_flag_ev_pending; /* RXFLAG Character is received */
193 
194   tRFC_PORT rfc; /* RFCOMM port control block */
195 
196   uint32_t ev_mask;           /* Event mask for the callback */
197   tPORT_CALLBACK* p_callback; /* Pointer to users callback function */
198   tPORT_MGMT_CALLBACK*
199       p_mgmt_callback; /* Callback function to receive connection up/down */
200   tPORT_DATA_CALLBACK*
201       p_data_callback; /* Callback function to receive data indications */
202   tPORT_DATA_CO_CALLBACK*
203       p_data_co_callback; /* Callback function with callouts and flowctrl */
204   uint16_t credit_tx;     /* Flow control credits for tx path */
205   uint16_t credit_rx;     /* Flow control credits for rx path, this is */
206                           /* number of buffers peer is allowed to sent */
207   uint16_t
208       credit_rx_max; /* Max number of credits we will allow this guy to sent */
209   uint16_t credit_rx_low;   /* Number of credits when we send credit update */
210   uint16_t rx_buf_critical; /* port receive queue critical watermark level */
211   bool keep_port_handle;    /* true if port is not deallocated when closing */
212   /* it is set to true for server when allocating port */
213   uint16_t keep_mtu; /* Max MTU that port can receive by server */
214   uint16_t sec_mask; /* Bitmask of security requirements for this port */
215                      /* see the BTM_SEC_* values in btm_api_types.h */
216 } tPORT;
217 
218 /* Define the PORT/RFCOMM control structure
219 */
220 typedef struct {
221   tPORT port[MAX_RFC_PORTS];            /* Port info pool */
222   tRFC_MCB rfc_mcb[MAX_BD_CONNECTIONS]; /* RFCOMM bd_connections pool */
223 } tPORT_CB;
224 
225 /*
226  * Functions provided by the port_utils.cc
227 */
228 tPORT* port_allocate_port(uint8_t dlci, const RawAddress& bd_addr);
229 void port_set_defaults(tPORT* p_port);
230 void port_select_mtu(tPORT* p_port);
231 void port_release_port(tPORT* p_port);
232 tPORT* port_find_mcb_dlci_port(tRFC_MCB* p_mcb, uint8_t dlci);
233 tRFC_MCB* port_find_mcb(const RawAddress& bd_addr);
234 tPORT* port_find_dlci_port(uint8_t dlci);
235 tPORT* port_find_port(uint8_t dlci, const RawAddress& bd_addr);
236 uint32_t port_get_signal_changes(tPORT* p_port, uint8_t old_signals,
237                                  uint8_t signal);
238 uint32_t port_flow_control_user(tPORT* p_port);
239 void port_flow_control_peer(tPORT* p_port, bool enable, uint16_t count);
240 
241 /*
242  * Functions provided by the port_rfc.cc
243 */
244 int port_open_continue(tPORT* p_port);
245 void port_start_port_open(tPORT* p_port);
246 void port_start_par_neg(tPORT* p_port);
247 void port_start_control(tPORT* p_port);
248 void port_start_close(tPORT* p_port);
249 void port_rfc_closed(tPORT* p_port, uint8_t res);
250 
251 #endif
252