// KomXXXDemo.cpp : Defines the entry point for the console application.
//

#pragma once


#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers
#include <stdio.h>
#include <tchar.h>
#include <conio.h>

#include <stdlib.h>
#include "KomWdg.h"


//****************************************************************************
// ExitIfError - helper function that checks if the status is OK.
//				In error conditions it prints error message and closes the 
//				application.
void ExitIfError(DWORD status,BOOL printOK=FALSE)
{
	if (status!=ERROR_SUCCESS)
	{	char buf[256];
		
		FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM,
						NULL, status, 0, buf, 255, NULL);

		printf("error 0x%08x (%s)\n",status,buf);

		exit(status);
	}

	if (printOK==TRUE)
		printf("OK\n");
}

//****************************************************************************
// GetRestoreOnRestart - Gets the RestoreOnRestart setting
//			Demonstrates the usage of KomWdgGetRestoreStatus
void GetRestoreOnRestart()
{	BOOL fRestore;
	DWORD status;
	
	printf("\n\nGetting RestoreOnRestart mode... ");
	status=KomWdgGetRestoreStatus(&fRestore);

	ExitIfError(status,TRUE);

	if (fRestore==TRUE)
		printf("\nRestoreOnRestart is ON.\n");
	else
		printf("\nRestoreOnRestart is OFF.\n");
}

//****************************************************************************
// SetRestoreOnRestart - Sets the RestoreOnRestart setting
//			Demonstrates the usage of KomWdgSetRestoreStatus
void SetRestoreOnRestart()
{	char ch;
	DWORD status;

	printf("Enable RestoreOnRestart? (Y for yes): ");
	getchar();
	ch=getchar();

	if ((ch=='y') || (ch=='Y'))
	{
		printf("Turning RestoreOnRestart ON... ");
		status=KomWdgSetRestoreStatus(TRUE);
	}
	else	//turn off
	{
		printf("Turning RestoreOnRestart OFF... ");
		status=KomWdgSetRestoreStatus(FALSE);
	}

	ExitIfError(status,TRUE);
}


//****************************************************************************
// GetWdgConfig - Gets watchdog configuration and timeout status.
//			Demonstrates the usage of KomWdgGetConfig and KomWdgCheckTimeoutStatus
void GetWdgConfig(void)
{
	DWORD status = ERROR_SUCCESS;
	tKomWdgConfig Config;
	BOOL fTimeout;

	status = KomWdgGetConfig(&Config);
	ExitIfError(status);

	status = KomWdgCheckTimeoutStatus(&fTimeout);
	ExitIfError(status);
	
	printf("Watchdog configuration:\n");
	printf("Timeout value:    %s\n", sKomWdgTimeoutValue[Config.bTimeout]);
	printf("Expiration mode:  %s\n", sKomWdgMode[Config.bMode]);
	printf("Enabled:          %s\n", Config.fEnabled?"Yes":"No");
	printf("Watchdog expired: %s\n", fTimeout?"Yes":"No");

	return;
}

//****************************************************************************
// SetWdgMode - Sets watchdog mode.
//			Demonstrates the usage of KomWdgGetConfig and KomWdgSetMode
void SetWdgMode(void)
{
	DWORD status = ERROR_SUCCESS;
	tKomWdgConfig Config;
	int choice;

	// check whether watchdog is enabled or not
	status = KomWdgGetConfig(&Config);
	ExitIfError(status);

	if(TRUE == Config.fEnabled)
	{
		printf("Watchdog already enabled - no change of mode possible!\n");
		return;
	}

	printf("Select mode to be set:\n");
	printf("1 %s\n", sKomWdgMode[0]);
	printf("2 %s\n", sKomWdgMode[1]);
	printf("3 %s\n", sKomWdgMode[2]);
	printf("4 %s\n", sKomWdgMode[3]);
	printf("\n");
	printf("0 abort\n");

	do
	{
		scanf("%d",&choice);
	} while ((choice<0) || (choice>5));

	if(0 == choice)
		return;

	status = KomWdgSetMode(choice - 1);
	ExitIfError(status);

	return;
}

//****************************************************************************
// SetWdgTimeout - Sets watchdog timeout.
//			Demonstrates the usage of KomWdgGetConfig and KomWdgSetTimeout
void SetWdgTimeout(void)
{
	DWORD status = ERROR_SUCCESS;
	tKomWdgConfig Config;
	int choice, cnt;

	// check whether watchdog is enabled or not
	status = KomWdgGetConfig(&Config);
	ExitIfError(status);

	if(TRUE == Config.fEnabled)
	{
		printf("Watchdog already enabled - no change of timeout possible!\n");
		return;
	}

	printf("Select mode to be set:\n");
	for(cnt = 0; cnt < 12; cnt++)
	{
		printf("%d %s\n", cnt + 1, sKomWdgTimeoutValue[cnt]);
	}
	printf("\n");
	printf("0 abort\n");

	do
	{
		scanf("%d",&choice);
	} while ((choice<0) || (choice>12));

	if(0 == choice)
		return;

	status = KomWdgSetTimeout((BYTE) (choice - 1));
	ExitIfError(status);

	return;
}


//****************************************************************************
// CheckWdgTimeoutStatus - Checks watchdog timeout status.
//			Demonstrates the usage of KomWdgCheckTimeoutStatus
void CheckWdgTimeoutStatus(void)
{
	DWORD status = ERROR_SUCCESS;
	BOOL fTimeout;

	status = KomWdgCheckTimeoutStatus(&fTimeout);
	ExitIfError(status);

	printf("Watchdog expired: %s\n", fTimeout?"Yes":"No");

	return;
}

//****************************************************************************
// RunWdg - Starts and retriggers watchdog.
//			Demonstrates the usage of KomWdgGetConfig, KomWdgCheckTimeoutStatus,
//			KomWdgRetrigger
void RunWdg(void)
{
	DWORD status;
	tKomWdgConfig Config;
	BOOL fTimeout;
	int choice;

	// check whether watchdog is already enabled
	status = KomWdgGetConfig(&Config);
	ExitIfError(status);

	// check whether watchdog already expired
	status = KomWdgCheckTimeoutStatus(&fTimeout);
	ExitIfError(status);

	printf("Watchdog configuration:\n");
	printf("Timeout value:    %s\n", sKomWdgTimeoutValue[Config.bTimeout]);
	printf("Expiration mode:  %s\n", sKomWdgMode[Config.bMode]);
	printf("Enabled:          %s\n", Config.fEnabled?"Yes":"No");

	if(FALSE == Config.fEnabled)
	{
		if(FALSE == fTimeout)
			printf("To change settings return to main menu\n");

		printf("1 continue\n");
		printf("0 abort\n");
		do
		{
			scanf("%d",&choice);
		} while ((choice<0) || (choice>1));

		if(0 == choice)
		{
			// abort running watchdog
			return;
		}
	}
	else if(TRUE == Config.fEnabled)
	{
		printf("Watchdog already active\n");
	}

	printf("Press any key to stop retriggering watchdog\n");
	do
	{
		// start/regtrigger watchdog
		status = KomWdgRetrigger();
		ExitIfError(status);
		// sleep a little bit to free cpu
		Sleep(50 * (Config.bTimeout + 1));
	} while(!_kbhit());

	printf("Retriggering watchdog stopped!\n");

	if((KOMWDG_MODE_IRQ == Config.bMode) || (KOMWDG_MODE_DUAL_STAGE == Config.bMode))
	{
		fTimeout = FALSE;
		status = KomWdgWaitForTimeout(&fTimeout);
		ExitIfError(status);

		if(TRUE == fTimeout)
		{
			printf("watchdog expired!\n");
		}
	}
	else if(KOMWDG_MODE_TIMER == Config.bMode)
	{
		fTimeout = FALSE;

		printf("Waiting for timeout.");

		do
		{
			Sleep(500);
			status = KomWdgWaitForTimeout(&fTimeout);
			ExitIfError(status);

			printf(".");
		} while (!fTimeout);

		printf(" Watchdog expired!\n");		
	}
	else
	{
		// we have reset mode
		printf("System will reset soon...\n");
		Sleep(256000);
	}

	if(KOMWDG_MODE_DUAL_STAGE == Config.bMode)
	{
		// restart triggering watch
		printf("Press any key to stop retriggering watchdog\n");
		do
		{
			// start/regtrigger watchdog
			status = KomWdgRetrigger();
			ExitIfError(status);
			// sleep a little bit to free cpu
			Sleep(50 * (Config.bTimeout + 1));
		}while(!_kbhit());

		printf("Retriggering watchdog stopped!\n");
		printf("System will reset soon...\n");
		Sleep(256000);
	}

	return;
}


//****************************************************************************
// Application's main entry point
int _tmain(int argc, _TCHAR* argv[])
{	
	int choice;

	//Hallo message 
	printf("KomWdgDemo - demo application for KomWdg API library.");
	
	do
	{
		//ask for operation:
		printf("\n\nOperation:\n");
		printf("1 Get watchdog configuration\n");
		printf("2 Set watchdog mode\n");
		printf("3 Set watchdog timeout value\n");
		printf("4 Check watchdog timeout status\n");
		printf("5 Run watchdog\n");
		printf("6 Get RestoreOnRestart mode\n");
		printf("7 Set RestoreOnRestart mode\n");
		printf("0 Exit\n");
		
		do
		{
			scanf("%d",&choice);
		} while ((choice<0) || (choice>7));
		
		//perform chosen operation:
		switch (choice)
		{
			case 1:	
				GetWdgConfig();
				break;

			case 2:
				SetWdgMode();
				break;

			case 3:
				SetWdgTimeout();
				break;

			case 4:
				CheckWdgTimeoutStatus();
				break;

			case 5:
				RunWdg();
				break;

			case 6:
				GetRestoreOnRestart();
				break;

			case 7:
				SetRestoreOnRestart();
				break;

			default:
				break;
		}

	} while (choice != 0);

	return 0;
}

