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:
parent
9f9c661d99
commit
14f1b1d4a7
@ -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;
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <Arduino.h>
|
||||
#include <flash_utils.h>
|
||||
#include <MD5Builder.h>
|
||||
#include <functional>
|
||||
|
||||
#define UPDATE_ERROR_OK (0)
|
||||
#define UPDATE_ERROR_WRITE (1)
|
||||
@ -48,6 +49,8 @@ class UpdaterVerifyClass {
|
||||
|
||||
class UpdaterClass {
|
||||
public:
|
||||
typedef std::function<void(size_t, size_t)> 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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user