/******************************************************************************* ** ** Function rfc_mx_sm_state_idle ** ** Description This function handles events when the multiplexer is in ** IDLE state. This state exists when connection is being ** initially established. ** ** Returns void ** *******************************************************************************/ void rfc_mx_sm_state_idle (tRFC_MCB *p_mcb, UINT16 event, void *p_data) { RFCOMM_TRACE_EVENT1 ("rfc_mx_sm_state_idle - evt:%d", event); switch (event) { case RFC_MX_EVENT_START_REQ: /* Initialize L2CAP MTU */ p_mcb->peer_l2cap_mtu = L2CAP_DEFAULT_MTU - RFCOMM_MIN_OFFSET - 1; if ((p_mcb->lcid = L2CA_ConnectReq (BT_PSM_RFCOMM, p_mcb->bd_addr)) == 0) { PORT_StartCnf (p_mcb, RFCOMM_ERROR); return; } /* Save entry for quicker access to mcb based on the LCID */ rfc_save_lcid_mcb (p_mcb, p_mcb->lcid); p_mcb->state = RFC_MX_STATE_WAIT_CONN_CNF; return; case RFC_MX_EVENT_START_RSP: case RFC_MX_EVENT_CONN_CNF: case RFC_MX_EVENT_CONF_IND: case RFC_MX_EVENT_CONF_CNF: RFCOMM_TRACE_ERROR2 ("Mx error state %d event %d", p_mcb->state, event); return; case RFC_MX_EVENT_CONN_IND: rfc_timer_start (p_mcb, RFCOMM_CONN_TIMEOUT); L2CA_ConnectRsp (p_mcb->bd_addr, *((UINT8 *)p_data), p_mcb->lcid, L2CAP_CONN_OK, 0); rfc_mx_send_config_req (p_mcb); p_mcb->state = RFC_MX_STATE_CONFIGURE; return; case RFC_EVENT_SABME: break; case RFC_EVENT_UA: case RFC_EVENT_DM: return; case RFC_EVENT_DISC: rfc_send_dm (p_mcb, RFCOMM_MX_DLCI, TRUE); return; case RFC_EVENT_UIH: rfc_send_dm (p_mcb, RFCOMM_MX_DLCI, FALSE); return; } RFCOMM_TRACE_EVENT2 ("RFCOMM MX ignored - evt:%d in state:%d", event, p_mcb->state); }
/******************************************************************************* ** ** Function RFCOMM_ConnectInd ** ** Description This is a callback function called by L2CAP when ** L2CA_ConnectInd received. Allocate multiplexer control block ** and dispatch the event to it. ** *******************************************************************************/ void RFCOMM_ConnectInd (BD_ADDR bd_addr, UINT16 lcid, UINT16 psm, UINT8 id) { tRFC_MCB *p_mcb = rfc_alloc_multiplexer_channel(bd_addr, FALSE); UNUSED(psm); if ((p_mcb)&&(p_mcb->state != RFC_MX_STATE_IDLE)) { /* if this is collision case */ if ((p_mcb->is_initiator)&&(p_mcb->state == RFC_MX_STATE_WAIT_CONN_CNF)) { p_mcb->pending_lcid = lcid; p_mcb->pending_id = id; /* wait random timeout (2 - 12) to resolve collision */ /* if peer gives up then local device rejects incoming connection and continues as initiator */ /* if timeout, local device disconnects outgoing connection and continues as acceptor */ RFCOMM_TRACE_DEBUG ("RFCOMM_ConnectInd start timer for collision, initiator's LCID(0x%x), acceptor's LCID(0x%x)", p_mcb->lcid, p_mcb->pending_lcid); rfc_timer_start(p_mcb, (UINT16)(GKI_get_tick_count()%10 + 2)); return; } else { /* we cannot accept connection request from peer at this state */ /* don't update lcid */ p_mcb = NULL; } } else { /* store mcb even if null */ rfc_save_lcid_mcb (p_mcb, lcid); } if (p_mcb == NULL) { L2CA_ConnectRsp (bd_addr, id, lcid, L2CAP_CONN_NO_RESOURCES, 0); return; } p_mcb->lcid = lcid; rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONN_IND, &id); }
/******************************************************************************* ** ** Function RFCOMM_ConnectCnf ** ** Description This is a callback function called by L2CAP when ** L2CA_ConnectCnf received. Save L2CAP handle and dispatch ** event to the FSM. ** *******************************************************************************/ void RFCOMM_ConnectCnf (UINT16 lcid, UINT16 result) { tRFC_MCB *p_mcb = rfc_find_lcid_mcb (lcid); if (!p_mcb) { RFCOMM_TRACE_ERROR ("RFCOMM_ConnectCnf LCID:0x%x", lcid); return; } if (p_mcb->pending_lcid) { /* if peer rejects our connect request but peer's connect request is pending */ if (result != L2CAP_CONN_OK ) { UINT16 i; UINT8 idx; RFCOMM_TRACE_DEBUG ("RFCOMM_ConnectCnf retry as acceptor on pending LCID(0x%x)", p_mcb->pending_lcid); /* remove mcb from mapping table */ rfc_save_lcid_mcb (NULL, p_mcb->lcid); p_mcb->lcid = p_mcb->pending_lcid; p_mcb->is_initiator = FALSE; p_mcb->state = RFC_MX_STATE_IDLE; /* store mcb into mapping table */ rfc_save_lcid_mcb (p_mcb, p_mcb->lcid); /* update direction bit */ for (i = 0; i < RFCOMM_MAX_DLCI; i += 2) { if ((idx = p_mcb->port_inx[i]) != 0) { p_mcb->port_inx[i] = 0; p_mcb->port_inx[i+1] = idx; rfc_cb.port.port[idx - 1].dlci += 1; RFCOMM_TRACE_DEBUG ("RFCOMM MX - DLCI:%d -> %d", i, rfc_cb.port.port[idx - 1].dlci); } } rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONN_IND, &(p_mcb->pending_id)); return; } else { RFCOMM_TRACE_DEBUG ("RFCOMM_ConnectCnf peer gave up pending LCID(0x%x)", p_mcb->pending_lcid); /* Peer gave up his connection request, make sure cleaning up L2CAP channel */ L2CA_ConnectRsp (p_mcb->bd_addr, p_mcb->pending_id, p_mcb->pending_lcid, L2CAP_CONN_NO_RESOURCES, 0); p_mcb->pending_lcid = 0; } } /* Save LCID to be used in all consecutive calls to L2CAP */ p_mcb->lcid = lcid; rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONN_CNF, &result); }
/******************************************************************************* ** ** Function rfc_mx_sm_state_disc_wait_ua ** ** Description This function handles events when the multiplexer sent ** DISC and is waiting for UA reply. ** ** Returns void ** *******************************************************************************/ void rfc_mx_sm_state_disc_wait_ua (tRFC_MCB *p_mcb, UINT16 event, void *p_data) { BT_HDR *p_buf; RFCOMM_TRACE_EVENT1 ("rfc_mx_sm_state_disc_wait_ua - evt:%d", event); switch (event) { case RFC_EVENT_UA: case RFC_EVENT_DM: case RFC_EVENT_TIMEOUT: L2CA_DisconnectReq (p_mcb->lcid); if (p_mcb->restart_required) { /* Start Request was received while disconnecting. Execute it again */ if ((p_mcb->lcid = L2CA_ConnectReq (BT_PSM_RFCOMM, p_mcb->bd_addr)) == 0) { PORT_StartCnf (p_mcb, RFCOMM_ERROR); return; } /* Save entry for quicker access to mcb based on the LCID */ rfc_save_lcid_mcb (p_mcb, p_mcb->lcid); /* clean up before reuse it */ while ((p_buf = (BT_HDR *)GKI_dequeue(&p_mcb->cmd_q)) != NULL) GKI_freebuf(p_buf); rfc_timer_start (p_mcb, RFC_MCB_INIT_INACT_TIMER); p_mcb->is_initiator = TRUE; p_mcb->restart_required = FALSE; p_mcb->local_cfg_sent = FALSE; p_mcb->peer_cfg_rcvd = FALSE; p_mcb->state = RFC_MX_STATE_WAIT_CONN_CNF; return; } rfc_release_multiplexer_channel (p_mcb); return; case RFC_EVENT_DISC: rfc_send_ua (p_mcb, RFCOMM_MX_DLCI); return; case RFC_EVENT_UIH: GKI_freebuf (p_data); rfc_send_dm (p_mcb, RFCOMM_MX_DLCI, FALSE); return; case RFC_MX_EVENT_START_REQ: p_mcb->restart_required = TRUE; return; case RFC_MX_EVENT_DISC_IND: p_mcb->state = RFC_MX_STATE_IDLE; PORT_CloseInd (p_mcb); return; case RFC_MX_EVENT_CLOSE_REQ: return; case RFC_MX_EVENT_QOS_VIOLATION_IND: break; } RFCOMM_TRACE_EVENT2 ("RFCOMM MX ignored - evt:%d in state:%d", event, p_mcb->state); }
/******************************************************************************* ** ** Function rfc_mx_sm_state_wait_conn_cnf ** ** Description This function handles events when the multiplexer is ** waiting for Connection Confirm from L2CAP. ** ** Returns void ** *******************************************************************************/ void rfc_mx_sm_state_wait_conn_cnf (tRFC_MCB *p_mcb, UINT16 event, void *p_data) { RFCOMM_TRACE_EVENT1 ("rfc_mx_sm_state_wait_conn_cnf - evt:%d", event); switch (event) { case RFC_MX_EVENT_START_REQ: RFCOMM_TRACE_ERROR2 ("Mx error state %d event %d", p_mcb->state, event); return; /* There is some new timing so that Config Ind comes before security is completed so we are still waiting fo the confirmation. */ case RFC_MX_EVENT_CONF_IND: rfc_mx_conf_ind (p_mcb, (tL2CAP_CFG_INFO *)p_data); return; case RFC_MX_EVENT_CONN_CNF: if (*((UINT16 *)p_data) != L2CAP_SUCCESS) { p_mcb->state = RFC_MX_STATE_IDLE; PORT_StartCnf (p_mcb, *((UINT16 *)p_data)); return; } p_mcb->state = RFC_MX_STATE_CONFIGURE; rfc_mx_send_config_req (p_mcb); return; case RFC_MX_EVENT_DISC_IND: p_mcb->state = RFC_MX_STATE_IDLE; PORT_CloseInd (p_mcb); return; case RFC_EVENT_TIMEOUT: p_mcb->state = RFC_MX_STATE_IDLE; L2CA_DisconnectReq (p_mcb->lcid); /* we gave up outgoing connection request then try peer's request */ if (p_mcb->pending_lcid) { UINT16 i; UINT8 idx; RFCOMM_TRACE_DEBUG2 ("RFCOMM MX retry as acceptor in collision case - evt:%d in state:%d", event, p_mcb->state); rfc_save_lcid_mcb (NULL, p_mcb->lcid); p_mcb->lcid = p_mcb->pending_lcid; rfc_save_lcid_mcb (p_mcb, p_mcb->lcid); p_mcb->is_initiator = FALSE; /* update direction bit */ for (i = 0; i < RFCOMM_MAX_DLCI; i += 2) { if ((idx = p_mcb->port_inx[i]) != 0) { p_mcb->port_inx[i] = 0; p_mcb->port_inx[i+1] = idx; rfc_cb.port.port[idx - 1].dlci += 1; RFCOMM_TRACE_DEBUG2 ("RFCOMM MX - DLCI:%d -> %d", i, rfc_cb.port.port[idx - 1].dlci); } } rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CONN_IND, &(p_mcb->pending_id)); } else { PORT_CloseInd (p_mcb); } return; } RFCOMM_TRACE_EVENT2 ("RFCOMM MX ignored - evt:%d in state:%d", event, p_mcb->state); }