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 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 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-20 20:19:24 +01:00
|
|
|
void performStageTwo() {
|
|
|
|
if (!SPIFFS.begin(true)) {
|
|
|
|
Serial.println("Error: Could not mount SPIFFS for stage 2");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
File firmwareFile = SPIFFS.open("/firmware.bin", "r");
|
|
|
|
if (!firmwareFile) {
|
|
|
|
Serial.println("Error: Could not open firmware.bin from SPIFFS");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t firmwareSize = firmwareFile.size();
|
|
|
|
size_t maxAppSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
|
|
|
|
|
|
|
|
Serial.printf("Stage 2 - Firmware size: %u bytes\n", firmwareSize);
|
|
|
|
Serial.printf("Available space: %u bytes\n", maxAppSpace);
|
|
|
|
|
|
|
|
if (firmwareSize > maxAppSpace) {
|
|
|
|
Serial.printf("Error: Not enough space for firmware. Need %u bytes but only have %u bytes\n",
|
|
|
|
firmwareSize, maxAppSpace);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Update.begin(firmwareSize)) {
|
|
|
|
Update.printError(Serial);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t written = Update.writeStream(firmwareFile);
|
|
|
|
if (written != firmwareSize) {
|
|
|
|
Update.printError(Serial);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Update.end(true)) {
|
|
|
|
Update.printError(Serial);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
firmwareFile.close();
|
|
|
|
SPIFFS.remove("/firmware.bin"); // Cleanup
|
|
|
|
Serial.println("Stage 2 update successful, restarting...");
|
|
|
|
delay(500);
|
|
|
|
ESP.restart();
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkForStagedUpdate() {
|
|
|
|
if (!SPIFFS.begin(true)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SPIFFS.exists("/firmware.bin")) {
|
|
|
|
Serial.println("Found staged firmware update, initiating stage 2...");
|
|
|
|
performStageTwo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-20 20:19:24 +01:00
|
|
|
static File stagingFile;
|
|
|
|
|
2025-02-18 11:42:52 +01:00
|
|
|
if (!index) {
|
2025-02-20 20:19:24 +01:00
|
|
|
bool isSpiffsUpdate = filename.endsWith("_spiffs.bin");
|
|
|
|
Serial.printf("Update Start: %s (type: %s)\n", filename.c_str(), isSpiffsUpdate ? "SPIFFS" : "OTA");
|
2025-02-20 17:04:56 +01:00
|
|
|
|
2025-02-20 16:14:49 +01:00
|
|
|
if (request->contentLength() == 0) {
|
2025-02-18 14:18:14 +01:00
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Invalid file size\"}");
|
2025-02-18 11:42:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-02-20 20:19:24 +01:00
|
|
|
// Stop tasks before update
|
2025-02-20 14:16:49 +01:00
|
|
|
if (!tasksAreStopped && (RfidReaderTask || BambuMqttTask || ScaleTask)) {
|
|
|
|
stopAllTasks();
|
|
|
|
tasksAreStopped = true;
|
|
|
|
}
|
|
|
|
|
2025-02-20 20:19:24 +01:00
|
|
|
size_t updateSize = request->contentLength();
|
|
|
|
|
|
|
|
if (isSpiffsUpdate) {
|
|
|
|
if (!SPIFFS.begin(true)) {
|
|
|
|
request->send(400, "application/json",
|
|
|
|
"{\"status\":\"error\",\"message\":\"Could not mount SPIFFS\"}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start SPIFFS update
|
|
|
|
if (!Update.begin(updateSize, U_SPIFFS)) {
|
|
|
|
Update.printError(Serial);
|
|
|
|
request->send(400, "application/json",
|
|
|
|
"{\"status\":\"error\",\"message\":\"SPIFFS update initialization failed\"}");
|
|
|
|
return;
|
|
|
|
}
|
2025-02-20 17:04:56 +01:00
|
|
|
} else {
|
2025-02-20 20:19:24 +01:00
|
|
|
// Regular OTA update
|
|
|
|
stagingFile = SPIFFS.open("/firmware.bin", "w");
|
|
|
|
if (!stagingFile) {
|
2025-02-20 20:04:58 +01:00
|
|
|
request->send(400, "application/json",
|
2025-02-20 20:19:24 +01:00
|
|
|
"{\"status\":\"error\",\"message\":\"Could not create staging file\"}");
|
2025-02-20 17:04:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2025-02-20 20:19:24 +01:00
|
|
|
}
|
2025-02-20 17:04:56 +01:00
|
|
|
|
2025-02-20 20:19:24 +01:00
|
|
|
if (stagingFile) {
|
|
|
|
// Stage 1: Write to SPIFFS
|
|
|
|
if (stagingFile.write(data, len) != len) {
|
|
|
|
stagingFile.close();
|
|
|
|
SPIFFS.remove("/firmware.bin");
|
|
|
|
request->send(400, "application/json",
|
|
|
|
"{\"status\":\"error\",\"message\":\"Write to SPIFFS failed\"}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Direct SPIFFS update
|
|
|
|
if (Update.write(data, len) != len) {
|
2025-02-20 14:56:31 +01:00
|
|
|
Update.printError(Serial);
|
2025-02-20 20:04:58 +01:00
|
|
|
request->send(400, "application/json",
|
2025-02-20 20:19:24 +01:00
|
|
|
"{\"status\":\"error\",\"message\":\"Write failed\"}");
|
2025-02-20 14:56:31 +01:00
|
|
|
return;
|
2025-02-18 11:42:52 +01:00
|
|
|
}
|
2025-02-20 15:07:13 +01:00
|
|
|
}
|
|
|
|
|
2025-02-18 11:42:52 +01:00
|
|
|
if (final) {
|
2025-02-20 20:19:24 +01:00
|
|
|
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...\"}");
|
|
|
|
performStageTwo();
|
|
|
|
} else {
|
|
|
|
// Finish direct SPIFFS update
|
|
|
|
if (!Update.end(true)) {
|
|
|
|
Update.printError(Serial);
|
|
|
|
request->send(400, "application/json",
|
|
|
|
"{\"status\":\"error\",\"message\":\"Update failed\"}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Serial.println("SPIFFS update successful, restarting...");
|
|
|
|
request->send(200, "application/json",
|
|
|
|
"{\"status\":\"success\",\"message\":\"SPIFFS update successful! Device will restart...\",\"restart\":true}");
|
|
|
|
delay(500);
|
|
|
|
ESP.restart();
|
2025-02-20 14:45:34 +01:00
|
|
|
}
|
2025-02-18 11:42:52 +01:00
|
|
|
}
|
|
|
|
}
|
2025-02-17 12:41:25 +01:00
|
|
|
|