AUX INTERLOCK Programming 2015.09.04 14:33

#include "tester.h"

//============================================================================
// AUX Supply checker code
int get_auxsupply_state( int apb ) {

 CString cmd;
 cmd.Format( "pccmd.exe -t apb%d -b \"get auxsupply_state\"", apb );
 char state[1024] = {0};

 SECURITY_ATTRIBUTES sa = {0};
 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
 sa.bInheritHandle = TRUE;

 int maxtrys = 3;// Max number of times to retry on failure
 int timeout = 5;// Delay between retrys in seconds
 static int trys = 0;// Retry counter

 //Syslog mysyslog;

 HANDLE rp, wp;
 if( CreatePipe( &rp, &wp, &sa, 0 ) ) { // Last 0 means use default buffer size

  SetHandleInformation(rp, HANDLE_FLAG_INHERIT, 0);

  STARTUPINFO si = { 0 };
  PROCESS_INFORMATION pi = { 0 };
  si.cb = sizeof( STARTUPINFO );
  si.dwFlags |=  STARTF_USESTDHANDLES;
  si.hStdOutput = wp;
  si.hStdError = wp;

  CreateProcess( NULL, cmd.GetBuffer( cmd.GetLength() ), NULL, NULL, TRUE, CREATE_NO_WINDOW|DETACHED_PROCESS, NULL, NULL, &si, &pi );
  CloseHandle( pi.hThread );
  CloseHandle( pi.hProcess );
  CloseHandle( wp );

  DWORD br;
  ReadFile( rp, state, sizeof(state), &br, NULL );
  CloseHandle( rp );

  if ( state[0] != '0' && state[0] != '1' ) {
   warning( state );
   if ( ++trys <= maxtrys ) {
    warning( "Retry %d of %d...", trys, maxtrys );
    Delay( timeout );
    return get_auxsupply_state( apb );
   }
   else {
    warning( "Failed to communicate with ABP%d after %d retrys.", apb, maxtrys );
    warning( "Please verify APB%d operation and try again.", apb );
   }
  }
 }

 // Reset retry counter
 trys = 0;

 // This will return zero when the value of status is '0'
 // AND when the value of status cannot be converted to
 // an int, such as when pccmd.exe returns an error message
 // or cannot be found.
 return atoi( state );
}

VOID_VARIABLE( Aux_Supply_Checker, "" )
{
 output( "Executing Aux_Supply_Checker" );

 int aux1 = get_auxsupply_state( 1 );
 int aux2 = get_auxsupply_state( 2 );

 if ( aux1 == 0 || aux2 == 0 ) {
  CString warnMsg = vFormat( "APB%s AUX supplies have power faulted.\n\n", aux1 + aux2 == 0 ? "1 and APB2" : aux1 == 0 ? "1" : "2" );
  warnMsg += "Check the loadboard and/or probecard for problems.\n\n";
  warnMsg += "Also check that the APB is responding and operating normally.\n\n";
  warnMsg += "Finally, check that the MPC has not faulted which can result in AUX power down.\n\n";
  warnMsg += "When ready, please press the Green Site Power button in HostMonView to reset the AUX supplies.\n\n";
  warnMsg += "Then either press Retry to continue or Cancel to unload the program.";
  warning( warnMsg );
  if ( message_box ( MB_RETRYCANCEL, warnMsg ) == IDCANCEL )
   remote_set( "ui_Close", 0, -1, TRUE, INFINITE );
 }

 output( "APB1 AUX Supply reports AUXSUPPLY_STATE is %d", aux1 );
 output( "APB2 AUX Supply reports AUXSUPPLY_STATE is %d", aux2 );
}

INITIALIZATION_HOOK( Suppress_Aux_Supply_Checker_timeout )
{
 struct local {
  static int message_box_interceptor( LPCTSTR text, UINT type ) { return IDRETRY; }
 };
 intercept_message_box( local::message_box_interceptor );

 remote_set( "ui_AddInterceptedDialogRegexp", "Aux_Supply_Checker", -1, TRUE, INFINITE );
}

/*
AFTER_TESTING_BLOCK( Wait_for_Aux_Supply_Checker )
{
 if ( site_num() == 1 ) {
  //DUT_DefaultState();
  remote_set( Aux_Supply_Checker, 0, 0, TRUE, INFINITE );
 }
}
*/


//#############################################################################
//#############################################################################
//#############################################################################
//#############################################################################
//#############################################################################
//#############################################################################
// NEW CODE!!!!
//#############################################################################
//#############################################################################
//#############################################################################
//#############################################################################
//#############################################################################
BOOL_VARIABLE(PGM_UNLOADED, TRUE, "") {}
VOID_VARIABLE(AUX_CHECKER_LOOP, "")
{
 PGM_UNLOADED = FALSE;
 while(!PGM_UNLOADED)
 {
  remote_set( Aux_Supply_Checker, 0, 0, TRUE, INFINITE );
  Sleep(10 * 1000);  // 10 secs
 }
 output("EXIT AUX_CHECKER_LOOP!!");
}

VOID_VARIABLE(ui_ProgLoaded, "")
{
 remote_set(AUX_CHECKER_LOOP, 0, 0, TRUE);
}

VOID_VARIABLE(ui_ProgUnLoaded, "")
{
 PGM_UNLOADED = TRUE;
}