
/**************   FICOSA COMMAND AND CONTROL SYSTEMS DIVISION  ****************
| File:              | TP_NetworkLayerLowerInterface_Tx.c
|-----------------------------------------------------------------------------
| Author:            | Author
|-----------------------------------------------------------------------------
| Project:           | Iso15765_2_Full
| System:            | Iso15765_2_Task_Full
| Diagram:           | TP_NetworkLayerLowerInterface_Tx
| Model Version:     | 1.0
| Model Date:        | Thursday, February 26, 2009 - 14:47:56
|-----------------------------------------------------------------------------
| CodeC Version:     | 1.0
| Codification Date: | 14/11/2011
|-----------------------------------------------------------------------------
| Description:
|    Codificacio automatica d'un diagrama d'estats
|
|    ++++ Informacio del Diagrama d'Estats +++++
|
|    Numero d'estats:       2
|    Numero de transicions: 4
|    Estats inicials:       1
|  
|    Variables d'entrada:
|    Variables de sortida:
|    Variables d'entrada/sortida:
|    Variables locals:
|
|    ++++ Descripcio del proces implementat ++++
|
|    Task responsible of assuring TTmin time between 2 Tx Can
frames.
|
**************************   FI DE LA CAPCALERA  ****************************/

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


#include "TP_Functions.h"

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


/*-------------------------------- 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_NetworkLayerLowerInterface_Tx0(UI_8 tp_hdl);
static void WaitingTxConfirmation(UI_8 tp_hdl);
static void ReadyToSend(UI_8 tp_hdl);
/*----------------------- funcions d'inicialitzacio -----------------------*/
 
/**************************************************************************** 
| Funcionalitat:                                                              
|   Inicialitzacio del diagrama d estats TP_NetworkLayerLowerInterface_Tx 
|---------------------------------------------------------------------------- 
| Interficie:                                                                 
|   Entrades: - 
|   Sortides:
|       estat_tp_networklayerlowerinterface_tx
|        
****************************************************************************/
void TP_NetworkLayerLowerInterface_TxInicialitza(UI_8 tp_hdl)
{
    /* Inicialitzacio de la variable d estat */
    tp_data[tp_hdl].state_can_phy_tx = ESTAT_0_TP_NETWORKLAYERLOWERINTERFACE_TX;

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


/*------------------------- rutines principals ---------------------------*/
 
/**************************************************************************** 
| Funcionalitat:                                                              
|   Rutina principal del diagrama d estats TP_NetworkLayerLowerInterface_Tx. 
|   Invoca a la funcio corresponent a l estat actual. 
|---------------------------------------------------------------------------- 
| Interficie:                                                                 
|   Entrades:
|       estat_tp_networklayerlowerinterface_tx
|        
|   Sortides:
|       estat_tp_networklayerlowerinterface_tx
|        
****************************************************************************/
void TP_NetworkLayerLowerInterface_Tx(UI_8 tp_hdl)
{ 
	 //<<Always enter here>>
    /* Estudi per casos del estat actual */
    switch (tp_data[tp_hdl].state_can_phy_tx){
    case ESTAT_0_TP_NETWORKLAYERLOWERINTERFACE_TX:
        TP_NetworkLayerLowerInterface_Tx0(tp_hdl);
	//<<AutoOnceTime-->tp_data[tp_hdl].state_can_phy_tx TO 002 REDAYTOSEND>>
        break;
    case ESTAT_TP_NETWORKLAYERLOWERINTERFACE_TX_WAITINGTXCONFIRMATION:
        WaitingTxConfirmation(tp_hdl);
        break;
    case ESTAT_TP_NETWORKLAYERLOWERINTERFACE_TX_READYTOSEND:
        ReadyToSend(tp_hdl);
		  //<<AlwaysHere02READAYTOSEND>>
        break;
    default:
        tp_data[tp_hdl].state_can_phy_tx = ESTAT_0_TP_NETWORKLAYERLOWERINTERFACE_TX;
        break;
    }
   
}


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

 
/**************************************************************************** 
| Funcionalitat: 
|   Funcio corresponent a l estat inicial del proces TP_NetworkLayerLowerInterface_Tx 
|---------------------------------------------------------------------------- 
| Interficie: -                                                                
****************************************************************************/
static void TP_NetworkLayerLowerInterface_Tx0(UI_8 tp_hdl)
{
    if (TPCheckPhyCfgData(tp_hdl) == TRUE) {
        /* Avaluem condicio de start del estat ReadyToSend */

        /* Canviem l estat inicial */
        tp_data[tp_hdl].state_can_phy_tx = ESTAT_TP_NETWORKLAYERLOWERINTERFACE_TX_READYTOSEND;
    }
    else {
        /* No s ha activat cap condicio inicial: ens quedem a l estat actual */
        tp_data[tp_hdl].state_can_phy_tx = ESTAT_0_TP_NETWORKLAYERLOWERINTERFACE_TX;
    }
}
 
/**************************************************************************** 
| Funcionalitat:                                                              
|   Funcio corresponent a l estat WaitingTxConfirmation.                              
|---------------------------------------------------------------------------- 
| Interficie:                                                                 
|   Entrades:
|       estat_tp_networklayerlowerinterface_tx
|        
|   Sortides:
|       estat_tp_networklayerlowerinterface_tx
|        
****************************************************************************/
static void WaitingTxConfirmation(UI_8 tp_hdl)
{
    /* -- codi de l estat actual -- */
    BOOL tx_confirmation, ax_timeout;
    BOOL tx_requested = FALSE;
    t_tp_tx_type tp_tx_type;
   // uint8 index_1;
    
    /* Check if CAN transmission has been confirmed */
    tx_confirmation = TPCheckCanFrameConfirmation(tp_hdl);
    /* Check if the TX timeout error has been confirmed */
    ax_timeout = TPCheckAxTimeout(tp_hdl);
     //<<ManyTimesHere>>//
    /* Case in which transmission is confirmed */
    if(tx_confirmation == TRUE) {
		//<<NeverHere>>/
        /* Notify the TX confirmation */
        TPNotifyCanFrmTx(tp_hdl, TP_TX_CONFIRMED);
        /* Check if another TX is pending */
        tx_requested = TPCheckTxCanFrame(tp_hdl, &tp_tx_type);
    }
    else {
        /* Do nothing */
    }

    if ((tx_confirmation == TRUE) && (tx_requested == FALSE)) {
        /* Cas en que executem la transicio  */

        /* -- canviem l estat -- */
        tp_data[tp_hdl].state_can_phy_tx = ESTAT_TP_NETWORKLAYERLOWERINTERFACE_TX_READYTOSEND;
    }
    else if ((ax_timeout == TRUE) && (tx_confirmation == FALSE)) {
        /* Cas en que executem la transicio  */

        /* -- action de la transicio -- */
        TPNotifyCanFrmTx(tp_hdl, TP_TX_ERROR);

        /* -- canviem l estat -- */
        tp_data[tp_hdl].state_can_phy_tx = ESTAT_TP_NETWORKLAYERLOWERINTERFACE_TX_READYTOSEND;
    }
    else if ((tx_confirmation == TRUE) && (tx_requested == TRUE)) {
        /* Cas en que executem la transicio  */

        /* -- action de la transicio -- */
        TPProcessTxCanFrame(tp_hdl);
        /* -- canviem l estat -- */
        tp_data[tp_hdl].state_can_phy_tx = ESTAT_TP_NETWORKLAYERLOWERINTERFACE_TX_WAITINGTXCONFIRMATION;
    }
    else {

        /* No s ha activat cap transicio: ens quedem a l estat actual */
        tp_data[tp_hdl].state_can_phy_tx = ESTAT_TP_NETWORKLAYERLOWERINTERFACE_TX_WAITINGTXCONFIRMATION;
    }
}
 
/**************************************************************************** 
| Funcionalitat:                                                              
|   Funcio corresponent a l estat ReadyToSend.                              
|---------------------------------------------------------------------------- 
| Interficie:                                                                 
|   Entrades:
|       estat_tp_networklayerlowerinterface_tx
|        
|   Sortides:
|       estat_tp_networklayerlowerinterface_tx
|        
****************************************************************************/
static void ReadyToSend(UI_8 tp_hdl)
{
    /* -- codi de l estat actual -- */
    BOOL tx_requested;
    t_tp_tx_type tp_tx_type;
   // uint8 index_2;
    
    tx_requested = TPCheckTxCanFrame(tp_hdl, &tp_tx_type);
   
    if (tx_requested == TRUE) {
        /* Cas en que executem la transicio  */

        /* -- action de la transicio -- */
        TPProcessTxCanFrame(tp_hdl);
        /* -- canviem l estat -- */
        tp_data[tp_hdl].state_can_phy_tx = ESTAT_TP_NETWORKLAYERLOWERINTERFACE_TX_WAITINGTXCONFIRMATION;
    }
    else {

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


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