Add progress callback to Update::writeStream(). (#948)

This commit is contained in:
Clemens Kirchgatterer 2018-01-18 00:02:58 +01:00 committed by Me No Dev
parent 78acedd2cf
commit 2633fc3c74
2 changed files with 26 additions and 1 deletions

View File

@ -3,6 +3,7 @@
#include <Arduino.h> #include <Arduino.h>
#include <MD5Builder.h> #include <MD5Builder.h>
#include <functional>
#include "esp_partition.h" #include "esp_partition.h"
#define UPDATE_ERROR_OK (0) #define UPDATE_ERROR_OK (0)
@ -27,7 +28,15 @@
class UpdateClass { class UpdateClass {
public: public:
typedef std::function<void(size_t, size_t)> THandlerFunction_Progress;
UpdateClass(); UpdateClass();
/*
This callback will be called when Update is receiving data
*/
UpdateClass& onProgress(THandlerFunction_Progress fn);
/* /*
Call this to check the space needed for the update Call this to check the space needed for the update
Will return false if there is not enough space Will return false if there is not enough space
@ -153,6 +162,8 @@ class UpdateClass {
bool _verifyHeader(uint8_t data); bool _verifyHeader(uint8_t data);
bool _verifyEnd(); bool _verifyEnd();
THandlerFunction_Progress _progress_callback;
uint8_t _error; uint8_t _error;
uint8_t *_buffer; uint8_t *_buffer;
size_t _bufferLen; size_t _bufferLen;

View File

@ -71,9 +71,15 @@ UpdateClass::UpdateClass()
, _progress(0) , _progress(0)
, _command(U_FLASH) , _command(U_FLASH)
, _partition(NULL) , _partition(NULL)
, _progress_callback(NULL)
{ {
} }
UpdateClass& UpdateClass::onProgress(THandlerFunction_Progress fn) {
_progress_callback = fn;
return *this;
}
void UpdateClass::_reset() { void UpdateClass::_reset() {
if (_buffer) if (_buffer)
delete[] _buffer; delete[] _buffer;
@ -306,7 +312,9 @@ size_t UpdateClass::writeStream(Stream &data) {
_reset(); _reset();
return 0; return 0;
} }
if (_progress_callback) {
_progress_callback(0, _size);
}
while(remaining()) { while(remaining()) {
toRead = data.readBytes(_buffer + _bufferLen, (SPI_FLASH_SEC_SIZE - _bufferLen)); toRead = data.readBytes(_buffer + _bufferLen, (SPI_FLASH_SEC_SIZE - _bufferLen));
if(toRead == 0) { //Timeout if(toRead == 0) { //Timeout
@ -321,6 +329,12 @@ size_t UpdateClass::writeStream(Stream &data) {
if((_bufferLen == remaining() || _bufferLen == SPI_FLASH_SEC_SIZE) && !_writeBuffer()) if((_bufferLen == remaining() || _bufferLen == SPI_FLASH_SEC_SIZE) && !_writeBuffer())
return written; return written;
written += toRead; written += toRead;
if(_progress_callback) {
_progress_callback(_progress, _size);
}
}
if(_progress_callback) {
_progress_callback(_size, _size);
} }
return written; return written;
} }