feat: enhance OTA upload handling with chunk validation and timeout checks

This commit is contained in:
2025-02-21 09:24:54 +01:00
parent 4477537cec
commit 8182b5f684
6 changed files with 210 additions and 83 deletions

View File

@ -7,7 +7,16 @@
#include "scale.h"
#include "nfc.h"
#define UPLOAD_TIMEOUT_MS 60000 // 60 Sekunden Timeout für den gesamten Upload
#define CHUNK_RESPONSE_TIMEOUT_MS 10000 // 10 Sekunden Timeout pro Chunk
#define MAX_FAILED_CHUNKS 3 // Maximale Anzahl fehlgeschlagener Chunks bevor Abbruch
#define MAX_FILE_SIZE 4000000 // 4MB Limit
static bool tasksAreStopped = false;
static uint32_t lastChunkTime = 0;
static size_t failedChunks = 0;
static size_t expectedOffset = 0;
static size_t totalSize = 0;
void stopAllTasks() {
Serial.println("Stopping RFID Reader");
@ -80,24 +89,44 @@ void checkForStagedUpdate() {
void handleOTAUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
static File stagingFile;
static uint32_t uploadStartTime = 0;
if (!index) {
// Überprüfe Gesamtgröße im Header
if (request->hasHeader("X-Total-Size")) {
totalSize = request->header("X-Total-Size").toInt();
if (totalSize > MAX_FILE_SIZE) {
request->send(413, "application/json",
"{\"status\":\"error\",\"message\":\"File too large\"}");
return;
}
}
uploadStartTime = millis();
lastChunkTime = millis();
expectedOffset = 0;
failedChunks = 0;
bool isSpiffsUpdate = filename.endsWith("_spiffs.bin");
Serial.printf("Update Start: %s (type: %s)\n", filename.c_str(), isSpiffsUpdate ? "SPIFFS" : "OTA");
Serial.printf("Total size: %u bytes\n", totalSize);
// Überprüfe Header für Chunk-Informationen
if (request->hasHeader("X-Chunk-Offset")) {
String offsetStr = request->header("X-Chunk-Offset");
expectedOffset = offsetStr.toInt();
}
if (request->contentLength() == 0) {
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Invalid file size\"}");
return;
}
// Stop tasks before update
if (!tasksAreStopped && (RfidReaderTask || BambuMqttTask || ScaleTask)) {
if (!tasksAreStopped) {
stopAllTasks();
tasksAreStopped = true;
}
size_t updateSize = request->contentLength();
if (isSpiffsUpdate) {
if (!SPIFFS.begin(true)) {
request->send(400, "application/json",
@ -105,15 +134,13 @@ void handleOTAUpload(AsyncWebServerRequest *request, String filename, size_t ind
return;
}
// Start SPIFFS update
if (!Update.begin(updateSize, U_SPIFFS)) {
if (!Update.begin(totalSize > 0 ? totalSize : request->contentLength(), U_SPIFFS)) {
Update.printError(Serial);
request->send(400, "application/json",
"{\"status\":\"error\",\"message\":\"SPIFFS update initialization failed\"}");
return;
}
} else {
// Regular OTA update
stagingFile = SPIFFS.open("/firmware.bin", "w");
if (!stagingFile) {
request->send(400, "application/json",
@ -123,9 +150,53 @@ void handleOTAUpload(AsyncWebServerRequest *request, String filename, size_t ind
}
}
// Chunk Validierung
if (request->hasHeader("X-Chunk-Offset")) {
size_t chunkOffset = request->header("X-Chunk-Offset").toInt();
if (chunkOffset != expectedOffset) {
failedChunks++;
if (failedChunks >= MAX_FAILED_CHUNKS) {
if (stagingFile) {
stagingFile.close();
SPIFFS.remove("/firmware.bin");
}
Update.abort();
request->send(400, "application/json",
"{\"status\":\"error\",\"message\":\"Too many failed chunks\"}");
return;
}
request->send(400, "application/json",
"{\"status\":\"error\",\"message\":\"Invalid chunk offset\"}");
return;
}
}
// Timeout Überprüfungen
uint32_t currentTime = millis();
if (currentTime - uploadStartTime > UPLOAD_TIMEOUT_MS) {
if (stagingFile) {
stagingFile.close();
SPIFFS.remove("/firmware.bin");
}
Update.abort();
request->send(408, "application/json", "{\"status\":\"error\",\"message\":\"Upload timeout\"}");
return;
}
if (currentTime - lastChunkTime > CHUNK_RESPONSE_TIMEOUT_MS) {
if (stagingFile) {
stagingFile.close();
SPIFFS.remove("/firmware.bin");
}
Update.abort();
request->send(408, "application/json", "{\"status\":\"error\",\"message\":\"Chunk timeout\"}");
return;
}
lastChunkTime = currentTime;
if (stagingFile) {
// Stage 1: Write to SPIFFS
if (stagingFile.write(data, len) != len) {
size_t written = stagingFile.write(data, len);
if (written != len) {
stagingFile.close();
SPIFFS.remove("/firmware.bin");
request->send(400, "application/json",
@ -133,7 +204,6 @@ void handleOTAUpload(AsyncWebServerRequest *request, String filename, size_t ind
return;
}
} else {
// Direct SPIFFS update
if (Update.write(data, len) != len) {
Update.printError(Serial);
request->send(400, "application/json",
@ -142,16 +212,17 @@ void handleOTAUpload(AsyncWebServerRequest *request, String filename, size_t ind
}
}
expectedOffset += len;
if (final) {
if (stagingFile) {
// Finish Stage 1
stagingFile.close();
Serial.println("Stage 1 complete - firmware staged in SPIFFS");
request->send(200, "application/json",
"{\"status\":\"success\",\"message\":\"Update staged successfully! Starting stage 2...\"}");
delay(100);
performStageTwo();
} else {
// Finish direct SPIFFS update
if (!Update.end(true)) {
Update.printError(Serial);
request->send(400, "application/json",