Skip to content

Commit aa60803

Browse files
OTA: Add firmware update callbacks
Signed-off-by: Vitaliy Andreevich <[email protected]>
1 parent 011cdbd commit aa60803

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

src/SettingsESP.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,20 @@ class SettingsESP : public sets::SettingsBase {
8585
String auth = server.arg(F("auth"));
8686
if (!authenticate(Text(auth).toInt32HEX())) return;
8787

88+
size_t maxSketchSpace = sets::OtaGetMaxSize();
8889
HTTPUpload& upload = server.upload();
8990
if (upload.status == UPLOAD_FILE_START) {
9091
sets::beginOta();
92+
if (_onStart)
93+
_onStart();
9194
} else if (upload.status == UPLOAD_FILE_WRITE) {
9295
Update.write(upload.buf, upload.currentSize);
96+
if (_onProgress)
97+
_onProgress(upload.totalSize, maxSketchSpace);
9398
} else if (upload.status == UPLOAD_FILE_END) {
94-
Update.end(true);
99+
bool result = Update.end(true);
100+
if (_onDone)
101+
_onDone(result);
95102
} });
96103

97104
server.onNotFound([this]() {

src/core/SettingsBase.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@
2626

2727
namespace sets {
2828

29+
typedef void (*onUpdateFWStart_t)();
30+
typedef void (*onUpdateFWProgress_t)(size_t current, size_t final);
31+
typedef void (*onUpdateFWDone_t)(bool success);
32+
2933
class SettingsBase {
3034
static const uint16_t FOCUS_TOUT = 5000;
3135
static const uint16_t DB_WS_UPDATE_PRD = 300;
3236

3337
protected:
38+
sets::onUpdateFWStart_t _onStart = nullptr;
39+
sets::onUpdateFWProgress_t _onProgress = nullptr;
40+
sets::onUpdateFWDone_t _onDone = nullptr;
41+
3442
typedef std::function<void(Builder& b)> BuildCallback;
3543
typedef std::function<void(Updater& upd)> UpdateCallback;
3644
typedef std::function<void(Text path)> FileCallback;
@@ -116,6 +124,18 @@ class SettingsBase {
116124
}
117125

118126
public:
127+
void onUpdateFWStart(sets::onUpdateFWStart_t fn) {
128+
_onStart = fn;
129+
};
130+
131+
void onUpdateFWProgress(sets::onUpdateFWProgress_t fn) {
132+
_onProgress = fn;
133+
};
134+
135+
void onUpdateFWDone(sets::onUpdateFWDone_t fn) {
136+
_onDone = fn;
137+
};
138+
119139
#ifndef SETT_NO_DB
120140
SettingsBase(const String& title = "", GyverDB* db = nullptr) : _title(title), _db(db) {
121141
#else

src/core/ota.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,38 @@
88
#endif
99

1010
namespace sets {
11+
size_t OtaGetMaxSize(bool ota_flash)
12+
{
13+
size_t ota_size = 0;
14+
if (ota_flash) {
15+
#ifdef ESP8266
16+
ota_size = (size_t)((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000);
17+
#else
18+
ota_size = UPDATE_SIZE_UNKNOWN;
19+
#endif
20+
} else {
21+
#ifdef ESP8266
22+
close_all_fs();
23+
ota_size = (size_t)&_FS_end - (size_t)&_FS_start;
24+
#else
25+
ota_size = UPDATE_SIZE_UNKNOWN;
26+
#endif
27+
}
28+
return ota_size;
29+
}
1130

1231
bool beginOta(bool ota_flash, bool async) {
13-
size_t ota_size = 0;
32+
size_t ota_size = OtaGetMaxSize(ota_flash);
1433
int ota_type = 0;
1534

1635
if (ota_flash) {
1736
ota_type = U_FLASH;
18-
#ifdef ESP8266
19-
ota_size = (size_t)((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000);
20-
#else
21-
ota_size = UPDATE_SIZE_UNKNOWN;
22-
#endif
2337
} else {
2438
#ifdef ESP8266
2539
ota_type = U_FS;
2640
close_all_fs();
27-
ota_size = (size_t)&_FS_end - (size_t)&_FS_start;
2841
#else
2942
ota_type = U_SPIFFS;
30-
ota_size = UPDATE_SIZE_UNKNOWN;
3143
#endif
3244
}
3345

src/core/ota.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#endif
88

99
namespace sets {
10-
10+
size_t OtaGetMaxSize(bool ota_flash = true);
1111
bool beginOta(bool ota_flash = true, bool async = false);
1212

1313
} // namespace sets

0 commit comments

Comments
 (0)