// KomRioDemo.cpp : Demo application for KomRio API.
//

#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <winioctl.h>
#include <KomRio.h>

UCHAR ports;	//number of ports in the system

//function declarations:
void GetRoute();			//Get route of one specified port
void SetRoute();			//Set route of one specified port
void GetRestoreOnRestart();		//Get RestoreOnRestart mode
void SetRestoreOnRestart();		//Set RestoreOnRestart mode


//****************************************************************************
// 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");
}

//****************************************************************************
// Application's main entry point
int main(int argc, char* argv[])
{	DWORD status;
	UCHAR port;
	int choice;

	//Hallo message 
	printf("KomRioDemo - demo application for KomRio API library.\n\n");
	
	//get available ports
	printf("Available ports: ");
	status=KomRioGetAvailablePorts(&ports);
	ExitIfError(status);

	//printf them out
	printf("%d (indices: 0..%d),",ports,ports-1);

	//Get the info:
	printf("\nPort types: ");
	for (port=0; port<ports; port++)
	{	KomRioInfo info;

		status=KomRioGetPortInfo(port,&info);
		ExitIfError(status);

		printf("\n port #%d '%s': ",info.portIdx,info.portName);

		switch (info.portType)
		{
			case ptCom:
				printf("COM (serial port)");
				break;

			case ptEth:
				printf("ETH (ethernet port)");
				break;

			default:
				printf("unknown port type");
		}
	}	

	do
	{
		//ask for operation:
		printf("\n\nOperation:\n 1. Get port route,\n 2. Set port route,\n 3. Get RestoreOnRestart mode,\n 4. Set RestoreOnRestart mode,\n 0. Exit,\nYour choice: ");
		scanf("%d",&choice);
		
		//perform chosen operation:
		switch (choice)
		{
			case 1:	
				GetRoute();
				break;

			case 2:
				SetRoute();
				break;

			case 3:
				GetRestoreOnRestart();
				break;

			case 4:
				SetRestoreOnRestart();
				break;

			default:
				break;
		}

	} while ((choice>0) && (choice<5));

	return 0;
}

//****************************************************************************
// GetRoute - Gets current route of one specified port.
//			Demonstrates the usage of KomRioRouteGet
void GetRoute()
{	UCHAR port;
	PortRoute route;
	DWORD status;

	printf("\nChoose which port (0..%d): ",ports-1);
	scanf("%d",&port);
			
	printf("Getting port #%d route... ",port);
	status=KomRioRouteGet(port,&route);

	ExitIfError(status,TRUE);

	printf("\nPort #%d route is: ",port);
	switch (route)
	{	
		case prFront:
			printf("FRONT (front panel)");
			break;

		case prRio:
			printf("RIO (back panel)");
			break;

		default:
			printf("unknown");
	}
}

//****************************************************************************
// SetRoute - Sets current route of one specified port.
//			Demonstrates the usage of KomRioRouteSet
void SetRoute()
{	UCHAR port;
	PortRoute route;
	DWORD status;

	printf("\nChoose which port (0..%d): ",ports-1);
	scanf("%d",&port);

	printf("\nRoutes:\n 1. FRONT (front panel)\n 2. RIO (back panel)\nChoose route: ");
	scanf("%d",&route);

	//convert the value to API value:
	switch (route)
	{
		case 1:
			route=prFront;
			break;

		case 2:
			route=prRio;
			break;

		default:
			break;
			//do nothing - this way we are able to test invalid parameter conditions
	}
			
	printf("Setting port #%d route... ",port);
	status=KomRioRouteSet(port,route);

	ExitIfError(status,TRUE);
}

//****************************************************************************
// GetRestoreOnRestart - Gets the RestoreOnRestart setting
//			Demonstrates the usage of KomRioGetRestoreStatus
void GetRestoreOnRestart()
{	BOOL fRestore;
	DWORD status;
	
	printf("\n\nGetting RestoreOnRestart mode... ");
	status=KomRioGetRestoreStatus(&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 KomRioSetRestoreStatus
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=KomRioSetRestoreStatus(TRUE);
	}
	else	//turn off
	{
		printf("Turning RestoreOnRestart OFF... ");
		status=KomRioSetRestoreStatus(FALSE);
	}

	ExitIfError(status,TRUE);
}


//End of file