OTA Upload Feature (#162)
* Improve restart behaviour under esp8266 * Backend to support firmware update over HTTP * UI for uploading new firmware * Documentation changes
This commit is contained in:
@ -15,6 +15,9 @@ ESP8266React::ESP8266React(AsyncWebServer* server, FS* fs) :
|
||||
#if FT_ENABLED(FT_OTA)
|
||||
_otaSettingsService(server, fs, &_securitySettingsService),
|
||||
#endif
|
||||
#if FT_ENABLED(FT_UPLOAD_FIRMWARE)
|
||||
_uploadFirmwareService(server, &_securitySettingsService),
|
||||
#endif
|
||||
#if FT_ENABLED(FT_MQTT)
|
||||
_mqttSettingsService(server, fs, &_securitySettingsService),
|
||||
_mqttStatus(server, &_mqttSettingsService, &_securitySettingsService),
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include <NTPSettingsService.h>
|
||||
#include <NTPStatus.h>
|
||||
#include <OTASettingsService.h>
|
||||
#include <UploadFirmwareService.h>
|
||||
#include <RestartService.h>
|
||||
#include <SecuritySettingsService.h>
|
||||
#include <SystemStatus.h>
|
||||
@ -98,6 +99,9 @@ class ESP8266React {
|
||||
#if FT_ENABLED(FT_OTA)
|
||||
OTASettingsService _otaSettingsService;
|
||||
#endif
|
||||
#if FT_ENABLED(FT_UPLOAD_FIRMWARE)
|
||||
UploadFirmwareService _uploadFirmwareService;
|
||||
#endif
|
||||
#if FT_ENABLED(FT_MQTT)
|
||||
MqttSettingsService _mqttSettingsService;
|
||||
MqttStatus _mqttStatus;
|
||||
|
@ -30,5 +30,5 @@ void FactoryResetService::factoryReset() {
|
||||
fs->remove(configDirectory.fileName());
|
||||
}
|
||||
#endif
|
||||
ESP.restart();
|
||||
RestartService::restartNow();
|
||||
}
|
||||
|
@ -11,6 +11,7 @@
|
||||
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <SecurityManager.h>
|
||||
#include <RestartService.h>
|
||||
#include <FS.h>
|
||||
|
||||
#define FS_CONFIG_DIRECTORY "/config"
|
||||
|
@ -28,4 +28,10 @@
|
||||
#define FT_OTA 1
|
||||
#endif
|
||||
|
||||
// upload firmware feature off by default
|
||||
#ifndef FT_UPLOAD_FIRMWARE
|
||||
#define FT_UPLOAD_FIRMWARE 0
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -31,6 +31,11 @@ void FeaturesService::features(AsyncWebServerRequest* request) {
|
||||
root["ota"] = true;
|
||||
#else
|
||||
root["ota"] = false;
|
||||
#endif
|
||||
#if FT_ENABLED(FT_UPLOAD_FIRMWARE)
|
||||
root["upload_firmware"] = true;
|
||||
#else
|
||||
root["upload_firmware"] = false;
|
||||
#endif
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
|
@ -8,6 +8,6 @@ RestartService::RestartService(AsyncWebServer* server, SecurityManager* security
|
||||
}
|
||||
|
||||
void RestartService::restart(AsyncWebServerRequest* request) {
|
||||
request->onDisconnect([]() { ESP.restart(); });
|
||||
request->onDisconnect(RestartService::restartNow);
|
||||
request->send(200);
|
||||
}
|
||||
|
@ -18,6 +18,12 @@ class RestartService {
|
||||
public:
|
||||
RestartService(AsyncWebServer* server, SecurityManager* securityManager);
|
||||
|
||||
static void restartNow() {
|
||||
WiFi.disconnect(true);
|
||||
delay(500);
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
private:
|
||||
void restart(AsyncWebServerRequest* request);
|
||||
};
|
||||
|
85
lib/framework/UploadFirmwareService.cpp
Normal file
85
lib/framework/UploadFirmwareService.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
#include <UploadFirmwareService.h>
|
||||
|
||||
UploadFirmwareService::UploadFirmwareService(AsyncWebServer* server, SecurityManager* securityManager) :
|
||||
_securityManager(securityManager) {
|
||||
server->on(UPLOAD_FIRMWARE_PATH,
|
||||
HTTP_POST,
|
||||
std::bind(&UploadFirmwareService::uploadComplete, this, std::placeholders::_1),
|
||||
std::bind(&UploadFirmwareService::handleUpload,
|
||||
this,
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2,
|
||||
std::placeholders::_3,
|
||||
std::placeholders::_4,
|
||||
std::placeholders::_5,
|
||||
std::placeholders::_6));
|
||||
#ifdef ESP8266
|
||||
Update.runAsync(true);
|
||||
#endif
|
||||
}
|
||||
|
||||
void UploadFirmwareService::handleUpload(AsyncWebServerRequest* request,
|
||||
const String& filename,
|
||||
size_t index,
|
||||
uint8_t* data,
|
||||
size_t len,
|
||||
bool final) {
|
||||
if (!index) {
|
||||
Authentication authentication = _securityManager->authenticateRequest(request);
|
||||
if (AuthenticationPredicates::IS_ADMIN(authentication)) {
|
||||
if (Update.begin(request->contentLength())) {
|
||||
// success, let's make sure we end the update if the client hangs up
|
||||
request->onDisconnect(UploadFirmwareService::handleEarlyDisconnect);
|
||||
} else {
|
||||
// failed to begin, send an error response
|
||||
Update.printError(Serial);
|
||||
handleError(request, 500);
|
||||
}
|
||||
} else {
|
||||
// send the forbidden response
|
||||
handleError(request, 403);
|
||||
}
|
||||
}
|
||||
|
||||
// if we haven't delt with an error, continue with the update
|
||||
if (!request->_tempObject) {
|
||||
if (Update.write(data, len) != len) {
|
||||
Update.printError(Serial);
|
||||
handleError(request, 500);
|
||||
}
|
||||
if (final) {
|
||||
if (!Update.end(true)) {
|
||||
Update.printError(Serial);
|
||||
handleError(request, 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UploadFirmwareService::uploadComplete(AsyncWebServerRequest* request) {
|
||||
// if no error, send the success response
|
||||
if (!request->_tempObject) {
|
||||
request->onDisconnect(RestartService::restartNow);
|
||||
AsyncWebServerResponse* response = request->beginResponse(200);
|
||||
request->send(response);
|
||||
}
|
||||
}
|
||||
|
||||
void UploadFirmwareService::handleError(AsyncWebServerRequest* request, int code) {
|
||||
// if we have had an error already, do nothing
|
||||
if (request->_tempObject) {
|
||||
return;
|
||||
}
|
||||
// send the error code to the client and record the error code in the temp object
|
||||
request->_tempObject = new int(code);
|
||||
AsyncWebServerResponse* response = request->beginResponse(code);
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
void UploadFirmwareService::handleEarlyDisconnect() {
|
||||
#ifdef ESP32
|
||||
Update.abort();
|
||||
#elif defined(ESP8266)
|
||||
Update.end();
|
||||
#endif
|
||||
}
|
38
lib/framework/UploadFirmwareService.h
Normal file
38
lib/framework/UploadFirmwareService.h
Normal file
@ -0,0 +1,38 @@
|
||||
#ifndef UploadFirmwareService_h
|
||||
#define UploadFirmwareService_h
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#ifdef ESP32
|
||||
#include <Update.h>
|
||||
#include <WiFi.h>
|
||||
#include <AsyncTCP.h>
|
||||
#elif defined(ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESPAsyncTCP.h>
|
||||
#endif
|
||||
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <SecurityManager.h>
|
||||
#include <RestartService.h>
|
||||
|
||||
#define UPLOAD_FIRMWARE_PATH "/rest/uploadFirmware"
|
||||
|
||||
class UploadFirmwareService {
|
||||
public:
|
||||
UploadFirmwareService(AsyncWebServer* server, SecurityManager* securityManager);
|
||||
|
||||
private:
|
||||
SecurityManager* _securityManager;
|
||||
void handleUpload(AsyncWebServerRequest* request,
|
||||
const String& filename,
|
||||
size_t index,
|
||||
uint8_t* data,
|
||||
size_t len,
|
||||
bool final);
|
||||
void uploadComplete(AsyncWebServerRequest* request);
|
||||
void handleError(AsyncWebServerRequest* request, int code);
|
||||
static void handleEarlyDisconnect();
|
||||
};
|
||||
|
||||
#endif // end UploadFirmwareService_h
|
Reference in New Issue
Block a user