1 /******************************************************************************
3 * Copyright 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"
35 /*******************************************************************************
37 * Function rfc_send_sabme
39 * Description This function sends SABME frame.
41 ******************************************************************************/
42 void rfc_send_sabme(tRFC_MCB* p_mcb, uint8_t dlci) {
44 uint8_t cr = RFCOMM_CR(p_mcb->is_initiator, true);
45 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
47 p_buf->offset = L2CAP_MIN_OFFSET;
48 p_data = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
50 /* SABME frame, command, PF = 1, dlci */
51 *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
52 *p_data++ = RFCOMM_SABME | RFCOMM_PF;
53 *p_data++ = RFCOMM_EA | 0;
56 RFCOMM_SABME_FCS((uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET, cr, dlci);
60 rfc_check_send_cmd(p_mcb, p_buf);
63 /*******************************************************************************
65 * Function rfc_send_ua
67 * Description This function sends UA frame.
69 ******************************************************************************/
70 void rfc_send_ua(tRFC_MCB* p_mcb, uint8_t dlci) {
72 uint8_t cr = RFCOMM_CR(p_mcb->is_initiator, false);
73 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
75 p_buf->offset = L2CAP_MIN_OFFSET;
76 p_data = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
78 /* ua frame, response, PF = 1, dlci */
79 *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
80 *p_data++ = RFCOMM_UA | RFCOMM_PF;
81 *p_data++ = RFCOMM_EA | 0;
83 *p_data = RFCOMM_UA_FCS((uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET, cr, dlci);
87 rfc_check_send_cmd(p_mcb, p_buf);
90 /*******************************************************************************
92 * Function rfc_send_dm
94 * Description This function sends DM frame.
96 ******************************************************************************/
97 void rfc_send_dm(tRFC_MCB* p_mcb, uint8_t dlci, bool pf) {
99 uint8_t cr = RFCOMM_CR(p_mcb->is_initiator, false);
100 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
102 p_buf->offset = L2CAP_MIN_OFFSET;
103 p_data = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
105 /* DM frame, response, PF = 1, dlci */
106 *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
107 *p_data++ = RFCOMM_DM | ((pf) ? RFCOMM_PF : 0);
108 *p_data++ = RFCOMM_EA | 0;
110 *p_data = RFCOMM_DM_FCS((uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET, cr, dlci);
114 rfc_check_send_cmd(p_mcb, p_buf);
117 /*******************************************************************************
119 * Function rfc_send_disc
121 * Description This function sends DISC frame.
123 ******************************************************************************/
124 void rfc_send_disc(tRFC_MCB* p_mcb, uint8_t dlci) {
126 uint8_t cr = RFCOMM_CR(p_mcb->is_initiator, true);
127 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
129 p_buf->offset = L2CAP_MIN_OFFSET;
130 p_data = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
132 /* DISC frame, command, PF = 1, dlci */
133 *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
134 *p_data++ = RFCOMM_DISC | RFCOMM_PF;
135 *p_data++ = RFCOMM_EA | 0;
137 *p_data = RFCOMM_DISC_FCS((uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET, cr, dlci);
141 rfc_check_send_cmd(p_mcb, p_buf);
144 /*******************************************************************************
146 * Function rfc_send_buf_uih
148 * Description This function sends UIH frame.
150 ******************************************************************************/
151 void rfc_send_buf_uih(tRFC_MCB* p_mcb, uint8_t dlci, BT_HDR* p_buf) {
153 uint8_t cr = RFCOMM_CR(p_mcb->is_initiator, true);
156 p_buf->offset -= RFCOMM_CTRL_FRAME_LEN;
157 if (p_buf->len > 127) {
162 credits = (uint8_t)p_buf->layer_specific;
171 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
173 /* UIH frame, command, PF = 0, dlci */
174 *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
175 *p_data++ = RFCOMM_UIH | ((credits) ? RFCOMM_PF : 0);
176 if (p_buf->len <= 127) {
177 *p_data++ = RFCOMM_EA | (p_buf->len << 1);
180 *p_data++ = (p_buf->len & 0x7f) << 1;
181 *p_data++ = p_buf->len >> RFCOMM_SHIFT_LENGTH2;
190 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset + p_buf->len++;
192 *p_data = RFCOMM_UIH_FCS((uint8_t*)(p_buf + 1) + p_buf->offset, dlci);
194 if (dlci == RFCOMM_MX_DLCI) {
195 rfc_check_send_cmd(p_mcb, p_buf);
197 L2CA_DataWrite(p_mcb->lcid, p_buf);
201 /*******************************************************************************
203 * Function rfc_send_pn
205 * Description This function sends DLC Parameters Negotiation Frame.
207 ******************************************************************************/
208 void rfc_send_pn(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command, uint16_t mtu,
209 uint8_t cl, uint8_t k) {
211 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
213 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
214 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
216 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_PN;
217 *p_data++ = RFCOMM_EA | (RFCOMM_MX_PN_LEN << 1);
220 *p_data++ = RFCOMM_PN_FRAM_TYPE_UIH | cl;
222 /* It appeared that we need to reply with the same priority bits as we
224 ** We will use the fact that we reply in the same context so rx_frame can
228 *p_data++ = RFCOMM_PN_PRIORITY_0;
230 *p_data++ = rfc_cb.rfc.rx_frame.u.pn.priority;
232 *p_data++ = RFCOMM_T1_DSEC;
233 *p_data++ = mtu & 0xFF;
234 *p_data++ = mtu >> 8;
235 *p_data++ = RFCOMM_N2;
238 /* Total length is sizeof PN data + mx header 2 */
239 p_buf->len = RFCOMM_MX_PN_LEN + 2;
241 rfc_send_buf_uih(p_mcb, RFCOMM_MX_DLCI, p_buf);
244 /*******************************************************************************
246 * Function rfc_send_fcon
248 * Description This function sends Flow Control On Command.
250 ******************************************************************************/
251 void rfc_send_fcon(tRFC_MCB* p_mcb, bool is_command) {
253 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
255 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
256 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
258 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_FCON;
259 *p_data++ = RFCOMM_EA | (RFCOMM_MX_FCON_LEN << 1);
261 /* Total length is sizeof FCON data + mx header 2 */
262 p_buf->len = RFCOMM_MX_FCON_LEN + 2;
264 rfc_send_buf_uih(p_mcb, RFCOMM_MX_DLCI, p_buf);
267 /*******************************************************************************
269 * Function rfc_send_fcoff
271 * Description This function sends Flow Control Off Command.
273 ******************************************************************************/
274 void rfc_send_fcoff(tRFC_MCB* p_mcb, bool is_command) {
276 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
278 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
279 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
281 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_FCOFF;
282 *p_data++ = RFCOMM_EA | (RFCOMM_MX_FCOFF_LEN << 1);
284 /* Total length is sizeof FCOFF data + mx header 2 */
285 p_buf->len = RFCOMM_MX_FCOFF_LEN + 2;
287 rfc_send_buf_uih(p_mcb, RFCOMM_MX_DLCI, p_buf);
290 /*******************************************************************************
292 * Function rfc_send_msc
294 * Description This function sends Modem Status Command Frame.
296 ******************************************************************************/
297 void rfc_send_msc(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command,
298 tPORT_CTRL* p_pars) {
301 uint8_t break_duration;
303 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
305 signals = p_pars->modem_signal;
306 break_duration = p_pars->break_signal;
308 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
309 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
312 len = RFCOMM_MX_MSC_LEN_WITH_BREAK;
314 len = RFCOMM_MX_MSC_LEN_NO_BREAK;
316 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_MSC;
317 *p_data++ = RFCOMM_EA | (len << 1);
319 *p_data++ = RFCOMM_EA | RFCOMM_CR_MASK | (dlci << RFCOMM_SHIFT_DLCI);
320 *p_data++ = RFCOMM_EA | ((p_pars->fc) ? RFCOMM_MSC_FC : 0) |
321 ((signals & MODEM_SIGNAL_DTRDSR) ? RFCOMM_MSC_RTC : 0) |
322 ((signals & MODEM_SIGNAL_RTSCTS) ? RFCOMM_MSC_RTR : 0) |
323 ((signals & MODEM_SIGNAL_RI) ? RFCOMM_MSC_IC : 0) |
324 ((signals & MODEM_SIGNAL_DCD) ? RFCOMM_MSC_DV : 0);
326 if (break_duration) {
327 *p_data++ = RFCOMM_EA | RFCOMM_MSC_BREAK_PRESENT_MASK |
328 (break_duration << RFCOMM_MSC_SHIFT_BREAK);
331 /* Total length is sizeof MSC data + mx header 2 */
332 p_buf->len = len + 2;
334 rfc_send_buf_uih(p_mcb, RFCOMM_MX_DLCI, p_buf);
337 /*******************************************************************************
339 * Function rfc_send_rls
341 * Description This function sends Remote Line Status Command Frame.
343 ******************************************************************************/
344 void rfc_send_rls(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command,
347 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
349 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
350 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
352 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_RLS;
353 *p_data++ = RFCOMM_EA | (RFCOMM_MX_RLS_LEN << 1);
355 *p_data++ = RFCOMM_EA | RFCOMM_CR_MASK | (dlci << RFCOMM_SHIFT_DLCI);
356 *p_data++ = RFCOMM_RLS_ERROR | status;
358 /* Total length is sizeof RLS data + mx header 2 */
359 p_buf->len = RFCOMM_MX_RLS_LEN + 2;
361 rfc_send_buf_uih(p_mcb, RFCOMM_MX_DLCI, p_buf);
364 /*******************************************************************************
366 * Function rfc_send_nsc
368 * Description This function sends Non Supported Command Response.
370 ******************************************************************************/
371 void rfc_send_nsc(tRFC_MCB* p_mcb) {
373 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
375 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
376 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
378 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(false) | RFCOMM_MX_NSC;
379 *p_data++ = RFCOMM_EA | (RFCOMM_MX_NSC_LEN << 1);
381 *p_data++ = rfc_cb.rfc.rx_frame.ea |
382 (rfc_cb.rfc.rx_frame.cr << RFCOMM_SHIFT_CR) |
383 rfc_cb.rfc.rx_frame.type;
385 /* Total length is sizeof NSC data + mx header 2 */
386 p_buf->len = RFCOMM_MX_NSC_LEN + 2;
388 rfc_send_buf_uih(p_mcb, RFCOMM_MX_DLCI, p_buf);
391 /*******************************************************************************
393 * Function rfc_send_rpn
395 * Description This function sends Remote Port Negotiation Command
397 ******************************************************************************/
398 void rfc_send_rpn(tRFC_MCB* p_mcb, uint8_t dlci, bool is_command,
399 tPORT_STATE* p_pars, uint16_t mask) {
401 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
403 p_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_CTRL_FRAME_LEN;
404 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
406 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_RPN;
409 *p_data++ = RFCOMM_EA | (RFCOMM_MX_RPN_REQ_LEN << 1);
411 *p_data++ = RFCOMM_EA | RFCOMM_CR_MASK | (dlci << RFCOMM_SHIFT_DLCI);
413 p_buf->len = RFCOMM_MX_RPN_REQ_LEN + 2;
415 *p_data++ = RFCOMM_EA | (RFCOMM_MX_RPN_LEN << 1);
417 *p_data++ = RFCOMM_EA | RFCOMM_CR_MASK | (dlci << RFCOMM_SHIFT_DLCI);
418 *p_data++ = p_pars->baud_rate;
419 *p_data++ = (p_pars->byte_size << RFCOMM_RPN_BITS_SHIFT) |
420 (p_pars->stop_bits << RFCOMM_RPN_STOP_BITS_SHIFT) |
421 (p_pars->parity << RFCOMM_RPN_PARITY_SHIFT) |
422 (p_pars->parity_type << RFCOMM_RPN_PARITY_TYPE_SHIFT);
423 *p_data++ = p_pars->fc_type;
424 *p_data++ = p_pars->xon_char;
425 *p_data++ = p_pars->xoff_char;
426 *p_data++ = (mask & 0xFF);
427 *p_data++ = (mask >> 8);
429 /* Total length is sizeof RPN data + mx header 2 */
430 p_buf->len = RFCOMM_MX_RPN_LEN + 2;
433 rfc_send_buf_uih(p_mcb, RFCOMM_MX_DLCI, p_buf);
436 /*******************************************************************************
438 * Function rfc_send_test
440 * Description This function sends Test frame.
442 ******************************************************************************/
443 void rfc_send_test(tRFC_MCB* p_mcb, bool is_command, BT_HDR* p_buf) {
444 /* Shift buffer to give space for header */
445 if (p_buf->offset < (L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET + 2)) {
446 uint8_t* p_src = (uint8_t*)(p_buf + 1) + p_buf->offset + p_buf->len - 1;
448 (BT_HDR*)osi_malloc(p_buf->len + (L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET +
449 2 + sizeof(BT_HDR) + 1));
451 p_new_buf->offset = L2CAP_MIN_OFFSET + RFCOMM_MIN_OFFSET + 2;
452 p_new_buf->len = p_buf->len;
455 (uint8_t*)(p_new_buf + 1) + p_new_buf->offset + p_new_buf->len - 1;
457 for (uint16_t xx = 0; xx < p_buf->len; xx++) *p_dest-- = *p_src--;
463 /* Adjust offset by number of bytes we are going to fill */
465 uint8_t* p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
467 *p_data++ = RFCOMM_EA | RFCOMM_I_CR(is_command) | RFCOMM_MX_TEST;
468 *p_data++ = RFCOMM_EA | (p_buf->len << 1);
472 rfc_send_buf_uih(p_mcb, RFCOMM_MX_DLCI, p_buf);
475 /*******************************************************************************
477 * Function rfc_send_credit
479 * Description This function sends a flow control credit in UIH frame.
481 ******************************************************************************/
482 void rfc_send_credit(tRFC_MCB* p_mcb, uint8_t dlci, uint8_t credit) {
484 uint8_t cr = RFCOMM_CR(p_mcb->is_initiator, true);
485 BT_HDR* p_buf = (BT_HDR*)osi_malloc(RFCOMM_CMD_BUF_SIZE);
487 p_buf->offset = L2CAP_MIN_OFFSET;
488 p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
490 *p_data++ = RFCOMM_EA | cr | (dlci << RFCOMM_SHIFT_DLCI);
491 *p_data++ = RFCOMM_UIH | RFCOMM_PF;
492 *p_data++ = RFCOMM_EA | 0;
494 *p_data = RFCOMM_UIH_FCS((uint8_t*)(p_buf + 1) + p_buf->offset, dlci);
498 rfc_check_send_cmd(p_mcb, p_buf);
501 /*******************************************************************************
503 * Function rfc_parse_data
505 * Description This function processes data packet received from L2CAP
507 ******************************************************************************/
508 uint8_t rfc_parse_data(tRFC_MCB* p_mcb, MX_FRAME* p_frame, BT_HDR* p_buf) {
509 uint8_t ead, eal, fcs;
510 uint8_t* p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
511 uint8_t* p_start = p_data;
514 if (p_buf->len < RFCOMM_CTRL_FRAME_LEN) {
515 RFCOMM_TRACE_ERROR("Bad Length1: %d", p_buf->len);
516 return (RFC_EVENT_BAD_FRAME);
519 RFCOMM_PARSE_CTRL_FIELD(ead, p_frame->cr, p_frame->dlci, p_data);
521 RFCOMM_TRACE_ERROR("Bad Address(EA must be 1)");
522 return (RFC_EVENT_BAD_FRAME);
524 RFCOMM_PARSE_TYPE_FIELD(p_frame->type, p_frame->pf, p_data);
526 eal = *(p_data)&RFCOMM_EA;
527 len = *(p_data)++ >> RFCOMM_SHIFT_LENGTH1;
528 if (eal == 0 && p_buf->len > RFCOMM_CTRL_FRAME_LEN) {
529 len += (*(p_data)++ << RFCOMM_SHIFT_LENGTH2);
530 } else if (eal == 0) {
531 RFCOMM_TRACE_ERROR("Bad Length when EAL = 0: %d", p_buf->len);
532 android_errorWriteLog(0x534e4554, "78288018");
533 return RFC_EVENT_BAD_FRAME;
536 if (p_buf->len < (3 + !ead + !eal + 1)) {
537 android_errorWriteLog(0x534e4554, "120255805");
538 RFCOMM_TRACE_ERROR("Bad Length: %d", p_buf->len);
539 return RFC_EVENT_BAD_FRAME;
541 p_buf->len -= (3 + !ead + !eal + 1); /* Additional 1 for FCS */
542 p_buf->offset += (3 + !ead + !eal);
544 /* handle credit if credit based flow control */
545 if ((p_mcb->flow == PORT_FC_CREDIT) && (p_frame->type == RFCOMM_UIH) &&
546 (p_frame->dlci != RFCOMM_MX_DLCI) && (p_frame->pf == 1)) {
547 if (p_buf->len < sizeof(uint8_t)) {
548 RFCOMM_TRACE_ERROR("Bad Length in flow control: %d", p_buf->len);
549 return RFC_EVENT_BAD_FRAME;
551 p_frame->credit = *p_data++;
558 if (p_buf->len != len) {
559 RFCOMM_TRACE_ERROR("Bad Length2 %d %d", p_buf->len, len);
560 return (RFC_EVENT_BAD_FRAME);
563 fcs = *(p_data + len);
565 /* All control frames that we are sending are sent with P=1, expect */
567 /* According to TS 07.10 spec ivalid frames are discarded without */
568 /* notification to the sender */
569 switch (p_frame->type) {
571 if (RFCOMM_FRAME_IS_RSP(p_mcb->is_initiator, p_frame->cr) ||
572 !p_frame->pf || len || !RFCOMM_VALID_DLCI(p_frame->dlci) ||
573 !rfc_check_fcs(RFCOMM_CTRL_FRAME_LEN, p_start, fcs)) {
574 RFCOMM_TRACE_ERROR("Bad SABME");
575 return (RFC_EVENT_BAD_FRAME);
577 return (RFC_EVENT_SABME);
580 if (RFCOMM_FRAME_IS_CMD(p_mcb->is_initiator, p_frame->cr) ||
581 !p_frame->pf || len || !RFCOMM_VALID_DLCI(p_frame->dlci) ||
582 !rfc_check_fcs(RFCOMM_CTRL_FRAME_LEN, p_start, fcs)) {
583 RFCOMM_TRACE_ERROR("Bad UA");
584 return (RFC_EVENT_BAD_FRAME);
586 return (RFC_EVENT_UA);
589 if (RFCOMM_FRAME_IS_CMD(p_mcb->is_initiator, p_frame->cr) || len ||
590 !RFCOMM_VALID_DLCI(p_frame->dlci) ||
591 !rfc_check_fcs(RFCOMM_CTRL_FRAME_LEN, p_start, fcs)) {
592 RFCOMM_TRACE_ERROR("Bad DM");
593 return (RFC_EVENT_BAD_FRAME);
595 return (RFC_EVENT_DM);
598 if (RFCOMM_FRAME_IS_RSP(p_mcb->is_initiator, p_frame->cr) ||
599 !p_frame->pf || len || !RFCOMM_VALID_DLCI(p_frame->dlci) ||
600 !rfc_check_fcs(RFCOMM_CTRL_FRAME_LEN, p_start, fcs)) {
601 RFCOMM_TRACE_ERROR("Bad DISC");
602 return (RFC_EVENT_BAD_FRAME);
604 return (RFC_EVENT_DISC);
607 if (!RFCOMM_VALID_DLCI(p_frame->dlci)) {
608 RFCOMM_TRACE_ERROR("Bad UIH - invalid DLCI");
609 return (RFC_EVENT_BAD_FRAME);
610 } else if (!rfc_check_fcs(2, p_start, fcs)) {
611 RFCOMM_TRACE_ERROR("Bad UIH - FCS");
612 return (RFC_EVENT_BAD_FRAME);
613 } else if (RFCOMM_FRAME_IS_RSP(p_mcb->is_initiator, p_frame->cr)) {
614 /* we assume that this is ok to allow bad implementations to work */
615 RFCOMM_TRACE_ERROR("Bad UIH - response");
616 return (RFC_EVENT_UIH);
618 return (RFC_EVENT_UIH);
622 return (RFC_EVENT_BAD_FRAME);
625 /*******************************************************************************
627 * Function rfc_process_mx_message
629 * Description This function processes UIH frames received on the
630 * multiplexer control channel.
632 ******************************************************************************/
633 void rfc_process_mx_message(tRFC_MCB* p_mcb, BT_HDR* p_buf) {
634 uint8_t* p_data = (uint8_t*)(p_buf + 1) + p_buf->offset;
635 MX_FRAME* p_rx_frame = &rfc_cb.rfc.rx_frame;
636 uint16_t length = p_buf->len;
637 uint8_t ea, cr, mx_len;
641 "%s: Illegal MX Frame len when reading EA, C/R. len:%d < 2", __func__,
643 android_errorWriteLog(0x534e4554, "111937065");
647 p_rx_frame->ea = *p_data & RFCOMM_EA;
648 p_rx_frame->cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
649 p_rx_frame->type = *p_data++ & ~(RFCOMM_CR_MASK | RFCOMM_EA_MASK);
651 if (!p_rx_frame->ea || !length) {
652 LOG(ERROR) << __func__
653 << ": Invalid MX frame ea=" << std::to_string(p_rx_frame->ea)
654 << ", len=" << length << ", bd_addr=" << p_mcb->bd_addr;
661 bool is_command = p_rx_frame->cr;
663 ea = *p_data & RFCOMM_EA;
665 mx_len = *p_data++ >> RFCOMM_SHIFT_LENGTH1;
670 RFCOMM_TRACE_ERROR("%s: Illegal MX Frame when EA = 0. len:%d < 1",
672 android_errorWriteLog(0x534e4554, "111937065");
676 mx_len += *p_data++ << RFCOMM_SHIFT_LENGTH2;
680 if (mx_len != length) {
681 LOG(ERROR) << __func__ << ": Bad MX frame, p_mcb=" << p_mcb
682 << ", bd_addr=" << p_mcb->bd_addr;
687 RFCOMM_TRACE_DEBUG("%s: type=0x%02x, bd_addr=%s", __func__, p_rx_frame->type,
688 p_mcb->bd_addr.ToString().c_str());
689 switch (p_rx_frame->type) {
691 if (length != RFCOMM_MX_PN_LEN) {
692 LOG(ERROR) << __func__ << ": Invalid PN length, p_mcb=" << p_mcb
693 << ", bd_addr=" << p_mcb->bd_addr;
697 p_rx_frame->dlci = *p_data++ & RFCOMM_PN_DLCI_MASK;
698 p_rx_frame->u.pn.frame_type = *p_data & RFCOMM_PN_FRAME_TYPE_MASK;
699 p_rx_frame->u.pn.conv_layer = *p_data++ & RFCOMM_PN_CONV_LAYER_MASK;
700 p_rx_frame->u.pn.priority = *p_data++ & RFCOMM_PN_PRIORITY_MASK;
701 p_rx_frame->u.pn.t1 = *p_data++;
702 p_rx_frame->u.pn.mtu = *p_data + (*(p_data + 1) << 8);
704 p_rx_frame->u.pn.n2 = *p_data++;
705 p_rx_frame->u.pn.k = *p_data++ & RFCOMM_PN_K_MASK;
707 if (!p_rx_frame->dlci || !RFCOMM_VALID_DLCI(p_rx_frame->dlci) ||
708 (p_rx_frame->u.pn.mtu < RFCOMM_MIN_MTU) ||
709 (p_rx_frame->u.pn.mtu > RFCOMM_MAX_MTU)) {
710 LOG(ERROR) << __func__ << ": Bad PN frame, p_mcb=" << p_mcb
711 << ", bd_addr=" << p_mcb->bd_addr;
717 rfc_process_pn(p_mcb, is_command, p_rx_frame);
723 p_rx_frame->u.test.p_data = p_data;
724 p_rx_frame->u.test.data_len = length;
730 rfc_send_test(p_mcb, false, p_buf);
732 rfc_process_test_rsp(p_mcb, p_buf);
736 if (length != RFCOMM_MX_FCON_LEN) break;
740 rfc_process_fcon(p_mcb, is_command);
743 case RFCOMM_MX_FCOFF:
744 if (length != RFCOMM_MX_FCOFF_LEN) break;
748 rfc_process_fcoff(p_mcb, is_command);
752 if (length != RFCOMM_MX_MSC_LEN_WITH_BREAK &&
753 length != RFCOMM_MX_MSC_LEN_NO_BREAK) {
754 RFCOMM_TRACE_ERROR("%s: Illegal MX MSC Frame len:%d", __func__, length);
755 android_errorWriteLog(0x534e4554, "111937065");
759 ea = *p_data & RFCOMM_EA;
760 cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
761 p_rx_frame->dlci = *p_data++ >> RFCOMM_SHIFT_DLCI;
763 if (!ea || !cr || !p_rx_frame->dlci ||
764 !RFCOMM_VALID_DLCI(p_rx_frame->dlci)) {
765 RFCOMM_TRACE_ERROR("Bad MSC frame");
769 p_rx_frame->u.msc.signals = *p_data++;
771 if (mx_len == RFCOMM_MX_MSC_LEN_WITH_BREAK) {
772 p_rx_frame->u.msc.break_present =
773 *p_data & RFCOMM_MSC_BREAK_PRESENT_MASK;
774 p_rx_frame->u.msc.break_duration =
775 (*p_data & RFCOMM_MSC_BREAK_MASK) >> RFCOMM_MSC_SHIFT_BREAK;
777 p_rx_frame->u.msc.break_present = false;
778 p_rx_frame->u.msc.break_duration = 0;
782 rfc_process_msc(p_mcb, is_command, p_rx_frame);
786 if ((length != RFCOMM_MX_NSC_LEN) || !is_command) break;
788 p_rx_frame->u.nsc.ea = *p_data & RFCOMM_EA;
789 p_rx_frame->u.nsc.cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
790 p_rx_frame->u.nsc.type = *p_data++ >> RFCOMM_SHIFT_DLCI;
794 rfc_process_nsc(p_mcb, p_rx_frame);
798 if ((length != RFCOMM_MX_RPN_REQ_LEN) && (length != RFCOMM_MX_RPN_LEN))
801 ea = *p_data & RFCOMM_EA;
802 cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
803 p_rx_frame->dlci = *p_data++ >> RFCOMM_SHIFT_DLCI;
805 if (!ea || !cr || !p_rx_frame->dlci ||
806 !RFCOMM_VALID_DLCI(p_rx_frame->dlci)) {
807 RFCOMM_TRACE_ERROR("Bad RPN frame");
811 p_rx_frame->u.rpn.is_request = (length == RFCOMM_MX_RPN_REQ_LEN);
813 if (!p_rx_frame->u.rpn.is_request) {
814 p_rx_frame->u.rpn.baud_rate = *p_data++;
815 p_rx_frame->u.rpn.byte_size =
816 (*p_data >> RFCOMM_RPN_BITS_SHIFT) & RFCOMM_RPN_BITS_MASK;
817 p_rx_frame->u.rpn.stop_bits =
818 (*p_data >> RFCOMM_RPN_STOP_BITS_SHIFT) & RFCOMM_RPN_STOP_BITS_MASK;
819 p_rx_frame->u.rpn.parity =
820 (*p_data >> RFCOMM_RPN_PARITY_SHIFT) & RFCOMM_RPN_PARITY_MASK;
821 p_rx_frame->u.rpn.parity_type =
822 (*p_data++ >> RFCOMM_RPN_PARITY_TYPE_SHIFT) &
823 RFCOMM_RPN_PARITY_TYPE_MASK;
825 p_rx_frame->u.rpn.fc_type = *p_data++ & RFCOMM_FC_MASK;
826 p_rx_frame->u.rpn.xon_char = *p_data++;
827 p_rx_frame->u.rpn.xoff_char = *p_data++;
828 p_rx_frame->u.rpn.param_mask =
829 (*p_data + (*(p_data + 1) << 8)) & RFCOMM_RPN_PM_MASK;
833 rfc_process_rpn(p_mcb, is_command, p_rx_frame->u.rpn.is_request,
838 if (length != RFCOMM_MX_RLS_LEN) break;
840 ea = *p_data & RFCOMM_EA;
841 cr = (*p_data & RFCOMM_CR_MASK) >> RFCOMM_SHIFT_CR;
843 p_rx_frame->dlci = *p_data++ >> RFCOMM_SHIFT_DLCI;
844 p_rx_frame->u.rls.line_status = (*p_data & ~0x01);
846 if (!ea || !cr || !p_rx_frame->dlci ||
847 !RFCOMM_VALID_DLCI(p_rx_frame->dlci)) {
848 RFCOMM_TRACE_ERROR("Bad RPN frame");
854 rfc_process_rls(p_mcb, is_command, p_rx_frame);
860 if (is_command) rfc_send_nsc(p_mcb);