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 RFC unit
22  *
23  *****************************************************************************/
24 
25 #ifndef RFC_INT_H
26 #define RFC_INT_H
27 
28 #include "l2c_api.h"
29 #include "port_int.h"
30 
31 /*
32  * Define RFCOMM result codes
33 */
34 #define RFCOMM_SUCCESS 0
35 #define RFCOMM_ERROR 1
36 #define RFCOMM_LOW_RESOURCES 2
37 #define RFCOMM_TRY_LATER 3
38 
39 #define RFCOMM_USER_ERR 111
40 #define RFCOMM_SECURITY_ERR 112
41 
42 /*
43  * Define max and min RFCOMM MTU (N1)
44 */
45 #define RFCOMM_MIN_MTU 23
46 #define RFCOMM_MAX_MTU 32767
47 
48 extern void RFCOMM_StartReq(tRFC_MCB* p_mcb);
49 extern void RFCOMM_StartRsp(tRFC_MCB* p_mcb, uint16_t result);
50 
51 extern void RFCOMM_DlcEstablishReq(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu);
52 extern void RFCOMM_DlcEstablishRsp(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu,
53                                    uint16_t result);
54 
55 extern void RFCOMM_DataReq(tRFC_MCB* p_mcb, uint8_t dlci, BT_HDR* p_buf);
56 
57 extern void RFCOMM_DlcReleaseReq(tRFC_MCB* p_mcb, uint8_t dlci);
58 
59 extern void RFCOMM_ParameterNegotiationRequest(tRFC_MCB* p_mcb, uint8_t dlci,
60                                                uint16_t mtu);
61 extern void RFCOMM_ParameterNegotiationResponse(tRFC_MCB* p_mcb, uint8_t dlci,
62                                                 uint16_t mtu, uint8_t cl,
63                                                 uint8_t k);
64 
65 extern void RFCOMM_TestReq(uint8_t* p_data, uint16_t len);
66 
67 extern void RFCOMM_FlowReq(tRFC_MCB* p_mcb, uint8_t dlci, bool state);
68 
69 extern void RFCOMM_PortParameterNegotiationRequest(tRFC_MCB* p_mcb,
70                                                    uint8_t dlci,
71                                                    tPORT_STATE* p_pars);
72 extern void RFCOMM_PortParameterNegotiationResponse(tRFC_MCB* p_mcb,
73                                                     uint8_t dlci,
74                                                     tPORT_STATE* p_pars,
75                                                     uint16_t param_mask);
76 
77 extern void RFCOMM_ControlReq(tRFC_MCB* p_mcb, uint8_t dlci,
78                               tPORT_CTRL* p_pars);
79 extern void RFCOMM_ControlRsp(tRFC_MCB* p_mcb, uint8_t dlci,
80                               tPORT_CTRL* p_pars);
81 
82 extern void RFCOMM_LineStatusReq(tRFC_MCB* p_mcb, uint8_t dlci,
83                                  uint8_t line_status);
84 /*
85  * Define logical struct used for sending and decoding MX frames
86 */
87 typedef struct {
88   uint8_t dlci;
89   uint8_t type;
90   uint8_t cr;
91   uint8_t ea;
92   uint8_t pf;
93   uint8_t credit;
94 
95   union {
96     struct {
97       uint8_t dlci;
98       uint8_t frame_type;
99       uint8_t conv_layer;
100       uint8_t priority;
101       uint8_t t1;
102       uint16_t mtu;
103       uint8_t n2;
104       uint8_t k;
105     } pn;
106     struct {
107       uint8_t* p_data;
108       uint16_t data_len;
109     } test;
110     struct {
111       uint8_t dlci;
112       uint8_t signals;
113       uint8_t break_present;
114       uint8_t break_duration;
115     } msc;
116     struct {
117       uint8_t ea;
118       uint8_t cr;
119       uint8_t type;
120     } nsc;
121     struct {
122       uint8_t dlci;
123       uint8_t is_request;
124       uint8_t baud_rate;
125       uint8_t byte_size;
126       uint8_t stop_bits;
127       uint8_t parity;
128       uint8_t parity_type;
129       uint8_t fc_type;
130       uint8_t xon_char;
131       uint8_t xoff_char;
132       uint16_t param_mask;
133     } rpn;
134     struct {
135       uint8_t dlci;
136       uint8_t line_status;
137     } rls;
138   } u;
139 } MX_FRAME;
140 
141 #define LINE_STATUS_NO_ERROR 0x00
142 #define LINE_STATUS_OVERRUN 0x02  /* Receive Overrun Error   */
143 #define LINE_STATUS_RXPARITY 0x04 /* Receive Parity Error    */
144 #define LINE_STATUS_FRAME 0x08    /* Receive Framing error   */
145 #define LINE_STATUS_FAILED 0x10   /* Connection Failed       */
146 
147 /*
148  * Define states and events for the RFC multiplexer state machine
149 */
150 #define RFC_MX_STATE_IDLE 0
151 #define RFC_MX_STATE_WAIT_CONN_CNF 1
152 #define RFC_MX_STATE_CONFIGURE 2
153 #define RFC_MX_STATE_SABME_WAIT_UA 3
154 #define RFC_MX_STATE_WAIT_SABME 4
155 #define RFC_MX_STATE_CONNECTED 5
156 #define RFC_MX_STATE_DISC_WAIT_UA 6
157 
158 /*
159  * Define port states
160 */
161 #define RFC_STATE_CLOSED 0
162 #define RFC_STATE_SABME_WAIT_UA 1
163 #define RFC_STATE_ORIG_WAIT_SEC_CHECK 2
164 #define RFC_STATE_TERM_WAIT_SEC_CHECK 3
165 #define RFC_STATE_OPENED 4
166 #define RFC_STATE_DISC_WAIT_UA 5
167 
168 /*
169  * Events that can be received by multiplexer as well as port state machines
170 */
171 #define RFC_EVENT_SABME 0
172 #define RFC_EVENT_UA 1
173 #define RFC_EVENT_DM 2
174 #define RFC_EVENT_DISC 3
175 #define RFC_EVENT_UIH 4
176 #define RFC_EVENT_TIMEOUT 5
177 #define RFC_EVENT_BAD_FRAME 50
178 /*
179  * Multiplexer events
180 */
181 #define RFC_MX_EVENT_START_REQ 6
182 #define RFC_MX_EVENT_START_RSP 7
183 #define RFC_MX_EVENT_CLOSE_REQ 8
184 #define RFC_MX_EVENT_CONN_CNF 9
185 #define RFC_MX_EVENT_CONN_IND 10
186 #define RFC_MX_EVENT_CONF_CNF 11
187 #define RFC_MX_EVENT_CONF_IND 12
188 #define RFC_MX_EVENT_QOS_VIOLATION_IND 13
189 #define RFC_MX_EVENT_DISC_IND 14
190 #define RFC_MX_EVENT_TEST_CMD 15
191 #define RFC_MX_EVENT_TEST_RSP 16
192 #define RFC_MX_EVENT_FCON_CMD 17
193 #define RFC_MX_EVENT_FCOFF_CMD 18
194 #define RFC_MX_EVENT_NSC 19
195 #define RFC_MX_EVENT_NSC_RSP 20
196 
197 /*
198  * Port events
199 */
200 #define RFC_EVENT_OPEN 9
201 #define RFC_EVENT_ESTABLISH_RSP 11
202 #define RFC_EVENT_CLOSE 12
203 #define RFC_EVENT_CLEAR 13
204 #define RFC_EVENT_DATA 14
205 #define RFC_EVENT_SEC_COMPLETE 15
206 
207 /* seconds to wait for reply with Poll bit */
208 #define RFC_T1_TIMEOUT 20
209 /* seconds to wait for reply with Poll bit other than MX */
210 #define RFC_PORT_T1_TIMEOUT 60
211 /* timeout to wait for Mx UIH */
212 #define RFC_T2_TIMEOUT 20
213 /* If something goes wrong and we send DISC we should not wait for min */
214 #define RFC_DISC_TIMEOUT 3
215 #define RFC_CLOSE_TIMEOUT 10
216 /* first connection to be established on Mx */
217 #define RFCOMM_CONN_TIMEOUT 120
218 
219 /* Define RFComm control block
220 */
221 typedef struct {
222   MX_FRAME rx_frame;
223   tL2CAP_APPL_INFO reg_info; /* L2CAP Registration info */
224 
225   /* MCB based on the L2CAP's lcid */
226   tRFC_MCB* p_rfc_lcid_mcb[MAX_L2CAP_CHANNELS];
227   bool peer_rx_disabled; /* If true peer sent FCOFF */
228   uint8_t last_mux;      /* Last mux allocated */
229   uint8_t last_port_index;  // Index of last port allocated in rfc_cb.port
230 } tRFCOMM_CB;
231 
232 /* Main Control Block for the RFCOMM Layer (PORT and RFC) */
233 typedef struct {
234   tRFCOMM_CB rfc;
235   tPORT_CB port;
236   uint8_t trace_level;
237 } tRFC_CB;
238 
239 extern tRFC_CB rfc_cb;
240 
241 /* Timer running on the multiplexor channel while no DLCI connection is open */
242 #define RFC_MCB_INIT_INACT_TIMER 60 /* in seconds */
243 
244 /* Timer running on the multiplexor channel after last DLCI is released */
245 #define RFC_MCB_RELEASE_INACT_TIMER 2 /* in seconds */
246 
247 /*
248  * Define RFCOMM frame processing errors
249 */
250 #define RFCOMM_ERR_BAD_SABME 1
251 #define RFCOMM_ERR_BAD_UA 2
252 #define RFCOMM_ERR_BAD_DM 3
253 #define RFCOMM_ERR_BAD_DISC 4
254 #define RFCOMM_ERR_BAD_UIH 5
255 
256 #ifdef RFCOMM_PRECALC_FCS
257 
258 #define RFCOMM_SABME_FCS(p_data, cr, dlci) rfc_sabme_fcs[cr][dlci]
259 #define RFCOMM_UA_FCS(p_data, cr, dlci) rfc_ua_fcs[cr][dlci]
260 #define RFCOMM_DM_FCS(p_data, cr, dlci) rfc_dm_fcs[cr][dlci]
261 #define RFCOMM_DISC_FCS(p_data, cr, dlci) rfc_disc_fcs[cr][dlci]
262 #define RFCOMM_UIH_FCS(p_data, dlci) rfc_uih_fcs[dlci]
263 
264 #else
265 
266 extern uint8_t rfc_calc_fcs(uint16_t len, uint8_t* p);
267 
268 #define RFCOMM_SABME_FCS(p_data, cr, dlci) rfc_calc_fcs(3, p_data)
269 #define RFCOMM_UA_FCS(p_data, cr, dlci) rfc_calc_fcs(3, p_data)
270 #define RFCOMM_DM_FCS(p_data, cr, dlci) rfc_calc_fcs(3, p_data)
271 #define RFCOMM_DISC_FCS(p_data, cr, dlci) rfc_calc_fcs(3, p_data)
272 #define RFCOMM_UIH_FCS(p_data, dlci) rfc_calc_fcs(2, p_data)
273 
274 #endif
275 
276 extern void rfc_mx_sm_execute(tRFC_MCB* p_mcb, uint16_t event, void* p_data);
277 
278 /*
279  * Functions provided by the rfc_port_fsm.cc
280 */
281 extern void rfc_port_sm_execute(tPORT* p_port, uint16_t event, void* p_data);
282 
283 extern void rfc_process_pn(tRFC_MCB* p_rfc_mcb, bool is_command,
284                            MX_FRAME* p_frame);
285 extern void rfc_process_msc(tRFC_MCB* p_rfc_mcb, bool is_command,
286                             MX_FRAME* p_frame);
287 extern void rfc_process_rpn(tRFC_MCB* p_rfc_mcb, bool is_command,
288                             bool is_request, MX_FRAME* p_frame);
289 extern void rfc_process_rls(tRFC_MCB* p_rfc_mcb, bool is_command,
290                             MX_FRAME* p_frame);
291 extern void rfc_process_nsc(tRFC_MCB* p_rfc_mcb, MX_FRAME* p_frame);
292 extern void rfc_process_test_rsp(tRFC_MCB* p_rfc_mcb, BT_HDR* p_buf);
293 extern void rfc_process_fcon(tRFC_MCB* p_rfc_mcb, bool is_command);
294 extern void rfc_process_fcoff(tRFC_MCB* p_rfc_mcb, bool is_command);
295 extern void rfc_process_l2cap_congestion(tRFC_MCB* p_mcb, bool is_congested);
296 
297 /*
298  * Functions provided by the rfc_utils.cc
299 */
300 tRFC_MCB* rfc_alloc_multiplexer_channel(const RawAddress& bd_addr,
301                                         bool is_initiator);
302 extern void rfc_release_multiplexer_channel(tRFC_MCB* p_rfc_mcb);
303 extern void rfc_timer_start(tRFC_MCB* p_rfc_mcb, uint16_t timeout);
304 extern void rfc_timer_stop(tRFC_MCB* p_rfc_mcb);
305 extern void rfc_port_timer_start(tPORT* p_port, uint16_t tout);
306 extern void rfc_port_timer_stop(tPORT* p_port);
307 
308 bool rfc_check_uih_fcs(uint8_t dlci, uint8_t received_fcs);
309 bool rfc_check_fcs(uint16_t len, uint8_t* p, uint8_t received_fcs);
310 tRFC_MCB* rfc_find_lcid_mcb(uint16_t lcid);
311 extern void rfc_save_lcid_mcb(tRFC_MCB* p_rfc_mcb, uint16_t lcid);
312 extern void rfc_check_mcb_active(tRFC_MCB* p_mcb);
313 extern void rfc_port_closed(tPORT* p_port);
314 extern void rfc_sec_check_complete(const RawAddress* bd_addr,
315                                    tBT_TRANSPORT transport, void* p_ref_data,
316                                    uint8_t res);
317 extern void rfc_inc_credit(tPORT* p_port, uint8_t credit);
318 extern void rfc_dec_credit(tPORT* p_port);
319 extern void rfc_check_send_cmd(tRFC_MCB* p_mcb, BT_HDR* p_buf);
320 
321 /*
322  * Functions provided by the rfc_ts_frames.cc
323 */
324 extern void rfc_send_sabme(tRFC_MCB* p_rfc_mcb, uint8_t dlci);
325 extern void rfc_send_ua(tRFC_MCB* p_rfc_mcb, uint8_t dlci);
326 extern void rfc_send_dm(tRFC_MCB* p_rfc_mcb, uint8_t dlci, bool pf);
327 extern void rfc_send_disc(tRFC_MCB* p_rfc_mcb, uint8_t dlci);
328 extern void rfc_send_pn(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command,
329                         uint16_t mtu, uint8_t cl, uint8_t k);
330 extern void rfc_send_test(tRFC_MCB* p_rfc_mcb, bool is_command, BT_HDR* p_buf);
331 extern void rfc_send_msc(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command,
332                          tPORT_CTRL* p_pars);
333 extern void rfc_send_rls(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command,
334                          uint8_t status);
335 extern void rfc_send_rpn(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command,
336                          tPORT_STATE* p_pars, uint16_t mask);
337 extern void rfc_send_fcon(tRFC_MCB* p_mcb, bool is_command);
338 extern void rfc_send_fcoff(tRFC_MCB* p_mcb, bool is_command);
339 extern void rfc_send_buf_uih(tRFC_MCB* p_rfc_mcb, uint8_t dlci, BT_HDR* p_buf);
340 extern void rfc_send_credit(tRFC_MCB* p_mcb, uint8_t dlci, uint8_t credit);
341 extern void rfc_process_mx_message(tRFC_MCB* p_rfc_mcb, BT_HDR* p_buf);
342 extern uint8_t rfc_parse_data(tRFC_MCB* p_rfc_mcb, MX_FRAME* p_frame,
343                               BT_HDR* p_buf);
344 
345 /* Call back functions from RFCOMM */
346 extern void rfcomm_l2cap_if_init(void);
347 
348 extern void PORT_StartInd(tRFC_MCB* p_mcb);
349 extern void PORT_StartCnf(tRFC_MCB* p_mcb, uint16_t result);
350 
351 extern void PORT_CloseInd(tRFC_MCB* p_mcb);
352 extern void Port_TimeOutCloseMux(tRFC_MCB* p_mcb);
353 
354 extern void PORT_DlcEstablishInd(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu);
355 extern void PORT_DlcEstablishCnf(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu,
356                                  uint16_t result);
357 
358 extern void PORT_DataInd(tRFC_MCB* p_mcb, uint8_t dlci, BT_HDR* p_buf);
359 
360 extern void PORT_DlcReleaseInd(tRFC_MCB* p_mcb, uint8_t dlci);
361 
362 extern void PORT_ParNegInd(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu,
363                            uint8_t cl, uint8_t k);
364 extern void PORT_ParNegCnf(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu,
365                            uint8_t cl, uint8_t k);
366 
367 extern void PORT_TestCnf(tRFC_MCB* p_mcb, uint8_t* p_data, uint16_t len);
368 
369 extern void PORT_FlowInd(tRFC_MCB* p_mcb, uint8_t dlci, bool fc);
370 
371 extern void PORT_PortNegInd(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_STATE* p_pars,
372                             uint16_t param_mask);
373 extern void PORT_PortNegCnf(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_STATE* p_pars,
374                             uint16_t result);
375 
376 extern void PORT_ControlInd(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_CTRL* p_pars);
377 extern void PORT_ControlCnf(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_CTRL* p_pars);
378 
379 extern void PORT_LineStatusInd(tRFC_MCB* p_mcb, uint8_t dlci,
380                                uint8_t line_status);
381 
382 #endif
383