Remove OTA feature #1738

This commit is contained in:
proddy
2024-05-04 14:19:19 +02:00
parent 6537abedb9
commit dabb958f06
44 changed files with 291 additions and 824 deletions

View File

@@ -6,18 +6,13 @@ AuthenticationService::AuthenticationService(AsyncWebServer * server, SecurityMa
server->on(SIGN_IN_PATH, [this](AsyncWebServerRequest * request, JsonVariant json) { signIn(request, json); });
}
/**
* Verifies that the request supplied a valid JWT.
*/
// Verifies that the request supplied a valid JWT.
void AuthenticationService::verifyAuthorization(AsyncWebServerRequest * request) {
Authentication authentication = _securityManager->authenticateRequest(request);
request->send(authentication.authenticated ? 200 : 401);
}
/**
* Signs in a user if the username and password match. Provides a JWT to be used in the Authorization header in
* subsequent requests.
*/
// Signs in a user if the username and password match. Provides a JWT to be used in the Authorization header in subsequent requests.
void AuthenticationService::signIn(AsyncWebServerRequest * request, JsonVariant json) {
if (json.is<JsonObject>()) {
String username = json["username"];
@@ -33,6 +28,7 @@ void AuthenticationService::signIn(AsyncWebServerRequest * request, JsonVariant
return;
}
}
AsyncWebServerResponse * response = request->beginResponse(401);
request->send(response);
}

View File

@@ -11,7 +11,6 @@ ESP8266React::ESP8266React(AsyncWebServer * server, FS * fs)
, _apStatus(server, &_securitySettingsService, &_apSettingsService)
, _ntpSettingsService(server, fs, &_securitySettingsService)
, _ntpStatus(server, &_securitySettingsService)
, _otaSettingsService(server, fs, &_securitySettingsService)
, _uploadFileService(server, &_securitySettingsService)
, _mqttSettingsService(server, fs, &_securitySettingsService)
, _mqttStatus(server, &_mqttSettingsService, &_securitySettingsService)
@@ -76,7 +75,6 @@ void ESP8266React::begin() {
});
_apSettingsService.begin();
_ntpSettingsService.begin();
_otaSettingsService.begin();
_mqttSettingsService.begin();
_securitySettingsService.begin();
}
@@ -84,6 +82,5 @@ void ESP8266React::begin() {
void ESP8266React::loop() {
_networkSettingsService.loop();
_apSettingsService.loop();
_otaSettingsService.loop();
_mqttSettingsService.loop();
}

View File

@@ -9,7 +9,6 @@
#include "MqttStatus.h"
#include "NTPSettingsService.h"
#include "NTPStatus.h"
#include "OTASettingsService.h"
#include "UploadFileService.h"
#include "RestartService.h"
#include "SecuritySettingsService.h"
@@ -48,10 +47,6 @@ class ESP8266React {
return &_ntpSettingsService;
}
StatefulService<OTASettings> * getOTASettingsService() {
return &_otaSettingsService;
}
StatefulService<MqttSettings> * getMqttSettingsService() {
return &_mqttSettingsService;
}
@@ -88,7 +83,6 @@ class ESP8266React {
APStatus _apStatus;
NTPSettingsService _ntpSettingsService;
NTPStatus _ntpStatus;
OTASettingsService _otaSettingsService;
UploadFileService _uploadFileService;
MqttSettingsService _mqttSettingsService;
MqttStatus _mqttStatus;

View File

@@ -21,15 +21,9 @@
#define FT_NTP 1
#endif
// ota feature on by default
#ifndef FT_OTA
#define FT_OTA 1
#endif
// upload firmware/file feature on by default
#ifndef FT_UPLOAD_FIRMWARE
#define FT_UPLOAD_FIRMWARE 1
#endif
#endif

View File

@@ -1,87 +0,0 @@
#include "OTASettingsService.h"
#include "../../src/emsesp_stub.hpp"
OTASettingsService::OTASettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager)
: _httpEndpoint(OTASettings::read, OTASettings::update, this, server, OTA_SETTINGS_SERVICE_PATH, securityManager)
, _fsPersistence(OTASettings::read, OTASettings::update, this, fs, OTA_SETTINGS_FILE)
, _arduinoOTA(nullptr) {
WiFi.onEvent([this](WiFiEvent_t event, WiFiEventInfo_t info) { WiFiEvent(event); });
addUpdateHandler([this] { configureArduinoOTA(); }, false);
}
void OTASettingsService::begin() {
_fsPersistence.readFromFS();
configureArduinoOTA();
}
void OTASettingsService::loop() {
if (_state.enabled && _arduinoOTA) {
_arduinoOTA->handle();
}
}
void OTASettingsService::configureArduinoOTA() {
if (_arduinoOTA) {
_arduinoOTA->end();
delete _arduinoOTA;
_arduinoOTA = nullptr;
}
if (_state.enabled) {
_arduinoOTA = new ArduinoOTAClass;
_arduinoOTA->setPort(static_cast<uint16_t>(_state.port));
_arduinoOTA->setPassword(_state.password.c_str());
_arduinoOTA->onStart([] { emsesp::EMSESP::system_.upload_status(true); });
_arduinoOTA->onEnd([] { emsesp::EMSESP::system_.upload_status(false); });
_arduinoOTA->onProgress([](unsigned int progress, unsigned int total) {
// Serial.printf("Progress: %u%%\r\n", (progress / (total / 100)));
});
_arduinoOTA->onError([](ota_error_t error) {
/*
#if defined(EMSESP_DEBUG)
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR)
Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR)
Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR)
Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR)
Serial.println("Receive Failed");
else if (error == OTA_END_ERROR)
Serial.println("End Failed");
#endif
*/
});
_arduinoOTA->setMdnsEnabled(false); // disable as handled in NetworkSettingsService.cpp. https://github.com/emsesp/EMS-ESP32/issues/161
_arduinoOTA->begin();
}
}
void OTASettingsService::WiFiEvent(WiFiEvent_t event) {
switch (event) {
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
case ARDUINO_EVENT_ETH_GOT_IP:
configureArduinoOTA();
break;
default:
break;
}
}
void OTASettings::read(OTASettings & settings, JsonObject root) {
root["enabled"] = settings.enabled;
root["port"] = settings.port;
root["password"] = settings.password;
}
StateUpdateResult OTASettings::update(JsonObject root, OTASettings & settings) {
settings.enabled = root["enabled"] | FACTORY_OTA_ENABLED;
settings.port = root["port"] | FACTORY_OTA_PORT;
settings.password = root["password"] | FACTORY_OTA_PASSWORD;
return StateUpdateResult::CHANGED;
}

View File

@@ -1,51 +0,0 @@
#ifndef OTASettingsService_h
#define OTASettingsService_h
#include "HttpEndpoint.h"
#include "FSPersistence.h"
#include <ArduinoOTA.h>
#include <WiFiUdp.h>
#ifndef FACTORY_OTA_PORT
#define FACTORY_OTA_PORT 8266
#endif
#ifndef FACTORY_OTA_PASSWORD
#define FACTORY_OTA_PASSWORD "ems-esp-neo"
#endif
#ifndef FACTORY_OTA_ENABLED
#define FACTORY_OTA_ENABLED false
#endif
#define OTA_SETTINGS_FILE "/config/otaSettings.json"
#define OTA_SETTINGS_SERVICE_PATH "/rest/otaSettings"
class OTASettings {
public:
bool enabled;
int port;
String password;
static void read(OTASettings & settings, JsonObject root);
static StateUpdateResult update(JsonObject root, OTASettings & settings);
};
class OTASettingsService : public StatefulService<OTASettings> {
public:
OTASettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager);
void begin();
void loop();
private:
HttpEndpoint<OTASettings> _httpEndpoint;
FSPersistence<OTASettings> _fsPersistence;
ArduinoOTAClass * _arduinoOTA;
void configureArduinoOTA();
void WiFiEvent(WiFiEvent_t event);
};
#endif

