
/*------------------------------- includes --------------------------------*/
//#include "Global.h"
#include "Std_Types.h"
#include "DiagnosticR/Comp_HAL_Autosar_Wrappers/Timer.h"
#include "DiagnosticR/Comp_ISO_15765_3/Iso15765_3_Task.h"


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

#include "Iso15765_3.h"

#define RESET_NO_REQUEST ((UI_8)0)
#define RESET_REQUESTED ((UI_8)1)
#define RESET_REQUESTED_PENDING ((UI_8)2)

#define TIMEOUT_RESET ((UI_16) 5000)

static UI_8 reset_request;
static UI_8 reset_type;

void UDSResetRequest(UI_8 req_reset_type, UI_8 size)
{
if(CHECK_RESET_TYPE(req_reset_type) == TRUE){
if(size == 1){
reset_type=req_reset_type;
reset_request=RESET_REQUESTED;
}
else{
Iso15765_3SendResponse(UDS_ERR_INVALID_FORMAT);
}
}
else{
Iso15765_3SendResponse(UDS_ERR_SUBFUNCTION_NOT_SUPPORTED);
}
}

static void SendResetResponse(UI_8 err)
{
tp_uds_ecu_reset_resp resp;
resp=ISO15765_3_GET_RESP_DATA(tp_uds_ecu_reset_resp);

if(err == ISO15765_3_POSITIVE_RESPONSE){
resp->reset_mode = reset_type;
Iso15765_3IncrementResponseSize(1);
}
Iso15765_3SendResponse(err);
}


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





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

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

typedef enum {
    ESTAT_ISO15765_3_ECURESETTASK_WAITING_FOR_RESET = 1, 
    ESTAT_ISO15765_3_ECURESETTASK_ANSWERING_RESET = 2, 
    ESTAT_ISO15765_3_ECURESETTASK_RESET_EXECUTE = 3, 
    ESTAT_ISO15765_3_ECURESETTASK_WAITINGTODORESET = 4, 
    ESTAT_0_ISO15765_3_ECURESETTASK = 0
} t_estat_iso15765_3_ecuresettask;


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

/* Variables d estat */
static t_estat_iso15765_3_ecuresettask estat_iso15765_3_ecuresettask = ESTAT_0_ISO15765_3_ECURESETTASK;

/* Timers implicits */
static t_clock temps_iso15765_3_ecuresettask = (t_clock)0;


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

static void Iso15765_3_EcuResetTask0(void);
static void Waiting_For_Reset(void);
static void Answering_Reset(void);
static void Reset_Execute(void);
static void WaitingToDoReset(void);


/*----------------------- funcions d'inicialitzacio -----------------------*/
 
/**************************************************************************** 
| Funcionalitat:                                                              
|   Inicialitzacio del diagrama d estats Iso15765_3_EcuResetTask 
|---------------------------------------------------------------------------- 
| Interficie:                                                                 
|   Entrades: - 
|   Sortides:
|       estat_iso15765_3_ecuresettask
|       temps_iso15765_3_ecuresettask 
****************************************************************************/
void Iso15765_3_EcuResetTaskInicialitza(void)
{
    /* Inicialitzacio de la variable d estat */
    estat_iso15765_3_ecuresettask = ESTAT_0_ISO15765_3_ECURESETTASK;

    /* Inicialitzacio del timer implicit */
    temps_iso15765_3_ecuresettask= (t_clock)0;

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


/*------------------------- rutines principals ---------------------------*/
 
/**************************************************************************** 
| Funcionalitat:                                                              
|   Rutina principal del diagrama d estats Iso15765_3_EcuResetTask. 
|   Invoca a la funcio corresponent a l estat actual. 
|---------------------------------------------------------------------------- 
| Interficie:                                                                 
|   Entrades:
|       estat_iso15765_3_ecuresettask
|       temps_iso15765_3_ecuresettask 
|   Sortides:
|       estat_iso15765_3_ecuresettask
|       temps_iso15765_3_ecuresettask 
****************************************************************************/
void Iso15765_3_EcuResetTask(void)
{
    /* Estudi per casos del estat actual */
    switch (estat_iso15765_3_ecuresettask){
    case ESTAT_0_ISO15765_3_ECURESETTASK:
        Iso15765_3_EcuResetTask0();
        break;
    case ESTAT_ISO15765_3_ECURESETTASK_WAITING_FOR_RESET:
        Waiting_For_Reset();
        break;
    case ESTAT_ISO15765_3_ECURESETTASK_ANSWERING_RESET:
        Answering_Reset();
        break;
    case ESTAT_ISO15765_3_ECURESETTASK_RESET_EXECUTE:
        Reset_Execute();
        break;
    case ESTAT_ISO15765_3_ECURESETTASK_WAITINGTODORESET:
        WaitingToDoReset();
        break;
    default:
        estat_iso15765_3_ecuresettask = ESTAT_0_ISO15765_3_ECURESETTASK;
        break;
    }

    /* Increment del temps de cicle al timer implicit */
    if (temps_iso15765_3_ecuresettask < MAX_COMPTADOR_TEMPS) {
        temps_iso15765_3_ecuresettask += TimerDeltaCicleConsulta();
    }
}


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

 
/**************************************************************************** 
| Funcionalitat: 
|   Funcio corresponent a l estat inicial del proces Iso15765_3_EcuResetTask 
|---------------------------------------------------------------------------- 
| Interficie: -                                                                
****************************************************************************/
static void Iso15765_3_EcuResetTask0(void)
{
    /* Transicio per defecte */

    /* -- entry de l estat -- */
    reset_request = RESET_NO_REQUEST;

    /* Inicialitzacio del timer implicit */
    temps_iso15765_3_ecuresettask= (t_clock)0;

    /* Canviem l estat inicial */
    estat_iso15765_3_ecuresettask = ESTAT_ISO15765_3_ECURESETTASK_WAITING_FOR_RESET;
}
 
/**************************************************************************** 
| Funcionalitat:                                                              
|   Funcio corresponent a l estat Waiting_For_Reset.                              
|---------------------------------------------------------------------------- 
| Interficie:                                                                 
|   Entrades:
|       estat_iso15765_3_ecuresettask
|       temps_iso15765_3_ecuresettask 
|   Sortides:
|       estat_iso15765_3_ecuresettask
|       temps_iso15765_3_ecuresettask 
****************************************************************************/
static void Waiting_For_Reset(void)
{
    if (((reset_request == RESET_REQUESTED) || (reset_request == RESET_REQUESTED_PENDING)) && (CHECK_RESET_POSSIBLE(reset_type)==TRUE) && (CHECK_RESET_TYPE(reset_type)==TRUE)) {
        /* Cas en que executem la transicio Check_Reset_Possible */

        /* -- action de la transicio -- */
        SendResetResponse(ISO15765_3_POSITIVE_RESPONSE);

        /* -- entry de l estat Answering_Reset -- */
        reset_request = RESET_NO_REQUEST;

        /* Inicialitzacio del timer implicit */
        temps_iso15765_3_ecuresettask= (t_clock)0;

        /* -- canviem l estat -- */
        estat_iso15765_3_ecuresettask = ESTAT_ISO15765_3_ECURESETTASK_ANSWERING_RESET;
    }
    else if (((reset_request == RESET_REQUESTED) || (reset_request == RESET_REQUESTED_PENDING)) && (CHECK_RESET_TYPE(reset_type)==TRUE) && (CHECK_RESET_POSSIBLE(reset_type)==FALSE)) {
        /* Cas en que executem la transicio Reset_Not_Possible_Now */

        /* -- action de la transicio -- */
        SendResetResponse(UDS_ERR_RESPONSE_PENDING);
        reset_request = RESET_NO_REQUEST;

        /* Inicialitzacio del timer implicit */
        temps_iso15765_3_ecuresettask= (t_clock)0;

        /* -- canviem l estat -- */
        estat_iso15765_3_ecuresettask = ESTAT_ISO15765_3_ECURESETTASK_WAITINGTODORESET;
    }
    else {

        /* No s ha activat cap transicio: ens quedem a l estat actual */
        estat_iso15765_3_ecuresettask = ESTAT_ISO15765_3_ECURESETTASK_WAITING_FOR_RESET;
    }
}
 
/**************************************************************************** 
| Funcionalitat:                                                              
|   Funcio corresponent a l estat Answering_Reset.                              
|---------------------------------------------------------------------------- 
| Interficie:                                                                 
|   Entrades:
|       estat_iso15765_3_ecuresettask
|       temps_iso15765_3_ecuresettask 
|   Sortides:
|       estat_iso15765_3_ecuresettask
|       temps_iso15765_3_ecuresettask 
****************************************************************************/
static void Answering_Reset(void)
{
    if ((tp_frm[DIAG_ISO15765_2_HDL].sts_tx == TP_FRM_TX_IDLE ) && (pending_response == FALSE)) {
        /* Cas en que executem la transicio Wait_answer_sent */

        /* -- entry de l estat Reset_Execute -- */
        EXECUTE_RESET(reset_type);

        /* Inicialitzacio del timer implicit */
        temps_iso15765_3_ecuresettask= (t_clock)0;

        /* -- canviem l estat -- */
        estat_iso15765_3_ecuresettask = ESTAT_ISO15765_3_ECURESETTASK_RESET_EXECUTE;
    }
    else {

        /* No s ha activat cap transicio: ens quedem a l estat actual */
        estat_iso15765_3_ecuresettask = ESTAT_ISO15765_3_ECURESETTASK_ANSWERING_RESET;
    }
}
 
/**************************************************************************** 
| Funcionalitat:                                                              
|   Funcio corresponent a l estat Reset_Execute.                              
|---------------------------------------------------------------------------- 
| Interficie:                                                                 
|   Entrades:
|       estat_iso15765_3_ecuresettask
|       temps_iso15765_3_ecuresettask 
|   Sortides:
|       estat_iso15765_3_ecuresettask
|       temps_iso15765_3_ecuresettask 
****************************************************************************/
static void Reset_Execute(void)
{
    /* Aquest es un estat pou. Ja no es canviara d estat */
    /* Per redundancia es reassigna el mateix estat */
    estat_iso15765_3_ecuresettask = ESTAT_ISO15765_3_ECURESETTASK_RESET_EXECUTE;
}
 
/**************************************************************************** 
| Funcionalitat:                                                              
|   Funcio corresponent a l estat WaitingToDoReset.                              
|---------------------------------------------------------------------------- 
| Interficie:                                                                 
|   Entrades:
|       estat_iso15765_3_ecuresettask
|       temps_iso15765_3_ecuresettask 
|   Sortides:
|       estat_iso15765_3_ecuresettask
|       temps_iso15765_3_ecuresettask 
****************************************************************************/
static void WaitingToDoReset(void)
{
    if ((CHECK_RESET_POSSIBLE(reset_type)==TRUE)) {
        /* Cas en que executem la transicio  */

        /* -- action de la transicio -- */
        SendResetResponse(ISO15765_3_POSITIVE_RESPONSE);

        /* -- entry de l estat Answering_Reset -- */
        reset_request = RESET_NO_REQUEST;

        /* Inicialitzacio del timer implicit */
        temps_iso15765_3_ecuresettask= (t_clock)0;

        /* -- canviem l estat -- */
        estat_iso15765_3_ecuresettask = ESTAT_ISO15765_3_ECURESETTASK_ANSWERING_RESET;
    }
    else if (temps_iso15765_3_ecuresettask >= TIMER_MS_TO_TICKS(TIMEOUT_RESET)) {
        /* Cas en que executem la transicio TimeoutReset */

        /* -- action de la transicio -- */
        SendResetResponse(UDS_ERR_BUSY_REPEAT_REQUEST);
        reset_request = RESET_NO_REQUEST;

        /* -- entry de l estat Waiting_For_Reset -- */
        reset_request = RESET_NO_REQUEST;

        /* Inicialitzacio del timer implicit */
        temps_iso15765_3_ecuresettask= (t_clock)0;

        /* -- canviem l estat -- */
        estat_iso15765_3_ecuresettask = ESTAT_ISO15765_3_ECURESETTASK_WAITING_FOR_RESET;
    }
    else {

        /* No s ha activat cap transicio: ens quedem a l estat actual */
        estat_iso15765_3_ecuresettask = ESTAT_ISO15765_3_ECURESETTASK_WAITINGTODORESET;
    }
}


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