From 7bf9868d79e25da2b7b9ada8d1bf97f7333f9798 Mon Sep 17 00:00:00 2001 From: Manuel Weiser Date: Sat, 30 Aug 2025 10:17:30 +0200 Subject: [PATCH] feat: implement robust page reading with error recovery for NFC tags --- src/nfc.cpp | 62 ++++++++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/src/nfc.cpp b/src/nfc.cpp index dde0012..f4c5e5a 100644 --- a/src/nfc.cpp +++ b/src/nfc.cpp @@ -108,6 +108,37 @@ bool formatNdefTag() { return buffer[2]*8; } +// Robust page reading with error recovery +bool robustPageRead(uint8_t page, uint8_t* buffer) { + const int MAX_READ_ATTEMPTS = 3; + + for (int attempt = 0; attempt < MAX_READ_ATTEMPTS; attempt++) { + esp_task_wdt_reset(); + yield(); + + if (nfc.ntag2xx_ReadPage(page, buffer)) { + return true; + } + + Serial.printf("Page %d read failed, attempt %d/%d\n", page, attempt + 1, MAX_READ_ATTEMPTS); + + // Try to stabilize connection between attempts + if (attempt < MAX_READ_ATTEMPTS - 1) { + vTaskDelay(pdMS_TO_TICKS(25)); + + // Re-verify tag presence with quick check + uint8_t uid[7]; + uint8_t uidLength; + if (!nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 100)) { + Serial.println("Tag lost during read operation"); + return false; + } + } + } + + return false; +} + String detectNtagType() { // Read capability container from page 3 to determine exact NTAG type @@ -1730,37 +1761,6 @@ void startWriteJsonToTag(const bool isSpoolTag, const char* payload) { } } -// Robust page reading with error recovery -bool robustPageRead(uint8_t page, uint8_t* buffer) { - const int MAX_READ_ATTEMPTS = 3; - - for (int attempt = 0; attempt < MAX_READ_ATTEMPTS; attempt++) { - esp_task_wdt_reset(); - yield(); - - if (nfc.ntag2xx_ReadPage(page, buffer)) { - return true; - } - - Serial.printf("Page %d read failed, attempt %d/%d\n", page, attempt + 1, MAX_READ_ATTEMPTS); - - // Try to stabilize connection between attempts - if (attempt < MAX_READ_ATTEMPTS - 1) { - vTaskDelay(pdMS_TO_TICKS(25)); - - // Re-verify tag presence with quick check - uint8_t uid[7]; - uint8_t uidLength; - if (!nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 100)) { - Serial.println("Tag lost during read operation"); - return false; - } - } - } - - return false; -} - // Safe tag detection with manual retry logic and short timeouts bool safeTagDetection(uint8_t* uid, uint8_t* uidLength) { const int MAX_ATTEMPTS = 3;