Skip to content

Add progress callback to Updater class. #5754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 17, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion cores/esp8266/Updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}

Expand Down
10 changes: 10 additions & 0 deletions cores/esp8266/Updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -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;
Expand Down