From 0389657614e954ef19ab585cd198c0e59a107dfc Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Thu, 10 Dec 2015 12:35:24 +0100 Subject: [PATCH 1/3] give the IP stack more time to handle the data may help with #1157 --- cores/esp8266/Updater.cpp | 50 +++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/cores/esp8266/Updater.cpp b/cores/esp8266/Updater.cpp index 28830fba5..f4b0fd862 100644 --- a/cores/esp8266/Updater.cpp +++ b/cores/esp8266/Updater.cpp @@ -168,9 +168,14 @@ bool UpdaterClass::end(bool evenIfRemaining){ } bool UpdaterClass::_writeBuffer(){ + + yield(); + bool result = ESP.flashEraseSector(_currentAddress/FLASH_SECTOR_SIZE); + yield(); + if (result) { + result = ESP.flashWrite(_currentAddress, (uint32_t*) _buffer, _bufferLen); + } yield(); - bool result = ESP.flashEraseSector(_currentAddress/FLASH_SECTOR_SIZE) && - ESP.flashWrite(_currentAddress, (uint32_t*) _buffer, _bufferLen); if (!result) { _error = UPDATE_ERROR_WRITE; @@ -217,29 +222,32 @@ size_t UpdaterClass::write(uint8_t *data, size_t len) { } size_t UpdaterClass::writeStream(Stream &data) { - size_t written = 0; - size_t toRead = 0; - if(hasError() || !isRunning()) - return 0; + size_t written = 0; + size_t toRead = 0; + if(hasError() || !isRunning()) + return 0; - while(remaining()) { - toRead = FLASH_SECTOR_SIZE - _bufferLen; - toRead = data.readBytes(_buffer + _bufferLen, toRead); - if(toRead == 0){ //Timeout - _error = UPDATE_ERROR_STREAM; - _currentAddress = (_startAddress + _size); + while(remaining()) { + toRead = data.readBytes(_buffer + _bufferLen, (FLASH_SECTOR_SIZE - _bufferLen)); + if(toRead == 0) { //Timeout + delay(100); + toRead = data.readBytes(_buffer + _bufferLen, (FLASH_SECTOR_SIZE - _bufferLen)); + if(toRead == 0) { //Timeout + _error = UPDATE_ERROR_STREAM; + _currentAddress = (_startAddress + _size); #ifdef DEBUG_UPDATER - printError(DEBUG_UPDATER); + printError(DEBUG_UPDATER); #endif - return written; + } + return written; + } + _bufferLen += toRead; + if((_bufferLen == remaining() || _bufferLen == FLASH_SECTOR_SIZE) && !_writeBuffer()) + return written; + written += toRead; + yield(); } - _bufferLen += toRead; - if((_bufferLen == remaining() || _bufferLen == FLASH_SECTOR_SIZE) && !_writeBuffer()) - return written; - written += toRead; - yield(); - } - return written; + return written; } void UpdaterClass::printError(Stream &out){ From 49536c78d35028883d7bbcbbea2bfdddce872682 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Thu, 10 Dec 2015 17:24:39 +0100 Subject: [PATCH 2/3] add more debug to Updater.cpp --- cores/esp8266/Updater.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/cores/esp8266/Updater.cpp b/cores/esp8266/Updater.cpp index f4b0fd862..7ad21300a 100644 --- a/cores/esp8266/Updater.cpp +++ b/cores/esp8266/Updater.cpp @@ -38,14 +38,14 @@ void UpdaterClass::_reset() { bool UpdaterClass::begin(size_t size, int command) { if(_size > 0){ #ifdef DEBUG_UPDATER - DEBUG_UPDATER.println("already running"); + DEBUG_UPDATER.println("[begin] already running"); #endif return false; } #ifdef DEBUG_UPDATER if (command == U_SPIFFS) { - DEBUG_UPDATER.println("Update SPIFFS."); + DEBUG_UPDATER.println("[begin] Update SPIFFS."); } #endif @@ -73,6 +73,12 @@ bool UpdaterClass::begin(size_t size, int command) { //address where we will start writing the update updateStartAddress = updateEndAddress - roundedSize; +#ifdef DEBUG_UPDATER + DEBUG_UPDATER.printf("[begin] roundedSize: 0x%08X (%d)\n", roundedSize, roundedSize); + DEBUG_UPDATER.printf("[begin] updateEndAddress: 0x%08X (%d)\n", updateEndAddress, updateEndAddress); + DEBUG_UPDATER.printf("[begin] currentSketchSize: 0x%08X (%d)\n", currentSketchSize, currentSketchSize); +#endif + //make sure that the size of both sketches is less than the total space (updateEndAddress) if(updateStartAddress < currentSketchSize) { _error = UPDATE_ERROR_SPACE; @@ -88,7 +94,7 @@ bool UpdaterClass::begin(size_t size, int command) { else { // unknown command #ifdef DEBUG_UPDATER - DEBUG_UPDATER.println("Unknown update command."); + DEBUG_UPDATER.println("[begin] Unknown update command."); #endif return false; } @@ -100,6 +106,12 @@ bool UpdaterClass::begin(size_t size, int command) { _buffer = new uint8_t[FLASH_SECTOR_SIZE]; _command = command; +#ifdef DEBUG_UPDATER + DEBUG_UPDATER.printf("[begin] _startAddress: 0x%08X (%d)\n", _startAddress, _startAddress); + DEBUG_UPDATER.printf("[begin] _currentAddress: 0x%08X (%d)\n", _currentAddress, _currentAddress); + DEBUG_UPDATER.printf("[begin] _size: 0x%08X (%d)\n", _size, _size); +#endif + _md5.begin(); return true; } From 7ea4eb452da62b8564855a62e824d97187604946 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Thu, 10 Dec 2015 17:37:09 +0100 Subject: [PATCH 3/3] fix String bug length where returning something that is not 0 while buffer where NULL!? --- cores/esp8266/WString.cpp | 4 ++-- cores/esp8266/WString.h | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index f92344b84..4ba4b1f9f 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -121,6 +121,7 @@ ICACHE_FLASH_ATTR String::~String() { if(buffer) { free(buffer); } + init(); } // /*********************************************/ @@ -136,8 +137,7 @@ inline void String::init(void) { void ICACHE_FLASH_ATTR String::invalidate(void) { if(buffer) free(buffer); - buffer = NULL; - capacity = len = 0; + init(); } unsigned char ICACHE_FLASH_ATTR String::reserve(unsigned int size) { diff --git a/cores/esp8266/WString.h b/cores/esp8266/WString.h index 4c502bfe5..3f216cee4 100644 --- a/cores/esp8266/WString.h +++ b/cores/esp8266/WString.h @@ -76,7 +76,11 @@ class String { // invalid string (i.e., "if (s)" will be true afterwards) unsigned char reserve(unsigned int size); inline unsigned int length(void) const { - return len; + if(buffer) { + return len; + } else { + return 0; + } } // creates a copy of the assigned value. if the value is null or