/*@!Encoding:1252*/
includes
{
  /* You can source out constants, variables and functions 
     that are needed identically in multiple CAPL programs.*/
  #include "..\CAPL Includes\DoorInclFile.cin"
}

variables
{
  //variables for diagnostic
  char gECU[15] = "%NODE_NAME%";
  double gSerNum = 12584075;
  int gFaultMemClear = 0;
  int gDefaultSession = 1;
  int gProgrammingSession = 0;
  int gCountryVar = 1;
  int gVehicleType = 1;
  byte gSpecialSetting = 0x0;

}

on start
{
  // set to 2 to get more information into write-window 
  setWriteDbgLevel(1);
  writeDbgLevel(2,gECU);

  SetMoveTimer();
}

on timer tMoveDownTimer
{
  if(gPosWN > 15 || gPosWN < 0 ) runError(1003,1003);

  if(gPosWN < 15 && gMoveDown == gTrue)
  { 
    gPosWN++;
    $DOOR_l::WN_Position = gPosWN;
  }

  setTimer(this,cMoveTime);
}

on timer tMoveUpTimer
{
  if(gPosWN > 15 || gPosWN < 0) runError(1004,1004);
  
  if(gPosWN > 0 && gMoveUp == gTrue) 
  {
    gPosWN--;
    $DOOR_l::WN_Position = gPosWN;
  }

  setTimer(this,cMoveTime);
}

on Message Console_1
{
  if(this.WN_left_up)
  {
    gMoveUp = gTrue;
    ReinitSleepTimer();
  }
  else gMoveUp = gFalse;


  if(this.WN_left_down)
  {  
    gMoveDown = gTrue;
    ReinitSleepTimer();
  }
  else gMoveDown = gFalse;
}

on diagRequest Door_Left.*
{
  //Diagnostic Request not supported
  DiagSendNegativeResponse(this,0x11);
}

on timer tWakeUpDsp
{
  @sysvar::NMTester::NMWakeUpDsp27 = 0;
}

on preStart
{
  canOffline(3);
}

on message Gateway_1
{
   if(this.Ig_15  == 1)  canOnline(3);
   if(this.Ig_15R == 1)  canOnline(3);
}

on diagRequest Door_Left.FAULT_MEMORY_DeleteAll
{
  diagResponse this resp;
  word dtc;

  gFaultMemClear = 1;

  dtc = DiagGetParameter(this, "GROUP_OF_DTC_RQ");

  // Set the parameters in the response.
  DiagSetParameter( resp, "GROUP_OF_DTC_RQ", dtc);

  /* Sends the response object back to the tester. 
  Can only be called in the ECU simulation. */
  DiagSendResponse( resp);
}

on diagRequest Door_Left.Tester_Present_Send_Response
{
  diagResponse this diagResp;

  DiagSendResponse(diagResp);
}

on diagRequest Door_Left.Development_Data_Read
{
  /* The server sends an identification datarecord included 
  in the ReadECUIdentification positive response message.*/

  diagResponse this resp;

  if(!gDefaultSession)
  {
    //Service not supported in active diagnostic mode
    DiagSendNegativeResponse(this,0x80);
    return;
  }

  /* Sets a parameter to the symbolically-specified value. This is possible 
  for all parameters, also numeric ones.  */
  DiagSetParameter( resp, "Operating_System_Version", 0x12);
  DiagSetParameter( resp, "CAN_Driver_Version", 0x31);
  DiagSetParameter( resp, "NM_Version", 0x99A4);
  DiagSetParameter( resp, "Diagnostic_Module_Version", 77);
  DiagSetParameter( resp, "Transport_Layer_Version", 99);

  /* Sends the response object back to the tester. 
  Can only be called in the ECU simulation. */
  DiagSendResponse( resp);
}

