Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/ccreutzig/NDEF into ccreu…
Browse files Browse the repository at this point in the history
…tzig-master
  • Loading branch information
don committed May 12, 2018
2 parents 3ecd97f + 1699127 commit 476688a
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 38 deletions.
50 changes: 48 additions & 2 deletions MifareClassic.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#ifdef NDEF_SUPPORT_MIFARE_CLASSIC
#include "MifareClassic.h"

#define BLOCK_SIZE 16
Expand Down Expand Up @@ -36,13 +37,17 @@ NfcTag MifareClassic::read(byte *uid, unsigned int uidLength)
}
else
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Error. Failed read block "));Serial.println(currentBlock);
#endif
return NfcTag(uid, uidLength, MIFARE_CLASSIC);
}
}
else
{
#ifdef NDEF_USE_SERIAL
Serial.println(F("Tag is not NDEF formatted."));
#endif
// TODO set tag.isFormatted = false
return NfcTag(uid, uidLength, MIFARE_CLASSIC);
}
Expand All @@ -66,7 +71,9 @@ NfcTag MifareClassic::read(byte *uid, unsigned int uidLength)
success = _nfcShield->mifareclassic_AuthenticateBlock(uid, uidLength, currentBlock, 0, key);
if (!success)
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Error. Block Authentication failed for "));Serial.println(currentBlock);
#endif
// TODO error handling
}
}
Expand All @@ -82,7 +89,9 @@ NfcTag MifareClassic::read(byte *uid, unsigned int uidLength)
}
else
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Read failed "));Serial.println(currentBlock);
#endif
// TODO handle errors here
}

Expand Down Expand Up @@ -144,7 +153,9 @@ int MifareClassic::getNdefStartIndex(byte *data)
}
else
{
#ifdef NDEF_USE_SERIAL
Serial.print("Unknown TLV ");Serial.println(data[i], HEX);
#endif
return -2;
}
}
Expand All @@ -166,7 +177,9 @@ bool MifareClassic::decodeTlv(byte *data, int &messageLength, int &messageStartI

if (i < 0 || data[i] != 0x3)
{
#ifdef NDEF_USE_SERIAL
Serial.println(F("Error. Can't decode message length."));
#endif
return false;
}
else
Expand Down Expand Up @@ -198,13 +211,17 @@ boolean MifareClassic::formatNDEF(byte * uid, unsigned int uidLength)
boolean success = _nfcShield->mifareclassic_AuthenticateBlock (uid, uidLength, 0, 0, keya);
if (!success)
{
#ifdef NDEF_USE_SERIAL
Serial.println(F("Unable to authenticate block 0 to enable card formatting!"));
#endif
return false;
}
success = _nfcShield->mifareclassic_FormatNDEF();
if (!success)
{
#ifdef NDEF_USE_SERIAL
Serial.println(F("Unable to format the card for NDEF"));
#endif
}
else
{
Expand All @@ -216,31 +233,43 @@ boolean MifareClassic::formatNDEF(byte * uid, unsigned int uidLength)
{
if (!(_nfcShield->mifareclassic_WriteDataBlock (i, emptyNdefMesg)))
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Unable to write block "));Serial.println(i);
#endif
}
}
else
{
if (!(_nfcShield->mifareclassic_WriteDataBlock (i, sectorbuffer0)))
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Unable to write block "));Serial.println(i);
#endif
}
}
if (!(_nfcShield->mifareclassic_WriteDataBlock (i+1, sectorbuffer0)))
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Unable to write block "));Serial.println(i+1);
#endif
}
if (!(_nfcShield->mifareclassic_WriteDataBlock (i+2, sectorbuffer0)))
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Unable to write block "));Serial.println(i+2);
#endif
}
if (!(_nfcShield->mifareclassic_WriteDataBlock (i+3, sectorbuffer4)))
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Unable to write block "));Serial.println(i+3);
#endif
}
} else {
unsigned int iii=uidLength;
#ifdef NDEF_USE_SERIAL
Serial.print(F("Unable to authenticate block "));Serial.println(i);
#endif
_nfcShield->readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, (uint8_t*)&iii);
}
}
Expand Down Expand Up @@ -276,7 +305,9 @@ boolean MifareClassic::formatMifare(byte * uid, unsigned int uidLength)
success = _nfcShield->mifareclassic_AuthenticateBlock (uid, uidLength, BLOCK_NUMBER_OF_SECTOR_TRAILER(idx), 1, (uint8_t *)KEY_DEFAULT_KEYAB);
if (!success)
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Authentication failed for sector ")); Serial.println(idx);
#endif
return false;
}

Expand All @@ -286,7 +317,9 @@ boolean MifareClassic::formatMifare(byte * uid, unsigned int uidLength)
memset(blockBuffer, 0, sizeof(blockBuffer));
if (!(_nfcShield->mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 2, blockBuffer)))
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Unable to write to sector ")); Serial.println(idx);
#endif
}
}
else
Expand All @@ -295,19 +328,25 @@ boolean MifareClassic::formatMifare(byte * uid, unsigned int uidLength)
// this block has not to be overwritten for block 0. It contains Tag id and other unique data.
if (!(_nfcShield->mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 3, blockBuffer)))
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Unable to write to sector ")); Serial.println(idx);
#endif
}
if (!(_nfcShield->mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 2, blockBuffer)))
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Unable to write to sector ")); Serial.println(idx);
#endif
}
}

memset(blockBuffer, 0, sizeof(blockBuffer));

if (!(_nfcShield->mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)) - 1, blockBuffer)))
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Unable to write to sector ")); Serial.println(idx);
#endif
}

// Step 3: Reset both keys to 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
Expand All @@ -319,7 +358,9 @@ boolean MifareClassic::formatMifare(byte * uid, unsigned int uidLength)
// Step 4: Write the trailer block
if (!(_nfcShield->mifareclassic_WriteDataBlock((BLOCK_NUMBER_OF_SECTOR_TRAILER(idx)), blockBuffer)))
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Unable to write trailer block of sector ")); Serial.println(idx);
#endif
}
}
return true;
Expand Down Expand Up @@ -357,7 +398,7 @@ boolean MifareClassic::write(NdefMessage& m, byte * uid, unsigned int uidLength)
}

// Write to tag
int index = 0;
unsigned int index = 0;
int currentBlock = 4;
uint8_t key[6] = { 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7 }; // this is Sector 1 - 15 key

Expand All @@ -369,7 +410,9 @@ boolean MifareClassic::write(NdefMessage& m, byte * uid, unsigned int uidLength)
int success = _nfcShield->mifareclassic_AuthenticateBlock(uid, uidLength, currentBlock, 0, key);
if (!success)
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Error. Block Authentication failed for "));Serial.println(currentBlock);
#endif
return false;
}
}
Expand All @@ -384,7 +427,9 @@ boolean MifareClassic::write(NdefMessage& m, byte * uid, unsigned int uidLength)
}
else
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Write failed "));Serial.println(currentBlock);
#endif
return false;
}
index += BLOCK_SIZE;
Expand All @@ -402,4 +447,5 @@ boolean MifareClassic::write(NdefMessage& m, byte * uid, unsigned int uidLength)
}

return true;
}
}
#endif
5 changes: 4 additions & 1 deletion MifareClassic.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef MifareClassic_h
#define MifareClassic_h

#ifdef NDEF_SUPPORT_MIFARE_CLASSIC

#include <Due.h>
#include <PN532.h>
#include <Ndef.h>
Expand All @@ -22,4 +24,5 @@ class MifareClassic
bool decodeTlv(byte *data, int &messageLength, int &messageStartIndex);
};

#endif
#endif
#endif
8 changes: 8 additions & 0 deletions MifareUltralight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ NfcTag MifareUltralight::read(byte * uid, unsigned int uidLength)
{
if (isUnformatted())
{
#ifdef NDEF_USE_SERIAL
Serial.println(F("WARNING: Tag is not formatted."));
#endif
return NfcTag(uid, uidLength, NFC_FORUM_TAG_TYPE_2);
}

Expand Down Expand Up @@ -56,7 +58,9 @@ NfcTag MifareUltralight::read(byte * uid, unsigned int uidLength)
}
else
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Read failed "));Serial.println(page);
#endif
// TODO error handling
messageLength = 0;
break;
Expand Down Expand Up @@ -86,7 +90,9 @@ boolean MifareUltralight::isUnformatted()
}
else
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Error. Failed read page "));Serial.println(page);
#endif
return false;
}
}
Expand Down Expand Up @@ -166,7 +172,9 @@ boolean MifareUltralight::write(NdefMessage& m, byte * uid, unsigned int uidLeng
{
if (isUnformatted())
{
#ifdef NDEF_USE_SERIAL
Serial.println(F("WARNING: Tag is not formatted."));
#endif
return false;
}
readCapabilityContainer(); // meta info for tag
Expand Down
6 changes: 4 additions & 2 deletions Ndef.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "Ndef.h"

#ifdef NDEF_USE_SERIAL
// Borrowed from Adafruit_NFCShield_I2C
void PrintHex(const byte * data, const long numBytes)
{
uint32_t szPos;
int32_t szPos;
for (szPos=0; szPos < numBytes; szPos++)
{
Serial.print("0x");
Expand All @@ -22,7 +23,7 @@ void PrintHex(const byte * data, const long numBytes)
// Borrowed from Adafruit_NFCShield_I2C
void PrintHexChar(const byte * data, const long numBytes)
{
uint32_t szPos;
int32_t szPos;
for (szPos=0; szPos < numBytes; szPos++)
{
// Append leading 0 for small values
Expand Down Expand Up @@ -55,3 +56,4 @@ void DumpHex(const byte * data, const long numBytes, const unsigned int blockSiz
data += blockSize;
}
}
#endif
6 changes: 2 additions & 4 deletions Ndef.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

#include <Arduino.h>

#ifndef NULL
#define NULL (void *)0
#endif

#ifdef NDEF_USE_SERIAL
void PrintHex(const byte *data, const long numBytes);
void PrintHexChar(const byte *data, const long numBytes);
void DumpHex(const byte *data, const long numBytes, const int blockSize);

#endif
#endif
Loading

0 comments on commit 476688a

Please sign in to comment.