📄 atlasusbdev.cpp
字号:
if (m_hDev == INVALID_HANDLE_VALUE)
{
TRACE("Open device failed\n");
return FALSE;
}
if(!DeviceIoControl(m_hDev,
IOCTL_ATLASUSB_VENDORCOMMAND,
vendorMsg,
vendorMsg->size,
vendorMsg->dataBuf,
vendorMsg->vendorLength,
&nReturnBytes,
NULL))
{
DWORD dwError = GetLastError();
TRACE("SendVendorCommand failed! Error Code : 0x%08x\n", dwError);
return FALSE;
}
sLock.Unlock();
return TRUE;
}
BOOL CAtlasUsbDev::IsConnected()
{
if (m_hDev == INVALID_HANDLE_VALUE)
return FALSE;
//If the device is already opened, open it again to check its validality
return OpenDevice();
}
BOOL CAtlasUsbDev::OpenPipe(DWORD iPipe)
{
ASSERT(m_strDevName.GetLength());
if(iPipe >= MAX_PIPES)
return FALSE;
CString strEpName;
char szEpName[128];
memset(szEpName, 0, 128);
sprintf(szEpName, "\\PIPE%02d", iPipe);
strEpName = m_strDevName + szEpName;
strcpy(szEpName, strEpName);
if(m_hPipe[iPipe] != INVALID_HANDLE_VALUE)
CloseHandle(m_hPipe[iPipe]);
m_hPipe[iPipe] = CreateFile(szEpName,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE | FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL);
if (m_hPipe[iPipe] == INVALID_HANDLE_VALUE)
{
TRACE("Unable to open pipe %d\n", iPipe);
return FALSE;
}
return TRUE;
}
void CAtlasUsbDev::ClosePipe(DWORD iPipe)
{
if(iPipe >= MAX_PIPES)
return;
if (m_hPipe[iPipe] == INVALID_HANDLE_VALUE)
{
TRACE("Unable to close an unopened pipe %d\n", iPipe);
return;
}
CloseHandle(m_hPipe[iPipe]);
m_hPipe[iPipe] = INVALID_HANDLE_VALUE;
return;
}
#ifdef _DEBUG
void CAtlasUsbDev::DumpDescriptors()
{
UINT success;
ULONG siz, nBytes;
char buf[256];
PUSB_CONFIGURATION_DESCRIPTOR cd;
PUSB_INTERFACE_DESCRIPTOR id;
PUSB_ENDPOINT_DESCRIPTOR ed;
siz = sizeof(buf);
if (m_hDev == INVALID_HANDLE_VALUE)
return;
success = DeviceIoControl(m_hDev,
IOCTL_ATLASUSB_GET_CONFIG_DESCRIPTOR,
buf,
siz,
buf,
siz,
&nBytes,
NULL);
TRACE("request complete, success = %d nBytes = %d\n", success, nBytes);
if (success)
{
ULONG i;
UINT j, n;
char *pch;
pch = buf;
n = 0;
cd = (PUSB_CONFIGURATION_DESCRIPTOR) pch;
DumpConfigDescriptor( cd );
pch += cd->bLength;
do {
id = (PUSB_INTERFACE_DESCRIPTOR) pch;
DumpInterfaceDescriptor(id, n++);
pch += id->bLength;
for (j=0; j<id->bNumEndpoints; j++) {
ed = (PUSB_ENDPOINT_DESCRIPTOR) pch;
DumpEndpointDescriptor(ed,j);
pch += ed->bLength;
}
i = pch - buf;
} while (i<cd->wTotalLength);
}
return;
}
char* CAtlasUsbDev::DumpDescriptorType(UCHAR bDescriptorType)
{
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* CAtlasUsbDev::DumpEndpointType(UCHAR bmAttributes)
{
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!!";
}
}
char* CAtlasUsbDev::DumpConfigAttributes(UCHAR bmAttributes)
{
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!!";
}
}
void CAtlasUsbDev::DumpConfigDescriptor(PUSB_CONFIGURATION_DESCRIPTOR cd)
{
TRACE("\n===================\nUSB_CONFIGURATION_DESCRIPTOR\n");
TRACE(
"bLength = 0x%x, decimal %d\n", cd->bLength, cd->bLength
);
TRACE(
"bDescriptorType = 0x%x ( %s )\n", cd->bDescriptorType, DumpDescriptorType( cd->bDescriptorType )
);
TRACE(
"wTotalLength = 0x%x, decimal %d\n", cd->wTotalLength, cd->wTotalLength
);
TRACE(
"bNumInterfaces = 0x%x, decimal %d\n", cd->bNumInterfaces, cd->bNumInterfaces
);
TRACE(
"bConfigurationValue = 0x%x, decimal %d\n", cd->bConfigurationValue, cd->bConfigurationValue
);
TRACE(
"iConfiguration = 0x%x, decimal %d\n", cd->iConfiguration, cd->iConfiguration
);
TRACE(
"bmAttributes = 0x%x ( %s )\n", cd->bmAttributes, DumpConfigAttributes( cd->bmAttributes )
);
TRACE(
"MaxPower = 0x%x, decimal %d\n", cd->MaxPower, cd->MaxPower
);
}
void CAtlasUsbDev::DumpInterfaceDescriptor(PUSB_INTERFACE_DESCRIPTOR id, UINT ix)
{
TRACE("\n-----------------------------\nUSB_INTERFACE_DESCRIPTOR #%d\n", ix);
TRACE(
"bLength = 0x%x\n", id->bLength
);
TRACE(
"bDescriptorType = 0x%x ( %s )\n", id->bDescriptorType, DumpDescriptorType( id->bDescriptorType )
);
TRACE(
"bInterfaceNumber = 0x%x\n", id->bInterfaceNumber
);
TRACE(
"bAlternateSetting = 0x%x\n", id->bAlternateSetting
);
TRACE(
"bNumEndpoints = 0x%x\n", id->bNumEndpoints
);
TRACE(
"bInterfaceClass = 0x%x\n", id->bInterfaceClass
);
TRACE(
"bInterfaceSubClass = 0x%x\n", id->bInterfaceSubClass
);
TRACE(
"bInterfaceProtocol = 0x%x\n", id->bInterfaceProtocol
);
TRACE(
"bInterface = 0x%x\n", id->iInterface
);
}
void CAtlasUsbDev::DumpEndpointDescriptor(PUSB_ENDPOINT_DESCRIPTOR ed, int i)
{
TRACE(
"------------------------------\nUSB_ENDPOINT_DESCRIPTOR for Pipe%02d\n", i
);
TRACE(
"bLength = 0x%x\n", ed->bLength
);
TRACE(
"bDescriptorType = 0x%x ( %s )\n", ed->bDescriptorType, DumpDescriptorType( ed->bDescriptorType )
);
if ( USB_ENDPOINT_DIRECTION_IN( ed->bEndpointAddress ) ) {
TRACE(
"bEndpointAddress= 0x%x ( INPUT )\n", ed->bEndpointAddress
);
} else {
TRACE(
"bEndpointAddress= 0x%x ( OUTPUT )\n", ed->bEndpointAddress
);
}
TRACE(
"bmAttributes= 0x%x ( %s )\n", ed->bmAttributes, DumpEndpointType ( ed->bmAttributes )
);
TRACE(
"wMaxPacketSize= 0x%x, decimal %d\n", ed->wMaxPacketSize, ed->wMaxPacketSize
);
TRACE(
"bInterval = 0x%x, decimal %d\n", ed->bInterval, ed->bInterval
);
}
#endif//#ifdef _DEBUG
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -