/*@!Encoding:1252*/
includes
{
  #include "Baudrate.cin"
  #include "vFlash\Utilities.cin"
}

variables
{
  float gFlashingBaudRate = 500000; // this is the increased baud rate during flashing
  char  gFlashpack[400] = "flashpacks\\demo.vflashpack";  
}

void MainTest()
{
  // the baud rate is read out and saved before switching to the flashing-baud-rate
  float testingBaudRate1;
  float testingBaudRate2;

  @vFlash::Progress = 0;
  
  TestModuleTitle("vFlash Automation Demo");
  TestModuleDescription("A simple example of how to use the vFlash for flashing a flashpack.");

  // increase baud rate for flashing (if necessary). Remember the old testing baud rate.
  testingBaudRate1 = SetBaudRate(gFlashingBaudRate, 1);
  testingBaudRate2 = SetBaudRate(gFlashingBaudRate, 2);

  BlockingReprogramming();
  NonBlockingReprogramming();

  // Reset baud rate after flashing to the old testing baud rate (if necessary).
  SetBaudRate(testingBaudRate1, 1);
  SetBaudRate(testingBaudRate2, 2);
}


testcase BlockingReprogramming()
{
  enum vFlashStatusCode result;
  
  TestCaseTitle("TC 1.0", "Blocking Reprogramming API");
  TestCaseDescription("Using TestWaitFor...() API (blocking calls allowed in TestModules only)");
  write("Start BlockingReprogramming Test");
  @vFlash::Progress = 0;
  
  result = TestWaitForvFlashPackReprogrammed(gFlashpack); 
  if (result != Success)
  {
    TestStepFail("Reprogramming failed. See CANoe Write window for detailed information.");
  }
  else
  {
    write("... BlockingReprogramming successfully finished");
  }
}


testcase NonBlockingReprogramming()
{
  TestCaseTitle("TC 2.0", "Non Blocking Reprogramming API");
  TestCaseDescription("Using vFlash...() API (all calls are asynchronous)");
  write("Start NonBlockingReprogramming Test");
  @vFlash::Progress = 0;
    
  vFlashStartPackReprogramming(gFlashpack);
  
  // wait explicit until all steps are performed asynchronous
  testWaitForTextEvent(gReprogrammingDone, 450000); 

  if (gReprogrammingResult != Success)
  {
    TestStepFail("Reprogramming failed. See CANoe Write window for detailed information.");
  }
  else
  {
    write("... NonBlockingReprogramming successfully finished");
  }
}
  
// Will be called when the flash progress changed
void vFlashProgramProgressCallback(DWORD progressInPercent, DWORD remainingTimeInS)
{
  @vFlash::Progress = progressInPercent;
}
