📄 testusbdlg.cpp
字号:
// TestUsbDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TestUsb.h"
#include "TestUsbDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <mmsystem.h>
/////////////////////////////////////////////////////////////////////////////
// CTestUsbDlg dialog
UINT InterruptIn(void * pParam) {
char c=0;
BOOLEAN bResult = FALSE;
ULONG nBytes = 0;
struct CThreadParam * threadParam;
HANDLE hFile, hDevice;
CTestUsbDlg* testDlg;
IO_BLOCK ioBlock;
threadParam=(struct CThreadParam *)pParam;
threadParam->bInThread = TRUE;
testDlg = threadParam->testDlg;
threadParam->pcIoBuffer = (unsigned char *)malloc (threadParam->uiLength + 16);
do {
do {
Sleep(500);
hFile = open_file(threadParam->pipe_name);
if(hFile != INVALID_HANDLE_VALUE) {
hDevice = open_dev();
ioBlock.uOffset = 0;
ioBlock.uLength = 1;
ioBlock.pbyData = (PUCHAR)&c;
ioBlock.uIndex = GET_FIRMWARE_VERSION;
bResult = DeviceIoControl(hDevice,
IOCTL_READ_REGISTERS,
(PVOID)&ioBlock,
sizeof(IO_BLOCK),
(PVOID)&c,
1,
&nBytes,
NULL);
if (bResult != TRUE) {
CloseHandle(hFile);
CloseHandle(hDevice);
threadParam->bInThread = FALSE;
}
}
} while(hFile == INVALID_HANDLE_VALUE && threadParam->bInThread == TRUE);
threadParam->ulData[0] |= D12_DRIVERLOAD;
threadParam->ulData[1] = c;
threadParam->bUpdate = TRUE;
if (hFile != INVALID_HANDLE_VALUE) {
testDlg->MessageBox(_T("Usb connected....."));
while(threadParam->bInThread == TRUE){
bResult = ReadFile(hFile,
threadParam->pcIoBuffer,
threadParam->uiLength,
&nBytes,
NULL);
if (bResult == TRUE) {
threadParam->ulData[0] |= D12_KEYSTATUS;
threadParam->ulData[2] = *(threadParam->pcIoBuffer);
threadParam->bUpdate = TRUE;
}
else {
DeviceIoControl(hFile,
IOCTL_D12_RESET_PIPE,
0,
0,
0,
0,
&nBytes,
NULL);
threadParam->ulData[0] |= D12_DRIVERUNLOAD;
threadParam->bUpdate = TRUE;
break;
}/* else ioctl failed */
};
CloseHandle(hFile);
CloseHandle(hDevice);
}// if valid hDevice
} while(threadParam->bInThread == TRUE);
free(threadParam->pcIoBuffer);
threadParam->pcIoBuffer = NULL;
threadParam->bInThread = FALSE;
return 0;
}
CTestUsbDlg::CTestUsbDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestUsbDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestUsbDlg)
m_strDisplay = _T("");
m_keystatus = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTestUsbDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestUsbDlg)
DDX_Text(pDX, IDC_EDIT2, m_strDisplay);
DDX_Text(pDX, IDC_STATIC_KEYSTATUS, m_keystatus);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestUsbDlg, CDialog)
//{{AFX_MSG_MAP(CTestUsbDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
ON_BN_CLICKED(IDC_BUTTON10, OnButton10)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestUsbDlg message handlers
BOOL CTestUsbDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
char output[200];
hDevice=open_dev();
if (hDevice==INVALID_HANDLE_VALUE) {
sprintf(output,"open device errror 0x%0x\r\n",GetLastError());
m_strDisplay+=output;
}
else
{
sprintf(output,"open device ok. Device Handle 0x%x\r\n",hDevice);
m_strDisplay+=output;
}
m_MainRead.bInThread = FALSE;
m_MainWrite.bInThread = FALSE;
m_ReadWrite.bInThread = FALSE;
m_MainRead.ulCount = 0;
m_MainWrite.ulCount = 0;
m_ReadWrite.ulCount = 0;
SetTimer(1, 50, 0);
//interrupt in
m_InterruptIn.testDlg = this;
m_InterruptIn.uiLength = 16;
strcpy(m_InterruptIn.pipe_name, (LPCSTR)"PIPE00");
strcpy(m_InterruptIn.driver_name, (LPCSTR)"D12TEST-0");
//中断读
CWinThread* wt=AfxBeginThread(InterruptIn,&m_InterruptIn);
m_InterruptIn.hThread=wt->m_hThread;
UpdateData(FALSE);
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTestUsbDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTestUsbDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTestUsbDlg::OnButton1() //复位管道
{
ULONG nBytes;
BOOL bResult;
HANDLE hFile;
char pipename[20];
char output[200];
//管道名称,共定义了四个管道,见驱动程序
//PIPE00 Endpoint1IN(中断读方式)
//PIPE01 Endpoint1OUT(中断写方式)
//PIPE02 Endpoint2IN(批量读方式)
//PIPE03 Endpoint2OUT(批量写方式)
strcpy(pipename,LPCSTR("PIPE01"));
hFile=open_file(pipename);
if (hFile==INVALID_HANDLE_VALUE) {
sprintf(output,"open endpoint errror 0x%0x\r\n",GetLastError());
m_strDisplay+=output;
}
else
{
sprintf(output,"open endpoint ok. File Handle 0x%x\r\n",hFile);
m_strDisplay+=output;
bResult=DeviceIoControl(hFile,
IOCTL_D12_RESET_PIPE,
NULL,0,NULL,0,&nBytes,NULL);
if (bResult) {
m_strDisplay+="Reset pipe ok\r\n";
}
CloseHandle(hFile);
}
UpdateData(FALSE);
}
void CTestUsbDlg::OnButton2() //得到配置描述符
{
ULONG nBytes,siz;
BOOL bResult;
char buf[256];
char output[200];
PUSB_CONFIGURATION_DESCRIPTOR cd;
PUSB_INTERFACE_DESCRIPTOR id;
PUSB_ENDPOINT_DESCRIPTOR ed;
siz=sizeof(buf);
bResult = DeviceIoControl(hDevice,
IOCTL_D12_GET_CONFIG_DESCRIPTOR,
(PVOID)buf,
siz,
(PVOID)buf,
siz,
&nBytes,
NULL);
if (bResult) {
ULONG i;
UINT j, n;
char *pch;
pch = buf;
n = 0;
sprintf(output,"get config descriptor ok\r\n nReturnBytes %d\r\n",nBytes);
m_strDisplay+=output;
cd = (PUSB_CONFIGURATION_DESCRIPTOR) pch;
print_USB_CONFIGURATION_DESCRIPTOR( cd );
pch += cd->bLength;
do {
id = (PUSB_INTERFACE_DESCRIPTOR) pch;
print_USB_INTERFACE_DESCRIPTOR(id, n++);
pch += id->bLength;
for (j=0; j<id->bNumEndpoints; j++) {
ed = (PUSB_ENDPOINT_DESCRIPTOR) pch;
print_USB_ENDPOINT_DESCRIPTOR(ed,j);
pch += ed->bLength;
}
i = (ULONG)(pch - buf);
} while (i<cd->wTotalLength);
}else{
m_strDisplay+="get config descriptor failed\r\n";
}
UpdateData(FALSE);
}
char* CTestUsbDlg::usbDescriptorTypeString(UCHAR bDescriptorType )
/*++
Routine Description:
Called to get ascii string of USB descriptor
Arguments:
PUSB_ENDPOINT_DESCRIPTOR->bDescriptorType or
PUSB_DEVICE_DESCRIPTOR->bDescriptorType or
PUSB_INTERFACE_DESCRIPTOR->bDescriptorType or
PUSB_STRING_DESCRIPTOR->bDescriptorType or
PUSB_POWER_DESCRIPTOR->bDescriptorType or
PUSB_CONFIGURATION_DESCRIPTOR->bDescriptorType
Return Value:
ptr to string
--*/
{
switch(bDescriptorType) {
case USB_DEVICE_DESCRIPTOR_TYPE:
return "USB_DEVICE_DESCRIPTOR_TYPE";
case USB_CONFIGURATION_DESCRIPTOR_TYPE:
return "USB_CONFIGURATION_DESCRIPTOR_TYPE";
case USB_STRING_DESCRIPTOR_TYPE:
return "USB_STRING_DESCRIPTOR_TYPE";
case USB_INTERFACE_DESCRIPTOR_TYPE:
return "USB_INTERFACE_DESCRIPTOR_TYPE";
case USB_ENDPOINT_DESCRIPTOR_TYPE:
return "USB_ENDPOINT_DESCRIPTOR_TYPE";
#ifdef USB_POWER_DESCRIPTOR_TYPE // this is the older definintion which is actually obsolete
// workaround for temporary bug in 98ddk, older USB100.h file
case USB_POWER_DESCRIPTOR_TYPE:
return "USB_POWER_DESCRIPTOR_TYPE";
#endif
#ifdef USB_RESERVED_DESCRIPTOR_TYPE // this is the current version of USB100.h as in NT5DDK
case USB_RESERVED_DESCRIPTOR_TYPE:
return "USB_RESERVED_DESCRIPTOR_TYPE";
case USB_CONFIG_POWER_DESCRIPTOR_TYPE:
return "USB_CONFIG_POWER_DESCRIPTOR_TYPE";
case USB_INTERFACE_POWER_DESCRIPTOR_TYPE:
return "USB_INTERFACE_POWER_DESCRIPTOR_TYPE";
#endif // for current nt5ddk version of USB100.h
default:
return "??? UNKNOWN!!";
}
}
char* CTestUsbDlg::usbConfigAttributesString(UCHAR bmAttributes)
/*++
Routine Description:
Called to get ascii string of USB_CONFIGURATION_DESCRIPTOR attributes
Arguments:
PUSB_CONFIGURATION_DESCRIPTOR->bmAttributes
Return Value:
ptr to string
--*/
{
UINT typ = bmAttributes & USB_CONFIG_POWERED_MASK;
switch( typ) {
case USB_CONFIG_BUS_POWERED:
return "USB_CONFIG_BUS_POWERED";
case USB_CONFIG_SELF_POWERED:
return "USB_CONFIG_SELF_POWERED";
case USB_CONFIG_REMOTE_WAKEUP:
return "USB_CONFIG_REMOTE_WAKEUP";
default:
return "??? UNKNOWN!!";
}
}
char* CTestUsbDlg::usbEndPointTypeString(UCHAR bmAttributes)
/*++
Routine Description:
Called to get ascii string of endpt descriptor type
Arguments:
PUSB_ENDPOINT_DESCRIPTOR->bmAttributes
Return Value:
ptr to string
--*/
{
UINT typ = bmAttributes & USB_ENDPOINT_TYPE_MASK;
switch( typ) {
case USB_ENDPOINT_TYPE_INTERRUPT:
return "USB_ENDPOINT_TYPE_INTERRUPT";
case USB_ENDPOINT_TYPE_BULK:
return "USB_ENDPOINT_TYPE_BULK";
case USB_ENDPOINT_TYPE_ISOCHRONOUS:
return "USB_ENDPOINT_TYPE_ISOCHRONOUS";
case USB_ENDPOINT_TYPE_CONTROL:
return "USB_ENDPOINT_TYPE_CONTROL";
default:
return "??? UNKNOWN!!";
}
}
void CTestUsbDlg::print_USB_CONFIGURATION_DESCRIPTOR(PUSB_CONFIGURATION_DESCRIPTOR cd)
{
char output[200];
sprintf(output,"\r\n===================\r\nUSB_CONFIGURATION_DESCRIPTOR\r\n");
m_strDisplay+=output;
sprintf(output,"bLength = 0x%x, decimal %d\r\n", cd->bLength, cd->bLength);
m_strDisplay+=output;
sprintf(output,"bDescriptorType = 0x%x ( %s )\r\n", cd->bDescriptorType, usbDescriptorTypeString( cd->bDescriptorType));
m_strDisplay+=output;
sprintf(output,"wTotalLength = 0x%x, decimal %d\r\n", cd->wTotalLength, cd->wTotalLength);
m_strDisplay+=output;
sprintf(output,"bNumInterfaces = 0x%x, decimal %d\r\n", cd->bNumInterfaces, cd->bNumInterfaces);
m_strDisplay+=output;
sprintf(output,"bConfigurationValue = 0x%x, decimal %d\r\n",
cd->bConfigurationValue, cd->bConfigurationValue);
m_strDisplay+=output;
sprintf(output,"iConfiguration = 0x%x, decimal %d\r\n", cd->iConfiguration, cd->iConfiguration );
m_strDisplay+=output;
sprintf(output,"bmAttributes = 0x%x ( %s )\r\n", cd->bmAttributes, usbConfigAttributesString( cd->bmAttributes ));
m_strDisplay+=output;
sprintf(output,"MaxPower = 0x%x, decimal %d\r\n", cd->MaxPower, cd->MaxPower);
m_strDisplay+=output;
}
void CTestUsbDlg::print_USB_INTERFACE_DESCRIPTOR(PUSB_INTERFACE_DESCRIPTOR id, UINT ix)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -