From a4917f4a23d0514a16f97b5d1b086734ed65f130 Mon Sep 17 00:00:00 2001 From: Clemens Kirchgatterer Date: Tue, 12 Feb 2019 13:16:42 +0100 Subject: [PATCH 1/5] Add progress callback to Updater class. This is a backport of the same functionality in the ESP32 core. --- cores/esp8266/Updater.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cores/esp8266/Updater.h b/cores/esp8266/Updater.h index e9eea05ef7..0d4854fca7 100644 --- a/cores/esp8266/Updater.h +++ b/cores/esp8266/Updater.h @@ -111,6 +111,11 @@ class UpdaterClass { */ void md5(uint8_t * result){ return _md5.getBytes(result); } + /* + This callback will be called when Updater is receiving data + */ + UpdateClass& onProgress(THandlerFunction_Progress fn); + //Helpers uint8_t getError(){ return _error; } void clearError(){ _error = UPDATE_ERROR_OK; } @@ -178,6 +183,7 @@ class UpdaterClass { size_t _bufferLen; // amount of data written into _buffer size_t _bufferSize; // total size of _buffer size_t _size; + THandlerFunction_Progress _progress_callback; uint32_t _startAddress; uint32_t _currentAddress; uint32_t _command; From 2d836a98f1922869051de245d1cedb567da66a4c Mon Sep 17 00:00:00 2001 From: Clemens Kirchgatterer Date: Tue, 12 Feb 2019 13:30:42 +0100 Subject: [PATCH 2/5] Add progress callback to Updater class. --- cores/esp8266/Updater.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/cores/esp8266/Updater.cpp b/cores/esp8266/Updater.cpp index de57ef3c66..9f68541f44 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; } From 0012bfde136ac5370e8332f7ab369494c1429ba8 Mon Sep 17 00:00:00 2001 From: Clemens Kirchgatterer Date: Tue, 12 Feb 2019 13:40:23 +0100 Subject: [PATCH 3/5] Add typedef for callback function. --- cores/esp8266/Updater.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cores/esp8266/Updater.h b/cores/esp8266/Updater.h index 0d4854fca7..0734e8c9d0 100644 --- a/cores/esp8266/Updater.h +++ b/cores/esp8266/Updater.h @@ -48,6 +48,8 @@ class UpdaterVerifyClass { class UpdaterClass { public: + typedef std::function THandlerFunction_Progress; + UpdaterClass(); /* Optionally add a cryptographic signature verification hash and method */ @@ -114,7 +116,7 @@ class UpdaterClass { /* This callback will be called when Updater is receiving data */ - UpdateClass& onProgress(THandlerFunction_Progress fn); + UpdaterClass& onProgress(THandlerFunction_Progress fn); //Helpers uint8_t getError(){ return _error; } From af7947082b6d92b37281d03a8f25464e4facc3eb Mon Sep 17 00:00:00 2001 From: Clemens Kirchgatterer Date: Tue, 12 Feb 2019 16:28:27 +0100 Subject: [PATCH 4/5] Fixed initializing order. --- cores/esp8266/Updater.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cores/esp8266/Updater.h b/cores/esp8266/Updater.h index 0734e8c9d0..2db705b60b 100644 --- a/cores/esp8266/Updater.h +++ b/cores/esp8266/Updater.h @@ -185,7 +185,6 @@ class UpdaterClass { size_t _bufferLen; // amount of data written into _buffer size_t _bufferSize; // total size of _buffer size_t _size; - THandlerFunction_Progress _progress_callback; uint32_t _startAddress; uint32_t _currentAddress; uint32_t _command; @@ -199,6 +198,8 @@ class UpdaterClass { // Optional signed binary verification UpdaterHashClass *_hash; UpdaterVerifyClass *_verify; + // Optional progress callback function + THandlerFunction_Progress _progress_callback; }; extern UpdaterClass Update; From a17f7b3fe65e6d403ff5ff52a08f02831a425c9a Mon Sep 17 00:00:00 2001 From: Clemens Kirchgatterer Date: Tue, 12 Feb 2019 17:17:44 +0100 Subject: [PATCH 5/5] Added missing include (functional). --- cores/esp8266/Updater.h | 1 + 1 file changed, 1 insertion(+) diff --git a/cores/esp8266/Updater.h b/cores/esp8266/Updater.h index 2db705b60b..8de16c7ed6 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)