

#include "rba_DemObdBasic_Prv.h"

static void rba_DemObdBasic_SetWarmupCycleQualified (boolean conditionActive)
{
    Dem_GenericNvData.ObdWarmupCycleCondition = conditionActive;
    /* notify to store in NVM */
    Dem_NvMWriteBlockOnShutdown(DEM_NVM_ID_DEM_GENERIC_NV_DATA);
}

static boolean rba_DemObdBasic_IsWarmupCycleQualified (void)
{
    return Dem_GenericNvData.ObdWarmupCycleCondition;
}


void rba_DemObdBasic_SetOperationCycleState (Dem_OperationCycleList *operationCycleBitmask, Dem_OperationCycleStateType CycleState)
{
    if (CycleState == DEM_CYCLE_STATE_START)
    {
        if (DEM_OPERATIONCYCLE_ISBITSET(*operationCycleBitmask, DEM_OPCYC_OBD_WarmUpCycle))
        {
            rba_DemObdBasic_SetWarmupCycleQualified(TRUE);
            DEM_OPERATIONCYCLE_CLEARBIT(operationCycleBitmask, DEM_OPCYC_OBD_WarmUpCycle);
        }

        if (DEM_OPERATIONCYCLE_ISBITSET(*operationCycleBitmask, DEM_OPCYC_OBD_DrivingCycle))
        {
            DEM_OPERATIONCYCLE_SETBIT(operationCycleBitmask, DEM_OPCYC_IgnitionCycle);

            if (rba_DemObdBasic_IsWarmupCycleQualified())
            {
                DEM_OPERATIONCYCLE_SETBIT(operationCycleBitmask, DEM_OPCYC_OBD_WarmUpCycle);
            }
        }
    }
}


void rba_DemObdBasic_StartOperationCycle (Dem_OperationCycleList currentTriggers)
{
    if(!Dem_GetEvMemLock())
    {
        if (DEM_OPERATIONCYCLE_ISBITSET(currentTriggers, DEM_OPCYC_OBD_DrivingCycle))
        {
            rba_DemObdBasic_Mil_StartDrivingCycle();
            rba_DemObdBasic_PdtcMem_StartDrivingCycle();
            rba_DemObdBasic_Iumpr_StartDrivingCycle();

            if (rba_DemObdBasic_IsWarmupCycleQualified())
            {
                rba_DemObdBasic_Pid_StartWarmupCycle();
                rba_DemObdBasic_SetWarmupCycleQualified(FALSE);
            }
        }

        if (DEM_OPERATIONCYCLE_ISBITSET(currentTriggers, DEM_OPCYC_IgnitionCycle))
        {
            rba_DemObdBasic_PdtcMem_StartIgnitionCycle();
        }
    }
}

