2#ifdef USE_WEBSERVER_OTA
7#ifdef USE_CAPTIVE_PORTAL
16 if (request->method() != HTTP_POST)
20 char url_buf[AsyncWebServerRequest::URL_BUF_SIZE];
21 bool is_ota_request = request->url_to(url_buf) ==
"/update";
23 bool is_ota_request = request->url() == ESPHOME_F(
"/update");
26#if defined(USE_WEBSERVER_OTA_DISABLED) && defined(USE_CAPTIVE_PORTAL)
32#elif defined(USE_WEBSERVER_OTA_DISABLED)
37 return is_ota_request;
44 float percentage = 0.0f;
45 if (request->contentLength() != 0) {
51 ESP_LOGD(TAG,
"OTA in progress: %0.1f%%", percentage);
53 ESP_LOGD(TAG,
"OTA in progress: %" PRIu32
" bytes read", this->
ota_read_length_);
55#ifdef USE_OTA_STATE_LISTENER
64 ESP_LOGI(TAG,
"OTA update successful!");
66 ESP_LOGI(TAG,
"Performing OTA reboot now");
72 ESP_LOGI(TAG,
"OTA Update Start: %s", filename);
78 uint8_t *data,
size_t len,
bool final) {
84 if (index == 0 &&
len > 0) {
86 if (this->ota_backend_) {
87 ESP_LOGW(TAG,
"OTA upload refused: another upload is in progress");
93 if (!this->ota_backend_) {
94 ESP_LOGE(TAG,
"Failed to create OTA backend");
95#ifdef USE_OTA_STATE_LISTENER
104 error_code = this->ota_backend_->begin(0);
107 ESP_LOGW(TAG,
"OTA upload refused: another update is in progress");
108 this->ota_backend_.reset();
116#ifdef USE_OTA_STATE_LISTENER
122 ESP_LOGE(TAG,
"OTA begin failed: %d", error_code);
123 this->ota_backend_.reset();
124#ifdef USE_OTA_STATE_LISTENER
131 this->ota_request_ = request;
132 request->onDisconnect([
this, request]() {
133 if (this->ota_request_ == request && this->ota_backend_) {
134 ESP_LOGW(TAG,
"OTA upload interrupted; aborting");
140 if (!this->ota_backend_ || request != this->ota_request_) {
146 error_code = this->ota_backend_->write(data,
len);
148 ESP_LOGE(TAG,
"OTA write failed: %d", error_code);
149 this->ota_backend_->abort();
150 this->ota_backend_.reset();
151 this->ota_request_ =
nullptr;
152#ifdef USE_OTA_STATE_LISTENER
163 ESP_LOGD(TAG,
"OTA final chunk: index=%zu, len=%zu, total_read=%" PRIu32
", contentLength=%zu", index,
len,
169 error_code = this->ota_backend_->end();
172#ifdef USE_OTA_STATE_LISTENER
178 ESP_LOGE(TAG,
"OTA end failed: %d", error_code);
180 this->ota_backend_->abort();
181#ifdef USE_OTA_STATE_LISTENER
185 this->ota_backend_.reset();
186 this->ota_request_ =
nullptr;
191 this->ota_backend_->abort();
192#ifdef USE_OTA_STATE_LISTENER
195 this->ota_backend_.reset();
196 this->ota_request_ =
nullptr;
200 this->refused_request_ = request;
201 request->onDisconnect([
this, request]() {
202 if (this->refused_request_ == request)
203 this->refused_request_ =
nullptr;
208 AsyncWebServerResponse *response;
210 const bool refused = request == this->refused_request_;
212 this->refused_request_ =
nullptr;
214 static const char UPDATE_SUCCESS[]
PROGMEM =
"Update Successful!";
215 static const char UPDATE_FAILED[]
PROGMEM =
"Update Failed!";
216 static const char UPDATE_BUSY[]
PROGMEM =
"Update Failed! Another update is in progress, retry later.";
217 static const char TEXT_PLAIN[]
PROGMEM =
"text/plain";
218 static const char CONNECTION_STR[]
PROGMEM =
"Connection";
219 static const char CLOSE_STR[]
PROGMEM =
"close";
220 const char *msg = refused ? UPDATE_BUSY : (this->
ota_success_ ? UPDATE_SUCCESS : UPDATE_FAILED);
221 response = request->beginResponse_P(200, TEXT_PLAIN, msg);
222 response->addHeader(CONNECTION_STR, CLOSE_STR);
224 const char *msg = refused ?
"Update Failed! Another update is in progress, retry later."
227 response = request->beginResponse(200,
"text/plain", msg);
228 response->addHeader(
"Connection",
"close");
230 request->send(response);
236 if (base ==
nullptr) {
237 ESP_LOGE(TAG,
"WebServerBase not found");
void mark_failed()
Mark this component as failed.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
void notify_state_deferred_(OTAState state, float progress, uint8_t error)
Notify state with deferral to main loop (for thread safety).
void dump_config() override
void handleRequest(AsyncWebServerRequest *request) override
void report_ota_progress_(AsyncWebServerRequest *request)
bool canHandle(AsyncWebServerRequest *request) const override
uint32_t last_ota_progress_
void ota_init_(const char *filename)
void refuse_(AsyncWebServerRequest *request)
Reply "busy" without touching the running upload.
uint32_t ota_read_length_
void abort_session_()
Abort and drop the backend, releasing the lock.
void schedule_ota_reboot_()
void handleUpload(AsyncWebServerRequest *request, const PlatformString &filename, size_t index, uint8_t *data, size_t len, bool final) override
void add_handler(AsyncWebHandler *handler)
CaptivePortal * global_captive_portal
@ OTA_RESPONSE_ERROR_UPDATE_BUSY
@ OTA_RESPONSE_ERROR_UNKNOWN
std::unique_ptr< ArduinoLibreTinyOTABackend > make_ota_backend()
WebServerBase * global_web_server_base
ESPHOME_LOG_TAG(TAG, "web_server.ota")
constexpr uint8_t INDEX_GZ[] PROGMEM
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
std::string PlatformString