• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_event.h"
38 #include "stack/rfcomm/rfc_state.h"
39 #include "types/raw_address.h"
40 
41 /*
42  * Flow control configuration values for the mux
43  */
44 #define PORT_FC_UNDEFINED 0 /* mux flow control mechanism not defined yet */
45 #define PORT_FC_TS710 1     /* use TS 07.10 flow control  */
46 #define PORT_FC_CREDIT 2    /* use RFCOMM credit based flow control */
47 
48 /*
49  * Define Port Data Transfer control block
50  */
51 typedef struct {
52   fixed_queue_t* queue;       /* Queue of buffers waiting to be sent */
53   bool peer_fc;               /* true if flow control is set based on peer's request */
54   bool user_fc;               /* true if flow control is set based on user's request  */
55   uint32_t queue_size;        /* Number of data bytes in the queue */
56   tPORT_CALLBACK* p_callback; /* Address of the callback function */
57 } tPORT_DATA;
58 
59 /*
60  * Port control structure used to pass modem info
61  */
62 typedef struct {
63 #define MODEM_SIGNAL_DTRDSR 0x01
64 #define MODEM_SIGNAL_RTSCTS 0x02
65 #define MODEM_SIGNAL_RI 0x04
66 #define MODEM_SIGNAL_DCD 0x08
67 
68   uint8_t modem_signal; /* [DTR/DSR | RTS/CTS | RI | DCD ] */
69 
70   uint8_t break_signal; /* 0-3 s in steps of 200 ms */
71 
72   uint8_t discard_buffers; /* 0 - do not discard, 1 - discard */
73 
74 #define RFCOMM_CTRL_BREAK_ASAP 0
75 #define RFCOMM_CTRL_BREAK_IN_SEQ 1
76 
77   uint8_t break_signal_seq; /* as soon as possible | in sequence (default) */
78 
79   bool fc; /* true when the device is unable to accept frames */
80 } tPORT_CTRL;
81 
82 /*
83  * RFCOMM multiplexer Control Block
84  */
85 typedef struct {
86   alarm_t* mcb_timer = nullptr;              /* MCB timer */
87   fixed_queue_t* cmd_q = nullptr;            /* Queue for command messages on this mux */
88   uint8_t port_handles[RFCOMM_MAX_DLCI + 1]; /* Array for quick access to  */
89   /* port handles based on dlci        */
90   RawAddress bd_addr = 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 pending_lcid;                   /* store LCID for incoming connection while connecting */
101   bool pending_configure_complete;         /* true if confiquration of the pending
102                                               connection was completed*/
103   tL2CAP_CFG_INFO pending_cfg_info = {};   /* store configure info for incoming
104                                          connection while connecting */
105 } tRFC_MCB;
106 
107 /*
108  * RFCOMM Port State Machine Control Block
109  */
110 struct RfcommPortSm {
111   tRFC_PORT_STATE state;
112   tRFC_PORT_STATE state_prior;
113   tRFC_PORT_EVENT last_event;
114   tPORT_RESULT close_reason;
115   uint64_t open_timestamp;
116   uint64_t close_timestamp;
117 };
118 
119 /*
120  * RFCOMM Port Connection Control Block
121  */
122 typedef struct {
123   RfcommPortSm sm_cb;  // State machine control block
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 typedef enum : uint8_t {
139   PORT_CONNECTION_STATE_CLOSED = 0,
140   PORT_CONNECTION_STATE_OPENING = 1,
141   PORT_CONNECTION_STATE_OPENED = 2,
142   PORT_CONNECTION_STATE_CLOSING = 3,
143 } tPORT_CONNECTION_STATE;
144 
port_connection_state_text(const tPORT_CONNECTION_STATE & state)145 inline std::string port_connection_state_text(const tPORT_CONNECTION_STATE& state) {
146   switch (state) {
147     CASE_RETURN_STRING(PORT_CONNECTION_STATE_CLOSED);
148     CASE_RETURN_STRING(PORT_CONNECTION_STATE_OPENING);
149     CASE_RETURN_STRING(PORT_CONNECTION_STATE_OPENED);
150     CASE_RETURN_STRING(PORT_CONNECTION_STATE_CLOSING);
151     default:
152       break;
153   }
154   RETURN_UNKNOWN_TYPE_STRING(tPORT_CONNECTION_STATE, state);
155 }
156 
157 namespace std {
158 template <>
159 struct formatter<tPORT_CONNECTION_STATE> : enum_formatter<tPORT_CONNECTION_STATE> {};
160 }  // namespace std
161 
162 /*
163  * Define control block containing information about PORT connection
164  */
165 typedef struct {
166   uint8_t handle;  // Starting from 1, unique for this object
167   bool in_use;     /* True when structure is allocated */
168 
169   tPORT_CONNECTION_STATE state; /* State of the application */
170 
171   uint8_t scn;              /* Service channel number */
172   uint16_t uuid;            /* Service UUID */
173   uint32_t app_uid;         /* UID of the app for which this socket was created */
174   uint64_t sdp_duration_ms; /* Time it took to perform SDP (for metrics) */
175 
176   RawAddress bd_addr; /* BD ADDR of the device for the multiplexer channel */
177   bool is_server;     /* true if the server application */
178   uint8_t dlci;       /* DLCI of the connection */
179 
180   uint8_t line_status; /* Line status as reported by peer */
181 
182   uint8_t default_signal_state; /* Initial signal state depending on uuid */
183 
184   uint16_t mtu;      /* Max MTU that port can receive */
185   uint16_t peer_mtu; /* Max MTU that port can send */
186 
187   tPORT_DATA tx; /* Control block for data from app to peer */
188   tPORT_DATA rx; /* Control block for data from peer to app */
189 
190   PortSettings user_port_settings; /* Port parameters for user connection */
191   PortSettings peer_port_settings; /* Port parameters for peer connection */
192 
193   tPORT_CTRL local_ctrl;
194   tPORT_CTRL peer_ctrl;
195 
196 #define PORT_CTRL_REQ_SENT 0x01
197 #define PORT_CTRL_REQ_CONFIRMED 0x02
198 #define PORT_CTRL_IND_RECEIVED 0x04
199 #define PORT_CTRL_IND_RESPONDED 0x08
200 #define PORT_CTRL_SETUP_COMPLETED \
201   (PORT_CTRL_REQ_SENT | PORT_CTRL_REQ_CONFIRMED | PORT_CTRL_IND_RECEIVED | PORT_CTRL_IND_RESPONDED)
202 
203   uint8_t port_ctrl; /* Modem Status Command  */
204 
205   bool rx_flag_ev_pending; /* RXFLAG Character is received */
206 
207   tRFC_PORT rfc; /* RFCOMM port control block */
208 
209   uint32_t ev_mask;                           /* Event mask for the callback */
210   tPORT_CALLBACK* p_callback;                 /* Pointer to users callback function */
211   tPORT_MGMT_CALLBACK* p_mgmt_callback;       /* Callback function to receive connection up/down */
212   tPORT_DATA_CALLBACK* p_data_callback;       /* Callback function to receive data indications */
213   tPORT_DATA_CO_CALLBACK* p_data_co_callback; /* Callback function with callouts and flowctrl */
214   uint16_t credit_tx;                         /* Flow control credits for tx path */
215   uint16_t credit_rx;                         /* Flow control credits for rx path, this is */
216                                               /* number of buffers peer is allowed to sent */
217   uint16_t credit_rx_max;   /* Max number of credits we will allow this guy to sent */
218   uint16_t credit_rx_low;   /* Number of credits when we send credit update */
219   uint16_t rx_buf_critical; /* port receive queue critical watermark level */
220   bool keep_port_handle;    /* true if port is not deallocated when closing */
221   /* it is set to true for server when allocating port */
222   uint16_t keep_mtu; /* Max MTU that port can receive by server */
223   uint16_t sec_mask; /* Bitmask of security requirements for this port */
224                      /* see the BTM_SEC_* values in btm_api_types.h */
225   RfcommCfgInfo rfc_cfg_info; /* store optional rfc configure info for incoming */
226                               /* connection while connecting */
227 } tPORT;
228 
229 /* Define the PORT/RFCOMM control structure
230  */
231 typedef struct {
232   tPORT port[MAX_RFC_PORTS];            /* Port info pool */
233   tRFC_MCB rfc_mcb[MAX_BD_CONNECTIONS]; /* RFCOMM bd_connections pool */
234 } tPORT_CB;
235 
236 /*
237  * Functions provided by the port_utils.cc
238  */
239 tPORT* port_allocate_port(uint8_t dlci, const RawAddress& bd_addr);
240 void port_set_defaults(tPORT* p_port);
241 void port_select_mtu(tPORT* p_port);
242 void port_release_port(tPORT* p_port);
243 tPORT* port_find_mcb_dlci_port(tRFC_MCB* p_mcb, uint8_t dlci);
244 tRFC_MCB* port_find_mcb(const RawAddress& bd_addr);
245 tPORT* port_find_dlci_port(uint8_t dlci);
246 tPORT* port_find_port(uint8_t dlci, const RawAddress& bd_addr);
247 uint32_t port_get_signal_changes(tPORT* p_port, uint8_t old_signals, uint8_t signal);
248 uint32_t port_flow_control_user(tPORT* p_port);
249 void port_flow_control_peer(tPORT* p_port, bool enable, uint16_t count);
250 
251 /*
252  * Functions provided by the port_rfc.cc
253  */
254 int port_open_continue(tPORT* p_port);
255 void port_start_port_open(tPORT* p_port);
256 void port_start_par_neg(tPORT* p_port);
257 void port_start_control(tPORT* p_port);
258 void port_start_close(tPORT* p_port);
259 void port_rfc_closed(tPORT* p_port, uint8_t res);
260 
261 #endif
262