From af0f3a78fdb8c7a82c8956286dc6b80ca62564c4 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Mon, 5 Nov 2018 15:28:26 +0100 Subject: [PATCH] OTA improved (LED flashing, faster buffer closing at end of write) --- include/ota.h | 2 +- include/update.h | 5 ++++- src/ota.cpp | 23 +++++++++++++++-------- src/update.cpp | 33 ++++++++++++++++++++++++++++----- 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/include/ota.h b/include/ota.h index 174e7a03..3c953ac0 100644 --- a/include/ota.h +++ b/include/ota.h @@ -11,7 +11,7 @@ #include #include -bool do_ota_update(); +int do_ota_update(); void start_ota_update(); int version_compare(const String v1, const String v2); void display(const uint8_t row, const std::string status, diff --git a/include/update.h b/include/update.h index 5b0c8d4b..87c6f35c 100644 --- a/include/update.h +++ b/include/update.h @@ -41,7 +41,7 @@ class UpdateClass { Call this to check the space needed for the update Will return false if there is not enough space */ - bool begin(size_t size=UPDATE_SIZE_UNKNOWN, int command = U_FLASH); + bool begin(size_t size=UPDATE_SIZE_UNKNOWN, int command = U_FLASH, int ledPin = -1, uint8_t ledOn = LOW); /* Writes a buffer to the flash and increments the address @@ -174,6 +174,9 @@ class UpdateClass { String _target_md5; MD5Builder _md5; + + int _ledPin; + uint8_t _ledOn; }; extern UpdateClass Update; diff --git a/src/ota.cpp b/src/ota.cpp index 8ede3aed..778e7cf2 100644 --- a/src/ota.cpp +++ b/src/ota.cpp @@ -26,8 +26,6 @@ const BintrayClient bintray(BINTRAY_USER, BINTRAY_REPO, BINTRAY_PACKAGE); // Connection port (HTTPS) const int port = 443; -const unsigned long STREAM_TIMEOUT = 30000; - // Variables to validate firmware content int volatile contentLength = 0; bool volatile isValidContentType = false; @@ -77,7 +75,7 @@ void start_ota_update() { WiFi.begin(WIFI_SSID, WIFI_PASS); int i = WIFI_MAX_TRY, j = OTA_MAX_TRY; - bool ret = 1; + int ret = 1; // 0 = finished, 1 = retry, -1 = abort ESP_LOGI(TAG, "Trying to connect to %s", WIFI_SSID); while (i--) { @@ -110,7 +108,9 @@ end: } // start_ota_update -bool do_ota_update() { +// Reads data vom wifi client and flashes it to ota partition +// returns: 0 = finished, 1 = retry, -1 = abort +int do_ota_update() { char buf[17]; bool redirect = true; @@ -128,7 +128,7 @@ bool do_ota_update() { } else if (version_compare(latest, cfg.version) <= 0) { ESP_LOGI(TAG, "Current firmware is up to date"); display(2, "NO", "no update found"); - return -2; + return -1; } ESP_LOGI(TAG, "New firmware version v%s available", latest.c_str()); display(2, "OK", latest.c_str()); @@ -147,7 +147,7 @@ bool do_ota_update() { WiFiClientSecure client; client.setCACert(bintray.getCertificate(currentHost)); - //client.setTimeout(RESPONSE_TIMEOUT_MS); + // client.setTimeout(RESPONSE_TIMEOUT_MS); // --> causing error [E][WiFiClient.cpp:236] setSocketOption(): 1006 : 9 // so we unfortunately need patched update.cpp which sets the stream timeout @@ -206,8 +206,7 @@ bool do_ota_update() { redirect = true; } else { ESP_LOGI(TAG, "Could not get firmware download URL"); - // Unexptected HTTP response. Retry or skip update? - redirect = false; + goto retry; } } @@ -248,7 +247,15 @@ bool do_ota_update() { goto retry; } +#ifdef HAS_LED +#ifndef LED_ACTIVE_LOW + if (!Update.begin(contentLength, U_FLASH, HAS_LED, HIGH)) { +#else + if (!Update.begin(contentLength, U_FLASH, HAS_LED, LOW)) { +#endif +#else if (!Update.begin(contentLength)) { +#endif ESP_LOGI(TAG, "Not enough space to start OTA update"); display(4, " E", "disk full"); goto abort; diff --git a/src/update.cpp b/src/update.cpp index b59ec284..618720f5 100644 --- a/src/update.cpp +++ b/src/update.cpp @@ -1,9 +1,9 @@ /* this file copied from esp32-arduino library and patched, see PR -https://github.com/espressif/arduino-esp32/pull/1886 +https://github.com/espressif/arduino-esp32/pull/1979 */ -#include "update.h" +#include "Update.h" #include "Arduino.h" #include "esp_spi_flash.h" #include "esp_ota_ops.h" @@ -93,6 +93,10 @@ void UpdateClass::_reset() { _progress = 0; _size = 0; _command = U_FLASH; + + if(_ledPin != -1) { + digitalWrite(_ledPin, !_ledOn); // off + } } bool UpdateClass::canRollBack(){ @@ -111,12 +115,15 @@ bool UpdateClass::rollBack(){ return _partitionIsBootable(partition) && !esp_ota_set_boot_partition(partition); } -bool UpdateClass::begin(size_t size, int command) { +bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) { if(_size > 0){ log_w("already running"); return false; } + _ledPin = ledPin; + _ledOn = !!ledOn; // 0(LOW) or 1(HIGH) + _reset(); _error = 0; @@ -321,16 +328,32 @@ size_t UpdateClass::writeStream(Stream &data) { if (_progress_callback) { _progress_callback(0, _size); } + + if(_ledPin != -1) { + pinMode(_ledPin, OUTPUT); + } + while(remaining()) { - toRead = data.readBytes(_buffer + _bufferLen, (SPI_FLASH_SEC_SIZE - _bufferLen)); + if(_ledPin != -1) { + digitalWrite(_ledPin, _ledOn); // Switch LED on + } + size_t bytesToRead = SPI_FLASH_SEC_SIZE - _bufferLen; + if(bytesToRead > remaining()) { + bytesToRead = remaining(); + } + + toRead = data.readBytes(_buffer + _bufferLen, bytesToRead); if(toRead == 0) { //Timeout delay(100); - toRead = data.readBytes(_buffer + _bufferLen, (SPI_FLASH_SEC_SIZE - _bufferLen)); + toRead = data.readBytes(_buffer + _bufferLen, bytesToRead); if(toRead == 0) { //Timeout _abort(UPDATE_ERROR_STREAM); return written; } } + if(_ledPin != -1) { + digitalWrite(_ledPin, !_ledOn); // Switch LED off + } _bufferLen += toRead; if((_bufferLen == remaining() || _bufferLen == SPI_FLASH_SEC_SIZE) && !_writeBuffer()) return written;