nfc.c

Go to the documentation of this file.
00001 /*-
00002  * Public platform independent Near Field Communication (NFC) library
00003  * 
00004  * Copyright (C) 2009, Roel Verdult
00005  * 
00006  * This program is free software: you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by the
00008  * Free Software Foundation, either version 3 of the License, or (at your
00009  * option) any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00014  * more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018  */
00019  
00025 #ifdef HAVE_CONFIG_H
00026   #include "config.h"
00027 #endif // HAVE_CONFIG_H
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <stddef.h>
00032 #include <string.h>
00033 
00034 #include <nfc/nfc.h>
00035 
00036 #include "chips.h"
00037 #include "drivers.h"
00038 
00039 #include <nfc/nfc-messages.h>
00040 
00041 nfc_device_desc_t * nfc_pick_device (void);
00042 
00043 // PN53X configuration
00044 extern const byte_t pncmd_get_firmware_version       [  2];
00045 extern const byte_t pncmd_get_general_status         [  2];
00046 extern const byte_t pncmd_get_register               [  4];
00047 extern const byte_t pncmd_set_register               [  5];
00048 extern const byte_t pncmd_set_parameters             [  3];
00049 extern const byte_t pncmd_rf_configure               [ 14];
00050 
00051 // Reader
00052 extern const byte_t pncmd_initiator_list_passive        [264];
00053 extern const byte_t pncmd_initiator_jump_for_dep        [ 68];
00054 extern const byte_t pncmd_initiator_select              [  3];
00055 extern const byte_t pncmd_initiator_deselect            [  3];
00056 extern const byte_t pncmd_initiator_release             [  3];
00057 extern const byte_t pncmd_initiator_set_baud_rate       [  5];
00058 extern const byte_t pncmd_initiator_exchange_data       [265];
00059 extern const byte_t pncmd_initiator_exchange_raw_data   [266];
00060 extern const byte_t pncmd_initiator_auto_poll           [  5];
00061 
00062 // Target
00063 extern const byte_t pncmd_target_get_data            [  2];
00064 extern const byte_t pncmd_target_set_data            [264];
00065 extern const byte_t pncmd_target_init                [ 39];
00066 extern const byte_t pncmd_target_virtual_card        [  4];
00067 extern const byte_t pncmd_target_receive             [  2];
00068 extern const byte_t pncmd_target_send                [264];
00069 extern const byte_t pncmd_target_get_status          [  2];
00070 
00071 nfc_device_desc_t *
00072 nfc_pick_device (void)
00073 {
00074   uint32_t uiDriver;
00075   nfc_device_desc_t *nddRes;
00076 
00077   for (uiDriver=0; uiDriver<sizeof(drivers_callbacks_list)/sizeof(drivers_callbacks_list[0]); uiDriver++)
00078   {
00079     if (drivers_callbacks_list[uiDriver].pick_device != NULL)
00080     {
00081       nddRes = drivers_callbacks_list[uiDriver].pick_device ();
00082       if (nddRes != NULL) return nddRes;
00083     }
00084   }
00085 
00086   return NULL;
00087 }
00088 
00095 void
00096 nfc_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound)
00097 {
00098   uint32_t uiDriver;
00099   size_t szN;
00100 
00101   *pszDeviceFound = 0;
00102 
00103   for (uiDriver=0; uiDriver<sizeof(drivers_callbacks_list)/sizeof(drivers_callbacks_list[0]); uiDriver++)
00104   {
00105     if (drivers_callbacks_list[uiDriver].list_devices != NULL)
00106     {
00107       DBG("List avaible device using %s driver",drivers_callbacks_list[uiDriver].acDriver);
00108       szN = 0;
00109       if (drivers_callbacks_list[uiDriver].list_devices (pnddDevices + (*pszDeviceFound), szDevices - (*pszDeviceFound), &szN))
00110       {
00111         *pszDeviceFound += szN;
00112       }
00113     }
00114     else
00115     {
00116       DBG("No listing function avaible for %s driver",drivers_callbacks_list[uiDriver].acDriver);
00117     }
00118   }
00119 }
00120 
00134 nfc_device_t* nfc_connect(nfc_device_desc_t* pndd)
00135 {
00136   nfc_device_t* pnd = NULL;
00137   uint32_t uiDriver;
00138   byte_t abtFw[4];
00139   size_t szFwLen = sizeof(abtFw);
00140 
00141   // Search through the device list for an available device
00142   for (uiDriver=0; uiDriver<sizeof(drivers_callbacks_list)/sizeof(drivers_callbacks_list[0]); uiDriver++)
00143   {
00144     if(pndd == NULL) {
00145       // No device description specified: try to automatically claim a device
00146       if(drivers_callbacks_list[uiDriver].pick_device != NULL) {
00147         DBG("Autodetecting available devices using %s driver.", drivers_callbacks_list[uiDriver].acDriver);
00148         pndd = drivers_callbacks_list[uiDriver].pick_device ();
00149 
00150         if(pndd != NULL) {
00151           DBG("Auto-connecting to %s using %s driver", pndd->acDevice, drivers_callbacks_list[uiDriver].acDriver); 
00152           pnd = drivers_callbacks_list[uiDriver].connect(pndd);
00153           if(pnd == NULL) {
00154             DBG("No device available using %s driver",drivers_callbacks_list[uiDriver].acDriver);
00155             pndd = NULL;
00156           }
00157 
00158           free(pndd);
00159         }
00160       }
00161     } else {
00162       // Specific device is requested: using device description pndd
00163       if( 0 != strcmp(drivers_callbacks_list[uiDriver].acDriver, pndd->pcDriver ) )
00164       {
00165         DBG("Looking for %s, found %s... Skip it.", pndd->pcDriver, drivers_callbacks_list[uiDriver].acDriver);
00166         continue;
00167       } else {
00168         DBG("Looking for %s, found %s... Use it.", pndd->pcDriver, drivers_callbacks_list[uiDriver].acDriver);
00169         pnd = drivers_callbacks_list[uiDriver].connect(pndd);
00170       }
00171     }
00172 
00173     // Test if the connection was successful
00174     if (pnd != NULL)
00175     {
00176       DBG("[%s] has been claimed.", pnd->acName);
00177       // Great we have claimed a device
00178       pnd->pdc = &(drivers_callbacks_list[uiDriver]);
00179 
00180       // Try to retrieve PN53x chip revision
00181       // We can not use pn53x_transceive() because abtRx[0] gives no status info
00182       if (!pnd->pdc->transceive(pnd->nds,pncmd_get_firmware_version,2,abtFw,&szFwLen))
00183       {
00184         // Failed to get firmware revision??, whatever...let's disconnect and clean up and return err
00185         DBG("Failed to get firmware revision for: %s", pnd->acName);
00186         pnd->pdc->disconnect(pnd);
00187         return NULL;
00188       }
00189 
00190       // Add the firmware revision to the device name, PN531 gives 2 bytes info, but PN532 gives 4
00191       switch(pnd->nc)
00192       {
00193         case NC_PN531: sprintf(pnd->acName,"%s - PN531 v%d.%d",pnd->acName,abtFw[0],abtFw[1]); break;
00194         case NC_PN532: sprintf(pnd->acName,"%s - PN532 v%d.%d (0x%02x)",pnd->acName,abtFw[1],abtFw[2],abtFw[3]); break;
00195         case NC_PN533: sprintf(pnd->acName,"%s - PN533 v%d.%d (0x%02x)",pnd->acName,abtFw[1],abtFw[2],abtFw[3]); break;
00196       }
00197 
00198       // Reset the ending transmission bits register, it is unknown what the last tranmission used there
00199       if (!pn53x_set_reg(pnd,REG_CIU_BIT_FRAMING,SYMBOL_TX_LAST_BITS,0x00)) return NULL;
00200 
00201       // Make sure we reset the CRC and parity to chip handling.
00202       if (!nfc_configure(pnd,NDO_HANDLE_CRC,true)) return NULL;
00203       if (!nfc_configure(pnd,NDO_HANDLE_PARITY,true)) return NULL;
00204 
00205       // Deactivate the CRYPTO1 chiper, it may could cause problems when still active
00206       if (!nfc_configure(pnd,NDO_ACTIVATE_CRYPTO1,false)) return NULL;
00207 
00208       return pnd;
00209     } else {
00210       DBG("No device found using driver: %s", drivers_callbacks_list[uiDriver].acDriver);
00211     }
00212   }
00213   // To bad, no reader is ready to be claimed
00214   return NULL;
00215 }
00216 
00223 void nfc_disconnect(nfc_device_t* pnd)
00224 {
00225   // Release and deselect all active communications
00226   nfc_initiator_deselect_tag(pnd);
00227   // Disable RF field to avoid heating
00228   nfc_configure(pnd,NDO_ACTIVATE_FIELD,false);
00229   // Disconnect, clean up and release the device 
00230   pnd->pdc->disconnect(pnd);
00231 }
00232 
00244 bool nfc_configure(nfc_device_t* pnd, const nfc_device_option_t dco, const bool bEnable)
00245 {
00246   byte_t btValue;
00247   byte_t abtCmd[sizeof(pncmd_rf_configure)];
00248   memcpy(abtCmd,pncmd_rf_configure,sizeof(pncmd_rf_configure));
00249 
00250   // Make sure we are dealing with a active device
00251   if (!pnd->bActive) return false;
00252 
00253   switch(dco)
00254   {
00255     case NDO_HANDLE_CRC:
00256       // Enable or disable automatic receiving/sending of CRC bytes
00257       // TX and RX are both represented by the symbol 0x80
00258       btValue = (bEnable) ? 0x80 : 0x00;
00259       if (!pn53x_set_reg(pnd,REG_CIU_TX_MODE,SYMBOL_TX_CRC_ENABLE,btValue)) return false;
00260       if (!pn53x_set_reg(pnd,REG_CIU_RX_MODE,SYMBOL_RX_CRC_ENABLE,btValue)) return false;
00261       pnd->bCrc = bEnable;
00262     break;
00263 
00264     case NDO_HANDLE_PARITY:
00265       // Handle parity bit by PN53X chip or parse it as data bit
00266       btValue = (bEnable) ? 0x00 : SYMBOL_PARITY_DISABLE;
00267       if (!pn53x_set_reg(pnd,REG_CIU_MANUAL_RCV,SYMBOL_PARITY_DISABLE,btValue)) return false;
00268       pnd->bPar = bEnable;
00269     break;
00270 
00271     case NDO_ACTIVATE_FIELD:
00272       abtCmd[2] = RFCI_FIELD;
00273       abtCmd[3] = (bEnable) ? 1 : 0;
00274       // We can not use pn53x_transceive() because abtRx[0] gives no status info
00275       if (!pnd->pdc->transceive(pnd->nds,abtCmd,4,NULL,NULL)) return false;
00276     break;
00277 
00278     case NDO_ACTIVATE_CRYPTO1:
00279       btValue = (bEnable) ? SYMBOL_MF_CRYPTO1_ON : 0x00;
00280       if (!pn53x_set_reg(pnd,REG_CIU_STATUS2,SYMBOL_MF_CRYPTO1_ON,btValue)) return false;
00281     break;
00282 
00283     case NDO_INFINITE_SELECT:
00284       // Retry format: 0x00 means only 1 try, 0xff means infinite
00285       abtCmd[2] = RFCI_RETRY_SELECT;
00286       abtCmd[3] = (bEnable) ? 0xff : 0x00; // MxRtyATR, default: active = 0xff, passive = 0x02
00287       abtCmd[4] = (bEnable) ? 0xff : 0x00; // MxRtyPSL, default: 0x01
00288       abtCmd[5] = (bEnable) ? 0xff : 0x00; // MxRtyPassiveActivation, default: 0xff
00289       // We can not use pn53x_transceive() because abtRx[0] gives no status info
00290       if (!pnd->pdc->transceive(pnd->nds,abtCmd,6,NULL,NULL)) return false;
00291     break;
00292 
00293     case NDO_ACCEPT_INVALID_FRAMES:
00294       btValue = (bEnable) ? SYMBOL_RX_NO_ERROR : 0x00;
00295       if (!pn53x_set_reg(pnd,REG_CIU_RX_MODE,SYMBOL_RX_NO_ERROR,btValue)) return false;
00296     break;
00297 
00298     case NDO_ACCEPT_MULTIPLE_FRAMES:
00299       btValue = (bEnable) ? SYMBOL_RX_MULTIPLE : 0x00;
00300       if (!pn53x_set_reg(pnd,REG_CIU_RX_MODE,SYMBOL_RX_MULTIPLE,btValue)) return false;
00301     return true;
00302 
00303     break;
00304   }
00305 
00306   // When we reach this, the configuration is completed and succesful
00307   return true;
00308 }
00309 
00310 
00318 bool nfc_initiator_init(const nfc_device_t* pnd)
00319 {
00320   // Make sure we are dealing with a active device
00321   if (!pnd->bActive) return false;
00322 
00323   // Set the PN53X to force 100% ASK Modified miller decoding (default for 14443A cards)
00324   if (!pn53x_set_reg(pnd,REG_CIU_TX_AUTO,SYMBOL_FORCE_100_ASK,0x40)) return false;
00325 
00326   // Configure the PN53X to be an Initiator or Reader/Writer
00327   if (!pn53x_set_reg(pnd,REG_CIU_CONTROL,SYMBOL_INITIATOR,0x10)) return false;
00328 
00329   return true;
00330 }
00331 
00344 bool nfc_initiator_select_dep_target(const nfc_device_t* pnd, const nfc_modulation_t nmInitModulation, const byte_t* pbtPidData, const size_t szPidDataLen, const byte_t* pbtNFCID3i, const size_t szNFCID3iDataLen, const byte_t *pbtGbData, const size_t szGbDataLen, nfc_target_info_t* pnti)
00345 {
00346   byte_t abtRx[MAX_FRAME_LEN];
00347   size_t szRxLen;
00348   size_t offset;
00349   byte_t abtCmd[sizeof(pncmd_initiator_jump_for_dep)];
00350   memcpy(abtCmd,pncmd_initiator_jump_for_dep,sizeof(pncmd_initiator_jump_for_dep));
00351 
00352   if(nmInitModulation == NM_ACTIVE_DEP) {
00353     abtCmd[2] = 0x01; /* active DEP */
00354   }
00355   abtCmd[3] = 0x00; /* baud rate = 106kbps */
00356 
00357   offset = 5;
00358   if(pbtPidData && nmInitModulation != NM_ACTIVE_DEP) { /* can't have passive initiator data when using active mode */
00359     abtCmd[4] |= 0x01;
00360     memcpy(abtCmd+offset,pbtPidData,szPidDataLen);
00361     offset+= szPidDataLen;
00362   }
00363 
00364   if(pbtNFCID3i) {
00365     abtCmd[4] |= 0x02;
00366     memcpy(abtCmd+offset,pbtNFCID3i,szNFCID3iDataLen);
00367     offset+= szNFCID3iDataLen;
00368   }
00369 
00370   if(pbtGbData) {
00371     abtCmd[4] |= 0x04;
00372     memcpy(abtCmd+offset,pbtGbData,szGbDataLen);
00373     offset+= szGbDataLen;
00374   }
00375 
00376   // Try to find a target, call the transceive callback function of the current device
00377   if (!pn53x_transceive(pnd,abtCmd,5+szPidDataLen+szNFCID3iDataLen+szGbDataLen,abtRx,&szRxLen)) return false;
00378 
00379   // Make sure one target has been found, the PN53X returns 0x00 if none was available
00380   if (abtRx[1] != 1) return false;
00381 
00382   // Is a target info struct available
00383   if (pnti)
00384   {
00385     memcpy(pnti->ndi.NFCID3i,abtRx+2,10);
00386     pnti->ndi.btDID = abtRx[12];
00387     pnti->ndi.btBSt = abtRx[13];
00388     pnti->ndi.btBRt = abtRx[14];
00389   }
00390   return true;
00391 }
00392 
00405 bool nfc_initiator_select_tag(const nfc_device_t* pnd, const nfc_modulation_t nmInitModulation, const byte_t* pbtInitData, const size_t szInitDataLen, nfc_target_info_t* pnti)
00406 {
00407   byte_t abtInit[MAX_FRAME_LEN];
00408   size_t szInitLen;
00409   byte_t abtRx[MAX_FRAME_LEN];
00410   size_t szRxLen;
00411   byte_t abtCmd[sizeof(pncmd_initiator_list_passive)];
00412   memcpy(abtCmd,pncmd_initiator_list_passive,sizeof(pncmd_initiator_list_passive));
00413 
00414   // Make sure we are dealing with a active device
00415   if (!pnd->bActive) return false;
00416 
00417   abtCmd[2] = 1;  // MaxTg, we only want to select 1 tag at the time
00418   abtCmd[3] = nmInitModulation; // BrTy, the type of init modulation used for polling a passive tag
00419 
00420   switch(nmInitModulation)
00421   {
00422     case NM_ISO14443A_106:
00423       switch (szInitDataLen)
00424       {
00425         case 7:
00426           abtInit[0] = 0x88;
00427           memcpy(abtInit+1,pbtInitData,7);
00428           szInitLen = 8;
00429         break;
00430 
00431         case 10:
00432           abtInit[0] = 0x88;
00433           memcpy(abtInit+1,pbtInitData,3);
00434           abtInit[4] = 0x88;
00435           memcpy(abtInit+4,pbtInitData+3,7);
00436           szInitLen = 12;
00437         break;
00438 
00439         case 4:
00440         default:
00441           memcpy(abtInit,pbtInitData,szInitDataLen);
00442           szInitLen = szInitDataLen;
00443         break;
00444       }
00445     break;
00446 
00447     default:
00448       memcpy(abtInit,pbtInitData,szInitDataLen);
00449       szInitLen = szInitDataLen;
00450     break;
00451   }
00452 
00453   // Set the optional initiator data (used for Felica, ISO14443B, Topaz Polling or for ISO14443A selecting a specific UID).
00454   if (pbtInitData) memcpy(abtCmd+4,abtInit,szInitLen);
00455 
00456   // Try to find a tag, call the tranceive callback function of the current device
00457   szRxLen = MAX_FRAME_LEN;
00458   // We can not use pn53x_transceive() because abtRx[0] gives no status info
00459   if (!pnd->pdc->transceive(pnd->nds,abtCmd,4+szInitLen,abtRx,&szRxLen)) return false;
00460 
00461   // Make sure one tag has been found, the PN53X returns 0x00 if none was available
00462   if (abtRx[0] != 1) return false;
00463 
00464   // Is a tag info struct available
00465   if (pnti)
00466   {
00467     // Fill the tag info struct with the values corresponding to this init modulation
00468     switch(nmInitModulation)
00469     {
00470       case NM_ISO14443A_106:
00471         // Somehow they switched the lower and upper ATQA bytes around for the PN531 chipset
00472         if (pnd->nc == NC_PN531)
00473         {
00474           pnti->nai.abtAtqa[0] = abtRx[3];
00475           pnti->nai.abtAtqa[1] = abtRx[2];
00476         } else {
00477           memcpy(pnti->nai.abtAtqa,abtRx+2,2);
00478         }
00479         pnti->nai.btSak = abtRx[4];
00480         // Copy the NFCID1
00481         pnti->nai.szUidLen = abtRx[5];
00482         memcpy(pnti->nai.abtUid,abtRx+6,pnti->nai.szUidLen);
00483         // Did we received an optional ATS (Smardcard ATR)
00484         if (szRxLen > pnti->nai.szUidLen+6)
00485         {
00486           pnti->nai.szAtsLen = abtRx[pnti->nai.szUidLen+6];
00487           memcpy(pnti->nai.abtAts,abtRx+pnti->nai.szUidLen+6,pnti->nai.szAtsLen);
00488         } else {
00489           pnti->nai.szAtsLen = 0;
00490         }
00491       break;
00492 
00493       case NM_FELICA_212:
00494       case NM_FELICA_424:
00495         // Store the mandatory info
00496         pnti->nfi.szLen = abtRx[2];
00497         pnti->nfi.btResCode = abtRx[3];
00498         // Copy the NFCID2t
00499         memcpy(pnti->nfi.abtId,abtRx+4,8);
00500         // Copy the felica padding
00501         memcpy(pnti->nfi.abtPad,abtRx+12,8);
00502         // Test if the System code (SYST_CODE) is available
00503         if (szRxLen > 20)
00504         {
00505           memcpy(pnti->nfi.abtSysCode,abtRx+20,2);  
00506         }
00507       break;
00508 
00509       case NM_ISO14443B_106:
00510         // Store the mandatory info
00511         memcpy(pnti->nbi.abtAtqb,abtRx+2,12);
00512         // Ignore the 0x1D byte, and just store the 4 byte id
00513         memcpy(pnti->nbi.abtId,abtRx+15,4);
00514         pnti->nbi.btParam1 = abtRx[19];
00515         pnti->nbi.btParam2 = abtRx[20];
00516         pnti->nbi.btParam3 = abtRx[21];
00517         pnti->nbi.btParam4 = abtRx[22];
00518         // Test if the Higher layer (INF) is available
00519         if (szRxLen > 22)
00520         {
00521           pnti->nbi.szInfLen = abtRx[23];
00522           memcpy(pnti->nbi.abtInf,abtRx+24,pnti->nbi.szInfLen);
00523         } else {
00524           pnti->nbi.szInfLen = 0;
00525         }
00526       break;
00527 
00528       case NM_JEWEL_106:
00529         // Store the mandatory info
00530         memcpy(pnti->nji.btSensRes,abtRx+2,2);
00531         memcpy(pnti->nji.btId,abtRx+4,4);
00532       break;
00533 
00534       default:
00535         // Should not be possible, so whatever...
00536       break;
00537     }
00538   }
00539   return true;
00540 }
00541 
00550 bool nfc_initiator_deselect_tag(const nfc_device_t* pnd)
00551 {
00552   return (pn53x_transceive(pnd,pncmd_initiator_deselect,3,NULL,NULL));
00553 }
00554 
00568 bool nfc_initiator_transceive_bits(const nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxBits, const byte_t* pbtTxPar, byte_t* pbtRx, size_t* pszRxBits, byte_t* pbtRxPar)
00569 {
00570   byte_t abtRx[MAX_FRAME_LEN];
00571   size_t szRxLen;
00572   size_t szFrameBits = 0;
00573   size_t szFrameBytes = 0;
00574   uint8_t ui8Bits = 0;
00575   byte_t abtCmd[sizeof(pncmd_initiator_exchange_raw_data)];
00576   memcpy(abtCmd,pncmd_initiator_exchange_raw_data,sizeof(pncmd_initiator_exchange_raw_data));
00577 
00578   // Check if we should prepare the parity bits ourself
00579   if (!pnd->bPar)
00580   {
00581     // Convert data with parity to a frame
00582     pn53x_wrap_frame(pbtTx,szTxBits,pbtTxPar,abtCmd+2,&szFrameBits);
00583   } else {
00584     szFrameBits = szTxBits;
00585   }
00586 
00587   // Retrieve the leading bits
00588   ui8Bits = szFrameBits%8;
00589 
00590   // Get the amount of frame bytes + optional (1 byte if there are leading bits) 
00591   szFrameBytes = (szFrameBits/8)+((ui8Bits==0)?0:1);
00592 
00593   // When the parity is handled before us, we just copy the data
00594   if (pnd->bPar) memcpy(abtCmd+2,pbtTx,szFrameBytes);
00595 
00596   // Set the amount of transmission bits in the PN53X chip register
00597   if (!pn53x_set_tx_bits(pnd,ui8Bits)) return false;
00598 
00599   // Send the frame to the PN53X chip and get the answer
00600   // We have to give the amount of bytes + (the two command bytes 0xD4, 0x42)
00601   if (!pn53x_transceive(pnd,abtCmd,szFrameBytes+2,abtRx,&szRxLen)) return false;
00602 
00603   // Get the last bit-count that is stored in the received byte 
00604   ui8Bits = pn53x_get_reg(pnd,REG_CIU_CONTROL) & SYMBOL_RX_LAST_BITS;
00605 
00606   // Recover the real frame length in bits
00607   szFrameBits = ((szRxLen-1-((ui8Bits==0)?0:1))*8)+ui8Bits;
00608 
00609   // Ignore the status byte from the PN53X here, it was checked earlier in pn53x_transceive()
00610   // Check if we should recover the parity bits ourself
00611   if (!pnd->bPar)
00612   {
00613     // Unwrap the response frame
00614     pn53x_unwrap_frame(abtRx+1,szFrameBits,pbtRx,pszRxBits,pbtRxPar);
00615   } else {
00616     // Save the received bits
00617     *pszRxBits = szFrameBits;
00618     // Copy the received bytes
00619     memcpy(pbtRx,abtRx+1,szRxLen-1);
00620   }
00621 
00622   // Everything went successful
00623   return true;
00624 }
00625 
00632 bool nfc_initiator_transceive_dep_bytes(const nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen)
00633 {
00634   byte_t abtRx[MAX_FRAME_LEN];
00635   size_t szRxLen;
00636   byte_t abtCmd[sizeof(pncmd_initiator_exchange_data)];
00637   memcpy(abtCmd,pncmd_initiator_exchange_data,sizeof(pncmd_initiator_exchange_data));
00638 
00639   // We can not just send bytes without parity if while the PN53X expects we handled them
00640   if (!pnd->bPar) return false;
00641 
00642   // Copy the data into the command frame
00643   abtCmd[2] = 1; /* target number */
00644   memcpy(abtCmd+3,pbtTx,szTxLen);
00645 
00646   // To transfer command frames bytes we can not have any leading bits, reset this to zero
00647   if (!pn53x_set_tx_bits(pnd,0)) return false;
00648 
00649   // Send the frame to the PN53X chip and get the answer
00650   // We have to give the amount of bytes + (the two command bytes 0xD4, 0x42)
00651   if (!pn53x_transceive(pnd,abtCmd,szTxLen+3,abtRx,&szRxLen)) return false;
00652 
00653   // Save the received byte count
00654   *pszRxLen = szRxLen-1;
00655 
00656   // Copy the received bytes
00657   memcpy(pbtRx,abtRx+1,*pszRxLen);
00658 
00659   // Everything went successful
00660   return true;
00661 }
00662 
00670 bool nfc_initiator_transceive_bytes(const nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen)
00671 {
00672   byte_t abtRx[MAX_FRAME_LEN];
00673   size_t szRxLen;
00674   byte_t abtCmd[sizeof(pncmd_initiator_exchange_raw_data)];
00675   memcpy(abtCmd,pncmd_initiator_exchange_raw_data,sizeof(pncmd_initiator_exchange_raw_data));
00676 
00677   // We can not just send bytes without parity if while the PN53X expects we handled them
00678   if (!pnd->bPar) return false;
00679 
00680   // Copy the data into the command frame
00681   memcpy(abtCmd+2,pbtTx,szTxLen);
00682 
00683   // To transfer command frames bytes we can not have any leading bits, reset this to zero
00684   if (!pn53x_set_tx_bits(pnd,0)) return false;
00685 
00686   // Send the frame to the PN53X chip and get the answer
00687   // We have to give the amount of bytes + (the two command bytes 0xD4, 0x42)
00688   if (!pn53x_transceive(pnd,abtCmd,szTxLen+2,abtRx,&szRxLen)) return false;
00689 
00690   // Save the received byte count
00691   *pszRxLen = szRxLen-1;
00692 
00693   // Copy the received bytes
00694   memcpy(pbtRx,abtRx+1,*pszRxLen);
00695 
00696   // Everything went successful
00697   return true;
00698 }
00699 
00710 bool nfc_initiator_mifare_cmd(const nfc_device_t* pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param* pmp)
00711 {
00712   byte_t abtRx[MAX_FRAME_LEN];
00713   size_t szRxLen;
00714   size_t szParamLen;
00715   byte_t abtCmd[sizeof(pncmd_initiator_exchange_data)];
00716   memcpy(abtCmd,pncmd_initiator_exchange_data,sizeof(pncmd_initiator_exchange_data));
00717 
00718   // Make sure we are dealing with a active device
00719   if (!pnd->bActive) return false;
00720 
00721   abtCmd[2] = 0x01;     // Use first target/card
00722   abtCmd[3] = mc;       // The MIFARE Classic command
00723   abtCmd[4] = ui8Block; // The block address (1K=0x00..0x39, 4K=0x00..0xff)
00724 
00725   switch (mc)
00726   {
00727     // Read and store command have no parameter
00728     case MC_READ:
00729     case MC_STORE:
00730       szParamLen = 0;
00731     break;
00732 
00733     // Authenticate command
00734     case MC_AUTH_A:
00735     case MC_AUTH_B:
00736       szParamLen = sizeof(mifare_param_auth);
00737     break;
00738 
00739     // Data command
00740     case MC_WRITE:
00741       szParamLen = sizeof(mifare_param_data);
00742     break;
00743 
00744     // Value command
00745     case MC_DECREMENT:
00746     case MC_INCREMENT:
00747     case MC_TRANSFER:
00748       szParamLen = sizeof(mifare_param_value);
00749     break;
00750 
00751     // Please fix your code, you never should reach this statement
00752     default:
00753       return false;
00754     break;
00755   }
00756 
00757   // When available, copy the parameter bytes
00758   if (szParamLen) memcpy(abtCmd+5,(byte_t*)pmp,szParamLen);
00759 
00760   // Fire the mifare command
00761   if (!pn53x_transceive(pnd,abtCmd,5+szParamLen,abtRx,&szRxLen)) return false;
00762 
00763   // When we have executed a read command, copy the received bytes into the param
00764   if (mc == MC_READ && szRxLen == 17) memcpy(pmp->mpd.abtData,abtRx+1,16);
00765 
00766   // Command succesfully executed
00767   return true;
00768 }
00769 
00778 bool nfc_target_init(const nfc_device_t* pnd, byte_t* pbtRx, size_t* pszRxBits)
00779 {
00780   byte_t abtRx[MAX_FRAME_LEN];
00781   size_t szRxLen;
00782   uint8_t ui8Bits;
00783   // Save the current configuration settings
00784   bool bCrc = pnd->bCrc;
00785   bool bPar = pnd->bPar;
00786   byte_t abtCmd[sizeof(pncmd_target_init)];
00787   memcpy(abtCmd,pncmd_target_init,sizeof(pncmd_target_init));
00788 
00789   // Clear the target init struct, reset to all zeros
00790   memset(abtCmd+2,0x00,37);
00791 
00792   // Set ATQA (SENS_RES)
00793   abtCmd[3] = 0x04;
00794   abtCmd[4] = 0x00;
00795 
00796   // Set SAK (SEL_RES)
00797   abtCmd[8] = 0x20;
00798 
00799   // Set UID
00800   abtCmd[5] = 0x00;
00801   abtCmd[6] = 0xb0;
00802   abtCmd[7] = 0x0b;
00803 
00804   // Make sure the CRC & parity are handled by the device, this is needed for target_init to work properly
00805   if (!bCrc) nfc_configure((nfc_device_t*)pnd,NDO_HANDLE_CRC,true);
00806   if (!bPar) nfc_configure((nfc_device_t*)pnd,NDO_HANDLE_PARITY,true);
00807 
00808   // Let the PN53X be activated by the RF level detector from power down mode
00809   if (!pn53x_set_reg(pnd,REG_CIU_TX_AUTO, SYMBOL_INITIAL_RF_ON,0x04)) return false;
00810 
00811   // Request the initialization as a target, we can not use pn53x_transceive() because
00812   // abtRx[0] contains the emulation mode (baudrate, 14443-4?, DEP and framing type)
00813   szRxLen = MAX_FRAME_LEN;
00814   if (!pnd->pdc->transceive(pnd->nds,abtCmd,39,abtRx,&szRxLen)) return false;
00815 
00816   // Get the last bit-count that is stored in the received byte 
00817   ui8Bits = pn53x_get_reg(pnd,REG_CIU_CONTROL) & SYMBOL_RX_LAST_BITS;
00818 
00819   // We are sure the parity is handled by the PN53X chip, so we handle it this way
00820   *pszRxBits = ((szRxLen-1-((ui8Bits==0)?0:1))*8)+ui8Bits;
00821   // Copy the received bytes
00822   memcpy(pbtRx,abtRx+1,szRxLen-1);
00823 
00824   // Restore the CRC & parity setting to the original value (if needed)
00825   if (!bCrc) nfc_configure((nfc_device_t*)pnd,NDO_HANDLE_CRC,false);
00826   if (!bPar) nfc_configure((nfc_device_t*)pnd,NDO_HANDLE_PARITY,false);
00827 
00828   return true;
00829 }
00830 
00837 bool nfc_target_receive_bits(const nfc_device_t* pnd, byte_t* pbtRx, size_t* pszRxBits, byte_t* pbtRxPar)
00838 {
00839   byte_t abtRx[MAX_FRAME_LEN];
00840   size_t szRxLen;
00841   size_t szFrameBits;
00842   uint8_t ui8Bits;
00843 
00844   // Try to gather a received frame from the reader
00845   if (!pn53x_transceive(pnd,pncmd_target_receive,2,abtRx,&szRxLen)) return false;
00846 
00847   // Get the last bit-count that is stored in the received byte 
00848   ui8Bits = pn53x_get_reg(pnd,REG_CIU_CONTROL) & SYMBOL_RX_LAST_BITS;
00849 
00850   // Recover the real frame length in bits
00851   szFrameBits = ((szRxLen-1-((ui8Bits==0)?0:1))*8)+ui8Bits;
00852 
00853   // Ignore the status byte from the PN53X here, it was checked earlier in pn53x_transceive()
00854   // Check if we should recover the parity bits ourself
00855   if (!pnd->bPar)
00856   {
00857     // Unwrap the response frame
00858     pn53x_unwrap_frame(abtRx+1,szFrameBits,pbtRx,pszRxBits,pbtRxPar);
00859   } else {
00860     // Save the received bits
00861     *pszRxBits = szFrameBits;
00862     // Copy the received bytes
00863     memcpy(pbtRx,abtRx+1,szRxLen-1);
00864   }
00865   // Everyting seems ok, return true
00866   return true;
00867 }
00868 
00875 bool nfc_target_receive_dep_bytes(const nfc_device_t* pnd, byte_t* pbtRx, size_t* pszRxLen)
00876 {
00877   byte_t abtRx[MAX_FRAME_LEN];
00878   size_t szRxLen;
00879 
00880   // Try to gather a received frame from the reader
00881   if (!pn53x_transceive(pnd,pncmd_target_get_data,2,abtRx,&szRxLen)) return false;
00882 
00883   // Save the received byte count
00884   *pszRxLen = szRxLen-1;
00885 
00886   // Copy the received bytes
00887   memcpy(pbtRx,abtRx+1,*pszRxLen);
00888 
00889   // Everyting seems ok, return true
00890   return true;
00891 }
00892 
00899 bool nfc_target_receive_bytes(const nfc_device_t* pnd, byte_t* pbtRx, size_t* pszRxLen)
00900 {
00901   byte_t abtRx[MAX_FRAME_LEN];
00902   size_t szRxLen;
00903 
00904   // Try to gather a received frame from the reader
00905   if (!pn53x_transceive(pnd,pncmd_target_receive,2,abtRx,&szRxLen)) return false;
00906 
00907   // Save the received byte count
00908   *pszRxLen = szRxLen-1;
00909 
00910   // Copy the received bytes
00911   memcpy(pbtRx,abtRx+1,*pszRxLen);
00912 
00913   // Everyting seems ok, return true
00914   return true;
00915 }
00916 
00923 bool nfc_target_send_bits(const nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxBits, const byte_t* pbtTxPar)
00924 {
00925   size_t szFrameBits = 0;
00926   size_t szFrameBytes = 0;
00927   uint8_t ui8Bits = 0;
00928   byte_t abtCmd[sizeof(pncmd_target_send)];
00929   memcpy(abtCmd,pncmd_target_send,sizeof(pncmd_target_send));
00930 
00931   // Check if we should prepare the parity bits ourself
00932   if (!pnd->bPar)
00933   {
00934     // Convert data with parity to a frame
00935     pn53x_wrap_frame(pbtTx,szTxBits,pbtTxPar,abtCmd+2,&szFrameBits);
00936   } else {
00937     szFrameBits = szTxBits;
00938   }
00939 
00940   // Retrieve the leading bits
00941   ui8Bits = szFrameBits%8;
00942 
00943   // Get the amount of frame bytes + optional (1 byte if there are leading bits) 
00944   szFrameBytes = (szFrameBits/8)+((ui8Bits==0)?0:1);
00945 
00946   // When the parity is handled before us, we just copy the data
00947   if (pnd->bPar) memcpy(abtCmd+2,pbtTx,szFrameBytes);
00948 
00949   // Set the amount of transmission bits in the PN53X chip register
00950   if (!pn53x_set_tx_bits(pnd,ui8Bits)) return false;
00951 
00952   // Try to send the bits to the reader
00953   if (!pn53x_transceive(pnd,abtCmd,szFrameBytes+2,NULL,NULL)) return false;
00954 
00955   // Everyting seems ok, return true
00956   return true;
00957 }
00958 
00959 
00966 bool nfc_target_send_bytes(const nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxLen)
00967 {
00968   byte_t abtCmd[sizeof(pncmd_target_send)];
00969   memcpy(abtCmd,pncmd_target_send,sizeof(pncmd_target_send));
00970 
00971   // We can not just send bytes without parity if while the PN53X expects we handled them
00972   if (!pnd->bPar) return false;
00973 
00974   // Copy the data into the command frame
00975   memcpy(abtCmd+2,pbtTx,szTxLen);
00976 
00977   // Try to send the bits to the reader
00978   if (!pn53x_transceive(pnd,abtCmd,szTxLen+2,NULL,NULL)) return false;
00979 
00980   // Everyting seems ok, return true
00981   return true;
00982 }
00983 
00990 bool nfc_target_send_dep_bytes(const nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxLen)
00991 {
00992   byte_t abtCmd[sizeof(pncmd_target_set_data)];
00993   memcpy(abtCmd,pncmd_target_set_data,sizeof(pncmd_target_set_data));
00994 
00995   // We can not just send bytes without parity if while the PN53X expects we handled them
00996   if (!pnd->bPar) return false;
00997 
00998   // Copy the data into the command frame
00999   memcpy(abtCmd+2,pbtTx,szTxLen);
01000 
01001   // Try to send the bits to the reader
01002   if (!pn53x_transceive(pnd,abtCmd,szTxLen+2,NULL,NULL)) return false;
01003 
01004   // Everyting seems ok, return true
01005   return true;
01006 }
01007 
01008 /* Special data accessors */
01009 
01014 const char* nfc_device_name(nfc_device_t* pnd)
01015 {
01016   return pnd->acName;
01017 }
01018 
01019 /* Misc. functions */
01020 
01025 const char* nfc_version(void)
01026 {
01027   #ifdef SVN_REVISION
01028   return PACKAGE_VERSION" (r"SVN_REVISION")";
01029   #else
01030   return PACKAGE_VERSION;
01031   #endif // SVN_REVISION
01032 }
01033