/******************************************************************************/
/* Williams Advanced Engineering Limited                     Copyright(C) 2021*/
/* Grove                                                  All rights reserved */
/*                                                                            */
/******************************************************************************/


#include "DcmAppl.h"
#include "DcmCore_DslDsd_Prot.h"
#include "Os_TimeServices.h"
#include "FBL_DataMCfg.h"

/* [$Satisfies $RTA_FBL_SDK_DD_PORT 137] */
#define FBL_PORT_ECU_RESET_TIMEOUT_MS           (55U)
/* [$Satisfies $RTA_FBL_SDK_DD_PORT 145] */
static uint32 Fbl_Port_ResponsePendingTimer = 0U;
/* [$Satisfies $RTA_FBL_SDK_DD_PORT 33] */


void Fbl_Port_ResetEcu(void)
{
    //todo WAE to implement
	DcmAppl_Switch_DcmExecuteReset();

}

/* [$Satisfies $RTA_FBL_SDK_DD_PORT 153]  */
Std_ReturnType DcmAppl_DcmEcuResetPreparation(uint8 ResetType, Dcm_NegativeResponseCodeType * ErrorCode)
{
  VAR(Std_ReturnType,AUTOMATIC) retVal = DCM_E_PENDING;
  static boolean                ECUResetFlag = (boolean)FALSE;
  Dcm_ProgConditionsType        programmingConditions;
  *ErrorCode = 0x00u;
  /* Check if the flag is not set */
  if ((boolean)FALSE == ECUResetFlag)
  {
    /* Set flag */
    ECUResetFlag = (boolean)TRUE;
    /* Fill Dcm structure to save request */
    programmingConditions.ProtocolId = DCM_UDS_ON_CAN;
    programmingConditions.Sid = 0x51u;  /*Part of DiagStatus Flag*/
    programmingConditions.SubFncId = ResetType;
    programmingConditions.StoreType = DCM_WARMRESPONSE_TYPE;
    programmingConditions.SessionLevel = 0x1u;
    programmingConditions.SecurityLevel = 0x0u;
    programmingConditions.ReqResLen = 0x2u;
    programmingConditions.TesterSourceAddr = Dcm_DsldConnTable_pcst[Dcm_DsldRxTable_pcu8[Dcm_DsldMsgContext_st.dcmRxPduId]].testaddr_u16;
    programmingConditions.ResponseRequired = (boolean)TRUE;
    programmingConditions.ElapsedTime = 0x00u;
    programmingConditions.freeForProjectUse[0u] = 0x00u;    /*Part of DiagStatus Flag: 0 - FBL; 1-App*/
    (void)OS_TimerExt_Start(&Fbl_Port_ResponsePendingTimer);
#ifdef __PQRA_QAC__
#pragma PRQA_MESSAGES_OFF 310
#endif
    /* MISRA Deviation: Casting to different object pointer type is acceptable */
    /* store response to send at next startup */
    (void)Fbl_DataM_NvmProgramConditionsWriteFunc((uint8*)&programmingConditions);
    (void)Dcm_ForceRespPend();
#ifdef __PQRA_QAC__
#pragma PRQA_MESSAGES_ON
#endif
  }
  /* PRQA S 3415 1 *//* <Deviation: rule_13.5_1> */
  else if (((boolean)TRUE == Fbl_DataM_NvmAllBlocksArePersisted()) && ((boolean)TRUE == OS_TimerExt_IsElapsed(Fbl_Port_ResponsePendingTimer,FBL_PORT_ECU_RESET_TIMEOUT_MS)))
  {
    /* reset ECU before transmitting positive response */
    Fbl_Port_ResetEcu();
  }
  else
  {
  }
  return retVal;
}
