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