refactor: enhance NFC write operation handling and prevent tag operations during write
This commit is contained in:
		
							
								
								
									
										26
									
								
								src/nfc.cpp
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								src/nfc.cpp
									
									
									
									
									
								
							| @@ -23,6 +23,7 @@ bool tagProcessed = false; | |||||||
| volatile bool pauseBambuMqttTask = false; | volatile bool pauseBambuMqttTask = false; | ||||||
| volatile bool nfcReadingTaskSuspendRequest = false; | volatile bool nfcReadingTaskSuspendRequest = false; | ||||||
| volatile bool nfcReadingTaskSuspendState = false; | volatile bool nfcReadingTaskSuspendState = false; | ||||||
|  | volatile bool nfcWriteInProgress = false; // Prevent any tag operations during write | ||||||
|  |  | ||||||
| struct NfcWriteParameterType { | struct NfcWriteParameterType { | ||||||
|   bool tagType; |   bool tagType; | ||||||
| @@ -1068,6 +1069,12 @@ bool quickSpoolIdCheck(String uidString) { | |||||||
|     // Fast-path: Read only first 2-3 pages to check for sm_id pattern |     // Fast-path: Read only first 2-3 pages to check for sm_id pattern | ||||||
|     // This dramatically speeds up known spool recognition |     // 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 ==="); |     Serial.println("=== FAST-PATH: Quick sm_id Check ==="); | ||||||
|      |      | ||||||
|     // Read first 3 pages (12 bytes) after NDEF header (pages 4-6) |     // Read first 3 pages (12 bytes) after NDEF header (pages 4-6) | ||||||
| @@ -1139,14 +1146,16 @@ void writeJsonToTag(void *parameter) { | |||||||
|   Serial.println(params->payload); |   Serial.println(params->payload); | ||||||
|  |  | ||||||
|   nfcReaderState = NFC_WRITING; |   nfcReaderState = NFC_WRITING; | ||||||
|  |   nfcWriteInProgress = true; // Block all tag operations during write | ||||||
|  |  | ||||||
|   // IMPORTANT: Do NOT suspend reading task during writing! |   // Suspend reading task during writing to prevent interference | ||||||
|   // We need to be able to read during write verification |   // But keep low-level NFC operations available for verification | ||||||
|   // Just set the state to WRITING to prevent scan conflicts |   nfcReadingTaskSuspendRequest = true; | ||||||
|   Serial.println("NFC Write Task starting - Reader remains active for verification"); |   while(nfcReadingTaskSuspendState == false){ | ||||||
|  |     vTaskDelay(100 / portTICK_PERIOD_MS); | ||||||
|  |   } | ||||||
|    |    | ||||||
|   // Small delay to ensure any ongoing operations complete |   Serial.println("NFC Write Task starting - All tag operations blocked during write"); | ||||||
|   vTaskDelay(100 / portTICK_PERIOD_MS); |  | ||||||
|  |  | ||||||
|   //pauseBambuMqttTask = true; |   //pauseBambuMqttTask = true; | ||||||
|   // aktualisieren der Website wenn sich der Status ändert |   // aktualisieren der Website wenn sich der Status ändert | ||||||
| @@ -1234,9 +1243,8 @@ void writeJsonToTag(void *parameter) { | |||||||
|   sendWriteResult(nullptr, success); |   sendWriteResult(nullptr, success); | ||||||
|   sendNfcData(); |   sendNfcData(); | ||||||
|  |  | ||||||
|   // Since we didn't suspend reading, we don't need to re-enable it |   nfcReadingTaskSuspendRequest = false; | ||||||
|   // Just reset the state back to IDLE |   nfcWriteInProgress = false; // Re-enable tag operations | ||||||
|   Serial.println("NFC Write Task completed - Reader was never suspended"); |  | ||||||
|   pauseBambuMqttTask = false; |   pauseBambuMqttTask = false; | ||||||
|  |  | ||||||
|   free(params->payload); |   free(params->payload); | ||||||
|   | |||||||
| @@ -24,6 +24,7 @@ extern String activeSpoolId; | |||||||
| extern String lastSpoolId; | extern String lastSpoolId; | ||||||
| extern volatile nfcReaderStateType nfcReaderState; | extern volatile nfcReaderStateType nfcReaderState; | ||||||
| extern volatile bool pauseBambuMqttTask; | extern volatile bool pauseBambuMqttTask; | ||||||
|  | extern volatile bool nfcWriteInProgress; | ||||||
| extern bool tagProcessed; | extern bool tagProcessed; | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user