/* * ═══════════════════════════════════════════════════════════════════════════════════ * OTAMANAGER.HPP - Over-The-Air Update Management System * ═══════════════════════════════════════════════════════════════════════════════════ * * 🔄 THE UPDATE ORCHESTRATOR OF VESPER 🔄 * * This class manages over-the-air firmware updates with safe, reliable * update mechanisms, version checking, and comprehensive error handling. * * 📋 VERSION: 2.1 (Enhanced with scheduled checks and full validation) * 📅 DATE: 2025 * 👨‍💻 AUTHOR: Advanced Bell Systems * ═══════════════════════════════════════════════════════════════════════════════════ */ #pragma once #include #include #include #include #include #include #include #include #include #include #include "../FileManager/FileManager.hpp" #include "../Telemetry/Telemetry.hpp" #if HAS_RTC #include "../TimeKeeper/TimeKeeper.hpp" #endif #include "../Version.hpp" class ConfigManager; // Forward declaration class Player; // Forward declaration for idle check #if HAS_RTC class Timekeeper; // Forward declaration for freeze mode #endif class Telemetry; // Forward declaration for freeze mode class OTAManager { public: enum class Status { IDLE, CHECKING_VERSION, DOWNLOADING, INSTALLING, SUCCESS, FAILED }; enum class ErrorCode { NONE, HTTP_ERROR, VERSION_CHECK_FAILED, DOWNLOAD_FAILED, INSUFFICIENT_SPACE, WRITE_FAILED, VERIFICATION_FAILED, CHECKSUM_MISMATCH, METADATA_PARSE_FAILED, SIZE_MISMATCH, VERSION_TOO_LOW, CHANNEL_MISMATCH, PLAYER_ACTIVE }; // Callback types using ProgressCallback = std::function; using StatusCallback = std::function; explicit OTAManager(ConfigManager& configManager); ~OTAManager(); void begin(); void setFileManager(FileManager* fm); void setPlayer(Player* player); // Set player reference for idle check #if HAS_RTC void setTimeKeeper(Timekeeper* tk); // Set timekeeper reference for freeze mode #endif void setTelemetry(Telemetry* telemetry); // Set telemetry reference for freeze mode void checkForUpdates(); void checkForUpdates(const String& channel); // Check specific channel void checkForEmergencyUpdates(); // NEW: Scheduled emergency-only check void update(); void update(const String& channel); // Update from specific channel void checkFirmwareUpdateFromSD(); // Check SD for firmware update bool performManualUpdate(); // Manual update triggered by app bool performManualUpdate(const String& channel); // Manual update from specific channel bool performCustomUpdate(const String& firmwareUrl, const String& checksum = "", size_t fileSize = 0, const String& version = ""); // Custom firmware update // Hardware identification String getHardwareVariant() const; void setHardwareVariant(const String& variant); // Deprecated: Use ConfigManager instead // Status and info Status getStatus() const { return _status; } ErrorCode getLastError() const { return _lastError; } SemVer getCurrentVersion() const; SemVer getAvailableVersion() const { return _availableVersion; } bool isUpdateAvailable() const { return _updateAvailable; } // millis() at the start of the most recent update-check attempt (scheduled, // emergency, or manual), or 0 if no check has run yet this boot. uint32_t getLastCheckUptimeMs() const { return _lastCheckUptimeMs; } // Human-readable name for an ErrorCode, e.g. "DOWNLOAD_FAILED". Used by // diagnostics_report and telemetry.get_diagnostics. static const char* errorCodeToString(ErrorCode code); // Callbacks void setProgressCallback(ProgressCallback callback) { _progressCallback = callback; } void setStatusCallback(StatusCallback callback) { _statusCallback = callback; } // ═══════════════════════════════════════════════════════════════════════════════ // HEALTH CHECK METHOD // ═══════════════════════════════════════════════════════════════════════════════ /** @brief Check if OTAManager is in healthy state */ bool isHealthy() const; private: ConfigManager& _configManager; FileManager* _fileManager; Player* _player; // Player reference for idle check #if HAS_RTC Timekeeper* _timeKeeper; // TimeKeeper reference for freeze mode #endif Telemetry* _telemetry; // Telemetry reference for freeze mode Status _status; ErrorCode _lastError; SemVer _availableVersion; SemVer _minVersion; size_t _expectedFileSize; // NEW: Expected firmware file size bool _updateAvailable; uint32_t _lastCheckUptimeMs = 0; // See getLastCheckUptimeMs() String _availableChecksum; String _availableDownloadPath; // Relative path from backend (e.g. /api/firmware/.../firmware.bin) String _updateChannel; bool _isMandatory; bool _isEmergency; ProgressCallback _progressCallback; StatusCallback _statusCallback; // NEW: Scheduled check timer TimerHandle_t _scheduledCheckTimer; static void scheduledCheckCallback(TimerHandle_t xTimer); // Initial boot check timer (non-blocking delayed check) TimerHandle_t _initialCheckTimer; static void initialCheckCallback(TimerHandle_t xTimer); void performInitialCheck(); // Async initial check after boot // Worker task for OTA operations (prevents stack overflow in timer callbacks) TaskHandle_t _otaWorkerTask; SemaphoreHandle_t _otaWorkSignal; static void otaWorkerTaskFunction(void* parameter); void otaWorkerLoop(); enum class OTAWorkType { NONE, INITIAL_CHECK, SCHEDULED_CHECK }; OTAWorkType _pendingWork; void setStatus(Status status, ErrorCode error = ErrorCode::NONE); void notifyProgress(size_t current, size_t total); bool checkVersion(); bool checkVersion(const String& channel); bool checkChannelsMetadata(); bool downloadAndInstall(); bool downloadAndInstall(const String& channel); bool downloadDirectToFlash(const String& url, size_t expectedSize, const String& fromVersion, const String& toVersion, const String& channel); bool downloadToSD(const String& url, const String& expectedChecksum, size_t expectedSize); bool verifyChecksum(const String& filePath, const String& expectedChecksum); String calculateSHA256(const String& filePath); bool installFromSD(const String& filePath); // FastAPI URL builders String buildLatestUrl(const String& baseUrl, const String& hwType, const String& channel) const; String buildVersionInfoUrl(const String& baseUrl, const String& hwType, const String& channel, const String& version) const; String buildFirmwareUrl(const String& baseUrl, const String& hwType, const String& channel, const String& version) const; // OTA telemetry — best-effort POSTs, never block or fail the update void postDownloadTelemetry(const String& fromVersion, const String& toVersion, const String& channel); void postFlashTelemetry(const String& fromVersion, const String& toVersion, const String& channel); // NEW: Helper methods bool isPlayerActive() const; bool checkAvailableSpace(size_t requiredBytes) const; };