89 uint32_t last_progress = 0;
90 uint32_t update_start_time =
millis();
92 std::unique_ptr<char[]> md5_receive_str(
new char[33]);
98 ESP_LOGD(TAG,
"MD5 expected: %s", this->
md5_expected_.c_str());
101 if (url_with_auth.empty()) {
104 ESP_LOGVV(TAG,
"url_with_auth: %s", url_with_auth.c_str());
105 ESP_LOGI(TAG,
"Connecting to: %s", this->
url_.c_str());
107 auto container = this->
parent_->get(url_with_auth);
109 if (container ==
nullptr || container->status_code !=
HTTP_STATUS_OK) {
115 ESP_LOGV(TAG,
"MD5Digest initialized");
117 ESP_LOGV(TAG,
"OTA backend begin");
119 auto error_code = backend->begin(container->content_length);
121 ESP_LOGW(TAG,
"backend->begin error: %d", error_code);
122 this->
cleanup_(std::move(backend), container);
126 while (container->get_bytes_read() < container->content_length) {
129 ESP_LOGVV(TAG,
"bytes_read_ = %u, body_length_ = %u, bufsize = %i", container->get_bytes_read(),
130 container->content_length, bufsize);
137 ESP_LOGE(TAG,
"Stream closed");
138 this->
cleanup_(std::move(backend), container);
142 md5_receive.
add(buf, bufsize);
146 error_code = backend->write(buf, bufsize);
150 ESP_LOGE(TAG,
"Error code (%02X) writing binary data to flash at offset %d and size %d", error_code,
151 container->get_bytes_read() - bufsize, container->content_length);
152 this->
cleanup_(std::move(backend), container);
158 if ((now - last_progress > 1000) or (container->get_bytes_read() == container->content_length)) {
160 float percentage = container->get_bytes_read() * 100.0f / container->content_length;
161 ESP_LOGD(TAG,
"Progress: %0.1f%%", percentage);
162#ifdef USE_OTA_STATE_CALLBACK
168 ESP_LOGI(TAG,
"Done in %.0f seconds",
float(
millis() - update_start_time) / 1000);
172 md5_receive.
get_hex(md5_receive_str.get());
174 if (strncmp(this->
md5_computed_.c_str(), this->md5_expected_.c_str(), MD5_SIZE) != 0) {
175 ESP_LOGE(TAG,
"MD5 computed: %s - Aborting due to MD5 mismatch", this->
md5_computed_.c_str());
176 this->
cleanup_(std::move(backend), container);
179 backend->set_update_md5(md5_receive_str.get());
189 error_code = backend->end();
191 ESP_LOGW(TAG,
"Error ending update! error_code: %d", error_code);
192 this->
cleanup_(std::move(backend), container);
196 ESP_LOGI(TAG,
"Update complete");