Files
bolar_2/main/ota/ota_service.h
2026-11-27 16:38:11 +08:00

109 lines
3.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#pragma once
#include "ota/ota_response_parser.h"
#include "ports/application_observer.h"
#include "ports/device_platform.h"
#include "ports/event_publisher.h"
#include "ports/ota_service.h"
#include "ports/realtime_protocol.h"
#include "protocol/realtime_config.h"
#include <array>
#include <atomic>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <freertos/task.h>
namespace borla {
struct OtaDiagnostics {
OtaManifest manifest{};
OtaParseStatus last_parse_status = OtaParseStatus::kOk;
uint32_t check_attempts = 0;
uint32_t downloaded_bytes = 0;
uint32_t last_error = 0;
bool checking = false;
bool upgrading = false;
};
class OtaService final
: public IOtaService,
public IApplicationObserver {
public:
bool Initialize(
const DeviceIdentity& identity,
IEventPublisher& publisher);
// Optional collaborators. Both are wired by CompositionRoot; the service
// degrades to "check + install only" when either is absent.
void SetRealtimeConfigStore(IRealtimeConfigStore* store)
{
config_store_ = store;
}
void SetRealtimeProtocol(IRealtimeProtocol* realtime)
{
realtime_ = realtime;
}
void StartUpgrade(uint32_t request_id) override;
void OnApplicationEvent(
const AppEvent& event,
const RuntimeState& state) override;
bool ConfirmCurrentImage();
void RejectCurrentImageAndRollback();
OtaDiagnostics GetDiagnostics() const;
private:
enum class TaskMode : uint8_t {
kCheck,
kUpgrade,
};
static constexpr uint32_t kTaskStackDepth = 12288;
// 版本检查是设备上的第二条 TLS:它与实时通道(websocket)的 TLS 都由
// kNetworkConnected 触发,两条同时握手会把内部 DMA 可用的 RAM 抢空 ——
// WiFi 驱动随即分配不到 RX 缓冲("wifi:mem fail" → ADDBA RX DELBA →
// 收包瘫痪 → "WebSocket read failed"),实测 internal free 掉到 863 B。
// 因此检查前先等实时通道把 TLS 握完;超过 kCheckStaggerMaxMs 就照常进行,
// 避免实时通道一直连不上时版本检查被永久推迟。
static constexpr uint32_t kCheckStaggerMaxMs = 8000;
static constexpr uint32_t kCheckStaggerStepMs = 100;
static void TaskEntry(void* context);
// Long-lived worker: the static task control block and stack cannot be
// recycled for a second task while the first is still terminating, so the
// worker parks on request_ instead of deleting itself.
[[noreturn]] void Run();
void RunOnce();
bool CheckVersion();
bool PerformUpgrade();
bool DownloadToPartition();
void PublishFailure(uint32_t error);
void ApplyEndpoint(const OtaEndpointConfig& endpoint);
void AnnounceUpgrade(const OtaManifest& manifest);
DeviceIdentity identity_{};
IEventPublisher* publisher_ = nullptr;
IRealtimeConfigStore* config_store_ = nullptr;
IRealtimeProtocol* realtime_ = nullptr;
OtaResponseParser parser_;
OtaManifest manifest_{};
OtaDiagnostics diagnostics_{};
mutable portMUX_TYPE lock_ = portMUX_INITIALIZER_UNLOCKED;
std::atomic<TaskMode> task_mode_{TaskMode::kCheck};
// 由 OnApplicationEvent 刷新,RunOnce 用它错开实时通道的 TLS 握手。
std::atomic<bool> realtime_open_{false};
std::atomic<bool> check_started_{false};
std::atomic<bool> busy_{false};
std::atomic<bool> upgrade_requested_{false};
std::atomic<uint32_t> request_id_{0};
SemaphoreHandle_t request_ = nullptr;
StaticSemaphore_t request_storage_{};
TaskHandle_t task_ = nullptr;
StaticTask_t task_control_{};
std::array<StackType_t, kTaskStackDepth> task_stack_{};
};
} // namespace borla