1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Add progress callback to Updater class. (#5754)

* Add progress callback to Updater class.

This is a backport of the same functionality in the ESP32 core.

* Add progress callback to Updater class.

* Add typedef for callback function.

* Fixed initializing order.

* Added missing include (functional).
This commit is contained in:
Clemens Kirchgatterer 2019-02-17 03:45:24 +01:00 committed by Develo
parent 9f9c661d99
commit 14f1b1d4a7
2 changed files with 25 additions and 1 deletions

View File

@ -37,12 +37,18 @@ UpdaterClass::UpdaterClass()
, _command(U_FLASH) , _command(U_FLASH)
, _hash(nullptr) , _hash(nullptr)
, _verify(nullptr) , _verify(nullptr)
, _progress_callback(nullptr)
{ {
#if ARDUINO_SIGNING #if ARDUINO_SIGNING
installSignature(&hash, &sign); installSignature(&hash, &sign);
#endif #endif
} }
UpdaterClass& UpdaterClass::onProgress(THandlerFunction_Progress fn) {
_progress_callback = fn;
return *this;
}
void UpdaterClass::_reset() { void UpdaterClass::_reset() {
if (_buffer) if (_buffer)
delete[] _buffer; delete[] _buffer;
@ -440,7 +446,9 @@ size_t UpdaterClass::writeStream(Stream &data) {
_reset(); _reset();
return 0; return 0;
} }
if (_progress_callback) {
_progress_callback(0, _size);
}
if(_ledPin != -1) { if(_ledPin != -1) {
pinMode(_ledPin, OUTPUT); pinMode(_ledPin, OUTPUT);
} }
@ -471,8 +479,14 @@ size_t UpdaterClass::writeStream(Stream &data) {
if((_bufferLen == remaining() || _bufferLen == _bufferSize) && !_writeBuffer()) if((_bufferLen == remaining() || _bufferLen == _bufferSize) && !_writeBuffer())
return written; return written;
written += toRead; written += toRead;
if(_progress_callback) {
_progress_callback(progress(), _size);
}
yield(); yield();
} }
if(_progress_callback) {
_progress_callback(progress(), _size);
}
return written; return written;
} }

View File

@ -4,6 +4,7 @@
#include <Arduino.h> #include <Arduino.h>
#include <flash_utils.h> #include <flash_utils.h>
#include <MD5Builder.h> #include <MD5Builder.h>
#include <functional>
#define UPDATE_ERROR_OK (0) #define UPDATE_ERROR_OK (0)
#define UPDATE_ERROR_WRITE (1) #define UPDATE_ERROR_WRITE (1)
@ -48,6 +49,8 @@ class UpdaterVerifyClass {
class UpdaterClass { class UpdaterClass {
public: public:
typedef std::function<void(size_t, size_t)> THandlerFunction_Progress;
UpdaterClass(); UpdaterClass();
/* Optionally add a cryptographic signature verification hash and method */ /* Optionally add a cryptographic signature verification hash and method */
@ -111,6 +114,11 @@ class UpdaterClass {
*/ */
void md5(uint8_t * result){ return _md5.getBytes(result); } 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 //Helpers
uint8_t getError(){ return _error; } uint8_t getError(){ return _error; }
void clearError(){ _error = UPDATE_ERROR_OK; } void clearError(){ _error = UPDATE_ERROR_OK; }
@ -191,6 +199,8 @@ class UpdaterClass {
// Optional signed binary verification // Optional signed binary verification
UpdaterHashClass *_hash; UpdaterHashClass *_hash;
UpdaterVerifyClass *_verify; UpdaterVerifyClass *_verify;
// Optional progress callback function
THandlerFunction_Progress _progress_callback;
}; };
extern UpdaterClass Update; extern UpdaterClass Update;