|
| 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