
/**************   FICOSA COMMAND AND CONTROL SYSTEMS DIVISION  ****************
| File:              | TP_Rx.c
|-----------------------------------------------------------------------------
| Author:            | Author
|-----------------------------------------------------------------------------
| Project:           | TP_FULL
| System:            | TP_Task
| Diagram:           | TP_Rx
| Model Version:     | 1.0
| Model Date:        | Thursday, February 26, 2009 - 14:47:56
|-----------------------------------------------------------------------------
| CodeC Version:     | 1.0
| Codification Date: | 18/10/2010
|-----------------------------------------------------------------------------
| Description:
|    Codificacio automatica d'un diagrama d'estats
|
|    ++++ Informacio del Diagrama d'Estats +++++
|
|    Numero d'estats:       3
|    Numero de transicions: 14
|    Estats inicials:       1
|  
|    Variables d'entrada:
|           NP_CAN_RX_NOTIF (BOOL)
|           NP_CAN_RX_SIZE (UI_8)
|           NP_CAN_RX_DATA (UI_8)
|    Variables de sortida:
|           tp_frm (t_tp_frm)
|    Variables d'entrada/sortida:
|    Variables locals:
|           timer_n_cr (t_clock)
|           num_frames_before_fc (UI_16)
|           timer_n_ar (t_clock)
|
|    ++++ Descripcio del proces implementat ++++
|
|     Maquina 0x1 
 ISO 15765: CAN -> KWP 
|
**************************   FI DE LA CAPCALERA  ****************************/

/*------------------------------- includes --------------------------------*/
#include "Std_Types.h"
#include "DiagnosticR/Comp_HAL_Autosar_Wrappers/Timer.h"
#include "TP_Task.h"


/*------------------------------ user code --------------------------------*/

#include "DiagnosticR/UDS/Iso15765_layer2/TP_CFG.h"
#include "TP_Functions.h"
#include "DiagnosticR/Comp_ISO_15765_2/TP.h"

/*-------------------------------- macros ---------------------------------*/

/*------------------------------- defines ---------------------------------*/
/* Maxim valor del compteig del temps */
#define MAX_COMPTADOR_TEMPS ((UI_16)65000)

/*---------------------------- tipus de dades -----------------------------*/

/*------------------------------ variables --------------------------------*/

/*------------------------- capcaleres de funcions ------------------------*/

static void TP_Rx_Full0(UI_8 tp_hdl);
static void Inicio(UI_8 tp_hdl);
static void RxCF(UI_8 tp_hdl);
static void ConfirmaFC(UI_8 tp_hdl);
//<<
//void TP_Rx_FullInicialitza(UI_8 iso2_hdl);
//void TP_Rx_Full(UI_8 iso2_hdl);
//>>

/*----------------------- funcions d'inicialitzacio -----------------------*/
 
/**************************************************************************** 
| Funcionalitat:                                                              
|   Inicialitzacio del diagrama d estats TP_Rx 
|---------------------------------------------------------------------------- 
| Interficie:                                                                 
|   Entrades: - 
|   Sortides:
|       
|       tp_frm 
****************************************************************************/
#if 1
void TP_Rx_FullInicialitza(UI_8 tp_hdl)
{
    /* Inicialitzacio de la variable d estat */
    tp_data[tp_hdl].state_rx = ESTAT_0_TP_RX_FULL;

    /* Execucio del cicle inicial de la maquina d estats */
    TP_Rx_Full(tp_hdl);
}
#endif

/*------------------------- rutines principals ---------------------------*/
 
/**************************************************************************** 
| Funcionalitat:                                                              
|   Rutina principal del diagrama d estats TP_Rx. 
|   Invoca a la funcio corresponent a l estat actual. 
|---------------------------------------------------------------------------- 
| Interficie:                                                                 
|   Entrades:
|       
|       NP_CAN_RX_NOTIF
|       NP_CAN_RX_SIZE
|       NP_CAN_RX_DATA 
|   Sortides:
|       
|       tp_frm 
****************************************************************************/
#if 1
void TP_Rx_Full(UI_8 tp_hdl) 
{
    /* Estudi per casos del estat actual */
    switch (tp_data[tp_hdl].state_rx){
    case ESTAT_0_TP_RX_FULL:
        TP_Rx_Full0(tp_hdl);
        break;
    case ESTAT_TP_RX_FULL_INICIO:
        Inicio(tp_hdl);
        break;
    case ESTAT_TP_RX_FULL_RXCF:
        RxCF(tp_hdl);
        break;
    case ESTAT_TP_RX_FULL_CONFIRMAFC:
        ConfirmaFC(tp_hdl);
        break;
    default:
        tp_data[tp_hdl].state_rx = ESTAT_0_TP_RX_FULL;
        break;
    }

    /* Increment del temps de cicle als timers */
    if (tp_data[tp_hdl].timers.n_cr < MAX_COMPTADOR_TEMPS) {
        tp_data[tp_hdl].timers.n_cr += TP_TASK_TICKS;
    }
}
#endif
#ifdef ENABLE_INCREMENT_EXTERNAL_TIMERS
void TPRxIncrTimers(UI_8 tp_hdl, UI_16 ticks)
{
    if (tp_data[tp_hdl].timers.n_cr < MAX_COMPTADOR_TEMPS) {
        tp_data[tp_hdl].timers.n_cr += ticks;
    }
}
#endif

