📄 usbmicard.cpp
字号:
// UsbMiCard.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "UsbMiCard.h"
#include "Card.h"
#include "USB340Functions.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//
DEFINE_GUID(GUID_INTERFACE_SILABS_BULK,
0x37538c66, 0x9584, 0x42d3, 0x96, 0x32, 0xeb, 0xad, 0xa, 0x23, 0xd, 0x13);
#define SILABS_BULK_WRITEPIPE "PIPE01"
#define SILABS_BULK_READPIPE "PIPE00"
HANDLE m_hUSBDevice;
HANDLE m_hUSBWrite;
HANDLE m_hUSBRead;
#define MAX_PACKET_SIZE_READ (64 *64 )
#define MAX_PACKET_SIZE_WRITE (64 *64 )
#define MAX_WRITE_PKTS 0x01
#define FT_READ_MSG 0x00
#define FT_WRITE_MSG 0x01
#define FT_READ_ACK 0x02
#define FT_MSG_SIZE 0x03
#define COMM_ERR "COMM ERROR"
#define CARD_ERR "CARD ERROR"
#define SN_ERR "SERIAL NO ERROR"
#define BWRITE_ERR "WRITE BLOCK ERROR"
#define PWRITE_ERR "WRITE E-PURSE ERROR"
#define BREAD_ERR "READ BLOCK ERROR"
#define PREAD_ERR "READ E-PURSE ERROR"
#define TOPUP_ERR "TOPUP E-PURSE ERROR"
#define DEDUCT_ERR "DEDUCT E-PURSE ERROR"
#define CHANGEKEY_ERR "CHANGE KEY ERROR"
#define SAVEKEY_ERR "SAVE KEY ERROR"
#define AUTHENTIC_ERR "AUTHENTICATION ERROR"
#define KEY_INVALID "INVALID KEY"
#define SECTOR_INVALID "INVALID SECTOR"
#define BLOCK_INVALID "INVALID BLOCK"
#define MODE_INVALID "INVALID MODE"
#define ACCESS_INVALID "INVALID ACCESS CONDITION"
#define VALUE_INVALID "INVALID VALUE"
static unsigned long *szCardSN; // Serial Number.
static char nBuf[255];
int MifareGetSN(void);
int MifareTransKey(BYTE nSector , BYTE *sKey);
int MifareReadBlock(BYTE nBlock, unsigned long sSN, BYTE nMode, BYTE *RevDat);
int MF_ReadBlock(int BlockSN,unsigned long CardSN,int mode, BYTE *RevDat);
int MF_GetSN(unsigned long *CardSN);
int MF_TransKey(int nSector , LPSTR sKey);
BYTE cBitStatus=0;
char CurrentCardType=0x00;
/////////////////////////////////////////////////////////////////////////////
// CUsbMiCardApp
BEGIN_MESSAGE_MAP(CUsbMiCardApp, CWinApp)
//{{AFX_MSG_MAP(CUsbMiCardApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUsbMiCardApp construction
CUsbMiCardApp::CUsbMiCardApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CUsbMiCardApp object
CUsbMiCardApp theApp;
/////////////////////////////////////////////////////////////////////////////
BOOL WINAPI DeviceRead(BYTE *buffer, DWORD dwSize, DWORD *lpdwBytesRead)
{
Z_STATUS status ;
DWORD endtime=GetTickCount()+1000;
while(GetTickCount()<endtime)
{
status = Z_Read(m_hUSBRead, buffer, dwSize, lpdwBytesRead);
if(status == Z_SUCCESS)
break;
}
return (status == Z_SUCCESS);
}
BOOL WINAPI DeviceWrite(BYTE *buffer, DWORD dwSize, DWORD *lpdwBytesWritten)
{
Z_STATUS status ;
DWORD endtime=GetTickCount()+1000;
while(GetTickCount()<endtime)
{
status = Z_Write(m_hUSBWrite, buffer, dwSize, lpdwBytesWritten);
if(status == Z_SUCCESS)
break;
}
return (status == Z_SUCCESS);
}
BOOL WINAPI WriteData(BYTE *sendbuffer, DWORD sendlen)
{
BOOL success = TRUE;
DWORD size = sendlen;
DWORD dwBytesWritten = 0;
DWORD dwBytesRead = 0;
BYTE buf[MAX_PACKET_SIZE_WRITE];
if (m_hUSBDevice == INVALID_HANDLE_VALUE)
return FALSE;
buf[0] = FT_WRITE_MSG;
buf[1] = (char)(size & 0x000000FF);
buf[2] = (char)((size & 0x0000FF00) >> 8);
if (size > 4096)
{
AfxMessageBox("the data is too large.");
success = FALSE;
}
else if (DeviceWrite(buf, FT_MSG_SIZE, &dwBytesWritten))
{
if (dwBytesWritten == FT_MSG_SIZE)
{
DWORD numPkts = (size / MAX_PACKET_SIZE_WRITE) + (((size % MAX_PACKET_SIZE_WRITE) > 0)? 1 : 0);
DWORD numLoops = (numPkts / MAX_WRITE_PKTS) + (((numPkts % MAX_WRITE_PKTS) > 0)? 1 : 0);
DWORD counterPkts = 0;
DWORD counterLoops = 0;
DWORD totalWritten = 0;
while (counterLoops < numLoops && success)
{
int i = 0;
while (i < MAX_WRITE_PKTS && counterPkts < numPkts && success)
{
DWORD dwWriteLength = 0;
if ((size - totalWritten) < MAX_PACKET_SIZE_WRITE)
{
dwWriteLength = size - totalWritten;
}
else
{
dwWriteLength = MAX_PACKET_SIZE_WRITE;
}
memset(buf, 0, dwWriteLength);
memcpy(buf,sendbuffer,dwWriteLength);
dwBytesWritten = 0;
success = DeviceWrite(buf, dwWriteLength, &dwBytesWritten);
totalWritten += dwWriteLength;
counterPkts++;
i++;
}
if (success)
{
memset(buf, 0, MAX_PACKET_SIZE_WRITE);
while ((buf[0] != 0xFF) && success)
{
success = DeviceRead(buf, 1, &dwBytesRead);
}
}
counterLoops++;
}
if (!success)
{
AfxMessageBox("Target device failure while sending data.\nCheck file size.");
success = FALSE;
}
}
else
{
AfxMessageBox("Incomplete write file size message sent to device.");
success = FALSE;
}
}
else
{
AfxMessageBox("Target device failure while data size information.");
success = FALSE;
}
return success;
}
BOOL WINAPI ReadData(BYTE *revbuffer,DWORD *revlen)
{
BOOL success = TRUE;
DWORD dwBytesRead = 0;
DWORD dwBytesWritten = 0;
DWORD TotalRead=0;
DWORD size = 0;
DWORD counterPkts = 0;
DWORD numPkts = 0;
DWORD endtime;
BYTE buf[MAX_PACKET_SIZE_READ];
BYTE msg[FT_MSG_SIZE];
msg[0] = (BYTE)FT_READ_MSG;
msg[1] = (BYTE)0xFF;
msg[2] = (BYTE)0xFF;
endtime=GetTickCount()+1000;
while(GetTickCount()<endtime && !size)
{
if (DeviceWrite(msg, FT_MSG_SIZE, &dwBytesWritten))
{
memset(buf, 0, FT_MSG_SIZE);
if (DeviceRead(buf, FT_MSG_SIZE, &dwBytesRead))
{
size = (buf[1] & 0x000000FF) | ((buf[2] << 8) & 0x0000FF00);
numPkts = (size/MAX_PACKET_SIZE_READ) + (((size % MAX_PACKET_SIZE_READ) > 0)? 1 : 0);
DWORD totalRead = 0;
// Now read data from board
while (counterPkts < numPkts && success)
{
DWORD dwReadLength = 0;
dwBytesRead = 0;
if ((size - totalRead) < MAX_PACKET_SIZE_READ)
{
dwReadLength = size - totalRead;
}
else
{
dwReadLength = MAX_PACKET_SIZE_READ;
}
memset(buf, 0, dwReadLength);
if (DeviceRead(buf, dwReadLength, &dwBytesRead))
{
memcpy(revbuffer+TotalRead,buf,dwReadLength);
TotalRead+=dwBytesRead;
}
counterPkts++;
}
*revlen=TotalRead;
}
}
}
if(!size)
success = FALSE;
return success;
}
BOOL WINAPI Communicate(BYTE *revbuffer,DWORD rlen,BYTE *sendbuffer, DWORD sendlen)
{
BOOL success = TRUE;
DWORD revlen;
success=WriteData(sendbuffer, sendlen);
if(!success)
return FALSE;
//DWORD endtiem=GetTickCount()+1000;
//while(GetTickCount()<endtiem);
success=ReadData(revbuffer,&revlen);
if(!success || revlen!=rlen)
return FALSE;
return TRUE;
}
void Delay_Ms(long dly_ms)
{
DWORD endtime;
endtime=GetTickCount()+dly_ms;
while(GetTickCount()<=endtime);
}
/***************************卡操作函数********************************************/
MI_CARD_API long WINAPI OpenDevice()
{
Z_STATUS status = Z_Open(0, &m_hUSBDevice);
// Open selected file.
if (status == Z_SUCCESS)
{
// Write file to device in MAX_PACKET_SIZE-byte chunks.
// Get the write handle
m_hUSBWrite = sgCUsbIF.OpenUSBfile(SILABS_BULK_WRITEPIPE);
if (m_hUSBWrite == INVALID_HANDLE_VALUE)
{
return 1;
}
// Get the read handle
m_hUSBRead = sgCUsbIF.OpenUSBfile(SILABS_BULK_READPIPE);
if (m_hUSBRead == INVALID_HANDLE_VALUE)
{
return 1;
}
return 0;
}
else
{
return 1;
}
}
MI_CARD_API long WINAPI CloseDevice()
{
if (m_hUSBDevice != INVALID_HANDLE_VALUE)
{
Z_STATUS status =Z_Close(m_hUSBDevice);
m_hUSBDevice = INVALID_HANDLE_VALUE;
if (status == Z_SUCCESS)
return 0;
else
return 1;
}
return 1;
}
MI_CARD_API long WINAPI LinkRW()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
unsigned char Rev_buf[3];
unsigned char Send_buf[3]={0x82,0x00,0x82};
Communicate(Rev_buf,3,Send_buf,3);
if(Rev_buf[0]==0x00 && Rev_buf[1]==0x00 && Rev_buf[2]==0x00)
{
return 0;
}
else
return 1;
}
MI_CARD_API long FAR PASCAL MifUSB_SetBeep(unsigned long nFlag)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
unsigned char Rev_buf[3];
unsigned char Send_buf[4];
if(nFlag==1){
cBitStatus |= 0x04;
}
else{
cBitStatus &= ~0x04;
}
Send_buf[0]=0x7A;
Send_buf[1]=0x01;
Send_buf[2]=cBitStatus;
Send_buf[3]=Send_buf[0]^Send_buf[1]^Send_buf[2];
Communicate(Rev_buf,3,Send_buf,4);
if(Rev_buf[0]==0x00 && Rev_buf[1]==0x00 && Rev_buf[2]==0x00)
return 0;
else
return 1;
}
MI_CARD_API long FAR PASCAL MifUSB_SetRedLed(unsigned long nFlag)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
unsigned char Rev_buf[3];
unsigned char Send_buf[4];
if(nFlag==1){
cBitStatus |= 0x01;
}
else{
cBitStatus &= ~0x01;
}
Send_buf[0]=0x7A;
Send_buf[1]=0x01;
Send_buf[2]=cBitStatus;
Send_buf[3]=Send_buf[0]^Send_buf[1]^Send_buf[2];
Communicate(Rev_buf,3,Send_buf,4);
if(Rev_buf[0]==0x00 && Rev_buf[1]==0x00 && Rev_buf[2]==0x00)
return 0;
else
return 1;
}
MI_CARD_API long FAR PASCAL MifUSB_SetGreenLed(unsigned long nFlag)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
unsigned char Rev_buf[3];
unsigned char Send_buf[4];
if(nFlag==1){
cBitStatus |= 0x02;
}
else{
cBitStatus &= ~0x02;
}
Send_buf[0]=0x7A;
Send_buf[1]=0x01;
Send_buf[2]=cBitStatus;
Send_buf[3]=Send_buf[0]^Send_buf[1]^Send_buf[2];
Communicate(Rev_buf,3,Send_buf,4);
if(Rev_buf[0]==0x00 && Rev_buf[1]==0x00 && Rev_buf[2]==0x00)
return 0;
else
return 1;
}
MI_CARD_API long FAR PASCAL MifUSB_SetGreenLed_OFF()
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
unsigned char Rev_buf[3];
unsigned char Send_buf[4];
cBitStatus &= ~0x02;
Send_buf[0]=0x7A;
Send_buf[1]=0x01;
Send_buf[2]=cBitStatus;
Send_buf[3]=Send_buf[0]^Send_buf[1]^Send_buf[2];
Communicate(Rev_buf,3,Send_buf,4);
if(Rev_buf[0]==0x00 && Rev_buf[1]==0x00 && Rev_buf[2]==0x00)
return 0;
else
return 1;
}
MI_CARD_API long FAR PASCAL MifUSB_SetGreenLed_ON()
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
unsigned char Rev_buf[3];
unsigned char Send_buf[4];
cBitStatus |= 0x02;
Send_buf[0]=0x7A;
Send_buf[1]=0x01;
Send_buf[2]=cBitStatus;
Send_buf[3]=Send_buf[0]^Send_buf[1]^Send_buf[2];
Communicate(Rev_buf,3,Send_buf,4);
if(Rev_buf[0]==0x00 && Rev_buf[1]==0x00 && Rev_buf[2]==0x00)
return 0;
else
return 1;
}
MI_CARD_API long FAR PASCAL SetAlarm(unsigned long Mode)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
unsigned char Rev_buf[3];
unsigned char Send_buf[4];
Send_buf[0]=0x7A;
Send_buf[1]=0x01;
Send_buf[2]=(unsigned char)Mode;
Send_buf[3]=Send_buf[0]^Send_buf[1]^Send_buf[2];
Communicate(Rev_buf,3,Send_buf,4);
if(Rev_buf[0]==0x00 && Rev_buf[1]==0x00 && Rev_buf[2]==0x00)
return 0;
else
return 1;
}
/******************************卡操作函数********************************************/
MI_CARD_API long FAR PASCAL MifUSB_GetCardType(unsigned long *CardType)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
BYTE SendBuf[3],RevBuf[5],i,check=0;
BOOL status;
SendBuf[0]=0xAA;
SendBuf[1]=0x00;
SendBuf[2]=0xAA;
status=Communicate(RevBuf,5,SendBuf,3);
if(status)
{
for(i=0;i<4;i++)
check^=RevBuf[i];
if(check==RevBuf[4] && RevBuf[0]==0x00 && RevBuf[1]==0x02)
{
CurrentCardType=RevBuf[2];
if(RevBuf[2]==0x04 && RevBuf[3]==0x00)
{
*CardType=4;
}
//"Mifare Light L10"
else if(RevBuf[2]==0x10 && RevBuf[3]==0x00)
{
*CardType=10;
}
else if(RevBuf[2]==0x44 && RevBuf[3]==0x00)
{
*CardType=44;
}
else if(RevBuf[2]==0x02 && RevBuf[3]==0x00)
{
*CardType=2;
}
else
return 1;
return 0;
}
else
return 1;
}
else
return 1;
}
MI_CARD_API long FAR PASCAL GetCardType(unsigned long *CardType)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
BYTE SendBuf[3],RevBuf[5],i,check=0;
BOOL status;
SendBuf[0]=0xAA;
SendBuf[1]=0x00;
SendBuf[2]=0xAA;
status=Communicate(RevBuf,5,SendBuf,3);
if(status)
{
for(i=0;i<4;i++)
check^=RevBuf[i];
if(check==RevBuf[4] && RevBuf[0]==0x00 && RevBuf[1]==0x02)
{
if(RevBuf[2]==0x04 && RevBuf[3]==0x00)
{
*CardType=4;
}
//"Mifare Light L10"
else if(RevBuf[2]==0x10 && RevBuf[3]==0x00)
{
*CardType=10;
}
else if(RevBuf[2]==0x44 && RevBuf[3]==0x00)
{
*CardType=44;
}
else if(RevBuf[2]==0x22 && RevBuf[3]==0x00)
{
*CardType=22;
}
else
return 1;
return 0;
}
else
return 1;
}
else
return 1;
}
MI_CARD_API LPSTR FAR PASCAL MifUSB_GetCardSN(unsigned long *CardSN)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
BYTE SendBuf[3],RevBuf[7],i,check=0;
BOOL status;
char szBuf[255];
memset(szBuf, 0, 255);
SendBuf[0]=0xAB;
SendBuf[1]=0x00;
SendBuf[2]=0xAB;
status=Communicate(RevBuf,7,SendBuf,3);
if(status)
{
for(i=0;i<6;i++)
check^=RevBuf[i];
if(check==RevBuf[6] && RevBuf[0]==0x00 && RevBuf[1]==0x04)
{
*CardSN=RevBuf[2]+RevBuf[3]*256+RevBuf[4]*256*256+RevBuf[5]*256*256*256;
wsprintf(szBuf,"%02X%02X%02X%02X",
(BYTE *)RevBuf[5],(BYTE *)RevBuf[4],
(BYTE *)RevBuf[3],(BYTE *)RevBuf[2]);
szBuf[8]=0x00;
}
else wsprintf(szBuf,"NAK");
}
else wsprintf(szBuf,"NAK");
return(szBuf);
/*
if(status)
{
for(i=0;i<6;i++)
check^=RevBuf[i];
if(check==RevBuf[6] && RevBuf[0]==0x00 && RevBuf[1]==0x04)
{
*CardSN=RevBuf[2]+RevBuf[3]*256+RevBuf[4]*256*256+RevBuf[5]*256*256*256;
// *CardSN=RevBuf[5]*256*256*256+RevBuf[4]*256*256+RevBuf[3]*256+RevBuf[2];
return 0;
}
else
return 1;
}
else
return 1;
*/
}
MI_CARD_API long FAR PASCAL GetCardSN(unsigned long *CardSN)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -