2#ifdef USE_WEBSERVER_OTA
8#ifdef USE_CAPTIVE_PORTAL
13#if defined(USE_LIBRETINY)
26static const char *
const TAG =
"web_server.ota";
28class OTARequestHandler :
public AsyncWebHandler {
30 OTARequestHandler(WebServerOTAComponent *parent) : parent_(parent) {}
31 void handleRequest(AsyncWebServerRequest *request)
override;
32 void handleUpload(AsyncWebServerRequest *request,
const PlatformString &filename,
size_t index, uint8_t *data,
33 size_t len,
bool final)
override;
34 bool canHandle(AsyncWebServerRequest *request)
const override {
36 bool is_ota_request = request->url() ==
"/update" && request->method() == HTTP_POST;
38#if defined(USE_WEBSERVER_OTA_DISABLED) && defined(USE_CAPTIVE_PORTAL)
44#elif defined(USE_WEBSERVER_OTA_DISABLED)
49 return is_ota_request;
54 bool isRequestHandlerTrivial()
const override {
return false; }
57 void report_ota_progress_(AsyncWebServerRequest *request);
58 void schedule_ota_reboot_();
59 void ota_init_(
const char *filename);
61 uint32_t last_ota_progress_{0};
62 uint32_t ota_read_length_{0};
63 WebServerOTAComponent *parent_;
64 bool ota_success_{
false};
67 std::unique_ptr<ota::OTABackend> ota_backend_{
nullptr};
70void OTARequestHandler::report_ota_progress_(AsyncWebServerRequest *request) {
71 const uint32_t now =
millis();
72 if (now - this->last_ota_progress_ > 1000) {
73 float percentage = 0.0f;
74 if (request->contentLength() != 0) {
79 percentage = (this->ota_read_length_ * 100.0f) / request->contentLength();
80 ESP_LOGD(TAG,
"OTA in progress: %0.1f%%", percentage);
82 ESP_LOGD(TAG,
"OTA in progress: %" PRIu32
" bytes read", this->ota_read_length_);
84#ifdef USE_OTA_STATE_LISTENER
88 this->last_ota_progress_ = now;
92void OTARequestHandler::schedule_ota_reboot_() {
93 ESP_LOGI(TAG,
"OTA update successful!");
95 ESP_LOGI(TAG,
"Performing OTA reboot now");
100void OTARequestHandler::ota_init_(
const char *filename) {
101 ESP_LOGI(TAG,
"OTA Update Start: %s", filename);
102 this->ota_read_length_ = 0;
103 this->ota_success_ =
false;
106void OTARequestHandler::handleUpload(AsyncWebServerRequest *request,
const PlatformString &filename,
size_t index,
107 uint8_t *data,
size_t len,
bool final) {
110 if (index == 0 && !this->ota_backend_) {
112 this->ota_init_(filename.c_str());
114#ifdef USE_OTA_STATE_LISTENER
121#if defined(USE_LIBRETINY)
122 if (Update.isRunning()) {
129 if (!this->ota_backend_) {
130 ESP_LOGE(TAG,
"Failed to create OTA backend");
131#ifdef USE_OTA_STATE_LISTENER
141 error_code = this->ota_backend_->begin(0);
143 ESP_LOGE(TAG,
"OTA begin failed: %d", error_code);
144 this->ota_backend_.reset();
145#ifdef USE_OTA_STATE_LISTENER
152 if (!this->ota_backend_) {
158 error_code = this->ota_backend_->write(data,
len);
160 ESP_LOGE(TAG,
"OTA write failed: %d", error_code);
161 this->ota_backend_->abort();
162 this->ota_backend_.reset();
163#ifdef USE_OTA_STATE_LISTENER
168 this->ota_read_length_ +=
len;
169 this->report_ota_progress_(request);
174 ESP_LOGD(TAG,
"OTA final chunk: index=%zu, len=%zu, total_read=%" PRIu32
", contentLength=%zu", index,
len,
175 this->ota_read_length_, request->contentLength());
180 error_code = this->ota_backend_->end();
182 this->ota_success_ =
true;
183#ifdef USE_OTA_STATE_LISTENER
187 this->schedule_ota_reboot_();
189 ESP_LOGE(TAG,
"OTA end failed: %d", error_code);
190#ifdef USE_OTA_STATE_LISTENER
194 this->ota_backend_.reset();
198void OTARequestHandler::handleRequest(AsyncWebServerRequest *request) {
199 AsyncWebServerResponse *response;
202 static const char UPDATE_SUCCESS[]
PROGMEM =
"Update Successful!";
203 static const char UPDATE_FAILED[]
PROGMEM =
"Update Failed!";
204 static const char TEXT_PLAIN[]
PROGMEM =
"text/plain";
205 static const char CONNECTION_STR[]
PROGMEM =
"Connection";
206 static const char CLOSE_STR[]
PROGMEM =
"close";
207 const char *msg = this->ota_success_ ? UPDATE_SUCCESS : UPDATE_FAILED;
208 response = request->beginResponse_P(200, TEXT_PLAIN, msg);
209 response->addHeader(CONNECTION_STR, CLOSE_STR);
211 const char *msg = this->ota_success_ ?
"Update Successful!" :
"Update Failed!";
212 response = request->beginResponse(200,
"text/plain", msg);
213 response->addHeader(
"Connection",
"close");
215 request->send(response);
221 if (base ==
nullptr) {
222 ESP_LOGE(TAG,
"WebServerBase not found");
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 notify_state_deferred_(OTAState state, float progress, uint8_t error)
Notify state with deferral to main loop (for thread safety).
void dump_config() override
friend class OTARequestHandler
void add_handler(AsyncWebHandler *handler)
CaptivePortal * global_captive_portal
std::unique_ptr< ota::OTABackend > make_ota_backend()
@ OTA_RESPONSE_ERROR_UNKNOWN
WebServerBase * global_web_server_base
const 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