📄 usb_diag_lib.cpp
字号:
////////////////////////////////////////////////////////////////
// File - USB_DIAG_LIB.C
//
// Utility functions for communication with USB devices
// using WinDriver's API.
//
// Copyright (c) 2003 - 2006 Jungo Ltd. http://www.jungo.com
//
////////////////////////////////////////////////////////////////
//将结构体USB_LISTEN_PIPE的定义移至usb_diag_lib.h中;
//将结构体listenPipe的定义由ReadPipesadder移至该文件顶部,且在头文件中声明它为外部变量
//CloseListening的声明由该文件移至它的头文件中
#include "StdAfx.h"
#include "windrvr.h"
#include "wdu_lib.h"
#include "status_strings.h"
#include "utils.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>
#if !defined(ERR)
#define ERR printf
#endif
#define TRANSFER_TIMEOUT 30000// in msecs
USB_LISTEN_PIPE listenPipe;
DWORD dwSize;
DWORD dwPipeNum;
DWORD cmd;
// Function: CloseListening()
// Stop listening to USB device pipe
//void CloseListening(USB_LISTEN_PIPE *pListenPipe);
// Function: ListenToPipe()
// Start listening to a USB device pipe
void ListenToPipe(USB_LISTEN_PIPE *pListenPipe);
// Function: pipeType2Str()
// Returns a string identifying the pipe type
// Parameters:
// pipeType [in] pipe type
// Return Value:
// String containing the description of the pipe
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];
// Function: CloseListening()
// Stop listening to USB device pipe
// Parameters:
// pListenPipe [in] pointer to USB device pipe
// Return Value:
// None
void CloseListening(USB_LISTEN_PIPE* pListenPipe)
{
if (!pListenPipe->hThread)
return;
printf("Stop listening to pipe\n");
pListenPipe->fStopped = TRUE;
WDU_HaltTransfer(pListenPipe->hDevice, pListenPipe->pPipe->dwNumber);
ThreadWait(pListenPipe->hThread);
pListenPipe->hThread = NULL;
}
// Function: PipeListenHandler()
// Callback function, which listens to a pipe continuously when there is data
// available in the pipe
// Parameters:
// pParam [in] contains the relevant pipe information
// Return Value:
// None
void DLLCALLCONV PipeListenHandler(void * pParam)
{
DWORD dwError;
USB_LISTEN_PIPE *pListenPipe = (USB_LISTEN_PIPE*) pParam;
DWORD dwBufsize = pListenPipe->pPipe->dwMaximumPacketSize;
DWORD dwOptions = 0;
int u=0;
int v=0;
int Tsize=64;
int Usize=2048;
int ju[64];
PVOID buf;
PVOID buffer;
buffer=malloc(Usize);
CFile ImageFile;
CFileException fe;
if(!ImageFile.Open ("e:\\gwq.txt",CFile::modeCreate | CFile::modeReadWrite, &fe) )
{
AfxMessageBox("can't open file");
return;
}
// memory=malloc(655360);
if (pListenPipe->pPipe->type == PIPE_TYPE_ISOCHRONOUS)
{
dwBufsize *= 8; // suit 8 minimum packets for high speed transfers
dwOptions |= USB_ISOCH_FULL_PACKETS_ONLY;
}
buf = malloc(dwBufsize);
DWORD dwBytesTransferred;
for (;;)
{
dwError = WDU_Transfer(pListenPipe->hDevice, pListenPipe->pPipe->dwNumber,
TRUE, dwOptions, buf, dwBufsize, &dwBytesTransferred, NULL, TRANSFER_TIMEOUT);
if (pListenPipe->fStopped)
break;
PBYTE ju = (PBYTE) buf;
if( ju[0]== 0xaa && ju[1]== 0x55 && ju[2]==0xEB && ju[3]==0x90)
{
u++;
ImageFile.Write(buf, 64);
//add code to receive the 60 useful bytes;
WDU_Transfer(pListenPipe->hDevice, pListenPipe->pPipe->dwNumber,
TRUE, dwOptions, buffer,2048,&dwBytesTransferred, NULL, TRANSFER_TIMEOUT);
ImageFile.Write(buffer, 2048);
}
//add code to receive the 1984 useful bytes
//my purpose of using u is to read
/// }
else
{
// if the condition can't be statified; please add code here.
}
if(u==640)
{
AfxMessageBox("Image Received");
ImageFile.Close();
break;
}
if (dwError)
{
pListenPipe->dwError = dwError;
break;
}
}
free(buf);
free(buffer);
}
// Function: ListenToPipe()
// Start listening to a USB device pipe
// Parameters:
// pListenPipe [in] pipe to listen to
// Return Value:
// None
void ListenToPipe(USB_LISTEN_PIPE *pListenPipe)
{
// start the running thread
pListenPipe->fStopped = FALSE;
printf("Start listening to pipe\n");
pListenPipe->dwError = ThreadStart(&pListenPipe->hThread,
PipeListenHandler, (PVOID) pListenPipe);
}
// Function: ReadWritePipesMenu()
// Displays menu to read/write from the device's pipes
// Parameters:
// hDevice [in] handle to the USB device
// Return Value:
// None
void ReadPipes(HANDLE hDevice)
{
DWORD dwError;
WDU_DEVICE *pDevice;
WDU_ALTERNATE_SETTING *pAltSet;
WDU_PIPE_INFO *pPipes;
BYTE SetupPacket[8];
DWORD dwBytesTransferred;
BYTE i=0;
VOID *pBuffer = NULL;
int shu[3];
dwError = WDU_GetDeviceInfo(hDevice, &pDevice);
if (dwError)
{
AfxMessageBox("ReadWritePipesMenu FAILED");
return;
}
pAltSet = pDevice->pActiveInterface[0]->pActiveAltSetting;
pPipes = pAltSet->pPipes;
// search for the pipe
if (dwPipeNum)
{
for (i=0; i<pAltSet->Descriptor.bNumEndpoints; i++)
{
if (pPipes[i].dwNumber==dwPipeNum)
break;
if (i >= pAltSet->Descriptor.bNumEndpoints)
{
AfxMessageBox("The pipe number 0x%lx does not exist, please try again.");
}
}
}
if (dwSize)
{
pBuffer= malloc(dwSize);
if (!pBuffer)
{
AfxMessageBox("cannot alloc memory!");
}
PBYTE shu = (PBYTE)pBuffer;
if (cmd==2)
{
shu[0]=0xa1;
shu[1]=0xa2;
shu[2]=0xa3;
}
else if(cmd==3)
{
shu[0]=0xb1;
shu[1]=0xb2;
shu[2]=0xb3;
}
}
dwError = WDU_Transfer(hDevice, dwPipeNum? pPipes[i].dwNumber : 0, cmd==1, 0, pBuffer,
dwSize, &dwBytesTransferred, NULL, TRANSFER_TIMEOUT);
if(dwError)
{
("ReadPipes: WDU_Transfer() failed");
}
if (pBuffer)
free(pBuffer);
}
void ReadPipesadder(HANDLE hDevice)
{
DWORD dwError;
WDU_DEVICE *pDevice;
WDU_ALTERNATE_SETTING *pAltSet;
WDU_PIPE_INFO *pPipes;
BYTE i=0;
VOID *pBuffer = NULL;
dwError = WDU_GetDeviceInfo(hDevice, &pDevice);
if (dwError)
{
AfxMessageBox("ReadWritePipesMenu FAILED");
return;
}
pAltSet = pDevice->pActiveInterface[0]->pActiveAltSetting;
pPipes = pAltSet->pPipes;
// search for the pipe
if (dwPipeNum)
{
for (i=0; i<pAltSet->Descriptor.bNumEndpoints; i++)
{
if (pPipes[i].dwNumber==dwPipeNum)
break;
if (i >= pAltSet->Descriptor.bNumEndpoints)
{
AfxMessageBox("The pipe number 0x%lx does not exist, please try again.");
}
}
}
BZERO(listenPipe);
listenPipe.hDevice = hDevice;
listenPipe.pPipe = &pPipes[i];
ListenToPipe(&listenPipe);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -