Skip to content

Commit 14f1b1d

Browse files
everslickdevyte
authored andcommitted
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).
1 parent 9f9c661 commit 14f1b1d

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

cores/esp8266/Updater.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,18 @@ UpdaterClass::UpdaterClass()
3737
, _command(U_FLASH)
3838
, _hash(nullptr)
3939
, _verify(nullptr)
40+
, _progress_callback(nullptr)
4041
{
4142
#if ARDUINO_SIGNING
4243
installSignature(&hash, &sign);
4344
#endif
4445
}
4546

47+
UpdaterClass& UpdaterClass::onProgress(THandlerFunction_Progress fn) {
48+
_progress_callback = fn;
49+
return *this;
50+
}
51+
4652
void UpdaterClass::_reset() {
4753
if (_buffer)
4854
delete[] _buffer;
@@ -440,7 +446,9 @@ size_t UpdaterClass::writeStream(Stream &data) {
440446
_reset();
441447
return 0;
442448
}
443-
449+
if (_progress_callback) {
450+
_progress_callback(0, _size);
451+
}
444452
if(_ledPin != -1) {
445453
pinMode(_ledPin, OUTPUT);
446454
}
@@ -471,8 +479,14 @@ size_t UpdaterClass::writeStream(Stream &data) {
471479
if((_bufferLen == remaining() || _bufferLen == _bufferSize) && !_writeBuffer())
472480
return written;
473481
written += toRead;
482+
if(_progress_callback) {
483+
_progress_callback(progress(), _size);
484+
}
474485
yield();
475486
}
487+
if(_progress_callback) {
488+
_progress_callback(progress(), _size);
489+
}
476490
return written;
477491
}
478492

cores/esp8266/Updater.h

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <Arduino.h>
55
#include <flash_utils.h>
66
#include <MD5Builder.h>
7+
#include <functional>
78

89
#define UPDATE_ERROR_OK (0)
910
#define UPDATE_ERROR_WRITE (1)
@@ -48,6 +49,8 @@ class UpdaterVerifyClass {
4849

4950
class UpdaterClass {
5051
public:
52+
typedef std::function<void(size_t, size_t)> THandlerFunction_Progress;
53+
5154
UpdaterClass();
5255

5356
/* Optionally add a cryptographic signature verification hash and method */
@@ -111,6 +114,11 @@ class UpdaterClass {
111114
*/
112115
void md5(uint8_t * result){ return _md5.getBytes(result); }
113116

117+
/*
118+
This callback will be called when Updater is receiving data
119+
*/
120+
UpdaterClass& onProgress(THandlerFunction_Progress fn);
121+
114122
//Helpers
115123
uint8_t getError(){ return _error; }
116124
void clearError(){ _error = UPDATE_ERROR_OK; }
@@ -191,6 +199,8 @@ class UpdaterClass {
191199
// Optional signed binary verification
192200
UpdaterHashClass *_hash;
193201
UpdaterVerifyClass *_verify;
202+
// Optional progress callback function
203+
THandlerFunction_Progress _progress_callback;
194204
};
195205

196206
extern UpdaterClass Update;

0 commit comments

Comments
 (0)