📄 contactlessdemovcdlg.cpp
字号:
{
m_ctrOutput.AddString("Writing has been done successfully");
ShowTimeRequired();
}
}
else
{
TurnOnErrorLed();
m_ctrOutput.AddString("The Card is not connected");
}
EXIT:
ShowCursorOutScreen();
UpdateData(FALSE);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CContactlessDemoVCDlg::OnIncrement()
{
UCHAR ucMifareIncrementValue[4] = {0};
ULONG ulMifareIncrementValueBufLen = 4;
m_strIncrementValue.Empty();
UpdateData(TRUE);
if (fCardConnected == TRUE)
{
if (m_strCardName == "MIFARE_ST1K" || m_strCardName == "MIFARE_ST4K" )
{
if (m_longBlockNr == 0)
{
TurnOnErrorLed();
m_ctrOutput.AddString("Block 0 can not be incremented");
goto EXIT;
}
if (m_longBlockNr<128)
{
if (ulAuthenticatedBlockNr/4 != ((ULONG)m_longBlockNr)/4)
{
TurnOnErrorLed();
m_ctrOutput.AddString("The selected block sector is not authenticated");
goto EXIT;
}
}
else
{
if (ulAuthenticatedBlockNr /16 != ((ULONG)m_longBlockNr)/16)
{
TurnOnErrorLed();
m_ctrOutput.AddString("The selected block sector is not authenticated");
goto EXIT;
}
}
if (m_longBlockNr < 128)
{
if (((m_longBlockNr + 1)%4)==0)
{
TurnOnErrorLed();
m_ctrOutput.AddString("Trailer block cannot be incremented");
goto EXIT;
}
}
else
{
if (m_longBlockNr> 128)
if (((m_longBlockNr + 1)%16)==0)
{
TurnOnErrorLed();
m_ctrOutput.AddString("Trailer block cannot be incremented");
}
}
CStringToUchar(m_strIncrementValue,ucMifareIncrementValue,&ulMifareIncrementValueBufLen);
if (ulMifareIncrementValueBufLen != 4)
{
TurnOnErrorLed();
m_ctrOutput.AddString("The length of the data by incremented must be 4");
goto EXIT;
}
GetSystemTime(&startTime);
lErr = SCardCLMifareStdIncrementVal(hCard,m_longBlockNr,ucMifareIncrementValue,ulMifareIncrementValueBufLen);
GetSystemTime(&stopTime);
if (lErr== NO_ERROR)
{
m_ctrOutput.AddString("Increment value has been done successfully");
ShowTimeRequired();
}
else
{
ShowErrorMessage(lErr);
m_ctrOutput.AddString("May be the block is not a value block");
}
}
else
{
TurnOnErrorLed();
m_ctrOutput.AddString("Except Mifare 1K or Mifare4K, other cards do not support Mifare std increment");
}
}
else
{
TurnOnErrorLed();
m_ctrOutput.AddString("The Card is not connected");
}
EXIT:
ShowCursorOutScreen();
UpdateData(FALSE);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CContactlessDemoVCDlg::OnDecrement()
{
UCHAR ucMifareDecrementValue[4] = {0};
ULONG ulMifareDecrementValueBufLen = 4;
m_strDecrementValue.Empty();
UpdateData(TRUE);
if (fCardConnected == TRUE)
{
if (m_strCardName == "MIFARE_ST1K" || m_strCardName == "MIFARE_ST4K" )
{
if (m_longBlockNr == 0)
{
TurnOnErrorLed();
m_ctrOutput.AddString("Block 0 can not be decremented");
goto EXIT;
}
if (m_longBlockNr<128)
{
if (ulAuthenticatedBlockNr/4 != ((ULONG)m_longBlockNr)/4)
{
TurnOnErrorLed();
m_ctrOutput.AddString("The selected block sector is not authenticated");
goto EXIT;
}
}
else
{
if (ulAuthenticatedBlockNr /16 != ((ULONG)m_longBlockNr)/16)
{
TurnOnErrorLed();
m_ctrOutput.AddString("The selected block sector is not authenticated");
goto EXIT;
}
}
if (m_longBlockNr < 128)
{
if (((m_longBlockNr + 1)%4)==0)
{
TurnOnErrorLed();
m_ctrOutput.AddString("Trailer block cannot be decremented");
goto EXIT;
}
}
else
{
if (m_longBlockNr> 128)
if (((m_longBlockNr + 1)%16)==0)
{
TurnOnErrorLed();
m_ctrOutput.AddString("Trailer block cannot be decremented");
}
}
CStringToUchar(m_strDecrementValue,ucMifareDecrementValue,&ulMifareDecrementValueBufLen);
if (ulMifareDecrementValueBufLen != 4)
{
TurnOnErrorLed();
m_ctrOutput.AddString("The length of the data by decremented must be 4");
goto EXIT;
}
GetSystemTime(&startTime);
lErr = SCardCLMifareStdDecrementVal(hCard,m_longBlockNr,ucMifareDecrementValue,ulMifareDecrementValueBufLen);
GetSystemTime(&stopTime);
if (lErr == NO_ERROR)
{
m_ctrOutput.AddString("Decrement value has been done successfully");
ShowTimeRequired();
}
else
{
ShowErrorMessage(lErr);
m_ctrOutput.AddString("May be the block is not a value block");
}
}
else
{
TurnOnErrorLed();
m_ctrOutput.AddString("Except Mifare 1K or Mifare4K, other cards do not support Mifare std decrement");
}
}
else
{
TurnOnErrorLed();
m_ctrOutput.AddString("The Card is not connected");
}
EXIT:
ShowCursorOutScreen();
UpdateData(FALSE);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CContactlessDemoVCDlg::TrackCard()
{
UpdateData(TRUE);
m_ctrAtrList.ResetContent();
m_ctrUidList.ResetContent();
m_ctrCardNameList.ResetContent();
THREADSTRUCT *_param = new THREADSTRUCT;
_param->_this = this;
AfxBeginThread (StartCardThread, _param);
UpdateData(FALSE);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UINT CContactlessDemoVCDlg::StartCardThread (LPVOID param)
{
THREADSTRUCT* ts = (THREADSTRUCT*)param;
char str[80] = "";
UCHAR PresentCardState = Card_Absent ;
UCHAR PreviousCardState = Card_Absent ;
UCHAR hATR[64] = {0};
//here is the time-consuming process which interacts with your dialog
while (ts->_this->fCM5121Selected == TRUE)
{
ts->_this->sReaderState.szReader = strdup(ts->_this->m_strReaderName);
ts->_this->sReaderState.dwCurrentState = SCARD_STATE_EMPTY;
ts->_this->sReaderState.dwEventState = SCARD_STATE_EMPTY;
//Look for the card state
SCardGetStatusChange(ts->_this->hContext,500,&(ts->_this->sReaderState),1);
if (ts->_this->sReaderState.dwEventState & SCARD_STATE_PRESENT)
{
PresentCardState = Card_Present ; //Card is present
//Here find if the ATR has been changed
memcpy(hATR,ts->_this->sReaderState.rgbAtr,ts->_this->sReaderState.cbAtr);
if (memcmp(hATR,ts->_this->ucATR,64) != 0)
PresentCardState = Card_Changed;
if (PreviousCardState == Card_Absent || PresentCardState == Card_Changed )
{
if (SCardConnect(ts->_this->hContext,ts->_this->m_strReaderName,
SCARD_SHARE_SHARED,SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1,&(ts->_this->hCard),&(ts->_this->dwActiveProtocols))
== SCARD_S_SUCCESS)
{
PresentCardState = Card_Present;
ts->_this->fCardConnected = TRUE;
memset(ts->_this->ucATR,0,64);
ts->_this->ulATRLen = ts->_this->sReaderState.cbAtr;
memcpy(ts->_this->ucATR,hATR,ts->_this->ulATRLen);
ts->_this->UcharToStr(ts->_this->ucATR,ts->_this->ulATRLen,str);
ts->_this->m_strATR = str;
if (ts->_this->GiveCardName(ts->_this->ucATR,ts->_this->ulATRLen,str)==TRUE)
{
memset(ts->_this->ucUID,12,0);
ts->_this->ulnByteUID = 12;
if (SCardCLGetUID(ts->_this->hCard,ts->_this->ucUID,ts->_this->ulUIDBufLen, &ts->_this->ulnByteUID)==
NO_ERROR)
{
ts->_this->UcharToStr(ts->_this->ucUID,ts->_this->ulnByteUID,str);
ts->_this->m_strUID = str;
}
}
else
ts->_this->m_strUID = "";
}
else
{
ts->_this->m_strUID = "";
ts->_this->m_strATR = "";
ts->_this->m_strCardName ="";
}
}
ts->_this->m_ctrAtrList.ResetContent();
ts->_this->m_ctrUidList.ResetContent();
ts->_this->m_ctrCardNameList.ResetContent();
ts->_this->m_ctrAtrList.AddString(ts->_this->m_strATR);
ts->_this->m_ctrUidList.AddString(ts->_this->m_strUID );
ts->_this->m_ctrCardNameList.AddString(ts->_this->m_strCardName);
}
else
{
PresentCardState = Card_Absent; //Card is not present
ts->_this->m_ctrAtrList.ResetContent();
ts->_this->m_ctrUidList.ResetContent();
ts->_this->m_ctrCardNameList.ResetContent();
ts->_this->DisableAllFields();
ts->_this->ulAuthenticatedBlockNr = 257;
}
if (PreviousCardState != PresentCardState)
{
if (PresentCardState == Card_Present)
{
ts->_this->m_ctrOutput.AddString("Card is present");
}
else if (PresentCardState == Card_Absent)
{
ts->_this->m_ctrOutput.AddString("Card is not present");
ts->_this->hCard = 0;
}
}
ts->_this->ShowCursorOutScreen();
PreviousCardState = PresentCardState;
Sleep(500);
} //while loop
AfxEndThread(0);
return 1;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CContactlessDemoVCDlg::ShowErrorMessage(long eError)
{
TurnOnErrorLed();
switch (eError)
{
case SCARD_E_CANCELLED:
m_ctrOutput.AddString( "SCARD_E_CANCELLED:\r\n The action was canceled by an SCardCancel request.");
break;
case SCARD_E_CANT_DISPOSE:
m_ctrOutput.AddString( "SCARD_E_CANT_DISPOSE:\r\n The system could not dispose of the media in the requested manner.");
break;
case SCARD_E_CARD_UNSUPPORTED:
m_ctrOutput.AddString( "SCARD_E_CARD_UNSUPPORTED:\r\n The smart card does not meet minimal requirements for support.");
break;
case SCARD_E_DUPLICATE_READER:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -