/*@!Encoding:936*/
includes
{
  #include "DoIP_TCPIP.cin"
  #include "DoIP_Flash.can"
  
}

variables
{
  
}

testfunction Initialize()
{
  int result;
  setWriteDbgLevel(cDebugWriteLevel);
  result = ConfigDUTAndTester();
  if(result == 0)
  {
    writeDbgLevel(cReleaseWriteLevel,"*******************************************");
    writeDbgLevel(cReleaseWriteLevel,"Tester Logic Address:0x%X",gTesterAddress);
    writeDbgLevel(cReleaseWriteLevel,"DUT Logic Address:0x%X",gVehicleAddress);
    writeDbgLevel(cReleaseWriteLevel,"DUT Func Logic Address:0x%X",gVehicleFunAddress);
    writeDbgLevel(cReleaseWriteLevel,"DUT IP Address:%s",DUTIpAddress);
    writeDbgLevel(cReleaseWriteLevel,"*******************************************");
  }
  
  sysvar::VTS::M1_VT7001.SetInterconnectionMode(eVTSInterconnectionModeSupInt);
  sysvar::VTS::M1_SupInt.SetRefVoltageMode(eVTSRefVoltageModeConstant);
  @sysvar::VTS::M1_SupInt::RefVoltage = 12+0.1;                                   //VT7001存在0.1V的压降
  @sysvar::VTS::M1_Out1::Active = 0;
  testWaitForTimeout(1000);                                                    //上电后等待一段时间 用于控制器的初始化
  
}

void PowerOnDUTAndWaitDUTInit()
{
  DoIP_CloseConnection();
  @sysvar::VTS::M1_Out1::Active = 1;
  testWaitForTimeout(5*1000);
  DoIPFlashTestFlagInit();
}

void PowerOffDUT()
{
  DoIP_CloseConnection();
  testWaitForTimeout(1*1000);
  @sysvar::VTS::M1_Out1::Active = 0;
  testWaitForTimeout(5*1000);
}

void DoIPFlashTestFlagInit()
{
  fDoIPTest_SkipProgrammingSession = 0;
  fDoIPTest_SkipSecurityAccess = 0;
  fDoIPTest_SecurityAccessFail = 0;
  fDoIPTest_FlashContentError = 0;
  fDoIPTest_SkipWriteFingerPrint = 0;
  fDoIPTest_DriverClearAfterPoweroff = 0;
  fDoIPTest_SkipDownloadFlashDriver = 0;
  fDoIPTest_SkipEarseMemory = 0;
  fDoIPTest_SkipRequestDownload = 0;
  fDoIPTest_SkipAppRequestDownload = 0;
  fDoIPTest_SkipTransmitData = 0;
  fDoIPTest_SkipTransmitAppData = 0;
  fDoIPTest_SkipRequestDownloadAndTransmitData = 0;
  fDoIPTest_SkipAppRequestDownloadAndTransmitData = 0;
  fDoIPTest_SkipFlashDataAndCheckProgrammingIntegrity = 0;
  fDoIPTest_SkipFlashAppDataAndCheckProgrammingIntegrity = 0;
  fDoIPTest_SkipFlashDataAndCheckProgrammingDependencies = 0;
  fDoIPTest_SkipCheckProgrammingDependencies = 0;
  fDoIPTest_SkipCheckProgrammingIntegrity = 0;
  fDoIPTest_SkipCheckAppProgrammingIntegrity = 0;
  fDoIPTest_CheckProgrammingIntegrity_ErrorCRC = 0;
  fDoIPTest_CheckAppProgrammingIntegrity_ErrorCRC = 0;
  
  fDoIPTest_PoweroffDuringEarseMemory = 0;
  fDoIPTest_PoweroffDuringFlashing = 0;
  fDoIPTest_PoweroffDuringFlashingApp = 0;
}

