⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_usbsoftlock.cpp

📁 < FPGA数字电子系统设计与开发实例导航> 一书的代码
💻 CPP
字号:
                // Test_USBSoftLock.cpp
//
// Generated by DriverWizard version DriverStudio 2.7.0 (Build 562)
//
// This console application demonstrates how to open a handle
// to a device in your driver, and communicate with the driver
// using Read, Write, and DeviceIoControl calls, as appropriate.
//
// This test program attempts to open the device with link name
// USBSoftLockDevice, which corresponds to Unit 0 of the
// USBSoftLockDevice device class in the driver.

//
// You can build this application using the command line compiler with
// the following command:
//
//		cl Test_USBSoftLock.cpp
//


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

#include <winioctl.h>
#include "..\USBSoftLockioctl.h"


typedef void VOIDFUNC();

// Prototypes
void Usage(void);
void ShowIoctlValues(void);

//	TODO:
//		You can redefine the IOCTL handler prototypes as needed, adding
//		appropriate parameters that can be collected from the command line.
//		To do this you must modify the command line parsing logic.  An
//		example of this is shown in comments throughout the test application.
//
//=== Parameterized IOCTL Example ===
// void Test_IOCTL_PARAMETERIZED(int nVal, ULONG dwVal);
void Test_USBSOFTLOCK_IOCTL_GET_PASSWORD(void);
void Test_USBSOFTLOCK_IOCTL_SET_PASSWORD(void);

void CloseIfOpen(void);
void doRead(int i);
void doWrite(int i);

// Global data


#define N_IOCODES	2

// Names of IOCTL codes
//
char *IOnames[N_IOCODES+1] =
{

//=== Parameterized IOCTL Example ===
//	"IOCTL_PARAMETERIZED",
	"USBSOFTLOCK_IOCTL_GET_PASSWORD",
	"USBSOFTLOCK_IOCTL_SET_PASSWORD",
	""
};

// IOCTL codes
//
int IOcodes[N_IOCODES+1] =
{

//=== Parameterized IOCTL Example ===
//	IOCTL_PARAMETERIZED,
	USBSOFTLOCK_IOCTL_GET_PASSWORD,
	USBSOFTLOCK_IOCTL_SET_PASSWORD,
	0
};


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

// Name used to open device
//
char *sLinkName = "\\\\.\\USBSoftLockDevice0";

////////////////////////////////////////////////////////////////////////
// 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 __cdecl main(int argc, char *argv[])
{
	int		nArgIndex;				// Walk through command line arguments
	int		nArgIncrement = 0;
	int		val;

//=== Parameterized IOCTL Example ===
//	int		nVal;
//	ULONG	dwVal;

	printf("Test application Test_USBSoftLock starting...\n");

	hDevice = OpenByName();
	if (hDevice == INVALID_HANDLE_VALUE)
	{
		printf("ERROR opening device: (%0x) returned from CreateFile\n", GetLastError());
		Exit(1);
	}
	else
	{
		printf("Device found, handle open.\n");
	}

	// Parse the command line

	if (argc < 2) Usage();

	nArgIndex = 1;
	while (nArgIndex < argc)
	{
		// Parse ahead to determine numeric value of argument

		if (nArgIndex+1 >= argc) Usage();
		if (!isdigit(argv[nArgIndex+1][0])) Usage();
		val = atoi(argv[nArgIndex+1]);

		switch (argv[nArgIndex][0])
		{

			case 'r':
			case 'R':
				doRead(val);
				nArgIncrement = 2;
				break;

			case 'w':
			case 'W':
				doWrite(val);
				nArgIncrement = 2;
				break;

			case 'i':
			case 'I':
				if (val >= N_IOCODES)
				{
					printf("IO control code index must be less than %d\n", N_IOCODES);
					ShowIoctlValues();
					Exit(1);
				}
				switch (IOcodes[val])
				{

//=== Parameterized IOCTL Example ===
//					case IOCTL_PARAMETERIZED:
//						if (nArgIndex+3 >= argc) Usage();
//						nVal = atoi(argv[nArgIndex+2]);
//						dwVal = strtoul(argv[nArgIndex+3], NULL, 0);
//						Test_IOCTL_PARAMETERIZED(nVal, dwVal);
//						nArgIncrement = 4;
//						break;

					case USBSOFTLOCK_IOCTL_GET_PASSWORD:
						Test_USBSOFTLOCK_IOCTL_GET_PASSWORD();
						nArgIncrement = 2;
						break;

					case USBSOFTLOCK_IOCTL_SET_PASSWORD:
						Test_USBSOFTLOCK_IOCTL_SET_PASSWORD();
						nArgIncrement = 2;
						break;

					default:
						printf("IO control code not valid\n");
						Exit(1);

				}
				break;

			case '?':
			case 'h':
			default:
				Usage();
		}
		nArgIndex += nArgIncrement;
	}
	
	printf("\n");
	system("pause");

	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;
	}
}