/*------------------------- funcions d'estats -----------------------------*/

 
/**************************************************************************** 
| Funcionalitat: 
|   Funcio corresponent a l estat inicial del proces TP_Rx 
|---------------------------------------------------------------------------- 
| Interficie: -                                                                
****************************************************************************/
static void TP_Rx_Full0(UI_8 tp_hdl)
{
    /* -- reset dels timers t_clock -- */
    tp_data[tp_hdl].timers.n_cr = (t_clock)0;
    
    if (TPCheckPhyCfgData(tp_hdl) == TRUE) {
        /* Canviem l estat inicial */
        tp_data[tp_hdl].state_rx = ESTAT_TP_RX_FULL_INICIO;
    }
    else {
        /* No s ha activat cap condicio inicial: ens quedem a l estat actual */
        tp_data[tp_hdl].state_rx = ESTAT_0_TP_RX_FULL;
    }
}
 
/**************************************************************************** 
| Funcionalitat:                                                              
|   Funcio corresponent a l estat Inicio.                              
|---------------------------------------------------------------------------- 
| Interficie:                                                                 
|   Entrades:
|       
|       NP_CAN_RX_NOTIF
|       NP_CAN_RX_SIZE
|       NP_CAN_RX_DATA 
|   Sortides:
|       
|       tp_frm 
****************************************************************************/
static void Inicio(UI_8 tp_hdl)
{
    if ((tp_data[tp_hdl].rx_st.frame_rx_type == TP_RX_SF) && 
        ((tp_frm[tp_hdl].sts_rx == TP_FRM_RX_IDLE) || (tp_frm[tp_hdl].sts_rx == TP_FRM_RX_ERR_NOTIF))) {
        /* Cas en que executem la transicio RxSF */

        /* -- action de la transicio -- */
        if(tp_frm[tp_hdl].iso15765_2_block_rx == FALSE ) {
            TPReadSF(tp_hdl); 
        }
        else {
            /* Reset the frame reception type */
            tp_data[tp_hdl].rx_st.frame_rx_type = TP_RX_NO_FRM;
        }

        /* -- reset del timer -- */
        tp_data[tp_hdl].timers.n_cr = (t_clock)0;

        /* -- canviem l estat -- */
        tp_data[tp_hdl].state_rx = ESTAT_TP_RX_FULL_INICIO;
    }
    else if ((tp_data[tp_hdl].rx_st.frame_rx_type == TP_RX_FF) && (tp_frm[tp_hdl].iso15765_2_block_rx == TRUE)) {
        /* Cas en que executem la transicio RxFFTxFCBlocked */

        /* -- action de la transicio -- */  
        /* Reset the frame reception type */
        tp_data[tp_hdl].rx_st.frame_rx_type = TP_RX_NO_FRM;

        /* -- reset del timer -- */
        tp_data[tp_hdl].timers.n_cr = (t_clock)0;

        /* -- canviem l estat -- */
        tp_data[tp_hdl].state_rx = ESTAT_TP_RX_FULL_INICIO;
    }
    else if ((tp_data[tp_hdl].rx_st.frame_rx_type == TP_RX_FF) && (tp_frm[tp_hdl].iso15765_2_block_rx == FALSE) && ((tp_frm[tp_hdl].sts_rx == TP_FRM_RX_IDLE) || (tp_frm[tp_hdl].sts_rx == TP_FRM_RX_ERR_NOTIF))) {
        /* Cas en que executem la transicio RxFFTxFCNotBlocked */

        /* -- action de la transicio -- */
        TPReadFFSendFC(tp_hdl);

        /* -- reset del timer -- */
        tp_data[tp_hdl].timers.n_cr = (t_clock)0;

        /* -- canviem l estat -- */
        tp_data[tp_hdl].state_rx = ESTAT_TP_RX_FULL_CONFIRMAFC;
    }
    else {

        /* No s ha activat cap transicio: ens quedem a l estat actual */
        tp_data[tp_hdl].state_rx = ESTAT_TP_RX_FULL_INICIO;
    }
}
 