/// <TG1>
testcase TG1_TC1_RoutingActivationMessageTest()
{
  int routingActRespCode;
  //routingActType 0x00 -> 0xE0
  byte routingAct_SA[2] = {0x0E,0x80},routingActType = 0xE0;
  int result;
  
  testCaseTitle("1.1","路由激活报文交互测试");
  PowerOnDUTAndWaitDUTInit();
  testStep("1.0","如拓扑图1所示进行连接");
  result = DoIP_CreateTCPConnect(DUTIpAddress,0);
  if(result != 0)
  {
    testStepFail("2.0","测试仪发送TCP连接失败");
    PowerOffDUT();
    return;
  }
  else
  {
    testStepPass("2.0","测试仪发起TCP连接成功");
  }
  if(DoIP_SendRoutingActivateRequest(routingAct_SA,routingActType)!=0)
  {
    testStepFail("2.1","发送路由激活指令失败");
    DoIP_CloseConnect();
    PowerOffDUT();
    return;
  }
  else
  {
    testStepPass("2.1","发送路由激活指令成功");
  }
  routingActRespCode = DoIP_WaitRoutingActResp();
  if(routingActRespCode == -100)
  {
    testStepFail("3.0","DUT回复一条路由激活响应报文");
    DoIP_CloseConnect();
    PowerOffDUT();
    return;
  }
  else
  {
    testStepPass("3.0","DUT回复一条路由激活响应报文");
  }
  if(routingActRespCode >= 0x00)
  {
    if(routingActRespCode == 0x10)
    {
      testStepPass("4.0","测试仪接收到路由激活响应报文的响应码为0x10");
    }
    else
    {
      testStepFail("4.0","测试仪接收到路由激活响应报文的响应码为0x%X",routingActRespCode);
    }
  }
  DoIP_CloseConnect();
  PowerOffDUT();
}

/// <TG1>
testcase TG1_TC2_DiagnosticCommuncationInteractTest()
{
  int i;
  int routingActRespCode;
  byte routingAct_SA[2] = {0x0E,0x80},routingActType = 0x00;
  byte diagMsg[2] = {0x10,0x01};
  int result;
  
  testCaseTitle("1.1","路由激活报文交互测试");
  PowerOnDUTAndWaitDUTInit();
  testStep("1.0","如拓扑图1所示进行连接");
  result = DoIP_CreateTCPConnect(DUTIpAddress,0);
  if(result != 0)
  {
    testStepFail("2.0","测试仪发送TCP连接失败");
    PowerOffDUT();
    return;
  }
  else
  {
    testStepPass("2.0","测试仪发起TCP连接成功");
  }
  if(DoIP_SendRoutingActivateRequest(routingAct_SA,routingActType)!=0)
  {
    testStepFail("2.1","发送路由激活指令失败");
    DoIP_CloseConnect();
    PowerOffDUT();
    return;
  }
  else
  {
    testStepPass("2.1","发送路由激活指令成功");
  }
  routingActRespCode = DoIP_WaitRoutingActResp();
  if(routingActRespCode == -100)
  {
    testStepFail("3.0","DUT回复一条路由激活响应报文");
    DoIP_CloseConnect();
    PowerOffDUT();
    return;
  }
  else
  {
    testStepPass("3.0","DUT回复一条路由激活响应报文");
  }
  if(routingActRespCode >= 0x00)
  {
    if(routingActRespCode == 0x10)
    {
      testStepPass("4.0","测试仪接收到路由激活响应报文的响应码为0x10");
    }
    else
    {
      testStepFail("4.0","测试仪接收到路由激活响应报文的响应码为0x%X",routingActRespCode);
    }
  }
  result = DoIP_SendDiagMessage(gTesterAddress,gVehicleAddress,diagMsg,2);
  if(result != 0)
  {
    testStepFail("5.0","测试仪发送诊断请求： 10 01");
    DoIP_CloseConnect();
    PowerOffDUT();
    return;
  }
  else
  {
    testStepPass("5.0","测试仪发送诊断请求： 10 01");
  }
  result = DoIP_WaitDiagMessgeResp();
  if(result != 0)
  {
    testStepFail("6.0","DUT回复一条诊断肯定响应，然后再回复一条诊断信息报文");
    DoIP_CloseConnect();
    PowerOffDUT();
    return;
  }
  else
  {
    testStepPass("6.0","DUT回复一条诊断肯定响应，然后再回复一条诊断信息报文");
  }
  for(i=0;i<6;i++)
  {
    write("gDoIP_DiagMsg:0x%X",gDoIP_DiagMsg[i]);
  }
  if((gDoIP_DiagMsg[0] == 0x50) && (gDoIP_DiagMsg[1] == 0x01))
  {
    testStepPass("7.0","DUT发送诊断肯定响应");
  }
  else
  {
    testStepFail("7.0","DUT发送诊断负响应");
  }
  DoIP_CloseConnect();
  PowerOffDUT();
}

