From 5668927160aa1e88a072ebb2c8046adcc0b76929 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 5 Dec 2018 13:59:53 +0100 Subject: [PATCH 1/3] Fix error in PR #2048: if ::available() is called before ::connect() _rxBuffer is not initialised --- libraries/WiFi/src/WiFiClient.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libraries/WiFi/src/WiFiClient.cpp b/libraries/WiFi/src/WiFiClient.cpp index 6669ef59baf..5c42cc9da1b 100644 --- a/libraries/WiFi/src/WiFiClient.cpp +++ b/libraries/WiFi/src/WiFiClient.cpp @@ -398,6 +398,10 @@ int WiFiClient::peek() int WiFiClient::available() { + if(!_rxBuffer) + { + return 0; + } int res = _rxBuffer->available(); if(_rxBuffer->failed()) { log_e("%d", errno); From cf9175b58f5884241fc507fdd34f1c839ff0c094 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 5 Dec 2018 23:08:23 +0100 Subject: [PATCH 2/3] Fixed flash size check and added SPIFFS size check --- libraries/HTTPUpdate/src/HTTPUpdate.cpp | 31 +++++++++++++++++++------ libraries/HTTPUpdate/src/HTTPUpdate.h | 1 + 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/libraries/HTTPUpdate/src/HTTPUpdate.cpp b/libraries/HTTPUpdate/src/HTTPUpdate.cpp index c3a04cc8913..b03ecaedc48 100644 --- a/libraries/HTTPUpdate/src/HTTPUpdate.cpp +++ b/libraries/HTTPUpdate/src/HTTPUpdate.cpp @@ -118,6 +118,8 @@ String HTTPUpdate::getLastErrorString(void) return "Verify Bin Header Failed"; case HTTP_UE_BIN_FOR_WRONG_FLASH: return "New Binary Does Not Fit Flash Size"; + case HTTP_UE_NO_PARTITION: + return "Partition Could Not be Found"; } return String(); @@ -229,14 +231,25 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren if(len > 0) { bool startUpdate = true; if(spiffs) { -// To do size_t spiffsSize = ((size_t) &_SPIFFS_end - (size_t) &_SPIFFS_start); -// To do if(len > (int) spiffsSize) { -// To do log_e("spiffsSize to low (%d) needed: %d\n", spiffsSize, len); -// To do startUpdate = false; -// To do } + const esp_partition_t* _partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, NULL); + if(!_partition){ + _lastError = HTTP_UE_NO_PARTITION; + return HTTP_UPDATE_FAILED; + } + + if(len > _partition->size) { + log_e("spiffsSize to low (%d) needed: %d\n", _partition->size, len); + startUpdate = false; + } } else { - if(len > (int) ESP.getFreeSketchSpace()) { - log_e("FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len); + const esp_partition_t* _partition = esp_ota_get_next_update_partition(NULL); + if(!_partition){ + _lastError = HTTP_UE_NO_PARTITION; + return HTTP_UPDATE_FAILED; + } + + if(len > _partition->size) { + log_e("FreeSketchSpace to low (%d) needed: %d\n", _partition->size, len); startUpdate = false; } } @@ -366,6 +379,10 @@ bool HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command) } } +// To do: the SHA256 could be checked if the server sends it + + delay(0); + if(Update.writeStream(in) != size) { _lastError = Update.getError(); Update.printError(error); diff --git a/libraries/HTTPUpdate/src/HTTPUpdate.h b/libraries/HTTPUpdate/src/HTTPUpdate.h index 33b77840110..f126cba0639 100644 --- a/libraries/HTTPUpdate/src/HTTPUpdate.h +++ b/libraries/HTTPUpdate/src/HTTPUpdate.h @@ -42,6 +42,7 @@ #define HTTP_UE_SERVER_FAULTY_MD5 (-105) #define HTTP_UE_BIN_VERIFY_HEADER_FAILED (-106) #define HTTP_UE_BIN_FOR_WRONG_FLASH (-107) +#define HTTP_UE_NO_PARTITION (-108) enum HTTPUpdateResult { HTTP_UPDATE_FAILED, From 9e629e733122d6cf474076179edacf2c29ef42b6 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Thu, 6 Dec 2018 19:28:14 +0100 Subject: [PATCH 3/3] Rewriting ESP.getFreeSketchSpace(), moving code from HTTPUpdate.cpp --- cores/esp32/Esp.cpp | 7 ++++++- libraries/HTTPUpdate/src/HTTPUpdate.cpp | 10 ++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cores/esp32/Esp.cpp b/cores/esp32/Esp.cpp index 7f57973ea66..9e5dbc25254 100644 --- a/cores/esp32/Esp.cpp +++ b/cores/esp32/Esp.cpp @@ -169,7 +169,12 @@ uint32_t EspClass::getSketchSize () { } uint32_t EspClass::getFreeSketchSpace () { - return sketchSize(SKETCH_SIZE_FREE); + const esp_partition_t* _partition = esp_ota_get_next_update_partition(NULL); + if(!_partition){ + return 0; + } + + return _partition->size; } uint8_t EspClass::getChipRevision(void) diff --git a/libraries/HTTPUpdate/src/HTTPUpdate.cpp b/libraries/HTTPUpdate/src/HTTPUpdate.cpp index e27b0d58d23..dd4f8623689 100644 --- a/libraries/HTTPUpdate/src/HTTPUpdate.cpp +++ b/libraries/HTTPUpdate/src/HTTPUpdate.cpp @@ -251,14 +251,14 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren startUpdate = false; } } else { - const esp_partition_t* _partition = esp_ota_get_next_update_partition(NULL); - if(!_partition){ + int sketchFreeSpace = ESP.getFreeSketchSpace(); + if(!sketchFreeSpace){ _lastError = HTTP_UE_NO_PARTITION; return HTTP_UPDATE_FAILED; } - if(len > _partition->size) { - log_e("FreeSketchSpace to low (%d) needed: %d\n", _partition->size, len); + if(len > sketchFreeSpace) { + log_e("FreeSketchSpace to low (%d) needed: %d\n", sketchFreeSpace, len); startUpdate = false; } } @@ -390,8 +390,6 @@ bool HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command) // To do: the SHA256 could be checked if the server sends it - delay(0); - if(Update.writeStream(in) != size) { _lastError = Update.getError(); Update.printError(error);