Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of PN532 by
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,&ATQA,&SAK); 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 uint8_t type = guessTagType(); 00094 if (uidLength == 4) 00095 { 00096 MifareClassic mifareClassic = MifareClassic(*shield); 00097 success = mifareClassic.write(ndefMessage, uid, uidLength); 00098 } 00099 else 00100 { 00101 DMSG("Ultralight Tag"); 00102 MifareUltralight mifareUltralight = MifareUltralight(*shield); 00103 success= mifareUltralight.write(ndefMessage, uid, uidLength); 00104 //success = false; 00105 } 00106 return success; 00107 } 00108 00109 // TODO this should return a Driver MifareClassic, MifareUltralight, Type 4, Unknown 00110 // Guess Tag Type by looking at the ATQA and SAK values 00111 // Need to follow spec for Card Identification. Maybe AN1303, AN1305 and ??? 00112 unsigned int NfcAdapter::guessTagType() 00113 { 00114 00115 // 4 uint8_t id - Mifare Classic 00116 // - ATQA 0x4 && SAK 0x8 00117 // 7 uint8_t id 00118 // - ATQA 0x44 && SAK 0x8 - Mifare Classic 00119 // - ATQA 0x44 && SAK 0x0 - Mifare Ultralight NFC Forum Type 2 00120 // - ATQA 0x344 && SAK 0x20 - NFC Forum Type 4 00121 DMSG("Guess type"); 00122 DMSG("ATQA: 0x"); DMSG_HEX(ATQA); 00123 DMSG("SAK: 0x"); DMSG_HEX(SAK); 00124 /*if (uidLength == 4) 00125 { 00126 return TAG_TYPE_MIFARE_CLASSIC; 00127 } 00128 else 00129 { 00130 return TAG_TYPE_2; 00131 }*/ 00132 if(ATQA==0x4&&SAK==0x8){ 00133 return TAG_TYPE_MIFARE_CLASSIC; 00134 } 00135 if(ATQA==0x44&&SAK==0x0){ 00136 return TAG_TYPE_2; 00137 } 00138 return TAG_TYPE_UNKNOWN; 00139 }
Generated on Tue Jul 12 2022 16:40:03 by
1.7.2
