// KomSysDemo.cpp : Demo application for KomSys API.
//

#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <winioctl.h>
#include <KomSys.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");
}

//****************************************************************************
// Application's main entry point
int main(int argc, char* argv[])
{	DWORD status,prop;
	KomSysInfo ksi;
	UCHAR bid,props;
	char strPropName[MAX_PROPERTY_NAME];


	//Hallo message 
	printf("KomSysDemo - demo application for KomSys API library.\n\n");
	
	//get board ID
	printf("Getting board ID: ");
	status=KomSysGetBoardId(&bid);
	ExitIfError(status,TRUE);

	printf("\nBoard ID is 0x%02x",bid);

	//get board info:
	printf("\n\nGetting board info: ");
	status=KomSysGetBoardInfo(&ksi);
	ExitIfError(status,TRUE);

	printf("\nBoard id:        0x%02x",ksi.boardId);
	printf("\nBoard revision:  0x%02x",ksi.boardRev);
	printf("\nGeogr. address:  %d",ksi.geogrAddr);

	printf("\nSystem Slot:     ");
	if (ksi.systemSlot==KOMSYS_NOT_SUPPORTED)
		printf("Not supported");
	else if (ksi.systemSlot==KOMSYS_SYSTEM_SLOT)
		printf("TRUE");
	else if (ksi.systemSlot==KOMSYS_PERIPH_SLOT)
		printf("FALSE (peripheral slot)");
	else
		printf("unknown value");


	printf("\nHotSwap handle:   ");
	if (ksi.hswpStatus==KOMSYS_NOT_SUPPORTED)
		printf("Not supported");
	else if (ksi.hswpStatus==KOMSYS_HSWP_OPEN)
		printf("OPEN");
	else if (ksi.hswpStatus==KOMSYS_HSWP_CLOSED)
		printf("CLOSED");
	else
		printf("unknown");

	printf("\nReset source:     ");
	if (ksi.resetSrc==KOMSYS_NOT_SUPPORTED)
		printf("Not supported");
	else
	{
		if ((ksi.resetSrc)==KOMSYS_RESET_NONE)
			printf(" NO RESET,");
		if ((ksi.resetSrc)&KOMSYS_RESET_POWERON)
			printf(" POWER_ON,");
		if ((ksi.resetSrc)&KOMSYS_RESET_IPMI)
			printf(" IPMI,");
		if ((ksi.resetSrc)&KOMSYS_RESET_BUTTON)
			printf(" RESET_BUTTON,");
		if ((ksi.resetSrc)&KOMSYS_RESET_CPCI)
			printf(" CPCI,");
		if ((ksi.resetSrc)&KOMSYS_RESET_WDG)
			printf(" WATCHDOG,");
	}

	//special board properties: get number of properties
	printf("\nReading special board properties: ");
	status=KomSysGetBoardPropertiesNumber(&props);
	ExitIfError(status);

	if (props)
		printf(" %d special properties available:\n",props);
	else
		printf(" no special properties available.\n");

	//read each property:
	for (int i=0; i<props; i++)
	{
		status=KomSysGetBoardProperty(i,strPropName,&prop);
		ExitIfError(status);

		printf(" - property #%d: '%s' = %d (0x%08x)\n",i,strPropName,prop,prop);
	}

	if (ksi.resetSrc!=KOMSYS_NOT_SUPPORTED)
	{
		printf("\n\nPress Y to clear reset source: ");
		bid=getchar();

		if ((bid=='Y') || (bid=='y'))
		{
			printf("\nClearing reset source...");
			status=KomSysClearResetSrc();
			ExitIfError(status,TRUE);
		}
	}

	return 0;
}

//End of file