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