/**************************************************************************** 
| Funcionalitat:                                                              
|   Funcio corresponent a l estat RxCF.                              
|---------------------------------------------------------------------------- 
| Interficie:                                                                 
|   Entrades:
|       
|       NP_CAN_RX_NOTIF
|       NP_CAN_RX_SIZE
|       NP_CAN_RX_DATA 
|   Sortides:
|       
|       tp_frm 
****************************************************************************/
static void RxCF(UI_8 tp_hdl)
{
    if ((tp_data[tp_hdl].timers.n_cr > TIMER_MS_TO_TICKS(tp_data[tp_hdl].cfg.n_cr_max) ) && (tp_frm[tp_hdl].sts_rx != TP_FRM_RX_FINISHED)) {
        /* Cas en que executem la transicio TimeOutSigSegmento */

        /* -- action de la transicio -- */
        tp_frm[tp_hdl].sts_rx = TP_FRM_RX_ERR_NOTIF;

        /* -- reset del timer -- */
        tp_data[tp_hdl].timers.n_cr = (t_clock)0;

        /* -- canviem l estat -- */
        tp_data[tp_hdl].state_rx = ESTAT_TP_RX_FULL_INICIO;
    }
    else if (tp_frm[tp_hdl].sts_rx == TP_FRM_RX_ERR_FINISHED) {
        /* Cas en que executem la transicio ErrorCF */

        /* -- action de la transicio -- */
        tp_frm[tp_hdl].sts_rx = TP_FRM_RX_ERR_NOTIF;

        /* -- reset del timer -- */
        tp_data[tp_hdl].timers.n_cr = (t_clock)0;

        /* -- canviem l estat -- */
        tp_data[tp_hdl].state_rx = ESTAT_TP_RX_FULL_INICIO;
    }
    else if ((tp_frm[tp_hdl].sts_rx != TP_FRM_RX_FINISHED) && ((tp_data[tp_hdl].rx_st.frame_rx_type == TP_RX_SF) || (tp_data[tp_hdl].rx_st.frame_rx_type == TP_RX_FF))) {
        /* Cas en que executem la transicio UnexpectedFForSF */

        /* -- action de la transicio -- */
        tp_frm[tp_hdl].sts_rx = TP_FRM_RX_ERR_NOTIF;

        /* -- reset del timer -- */
        tp_data[tp_hdl].timers.n_cr = (t_clock)0;

        /* -- canviem l estat -- */
        tp_data[tp_hdl].state_rx = ESTAT_TP_RX_FULL_INICIO;
    }
    else if ((tp_frm[tp_hdl].sts_rx == TP_FRM_RX_FINISHED)) {
        /* Cas en que executem la transicio RxAcabada */

        /* -- action de la transicio -- */
        tp_frm[tp_hdl].sts_rx = TP_FRM_RX_NOTIF;

        /* -- reset del timer -- */
        tp_data[tp_hdl].timers.n_cr = (t_clock)0;

        /* -- canviem l estat -- */
        tp_data[tp_hdl].state_rx = ESTAT_TP_RX_FULL_INICIO;
    }
    else if ((tp_data[tp_hdl].rx_st.frame_rx_type == TP_RX_CF) && (tp_data[tp_hdl].cfg.block_size > (UI_8)0) && (tp_data[tp_hdl].rx_st.num_frames_before_fc >= (tp_data[tp_hdl].cfg.block_size - (UI_8)1)) && (tp_data[tp_hdl].rx_st.byte_actual_rx < (tp_frm[tp_hdl].size_rx - CF_DATA_LEN))) {
        /* Cas en que executem la transicio TPSendFC */

        /* -- action de la transicio -- */
        TPReadCFSendFC(tp_hdl);

        /* -- reset del timer -- */
        tp_data[tp_hdl].timers.n_cr = (t_clock)0;

        /* -- canviem l estat -- */
        tp_data[tp_hdl].state_rx = ESTAT_TP_RX_FULL_CONFIRMAFC;
    }
    else if ((tp_data[tp_hdl].rx_st.frame_rx_type == TP_RX_CF) && ((tp_data[tp_hdl].cfg.block_size == (UI_8)0) || (tp_data[tp_hdl].rx_st.num_frames_before_fc < (tp_data[tp_hdl].cfg.block_size - (UI_8)1)) || (tp_data[tp_hdl].rx_st.byte_actual_rx >= (tp_frm[tp_hdl].size_rx - CF_DATA_LEN)))) {
        /* Cas en que executem la transicio ReceivedCFNoFC */

        /* -- action de la transicio -- */
        TPReadCF(tp_hdl);
        tp_data[tp_hdl].rx_st.num_frames_before_fc++;

        /* -- reset del timer -- */
        tp_data[tp_hdl].timers.n_cr = (t_clock)0;

        /* -- canviem l estat -- */
        tp_data[tp_hdl].state_rx = ESTAT_TP_RX_FULL_RXCF;
    }
    else {

        /* No s ha activat cap transicio: ens quedem a l estat actual */
        tp_data[tp_hdl].state_rx = ESTAT_TP_RX_FULL_RXCF;
    }
}
 
