Milan Jovanovic
/
readMifareORG
Test example
Fork of readMifare by
Diff: main.cpp
- Revision:
- 5:4b238fd5f347
- Parent:
- 4:70acff42d8b4
- Child:
- 6:9109b95c3e97
--- a/main.cpp Wed Mar 16 20:05:34 2016 +0000 +++ b/main.cpp Thu Mar 17 09:49:16 2016 +0000 @@ -49,6 +49,7 @@ Adafruit_PN532 nfc(SCK, MISO, MOSI, SS); void loop(void); +int sectorToBlock(int sectorIndex); int main() { pc.printf("Hello!\r\n"); @@ -82,6 +83,7 @@ uint8_t success; uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) + int sector = 32; // Wait for an ISO14443A type cards (Mifare, etc.). When one is found // 'uid' will be populated with the UID, and uidLength will indicate @@ -103,13 +105,14 @@ // Now we need to try to authenticate it for read/write access // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF - pc.printf("Trying to authenticate block 4 with default KEYA value\r\n"); + pc.printf("Trying to authenticate sector %d with default KEYA value\r\n", sector); uint8_t keya[6] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 }; + int blockNumber = sectorToBlock(sector); // Start with block 4 (the first block of sector 1) since sector 0 // contains the manufacturer data and it's probably better just // to leave it alone unless you know what you're doing - success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 128, 0, keya); + success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, blockNumber, 0, keya); if (success) { @@ -117,7 +120,7 @@ int respSize = 16; uint8_t data[respSize]; - success = nfc.mifareclassic_ReadDataBlock(128, data); + success = nfc.mifareclassic_ReadDataBlock(blockNumber, data); if (success) { @@ -166,4 +169,12 @@ } } } +} + +int sectorToBlock(int sectorIndex) { + if (sectorIndex < 32) { + return sectorIndex * 4; + } else { + return 32 * 4 + (sectorIndex - 32) * 16; + } } \ No newline at end of file