View File

@@ -71,7 +71,6 @@ class SecuritySettingsService final : public StatefulService<SecuritySettings>,
void begin();
// Functions to implement SecurityManager
Authentication authenticate(const String & username, const String & password) override;
Authentication authenticateRequest(AsyncWebServerRequest * request) override;
String generateJWT(const User * user) override;
@@ -88,15 +87,9 @@ class SecuritySettingsService final : public StatefulService<SecuritySettings>,
void configureJWTHandler();
/*
* Lookup the user by JWT
*/
Authentication authenticateJWT(String & jwt);
Authentication authenticateJWT(String & jwt); // Lookup the user by JWT
/*
* Verify the payload is correct
*/
boolean validatePayload(JsonObject parsedPayload, const User * user);
boolean validatePayload(JsonObject parsedPayload, const User * user); // Verify the payload is correct
};
#endif

View File

@@ -1,4 +1,5 @@
#include "UploadFileService.h"
#include "../../src/emsesp_stub.hpp"
#include <esp_app_format.h>
@@ -84,6 +85,7 @@ void UploadFileService::handleUpload(AsyncWebServerRequest * request, const Stri
Update.setMD5(_md5.data());
_md5.front() = '\0';
}
// emsesp::EMSESP::system_.upload_status(true); // force just in case, this is stop UART, MQTT and other services
request->onDisconnect([this] { handleEarlyDisconnect(); }); // success, let's make sure we end the update if the client hangs up
} else {
handleError(request, 507); // failed to begin, send an error response Insufficient Storage
@@ -101,11 +103,11 @@ void UploadFileService::handleUpload(AsyncWebServerRequest * request, const Stri
}
} else if (!request->_tempObject) { // if we haven't delt with an error, continue with the firmware update
if (Update.write(data, len) != len) {
handleError(request, 500);
handleError(request, 500); // internal error, failed
return;
}
if (final && !Update.end(true)) {
handleError(request, 500);
handleError(request, 500); // internal error, failed
}
}
}