diff --git a/android/app/src/main/assets/firmware/server/firmware.bin b/android/app/src/main/assets/firmware/server/firmware.bin index 3bfedd2..0e91add 100644 Binary files a/android/app/src/main/assets/firmware/server/firmware.bin and b/android/app/src/main/assets/firmware/server/firmware.bin differ diff --git a/android/app/src/main/java/nl/eddydevink/sylda/BleRelayManager.kt b/android/app/src/main/java/nl/eddydevink/sylda/BleRelayManager.kt index 29ceeae..cf5a98e 100644 --- a/android/app/src/main/java/nl/eddydevink/sylda/BleRelayManager.kt +++ b/android/app/src/main/java/nl/eddydevink/sylda/BleRelayManager.kt @@ -373,7 +373,8 @@ class BleRelayManager( ((value[3].toInt() and 0xFF) shl 8) or (value[4].toInt() and 0xFF) } else 0 - Log.d(TAG, "OTA notify: status=$status seq=$seq") + val statusName = when (status) { 0x00 -> "ACK"; 0x01 -> "NAK"; 0x10 -> "COMPLETE"; 0x11 -> "ERROR"; else -> "UNKNOWN(0x${status.toString(16)})" } + Log.d(TAG, "OTA notify: status=$statusName seq=$seq raw=${value.joinToString("") { "%02X".format(it) }}") handler.post { otaNotifyCallback?.onOtaNotify(status, seq) } } } diff --git a/android/app/src/main/java/nl/eddydevink/sylda/OtaManager.kt b/android/app/src/main/java/nl/eddydevink/sylda/OtaManager.kt index 993ad07..0b2d3c9 100644 --- a/android/app/src/main/java/nl/eddydevink/sylda/OtaManager.kt +++ b/android/app/src/main/java/nl/eddydevink/sylda/OtaManager.kt @@ -65,6 +65,7 @@ class OtaManager( val notifyCallback = object : BleRelayManager.OtaNotifyCallback { override fun onOtaNotify(status: Int, seq: Int) { + Log.d(TAG, "OTA notify received: status=0x${status.toString(16)} seq=$seq (0x00=ACK, 0x01=NAK, 0x10=COMPLETE, 0x11=ERROR)") when (status) { 0x00 -> chunkAckReceived = true 0x01 -> chunkNakReceived = true @@ -163,7 +164,9 @@ class OtaManager( withContext(Dispatchers.Main) { callback.onProgress(99, "Hash verifiëren...") } finalStatusReceived = false - relayManager.sendOtaData(sha256) + Log.d(TAG, "Sending SHA256 hash (${sha256.size} bytes) to ESP") + val shaSent = relayManager.sendOtaData(sha256) + Log.d(TAG, "SHA256 write result: $shaSent") try { withTimeout(OTA_FINAL_TIMEOUT_MS) { @@ -173,12 +176,17 @@ class OtaManager( } } } catch (_: kotlinx.coroutines.TimeoutCancellationException) { - Log.w(TAG, "Timeout waiting for final OTA status") + Log.w(TAG, "Timeout waiting for final OTA status after SHA256") } + Log.d(TAG, "Final status: received=$finalStatusReceived status=$finalStatus (0x10=COMPLETE, 0x11=ERROR)") + relayManager.setOtaNotifyCallback(null) if (finalStatus == 0x10) { + Log.d(TAG, "ESP confirmed SHA256 OK, sending 0xFF reboot confirmation") + val confirmSent = relayManager.sendOtaData(byteArrayOf(0xFF.toByte())) + Log.d(TAG, "0xFF write result: $confirmSent") withContext(Dispatchers.Main) { callback.onProgress(100, "Herstart...") callback.onComplete() diff --git a/src/client.cpp b/src/client.cpp index 5053873..2f53914 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -424,7 +424,7 @@ void loop() { } static uint32_t heartbeat = 0; - if (millis() - heartbeat > 5000) { + if (millis() - heartbeat > 60000) { Serial.printf("[heartbeat] t1=%s t2=%s btn=%s r1=%s r2=%s connected=%s\n", t1Active ? "YES" : "no", t2Active ? "YES" : "no", diff --git a/src/main.cpp b/src/main.cpp index faa8f80..75a67ef 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -63,6 +63,11 @@ uint32_t otaWritten = 0; uint8_t otaExpectedSha[32]; mbedtls_sha256_context otaShaCtx; bool otaShaStarted = false; +bool otaReady = false; +bool otaEnded = false; +bool otaConfirmed = false; +uint32_t otaEndedAt = 0; +#define OTA_CONFIRM_TIMEOUT_MS 10000 uint8_t hmacKey[HMAC_KEY_LEN]; uint8_t deviceUuid[ESP_UUID_LEN]; @@ -183,6 +188,9 @@ static void otaReset() { otaPartition = nullptr; otaExpectedSize = 0; otaWritten = 0; + otaReady = false; + otaEnded = false; + otaConfirmed = false; if (otaShaStarted) { mbedtls_sha256_free(&otaShaCtx); otaShaStarted = false; @@ -258,36 +266,40 @@ class OtaCharacteristicCallbacks : public NimBLECharacteristicCallbacks { mbedtls_sha256_finish_ret(&otaShaCtx, computedSha); mbedtls_sha256_free(&otaShaCtx); otaShaStarted = false; - esp_err_t err = esp_ota_end(otaHandle); - if (err != ESP_OK) { - Serial.printf("OTA: end failed (%d)\n", err); - otaReset(); - sendOtaNotify(pChar, OTA_STATUS_ERROR, 0); - return; - } + + Serial.printf("OTA: SHA256 verify — written=%lu expected=%lu\n", otaWritten, otaExpectedSize); if (memcmp(computedSha, otaExpectedSha, 32) != 0) { Serial.println("OTA: SHA256 mismatch — aborting"); + esp_ota_abort(otaHandle); otaReset(); sendOtaNotify(pChar, OTA_STATUS_ERROR, 0); return; } - err = esp_ota_set_boot_partition(otaPartition); - if (err != ESP_OK) { - Serial.printf("OTA: set boot partition failed (%d)\n", err); - otaReset(); - sendOtaNotify(pChar, OTA_STATUS_ERROR, 0); - return; - } - leds[0] = CRGB::Green; - FastLED.show(); + + Serial.println("OTA: SHA256 OK, setting otaReady=true, sending COMPLETE notify"); + otaReady = true; sendOtaNotify(pChar, OTA_STATUS_COMPLETE, 0); - Serial.println("OTA: SHA256 OK, rebooting..."); - delay(500); - esp_restart(); + Serial.printf("OTA: COMPLETE notify sent, otaReady=%d, otaConfirmed=%d, otaInProgress=%d\n", + otaReady, otaConfirmed, otaInProgress); return; } - if (!otaInProgress) return; + if (!otaInProgress) { + Serial.printf("OTA: write ignored — otaInProgress=false (len=%d, data0=0x%02X)\n", len, data[0]); + return; + } + + if (len == 1 && data[0] == 0xFF) { + 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)"); + return; + } + Serial.println("OTA: 0xFF confirm accepted, setting otaConfirmed=true"); + otaConfirmed = true; + return; + } if (data[0] < 0x10) { uint32_t seq = ((uint32_t)data[0] << 24) | ((uint32_t)data[1] << 16) | @@ -521,6 +533,46 @@ void loop() { } } + if (otaReady) { + Serial.printf("OTA: loop() processing otaReady — otaWritten=%lu, otaExpectedSize=%lu\n", + otaWritten, otaExpectedSize); + esp_err_t err = esp_ota_end(otaHandle); + if (err != ESP_OK) { + Serial.printf("OTA: esp_ota_end failed (%d) — aborting\n", err); + otaReset(); + } else { + Serial.println("OTA: esp_ota_end OK, setting otaEnded=true, waiting for 0xFF confirm"); + otaEnded = true; + otaEndedAt = millis(); + leds[0] = CRGB::Yellow; + FastLED.show(); + } + otaReady = false; + Serial.printf("OTA: otaReady cleared — otaReady=%d, otaEnded=%d, otaConfirmed=%d, otaInProgress=%d\n", + otaReady, otaEnded, otaConfirmed, otaInProgress); + } + + 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); + otaReset(); + } + + if (otaConfirmed) { + Serial.println("OTA: otaConfirmed=true, setting boot partition"); + esp_err_t err = esp_ota_set_boot_partition(otaPartition); + if (err != ESP_OK) { + Serial.printf("OTA: set boot partition failed (%d)\n", err); + } else { + leds[0] = CRGB::Green; + FastLED.show(); + Serial.println("OTA: done, rebooting..."); + delay(500); + esp_restart(); + } + otaReset(); + } + static bool t1Stable = false; static bool t2Stable = false; static bool btnStable = false; @@ -574,7 +626,7 @@ void loop() { } static uint32_t heartbeat = 0; - if (!otaInProgress && millis() - heartbeat > 5000) { + if (!otaInProgress && millis() - heartbeat > 60000) { bool anyBle1 = (connActive[0] && bleRelay1[0]) || (connActive[1] && bleRelay1[1]); bool anyBle2 = (connActive[0] && bleRelay2[0]) || (connActive[1] && bleRelay2[1]); Serial.printf("[heartbeat] R1=%s R2=%s t1=%s t2=%s btn=%s ble1=%s ble2=%s clients=%d\n", diff --git a/version.txt b/version.txt index 9f9522d..cf5c08b 100644 --- a/version.txt +++ b/version.txt @@ -1,2 +1,2 @@ -server=8 +server=15 client=1