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 11:52:36 +01:00
|
|
|
void stopAllTasks() {
|
2025-02-20 11:42:36 +01:00
|
|
|
// Stop all tasks
|
2025-02-20 11:53:04 +01:00
|
|
|
vTaskSuspend(RfidReaderTask);
|
2025-02-20 11:52:36 +01:00
|
|
|
vTaskSuspend(BambuMqttTask);
|
|
|
|
vTaskSuspend(ScaleTask);
|
|
|
|
//vTaskDelay(100 / portTICK_PERIOD_MS);
|
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 11:27:11 +01:00
|
|
|
static uint8_t *spiffsBuffer = nullptr;
|
|
|
|
static size_t spiffsSize = 0;
|
2025-02-20 11:42:36 +01:00
|
|
|
static const uint32_t SPIFFS_START = 0x310000; // SPIFFS start in full.bin
|
2025-02-18 14:18:14 +01:00
|
|
|
|
2025-02-20 11:52:36 +01:00
|
|
|
stopAllTasks();
|
2025-02-20 11:42:36 +01:00
|
|
|
|
2025-02-18 11:42:52 +01:00
|
|
|
if (!index) {
|
2025-02-20 11:42:36 +01:00
|
|
|
// Reset static variables
|
|
|
|
if (spiffsBuffer) {
|
|
|
|
free(spiffsBuffer);
|
|
|
|
spiffsBuffer = nullptr;
|
|
|
|
}
|
|
|
|
|
2025-02-18 14:18:14 +01:00
|
|
|
contentLength = request->contentLength();
|
|
|
|
Serial.printf("Update size: %u bytes\n", contentLength);
|
|
|
|
|
|
|
|
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 10:53:23 +01:00
|
|
|
if (data[0] == ESP_MAGIC) {
|
|
|
|
isFullImage = (contentLength > 0x3D0000);
|
|
|
|
|
2025-02-20 11:42:36 +01:00
|
|
|
// Korrektur: Bei full.bin die gesamte Größe verwenden
|
|
|
|
if (!Update.begin(contentLength, U_FLASH)) {
|
2025-02-20 10:06:06 +01:00
|
|
|
Serial.printf("Not enough space: %u required\n", contentLength);
|
2025-02-20 10:53:23 +01:00
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Not enough space available\"}");
|
2025-02-20 10:06:06 +01:00
|
|
|
return;
|
|
|
|
}
|
2025-02-20 10:53:23 +01:00
|
|
|
|
|
|
|
Serial.println(isFullImage ? "Full image update started" : "Firmware update started");
|
2025-02-20 11:27:11 +01:00
|
|
|
|
|
|
|
if (isFullImage) {
|
2025-02-20 11:42:36 +01:00
|
|
|
spiffsSize = 0x0F0000; // 960KB SPIFFS size
|
2025-02-20 11:27:11 +01:00
|
|
|
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\"}");
|
|
|
|
return;
|
|
|
|
}
|
2025-02-20 11:42:36 +01:00
|
|
|
memset(spiffsBuffer, 0xFF, spiffsSize); // Initialize buffer with 0xFF
|
2025-02-20 11:27:11 +01:00
|
|
|
}
|
2025-02-20 10:53:23 +01:00
|
|
|
} else {
|
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Invalid image format\"}");
|
|
|
|
return;
|
2025-02-18 11:42:52 +01:00
|
|
|
}
|
2025-02-20 10:53:23 +01:00
|
|
|
currentOffset = 0;
|
2025-02-18 11:42:52 +01:00
|
|
|
}
|
2025-02-20 01:08:48 +01:00
|
|
|
|
2025-02-20 11:42:36 +01:00
|
|
|
if (isFullImage) {
|
|
|
|
// Zuerst vollständiges Firmware-Update durchführen
|
|
|
|
if (Update.write(data, len) != len) {
|
|
|
|
String errorMsg = Update.errorString();
|
|
|
|
if (errorMsg != "No Error") {
|
|
|
|
Update.printError(Serial);
|
|
|
|
if (spiffsBuffer) free(spiffsBuffer);
|
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Error writing firmware: " + errorMsg + "\"}");
|
|
|
|
return;
|
2025-02-20 11:27:11 +01:00
|
|
|
}
|
|
|
|
}
|
2025-02-20 11:42:36 +01:00
|
|
|
|
|
|
|
// SPIFFS-Daten separat sammeln
|
|
|
|
if (currentOffset >= SPIFFS_START) {
|
|
|
|
uint32_t spiffsOffset = currentOffset - SPIFFS_START;
|
|
|
|
if (spiffsOffset < spiffsSize) {
|
|
|
|
size_t copyLen = min(len, spiffsSize - spiffsOffset);
|
|
|
|
memcpy(spiffsBuffer + spiffsOffset, data, copyLen);
|
|
|
|
Serial.printf("Collecting SPIFFS data at offset %u, length %u\n", spiffsOffset, copyLen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Normal firmware update
|
|
|
|
if (Update.write(data, len) != len) {
|
|
|
|
String errorMsg = Update.errorString();
|
|
|
|
if (errorMsg != "No Error") {
|
|
|
|
Update.printError(Serial);
|
|
|
|
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
|
|
|
|
|
|
|
currentOffset += len;
|
2025-02-18 11:42:52 +01:00
|
|
|
|
|
|
|
if (final) {
|
2025-02-18 14:18:14 +01:00
|
|
|
if (Update.end(true)) {
|
2025-02-20 11:27:11 +01:00
|
|
|
Serial.println("Firmware update complete");
|
|
|
|
|
|
|
|
if (isFullImage && spiffsBuffer) {
|
|
|
|
Serial.println("Starting SPIFFS update");
|
2025-02-20 11:42:36 +01:00
|
|
|
|
|
|
|
// Unmount SPIFFS first
|
|
|
|
if (SPIFFS.begin()) {
|
|
|
|
SPIFFS.end();
|
2025-02-20 11:27:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update SPIFFS
|
2025-02-20 11:42:36 +01:00
|
|
|
if (!Update.begin(spiffsSize, U_SPIFFS)) {
|
|
|
|
Serial.println("Could not begin SPIFFS update");
|
|
|
|
free(spiffsBuffer);
|
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"SPIFFS update initialization failed\"}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Update.write(spiffsBuffer, spiffsSize) != spiffsSize) {
|
|
|
|
Serial.println("SPIFFS Write Failed");
|
|
|
|
Update.printError(Serial);
|
|
|
|
free(spiffsBuffer);
|
|
|
|
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);
|
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"SPIFFS finish failed\"}");
|
|
|
|
return;
|
2025-02-20 11:27:11 +01:00
|
|
|
}
|
2025-02-20 11:42:36 +01:00
|
|
|
|
2025-02-20 11:27:11 +01:00
|
|
|
free(spiffsBuffer);
|
2025-02-20 11:42:36 +01:00
|
|
|
spiffsBuffer = nullptr;
|
2025-02-20 11:27:11 +01:00
|
|
|
Serial.println("SPIFFS update complete");
|
2025-02-20 11:42:36 +01:00
|
|
|
|
|
|
|
// 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();
|
2025-02-20 11:27:11 +01:00
|
|
|
}
|
|
|
|
|
2025-02-18 14:18:14 +01:00
|
|
|
request->send(200, "application/json", "{\"status\":\"success\",\"message\":\"Update successful! Device will restart...\",\"restart\":true}");
|
|
|
|
delay(1000);
|
2025-02-18 11:42:52 +01:00
|
|
|
ESP.restart();
|
2025-02-18 14:18:14 +01:00
|
|
|
} else {
|
2025-02-20 11:42:36 +01:00
|
|
|
if (spiffsBuffer) {
|
|
|
|
free(spiffsBuffer);
|
|
|
|
spiffsBuffer = nullptr;
|
|
|
|
}
|
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);
|
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Update failed: " + errorMsg + "\"}");
|
|
|
|
} else {
|
|
|
|
request->send(200, "application/json", "{\"status\":\"success\",\"message\":\"Update successful! Device will restart...\",\"restart\":true}");
|
|
|
|
delay(1000);
|
|
|
|
ESP.restart();
|
|
|
|
}
|
2025-02-18 11:42:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-02-17 12:41:25 +01:00
|
|
|
|