ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
ota_backend.h
Go to the documentation of this file.
1#pragma once
2
6
7#ifdef USE_OTA_STATE_LISTENER
8#include <vector>
9#endif
10
11namespace esphome {
12namespace ota {
13
43
51
53 public:
54 virtual ~OTABackend() = default;
55 virtual OTAResponseTypes begin(size_t image_size) = 0;
56 virtual void set_update_md5(const char *md5) = 0;
57 virtual OTAResponseTypes write(uint8_t *data, size_t len) = 0;
58 virtual OTAResponseTypes end() = 0;
59 virtual void abort() = 0;
60 virtual bool supports_compression() = 0;
61};
62
69 public:
70 virtual ~OTAStateListener() = default;
71 virtual void on_ota_state(OTAState state, float progress, uint8_t error) = 0;
72};
73
74class OTAComponent : public Component {
75#ifdef USE_OTA_STATE_LISTENER
76 public:
77 void add_state_listener(OTAStateListener *listener) { this->state_listeners_.push_back(listener); }
78
79 protected:
80 void notify_state_(OTAState state, float progress, uint8_t error);
81
87 void notify_state_deferred_(OTAState state, float progress, uint8_t error) {
88 this->defer([this, state, progress, error]() { this->notify_state_(state, progress, error); });
89 }
90
91 std::vector<OTAStateListener *> state_listeners_;
92#endif
93};
94
95#ifdef USE_OTA_STATE_LISTENER
96
102 public:
103 virtual ~OTAGlobalStateListener() = default;
104 virtual void on_ota_global_state(OTAState state, float progress, uint8_t error, OTAComponent *component) = 0;
105};
106
113 public:
114 void add_global_state_listener(OTAGlobalStateListener *listener) { this->global_listeners_.push_back(listener); }
115
116 void notify_ota_state(OTAState state, float progress, uint8_t error, OTAComponent *component) {
117 for (auto *listener : this->global_listeners_) {
118 listener->on_ota_global_state(state, progress, error, component);
119 }
120 }
121
122 protected:
123 std::vector<OTAGlobalStateListener *> global_listeners_;
124};
125
127
128// OTA implementations should use:
129// - notify_state_() when already in main loop (e.g., esphome OTA)
130// - notify_state_deferred_() when in separate task (e.g., web_server OTA)
131// This ensures proper listener execution in all contexts.
132#endif
133std::unique_ptr<ota::OTABackend> make_ota_backend();
134
135} // namespace ota
136} // namespace esphome
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
virtual OTAResponseTypes end()=0
virtual OTAResponseTypes begin(size_t image_size)=0
virtual ~OTABackend()=default
virtual void abort()=0
virtual bool supports_compression()=0
virtual OTAResponseTypes write(uint8_t *data, size_t len)=0
virtual void set_update_md5(const char *md5)=0
void notify_state_deferred_(OTAState state, float progress, uint8_t error)
Notify state with deferral to main loop (for thread safety).
Definition ota_backend.h:87
void add_state_listener(OTAStateListener *listener)
Definition ota_backend.h:77
std::vector< OTAStateListener * > state_listeners_
Definition ota_backend.h:91
void notify_state_(OTAState state, float progress, uint8_t error)
Global callback that aggregates OTA state from all OTA components.
std::vector< OTAGlobalStateListener * > global_listeners_
void notify_ota_state(OTAState state, float progress, uint8_t error, OTAComponent *component)
void add_global_state_listener(OTAGlobalStateListener *listener)
Listener interface for global OTA state changes (includes OTA component pointer).
virtual void on_ota_global_state(OTAState state, float progress, uint8_t error, OTAComponent *component)=0
virtual ~OTAGlobalStateListener()=default
Listener interface for OTA state changes.
Definition ota_backend.h:68
virtual ~OTAStateListener()=default
virtual void on_ota_state(OTAState state, float progress, uint8_t error)=0
const Component * component
Definition component.cpp:37
bool state
Definition fan.h:0
OTAGlobalCallback * get_global_ota_callback()
std::unique_ptr< ota::OTABackend > make_ota_backend()
@ OTA_RESPONSE_ERROR_MD5_MISMATCH
Definition ota_backend.h:39
@ OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE
Definition ota_backend.h:37
@ OTA_RESPONSE_UPDATE_PREPARE_OK
Definition ota_backend.h:21
@ OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG
Definition ota_backend.h:35
@ OTA_RESPONSE_SUPPORTS_COMPRESSION
Definition ota_backend.h:25
@ OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG
Definition ota_backend.h:34
@ OTA_RESPONSE_BIN_MD5_OK
Definition ota_backend.h:22
@ OTA_RESPONSE_UPDATE_END_OK
Definition ota_backend.h:24
@ OTA_RESPONSE_RECEIVE_OK
Definition ota_backend.h:23
@ OTA_RESPONSE_CHUNK_OK
Definition ota_backend.h:26
@ OTA_RESPONSE_ERROR_WRITING_FLASH
Definition ota_backend.h:31
@ OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE
Definition ota_backend.h:36
@ OTA_RESPONSE_ERROR_UPDATE_END
Definition ota_backend.h:32
@ OTA_RESPONSE_ERROR_AUTH_INVALID
Definition ota_backend.h:30
@ OTA_RESPONSE_ERROR_RP2040_NOT_ENOUGH_SPACE
Definition ota_backend.h:40
@ OTA_RESPONSE_ERROR_UNKNOWN
Definition ota_backend.h:41
@ OTA_RESPONSE_REQUEST_SHA256_AUTH
Definition ota_backend.h:17
@ OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION
Definition ota_backend.h:38
@ OTA_RESPONSE_ERROR_MAGIC
Definition ota_backend.h:28
@ OTA_RESPONSE_ERROR_UPDATE_PREPARE
Definition ota_backend.h:29
@ OTA_RESPONSE_HEADER_OK
Definition ota_backend.h:19
@ OTA_RESPONSE_REQUEST_AUTH
Definition ota_backend.h:16
@ OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING
Definition ota_backend.h:33
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:533