to colorize a colorful pixel with a simple touch using nfc technology

Dependencies:   Chainable_RGB_LED mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NfcAdapter.cpp Source File

NfcAdapter.cpp

00001 #include <NfcAdapter.h>
00002 #include <PN532_debug.h>
00003 
00004 NfcAdapter::NfcAdapter(PN532Interface &interface)
00005 {
00006     shield = new PN532(interface);
00007 }
00008 
00009 NfcAdapter::~NfcAdapter(void)
00010 {
00011     delete shield;
00012 }
00013 
00014 void NfcAdapter::begin()
00015 {
00016     shield->begin();
00017 
00018     uint32_t versiondata = shield->getFirmwareVersion();
00019     if (! versiondata) {
00020         DMSG("Didn't find PN53x board");
00021         while (1); // halt
00022     }
00023     
00024     DMSG("Found chip PN5%2X\r\n", versiondata >> 24);
00025     DMSG("Firmware V%d.%d\r\n", (versiondata >> 16) & 0xFF, (versiondata >> 8) & 0xFF);
00026 
00027     // configure board to read RFID tags
00028     shield->SAMConfig();
00029 }
00030 
00031 bool NfcAdapter::tagPresent()
00032 {
00033     uint8_t success;
00034     uidLength = 0;
00035 
00036     // TODO is cast of uidLength OK?
00037     success = shield->readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, (uint8_t*)&uidLength);
00038 
00039     // if (success)
00040     // {
00041     //   DMSG("Found an ISO14443A card");
00042     //   DMSG("  UID Length: ");Serial.print(uidLength, DEC);DMSG(" uint8_ts");
00043     //   DMSG("  UID Value: ");
00044     //   shield->PrintHex(uid, uidLength);
00045     //   DMSG("");
00046     // }
00047 
00048     return success;
00049 }
00050 
00051 NfcTag NfcAdapter::read()
00052 {
00053 
00054     uint8_t type = guessTagType();
00055 
00056     // TODO need an abstraction of Driver
00057     if (type == TAG_TYPE_MIFARE_CLASSIC)
00058     {
00059         #ifdef NDEF_DEBUG
00060         DMSG("Reading Mifare Classic");
00061         #endif
00062         MifareClassic mifareClassic = MifareClassic(*shield);
00063         return mifareClassic.read(uid, uidLength);
00064     }
00065     else if (type == TAG_TYPE_2)
00066     {
00067         #ifdef NDEF_DEBUG
00068         DMSG("Reading Mifare Ultralight");
00069         #endif
00070         MifareUltralight ultralight = MifareUltralight(*shield);
00071         return ultralight.read(uid, uidLength);
00072     }
00073     else if (type == TAG_TYPE_UNKNOWN)
00074     {
00075         DMSG("Can not determine tag type");
00076         //DMSG("Can not determine tag type for ATQA 0x");
00077         //Serial.print(atqa, HEX);DMSG(" SAK 0x");DMSG(sak, HEX);
00078         return NfcTag(uid, uidLength);
00079     }
00080     else
00081     {
00082         DMSG("No driver for card type ");
00083         DMSG_INT(type);
00084         // TODO should set type here
00085         return NfcTag(uid, uidLength);
00086     }
00087 
00088 }
00089 
00090 bool NfcAdapter::write(NdefMessage& ndefMessage)
00091 {
00092     bool success;
00093 
00094     if (uidLength == 4)
00095     {
00096         MifareClassic mifareClassic = MifareClassic(*shield);
00097         success = mifareClassic.write(ndefMessage, uid, uidLength);
00098     }
00099     else
00100     {
00101         DMSG("Unsupported Tag");
00102         success = false;
00103     }
00104     return success;
00105 }
00106 
00107 // TODO this should return a Driver MifareClassic, MifareUltralight, Type 4, Unknown
00108 // Guess Tag Type by looking at the ATQA and SAK values
00109 // Need to follow spec for Card Identification. Maybe AN1303, AN1305 and ???
00110 unsigned int NfcAdapter::guessTagType()
00111 {
00112 
00113     // 4 uint8_t id - Mifare Classic
00114     //  - ATQA 0x4 && SAK 0x8
00115     // 7 uint8_t id
00116     //  - ATQA 0x44 && SAK 0x8 - Mifare Classic
00117     //  - ATQA 0x44 && SAK 0x0 - Mifare Ultralight NFC Forum Type 2
00118     //  - ATQA 0x344 && SAK 0x20 - NFC Forum Type 4
00119 
00120     if (uidLength == 4)
00121     {
00122         return TAG_TYPE_MIFARE_CLASSIC;
00123     }
00124     else
00125     {
00126         return TAG_TYPE_2;
00127     }
00128 }