Skip to content

Commit cf0952b

Browse files
committed
Supports a callback with OTA status change #325
1 parent 0daf985 commit cf0952b

File tree

5 files changed

+78
-32
lines changed

5 files changed

+78
-32
lines changed

src/AutoConnect.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* AutoConnect class implementation.
33
* @file AutoConnect.cpp
44
5-
* @version 1.2.3
6-
* @date 2021-01-13
5+
* @version 1.3.0
6+
* @date 2021-03-29
77
* @copyright MIT license.
88
*/
99

@@ -642,12 +642,14 @@ void AutoConnect::handleRequest(void) {
642642
_ota->attach(*this);
643643
_ota->authentication(_apConfig.auth);
644644
_ota->setTicker(_apConfig.tickerPort, _apConfig.tickerOn);
645+
if (_onOTAStatusChangeExit)
646+
_ota->onStatusChange(_onOTAStatusChangeExit);
645647
}
646648
}
647649

648650
// Post-process for AutoConnectOTA
649651
if (_ota) {
650-
if (_ota->status() == AutoConnectOTA::OTA_RIP) {
652+
if (_ota->status() == AC_OTA_RIP) {
651653
// Indicate the reboot at the next handleClient turn
652654
// with on completion of the update via OTA.
653655
if (_webServer->client().connected()) {
@@ -659,7 +661,7 @@ void AutoConnect::handleRequest(void) {
659661
// OTA for firmware update requires module reset.
660662
_rfReset = true;
661663
}
662-
else if (_ota->status() == AutoConnectOTA::OTA_PROGRESS)
664+
else if (_ota->status() == AC_OTA_PROGRESS)
663665
skipPostTicker = true;
664666
// Reflect the menu display specifier from AutoConnectConfig to
665667
// AutoConnectOTA page
@@ -860,6 +862,14 @@ void AutoConnect::whileCaptivePortal(WhileCaptivePortalExit_ft fn) {
860862
_whileCaptivePortal = fn;
861863
}
862864

865+
/**
866+
* Register a status change notification callback function
867+
* @param fn A status change notification callback function.
868+
*/
869+
void AutoConnect::onOTAStatusChange(OTAStatusChangeExit_ft fn) {
870+
_onOTAStatusChangeExit = fn;
871+
}
872+
863873
/**
864874
* Load current available credential
865875
* @param ssid A pointer to the buffer that SSID should be stored.

src/AutoConnect.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* Declaration of AutoConnect class and accompanying AutoConnectConfig class.
33
* @file AutoConnect.h
44
5-
* @version 1.2.3
6-
* @date 2021-01-13
5+
* @version 1.3.0
6+
* @date 2021-03-29
77
* @copyright MIT license.
88
*/
99

@@ -29,11 +29,11 @@ using WebServerClass = WebServer;
2929
#include <EEPROM.h>
3030
#include <PageBuilder.h>
3131
#include "AutoConnectDefs.h"
32+
#include "AutoConnectTypes.h"
3233
#include "AutoConnectPage.h"
3334
#include "AutoConnectCredential.h"
3435
#include "AutoConnectTicker.h"
3536
#include "AutoConnectAux.h"
36-
#include "AutoConnectTypes.h"
3737

3838
// The realization of AutoConnectOTA is effective only by the explicit
3939
#include "AutoConnectOTA.h"
@@ -264,10 +264,12 @@ class AutoConnect {
264264
typedef std::function<bool(IPAddress&)> DetectExit_ft;
265265
typedef std::function<void(IPAddress&)> ConnectExit_ft;
266266
typedef std::function<bool(void)> WhileCaptivePortalExit_ft;
267+
typedef std::function<bool(const AC_OTAStatus_t)> OTAStatusChangeExit_ft;
267268
void onDetect(DetectExit_ft fn);
268269
void onConnect(ConnectExit_ft fn);
269270
void onNotFound(WebServerClass::THandlerFunction fn);
270271
void whileCaptivePortal(WhileCaptivePortalExit_ft fn);
272+
void onOTAStatusChange(OTAStatusChangeExit_ft fn);
271273

272274
protected:
273275
typedef enum {
@@ -329,6 +331,7 @@ class AutoConnect {
329331
DetectExit_ft _onDetectExit;
330332
WhileCaptivePortalExit_ft _whileCaptivePortal;
331333
WebServerClass::THandlerFunction _notFoundHandler;
334+
OTAStatusChangeExit_ft _onOTAStatusChangeExit;
332335
size_t _freeHeapSize;
333336

334337
/** Servers which works in concert. */

src/AutoConnectOTA.cpp

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* AutoConnectOTA class implementation.
33
* @file AutoConnectOTA.cpp
44
5-
* @version 1.2.3
6-
* @date 2021-01-23
5+
* @version 1.3.0
6+
* @date 2021-03-29
77
* @copyright MIT license.
88
*/
99

@@ -230,8 +230,16 @@ bool AutoConnectOTA::_open(const char* filename, const char* mode) {
230230
if (bc) {
231231
if (_tickerPort != -1)
232232
pinMode(static_cast<uint8_t>(_tickerPort), OUTPUT);
233-
_status = OTA_START;
233+
_status = AC_OTA_START;
234234
AC_DBG("%s up%s start\n", filename, _dest == OTA_DEST_FIRM ? "dating" : "loading");
235+
236+
// Notify an OTA status change
237+
if (_onStatusChange) {
238+
if (!_onStatusChange(_status)) {
239+
_setError("OTA cancellation requested");
240+
bc = false;
241+
}
242+
}
235243
}
236244
return bc;
237245
}
@@ -244,11 +252,21 @@ bool AutoConnectOTA::_open(const char* filename, const char* mode) {
244252
* @return the amount written
245253
*/
246254
size_t AutoConnectOTA::_write(const uint8_t *buf, const size_t size) {
255+
AC_OTAStatus_t ps = _status;
247256
size_t wsz = 0;
248257
if (_tickerPort != -1)
249258
digitalWrite(_tickerPort, digitalRead(_tickerPort) ^ 0x01);
250259
if (!_err.length()) {
251-
_status = OTA_PROGRESS;
260+
_status = AC_OTA_PROGRESS;
261+
if (ps != _status) {
262+
// Notify an OTA status change
263+
if (_onStatusChange) {
264+
if (!_onStatusChange(_status)) {
265+
_setError("OTA cancellation requested");
266+
return 0;
267+
}
268+
}
269+
}
252270

253271
if (_dest == OTA_DEST_FIRM) {
254272
wsz = Update.write(const_cast<uint8_t*>(buf), size);
@@ -282,8 +300,10 @@ void AutoConnectOTA::_close(const HTTPUploadStatus status) {
282300
_file.close();
283301
}
284302
if (ec) {
285-
_status = OTA_SUCCESS;
303+
_status = AC_OTA_SUCCESS;
286304
AC_DBG_DUMB("succeeds");
305+
if (_onStatusChange) // Notify an OTA status change
306+
_onStatusChange(_status);
287307
}
288308
else {
289309
_setError();
@@ -315,11 +335,15 @@ String AutoConnectOTA::_updated(AutoConnectAux& result, PageArgument& args) {
315335

316336
// Build an updating result caption.
317337
// Change the color of the bin name depending on the result of the update.
318-
if (_status == OTA_SUCCESS) {
338+
if (_status == AC_OTA_SUCCESS) {
319339
st = _dest == OTA_DEST_FIRM ? String(F(AUTOCONNECT_TEXT_OTASUCCESS)) : String(F(AUTOCONNECT_TEXT_OTAUPLOADED));
320340
stColor = PSTR("3d7e9a");
321341
// Notify to the handleClient of loop() thread that it can reboot.
322-
_status = OTA_RIP;
342+
_status = AC_OTA_RIP;
343+
if (_onStatusChange) { // Notify an OTA status change
344+
if (!_onStatusChange(_status))
345+
_status = AC_OTA_IDLE;
346+
}
323347
}
324348
else {
325349
st = String(F(AUTOCONNECT_TEXT_OTAFAILURE)) + _err;
@@ -347,11 +371,12 @@ String AutoConnectOTA::_updated(AutoConnectAux& result, PageArgument& args) {
347371
void AutoConnectOTA::_setError(void) {
348372
StreamString eStr;
349373
Update.printError(eStr);
350-
_err = String(eStr.c_str());
351-
_status = OTA_FAIL;
374+
_setError(eStr.c_str());
352375
}
353376

354377
void AutoConnectOTA::_setError(const char* err) {
355378
_err = String(err);
356-
_status = OTA_FAIL;
379+
_status = AC_OTA_FAIL;
380+
if (_onStatusChange)
381+
_onStatusChange(_status);
357382
}

src/AutoConnectOTA.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99
* binary file.
1010
* @file AutoConnectOTA.h
1111
12-
* @version 1.2.3
13-
* @date 2021-01-23
12+
* @version 1.3.0
13+
* @date 2021-03-29
1414
* @copyright MIT license.
1515
*/
1616

1717
#ifndef _AUTOCONNECTOTA_H_
1818
#define _AUTOCONNECTOTA_H_
1919

20+
#include <functional>
2021
#include <memory>
2122
#include "AutoConnect.h"
23+
#include "AutoConnectTypes.h"
2224
#include "AutoConnectUpload.h"
2325

2426
#include <FS.h>
@@ -34,32 +36,26 @@ typedef fs::SPIFFSFS SPIFFST;
3436

3537
class AutoConnectOTA : public AutoConnectUploadHandler {
3638
public:
37-
// Updating process status
38-
typedef enum {
39-
OTA_IDLE, /**< Update process has not started */
40-
OTA_START, /**< Update process has started */
41-
OTA_PROGRESS, /**< Update process in progress */
42-
OTA_SUCCESS, /**< A binary updater has uploaded fine */
43-
OTA_RIP, /**< Ready for module restart */
44-
OTA_FAIL /**< Failed to save binary updater by Update class */
45-
} AC_OTAStatus_t;
39+
// Type declaration of callback function to notify OTA status change
40+
typedef std::function<bool(const AC_OTAStatus_t)> StatusChange_ft;
4641

4742
// The treating destination of OTA transferred data
4843
typedef enum {
4944
OTA_DEST_FILE, /**< To be upload the file */
5045
OTA_DEST_FIRM /**< To update the firmware */
5146
} AC_OTADest_t;
5247

53-
AutoConnectOTA() : _dest(OTA_DEST_FIRM), _status(OTA_IDLE), _tickerPort(-1), _tickerOn(LOW) {};
48+
AutoConnectOTA() : _dest(OTA_DEST_FIRM), _status(AC_OTA_IDLE), _tickerPort(-1), _tickerOn(LOW) {};
5449
~AutoConnectOTA();
5550
void attach(AutoConnect& portal); /**< Attach itself to AutoConnect */
5651
void authentication(const AC_AUTH_t auth); /**< Set certain page authentication */
5752
String error(void) const { return _err; } /**< Returns current error string */
5853
void menu(const bool post) { _auxUpdate->menu(post); } /**< Enabel or disable arranging a created AutoConnectOTA page in the menu. */
59-
void reset(void) { _status = OTA_IDLE; } /**< Reset the status */
54+
void reset(void) { _status = AC_OTA_IDLE; } /**< Reset the status */
6055
AC_OTAStatus_t status(void) const { return _status; } /**< Return a current error status of the Update class */
6156
AC_OTADest_t dest(void) const { return _dest; } /**< Return a current uploading destination */
6257
void setTicker(int8_t pin, uint8_t on) { _tickerPort = pin, _tickerOn = on; } /**< Set ticker LED port */
58+
void onStatusChange(StatusChange_ft fn) { _onStatusChange = fn; } /**< Register a status change notification callback function */
6359

6460
protected:
6561
template <typename T, size_t N> constexpr size_t lengthOf(T (&)[N]) noexcept {
@@ -74,6 +70,8 @@ class AutoConnectOTA : public AutoConnectUploadHandler {
7470
std::unique_ptr<AutoConnectAux> _auxUpdate; /**< An update operation page */
7571
std::unique_ptr<AutoConnectAux> _auxResult; /**< An update result page */
7672

73+
StatusChange_ft _onStatusChange; /**< A status change notification callback function */
74+
7775
private:
7876
void _setError(void);
7977
void _setError(const char* err);

src/AutoConnectTypes.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* AutoConnect quoted type declarations.
33
* @file AutoConnectTypes.h
44
5-
* @version 1.2.0
6-
* @date 2020-04-17
5+
* @version 1.3.0
6+
* @date 2021-03-29
77
* @copyright MIT license.
88
*/
99

@@ -62,4 +62,14 @@ typedef enum AC_AUTH {
6262
AC_AUTH_BASIC
6363
} AC_AUTH_t;
6464

65+
/**< OTA Updating process status */
66+
typedef enum {
67+
AC_OTA_IDLE, /**< Update process has not started */
68+
AC_OTA_START, /**< Update process has started */
69+
AC_OTA_PROGRESS, /**< Update process in progress */
70+
AC_OTA_SUCCESS, /**< A binary updater has uploaded fine */
71+
AC_OTA_RIP, /**< Ready for module restart */
72+
AC_OTA_FAIL /**< Failed to save binary updater by Update class */
73+
} AC_OTAStatus_t;
74+
6575
#endif // !_AUTOCONNECTTYPES_H_

0 commit comments

Comments
 (0)