////////////////////////////////////////////////////////////////////////
// doRead
//
//		Read 'n' bytes of data from the device
//
// Note: This simple test app reads data from the device and displays the
//			data as characters.  This behavior can be modified as appropriate
//			for your device.
//
void doRead(int n)
{
	char	*buf;
	ULONG	nRead;
	int		i;
	int		j;

	buf = (char *) malloc(n);
	if (buf == NULL)
	{
		printf("Failed to allocate buffer for read");
		Exit(1);
	}

	// Read data from driver
	printf("Reading from device - ");
	ReadFile(hDevice, buf, n, &nRead, NULL);
	printf("%d bytes read from device (%d requested).\n", nRead, n);

	// Print what was read
	i = 0;
	while(i < n)
	{
		j = min((i+26),n);
		for(; i < j; i++)
		{
			printf("%c, ", buf[i]);
		}
		printf("\n");
	}

	free(buf);
}


////////////////////////////////////////////////////////////////////////
// doWrite
//
//		Write 'n' bytes of data to the device
//
// Note: This simple test app writes sequential characters to the
// 			device.  This behavior can be modified as appropriate
//			for your device.
//
void doWrite(int n)
{
	char	*buf;
	ULONG	nWritten;
	int		i;
	int		j;

	buf = (char *) malloc(n);
	if (buf == NULL)
	{
		printf("Failed to allocate buffer for write");
		Exit(1);
	}

	// start with the mod26 letter of the number of bytes to write
	j = (n % 26);
	// load buffer with dummy data (abcdefg...)
	for (i=0; i<n; i++, j=(j + 1)%26)
	{
		buf[i] = 'a' + j;
	}

	// Write data to driver
	printf("Writing to device - ");
	WriteFile(hDevice, buf, n, &nWritten, NULL);
	printf("%d bytes written to device (%d attempted).\n", nWritten, n);

	// Print what was written
	i = 0;
	while(i < n)
	{
		j = min((i+26),n);
		for(; i < j; i++)
		{
			printf("%c, ", buf[i]);
		}
		printf("\n");
	}

	free(buf);
}


////////////////////////////////////////////////////////////////////////
// Usage
//
//		Print a usage message describing arguments to this program
//
void Usage(void)
{
	printf("Usage: Test_USBSoftLock [r n] [w n] [i n]\n");
	printf("       r initiates a read of specified number of bytes\n");
	printf("       w initiates a write of specified number of bytes\n");
	printf("       i initiates an IO Control Code message with specified index value\n");
	ShowIoctlValues();
	printf("Example:\n");
	printf("    Test_USBSoftLock r 32 w 32\n");
	printf("        read 32 bytes, then write 32 bytes\n");

	Exit(1);
}



#define	IOCTL_INBUF_SIZE	512
#define	IOCTL_OUTBUF_SIZE	512

#define BUFFER_LENGTH		512

#define PASSWORD_LENGTH		8

//=== Parameterized IOCTL Example ===
//void Test_IOCTL_PARAMETERIZED(int nVal, ULONG dwVal)
//{
//  Function body same as other IOCTL handlers, with command line
//	parameters 'nVal' and 'dwVal' available as input.
//}

