mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
esp_yield() now also calls esp_schedule(), original esp_yield() function renamed to esp_suspend(). Don't use delay(0) in the Core internals, libraries and examples. Use yield() when the code is supposed to be called from CONT, use esp_yield() when the code can be called from either CONT or SYS. Clean-up esp_yield() and esp_schedule() declarations across the code and use coredecls.h instead. Implement helper functions for libraries that were previously using esp_yield(), esp_schedule() and esp_delay() directly to wait for certain SYS context tasks to complete. Correctly use esp_delay() for timeouts, make sure scheduled functions have a chance to run (e.g. LwIP_Ethernet uses recurrent) Related issues: - #6107 - discussion about the esp_yield() and esp_delay() usage in ClientContext - #6212 - discussion about replacing delay() with a blocking loop - #6680 - pull request introducing LwIP-based Ethernet - #7146 - discussion that originated UART code changes - #7969 - proposal to remove delay(0) from the example code - #8291 - discussion related to the run_scheduled_recurrent_functions() usage in LwIP Ethernet - #8317 - yieldUntil() implementation, similar to the esp_delay() overload with a timeout and a 0 interval
137 lines
4.8 KiB
C++
137 lines
4.8 KiB
C++
#include <Arduino.h>
|
|
#include <coredecls.h>
|
|
#include <WiFiClient.h>
|
|
#include <WiFiServer.h>
|
|
#include <ESP8266WebServer.h>
|
|
#include <WiFiUdp.h>
|
|
#include <flash_hal.h>
|
|
#include <FS.h>
|
|
#include "StreamString.h"
|
|
#include "ESP8266HTTPUpdateServer.h"
|
|
|
|
namespace esp8266httpupdateserver {
|
|
using namespace esp8266webserver;
|
|
|
|
static const char serverIndex[] PROGMEM =
|
|
R"(<!DOCTYPE html>
|
|
<html lang='en'>
|
|
<head>
|
|
<meta charset='utf-8'>
|
|
<meta name='viewport' content='width=device-width,initial-scale=1'/>
|
|
</head>
|
|
<body>
|
|
<form method='POST' action='' enctype='multipart/form-data'>
|
|
Firmware:<br>
|
|
<input type='file' accept='.bin,.bin.gz' name='firmware'>
|
|
<input type='submit' value='Update Firmware'>
|
|
</form>
|
|
<form method='POST' action='' enctype='multipart/form-data'>
|
|
FileSystem:<br>
|
|
<input type='file' accept='.bin,.bin.gz' name='filesystem'>
|
|
<input type='submit' value='Update FileSystem'>
|
|
</form>
|
|
</body>
|
|
</html>)";
|
|
static const char successResponse[] PROGMEM =
|
|
"<META http-equiv=\"refresh\" content=\"15;URL=/\">Update Success! Rebooting...";
|
|
|
|
template <typename ServerType>
|
|
ESP8266HTTPUpdateServerTemplate<ServerType>::ESP8266HTTPUpdateServerTemplate(bool serial_debug)
|
|
{
|
|
_serial_output = serial_debug;
|
|
_server = NULL;
|
|
_username = emptyString;
|
|
_password = emptyString;
|
|
_authenticated = false;
|
|
}
|
|
|
|
template <typename ServerType>
|
|
void ESP8266HTTPUpdateServerTemplate<ServerType>::setup(ESP8266WebServerTemplate<ServerType> *server, const String& path, const String& username, const String& password)
|
|
{
|
|
_server = server;
|
|
_username = username;
|
|
_password = password;
|
|
|
|
// handler for the /update form page
|
|
_server->on(path.c_str(), HTTP_GET, [&](){
|
|
if(_username != emptyString && _password != emptyString && !_server->authenticate(_username.c_str(), _password.c_str()))
|
|
return _server->requestAuthentication();
|
|
_server->send_P(200, PSTR("text/html"), serverIndex);
|
|
});
|
|
|
|
// handler for the /update form POST (once file upload finishes)
|
|
_server->on(path.c_str(), HTTP_POST, [&](){
|
|
if(!_authenticated)
|
|
return _server->requestAuthentication();
|
|
if (Update.hasError()) {
|
|
_server->send(200, F("text/html"), String(F("Update error: ")) + _updaterError);
|
|
} else {
|
|
_server->client().setNoDelay(true);
|
|
_server->send_P(200, PSTR("text/html"), successResponse);
|
|
delay(100);
|
|
_server->client().stop();
|
|
ESP.restart();
|
|
}
|
|
},[&](){
|
|
// handler for the file upload, gets the sketch bytes, and writes
|
|
// them through the Update object
|
|
HTTPUpload& upload = _server->upload();
|
|
|
|
if(upload.status == UPLOAD_FILE_START){
|
|
_updaterError.clear();
|
|
if (_serial_output)
|
|
Serial.setDebugOutput(true);
|
|
|
|
_authenticated = (_username == emptyString || _password == emptyString || _server->authenticate(_username.c_str(), _password.c_str()));
|
|
if(!_authenticated){
|
|
if (_serial_output)
|
|
Serial.printf("Unauthenticated Update\n");
|
|
return;
|
|
}
|
|
|
|
WiFiUDP::stopAll();
|
|
if (_serial_output)
|
|
Serial.printf("Update: %s\n", upload.filename.c_str());
|
|
if (upload.name == "filesystem") {
|
|
size_t fsSize = ((size_t) &_FS_end - (size_t) &_FS_start);
|
|
close_all_fs();
|
|
if (!Update.begin(fsSize, U_FS)){//start with max available size
|
|
if (_serial_output) Update.printError(Serial);
|
|
}
|
|
} else {
|
|
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
|
|
if (!Update.begin(maxSketchSpace, U_FLASH)){//start with max available size
|
|
_setUpdaterError();
|
|
}
|
|
}
|
|
} else if(_authenticated && upload.status == UPLOAD_FILE_WRITE && !_updaterError.length()){
|
|
if (_serial_output) Serial.printf(".");
|
|
if(Update.write(upload.buf, upload.currentSize) != upload.currentSize){
|
|
_setUpdaterError();
|
|
}
|
|
} else if(_authenticated && upload.status == UPLOAD_FILE_END && !_updaterError.length()){
|
|
if(Update.end(true)){ //true to set the size to the current progress
|
|
if (_serial_output) Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
|
|
} else {
|
|
_setUpdaterError();
|
|
}
|
|
if (_serial_output) Serial.setDebugOutput(false);
|
|
} else if(_authenticated && upload.status == UPLOAD_FILE_ABORTED){
|
|
Update.end();
|
|
if (_serial_output) Serial.println("Update was aborted");
|
|
}
|
|
esp_yield();
|
|
});
|
|
}
|
|
|
|
template <typename ServerType>
|
|
void ESP8266HTTPUpdateServerTemplate<ServerType>::_setUpdaterError()
|
|
{
|
|
if (_serial_output) Update.printError(Serial);
|
|
StreamString str;
|
|
Update.printError(str);
|
|
_updaterError = str.c_str();
|
|
}
|
|
|
|
};
|