diff --git a/src/main.cpp b/src/main.cpp index 1b1b728..06255a5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -70,6 +70,7 @@ bool otaShaStarted = false; bool otaReady = false; bool otaEnded = false; bool otaConfirmed = false; +bool otaEarlyConfirm = false; uint32_t otaEndedAt = 0; #define OTA_CONFIRM_TIMEOUT_MS 10000 @@ -191,6 +192,7 @@ static void otaReset() { otaReady = false; otaEnded = false; otaConfirmed = false; + otaEarlyConfirm = false; if (otaShaStarted) { mbedtls_sha256_free(&otaShaCtx); otaShaStarted = false; @@ -296,7 +298,8 @@ class OtaCharacteristicCallbacks : public NimBLECharacteristicCallbacks { Serial.printf("OTA: 0xFF received — otaReady=%d, otaEnded=%d, otaConfirmed=%d, otaInProgress=%d\n", otaReady, otaEnded, otaConfirmed, otaInProgress); if (!otaEnded) { - Serial.println("OTA: 0xFF IGNORED — otaEnded is false (esp_ota_end not yet called or failed)"); + Serial.println("OTA: 0xFF arrived early (esp_ota_end not yet called), deferring"); + otaEarlyConfirm = true; return; } Serial.println("OTA: 0xFF confirm accepted, setting otaConfirmed=true"); @@ -550,6 +553,12 @@ void loop() { otaReady, otaEnded, otaConfirmed, otaInProgress); } + if (otaEnded && otaEarlyConfirm) { + Serial.println("OTA: processing deferred 0xFF confirm"); + otaConfirmed = true; + otaEarlyConfirm = false; + } + if (otaEnded && !otaConfirmed && (millis() - otaEndedAt > OTA_CONFIRM_TIMEOUT_MS)) { Serial.printf("OTA: 0xFF confirm timeout (%lu ms) — discarding update, resuming normal operation\n", millis() - otaEndedAt);