/**************************************************************************** 
| Funcionalitat:                                                              
|   Funcio corresponent a l estat ConfirmaFC.                              
|---------------------------------------------------------------------------- 
| Interficie:                                                                 
|   Entrades:
|       
|       NP_CAN_RX_NOTIF
|       NP_CAN_RX_SIZE
|       NP_CAN_RX_DATA 
|   Sortides:
|       
|       tp_frm 
****************************************************************************/
static void ConfirmaFC(UI_8 tp_hdl)
{
    BOOL tx_confirmed;
    
    /* Check if the transmission has been confirmed */
    tx_confirmed = TPCheckTxConfirmed(tp_hdl, tp_data[tp_hdl].tx_st.frame_tx_fc_sts);
    
    if ((tx_confirmed == TRUE) && (tp_data[tp_hdl].rx_st.frame_rx_type == TP_RX_NO_FRM)) {
        /* Cas en que executem la transicio ConfirmatFCNoReceivedCF */

        /* -- action de la transicio -- */
        tp_data[tp_hdl].rx_st.num_frames_before_fc = (UI_16)0;

        /* -- reset del timer -- */
        tp_data[tp_hdl].timers.n_cr = (t_clock)0;

        /* -- canviem l estat -- */
        tp_data[tp_hdl].state_rx = ESTAT_TP_RX_FULL_RXCF;
    }
    else if (tp_data[tp_hdl].tx_st.frame_tx_fc_sts == TP_TX_ERROR) {
        
        /* Cas en que executem la transicio TimeOutAr */

        /* -- action de la transicio -- */
        tp_frm[tp_hdl].sts_rx = TP_FRM_RX_ERR_NOTIF;

        /* -- reset del timer -- */
        tp_data[tp_hdl].timers.n_cr = (t_clock)0;

        /* -- canviem l estat -- */
        tp_data[tp_hdl].state_rx = ESTAT_TP_RX_FULL_INICIO;
    }
    else if ((tx_confirmed == TRUE) && (tp_data[tp_hdl].rx_st.frame_rx_type == TP_RX_CF) && (tp_data[tp_hdl].cfg.block_size == (UI_8)1) && (tp_data[tp_hdl].rx_st.byte_actual_rx < (tp_frm[tp_hdl].size_rx - CF_DATA_LEN))) {
        /* Cas en que executem la transicio ConfirmatFCReceivedCFSendFC */

        /* -- action de la transicio -- */
        TPReadCFSendFC(tp_hdl);

        /* -- reset del timer -- */
        tp_data[tp_hdl].timers.n_cr = (t_clock)0;

        /* -- canviem l estat -- */
        tp_data[tp_hdl].state_rx = ESTAT_TP_RX_FULL_CONFIRMAFC;
    }
    else if ((tx_confirmed == TRUE) && (tp_data[tp_hdl].rx_st.frame_rx_type == TP_RX_CF) && ((tp_data[tp_hdl].cfg.block_size != (UI_8)1) || (tp_data[tp_hdl].rx_st.byte_actual_rx >= (tp_frm[tp_hdl].size_rx - CF_DATA_LEN)))) {
        /* Cas en que executem la transicio ConfirmatFCReceivedCFNotSendFC */

        /* -- action de la transicio -- */
        TPReadCF(tp_hdl);
        tp_data[tp_hdl].rx_st.num_frames_before_fc = (UI_16)1;

        /* -- reset del timer -- */
        tp_data[tp_hdl].timers.n_cr = (t_clock)0;

        /* -- canviem l estat -- */
        tp_data[tp_hdl].state_rx = ESTAT_TP_RX_FULL_RXCF;
    }
    else if ((tp_frm[tp_hdl].sts_rx != TP_FRM_RX_FINISHED) && ((tp_data[tp_hdl].rx_st.frame_rx_type == TP_RX_SF) || (tp_data[tp_hdl].rx_st.frame_rx_type == TP_RX_FF))) {
        /* Cas en que executem la transicio UnexpectedSForFF */

        /* -- action de la transicio -- */
        tp_frm[tp_hdl].sts_rx = TP_FRM_RX_ERR_NOTIF;

        /* -- reset del timer -- */
        tp_data[tp_hdl].timers.n_cr = (t_clock)0;

        /* -- canviem l estat -- */
        tp_data[tp_hdl].state_rx = ESTAT_TP_RX_FULL_INICIO;
    }
    else {

        /* No s ha activat cap transicio: ens quedem a l estat actual */
        tp_data[tp_hdl].state_rx = ESTAT_TP_RX_FULL_CONFIRMAFC;
    }
}


/********************************** FI **************************************/
