📄 mwicrd.cpp
字号:
/*
* MW IC Reader Routines
*
* Writen by Alfred Meng
* July 9, 2002
*/
#include <string.h>
#include <stdlib.h>
#include "cardutil.h"
#include "gdcmd.h"
#include "mwicrd.h"
// MWIC_32.DLL function interface
HANDLE __stdcall (* auto_init)(__int16 port,unsigned long baud);
__int16 __stdcall (* ic_exit)(HANDLE icdev);
__int16 __stdcall (* sam_reset)(HANDLE icdev,unsigned char CardType,__int16 *len,unsigned char *receive_data);
__int16 __stdcall (* sam_comres)(HANDLE icdev,unsigned char CardType,__int16 sLen, unsigned char *send_cmd, unsigned char *receive_data);
static char __MWID__[] = { 0,1,2,3 };
//------------------------------------------------------------------
TMWICRD::TMWICRD()
{
assign(GDCommand,TOTAL_CMD);
iso_case4( CASE4_Disable );
__HMWICDLL__ = NULL;
__curport__ = IC_ICC;
bOpened = 0;
}
//------------------------------------------------------------------
int TMWICRD::LoadDLL()
{
if ((__HMWICDLL__ = LoadLibrary("mwic_32.dll")) == NULL ) return -1;
(void *)auto_init = (void *)GetProcAddress(__HMWICDLL__,"auto_init");
(void *)ic_exit = (void *)GetProcAddress(__HMWICDLL__,"ic_exit");
(void *)sam_reset = (void *)GetProcAddress(__HMWICDLL__,"sam_reset");
(void *)sam_comres = (void *)GetProcAddress(__HMWICDLL__,"sam_comres");
return 0;
}
//------------------------------------------------------------------
int TMWICRD::openterm(short port)
{
short mwPort = port-1;
if ( LoadDLL()!= 0 ) return -1;
__HMWICRD__ = auto_init(mwPort,9600);
bOpened = 1;
return (__HMWICRD__==NULL)?(-1):0;
}
//------------------------------------------------------------------
int TMWICRD::closeterm()
{
if (__HMWICRD__)
rc = ic_exit(__HMWICRD__);
if (__HMWICDLL__) {
FreeLibrary(__HMWICDLL__);
__HMWICDLL__ = NULL;
}
bOpened = 0;
return 0;
}
//------------------------------------------------------------------
int TMWICRD::opensam(int plug)
{
rc = sam_reset(__HMWICRD__, __MWID__[plug], &RespLen,Resp);
__curport__ = plug;
return 0;
}
//------------------------------------------------------------------
int TMWICRD::selectICC(int icc,bool reqATR)
{
__curport__ = icc;
if(reqATR)
RequestATR();
return rc;
}
int TMWICRD::hascard()
{
//get_status
return 0;
}
int TMWICRD::ejectcard(bool beep)
{
return 0;
}
//------------------------------------------------------------------
int TMWICRD::RequestATR()
{
rc = sam_reset(__HMWICRD__, __MWID__[__curport__], &RespLen, Resp);
return 0;
}
//------------------------------------------------------------------
int TMWICRD::sendapdu(TAPDU *pApdu)
{
register char _data[BUF_SIZE];
register i,len;
__wrong_le:
// check CASE4
memset( _data, 0 , BUF_SIZE );
memcpy( _data, "\x00\x40", 2);
_data[2] = pApdu->len;
memcpy( _data+3,pApdu->apdu, pApdu->len);
len = 3 + pApdu->len;
for( i = 0 ; i< len ; i++ ) _data[len] = _data[len] ^ _data[i];
len++;
rc = sam_comres(__HMWICRD__, __MWID__[__curport__], len, _data, Resp);
if ( rc != 0 ) return rc;
RespLen = Resp[2];
memcpy(Resp,Resp+3, RespLen);
SW1 = Resp[RespLen-2];
SW2 = Resp[RespLen-1];
RespLen -= 2;
if ( SW1 == 0x6C ) { // wrong Le
pApdu->apdu[pApdu->len-1] = SW2;
goto __wrong_le;
}
if ( SW1== 0x61 ) {
TAPDU Rapdu;
Rapdu.len =5;
memcpy(Rapdu.apdu,"\x00\xc0\x00\x00",4);
Rapdu.apdu[0] = __iso_cla;
Rapdu.apdu[4] = SW2;
rc = sendapdu(&Rapdu);
//memcpy(&apdu,pApdu, sizeof(TAPDU)); // restore pre APDU
}
return 0;
}
//------------------------------------------------------------------
char TMWICRD::TermType() const
{
return 0x7;
}
//------------------------------------------------------------------
char *TMWICRD::GetError() const
{
static char szCurError[16];
unsigned char __ec__ = abs(rc);
switch ( __ec__ ) {
case 0X80: strcpy(szCurError,"128: 读错误"); break;
case 0X81: strcpy(szCurError,"129: 写错误"); break;
case 0X82: strcpy(szCurError,"130: 命令错误"); break;
case 0X83: strcpy(szCurError,"131: 密码错误"); break;
case 0X84: strcpy(szCurError,"132: 超时错误"); break;
case 0X85: strcpy(szCurError,"133: 测卡错误"); break;
case 0X86: strcpy(szCurError,"134: 无卡错误"); break;
case 0X87: strcpy(szCurError,"135: 超值错误"); break;
case 0X88: strcpy(szCurError,"136: 通讯错误"); break;
case 0X89: strcpy(szCurError,"137: 卡型错误"); break;
case 0X8A: strcpy(szCurError,"138: 校验和错误"); break;
case 0X8C: strcpy(szCurError,"140: 非法拔卡"); break;
case 0X8D: strcpy(szCurError,"141: 通用错误"); break;
case 0X8E: strcpy(szCurError,"142: 命令头错误"); break;
case 0X90: strcpy(szCurError,"144: 地址错误"); break;
case 0X91: strcpy(szCurError,"145: 长度错误"); break;
case 0X95: strcpy(szCurError,"149: 串口被占用"); break;
case 0XCA: strcpy(szCurError,"202: 校验和错误"); break;
case 0XCB: strcpy(szCurError,"203: 长度错误"); break;
case 0XCD: strcpy(szCurError,"205: 操作码错误"); break;
case 0XCE: strcpy(szCurError,"206: 校验位错误"); break;
case 0XCF: strcpy(szCurError,"207: 超时错误"); break;
}
return szCurError;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -