diff --git a/cores/esp8266/Updater.cpp b/cores/esp8266/Updater.cpp index de57ef3c6..9f68541f4 100644 --- a/cores/esp8266/Updater.cpp +++ b/cores/esp8266/Updater.cpp @@ -37,12 +37,18 @@ UpdaterClass::UpdaterClass() , _command(U_FLASH) , _hash(nullptr) , _verify(nullptr) +, _progress_callback(nullptr) { #if ARDUINO_SIGNING installSignature(&hash, &sign); #endif } +UpdaterClass& UpdaterClass::onProgress(THandlerFunction_Progress fn) { + _progress_callback = fn; + return *this; +} + void UpdaterClass::_reset() { if (_buffer) delete[] _buffer; @@ -440,7 +446,9 @@ size_t UpdaterClass::writeStream(Stream &data) { _reset(); return 0; } - + if (_progress_callback) { + _progress_callback(0, _size); + } if(_ledPin != -1) { pinMode(_ledPin, OUTPUT); } @@ -471,8 +479,14 @@ size_t UpdaterClass::writeStream(Stream &data) { if((_bufferLen == remaining() || _bufferLen == _bufferSize) && !_writeBuffer()) return written; written += toRead; + if(_progress_callback) { + _progress_callback(progress(), _size); + } yield(); } + if(_progress_callback) { + _progress_callback(progress(), _size); + } return written; } diff --git a/cores/esp8266/Updater.h b/cores/esp8266/Updater.h index e9eea05ef..8de16c7ed 100644 --- a/cores/esp8266/Updater.h +++ b/cores/esp8266/Updater.h @@ -4,6 +4,7 @@ #include #include #include +#include #define UPDATE_ERROR_OK (0) #define UPDATE_ERROR_WRITE (1) @@ -48,6 +49,8 @@ class UpdaterVerifyClass { class UpdaterClass { public: + typedef std::function THandlerFunction_Progress; + UpdaterClass(); /* Optionally add a cryptographic signature verification hash and method */ @@ -111,6 +114,11 @@ class UpdaterClass { */ void md5(uint8_t * result){ return _md5.getBytes(result); } + /* + This callback will be called when Updater is receiving data + */ + UpdaterClass& onProgress(THandlerFunction_Progress fn); + //Helpers uint8_t getError(){ return _error; } void clearError(){ _error = UPDATE_ERROR_OK; } @@ -191,6 +199,8 @@ class UpdaterClass { // Optional signed binary verification UpdaterHashClass *_hash; UpdaterVerifyClass *_verify; + // Optional progress callback function + THandlerFunction_Progress _progress_callback; }; extern UpdaterClass Update;