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 collection of utility functions used the RFCOMM unit
23 *****************************************************************************/
25 #include "bt_target.h"
39 /*******************************************************************************
41 ** Function rfc_calc_fcs
43 ** Description Reversed CRC Table , 8-bit, poly=0x07
44 ** (GSM 07.10 TS 101 369 V6.3.0)
45 *******************************************************************************/
46 static const UINT8 rfc_crctable[] = {
47 0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75, 0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B,
48 0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A, 0xF8, 0x69, 0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67,
49 0x38, 0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D, 0x36, 0xA7, 0xD5, 0x44, 0x31, 0xA0, 0xD2, 0x43,
50 0x24, 0xB5, 0xC7, 0x56, 0x23, 0xB2, 0xC0, 0x51, 0x2A, 0xBB, 0xC9, 0x58, 0x2D, 0xBC, 0xCE, 0x5F,
52 0x70, 0xE1, 0x93, 0x02, 0x77, 0xE6, 0x94, 0x05, 0x7E, 0xEF, 0x9D, 0x0C, 0x79, 0xE8, 0x9A, 0x0B,
53 0x6C, 0xFD, 0x8F, 0x1E, 0x6B, 0xFA, 0x88, 0x19, 0x62, 0xF3, 0x81, 0x10, 0x65, 0xF4, 0x86, 0x17,
54 0x48, 0xD9, 0xAB, 0x3A, 0x4F, 0xDE, 0xAC, 0x3D, 0x46, 0xD7, 0xA5, 0x34, 0x41, 0xD0, 0xA2, 0x33,
55 0x54, 0xC5, 0xB7, 0x26, 0x53, 0xC2, 0xB0, 0x21, 0x5A, 0xCB, 0xB9, 0x28, 0x5D, 0xCC, 0xBE, 0x2F,
57 0xE0, 0x71, 0x03, 0x92, 0xE7, 0x76, 0x04, 0x95, 0xEE, 0x7F, 0x0D, 0x9C, 0xE9, 0x78, 0x0A, 0x9B,
58 0xFC, 0x6D, 0x1F, 0x8E, 0xFB, 0x6A, 0x18, 0x89, 0xF2, 0x63, 0x11, 0x80, 0xF5, 0x64, 0x16, 0x87,
59 0xD8, 0x49, 0x3B, 0xAA, 0xDF, 0x4E, 0x3C, 0xAD, 0xD6, 0x47, 0x35, 0xA4, 0xD1, 0x40, 0x32, 0xA3,
60 0xC4, 0x55, 0x27, 0xB6, 0xC3, 0x52, 0x20, 0xB1, 0xCA, 0x5B, 0x29, 0xB8, 0xCD, 0x5C, 0x2E, 0xBF,
62 0x90, 0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5, 0x9E, 0x0F, 0x7D, 0xEC, 0x99, 0x08, 0x7A, 0xEB,
63 0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A, 0x68, 0xF9, 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7,
64 0xA8, 0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD, 0xA6, 0x37, 0x45, 0xD4, 0xA1, 0x30, 0x42, 0xD3,
65 0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50, 0xC1, 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF
69 /*******************************************************************************
71 ** Function rfc_calc_fcs
73 ** Description This function calculate FCS for the RFCOMM frame
74 ** (GSM 07.10 TS 101 369 V6.3.0)
76 ** Input len - number of bytes in the message
77 ** p - points to message
79 *******************************************************************************/
80 UINT8 rfc_calc_fcs (UINT16 len, UINT8 *p)
85 fcs = rfc_crctable[fcs ^ *p++];
93 /*******************************************************************************
95 ** Function rfc_check_fcs
97 ** Description This function checks FCS for the RFCOMM frame
98 ** (GSM 07.10 TS 101 369 V6.3.0)
100 ** Input len - number of bytes in the message
101 ** p - points to message
102 ** received_fcs - received FCS
104 *******************************************************************************/
105 BOOLEAN rfc_check_fcs (UINT16 len, UINT8 *p, UINT8 received_fcs)
110 fcs = rfc_crctable[fcs ^ *p++];
113 /* Ones compliment */
114 fcs = rfc_crctable[fcs ^ received_fcs];
116 /*0xCF is the reversed order of 11110011.*/
117 return (fcs == 0xCF);
121 /*******************************************************************************
123 ** Function rfc_alloc_multiplexer_channel
125 ** Description This function returns existing or new control block for
128 *******************************************************************************/
129 tRFC_MCB *rfc_alloc_multiplexer_channel (BD_ADDR bd_addr, BOOLEAN is_initiator)
132 tRFC_MCB *p_mcb = NULL;
133 RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel: bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
134 bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
135 RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel:is_initiator:%d", is_initiator);
137 for (i = 0; i < MAX_BD_CONNECTIONS; i++) {
138 RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel rfc_cb.port.rfc_mcb[%d].state:%d",
139 i, rfc_cb.port.rfc_mcb[i].state);
140 RFCOMM_TRACE_DEBUG("(rfc_cb.port.rfc_mcb[i].bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
141 rfc_cb.port.rfc_mcb[i].bd_addr[0], rfc_cb.port.rfc_mcb[i].bd_addr[1],
142 rfc_cb.port.rfc_mcb[i].bd_addr[2], rfc_cb.port.rfc_mcb[i].bd_addr[3],
143 rfc_cb.port.rfc_mcb[i].bd_addr[4], rfc_cb.port.rfc_mcb[i].bd_addr[5]);
145 if ((rfc_cb.port.rfc_mcb[i].state != RFC_MX_STATE_IDLE)
146 && (!memcmp (rfc_cb.port.rfc_mcb[i].bd_addr, bd_addr, BD_ADDR_LEN))) {
147 /* Multiplexer channel found do not change anything */
148 /* If there was an inactivity timer running stop it now */
149 if (rfc_cb.port.rfc_mcb[i].state == RFC_MX_STATE_CONNECTED) {
150 rfc_timer_stop (&rfc_cb.port.rfc_mcb[i]);
152 RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel:is_initiator:%d, found, state:%d, p_mcb:%p",
153 is_initiator, rfc_cb.port.rfc_mcb[i].state, &rfc_cb.port.rfc_mcb[i]);
154 return (&rfc_cb.port.rfc_mcb[i]);
158 /* connection with bd_addr does not exist */
159 for (i = 0, j = rfc_cb.rfc.last_mux + 1; i < MAX_BD_CONNECTIONS; i++, j++) {
160 if (j >= MAX_BD_CONNECTIONS) {
164 p_mcb = &rfc_cb.port.rfc_mcb[j];
165 if (rfc_cb.port.rfc_mcb[j].state == RFC_MX_STATE_IDLE) {
166 /* New multiplexer control block */
167 fixed_queue_free(p_mcb->cmd_q, NULL);
168 memset (p_mcb, 0, sizeof (tRFC_MCB));
169 memcpy (p_mcb->bd_addr, bd_addr, BD_ADDR_LEN);
170 RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel:is_initiator:%d, create new p_mcb:%p, index:%d",
171 is_initiator, &rfc_cb.port.rfc_mcb[j], j);
173 p_mcb->cmd_q = fixed_queue_new(SIZE_MAX);
175 p_mcb->is_initiator = is_initiator;
177 rfc_timer_start (p_mcb, RFC_MCB_INIT_INACT_TIMER);
179 rfc_cb.rfc.last_mux = (UINT8) j;
187 /*******************************************************************************
189 ** Function rfc_release_multiplexer_channel
191 ** Description This function returns existing or new control block for
194 *******************************************************************************/
195 void rfc_release_multiplexer_channel (tRFC_MCB *p_mcb)
198 rfc_timer_stop (p_mcb);
201 fixed_queue_free(p_mcb->cmd_q, osi_free);
203 memset (p_mcb, 0, sizeof (tRFC_MCB));
204 p_mcb->state = RFC_MX_STATE_IDLE;
208 /*******************************************************************************
210 ** Function rfc_timer_start
212 ** Description Start RFC Timer
214 *******************************************************************************/
215 void rfc_timer_start (tRFC_MCB *p_mcb, UINT16 timeout)
217 TIMER_LIST_ENT *p_tle = &p_mcb->tle;
219 RFCOMM_TRACE_EVENT ("rfc_timer_start - timeout:%d", timeout);
221 p_tle->param = (UINT32)p_mcb;
223 btu_start_timer (p_tle, BTU_TTYPE_RFCOMM_MFC, timeout);
227 /*******************************************************************************
229 ** Function rfc_timer_stop
231 ** Description Stop RFC Timer
233 *******************************************************************************/
234 void rfc_timer_stop (tRFC_MCB *p_mcb)
236 RFCOMM_TRACE_EVENT ("rfc_timer_stop");
238 btu_stop_timer (&p_mcb->tle);
242 /*******************************************************************************
244 ** Function rfc_port_timer_start
246 ** Description Start RFC Timer
248 *******************************************************************************/
249 void rfc_port_timer_start (tPORT *p_port, UINT16 timeout)
251 TIMER_LIST_ENT *p_tle = &p_port->rfc.tle;
253 RFCOMM_TRACE_EVENT ("rfc_port_timer_start - timeout:%d", timeout);
255 p_tle->param = (UINT32)p_port;
257 btu_start_timer (p_tle, BTU_TTYPE_RFCOMM_PORT, timeout);
261 /*******************************************************************************
263 ** Function rfc_port_timer_stop
265 ** Description Stop RFC Timer
267 *******************************************************************************/
268 void rfc_port_timer_stop (tPORT *p_port)
270 RFCOMM_TRACE_EVENT ("rfc_port_timer_stop");
272 btu_stop_timer (&p_port->rfc.tle);
276 /*******************************************************************************
278 ** Function rfc_check_mcb_active
280 ** Description Check if there are any opened ports on the MCB if not
281 ** start MCB Inact timer.
285 *******************************************************************************/
286 void rfc_check_mcb_active (tRFC_MCB *p_mcb)
290 for (i = 0; i < RFCOMM_MAX_DLCI; i++) {
291 if (p_mcb->port_inx[i] != 0) {
292 p_mcb->is_disc_initiator = FALSE;
296 /* The last port was DISCed. On the client side start disconnecting Mx */
297 /* On the server side start inactivity timer */
298 if (p_mcb->is_disc_initiator) {
299 p_mcb->is_disc_initiator = FALSE;
300 rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CLOSE_REQ, NULL);
302 rfc_timer_start (p_mcb, RFC_MCB_RELEASE_INACT_TIMER);
307 /*******************************************************************************
309 ** Function rfcomm_process_timeout
311 ** Description The function called every second to check RFCOMM timers
315 *******************************************************************************/
316 void rfcomm_process_timeout (TIMER_LIST_ENT *p_tle)
318 switch (p_tle->event) {
319 case BTU_TTYPE_RFCOMM_MFC:
320 rfc_mx_sm_execute ((tRFC_MCB *)p_tle->param, RFC_EVENT_TIMEOUT, NULL);
323 case BTU_TTYPE_RFCOMM_PORT:
324 rfc_port_sm_execute ((tPORT *)p_tle->param, RFC_EVENT_TIMEOUT, NULL);
333 /*******************************************************************************
335 ** Function rfc_sec_check_complete
337 ** Description The function called when Security Manager finishes
338 ** verification of the service side connection
342 *******************************************************************************/
343 void rfc_sec_check_complete (BD_ADDR bd_addr, tBT_TRANSPORT transport, void *p_ref_data, UINT8 res)
345 tPORT *p_port = (tPORT *)p_ref_data;
349 /* Verify that PORT is still waiting for Security to complete */
351 || ((p_port->rfc.state != RFC_STATE_ORIG_WAIT_SEC_CHECK)
352 && (p_port->rfc.state != RFC_STATE_TERM_WAIT_SEC_CHECK))) {
356 rfc_port_sm_execute ((tPORT *)p_ref_data, RFC_EVENT_SEC_COMPLETE, &res);
360 /*******************************************************************************
362 ** Function rfc_port_closed
364 ** Description The function is called when port is released based on the
365 ** event received from the lower layer, typically L2CAP
366 ** connection down, DISC, or DM frame.
370 *******************************************************************************/
371 void rfc_port_closed (tPORT *p_port)
373 tRFC_MCB *p_mcb = p_port->rfc.p_mcb;
375 RFCOMM_TRACE_DEBUG ("rfc_port_closed");
377 rfc_port_timer_stop (p_port);
379 p_port->rfc.state = RFC_STATE_CLOSED;
381 /* If multiplexer channel was up mark it as down */
383 p_mcb->port_inx[p_port->dlci] = 0;
385 /* If there are no more ports opened on this MCB release it */
386 rfc_check_mcb_active (p_mcb);
389 /* Notify port that RFC connection is gone */
390 port_rfc_closed (p_port, PORT_CLOSED);
393 /*******************************************************************************
395 ** Function rfc_inc_credit
397 ** Description The function is called when a credit is received in a UIH
398 ** frame. It increments the TX credit count, and if data
399 ** flow had halted, it restarts it.
403 *******************************************************************************/
404 void rfc_inc_credit (tPORT *p_port, UINT8 credit)
406 if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) {
407 p_port->credit_tx += credit;
409 RFCOMM_TRACE_EVENT ("rfc_inc_credit:%d", p_port->credit_tx);
411 if (p_port->tx.peer_fc == TRUE) {
412 PORT_FlowInd(p_port->rfc.p_mcb, p_port->dlci, TRUE);
417 /*******************************************************************************
419 ** Function rfc_dec_credit
421 ** Description The function is called when a UIH frame of user data is
422 ** sent. It decrements the credit count. If credit count
423 ** Reaches zero, peer_fc is set.
427 *******************************************************************************/
428 void rfc_dec_credit (tPORT *p_port)
430 if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) {
431 if (p_port->credit_tx > 0) {
435 if (p_port->credit_tx == 0) {
436 p_port->tx.peer_fc = TRUE;
442 /*******************************************************************************
444 ** Function rfc_check_send_cmd
446 ** Description This function is called to send an RFCOMM command message
447 ** or to handle the RFCOMM command message queue.
451 *******************************************************************************/
452 void rfc_check_send_cmd(tRFC_MCB *p_mcb, BT_HDR *p_buf)
456 /* if passed a buffer queue it */
458 if (p_mcb->cmd_q == NULL) {
459 RFCOMM_TRACE_ERROR("%s: empty queue: p_mcb = %p p_mcb->lcid = %u cached p_mcb = %p",
460 __func__, p_mcb, p_mcb->lcid,
461 rfc_find_lcid_mcb(p_mcb->lcid));
463 fixed_queue_enqueue(p_mcb->cmd_q, p_buf);
466 /* handle queue if L2CAP not congested */
467 while (p_mcb->l2cap_congested == FALSE) {
468 if ((p = (BT_HDR *)fixed_queue_try_dequeue(p_mcb->cmd_q)) == NULL) {
473 L2CA_DataWrite (p_mcb->lcid, p);