/******************************************************************************* ** ** Function PORT_PortNegCnf ** ** Description This function is called from the RFCOMM layer to change ** state for the port. Propagate change to the user. ** *******************************************************************************/ void PORT_PortNegCnf (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_STATE *p_pars, UINT16 result) { tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci); UNUSED(p_pars); RFCOMM_TRACE_EVENT ("PORT_PortNegCnf"); if (!p_port) { RFCOMM_TRACE_WARNING ("PORT_PortNegCnf no port"); return; } /* Port negotiation failed. Drop the connection */ if (result != RFCOMM_SUCCESS) { p_port->error = PORT_PORT_NEG_FAILED; RFCOMM_DlcReleaseReq (p_mcb, p_port->dlci); port_rfc_closed (p_port, PORT_PORT_NEG_FAILED); return; } if (!(p_port->port_ctrl & PORT_CTRL_REQ_SENT)) { RFCOMM_ControlReq (p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl); } else { RFCOMM_TRACE_WARNING ("PORT_PortNegCnf Control Already sent"); } }
/******************************************************************************* ** ** Function PORT_ControlCnf ** ** Description This function is called from the RFCOMM layer when ** peer acknowleges change of the modem signals. ** *******************************************************************************/ void PORT_ControlCnf (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_CTRL *p_pars) { tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci); UINT32 event = 0; UNUSED(p_pars); RFCOMM_TRACE_EVENT ("PORT_ControlCnf"); if (!p_port) { return; } if (!(p_port->port_ctrl & PORT_CTRL_REQ_CONFIRMED)) { p_port->port_ctrl |= PORT_CTRL_REQ_CONFIRMED; if (p_port->port_ctrl & PORT_CTRL_IND_RECEIVED) { event = (p_port->ev_mask & PORT_EV_CONNECTED); } } if (p_port->port_ctrl & PORT_CTRL_IND_RECEIVED) { event |= port_rfc_send_tx_data(p_port); } /* execute call back function only if the application is registered for events */ if (event && p_port->p_callback) { (p_port->p_callback)(event, p_port->inx); } }
/******************************************************************************* ** ** Function PORT_LineStatusInd ** ** Description This function is called from the RFCOMM layer when ** peer indicates change in the line status ** *******************************************************************************/ void PORT_LineStatusInd (tRFC_MCB *p_mcb, UINT8 dlci, UINT8 line_status) { tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci); UINT32 event = 0; RFCOMM_TRACE_EVENT ("PORT_LineStatusInd"); if (!p_port) { return; } p_port->line_status |= line_status; if (line_status & PORT_ERR_OVERRUN) { event |= PORT_EV_OVERRUN; } if (line_status & PORT_ERR_BREAK) { event |= PORT_EV_BREAK; } if (line_status & ~(PORT_ERR_OVERRUN | PORT_ERR_BREAK)) { event |= PORT_EV_ERR; } if ((p_port->p_callback != NULL) && (p_port->ev_mask & event)) { p_port->p_callback ((p_port->ev_mask & event), p_port->inx); } }
/******************************************************************************* ** ** Function rfc_process_pn ** ** Description This function handles DLC parameter negotiation frame. ** Record MTU and pass indication to the upper layer. ** *******************************************************************************/ void rfc_process_pn (tRFC_MCB *p_mcb, BOOLEAN is_command, MX_FRAME *p_frame) { tPORT *p_port; UINT8 dlci = p_frame->dlci; if (is_command) { /* Ignore if Multiplexer is being shut down */ if (p_mcb->state != RFC_MX_STATE_DISC_WAIT_UA) { PORT_ParNegInd (p_mcb, dlci, p_frame->u.pn.mtu, p_frame->u.pn.conv_layer, p_frame->u.pn.k); } else { rfc_send_dm(p_mcb, dlci, FALSE); RFCOMM_TRACE_WARNING("***** MX PN while disconnecting *****"); } return; } /* If we are not awaiting response just ignore it */ p_port = port_find_mcb_dlci_port (p_mcb, dlci); if ((p_port == NULL) || !(p_port->rfc.expected_rsp & RFC_RSP_PN)) { return; } p_port->rfc.expected_rsp &= ~RFC_RSP_PN; rfc_port_timer_stop (p_port); PORT_ParNegCnf (p_mcb, dlci, p_frame->u.pn.mtu, p_frame->u.pn.conv_layer, p_frame->u.pn.k); }
/******************************************************************************* ** ** Function RFCOMM_PortNegReq ** ** Description This function is called by the user app to start ** Remote Port parameter negotiation. Port emulation can ** send this request before actually establishing the DLC. ** In this case the function will allocate RFCOMM connection ** control block. ** *******************************************************************************/ void RFCOMM_PortNegReq (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_STATE *p_pars) { if (p_mcb->state != RFC_MX_STATE_CONNECTED) { PORT_PortNegCnf (p_mcb, dlci, NULL, RFCOMM_ERROR); return; } tPORT *p_port = port_find_mcb_dlci_port(p_mcb, dlci); if (p_port == NULL) { RFCOMM_TRACE_WARNING("%s Unable to find DLCI port dlci:%d", __func__, dlci); return; } /* Send Parameter Negotiation Command UIH frame */ if (!p_pars) p_port->rfc.expected_rsp |= RFC_RSP_RPN_REPLY; else p_port->rfc.expected_rsp |= RFC_RSP_RPN; rfc_send_rpn (p_mcb, dlci, TRUE, p_pars, RFCOMM_RPN_PM_MASK); rfc_port_timer_start (p_port, RFC_T2_TIMEOUT) ; }
/******************************************************************************* ** ** Function PORT_DlcReleaseInd ** ** Description This function is called from the RFCOMM layer when ** DLC connection is released. ** *******************************************************************************/ void PORT_DlcReleaseInd (tRFC_MCB *p_mcb, UINT8 dlci) { tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci); RFCOMM_TRACE_EVENT ("PORT_DlcReleaseInd"); if (!p_port) return; port_rfc_closed (p_port, PORT_CLOSED); }
/******************************************************************************* ** ** Function RFCOMM_DlcEstablishRsp ** ** Description This function is called by the port emulation entity ** acks Establish Indication. ** *******************************************************************************/ void RFCOMM_DlcEstablishRsp (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu, UINT16 result) { tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci); if ((p_mcb->state != RFC_MX_STATE_CONNECTED) && (result == RFCOMM_SUCCESS)) { PORT_DlcReleaseInd (p_mcb, dlci); return; } rfc_port_sm_execute(p_port, RFC_EVENT_ESTABLISH_RSP, &result); }
/******************************************************************************* ** ** Function RFCOMM_DlcEstablishReq ** ** Description This function is called by the user app to establish ** connection with the specific dlci on a specific bd device. ** It will allocate RFCOMM connection control block if not ** allocated before and dispatch open event to the state ** machine. ** *******************************************************************************/ void RFCOMM_DlcEstablishReq (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu) { tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci); if (p_mcb->state != RFC_MX_STATE_CONNECTED) { PORT_DlcEstablishCnf (p_mcb, dlci, 0, RFCOMM_ERROR); return; } rfc_port_sm_execute(p_port, RFC_EVENT_OPEN, NULL); }
/******************************************************************************* ** ** Function RFCOMM_LineStatusReq ** ** Description This function is called by the port entity when line ** status should be delivered to the peer. ** *******************************************************************************/ void RFCOMM_LineStatusReq (tRFC_MCB *p_mcb, UINT8 dlci, UINT8 status) { tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci); if ((p_port->state != PORT_STATE_OPENED) || (p_port->rfc.state != RFC_STATE_OPENED)) return; p_port->rfc.expected_rsp |= RFC_RSP_RLS; rfc_send_rls (p_mcb, dlci, TRUE, status); rfc_port_timer_start (p_port, RFC_T2_TIMEOUT); }
/******************************************************************************* ** ** Function PORT_FlowInd ** ** Description This function is called from the RFCOMM layer on the flow ** control signal change. Propagate change to the user. ** *******************************************************************************/ void PORT_FlowInd (tRFC_MCB *p_mcb, UINT8 dlci, BOOLEAN enable_data) { tPORT *p_port = (tPORT *)NULL; UINT32 events = 0; int i; RFCOMM_TRACE_EVENT ("PORT_FlowInd fc:%d", enable_data); if (dlci == 0) { p_mcb->peer_ready = enable_data; } else { if ((p_port = port_find_mcb_dlci_port (p_mcb, dlci)) == NULL) return; p_port->tx.peer_fc = !enable_data; } for (i = 0; i < MAX_RFC_PORTS; i++) { /* If DLCI is 0 event applies to all ports */ if (dlci == 0) { p_port = &rfc_cb.port.port[i]; if (!p_port->in_use || (p_port->rfc.p_mcb != p_mcb) || (p_port->rfc.state != RFC_STATE_OPENED)) continue; } events = 0; /* Check if flow of data is still enabled */ events |= port_flow_control_user (p_port); /* Check if data can be sent and send it */ events |= port_rfc_send_tx_data (p_port); /* Mask out all events that are not of interest to user */ events &= p_port->ev_mask; /* Send event to the application */ if (p_port->p_callback && events) (p_port->p_callback)(events, p_port->inx); /* If DLCI is not 0 event applies to one port only */ if (dlci != 0) break; } }
/******************************************************************************* ** ** Function PORT_ControlInd ** ** Description This function is called from the RFCOMM layer on the modem ** signal change. Propagate change to the user. ** *******************************************************************************/ void PORT_ControlInd (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_CTRL *p_pars) { tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci); UINT32 event; UINT8 old_signals; RFCOMM_TRACE_EVENT ("PORT_ControlInd"); if (!p_port) { return; } old_signals = p_port->peer_ctrl.modem_signal; event = port_get_signal_changes (p_port, old_signals, p_pars->modem_signal); p_port->peer_ctrl = *p_pars; if (!(p_port->port_ctrl & PORT_CTRL_REQ_SENT)) { RFCOMM_ControlReq (p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl); } else { /* If this is the first time we received control RFCOMM is connected */ if (!(p_port->port_ctrl & PORT_CTRL_IND_RECEIVED)) { event |= (PORT_EV_CONNECTED & p_port->ev_mask); } if (p_port->port_ctrl & PORT_CTRL_REQ_CONFIRMED) { event |= port_rfc_send_tx_data(p_port); } } p_port->port_ctrl |= (PORT_CTRL_IND_RECEIVED | PORT_CTRL_IND_RESPONDED); if (p_pars->break_signal) { event |= (PORT_EV_BREAK & p_port->ev_mask); } /* execute call back function only if the application is registered for events */ if (event && p_port->p_callback) { (p_port->p_callback)(event, p_port->inx); } RFCOMM_TRACE_EVENT ("PORT_ControlInd DTR_DSR : %d, RTS_CTS : %d, RI : %d, DCD : %d", ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_DTRDSR) ? 1 : 0), ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_RTSCTS) ? 1 : 0), ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_RI) ? 1 : 0), ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_DCD) ? 1 : 0)); }
/******************************************************************************* ** ** Function RFCOMM_ControlReq ** ** Description This function is called by the port entity to send control ** parameters to remote port emulation entity. ** *******************************************************************************/ void RFCOMM_ControlReq (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_CTRL *p_pars) { tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci); if ((p_port->state != PORT_STATE_OPENED) || (p_port->rfc.state != RFC_STATE_OPENED)) return; p_port->port_ctrl |= PORT_CTRL_REQ_SENT; p_port->rfc.expected_rsp |= RFC_RSP_MSC; rfc_send_msc (p_mcb, dlci, TRUE, p_pars); rfc_port_timer_start (p_port, RFC_T2_TIMEOUT) ; }
/******************************************************************************* ** ** Function RFCOMM_FlowReq ** ** Description This function is called by the port entity when flow ** control state has changed. Enable flag passed shows if ** port can accept more data. ** *******************************************************************************/ void RFCOMM_FlowReq (tRFC_MCB *p_mcb, UINT8 dlci, UINT8 enable) { tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci); if ((p_port->state != PORT_STATE_OPENED) || (p_port->rfc.state != RFC_STATE_OPENED)) return; p_port->local_ctrl.fc = !enable; p_port->rfc.expected_rsp |= RFC_RSP_MSC; rfc_send_msc (p_mcb, dlci, TRUE, &p_port->local_ctrl); rfc_port_timer_start (p_port, RFC_T2_TIMEOUT) ; }
/******************************************************************************* ** ** Function RFCOMM_DlcEstablishRsp ** ** Description This function is called by the port emulation entity ** acks Establish Indication. ** *******************************************************************************/ void RFCOMM_DlcEstablishRsp (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu, UINT16 result) { UNUSED(mtu); if ((p_mcb->state != RFC_MX_STATE_CONNECTED) && (result == RFCOMM_SUCCESS)) { PORT_DlcReleaseInd (p_mcb, dlci); return; } tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci); if (p_port == NULL) { RFCOMM_TRACE_WARNING("%s Unable to find DLCI port dlci:%d", __func__, dlci); return; } rfc_port_sm_execute(p_port, RFC_EVENT_ESTABLISH_RSP, &result); }
/******************************************************************************* ** ** Function PORT_ParNegCnf ** ** Description This function is called from the RFCOMM layer to change ** DLCI parameters (currently only MTU is negotiated). ** Save the MTU size supported by the peer. ** If the confirmation is received during the port opening ** procedure send EstablishRequest to continue. ** *******************************************************************************/ void PORT_ParNegCnf (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu, UINT8 cl, UINT8 k) { tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci); RFCOMM_TRACE_EVENT ("PORT_ParNegCnf dlci:%d mtu:%d cl: %d k: %d", dlci, mtu, cl, k); if (!p_port) return; /* Flow control mechanism not set yet. Negotiate flow control mechanism. */ if (p_mcb->flow == PORT_FC_UNDEFINED) { /* Our stack is configured for TS07.10 and they responded with credit-based. */ /* This is illegal-- negotiation fails. */ if ((PORT_FC_DEFAULT == PORT_FC_TS710) && (cl == RFCOMM_PN_CONV_LAYER_CBFC_R)) { rfc_send_disc (p_mcb, p_port->dlci); rfc_port_closed (p_port); return; } /* Our stack is configured for credit-based and they responded with credit-based. */ else if (cl == RFCOMM_PN_CONV_LAYER_CBFC_R) { p_mcb->flow = PORT_FC_CREDIT; } /* They responded with any other value. Treat this as negotiation to TS07.10. */ else { p_mcb->flow = PORT_FC_TS710; } } /* If mux flow control mechanism set, we honor that setting regardless of */ /* the CL value in their response. This allows us to gracefully accept any */ /* illegal PN negotiation scenarios. */ p_port->mtu = (p_port->mtu < mtu) ? p_port->mtu : mtu; p_port->peer_mtu = p_port->mtu; if (p_mcb->flow == PORT_FC_CREDIT) { port_get_credits (p_port, k); } if (p_port->state == PORT_STATE_OPENING) RFCOMM_DlcEstablishReq (p_mcb, p_port->dlci, p_port->mtu); }
/******************************************************************************* ** ** Function RFCOMM_DlcEstablishReq ** ** Description This function is called by the user app to establish ** connection with the specific dlci on a specific bd device. ** It will allocate RFCOMM connection control block if not ** allocated before and dispatch open event to the state ** machine. ** *******************************************************************************/ void RFCOMM_DlcEstablishReq (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu) { UNUSED(mtu); if (p_mcb->state != RFC_MX_STATE_CONNECTED) { PORT_DlcEstablishCnf (p_mcb, dlci, 0, RFCOMM_ERROR); return; } tPORT *p_port = port_find_mcb_dlci_port(p_mcb, dlci); if (p_port == NULL) { RFCOMM_TRACE_WARNING("%s Unable to find DLCI port dlci:%d", __func__, dlci); return; } rfc_port_sm_execute(p_port, RFC_EVENT_OPEN, NULL); }
/******************************************************************************* ** ** Function RFCOMM_LineStatusReq ** ** Description This function is called by the port entity when line ** status should be delivered to the peer. ** *******************************************************************************/ void RFCOMM_LineStatusReq (tRFC_MCB *p_mcb, UINT8 dlci, UINT8 status) { tPORT *p_port = port_find_mcb_dlci_port(p_mcb, dlci); if (p_port == NULL) { RFCOMM_TRACE_WARNING("%s Unable to find DLCI port dlci:%d", __func__, dlci); return; } if ((p_port->state != PORT_STATE_OPENED) || (p_port->rfc.state != RFC_STATE_OPENED)) return; p_port->rfc.expected_rsp |= RFC_RSP_RLS; rfc_send_rls (p_mcb, dlci, TRUE, status); rfc_port_timer_start (p_port, RFC_T2_TIMEOUT); }
/******************************************************************************* ** ** Function RFCOMM_ParNegReq ** ** Description This function is called by the user app to start ** DLC parameter negotiation. Port emulation can send this ** request before actually establishing the DLC. In this ** case the function will allocate RFCOMM connection control ** block. ** *******************************************************************************/ void RFCOMM_ParNegReq (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu) { UINT8 flow; UINT8 cl; UINT8 k; tPORT *p_port = port_find_mcb_dlci_port(p_mcb, dlci); if (p_port == NULL) { RFCOMM_TRACE_WARNING("%s Unable to find DLCI port dlci:%d", __func__, dlci); return; } if (p_mcb->state != RFC_MX_STATE_CONNECTED) { p_port->error = PORT_PAR_NEG_FAILED; return; } /* Negotiate the flow control mechanism. If flow control mechanism for */ /* mux has not been set yet, use our default value. If it has been set, */ /* use that value. */ flow = (p_mcb->flow == PORT_FC_UNDEFINED) ? PORT_FC_DEFAULT : p_mcb->flow; /* Set convergence layer and number of credits (k) */ if (flow == PORT_FC_CREDIT) { cl = RFCOMM_PN_CONV_LAYER_CBFC_I; k = (p_port->credit_rx_max < RFCOMM_K_MAX) ? p_port->credit_rx_max : RFCOMM_K_MAX; p_port->credit_rx = k; } else { cl = RFCOMM_PN_CONV_LAYER_TYPE_1; k = 0; } /* Send Parameter Negotiation Command UIH frame */ p_port->rfc.expected_rsp |= RFC_RSP_PN; rfc_send_pn (p_mcb, dlci, TRUE, mtu, cl, k); rfc_port_timer_start (p_port, RFC_T2_TIMEOUT) ; }
/******************************************************************************* ** ** Function PORT_DlcEstablishCnf ** ** Description This function is called from the RFCOMM layer when peer ** acknowledges establish procedure (SABME/UA). Send reply ** to the user and set state to OPENED if result was ** successfull. ** *******************************************************************************/ void PORT_DlcEstablishCnf (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu, UINT16 result) { tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci); RFCOMM_TRACE_EVENT ("PORT_DlcEstablishCnf dlci:%d mtu:%d result:%d", dlci, mtu, result); if (!p_port) { return; } if (result != RFCOMM_SUCCESS) { p_port->error = PORT_START_FAILED; port_rfc_closed (p_port, PORT_START_FAILED); return; } /* If L2CAP's mtu less then RFCOMM's take it */ if (mtu && (mtu < p_port->peer_mtu)) { p_port->peer_mtu = mtu; } /* If there was an inactivity timer running for MCB stop it */ rfc_timer_stop (p_mcb); if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECTED)) { (p_port->p_callback)(PORT_EV_CONNECTED, p_port->inx); } if (p_port->p_mgmt_callback) { p_port->p_mgmt_callback (PORT_SUCCESS, p_port->inx); } p_port->state = PORT_STATE_OPENED; /* RPN is required only if we want to tell DTE how the port should be opened */ if ((p_port->uuid == UUID_SERVCLASS_DIALUP_NETWORKING) || (p_port->uuid == UUID_SERVCLASS_FAX)) { RFCOMM_PortNegReq (p_port->rfc.p_mcb, p_port->dlci, NULL); } else { RFCOMM_ControlReq (p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl); } }
/******************************************************************************* ** ** Function rfc_process_rls ** ** Description This function handles Remote Line Status command. ** Pass command to the user. ** *******************************************************************************/ void rfc_process_rls (tRFC_MCB *p_mcb, BOOLEAN is_command, MX_FRAME *p_frame) { tPORT *p_port; if (is_command) { PORT_LineStatusInd (p_mcb, p_frame->dlci, p_frame->u.rls.line_status); rfc_send_rls (p_mcb, p_frame->dlci, FALSE, p_frame->u.rls.line_status); } else { p_port = port_find_mcb_dlci_port (p_mcb, p_frame->dlci); /* If we are not awaiting response just ignore it */ if (!p_port || !(p_port->rfc.expected_rsp & RFC_RSP_RLS)) { return; } p_port->rfc.expected_rsp &= ~RFC_RSP_RLS; rfc_port_timer_stop (p_port); } }
/******************************************************************************* ** ** Function PORT_DlcEstablishInd ** ** Description This function is called from the RFCOMM layer when peer ** device wants to establish a new DLC. If this is not the ** first message in the establishment procedure port_handle ** has a handle to the port control block otherwise the control ** block should be found based on the muliplexer channel and ** dlci. The block should be allocated allocated before ** meaning that application already made open. ** *******************************************************************************/ void PORT_DlcEstablishInd (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu) { tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci); RFCOMM_TRACE_DEBUG ("PORT_DlcEstablishInd p_mcb:%p, dlci:%d mtu:%di, p_port:%p", p_mcb, dlci, mtu, p_port); RFCOMM_TRACE_DEBUG ("PORT_DlcEstablishInd p_mcb addr:%02x:%02x:%02x:%02x:%02x:%02x", p_mcb->bd_addr[0], p_mcb->bd_addr[1], p_mcb->bd_addr[2], p_mcb->bd_addr[3], p_mcb->bd_addr[4], p_mcb->bd_addr[5]); if (!p_port) { /* This can be a first request for this port */ p_port = port_find_dlci_port (dlci); if (!p_port) { RFCOMM_DlcEstablishRsp (p_mcb, dlci, 0, RFCOMM_ERROR); return; } p_mcb->port_inx[dlci] = p_port->inx; } /* If L2CAP's mtu less then RFCOMM's take it */ if (mtu && (mtu < p_port->peer_mtu)) { p_port->peer_mtu = mtu; } /* If there was an inactivity timer running for MCB stop it */ rfc_timer_stop (p_mcb); RFCOMM_DlcEstablishRsp (p_mcb, dlci, p_port->mtu, RFCOMM_SUCCESS); /* This is the server side. If application wants to know when connection */ /* is established, thats the place */ if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECTED)) { (p_port->p_callback)(PORT_EV_CONNECTED, p_port->inx); } if (p_port->p_mgmt_callback) { p_port->p_mgmt_callback (PORT_SUCCESS, p_port->inx); } p_port->state = PORT_STATE_OPENED; }
/******************************************************************************* ** ** Function RFCOMM_ControlReq ** ** Description This function is called by the port entity to send control ** parameters to remote port emulation entity. ** *******************************************************************************/ void RFCOMM_ControlReq (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_CTRL *p_pars) { tPORT *p_port = port_find_mcb_dlci_port(p_mcb, dlci); if (p_port == NULL) { RFCOMM_TRACE_WARNING("%s Unable to find DLCI port dlci:%d", __func__, dlci); return; } if ((p_port->state != PORT_STATE_OPENED) || (p_port->rfc.state != RFC_STATE_OPENED)) return; p_port->port_ctrl |= PORT_CTRL_REQ_SENT; p_port->rfc.expected_rsp |= RFC_RSP_MSC; rfc_send_msc (p_mcb, dlci, TRUE, p_pars); rfc_port_timer_start (p_port, RFC_T2_TIMEOUT) ; }
/******************************************************************************* ** ** Function RFCOMM_FlowReq ** ** Description This function is called by the port entity when flow ** control state has changed. Enable flag passed shows if ** port can accept more data. ** *******************************************************************************/ void RFCOMM_FlowReq (tRFC_MCB *p_mcb, UINT8 dlci, UINT8 enable) { tPORT *p_port = port_find_mcb_dlci_port(p_mcb, dlci); if (p_port == NULL) { RFCOMM_TRACE_WARNING("%s Unable to find DLCI port dlci:%d", __func__, dlci); return; } if ((p_port->state != PORT_STATE_OPENED) || (p_port->rfc.state != RFC_STATE_OPENED)) return; p_port->local_ctrl.fc = !enable; p_port->rfc.expected_rsp |= RFC_RSP_MSC; rfc_send_msc (p_mcb, dlci, TRUE, &p_port->local_ctrl); rfc_port_timer_start (p_port, RFC_T2_TIMEOUT) ; }
/******************************************************************************* ** ** Function PORT_PortNegInd ** ** Description This function is called from the RFCOMM layer when peer ** device wants to set parameters of the port. As per the spec ** this message has to be sent before the first data packet ** and can be sent before establish. The block should be ** allocated before meaning that application already made open. ** *******************************************************************************/ void PORT_PortNegInd (tRFC_MCB *p_mcb, UINT8 dlci, tPORT_STATE *p_pars, UINT16 param_mask) { tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci); RFCOMM_TRACE_EVENT ("PORT_PortNegInd"); if (!p_port) { /* This can be a first request for this port */ p_port = port_find_dlci_port (dlci); if (!p_port) { RFCOMM_PortNegRsp (p_mcb, dlci, p_pars, 0); return; } p_mcb->port_inx[dlci] = p_port->inx; } /* Check if the flow control is acceptable on local side */ p_port->peer_port_pars = *p_pars; RFCOMM_PortNegRsp (p_mcb, dlci, p_pars, param_mask); }
/******************************************************************************* ** ** Function RFCOMM_DataReq ** ** Description This function is called by the user app to send data buffer ** *******************************************************************************/ void RFCOMM_DataReq (tRFC_MCB *p_mcb, UINT8 dlci, BT_HDR *p_buf) { rfc_port_sm_execute(port_find_mcb_dlci_port (p_mcb, dlci), RFC_EVENT_DATA, p_buf); }
/******************************************************************************* ** ** Function RFCOMM_DlcReleaseReq ** ** Description This function is called by the PORT unit to close DLC ** *******************************************************************************/ void RFCOMM_DlcReleaseReq (tRFC_MCB *p_mcb, UINT8 dlci) { rfc_port_sm_execute(port_find_mcb_dlci_port (p_mcb, dlci), RFC_EVENT_CLOSE, 0); }
/******************************************************************************* ** ** Function PORT_DataInd ** ** Description This function is called from the RFCOMM layer when data ** buffer is received from the peer. ** *******************************************************************************/ void PORT_DataInd (tRFC_MCB *p_mcb, UINT8 dlci, BT_HDR *p_buf) { tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci); UINT8 rx_char1; UINT32 events = 0; UINT8 *p; int i; RFCOMM_TRACE_EVENT("PORT_DataInd with data length %d, p_mcb:%p,p_port:%p,dlci:%d", p_buf->len, p_mcb, p_port, dlci); if (!p_port) { osi_free (p_buf); return; } /* If client registered callout callback with flow control we can just deliver receive data */ if (p_port->p_data_co_callback) { /* Another packet is delivered to user. Send credits to peer if required */ if (p_port->p_data_co_callback(p_port->inx, (UINT8 *)p_buf, -1, DATA_CO_CALLBACK_TYPE_INCOMING)) { port_flow_control_peer(p_port, TRUE, 1); } else { port_flow_control_peer(p_port, FALSE, 0); } //osi_free (p_buf); return; } else { RFCOMM_TRACE_DEBUG("PORT_DataInd, p_port:%p, p_data_co_callback is null", p_port); } /* If client registered callback we can just deliver receive data */ if (p_port->p_data_callback) { /* Another packet is delivered to user. Send credits to peer if required */ port_flow_control_peer(p_port, TRUE, 1); p_port->p_data_callback (p_port->inx, (UINT8 *)(p_buf + 1) + p_buf->offset, p_buf->len); osi_free (p_buf); return; } /* Check if rx queue exceeds the limit */ if ((p_port->rx.queue_size + p_buf->len > PORT_RX_CRITICAL_WM) || (fixed_queue_length(p_port->rx.queue) + 1 > p_port->rx_buf_critical)) { RFCOMM_TRACE_EVENT ("PORT_DataInd. Buffer over run. Dropping the buffer"); osi_free (p_buf); RFCOMM_LineStatusReq (p_mcb, dlci, LINE_STATUS_OVERRUN); return; } /* If user registered to receive notification when a particular byte is */ /* received we mast check all received bytes */ if (((rx_char1 = p_port->user_port_pars.rx_char1) != 0) && (p_port->ev_mask & PORT_EV_RXFLAG)) { for (i = 0, p = (UINT8 *)(p_buf + 1) + p_buf->offset; i < p_buf->len; i++) { if (*p++ == rx_char1) { events |= PORT_EV_RXFLAG; break; } } } osi_mutex_global_lock(); fixed_queue_enqueue(p_port->rx.queue, p_buf); p_port->rx.queue_size += p_buf->len; osi_mutex_global_unlock(); /* perform flow control procedures if necessary */ port_flow_control_peer(p_port, FALSE, 0); /* If user indicated flow control can not deliver any notifications to him */ if (p_port->rx.user_fc) { if (events & PORT_EV_RXFLAG) { p_port->rx_flag_ev_pending = TRUE; } return; } events |= PORT_EV_RXCHAR; /* Mask out all events that are not of interest to user */ events &= p_port->ev_mask; if (p_port->p_callback && events) { p_port->p_callback (events, p_port->inx); } }
/******************************************************************************* ** ** Function rfc_process_rpn ** ** Description This function handles Remote DLC parameter negotiation ** command/response. Pass command to the user. ** *******************************************************************************/ void rfc_process_rpn (tRFC_MCB *p_mcb, BOOLEAN is_command, BOOLEAN is_request, MX_FRAME *p_frame) { tPORT_STATE port_pars; tPORT *p_port; if ((p_port = port_find_mcb_dlci_port (p_mcb, p_frame->dlci)) == NULL) { /* This is the first command on the port */ if (is_command) { memset(&port_pars, 0, sizeof(tPORT_STATE)); rfc_set_port_state(&port_pars, p_frame); PORT_PortNegInd(p_mcb, p_frame->dlci, &port_pars, p_frame->u.rpn.param_mask); } return; } if (is_command && is_request) { /* This is the special situation when peer just request local pars */ port_pars = p_port->peer_port_pars; rfc_send_rpn (p_mcb, p_frame->dlci, FALSE, &p_port->peer_port_pars, 0); return; } port_pars = p_port->peer_port_pars; rfc_set_port_state(&port_pars, p_frame); if (is_command) { PORT_PortNegInd (p_mcb, p_frame->dlci, &port_pars, p_frame->u.rpn.param_mask); return; } /* If we are not awaiting response just ignore it */ p_port = port_find_mcb_dlci_port (p_mcb, p_frame->dlci); if ((p_port == NULL) || !(p_port->rfc.expected_rsp & (RFC_RSP_RPN | RFC_RSP_RPN_REPLY))) { return; } /* If we sent a request for port parameters to the peer he is replying with */ /* mask 0. */ rfc_port_timer_stop (p_port); if (p_port->rfc.expected_rsp & RFC_RSP_RPN_REPLY) { p_port->rfc.expected_rsp &= ~RFC_RSP_RPN_REPLY; p_port->peer_port_pars = port_pars; if ((port_pars.fc_type == (RFCOMM_FC_RTR_ON_INPUT | RFCOMM_FC_RTR_ON_OUTPUT)) || (port_pars.fc_type == (RFCOMM_FC_RTC_ON_INPUT | RFCOMM_FC_RTC_ON_OUTPUT))) { /* This is satisfactory port parameters. Set mask as it was Ok */ p_frame->u.rpn.param_mask = RFCOMM_RPN_PM_MASK; } else { /* Current peer parameters are not good, try to fix them */ p_port->peer_port_pars.fc_type = (RFCOMM_FC_RTR_ON_INPUT | RFCOMM_FC_RTR_ON_OUTPUT); p_port->rfc.expected_rsp |= RFC_RSP_RPN; rfc_send_rpn (p_mcb, p_frame->dlci, TRUE, &p_port->peer_port_pars, RFCOMM_RPN_PM_RTR_ON_INPUT | RFCOMM_RPN_PM_RTR_ON_OUTPUT); rfc_port_timer_start (p_port, RFC_T2_TIMEOUT) ; return; } } else { p_port->rfc.expected_rsp &= ~RFC_RSP_RPN; } /* Check if all suggested parameters were accepted */ if (((p_frame->u.rpn.param_mask & (RFCOMM_RPN_PM_RTR_ON_INPUT | RFCOMM_RPN_PM_RTR_ON_OUTPUT)) == (RFCOMM_RPN_PM_RTR_ON_INPUT | RFCOMM_RPN_PM_RTR_ON_OUTPUT)) || ((p_frame->u.rpn.param_mask & (RFCOMM_RPN_PM_RTC_ON_INPUT | RFCOMM_RPN_PM_RTC_ON_OUTPUT)) == (RFCOMM_RPN_PM_RTC_ON_INPUT | RFCOMM_RPN_PM_RTC_ON_OUTPUT))) { PORT_PortNegCnf (p_mcb, p_port->dlci, &port_pars, RFCOMM_SUCCESS); return; } /* If we were proposing RTR flow control try RTC flow control */ /* If we were proposing RTC flow control try no flow control */ /* otherwise drop the connection */ if (p_port->peer_port_pars.fc_type == (RFCOMM_FC_RTR_ON_INPUT | RFCOMM_FC_RTR_ON_OUTPUT)) { /* Current peer parameters are not good, try to fix them */ p_port->peer_port_pars.fc_type = (RFCOMM_FC_RTC_ON_INPUT | RFCOMM_FC_RTC_ON_OUTPUT); p_port->rfc.expected_rsp |= RFC_RSP_RPN; rfc_send_rpn (p_mcb, p_frame->dlci, TRUE, &p_port->peer_port_pars, RFCOMM_RPN_PM_RTC_ON_INPUT | RFCOMM_RPN_PM_RTC_ON_OUTPUT); rfc_port_timer_start (p_port, RFC_T2_TIMEOUT) ; return; } /* Other side does not support flow control */ if (p_port->peer_port_pars.fc_type == (RFCOMM_FC_RTC_ON_INPUT | RFCOMM_FC_RTC_ON_OUTPUT)) { p_port->peer_port_pars.fc_type = RFCOMM_FC_OFF; PORT_PortNegCnf (p_mcb, p_port->dlci, &port_pars, RFCOMM_SUCCESS); } }
/******************************************************************************* ** ** Function rfc_process_msc ** ** Description This function handles Modem Status Command. ** Pass command to the user. ** *******************************************************************************/ void rfc_process_msc (tRFC_MCB *p_mcb, BOOLEAN is_command, MX_FRAME *p_frame) { tPORT_CTRL pars; tPORT *p_port; UINT8 modem_signals = p_frame->u.msc.signals; BOOLEAN new_peer_fc = FALSE; p_port = port_find_mcb_dlci_port (p_mcb, p_frame->dlci); if (p_port == NULL) { return; } pars.modem_signal = 0; if (modem_signals & RFCOMM_MSC_RTC) { pars.modem_signal |= MODEM_SIGNAL_DTRDSR; } if (modem_signals & RFCOMM_MSC_RTR) { pars.modem_signal |= MODEM_SIGNAL_RTSCTS; } if (modem_signals & RFCOMM_MSC_IC) { pars.modem_signal |= MODEM_SIGNAL_RI; } if (modem_signals & RFCOMM_MSC_DV) { pars.modem_signal |= MODEM_SIGNAL_DCD; } pars.fc = ((modem_signals & RFCOMM_MSC_FC) == RFCOMM_MSC_FC); pars.break_signal = (p_frame->u.msc.break_present) ? p_frame->u.msc.break_duration : 0; pars.discard_buffers = 0; pars.break_signal_seq = RFCOMM_CTRL_BREAK_IN_SEQ; /* this is default */ /* Check if this command is passed only to indicate flow control */ if (is_command) { rfc_send_msc (p_mcb, p_frame->dlci, FALSE, &pars); if (p_port->rfc.p_mcb->flow != PORT_FC_CREDIT) { /* Spec 1.1 indicates that only FC bit is used for flow control */ p_port->peer_ctrl.fc = new_peer_fc = pars.fc; if (new_peer_fc != p_port->tx.peer_fc) { PORT_FlowInd (p_mcb, p_frame->dlci, (BOOLEAN)!new_peer_fc); } } PORT_ControlInd (p_mcb, p_frame->dlci, &pars); return; } /* If we are not awaiting response just ignore it */ if (!(p_port->rfc.expected_rsp & RFC_RSP_MSC)) { return; } p_port->rfc.expected_rsp &= ~RFC_RSP_MSC; rfc_port_timer_stop (p_port); PORT_ControlCnf (p_port->rfc.p_mcb, p_port->dlci, &pars); }
/******************************************************************************* ** ** Function PORT_ParNegInd ** ** Description This function is called from the RFCOMM layer to change ** DLCI parameters (currently only MTU is negotiated). ** If can not find the port do not accept the request. ** Otherwise save the MTU size supported by the peer. ** *******************************************************************************/ void PORT_ParNegInd (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu, UINT8 cl, UINT8 k) { tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci); UINT8 our_cl; UINT8 our_k; RFCOMM_TRACE_EVENT ("PORT_ParNegInd dlci:%d mtu:%d", dlci, mtu); if (!p_port) { /* This can be a first request for this port */ p_port = port_find_dlci_port (dlci); if (!p_port) { /* If the port cannot be opened, send a DM. Per Errata 1205 */ rfc_send_dm(p_mcb, dlci, FALSE); /* check if this is the last port open, some headsets have problem, they don't disconnect if we send DM */ rfc_check_mcb_active( p_mcb ); RFCOMM_TRACE_EVENT( "PORT_ParNegInd: port not found" ); return; } p_mcb->port_inx[dlci] = p_port->inx; } memcpy (p_port->bd_addr, p_mcb->bd_addr, BD_ADDR_LEN); /* Connection is up and we know local and remote features, select MTU */ port_select_mtu (p_port); p_port->rfc.p_mcb = p_mcb; p_port->mtu = (p_port->mtu < mtu) ? p_port->mtu : mtu; p_port->peer_mtu = p_port->mtu; /* Negotiate the flow control mechanism. If flow control mechanism for */ /* mux has not been set yet, set it now. If either we or peer wants TS 07.10, */ /* use that. Otherwise both must want credit based, so use that. If flow is */ /* already defined for this mux, we respond with that value. */ if (p_mcb->flow == PORT_FC_UNDEFINED) { if ((PORT_FC_DEFAULT == PORT_FC_TS710) || (cl == RFCOMM_PN_CONV_LAYER_TYPE_1)) { p_mcb->flow = PORT_FC_TS710; } else { p_mcb->flow = PORT_FC_CREDIT; } } /* Regardless of our flow control mechanism, if the PN cl is zero, we must */ /* respond with zero. "A responding implementation must set this field to 14 */ /* if (and only if) the PN request was 15." This could happen if a PN is sent */ /* after the DLCI is already established-- the PN in that case must have cl = 0. */ /* See RFCOMM spec 5.5.3 */ if (cl == RFCOMM_PN_CONV_LAYER_TYPE_1) { our_cl = RFCOMM_PN_CONV_LAYER_TYPE_1; our_k = 0; } else if (p_mcb->flow == PORT_FC_CREDIT) { /* get credits */ port_get_credits (p_port, k); /* Set convergence layer and number of credits (k) */ our_cl = RFCOMM_PN_CONV_LAYER_CBFC_R; our_k = (p_port->credit_rx_max < RFCOMM_K_MAX) ? p_port->credit_rx_max : RFCOMM_K_MAX; p_port->credit_rx = our_k; } else { /* must not be using credit based flow control; use TS 7.10 */ our_cl = RFCOMM_PN_CONV_LAYER_TYPE_1; our_k = 0; } RFCOMM_ParNegRsp (p_mcb, dlci, p_port->mtu, our_cl, our_k); }