2025-02-17 12:41:25 +01:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include "ota.h"
|
2025-02-18 11:42:52 +01:00
|
|
|
#include <Update.h>
|
|
|
|
#include <SPIFFS.h>
|
|
|
|
#include "commonFS.h"
|
2025-02-20 11:52:36 +01:00
|
|
|
#include "bambu.h"
|
|
|
|
#include "scale.h"
|
|
|
|
#include "nfc.h"
|
2025-02-18 11:42:52 +01:00
|
|
|
|
2025-02-20 10:53:23 +01:00
|
|
|
// Magic byte patterns für verschiedene Image-Typen
|
|
|
|
const uint8_t FIRMWARE_MAGIC = 0xE9;
|
|
|
|
const uint8_t ESP_MAGIC = 0xE9;
|
2025-02-20 14:08:17 +01:00
|
|
|
static bool tasksAreStopped = false;
|
2025-02-20 10:53:23 +01:00
|
|
|
|
2025-02-20 11:52:36 +01:00
|
|
|
void stopAllTasks() {
|
2025-02-20 11:42:36 +01:00
|
|
|
// Stop all tasks
|
2025-02-20 14:08:17 +01:00
|
|
|
Serial.println("Stopping RFID Reader");
|
|
|
|
if (RfidReaderTask) vTaskSuspend(RfidReaderTask);
|
|
|
|
Serial.println("Stopping Bambu");
|
|
|
|
if (BambuMqttTask) vTaskSuspend(BambuMqttTask);
|
|
|
|
Serial.println("Stopping Scale");
|
|
|
|
if (ScaleTask) vTaskSuspend(ScaleTask);
|
2025-02-20 12:00:45 +01:00
|
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
|
|
Serial.println("All tasks stopped");
|
2025-02-20 11:42:36 +01:00
|
|
|
}
|
|
|
|
|
2025-02-18 11:42:52 +01:00
|
|
|
void handleOTAUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
|
2025-02-18 14:18:14 +01:00
|
|
|
static size_t contentLength = 0;
|
2025-02-20 10:53:23 +01:00
|
|
|
static bool isFullImage = false;
|
|
|
|
static uint32_t currentOffset = 0;
|
2025-02-20 14:45:34 +01:00
|
|
|
static uint8_t *spiffsBuffer = nullptr;
|
|
|
|
static size_t spiffsSize = 0x30000; // 192KB SPIFFS size
|
|
|
|
static size_t spiffsStored = 0;
|
2025-02-20 14:31:10 +01:00
|
|
|
|
2025-02-18 11:42:52 +01:00
|
|
|
if (!index) {
|
2025-02-18 14:18:14 +01:00
|
|
|
contentLength = request->contentLength();
|
2025-02-20 14:31:10 +01:00
|
|
|
Serial.printf("Update size: %u bytes (0x%X)\n", contentLength, contentLength);
|
2025-02-18 14:18:14 +01:00
|
|
|
|
|
|
|
if (contentLength == 0) {
|
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Invalid file size\"}");
|
2025-02-18 11:42:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-02-20 14:16:49 +01:00
|
|
|
if (!tasksAreStopped && (RfidReaderTask || BambuMqttTask || ScaleTask)) {
|
|
|
|
stopAllTasks();
|
|
|
|
tasksAreStopped = true;
|
|
|
|
}
|
|
|
|
|
2025-02-20 14:45:34 +01:00
|
|
|
isFullImage = (contentLength > 0x300000); // Über 3MB ist ein full image
|
2025-02-20 14:28:11 +01:00
|
|
|
|
2025-02-20 14:45:34 +01:00
|
|
|
if (isFullImage) {
|
|
|
|
// Alloziere Buffer für SPIFFS-Daten
|
|
|
|
spiffsBuffer = (uint8_t*)malloc(spiffsSize);
|
|
|
|
if (!spiffsBuffer) {
|
|
|
|
Serial.println("Failed to allocate SPIFFS buffer");
|
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Memory allocation failed\"}");
|
2025-02-20 14:31:10 +01:00
|
|
|
return;
|
|
|
|
}
|
2025-02-20 14:45:34 +01:00
|
|
|
memset(spiffsBuffer, 0xFF, spiffsSize);
|
|
|
|
spiffsStored = 0;
|
2025-02-20 14:31:10 +01:00
|
|
|
|
2025-02-20 14:45:34 +01:00
|
|
|
// Starte Update mit der Firmware-Größe
|
|
|
|
if (!Update.begin(0x3D0000, U_FLASH)) { // Nur bis zum SPIFFS-Start
|
|
|
|
Serial.println("Could not begin full image update");
|
|
|
|
free(spiffsBuffer);
|
|
|
|
spiffsBuffer = nullptr;
|
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Could not start full update\"}");
|
2025-02-20 14:16:49 +01:00
|
|
|
return;
|
2025-02-20 11:27:11 +01:00
|
|
|
}
|
2025-02-20 10:53:23 +01:00
|
|
|
} else {
|
2025-02-20 14:45:34 +01:00
|
|
|
// Normales Firmware-Update
|
|
|
|
if (!Update.begin(contentLength)) {
|
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Not enough space available\"}");
|
2025-02-20 14:16:49 +01:00
|
|
|
return;
|
|
|
|
}
|
2025-02-18 11:42:52 +01:00
|
|
|
}
|
2025-02-20 14:45:34 +01:00
|
|
|
|
2025-02-20 10:53:23 +01:00
|
|
|
currentOffset = 0;
|
2025-02-20 14:45:34 +01:00
|
|
|
Serial.printf("%s update started\n", isFullImage ? "Full image" : "Firmware");
|
2025-02-18 11:42:52 +01:00
|
|
|
}
|
2025-02-20 14:16:49 +01:00
|
|
|
|
2025-02-20 14:45:34 +01:00
|
|
|
if (isFullImage && currentOffset >= 0x3D0000) {
|
|
|
|
// SPIFFS-Daten sammeln
|
|
|
|
size_t spiffsOffset = currentOffset - 0x3D0000;
|
|
|
|
if (spiffsOffset < spiffsSize) {
|
|
|
|
size_t copyLen = min(len, spiffsSize - spiffsOffset);
|
|
|
|
memcpy(spiffsBuffer + spiffsOffset, data, copyLen);
|
|
|
|
spiffsStored += copyLen;
|
|
|
|
Serial.printf("Stored SPIFFS data: offset=0x%X, len=%u\n", spiffsOffset, copyLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nur die Firmware-Daten an Update.write() übergeben
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Schreibe Firmware-Daten
|
|
|
|
size_t writeLen = isFullImage ? min(len, 0x3D0000 - currentOffset) : len;
|
|
|
|
if (writeLen > 0) {
|
|
|
|
if (Update.write(data, writeLen) != writeLen) {
|
|
|
|
String errorMsg = Update.errorString();
|
|
|
|
if (errorMsg != "No Error") {
|
|
|
|
Update.printError(Serial);
|
|
|
|
if (spiffsBuffer) {
|
|
|
|
free(spiffsBuffer);
|
|
|
|
spiffsBuffer = nullptr;
|
|
|
|
}
|
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Error writing update: " + errorMsg + "\"}");
|
|
|
|
return;
|
|
|
|
}
|
2025-02-20 11:13:49 +01:00
|
|
|
}
|
2025-02-18 11:42:52 +01:00
|
|
|
}
|
2025-02-20 10:53:23 +01:00
|
|
|
|
2025-02-20 14:45:34 +01:00
|
|
|
currentOffset += len;
|
|
|
|
|
2025-02-20 14:31:10 +01:00
|
|
|
// Progress logging
|
2025-02-20 14:45:34 +01:00
|
|
|
if ((currentOffset % 0x40000) == 0) {
|
2025-02-20 14:31:10 +01:00
|
|
|
Serial.printf("Update progress: 0x%X of 0x%X bytes (%.1f%%)\n",
|
2025-02-20 14:45:34 +01:00
|
|
|
currentOffset, contentLength, (currentOffset * 100.0) / contentLength);
|
2025-02-20 14:31:10 +01:00
|
|
|
}
|
|
|
|
|
2025-02-18 11:42:52 +01:00
|
|
|
if (final) {
|
2025-02-20 14:45:34 +01:00
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
// Beende Firmware-Update
|
|
|
|
if (!Update.end(true)) {
|
|
|
|
success = false;
|
2025-02-20 10:06:06 +01:00
|
|
|
String errorMsg = Update.errorString();
|
2025-02-20 11:13:49 +01:00
|
|
|
if (errorMsg != "No Error") {
|
|
|
|
Update.printError(Serial);
|
2025-02-20 14:45:34 +01:00
|
|
|
if (spiffsBuffer) {
|
|
|
|
free(spiffsBuffer);
|
|
|
|
spiffsBuffer = nullptr;
|
|
|
|
}
|
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Firmware update failed: " + errorMsg + "\"}");
|
|
|
|
return;
|
2025-02-20 11:13:49 +01:00
|
|
|
}
|
2025-02-18 11:42:52 +01:00
|
|
|
}
|
2025-02-20 14:45:34 +01:00
|
|
|
|
|
|
|
// SPIFFS aktualisieren wenn es ein full image war
|
|
|
|
if (success && isFullImage && spiffsBuffer && spiffsStored > 0) {
|
|
|
|
Serial.printf("Starting SPIFFS update (size: 0x%X)\n", spiffsStored);
|
|
|
|
|
|
|
|
if (SPIFFS.begin(true)) {
|
|
|
|
SPIFFS.end(); // Unmount first
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Update.begin(spiffsSize, U_SPIFFS)) {
|
|
|
|
Serial.println("Could not begin SPIFFS update");
|
|
|
|
free(spiffsBuffer);
|
|
|
|
spiffsBuffer = nullptr;
|
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"SPIFFS update failed to start\"}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Update.write(spiffsBuffer, spiffsSize) != spiffsSize) {
|
|
|
|
Serial.println("SPIFFS Write Failed");
|
|
|
|
Update.printError(Serial);
|
|
|
|
free(spiffsBuffer);
|
|
|
|
spiffsBuffer = nullptr;
|
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"SPIFFS write failed\"}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Update.end(true)) {
|
|
|
|
Serial.println("SPIFFS End Failed");
|
|
|
|
Update.printError(Serial);
|
|
|
|
free(spiffsBuffer);
|
|
|
|
spiffsBuffer = nullptr;
|
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"SPIFFS finish failed\"}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
free(spiffsBuffer);
|
|
|
|
spiffsBuffer = nullptr;
|
|
|
|
|
|
|
|
// Verify SPIFFS
|
|
|
|
if (!SPIFFS.begin(true)) {
|
|
|
|
Serial.println("SPIFFS mount after update failed");
|
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"SPIFFS verification failed\"}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SPIFFS.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alles erfolgreich
|
|
|
|
Serial.printf("Update complete: 0x%X bytes written\n", currentOffset);
|
|
|
|
request->send(200, "application/json", "{\"status\":\"success\",\"message\":\"Update successful! Device will restart...\",\"restart\":true}");
|
|
|
|
delay(500);
|
|
|
|
ESP.restart();
|
2025-02-18 11:42:52 +01:00
|
|
|
}
|
|
|
|
}
|
2025-02-17 12:41:25 +01:00
|
|
|
|