Skip to content

Commit 83a272c

Browse files
committed
Merge feat/multicore-task-architecture into refactor/adapter-command-snapshot-migration
Keep the typed-command MQTT handling and take over the deferred OtaUpdater::requestUpdate() for the firmware INSTALL command from #207. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012JY8RZscs5W8tLQMuZB95K
2 parents 130c11b + 74a3364 commit 83a272c

16 files changed

Lines changed: 753 additions & 143 deletions

‎docs/ota-updates.de.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ Die Firmware wird bei jedem GitHub-Release automatisch gebaut und veröffentlich
5050
3. Klicken, um die neueste Firmware direkt von GitHub herunterzuladen und zu flashen
5151
4. Der Fortschritt wird in der UI angezeigt; das Gerät startet nach Abschluss neu
5252

53+
Der Download läuft in kleinen Schritten in der Hauptschleife, Pumpen, Regeln und Fühler arbeiten
54+
während der Übertragung weiter. Ein Download wird abgebrochen (und kann wiederholt werden), wenn
55+
30 Sekunden lang keine Daten ankommen oder er insgesamt länger als 10 Minuten dauert.
56+
5357
### Methode 3: PlatformIO Seriell-Upload
5458

5559
Für die Entwicklung:

‎docs/ota-updates.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ Firmware is built and published automatically on each GitHub release:
5050
3. Click to download and flash the latest firmware directly from GitHub
5151
4. Progress is shown in the UI; device reboots on completion
5252

53+
The download runs in small steps inside the main loop, so pumps, rules and sensors keep working
54+
while the firmware is transferred. A download is aborted (and can be retried) if no data arrives
55+
for 30 seconds or if it takes longer than 10 minutes in total.
56+
5357
### Method 3: PlatformIO Serial Upload
5458

5559
For development:

‎src/MqttPublisher.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,8 +1055,10 @@ void MqttPublisher::handleMqttMessage(const char *topic, const char *payload, si
10551055
return;
10561056
}
10571057

1058-
LOG_INFO("MQTT: Firmware update triggered from Home Assistant\n");
1059-
OtaUpdater::startUpdate();
1058+
LOG_INFO("MQTT: Firmware update requested from Home Assistant\n");
1059+
// Only request it here: the download is started and streamed step by
1060+
// step by OtaUpdater::loop(), so this handler never blocks the loop task.
1061+
OtaUpdater::requestUpdate();
10601062
}
10611063

10621064
} // namespace PoolController

‎src/OtaDownloadGuard.hpp‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2018-2026 Smart Swimming Pool, Stephan Strittmatter
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
/**
6+
* @file OtaDownloadGuard.hpp
7+
* @brief Stall and overall timeout detection for the OTA download loop.
8+
*
9+
* The download loop feeds the task watchdog while it waits for data, so the
10+
* watchdog cannot detect a TCP connection that stalls without being closed.
11+
* This guard aborts such a download after a period without data or after an
12+
* overall time limit. Header-only and hardware-independent for native tests;
13+
* unsigned arithmetic keeps it correct across millis() wrap-around.
14+
*/
15+
16+
#pragma once
17+
18+
#include <cstdint>
19+
20+
namespace PoolController {
21+
22+
class OtaDownloadGuard {
23+
public:
24+
enum class Status : uint8_t {
25+
OK, ///< Download may continue
26+
STALLED, ///< No data received for longer than the stall timeout
27+
TIMED_OUT, ///< Overall download time exceeded
28+
};
29+
30+
OtaDownloadGuard(uint32_t startMs, uint32_t stallTimeoutMs, uint32_t totalTimeoutMs)
31+
: startMs_(startMs), lastDataMs_(startMs), stallTimeoutMs_(stallTimeoutMs), totalTimeoutMs_(totalTimeoutMs) {}
32+
33+
/// Call whenever bytes were received.
34+
void onData(uint32_t nowMs) { lastDataMs_ = nowMs; }
35+
36+
/// Check whether the download has to be aborted.
37+
Status check(uint32_t nowMs) const {
38+
if ((nowMs - startMs_) >= totalTimeoutMs_) {
39+
return Status::TIMED_OUT;
40+
}
41+
if ((nowMs - lastDataMs_) >= stallTimeoutMs_) {
42+
return Status::STALLED;
43+
}
44+
return Status::OK;
45+
}
46+
47+
private:
48+
uint32_t startMs_;
49+
uint32_t lastDataMs_;
50+
uint32_t stallTimeoutMs_;
51+
uint32_t totalTimeoutMs_;
52+
};
53+
54+
} // namespace PoolController

