00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #if !defined(_FEDM_NO_TAG_HANDLER_ISO14443)
00035
00036 #include "FedmIscTagHandler_ISO14443_4.h"
00037 #include "../FEDM_ISCReader.h"
00038 #include "../FEDM_ISOTabItem.h"
00039 #include "../FEDM_ISCReaderID.h"
00040 #include "../FEDM_ISC.h"
00041
00042
00043 #include "fetcl.h"
00044
00045
00046
00047 #if _MSC_VER >= 1400
00048 #pragma warning(disable : 4996)
00049 #endif
00050
00051
00052
00053
00054
00055
00056
00057
00058 FedmIscTagHandler_ISO14443_4::FedmIscTagHandler_ISO14443_4(
00059 FEDM_ISCReader* pReader,
00060 FEDM_ISOTabItem* pTabItem )
00061 : FedmIscTagHandler_ISO14443_3(pReader, FedmIscTagHandler::TYPE_ISO14443_4, pTabItem)
00062 {
00063 m_iFetclHnd = 0;
00064
00065 m_cbFct = NULL;
00066 m_pAny = NULL;
00067
00068 if(m_pTabItem != NULL)
00069 {
00070 if(m_pTabItem->m_ucTrType == FEDM_ISC_TR_TYPE_ISO14443A)
00071 m_sTagName = "ISO 14443-4 Type A";
00072 else if(m_pTabItem->m_ucTrType == FEDM_ISC_TR_TYPE_ISO14443B)
00073 m_sTagName = "ISO 14443-4 Type B";
00074
00075 if(m_pReader != NULL)
00076 {
00077
00078 m_iFetclHnd = FETCL_NewTransponder( m_pReader->GetReaderHnd(),
00079 m_pReader->GetBusAddress(),
00080 0,
00081 0,
00082 false,
00083 false );
00084 }
00085 }
00086
00087 Init();
00088 }
00089
00090 FedmIscTagHandler_ISO14443_4::FedmIscTagHandler_ISO14443_4(
00091 FEDM_ISCReader* pReader,
00092 unsigned int uiTagHandlerType,
00093 FEDM_ISOTabItem* pTabItem )
00094 : FedmIscTagHandler_ISO14443_3(pReader, uiTagHandlerType, pTabItem)
00095 {
00096 m_iFetclHnd = 0;
00097
00098 m_cbFct = NULL;
00099 m_pAny = NULL;
00100
00101 if(m_pTabItem != NULL)
00102 {
00103 if(m_pTabItem->m_ucTrType == FEDM_ISC_TR_TYPE_ISO14443A)
00104 m_sTagName = "ISO 14443-4 Type A";
00105 else if(m_pTabItem->m_ucTrType == FEDM_ISC_TR_TYPE_ISO14443B)
00106 m_sTagName = "ISO 14443-4 Type B";
00107
00108 if(m_pReader != NULL)
00109 {
00110
00111 m_iFetclHnd = FETCL_NewTransponder( m_pReader->GetReaderHnd(),
00112 m_pReader->GetBusAddress(),
00113 0,
00114 0,
00115 false,
00116 false );
00117 }
00118 }
00119
00120 Init();
00121 }
00122
00123 FedmIscTagHandler_ISO14443_4::~FedmIscTagHandler_ISO14443_4()
00124 {
00125 if(m_iFetclHnd > 0)
00126 FETCL_DeleteTransponder(m_iFetclHnd);
00127 }
00128
00129
00130 void FedmIscTagHandler_ISO14443_4::Init()
00131 {
00132 FedmIscTagHandler::Init();
00133
00134 m_ucCID = 0;
00135 m_ucNAD = 0;
00136 m_bUseCID = false;
00137 m_bUseNAD = false;
00138
00139 m_uiResponseLen = 0;
00140
00141 m_ucISO14443ErrorCode = 0;
00142 }
00143
00144 unsigned char FedmIscTagHandler_ISO14443_4::GetLastISOErrorCode()
00145 {
00146 return m_ucISO14443ErrorCode;
00147 }
00148
00149 void FedmIscTagHandler_ISO14443_4::SetCID(unsigned char ucCID)
00150 {
00151 m_ucCID = ucCID;
00152 }
00153
00154 int FedmIscTagHandler_ISO14443_4::EnableCID(bool bEnable)
00155 {
00156 m_bUseCID = bEnable;
00157
00158 if(m_iFetclHnd > 0)
00159 FETCL_DeleteTransponder(m_iFetclHnd);
00160
00161
00162 m_iFetclHnd = FETCL_NewTransponder( m_pReader->GetReaderHnd(),
00163 m_pReader->GetBusAddress(),
00164 m_ucCID,
00165 m_ucNAD,
00166 m_bUseCID,
00167 m_bUseNAD );
00168
00169 if(m_iFetclHnd < 0)
00170 return m_iFetclHnd;
00171
00172 return 0;
00173 }
00174
00175 unsigned char FedmIscTagHandler_ISO14443_4::GetCID()
00176 {
00177 return m_ucCID;
00178 }
00179
00180 bool FedmIscTagHandler_ISO14443_4::IsCIDEnabled()
00181 {
00182 return m_bUseCID;
00183 }
00184
00185
00186 void FedmIscTagHandler_ISO14443_4::SetNAD(unsigned char ucNAD)
00187 {
00188 m_ucNAD = ucNAD;
00189 }
00190
00191 int FedmIscTagHandler_ISO14443_4::EnableNAD(bool bEnable)
00192 {
00193 m_bUseNAD = bEnable;
00194
00195 if(m_iFetclHnd > 0)
00196 FETCL_DeleteTransponder(m_iFetclHnd);
00197
00198
00199 m_iFetclHnd = FETCL_NewTransponder( m_pReader->GetReaderHnd(),
00200 m_pReader->GetBusAddress(),
00201 m_ucCID,
00202 m_ucNAD,
00203 m_bUseCID,
00204 m_bUseNAD );
00205
00206 if(m_iFetclHnd < 0)
00207 return m_iFetclHnd;
00208
00209 return 0;
00210 }
00211
00212 unsigned char FedmIscTagHandler_ISO14443_4::GetNAD()
00213 {
00214 return m_ucNAD;
00215 }
00216
00217 bool FedmIscTagHandler_ISO14443_4::IsNADEnabled()
00218 {
00219 return m_bUseNAD;
00220 }
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 int FedmIscTagHandler_ISO14443_4::GetTransponderInfo(
00245 unsigned char& ucFSCI,
00246 unsigned char& ucFWI,
00247 unsigned char& ucDSI,
00248 unsigned char& ucDRI,
00249 unsigned char& ucNADSupport,
00250 unsigned char& ucCIDSupport )
00251 {
00252 FEDM_TRACE(m_pReader->GetLogger(), FELOGMNG_DBG_FUNCTION_CALL | FELOGMNG_DBG_LF_COMM_TAG_HANDLER, __FUNCTION__);
00253
00254 int iErr = 0;
00255
00256 FEDM_CHK1(iErr, m_pReader->SetData(FEDM_ISC_TMP_B2_CMD, (unsigned char)0x2B));
00257 FEDM_CHK1(iErr, m_pReader->SetData(FEDM_ISC_TMP_B2_MODE, (unsigned char)FEDM_ISC_ISO_MODE_SEL));
00258
00259 m_ucISO14443ErrorCode = 0;
00260
00261
00262 int iBack = m_pReader->SendProtocol(0xB2);
00263 if(iBack == 0)
00264 {
00265 int iIdx = m_pReader->FindTableIndex(0, FEDM_ISC_ISO_TABLE, FEDM_ISC_DATA_IS_SELECTED, true);
00266 if(iIdx < 0)
00267 {
00268
00269 if( m_pReader->m_uiISOTableLength == 1 && (m_pReader->m_ISOTable[0]->m_ucTrType == FEDM_ISC_TR_TYPE_ISO14443A || m_pReader->m_ISOTable[0]->m_ucTrType == FEDM_ISC_TR_TYPE_ISO14443B) )
00270 iIdx = 0;
00271 else
00272 return FEDM_ERROR_NO_TABLE_DATA;
00273 }
00274
00275 FEDM_CHK1(iErr, m_pReader->GetTableData(iIdx, FEDM_ISC_ISO_TABLE, FEDM_ISC_DATA_FSCI, &ucFSCI));
00276 FEDM_CHK1(iErr, m_pReader->GetTableData(iIdx, FEDM_ISC_ISO_TABLE, FEDM_ISC_DATA_FWI, &ucFWI));
00277 FEDM_CHK1(iErr, m_pReader->GetTableData(iIdx, FEDM_ISC_ISO_TABLE, FEDM_ISC_DATA_DSI, &ucDSI));
00278 FEDM_CHK1(iErr, m_pReader->GetTableData(iIdx, FEDM_ISC_ISO_TABLE, FEDM_ISC_DATA_DRI, &ucDRI));
00279 FEDM_CHK1(iErr, m_pReader->GetTableData(iIdx, FEDM_ISC_ISO_TABLE, FEDM_ISC_DATA_NAD, &ucNADSupport));
00280 FEDM_CHK1(iErr, m_pReader->GetTableData(iIdx, FEDM_ISC_ISO_TABLE, FEDM_ISC_DATA_CID, &ucCIDSupport));
00281 }
00282 else if(iBack == 0x96)
00283 {
00284
00285 FEDM_CHK1(iErr, m_pReader->GetData(FEDM_ISC_TMP_B2_ISO_ERROR, &m_ucISO14443ErrorCode));
00286 }
00287
00288 return iBack;
00289 }
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311 int FedmIscTagHandler_ISO14443_4::Apdu(
00312 unsigned char* pucRequestData,
00313 unsigned int uiRequestLen,
00314 unsigned char* pucResponseData,
00315 unsigned int uiResponseBufferLen,
00316 unsigned int& uiResponseLen )
00317 {
00318 FEDM_TRACE(m_pReader->GetLogger(), FELOGMNG_DBG_FUNCTION_CALL | FELOGMNG_DBG_LF_COMM_TAG_HANDLER, __FUNCTION__ << " sync call");
00319
00320 FEDM_CHK3(pucRequestData);
00321 FEDM_CHK3(pucResponseData);
00322
00323 if(m_iFetclHnd < 0)
00324 return m_iFetclHnd;
00325
00326 int iBack = 0;
00327 int iErr = 0;
00328
00329 m_ucISO14443ErrorCode = 0;
00330
00331
00332 iBack = FETCL_Apdu(m_iFetclHnd, pucRequestData, uiRequestLen, NULL);
00333 m_pReader->SetLastError(iBack);
00334 if(iBack != 0 && iBack != 0x96)
00335 return iBack;
00336
00337 int iResponseLen = FETCL_GetResponseData(m_iFetclHnd, pucResponseData, uiResponseBufferLen);
00338 if(iResponseLen < 0)
00339 {
00340 m_pReader->SetLastError(iResponseLen);
00341 return iResponseLen;
00342 }
00343 else if(iResponseLen == 0)
00344 {
00345 m_pReader->SetLastError(FEDM_ERROR_NO_DATA);
00346 return FEDM_ERROR_NO_DATA;
00347 }
00348
00349 uiResponseLen = (unsigned int)iResponseLen;
00350
00351 if(iBack == 0x96)
00352 {
00353
00354 m_ucISO14443ErrorCode = pucResponseData[0];
00355 FEDM_CHK1(iErr, m_pReader->SetData(FEDM_ISC_TMP_B2_ISO_ERROR, m_ucISO14443ErrorCode));
00356 }
00357
00358 return iBack;
00359 }
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 int FedmIscTagHandler_ISO14443_4::Apdu(
00381 unsigned char* pucRequestData,
00382 unsigned int uiRequestLen,
00383 void* pAny,
00384 void (*cbFct)(void*, int, int, int) )
00385 {
00386 FEDM_TRACE(m_pReader->GetLogger(), FELOGMNG_DBG_FUNCTION_CALL | FELOGMNG_DBG_LF_COMM_TAG_HANDLER, __FUNCTION__ << " async call");
00387
00388 FEDM_CHK3(pucRequestData);
00389
00390 if(m_iFetclHnd < 0)
00391 return m_iFetclHnd;
00392
00393 int iBack = 0;
00394 int iErr = 0;
00395 int iResponseLen = 0;
00396 unsigned char ucResponseData = 0;
00397 FETCL_EVENT_INIT Init;
00398
00399
00400 m_cbFct = cbFct;
00401 m_pAny = pAny;
00402
00403 Init.pAny = this;
00404 Init.uiFlag = FETCL_CALLBACK2;
00405 #if defined(__BORLANDC__)
00406 Init.cbFct2 = (void(*)(void*,int,int,int))ApduCallback;
00407 #else
00408 Init.cbFct2 = ApduCallback;
00409 #endif
00410
00411 m_ucISO14443ErrorCode = 0;
00412
00413 iBack = FETCL_Apdu(m_iFetclHnd, pucRequestData, uiRequestLen, &Init);
00414 m_pReader->SetLastError(iBack);
00415 if(iBack == 0x96)
00416 {
00417 iResponseLen = FETCL_GetResponseData(m_iFetclHnd, &ucResponseData, 1);
00418 if(iResponseLen < 0)
00419 {
00420 m_pReader->SetLastError(iResponseLen);
00421 return iResponseLen;
00422 }
00423 else if(iResponseLen == 0)
00424 {
00425 m_pReader->SetLastError(FEDM_ERROR_NO_DATA);
00426 return FEDM_ERROR_NO_DATA;
00427 }
00428
00429
00430 m_ucISO14443ErrorCode = ucResponseData;
00431 FEDM_CHK1(iErr, m_pReader->SetData(FEDM_ISC_TMP_B2_ISO_ERROR, m_ucISO14443ErrorCode));
00432 }
00433
00434 return iBack;
00435 }
00436
00437
00438 int FedmIscTagHandler_ISO14443_4::Ping()
00439 {
00440 FEDM_TRACE(m_pReader->GetLogger(), FELOGMNG_DBG_FUNCTION_CALL | FELOGMNG_DBG_LF_COMM_TAG_HANDLER, __FUNCTION__);
00441
00442 if(m_iFetclHnd < 0)
00443 return m_iFetclHnd;
00444
00445 int iBack = 0;
00446 int iErr = 0;
00447 int iResponseLen = 0;
00448 unsigned char ucResponseData = 0;
00449
00450 m_ucISO14443ErrorCode = 0;
00451
00452 iBack = FETCL_Ping(m_iFetclHnd);
00453 m_pReader->SetLastError(iBack);
00454 if(iBack == 0x96)
00455 {
00456 iResponseLen = FETCL_GetResponseData(m_iFetclHnd, &ucResponseData, 1);
00457 if(iResponseLen < 0)
00458 {
00459 m_pReader->SetLastError(iResponseLen);
00460 return iResponseLen;
00461 }
00462 else if(iResponseLen == 0)
00463 {
00464 m_pReader->SetLastError(FEDM_ERROR_NO_DATA);
00465 return FEDM_ERROR_NO_DATA;
00466 }
00467
00468
00469 m_ucISO14443ErrorCode = ucResponseData;
00470 FEDM_CHK1(iErr, m_pReader->SetData(FEDM_ISC_TMP_B2_ISO_ERROR, m_ucISO14443ErrorCode));
00471 }
00472
00473 return iBack;
00474 }
00475
00476
00477 int FedmIscTagHandler_ISO14443_4::Deselect()
00478 {
00479 FEDM_TRACE(m_pReader->GetLogger(), FELOGMNG_DBG_FUNCTION_CALL | FELOGMNG_DBG_LF_COMM_TAG_HANDLER, __FUNCTION__);
00480
00481 if(m_iFetclHnd < 0)
00482 return m_iFetclHnd;
00483
00484 int iBack = 0;
00485 int iErr = 0;
00486 int iResponseLen = 0;
00487 unsigned char ucResponseData = 0;
00488
00489 m_ucISO14443ErrorCode = 0;
00490
00491 iBack = FETCL_Deselect(m_iFetclHnd);
00492 m_pReader->SetLastError(iBack);
00493 if(iBack == 0x96)
00494 {
00495 iResponseLen = FETCL_GetResponseData(m_iFetclHnd, &ucResponseData, 1);
00496 if(iResponseLen < 0)
00497 {
00498 m_pReader->SetLastError(iResponseLen);
00499 return iResponseLen;
00500 }
00501 else if(iResponseLen == 0)
00502 {
00503 m_pReader->SetLastError(FEDM_ERROR_NO_DATA);
00504 return FEDM_ERROR_NO_DATA;
00505 }
00506
00507
00508 m_ucISO14443ErrorCode = ucResponseData;
00509 FEDM_CHK1(iErr, m_pReader->SetData(FEDM_ISC_TMP_B2_ISO_ERROR, m_ucISO14443ErrorCode));
00510 }
00511
00512 return iBack;
00513 }
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530 int FedmIscTagHandler_ISO14443_4::GetResponseData(
00531 unsigned char* pucResponseData,
00532 unsigned int uiResponseBufferLen,
00533 unsigned int& uiResponseLen )
00534 {
00535 FEDM_TRACE(m_pReader->GetLogger(), FELOGMNG_DBG_FUNCTION_CALL | FELOGMNG_DBG_LF_COMM_TAG_HANDLER, __FUNCTION__);
00536
00537 FEDM_CHK3(pucResponseData);
00538
00539 if(uiResponseBufferLen < m_uiResponseLen)
00540 {
00541 m_pReader->SetLastError(FEDM_ERROR_BUFFER_LENGTH);
00542 return FEDM_ERROR_BUFFER_LENGTH;
00543 }
00544
00545 memcpy(pucResponseData, m_ucResponseData, m_uiResponseLen);
00546 uiResponseLen = m_uiResponseLen;
00547 return 0;
00548 }
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565 void FedmIscTagHandler_ISO14443_4::ApduCallback(void* pAny, int iApduHnd, int iError, int iDataLength)
00566 {
00567 if(pAny == NULL)
00568 return;
00569
00570 FEDM_TRACE(((FedmIscTagHandler_ISO14443_4*)pAny)->m_pReader->GetLogger(), FELOGMNG_DBG_FUNCTION_CALL | FELOGMNG_DBG_LF_COMM_TAG_HANDLER, __FUNCTION__);
00571
00572 if(iApduHnd != ((FedmIscTagHandler_ISO14443_4*)pAny)->m_iFetclHnd)
00573 return;
00574
00575 int iResponseLen = 0;
00576
00577 ((FedmIscTagHandler_ISO14443_4*)pAny)->m_pReader->SetLastError(iError);
00578
00579 if(iError < 0)
00580 {
00581 ((FedmIscTagHandler_ISO14443_4*)pAny)->m_cbFct(((FedmIscTagHandler_ISO14443_4*)pAny)->m_pAny, iApduHnd, iError, iDataLength);
00582 }
00583 else if(iError == 0x96)
00584 {
00585 iResponseLen = FETCL_GetResponseData(iApduHnd, ((FedmIscTagHandler_ISO14443_4*)pAny)->m_ucResponseData, FEDM_ISO14443_4_APDU_BUFFER_SIZE);
00586 if(iResponseLen < 0)
00587 {
00588 ((FedmIscTagHandler_ISO14443_4*)pAny)->m_pReader->SetLastError(iResponseLen);
00589 ((FedmIscTagHandler_ISO14443_4*)pAny)->m_cbFct(((FedmIscTagHandler_ISO14443_4*)pAny)->m_pAny, iApduHnd, iResponseLen, 0);
00590 }
00591 else if(iResponseLen == 0)
00592 {
00593 ((FedmIscTagHandler_ISO14443_4*)pAny)->m_pReader->SetLastError(FEDM_ERROR_NO_DATA);
00594 ((FedmIscTagHandler_ISO14443_4*)pAny)->m_cbFct(((FedmIscTagHandler_ISO14443_4*)pAny)->m_pAny, iApduHnd, FEDM_ERROR_NO_DATA, 0);
00595 }
00596 else
00597 {
00598
00599 ((FedmIscTagHandler_ISO14443_4*)pAny)->m_ucISO14443ErrorCode = ((FedmIscTagHandler_ISO14443_4*)pAny)->m_ucResponseData[0];
00600 ((FedmIscTagHandler_ISO14443_4*)pAny)->m_pReader->SetData(FEDM_ISC_TMP_B2_ISO_ERROR, ((FedmIscTagHandler_ISO14443_4*)pAny)->m_ucISO14443ErrorCode);
00601 ((FedmIscTagHandler_ISO14443_4*)pAny)->m_cbFct(((FedmIscTagHandler_ISO14443_4*)pAny)->m_pAny, iApduHnd, iError, iDataLength);
00602 }
00603 }
00604 else if(iError == 0)
00605 {
00606 iResponseLen = FETCL_GetResponseData(iApduHnd, ((FedmIscTagHandler_ISO14443_4*)pAny)->m_ucResponseData, FEDM_ISO14443_4_APDU_BUFFER_SIZE);
00607 if(iResponseLen < 0)
00608 {
00609 ((FedmIscTagHandler_ISO14443_4*)pAny)->m_cbFct(((FedmIscTagHandler_ISO14443_4*)pAny)->m_pAny, iApduHnd, iResponseLen, 0);
00610 }
00611 else if(iResponseLen == 0)
00612 {
00613 ((FedmIscTagHandler_ISO14443_4*)pAny)->m_cbFct(((FedmIscTagHandler_ISO14443_4*)pAny)->m_pAny, iApduHnd, FEDM_ERROR_NO_DATA, 0);
00614 }
00615 else
00616 {
00617 ((FedmIscTagHandler_ISO14443_4*)pAny)->m_cbFct(((FedmIscTagHandler_ISO14443_4*)pAny)->m_pAny, iApduHnd, iError, iDataLength);
00618 }
00619 }
00620 else
00621 {
00622 ((FedmIscTagHandler_ISO14443_4*)pAny)->m_cbFct(((FedmIscTagHandler_ISO14443_4*)pAny)->m_pAny, iApduHnd, iError, iDataLength);
00623 }
00624 }
00625
00626 #endif // #if !defined(_FEDM_NO_TAG_HANDLER_ISO14443)