#ifndef ERROR_HANDLER_H #define ERROR_HANDLER_H #include #ifdef ARDUINO #include #else #include #endif // Error codes enum ErrorCode { ERR_NONE = 0, ERR_NFC_INIT_FAILED = 1, ERR_ESPNOW_INIT_FAILED = 2, ERR_SD_INIT_FAILED = 3, ERR_COMMUNICATION_TIMEOUT = 4, ERR_INVALID_MESSAGE = 5, ERR_PAIRING_FAILED = 6, ERR_NFC_READ_FAILED = 7, ERR_SD_WRITE_FAILED = 8, ERR_MEMORY_ALLOCATION = 9, ERR_HARDWARE_FAULT = 10 }; // Error severity levels enum ErrorSeverity { SEVERITY_INFO = 0, SEVERITY_WARNING = 1, SEVERITY_ERROR = 2, SEVERITY_CRITICAL = 3 }; // Error entry structure struct ErrorEntry { ErrorCode code; ErrorSeverity severity; unsigned long timestamp; char description[64]; }; // Error statistics struct ErrorStats { uint32_t total_errors; uint32_t critical_errors; uint32_t last_error_time; ErrorCode last_error_code; uint32_t nfc_errors; uint32_t communication_errors; uint32_t sd_errors; }; class ErrorHandler { private: static const int MAX_ERROR_HISTORY = 10; ErrorEntry error_history[MAX_ERROR_HISTORY]; int error_index; ErrorStats stats; bool logging_enabled; public: ErrorHandler(); // Error reporting void reportError(ErrorCode code, ErrorSeverity severity, const char* description = nullptr); void reportNFCError(const char* description = nullptr); void reportCommunicationError(const char* description = nullptr); void reportSDError(const char* description = nullptr); // Error handling bool handleError(ErrorCode code); void clearErrors(); // Recovery mechanisms bool attemptNFCRecovery(); bool attemptCommunicationRecovery(); bool attemptSDRecovery(); // Status and statistics ErrorStats getStats() const; bool hasRecentErrors(unsigned long time_window_ms = 5000) const; bool hasCriticalErrors() const; void printErrorHistory() const; void printStats() const; // Configuration void enableLogging(bool enable); void setMaxRetries(int retries); // Watchdog functionality void feedWatchdog(); bool isSystemHealthy() const; }; // Global error handler instance extern ErrorHandler errorHandler; // Convenience macros #define REPORT_ERROR(code, severity, desc) errorHandler.reportError(code, severity, desc) #define REPORT_NFC_ERROR(desc) errorHandler.reportNFCError(desc) #define REPORT_COMM_ERROR(desc) errorHandler.reportCommunicationError(desc) #define REPORT_SD_ERROR(desc) errorHandler.reportSDError(desc) #endif // ERROR_HANDLER_H