‎src/OtaDownloadSession.hpp‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright (c) 2018-2026 Smart Swimming Pool, Stephan Strittmatter
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
/**
6+
* @file OtaDownloadSession.hpp
7+
* @brief Incremental OTA download: one bounded step per main-loop iteration.
8+
*
9+
* The firmware download used to run in a blocking loop, which paused rule
10+
* evaluation, sensor handling and the web portal for the whole transfer.
11+
* OtaUpdater now calls step() once per loop() iteration. A step never waits
12+
* for data and moves at most one buffer of bytes from the stream to the
13+
* flash sink, so the control loop keeps running during an update.
14+
*
15+
* Templated on the stream (available()/read(buf, len)) and the sink (write()),
16+
* so it is hardware-independent and covered by native unit tests.
17+
*/
18+
19+
#pragma once
20+
21+
#include <cstddef>
22+
#include <cstdint>
23+
24+
#include "OtaDownloadGuard.hpp"
25+
26+
namespace PoolController {
27+
28+
class OtaDownloadSession {
29+
public:
30+
enum class Result : uint8_t {
31+
IN_PROGRESS, ///< Call step() again in the next loop iteration
32+
COMPLETE, ///< All bytes written
33+
STALLED, ///< No data for longer than the stall timeout
34+
TIMED_OUT, ///< Overall time limit exceeded
35+
WRITE_ERROR, ///< Sink accepted fewer bytes than given
36+
DISCONNECTED, ///< Connection closed before all bytes were received
37+
};
38+
39+
OtaDownloadSession(uint32_t startMs, size_t totalSize, uint32_t stallTimeoutMs, uint32_t totalTimeoutMs)
40+
: guard_(startMs, stallTimeoutMs, totalTimeoutMs), totalSize_(totalSize) {}
41+
42+
/**
43+
* @brief Perform one bounded download step.
44+
*
45+
* Reads at most @p bufferSize bytes that are already available and writes
46+
* them to @p sink. Returns immediately when no data is available.
47+
*/
48+
template <typename Stream, typename Sink>
49+
Result step(Stream &stream, Sink &sink, bool connected, uint32_t nowMs, uint8_t *buffer, size_t bufferSize) {
50+
if (written_ >= totalSize_) {
51+
return Result::COMPLETE;
52+
}
53+
switch (guard_.check(nowMs)) {
54+
case OtaDownloadGuard::Status::STALLED:
55+
return Result::STALLED;
56+
case OtaDownloadGuard::Status::TIMED_OUT:
57+
return Result::TIMED_OUT;
58+
case OtaDownloadGuard::Status::OK:
59+
break;
60+
}
61+
62+
size_t available = static_cast<size_t>(stream.available());
63+
if (available == 0) {
64+
return connected ? Result::IN_PROGRESS : Result::DISCONNECTED;
65+
}
66+
size_t toRead = available;
67+
if (toRead > bufferSize) {
68+
toRead = bufferSize;
69+
}
70+
if (toRead > totalSize_ - written_) {
71+
toRead = totalSize_ - written_;
72+
}
73+
// Client::read(buf, len) returns what is buffered right now; readBytes()
74+
// would wait up to the stream timeout when fewer bytes arrive than
75+
// available() reported, stalling the control loop.
76+
const int got = stream.read(buffer, toRead);
77+
if (got <= 0) {
78+
return Result::IN_PROGRESS;
79+
}
80+
const size_t read = static_cast<size_t>(got);
81+
guard_.onData(nowMs);
82+
83+
if (sink.write(buffer, read) != read) {
84+
return Result::WRITE_ERROR;
85+
}
86+
written_ += read;
87+
return written_ >= totalSize_ ? Result::COMPLETE : Result::IN_PROGRESS;
88+
}
89+
90+
size_t bytesWritten() const { return written_; }
91+
size_t totalSize() const { return totalSize_; }
92+
int progressPercent() const {
93+
return totalSize_ == 0 ? 0 : static_cast<int>((static_cast<uint64_t>(written_) * 100U) / totalSize_);
94+
}
95+
96+
private:
97+
OtaDownloadGuard guard_;
98+
size_t totalSize_;
99+
size_t written_ = 0;
100+
};
101+
102+
} // namespace PoolController

0 commit comments

Comments
 (0)