#include "usbdebug.h"
#define DBG_COMMAND_SIZE 64
#define DBG_PACKET_SIZE 64
//Follow is the leader for a command data
#define DBG_COMMAND_START0 0xFF
#define DBG_COMMAND_START1 0xAA
#define DBG_COMMAND_START2 0x55
//follow is command
#define DBG_RESET_DEVICE 0x01 //reset flash
#define DBG_GET_PRINTFS 0x02
#define DBG_UPDATE_SDRAM 0x03
#define DBG_UPDATE_FLASH 0x04
#define DBG_DOWNLOAD_SDRAM 0x05
#define DBG_CHECK_VERSION 0x06
#define DBG_RESET_SDRAM 0x07
unsigned char DevicePortName[256];
unsigned char DM_buffer[256];
void Process_Device_Message(DWORD Msg_Size, unsigned char *Msg_buffer);
DWORD Read_Device_Message(unsigned char *DataBuffer);
bool Connect_Usb_Device(void);
/*************************************************************************
Check if USB device attach?
*************************************************************************/
bool USB_Detecting_Device(void)
{
HDEVINFO hDev;
SP_DEVICE_INTERFACE_DATA devIntData;
SP_DEVICE_INTERFACE_DETAIL_DATA *devIntDetail;
bool Printer_Detect_Flag;
DWORD index,rv,cbNeeded,err;
/* printer usb interface GUID */
GUID ObjGUID={0x28d78fad,0x5a12,0x11d1,0xae,0x5b,0x00,0x00,0xf8,0x03,0xa8,0xc2};
if(UsbM.CurrentStatus != ATTACH_STATUS)
return true;
devIntData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
devIntDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(256);
devIntDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
//
for(index=0,Printer_Detect_Flag = false;index<6;index++)
{
hDev = SetupDiGetClassDevs(&ObjGUID,NULL,NULL, DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
if(hDev == INVALID_HANDLE_VALUE)
break;
rv = SetupDiEnumDeviceInterfaces(hDev, NULL, &ObjGUID, index, &devIntData);
if(rv==0)
break;
rv = SetupDiGetDeviceInterfaceDetail(hDev, &devIntData, devIntDetail, 256, &cbNeeded, NULL);
if(rv==0)
break;
else
{
strcpy((char *)DevicePortName,devIntDetail->DevicePath);
UsbWrite((char *)DevicePortName);
UsbWrite(" Obtained \r\n================================================================================= \r\n");
Printer_Detect_Flag = true;
RW_Handle = INVALID_HANDLE_VALUE;
SetupDiDestroyDeviceInfoList(hDev);
}
}
free(devIntDetail);
if(hDev != INVALID_HANDLE_VALUE)
SetupDiDestroyDeviceInfoList(hDev);
if(Printer_Detect_Flag == true)
return true;
else{
err = GetLastError();
//find in http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/system_error_codes.asp
return false;
}
}
/*************************************************************************
Usb dectect device message (Comunicationing……)
*************************************************************************/
bool USB_Conmunication(void)
{
DWORD R_Size;
R_Size = 0;
if((RW_Handle==INVALID_HANDLE_VALUE)||(RW_Handle==NULL))
RW_Handle = CreateFile(
(char *)DevicePortName,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL
);
if(RW_Handle==INVALID_HANDLE_VALUE)
{
int werr=GetLastError();
return false;
}
if(Connect_Usb_Device() == false){
ResetDeviceHandle();
return false;
}
Sleep(100);
R_Size = Read_Device_Message(DM_buffer);
while(R_Size>0)
{
Process_Device_Message(R_Size, DM_buffer);
R_Size = Read_Device_Message(DM_buffer);
}
return true;
}
/*************************************************************************
Usb host(PC) send a command to device
*************************************************************************/
bool Connect_Usb_Device(void)
{
OVERLAPPED ov;
bool rv;
DWORD cbRead,cbWrote;
int timeout;
unsigned char CmdRequest[64] = {DBG_COMMAND_START0,
DBG_COMMAND_START1,
DBG_COMMAND_START2,
DBG_GET_PRINTFS
};
cbWrote=0;
memset(&ov, 0, sizeof(ov));
rv = WriteFile(RW_Handle, CmdRequest, 64, &cbWrote, &ov);
int werr=GetLastError();
if(werr==ERROR_ACCESS_DENIED)
return false;
if(rv == true)
return true;
for(timeout=0; timeout<4; timeout++)
{
rv = GetOverlappedResult(RW_Handle, &ov, &cbRead, FALSE);
if(rv == true)
return true;
Sleep(5);
}
return false;
}
/*************************************************************************
Usb host(PC) Get message form printer device
*************************************************************************/
DWORD Read_Device_Message(unsigned char *DataBuffer)
{
OVERLAPPED ov;
BOOL rv;
int timeout;
DWORD cbRead;
memset(&ov, 0, sizeof(ov));
cbRead = 0;
rv = ReadFile(RW_Handle, DataBuffer, DBG_PACKET_SIZE, &cbRead, &ov);
int werr=GetLastError();
if(werr==ERROR_ACCESS_DENIED)
return 0;
if(rv == true)
return cbRead;
for(timeout=0;timeout<6;timeout++)
{
rv = GetOverlappedResult(RW_Handle, &ov, &cbRead, FALSE);
if(rv == true)
return cbRead;
Sleep(1);
}
if(cbRead == 0)
//ResetDeviceHandle();
CancelIo(RW_Handle);
return cbRead;
}
/*************************************************************************
Process device message and ouput in windows
*************************************************************************/
void Process_Device_Message(DWORD Msg_Size, unsigned char *Msg_buffer)
{
int i,j,Pindex,stringlen;
char Newstring[100];
static DWORD BlockSize=0;
Pindex=0;
while(Msg_Size>0)
{
//check get Command head
if(Msg_buffer[Pindex]==DBG_COMMAND_START0)
{
if(Msg_buffer[Pindex+1]==DBG_COMMAND_START1)
if(Msg_buffer[Pindex+2]==DBG_COMMAND_START2)
if(Msg_buffer[Pindex+3]==DBG_GET_PRINTFS)
{
BlockSize = Msg_buffer[4];
BlockSize += Msg_buffer[5]<<8;
BlockSize += Msg_buffer[6]<<16;
BlockSize += Msg_buffer[7]<<24;
Msg_Size = Msg_Size - DBG_COMMAND_SIZE;
Pindex=Pindex + DBG_COMMAND_SIZE;
}
}
if((BlockSize>0)&&(Msg_Size>0))
{
if(BlockSize>Msg_Size)
{
BlockSize-=Msg_Size;
stringlen=Msg_Size;
Msg_Size=0;
}
else
{
Msg_Size -= BlockSize;
stringlen=BlockSize;
BlockSize = 0;
}
/* make sure the line end*/
for(i=Pindex,j=0;i<stringlen;i++)
if(((Msg_buffer[i]=='\n')&&(Msg_buffer[i-1]!='\r'))||(Msg_buffer[i]=='\0'))
{
Newstring[j++]='\r';
Newstring[j++]='\n';
}
else
Newstring[j++]=Msg_buffer[i];
Newstring[j]='\0';
UsbWrite(Newstring);
Pindex=Pindex+stringlen;
}
else{
Msg_Size = 0;
BlockSize = 0;
}
}
return ;
}
void ResetDeviceHandle(void)
{
CloseHandle(RW_Handle);
CancelIo(RW_Handle);
RW_Handle==INVALID_HANDLE_VALUE;
}
bool UpdateFirmware(REQUESTTYPE Mode)
{
static TCHAR pstrFileName[MAX_PATH];
if(UsbM.CurrentStatus != WORKING_STATUS){
UsbWrite("Error, No any Device attach!!\r\n");
return false;
}
if((Mode == UPDATA_SDRAM)||(Mode == UPDATA_FLASH))
if(OpenAFile (pstrFileName) == false)
return false;
if(SendBinFileToFW(pstrFileName,Mode) == true)
return true;
else
return false;
}
bool SendBinFileToFW(TCHAR pFileName[], REQUESTTYPE sMode)
{
bool rv;
PBYTE BlcokBuf;
HANDLE hFile,hDevice;
DWORD dwBytesRead,BlockLen,iFileLength,SendLen,msecs;
UsbWrite("\r\n\r\n ** ");
UsbWrite(pFileName);
if(sMode == UPDATA_FLASH)
UsbWrite(" ** Flash Updating ...\r\n");
else
UsbWrite(" ** SDRam Updating ...\r\n");
Disable_Muli_Thread();
hDevice = INVALID_HANDLE_VALUE;
hFile = INVALID_HANDLE_VALUE;
hDevice = CreateFile(
(char *)DevicePortName,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL
);
if(hDevice==INVALID_HANDLE_VALUE){
UsbWrite(" Can't Open Usb device pipe! \r\n");
return false;
}
hFile = CreateFile (pFileName, GENERIC_READ, FILE_SHARE_READ,NULL, OPEN_EXISTING, 0, NULL);
if (hFile==INVALID_HANDLE_VALUE){
UsbWrite(" Can't Open file! \r\n");
CloseHandle (hDevice);
return false ;
}
iFileLength = GetFileSize (hFile, NULL) ;
if(USBSendFWRquest(iFileLength,sMode) == false){
UsbWrite(" Can't get reply from FW! \r\n");
CloseHandle (hDevice);
CloseHandle (hFile);
return false;
}
BlcokBuf =(PBYTE) malloc (FILEBLOCKSIZE);
while(iFileLength)
{
BlockLen=(iFileLength>FILEBLOCKSIZE)?FILEBLOCKSIZE:iFileLength;
// Read file and put terminating zeros at end.
ReadFile (hFile, BlcokBuf, BlockLen, &dwBytesRead, NULL) ;
rv = WriteFile(RW_Handle, BlcokBuf, BlockLen, &SendLen, 0);
if(rv==FALSE)
{
int werr=GetLastError();
CloseHandle (hDevice);
CloseHandle (hFile);
UsbWrite("Disconnect while updating! \r\n");
return false;
}
iFileLength=iFileLength-SendLen;
}
CloseHandle (hDevice);
CloseHandle (hFile);
RW_Handle=INVALID_HANDLE_VALUE;
Sleep(500);
return true;
}
bool USBSendFWRquest(DWORD DataSize, REQUESTTYPE RMode)
{
return true;
} ...