#include "Rte_Type.h"
#include "Diag_SWC.h"
#include "Rte_Diag_SWC.h"

#define SWC_COMPARE_KEY_FAILED 11

uint16 VehicleSpeed_Data = 20;
uint16 EngineSpeed_Data = 10;
uint8 genUint8;
volatile uint16 RoutineControl_Test_Status;
volatile uint16 RoutineControl_CheckProgCond_Status;

/* Seed values for Service 0x27 */

const uint8 DcmSeed[8] =
{
	0x11,0x11,
	0x22,0x22,
	0x33,0x33,
	0x44,0x44

};

/* Key values for Service 0x27 */

const uint8 DcmKey[8] =
{
	0x10, 0x10,
	0x20, 0x20,
	0x30, 0x30,
	0x40, 0x40

};

/*Vehicle Manufacturer ECU Software Version Number	0xF189*/
Dcm_DataArrayTypeUint8_DspData_ASW_VER_F189Type   dataF189={0x10, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3};
Dcm_DataArrayTypeUint8_DspData_MANU_DATE_F18BType dataF18B={0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02};
Dcm_DataArrayTypeUint8_DspData_SYS_SUP_ID_F18AType dataF18A={0x01, 0x02, 0x03, 0x01, 0x02, 0x02, 0x02, 0x02};



FUNC(Std_ReturnType,DCM_APPL_CODE) RTE_VehicleSpeed_Data (uint8* Buffer)
{
   uint8 i=0;
   Buffer[i] = (uint8)(VehicleSpeed_Data & 0x00FF);
   return E_OK;
}

Std_ReturnType RTE_EngineSpeed_Data(uint8* Buffer)
{
   uint8 i=0;
   Buffer[i] = EngineSpeed_Data;
   return E_OK;
}

uint8 TestDiag_Fim_Permission;
uint8 Dem_test_case, Dem_test_case_old;
FUNC(void, TestDiag_CODE) RE_Swc_Dem_Periodic(void)
{
	Dem_test_case = Rte_IRead_Runnable_Dcm_Swc_RPort_TST_Diag_TestNumber_uint8_Impl();

   	if( genUint8 == 0 )
	{
   		Rte_Call_RPort_OpCycle_DemOperationCycle_0_SetOperationCycleState(DEM_CYCLE_STATE_START);
		Rte_Call_RPort_OpCycle_DEM_OPCYC_WARMUP_SetOperationCycleState(DEM_CYCLE_STATE_START);
		Rte_Call_RPort_OpCycle_DEM_OPCY_IGNITION_SetOperationCycleState(DEM_CYCLE_STATE_START);
		Rte_Call_RPort_OpCycle_DEM_OPCY_OBD_DCY_SetOperationCycleState(DEM_CYCLE_STATE_START);
		genUint8 = 1;
	}
   	if(Dem_test_case != Dem_test_case_old )
   	{
		switch(Dem_test_case)
		{
		case 0x01:
			Rte_Call_RPort_Event_Event_LostComm_SEN_SetEventStatus(DEM_EVENT_STATUS_FAILED);
			break;
		case 0x02:
			Rte_Call_RPort_Event_Event_LostComm_SEN_1_SetEventStatus(DEM_EVENT_STATUS_FAILED);
			break;
		case 0x03:
			Rte_Call_RPort_Event_Event_LostComm_MPDU_SetEventStatus(DEM_EVENT_STATUS_FAILED);
			break;
		case 0x04:
			Rte_Call_RPort_Event_Event_LostComm_SEN_SetEventStatus(DEM_EVENT_STATUS_PASSED);
			break;
		case 0x05:
			Rte_Call_RPort_Event_Event_LostComm_SEN_1_SetEventStatus(DEM_EVENT_STATUS_PASSED);
			break;
		case 0x06:
			Rte_Call_RPort_Event_Event_LostComm_MPDU_SetEventStatus(DEM_EVENT_STATUS_PASSED);
			break;

		default:
			break;

		}
		Dem_test_case_old = Dem_test_case;
   	}

   	Rte_Call_RPort_FiMFID_0_GetFunctionPermission(&TestDiag_Fim_Permission);
   	Rte_Write_PPort_TST_FimPermisison_TST_FimPermission(TestDiag_Fim_Permission);
}
FUNC(void, TestDiag_CODE) RunnableEntity_dcm(void)
{

	Dcm_ProtocolType ActiveProtocol1;
	Dcm_SecLevelType SecLevel2;
	Dcm_SesCtrlType SesCtrlType3;
	uint8 Mode1;


	/* Local Data Declaration */
	
	Rte_Write_PPort_DataServices_DspData_ASW_VER_F189_Data( (uint8 * const)&dataF189 );

	Rte_Write_PPort_DataServices_DspData_MANU_DATE_F18B_Data((uint8 * const)&dataF18B);

	Rte_Read_RPort_DataServices_DspData_SYS_SUP_ID_F18A_Data((uint8 * const)&dataF18A);
	Rte_Write_PPort_DataServices_DspData_SYS_SUP_ID_F18A_Data((uint8 * const)&dataF18A);


	/*  -------------------------------------- Server Call Point  --------------------------------  */

    (void) Rte_Call_RPort_DCMServices_GetActiveProtocol(&ActiveProtocol1);

	(void) Rte_Call_RPort_DCMServices_GetSecurityLevel(&SecLevel2);

	(void) Rte_Call_RPort_DCMServices_GetSesCtrlType(&SesCtrlType3);

	/*  -------------------------------------- CDATA ---------------------------------------------  */

	/*  -------------------------------------- Mode Management -----------------------------------  */
	(void) Rte_Switch_PPort_Mdg2_MDGP_Mdg2( Mode1);

}

FUNC(Std_ReturnType, TestDiag_CODE) Swc_DSP_SEC_LEV_L1_CK_func(	P2CONST(uint8, AUTOMATIC, RTE_APPL_DATA) Key,
        														VAR(Dcm_OpStatusType, AUTOMATIC) OpStatus,
																CONSTP2VAR(Dcm_NegativeResponseCodeType, AUTOMATIC, RTE_APPL_DATA) ErrorCode)
{
	Std_ReturnType retValue = RTE_E_OK;
	
	if ( ( Key[ 0 ] != DcmKey[ 0 ] ) || ( Key[ 1 ] != DcmKey[ 1 ] ) )
	{
		retValue = SWC_COMPARE_KEY_FAILED;
	}
	else{ /* Do nothing. Key is correct. */ }
	
	return ( retValue );
}
FUNC(Std_ReturnType, TestDiag_CODE) Swc_DSP_SEC_LEV_L1_GS_func( VAR(Dcm_OpStatusType, AUTOMATIC) OpStatus,
        														CONSTP2VAR(uint8, AUTOMATIC, RTE_APPL_DATA) Seed,
																CONSTP2VAR(Dcm_NegativeResponseCodeType, AUTOMATIC, RTE_APPL_DATA) ErrorCode)
{
	Std_ReturnType retValue = RTE_E_OK;
	*Seed = DcmSeed[0];
	*(Seed+1) = DcmSeed[1];
	return (retValue);
}
FUNC(Std_ReturnType, TestDiag_CODE) Swc_DSP_SEC_LEV_L2_CK_func( P2CONST(uint8, AUTOMATIC, RTE_APPL_DATA) Key,
																VAR(Dcm_OpStatusType, AUTOMATIC) OpStatus,
																CONSTP2VAR(Dcm_NegativeResponseCodeType, AUTOMATIC, RTE_APPL_DATA) ErrorCode)
{
	Std_ReturnType retValue = RTE_E_OK;
	
	if ( ( Key[ 0 ] != DcmKey[ 2 ] ) || ( Key[ 1 ] != DcmKey[ 3 ] ) )
	{
		retValue = SWC_COMPARE_KEY_FAILED;
	}
	else{ /* Do nothing. Key is correct. */ }
	
	return ( retValue );
}
FUNC(Std_ReturnType, TestDiag_CODE) Swc_DSP_SEC_LEV_L2_GS_func( VAR(Dcm_OpStatusType, AUTOMATIC) OpStatus,
																CONSTP2VAR(uint8, AUTOMATIC, RTE_APPL_DATA) Seed,
																CONSTP2VAR(Dcm_NegativeResponseCodeType, AUTOMATIC, RTE_APPL_DATA) ErrorCode)
{
	Std_ReturnType retValue = RTE_E_OK;
	*Seed = DcmSeed[2];
	*(Seed+1) = DcmSeed[3];
	return (retValue);
}
FUNC(Std_ReturnType, TestDiag_CODE) Swc_DSP_SEC_LEV_L3_CK_func( P2CONST(uint8, AUTOMATIC, RTE_APPL_DATA) Key,
																VAR(Dcm_OpStatusType, AUTOMATIC) OpStatus,
																CONSTP2VAR(Dcm_NegativeResponseCodeType, AUTOMATIC, RTE_APPL_DATA) ErrorCode)
{
	Std_ReturnType retValue = RTE_E_OK;
	
	if ( ( Key[ 0 ] != DcmKey[ 4 ] ) || ( Key[ 1 ] != DcmKey[ 5 ] ) )
	{
		retValue = SWC_COMPARE_KEY_FAILED;
	}
	else{ /* Do nothing. Key is correct. */ }
	
	return ( retValue );
}
FUNC(Std_ReturnType, TestDiag_CODE) Swc_DSP_SEC_LEV_L3_GS_func( VAR(Dcm_OpStatusType, AUTOMATIC) OpStatus,
        														CONSTP2VAR(uint8, AUTOMATIC, RTE_APPL_DATA) Seed,
																CONSTP2VAR(Dcm_NegativeResponseCodeType, AUTOMATIC, RTE_APPL_DATA) ErrorCode)
{
	Std_ReturnType retValue = RTE_E_OK;
	*Seed = DcmSeed[4];
	*(Seed+1) = DcmSeed[5];
	return (retValue);
}
FUNC(Std_ReturnType, TestDiag_CODE) Swc_DSP_SEC_LEV_L4_CK_func( P2CONST(uint8, AUTOMATIC, RTE_APPL_DATA) Key,
																VAR(Dcm_OpStatusType, AUTOMATIC) OpStatus,
																CONSTP2VAR(Dcm_NegativeResponseCodeType, AUTOMATIC, RTE_APPL_DATA) ErrorCode)
{
	Std_ReturnType retValue = RTE_E_OK;
	
	if ( ( Key[ 0 ] != DcmKey[ 6 ] ) || ( Key[ 1 ] != DcmKey[ 7 ] ) )
	{
		retValue = SWC_COMPARE_KEY_FAILED;
	}
	else{ /* Do nothing. Key is correct. */ }
	
	return ( retValue );
}
FUNC(Std_ReturnType, TestDiag_CODE) Swc_DSP_SEC_LEV_L4_GS_func( VAR(Dcm_OpStatusType, AUTOMATIC) OpStatus,
        														CONSTP2VAR(uint8, AUTOMATIC, RTE_APPL_DATA) Seed,
																CONSTP2VAR(Dcm_NegativeResponseCodeType, AUTOMATIC, RTE_APPL_DATA) ErrorCode)
{
	Std_ReturnType retValue = RTE_E_OK;
	*Seed = DcmSeed[6];
	*(Seed+1) = DcmSeed[7];
	return (retValue);
}
FUNC(Std_ReturnType, TestDiag_CODE) Swc_RoutineServices_DcmDspRoutine_CheckProgCond_Result_func(VAR(Dcm_OpStatusType, AUTOMATIC) OpStatus,
																								CONSTP2VAR(Dcm_RequestDataOut_DcmDspRoutine_CheckPgmPreCond_DcmDspRequestRoutineResultsOutSignal_0Type, AUTOMATIC, RTE_APPL_DATA) dataOut_DcmDspRequestRoutineResultsOutSignal_0,
																								CONSTP2VAR(Dcm_NegativeResponseCodeType, AUTOMATIC, RTE_APPL_DATA) ErrorCode)
{
	Std_ReturnType retValue = RTE_E_OK;
	*ErrorCode = ( Dcm_NegativeResponseCodeType )RTE_E_OK;
	RoutineControl_CheckProgCond_Status += 0x100;

	return ( retValue );
}
FUNC(Std_ReturnType, TestDiag_CODE) Swc_RoutineServices_DcmDspRoutine_CheckProgCond_Start_func( VAR(Dcm_OpStatusType, AUTOMATIC) OpStatus,
        																						CONSTP2VAR(Dcm_NegativeResponseCodeType, AUTOMATIC, RTE_APPL_DATA) ErrorCode)
{
	Std_ReturnType retValue = RTE_E_OK;
	*ErrorCode = ( Dcm_NegativeResponseCodeType )RTE_E_OK;
	RoutineControl_CheckProgCond_Status += 0x01;

	return ( retValue );
}
FUNC(Std_ReturnType, TestDiag_CODE) Swc_RoutineServices_DcmDspRoutine_CheckProgCond_Stop_func(  VAR(Dcm_StopDataIn_DcmDspRoutine_CheckPgmPreCond_DcmDspStopRoutineInSignal_0Type, AUTOMATIC) dataIn_DcmDspStopRoutineInSignal_0,
																								VAR(Dcm_OpStatusType, AUTOMATIC) OpStatus,
																								CONSTP2VAR(Dcm_StopDataOut_DcmDspRoutine_CheckPgmPreCond_DcmDspStopRoutineOutSignal_0Type, AUTOMATIC, RTE_APPL_DATA) dataOut_DcmDspStopRoutineOutSignal_0,
																								CONSTP2VAR(Dcm_NegativeResponseCodeType, AUTOMATIC, RTE_APPL_DATA) ErrorCode)
{
	Std_ReturnType retValue = RTE_E_OK;
	*ErrorCode = ( Dcm_NegativeResponseCodeType )RTE_E_OK;
	RoutineControl_CheckProgCond_Status += 0x10;

	return ( retValue );
}
FUNC(Std_ReturnType, TestDiag_CODE) Swc_RoutineServices_DcmDspRoutine_Test_Result_func( VAR(Dcm_OpStatusType, AUTOMATIC) OpStatus,
																						CONSTP2VAR(Dcm_RequestDataOut_DcmDspRoutine_Test_DcmDspRequestRoutineResultsOutSignal_0Type, AUTOMATIC, RTE_APPL_DATA) dataOut_DcmDspRequestRoutineResultsOutSignal_0,
																						CONSTP2VAR(Dcm_NegativeResponseCodeType, AUTOMATIC, RTE_APPL_DATA) ErrorCode)
{
	Std_ReturnType retValue = RTE_E_OK;
	*ErrorCode = ( Dcm_NegativeResponseCodeType )RTE_E_OK;
	RoutineControl_Test_Status += 0x100;

	return ( retValue );
}
FUNC(Std_ReturnType, TestDiag_CODE) Swc_RoutineServices_DcmDspRoutine_Test_Start_func(	VAR(Dcm_OpStatusType, AUTOMATIC) OpStatus,
        																				CONSTP2VAR(Dcm_NegativeResponseCodeType, AUTOMATIC, RTE_APPL_DATA) ErrorCode)
{
	Std_ReturnType retValue = RTE_E_OK;
	*ErrorCode = ( Dcm_NegativeResponseCodeType )RTE_E_OK;
	RoutineControl_Test_Status += 0x01;

	return ( retValue );
}
FUNC(Std_ReturnType, TestDiag_CODE) Swc_RoutineServices_DcmDspRoutine_Test_Stop_func(	VAR(Dcm_StopDataIn_DcmDspRoutine_Test_DcmDspStopRoutineInSignal_0Type, AUTOMATIC) dataIn_DcmDspStopRoutineInSignal_0,
																						VAR(Dcm_OpStatusType, AUTOMATIC) OpStatus,
																						CONSTP2VAR(Dcm_StopDataOut_DcmDspRoutine_Test_DcmDspStopRoutineOutSignal_0Type, AUTOMATIC, RTE_APPL_DATA) dataOut_DcmDspStopRoutineOutSignal_0,
																						CONSTP2VAR(Dcm_NegativeResponseCodeType, AUTOMATIC, RTE_APPL_DATA) ErrorCode)
{
	Std_ReturnType retValue = RTE_E_OK;
	*ErrorCode = ( Dcm_NegativeResponseCodeType )RTE_E_OK;
	RoutineControl_Test_Status += 0x10;

	return ( retValue );
}

/* DEM Callbacks */

FUNC(void, Diag_SWC_CODE) CBStatusComp_DemComponent_MCAL_Status_Cbk(VAR(boolean, AUTOMATIC) ComponentFailedStatus)
{}

FUNC(void, Diag_SWC_CODE) CBStatusComp_DemComponent_MCU_Status_Cbk(VAR(boolean, AUTOMATIC) ComponentFailedStatus)
{}

FUNC(void, Diag_SWC_CODE) CBStatusComp_DemComponent_MPDU_CommStatus_Cbk(VAR(boolean, AUTOMATIC) ComponentFailedStatus)
{}

FUNC(void, Diag_SWC_CODE) CBStatusComp_DemComponent_SEN_1_CommStatus_Cbk(VAR(boolean, AUTOMATIC) ComponentFailedStatus)
{}

FUNC(void, Diag_SWC_CODE) CBStatusComp_DemComponent_SEN_CommStatus_Cbk(VAR(boolean, AUTOMATIC) ComponentFailedStatus)
{}
