Skip to content

Commit c9afb65

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

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

src/SettingsESP.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,18 @@ class SettingsESP : public sets::SettingsBase {
8888
HTTPUpload& upload = server.upload();
8989
if (upload.status == UPLOAD_FILE_START) {
9090
sets::beginOta();
91+
if (_onStart)
92+
_onStart();
9193
} else if (upload.status == UPLOAD_FILE_WRITE) {
9294
Update.write(upload.buf, upload.currentSize);
95+
if (_onProgress) {
96+
size_t maxSketchSpace = sets::OtaGetMaxSize();
97+
_onProgress(upload.totalSize, maxSketchSpace);
98+
}
9399
} else if (upload.status == UPLOAD_FILE_END) {
94-
Update.end(true);
100+
bool result = Update.end(true);
101+
if (_onDone)
102+
_onDone(result);
95103
} });
96104

97105
server.onNotFound([this]() {
@@ -193,4 +201,4 @@ class SettingsESP : public sets::SettingsBase {
193201
server.sendHeader(F("Access-Control-Allow-Methods"), F("*"));
194202
#endif
195203
}
196-
};
204+
};

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)