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-18 14:18:14 +01:00
|
|
|
|
2025-02-20 14:31:10 +01:00
|
|
|
// Flash layout constants from partitions.csv
|
|
|
|
static const uint32_t FLASH_SIZE = 0x400000; // 4MB total
|
|
|
|
static const uint32_t APP_SIZE = 0x1E0000; // Size per app partition
|
|
|
|
static const uint32_t SPIFFS_OFFSET = 0x3D0000; // SPIFFS start
|
|
|
|
|
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:31:10 +01:00
|
|
|
isFullImage = (contentLength >= SPIFFS_OFFSET);
|
2025-02-20 14:28:11 +01:00
|
|
|
|
2025-02-20 14:16:49 +01:00
|
|
|
if (!isFullImage) {
|
2025-02-20 14:31:10 +01:00
|
|
|
// Regular firmware update must not exceed app partition size
|
|
|
|
if (contentLength > APP_SIZE) {
|
|
|
|
Serial.printf("Firmware too large: 0x%X > 0x%X\n", contentLength, APP_SIZE);
|
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Firmware too large\"}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-02-20 14:16:49 +01:00
|
|
|
if (!Update.begin(contentLength)) {
|
2025-02-20 14:28:11 +01:00
|
|
|
Serial.printf("Not enough space for firmware: %u required\n", contentLength);
|
2025-02-20 14:16:49 +01:00
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Not enough space available\"}");
|
|
|
|
return;
|
2025-02-20 11:27:11 +01:00
|
|
|
}
|
2025-02-20 14:31:10 +01:00
|
|
|
Serial.printf("Firmware update started (size: 0x%X)\n", contentLength);
|
2025-02-20 10:53:23 +01:00
|
|
|
} else {
|
2025-02-20 14:31:10 +01:00
|
|
|
// Full image update
|
|
|
|
if (contentLength > FLASH_SIZE) {
|
|
|
|
Serial.printf("Image too large: 0x%X > 0x%X\n", contentLength, FLASH_SIZE);
|
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Image too large\"}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Update.begin(FLASH_SIZE, U_FLASH)) {
|
2025-02-20 14:28:11 +01:00
|
|
|
Serial.println("Could not begin full image update");
|
|
|
|
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 14:31:10 +01:00
|
|
|
Serial.printf("Full image update started (size: 0x%X)\n", contentLength);
|
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 14:16:49 +01:00
|
|
|
|
|
|
|
if (Update.write(data, len) != len) {
|
|
|
|
String errorMsg = Update.errorString();
|
|
|
|
if (errorMsg != "No Error") {
|
|
|
|
Update.printError(Serial);
|
2025-02-20 14:31:10 +01:00
|
|
|
Serial.printf("Error at offset: 0x%X of 0x%X bytes\n", currentOffset, contentLength);
|
2025-02-20 14:16:49 +01:00
|
|
|
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:31:10 +01:00
|
|
|
// Progress logging
|
|
|
|
if ((currentOffset % 0x40000) == 0) { // Log every 256KB
|
|
|
|
Serial.printf("Update progress: 0x%X of 0x%X bytes (%.1f%%)\n",
|
|
|
|
currentOffset,
|
|
|
|
contentLength,
|
|
|
|
(currentOffset * 100.0) / contentLength);
|
|
|
|
}
|
|
|
|
|
2025-02-20 10:53:23 +01:00
|
|
|
currentOffset += len;
|
2025-02-20 14:16:49 +01:00
|
|
|
|
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 14:31:10 +01:00
|
|
|
Serial.printf("Update complete: 0x%X bytes written\n", currentOffset);
|
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 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
|
|
|
|