// JWDogTst.cpp ansi_c&windows
// JUMPtec WatchDog API Demo
// Copyright 1999 JUMPtec AG
// {G)U(2} 1998.03.11 2001.10.19

//***************************************************************************

#include "JidaApp.h"

//***************************************************************************

static TCHAR szAppTitle[]=TEXT("JUMPtec WatchDog API Demo");

//***************************************************************************

DeclareMain(jidaTestWD)
  {
  // perform a hard reset if -r command line parameter is present
#ifdef WIN32
  BOOL bReset=_tcsstr(lpCmdLine,TEXT("-r"))!=NULL;
#else
  BOOL bReset=TRUE;
#endif
  HJIDA hJida; // Our JIDA board handle

  // Initialize the JIDA API
  if (!JidaDllInitialize()) {
    // The init has failed
    MessageBox(0,TEXT("Jida Driver is incompatible or not installed"),szAppTitle,0);
    return 0;
    }

  // Open any CPU class board that might have a watchdog
  if (!JidaBoardOpen(JIDA_BOARD_CLASS_CPU,0,0,&hJida)) {
    MessageBox(0,TEXT("Could not open a CPU Board"),szAppTitle,0);
    }
  else {
    if (MessageBox(0,
        TEXT("This progam will set up a watch dog that must be triggered within 10 seconds. ")
        TEXT("You must confirm the next message box with this 10 second interval or ")
        TEXT("Windows will be restarted."),
        szAppTitle,MB_OKCANCEL)==IDOK) {

      // Set a WatchDog time out of 10 seconds (10000 milliseconds)
      // and a initial delay of 0 seconds.
      // We also want the WatchDog to restart Windows in a clean way
      // as opposed to performing a hard reboot unless -r is specified.

      JidaWDogSetConfig(hJida,0,10000,0,(bReset)?JWDModeRebootPC:JWDModeRestartWindows);

      while (MessageBox(0,
          TEXT("You must press OK within 10 seconds in order to trigger the watch dog. ")
          TEXT("Otherwise the watch dog will restart Windows. Press Cancel to ")
          TEXT("set the watch dog time out to an hour and end this demo."),
          szAppTitle,MB_OKCANCEL)==IDOK) {

        // Trigger the WatchDog so that it won't shut us down.
        JidaWDogTrigger(hJida,0);
        }

      // Turn off the WatchDog again 
      // This is not supported by current watch dog implementations!!
      // JidaWDogDisable(hJida,0);

      // We just set the time out to an hour
      JidaWDogSetConfig(hJida,0,3600000,0,JWDModeRestartWindows);
      }

    // Let go of the JIDA board
    JidaBoardClose(hJida);
    }

  // Let the JIDA API clean up
  JidaDllUninitialize();
  return 0;
  }
