20 this->
request_ = make_unique<Request>();
46 ESP_LOGE(TAG,
"URL not set; cannot start update");
50 ESP_LOGI(TAG,
"Starting update");
51#ifdef USE_OTA_STATE_LISTENER
55 auto ota_status = this->
do_ota_();
59#ifdef USE_OTA_STATE_LISTENER
67#ifdef USE_OTA_STATE_LISTENER
70 this->
request_->md5_computed.clear();
71 this->
request_->md5_expected.clear();
79 ESP_LOGV(TAG,
"Aborting OTA backend");
82 ESP_LOGV(TAG,
"Aborting HTTP connection");
91 char md5_receive_str[33];
93 if (this->
request_->md5_expected.empty() && !this->http_get_md5_()) {
97 ESP_LOGD(TAG,
"MD5 expected: %s", this->
request_->md5_expected.c_str());
100 if (url_with_auth.empty()) {
103 ESP_LOGVV(TAG,
"url_with_auth: %s", url_with_auth.c_str());
104 ESP_LOGI(TAG,
"Connecting to: %s", this->
request_->url.c_str());
106 auto container = this->
parent_->get(url_with_auth);
108 if (container ==
nullptr || container->status_code !=
HTTP_STATUS_OK) {
109 if (container !=
nullptr)
116 ESP_LOGV(TAG,
"MD5Digest initialized, OTA backend begin");
118 auto error_code = backend->begin(container->content_length);
120 ESP_LOGW(TAG,
"backend->begin error: %d", error_code);
122 this->
cleanup_(std::move(backend), container,
false);
131 while (container->get_bytes_read() < container->content_length) {
134 ESP_LOGVV(TAG,
"bytes_read_ = %u, body_length_ = %u, bufsize_or_error = %i", container->get_bytes_read(),
135 container->content_length, bufsize_or_error);
141 auto result =
http_read_loop_result(bufsize_or_error, last_data_time, read_timeout, container->is_read_complete());
152 ESP_LOGE(TAG,
"Timeout reading data");
154 ESP_LOGE(TAG,
"Error reading data: %d", bufsize_or_error);
156 this->
cleanup_(std::move(backend), container,
true);
163 md5_receive.
add(buf, bufsize_or_error);
166 error_code = backend->write(buf, bufsize_or_error);
170 ESP_LOGE(TAG,
"Error code (%02X) writing binary data to flash at offset %d and size %d", error_code,
171 container->get_bytes_read() - bufsize_or_error, container->content_length);
172 this->
cleanup_(std::move(backend), container,
true);
178 if ((now - last_progress > 1000) or (container->get_bytes_read() == container->content_length)) {
180 float percentage = container->get_bytes_read() * 100.0f / container->content_length;
181 ESP_LOGD(TAG,
"Progress: %0.1f%%", percentage);
182#ifdef USE_OTA_STATE_LISTENER
188 ESP_LOGI(TAG,
"Done in %.0f seconds",
float(
millis() - update_start_time) / 1000);
192 md5_receive.
get_hex(md5_receive_str);
193 this->
request_->md5_computed = md5_receive_str;
194 if (strncmp(this->
request_->md5_computed.c_str(), this->request_->md5_expected.c_str(), MD5_SIZE) != 0) {
195 ESP_LOGE(TAG,
"MD5 computed: %s - Aborting due to MD5 mismatch", this->
request_->md5_computed.c_str());
196 this->
cleanup_(std::move(backend), container,
true);
199 backend->set_update_md5(md5_receive_str);
209 error_code = backend->end();
211 ESP_LOGW(TAG,
"Error ending update! error_code: %d", error_code);
212 this->
cleanup_(std::move(backend), container,
true);
216 ESP_LOGI(TAG,
"Update complete");
222static std::string url_encode(
const std::string &str) {
224 result.reserve(str.size());
226 if (std::isalnum(
static_cast<unsigned char>(c)) || c ==
'-' || c ==
'_' || c ==
'.' || c ==
'~') {
245 if (this->
request_->username.empty() || this->request_->password.empty()) {
249 auto start_char = url.find(
"://");
250 if ((start_char == std::string::npos) || (start_char < 4)) {
251 ESP_LOGE(TAG,
"Incorrect URL prefix");
255 ESP_LOGD(TAG,
"Using basic HTTP authentication");
258 auto url_with_auth = url.substr(0, start_char) + this->
request_->username +
":" + this->
request_->password +
"@" +
259 url.substr(start_char);
260 return url_with_auth;
264 if (this->
request_->md5_url.empty()) {
269 if (url_with_auth.empty()) {
273 ESP_LOGVV(TAG,
"url_with_auth: %s", url_with_auth.c_str());
274 ESP_LOGI(TAG,
"Connecting to: %s", this->
request_->md5_url.c_str());
275 auto container = this->
parent_->get(url_with_auth);
276 if (container ==
nullptr) {
277 ESP_LOGE(TAG,
"Failed to connect to MD5 URL");
280 size_t length = container->content_length;
286 ESP_LOGE(TAG,
"MD5 file must be %u bytes; %u bytes reported by HTTP server. Aborting", MD5_SIZE,
length);
291 this->
request_->md5_expected.resize(MD5_SIZE);
292 auto result =
http_read_fully(container.get(), (uint8_t *) this->request_->md5_expected.data(), MD5_SIZE, MD5_SIZE,
293 this->parent_->get_timeout());
298 ESP_LOGE(TAG,
"Timeout reading MD5");
300 ESP_LOGE(TAG,
"Error reading MD5: %d", result.error_code);
308 if ((url.length() < 8) || !url.starts_with(
"http") || (url.find(
"://") == std::string::npos)) {
309 ESP_LOGE(TAG,
"URL is invalid and/or must be prefixed with 'http://' or 'https://'");
void feed_wdt()
Feed the task watchdog.
void get_hex(char *output)
Retrieve the hash as hex characters. Output buffer must hold get_size() * 2 + 1 bytes.
HttpRequestComponent * parent_
void set_url(const std::string &url)
void dump_config() override
void cleanup_(ota::OTABackendPtr backend, const std::shared_ptr< HttpContainer > &container, bool abort_backend)
static const uint16_t HTTP_RECV_BUFFER
void set_password(const std::string &password)
bool validate_url_(const std::string &url)
void set_username(const std::string &username)
std::string get_url_with_auth_(const std::string &url)
std::unique_ptr< Request > request_
void set_md5_url(const std::string &md5_url)
void calculate() override
Compute the digest, based on the provided data.
void add(const uint8_t *data, size_t len) override
Add bytes of data for the digest.
void init() override
Initialize a new MD5 digest computation.
void notify_state_(OTAState state, float progress, uint8_t error)
@ TIMEOUT
Timeout waiting for data, caller should exit loop.
@ COMPLETE
All content has been read, caller should exit loop.
@ RETRY
No data yet, already delayed, caller should continue loop.
@ DATA
Data was read, process it.
HttpReadLoopResult http_read_loop_result(int bytes_read_or_error, uint32_t &last_data_time, uint32_t timeout_ms, bool is_read_complete)
Process a read result with timeout tracking and delay handling.
ESPHOME_LOG_TAG(TAG, "http_request")
HttpReadResult http_read_fully(HttpContainer *container, uint8_t *buffer, size_t total_size, size_t chunk_size, uint32_t timeout_ms)
Read data from HTTP container into buffer with timeout handling Handles feed_wdt, yield,...
@ TIMEOUT
Timeout waiting for data.
@ OK
Read completed successfully.
decltype(make_ota_backend()) OTABackendPtr
@ OTA_RESPONSE_ERROR_MD5_MISMATCH
std::unique_ptr< ArduinoLibreTinyOTABackend > make_ota_backend()
ESPHOME_ALWAYS_INLINE char format_hex_pretty_char(uint8_t v)
Convert a nibble (0-15) to uppercase hex char (used for pretty printing)
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
Allocated by the first setter, which only runs before a flash, and never freed, so a flash never lose...