1 /******************************************************************************
3 * Copyright (C) 1999-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * This file contains functions to send TS 07.10 frames
23 ******************************************************************************/
26 #include "bt_common.h"
27 #include "bt_target.h"
34 /*******************************************************************************
36 * Function rfc_send_sabme
38 * Description This function sends SABME frame.
40 ******************************************************************************/
41 void rfc_send_sabme(tRFC_MCB* p_mcb, uint8_t dlci) {
43 uint8_t cr = RFCOMM_CR(p_mcb->is_initiator, true);
44 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
46 p_buf->offset = L2CAP_MIN_OFFSET;
47 p_data = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
49 /* SABME frame, command, PF = 1, dlci */
50 *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
51 *p_data++ = RFCOMM_SABME | RFCOMM_PF;
52 *p_data++ = RFCOMM_EA | 0;
55 RFCOMM_SABME_FCS((uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET, cr, dlci);
59 rfc_check_send_cmd(p_mcb, p_buf);
62 /*******************************************************************************
64 * Function rfc_send_ua
66 * Description This function sends UA frame.
68 ******************************************************************************/
69 void rfc_send_ua(tRFC_MCB* p_mcb, uint8_t dlci) {
71 uint8_t cr = RFCOMM_CR(p_mcb->is_initiator, false);
72 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
74 p_buf->offset = L2CAP_MIN_OFFSET;
75 p_data = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
77 /* ua frame, response, PF = 1, dlci */
78 *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
79 *p_data++ = RFCOMM_UA | RFCOMM_PF;
80 *p_data++ = RFCOMM_EA | 0;
82 *p_data = RFCOMM_UA_FCS((uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET, cr, dlci);
86 rfc_check_send_cmd(p_mcb, p_buf);
89 /*******************************************************************************
91 * Function rfc_send_dm
93 * Description This function sends DM frame.
95 ******************************************************************************/
96 void rfc_send_dm(tRFC_MCB* p_mcb, uint8_t dlci, bool pf) {
98 uint8_t cr = RFCOMM_CR(p_mcb->is_initiator, false);
99 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
101 p_buf->offset = L2CAP_MIN_OFFSET;
102 p_data = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
104 /* DM frame, response, PF = 1, dlci */
105 *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
106 *p_data++ = RFCOMM_DM | ((pf) ? RFCOMM_PF : 0);
107 *p_data++ = RFCOMM_EA | 0;
109 *p_data = RFCOMM_DM_FCS((uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET, cr, dlci);
113 rfc_check_send_cmd(p_mcb, p_buf);
116 /*******************************************************************************
118 * Function rfc_send_disc
120 * Description This function sends DISC frame.
122 ******************************************************************************/
123 void rfc_send_disc(tRFC_MCB* p_mcb, uint8_t dlci) {
125 uint8_t cr = RFCOMM_CR(p_mcb->is_initiator, true);
126 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
128 p_buf->offset = L2CAP_MIN_OFFSET;
129 p_data = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
131 /* DISC frame, command, PF = 1, dlci */
132 *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
133 *p_data++ = RFCOMM_DISC | RFCOMM_PF;
134 *p_data++ = RFCOMM_EA | 0;
136 *p_data = RFCOMM_DISC_FCS((uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET, cr, dlci);
140 rfc_check_send_cmd(p_mcb, p_buf);
143 /*******************************************************************************
145 * Function rfc_send_buf_uih
147 * Description This function sends UIH frame.
149 ******************************************************************************/
150 void rfc_send_buf_uih(tRFC_MCB* p_mcb, uint8_t dlci, BT_HDR* p_buf) {
152 uint8_t cr = RFCOMM_CR(p_mcb->is_initiator, true);
155 p_buf->offset -= RFCOMM_CTRL_FRAME_LEN;
156 if (p_buf->len > 127) p_buf->offset--;
159 credits = (uint8_t)p_buf->layer_specific;
163 if (credits) p_buf->offset--;
165 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
167 /* UIH frame, command, PF = 0, dlci */
168 *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
169 *p_data++ = RFCOMM_UIH | ((credits) ? RFCOMM_PF : 0);
170 if (p_buf->len <= 127) {
171 *p_data++ = RFCOMM_EA | (p_buf->len << 1);
174 *p_data++ = (p_buf->len & 0x7f) << 1;
175 *p_data++ = p_buf->len >> RFCOMM_SHIFT_LENGTH2;
184 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset + p_buf->len++;
186 *p_data = RFCOMM_UIH_FCS((uint8_t*)(p_buf + 1) + p_buf->offset, dlci);
188 if (dlci == RFCOMM_MX_DLCI) {
189 rfc_check_send_cmd(p_mcb, p_buf);
191 L2CA_DataWrite(p_mcb->lcid, p_buf);
195 /*******************************************************************************
197 * Function rfc_send_pn
199 * Description This function sends DLC Parameters Negotiation Frame.
201 ******************************************************************************/
202 void rfc_send_pn(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command, uint16_t mtu,
203 uint8_t cl, uint8_t k) {
205 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
207 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
208 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
210 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_PN;
211 *p_data++ = RFCOMM_EA | (RFCOMM_MX_PN_LEN << 1);
214 *p_data++ = RFCOMM_PN_FRAM_TYPE_UIH | cl;
216 /* It appeared that we need to reply with the same priority bits as we
218 ** We will use the fact that we reply in the same context so rx_frame can
222 *p_data++ = RFCOMM_PN_PRIORITY_0;
224 *p_data++ = rfc_cb.rfc.rx_frame.u.pn.priority;
226 *p_data++ = RFCOMM_T1_DSEC;
227 *p_data++ = mtu & 0xFF;
228 *p_data++ = mtu >> 8;
229 *p_data++ = RFCOMM_N2;
232 /* Total length is sizeof PN data + mx header 2 */
233 p_buf->len = RFCOMM_MX_PN_LEN + 2;
235 rfc_send_buf_uih(p_mcb, RFCOMM_MX_DLCI, p_buf);
238 /*******************************************************************************
240 * Function rfc_send_fcon
242 * Description This function sends Flow Control On Command.
244 ******************************************************************************/
245 void rfc_send_fcon(tRFC_MCB* p_mcb, bool is_command) {
247 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
249 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
250 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
252 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_FCON;
253 *p_data++ = RFCOMM_EA | (RFCOMM_MX_FCON_LEN << 1);
255 /* Total length is sizeof FCON data + mx header 2 */
256 p_buf->len = RFCOMM_MX_FCON_LEN + 2;
258 rfc_send_buf_uih(p_mcb, RFCOMM_MX_DLCI, p_buf);
261 /*******************************************************************************
263 * Function rfc_send_fcoff
265 * Description This function sends Flow Control Off Command.
267 ******************************************************************************/
268 void rfc_send_fcoff(tRFC_MCB* p_mcb, bool is_command) {
270 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
272 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
273 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
275 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_FCOFF;
276 *p_data++ = RFCOMM_EA | (RFCOMM_MX_FCOFF_LEN << 1);
278 /* Total length is sizeof FCOFF data + mx header 2 */
279 p_buf->len = RFCOMM_MX_FCOFF_LEN + 2;
281 rfc_send_buf_uih(p_mcb, RFCOMM_MX_DLCI, p_buf);
284 /*******************************************************************************
286 * Function rfc_send_msc
288 * Description This function sends Modem Status Command Frame.
290 ******************************************************************************/
291 void rfc_send_msc(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command,
292 tPORT_CTRL* p_pars) {
295 uint8_t break_duration;
297 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
299 signals = p_pars->modem_signal;
300 break_duration = p_pars->break_signal;
302 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
303 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
306 len = RFCOMM_MX_MSC_LEN_WITH_BREAK;
308 len = RFCOMM_MX_MSC_LEN_NO_BREAK;
310 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_MSC;
311 *p_data++ = RFCOMM_EA | (len << 1);
313 *p_data++ = RFCOMM_EA | RFCOMM_CR_MASK | (dlci << RFCOMM_SHIFT_DLCI);
314 *p_data++ = RFCOMM_EA | ((p_pars->fc) ? RFCOMM_MSC_FC : 0) |
315 ((signals & MODEM_SIGNAL_DTRDSR) ? RFCOMM_MSC_RTC : 0) |
316 ((signals & MODEM_SIGNAL_RTSCTS) ? RFCOMM_MSC_RTR : 0) |
317 ((signals & MODEM_SIGNAL_RI) ? RFCOMM_MSC_IC : 0) |
318 ((signals & MODEM_SIGNAL_DCD) ? RFCOMM_MSC_DV : 0);
320 if (break_duration) {
321 *p_data++ = RFCOMM_EA | RFCOMM_MSC_BREAK_PRESENT_MASK |
322 (break_duration << RFCOMM_MSC_SHIFT_BREAK);
325 /* Total length is sizeof MSC data + mx header 2 */
326 p_buf->len = len + 2;
328 rfc_send_buf_uih(p_mcb, RFCOMM_MX_DLCI, p_buf);
331 /*******************************************************************************
333 * Function rfc_send_rls
335 * Description This function sends Remote Line Status Command Frame.
337 ******************************************************************************/
338 void rfc_send_rls(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command,
341 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
343 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
344 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
346 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_RLS;
347 *p_data++ = RFCOMM_EA | (RFCOMM_MX_RLS_LEN << 1);
349 *p_data++ = RFCOMM_EA | RFCOMM_CR_MASK | (dlci << RFCOMM_SHIFT_DLCI);
350 *p_data++ = RFCOMM_RLS_ERROR | status;
352 /* Total length is sizeof RLS data + mx header 2 */
353 p_buf->len = RFCOMM_MX_RLS_LEN + 2;
355 rfc_send_buf_uih(p_mcb, RFCOMM_MX_DLCI, p_buf);
358 /*******************************************************************************
360 * Function rfc_send_nsc
362 * Description This function sends Non Supported Command Response.
364 ******************************************************************************/
365 void rfc_send_nsc(tRFC_MCB* p_mcb) {
367 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
369 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
370 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
372 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(false) | RFCOMM_MX_NSC;
373 *p_data++ = RFCOMM_EA | (RFCOMM_MX_NSC_LEN << 1);
375 *p_data++ = rfc_cb.rfc.rx_frame.ea |
376 (rfc_cb.rfc.rx_frame.cr << RFCOMM_SHIFT_CR) |
377 rfc_cb.rfc.rx_frame.type;
379 /* Total length is sizeof NSC data + mx header 2 */
380 p_buf->len = RFCOMM_MX_NSC_LEN + 2;
382 rfc_send_buf_uih(p_mcb, RFCOMM_MX_DLCI, p_buf);
385 /*******************************************************************************
387 * Function rfc_send_rpn
389 * Description This function sends Remote Port Negotiation Command
391 ******************************************************************************/
392 void rfc_send_rpn(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command,
393 tPORT_STATE* p_pars, uint16_t mask) {
395 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
397 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
398 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
400 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_RPN;
403 *p_data++ = RFCOMM_EA | (RFCOMM_MX_RPN_REQ_LEN << 1);
405 *p_data++ = RFCOMM_EA | RFCOMM_CR_MASK | (dlci << RFCOMM_SHIFT_DLCI);
407 p_buf->len = RFCOMM_MX_RPN_REQ_LEN + 2;
409 *p_data++ = RFCOMM_EA | (RFCOMM_MX_RPN_LEN << 1);
411 *p_data++ = RFCOMM_EA | RFCOMM_CR_MASK | (dlci << RFCOMM_SHIFT_DLCI);
412 *p_data++ = p_pars->baud_rate;
413 *p_data++ = (p_pars->byte_size << RFCOMM_RPN_BITS_SHIFT) |
414 (p_pars->stop_bits << RFCOMM_RPN_STOP_BITS_SHIFT) |
415 (p_pars->parity << RFCOMM_RPN_PARITY_SHIFT) |
416 (p_pars->parity_type << RFCOMM_RPN_PARITY_TYPE_SHIFT);
417 *p_data++ = p_pars->fc_type;
418 *p_data++ = p_pars->xon_char;
419 *p_data++ = p_pars->xoff_char;
420 *p_data++ = (mask & 0xFF);
421 *p_data++ = (mask >> 8);
423 /* Total length is sizeof RPN data + mx header 2 */
424 p_buf->len = RFCOMM_MX_RPN_LEN + 2;
427 rfc_send_buf_uih(p_mcb, RFCOMM_MX_DLCI, p_buf);
430 /*******************************************************************************
432 * Function rfc_send_test
434 * Description This function sends Test frame.
436 ******************************************************************************/
437 void rfc_send_test(tRFC_MCB* p_mcb, bool is_command, BT_HDR* p_buf) {
438 /* Shift buffer to give space for header */
439 if (p_buf->offset < (L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET + 2)) {
440 uint8_t* p_src = (uint8_t*)(p_buf + 1) + p_buf->offset + p_buf->len - 1;
442 (BT_HDR*)osi_malloc(p_buf->len + (L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET +
443 2 + sizeof(BT_HDR) + 1));
445 p_new_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET + 2;
446 p_new_buf->len = p_buf->len;
449 (uint8_t*)(p_new_buf + 1) + p_new_buf->offset + p_new_buf->len - 1;
451 for (uint16_t xx = 0; xx < p_buf->len; xx++) *p_dest-- = *p_src--;
457 /* Adjust offset by number of bytes we are going to fill */
459 uint8_t* p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
461 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_TEST;
462 *p_data++ = RFCOMM_EA | (p_buf->len << 1);
466 rfc_send_buf_uih(p_mcb, RFCOMM_MX_DLCI, p_buf);
469 /*******************************************************************************
471 * Function rfc_send_credit
473 * Description This function sends a flow control credit in UIH frame.
475 ******************************************************************************/
476 void rfc_send_credit(tRFC_MCB* p_mcb, uint8_t dlci, uint8_t credit) {
478 uint8_t cr = RFCOMM_CR(p_mcb->is_initiator, true);
479 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
481 p_buf->offset = L2CAP_MIN_OFFSET;
482 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
484 *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
485 *p_data++ = RFCOMM_UIH | RFCOMM_PF;
486 *p_data++ = RFCOMM_EA | 0;
488 *p_data = RFCOMM_UIH_FCS((uint8_t*)(p_buf + 1) + p_buf->offset, dlci);
492 rfc_check_send_cmd(p_mcb, p_buf);
495 /*******************************************************************************
497 * Function rfc_parse_data
499 * Description This function processes data packet received from L2CAP
501 ******************************************************************************/
502 uint8_t rfc_parse_data(tRFC_MCB* p_mcb, MX_FRAME* p_frame, BT_HDR* p_buf) {
503 uint8_t ead, eal, fcs;
504 uint8_t* p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
505 uint8_t* p_start = p_data;
508 if (p_buf->len < RFCOMM_CTRL_FRAME_LEN) {
509 RFCOMM_TRACE_ERROR("Bad Length1: %d", p_buf->len);
510 return (RFC_EVENT_BAD_FRAME);
513 RFCOMM_PARSE_CTRL_FIELD(ead, p_frame->cr, p_frame->dlci, p_data);
515 RFCOMM_TRACE_ERROR("Bad Address(EA must be 1)");
516 return (RFC_EVENT_BAD_FRAME);
518 RFCOMM_PARSE_TYPE_FIELD(p_frame->type, p_frame->pf, p_data);
519 RFCOMM_PARSE_LEN_FIELD(eal, len, p_data);
521 p_buf->len -= (3 + !ead + !eal + 1); /* Additional 1 for FCS */
522 p_buf->offset += (3 + !ead + !eal);
524 /* handle credit if credit based flow control */
525 if ((p_mcb->flow == PORT_FC_CREDIT) && (p_frame->type == RFCOMM_UIH) &&
526 (p_frame->dlci != RFCOMM_MX_DLCI) && (p_frame->pf == 1)) {
527 p_frame->credit = *p_data++;
533 if (p_buf->len != len) {
534 RFCOMM_TRACE_ERROR("Bad Length2 %d %d", p_buf->len, len);
535 return (RFC_EVENT_BAD_FRAME);
538 fcs = *(p_data + len);
540 /* All control frames that we are sending are sent with P=1, expect */
542 /* According to TS 07.10 spec ivalid frames are discarded without */
543 /* notification to the sender */
544 switch (p_frame->type) {
546 if (RFCOMM_FRAME_IS_RSP(p_mcb->is_initiator, p_frame->cr) ||
547 !p_frame->pf || len || !RFCOMM_VALID_DLCI(p_frame->dlci) ||
548 !rfc_check_fcs(RFCOMM_CTRL_FRAME_LEN, p_start, fcs)) {
549 RFCOMM_TRACE_ERROR("Bad SABME");
550 return (RFC_EVENT_BAD_FRAME);
552 return (RFC_EVENT_SABME);
555 if (RFCOMM_FRAME_IS_CMD(p_mcb->is_initiator, p_frame->cr) ||
556 !p_frame->pf || len || !RFCOMM_VALID_DLCI(p_frame->dlci) ||
557 !rfc_check_fcs(RFCOMM_CTRL_FRAME_LEN, p_start, fcs)) {
558 RFCOMM_TRACE_ERROR("Bad UA");
559 return (RFC_EVENT_BAD_FRAME);
561 return (RFC_EVENT_UA);
564 if (RFCOMM_FRAME_IS_CMD(p_mcb->is_initiator, p_frame->cr) || len ||
565 !RFCOMM_VALID_DLCI(p_frame->dlci) ||
566 !rfc_check_fcs(RFCOMM_CTRL_FRAME_LEN, p_start, fcs)) {
567 RFCOMM_TRACE_ERROR("Bad DM");
568 return (RFC_EVENT_BAD_FRAME);
570 return (RFC_EVENT_DM);
573 if (RFCOMM_FRAME_IS_RSP(p_mcb->is_initiator, p_frame->cr) ||
574 !p_frame->pf || len || !RFCOMM_VALID_DLCI(p_frame->dlci) ||
575 !rfc_check_fcs(RFCOMM_CTRL_FRAME_LEN, p_start, fcs)) {
576 RFCOMM_TRACE_ERROR("Bad DISC");
577 return (RFC_EVENT_BAD_FRAME);
579 return (RFC_EVENT_DISC);
582 if (!RFCOMM_VALID_DLCI(p_frame->dlci)) {
583 RFCOMM_TRACE_ERROR("Bad UIH - invalid DLCI");
584 return (RFC_EVENT_BAD_FRAME);
585 } else if (!rfc_check_fcs(2, p_start, fcs)) {
586 RFCOMM_TRACE_ERROR("Bad UIH - FCS");
587 return (RFC_EVENT_BAD_FRAME);
588 } else if (RFCOMM_FRAME_IS_RSP(p_mcb->is_initiator, p_frame->cr)) {
589 /* we assume that this is ok to allow bad implementations to work */
590 RFCOMM_TRACE_ERROR("Bad UIH - response");
591 return (RFC_EVENT_UIH);
593 return (RFC_EVENT_UIH);
596 return (RFC_EVENT_BAD_FRAME);
599 /*******************************************************************************
601 * Function rfc_process_mx_message
603 * Description This function processes UIH frames received on the
604 * multiplexer control channel.
606 ******************************************************************************/
607 void rfc_process_mx_message(tRFC_MCB* p_mcb, BT_HDR* p_buf) {
608 uint8_t* p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
609 MX_FRAME* p_rx_frame = &rfc_cb.rfc.rx_frame;
610 uint16_t length = p_buf->len;
611 uint8_t ea, cr, mx_len;
614 p_rx_frame->ea = *p_data & RFCOMM_EA;
615 p_rx_frame->cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
616 p_rx_frame->type = *p_data++ & ~(RFCOMM_CR_MASK | RFCOMM_EA_MASK);
618 if (!p_rx_frame->ea || !length) {
619 RFCOMM_TRACE_ERROR("Illegal MX Frame ea:%d len:%d", p_rx_frame->ea, length);
626 is_command = p_rx_frame->cr;
628 ea = *p_data & RFCOMM_EA;
630 mx_len = *p_data++ >> RFCOMM_SHIFT_LENGTH1;
634 mx_len += *p_data++ << RFCOMM_SHIFT_LENGTH2;
638 if (mx_len != length) {
639 RFCOMM_TRACE_ERROR("Bad MX frame");
644 switch (p_rx_frame->type) {
646 if (length != RFCOMM_MX_PN_LEN) break;
648 p_rx_frame->dlci = *p_data++ & RFCOMM_PN_DLCI_MASK;
649 p_rx_frame->u.pn.frame_type = *p_data & RFCOMM_PN_FRAME_TYPE_MASK;
650 p_rx_frame->u.pn.conv_layer = *p_data++ & RFCOMM_PN_CONV_LAYER_MASK;
651 p_rx_frame->u.pn.priority = *p_data++ & RFCOMM_PN_PRIORITY_MASK;
652 p_rx_frame->u.pn.t1 = *p_data++;
653 p_rx_frame->u.pn.mtu = *p_data + (*(p_data + 1) << 8);
655 p_rx_frame->u.pn.n2 = *p_data++;
656 p_rx_frame->u.pn.k = *p_data++ & RFCOMM_PN_K_MASK;
658 if (!p_rx_frame->dlci || !RFCOMM_VALID_DLCI(p_rx_frame->dlci) ||
659 (p_rx_frame->u.pn.mtu < RFCOMM_MIN_MTU) ||
660 (p_rx_frame->u.pn.mtu > RFCOMM_MAX_MTU)) {
661 RFCOMM_TRACE_ERROR("Bad PN frame");
667 rfc_process_pn(p_mcb, is_command, p_rx_frame);
673 p_rx_frame->u.test.p_data = p_data;
674 p_rx_frame->u.test.data_len = length;
680 rfc_send_test(p_mcb, false, p_buf);
682 rfc_process_test_rsp(p_mcb, p_buf);
686 if (length != RFCOMM_MX_FCON_LEN) break;
690 rfc_process_fcon(p_mcb, is_command);
693 case RFCOMM_MX_FCOFF:
694 if (length != RFCOMM_MX_FCOFF_LEN) break;
698 rfc_process_fcoff(p_mcb, is_command);
703 ea = *p_data & RFCOMM_EA;
704 cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
705 p_rx_frame->dlci = *p_data++ >> RFCOMM_SHIFT_DLCI;
707 if (!ea || !cr || !p_rx_frame->dlci ||
708 !RFCOMM_VALID_DLCI(p_rx_frame->dlci)) {
709 RFCOMM_TRACE_ERROR("Bad MSC frame");
713 p_rx_frame->u.msc.signals = *p_data++;
715 if (mx_len == RFCOMM_MX_MSC_LEN_WITH_BREAK) {
716 p_rx_frame->u.msc.break_present =
717 *p_data & RFCOMM_MSC_BREAK_PRESENT_MASK;
718 p_rx_frame->u.msc.break_duration =
719 (*p_data & RFCOMM_MSC_BREAK_MASK) >> RFCOMM_MSC_SHIFT_BREAK;
721 p_rx_frame->u.msc.break_present = false;
722 p_rx_frame->u.msc.break_duration = 0;
726 rfc_process_msc(p_mcb, is_command, p_rx_frame);
730 if ((length != RFCOMM_MX_NSC_LEN) || !is_command) break;
732 p_rx_frame->u.nsc.ea = *p_data & RFCOMM_EA;
733 p_rx_frame->u.nsc.cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
734 p_rx_frame->u.nsc.type = *p_data++ >> RFCOMM_SHIFT_DLCI;
738 rfc_process_nsc(p_mcb, p_rx_frame);
742 if ((length != RFCOMM_MX_RPN_REQ_LEN) && (length != RFCOMM_MX_RPN_LEN))
745 ea = *p_data & RFCOMM_EA;
746 cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
747 p_rx_frame->dlci = *p_data++ >> RFCOMM_SHIFT_DLCI;
749 if (!ea || !cr || !p_rx_frame->dlci ||
750 !RFCOMM_VALID_DLCI(p_rx_frame->dlci)) {
751 RFCOMM_TRACE_ERROR("Bad RPN frame");
755 p_rx_frame->u.rpn.is_request = (length == RFCOMM_MX_RPN_REQ_LEN);
757 if (!p_rx_frame->u.rpn.is_request) {
758 p_rx_frame->u.rpn.baud_rate = *p_data++;
759 p_rx_frame->u.rpn.byte_size =
760 (*p_data >> RFCOMM_RPN_BITS_SHIFT) & RFCOMM_RPN_BITS_MASK;
761 p_rx_frame->u.rpn.stop_bits =
762 (*p_data >> RFCOMM_RPN_STOP_BITS_SHIFT) & RFCOMM_RPN_STOP_BITS_MASK;
763 p_rx_frame->u.rpn.parity =
764 (*p_data >> RFCOMM_RPN_PARITY_SHIFT) & RFCOMM_RPN_PARITY_MASK;
765 p_rx_frame->u.rpn.parity_type =
766 (*p_data++ >> RFCOMM_RPN_PARITY_TYPE_SHIFT) &
767 RFCOMM_RPN_PARITY_TYPE_MASK;
769 p_rx_frame->u.rpn.fc_type = *p_data++ & RFCOMM_FC_MASK;
770 p_rx_frame->u.rpn.xon_char = *p_data++;
771 p_rx_frame->u.rpn.xoff_char = *p_data++;
772 p_rx_frame->u.rpn.param_mask =
773 (*p_data + (*(p_data + 1) << 8)) & RFCOMM_RPN_PM_MASK;
777 rfc_process_rpn(p_mcb, is_command, p_rx_frame->u.rpn.is_request,
782 if (length != RFCOMM_MX_RLS_LEN) break;
784 ea = *p_data & RFCOMM_EA;
785 cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
787 p_rx_frame->dlci = *p_data++ >> RFCOMM_SHIFT_DLCI;
788 p_rx_frame->u.rls.line_status = (*p_data & ~0x01);
790 if (!ea || !cr || !p_rx_frame->dlci ||
791 !RFCOMM_VALID_DLCI(p_rx_frame->dlci)) {
792 RFCOMM_TRACE_ERROR("Bad RPN frame");
798 rfc_process_rls(p_mcb, is_command, p_rx_frame);
804 if (is_command) rfc_send_nsc(p_mcb);