Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More robust firmware upload #1560

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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