From 8637b0f72d0e4ec0d496dd9137479d760e7cfb04 Mon Sep 17 00:00:00 2001 From: Magnus Persson Date: Sun, 20 Mar 2022 15:35:37 +0100 Subject: [PATCH] Fix crashbug in ota --- src/wifi.cpp | 31 +++++++------------------------ src/wifi.hpp | 8 +++++++- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/src/wifi.cpp b/src/wifi.cpp index a1644c7..494a011 100644 --- a/src/wifi.cpp +++ b/src/wifi.cpp @@ -22,11 +22,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #if defined(ESP8266) -#include #include #include #else // defined (ESP32) -#include #include #include #include @@ -276,37 +274,22 @@ bool WifiConnection::updateFirmware() { // // Download and save file // -void WifiConnection::downloadFile(const char *fname) { +void WifiConnection::downloadFile(HTTPClient& http, String& fname) { #if LOG_LEVEL == 6 && !defined(WIFI_DISABLE_LOGGING) Log.verbose(F("WIFI: Download file %s." CR), fname); #endif - WiFiClient wifi; - WiFiClientSecure wifiSecure; - HTTPClient http; - String serverPath = myConfig.getOtaURL(); - serverPath += fname; - - if (myConfig.isOtaSSL()) { - wifiSecure.setInsecure(); - Log.notice(F("WIFI: OTA, SSL enabled without validation." CR)); - http.begin(wifiSecure, serverPath); - } else { - http.begin(wifi, serverPath); - } - int httpResponseCode = http.GET(); if (httpResponseCode == 200) { File f = LittleFS.open(fname, "w"); http.writeToStream(&f); f.close(); - Log.notice(F("WIFI: Downloaded file %s." CR), fname); + Log.notice(F("WIFI: Downloaded file %s." CR), fname.c_str()); } else { ErrorFileLog errLog; errLog.addEntry("WIFI: Failed to download html-file " + String(httpResponseCode)); } - http.end(); } // @@ -332,6 +315,7 @@ bool WifiConnection::checkFirmwareVersion() { } // Send HTTP GET request + DynamicJsonDocument ver(300); int httpResponseCode = http.GET(); if (httpResponseCode == 200) { @@ -341,7 +325,6 @@ bool WifiConnection::checkFirmwareVersion() { #if LOG_LEVEL == 6 && !defined(WIFI_DISABLE_LOGGING) Log.verbose(F("WIFI: Payload %s." CR), payload.c_str()); #endif - DynamicJsonDocument ver(300); DeserializationError err = deserializeJson(ver, payload); if (err) { ErrorFileLog errLog; @@ -364,10 +347,10 @@ bool WifiConnection::checkFirmwareVersion() { // Compare major version if (newVer[0] > curVer[0]) _newFirmware = true; // Compare minor version - if (newVer[0] == curVer[0] && newVer[1] > curVer[1]) + else if (newVer[0] == curVer[0] && newVer[1] > curVer[1]) _newFirmware = true; // Compare patch version - if (newVer[0] == curVer[0] && newVer[1] == curVer[1] && + else if (newVer[0] == curVer[0] && newVer[1] == curVer[1] && newVer[2] > curVer[2]) _newFirmware = true; } @@ -375,14 +358,14 @@ bool WifiConnection::checkFirmwareVersion() { // Download new html files to filesystem if they are present. if (!ver["html"].isNull() && _newFirmware) { - Log.notice(F("WIFI: OTA downloading new html files." CR)); + Log.notice(F("WIFI: OTA checking if html files should be downloaded." CR)); JsonArray htmlFiles = ver["html"].as(); for (JsonVariant v : htmlFiles) { String s = v; #if LOG_LEVEL == 6 Log.verbose(F("WIFI: OTA listed html file %s" CR), s.c_str()); #endif - downloadFile(s.c_str()); + downloadFile(http, s); } } } diff --git a/src/wifi.hpp b/src/wifi.hpp index f986d75..7eb71bf 100644 --- a/src/wifi.hpp +++ b/src/wifi.hpp @@ -24,6 +24,12 @@ SOFTWARE. #ifndef SRC_WIFI_HPP_ #define SRC_WIFI_HPP_ +#if defined(ESP8266) +#include +#else // defined (ESP32) +#include +#endif + #define WIFI_DEFAULT_SSID "GravityMon" // Name of created SSID #define WIFI_DEFAULT_PWD "password" // Password for created SSID #define WIFI_MDNS "gravitymon" // Prefix for MDNS name @@ -35,7 +41,7 @@ class WifiConnection { // OTA bool _newFirmware = false; bool parseFirmwareVersionString(int (&num)[3], const char* version); - void downloadFile(const char* fname); + void downloadFile(HTTPClient& http, String& fname); void connectAsync(); bool waitForConnection(int maxTime = 20);