88 uint32_t last_progress = 0;
89 uint32_t update_start_time =
millis();
91 std::unique_ptr<char[]> md5_receive_str(
new char[33]);
97 ESP_LOGD(TAG,
"MD5 expected: %s", this->
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->
url_.c_str());
106 auto container = this->
parent_->get(url_with_auth);
108 if (container ==
nullptr || container->status_code !=
HTTP_STATUS_OK) {
114 ESP_LOGV(TAG,
"MD5Digest initialized");
116 ESP_LOGV(TAG,
"OTA backend begin");
118 auto error_code = backend->begin(container->content_length);
120 ESP_LOGW(TAG,
"backend->begin error: %d", error_code);
121 this->
cleanup_(std::move(backend), container);
125 while (container->get_bytes_read() < container->content_length) {
128 ESP_LOGVV(TAG,
"bytes_read_ = %u, body_length_ = %u, bufsize = %i", container->get_bytes_read(),
129 container->content_length, bufsize);
138 ESP_LOGE(TAG,
"Stream closed with error");
139 this->
cleanup_(std::move(backend), container);
148 md5_receive.
add(buf, bufsize);
152 error_code = backend->write(buf, bufsize);
156 ESP_LOGE(TAG,
"Error code (%02X) writing binary data to flash at offset %d and size %d", error_code,
157 container->get_bytes_read() - bufsize, container->content_length);
158 this->
cleanup_(std::move(backend), container);
164 if ((now - last_progress > 1000) or (container->get_bytes_read() == container->content_length)) {
166 float percentage = container->get_bytes_read() * 100.0f / container->content_length;
167 ESP_LOGD(TAG,
"Progress: %0.1f%%", percentage);
168#ifdef USE_OTA_STATE_CALLBACK
174 ESP_LOGI(TAG,
"Done in %.0f seconds",
float(
millis() - update_start_time) / 1000);
178 md5_receive.
get_hex(md5_receive_str.get());
180 if (strncmp(this->
md5_computed_.c_str(), this->md5_expected_.c_str(), MD5_SIZE) != 0) {
181 ESP_LOGE(TAG,
"MD5 computed: %s - Aborting due to MD5 mismatch", this->
md5_computed_.c_str());
182 this->
cleanup_(std::move(backend), container);
185 backend->set_update_md5(md5_receive_str.get());
195 error_code = backend->end();
197 ESP_LOGW(TAG,
"Error ending update! error_code: %d", error_code);
198 this->
cleanup_(std::move(backend), container);
202 ESP_LOGI(TAG,
"Update complete");