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