📄 common.cpp
字号:
#include <windows.h>
#include "usbdi.h"
#include "common.h"
#ifdef DEBUG
void dmesg(WCHAR const* fmt, ...)
{
va_list va;
va_start(va, fmt);
static int inited= 0;
static CRITICAL_SECTION cs;
char buf[1000];
int n= vswprintf((WCHAR*)buf, fmt, va);
char*p= buf;
for(int i= 0; i<n; i++) {
char c= buf[i*2];
if(c=='\n') *p++= '\r';
*p++= c;
};
if(inited==0) {
InitializeCriticalSection(&cs);
inited= 1;
};
EnterCriticalSection(&cs);
HANDLE fh= CreateFile(L"\\hoe.txt", GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
SetFilePointer(fh, 0, 0, FILE_END);
DWORD rd;
WriteFile(fh, buf, p-buf, &rd, 0);
CloseHandle(fh);
LeaveCriticalSection(&cs);
};
void infodump(
USB_HANDLE uh,
USB_FUNCS const* uf,
USB_DEVICE const* ui)
{
if(ui==0) return;
dmesg(L"count=%d configs=x%x active=x%x\n", ui->dwCount, ui->lpConfigs, ui->lpActiveConfig);
dmesg(L"Descriptor=%d %d x%x\n", ui->Descriptor.bLength, ui->Descriptor.bDescriptorType, ui->Descriptor.bcdUSB);
dmesg(L" device=%d %d %d pkt=%d\n", ui->Descriptor.bDeviceClass, ui->Descriptor.bDeviceSubClass, ui->Descriptor.bDeviceProtocol, ui->Descriptor.bMaxPacketSize0);
dmesg(L" vendor=x%x x%x x%x\n", ui->Descriptor.idVendor, ui->Descriptor.idProduct, ui->Descriptor.bcdDevice);
dmesg(L" manu='%d product='%d serial='%d numconfig=%d\n", ui->Descriptor.iManufacturer, ui->Descriptor.iProduct, ui->Descriptor.iSerialNumber, ui->Descriptor.bNumConfigurations);
if(0&&ui->Descriptor.iManufacturer) { //error after GetDescriptor..something wrong?
char buf[64];
USB_TRANSFER ut;
ut= uf->lpGetDescriptor(uh, 0, 0, USB_IN_TRANSFER|USB_SHORT_TRANSFER_OK
, USB_STRING_DESCRIPTOR_TYPE, ui->Descriptor.iManufacturer, 0, sizeof(buf), buf);
if(ut) {
DWORD len, rc;// USB_NO_ERROR
uf->lpGetTransferStatus(ut, &len, &rc);
uf->lpCloseTransfer(ut);
dmesg(L"string %d %d\n", len, rc);
WCHAR moke[50];
if(uf->lpTranslateStringDesc((USB_STRING_DESCRIPTOR*)buf, moke, 50)) dmesg(L" manu-str=%s\n", moke); else dmesg(L" manu-str:error\n");
};
};
if(0&&ui->Descriptor.iProduct) { //error after GetDescriptor..something wrong?
char buf[64];
USB_TRANSFER ut;
ut= uf->lpGetDescriptor(uh, 0, 0, USB_IN_TRANSFER|USB_SHORT_TRANSFER_OK
, USB_STRING_DESCRIPTOR_TYPE, ui->Descriptor.iProduct, 0, sizeof(buf), buf);
if(ut) {
DWORD len, rc;// USB_NO_ERROR
uf->lpGetTransferStatus(ut, &len, &rc);
uf->lpCloseTransfer(ut);
dmesg(L"string %d %d\n", len, rc);
WCHAR moke[50];
if(uf->lpTranslateStringDesc((USB_STRING_DESCRIPTOR*)buf, moke, 50)) dmesg(L" manu-str=%s\n", moke); else dmesg(L" manu-str:error\n");
};
};
dmesg(L"configvalue=%d wTotal=%d interfaces=%d attr=x%x power=%d ext=x%x\n"
, ui->lpActiveConfig->Descriptor.bConfigurationValue, ui->lpActiveConfig->Descriptor.wTotalLength
, ui->lpActiveConfig->Descriptor.bNumInterfaces, ui->lpActiveConfig->Descriptor.bmAttributes, ui->lpActiveConfig->Descriptor.MaxPower
, ui->lpActiveConfig->lpvExtended);
dmesg(L"numinterfaces*numalt=%d count=%d\n", ui->lpActiveConfig->dwNumInterfaces, ui->lpActiveConfig->dwCount);
USB_INTERFACE const* ua= ui->lpActiveConfig->lpInterfaces;
for(UINT i= 0; i<ui->lpActiveConfig->dwNumInterfaces; i++) {
dmesg(L"num=%d alt=%d endpoints=%d\n", ua->Descriptor.bInterfaceNumber, ua->Descriptor.bAlternateSetting, ua->Descriptor.bNumEndpoints);
dmesg(L" interface=%d %d %d\n", ua->Descriptor.bInterfaceClass, ua->Descriptor.bInterfaceSubClass, ua->Descriptor.bInterfaceProtocol);
dmesg(L" intstr='%d ext=x%x\n", ua->Descriptor.iInterface, ua->lpvExtended);
USB_ENDPOINT const* ue= ua->lpEndpoints;
for(UINT j= 0; j<ua->Descriptor.bNumEndpoints; j++) {
dmesg(L" endpoint=%d count=%d addr=%d attr=x%x pkt=%d interval=%d ext=x%x\n", j, ue->dwCount, ue->Descriptor.bEndpointAddress, ue->Descriptor.bmAttributes, ue->Descriptor.wMaxPacketSize, ue->Descriptor.bInterval
, ue->lpvExtended);
if(ue->lpvExtended) {
int sz= _msize((void*)ue->lpvExtended);
BYTE* e= (BYTE*)ue->lpvExtended;
dmesg(L" [%d]", sz);
for(int i= 0; i<sz; i++) dmesg(L" %02x", e[i]);
dmesg(L"\n");
};
ue++;
};
ua++;
};
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -