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.
Dependencies: Chainable_RGB_LED mbed
nfc/NfcAdapter.cpp@0:88960f3eeb2c, 2013-12-27 (annotated)
- Committer:
- yihui
- Date:
- Fri Dec 27 01:46:32 2013 +0000
- Revision:
- 0:88960f3eeb2c
initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 0:88960f3eeb2c | 1 | #include <NfcAdapter.h> |
yihui | 0:88960f3eeb2c | 2 | #include <PN532_debug.h> |
yihui | 0:88960f3eeb2c | 3 | |
yihui | 0:88960f3eeb2c | 4 | NfcAdapter::NfcAdapter(PN532Interface &interface) |
yihui | 0:88960f3eeb2c | 5 | { |
yihui | 0:88960f3eeb2c | 6 | shield = new PN532(interface); |
yihui | 0:88960f3eeb2c | 7 | } |
yihui | 0:88960f3eeb2c | 8 | |
yihui | 0:88960f3eeb2c | 9 | NfcAdapter::~NfcAdapter(void) |
yihui | 0:88960f3eeb2c | 10 | { |
yihui | 0:88960f3eeb2c | 11 | delete shield; |
yihui | 0:88960f3eeb2c | 12 | } |
yihui | 0:88960f3eeb2c | 13 | |
yihui | 0:88960f3eeb2c | 14 | void NfcAdapter::begin() |
yihui | 0:88960f3eeb2c | 15 | { |
yihui | 0:88960f3eeb2c | 16 | shield->begin(); |
yihui | 0:88960f3eeb2c | 17 | |
yihui | 0:88960f3eeb2c | 18 | uint32_t versiondata = shield->getFirmwareVersion(); |
yihui | 0:88960f3eeb2c | 19 | if (! versiondata) { |
yihui | 0:88960f3eeb2c | 20 | DMSG("Didn't find PN53x board"); |
yihui | 0:88960f3eeb2c | 21 | while (1); // halt |
yihui | 0:88960f3eeb2c | 22 | } |
yihui | 0:88960f3eeb2c | 23 | |
yihui | 0:88960f3eeb2c | 24 | DMSG("Found chip PN5%2X\r\n", versiondata >> 24); |
yihui | 0:88960f3eeb2c | 25 | DMSG("Firmware V%d.%d\r\n", (versiondata >> 16) & 0xFF, (versiondata >> 8) & 0xFF); |
yihui | 0:88960f3eeb2c | 26 | |
yihui | 0:88960f3eeb2c | 27 | // configure board to read RFID tags |
yihui | 0:88960f3eeb2c | 28 | shield->SAMConfig(); |
yihui | 0:88960f3eeb2c | 29 | } |
yihui | 0:88960f3eeb2c | 30 | |
yihui | 0:88960f3eeb2c | 31 | bool NfcAdapter::tagPresent() |
yihui | 0:88960f3eeb2c | 32 | { |
yihui | 0:88960f3eeb2c | 33 | uint8_t success; |
yihui | 0:88960f3eeb2c | 34 | uidLength = 0; |
yihui | 0:88960f3eeb2c | 35 | |
yihui | 0:88960f3eeb2c | 36 | // TODO is cast of uidLength OK? |
yihui | 0:88960f3eeb2c | 37 | success = shield->readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, (uint8_t*)&uidLength); |
yihui | 0:88960f3eeb2c | 38 | |
yihui | 0:88960f3eeb2c | 39 | // if (success) |
yihui | 0:88960f3eeb2c | 40 | // { |
yihui | 0:88960f3eeb2c | 41 | // DMSG("Found an ISO14443A card"); |
yihui | 0:88960f3eeb2c | 42 | // DMSG(" UID Length: ");Serial.print(uidLength, DEC);DMSG(" uint8_ts"); |
yihui | 0:88960f3eeb2c | 43 | // DMSG(" UID Value: "); |
yihui | 0:88960f3eeb2c | 44 | // shield->PrintHex(uid, uidLength); |
yihui | 0:88960f3eeb2c | 45 | // DMSG(""); |
yihui | 0:88960f3eeb2c | 46 | // } |
yihui | 0:88960f3eeb2c | 47 | |
yihui | 0:88960f3eeb2c | 48 | return success; |
yihui | 0:88960f3eeb2c | 49 | } |
yihui | 0:88960f3eeb2c | 50 | |
yihui | 0:88960f3eeb2c | 51 | NfcTag NfcAdapter::read() |
yihui | 0:88960f3eeb2c | 52 | { |
yihui | 0:88960f3eeb2c | 53 | |
yihui | 0:88960f3eeb2c | 54 | uint8_t type = guessTagType(); |
yihui | 0:88960f3eeb2c | 55 | |
yihui | 0:88960f3eeb2c | 56 | // TODO need an abstraction of Driver |
yihui | 0:88960f3eeb2c | 57 | if (type == TAG_TYPE_MIFARE_CLASSIC) |
yihui | 0:88960f3eeb2c | 58 | { |
yihui | 0:88960f3eeb2c | 59 | #ifdef NDEF_DEBUG |
yihui | 0:88960f3eeb2c | 60 | DMSG("Reading Mifare Classic"); |
yihui | 0:88960f3eeb2c | 61 | #endif |
yihui | 0:88960f3eeb2c | 62 | MifareClassic mifareClassic = MifareClassic(*shield); |
yihui | 0:88960f3eeb2c | 63 | return mifareClassic.read(uid, uidLength); |
yihui | 0:88960f3eeb2c | 64 | } |
yihui | 0:88960f3eeb2c | 65 | else if (type == TAG_TYPE_2) |
yihui | 0:88960f3eeb2c | 66 | { |
yihui | 0:88960f3eeb2c | 67 | #ifdef NDEF_DEBUG |
yihui | 0:88960f3eeb2c | 68 | DMSG("Reading Mifare Ultralight"); |
yihui | 0:88960f3eeb2c | 69 | #endif |
yihui | 0:88960f3eeb2c | 70 | MifareUltralight ultralight = MifareUltralight(*shield); |
yihui | 0:88960f3eeb2c | 71 | return ultralight.read(uid, uidLength); |
yihui | 0:88960f3eeb2c | 72 | } |
yihui | 0:88960f3eeb2c | 73 | else if (type == TAG_TYPE_UNKNOWN) |
yihui | 0:88960f3eeb2c | 74 | { |
yihui | 0:88960f3eeb2c | 75 | DMSG("Can not determine tag type"); |
yihui | 0:88960f3eeb2c | 76 | //DMSG("Can not determine tag type for ATQA 0x"); |
yihui | 0:88960f3eeb2c | 77 | //Serial.print(atqa, HEX);DMSG(" SAK 0x");DMSG(sak, HEX); |
yihui | 0:88960f3eeb2c | 78 | return NfcTag(uid, uidLength); |
yihui | 0:88960f3eeb2c | 79 | } |
yihui | 0:88960f3eeb2c | 80 | else |
yihui | 0:88960f3eeb2c | 81 | { |
yihui | 0:88960f3eeb2c | 82 | DMSG("No driver for card type "); |
yihui | 0:88960f3eeb2c | 83 | DMSG_INT(type); |
yihui | 0:88960f3eeb2c | 84 | // TODO should set type here |
yihui | 0:88960f3eeb2c | 85 | return NfcTag(uid, uidLength); |
yihui | 0:88960f3eeb2c | 86 | } |
yihui | 0:88960f3eeb2c | 87 | |
yihui | 0:88960f3eeb2c | 88 | } |
yihui | 0:88960f3eeb2c | 89 | |
yihui | 0:88960f3eeb2c | 90 | bool NfcAdapter::write(NdefMessage& ndefMessage) |
yihui | 0:88960f3eeb2c | 91 | { |
yihui | 0:88960f3eeb2c | 92 | bool success; |
yihui | 0:88960f3eeb2c | 93 | |
yihui | 0:88960f3eeb2c | 94 | if (uidLength == 4) |
yihui | 0:88960f3eeb2c | 95 | { |
yihui | 0:88960f3eeb2c | 96 | MifareClassic mifareClassic = MifareClassic(*shield); |
yihui | 0:88960f3eeb2c | 97 | success = mifareClassic.write(ndefMessage, uid, uidLength); |
yihui | 0:88960f3eeb2c | 98 | } |
yihui | 0:88960f3eeb2c | 99 | else |
yihui | 0:88960f3eeb2c | 100 | { |
yihui | 0:88960f3eeb2c | 101 | DMSG("Unsupported Tag"); |
yihui | 0:88960f3eeb2c | 102 | success = false; |
yihui | 0:88960f3eeb2c | 103 | } |
yihui | 0:88960f3eeb2c | 104 | return success; |
yihui | 0:88960f3eeb2c | 105 | } |
yihui | 0:88960f3eeb2c | 106 | |
yihui | 0:88960f3eeb2c | 107 | // TODO this should return a Driver MifareClassic, MifareUltralight, Type 4, Unknown |
yihui | 0:88960f3eeb2c | 108 | // Guess Tag Type by looking at the ATQA and SAK values |
yihui | 0:88960f3eeb2c | 109 | // Need to follow spec for Card Identification. Maybe AN1303, AN1305 and ??? |
yihui | 0:88960f3eeb2c | 110 | unsigned int NfcAdapter::guessTagType() |
yihui | 0:88960f3eeb2c | 111 | { |
yihui | 0:88960f3eeb2c | 112 | |
yihui | 0:88960f3eeb2c | 113 | // 4 uint8_t id - Mifare Classic |
yihui | 0:88960f3eeb2c | 114 | // - ATQA 0x4 && SAK 0x8 |
yihui | 0:88960f3eeb2c | 115 | // 7 uint8_t id |
yihui | 0:88960f3eeb2c | 116 | // - ATQA 0x44 && SAK 0x8 - Mifare Classic |
yihui | 0:88960f3eeb2c | 117 | // - ATQA 0x44 && SAK 0x0 - Mifare Ultralight NFC Forum Type 2 |
yihui | 0:88960f3eeb2c | 118 | // - ATQA 0x344 && SAK 0x20 - NFC Forum Type 4 |
yihui | 0:88960f3eeb2c | 119 | |
yihui | 0:88960f3eeb2c | 120 | if (uidLength == 4) |
yihui | 0:88960f3eeb2c | 121 | { |
yihui | 0:88960f3eeb2c | 122 | return TAG_TYPE_MIFARE_CLASSIC; |
yihui | 0:88960f3eeb2c | 123 | } |
yihui | 0:88960f3eeb2c | 124 | else |
yihui | 0:88960f3eeb2c | 125 | { |
yihui | 0:88960f3eeb2c | 126 | return TAG_TYPE_2; |
yihui | 0:88960f3eeb2c | 127 | } |
yihui | 0:88960f3eeb2c | 128 | } |