Skip to content

Commit af14c4d

Browse files
committed
added timeout to stream reads for slow networks
1 parent a671d63 commit af14c4d

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

src/esp32FOTA.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,18 @@ bool esp32FOTA::execOTA( int partition, bool restart_after )
438438
return false;
439439
}
440440

441+
// some network streams (e.g. Ethernet) can be laggy and need to 'breathe'
442+
if( ! _stream->available() ) {
443+
uint32_t timeout = millis() + _stream_timeout;
444+
while( ! _stream->available() ) {
445+
if( millis()>timeout ) {
446+
log_e("Stream timed out");
447+
return false;
448+
}
449+
vTaskDelay(1);
450+
}
451+
}
452+
441453
mode_z = F_isZlibStream();
442454

443455
log_d("compression: %s", mode_z ? "enabled" : "disabled" );

src/esp32FOTA.hpp

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,53 @@ extern "C" {
7777
#if __has_include(<flashz.hpp>)
7878
#pragma message "Using FlashZ as Update agent"
7979
#include <flashz.hpp>
80-
#define F_Update FlashZ::getInstance()
80+
#define F_Compression "zlib"
8181
#define F_hasZlib() true
82-
#define F_isZlibStream() (_stream->peek() == ZLIB_HEADER && ((partition == U_SPIFFS && _flashFileSystemUrl.indexOf("zz")>-1) || (partition == U_FLASH && _firmwareUrl.indexOf("zz")>-1)))
83-
#define F_canBegin() (mode_z ? F_Update.beginz(UPDATE_SIZE_UNKNOWN, partition) : F_Update.begin(updateSize, partition))
82+
#define F_Update FlashZ::getInstance()
8483
#define F_UpdateEnd() (mode_z ? F_Update.endz() : F_Update.end())
8584
#define F_abort() if (mode_z) F_Update.abortz(); else F_Update.abort()
8685
#define F_writeStream() (mode_z ? F_Update.writezStream(*_stream, updateSize) : F_Update.writeStream(*_stream))
86+
// #define DEBUG_ESP32_FLASHZ
87+
#if !defined DEBUG_ESP32_FLASHZ
88+
#define F_isZlibStream() (_stream->peek() == ZLIB_HEADER && ((partition == U_SPIFFS && _flashFileSystemUrl.indexOf("zz")>-1) || (partition == U_FLASH && _firmwareUrl.indexOf("zz")>-1)))
89+
#define F_canBegin() (mode_z ? F_Update.beginz(UPDATE_SIZE_UNKNOWN, partition) : F_Update.begin(updateSize, partition))
90+
#else
91+
__attribute__((unused)) static bool F_canBegin_cb(bool mode_z, int updateSize, int partition) { // implement debug here
92+
return (mode_z ? F_Update.beginz(UPDATE_SIZE_UNKNOWN, partition) : F_Update.begin(updateSize, partition));
93+
}
94+
__attribute__((unused)) static bool F_isZlibStream_cb( Stream* stream, int partition, String flashFileSystemUrl, String firmwareUrl ) { // implement debug here
95+
return (stream->peek() == ZLIB_HEADER && ((partition == U_SPIFFS && flashFileSystemUrl.indexOf("zz")>-1) || (partition == U_FLASH && firmwareUrl.indexOf("zz")>-1)));
96+
}
97+
#define F_isZlibStream() F_isZlibStream_cb( _stream, partition, _flashFileSystemUrl, _firmwareUrl )
98+
#define F_canBegin() F_canBegin_cb(mode_z, updateSize, partition)
99+
#endif
100+
87101
#elif __has_include("ESP32-targz.h")
88102
#pragma message "Using GzUpdateClass as Update agent"
89103
#include <ESP32-targz.h>
90-
#define F_Update GzUpdateClass::getInstance()
104+
#define F_Compression "gzip"
91105
#define F_hasZlib() true
92-
#define F_isZlibStream() (_stream->peek() == 0x1f && ((partition == U_SPIFFS && _flashFileSystemUrl.indexOf("gz")>-1) || (partition == U_FLASH && _firmwareUrl.indexOf("gz")>-1)) )
93-
#define F_canBegin() (mode_z ? F_Update.begingz(UPDATE_SIZE_UNKNOWN, partition) : F_Update.begin(updateSize, partition))
106+
#define F_Update GzUpdateClass::getInstance()
94107
#define F_UpdateEnd() (mode_z ? F_Update.endgz() : F_Update.end())
95108
#define F_abort() if (mode_z) F_Update.abortgz(); else F_Update.abort()
96109
#define F_writeStream() (mode_z ? F_Update.writeGzStream(*_stream, updateSize) : F_Update.writeStream(*_stream))
110+
// #define DEBUG_ESP32_TARGZ
111+
#if !defined DEBUG_ESP32_TARGZ
112+
#define F_isZlibStream() (_stream->peek() == 0x1f && ((partition == U_SPIFFS && _flashFileSystemUrl.indexOf("gz")>-1) || (partition == U_FLASH && _firmwareUrl.indexOf("gz")>-1)) )
113+
#define F_canBegin() (mode_z ? F_Update.begingz(UPDATE_SIZE_UNKNOWN, partition) : F_Update.begin(updateSize, partition))
114+
#else
115+
__attribute__((unused)) static bool F_canBegin_cb(bool mode_z, int updateSize, int partition) { // implement debug here
116+
return (mode_z ? F_Update.begingz(UPDATE_SIZE_UNKNOWN, partition) : F_Update.begin(updateSize, partition));
117+
}
118+
__attribute__((unused)) static bool F_isZlibStream_cb( Stream* stream, int partition, String flashFileSystemUrl, String firmwareUrl ) { // implement debug here
119+
return (stream->peek() == 0x1f && ((partition == U_SPIFFS && flashFileSystemUrl.indexOf("gz")>-1) || (partition == U_FLASH && firmwareUrl.indexOf("gz")>-1)) );
120+
}
121+
#define F_isZlibStream() F_isZlibStream_cb( _stream, partition, _flashFileSystemUrl, _firmwareUrl )
122+
#define F_canBegin() F_canBegin_cb(mode_z, updateSize, partition)
123+
#endif
97124
#else
98125
#include <Update.h>
126+
#define F_Compression "none"
99127
#define F_Update Update
100128
#define F_hasZlib() false
101129
#define F_isZlibStream() false
@@ -259,6 +287,7 @@ class esp32FOTA
259287

260288
// updating from a File or from Serial?
261289
void setStreamType( FOTAStreamType_t stream_type ) { _stream_type = stream_type; }
290+
void setStreamTimeout( uint32_t timeout ) { _stream_timeout = timeout; }
262291

263292
const char* getManifestURL() { return _manifestUrl.c_str(); }
264293
const char* getFirmwareURL() { return _firmwareUrl.c_str(); }
@@ -296,6 +325,7 @@ class esp32FOTA
296325
bool mode_z = F_hasZlib();
297326

298327
FOTAStreamType_t _stream_type = FOTA_HTTP_STREAM; // defaults to HTTP
328+
uint32_t _stream_timeout = 10000; // max wait for stream->available()
299329

300330
void setupStream();
301331
void stopStream();

0 commit comments

Comments
 (0)