Skip to content

Commit

Permalink
update to v3.6 cpp files
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDvP committed Sep 5, 2023
1 parent 5a3d86a commit cfa0744
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/framework/HttpEndpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class HttpPostEndpoint {
response->setLength();

if (outcome == StateUpdateResult::CHANGED_RESTART) {
response->setCode(202); // added by proddy
response->setCode(205); // added by proddy
}
request->send(response);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/framework/NTPSettingsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void NTPSettingsService::WiFiEvent(WiFiEvent_t event) {
void NTPSettingsService::configureNTP() {
// emsesp::EMSESP::system_.ntp_connected(false);
if (connected_ && _state.enabled) {
sntp_set_sync_interval(3600000); // onehour
sntp_set_sync_interval(3600000); // one hour
sntp_set_time_sync_notification_cb(ntp_received);
configTzTime(_state.tzFormat.c_str(), _state.server.c_str());
} else {
Expand Down
4 changes: 4 additions & 0 deletions lib/framework/NetworkSettingsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,8 @@ void NetworkSettingsService::WiFiEvent(WiFiEvent_t event) {
_stopping = false;
}
}
if (event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED && WiFi.scanComplete() != -1) {
WiFi.scanDelete();
WiFi.scanNetworks(true);
}
}
41 changes: 31 additions & 10 deletions lib/framework/UploadFileService.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <UploadFileService.h>
#include <esp_ota_ops.h>

using namespace std::placeholders; // for `_1` etc

Expand Down Expand Up @@ -47,40 +48,46 @@ void UploadFileService::handleUpload(AsyncWebServerRequest * request, const Stri
return;
} else {
md5[0] = '\0';
return; // not support file type
handleError(request, 406); // Not Acceptable - unsupported file type
return;
}

if (is_firmware) {
// Check firmware header, 0xE9 magic offset 0 indicates esp bin, chip offset 12: esp32:0, S2:2, C3:5, S3:9
#if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4
if (len > 12 && (data[0] != 0xE9 || data[12] != 0)) {
handleError(request, 503); // service unavailable
handleError(request, 406); // 406-Not Acceptable
// handleError(request, 503); // 503-service unavailable
return;
}
#elif CONFIG_IDF_TARGET_ESP32S2
if (len > 12 && (data[0] != 0xE9 || data[12] != 2)) {
handleError(request, 503); // service unavailable
handleError(request, 406); // Not Acceptable
// handleError(request, 503); // service unavailable
return;
}
#elif CONFIG_IDF_TARGET_ESP32C3
if (len > 12 && (data[0] != 0xE9 || data[12] != 5)) {
handleError(request, 503); // service unavailable
handleError(request, 406); // Not Acceptable
// handleError(request, 503); // service unavailable
return;
}
#elif CONFIG_IDF_TARGET_ESP32S3
if (len > 12 && (data[0] != 0xE9 || data[12] != 9)) {
handleError(request, 503); // service unavailable
handleError(request, 406); // Not Acceptable
// handleError(request, 503); // service unavailable
return;
}
#endif
// it's firmware - initialize the ArduinoOTA updater
if (Update.begin()) {
if (Update.begin(fsize - sizeof(esp_image_header_t))) {
if (strlen(md5) == 32) {
Update.setMD5(md5);
md5[0] = '\0';
}
request->onDisconnect(UploadFileService::handleEarlyDisconnect); // success, let's make sure we end the update if the client hangs up
} else {
md5[0] = '\0';
#if defined(EMSESP_USE_SERIAL)
Update.printError(Serial);
#endif
Expand All @@ -95,7 +102,9 @@ void UploadFileService::handleUpload(AsyncWebServerRequest * request, const Stri

if (!is_firmware) {
if (len) {
request->_tempFile.write(data, len); // stream the incoming chunk to the opened file
if (len != request->_tempFile.write(data, len)) { // stream the incoming chunk to the opened file
handleError(request, 507); // 507-Insufficient Storage
}
}
} else {
// if we haven't delt with an error, continue with the firmware update
Expand Down Expand Up @@ -136,13 +145,17 @@ void UploadFileService::uploadComplete(AsyncWebServerRequest * request) {
request->send(response);
return;
}

if (strlen(md5) == 32) {
AsyncWebServerResponse * response = request->beginResponse(201, "text/plain", md5); // created
auto * response = new AsyncJsonResponse(false, 256);
JsonObject root = response->getRoot();
root["md5"] = md5;
response->setLength();
request->send(response);
return;
}

handleError(request, 403); // send the forbidden response
handleError(request, 500);
}

void UploadFileService::handleError(AsyncWebServerRequest * request, int code) {
Expand All @@ -152,9 +165,17 @@ void UploadFileService::handleError(AsyncWebServerRequest * request, int code) {
}

// send the error code to the client and record the error code in the temp object
request->_tempObject = new int(code);
// request->_tempObject = new int(code);
AsyncWebServerResponse * response = request->beginResponse(code);
request->send(response);
md5[0] = '\0';

// check for invalid extension and immediately kill the connection, which will through an error
// that is caught by the web code. Unfortunately the http error code is not sent to the client on fast network connections
if (code == 406) {
handleEarlyDisconnect();
request->client()->close(true);
}
}

void UploadFileService::handleEarlyDisconnect() {
Expand Down
3 changes: 2 additions & 1 deletion lib/framework/WiFiScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ WiFiScanner::WiFiScanner(AsyncWebServer * server, SecurityManager * securityMana
};

void WiFiScanner::scanNetworks(AsyncWebServerRequest * request) {
request->send(202); // special code to indicate scan in progress

if (WiFi.scanComplete() != -1) {
WiFi.scanDelete();
WiFi.scanNetworks(true);
}
request->send(202);
}

void WiFiScanner::listNetworks(AsyncWebServerRequest * request) {
Expand Down

0 comments on commit cfa0744

Please sign in to comment.