Skip to content

Commit

Permalink
make use of Serial optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ccreutzig committed Mar 10, 2015
1 parent 6a4e283 commit 3d31069
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 6 deletions.
44 changes: 44 additions & 0 deletions MifareClassic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,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 +70,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 +88,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 +152,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 +176,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 +210,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 +232,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 +304,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 +316,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 +327,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 +357,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 @@ -369,7 +409,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 +426,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 Down
2 changes: 2 additions & 0 deletions Ndef.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Ndef.h"

#ifdef NDEF_USE_SERIAL
// Borrowed from Adafruit_NFCShield_I2C
void PrintHex(const byte * data, const long numBytes)
{
Expand Down Expand Up @@ -55,3 +56,4 @@ void DumpHex(const byte * data, const long numBytes, const unsigned int blockSiz
data += blockSize;
}
}
#endif
2 changes: 2 additions & 0 deletions Ndef.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

#include <Arduino.h>

#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
4 changes: 4 additions & 0 deletions NdefMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ boolean NdefMessage::addRecord(NdefRecord& record)
}
else
{
#ifdef NDEF_USE_SERIAL
Serial.println(F("WARNING: Too many records. Increase MAX_NDEF_RECORDS."));
#endif
return false;
}
}
Expand Down Expand Up @@ -261,6 +263,7 @@ NdefRecord NdefMessage::operator[](int index)
return getRecord(index);
}

#ifdef NDEF_USE_SERIAL
void NdefMessage::print()
{
Serial.print(F("\nNDEF Message "));Serial.print(_recordCount);Serial.print(F(" record"));
Expand All @@ -272,3 +275,4 @@ void NdefMessage::print()
_records[i].print();
}
}
#endif
2 changes: 2 additions & 0 deletions NdefMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class NdefMessage
NdefRecord getRecord(int index);
NdefRecord operator[](int index);

#ifdef NDEF_USE_SERIAL
void print();
#endif
private:
NdefRecord _records[MAX_NDEF_RECORDS];
unsigned int _recordCount;
Expand Down
4 changes: 3 additions & 1 deletion NdefRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ void NdefRecord::setId(const byte * id, const unsigned int numBytes)
memcpy(_id, id, numBytes);
_idLength = numBytes;
}
#ifdef NDEF_USE_SERIAL

void NdefRecord::print()
{
Expand Down Expand Up @@ -349,4 +350,5 @@ void NdefRecord::print()
}
Serial.print(F(" Record is "));Serial.print(getEncodedSize());Serial.println(" bytes");

}
}
#endif
2 changes: 2 additions & 0 deletions NdefRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class NdefRecord
void setPayload(const byte *payload, const int numBytes);
void setId(const byte *id, const unsigned int numBytes);

#ifdef NDEF_USE_SERIAL
void print();
#endif
private:
byte getTnfByte(bool firstRecord, bool lastRecord);
byte _tnf; // 3 bit
Expand Down
14 changes: 14 additions & 0 deletions NfcAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ void NfcAdapter::begin(boolean verbose)

if (! versiondata)
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Didn't find PN53x board"));
#endif
while (1); // halt
}

if (verbose)
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Found chip PN5")); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print(F("Firmware ver. ")); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
#endif
}
// configure board to read RFID tags
shield->SAMConfig();
Expand Down Expand Up @@ -65,7 +69,9 @@ boolean NfcAdapter::format()
}
else
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Unsupported Tag."));
#endif
success = false;
}
return success;
Expand Down Expand Up @@ -93,7 +99,9 @@ boolean NfcAdapter::clean()
}
else
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("No driver for card type "));Serial.println(type);
#endif
return false;
}

Expand Down Expand Up @@ -123,7 +131,9 @@ NfcTag NfcAdapter::read()
}
else if (type == TAG_TYPE_UNKNOWN)
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Can not determine tag type"));
#endif
return NfcTag(uid, uidLength);
}
else
Expand Down Expand Up @@ -158,12 +168,16 @@ boolean NfcAdapter::write(NdefMessage& ndefMessage)
}
else if (type == TAG_TYPE_UNKNOWN)
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("Can not determine tag type"));
#endif
success = false;
}
else
{
#ifdef NDEF_USE_SERIAL
Serial.print(F("No driver for card type "));Serial.println(type);
#endif
success = false;
}

Expand Down
2 changes: 2 additions & 0 deletions NfcTag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ NdefMessage NfcTag::getNdefMessage()
{
return *_ndefMessage;
}
#ifdef NDEF_USE_SERIAL

void NfcTag::print()
{
Expand All @@ -119,3 +120,4 @@ void NfcTag::print()
_ndefMessage->print();
}
}
#endif
2 changes: 2 additions & 0 deletions NfcTag.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class NfcTag
String getTagType();
boolean hasNdefMessage();
NdefMessage getNdefMessage();
#ifdef NDEF_USE_SERIAL
void print();
#endif
private:
byte *_uid;
unsigned int _uidLength;
Expand Down
Loading

0 comments on commit 3d31069

Please sign in to comment.