FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1.cpp
Go to the documentation of this file.
00001 /*-------------------------------------------------------
00002 |                                                       |
00003 | FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1.cpp  |
00004 |                                                       |
00005 ---------------------------------------------------------
00006 
00007 Copyright © 2009-2010   FEIG ELECTRONIC GmbH, All Rights Reserved.
00008                                                 Lange Strasse 4
00009                                                 D-35781 Weilburg
00010                                                 Federal Republic of Germany
00011                                                 phone    : +49 6471 31090
00012                                                 fax      : +49 6471 310999
00013                                                 e-mail   : obid-support@feig.de
00014                                                 Internet : http://www.feig.de
00015                                         
00016 Author                  :       Markus Hultsch
00017 Begin                   :       30.03.2009
00018 
00019 Version                 :       03.02.05 / 07.09.2010 / M. Hultsch
00020 
00021 Operation Systems       :       independent
00022 
00023 Function                        :       implementation class for OBID® classic-pro transponder ISO14443-4 MIFARE DESFire
00024 
00025 NOTE                            :       this class supports only selected mode in Host-Mode
00026 
00027 
00028 Trademarks:
00029 -----------
00030 OBID®, OBID i-scan® and OBID myAXXESS® are registered Trademarks of FEIG ELECTRONIC GmbH
00031 Other Trademarks: see FEDM.h
00032 */
00033 
00034 
00035 /*############### Questions ##################################################
00036 - CID und NAD für T=CL?
00037 
00038 */
00039 
00040 #if !defined(_FEDM_NO_TAG_HANDLER_ISO14443)
00041 
00042 #include "FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1.h"
00043 #include "FedmIscTagHandler_ISO14443_4_MIFARE_DESFire.h"
00044 #include "FedmIscTagHandler_ISO14443_4.h"
00045 #include "../FEDM_ISCReader.h"
00046 #include "../FEDM_ISOTabItem.h"
00047 #include "../FEDM_ISCReaderID.h"
00048 #include "../FEDM_ISC.h"
00049 
00050 
00051 #if _MSC_VER >= 1400
00052         #pragma warning(disable : 4996)
00053 #endif
00054 
00055 
00056 
00057 //####################################################################
00058 // class FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1
00059 // >> supports only ISO Host Commands <<
00060 //####################################################################
00061 
00062 FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1(
00063         FEDM_ISOTabItem* pTabItem, 
00064         FedmIscTagHandler_ISO14443_4_MIFARE_DESFire* pParent )
00065 {
00066         m_pTabItem                      = pTabItem;
00067         m_pParent                       = pParent;
00068 
00069         Init();
00070 }
00071 
00072 FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::~FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1()
00073 {
00074 }
00075 
00076 // set all data members to 0
00077 void FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::Init()
00078 {
00079         m_ucErrorSource = 0;
00080         m_uiErrorCode   = 0;
00081 }
00082 
00083 
00084         //####################################################################
00085         // 5 security related commands
00086         
00087         // [0xFA] : [0xC1] DESFire Commands (Authenticate, AuthenticateISO, AuthenticateAES)
00088 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::Authenticate(
00089         unsigned int uiApplicationID,
00090         unsigned char ucReaderKeyIndex,
00091         unsigned char ucDESFireKeyNo )
00092 {
00093         int iErr = 0;
00094 
00095         // swap MSB with LSB
00096         unsigned int uiTmp = 0;
00097         uiTmp |= ((uiApplicationID & 0x000000FF) << 16);
00098         uiTmp |= ((uiApplicationID & 0x0000FF00) );
00099         uiTmp |= ((uiApplicationID & 0x00FF0000) >> 16);
00100 
00101 
00102         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_CMD, (unsigned char)0xFA));
00103         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_MODE, (unsigned char)0x00));
00104         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_APP_ID, uiTmp));
00105         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_INDEX, ucReaderKeyIndex));
00106         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_NO, ucDESFireKeyNo));
00107 
00108         int iBack = m_pParent->m_pReader->SendProtocol(0xC1);
00109         if( iBack==0x00 )
00110         {
00111                 m_ucErrorSource = 0;
00112                 m_uiErrorCode = 0;
00113         }
00114         else if( iBack==0x97 )
00115         {
00116                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_SOURCE, &m_ucErrorSource));
00117                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_CODE, &m_uiErrorCode));
00118         }
00119 
00120         return iBack;
00121 }
00122 
00123         // [0x54] : [0xC1] DESFire Commands
00124 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::ChangeKeySettings(
00125         unsigned int uiApplicationID,
00126         unsigned char ucReaderKeyIndex,
00127         unsigned char ucNewKeySettings )
00128 {
00129         int iErr = 0;
00130 
00131         // swap MSB with LSB
00132         unsigned int uiTmp = 0;
00133         uiTmp |= ((uiApplicationID & 0x000000FF) << 16);
00134         uiTmp |= ((uiApplicationID & 0x0000FF00) );
00135         uiTmp |= ((uiApplicationID & 0x00FF0000) >> 16);
00136 
00137         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_CMD, (unsigned char)0x54));
00138         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_MODE, (unsigned char)0x00));
00139         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_APP_ID, uiTmp));
00140         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_INDEX, ucReaderKeyIndex));
00141         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_NEW_KEY_SETTINGS, ucNewKeySettings));
00142 
00143         int iBack = m_pParent->m_pReader->SendProtocol(0xC1);
00144         if( iBack==0x00 )
00145         {
00146                 m_ucErrorSource = 0;
00147                 m_uiErrorCode = 0;
00148         }
00149         else if( iBack==0x97 )
00150         {
00151                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_SOURCE, &m_ucErrorSource));
00152                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_CODE, &m_uiErrorCode));
00153         }
00154 
00155         return iBack;
00156 }
00157 
00158         // [0x45] : T=CL
00159 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::GetKeySettings(
00160         unsigned char* pucResponseData,
00161         unsigned int uiResponseBufferLength,
00162         unsigned int& uiResponseLength )
00163 {
00164         FEDM_CHK3(pucResponseData);
00165 
00166         unsigned int uiCnt = 0;
00167 
00168         m_pParent->m_ucRequestData[uiCnt++] = 0x45;
00169 
00170         return m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
00171 }
00172 
00173         // [0xC4] : [0xC1] DESFire Commands
00174 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::ChangeKey(
00175         unsigned int uiApplicationID,
00176         unsigned char ucReaderKeyIndex,
00177         unsigned char ucDESFireKeyNo,
00178         unsigned char ucKeyNoToBeChanged,
00179         unsigned char ucAESNewKeyVersion,
00180         string sOldKey,
00181         string sNewKey )
00182 {
00183         int iErr = 0;
00184         size_t iPadd = 0;
00185 
00186         if(sOldKey.length() != sNewKey.length())
00187                 return FEDM_ERROR_STRING_LENGTH;
00188 
00189         if(sOldKey.length() != 16 && sOldKey.length() != 32 && sOldKey.length() != 48)
00190                 return FEDM_ERROR_STRING_LENGTH;
00191 
00192         if(sNewKey.length() != 16 && sNewKey.length() != 32 && sNewKey.length() != 48)
00193                 return FEDM_ERROR_STRING_LENGTH;
00194 
00195         // swap MSB with LSB
00196         unsigned int uiTmp = 0;
00197         uiTmp |= ((uiApplicationID & 0x000000FF) << 16);
00198         uiTmp |= ((uiApplicationID & 0x0000FF00) );
00199         uiTmp |= ((uiApplicationID & 0x00FF0000) >> 16);
00200 
00201         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_CMD, (unsigned char)0xC4));
00202         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_MODE, (unsigned char)0x00));
00203         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_APP_ID, uiTmp));
00204         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_INDEX, ucReaderKeyIndex));
00205         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_NO, ucDESFireKeyNo));
00206         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_NO_TB_CHANGED, ucKeyNoToBeChanged));
00207         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_LEN, (unsigned char)(sNewKey.length()>>1))); // :2
00208         //Padding with '00' to 24 Byte KeyLength
00209         for(iPadd = sOldKey.length(); iPadd < 48; iPadd++)
00210         {
00211                 sOldKey += "0";
00212         }
00213         for(iPadd = sNewKey.length(); iPadd < 48; iPadd++)
00214         {
00215                 sNewKey += "0";
00216         }
00217         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_OLD_KEY, sOldKey));
00218         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_NEW_KEY, sNewKey));
00219         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_NEW_KEY_VERSION, ucAESNewKeyVersion));
00220 
00221         int iBack = m_pParent->m_pReader->SendProtocol(0xC1);
00222         if( iBack==0x00 )
00223         {
00224                 m_ucErrorSource = 0;
00225                 m_uiErrorCode = 0;
00226         }
00227         else if( iBack==0x97 )
00228         {
00229                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_SOURCE, &m_ucErrorSource));
00230                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_CODE, &m_uiErrorCode));
00231         }
00232 
00233         return iBack;
00234 }
00235 
00236         // [0x64] : T=CL
00237 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::GetKeyVersion(
00238         unsigned char ucDESFireKeyNo,
00239         unsigned char* pucResponseData,
00240         unsigned int uiResponseBufferLength,
00241         unsigned int& uiResponseLength )
00242 {
00243         FEDM_CHK3(pucResponseData);
00244 
00245         unsigned int uiCnt = 0;
00246 
00247         m_pParent->m_ucRequestData[uiCnt++] = 0x64;
00248         m_pParent->m_ucRequestData[uiCnt++] = ucDESFireKeyNo;
00249 
00250         return m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
00251 }
00252 
00253 
00254         //####################################################################
00255         // 10 PICC level commands
00256 
00257         // [0xCA] : T=CL
00258 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::CreateApplication(
00259         unsigned int uiApplicationID,
00260         unsigned char ucKeySetting1,
00261         unsigned char ucKeySetting2,
00262         unsigned int uiISOFileID,
00263         string sDFName,
00264         unsigned char* pucResponseData,
00265         unsigned int uiResponseBufferLength,
00266         unsigned int& uiResponseLength )
00267 {
00268         FEDM_CHK3(pucResponseData);
00269 
00270         unsigned int uiCnt = 0;
00271 
00272         m_pParent->m_ucRequestData[uiCnt++] = 0xCA;
00273         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char) (uiApplicationID & 0x000000FF);
00274         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiApplicationID & 0x0000FF00) >>  8);
00275         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiApplicationID & 0x00FF0000) >> 16);
00276         m_pParent->m_ucRequestData[uiCnt++] = ucKeySetting1;
00277         m_pParent->m_ucRequestData[uiCnt++] = ucKeySetting2;
00278         if(ucKeySetting2 & 0x40)
00279         {
00280                 m_pParent->m_ucRequestData[uiCnt++] = (unsigned char) (uiISOFileID & 0x000000FF);
00281                 m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiISOFileID & 0x0000FF00) >> 8);
00282         }
00283         strncpy((char*)&m_pParent->m_ucRequestData[uiCnt], sDFName.c_str(), sDFName.length());
00284         uiCnt += (unsigned int)sDFName.length();
00285 
00286         return m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
00287 }
00288 
00289         // [0xDA] : T=CL
00290 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::DeleteApplication(
00291         unsigned int uiApplicationID,
00292         unsigned char* pucResponseData,
00293         unsigned int uiResponseBufferLength,
00294         unsigned int& uiResponseLength )
00295 {
00296         FEDM_CHK3(pucResponseData);
00297 
00298         unsigned int uiCnt = 0;
00299 
00300         m_pParent->m_ucRequestData[uiCnt++] = 0xDA;
00301         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char) (uiApplicationID & 0x000000FF);
00302         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiApplicationID & 0x0000FF00) >>  8);
00303         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiApplicationID & 0x00FF0000) >> 16);
00304 
00305         return m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
00306 }
00307 
00308         // [0x6A] : T=CL
00309 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::GetApplicationIDs(
00310         unsigned char* pucResponseData,
00311         unsigned int uiResponseBufferLength,
00312         unsigned int& uiResponseLength )
00313 {
00314         FEDM_CHK3(pucResponseData);
00315 
00316         int iBack = 0;
00317         unsigned int uiRspLen = 0;
00318         unsigned int uiCnt = 0;
00319 
00320         uiResponseLength = 1; // increment for status byte
00321         
00322         m_pParent->m_ucRequestData[uiCnt++] = 0x6A;
00323 
00324         iBack = m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, m_pParent->m_ucResponseData, FEDM_ISO14443_4_APDU_BUFFER_SIZE, uiRspLen);
00325         while( iBack == 0 && uiRspLen > 0 && m_pParent->m_ucResponseData[0] == 0xAF)
00326         {
00327                 if((uiResponseLength + uiRspLen - 1) > uiResponseBufferLength)
00328                         return FEDM_ERROR_BUFFER_LENGTH;
00329 
00330                 // copy receive data into application buffer (without status 0xAF)
00331                 memcpy(&pucResponseData[uiResponseLength], &m_pParent->m_ucResponseData[1], uiRspLen - 1);
00332 
00333                 uiCnt = 0;
00334                 uiResponseLength += uiRspLen - 1;
00335                 m_pParent->m_ucRequestData[uiCnt++] = 0xAF;
00336                 iBack = m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, m_pParent->m_ucResponseData, FEDM_ISO14443_4_APDU_BUFFER_SIZE, uiRspLen);
00337         }
00338 
00339         if(iBack == 0 && uiRspLen > 0)
00340         {
00341                 if((uiResponseLength + uiRspLen - 1) > uiResponseBufferLength)
00342                         return FEDM_ERROR_BUFFER_LENGTH;
00343 
00344                 pucResponseData[0] = m_pParent->m_ucResponseData[0]; // status
00345                 memcpy(&pucResponseData[uiResponseLength], &m_pParent->m_ucResponseData[1], uiRspLen - 1);
00346                 uiResponseLength += uiRspLen - 1;
00347         }
00348 
00349         return iBack;
00350 }
00351 
00352         // [0x6E] : T=CL
00353 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::FreeMemory(
00354         unsigned char* pucResponseData,
00355         unsigned int uiResponseBufferLength,
00356         unsigned int& uiResponseLength )
00357 {
00358         FEDM_CHK3(pucResponseData);
00359 
00360         unsigned int uiCnt = 0;
00361 
00362         m_pParent->m_ucRequestData[uiCnt++] = 0x6E;
00363 
00364         return m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
00365 }
00366 
00367         // [0x6D] : T=CL
00368 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::GetDFNames(
00369         unsigned char* pucResponseData,
00370         unsigned int uiResponseBufferLength,
00371         unsigned int& uiResponseLength )
00372 {
00373         FEDM_CHK3(pucResponseData);
00374 
00375         int iBack = 0;
00376         unsigned int uiRspLen = 0;
00377         unsigned int uiCnt = 0;
00378 
00379         uiResponseLength = 1; // increment for status byte
00380         
00381         m_pParent->m_ucRequestData[uiCnt++] = 0x6D;
00382 
00383         iBack = m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, m_pParent->m_ucResponseData, FEDM_ISO14443_4_APDU_BUFFER_SIZE, uiRspLen);
00384         while( iBack == 0 && uiRspLen > 0 && m_pParent->m_ucResponseData[0] == 0xAF)
00385         {
00386                 if((uiResponseLength + uiRspLen - 1) > uiResponseBufferLength)
00387                         return FEDM_ERROR_BUFFER_LENGTH;
00388 
00389                 // copy receive data into application buffer (without status 0xAF)
00390                 memcpy(&pucResponseData[uiResponseLength], &m_pParent->m_ucResponseData[1], uiRspLen - 1);
00391 
00392                 uiCnt = 0;
00393                 uiResponseLength += uiRspLen - 1;
00394                 m_pParent->m_ucRequestData[uiCnt++] = 0xAF;
00395                 iBack = m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, m_pParent->m_ucResponseData, FEDM_ISO14443_4_APDU_BUFFER_SIZE, uiRspLen);
00396         }
00397 
00398         if(iBack == 0 && uiRspLen > 0)
00399         {
00400                 if((uiResponseLength + uiRspLen - 1) > uiResponseBufferLength)
00401                         return FEDM_ERROR_BUFFER_LENGTH;
00402 
00403                 pucResponseData[0] = m_pParent->m_ucResponseData[0]; // status
00404                 memcpy(&pucResponseData[uiResponseLength], &m_pParent->m_ucResponseData[1], uiRspLen - 1);
00405                 uiResponseLength += uiRspLen - 1;
00406         }
00407 
00408         return iBack;
00409 }
00410 
00411         // [0x5A] : T=CL
00412 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::SelectApplication(
00413         unsigned int uiApplicationID,
00414         unsigned char* pucResponseData,
00415         unsigned int uiResponseBufferLength,
00416         unsigned int& uiResponseLength )
00417 {
00418         FEDM_CHK3(pucResponseData);
00419 
00420         unsigned int uiCnt = 0;
00421 
00422         m_pParent->m_ucRequestData[uiCnt++] = 0x5A;
00423         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char) (uiApplicationID & 0x000000FF);
00424         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiApplicationID & 0x0000FF00) >>  8);
00425         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiApplicationID & 0x00FF0000) >> 16);
00426 
00427         return m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
00428 }
00429 
00430         // [0xFC] : T=CL
00431 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::FormatPICC(
00432         unsigned char* pucResponseData,
00433         unsigned int uiResponseBufferLength,
00434         unsigned int& uiResponseLength )
00435 {
00436         FEDM_CHK3(pucResponseData);
00437 
00438         unsigned int uiCnt = 0;
00439 
00440         m_pParent->m_ucRequestData[uiCnt++] = 0xFC;
00441 
00442         return m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
00443 }
00444 
00445         // [0x60] : T=CL
00446 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::GetVersion(
00447         unsigned char* pucResponseData,
00448         unsigned int uiResponseBufferLength,
00449         unsigned int& uiResponseLength )
00450 {
00451         FEDM_CHK3(pucResponseData);
00452 
00453         int iBack = 0;
00454         unsigned int uiRspLen = 0;
00455         unsigned int uiCnt = 0;
00456 
00457         uiResponseLength = 1; // increment for status byte
00458         
00459         m_pParent->m_ucRequestData[uiCnt++] = 0x60;
00460 
00461         iBack = m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, m_pParent->m_ucResponseData, FEDM_ISO14443_4_APDU_BUFFER_SIZE, uiRspLen);
00462         while( iBack == 0 && uiRspLen > 0 && m_pParent->m_ucResponseData[0] == 0xAF)
00463         {
00464                 if((uiResponseLength + uiRspLen - 1) > uiResponseBufferLength)
00465                         return FEDM_ERROR_BUFFER_LENGTH;
00466 
00467                 // copy receive data into application buffer (without status 0xAF)
00468                 memcpy(&pucResponseData[uiResponseLength], &m_pParent->m_ucResponseData[1], uiRspLen - 1);
00469 
00470                 uiCnt = 0;
00471                 uiResponseLength += uiRspLen - 1;
00472                 m_pParent->m_ucRequestData[uiCnt++] = 0xAF;
00473                 iBack = m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, m_pParent->m_ucResponseData, FEDM_ISO14443_4_APDU_BUFFER_SIZE, uiRspLen);
00474         }
00475 
00476         if(iBack == 0 && uiRspLen > 0)
00477         {
00478                 if((uiResponseLength + uiRspLen - 1) > uiResponseBufferLength)
00479                         return FEDM_ERROR_BUFFER_LENGTH;
00480 
00481                 pucResponseData[0] = 0x00; // status
00482                 memcpy(&pucResponseData[uiResponseLength], &m_pParent->m_ucResponseData[1], uiRspLen - 1);
00483                 uiResponseLength += uiRspLen - 1;
00484         }
00485 
00486         return iBack;
00487 }
00488 
00489         // [0x51] : T=CL
00490 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::GetCardUID(
00491         unsigned char* pucResponseData,
00492         unsigned int uiResponseBufferLength,
00493         unsigned int& uiResponseLength )
00494 {
00495         FEDM_CHK3(pucResponseData);
00496 
00497         unsigned int uiCnt = 0;
00498 
00499         m_pParent->m_ucRequestData[uiCnt++] = 0x51;
00500 
00501         return m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
00502 }
00503 
00504         // [0x5C] : T=CL
00505 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::SetConfiguration(
00506         unsigned char* pucRequestData,
00507         unsigned int uiRequestLength,
00508         unsigned char* pucResponseData,
00509         unsigned int uiResponseBufferLength,
00510         unsigned int& uiResponseLength )
00511 {
00512         FEDM_CHK3(pucResponseData);
00513 
00514         unsigned int uiCnt = 0;
00515 
00516         if(uiRequestLength > FEDM_ISO14443_4_APDU_BUFFER_SIZE - 1)
00517                 return FEDM_ERROR_BUFFER_LENGTH;
00518 
00519         m_pParent->m_ucRequestData[uiCnt++] = 0x64;
00520         if(pucRequestData != NULL)
00521         {
00522                 memcpy(&m_pParent->m_ucRequestData[uiCnt], pucRequestData, uiRequestLength);
00523                 uiCnt += uiRequestLength;
00524         }
00525 
00526         m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
00527 
00528         return FEDM_OK;
00529 }
00530 
00531 
00532         //####################################################################
00533         // 10 application level commands
00534 
00535         // [0x5F] : [0xC1] DESFire Commands
00536 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::ChangeFileSettings(
00537         unsigned int uiApplicationID,
00538         unsigned char ucReaderKeyIndex,
00539         unsigned char ucDESFireKeyNo,
00540         unsigned char ucFileNo,
00541         unsigned char ucNewFileCommSettings,
00542         unsigned char ucNewFileReadWriteAccessRights,
00543         unsigned char ucNewFileChangeAccessRights,
00544         unsigned char ucNewFileReadAccessRights,
00545         unsigned char ucNewFileWriteAccessRights )
00546 {
00547         int iErr = 0;
00548 
00549         // swap MSB with LSB
00550         unsigned int uiTmp = 0;
00551         uiTmp |= ((uiApplicationID & 0x000000FF) << 16);
00552         uiTmp |= ((uiApplicationID & 0x0000FF00) );
00553         uiTmp |= ((uiApplicationID & 0x00FF0000) >> 16);
00554 
00555         unsigned char ucAccessRights[2];
00556         ucAccessRights[0] = ((ucNewFileReadWriteAccessRights & 0x0F) << 4) + (ucNewFileChangeAccessRights & 0x0F);
00557         ucAccessRights[1] = ((ucNewFileReadAccessRights & 0x0F) << 4) + (ucNewFileWriteAccessRights & 0x0F);
00558 
00559         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_CMD, (unsigned char)0x5F));
00560         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_MODE, (unsigned char)0x00));
00561         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_APP_ID, uiTmp));
00562         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_INDEX, ucReaderKeyIndex));
00563         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_NO, ucDESFireKeyNo));
00564         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_NO, ucFileNo));
00565         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_COMM_SETTINGS, ucNewFileCommSettings));
00566         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_ACCESS_RIGHTS, ucAccessRights, 2));
00567 
00568         int iBack = m_pParent->m_pReader->SendProtocol(0xC1);
00569         if( iBack==0x00 )
00570         {
00571                 m_ucErrorSource = 0;
00572                 m_uiErrorCode = 0;
00573         }
00574         else if( iBack==0x97 )
00575         {
00576                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_SOURCE, &m_ucErrorSource));
00577                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_CODE, &m_uiErrorCode));
00578         }
00579 
00580         return iBack;
00581 }
00582 
00583         // [0x6F] : T=CL
00584 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::GetFileIDs(
00585         unsigned char* pucResponseData,
00586         unsigned int uiResponseBufferLength,
00587         unsigned int& uiResponseLength )
00588 {
00589         FEDM_CHK3(pucResponseData);
00590 
00591         unsigned int uiCnt = 0;
00592 
00593         m_pParent->m_ucRequestData[uiCnt++] = 0x6F;
00594 
00595         return m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
00596 }
00597 
00598         // [0xF5] : T=CL
00599 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::GetFileSettings(
00600         unsigned char ucFileNo,
00601         unsigned char* pucResponseData,
00602         unsigned int uiResponseBufferLength,
00603         unsigned int& uiResponseLength )
00604 {
00605         FEDM_CHK3(pucResponseData);
00606 
00607         unsigned int uiCnt = 0;
00608 
00609         m_pParent->m_ucRequestData[uiCnt++] = 0xF5;
00610         m_pParent->m_ucRequestData[uiCnt++] = ucFileNo;
00611 
00612         return m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
00613 }
00614 
00615         // [0xCD] : T=CL
00616 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::CreateStdDataFile(
00617         unsigned char ucFileNo,
00618         bool bWithISOFileID,
00619         unsigned int uiISOFileID,
00620         unsigned char ucFileCommSettings,
00621         unsigned char ucFileReadWriteAccessRights,
00622         unsigned char ucFileChangeAccessRights,
00623         unsigned char ucFileReadAccessRights,
00624         unsigned char ucFileWriteAccessRights,
00625         unsigned int uiFileSize,
00626         unsigned char* pucResponseData,
00627         unsigned int uiResponseBufferLength,
00628         unsigned int& uiResponseLength )
00629 {
00630         FEDM_CHK3(pucResponseData);
00631 
00632         unsigned int uiCnt = 0;
00633 
00634         m_pParent->m_ucRequestData[uiCnt++] = 0xCD;
00635         m_pParent->m_ucRequestData[uiCnt++] = ucFileNo;
00636         if(bWithISOFileID)
00637         {
00638                 m_pParent->m_ucRequestData[uiCnt++] = (unsigned char) (uiISOFileID & 0x000000FF);
00639                 m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiISOFileID & 0x0000FF00) >> 8);
00640         }
00641         m_pParent->m_ucRequestData[uiCnt++] = ucFileCommSettings;
00642         m_pParent->m_ucRequestData[uiCnt++] = ((ucFileReadWriteAccessRights & 0x0F) << 4) + (ucFileChangeAccessRights & 0x0F);
00643         m_pParent->m_ucRequestData[uiCnt++] = ((ucFileReadAccessRights & 0x0F) << 4) + (ucFileWriteAccessRights & 0x0F);
00644         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char) (uiFileSize & 0x000000FF);
00645         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiFileSize & 0x0000FF00) >>  8);
00646         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiFileSize & 0x00FF0000) >> 16);
00647 
00648         return m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
00649 }
00650 
00651         // [0xCB] : T=CL
00652 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::CreateBackupDataFile(
00653         unsigned char ucFileNo,
00654         bool bWithISOFileID,
00655         unsigned int uiISOFileID,
00656         unsigned char ucFileCommSettings,
00657         unsigned char ucFileReadWriteAccessRights,
00658         unsigned char ucFileChangeAccessRights,
00659         unsigned char ucFileReadAccessRights,
00660         unsigned char ucFileWriteAccessRights,
00661         unsigned int uiFileSize,
00662         unsigned char* pucResponseData,
00663         unsigned int uiResponseBufferLength,
00664         unsigned int& uiResponseLength )
00665 {
00666         FEDM_CHK3(pucResponseData);
00667 
00668         unsigned int uiCnt = 0;
00669 
00670         m_pParent->m_ucRequestData[uiCnt++] = 0xCB;
00671         m_pParent->m_ucRequestData[uiCnt++] = ucFileNo;
00672         if(bWithISOFileID)
00673         {
00674                 m_pParent->m_ucRequestData[uiCnt++] = (unsigned char) (uiISOFileID & 0x000000FF);
00675                 m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiISOFileID & 0x0000FF00) >> 8);
00676         }
00677         m_pParent->m_ucRequestData[uiCnt++] = ucFileCommSettings;
00678         m_pParent->m_ucRequestData[uiCnt++] = ((ucFileReadWriteAccessRights & 0x0F) << 4) + (ucFileChangeAccessRights & 0x0F);
00679         m_pParent->m_ucRequestData[uiCnt++] = ((ucFileReadAccessRights & 0x0F) << 4) + (ucFileWriteAccessRights & 0x0F);
00680         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char) (uiFileSize & 0x000000FF);
00681         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiFileSize & 0x0000FF00) >>  8);
00682         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiFileSize & 0x00FF0000) >> 16);
00683 
00684         return m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
00685 }
00686 
00687         // [0xCC] : T=CL
00688 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::CreateValueFile(
00689         unsigned char ucFileNo,
00690         unsigned char ucFileCommSettings,
00691         unsigned char ucFileReadWriteAccessRights,
00692         unsigned char ucFileChangeAccessRights,
00693         unsigned char ucFileReadAccessRights,
00694         unsigned char ucFileWriteAccessRights,
00695         unsigned int uiLowerLimit,
00696         unsigned int uiUpperLimit,
00697         unsigned int uiValue,
00698         unsigned char ucLimitedCreditEnabled,
00699         unsigned char* pucResponseData,
00700         unsigned int uiResponseBufferLength,
00701         unsigned int& uiResponseLength )
00702 {
00703         FEDM_CHK3(pucResponseData);
00704 
00705         unsigned int uiCnt = 0;
00706 
00707         m_pParent->m_ucRequestData[uiCnt++] = 0xCC;
00708         m_pParent->m_ucRequestData[uiCnt++] = ucFileNo;
00709         m_pParent->m_ucRequestData[uiCnt++] = ucFileCommSettings;
00710         m_pParent->m_ucRequestData[uiCnt++] = ((ucFileReadWriteAccessRights & 0x0F) << 4) + (ucFileChangeAccessRights & 0x0F);
00711         m_pParent->m_ucRequestData[uiCnt++] = ((ucFileReadAccessRights & 0x0F) << 4) + (ucFileWriteAccessRights & 0x0F);
00712         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char) (uiLowerLimit & 0x000000FF);
00713         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiLowerLimit & 0x0000FF00) >>  8);
00714         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiLowerLimit & 0x00FF0000) >> 16);
00715         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiLowerLimit & 0xFF000000) >> 24);
00716         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char) (uiUpperLimit & 0x000000FF);
00717         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiUpperLimit & 0x0000FF00) >>  8);
00718         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiUpperLimit & 0x00FF0000) >> 16);
00719         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiUpperLimit & 0xFF000000) >> 24);
00720         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char) (uiValue & 0x000000FF);
00721         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiValue & 0x0000FF00) >>  8);
00722         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiValue & 0x00FF0000) >> 16);
00723         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiValue & 0xFF000000) >> 24);
00724         m_pParent->m_ucRequestData[uiCnt++] = ucLimitedCreditEnabled;
00725 
00726         return m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
00727 }
00728 
00729         // [0xC1] : T=CL
00730 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::CreateLinearRecordFile(
00731         unsigned char ucFileNo,
00732         bool bWithISOFileID,
00733         unsigned int uiISOFileID,
00734         unsigned char ucFileCommSettings,
00735         unsigned char ucFileReadWriteAccessRights,
00736         unsigned char ucFileChangeAccessRights,
00737         unsigned char ucFileReadAccessRights,
00738         unsigned char ucFileWriteAccessRights,
00739         unsigned int uiRecordSize,
00740         unsigned int uiMaxNoOfRecords,
00741         unsigned char* pucResponseData,
00742         unsigned int uiResponseBufferLength,
00743         unsigned int& uiResponseLength )
00744 {
00745         FEDM_CHK3(pucResponseData);
00746 
00747         unsigned int uiCnt = 0;
00748 
00749         m_pParent->m_ucRequestData[uiCnt++] = 0xC1;
00750         m_pParent->m_ucRequestData[uiCnt++] = ucFileNo;
00751         if(bWithISOFileID)
00752         {
00753                 m_pParent->m_ucRequestData[uiCnt++] = (unsigned char) (uiISOFileID & 0x000000FF);
00754                 m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiISOFileID & 0x0000FF00) >> 8);
00755         }
00756         m_pParent->m_ucRequestData[uiCnt++] = ucFileCommSettings;
00757         m_pParent->m_ucRequestData[uiCnt++] = ((ucFileReadWriteAccessRights & 0x0F) << 4) + (ucFileChangeAccessRights & 0x0F);
00758         m_pParent->m_ucRequestData[uiCnt++] = ((ucFileReadAccessRights & 0x0F) << 4) + (ucFileWriteAccessRights & 0x0F);
00759         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char) (uiRecordSize & 0x000000FF);
00760         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiRecordSize & 0x0000FF00) >>  8);
00761         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiRecordSize & 0x00FF0000) >> 16);
00762         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char) (uiMaxNoOfRecords & 0x000000FF);
00763         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiMaxNoOfRecords & 0x0000FF00) >>  8);
00764         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiMaxNoOfRecords & 0x00FF0000) >> 16);
00765 
00766         return m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
00767 }
00768 
00769         // [0xC0] : T=CL
00770 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::CreateCyclicRecordFile(
00771         unsigned char ucFileNo,
00772         bool bWithISOFileID,
00773         unsigned int uiISOFileID,
00774         unsigned char ucFileCommSettings,
00775         unsigned char ucFileReadWriteAccessRights,
00776         unsigned char ucFileChangeAccessRights,
00777         unsigned char ucFileReadAccessRights,
00778         unsigned char ucFileWriteAccessRights,
00779         unsigned int uiRecordSize,
00780         unsigned int uiMaxNoOfRecords,
00781         unsigned char* pucResponseData,
00782         unsigned int uiResponseBufferLength,
00783         unsigned int& uiResponseLength )
00784 {
00785         FEDM_CHK3(pucResponseData);
00786 
00787         unsigned int uiCnt = 0;
00788 
00789         m_pParent->m_ucRequestData[uiCnt++] = 0xC0;
00790         m_pParent->m_ucRequestData[uiCnt++] = ucFileNo;
00791         if(bWithISOFileID)
00792         {
00793                 m_pParent->m_ucRequestData[uiCnt++] = (unsigned char) (uiISOFileID & 0x000000FF);
00794                 m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiISOFileID & 0x0000FF00) >> 8);
00795         }
00796         m_pParent->m_ucRequestData[uiCnt++] = ucFileCommSettings;
00797         m_pParent->m_ucRequestData[uiCnt++] = ((ucFileReadWriteAccessRights & 0x0F) << 4) + (ucFileChangeAccessRights & 0x0F);
00798         m_pParent->m_ucRequestData[uiCnt++] = ((ucFileReadAccessRights & 0x0F) << 4) + (ucFileWriteAccessRights & 0x0F);
00799         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char) (uiRecordSize & 0x000000FF);
00800         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiRecordSize & 0x0000FF00) >>  8);
00801         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiRecordSize & 0x00FF0000) >> 16);
00802         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char) (uiMaxNoOfRecords & 0x000000FF);
00803         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiMaxNoOfRecords & 0x0000FF00) >>  8);
00804         m_pParent->m_ucRequestData[uiCnt++] = (unsigned char)((uiMaxNoOfRecords & 0x00FF0000) >> 16);
00805 
00806         return m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
00807 }
00808 
00809         // [0xDF] : T=CL
00810 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::DeleteFile(
00811         unsigned char ucFileNo,
00812         unsigned char* pucResponseData,
00813         unsigned int uiResponseBufferLength,
00814         unsigned int& uiResponseLength )
00815 {
00816         FEDM_CHK3(pucResponseData);
00817 
00818         unsigned int uiCnt = 0;
00819 
00820         m_pParent->m_ucRequestData[uiCnt++] = 0xDF;
00821         m_pParent->m_ucRequestData[uiCnt++] = ucFileNo;
00822 
00823         return m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
00824 }
00825 
00826         // [0x61] : T=CL
00827 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::GetISOFileIDs(
00828         unsigned char* pucResponseData,
00829         unsigned int uiResponseBufferLength,
00830         unsigned int& uiResponseLength )
00831 {
00832         FEDM_CHK3(pucResponseData);
00833 
00834         int iBack = 0;
00835         unsigned int uiRspLen = 0;
00836         unsigned int uiCnt = 0;
00837 
00838         uiResponseLength = 1; // increment for status byte
00839         
00840         m_pParent->m_ucRequestData[uiCnt++] = 0x61;
00841 
00842         iBack = m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, m_pParent->m_ucResponseData, FEDM_ISO14443_4_APDU_BUFFER_SIZE, uiRspLen);
00843         while( iBack == 0 && uiRspLen > 0 && m_pParent->m_ucResponseData[0] == 0xAF)
00844         {
00845                 if((uiResponseLength + uiRspLen - 1) > uiResponseBufferLength)
00846                         return FEDM_ERROR_BUFFER_LENGTH;
00847 
00848                 // copy receive data into application buffer (without status 0xAF)
00849                 memcpy(&pucResponseData[uiResponseLength], &m_pParent->m_ucResponseData[1], uiRspLen - 1);
00850 
00851                 uiCnt = 0;
00852                 uiResponseLength += uiRspLen - 1;
00853                 m_pParent->m_ucRequestData[uiCnt++] = 0xAF;
00854                 iBack = m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, m_pParent->m_ucResponseData, FEDM_ISO14443_4_APDU_BUFFER_SIZE, uiRspLen);
00855         }
00856 
00857         if(iBack == 0 && uiRspLen > 0)
00858         {
00859                 if((uiResponseLength + uiRspLen - 1) > uiResponseBufferLength)
00860                         return FEDM_ERROR_BUFFER_LENGTH;
00861 
00862                 pucResponseData[0] = m_pParent->m_ucResponseData[0]; // status
00863                 memcpy(&pucResponseData[uiResponseLength], &m_pParent->m_ucResponseData[1], uiRspLen - 1);
00864                 uiResponseLength += uiRspLen - 1;
00865         }
00866 
00867         return iBack;
00868 }
00869 
00870 
00871         //####################################################################
00872         // 11 data manipulation commands
00873         
00874         // [0xBD] : [0xC1] DESFire Commands
00875 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::ReadStandardData(
00876         unsigned int uiApplicationID,
00877         unsigned char ucReaderKeyIndex,
00878         unsigned char ucDESFireKeyNo,
00879         unsigned char ucFileNo,
00880         unsigned char ucFileCommSettings,
00881         unsigned int uiFileOffset,
00882         unsigned int uiFileLen,
00883         unsigned char* pucFileData )
00884 {
00885         FEDM_CHK3(pucFileData);
00886 
00887         int iErr = 0;
00888 
00889         // swap MSB with LSB
00890         unsigned int uiTmp = 0;
00891         uiTmp |= ((uiApplicationID & 0x000000FF) << 16);
00892         uiTmp |= ((uiApplicationID & 0x0000FF00) );
00893         uiTmp |= ((uiApplicationID & 0x00FF0000) >> 16);
00894 
00895         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_CMD, (unsigned char)0xBD));
00896         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_MODE, (unsigned char)0x00));
00897         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_APP_ID, uiTmp));
00898         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_INDEX, ucReaderKeyIndex));
00899         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_NO, ucDESFireKeyNo));
00900         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_NO, ucFileNo));
00901         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_COMM_SETTINGS, ucFileCommSettings));
00902         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_OFFSET, uiFileOffset));
00903         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_DATA_LEN, uiFileLen));
00904 
00905         int iBack = m_pParent->m_pReader->SendProtocol(0xC1);
00906         if( iBack==0x00 )
00907         {
00908                 memcpy(pucFileData, &m_pTabItem->m_ucRxDB[uiFileOffset], uiFileLen);
00909                 m_ucErrorSource = 0;
00910                 m_uiErrorCode = 0;
00911         }
00912         else if( iBack==0x97 )
00913         {
00914                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_SOURCE, &m_ucErrorSource));
00915                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_CODE, &m_uiErrorCode));
00916         }
00917 
00918         return iBack;
00919 }
00920         
00921         // [0x3D] : [0xC1] DESFire Commands
00922 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::WriteStandardData(
00923         unsigned int uiApplicationID,
00924         unsigned char ucReaderKeyIndex,
00925         unsigned char ucDESFireKeyNo,
00926         unsigned char ucFileNo,
00927         unsigned char ucFileCommSettings,
00928         unsigned int uiFileOffset,
00929         unsigned int uiFileLen,
00930         unsigned char* pucFileData )
00931 {
00932         FEDM_CHK3(pucFileData);
00933 
00934         int iErr = 0;
00935 
00936         if(uiFileOffset + uiFileLen > FEDM_ISC_ISO_TABLE_RxDB_SIZE)
00937                 return FEDM_ERROR_ARRAY_BOUNDARY;
00938 
00939         // swap MSB with LSB
00940         unsigned int uiTmp = 0;
00941         uiTmp |= ((uiApplicationID & 0x000000FF) << 16);
00942         uiTmp |= ((uiApplicationID & 0x0000FF00) );
00943         uiTmp |= ((uiApplicationID & 0x00FF0000) >> 16);
00944 
00945         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_CMD, (unsigned char)0x3D));
00946         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_MODE, (unsigned char)0x00));
00947         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_APP_ID, uiTmp));
00948         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_INDEX, ucReaderKeyIndex));
00949         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_NO, ucDESFireKeyNo));
00950         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_NO, ucFileNo));
00951         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_COMM_SETTINGS, ucFileCommSettings));
00952         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_OFFSET, uiFileOffset));
00953         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_DATA_LEN, uiFileLen));
00954 
00955         memcpy(&m_pTabItem->m_ucTxDB[uiFileOffset], pucFileData, uiFileLen);
00956 
00957         int iBack = m_pParent->m_pReader->SendProtocol(0xC1);
00958         if( iBack==0x00 )
00959         {
00960                 m_ucErrorSource = 0;
00961                 m_uiErrorCode = 0;
00962         }
00963         else if( iBack==0x97 )
00964         {
00965                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_SOURCE, &m_ucErrorSource));
00966                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_CODE, &m_uiErrorCode));
00967         }
00968 
00969         return iBack;
00970 }
00971 
00972         // [0x6C] : [0xC1] DESFire Commands
00973 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::GetValue(
00974         unsigned int uiApplicationID,
00975         unsigned char ucReaderKeyIndex,
00976         unsigned char ucDESFireKeyNo,
00977         unsigned char ucFileNo,
00978         unsigned char ucFileCommSettings,
00979         unsigned int& uiValue )
00980 {
00981         int iErr = 0;
00982 
00983         // swap MSB with LSB
00984         unsigned int uiTmp = 0;
00985         uiTmp |= ((uiApplicationID & 0x000000FF) << 16);
00986         uiTmp |= ((uiApplicationID & 0x0000FF00) );
00987         uiTmp |= ((uiApplicationID & 0x00FF0000) >> 16);
00988 
00989         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_CMD, (unsigned char)0x6C));
00990         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_MODE, (unsigned char)0x00));
00991         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_APP_ID, uiTmp));
00992         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_INDEX, ucReaderKeyIndex));
00993         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_NO, ucDESFireKeyNo));
00994         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_NO, ucFileNo));
00995         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_COMM_SETTINGS, ucFileCommSettings));
00996 
00997         int iBack = m_pParent->m_pReader->SendProtocol(0xC1);
00998         if( iBack==0x00 )
00999         {
01000                 uiValue = m_pTabItem->m_uiValue;
01001                 m_ucErrorSource = 0;
01002                 m_uiErrorCode = 0;
01003         }
01004         else if( iBack==0x97 )
01005         {
01006                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_SOURCE, &m_ucErrorSource));
01007                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_CODE, &m_uiErrorCode));
01008         }
01009 
01010         return iBack;
01011 }
01012 
01013         // [0x0C] : [0xC1] DESFire Commands
01014 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::Credit(
01015         unsigned int uiApplicationID,
01016         unsigned char ucReaderKeyIndex,
01017         unsigned char ucDESFireKeyNo,
01018         unsigned char ucFileNo,
01019         unsigned char ucFileCommSettings,
01020         unsigned int uiCreditValue )
01021 {
01022         int iErr = 0;
01023 
01024         // swap MSB with LSB
01025         unsigned int uiTmp = 0;
01026         uiTmp |= ((uiApplicationID & 0x000000FF) << 16);
01027         uiTmp |= ((uiApplicationID & 0x0000FF00) );
01028         uiTmp |= ((uiApplicationID & 0x00FF0000) >> 16);
01029 
01030         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_CMD, (unsigned char)0x0C));
01031         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_MODE, (unsigned char)0x00));
01032         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_APP_ID, uiTmp));
01033         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_INDEX, ucReaderKeyIndex));
01034         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_NO, ucDESFireKeyNo));
01035         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_NO, ucFileNo));
01036         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_COMM_SETTINGS, ucFileCommSettings));
01037         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_CREDIT_VALUE, uiCreditValue));
01038 
01039         int iBack = m_pParent->m_pReader->SendProtocol(0xC1);
01040         if( iBack==0x00 )
01041         {
01042                 m_ucErrorSource = 0;
01043                 m_uiErrorCode = 0;
01044         }
01045         else if( iBack==0x97 )
01046         {
01047                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_SOURCE, &m_ucErrorSource));
01048                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_CODE, &m_uiErrorCode));
01049         }
01050 
01051         return iBack;
01052 }
01053 
01054         // [0xDC] : [0xC1] DESFire Commands
01055 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::Debit(
01056         unsigned int uiApplicationID,
01057         unsigned char ucReaderKeyIndex,
01058         unsigned char ucDESFireKeyNo,
01059         unsigned char ucFileNo,
01060         unsigned char ucFileCommSettings,
01061         unsigned int uiDebitValue )
01062 {
01063         int iErr = 0;
01064 
01065         // swap MSB with LSB
01066         unsigned int uiTmp = 0;
01067         uiTmp |= ((uiApplicationID & 0x000000FF) << 16);
01068         uiTmp |= ((uiApplicationID & 0x0000FF00) );
01069         uiTmp |= ((uiApplicationID & 0x00FF0000) >> 16);
01070 
01071         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_CMD, (unsigned char)0xDC));
01072         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_MODE, (unsigned char)0x00));
01073         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_APP_ID, uiTmp));
01074         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_INDEX, ucReaderKeyIndex));
01075         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_NO, ucDESFireKeyNo));
01076         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_NO, ucFileNo));
01077         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_COMM_SETTINGS, ucFileCommSettings));
01078         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_CREDIT_VALUE, uiDebitValue));
01079 
01080         int iBack = m_pParent->m_pReader->SendProtocol(0xC1);
01081         if( iBack==0x00 )
01082         {
01083                 m_ucErrorSource = 0;
01084                 m_uiErrorCode = 0;
01085         }
01086         else if( iBack==0x97 )
01087         {
01088                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_SOURCE, &m_ucErrorSource));
01089                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_CODE, &m_uiErrorCode));
01090         }
01091 
01092         return iBack;
01093 }
01094 
01095 
01096         // [0x1C] : [0xC1] DESFire Commands
01097 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::LimitedCredit(
01098         unsigned int uiApplicationID,
01099         unsigned char ucReaderKeyIndex,
01100         unsigned char ucDESFireKeyNo,
01101         unsigned char ucFileNo,
01102         unsigned char ucFileCommSettings,
01103         unsigned int uiCreditValue )
01104 {
01105         int iErr = 0;
01106 
01107         // swap MSB with LSB
01108         unsigned int uiTmp = 0;
01109         uiTmp |= ((uiApplicationID & 0x000000FF) << 16);
01110         uiTmp |= ((uiApplicationID & 0x0000FF00) );
01111         uiTmp |= ((uiApplicationID & 0x00FF0000) >> 16);
01112 
01113         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_CMD, (unsigned char)0x1C));
01114         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_MODE, (unsigned char)0x00));
01115         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_APP_ID, uiTmp));
01116         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_INDEX, ucReaderKeyIndex));
01117         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_NO, ucDESFireKeyNo));
01118         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_NO, ucFileNo));
01119         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_COMM_SETTINGS, ucFileCommSettings));
01120         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_CREDIT_VALUE, uiCreditValue));
01121 
01122         int iBack = m_pParent->m_pReader->SendProtocol(0xC1);
01123         if( iBack==0x00 )
01124         {
01125                 m_ucErrorSource = 0;
01126                 m_uiErrorCode = 0;
01127         }
01128         else if( iBack==0x97 )
01129         {
01130                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_SOURCE, &m_ucErrorSource));
01131                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_CODE, &m_uiErrorCode));
01132         }
01133 
01134         return iBack;
01135 }
01136 
01137         // [0x3B] : [0xC1] DESFire Commands
01138 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::WriteRecord(
01139         unsigned int uiApplicationID,
01140         unsigned char ucReaderKeyIndex,
01141         unsigned char ucDESFireKeyNo,
01142         unsigned char ucFileNo,
01143         unsigned char ucFileCommSettings,
01144         unsigned int uiFileOffset,
01145         unsigned int uiRecordLen,
01146         unsigned char* pucRecordData )
01147 {
01148         FEDM_CHK3(pucRecordData);
01149 
01150         int iErr = 0;
01151 
01152         if(uiFileOffset + uiRecordLen > FEDM_ISC_ISO_TABLE_TxDB_SIZE)
01153                 return FEDM_ERROR_ARRAY_BOUNDARY;
01154 
01155         // swap MSB with LSB
01156         unsigned int uiTmp = 0;
01157         uiTmp |= ((uiApplicationID & 0x000000FF) << 16);
01158         uiTmp |= ((uiApplicationID & 0x0000FF00) );
01159         uiTmp |= ((uiApplicationID & 0x00FF0000) >> 16);
01160 
01161         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_CMD, (unsigned char)0x3B));
01162         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_MODE, (unsigned char)0x00));
01163         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_APP_ID, uiTmp));
01164         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_INDEX, ucReaderKeyIndex));
01165         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_NO, ucDESFireKeyNo));
01166         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_NO, ucFileNo));
01167         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_COMM_SETTINGS, ucFileCommSettings));
01168         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_OFFSET, uiFileOffset));
01169         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_DATA_LEN, uiRecordLen));
01170 
01171         memcpy(&m_pTabItem->m_ucTxDB[uiFileOffset], pucRecordData, uiRecordLen);
01172 
01173         int iBack = m_pParent->m_pReader->SendProtocol(0xC1);
01174         if( iBack==0x00 )
01175         {
01176                 m_ucErrorSource = 0;
01177                 m_uiErrorCode = 0;
01178         }
01179         else if( iBack==0x97 )
01180         {
01181                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_SOURCE, &m_ucErrorSource));
01182                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_CODE, &m_uiErrorCode));
01183         }
01184 
01185         return iBack;
01186 }
01187 
01188         // [0xBB] : [0xC1] DESFire Commands
01189 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::ReadRecords(
01190         unsigned int uiApplicationID,
01191         unsigned char ucReaderKeyIndex,
01192         unsigned char ucDESFireKeyNo,
01193         unsigned char ucFileNo,
01194         unsigned char ucFileCommSettings,
01195         unsigned int uiRecordOffset,
01196         unsigned int uiNoOfRecords,
01197         unsigned int uiRecordSize,
01198         unsigned char* pucRecordData )
01199 {
01200         FEDM_CHK3(pucRecordData);
01201 
01202         int iErr = 0;
01203 
01204         if(uiNoOfRecords * uiRecordSize > FEDM_ISC_ISO_TABLE_RxDB_SIZE)
01205                 return FEDM_ERROR_ARRAY_BOUNDARY;
01206 
01207         // swap MSB with LSB
01208         unsigned int uiTmp = 0;
01209         uiTmp |= ((uiApplicationID & 0x000000FF) << 16);
01210         uiTmp |= ((uiApplicationID & 0x0000FF00) );
01211         uiTmp |= ((uiApplicationID & 0x00FF0000) >> 16);
01212 
01213         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_CMD, (unsigned char)0xBB));
01214         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_MODE, (unsigned char)0x00));
01215         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_APP_ID, uiTmp));
01216         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_INDEX, ucReaderKeyIndex));
01217         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_NO, ucDESFireKeyNo));
01218         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_NO, ucFileNo));
01219         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_COMM_SETTINGS, ucFileCommSettings));
01220         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_RECORD_OFFSET, uiRecordOffset));
01221         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_NO_OF_RECORDS, uiNoOfRecords));
01222         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_RECORD_SIZE, uiRecordSize));
01223 
01224         int iBack = m_pParent->m_pReader->SendProtocol(0xC1);
01225         if( iBack==0x00 )
01226         {
01227                 memcpy(pucRecordData, &m_pTabItem->m_ucRxDB[0], uiNoOfRecords*uiRecordSize);
01228                 m_ucErrorSource = 0;
01229                 m_uiErrorCode = 0;
01230         }
01231         else if( iBack==0x97 )
01232         {
01233                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_SOURCE, &m_ucErrorSource));
01234                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_CODE, &m_uiErrorCode));
01235         }
01236 
01237         return iBack;
01238 }
01239 
01240         // [0xEB] : [0xC1] DESFire Commands
01241 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::ClearRecordFile(
01242         unsigned int uiApplicationID,
01243         unsigned char ucReaderKeyIndex,
01244         unsigned char ucDESFireKeyNo,
01245         unsigned char ucFileNo,
01246         unsigned char ucFileCommSettings )
01247 {
01248         int iErr = 0;
01249 
01250         // swap MSB with LSB
01251         unsigned int uiTmp = 0;
01252         uiTmp |= ((uiApplicationID & 0x000000FF) << 16);
01253         uiTmp |= ((uiApplicationID & 0x0000FF00) );
01254         uiTmp |= ((uiApplicationID & 0x00FF0000) >> 16);
01255 
01256         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_CMD, (unsigned char)0xEB));
01257         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_MODE, (unsigned char)0x00));
01258         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_APP_ID, uiTmp));
01259         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_INDEX, ucReaderKeyIndex));
01260         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_KEY_NO, ucDESFireKeyNo));
01261         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_NO, ucFileNo));
01262         FEDM_CHK1(iErr, m_pParent->m_pReader->SetData(FEDM_ISC_TMP_0xC1_FILE_COMM_SETTINGS, ucFileCommSettings));
01263 
01264         int iBack = m_pParent->m_pReader->SendProtocol(0xC1);
01265         if( iBack==0x00 )
01266         {
01267                 m_ucErrorSource = 0;
01268                 m_uiErrorCode = 0;
01269         }
01270         else if( iBack==0x97 )
01271         {
01272                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_SOURCE, &m_ucErrorSource));
01273                 FEDM_CHK1(iErr, m_pParent->m_pReader->GetData(FEDM_ISC_TMP_0xC1_ERROR_CODE, &m_uiErrorCode));
01274         }
01275 
01276         return iBack;
01277 }
01278 
01279         // [0xC7] : T=CL
01280 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::CommitTransaction(
01281         unsigned char* pucResponseData,
01282         unsigned int uiResponseBufferLength,
01283         unsigned int& uiResponseLength )
01284 {
01285         FEDM_CHK3(pucResponseData);
01286 
01287         unsigned int uiCnt = 0;
01288 
01289         m_pParent->m_ucRequestData[uiCnt++] = 0xC7;
01290 
01291         return m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
01292 }
01293 
01294         // [0xA7] : T=CL
01295 int FedmIscTagHandler_ISO14443_4_MIFARE_DESFire_Impl_C1::AbortTransaction(
01296         unsigned char* pucResponseData,
01297         unsigned int uiResponseBufferLength,
01298         unsigned int& uiResponseLength )
01299 {
01300         FEDM_CHK3(pucResponseData);
01301 
01302         unsigned int uiCnt = 0;
01303 
01304         m_pParent->m_ucRequestData[uiCnt++] = 0xA7;
01305 
01306         return m_pParent->Apdu(m_pParent->m_ucRequestData, uiCnt, pucResponseData, uiResponseBufferLength, uiResponseLength);
01307 }
01308 
01309 #endif // #if !defined(_FEDM_NO_TAG_HANDLER_ISO14443)


rfid_drivers
Author(s): Raul Perula-Martinez
autogenerated on Thu Apr 2 2015 03:06:14