FedmIscTagHandler_ISO15693_Maxim_MAX66140.cpp
Go to the documentation of this file.
00001 /*-------------------------------------------------------
00002 |                                                       |
00003 |      FedmIscTagHandler_ISO15693_Maxim_MAX66140.cpp    |
00004 |                                                       |
00005 ---------------------------------------------------------
00006 
00007 Copyright © 2010-2011   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                  :       Marcel Raner
00017 Begin                   :       20.09.2010
00018 
00019 Version                 :       03.03.02 / 29.04.2011 / M. Raner, M. Hultsch
00020 
00021 Operation Systems       :       independent
00022 
00023 Function                        :       class for OBID i-scan® HF transponder ISO15693 of manufacturer MAXIM
00024 
00025 NOTE                            :       this class supports selected, addressed and Nonaddressed 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 #if !defined(_FEDM_NO_TAG_HANDLER_ISO15693)
00035 
00036 #include "FedmIscTagHandler_ISO15693_Maxim_MAX66140.h"
00037 #include "../FEDM_ISCReader.h"
00038 #include "../FEDM_ISOTabItem.h"
00039 #include "../FEDM_ISCReaderID.h"
00040 #include "../FEDM_ISC.h"
00041 #include "../../FEDM_Functions.h"
00042 #include "feisc.h"
00043 
00044 
00045 #if _MSC_VER >= 1400
00046         #pragma warning(disable : 4996)
00047 #endif
00048 
00049 // constructor
00050 FedmIscTagHandler_ISO15693_Maxim_MAX66140::FedmIscTagHandler_ISO15693_Maxim_MAX66140(
00051         FEDM_ISCReader* pReader,
00052         FEDM_ISOTabItem* pTabItem )
00053         : FedmIscTagHandler_ISO15693_Maxim(pReader, FedmIscTagHandler::TYPE_ISO15693_MAXIM_MAX66140, pTabItem)
00054 {
00055         m_sTagName = "ISO 15693 : ";
00056         m_sTagName += FEDM_TabItem::GetISO15693Manufacturer(FEDM_ISC_ISO_MFR_MAXIM);
00057         m_sTagName += " MAX66140";
00058         FedmIscTagHandler_ISO15693_Maxim_MAX66140::UpdateChallenge();
00059 }
00060 
00061 FedmIscTagHandler_ISO15693_Maxim_MAX66140::FedmIscTagHandler_ISO15693_Maxim_MAX66140(
00062         FEDM_ISCReader* pReader,
00063         unsigned int uiTagHandlerType,
00064         FEDM_ISOTabItem* pTabItem )
00065         : FedmIscTagHandler_ISO15693_Maxim(pReader, uiTagHandlerType, pTabItem)
00066 {
00067         m_sTagName = "ISO 15693 : ";
00068         m_sTagName += FEDM_TabItem::GetISO15693Manufacturer(FEDM_ISC_ISO_MFR_MAXIM);
00069         m_sTagName += " MAX66140";
00070         FedmIscTagHandler_ISO15693_Maxim_MAX66140::UpdateChallenge();
00071 }
00072 
00073 // destructor
00074 FedmIscTagHandler_ISO15693_Maxim_MAX66140::~FedmIscTagHandler_ISO15693_Maxim_MAX66140()
00075 {
00076 }
00077 
00078 // set all data members to 0
00079 /*void Init()
00080 {
00081 }*/
00082 
00083 /***************************************************************************
00084   Begin                 :       23.09.2010 / M. Raner
00085 
00086   Version               :       03.02.05 / 23.09.2010 / M. Raner
00087 
00088   Function                      :       Read Memory Page (use ReadSingleBlock())
00089   
00090   Parameters            :       unsigned char   ucRequestFlags          - [in]  request flags byte
00091                                                 unsigned char   ucPageNumber            - [in]  number of page, which should be read
00092                                                 unsigned char&  ucResponseFlags         - [out] 
00093                                                 unsigned char*  pucMemoryPageData       - [out] 32byte memory page data
00094 
00095   Return value          :       0                               - if tramsmission was successful
00096                                                 status byte (>1)- if the reader signals a problem
00097                                                 error code (<0) - if something goes wrong
00098 ***************************************************************************/
00099 int FedmIscTagHandler_ISO15693_Maxim_MAX66140::GetMemoryPageData(
00100         unsigned char ucRequestFlags, 
00101         unsigned char ucPageNumber, 
00102         unsigned char& ucResponseFlags,
00103         unsigned char* pucMemoryPageData)
00104 {
00105         FEDM_CHK3(pucMemoryPageData);
00106 
00107         int iBack = 0;
00108         unsigned char ucSecurityStatus; //temporary
00109         unsigned char pucMemoryData[8]; //temporary
00110 
00111         int iRounds = 0;
00112         if(ucPageNumber >= 0 && ucPageNumber < 4)
00113         {
00114                 iRounds = 4;
00115         }
00116         else if(ucPageNumber == 4)
00117         {
00118                 iRounds = 3;
00119         }
00120 
00121         for(int iCnt = 0; iCnt < iRounds; iCnt++)
00122         {
00123                 iBack = FedmIscTagHandler_ISO15693_Maxim_MAX66140::ReadSingleBlock(ucRequestFlags, ucPageNumber * 4 + iCnt, ucResponseFlags, ucSecurityStatus, pucMemoryData);
00124                 if(iBack == 0x95)
00125                 {
00126                         return iBack;
00127                 }
00128                 else if(iBack == 0)
00129                 {
00130                         if(ucResponseFlags)
00131                                 return 0;
00132 
00133                         for(int iCnt2 = 0; iCnt2 < 8; iCnt2++)
00134                         {
00135                                 pucMemoryPageData[iCnt*8+iCnt2] = pucMemoryData[iCnt2];
00136                         }
00137                 }
00138         }
00139 
00140         return 0;
00141 }
00142 
00143 /***************************************************************************
00144   Begin                 :       23.09.2010 / M. Raner
00145 
00146   Version               :       03.02.05 / 23.09.2010 / M. Raner
00147 
00148   Function                      :       computes PageMAC
00149   
00150   Parameters            :       unsigned char* pucSecret                        - [in]  request flags byte
00151                                                 unsigned char* pucMemoryPageData        - [in]  32byte memory page data
00152                                                 unsigned char ucPageNumber                      - [in]  number of page involved in computation
00153                                                 unsigned char* pucUID                           - [in]  uid value of tag
00154                                                 unsigned char* pucMAC                           - [out] computed page MAC
00155 
00156   Return value          :       0                               - if tramsmission was successful
00157                                                 status byte (>1)- if the reader signals a problem
00158                                                 error code (<0) - if something goes wrong
00159 ***************************************************************************/
00160 int FedmIscTagHandler_ISO15693_Maxim_MAX66140::UserComputePageMAC(
00161         unsigned char* pucSecret, 
00162         unsigned char* pucMemoryPageData, 
00163         unsigned char ucPageNumber, 
00164         unsigned char* pucUID,
00165         unsigned char* pucMAC)
00166 {
00167         FEDM_CHK3(pucSecret);
00168         FEDM_CHK3(pucMemoryPageData);
00169         FEDM_CHK3(pucUID);
00170         FEDM_CHK3(pucMAC);
00171 
00172         unsigned char buffer[64];
00173 
00174         int iCnt = 0;
00175 
00176         if((ucPageNumber >= 0x00) && (ucPageNumber <= 0x04))
00177         {
00178                 buffer[0] = pucSecret[0];
00179                 buffer[1] = pucSecret[1];
00180                 buffer[2] = pucSecret[2];
00181                 buffer[3] = pucSecret[3];
00182 
00183                 for(iCnt = 4; iCnt < 36; iCnt++)
00184                 {
00185                         buffer[iCnt] = pucMemoryPageData[iCnt - 4];
00186                 }
00187 
00188                 if(ucPageNumber == 0x04)
00189                 {
00190                         for(iCnt = 20; iCnt < 36; iCnt++)
00191                         {
00192                                 buffer[iCnt] = 0xFF;
00193                         }
00194                 }
00195 
00196                 buffer[36] = FedmIscTagHandler_ISO15693_Maxim_MAX66140::ucChallenge[0];
00197                 buffer[37] = FedmIscTagHandler_ISO15693_Maxim_MAX66140::ucChallenge[1];
00198 
00199                 buffer[38] = 0xFF;
00200                 buffer[39] = 0xFF;
00201 
00202                 buffer[40] = (unsigned char)((0x4 << 4) |ucPageNumber);
00203 
00204                 for(iCnt = 41; iCnt < 48; iCnt++)
00205                 {       
00206                         buffer[iCnt] = pucUID[iCnt - 41];       
00207                 }
00208                 
00209                 buffer[48] = pucSecret[4];
00210                 buffer[49] = pucSecret[5];
00211                 buffer[50] = pucSecret[6];
00212                 buffer[51] = pucSecret[7];
00213 
00214                 buffer[52] = FedmIscTagHandler_ISO15693_Maxim_MAX66140::ucChallenge[2];
00215                 buffer[53] = FedmIscTagHandler_ISO15693_Maxim_MAX66140::ucChallenge[3];
00216                 buffer[54] = FedmIscTagHandler_ISO15693_Maxim_MAX66140::ucChallenge[4];
00217 
00218                 buffer[55] = 0x80;
00219                 
00220                 for(iCnt = 56; iCnt < 62; iCnt++)
00221                 {
00222                         buffer[iCnt] = 0x00;
00223                 }
00224 
00225                 buffer[62] = 0x01;
00226                 buffer[63] = 0xB8;
00227         }
00228 
00229         unsigned int edcba[5];
00230         FedmIscTagHandler_ISO15693_Maxim_MAX66140::ComputeMAC(buffer,edcba);
00231 
00232         for(iCnt = 0; iCnt < 4; iCnt++)
00233         {
00234                 pucMAC[0+iCnt] = (unsigned char)((edcba[0] >> (iCnt * 8)) & 0xFF);
00235         }
00236         for(iCnt = 0; iCnt < 4; iCnt++)
00237         {
00238                 pucMAC[4+iCnt] = (unsigned char)((edcba[1] >> (iCnt * 8)) & 0xFF);
00239         }
00240         for(iCnt = 0; iCnt < 4; iCnt++)
00241         {
00242                 pucMAC[8+iCnt] = (unsigned char)((edcba[2] >> (iCnt * 8)) & 0xFF);
00243         }
00244         for(iCnt = 0; iCnt < 4; iCnt++)
00245         {
00246                 pucMAC[12+iCnt] = (unsigned char)((edcba[3] >> (iCnt * 8)) & 0xFF);
00247         }
00248         for(iCnt = 0; iCnt < 4; iCnt++)
00249         {
00250                 pucMAC[16+iCnt] = (unsigned char)((edcba[4] >> (iCnt * 8)) & 0xFF);
00251         }
00252 
00253         return 0;
00254 }
00255 
00256 /***************************************************************************
00257   Begin                 :       23.09.2010 / M. Raner
00258 
00259   Version               :       03.02.05 / 23.09.2010 / M. Raner
00260 
00261   Function                      :       computes a new Secret Value
00262   
00263   Parameters            :       unsigned char* pucSecret                        - [in]  old secret value
00264                                                 unsigned char* pucMemoryPageData        - [in]  memory page must match memory page number in GenerateSecret()
00265                                                 unsigned char* pucPartialSecret         - [in]  this value must match partial secret value in GenerateSecret()
00266                                                 unsigned char* pucNewSecret                     - [out] computed new secret
00267 
00268   Return value          :       0                               - if tramsmission was successful
00269                                                 status byte (>1)- if the reader signals a problem
00270                                                 error code (<0) - if something goes wrong
00271 ***************************************************************************/
00272 int     FedmIscTagHandler_ISO15693_Maxim_MAX66140::UserComputeSecret(
00273         unsigned char* pucSecret, 
00274         unsigned char* pucMemoryPageData, 
00275         unsigned char* pucPartialSecret,
00276         unsigned char* pucNewSecret)
00277 {
00278         FEDM_CHK3(pucSecret);
00279         FEDM_CHK3(pucMemoryPageData);
00280         FEDM_CHK3(pucPartialSecret);
00281         FEDM_CHK3(pucNewSecret);
00282 
00283         unsigned char buffer[64];
00284         int iCnt = 0;
00285 
00286         buffer[0] = pucSecret[0];
00287         buffer[1] = pucSecret[1];
00288         buffer[2] = pucSecret[2];
00289         buffer[3] = pucSecret[3];
00290 
00291         for(iCnt = 4; iCnt < 36; iCnt++)
00292         {
00293                 buffer[iCnt] = pucMemoryPageData[iCnt - 4];
00294         }
00295 
00296         buffer[36] = 0xFF;
00297         buffer[37] = 0xFF;
00298         buffer[38] = 0xFF;
00299         buffer[39] = 0xFF;
00300 
00301         buffer[40] = pucPartialSecret[0] & 0x3f;
00302 
00303         for(iCnt = 41; iCnt < 48; iCnt++)
00304         {
00305                 buffer[iCnt] = pucPartialSecret[iCnt-40];
00306         }
00307         
00308         buffer[48] = pucSecret[4];
00309         buffer[49] = pucSecret[5];
00310         buffer[50] = pucSecret[6];
00311         buffer[51] = pucSecret[7];
00312 
00313         buffer[52] = 0xFF;
00314         buffer[53] = 0xFF;
00315         buffer[54] = 0xFF;
00316 
00317         buffer[55] = 0x80;
00318         
00319         for(iCnt = 56; iCnt < 62; iCnt++)
00320         {
00321                 buffer[iCnt] = 0x00;
00322         }
00323 
00324         buffer[62] = 0x01;
00325         buffer[63] = 0xB8;
00326 
00327         unsigned int edcba[5];
00328         FedmIscTagHandler_ISO15693_Maxim_MAX66140::ComputeMAC(buffer,edcba);
00329 
00330         for(iCnt = 0; iCnt < 4; iCnt++)
00331         {
00332                 pucNewSecret[0+iCnt] = (unsigned char)((edcba[0] >> (iCnt * 8)) & 0xFF);
00333         }
00334         for(iCnt = 0; iCnt < 4; iCnt++)
00335         {
00336                 pucNewSecret[4+iCnt] = (unsigned char)((edcba[1] >> (iCnt * 8)) & 0xFF);
00337         }
00338 
00339         return 0;
00340 }
00341 
00342 /***************************************************************************
00343   Begin                 :       24.09.2010 / M. Raner
00344 
00345   Version               :       03.02.05 / 24.09.2010 / M. Raner
00346 
00347   Function                      :       UserComputeCopyBufferMAC
00348   
00349   Parameters            :       unsigned char* pucSecret                                - [in]  secret
00350                                                 unsigned char* pucMemoryPageData                - [in]  memory page must match memory page number used in CopyBuffer()
00351                                                 unsigned char* pucReadWriteBufferData   - [in]  must match data in read/write Buffer
00352                                                 unsigned char* pucIntegrityBytes                - [in]  integrity bytes(write counter) of datablock
00353                                                 unsigned char pucPageNumber                             - [in]  number of page used in CopyBuffer()
00354                                                 unsigned char* pucUID                                   - [in]  uid value of tag
00355                                                 unsigned char* pucMAC                                   - [out] computed CopyBuffer MAC
00356 
00357   Return value          :       0                               - if tramsmission was successful
00358                                                 status byte (>1)- if the reader signals a problem
00359                                                 error code (<0) - if something goes wrong
00360 ***************************************************************************/
00361 int FedmIscTagHandler_ISO15693_Maxim_MAX66140::UserComputeCopyBufferMAC(
00362         unsigned char* pucSecret, 
00363         unsigned char* pucMemoryPageData, 
00364         unsigned char* pucReadWriteBufferData, 
00365         unsigned char* pucIntegrityBytes,
00366         unsigned char pucPageNumber, 
00367         unsigned char* pucUID,
00368         unsigned char* pucMAC)
00369 {
00370         FEDM_CHK3(pucSecret);
00371         FEDM_CHK3(pucMemoryPageData);
00372         FEDM_CHK3(pucReadWriteBufferData);
00373         FEDM_CHK3(pucIntegrityBytes);
00374         FEDM_CHK3(pucUID);
00375         FEDM_CHK3(pucMAC);
00376 
00377         unsigned char buffer[64];
00378 
00379         int iCnt = 0;
00380 
00381         if((pucPageNumber >= 0x00) && (pucPageNumber <= 0x04))
00382         {
00383                 buffer[0] = pucSecret[0];
00384                 buffer[1] = pucSecret[1];
00385                 buffer[2] = pucSecret[2];
00386                 buffer[3] = pucSecret[3];
00387 
00388                 for(iCnt = 4; iCnt < 32; iCnt++)
00389                 {
00390                         buffer[iCnt] = pucMemoryPageData[iCnt - 4];
00391                 }
00392 
00393                 if(pucPageNumber == 0x04)
00394                 {
00395                         for(iCnt = 20; iCnt < 32; iCnt++)
00396                         {
00397                                 buffer[iCnt] = 0xFF;
00398                         }
00399                 }
00400 
00401                 for(iCnt = 32; iCnt < 40; iCnt++)
00402                 {
00403                         buffer[iCnt] = pucReadWriteBufferData[iCnt-32];
00404                 }
00405 
00406                 buffer[40] = pucPageNumber;
00407 
00408                 for(iCnt = 41; iCnt < 48; iCnt++)
00409                 {
00410                         buffer[iCnt] = pucUID[iCnt - 41];       
00411                 }
00412                 
00413                 buffer[48] = pucSecret[4];
00414                 buffer[49] = pucSecret[5];
00415                 buffer[50] = pucSecret[6];
00416                 buffer[51] = pucSecret[7];
00417 
00418                 buffer[52] = pucIntegrityBytes[0];
00419                 buffer[53] = pucIntegrityBytes[1];
00420                 buffer[54] = 0xFF;
00421 
00422                 buffer[55] = 0x80;
00423                 
00424                 for(iCnt = 56; iCnt < 62; iCnt++)
00425                 {
00426                         buffer[iCnt] = 0x00;
00427                 }
00428 
00429                 buffer[62] = 0x01;
00430                 buffer[63] = 0xB8;
00431         }       
00432 
00433         unsigned int edcba[5];
00434         FedmIscTagHandler_ISO15693_Maxim_MAX66140::ComputeMAC(buffer,edcba);
00435 
00436         for(iCnt = 0; iCnt < 4; iCnt++)
00437         {
00438                 pucMAC[0+iCnt] = (unsigned char)((edcba[0] >> (iCnt * 8)) & 0xFF);
00439         }
00440         for(iCnt = 0; iCnt < 4; iCnt++)
00441         {
00442                 pucMAC[4+iCnt] = (unsigned char)((edcba[1] >> (iCnt * 8)) & 0xFF);
00443         }
00444         for(iCnt = 0; iCnt < 4; iCnt++)
00445         {
00446                 pucMAC[8+iCnt] = (unsigned char)((edcba[2] >> (iCnt * 8)) & 0xFF);
00447         }
00448         for(iCnt = 0; iCnt < 4; iCnt++)
00449         {
00450                 pucMAC[12+iCnt] = (unsigned char)((edcba[3] >> (iCnt * 8)) & 0xFF);
00451         }
00452         for(iCnt = 0; iCnt < 4; iCnt++)
00453         {
00454                 pucMAC[16+iCnt] = (unsigned char)((edcba[4] >> (iCnt * 8)) & 0xFF);
00455         }
00456 
00457         return 0;
00458 }
00459 
00460 /***************************************************************************
00461   Begin                 :       22.09.2010 / M. Raner
00462 
00463   Version               :       03.02.05 / 22.09.2010 / M. Raner
00464 
00465   Function                      :       computes MAC for UserCompute... functions
00466   
00467   Parameters            :       unsigned char* ucM              - [in]  input data of sixteen 32-bit Words
00468                                                 unsigned int* uiMAC             - [out] computed MAC
00469 
00470   Return value          :       0                               - if tramsmission was successful
00471                                                 status byte (>1)- if the reader signals a problem
00472                                                 error code (<0) - if something goes wrong
00473 ***************************************************************************/
00474 int FedmIscTagHandler_ISO15693_Maxim_MAX66140::ComputeMAC(
00475         unsigned char* ucM, 
00476         unsigned int* uiMAC)
00477 {
00478         FEDM_CHK3(ucM);
00479         FEDM_CHK3(uiMAC);
00480 
00481         /*
00482                 This function includes a shriked version of SHA1 algorithm. It starts with input of 
00483                 eighty 32-bit words and ends with output of the values A,B,C,D,E of the 160-bit key.
00484                 The final constants, defined in ISO/IEC 10118-3 specification, are removed in this 
00485                 shriked version. The final constants are normally added after each 
00486                 block is computed. Because the MAX66x40 only processes one block, adding these 
00487                 constants does not add additional security.
00488         */
00489         unsigned int uiW[80];                           /* eighty 32-bit words */
00490         unsigned int uiA = 0x67452301;          /* init values defined in FIPS 180-1 */
00491     unsigned int uiB = 0xEFCDAB89;              /* init values defined in FIPS 180-1 */
00492     unsigned int uiC = 0x98BADCFE;              /* init values defined in FIPS 180-1 */
00493     unsigned int uiD = 0x10325476;              /* init values defined in FIPS 180-1 */
00494     unsigned int uiE = 0xC3D2E1F0;              /* init values defined in FIPS 180-1 */
00495     unsigned int uiF;
00496     unsigned int uiK;
00497     unsigned int uiTmp;
00498         int iCnt2 = 0;
00499 
00500         for(int iCnt = 0; iCnt*64 < 64; iCnt++)
00501         {
00502                 for (iCnt2 = 0; iCnt2 < 16; iCnt2++)
00503         {
00504             uiW[iCnt2] = (unsigned int)ucM[iCnt2 * 4 + (iCnt * 64)] << 24 | (unsigned int)ucM[iCnt2 * 4 + 1 + (iCnt * 64)] << 16 | (unsigned int)ucM[iCnt2 * 4 + 2 + (iCnt * 64)] << 8 | (unsigned int)ucM[iCnt2 * 4 + 3 + (iCnt * 64)];
00505         }
00506 
00507         /* Expand from 16 to 80 words */
00508         for (iCnt2 = 16; iCnt2 < 80; iCnt2++)
00509         {
00510                         uiW[iCnt2] = (((uiW[iCnt2 - 3] ^ uiW[iCnt2 - 8] ^ uiW[iCnt2 - 14] ^ uiW[iCnt2 - 16]) << 1) | ((uiW[iCnt2 - 3] ^ uiW[iCnt2 - 8] ^ uiW[iCnt2 - 14] ^ uiW[iCnt2 - 16]) >> (32 - 1)));
00511         }
00512 
00513         for (int iCnt2 = 0; iCnt2 < 80; iCnt2++)
00514         {
00515             if (iCnt2 >= 0 && iCnt2 <= 19)
00516             {
00517                 uiF = (uiB & uiC) | (~uiB & uiD);       /* defined in FIPS 180-1 */
00518                 uiK = 0x5A827999;                                               /* defined in FIPS 180-1 */
00519             }
00520             else if (iCnt2 >= 20 && iCnt2 <= 39)
00521             {
00522                 uiF = uiB ^ uiC ^ uiD;  /* defined in FIPS 180-1 */
00523                 uiK = 0x6ED9EBA1;                       /* defined in FIPS 180-1 */
00524             }
00525             else if (iCnt2 >= 40 && iCnt2 <= 59)
00526             {
00527                 uiF = (uiB & uiC) | (uiB & uiD) | (uiC & uiD);  /* defined in FIPS 180-1 */
00528                 uiK = 0x8F1BBCDC;                                                                       /* defined in FIPS 180-1 */
00529             }
00530             else if (iCnt2 >= 60 && iCnt2 <= 79)
00531             {
00532                 uiF = uiB ^ uiC ^ uiD;  /* defined in FIPS 180-1 */
00533                 uiK = 0xCA62C1D6;                       /* defined in FIPS 180-1 */
00534             }
00535 
00536                         uiTmp = ((uiA << 5) | (uiA >> (32 - 5))) + uiF + uiE + uiK + uiW[iCnt2];
00537             uiE = uiD;
00538             uiD = uiC;
00539                         uiC = ((uiB << 30) | (uiB >> (32 - 30)));
00540             uiB = uiA;
00541             uiA = uiTmp;
00542         }
00543         }
00544 
00545         uiMAC[0] = uiE;
00546         uiMAC[1] = uiD;
00547         uiMAC[2] = uiC;
00548         uiMAC[3] = uiB;
00549         uiMAC[4] = uiA;
00550 
00551         return 0;
00552 }
00553 
00554 /***************************************************************************
00555   Begin                 :       24.09.2010 / M. Raner
00556 
00557   Version               :       03.02.05 / 24.09.2010 / M. Raner
00558 
00559   Function                      :       generates new random challenge
00560   
00561   Parameters            :       -
00562 
00563   Return value          :       0                               - if tramsmission was successful
00564                                                 status byte (>1)- if the reader signals a problem
00565                                                 error code (<0) - if something goes wrong
00566 ***************************************************************************/
00567 int FedmIscTagHandler_ISO15693_Maxim_MAX66140::UpdateChallenge()
00568 {
00569         for(int iCnt = 0; iCnt < 5; iCnt++)
00570         {
00571                 ucChallenge[iCnt] = (unsigned char)rand();
00572         }
00573         return 0;
00574 }
00575 
00576 /***************************************************************************
00577   Begin                 :       21.09.2010 / M. Raner
00578 
00579   Version               :       03.03.02 / 28.04.2011 / M. Raner, M. Hultsch
00580 
00581   Function                      :       [0xBF][0xA0] GenerateSecret
00582   
00583   Parameters            :       unsigned char ucRequestFlags    - [in]  request flags byte
00584                                                 unsigned char ucControlByte             - [in]  control code (load, compute, locking)
00585                                                 unsigned char* pucPartialSecret - [in]  secret to be load, used in computation
00586                                                 unsigned char& ucResponseFlags  - [out] response flags(if 0x00 then command execution was successful)
00587                                                 unsigned char& ucPageCRCStatus  - [out] status code of crc check
00588 
00589   Return value          :       0                               - if tramsmission was successful
00590                                                 status byte (>1)- if the reader signals a problem
00591                                                 error code (<0) - if something goes wrong
00592 ***************************************************************************/
00593 int FedmIscTagHandler_ISO15693_Maxim_MAX66140::GenerateSecret( 
00594         unsigned char ucRequestFlags,
00595         unsigned char ucControlByte,
00596         unsigned char* pucPartialSecret,
00597         unsigned char& ucResponseFlags,
00598         unsigned char& ucPageCRCStatus)
00599 {
00600         FEDM_CHK3(pucPartialSecret);
00601 
00602         //******************************************************************************
00603         // ParameterDescription
00604 
00605                 /*
00606                 request flags(size: 1byte)
00607                         bit:    function:                               info:
00608                         8               no function                             forced to 0
00609                         7               no function                             forced to 0
00610                         6               addressed flag                  addressed = 1, nonaddressed or selected = 0
00611                         5               selected flag                   selected = 1, (non)addressed = 0
00612                         4               no function                             forced to 0
00613                         3               no function                             forced to 0
00614                         2               datarate flag                   low datarate = 0, high datarate = 1
00615                         1               subcarrier flag                 single subcarrier = 0, two subcarriers = 1              
00616 
00617                 control byte(size: 1byte)
00618                         code:   function:                                                       info:
00619                         0xF0    load secret without locking                     
00620                         0x0F    load secret with locking                        secret write protection byte is changed to 0x0A
00621                         0xn5    compute secret without locking          n specifies the page number to be used in computation
00622                         0xnA    compute and lock secret                         WARNING: permanently write protect the secret, n specifies the page number to be used in computation
00623                         0xAA    load and lock secret                            WARNING: permanently write protect the secret
00624 
00625                 partial secret(size 8byte)
00626                         controlByte:            function:
00627                         0xF0, 0x0F, 0xAA        the new secret that will be loaded
00628                         0xn5, 0xnA                      partial secret to be used in computation of new secret
00629                 */
00630 
00631 
00632         //Check if request flags are addressed and selected
00633         if(((ucRequestFlags & 0x30) == 0x30))
00634         {
00635                 //Error: Flags = Addressed and Selected
00636                 return FEDM_ERROR_PARAMETER;
00637         }
00638 
00639         //Check ControlByte for valid values
00640         if(!(ucControlByte == 0xF0 || 
00641                 ucControlByte == 0x0F || 
00642                 (ucControlByte & 0x05) == 0x05 || 
00643                 (ucControlByte & 0x0A) == 0x0A ||
00644                 ucControlByte == 0xAA))
00645         {
00646                 //Error: Control byte is invalid
00647                 return FEDM_ERROR_PARAMETER;
00648         }
00649 
00650 
00651         //******************************************************************************        
00652         // Declaration
00653         int iLengthOfRequestData = 14;
00654         if((ucRequestFlags & 0x20) == 0x20)
00655         {
00656                 iLengthOfRequestData += 8;      //Addressed mode, length with uid
00657         }
00658 
00659         int iLengthOfResponseData;
00660 
00661 
00662         //******************************************************************************
00663         // RequestData
00664         if(ucControlByte == 0xF0)
00665         {
00666                 m_ucRequestData[0] = 0x02;
00667                 m_ucRequestData[1] = 0x12;
00668         }
00669         else if((ucControlByte & 0x05) == 0x05)
00670         {
00671                 m_ucRequestData[0] = 0x02;
00672                 m_ucRequestData[1] = 0x24;
00673         }
00674         else
00675         {
00676                 m_ucRequestData[0] = 0x02;      //command response delay time = 0x201f * 590ns = 320.9us
00677                 m_ucRequestData[1] = 0x1f;
00678         }
00679 
00680         m_ucRequestData[2] = ucRequestFlags & 0x33;     //important request flags
00681         m_ucRequestData[3] = 0xA0;                                      //command Code 
00682         m_ucRequestData[4] = 0x2B;                                      //MFG Code
00683 
00684         //if addressed mode then uid
00685         if((ucRequestFlags & 0x20) == 0x20)
00686         {
00687                 for(int iCnt = 0; iCnt < 8; iCnt++)
00688                 {
00689                         m_ucRequestData[12 - iCnt] = m_pTabItem->m_ucSnr[iCnt]; //uid
00690                 }
00691         }
00692 
00693         //control byte
00694         m_ucRequestData[iLengthOfRequestData - 9] = ucControlByte;
00695 
00696         //partial secret
00697         for(int iCnt = 0; iCnt < 8; iCnt++)
00698         {
00699                 m_ucRequestData[iLengthOfRequestData - 8 + iCnt] = pucPartialSecret[iCnt];      //new block data
00700         }
00701 
00702 
00703         //******************************************************************************
00704         // CommandExecute
00705         //execute command
00706         int iBack = FEISC_0xBF_ISOTranspCmd(    m_pReader->GetReaderHnd(), 
00707                                                                                         m_pReader->GetBusAddress(), 
00708                                                                                         0x02, 
00709                                                                                         0x20, 
00710                                                                                         m_ucRequestData, 
00711                                                                                         iLengthOfRequestData, 
00712                                                                                         m_ucResponseData, 
00713                                                                                         &iLengthOfResponseData, 
00714                                                                                         0);
00715         
00716         if(iBack == 0x95)
00717         {
00718                 m_ucISO15693ErrorCode = m_ucResponseData[0];
00719         }
00720         else if(iBack == 0)
00721         {
00722                 //******************************************************************************
00723                 // ResponseData
00724                 ucResponseFlags = m_ucResponseData[0];
00725                 if(ucResponseFlags == 0)
00726                 {
00727                         ucPageCRCStatus = m_ucResponseData[1];
00728                 }
00729         }
00730 
00731         return iBack;
00732 }
00733 
00734 /***************************************************************************
00735   Begin                 :       20.09.2010 / M. Raner
00736 
00737   Version               :       03.03.02 / 28.04.2011 / M. Raner, M. Hultsch
00738 
00739   Function                      :       [0xBF][0xA1] WriteBuffer
00740   
00741   Parameters            :       unsigned char   ucRequestFlags          - [in]  request flags byte
00742                                                 unsigned char   ucBlockNumber           - [in]  number of block(0x00 to 0x11)
00743                                                 unsigned char*  pucNewBlockData         - [in]  New block-data
00744                                                 unsigned char&  ucResponseFlags         - [out] response flags(if 0x00 then command execution was successful)
00745 
00746   Return value          :       0                               - if tramsmission was successful
00747                                                 status byte (>1)- if the reader signals a problem
00748                                                 error code (<0) - if something goes wrong
00749 ***************************************************************************/
00750 int FedmIscTagHandler_ISO15693_Maxim_MAX66140::WriteBuffer(
00751         unsigned char ucRequestFlags,
00752         unsigned char ucBlockNumber,
00753         unsigned char* pucNewBlockData,
00754         unsigned char& ucResponseFlags)
00755 {
00756         //******************************************************************************
00757         // ParameterDescription
00758 
00759                 /*
00760                         RequestFlags(size 1byte, important flags mask for WriteBuffer command 0x33)
00761                                 bits 8 and 4 have no function. They are both 0
00762                                 bit 7: option flag - used with block read commands to include the block security status in the response. In this case 0
00763                                 bit 6: specifies Addressed State. If this and bit5 are both 0 then state is non addressed.
00764                                 bit 5: specifies Selected State. If this and bit6 are both 0 then state is non addressed.
00765                                 bit 3: inventory flag - used with inventory Command only. For all other commands this bit must be 0.
00766                                 bit 2: datarate flag - low datarate = 0, high datarate = 1
00767                                 bit 1: subcarrier flag - single subcarrier = 0, two subcarriers = 1             
00768                 */
00769 
00770 
00771         //******************************************************************************
00772         // ParameterTesting
00773         //Test Pointer for NULL
00774         FEDM_CHK3(pucNewBlockData);
00775 
00776         if(((ucRequestFlags & 0x20) == 0x20) && ((ucRequestFlags & 0x10) == 0x10))
00777         {
00778                 //Error: Flags = Addressed and Selected
00779                 return FEDM_ERROR_PARAMETER;
00780         }
00781 
00782 
00783         //******************************************************************************
00784         // Declaration
00785         int iLengthOfRequestData = 14;
00786         if((ucRequestFlags & 0x20) == 0x20)
00787         {
00788                 iLengthOfRequestData += 8;      //Addressed mode, length with uid
00789         }
00790 
00791         int iLengthOfResponseData;
00792         
00793         //******************************************************************************
00794         // RequestData
00795         m_ucRequestData[0] = 0x02;      //command response delay time = 0x201f * 590ns = 320.9us
00796         m_ucRequestData[1] = 0x1f;
00797 
00798         m_ucRequestData[2] = ucRequestFlags & 0x33;     //important request flags
00799         m_ucRequestData[3] = 0xA1;                                      //command Code 
00800         m_ucRequestData[4] = 0x2B;                                      //MFG Code
00801 
00802         if(iLengthOfRequestData == 22)
00803         {
00804                 for(int iCnt = 0; iCnt < 8; iCnt++)
00805                 {
00806                         m_ucRequestData[12 - iCnt] = m_pTabItem->m_ucSnr[iCnt]; //uid
00807                 }
00808                 m_ucRequestData[13] = ucBlockNumber;                    //block number
00809         }
00810         else
00811         {
00812                 m_ucRequestData[5] = ucBlockNumber;                     //block number
00813         }
00814 
00815         for(int iCnt = 0; iCnt < 8; iCnt++)
00816         {
00817                 m_ucRequestData[iLengthOfRequestData - 8 + iCnt] = pucNewBlockData[iCnt];       //new block data
00818         }
00819 
00820 
00821         //******************************************************************************
00822         // CommandExecute
00823         int iBack = FEISC_0xBF_ISOTranspCmd(    m_pReader->GetReaderHnd(), 
00824                                                                                         m_pReader->GetBusAddress(), 
00825                                                                                         0x02, 
00826                                                                                         0x18, 
00827                                                                                         m_ucRequestData, 
00828                                                                                         iLengthOfRequestData, 
00829                                                                                         m_ucResponseData, 
00830                                                                                         &iLengthOfResponseData, 
00831                                                                                         0);
00832 
00833         if(iBack == 0x95)
00834         {
00835                 m_ucISO15693ErrorCode = m_ucResponseData[0];
00836         }
00837         else if(iBack == 0)
00838         {
00839                 //******************************************************************************
00840                 // ResponseData
00841                 ucResponseFlags = m_ucResponseData[0];  //response flags
00842                 if(ucResponseFlags == 0)
00843                 {
00844                         if(ucResponseFlags == 0x10)
00845                         {
00846                                 //0x10 invalid block number
00847                                 return FEDM_ERROR_UNKNOWN_MEM_ID;
00848                         }
00849                 }
00850         }
00851 
00852         return iBack;
00853 }
00854 
00855 /***************************************************************************
00856   Begin                 :       21.09.2010 / M. Raner
00857 
00858   Version               :       03.03.02 / 28.04.2011 / M. Raner, M. Hultsch
00859 
00860   Function                      :       [0xBF][0xA2] ReadBuffer
00861   
00862   Parameters            :       unsigned char   ucRequestFlags                  - [in]  request flags byte
00863                                                 unsigned char&  ucResponseFlags                 - [out] response flags(if 0x00 then command execution was successful)
00864                                                 unsigned char&  ucBlockNumber                   - [out] number of block
00865                                                 unsigned char*  pucReadWriteBufferData  - [out] data of read/write Buffer
00866 
00867   Return value          :       0                               - if tramsmission was successful
00868                                                 status byte (>1)- if the reader signals a problem
00869                                                 error code (<0) - if something goes wrong
00870 ***************************************************************************/
00871 int FedmIscTagHandler_ISO15693_Maxim_MAX66140::ReadBuffer(
00872         unsigned char ucRequestFlags,
00873         unsigned char& ucResponseFlags,
00874         unsigned char& ucBlockNumber,
00875         unsigned char* pucReadWriteBufferData)
00876 {
00877         //******************************************************************************
00878         // ParameterDescription
00879 
00880                 /*
00881                         RequestFlags(size 1byte, important flags mask for ReadBuffer command 0x33)
00882                                 bits 8 and 4 have no function. They are both 0
00883                                 bit 7: option flag - used with block read commands to include the block security status in the response. In this case 0
00884                                 bit 6: specifies Addressed State. If this and bit5 are both 0 then state is non addressed.
00885                                 bit 5: specifies Selected State. If this and bit6 are both 0 then state is non addressed.
00886                                 bit 3: inventory flag - used with inventory Command only. For all other commands this bit must be 0.
00887                                 bit 2: datarate flag - low datarate = 0, high datarate = 1
00888                                 bit 1: subcarrier flag - single subcarrier = 0, two subcarriers = 1             
00889                 */
00890 
00891         //******************************************************************************
00892         // ParameterTesting
00893         //Test Pointer for NULL
00894         FEDM_CHK3(pucReadWriteBufferData);
00895 
00896         if(((ucRequestFlags & 0x20) == 0x20) && ((ucRequestFlags & 0x10) == 0x10))
00897         {
00898                 //Error: Flags = Addressed and Selected
00899                 return FEDM_ERROR_PARAMETER;
00900         }
00901 
00902 
00903         //******************************************************************************
00904         // Declaration
00905         int iLengthOfRequestData = 5;
00906         if((ucRequestFlags & 0x20) == 0x20)
00907         {
00908                 iLengthOfRequestData += 8;      //Addressed mode, length with uid
00909         }
00910 
00911         int iLengthOfResponseData;
00912 
00913 
00914         //******************************************************************************
00915         // RequestData
00916         m_ucRequestData[0] = 0x02;      //command response delay time = 0x201f * 590ns = 320.9us
00917         m_ucRequestData[1] = 0x1f;
00918 
00919         m_ucRequestData[2] = ucRequestFlags & 0x33;     //important request flags
00920         m_ucRequestData[3] = 0xA2;                                      //command Code 
00921         m_ucRequestData[4] = 0x2B;                                      //MFG Code
00922 
00923         if(iLengthOfRequestData == 13)
00924         {
00925                 for(int iCnt = 0; iCnt < 8; iCnt++)
00926                 {
00927                         m_ucRequestData[12 - iCnt] = m_pTabItem->m_ucSnr[iCnt]; //uid
00928                 }
00929         }
00930 
00931         //******************************************************************************
00932         // CommandExecute
00933         int iBack = FEISC_0xBF_ISOTranspCmd(    m_pReader->GetReaderHnd(), 
00934                                                                                         m_pReader->GetBusAddress(), 
00935                                                                                         0x02, 
00936                                                                                         0x60, 
00937                                                                                         m_ucRequestData, 
00938                                                                                         iLengthOfRequestData, 
00939                                                                                         m_ucResponseData, 
00940                                                                                         &iLengthOfResponseData, 
00941                                                                                         0);
00942         
00943 
00944         if(iBack == 0x95)
00945         {
00946                 m_ucISO15693ErrorCode = m_ucResponseData[0];
00947         }
00948         else if(iBack == 0)
00949         {
00950                 //******************************************************************************
00951                 // ResponseData
00952                 ucResponseFlags = m_ucResponseData[0];  //response flags
00953                 if(ucResponseFlags == 0)
00954                 {
00955                         if(ucResponseFlags == 0xA3)
00956                         {
00957                                 //0xA3 no valid data in read/write buffer
00958                                 return FEDM_ERROR_NO_DATA;
00959                         }
00960                         ucBlockNumber = m_ucResponseData[1];    //block number
00961                         for(int iCnt = 0; iCnt < 8; iCnt++)
00962                         {
00963                                 pucReadWriteBufferData[iCnt] = m_ucResponseData[2+iCnt];        //read write buffer
00964                         }
00965                 }
00966         }
00967 
00968         return iBack;
00969 }
00970 
00971 /***************************************************************************
00972   Begin                 :       22.09.2010 / M. Raner
00973 
00974   Version               :       03.03.02 / 28.04.2011 / M. Raner, M. Hultsch
00975 
00976   Function                      :       [0xBF][0xA3] CopyBuffer
00977   
00978   Parameters            :       unsigned char ucRequestFlags    - [in]  request flags byte
00979                                                 unsigned char ucBlockNumber             - [in]  number of block(0x00 to 0x12)
00980                                                 unsigned char* pucMAC                   - [in]  CopyBufferMAC from UserComputeCopyBufferMAC()
00981                                                 unsigned char& ucResponseFlags  - [out] response flags(if 0x00 then command execution was successful)
00982                                                 unsigned char& ucPageCRCStatus  - [out] status code of crc check
00983 
00984   Return value          :       0                               - if tramsmission was successful
00985                                                 status byte (>1)- if the reader signals a problem
00986                                                 error code (<0) - if something goes wrong
00987 ***************************************************************************/
00988 int FedmIscTagHandler_ISO15693_Maxim_MAX66140::CopyBuffer(
00989         unsigned char ucRequestFlags,
00990         unsigned char ucBlockNumber,
00991         unsigned char* pucMAC,
00992         unsigned char& ucResponseFlags,
00993         unsigned char& ucPageCRCStatus)
00994 {
00995         //******************************************************************************
00996         // ParameterTesting
00997         //Test Pointer for NULL
00998         FEDM_CHK3(pucMAC);
00999 
01000         //Check if request flags are addressed and selected
01001         if(((ucRequestFlags & 0x30) == 0x30))
01002         {
01003                 //Error: Flags = Addressed and Selected
01004                 return FEDM_ERROR_PARAMETER;
01005         }
01006 
01007         //Check BlockNumber for valid values
01008         if(!(ucBlockNumber >= 0x00 && ucBlockNumber <= 0x11))
01009         {
01010                 //Error: block number not valid
01011                 return FEDM_ERROR_PARAMETER;
01012         }
01013 
01014 
01015         //******************************************************************************
01016         // Declaration
01017         int iLengthOfRequestData = 26;
01018         if((ucRequestFlags & 0x20) == 0x20)
01019         {
01020                 iLengthOfRequestData += 8;      //Addressed mode, length with uid
01021         }
01022 
01023         int iLengthOfResponseData = 0;
01024 
01025 
01026         //******************************************************************************
01027         // RequestData
01028         m_ucRequestData[0] = 0x02;      //command response delay time = 0x201f * 590ns = 320.9us
01029         m_ucRequestData[1] = 0x1f;
01030 
01031         m_ucRequestData[2] = ucRequestFlags & 0x33;     //important request flags
01032         m_ucRequestData[3] = 0xA3;                                      //command Code 
01033         m_ucRequestData[4] = 0x2B;                                      //MFG Code
01034 
01035         //if addressed mode then uid
01036         if((ucRequestFlags & 0x20) == 0x20)
01037         {
01038                 for(int iCnt = 0; iCnt < 8; iCnt++)
01039                 {
01040                         m_ucRequestData[12 - iCnt] = m_pTabItem->m_ucSnr[iCnt]; //uid
01041                 }
01042         }
01043 
01044         //block number
01045         m_ucRequestData[iLengthOfRequestData - 21] = ucBlockNumber;
01046 
01047         //mac
01048         for(int iCnt = 0; iCnt < 20; iCnt++)
01049         {
01050                 m_ucRequestData[iLengthOfRequestData - 20 + iCnt] = pucMAC[iCnt];
01051         }
01052 
01053 
01054         //******************************************************************************
01055         // CommandExecute
01056         int iBack = FEISC_0xBF_ISOTranspCmd(    m_pReader->GetReaderHnd(), 
01057                                                                                         m_pReader->GetBusAddress(), 
01058                                                                                         0x02, 
01059                                                                                         0x20, 
01060                                                                                         m_ucRequestData, 
01061                                                                                         iLengthOfRequestData, 
01062                                                                                         m_ucResponseData, 
01063                                                                                         &iLengthOfResponseData, 
01064                                                                                         0);
01065 
01066 
01067         if(iBack == 0x95)
01068         {
01069                 m_ucISO15693ErrorCode = m_ucResponseData[0];
01070         }
01071         else if(iBack == 0)
01072         {
01073                 //******************************************************************************
01074                 // ResponseData
01075                 ucResponseFlags = m_ucResponseData[0];
01076                 if(ucResponseFlags == 0)
01077                 {
01078                         ucPageCRCStatus = m_ucResponseData[1];
01079                 }
01080         }
01081 
01082         return iBack;
01083 }
01084 
01085 /***************************************************************************
01086   Begin                 :       21.09.2010 / M. Raner
01087 
01088   Version               :       03.03.02 / 28.04.2011 / M. Raner, M. Hultsch
01089 
01090   Function                      :       [0xBF][0x20] ReadSingleBlock
01091   
01092   Parameters            :       unsigned char ucRequestFlags            - [in]  request flags byte
01093                                                 unsigned char ucBlockNumber                     - [in]  number of block(0x00 to 0x12)
01094                                                 unsigned char& ucResponseFlags          - [out] response flags(if 0x00 then command execution was successful)
01095                                                 unsigned char& ucSecurityStatus         - [out] security status code
01096                                                 unsigned char* pucMemoryData            - [out] readed memory data
01097 
01098   Return value          :       0                               - if tramsmission was successful
01099                                                 status byte (>1)- if the reader signals a problem
01100                                                 error code (<0) - if something goes wrong
01101 ***************************************************************************/
01102 int FedmIscTagHandler_ISO15693_Maxim_MAX66140::ReadSingleBlock(
01103         unsigned char ucRequestFlags,
01104         unsigned char ucBlockNumber,
01105         unsigned char& ucResponseFlags,
01106         unsigned char& ucSecurityStatus, /* <<<<<<<<<<<< Optional */
01107         unsigned char* pucMemoryData)
01108 {
01109         //******************************************************************************
01110         // ParameterTesting
01111         //Test Pointer for NULL
01112         FEDM_CHK3(pucMemoryData);
01113 
01114         //Check if request flags are addressed and selected
01115         if(((ucRequestFlags & 0x30) == 0x30))
01116         {
01117                 //Error: Flags = Addressed and Selected
01118                 return FEDM_ERROR_PARAMETER;
01119         }
01120 
01121         //Check BlockNumber for valid values
01122         if(!(ucBlockNumber >= 0x00 && ucBlockNumber <= 0x12))
01123         {
01124                 //Error: block number not valid
01125                 return FEDM_ERROR_PARAMETER;
01126         }
01127 
01128 
01129         //******************************************************************************
01130         // Declaration
01131         int iLengthOfRequestData = 5;
01132         if((ucRequestFlags & 0x20) == 0x20)
01133         {
01134                 iLengthOfRequestData += 8;      //Addressed mode, length with uid
01135         }
01136 
01137         int iLengthOfResponseData = 0;
01138         int iResponseDataOffset = 0;
01139 
01140 
01141         //******************************************************************************
01142         // RequestData
01143         m_ucRequestData[0] = 0x02;      //command response delay time = 0x201f * 590ns = 320.9us
01144         m_ucRequestData[1] = 0x1f;
01145 
01146         m_ucRequestData[2] = ucRequestFlags & 0x73;     //important request flags
01147         m_ucRequestData[3] = 0x20;                                      //command Code 
01148 
01149         //if addressed mode then uid
01150         if((ucRequestFlags & 0x20) == 0x20)
01151         {
01152                 for(int iCnt = 0; iCnt < 8; iCnt++)
01153                 {
01154                         m_ucRequestData[11 - iCnt] = m_pTabItem->m_ucSnr[iCnt]; //uid
01155                 }
01156         }
01157 
01158         //block number
01159         m_ucRequestData[iLengthOfRequestData - 1] = ucBlockNumber;
01160 
01161 
01162         //******************************************************************************
01163         // CommandExecute
01164         int iBack = FEISC_0xBF_ISOTranspCmd(    m_pReader->GetReaderHnd(), 
01165                                                                                         m_pReader->GetBusAddress(), 
01166                                                                                         0x02, 
01167                                                                                         0x58, 
01168                                                                                         m_ucRequestData, 
01169                                                                                         iLengthOfRequestData, 
01170                                                                                         m_ucResponseData, 
01171                                                                                         &iLengthOfResponseData, 
01172                                                                                         0);
01173 
01174 
01175         if(iBack == 0x95)
01176         {
01177                 m_ucISO15693ErrorCode = m_ucResponseData[0];
01178         }
01179         else if(iBack == 0)
01180         {
01181                 //******************************************************************************
01182                 // ResponseData
01183                 ucResponseFlags = m_ucResponseData[0];
01184                 if(ucResponseFlags == 0)
01185                 {
01186                         if((ucRequestFlags & 0x40) == 0x40)
01187                         {
01188                                 ucSecurityStatus = m_ucResponseData[1];
01189                                 iResponseDataOffset++;
01190                         }
01191                         for(int iCnt = 0; iCnt < 8; iCnt++)
01192                         {
01193                                 pucMemoryData[iCnt] = m_ucResponseData[1 + iResponseDataOffset +iCnt];
01194                         }
01195                 }
01196         }
01197 
01198         return iBack;
01199 }
01200 
01201 /***************************************************************************
01202   Begin                 :       21.09.2010 / M. Raner
01203 
01204   Version               :       03.03.02 / 28.04.2011 / M. Raner, M. Hultsch
01205 
01206   Function                      :       [0xBF][0xA4] CustomReadBlock
01207   
01208   Parameters            :       unsigned char ucRequestFlags            - [in]  request flags byte
01209                                                 unsigned char ucBlockNumber                     - [in]  number of block(0x00 to 0x12)
01210                                                 unsigned char& ucResponseFlags          - [out] response flags(if 0x00 then command execution was successful)
01211                                                 unsigned char& ucSecurityStatus         - [out] security status code
01212                                                 unsigned char* pucMemoryData            - [out] readed memory data
01213                                                 unsigned char* pucIntegrityBytes        - [out] integrity bytes(write counter) of datablock
01214 
01215   Return value          :       0                               - if tramsmission was successful
01216                                                 status byte (>1)- if the reader signals a problem
01217                                                 error code (<0) - if something goes wrong
01218 ***************************************************************************/
01219 int FedmIscTagHandler_ISO15693_Maxim_MAX66140::CustomReadBlock(
01220         unsigned char ucRequestFlags,
01221         unsigned char ucBlockNumber,
01222         unsigned char& ucResponseFlags,
01223         unsigned char& ucSecurityStatus, /* <<<<<<<<<<<< Optional */
01224         unsigned char* pucMemoryData,
01225         unsigned char* pucIntegrityBytes)
01226 {
01227         //******************************************************************************
01228         // ParameterTesting
01229         //Test Pointer for NULL
01230         FEDM_CHK3(pucMemoryData);
01231         FEDM_CHK3(pucIntegrityBytes);
01232 
01233         //Check if request flags are addressed and selected
01234         if(((ucRequestFlags & 0x30) == 0x30))
01235         {
01236                 //Error: Flags = Addressed and Selected
01237                 return FEDM_ERROR_PARAMETER;
01238         }
01239 
01240         //Check BlockNumber for valid values
01241         if(!(ucBlockNumber >= 0x00 && ucBlockNumber <= 0x12))
01242         {
01243                 //Error: block number not valid
01244                 return FEDM_ERROR_PARAMETER;
01245         }
01246 
01247 
01248         //******************************************************************************
01249         // Declaration
01250         int iLengthOfRequestData = 6;
01251         if((ucRequestFlags & 0x20) == 0x20)
01252         {
01253                 iLengthOfRequestData += 8;      //Addressed mode, length with uid
01254         }
01255 
01256         int iLengthOfResponseData = 0;
01257         int iResponseDataOffset = 0;
01258 
01259 
01260         //******************************************************************************
01261         // RequestData
01262         m_ucRequestData[0] = 0x02;      //command response delay time = 0x201f * 590ns = 320.9us
01263         m_ucRequestData[1] = 0x1f;
01264 
01265         m_ucRequestData[2] = ucRequestFlags & 0x73;     //important request flags
01266         m_ucRequestData[3] = 0xA4;                                      //command Code 
01267         m_ucRequestData[4] = 0x2B;                                      //MFG Code
01268 
01269         //if addressed mode then uid
01270         if((ucRequestFlags & 0x20) == 0x20)
01271         {
01272                 for(int iCnt = 0; iCnt < 8; iCnt++)
01273                 {
01274                         m_ucRequestData[12 - iCnt] = m_pTabItem->m_ucSnr[iCnt]; //uid
01275                 }
01276         }
01277 
01278         //block number
01279         m_ucRequestData[iLengthOfRequestData - 1] = ucBlockNumber;
01280 
01281 
01282         //******************************************************************************
01283         // CommandExecute
01284         int iBack = FEISC_0xBF_ISOTranspCmd(    m_pReader->GetReaderHnd(), 
01285                                                                                         m_pReader->GetBusAddress(), 
01286                                                                                         0x01, 
01287                                                                                         0x68, 
01288                                                                                         m_ucRequestData, 
01289                                                                                         iLengthOfRequestData, 
01290                                                                                         m_ucResponseData, 
01291                                                                                         &iLengthOfResponseData, 
01292                                                                                         0);
01293 
01294 
01295         if(iBack == 0x95)
01296         {
01297                 m_ucISO15693ErrorCode = m_ucResponseData[0];
01298         }
01299         else if(iBack == 0)
01300         {
01301                 //******************************************************************************
01302                 // ResponseData
01303                 ucResponseFlags = m_ucResponseData[0];
01304                 if(ucResponseFlags == 0)
01305                 {
01306                         if((ucRequestFlags & 0x40) == 0x40)
01307                         {
01308                                 ucSecurityStatus = m_ucResponseData[1];
01309                                 iResponseDataOffset++;
01310                         }
01311                         for(int iCnt = 0; iCnt < 8; iCnt++)
01312                         {
01313                                 pucMemoryData[iCnt] = m_ucResponseData[1 + iResponseDataOffset +iCnt];
01314                         }
01315                         pucIntegrityBytes[0] = m_ucResponseData[9 + iResponseDataOffset];
01316                         pucIntegrityBytes[1] = m_ucResponseData[10 + iResponseDataOffset];
01317                 }
01318         }
01319 
01320         return iBack;
01321 }
01322 
01323 /***************************************************************************
01324   Begin                 :       22.09.2010 / M. Raner
01325 
01326   Version               :       03.03.02 / 28.04.2011 / M. Raner, M. Hultsch
01327 
01328   Function                      :       [0xBF][0xA5] ComputePageMAC
01329   
01330   Parameters            :       unsigned char ucRequestFlags            - [in]  request flags byte
01331                                                 unsigned char ucPageNumber                      - [in]  number of page will involve in computation
01332                                                 unsigned char& ucResponseFlags          - [out] response flags(if 0x00 then command execution was successful)
01333                                                 unsigned char& ucPageCRCStatus          - [out] status code of crc check
01334                                                 unsigned char* pucMAC                           - [out] from transponder computed PageMAC
01335 
01336   Return value          :       0                               - if tramsmission was successful
01337                                                 status byte (>1)- if the reader signals a problem
01338                                                 error code (<0) - if something goes wrong
01339 ***************************************************************************/
01340 int FedmIscTagHandler_ISO15693_Maxim_MAX66140::ComputePageMAC(
01341         unsigned char ucRequestFlags,
01342         unsigned char ucPageNumber,
01343         unsigned char& ucResponseFlags,
01344         unsigned char& ucPageCRCStatus,
01345         unsigned char* pucMAC)
01346 {
01347         //******************************************************************************
01348         // ParameterTesting
01349         //Test Pointer for NULL
01350         FEDM_CHK3(pucMAC);
01351 
01352         int iCnt = 0;
01353 
01354         //Check if request flags are addressed and selected
01355         if(((ucRequestFlags & 0x30) == 0x30))
01356         {
01357                 //Error: Flags = Addressed and Selected
01358                 return FEDM_ERROR_PARAMETER;
01359         }
01360 
01361         //Check PageNumber for valid values
01362         if(!(ucPageNumber >= 0x00 && ucPageNumber <= 0x04))
01363         {
01364                 //Error: block number not valid
01365                 return FEDM_ERROR_PARAMETER;
01366         }
01367 
01368 
01369         //******************************************************************************
01370         // Declaration
01371         int iLengthOfRequestData = 11;
01372         if((ucRequestFlags & 0x20) == 0x20)
01373         {
01374                 iLengthOfRequestData += 8;      //Addressed mode, length with uid
01375         }
01376 
01377         int iLengthOfResponseData = 0;
01378 
01379         //******************************************************************************
01380         // RequestData
01381         m_ucRequestData[0] = 0x02;      //command response delay time = 0x201f * 590ns = 320.9us
01382         //if selected then change response time
01383         if(m_pTabItem->m_bIsSelected)
01384         {
01385                 m_ucRequestData[1] = 0x41;
01386         }
01387         else
01388         {
01389                 m_ucRequestData[1] = 0x45;
01390         }
01391 
01392         m_ucRequestData[2] = ucRequestFlags & 0x33;     //important request flags
01393         m_ucRequestData[3] = 0xA5;                                      //command Code 
01394         m_ucRequestData[4] = 0x2B;                                      //MFG Code
01395 
01396 
01397         //if addressed mode then uid
01398         if((ucRequestFlags & 0x20) == 0x20)
01399         {
01400                 for(iCnt = 0; iCnt < 8; iCnt++)
01401                 {
01402                         m_ucRequestData[12 - iCnt] = m_pTabItem->m_ucSnr[iCnt]; //uid
01403                 }
01404         }
01405 
01406         //page number
01407         m_ucRequestData[iLengthOfRequestData - 6] = ucPageNumber;
01408 
01409         //challenge
01410         for(iCnt = 0; iCnt < 5; iCnt++)
01411         {
01412                 m_ucRequestData[iLengthOfRequestData - 5 + iCnt] = ucChallenge[iCnt];
01413         }
01414 
01415         //******************************************************************************
01416         // CommandExecute
01417         int iBack = FEISC_0xBF_ISOTranspCmd(    m_pReader->GetReaderHnd(), 
01418                                                                                         m_pReader->GetBusAddress(), 
01419                                                                                         0x02, 
01420                                                                                         0xC0, 
01421                                                                                         m_ucRequestData, 
01422                                                                                         iLengthOfRequestData, 
01423                                                                                         m_ucResponseData, 
01424                                                                                         &iLengthOfResponseData, 
01425                                                                                         0);
01426 
01427 
01428         if(iBack == 0x95)
01429         {
01430                 m_ucISO15693ErrorCode = m_ucResponseData[0];
01431         }
01432         else if(iBack == 0)
01433         {
01434                 //******************************************************************************
01435                 // ResponseData
01436                 ucResponseFlags = m_ucResponseData[0];
01437                 if(ucResponseFlags == 0)
01438                 {
01439                         ucPageCRCStatus = m_ucResponseData[1];
01440                         for(iCnt = 0; iCnt < 20; iCnt++)
01441                         {
01442                                 pucMAC[iCnt] = m_ucResponseData[2+iCnt];
01443                         }
01444                 }
01445         }
01446 
01447         return iBack;
01448 }
01449 
01450 #endif // #if !defined(_FEDM_NO_TAG_HANDLER_ISO15693)


maggie_rfid_drivers
Author(s): Raul Perula-Martinez
autogenerated on Mon Sep 14 2015 03:05:30