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 state machine and action routines for multiplexer
22 * channel of the RFCOMM unit
24 ******************************************************************************/
26 #include "bt_common.h"
36 #define L2CAP_SUCCESS 0
40 /********************************************************************************/
41 /* L O C A L F U N C T I O N P R O T O T Y P E S */
42 /********************************************************************************/
43 static void rfc_mx_sm_state_idle (tRFC_MCB *p_mcb, UINT16 event, void *p_data);
44 static void rfc_mx_sm_state_wait_conn_cnf (tRFC_MCB *p_mcb, UINT16 event, void *p_data);
45 static void rfc_mx_sm_state_configure (tRFC_MCB *p_mcb, UINT16 event, void *p_data);
46 static void rfc_mx_sm_sabme_wait_ua (tRFC_MCB *p_mcb, UINT16 event, void *p_data);
47 static void rfc_mx_sm_state_wait_sabme (tRFC_MCB *p_mcb, UINT16 event, void *p_data);
48 static void rfc_mx_sm_state_connected (tRFC_MCB *p_mcb, UINT16 event, void *p_data);
49 static void rfc_mx_sm_state_disc_wait_ua (tRFC_MCB *p_mcb, UINT16 event, void *p_data);
51 static void rfc_mx_send_config_req (tRFC_MCB *p_mcb);
52 static void rfc_mx_conf_ind (tRFC_MCB *p_mcb, tL2CAP_CFG_INFO *p_cfg);
53 static void rfc_mx_conf_cnf (tRFC_MCB *p_mcb, tL2CAP_CFG_INFO *p_cfg);
57 /*******************************************************************************
59 ** Function rfc_mx_sm_execute
61 ** Description This function sends multiplexor events through the state
66 *******************************************************************************/
67 void rfc_mx_sm_execute (tRFC_MCB *p_mcb, UINT16 event, void *p_data)
71 case RFC_MX_STATE_IDLE:
72 rfc_mx_sm_state_idle (p_mcb, event, p_data);
75 case RFC_MX_STATE_WAIT_CONN_CNF:
76 rfc_mx_sm_state_wait_conn_cnf (p_mcb, event, p_data);
79 case RFC_MX_STATE_CONFIGURE:
80 rfc_mx_sm_state_configure (p_mcb, event, p_data);
83 case RFC_MX_STATE_SABME_WAIT_UA:
84 rfc_mx_sm_sabme_wait_ua (p_mcb, event, p_data);
87 case RFC_MX_STATE_WAIT_SABME:
88 rfc_mx_sm_state_wait_sabme (p_mcb, event, p_data);
91 case RFC_MX_STATE_CONNECTED:
92 rfc_mx_sm_state_connected (p_mcb, event, p_data);
95 case RFC_MX_STATE_DISC_WAIT_UA:
96 rfc_mx_sm_state_disc_wait_ua (p_mcb, event, p_data);
103 /*******************************************************************************
105 ** Function rfc_mx_sm_state_idle
107 ** Description This function handles events when the multiplexer is in
108 ** IDLE state. This state exists when connection is being
109 ** initially established.
113 *******************************************************************************/
114 void rfc_mx_sm_state_idle (tRFC_MCB *p_mcb, UINT16 event, void *p_data)
116 RFCOMM_TRACE_EVENT ("rfc_mx_sm_state_idle - evt:%d", event);
119 case RFC_MX_EVENT_START_REQ:
121 /* Initialize L2CAP MTU */
122 p_mcb->peer_l2cap_mtu = L2CAP_DEFAULT_MTU - RFCOMM_MIN_OFFSET - 1;
124 UINT16 lcid = L2CA_ConnectReq(BT_PSM_RFCOMM, p_mcb->bd_addr);
126 rfc_save_lcid_mcb(NULL, p_mcb->lcid);
128 PORT_StartCnf(p_mcb, RFCOMM_ERROR);
132 /* Save entry for quicker access to mcb based on the LCID */
133 rfc_save_lcid_mcb (p_mcb, p_mcb->lcid);
135 p_mcb->state = RFC_MX_STATE_WAIT_CONN_CNF;
138 case RFC_MX_EVENT_START_RSP:
139 case RFC_MX_EVENT_CONN_CNF:
140 case RFC_MX_EVENT_CONF_IND:
141 case RFC_MX_EVENT_CONF_CNF:
142 RFCOMM_TRACE_ERROR ("Mx error state %d event %d", p_mcb->state, event);
145 case RFC_MX_EVENT_CONN_IND:
147 rfc_timer_start (p_mcb, RFCOMM_CONN_TIMEOUT);
148 L2CA_ConnectRsp (p_mcb->bd_addr, *((UINT8 *)p_data), p_mcb->lcid, L2CAP_CONN_OK, 0);
150 rfc_mx_send_config_req (p_mcb);
152 p_mcb->state = RFC_MX_STATE_CONFIGURE;
155 case RFC_EVENT_SABME:
163 rfc_send_dm (p_mcb, RFCOMM_MX_DLCI, TRUE);
167 rfc_send_dm (p_mcb, RFCOMM_MX_DLCI, FALSE);
170 RFCOMM_TRACE_EVENT ("RFCOMM MX ignored - evt:%d in state:%d", event, p_mcb->state);
174 /*******************************************************************************
176 ** Function rfc_mx_sm_state_wait_conn_cnf
178 ** Description This function handles events when the multiplexer is
179 ** waiting for Connection Confirm from L2CAP.
183 *******************************************************************************/
184 void rfc_mx_sm_state_wait_conn_cnf (tRFC_MCB *p_mcb, UINT16 event, void *p_data)
186 RFCOMM_TRACE_EVENT ("rfc_mx_sm_state_wait_conn_cnf - evt:%d", event);
189 case RFC_MX_EVENT_START_REQ:
190 RFCOMM_TRACE_ERROR ("Mx error state %d event %d", p_mcb->state, event);
193 /* There is some new timing so that Config Ind comes before security is completed
194 so we are still waiting fo the confirmation. */
195 case RFC_MX_EVENT_CONF_IND:
196 rfc_mx_conf_ind (p_mcb, (tL2CAP_CFG_INFO *)p_data);
199 case RFC_MX_EVENT_CONN_CNF:
200 if (*((UINT16 *)p_data) != L2CAP_SUCCESS)
202 p_mcb->state = RFC_MX_STATE_IDLE;
204 PORT_StartCnf (p_mcb, *((UINT16 *)p_data));
207 p_mcb->state = RFC_MX_STATE_CONFIGURE;
208 rfc_mx_send_config_req (p_mcb);
211 case RFC_MX_EVENT_DISC_IND:
212 p_mcb->state = RFC_MX_STATE_IDLE;
213 PORT_CloseInd (p_mcb);
216 case RFC_EVENT_TIMEOUT:
217 p_mcb->state = RFC_MX_STATE_IDLE;
218 L2CA_DisconnectReq (p_mcb->lcid);
220 /* we gave up outgoing connection request then try peer's request */
221 if (p_mcb->pending_lcid)
226 RFCOMM_TRACE_DEBUG ("RFCOMM MX retry as acceptor in collision case - evt:%d in state:%d", event, p_mcb->state);
228 rfc_save_lcid_mcb (NULL, p_mcb->lcid);
229 p_mcb->lcid = p_mcb->pending_lcid;
230 rfc_save_lcid_mcb (p_mcb, p_mcb->lcid);
232 p_mcb->is_initiator = FALSE;
234 /* update direction bit */
235 for (i = 0; i < RFCOMM_MAX_DLCI; i += 2)
237 if ((idx = p_mcb->port_inx[i]) != 0)
239 p_mcb->port_inx[i] = 0;
240 p_mcb->port_inx[i+1] = idx;
241 rfc_cb.port.port[idx - 1].dlci += 1;
242 RFCOMM_TRACE_DEBUG ("RFCOMM MX - DLCI:%d -> %d", i, rfc_cb.port.port[idx - 1].dlci);
246 rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONN_IND, &(p_mcb->pending_id));
250 PORT_CloseInd (p_mcb);
254 RFCOMM_TRACE_EVENT ("RFCOMM MX ignored - evt:%d in state:%d", event, p_mcb->state);
258 /*******************************************************************************
260 ** Function rfc_mx_sm_state_configure
262 ** Description This function handles events when the multiplexer in the
263 ** configuration state.
267 *******************************************************************************/
268 void rfc_mx_sm_state_configure (tRFC_MCB *p_mcb, UINT16 event, void *p_data)
270 RFCOMM_TRACE_EVENT ("rfc_mx_sm_state_configure - evt:%d", event);
273 case RFC_MX_EVENT_START_REQ:
274 case RFC_MX_EVENT_CONN_CNF:
276 RFCOMM_TRACE_ERROR ("Mx error state %d event %d", p_mcb->state, event);
279 case RFC_MX_EVENT_CONF_IND:
280 rfc_mx_conf_ind (p_mcb, (tL2CAP_CFG_INFO *)p_data);
283 case RFC_MX_EVENT_CONF_CNF:
284 rfc_mx_conf_cnf (p_mcb, (tL2CAP_CFG_INFO *)p_data);
287 case RFC_MX_EVENT_DISC_IND:
288 p_mcb->state = RFC_MX_STATE_IDLE;
289 PORT_CloseInd (p_mcb);
292 case RFC_EVENT_TIMEOUT:
293 p_mcb->state = RFC_MX_STATE_IDLE;
294 L2CA_DisconnectReq (p_mcb->lcid);
296 PORT_StartCnf (p_mcb, RFCOMM_ERROR);
299 RFCOMM_TRACE_EVENT ("RFCOMM MX ignored - evt:%d in state:%d", event, p_mcb->state);
303 /*******************************************************************************
305 ** Function rfc_mx_sm_sabme_wait_ua
307 ** Description This function handles events when the multiplexer sent
308 ** SABME and is waiting for UA reply.
312 *******************************************************************************/
313 void rfc_mx_sm_sabme_wait_ua (tRFC_MCB *p_mcb, UINT16 event, void *p_data)
317 RFCOMM_TRACE_EVENT ("rfc_mx_sm_sabme_wait_ua - evt:%d", event);
320 case RFC_MX_EVENT_START_REQ:
321 case RFC_MX_EVENT_CONN_CNF:
322 RFCOMM_TRACE_ERROR ("Mx error state %d event %d", p_mcb->state, event);
325 /* workaround: we don't support reconfig */
326 /* commented out until we support reconfig
327 case RFC_MX_EVENT_CONF_IND:
328 rfc_mx_conf_ind (p_mcb, (tL2CAP_CFG_INFO *)p_data);
331 case RFC_MX_EVENT_CONF_CNF:
332 rfc_mx_conf_cnf (p_mcb, (tL2CAP_CFG_INFO *)p_data);
336 case RFC_MX_EVENT_DISC_IND:
337 p_mcb->state = RFC_MX_STATE_IDLE;
338 PORT_CloseInd (p_mcb);
342 rfc_timer_stop (p_mcb);
344 p_mcb->state = RFC_MX_STATE_CONNECTED;
345 p_mcb->peer_ready = TRUE;
347 PORT_StartCnf (p_mcb, RFCOMM_SUCCESS);
351 rfc_timer_stop (p_mcb);
352 /* Case falls through */
354 case RFC_MX_EVENT_CONF_IND: /* workaround: we don't support reconfig */
355 case RFC_MX_EVENT_CONF_CNF: /* workaround: we don't support reconfig */
356 case RFC_EVENT_TIMEOUT:
357 p_mcb->state = RFC_MX_STATE_IDLE;
358 L2CA_DisconnectReq (p_mcb->lcid);
360 PORT_StartCnf (p_mcb, RFCOMM_ERROR);
363 RFCOMM_TRACE_EVENT ("RFCOMM MX ignored - evt:%d in state:%d", event, p_mcb->state);
366 /*******************************************************************************
368 ** Function rfc_mx_sm_state_wait_sabme
370 ** Description This function handles events when the multiplexer is
371 ** waiting for SABME on the acceptor side after configuration
375 *******************************************************************************/
376 void rfc_mx_sm_state_wait_sabme (tRFC_MCB *p_mcb, UINT16 event, void *p_data)
378 RFCOMM_TRACE_EVENT ("rfc_mx_sm_state_wait_sabme - evt:%d", event);
381 case RFC_MX_EVENT_DISC_IND:
382 p_mcb->state = RFC_MX_STATE_IDLE;
383 PORT_CloseInd (p_mcb);
386 case RFC_EVENT_SABME:
387 /* if we gave up outgoing connection request */
388 if (p_mcb->pending_lcid)
390 p_mcb->pending_lcid = 0;
392 rfc_send_ua (p_mcb, RFCOMM_MX_DLCI);
394 rfc_timer_stop (p_mcb);
395 p_mcb->state = RFC_MX_STATE_CONNECTED;
396 p_mcb->peer_ready = TRUE;
398 /* MX channel collision has been resolved, continue to open ports */
399 PORT_StartCnf (p_mcb, RFCOMM_SUCCESS);
403 rfc_timer_stop (p_mcb);
404 PORT_StartInd (p_mcb);
408 case RFC_MX_EVENT_START_RSP:
409 if (*((UINT16 *)p_data) != RFCOMM_SUCCESS)
410 rfc_send_dm (p_mcb, RFCOMM_MX_DLCI, TRUE);
413 rfc_send_ua (p_mcb, RFCOMM_MX_DLCI);
415 p_mcb->state = RFC_MX_STATE_CONNECTED;
416 p_mcb->peer_ready = TRUE;
417 PORT_StartCnf (p_mcb, RFCOMM_SUCCESS);
421 case RFC_MX_EVENT_CONF_IND: /* workaround: we don't support reconfig */
422 case RFC_MX_EVENT_CONF_CNF: /* workaround: we don't support reconfig */
423 case RFC_EVENT_TIMEOUT:
424 p_mcb->state = RFC_MX_STATE_IDLE;
425 L2CA_DisconnectReq (p_mcb->lcid);
427 PORT_CloseInd (p_mcb);
430 RFCOMM_TRACE_EVENT ("RFCOMM MX ignored - evt:%d in state:%d", event, p_mcb->state);
434 /*******************************************************************************
436 ** Function rfc_mx_sm_state_connected
438 ** Description This function handles events when the multiplexer is
439 ** in the CONNECTED state
443 *******************************************************************************/
444 void rfc_mx_sm_state_connected (tRFC_MCB *p_mcb, UINT16 event, void *p_data)
448 RFCOMM_TRACE_EVENT ("rfc_mx_sm_state_connected - evt:%d", event);
452 case RFC_EVENT_TIMEOUT:
453 case RFC_MX_EVENT_CLOSE_REQ:
454 rfc_timer_start (p_mcb, RFC_DISC_TIMEOUT);
455 p_mcb->state = RFC_MX_STATE_DISC_WAIT_UA;
456 rfc_send_disc (p_mcb, RFCOMM_MX_DLCI);
459 case RFC_MX_EVENT_DISC_IND:
460 p_mcb->state = RFC_MX_STATE_IDLE;
461 PORT_CloseInd (p_mcb);
465 /* Reply with UA. If initiator bring down L2CAP connection */
466 /* If server wait for some time if client decide to reinitiate channel */
467 rfc_send_ua (p_mcb, RFCOMM_MX_DLCI);
468 if (p_mcb->is_initiator)
470 L2CA_DisconnectReq (p_mcb->lcid);
472 /* notify all ports that connection is gone */
473 PORT_CloseInd (p_mcb);
476 RFCOMM_TRACE_EVENT ("RFCOMM MX ignored - evt:%d in state:%d", event, p_mcb->state);
480 /*******************************************************************************
482 ** Function rfc_mx_sm_state_disc_wait_ua
484 ** Description This function handles events when the multiplexer sent
485 ** DISC and is waiting for UA reply.
489 *******************************************************************************/
490 void rfc_mx_sm_state_disc_wait_ua (tRFC_MCB *p_mcb, UINT16 event, void *p_data)
494 RFCOMM_TRACE_EVENT ("rfc_mx_sm_state_disc_wait_ua - evt:%d", event);
499 case RFC_EVENT_TIMEOUT:
500 L2CA_DisconnectReq (p_mcb->lcid);
502 if (p_mcb->restart_required)
504 /* Start Request was received while disconnecting. Execute it again */
505 UINT16 lcid = L2CA_ConnectReq(BT_PSM_RFCOMM, p_mcb->bd_addr);
507 rfc_save_lcid_mcb(NULL, p_mcb->lcid);
509 PORT_StartCnf(p_mcb, RFCOMM_ERROR);
513 /* Save entry for quicker access to mcb based on the LCID */
514 rfc_save_lcid_mcb (p_mcb, p_mcb->lcid);
516 /* clean up before reuse it */
517 while ((p_buf = (BT_HDR *)fixed_queue_try_dequeue(p_mcb->cmd_q)) != NULL)
520 rfc_timer_start (p_mcb, RFC_MCB_INIT_INACT_TIMER);
522 p_mcb->is_initiator = TRUE;
523 p_mcb->restart_required = FALSE;
524 p_mcb->local_cfg_sent = FALSE;
525 p_mcb->peer_cfg_rcvd = FALSE;
527 p_mcb->state = RFC_MX_STATE_WAIT_CONN_CNF;
530 rfc_release_multiplexer_channel (p_mcb);
534 rfc_send_ua (p_mcb, RFCOMM_MX_DLCI);
539 rfc_send_dm (p_mcb, RFCOMM_MX_DLCI, FALSE);
542 case RFC_MX_EVENT_START_REQ:
543 p_mcb->restart_required = TRUE;
546 case RFC_MX_EVENT_DISC_IND:
547 p_mcb->state = RFC_MX_STATE_IDLE;
548 PORT_CloseInd (p_mcb);
551 case RFC_MX_EVENT_CLOSE_REQ:
554 case RFC_MX_EVENT_QOS_VIOLATION_IND:
557 RFCOMM_TRACE_EVENT ("RFCOMM MX ignored - evt:%d in state:%d", event, p_mcb->state);
561 /*******************************************************************************
563 ** Function rfc_mx_send_config_req
565 ** Description This function handles L2CA_ConnectInd message from the
566 ** L2CAP. Accept connection.
568 *******************************************************************************/
569 static void rfc_mx_send_config_req (tRFC_MCB *p_mcb)
573 RFCOMM_TRACE_EVENT ("rfc_mx_send_config_req");
575 memset (&cfg, 0, sizeof (tL2CAP_CFG_INFO));
577 cfg.mtu_present = TRUE;
578 cfg.mtu = L2CAP_MTU_SIZE;
580 /* Defaults set by memset
581 cfg.flush_to_present = FALSE;
582 cfg.qos_present = FALSE;
583 cfg.fcr_present = FALSE;
584 cfg.fcr.mode = L2CAP_FCR_BASIC_MODE;
585 cfg.fcs_present = FALSE;
586 cfg.fcs = N/A when fcs_present is FALSE;
588 L2CA_ConfigReq (p_mcb->lcid, &cfg);
592 /*******************************************************************************
594 ** Function rfc_mx_conf_cnf
596 ** Description This function handles L2CA_ConfigCnf message from the
597 ** L2CAP. If result is not success tell upper layer that
598 ** start has not been accepted. If initiator send SABME
599 ** on DLCI 0. T1 is still running.
601 *******************************************************************************/
602 static void rfc_mx_conf_cnf (tRFC_MCB *p_mcb, tL2CAP_CFG_INFO *p_cfg)
604 RFCOMM_TRACE_EVENT ("rfc_mx_conf_cnf p_cfg:%08x res:%d ", p_cfg, (p_cfg) ? p_cfg->result : 0);
606 if (p_cfg->result != L2CAP_CFG_OK)
608 if (p_mcb->is_initiator)
610 PORT_StartCnf (p_mcb, p_cfg->result);
611 L2CA_DisconnectReq (p_mcb->lcid);
613 rfc_release_multiplexer_channel (p_mcb);
617 p_mcb->local_cfg_sent = TRUE;
618 if ((p_mcb->state == RFC_MX_STATE_CONFIGURE) && p_mcb->peer_cfg_rcvd)
620 if (p_mcb->is_initiator)
622 p_mcb->state = RFC_MX_STATE_SABME_WAIT_UA;
623 rfc_send_sabme (p_mcb, RFCOMM_MX_DLCI);
624 rfc_timer_start (p_mcb, RFC_T1_TIMEOUT);
628 p_mcb->state = RFC_MX_STATE_WAIT_SABME;
629 rfc_timer_start (p_mcb, RFCOMM_CONN_TIMEOUT); /* - increased from T2=20 to CONN=120
630 to allow the user more than 10 sec to type in the
631 pin which can be e.g. 16 digits */
637 /*******************************************************************************
639 ** Function rfc_mx_conf_ind
641 ** Description This function handles L2CA_ConfigInd message from the
642 ** L2CAP. Send the L2CA_ConfigRsp message.
644 *******************************************************************************/
645 static void rfc_mx_conf_ind (tRFC_MCB *p_mcb, tL2CAP_CFG_INFO *p_cfg)
647 /* Save peer L2CAP MTU if present */
648 /* RFCOMM adds 3-4 bytes in the beginning and 1 bytes FCS */
649 if (p_cfg->mtu_present)
650 p_mcb->peer_l2cap_mtu = p_cfg->mtu - RFCOMM_MIN_OFFSET - 1;
652 p_mcb->peer_l2cap_mtu = L2CAP_DEFAULT_MTU - RFCOMM_MIN_OFFSET - 1;
654 p_cfg->mtu_present = FALSE;
655 p_cfg->flush_to_present = FALSE;
656 p_cfg->qos_present = FALSE;
658 p_cfg->result = L2CAP_CFG_OK;
660 L2CA_ConfigRsp (p_mcb->lcid, p_cfg);
662 p_mcb->peer_cfg_rcvd = TRUE;
663 if ((p_mcb->state == RFC_MX_STATE_CONFIGURE) && p_mcb->local_cfg_sent)
665 if (p_mcb->is_initiator)
667 p_mcb->state = RFC_MX_STATE_SABME_WAIT_UA;
668 rfc_send_sabme (p_mcb, RFCOMM_MX_DLCI);
669 rfc_timer_start (p_mcb, RFC_T1_TIMEOUT);
673 p_mcb->state = RFC_MX_STATE_WAIT_SABME;
674 rfc_timer_start (p_mcb, RFCOMM_CONN_TIMEOUT); /* - increased from T2=20 to CONN=120
675 to allow the user more than 10 sec to type in the
676 pin which can be e.g. 16 digits */