on diagRequest Door_Left.ECU_Identification_Read
{
  /* The server sends an identification datarecord included in the 
  ReadECUIdentification positive response message. */

  diagResponse this resp;

  if(!gDefaultSession)
  {
    //Service not supported in active diagnostic mode
    DiagSendNegativeResponse(this,0x80);
    return;
  }

  // Set the parameters in the response.
  DiagSetParameter( resp, "Ident_Number_7_6", "6789");
  DiagSetParameter( resp, "Ident_Number_5_4", "1975");
  DiagSetParameter( resp, "Ident_Number_3_2", "1804");
  DiagSetParameter( resp, "Ident_Number_1_0", "0100");
  DiagSetParameter( resp, "Diagnostic_Identification", 2);

  /* Sends the response object back to the tester. 
  Can only be called in the ECU simulation. */
  DiagSendResponse( resp);
}

on diagRequest Door_Left.Serial_Number_Read
{
  /* The server sends an identification datarecord included in the 
  ReadECUIdentification positive response message. */

  diagResponse this resp;

  if(!gDefaultSession)
  {
    //Service not supported in active diagnostic mode
    DiagSendNegativeResponse(this,0x80);
    return;
  }

  // Set the parameters in the response.
  DiagSetParameter( resp, "Serial_Number", gSerNum);

  /* Sends the response object back to the tester. 
  Can only be called in the ECU simulation. */
  DiagSendResponse( resp);
}

on diagRequest Door_Left.DEFAULT_SESSION_Start
{
  /* The application functionality starts here the default session. */

  diagResponse this resp;

  gDefaultSession = 1;
  gProgrammingSession = 0;

  /* Sends the response object back to the tester. 
  Can only be called in the ECU simulation. */
  DiagSendResponse( resp);
}

on diagRequest Door_Left.ProgrammingSession_Start
{
  /* The application functionality starts here the programming session. */

  diagResponse this resp;

  gDefaultSession = 0;
  gProgrammingSession = 1;

  /* Sends the response object back to the tester. 
  Can only be called in the ECU simulation. */
  DiagSendResponse( resp);
}

on diagRequest Door_Left.STOP_SESSION_Stop
{
  /* The application functionality stops 
  here the active default session. */

  diagResponse this resp;

  gDefaultSession = 0;
  gProgrammingSession = 0;

  /* Sends the response object back to the tester. 
  Can only be called in the ECU simulation. */
  DiagSendResponse(resp);
}

on diagRequest Door_Left.Coding_Read
{
  /* The server sends an identification datarecord included 
  in the ReadECUIdentification positive response message.*/

  diagResponse this resp;

  if(!gDefaultSession)
  {
    //Service not supported in active diagnostic mode
    DiagSendNegativeResponse(this,0x80);
    return;
  }

  /* Sets a parameter to the symbolically-specified value. This is possible 
  for all parameters, also numeric ones.  */
  DiagSetParameter( resp, "Code_string.Country_variant", gCountryVar);
  DiagSetParameter( resp, "Code_string.Vehicle_type", gVehicleType);
  DiagSetParameter( resp, "Code_string.Special_setting", gSpecialSetting);

  /* Sends the response object back to the tester. 
  Can only be called in the ECU simulation. */
  DiagSendResponse( resp);
}

