📄 usb_diag_lib.cpp
字号:
////////////////////////////////////////////////////////////////
// File - USB_DIAG_LIB.C
//
// Utility functions for printing device information,
// detecting USB devices
//
////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "usb.h"
#include "usbDlg.h"
#include "c:/windriver/include/windrvr.h"
#ifdef _USE_SPECIFIC_KERNEL_DRIVER_
#undef WD_Open
#define WD_Open WD_OpenKernelHandle
#if defined(UNIX)
#undef WD_FUNCTION
#define WD_FUNCTION(wFuncNum,h,pParam,dwSize,fWait) ((ULONG) ioctl((int)(h), wFuncNum, pParam))
#endif
#endif
#include "usb_diag_lib.h"
//#include <stdio.h>
#include <ctype.h>
#include <string.h>
extern char temp[256];
extern CUsbDlg *pDlg;
char *pipeType2Str(ULONG pipeType)
{
char *res = "unknown";
switch (pipeType)
{
case PIPE_TYPE_CONTROL:
res = "Control";
break;
case PIPE_TYPE_ISOCHRONOUS:
res = "Isochronous";
break;
case PIPE_TYPE_BULK:
res = "Bulk";
break;
case PIPE_TYPE_INTERRUPT:
res = "Interrupt";
break;
}
return res;
}
// input of command from user
static char line[256];
BOOL USB_Get_WD_handle(HANDLE *phWD)
{
WD_VERSION ver;
*phWD = INVALID_HANDLE_VALUE;
*phWD = WD_Open();
// Check whether handle is valid and version OK
if (*phWD==INVALID_HANDLE_VALUE)
{
sprintf(temp, "Cannot open " WD_PROD_NAME " device\x0D\x0A");
pDlg->m_Message += temp;
pDlg->UpdateData();
return FALSE;
}
BZERO(ver);
WD_Version(*phWD, &ver);
if (ver.dwVer<WD_VER)
{
sprintf(temp, "Error - incorrect " WD_PROD_NAME " version\n");
pDlg->m_Message += temp;
pDlg->UpdateData();
WD_Close (*phWD);
*phWD = INVALID_HANDLE_VALUE;
return FALSE;
}
return TRUE;
}
void USB_Print_device_info(DWORD dwVendorId, DWORD dwProductId)
{
DWORD i;
HANDLE hWD;
WD_USB_SCAN_DEVICES usbScan;
if (!USB_Get_WD_handle (&hWD)) return;
BZERO(usbScan);
usbScan.searchId.dwVendorId = dwVendorId;
usbScan.searchId.dwProductId = dwProductId;
WD_UsbScanDevice(hWD,&usbScan);
for (i=0; i<usbScan.dwDevices; i++)
{
WD_USB_DEVICE_GENERAL_INFO *genInfo = &usbScan.deviceGeneralInfo[i];
sprintf(temp, "USB device - Vendor ID: %04x, Product ID: %04x, unique ID: 0x%x\x0D\x0A",
genInfo->deviceId.dwVendorId,
genInfo->deviceId.dwProductId,
usbScan.uniqueId[i]);
pDlg->m_Message += temp;
sprintf(temp, " physical address: 0x%x, Hub No. %d, Port No.%d\x0D\x0A",
genInfo->deviceAddress,
genInfo->dwHubNum,
genInfo->dwPortNum);
pDlg->m_Message += temp;
sprintf(temp, " %s speed, device has %d configuration(s)\x0D\x0A",
genInfo->fFullSpeed ? "Full" : "Low",
genInfo->dwConfigurationsNum);
pDlg->m_Message += temp;
if (genInfo->fHub)
{
sprintf(temp, " Device is Hub, Hub has %d ports, %s powered, %d mA\x0D\x0A",
genInfo->hubInfo.dwPorts,
genInfo->hubInfo.fBusPowered ? "Bus": "Self",
genInfo->hubInfo.dwHubControlCurrent);
pDlg->m_Message += temp;
}
pDlg->m_Message += "\x0D\x0A";
// if (i < usbScan.dwDevices-1)
// {
// printf("Press Enter to continue to the next device\n");
// fgets(tmp, sizeof(tmp), stdin);
// }
}
pDlg->Display();
WD_Close (hWD);
}
void USB_Print_all_devices_info()
{
USB_Print_device_info(0, 0);
}
void USB_Print_device_Configurations()
{
WD_USB_CONFIGURATION config;
DWORD i, j;
HANDLE hWD;
WD_USB_SCAN_DEVICES usbScan;
if (!USB_Get_WD_handle (&hWD)) return;
BZERO (config);
BZERO (usbScan);
// printf("Please enter the unique ID of the device: ");
// fgets(line, sizeof(line), stdin);
// sscanf(line, "0x%x", &config.uniqueId);
// printf("Please enter the configuration index to display (zero based): ");
// fgets(line, sizeof(line), stdin);
// sscanf(line, "%d", &config.dwConfigurationIndex);
WD_UsbScanDevice(hWD,&usbScan);
for (i=0;i<usbScan.dwDevices;i++)
{
config.uniqueId = usbScan.uniqueId[i];
config.dwConfigurationIndex = 0;
WD_UsbGetConfiguration(hWD, &config);
sprintf(temp, "Configuration no. %d has %d interface(s)\x0D\x0A",
config.configuration.dwValue,
config.configuration.dwNumInterfaces);
pDlg->m_Message += temp;
sprintf(temp, "configuration attributes: 0x%x, max power: %d mA\x0D\x0A\x0D\x0A",
config.configuration.dwAttributes,
config.configuration.MaxPower*2);
pDlg->m_Message += temp;
}
for (i=0; i<config.dwInterfaceAlternatives; i++)
{
WD_USB_INTERFACE_DESC *pInterface = &config.Interface[i].Interface;
sprintf(temp, "interface no. %d, alternate setting: %d, index: %d\x0D\x0A",
pInterface->dwNumber,
pInterface->dwAlternateSetting,
pInterface->dwIndex);
pDlg->m_Message += temp;
sprintf(temp, "end-points: %d, class: 0x%x, sub-class: 0x%x, protocol: 0x%x\x0D\x0A",
pInterface->dwNumEndpoints,
pInterface->dwClass,
pInterface->dwSubClass,
pInterface->dwProtocol);
pDlg->m_Message += temp;
for (j=0; j<pInterface->dwNumEndpoints; j++)
{
WD_USB_ENDPOINT_DESC *pEndPoint = &config.Interface[i].Endpoints[j];
sprintf(temp, " end-point address: 0x%x, attributes: 0x%x, max packet size: %d, Interval: %d\x0D\x0A",
pEndPoint->dwEndpointAddress,
pEndPoint->dwAttributes,
pEndPoint->dwMaxPacketSize,
pEndPoint->dwInterval);
pDlg->m_Message += temp;
}
pDlg->m_Message += "\x0D\x0A";
// if (i < config.dwInterfaceAlternatives-1)
// {
// printf("Press Enter to continue to the next configuration\n");
// fgets(tmp, sizeof(tmp), stdin);
// }
}
pDlg->Display();
WD_Close (hWD);
}
#define BYTES_IN_LINE 16
#define HEX_CHARS_PER_BYTE 3
#define HEX_STOP_POS BYTES_IN_LINE*HEX_CHARS_PER_BYTE
void PrintHexBuffer(PVOID pBuffer, DWORD dwBytes)
{
PBYTE pData = (PBYTE) pBuffer;
CHAR pHex[HEX_STOP_POS+1];
CHAR pAscii[BYTES_IN_LINE+1];
DWORD offset;
// DWORD i;
if (!dwBytes)
return;
for (offset=0; offset<dwBytes; offset++)
{
DWORD line_offset = offset%BYTES_IN_LINE;
if (offset && !line_offset)
{
pAscii[line_offset] = '\0';
sprintf(temp, "%s | %s\x0D\x0A", pHex, pAscii);
pDlg->m_Message += temp;
}
sprintf(pHex+line_offset*HEX_CHARS_PER_BYTE, "%02X ", (UINT)pData[offset]);
pAscii[line_offset] = (CHAR)((pData[offset]>=0x20) ? pData[offset] : '.');
}
// print the last line. fill with blanks if needed
/* if (offset%BYTES_IN_LINE)
{
for (i=(offset%BYTES_IN_LINE)*HEX_CHARS_PER_BYTE; i<BYTES_IN_LINE*HEX_CHARS_PER_BYTE; i++)
pHex[i] = ' ';
pHex[i] = '\0';
}
pAscii[offset%BYTES_IN_LINE]='\0';
*/
// sprintf(temp, "%s | %s\x0D\x0A", pHex, pAscii);
pDlg->m_Message += pHex;
pDlg->m_Message += " ";
// pDlg->Display();
}
void CloseListening(USB_LISTEN_PIPE* pListenPipe)
{
WD_USB_TRANSFER transfer;
BZERO(transfer);
if (!pListenPipe->hThread)
return;
pDlg->m_Message += "\x0D\x0AStop listening to pipe.\x0D\x0A\x0D\x0A";
pDlg->Display();
pListenPipe->fStopped = TRUE;
pListenPipe->stop_pipe_func(pListenPipe->hDevice);
WaitForSingleObject(pListenPipe->hThread, INFINITE);
CloseHandle(pListenPipe->hThread);
pListenPipe->hThread = NULL;
}
DWORD WINAPI PipeListenHandler(void * pParam)
{
USB_LISTEN_PIPE *pListenPipe = (USB_LISTEN_PIPE*) pParam;
PVOID buf = malloc(pListenPipe->dwPacketSize);
for (;;)
{
DWORD dwBytesTransfered = pListenPipe->read_pipe_func(pListenPipe->hDevice, buf, pListenPipe->dwPacketSize);
if(pListenPipe->fStopped)
break;
if(dwBytesTransfered==-1)
{
pDlg->m_Message += "Transfer failed\x0D\x0A";
pDlg->Display();
break;
}
if (pListenPipe->process_data_func)
pListenPipe->process_data_func(buf, dwBytesTransfered, pListenPipe->pContext);
else
PrintHexBuffer(buf, dwBytesTransfered);
}
free(buf);
return 0;
}
void ListenToPipe(USB_LISTEN_PIPE *pListenPipe)
{
// start the running thread
DWORD threadId;
pListenPipe->fStopped = FALSE;
pListenPipe->hThread = CreateThread (0, 0x1000, PipeListenHandler,
(PVOID) pListenPipe, 0, &threadId);
}
int GetHexChar()
{
int ch;
ch = getchar();
if (!isxdigit(ch))
return -1;
if (isdigit(ch))
return ch - '0';
else
return toupper(ch) - 'A' + 10;
}
DWORD GetHexBuffer(PVOID pBuffer, DWORD dwBytes)
{
DWORD i;
PBYTE pData = (PBYTE) pBuffer;
int res;
int ch;
for (i=0; i<dwBytes;)
{
ch = GetHexChar();
if (ch<0)
continue;
res = ch << 4;
ch = GetHexChar();
if (ch<0)
continue;
res += ch;
pData[i] = (BYTE)res;
i++;
}
// advance to new line
while (getchar()!=10){}
// return the number of bytes that was read
return i;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -