// KomCoreStressDemo.cpp : 
// Test of error conditions for KomCore driver

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

void CloseIfOpen(void);

// Handle to device opened in driver.
//
HANDLE	hDevice = INVALID_HANDLE_VALUE;

// Name used to open device
//
wchar_t *sLinkName = L"\\\\.\\KomCoreDevice0";

////////////////////////////////////////////////////////////////////////
// OpenByName
//
//		Open a handle to the requested device
//
HANDLE OpenByName(void)
{	
	// Create a handle to the driver
	return CreateFile(sLinkName,
					  GENERIC_READ | GENERIC_WRITE,
					  FILE_SHARE_READ,
					  NULL,
					  OPEN_EXISTING,
					  0,
					  NULL);
}

////////////////////////////////////////////////////////////////////////
// Exit
//
//		Print a message and exit
//
   void Exit(int res)
{
	printf("Exiting...\n\n");
	CloseIfOpen();
	exit(res);
}


////////////////////////////////////////////////////////////////////////
// Main entry point
//
//

int main(int argc, char* argv[])
{	

	printf("KomCoreStressDemo - KomCore driver test for error conditions\n\n");
	
	hDevice = OpenByName();
	if (hDevice == INVALID_HANDLE_VALUE)
	{
		printf("ERROR opening device: (%0x) returned from CreateFile\n\n", GetLastError());
		Exit(1);
	}
	else
	{
		printf("KomCore device found, handle open.\n\n");
	}

    UCHAR buffer[1000];
	ULONG nOutput;	// Count written to bufOutput

	printf("Issuing IOCTL_KOMCORE_GET_INFO without output buffer:\n");
	if (!DeviceIoControl(hDevice,IOCTL_KOMCORE_GET_INFO,
						 NULL,0,
						 NULL,0,
						 &nOutput,NULL))
	{
		printf("ERROR: DeviceIoControl returns %d bytes and error 0x%08x.\n\n",nOutput,GetLastError());
	}
	else
		printf("DeviceIoControl returns %d bytes, no error\n\n",nOutput);

	printf("Issuing IOCTL_KOMCORE_GET_INFO with big buffer:\n");
	if (!DeviceIoControl(hDevice,IOCTL_KOMCORE_GET_INFO,
						 NULL,0,
						 buffer,1000,
						 &nOutput,NULL))
	{
		printf("ERROR: DeviceIoControl returns %d bytes and error 0x%08x.\n\n",nOutput,GetLastError());
	}
	else
		printf("DeviceIoControl returns %d bytes, no error\n\n",nOutput);

	printf("Issuing IOCTL_KOMCORE_GET_TEMP with too small buffer:\n");
	if (!DeviceIoControl(hDevice,IOCTL_KOMCORE_GET_TEMP,
						 NULL,0,
						 buffer,2,
						 &nOutput,NULL))
	{
		printf("ERROR: DeviceIoControl returns %d bytes and error 0x%08x.\n\n",nOutput,GetLastError());
	}
	else
		printf("DeviceIoControl returns %d bytes, no error\n\n",nOutput);

	printf("Issuing IOCTL_KOMCORE_GET_TEMP with big in/out buffers:\n");
	if (!DeviceIoControl(hDevice,IOCTL_KOMCORE_GET_TEMP,
						 buffer,500,
						 buffer+500,500,
						 &nOutput,NULL))
	{
		printf("ERROR: DeviceIoControl returns %d bytes and error 0x%08x.\n\n",nOutput,GetLastError());
	}
	else
		printf("DeviceIoControl returns %d bytes, no error\n\n",nOutput);

	printf("Trying ReadFile: \n");
	if (ReadFile(hDevice,buffer,10,&nOutput,NULL))
		printf("ReadFile succeeded, %d bytes read\n\n",nOutput);
	else
		printf("ReadFile returns %d bytes and error 0x%08x\n\n",nOutput,GetLastError());

	printf("Trying WriteFile: \n");
	if (WriteFile(hDevice,buffer,10,&nOutput,NULL))
		printf("WriteFile succeeded, %d bytes written\n\n",nOutput);
	else
		printf("WriteFile returns %d bytes written and error 0x%08x\n\n",nOutput,GetLastError());
	

	CloseIfOpen();

	return 0;
}


////////////////////////////////////////////////////////////////////////
// CloseIfOpen
//
//		Close the device if we previously opened a handle to it.
//
void CloseIfOpen(void)
{
	if (hDevice != INVALID_HANDLE_VALUE)
	{
		// Close the handle to the driver
		if (!CloseHandle(hDevice))
		{
			printf("ERROR: CloseHandle returns %0x.\n", GetLastError());
		}
		hDevice = INVALID_HANDLE_VALUE;
	}
}


