2#ifdef USE_WEBSERVER_OTA
8#ifdef USE_CAPTIVE_PORTAL
15#elif defined(USE_ESP32) || defined(USE_LIBRETINY)
29static const char *
const TAG =
"web_server.ota";
31class OTARequestHandler :
public AsyncWebHandler {
33 OTARequestHandler(WebServerOTAComponent *parent) : parent_(parent) {}
34 void handleRequest(AsyncWebServerRequest *request)
override;
35 void handleUpload(AsyncWebServerRequest *request,
const PlatformString &filename,
size_t index, uint8_t *data,
36 size_t len,
bool final)
override;
37 bool canHandle(AsyncWebServerRequest *request)
const override {
39 bool is_ota_request = request->url() ==
"/update" && request->method() == HTTP_POST;
41#if defined(USE_WEBSERVER_OTA_DISABLED) && defined(USE_CAPTIVE_PORTAL)
47#elif defined(USE_WEBSERVER_OTA_DISABLED)
52 return is_ota_request;
57 bool isRequestHandlerTrivial()
const override {
return false; }
60 void report_ota_progress_(AsyncWebServerRequest *request);
61 void schedule_ota_reboot_();
62 void ota_init_(
const char *filename);
64 uint32_t last_ota_progress_{0};
65 uint32_t ota_read_length_{0};
66 WebServerOTAComponent *parent_;
67 bool ota_success_{
false};
70 std::unique_ptr<ota::OTABackend> ota_backend_{
nullptr};
73void OTARequestHandler::report_ota_progress_(AsyncWebServerRequest *request) {
74 const uint32_t now =
millis();
75 if (now - this->last_ota_progress_ > 1000) {
76 float percentage = 0.0f;
77 if (request->contentLength() != 0) {
82 percentage = (this->ota_read_length_ * 100.0f) / request->contentLength();
83 ESP_LOGD(TAG,
"OTA in progress: %0.1f%%", percentage);
85 ESP_LOGD(TAG,
"OTA in progress: %" PRIu32
" bytes read", this->ota_read_length_);
87#ifdef USE_OTA_STATE_CALLBACK
91 this->last_ota_progress_ = now;
95void OTARequestHandler::schedule_ota_reboot_() {
96 ESP_LOGI(TAG,
"OTA update successful!");
98 ESP_LOGI(TAG,
"Performing OTA reboot now");
103void OTARequestHandler::ota_init_(
const char *filename) {
104 ESP_LOGI(TAG,
"OTA Update Start: %s", filename);
105 this->ota_read_length_ = 0;
106 this->ota_success_ =
false;
109void OTARequestHandler::handleUpload(AsyncWebServerRequest *request,
const PlatformString &filename,
size_t index,
110 uint8_t *data,
size_t len,
bool final) {
113 if (index == 0 && !this->ota_backend_) {
115 this->ota_init_(filename.c_str());
117#ifdef USE_OTA_STATE_CALLBACK
125 Update.runAsync(
true);
127#if defined(USE_ESP32) || defined(USE_LIBRETINY)
128 if (Update.isRunning()) {
135 if (!this->ota_backend_) {
136 ESP_LOGE(TAG,
"Failed to create OTA backend");
137#ifdef USE_OTA_STATE_CALLBACK
147 error_code = this->ota_backend_->begin(0);
149 ESP_LOGE(TAG,
"OTA begin failed: %d", error_code);
150 this->ota_backend_.reset();
151#ifdef USE_OTA_STATE_CALLBACK
158 if (!this->ota_backend_) {
164 error_code = this->ota_backend_->write(data,
len);
166 ESP_LOGE(TAG,
"OTA write failed: %d", error_code);
167 this->ota_backend_->abort();
168 this->ota_backend_.reset();
169#ifdef USE_OTA_STATE_CALLBACK
174 this->ota_read_length_ +=
len;
175 this->report_ota_progress_(request);
180 ESP_LOGD(TAG,
"OTA final chunk: index=%zu, len=%zu, total_read=%" PRIu32
", contentLength=%zu", index,
len,
181 this->ota_read_length_, request->contentLength());
186 error_code = this->ota_backend_->end();
188 this->ota_success_ =
true;
189#ifdef USE_OTA_STATE_CALLBACK
193 this->schedule_ota_reboot_();
195 ESP_LOGE(TAG,
"OTA end failed: %d", error_code);
196#ifdef USE_OTA_STATE_CALLBACK
200 this->ota_backend_.reset();
204void OTARequestHandler::handleRequest(AsyncWebServerRequest *request) {
205 AsyncWebServerResponse *response;
208 static const char UPDATE_SUCCESS[]
PROGMEM =
"Update Successful!";
209 static const char UPDATE_FAILED[]
PROGMEM =
"Update Failed!";
210 static const char TEXT_PLAIN[]
PROGMEM =
"text/plain";
211 static const char CONNECTION_STR[]
PROGMEM =
"Connection";
212 static const char CLOSE_STR[]
PROGMEM =
"close";
213 const char *msg = this->ota_success_ ? UPDATE_SUCCESS : UPDATE_FAILED;
214 response = request->beginResponse_P(200, TEXT_PLAIN, msg);
215 response->addHeader(CONNECTION_STR, CLOSE_STR);
217 const char *msg = this->ota_success_ ?
"Update Successful!" :
"Update Failed!";
218 response = request->beginResponse(200,
"text/plain", msg);
219 response->addHeader(
"Connection",
"close");
221 request->send(response);
227 if (base ==
nullptr) {
228 ESP_LOGE(TAG,
"WebServerBase not found");
235#ifdef USE_OTA_STATE_CALLBACK
virtual void mark_failed()
Mark this component as failed.
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void call_deferred(ota::OTAState state, float progress, uint8_t error)
Call callbacks with deferral to main loop (for thread safety).
StateCallbackManager state_callback_
void dump_config() override
friend class OTARequestHandler
void add_handler(AsyncWebHandler *handler)
CaptivePortal * global_captive_portal
void register_ota_platform(OTAComponent *ota_caller)
std::unique_ptr< ota::OTABackend > make_ota_backend()
@ OTA_RESPONSE_ERROR_UNKNOWN
WebServerBase * global_web_server_base
const uint8_t INDEX_GZ[] PROGMEM
Providing packet encoding functions for exchanging data with a remote host.
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
std::string PlatformString