////////////////////////////////////////////////////////////////////////
// Test_USBSOFTLOCK_IOCTL_GET_PASSWORD
//
//		Test one Io Control Code
//
// TODO:
//		Pass appropriate arguments to your device and check
//		the return value
//
void Test_USBSOFTLOCK_IOCTL_GET_PASSWORD(void)
{
// Note that Input and Output are named from the point of view
// of the DEVICE:
//		bufInput  supplies data to the device
//		bufOutput is written by the device to return data to this application

	CHAR	bufInput[BUFFER_LENGTH];		// Input to device
	CHAR	bufOutput[BUFFER_LENGTH];	// Output from device
	ULONG	nOutput;						// Count written to bufOutput

	memset(bufInput, 0, BUFFER_LENGTH);
	memset(bufOutput, 0, BUFFER_LENGTH);

	printf("bufInput : 0x%X, bufOutput : 0x%X\n", (LONG)bufInput, (LONG)bufOutput);

	// Call device IO Control interface (USBSOFTLOCK_IOCTL_GET_PASSWORD) in driver
	printf("Issuing Ioctl to device - ");
	if (!DeviceIoControl(hDevice,
						 USBSOFTLOCK_IOCTL_GET_PASSWORD,
						 bufInput,
						 PASSWORD_LENGTH,
						 bufOutput,
						 PASSWORD_LENGTH,
						 &nOutput,
						 NULL) )
	{
		printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
		Exit(1);
	}
	else {
		printf("input buffer is : %s, output buffer is %s, output buffer size is %d", 
			bufInput, 
			bufOutput,
			nOutput);
	}
}

////////////////////////////////////////////////////////////////////////
// Test_USBSOFTLOCK_IOCTL_SET_PASSWORD
//
//		Test one Io Control Code
//
// TODO:
//		Pass appropriate arguments to your device and check
//		the return value
//
void Test_USBSOFTLOCK_IOCTL_SET_PASSWORD(void)
{
// Note that Input and Output are named from the point of view
// of the DEVICE:
//		bufInput  supplies data to the device
//		bufOutput is written by the device to return data to this application

	CHAR	bufInput[IOCTL_INBUF_SIZE];		// Input to device
	CHAR	bufOutput[IOCTL_OUTBUF_SIZE];	// Output from device
	ULONG	nOutput;						// Count written to bufOutput

	memset(bufInput, 0, BUFFER_LENGTH);
	memset(bufOutput, 0, BUFFER_LENGTH);

	strcpy(bufInput, "ABCDEFGH");

	// Call device IO Control interface (USBSOFTLOCK_IOCTL_SET_PASSWORD) in driver
	printf("Issuing Ioctl to device - ");
	if (!DeviceIoControl(hDevice,
						 USBSOFTLOCK_IOCTL_SET_PASSWORD,
						 bufInput,
						 PASSWORD_LENGTH,
						 bufOutput,
						 PASSWORD_LENGTH,
						 &nOutput,
						 NULL) )
	{
		printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
		Exit(1);
	}
	else {
		printf("input buffer is : %s, output buffer is %s, output buffer size is %d", 
			bufInput, 
			bufOutput,
			nOutput);
	}
}


////////////////////////////////////////////////////////////////////////
// ShowIoctlValues
//
//		Print list of IO Control Code values for usage display
//
void ShowIoctlValues(void)
{
	int i;

	for (i=0; i<N_IOCODES; i++)
	{
		if (i==0)
			printf( "         IO control code index\n");
		printf( "           %d is code %s [%x]\n", i, IOnames[i], IOcodes[i]);
	}


//=== Parameterized IOCTL Example ===
//		if (IOcodes[i] == IOCTL_PARAMETERIZED)
//		{
//			printf( "               and has two arguments: <arg1 desc.> <arg1 desc.>\n");
//			printf( "               Example: i %d <IOCTL index> <ex. arg1> <ex. arg2>\n", i);
//
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -