1 /******************************************************************************
2 *
3 * Copyright (C) 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 functions to send TS 07.10 frames
22 *
23 ******************************************************************************/
24
25 #include <stddef.h>
26 #include <string.h>
27 #include "common/bt_target.h"
28 #include "stack/rfcdefs.h"
29 #include "stack/port_api.h"
30 #include "stack/l2c_api.h"
31 #include "port_int.h"
32 #include "rfc_int.h"
33 #include "osi/mutex.h"
34 #include "osi/allocator.h"
35 #if (defined RFCOMM_INCLUDED && RFCOMM_INCLUDED == TRUE)
36
37 /*******************************************************************************
38 **
39 ** Function rfc_send_sabme
40 **
41 ** Description This function sends SABME frame.
42 **
43 *******************************************************************************/
rfc_send_sabme(tRFC_MCB * p_mcb,UINT8 dlci)44 void rfc_send_sabme (tRFC_MCB *p_mcb, UINT8 dlci)
45 {
46 BT_HDR *p_buf;
47 UINT8 *p_data;
48 UINT8 cr = RFCOMM_CR(p_mcb->is_initiator, TRUE);
49
50 if ((p_buf = (BT_HDR *)osi_malloc(RFCOMM_CMD_BUF_SIZE)) == NULL) {
51 return;
52 }
53
54 p_buf->offset = L2CAP_MIN_OFFSET;
55 p_data = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
56
57 /* SABME frame, command, PF = 1, dlci */
58 *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
59 *p_data++ = RFCOMM_SABME | RFCOMM_PF;
60 *p_data++ = RFCOMM_EA | 0;
61
62 *p_data = RFCOMM_SABME_FCS ((UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET, cr, dlci);
63
64 p_buf->len = 4;
65
66 rfc_check_send_cmd(p_mcb, p_buf);
67 }
68
69
70 /*******************************************************************************
71 **
72 ** Function rfc_send_ua
73 **
74 ** Description This function sends UA frame.
75 **
76 *******************************************************************************/
rfc_send_ua(tRFC_MCB * p_mcb,UINT8 dlci)77 void rfc_send_ua (tRFC_MCB *p_mcb, UINT8 dlci)
78 {
79 BT_HDR *p_buf;
80 UINT8 *p_data;
81 UINT8 cr = RFCOMM_CR(p_mcb->is_initiator, FALSE);
82
83 if ((p_buf = (BT_HDR *)osi_malloc(RFCOMM_CMD_BUF_SIZE)) == NULL) {
84 return;
85 }
86
87 p_buf->offset = L2CAP_MIN_OFFSET;
88 p_data = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
89
90 /* ua frame, response, PF = 1, dlci */
91 *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
92 *p_data++ = RFCOMM_UA | RFCOMM_PF;
93 *p_data++ = RFCOMM_EA | 0;
94
95 *p_data = RFCOMM_UA_FCS ((UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET, cr, dlci);
96
97 p_buf->len = 4;
98
99 rfc_check_send_cmd(p_mcb, p_buf);
100 }
101
102
103 /*******************************************************************************
104 **
105 ** Function rfc_send_dm
106 **
107 ** Description This function sends DM frame.
108 **
109 *******************************************************************************/
rfc_send_dm(tRFC_MCB * p_mcb,UINT8 dlci,BOOLEAN pf)110 void rfc_send_dm (tRFC_MCB *p_mcb, UINT8 dlci, BOOLEAN pf)
111 {
112 BT_HDR *p_buf;
113 UINT8 *p_data;
114 UINT8 cr = RFCOMM_CR(p_mcb->is_initiator, FALSE);
115
116 if ((p_buf = (BT_HDR *)osi_malloc(RFCOMM_CMD_BUF_SIZE)) == NULL) {
117 return;
118 }
119
120 p_buf->offset = L2CAP_MIN_OFFSET;
121 p_data = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
122
123 /* DM frame, response, PF = 1, dlci */
124 *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
125 *p_data++ = RFCOMM_DM | ((pf) ? RFCOMM_PF : 0);
126 *p_data++ = RFCOMM_EA | 0;
127
128 *p_data = RFCOMM_DM_FCS ((UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET, cr, dlci);
129
130 p_buf->len = 4;
131
132 rfc_check_send_cmd(p_mcb, p_buf);
133 }
134
135
136 /*******************************************************************************
137 **
138 ** Function rfc_send_disc
139 **
140 ** Description This function sends DISC frame.
141 **
142 *******************************************************************************/
rfc_send_disc(tRFC_MCB * p_mcb,UINT8 dlci)143 void rfc_send_disc (tRFC_MCB *p_mcb, UINT8 dlci)
144 {
145 BT_HDR *p_buf;
146 UINT8 *p_data;
147 UINT8 cr = RFCOMM_CR(p_mcb->is_initiator, TRUE);
148
149 if ((p_buf = (BT_HDR *)osi_malloc(RFCOMM_CMD_BUF_SIZE)) == NULL) {
150 return;
151 }
152
153 p_buf->offset = L2CAP_MIN_OFFSET;
154 p_data = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
155
156 /* DISC frame, command, PF = 1, dlci */
157 *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
158 *p_data++ = RFCOMM_DISC | RFCOMM_PF;
159 *p_data++ = RFCOMM_EA | 0;
160
161 *p_data = RFCOMM_DISC_FCS ((UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET, cr, dlci);
162
163 p_buf->len = 4;
164
165 rfc_check_send_cmd(p_mcb, p_buf);
166 }
167
168
169 /*******************************************************************************
170 **
171 ** Function rfc_send_buf_uih
172 **
173 ** Description This function sends UIH frame.
174 **
175 *******************************************************************************/
rfc_send_buf_uih(tRFC_MCB * p_mcb,UINT8 dlci,BT_HDR * p_buf)176 void rfc_send_buf_uih (tRFC_MCB *p_mcb, UINT8 dlci, BT_HDR *p_buf)
177 {
178 UINT8 *p_data;
179 UINT8 cr = RFCOMM_CR(p_mcb->is_initiator, TRUE);
180 UINT8 credits;
181
182 p_buf->offset -= RFCOMM_CTRL_FRAME_LEN;
183 if (p_buf->len > 127) {
184 p_buf->offset--;
185 }
186
187 if (dlci) {
188 credits = (UINT8)p_buf->layer_specific;
189 } else {
190 credits = 0;
191 }
192
193 if (credits) {
194 p_buf->offset--;
195 }
196
197 p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
198
199 /* UIH frame, command, PF = 0, dlci */
200 *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
201 *p_data++ = RFCOMM_UIH | ((credits) ? RFCOMM_PF : 0);
202 if (p_buf->len <= 127) {
203 *p_data++ = RFCOMM_EA | (p_buf->len << 1);
204 p_buf->len += 3;
205 } else {
206 *p_data++ = (p_buf->len & 0x7f) << 1;
207 *p_data++ = p_buf->len >> RFCOMM_SHIFT_LENGTH2;
208 p_buf->len += 4;
209 }
210
211 if (credits) {
212 *p_data++ = credits;
213 p_buf->len++;
214 }
215
216 p_data = (UINT8 *)(p_buf + 1) + p_buf->offset + p_buf->len++;
217
218 *p_data = RFCOMM_UIH_FCS ((UINT8 *)(p_buf + 1) + p_buf->offset, dlci);
219
220 if (dlci == RFCOMM_MX_DLCI) {
221 rfc_check_send_cmd(p_mcb, p_buf);
222 } else {
223 L2CA_DataWrite (p_mcb->lcid, p_buf);
224 }
225 }
226
227
228 /*******************************************************************************
229 **
230 ** Function rfc_send_pn
231 **
232 ** Description This function sends DLC Parameters Negotiation Frame.
233 **
234 *******************************************************************************/
rfc_send_pn(tRFC_MCB * p_mcb,UINT8 dlci,BOOLEAN is_command,UINT16 mtu,UINT8 cl,UINT8 k)235 void rfc_send_pn (tRFC_MCB *p_mcb, UINT8 dlci, BOOLEAN is_command, UINT16 mtu, UINT8 cl, UINT8 k)
236 {
237 BT_HDR *p_buf;
238 UINT8 *p_data;
239
240 if ((p_buf = (BT_HDR *)osi_malloc(RFCOMM_CMD_BUF_SIZE)) == NULL) {
241 return;
242 }
243
244 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
245 p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
246
247 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_PN;
248 *p_data++ = RFCOMM_EA | (RFCOMM_MX_PN_LEN << 1);
249
250 *p_data++ = dlci;
251 *p_data++ = RFCOMM_PN_FRAM_TYPE_UIH | cl;
252
253 /* It appeared that we need to reply with the same priority bits as we received.
254 ** We will use the fact that we reply in the same context so rx_frame can still be used.
255 */
256 if (is_command) {
257 *p_data++ = RFCOMM_PN_PRIORITY_0;
258 } else {
259 *p_data++ = rfc_cb.rfc.rx_frame.u.pn.priority;
260 }
261
262 *p_data++ = RFCOMM_T1_DSEC;
263 *p_data++ = mtu & 0xFF;
264 *p_data++ = mtu >> 8;
265 *p_data++ = RFCOMM_N2;
266 *p_data = k;
267
268 /* Total length is sizeof PN data + mx header 2 */
269 p_buf->len = RFCOMM_MX_PN_LEN + 2;
270
271 rfc_send_buf_uih (p_mcb, RFCOMM_MX_DLCI, p_buf);
272 }
273
274
275 /*******************************************************************************
276 **
277 ** Function rfc_send_fcon
278 **
279 ** Description This function sends Flow Control On Command.
280 **
281 *******************************************************************************/
rfc_send_fcon(tRFC_MCB * p_mcb,BOOLEAN is_command)282 void rfc_send_fcon (tRFC_MCB *p_mcb, BOOLEAN is_command)
283 {
284 BT_HDR *p_buf;
285 UINT8 *p_data;
286
287 if ((p_buf = (BT_HDR *)osi_malloc(RFCOMM_CMD_BUF_SIZE)) == NULL) {
288 return;
289 }
290
291 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
292 p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
293
294 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_FCON;
295 *p_data++ = RFCOMM_EA | (RFCOMM_MX_FCON_LEN << 1);
296
297 /* Total length is sizeof FCON data + mx header 2 */
298 p_buf->len = RFCOMM_MX_FCON_LEN + 2;
299
300 rfc_send_buf_uih (p_mcb, RFCOMM_MX_DLCI, p_buf);
301 }
302
303
304 /*******************************************************************************
305 **
306 ** Function rfc_send_fcoff
307 **
308 ** Description This function sends Flow Control Off Command.
309 **
310 *******************************************************************************/
rfc_send_fcoff(tRFC_MCB * p_mcb,BOOLEAN is_command)311 void rfc_send_fcoff (tRFC_MCB *p_mcb, BOOLEAN is_command)
312 {
313 BT_HDR *p_buf;
314 UINT8 *p_data;
315
316 if ((p_buf = (BT_HDR *)osi_malloc(RFCOMM_CMD_BUF_SIZE)) == NULL) {
317 return;
318 }
319
320 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
321 p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
322
323 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_FCOFF;
324 *p_data++ = RFCOMM_EA | (RFCOMM_MX_FCOFF_LEN << 1);
325
326 /* Total length is sizeof FCOFF data + mx header 2 */
327 p_buf->len = RFCOMM_MX_FCOFF_LEN + 2;
328
329 rfc_send_buf_uih (p_mcb, RFCOMM_MX_DLCI, p_buf);
330 }
331
332
333 /*******************************************************************************
334 **
335 ** Function rfc_send_msc
336 **
337 ** Description This function sends Modem Status Command Frame.
338 **
339 *******************************************************************************/
rfc_send_msc(tRFC_MCB * p_mcb,UINT8 dlci,BOOLEAN is_command,tPORT_CTRL * p_pars)340 void rfc_send_msc (tRFC_MCB *p_mcb, UINT8 dlci, BOOLEAN is_command,
341 tPORT_CTRL *p_pars)
342 {
343 BT_HDR *p_buf;
344 UINT8 *p_data;
345 UINT8 signals;
346 UINT8 break_duration;
347 UINT8 len;
348
349 signals = p_pars->modem_signal;
350 break_duration = p_pars->break_signal;
351
352 if ((p_buf = (BT_HDR *)osi_malloc(RFCOMM_CMD_BUF_SIZE)) == NULL) {
353 return;
354 }
355
356 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
357 p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
358
359 if (break_duration) {
360 len = RFCOMM_MX_MSC_LEN_WITH_BREAK;
361 } else {
362 len = RFCOMM_MX_MSC_LEN_NO_BREAK;
363 }
364
365 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_MSC;
366 *p_data++ = RFCOMM_EA | (len << 1);
367
368 *p_data++ = RFCOMM_EA | RFCOMM_CR_MASK | (dlci << RFCOMM_SHIFT_DLCI);
369 *p_data++ = RFCOMM_EA |
370 ((p_pars->fc) ? RFCOMM_MSC_FC : 0) |
371 ((signals & MODEM_SIGNAL_DTRDSR) ? RFCOMM_MSC_RTC : 0) |
372 ((signals & MODEM_SIGNAL_RTSCTS) ? RFCOMM_MSC_RTR : 0) |
373 ((signals & MODEM_SIGNAL_RI) ? RFCOMM_MSC_IC : 0) |
374 ((signals & MODEM_SIGNAL_DCD) ? RFCOMM_MSC_DV : 0);
375
376 if (break_duration) {
377 *p_data++ = RFCOMM_EA | RFCOMM_MSC_BREAK_PRESENT_MASK |
378 (break_duration << RFCOMM_MSC_SHIFT_BREAK);
379 }
380
381 /* Total length is sizeof MSC data + mx header 2 */
382 p_buf->len = len + 2;
383
384 rfc_send_buf_uih (p_mcb, RFCOMM_MX_DLCI, p_buf);
385 }
386
387
388 /*******************************************************************************
389 **
390 ** Function rfc_send_rls
391 **
392 ** Description This function sends Remote Line Status Command Frame.
393 **
394 *******************************************************************************/
rfc_send_rls(tRFC_MCB * p_mcb,UINT8 dlci,BOOLEAN is_command,UINT8 status)395 void rfc_send_rls (tRFC_MCB *p_mcb, UINT8 dlci, BOOLEAN is_command, UINT8 status)
396 {
397 BT_HDR *p_buf;
398 UINT8 *p_data;
399
400 if ((p_buf = (BT_HDR *)osi_malloc(RFCOMM_CMD_BUF_SIZE)) == NULL) {
401 return;
402 }
403
404 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
405 p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
406
407 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_RLS;
408 *p_data++ = RFCOMM_EA | (RFCOMM_MX_RLS_LEN << 1);
409
410 *p_data++ = RFCOMM_EA | RFCOMM_CR_MASK | (dlci << RFCOMM_SHIFT_DLCI);
411 *p_data++ = RFCOMM_RLS_ERROR | status;
412
413 /* Total length is sizeof RLS data + mx header 2 */
414 p_buf->len = RFCOMM_MX_RLS_LEN + 2;
415
416 rfc_send_buf_uih (p_mcb, RFCOMM_MX_DLCI, p_buf);
417 }
418
419
420 /*******************************************************************************
421 **
422 ** Function rfc_send_nsc
423 **
424 ** Description This function sends Non Supported Command Response.
425 **
426 *******************************************************************************/
rfc_send_nsc(tRFC_MCB * p_mcb)427 void rfc_send_nsc (tRFC_MCB *p_mcb)
428 {
429 BT_HDR *p_buf;
430 UINT8 *p_data;
431
432 if ((p_buf = (BT_HDR *)osi_malloc(RFCOMM_CMD_BUF_SIZE)) == NULL) {
433 return;
434 }
435
436 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
437 p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
438
439 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(FALSE) | RFCOMM_MX_NSC;
440 *p_data++ = RFCOMM_EA | (RFCOMM_MX_NSC_LEN << 1);
441
442 *p_data++ = rfc_cb.rfc.rx_frame.ea |
443 (rfc_cb.rfc.rx_frame.cr << RFCOMM_SHIFT_CR) |
444 rfc_cb.rfc.rx_frame.type;
445
446 /* Total length is sizeof NSC data + mx header 2 */
447 p_buf->len = RFCOMM_MX_NSC_LEN + 2;
448
449 rfc_send_buf_uih (p_mcb, RFCOMM_MX_DLCI, p_buf);
450 }
451
452
453 /*******************************************************************************
454 **
455 ** Function rfc_send_rpn
456 **
457 ** Description This function sends Remote Port Negotiation Command
458 **
459 *******************************************************************************/
rfc_send_rpn(tRFC_MCB * p_mcb,UINT8 dlci,BOOLEAN is_command,tPORT_STATE * p_pars,UINT16 mask)460 void rfc_send_rpn (tRFC_MCB *p_mcb, UINT8 dlci, BOOLEAN is_command,
461 tPORT_STATE *p_pars, UINT16 mask)
462 {
463 BT_HDR *p_buf;
464 UINT8 *p_data;
465
466 if ((p_buf = (BT_HDR *)osi_malloc(RFCOMM_CMD_BUF_SIZE)) == NULL) {
467 return;
468 }
469
470 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
471 p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
472
473 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_RPN;
474
475 if (!p_pars) {
476 *p_data++ = RFCOMM_EA | (RFCOMM_MX_RPN_REQ_LEN << 1);
477
478 *p_data++ = RFCOMM_EA | RFCOMM_CR_MASK | (dlci << RFCOMM_SHIFT_DLCI);
479
480 p_buf->len = RFCOMM_MX_RPN_REQ_LEN + 2;
481 } else {
482 *p_data++ = RFCOMM_EA | (RFCOMM_MX_RPN_LEN << 1);
483
484 *p_data++ = RFCOMM_EA | RFCOMM_CR_MASK | (dlci << RFCOMM_SHIFT_DLCI);
485 *p_data++ = p_pars->baud_rate;
486 *p_data++ = (p_pars->byte_size << RFCOMM_RPN_BITS_SHIFT)
487 | (p_pars->stop_bits << RFCOMM_RPN_STOP_BITS_SHIFT)
488 | (p_pars->parity << RFCOMM_RPN_PARITY_SHIFT)
489 | (p_pars->parity_type << RFCOMM_RPN_PARITY_TYPE_SHIFT);
490 *p_data++ = p_pars->fc_type;
491 *p_data++ = p_pars->xon_char;
492 *p_data++ = p_pars->xoff_char;
493 *p_data++ = (mask & 0xFF);
494 *p_data++ = (mask >> 8);
495
496 /* Total length is sizeof RPN data + mx header 2 */
497 p_buf->len = RFCOMM_MX_RPN_LEN + 2;
498 }
499
500 rfc_send_buf_uih (p_mcb, RFCOMM_MX_DLCI, p_buf);
501 }
502
503
504 /*******************************************************************************
505 **
506 ** Function rfc_send_test
507 **
508 ** Description This function sends Test frame.
509 **
510 *******************************************************************************/
rfc_send_test(tRFC_MCB * p_mcb,BOOLEAN is_command,BT_HDR * p_buf)511 void rfc_send_test (tRFC_MCB *p_mcb, BOOLEAN is_command, BT_HDR *p_buf)
512 {
513 UINT8 *p_data;
514 UINT16 xx;
515 UINT8 *p_src, *p_dest;
516
517 BT_HDR *p_buf_new;
518 if ((p_buf_new = (BT_HDR *)osi_malloc(RFCOMM_CMD_BUF_SIZE)) == NULL) {
519 return;
520 }
521 memcpy(p_buf_new, p_buf, sizeof(BT_HDR) + p_buf->offset + p_buf->len);
522 osi_free(p_buf);
523 p_buf = p_buf_new;
524 /* Shift buffer to give space for header */
525 if (p_buf->offset < (L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET + 2)) {
526 p_src = (UINT8 *) (p_buf + 1) + p_buf->offset + p_buf->len - 1;
527 p_dest = (UINT8 *) (p_buf + 1) + L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET + 2 + p_buf->len - 1;
528
529 for (xx = 0; xx < p_buf->len; xx++) {
530 *p_dest-- = *p_src--;
531 }
532
533 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET + 2;
534 }
535
536 /* Adjust offset by number of bytes we are going to fill */
537 p_buf->offset -= 2;
538 p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
539
540 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_TEST;
541 *p_data++ = RFCOMM_EA | (p_buf->len << 1);
542
543 p_buf->len += 2;
544
545 rfc_send_buf_uih (p_mcb, RFCOMM_MX_DLCI, p_buf);
546 }
547
548 /*******************************************************************************
549 **
550 ** Function rfc_send_credit
551 **
552 ** Description This function sends a flow control credit in UIH frame.
553 **
554 *******************************************************************************/
rfc_send_credit(tRFC_MCB * p_mcb,UINT8 dlci,UINT8 credit)555 void rfc_send_credit(tRFC_MCB *p_mcb, UINT8 dlci, UINT8 credit)
556 {
557 BT_HDR *p_buf;
558 UINT8 *p_data;
559 UINT8 cr = RFCOMM_CR(p_mcb->is_initiator, TRUE);
560
561 if ((p_buf = (BT_HDR *)osi_malloc(RFCOMM_CMD_BUF_SIZE)) == NULL) {
562 return;
563 }
564
565 p_buf->offset = L2CAP_MIN_OFFSET;
566 p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
567
568 *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
569 *p_data++ = RFCOMM_UIH | RFCOMM_PF;
570 *p_data++ = RFCOMM_EA | 0;
571 *p_data++ = credit;
572 *p_data = RFCOMM_UIH_FCS ((UINT8 *)(p_buf + 1) + p_buf->offset, dlci);
573
574 p_buf->len = 5;
575
576 rfc_check_send_cmd(p_mcb, p_buf);
577 }
578
579
580 /*******************************************************************************
581 **
582 ** Function rfc_parse_data
583 **
584 ** Description This function processes data packet received from L2CAP
585 **
586 *******************************************************************************/
rfc_parse_data(tRFC_MCB * p_mcb,MX_FRAME * p_frame,BT_HDR * p_buf)587 UINT8 rfc_parse_data (tRFC_MCB *p_mcb, MX_FRAME *p_frame, BT_HDR *p_buf)
588 {
589 UINT8 ead, eal, fcs;
590 UINT8 *p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
591 UINT8 *p_start = p_data;
592 UINT16 len;
593
594 if (p_buf->len < RFCOMM_CTRL_FRAME_LEN) {
595 RFCOMM_TRACE_ERROR ("Bad Length1: %d", p_buf->len);
596 return (RFC_EVENT_BAD_FRAME);
597 }
598
599 RFCOMM_PARSE_CTRL_FIELD (ead, p_frame->cr, p_frame->dlci, p_data);
600 if ( !ead ) {
601 RFCOMM_TRACE_ERROR ("Bad Address(EA must be 1)");
602 return (RFC_EVENT_BAD_FRAME);
603 }
604 RFCOMM_PARSE_TYPE_FIELD (p_frame->type, p_frame->pf, p_data);
605 RFCOMM_PARSE_LEN_FIELD (eal, len, p_data);
606
607 p_buf->len -= (3 + !ead + !eal + 1); /* Additional 1 for FCS */
608 p_buf->offset += (3 + !ead + !eal);
609
610 /* handle credit if credit based flow control */
611 if ((p_mcb->flow == PORT_FC_CREDIT) && (p_frame->type == RFCOMM_UIH) &&
612 (p_frame->dlci != RFCOMM_MX_DLCI) && (p_frame->pf == 1)) {
613 p_frame->credit = *p_data++;
614 p_buf->len--;
615 p_buf->offset++;
616 } else {
617 p_frame->credit = 0;
618 }
619
620 if (p_buf->len != len) {
621 RFCOMM_TRACE_ERROR ("Bad Length2 %d %d", p_buf->len, len);
622 return (RFC_EVENT_BAD_FRAME);
623 }
624
625 fcs = *(p_data + len);
626
627 /* All control frames that we are sending are sent with P=1, expect */
628 /* reply with F=1 */
629 /* According to TS 07.10 spec ivalid frames are discarded without */
630 /* notification to the sender */
631 switch (p_frame->type) {
632 case RFCOMM_SABME:
633 if (RFCOMM_FRAME_IS_RSP(p_mcb->is_initiator, p_frame->cr)
634 || !p_frame->pf || len || !RFCOMM_VALID_DLCI (p_frame->dlci)
635 || !rfc_check_fcs (RFCOMM_CTRL_FRAME_LEN, p_start, fcs)) {
636 RFCOMM_TRACE_ERROR ("Bad SABME");
637 return (RFC_EVENT_BAD_FRAME);
638 } else {
639 return (RFC_EVENT_SABME);
640 }
641
642 case RFCOMM_UA:
643 if (RFCOMM_FRAME_IS_CMD(p_mcb->is_initiator, p_frame->cr)
644 || !p_frame->pf || len || !RFCOMM_VALID_DLCI (p_frame->dlci)
645 || !rfc_check_fcs (RFCOMM_CTRL_FRAME_LEN, p_start, fcs)) {
646 RFCOMM_TRACE_ERROR ("Bad UA");
647 return (RFC_EVENT_BAD_FRAME);
648 } else {
649 return (RFC_EVENT_UA);
650 }
651
652 case RFCOMM_DM:
653 if (RFCOMM_FRAME_IS_CMD(p_mcb->is_initiator, p_frame->cr)
654 || len || !RFCOMM_VALID_DLCI(p_frame->dlci)
655 || !rfc_check_fcs (RFCOMM_CTRL_FRAME_LEN, p_start, fcs)) {
656 RFCOMM_TRACE_ERROR ("Bad DM");
657 return (RFC_EVENT_BAD_FRAME);
658 } else {
659 return (RFC_EVENT_DM);
660 }
661
662 case RFCOMM_DISC:
663 if (RFCOMM_FRAME_IS_RSP(p_mcb->is_initiator, p_frame->cr)
664 || !p_frame->pf || len || !RFCOMM_VALID_DLCI(p_frame->dlci)
665 || !rfc_check_fcs (RFCOMM_CTRL_FRAME_LEN, p_start, fcs)) {
666 RFCOMM_TRACE_ERROR ("Bad DISC");
667 return (RFC_EVENT_BAD_FRAME);
668 } else {
669 return (RFC_EVENT_DISC);
670 }
671
672 case RFCOMM_UIH:
673 if (!RFCOMM_VALID_DLCI(p_frame->dlci)) {
674 RFCOMM_TRACE_ERROR ("Bad UIH - invalid DLCI");
675 return (RFC_EVENT_BAD_FRAME);
676 } else if (!rfc_check_fcs (2, p_start, fcs)) {
677 RFCOMM_TRACE_ERROR ("Bad UIH - FCS");
678 return (RFC_EVENT_BAD_FRAME);
679 } else if (RFCOMM_FRAME_IS_RSP(p_mcb->is_initiator, p_frame->cr)) {
680 /* we assume that this is ok to allow bad implementations to work */
681 RFCOMM_TRACE_ERROR ("Bad UIH - response");
682 return (RFC_EVENT_UIH);
683 } else {
684 return (RFC_EVENT_UIH);
685 }
686 }
687
688 return (RFC_EVENT_BAD_FRAME);
689 }
690
691
692 /*******************************************************************************
693 **
694 ** Function rfc_process_mx_message
695 **
696 ** Description This function processes UIH frames received on the
697 ** multiplexer control channel.
698 **
699 *******************************************************************************/
rfc_process_mx_message(tRFC_MCB * p_mcb,BT_HDR * p_buf)700 void rfc_process_mx_message (tRFC_MCB *p_mcb, BT_HDR *p_buf)
701 {
702 UINT8 *p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
703 MX_FRAME *p_rx_frame = &rfc_cb.rfc.rx_frame;
704 UINT16 length = p_buf->len;
705 UINT8 ea, cr, mx_len;
706 BOOLEAN is_command;
707
708 p_rx_frame->ea = *p_data & RFCOMM_EA;
709 p_rx_frame->cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
710 p_rx_frame->type = *p_data++ & ~(RFCOMM_CR_MASK | RFCOMM_EA_MASK);
711
712 if (!p_rx_frame->ea || !length) {
713 RFCOMM_TRACE_ERROR ("Illegal MX Frame ea:%d len:%d", p_rx_frame->ea, length);
714 osi_free (p_buf);
715 return;
716 }
717
718 length--;
719
720 is_command = p_rx_frame->cr;
721
722 ea = *p_data & RFCOMM_EA;
723
724 mx_len = *p_data++ >> RFCOMM_SHIFT_LENGTH1;
725 length--;
726
727 if (!ea) {
728 mx_len += *p_data++ << RFCOMM_SHIFT_LENGTH2;
729 length --;
730 }
731
732 if (mx_len != length) {
733 RFCOMM_TRACE_ERROR ("Bad MX frame");
734 osi_free (p_buf);
735 return;
736 }
737
738 switch (p_rx_frame->type) {
739 case RFCOMM_MX_PN:
740 if (length != RFCOMM_MX_PN_LEN) {
741 break;
742 }
743
744 p_rx_frame->dlci = *p_data++ & RFCOMM_PN_DLCI_MASK;
745 p_rx_frame->u.pn.frame_type = *p_data & RFCOMM_PN_FRAME_TYPE_MASK;
746 p_rx_frame->u.pn.conv_layer = *p_data++ & RFCOMM_PN_CONV_LAYER_MASK;
747 p_rx_frame->u.pn.priority = *p_data++ & RFCOMM_PN_PRIORITY_MASK;
748 p_rx_frame->u.pn.t1 = *p_data++;
749 p_rx_frame->u.pn.mtu = *p_data + (*(p_data + 1) << 8);
750 p_data += 2;
751 p_rx_frame->u.pn.n2 = *p_data++;
752 p_rx_frame->u.pn.k = *p_data++ & RFCOMM_PN_K_MASK;
753
754 if (!p_rx_frame->dlci
755 || !RFCOMM_VALID_DLCI (p_rx_frame->dlci)
756 || (p_rx_frame->u.pn.mtu < RFCOMM_MIN_MTU)
757 || (p_rx_frame->u.pn.mtu > RFCOMM_MAX_MTU)) {
758 RFCOMM_TRACE_ERROR ("Bad PN frame");
759 break;
760 }
761
762 osi_free (p_buf);
763
764 rfc_process_pn (p_mcb, is_command, p_rx_frame);
765 return;
766
767 case RFCOMM_MX_TEST:
768 if (!length) {
769 break;
770 }
771
772 p_rx_frame->u.test.p_data = p_data;
773 p_rx_frame->u.test.data_len = length;
774
775 p_buf->offset += 2;
776 p_buf->len -= 2;
777
778 if (is_command) {
779 rfc_send_test (p_mcb, FALSE, p_buf);
780 } else {
781 rfc_process_test_rsp (p_mcb, p_buf);
782 }
783 return;
784
785 case RFCOMM_MX_FCON:
786 if (length != RFCOMM_MX_FCON_LEN) {
787 break;
788 }
789
790 osi_free (p_buf);
791
792 rfc_process_fcon (p_mcb, is_command);
793 return;
794
795 case RFCOMM_MX_FCOFF:
796 if (length != RFCOMM_MX_FCOFF_LEN) {
797 break;
798 }
799
800 osi_free (p_buf);
801
802 rfc_process_fcoff (p_mcb, is_command);
803 return;
804
805 case RFCOMM_MX_MSC:
806
807 ea = *p_data & RFCOMM_EA;
808 cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
809 p_rx_frame->dlci = *p_data++ >> RFCOMM_SHIFT_DLCI;
810
811 if (!ea || !cr || !p_rx_frame->dlci
812 || !RFCOMM_VALID_DLCI (p_rx_frame->dlci)) {
813 RFCOMM_TRACE_ERROR ("Bad MSC frame");
814 break;
815 }
816
817 p_rx_frame->u.msc.signals = *p_data++;
818
819 if (mx_len == RFCOMM_MX_MSC_LEN_WITH_BREAK) {
820 p_rx_frame->u.msc.break_present = *p_data & RFCOMM_MSC_BREAK_PRESENT_MASK;
821 p_rx_frame->u.msc.break_duration = (*p_data & RFCOMM_MSC_BREAK_MASK) >> RFCOMM_MSC_SHIFT_BREAK;
822 } else {
823 p_rx_frame->u.msc.break_present = FALSE;
824 p_rx_frame->u.msc.break_duration = 0;
825 }
826 osi_free (p_buf);
827
828 rfc_process_msc (p_mcb, is_command, p_rx_frame);
829 return;
830
831 case RFCOMM_MX_NSC:
832 if ((length != RFCOMM_MX_NSC_LEN) || !is_command) {
833 break;
834 }
835
836 p_rx_frame->u.nsc.ea = *p_data & RFCOMM_EA;
837 p_rx_frame->u.nsc.cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
838 p_rx_frame->u.nsc.type = *p_data++ >> RFCOMM_SHIFT_DLCI;
839
840 osi_free (p_buf);
841
842 rfc_process_nsc (p_mcb, p_rx_frame);
843 return;
844
845 case RFCOMM_MX_RPN:
846 if ((length != RFCOMM_MX_RPN_REQ_LEN) && (length != RFCOMM_MX_RPN_LEN)) {
847 break;
848 }
849
850 ea = *p_data & RFCOMM_EA;
851 cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
852 p_rx_frame->dlci = *p_data++ >> RFCOMM_SHIFT_DLCI;
853
854 if (!ea || !cr || !p_rx_frame->dlci
855 || !RFCOMM_VALID_DLCI (p_rx_frame->dlci)) {
856 RFCOMM_TRACE_ERROR ("Bad RPN frame");
857 break;
858 }
859
860 p_rx_frame->u.rpn.is_request = (length == RFCOMM_MX_RPN_REQ_LEN);
861
862 if (!p_rx_frame->u.rpn.is_request) {
863 p_rx_frame->u.rpn.baud_rate = *p_data++;
864 p_rx_frame->u.rpn.byte_size = (*p_data >> RFCOMM_RPN_BITS_SHIFT) & RFCOMM_RPN_BITS_MASK;
865 p_rx_frame->u.rpn.stop_bits = (*p_data >> RFCOMM_RPN_STOP_BITS_SHIFT) & RFCOMM_RPN_STOP_BITS_MASK;
866 p_rx_frame->u.rpn.parity = (*p_data >> RFCOMM_RPN_PARITY_SHIFT) & RFCOMM_RPN_PARITY_MASK;
867 p_rx_frame->u.rpn.parity_type = (*p_data++ >> RFCOMM_RPN_PARITY_TYPE_SHIFT) & RFCOMM_RPN_PARITY_TYPE_MASK;
868
869 p_rx_frame->u.rpn.fc_type = *p_data++ & RFCOMM_FC_MASK;
870 p_rx_frame->u.rpn.xon_char = *p_data++;
871 p_rx_frame->u.rpn.xoff_char = *p_data++;
872 p_rx_frame->u.rpn.param_mask = (*p_data + (*(p_data + 1) << 8)) & RFCOMM_RPN_PM_MASK;
873 }
874 osi_free (p_buf);
875
876 rfc_process_rpn (p_mcb, is_command, p_rx_frame->u.rpn.is_request, p_rx_frame);
877 return;
878
879 case RFCOMM_MX_RLS:
880 if (length != RFCOMM_MX_RLS_LEN) {
881 break;
882 }
883
884 ea = *p_data & RFCOMM_EA;
885 cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
886
887 p_rx_frame->dlci = *p_data++ >> RFCOMM_SHIFT_DLCI;
888 p_rx_frame->u.rls.line_status = (*p_data & ~0x01);
889
890 if (!ea || !cr || !p_rx_frame->dlci
891 || !RFCOMM_VALID_DLCI (p_rx_frame->dlci)) {
892 RFCOMM_TRACE_ERROR ("Bad RPN frame");
893 break;
894 }
895
896 osi_free (p_buf);
897
898 rfc_process_rls (p_mcb, is_command, p_rx_frame);
899 return;
900 }
901
902 osi_free (p_buf);
903
904 if (is_command) {
905 rfc_send_nsc (p_mcb);
906 }
907 }
908
909 #endif ///(defined RFCOMM_INCLUDED && RFCOMM_INCLUDED == TRUE)
910