on diagRequest Door_Left.FAULT_MEMORY_ReadAllIdentifiedTroubleCodes
{
  /* This request of the tester, will read the identified trouble code 
  of the ECU. The ECU will send back the List of selected DTC status */
  diagResponse this respFaultMem;
  int count = 0; 
  int curDTC1;
  int curDTC2;
  int curDTC3;
  int curDTC4;
  int curDTC5;
  int curDTC6;
  int nrOfcurDTCs;

  curDTC2 = 0;
  curDTC4 = 0;

  if(gFaultMemClear)
  {
    gFaultMemClear=0;
    DiagSetParameter(respFaultMem, "NUMBER_OF_DTC", 0);
    DiagSendResponse(respFaultMem);    
    return;
  } 

  curDTC1 = random(2);
  if(!curDTC1) curDTC2 = random(2);
  curDTC3 = random(2);
  if(!curDTC3) curDTC4 = random(2);
  curDTC5 = random(2);
  curDTC6 = random(2);

  nrOfcurDTCs = curDTC1 + curDTC2 + curDTC3 + curDTC4 + curDTC5 + curDTC6;

  /* Sets a parameter to the symbolically-specified value. This is possible 
  for all parameters, also numeric ones.  */
  DiagSetParameter(respFaultMem, "NUMBER_OF_DTC", nrOfcurDTCs);

  /* Size adjustment of the diagnostics object to the desired number of 
  repetitions of the complex parameters. */
  if(nrOfcurDTCs!=0) DiagResize(respFaultMem);

  /* DiagSetComplexParameter functions sets one of the sub-parameters within a 
  complex parameter to the specified (numeric or symbolic) value. For this 
  first the complex parameter, that is, the name of the iteration, must be 
  specified; then the number of repetitions of the sub-parameter list that 
  is the goal, and then the sub-parameter in the iteration itself. 
  */
  if(curDTC1)
  { 
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTC",0x9001);
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestFailed",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestFailedThisMonitoringCycle",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.PendingDTC",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.ConfirmedDTC",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestNotCompletedSinceLastClear",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestFailedSinceLastClear",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestNotCompletedThisMonitoringCycle",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.WarningIndicatorRequested",random(2));
    count = count + 1;
  }
  if(curDTC2)
  { 
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTC",0x9002);
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestFailed",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestFailedThisMonitoringCycle",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.PendingDTC",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.ConfirmedDTC",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestNotCompletedSinceLastClear",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestFailedSinceLastClear",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestNotCompletedThisMonitoringCycle",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.WarningIndicatorRequested",random(2));
    count = count + 1;
  }

  if(curDTC3)
  { 
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTC",0x9013);
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestFailed",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestFailedThisMonitoringCycle",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.PendingDTC",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.ConfirmedDTC",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestNotCompletedSinceLastClear",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestFailedSinceLastClear",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestNotCompletedThisMonitoringCycle",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.WarningIndicatorRequested",random(2));
    count = count + 1;
  }

  if(curDTC4)
  { 
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTC",0x9014);
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestFailed",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestFailedThisMonitoringCycle",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.PendingDTC",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.ConfirmedDTC",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestNotCompletedSinceLastClear",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestFailedSinceLastClear",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestNotCompletedThisMonitoringCycle",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.WarningIndicatorRequested",random(2));
    count = count + 1;
  }
  if(curDTC5)
  { 
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTC",0x9015);
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestFailed",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestFailedThisMonitoringCycle",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.PendingDTC",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.ConfirmedDTC",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestNotCompletedSinceLastClear",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestFailedSinceLastClear",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestNotCompletedThisMonitoringCycle",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.WarningIndicatorRequested",random(2));
    count = count + 1;
  }
  if(curDTC6)
  { 
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTC",0x9016);
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestFailed",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestFailedThisMonitoringCycle",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.PendingDTC",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.ConfirmedDTC",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestNotCompletedSinceLastClear",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestFailedSinceLastClear",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.TestNotCompletedThisMonitoringCycle",random(2));
    DiagSetComplexParameter( respFaultMem, "LIST_OF_DTC_AND_STATUS", count, "DTCStatusByte.WarningIndicatorRequested",random(2));
  }


  /* Sends the response object back to the tester. 
  Can only be called in the ECU simulation. */
  count = 0; 
  DiagSendResponse(respFaultMem);
}

on diagRequest Door_Left.Tester_Present_Send_No_Response
{
  //send no response, because no response is required
}

on sysvar_update sysvar::NMTester::NMOnOff27
{
{
  dword var;

  if(@this) 
  {
    SilentNM();
    var = canOffline(3); 
  }
  else 
  {
    TalkNM();
    var = canOnline(3); 
  } 
}
}

on sysvar_update sysvar::NMTester::NMSleep27
{
  if(@this)
  {
    // Set Sleep indication.
    //---------------------
    GotoMode_BusSleep();
  }
}

on sysvar_update sysvar::NMTester::NMWakeUp27
{
  GotoMode_Awake();
  @sysvar::NMTester::NMWakeUpDsp27 = 1;

  WakeUpDsp();

  ReinitSleepTimer();
}

