📄 testdlg.cpp
字号:
// TestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Test.h"
#include "TestDlg.h"
#include <process.h>
#include <winioctl.h>
#include "ezusbsys.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
char PIPE_TYPE_STRINGS[4][4] =
{
"CTL",
"ISO",
"BLK",
"INT"
};
char PIPE_DIRECTION[2][4] =
{
"OUT",
"IN"
};
BOOL gStopTest;
/*
purpose:
打开设备函数
input:
devname 设备名
output:
phDeviceHandle 设备句柄
return value:
BOOLEAN变量 成功或者失败
*/
BOOL bOpenDriver (HANDLE * phDeviceHandle, PCHAR devname)
{
char completeDeviceName[64] = "";
char pcMsg[64] = "";
strcat (completeDeviceName,
"\\\\.\\"
);
strcat (completeDeviceName,
devname);
*phDeviceHandle = CreateFile( completeDeviceName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (*phDeviceHandle == INVALID_HANDLE_VALUE) {
return (FALSE);
} else {
return (TRUE);
}
}
UINT TransferThread(
void * pParam
)
{
PTHREAD_CONTROL threadControl;
threadControl=(PTHREAD_CONTROL)pParam;
// perform the ISO transfer
threadControl->status = DeviceIoControl (threadControl->hDevice,
threadControl->Ioctl,
threadControl->InBuffer,
threadControl->InBufferSize,
threadControl->OutBuffer,
threadControl->OutBufferSize,
&threadControl->BytesReturned,
NULL);
// if an event exists, set it
if (threadControl->completionEvent)
SetEvent(threadControl->completionEvent);
return 0;
}
UINT TestThread(
void * pParam // thread data
)
{
CThreadParam* ThreadParam;
CTestDlg* TestDlg;
HANDLE hInDevice = NULL;
HANDLE hOutDevice = NULL;
PUCHAR inBuffer = NULL;
PUCHAR outBuffer = NULL;
ULONG MaxTransferSize, CurrentTransferSize;
ULONG InPipeNum;
ULONG OutPipeNum;
ULONG TestPass;
ULONG Errors;
HANDLE ReadCompleteEvent;
HANDLE WriteCompleteEvent;
ULONG i;
BULK_TRANSFER_CONTROL inBulkControl,outBulkControl;
THREAD_CONTROL inThreadControl,outThreadControl;
char tempbuff[256];
FILE *dumpfile;//保存测试数据
FILE *binfile;
ThreadParam=(CThreadParam *)pParam;
TestDlg=ThreadParam->testDlg;
if (bOpenDriver (&hInDevice, TestDlg->m_strName.GetBuffer(TestDlg->m_strName.GetLength())) != TRUE)
{
TestDlg->m_strOutput+="打开设备失败,测试线程结束\r\n";
ThreadParam->bUpdate=TRUE;
return 0;
}
if (bOpenDriver (&hOutDevice, TestDlg->m_strName.GetBuffer(TestDlg->m_strName.GetLength())) != TRUE)
{
TestDlg->m_strOutput+="打开设备失败\r\n";
ThreadParam->bUpdate=TRUE;
return 0;
}
InPipeNum=2;
OutPipeNum=0;
sprintf(tempbuff,"IN PIPE = %d OUT PIPE = %d",InPipeNum,OutPipeNum);
ThreadParam->bUpdate=TRUE;
TestDlg->m_strOutput+=tempbuff;
//
// get the transfer size and allocate the transfer buffers
//
MaxTransferSize = TestDlg->m_nSize;
CurrentTransferSize = MaxTransferSize;
outBuffer = (unsigned char *)malloc(MaxTransferSize);
inBuffer = (unsigned char *)malloc(MaxTransferSize);
//
// seed the random number generator, and set our starting pattern
//
srand(TestDlg->m_nSize);
//
// initialize the pass and error counters
//
TestPass=0;
Errors=0;
//
// Set up our event objects
//
ReadCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL);
WriteCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL);
//
// main test loop
//
while (!gStopTest)//全局变量来决定是否退出测试线程
{
// initialize the out buffer
for (i=0;i<CurrentTransferSize;i++)
outBuffer[i] = rand();
// initialize the in buffer
memset(inBuffer, 0, MaxTransferSize);
// initialize data structures for the read thread
inBulkControl.pipeNum = InPipeNum;
inThreadControl.hDevice = hInDevice;
inThreadControl.Ioctl = IOCTL_EZUSB_BULK_READ;
inThreadControl.InBuffer = (PVOID)&inBulkControl;
inThreadControl.InBufferSize = sizeof(BULK_TRANSFER_CONTROL);
inThreadControl.OutBuffer = inBuffer;
inThreadControl.OutBufferSize = CurrentTransferSize;
inThreadControl.completionEvent = ReadCompleteEvent;
inThreadControl.status = FALSE;
inThreadControl.BytesReturned = 0;
// initialize data structures for the write thread
outBulkControl.pipeNum = OutPipeNum;
outThreadControl.hDevice = hOutDevice;
outThreadControl.Ioctl = IOCTL_EZUSB_BULK_WRITE;
outThreadControl.InBuffer = (PVOID)&outBulkControl;
outThreadControl.InBufferSize = sizeof(BULK_TRANSFER_CONTROL);
outThreadControl.OutBuffer = outBuffer;
outThreadControl.OutBufferSize = CurrentTransferSize;
outThreadControl.completionEvent = WriteCompleteEvent;
outThreadControl.status = FALSE;
outThreadControl.BytesReturned = 0;
// start and wait for transfer threads
CWinThread * rd = AfxBeginThread(
TransferThread, // thread function
&inThreadControl); // argument to thread function
inThreadControl.hThread = rd->m_hThread;
CWinThread * wt = AfxBeginThread(
TransferThread, // thread function
&outThreadControl); // argument to thread function
outThreadControl.hThread = wt->m_hThread;
WaitForSingleObject(ReadCompleteEvent,INFINITE);
WaitForSingleObject(WriteCompleteEvent,INFINITE);
// if either the read or write failed, we want to stop the test
if (!inThreadControl.status)
{
TestDlg->m_strOutput+= "Error: Read failed\r\n";
TestDlg->m_strOutput+="Dumping OUT packet to file\r\n";
dumpfile = fopen("dumpfile.txt","w");
for (i=0;i<CurrentTransferSize;i++)
fprintf(dumpfile,"%X\n",outBuffer[i]);
fclose(dumpfile);
binfile = fopen("binfile.bix","wb");
fwrite(outBuffer,sizeof(BYTE),CurrentTransferSize,binfile);
fclose(binfile);
gStopTest = TRUE;
}
if( !outThreadControl.status)
{
TestDlg->m_strOutput+= "Error: Write failed\r\n";
gStopTest = TRUE;
}
// verify the data, unless the user has disabled verify data
// or this was a write only test.
// make sure the correct amount of data was returned
if (inThreadControl.BytesReturned != CurrentTransferSize)
{
sprintf(tempbuff,
"Error: Length expected = %d actual = %d\r\n",
CurrentTransferSize,
inThreadControl.BytesReturned);
TestDlg->m_strOutput+=tempbuff;
Errors++;
// gStopTest = TRUE;
}
// copmpare the buffers
for (i=0;i<CurrentTransferSize;i++)
{
if (inBuffer[i] != outBuffer[i])
{
sprintf(tempbuff,
"Error: Data offset 0x%x expected = 0x%x actual = 0x%x\r\n",
i,
outBuffer[i],
inBuffer[i]);
TestDlg->m_strOutput+=tempbuff;
Errors++;
// gStopTest = TRUE;
}
}
//如果有错误发生
if (Errors && (gStopTest == TRUE))
{
TestDlg->m_strOutput+="Dumping Error packet to file\r\n";
dumpfile = fopen("dumpfile.txt","w");
for (i=0;i<CurrentTransferSize;i++)
fprintf(dumpfile,"%X\n",outBuffer[i]);
fclose(dumpfile);
binfile = fopen("binfile.bix","wb");
fwrite(outBuffer,sizeof(BYTE),CurrentTransferSize,binfile);
fclose(binfile);
}
// dumpfile = fopen("dumpfile.txt","w");
// for (i=0;i<CurrentTransferSize;i++)
// fprintf(dumpfile,"%X\n",outBuffer[i]);
// fclose(dumpfile);
// update pass counter
TestPass++;
TestDlg->m_nError=Errors;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -