|
|
|
@@ -23,6 +23,7 @@ bool tagProcessed = false;
|
|
|
|
|
volatile bool pauseBambuMqttTask = false;
|
|
|
|
|
volatile bool nfcReadingTaskSuspendRequest = false;
|
|
|
|
|
volatile bool nfcReadingTaskSuspendState = false;
|
|
|
|
|
volatile bool nfcWriteInProgress = false; // Prevent any tag operations during write
|
|
|
|
|
|
|
|
|
|
struct NfcWriteParameterType {
|
|
|
|
|
bool tagType;
|
|
|
|
@@ -410,14 +411,54 @@ uint8_t ntag2xx_WriteNDEF(const char *payload) {
|
|
|
|
|
|
|
|
|
|
// STEP 1: Read current tag content for debugging
|
|
|
|
|
Serial.println();
|
|
|
|
|
Serial.println("=== SCHRITT 1: AKTUELLER TAG-INHALT ===");
|
|
|
|
|
uint8_t currentContent[64]; // Read first 16 pages
|
|
|
|
|
memset(currentContent, 0, 64);
|
|
|
|
|
Serial.println("=== SCHRITT 1: NFC-INTERFACE-DIAGNOSE ===");
|
|
|
|
|
|
|
|
|
|
// First, check if the NFC interface is working at all
|
|
|
|
|
Serial.println("Teste NFC-Interface-Zustand...");
|
|
|
|
|
|
|
|
|
|
// Try to read capability container (which worked during detection)
|
|
|
|
|
uint8_t ccTest[4];
|
|
|
|
|
bool ccReadable = nfc.ntag2xx_ReadPage(3, ccTest);
|
|
|
|
|
Serial.print("Capability Container (Seite 3) lesbar: ");
|
|
|
|
|
Serial.println(ccReadable ? "✓" : "❌");
|
|
|
|
|
|
|
|
|
|
if (ccReadable) {
|
|
|
|
|
Serial.print("CC Inhalt: ");
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
if (ccTest[i] < 0x10) Serial.print("0");
|
|
|
|
|
Serial.print(ccTest[i], HEX);
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
}
|
|
|
|
|
Serial.println();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test a few different pages to see which ones are accessible
|
|
|
|
|
uint8_t testData[4];
|
|
|
|
|
for (uint8_t testPage = 0; testPage <= 10; testPage++) {
|
|
|
|
|
bool readable = nfc.ntag2xx_ReadPage(testPage, testData);
|
|
|
|
|
Serial.print("Seite ");
|
|
|
|
|
Serial.print(testPage);
|
|
|
|
|
Serial.print(": ");
|
|
|
|
|
if (readable) {
|
|
|
|
|
Serial.print("✓ - ");
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
if (testData[i] < 0x10) Serial.print("0");
|
|
|
|
|
Serial.print(testData[i], HEX);
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
}
|
|
|
|
|
Serial.println();
|
|
|
|
|
} else {
|
|
|
|
|
Serial.println("❌ - Nicht lesbar");
|
|
|
|
|
}
|
|
|
|
|
vTaskDelay(10 / portTICK_PERIOD_MS); // Small delay between reads
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Serial.println("=== SCHRITT 2: AKTUELLER TAG-INHALT ===");
|
|
|
|
|
|
|
|
|
|
// Only read user data pages that are confirmed to be readable
|
|
|
|
|
for (uint8_t page = 4; page < 20; page++) {
|
|
|
|
|
uint8_t pageData[4];
|
|
|
|
|
if (nfc.ntag2xx_ReadPage(page, pageData)) {
|
|
|
|
|
memcpy(¤tContent[(page-4)*4], pageData, 4);
|
|
|
|
|
Serial.print("Seite ");
|
|
|
|
|
Serial.print(page);
|
|
|
|
|
Serial.print(": ");
|
|
|
|
@@ -430,19 +471,82 @@ uint8_t ntag2xx_WriteNDEF(const char *payload) {
|
|
|
|
|
} else {
|
|
|
|
|
Serial.print("Fehler beim Lesen von Seite ");
|
|
|
|
|
Serial.println(page);
|
|
|
|
|
// If we can't read basic user data pages, there's a fundamental problem
|
|
|
|
|
if (page <= 6) {
|
|
|
|
|
Serial.println("KRITISCHER FEHLER: Kann grundlegende User-Data-Seiten nicht lesen!");
|
|
|
|
|
Serial.println("Möglicherweise NFC-Interface-Problem oder Tag-Zustandsproblem");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
vTaskDelay(10 / portTICK_PERIOD_MS); // Small delay between reads
|
|
|
|
|
}
|
|
|
|
|
Serial.println("=========================================");
|
|
|
|
|
|
|
|
|
|
// STEP 2: Simple write test - write one test page
|
|
|
|
|
Serial.println();
|
|
|
|
|
Serial.println("=== SCHRITT 2: SCHREIBTEST ===");
|
|
|
|
|
Serial.println("=== SCHRITT 3: SCHREIBTEST ===");
|
|
|
|
|
Serial.println();
|
|
|
|
|
Serial.println("=== SCHRITT 3: SCHREIBTEST ===");
|
|
|
|
|
|
|
|
|
|
// If basic pages are not readable, try to reinitialize NFC interface
|
|
|
|
|
Serial.println("Versuche NFC-Interface zu stabilisieren...");
|
|
|
|
|
|
|
|
|
|
// Give the NFC interface time to stabilize
|
|
|
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
|
|
|
|
|
|
|
|
// Try to reestablish communication
|
|
|
|
|
bool interfaceOk = false;
|
|
|
|
|
for (int retry = 0; retry < 3; retry++) {
|
|
|
|
|
Serial.print("NFC-Interface Test ");
|
|
|
|
|
Serial.print(retry + 1);
|
|
|
|
|
Serial.print("/3... ");
|
|
|
|
|
|
|
|
|
|
uint8_t ccRetest[4];
|
|
|
|
|
if (nfc.ntag2xx_ReadPage(3, ccRetest)) {
|
|
|
|
|
Serial.println("✓");
|
|
|
|
|
interfaceOk = true;
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
Serial.println("❌");
|
|
|
|
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!interfaceOk) {
|
|
|
|
|
Serial.println("FEHLER: NFC-Interface nicht stabil - Schreibvorgang abgebrochen");
|
|
|
|
|
oledShowMessage("NFC Interface Error");
|
|
|
|
|
vTaskDelay(3000 / portTICK_PERIOD_MS);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Serial.println("NFC-Interface ist stabil - fahre mit Schreibtest fort");
|
|
|
|
|
|
|
|
|
|
uint8_t testPage[4] = {0xAA, 0xBB, 0xCC, 0xDD}; // Test pattern
|
|
|
|
|
|
|
|
|
|
if (!nfc.ntag2xx_WritePage(10, testPage)) { // Use page 10 for test
|
|
|
|
|
Serial.println("FEHLER: Einfacher Schreibtest fehlgeschlagen!");
|
|
|
|
|
Serial.println("Tag ist möglicherweise schreibgeschützt oder defekt");
|
|
|
|
|
oledShowMessage("Tag write protected?");
|
|
|
|
|
|
|
|
|
|
// Additional diagnostics
|
|
|
|
|
Serial.println("=== ERWEITERTE DIAGNOSE ===");
|
|
|
|
|
|
|
|
|
|
// Check if this is a timing issue
|
|
|
|
|
Serial.println("Teste Tag-Erkennung erneut...");
|
|
|
|
|
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
|
uint8_t uidLength;
|
|
|
|
|
bool tagStillPresent = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 1000);
|
|
|
|
|
Serial.print("Tag noch erkannt: ");
|
|
|
|
|
Serial.println(tagStillPresent ? "✓" : "❌");
|
|
|
|
|
|
|
|
|
|
if (!tagStillPresent) {
|
|
|
|
|
Serial.println("URSACHE: Tag wurde während Schreibvorgang entfernt!");
|
|
|
|
|
oledShowMessage("Tag removed during write");
|
|
|
|
|
} else {
|
|
|
|
|
Serial.println("URSACHE: Tag ist vorhanden aber nicht beschreibbar");
|
|
|
|
|
Serial.println("Möglicherweise: Schreibschutz, Defekt, oder Timing-Problem");
|
|
|
|
|
oledShowMessage("Tag write protected?");
|
|
|
|
|
}
|
|
|
|
|
Serial.println("===============================");
|
|
|
|
|
|
|
|
|
|
vTaskDelay(3000 / portTICK_PERIOD_MS);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
@@ -1068,6 +1172,12 @@ bool quickSpoolIdCheck(String uidString) {
|
|
|
|
|
// Fast-path: Read only first 2-3 pages to check for sm_id pattern
|
|
|
|
|
// This dramatically speeds up known spool recognition
|
|
|
|
|
|
|
|
|
|
// CRITICAL: Do not execute during write operations!
|
|
|
|
|
if (nfcWriteInProgress) {
|
|
|
|
|
Serial.println("FAST-PATH: Skipped during write operation");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Serial.println("=== FAST-PATH: Quick sm_id Check ===");
|
|
|
|
|
|
|
|
|
|
// Read first 3 pages (12 bytes) after NDEF header (pages 4-6)
|
|
|
|
@@ -1139,14 +1249,11 @@ void writeJsonToTag(void *parameter) {
|
|
|
|
|
Serial.println(params->payload);
|
|
|
|
|
|
|
|
|
|
nfcReaderState = NFC_WRITING;
|
|
|
|
|
nfcWriteInProgress = true; // Block high-level tag operations during write
|
|
|
|
|
|
|
|
|
|
// IMPORTANT: Do NOT suspend reading task during writing!
|
|
|
|
|
// We need to be able to read during write verification
|
|
|
|
|
// Just set the state to WRITING to prevent scan conflicts
|
|
|
|
|
Serial.println("NFC Write Task starting - Reader remains active for verification");
|
|
|
|
|
|
|
|
|
|
// Small delay to ensure any ongoing operations complete
|
|
|
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
|
|
|
// Do NOT suspend the reading task - we need NFC interface for verification
|
|
|
|
|
// Just use nfcWriteInProgress to prevent scanning and fast-path operations
|
|
|
|
|
Serial.println("NFC Write Task starting - High-level operations blocked, low-level NFC available");
|
|
|
|
|
|
|
|
|
|
//pauseBambuMqttTask = true;
|
|
|
|
|
// aktualisieren der Website wenn sich der Status ändert
|
|
|
|
@@ -1234,9 +1341,8 @@ void writeJsonToTag(void *parameter) {
|
|
|
|
|
sendWriteResult(nullptr, success);
|
|
|
|
|
sendNfcData();
|
|
|
|
|
|
|
|
|
|
// Since we didn't suspend reading, we don't need to re-enable it
|
|
|
|
|
// Just reset the state back to IDLE
|
|
|
|
|
Serial.println("NFC Write Task completed - Reader was never suspended");
|
|
|
|
|
// Only reset the write protection flag - reading task was never suspended
|
|
|
|
|
nfcWriteInProgress = false; // Re-enable high-level tag operations
|
|
|
|
|
pauseBambuMqttTask = false;
|
|
|
|
|
|
|
|
|
|
free(params->payload);
|
|
|
|
@@ -1271,8 +1377,8 @@ void startWriteJsonToTag(const bool isSpoolTag, const char* payload) {
|
|
|
|
|
void scanRfidTask(void * parameter) {
|
|
|
|
|
Serial.println("RFID Task gestartet");
|
|
|
|
|
for(;;) {
|
|
|
|
|
// Wenn geschrieben wird Schleife aussetzen
|
|
|
|
|
if (nfcReaderState != NFC_WRITING && !nfcReadingTaskSuspendRequest && !booting)
|
|
|
|
|
// Skip scanning during write operations, but keep NFC interface active
|
|
|
|
|
if (nfcReaderState != NFC_WRITING && !nfcWriteInProgress && !nfcReadingTaskSuspendRequest && !booting)
|
|
|
|
|
{
|
|
|
|
|
nfcReadingTaskSuspendState = false;
|
|
|
|
|
yield();
|
|
|
|
@@ -1417,8 +1523,17 @@ void scanRfidTask(void * parameter) {
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
nfcReadingTaskSuspendState = true;
|
|
|
|
|
Serial.println("NFC Reading disabled");
|
|
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
|
|
|
|
|
|
|
|
// Different behavior for write protection vs. full suspension
|
|
|
|
|
if (nfcWriteInProgress) {
|
|
|
|
|
// During write: Just pause scanning, don't disable NFC interface
|
|
|
|
|
// Serial.println("NFC Scanning paused during write operation");
|
|
|
|
|
vTaskDelay(100 / portTICK_PERIOD_MS); // Shorter delay during write
|
|
|
|
|
} else {
|
|
|
|
|
// Full suspension requested
|
|
|
|
|
Serial.println("NFC Reading disabled");
|
|
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
yield();
|
|
|
|
|
}
|
|
|
|
|