Skip to content

Commit

Permalink
More robust firmware upload (#1560)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkendall64 committed May 7, 2022
1 parent be4f974 commit d9af2e8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/html/scan.js
Expand Up @@ -246,6 +246,7 @@ function uploadFile() {
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "/update");
ajax.setRequestHeader("X-FileSize", file.size);
ajax.send(formdata);
}

Expand Down
32 changes: 11 additions & 21 deletions src/lib/WIFI/devWIFI.cpp
Expand Up @@ -327,9 +327,13 @@ static void WebUpdateHandleNotFound(AsyncWebServerRequest *request)
}

static void WebUploadResponseHandler(AsyncWebServerRequest *request) {
if (Update.hasError()) {
if (!Update.end()) {
StreamString p = StreamString();
Update.printError(p);
if (Update.hasError()) {
Update.printError(p);
} else {
p.println("Not enough data uploaded!");
}
p.trim();
DBGLN("Failed to upload firmware: %s", p.c_str());
AsyncWebServerResponse *response = request->beginResponse(200, "application/json", String("{\"status\": \"error\", \"msg\": \"") + p + "\"}");
Expand Down Expand Up @@ -363,15 +367,14 @@ static void WebUploadResponseHandler(AsyncWebServerRequest *request) {

static void WebUploadDataHandler(AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final) {
if (index == 0) {
DBGLN("Update: %s", filename.c_str());
size_t filesize = request->header("X-FileSize").toInt();
DBGLN("Update: '%s' size %u", filename.c_str(), filesize);
#if defined(PLATFORM_ESP8266)
Update.runAsync(true);
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
DBGLN("Free space = %u", maxSketchSpace);
if (!Update.begin(maxSketchSpace, U_FLASH)){//start with max available size
#else
if (!Update.begin()) { //start with max available size
#endif
if (!Update.begin(filesize, U_FLASH)) { // pass the size provided
Update.printError(LOGGING_UART);
}
target_seen = false;
Expand Down Expand Up @@ -410,28 +413,15 @@ static void WebUploadDataHandler(AsyncWebServerRequest *request, const String& f
}
}
totalSize += len;
}
}
if (final && !Update.getError()) {
DBGVLN("finish");
if (target_seen) {
if (Update.end(true)) { //true to set the size to the current progress
DBGLN("Upload Success: %ubytes\nPlease wait for LED to resume blinking before disconnecting power", totalSize);
} else {
Update.printError(LOGGING_UART);
}
} else {
DBGLN("write failed to write %d", len);
}
}
}

static void WebUploadForceUpdateHandler(AsyncWebServerRequest *request) {
target_seen = true;
if (request->arg("action").equals("confirm")) {
if (Update.end(true)) { //true to set the size to the current progress
DBGLN("Upload Success: %ubytes\nPlease wait for LED to resume blinking before disconnecting power", totalSize);
} else {
Update.printError(LOGGING_UART);
}
WebUploadResponseHandler(request);
} else {
#if defined(PLATFORM_ESP32)
Expand Down

0 comments on commit d9af2e8

Please sign in to comment.