📄 ezmain.cpp
字号:
ISO_TRANSFER_CONTROL IsoControl;
ULONG numPackets = 0;
char* buffer = NULL;
ULONG bufferLength = 0;
EzSendMessage(hOutputBox, LB_ADDSTRING, 0, (LPARAM)"ISO Transfer");
// Open the driver
if (bOpenDriver (&hDevice, pcDriverName) != TRUE)
{
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Failed to Open Driver");
hDevice = NULL;
}
#ifdef USING_MFC
IsoControl.PipeNum = pTh->pipeNum;
IsoControl.PacketSize = pTh->length;
IsoControl.BufferCount = pTh->bufCount;
IsoControl.FramesPerBuffer = pTh->pakPer;
IsoControl.PacketCount = pTh->packets;
buffer = (char *)pTh->pBuf;
#else
BOOL Success;
IsoControl.PipeNum = GetDlgItemInt(hDlg,IDC_ISO_PIPE,&Success,FALSE);
IsoControl.PacketSize = GetDlgItemInt(hDlg,IDC_ISO_SIZE,&Success,FALSE);
IsoControl.BufferCount = GetDlgItemInt(hDlg,IDC_ISO_BUFFER_COUNT,&Success,FALSE);
IsoControl.FramesPerBuffer = GetDlgItemInt(hDlg,IDC_ISO_FRAMES_PER_BUFFER,&Success,FALSE);
IsoControl.PacketCount = GetDlgItemInt(hDlg,IDC_ISO_PACKETS,&Success,FALSE);
#endif
bufferLength = IsoControl.PacketCount * (IsoControl.PacketSize + sizeof(USBD_ISO_PACKET_DESCRIPTOR));
if (!buffer)
{
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Iso transfer too big! Aborted transfer.");
break;
}
// reset the pipe
if (hDevice != NULL)
{
bResult = DeviceIoControl (hDevice,
IOCTL_Ezusb_RESETPIPE,
&IsoControl.PipeNum,
sizeof(ULONG),
NULL,
0,
(unsigned long *)&nBytes,
NULL);
if (bResult != TRUE)
{
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Pipe Reset Failed");
break;
}
}
if (hDevice != NULL)
{
PUSBD_PIPE_INFORMATION pPipe = ((PUSBD_INTERFACE_INFORMATION)theApp.m_uInterfaceInfo)->Pipes;
DWORD ioctl_val = IOCTL_EZUSB_ISO_WRITE;
if(pPipe[IsoControl.PipeNum].EndpointAddress >> 7)
ioctl_val = IOCTL_EZUSB_ISO_READ;
bResult = DeviceIoControl (hDevice,
ioctl_val,
&IsoControl,
sizeof(ISO_TRANSFER_CONTROL),
buffer,
bufferLength,
(unsigned long *)&nBytes,
NULL);
}/* if valid driver handle */
if (bResult==TRUE)
{
char temp[64] = "";
wsprintf (temp, "ISO transfer success");
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);
// dump the whole buffer
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Buffer Contents");
DumpBuffer(buffer,IsoControl.PacketSize*IsoControl.PacketCount,hOutputBox);
}
else
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"ISO transfer failed");
CloseHandle (hDevice); // Close the handle
}
break;
case IDC_GETDEVICEDESCRIPTOR:
// Get some memory, plus some guardband area
pvBuffer = malloc (sizeof (Usb_Device_Descriptor) + 128);
MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);
// Open the driver
if (bOpenDriver (&hDevice, pcDriverName) != TRUE)
{
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Failed to Open Driver");
hDevice = NULL;
}
if (hDevice != NULL)
{// Perform the Get-Descriptor IOCTL
bResult = DeviceIoControl (hDevice,
IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR,
pvBuffer,
sizeof (Usb_Device_Descriptor),
pvBuffer,
sizeof (Usb_Device_Descriptor),
(unsigned long *)&nBytes,
NULL);
}/* if valid driver handle */
if (bResult==TRUE)
{
ParseDeviceDescriptor(pvBuffer, hOutputBox);
#ifdef USING_MFC
memcpy(theApp.m_uDevDescInfo, pvBuffer, sizeof(Usb_Device_Descriptor));
#endif
}
else
{
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Get Device Descriptor Failed");
ShowSystemError(hOutputBox);
}
CloseHandle (hDevice); // Close the handle
free (pvBuffer); // Free the memory
break;
case IDC_GETCONFIGDESCRIPTOR:
// The configuration descriptor is obtained using two separate calls. The
// first call is done to determine the size of the entire configuration descriptor,
// and the second call is done with that total size specified. For more information,
// please refer to the USB Specification, Chapter 9.
// Get some memory, plus some guardband area
pvBuffer = malloc (sizeof (Usb_Configuration_Descriptor) + 128);
MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);
// Open the driver
if (bOpenDriver (&hDevice, pcDriverName) != TRUE)
{
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Failed to Open Driver");
hDevice = NULL;
}
// Get the first bytes of the configuration descriptor to determine the size of
// the entire configuration descriptor.
if (hDevice != NULL)
{// Perform the Get-Descriptor IOCTL
bResult = DeviceIoControl (hDevice,
IOCTL_Ezusb_GET_CONFIGURATION_DESCRIPTOR,
pvBuffer,
sizeof (Usb_Configuration_Descriptor),
pvBuffer,
sizeof (Usb_Configuration_Descriptor),
(unsigned long *)&nBytes,
NULL);
}/* if valid driver handle */
if (bResult!=TRUE) {
// Inform user that something bad happened
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Get Configuration Descriptor Failed");
ShowSystemError(hOutputBox);
CloseHandle (hDevice);
free (pvBuffer);
break;
}/* if */
ulLength = GET_CONFIG_DESCRIPTOR_LENGTH(pvBuffer);
assert (ulLength >= 0);
// Now get the entire configuration descriptor
pvBuffer = realloc (pvBuffer, ulLength);
if (pvBuffer)
{
MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);
// Perform the Get-Descriptor IOCTL
bResult = DeviceIoControl (hDevice,
IOCTL_Ezusb_GET_CONFIGURATION_DESCRIPTOR,
pvBuffer,
ulLength,
pvBuffer,
ulLength,
(unsigned long *)&nBytes,
NULL);
if (bResult==TRUE)
{
ParseConfigurationDescriptor(pvBuffer, hOutputBox);
}
else
{// Inform user that the Ioctl failed
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"GetConfigDescriptor Ioctl failed");
ShowSystemError(hOutputBox);
}
}
else
{// Inform user that mem alloc failed
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Memory Allocation failed in GetConfigDescriptor");
}
CloseHandle (hDevice); // Close the handle
free (pvBuffer); // Free the memory
break;
case IDC_TRAN_BULK_DATA:
{
char *buffer = NULL;
ULONG length;
BULK_TRANSFER_CONTROL bulkControl;
UINT uiData = 0;
MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);
#ifdef USING_MFC
buffer = (char *)pTh->pBuf;
length = pTh->length;
bulkControl.pipeNum = pTh->pipeNum;
uiData = pTh->data;
#else
length = GetDlgItemInt (hDlg, IDC_EDIT_READ_DATA_LENGTH, NULL, FALSE);
// Get the User Input for the Endpoint Index
bulkControl.pipeNum = GetDlgItemInt (hDlg,IDC_EDIT_IN_EP_INDEX, NULL, FALSE);
buffer = (char *)malloc(length);
if (!buffer)
{
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Unable to allocate transfer buffer");
break;
}
// Get the text in the driver name edit box
GetDlgItemText (hDlg, IDC_DRIVER_NAME, pcDriverName, MAX_DRIVER_NAME);
#endif
// Open the driver
if (bOpenDriver (&hDevice, pcDriverName) != TRUE)
{
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Failed to Open Driver");
hDevice = NULL;
}
if (hDevice != NULL)
{
PUSBD_PIPE_INFORMATION pPipe = ((PUSBD_INTERFACE_INFORMATION)theApp.m_uInterfaceInfo)->Pipes;
DWORD ioctl_val = IOCTL_EZUSB_BULK_WRITE;
if(pPipe[bulkControl.pipeNum].EndpointAddress >> 7)
ioctl_val = IOCTL_EZUSB_BULK_READ;
bResult = DeviceIoControl (hDevice,
ioctl_val,
&bulkControl,
sizeof (BULK_TRANSFER_CONTROL),
buffer,
length,
(unsigned long *)&nBytes,
NULL);
if( (bResult == TRUE) && (hOutputBox) )
{
if(pPipe[bulkControl.pipeNum].EndpointAddress >> 7)
EzSendMessage (hOutputBox, LB_ADDSTRING, 0 , (LPARAM)"Read IOCTL passed");
else
EzSendMessage (hOutputBox, LB_ADDSTRING, 0 , (LPARAM)"Write IOCTL passed");
if(theApp.m_nVerbose)
DumpBuffer(buffer,nBytes,hOutputBox);
}
else
{
if(pPipe[bulkControl.pipeNum].EndpointAddress >> 7)
EzSendMessage (hOutputBox, LB_ADDSTRING, 0 , (LPARAM)"Read IOCTL failed");
else
EzSendMessage (hOutputBox, LB_ADDSTRING, 0 , (LPARAM)"Write IOCTL failed");
ShowSystemError(hOutputBox);
}/* else ioctl failed */
pTh->length = nBytes;
}// if valid hDevice
#ifndef USING_MFC
free(buffer);
#endif
CloseHandle (hDevice);
}
break;
case IDC_ANCHOR_DOWNLOAD:
{
int numread = 0;
char temp[64] = "";
ANCHOR_DOWNLOAD_CONTROL downloadControl;
MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);
#ifdef USING_MFC
//strcpy(DownloadFilename, pTh->strDldFile);
unsigned char* buffer;
downloadControl.Offset = (USHORT) pTh->dldOffset;
buffer = (unsigned char*)(pTh->pBuf);
numread = pTh->length;
#else
char DownloadFilename[256];
unsigned char buffer[MAX_FILE_SIZE];
FILE *fp;
GetDlgItemText (hDlg, IDC_DOWNLOAD_FILENAME, DownloadFilename, 256);
if ((fp = fopen(DownloadFilename,"rb")) != NULL)
{
numread = fread(buffer,sizeof(unsigned char),MAX_FILE_SIZE,fp);
fclose(fp);
}
#endif
wsprintf (temp, "Anchor Download %d bytes: addr=%x",numread, downloadControl.Offset);
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);
if(theApp.m_nVerbose)
DumpBuffer(buffer,numread,hOutputBox);
// Open the driver
if (bOpenDriver (&hDevice, pcDriverName) != TRUE)
{
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Failed to Open Driver");
hDevice = NULL;
}
if (hDevice != NULL)
{
if(pTh->dldOffset < 0x2000)
{
bResult = DeviceIoControl (hDevice,
IOCTL_EZUSB_ANCHOR_DOWNLOAD,
&downloadControl,
sizeof(ANCHOR_DOWNLOAD_CONTROL),
buffer,
numread,
(unsigned long *)&nBytes,
NULL);
if(!bResult)
ShowSystemError(hOutputBox);
}
else
{
VENDOR_OR_CLASS_REQUEST_CONTROL myRequest;
myRequest.request = (BYTE) 0xA3;
myRequest.value = (WORD) pTh->dldOffset;
myRequest.index = (WORD) 0x00;
myRequest.direction = 0;
myRequest.requestType = 2;
myRequest.recepient = 0;
bResult = DeviceIoControl (hDevice,
IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST,
&myRequest,
sizeof(VENDOR_OR_CLASS_REQUEST_CONTROL),
pTh->pBuf,
pTh->length,
(unsigned long *)&nBytes,
NULL);
if (bResult==TRUE)
{
if(theApp.m_nVerbose)
DumpBuffer(buffer,nBytes,hOutputBox);
}
else
{
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Vendor Request failed");
ShowSystemError(hOutputBox);
}
}
}/* if valid driver handle */
if (bResult==TRUE)
{
// do nothin
}
else
{
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Anchor Download failed");
ShowSystemError(hOutputBox);
}
CloseHandle (hDevice); // Close the handle
}
break;
case IDC_RESET_PORT:
{
int numread = 0;
char temp[64] = "";
MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);
// Open the driver
if (bOpenDriver (&hDevice, pcDriverName) != TRUE)
{
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Failed to Open Driver");
hDevice = NULL;
}
#ifdef USING_MFC
wsprintf (temp, "Reset Port");
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);
#endif
if (hDevice != NULL) {
bResult = DeviceIoControl (hDevice,
IOCTL_Ezusb_RESET,
NULL,
0,
NULL,
0,
(unsigned long *)&nBytes,
NULL);
}/* if valid driver handle */
if (bResult == TRUE)
{
EzSendMessage (hOutputBox, LB_ADDSTRING, 0 , (LPARAM)"RESETPIPE passed");
}
else
{
EzSendMessage (hOutputBox, LB_ADDSTRING, 0 , (LPARAM)"RESETPIPE failed");
ShowSystemError(hOutputBox);
}/* else ioctl failed */
CloseHandle (hDevice); // Close the handle
}
break;
case IDC_RESETPIPE:
{
char temp[64] = "";
int pipenum;
MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);
// Open the driver
if (bOpenDriver (&hDevice, pcDriverName) != TRUE)
{
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Failed to Open Driver");
hDevice = NULL;
}
#ifdef USING_MFC
pipenum = pTh->pipeNum;
wsprintf (temp, "Reset Pipe: Pipe=%x", pipenum);
EzSendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);
#else
pipenum = GetDlgItemInt (hDlg, IDC_RESETPIPENUM, NULL, FALSE);
#endif
if (hDevice != NULL)
{
bResult = DeviceIoControl (hDevice,
IOCTL_Ezusb_RESETPIPE,
&pipenum,
sizeof(int),
NULL,
0,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -