mifare.c

00001 #include "mifare.h"
00002 
00003 #include <string.h>
00004 
00005 #include <nfc/nfc.h>
00006 
00020 bool
00021 nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp)
00022 {
00023   byte_t  abtRx[265];
00024   size_t  szRxLen;
00025   size_t  szParamLen;
00026   byte_t  abtCmd[265];
00027 
00028   // Make sure we are dealing with a active device
00029   if (!pnd->bActive)
00030     return false;
00031 
00032   abtCmd[0] = mc;               // The MIFARE Classic command
00033   abtCmd[1] = ui8Block;         // The block address (1K=0x00..0x39, 4K=0x00..0xff)
00034 
00035   switch (mc) {
00036     // Read and store command have no parameter
00037   case MC_READ:
00038   case MC_STORE:
00039     szParamLen = 0;
00040     break;
00041 
00042     // Authenticate command
00043   case MC_AUTH_A:
00044   case MC_AUTH_B:
00045     szParamLen = sizeof (mifare_param_auth);
00046     break;
00047 
00048     // Data command
00049   case MC_WRITE:
00050     szParamLen = sizeof (mifare_param_data);
00051     break;
00052 
00053     // Value command
00054   case MC_DECREMENT:
00055   case MC_INCREMENT:
00056   case MC_TRANSFER:
00057     szParamLen = sizeof (mifare_param_value);
00058     break;
00059 
00060     // Please fix your code, you never should reach this statement
00061   default:
00062     return false;
00063     break;
00064   }
00065 
00066   // When available, copy the parameter bytes
00067   if (szParamLen)
00068     memcpy (abtCmd + 2, (byte_t *) pmp, szParamLen);
00069 
00070   // Fire the mifare command
00071   if (!nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRxLen)) {
00072     if (pnd->iLastError != 0x14)
00073       nfc_perror (pnd, "nfc_initiator_transceive_bytes");
00074     return false;
00075   }
00076   // When we have executed a read command, copy the received bytes into the param
00077   if (mc == MC_READ) {
00078     if (szRxLen == 16) {
00079       memcpy (pmp->mpd.abtData, abtRx, 16);
00080     } else {
00081       return false;
00082     }
00083   }
00084   // Command succesfully executed
00085   return true;
00086 }