/// <TG2>
testcase TG2_TC1_FlashValidTest()
{
//  dword handle,size;
//  byte buff[1000];
//  handle = openFileRead("C:\\Users\\siee\\Desktop\\driver.bin",1);
//  size = fileGetBinaryBlock (buff,1000,handle);
//  write("size:%d",size);
  
  
  //暂时测试3E 80问题，先注释
  int result;
  testCaseTitle("2.1","正向刷写流程测试");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_FlashValidTest();
  PowerOffDUT();
  
  
  
  /*
  //notes
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  TestFor3E80();
  PowerOffDUT();
  */
}


/*
testfunction TestFor3E80()
{
  result = DoIP_Send3E80();
  if(result != 0) return;
}
*/


testfunction DoIPFlashTest_FlashValidTest()
{
  int result;
  testStep("","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("","执行刷写主编程步骤");
  result = ProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("","执行刷写后编程步骤");
  result = PostProgrammingPhase();
  if(result != 0)
  {
    return;
  }
}

/// <TG3>
testcase TG3_TC1_SkipProgrammingSession()
{
  int result;
  
  testCaseTitle("3.1","刷写时跳过执行10 02会话模式");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_SkipProgrammingSession();
  //DoIPFlashTest_FlashValidTest();
  PowerOffDUT();
}

testfunction DoIPFlashTest_SkipProgrammingSession()
{
  int result;
  testStep("1.0","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("2.0","执行刷写主编程步骤 跳过执行10 02会话模式");
  fDoIPTest_SkipProgrammingSession = 1;
  result = ProgrammingPhase();
  fDoIPTest_SkipProgrammingSession = 0;
}

/// <TG3>
testcase TG3_TC2_SkipSecurityAccess()
{
  int result;
  
  testCaseTitle("3.2","刷写时跳过安全访问");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_SkipSecurityAccess();
  //DoIPFlashTest_FlashValidTest();
  PowerOffDUT();
}

testfunction DoIPFlashTest_SkipSecurityAccess()
{
  int result;
  testStep("1.0","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("2.0","执行刷写主编程步骤 跳过安全访问");
  fDoIPTest_SkipSecurityAccess = 1;
  result = ProgrammingPhase();
  fDoIPTest_SkipSecurityAccess = 0;
}

/// <TG3>
testcase TG3_TC3_SecurityAccessFail()
{
  int result;
  
  testCaseTitle("3.3","安全访问失败");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_SecurityAccessFail();
//  DoIPFlashTest_FlashValidTest();
  PowerOffDUT();
}

testfunction DoIPFlashTest_SecurityAccessFail()
{
  int result;
  testStep("1.0","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("2.0","执行刷写主编程步骤 安全访问失败");
  fDoIPTest_SecurityAccessFail = 1;
  result = ProgrammingPhase();
  fDoIPTest_SecurityAccessFail = 0;
}

/// <TG3>
testcase TG3_TC4_FlashContentError()
{
  int result;
  
  testCaseTitle("3.4","刷写文件异常");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_FlashContentError();
//  DoIPFlashTest_FlashValidTest();
  PowerOffDUT();
}

testfunction DoIPFlashTest_FlashContentError()
{
  int result;
  testStep("1.0","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("2.0","执行刷写主编程步骤 刷写文件异常");
  fDoIPTest_FlashContentError = 1;
  result = ProgrammingPhase();
  fDoIPTest_FlashContentError = 0;
}

//TODO 跳过指纹写入 应该在下面那一步fail待确定
/// <TG3>
testcase TG3_TC5_SkipWriteFingerPrint()
{
  int result;
  
  testCaseTitle("3.5","跳过指纹写入");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_SkipWriteFingerPrint();
  //DoIPFlashTest_FlashValidTest();
  PowerOffDUT();
}

testfunction DoIPFlashTest_SkipWriteFingerPrint()
{
  int result;
  testStep("1.0","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("2.0","执行刷写主编程步骤 跳过指纹写入");
  fDoIPTest_SkipWriteFingerPrint = 1;
  result = ProgrammingPhase();
  fDoIPTest_SkipWriteFingerPrint = 0;
}

/// <TG3>
testcase TG3_TC6_DriverClearAfterPoweroff()
{
  int result;
  
  testCaseTitle("3.6","断电后FlashDriver清除");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_DriverClearAfterPoweroff();
  PowerOffDUT();
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_SkipDownloadFlashDriver();
  PowerOffDUT();
}

testfunction DoIPFlashTest_DriverClearAfterPoweroff()
{
  int result;
  testStep("1.0","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("2.0","执行刷写主编程步骤 驱动下载完成后关闭ECU电源");
  fDoIPTest_DriverClearAfterPoweroff = 1;
  result = ProgrammingPhase();
  fDoIPTest_DriverClearAfterPoweroff = 0;
}

//TODO 跳过下载Flash驱动 应该在哪一步check fail
/// <TG3>
testcase TG3_TC7_SkipDownloadFlashDriver()
{
  int result;
  
  testCaseTitle("3.7","跳过下载Flash Driver");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_SkipDownloadFlashDriver();
  //DoIPFlashTest_FlashValidTest();
  PowerOffDUT();
}

testfunction DoIPFlashTest_SkipDownloadFlashDriver()
{
  int result;
  testStep("1.0","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("2.0","执行刷写主编程步骤 跳过下载Flash Driver");
  fDoIPTest_SkipDownloadFlashDriver = 1;
  result = ProgrammingPhase();
  fDoIPTest_SkipDownloadFlashDriver = 0;
}

/// <TG3>
testcase TG3_TC8_SkipEarseMemory()
{
  int result;
  
  testCaseTitle("3.8","跳过擦除Memory");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_SkipEarseMemory();
  //DoIPFlashTest_FlashValidTest();
  PowerOffDUT();
}

testfunction DoIPFlashTest_SkipEarseMemory()
{
  int result;
  testStep("1.0","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("2.0","执行刷写主编程步骤 跳过擦除Memory");
  fDoIPTest_SkipEarseMemory = 1;
  result = ProgrammingPhase();
  fDoIPTest_SkipEarseMemory = 0;
}

/// <TG3>
testcase TG3_TC9_SkipRequestDownload()
{
  int result;
  
  testCaseTitle("3.9","跳过请求下载");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_SkipRequestDownload();
  //DoIPFlashTest_FlashValidTest();
  PowerOffDUT();
}

testfunction DoIPFlashTest_SkipRequestDownload()
{
  int result;
  testStep("1.0","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("2.0","执行刷写主编程步骤 跳过请求下载");
  fDoIPTest_SkipRequestDownload = 1;
  result = ProgrammingPhase();
  fDoIPTest_SkipRequestDownload = 0;
}

/// <TG3>
testcase TG3_TC10_SkipTransmitData()
{
  int result;
  
  testCaseTitle("3.10","跳过传输数据");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_SkipTransmitData();
  //DoIPFlashTest_FlashValidTest();
  PowerOffDUT();
}

testfunction DoIPFlashTest_SkipTransmitData()
{
  int result;
  testStep("1.0","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("2.0","执行刷写主编程步骤 跳过传输数据");
  fDoIPTest_SkipTransmitData = 1;
  result = ProgrammingPhase();
  fDoIPTest_SkipTransmitData = 0;
}

/// <TG3>
testcase TG3_TC11_SkipRequestDownloadAndTransmitData()
{
  int result;
  
  testCaseTitle("3.11","跳过请求下载和传输数据");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_SkipRequestDownloadAndTransmitData();
  //DoIPFlashTest_FlashValidTest();
  PowerOffDUT();  
}

testfunction DoIPFlashTest_SkipRequestDownloadAndTransmitData()
{
  int result;
  testStep("1.0","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("2.0","执行刷写主编程步骤 跳过请求下载和传输数据");
  fDoIPTest_SkipRequestDownloadAndTransmitData = 1;
  result = ProgrammingPhase();
  fDoIPTest_SkipRequestDownloadAndTransmitData = 0; 
}

/// <TG3>
testcase TG3_TC12_SkipFlashDataAndCheckProgrammingIntegrity()
{
  int result;
  
  testCaseTitle("3.12","跳过数据下载便执行请求完整性检查例程");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_SkipFlashDataAndCheckProgrammingIntegrity();
  //DoIPFlashTest_FlashValidTest();
  PowerOffDUT();  
}

testfunction DoIPFlashTest_SkipFlashDataAndCheckProgrammingIntegrity()
{
  int result;
  testStep("1.0","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("2.0","执行刷写主编程步骤 跳过数据下载便执行请求完整性检查例程");
  fDoIPTest_SkipFlashDataAndCheckProgrammingIntegrity = 1;
  result = ProgrammingPhase();
  fDoIPTest_SkipFlashDataAndCheckProgrammingIntegrity = 0; 
}

/// <TG3>
testcase TG3_TC13_SkipFlashDataAndCheckProgrammingDependencies()
{
  int result;
  
  testCaseTitle("3.13","跳过数据下载和完整性检查例程 执行依赖性检查例程");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_SkipFlashDataAndCheckProgrammingDependencies();
  //DoIPFlashTest_FlashValidTest();
  PowerOffDUT(); 
}

testfunction DoIPFlashTest_SkipFlashDataAndCheckProgrammingDependencies()
{
  int result;
  testStep("1.0","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("2.0","执行刷写主编程步骤 跳过数据下载和完整性检查例程 执行依赖性检查例程");
  fDoIPTest_SkipFlashDataAndCheckProgrammingDependencies = 1;
  result = ProgrammingPhase();
  fDoIPTest_SkipFlashDataAndCheckProgrammingDependencies = 0; 
}

/// <TG3>
testcase TG3_TC14_SkipCheckProgrammingDependencies()
{
  int result;
  
  testCaseTitle("3.14","跳过执行依赖性检查例程");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_SkipCheckProgrammingDependencies();
  //DoIPFlashTest_FlashValidTest();
  PowerOffDUT(); 
}

testfunction DoIPFlashTest_SkipCheckProgrammingDependencies()
{
  int result;
  testStep("1.0","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("2.0","执行刷写主编程步骤 跳过执行依赖性检查例程");
  fDoIPTest_SkipCheckProgrammingDependencies = 1;
  result = ProgrammingPhase();
  fDoIPTest_SkipCheckProgrammingDependencies = 0; 
}

/// <TG3>
testcase TG3_TC15_SkipCheckProgrammingIntegrity()
{
  int result;
  
  testCaseTitle("3.15","跳过执行完整性检查例程");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_SkipCheckProgrammingIntegrity();
  //DoIPFlashTest_FlashValidTest();
  PowerOffDUT(); 
}

testfunction DoIPFlashTest_SkipCheckProgrammingIntegrity()
{
  int result;
  testStep("1.0","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("2.0","执行刷写主编程步骤 跳过执行完整性检查例程");
  fDoIPTest_SkipCheckProgrammingIntegrity = 1;
  result = ProgrammingPhase();
  fDoIPTest_SkipCheckProgrammingIntegrity = 0; 
}

/// <TG3>
testcase TG3_TC16_CheckProgrammingIntegrity_ErrorCRC()
{
  int result;
  
  testCaseTitle("3.16","编程完整性检查例程错误");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_CheckProgrammingIntegrity_ErrorCRC();
  //DoIPFlashTest_FlashValidTest();
  PowerOffDUT(); 
}

testfunction DoIPFlashTest_CheckProgrammingIntegrity_ErrorCRC()
{
  int result;
  testStep("1.0","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("2.0","执行刷写主编程步骤 编程完整性检查例程错误");
  fDoIPTest_CheckProgrammingIntegrity_ErrorCRC = 1;
  result = ProgrammingPhase();
  fDoIPTest_CheckProgrammingIntegrity_ErrorCRC = 0; 
}

/// <TG4>
testcase TG4_TC1_PoweroffDuringEarseMemory()
{
  int result;
  
  testCaseTitle("4.1","擦除内存过程中断电");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_PoweroffDuringEarseMemory();
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_FlashValidTest();
  PowerOffDUT(); 
}

testfunction DoIPFlashTest_PoweroffDuringEarseMemory()
{
  int result;
  testStep("1.0","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("2.0","执行刷写主编程步骤 编程完整性检查例程错误");
  fDoIPTest_PoweroffDuringEarseMemory = 1;
  result = ProgrammingPhase();
  fDoIPTest_PoweroffDuringEarseMemory = 0; 
}

/// <TG4>
testcase TG4_TC2_PoweroffDuringFlashProgress10Percent()
{
  int result;
  
  testCaseTitle("4.2","应用程序刷写到10%时断电测试");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_PoweroffDuringFlashProgress(10);
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_FlashValidTest();
  PowerOffDUT(); 
}

/// <TG4>
testcase TG4_TC3_PoweroffDuringFlashProgress30Percent()
{
  int result;
  
  testCaseTitle("4.3","应用程序刷写到30%时断电测试");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_PoweroffDuringFlashProgress(30);
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_FlashValidTest();
  PowerOffDUT(); 
}

/// <TG4>
testcase TG4_TC4_PoweroffDuringFlashProgress70Percent()
{
  int result;
  
  testCaseTitle("4.4","应用程序刷写到70%时断电测试");
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_PoweroffDuringFlashProgress(70);
  PowerOnDUTAndWaitDUTInit();
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_FlashValidTest();
  PowerOffDUT();  
}

testfunction DoIPFlashTest_PoweroffDuringFlashProgress(int percent)
{
  int result;
  testStep("1.0","执行刷写预编程步骤");
  result = PreProgrammingPhase();
  if(result != 0)
  {
    return;
  }
  testStep("2.0","执行刷写主编程步骤 应用程序刷写到%d%时断电测试",percent);
  fDoIPTest_PoweroffDuringFlashing = 1;
  gDoIPTest_PoweroffDuringFlashing_TargetProgress = percent;
  result = ProgrammingPhase();
  fDoIPTest_PoweroffDuringFlashing = 0;
}

/// <TG4>
testcase TG4_TC5_PoweroffDuringLowVoltage()
{
  int result;
  testCaseTitle("4.5","低电压下刷写测试");
  PowerOnDUTAndWaitDUTInit();
  @sysvar::VTS::M1_SupInt::RefVoltage = 9.1;
  testWaitForTimeout(1000);
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_FlashValidTest();
  PowerOffDUT();
}

/// <TG4>
testcase TG4_TC6_PoweroffDuringHighVoltage()
{
  int result;
  testCaseTitle("4.7","高电压下刷写测试");
  PowerOnDUTAndWaitDUTInit();
  @sysvar::VTS::M1_SupInt::RefVoltage = 16.1;
  testWaitForTimeout(1000);
  result = ConnectDUT();
  if(result != 0)
  {
    return;
  }
  DoIPFlashTest_FlashValidTest();
  PowerOffDUT();
}