ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
ota_backend.cpp
Go to the documentation of this file.
1#include "ota_backend.h"
2
3namespace esphome::ota {
4
5#ifdef USE_OTA_STATE_LISTENER
6OTAGlobalCallback *global_ota_callback{nullptr}; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
7
9 if (global_ota_callback == nullptr) {
10 global_ota_callback = new OTAGlobalCallback(); // NOLINT(cppcoreguidelines-owning-memory)
11 }
13}
14
15void OTAComponent::notify_state_deferred_(OTAState state, float progress, uint8_t error) {
16 // Pack state, error, and progress into a single uint32_t so the lambda
17 // captures only [this, packed] (8 bytes) — fits in std::function SBO.
18 // Layout: [state:8][error:8][progress_fixed:16] where progress is 0–10000 (0.01% resolution)
19 static_assert(OTA_ERROR <= 0xFF, "OTAState must fit in 8 bits for packing");
20 uint32_t packed = (static_cast<uint32_t>(state) << 24) | (static_cast<uint32_t>(error) << 16) |
21 static_cast<uint16_t>(progress * 100.0f);
22 this->defer([this, packed]() {
23 this->notify_state_(static_cast<OTAState>(packed >> 24), static_cast<float>(packed & 0xFFFF) / 100.0f,
24 static_cast<uint8_t>(packed >> 16));
25 });
26}
27
28void OTAComponent::notify_state_(OTAState state, float progress, uint8_t error) {
29 for (auto *listener : this->state_listeners_) {
30 listener->on_ota_state(state, progress, error);
31 }
32 get_global_ota_callback()->notify_ota_state(state, progress, error, this);
33}
34#endif
35
36} // namespace esphome::ota
ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0") void defer(const std voi defer)(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call.
Definition component.h:560
void notify_state_deferred_(OTAState state, float progress, uint8_t error)
Notify state with deferral to main loop (for thread safety).
std::vector< OTAStateListener * > state_listeners_
Definition ota_backend.h:78
void notify_state_(OTAState state, float progress, uint8_t error)
Global callback that aggregates OTA state from all OTA components.
Definition ota_backend.h:99
void notify_ota_state(OTAState state, float progress, uint8_t error, OTAComponent *component)
bool state
Definition fan.h:2
OTAGlobalCallback * get_global_ota_callback()
OTAGlobalCallback * global_ota_callback
static void uint32_t