ESPHome  2022.8.0
ota_backend_esp_idf.cpp
Go to the documentation of this file.
1 #include "esphome/core/defines.h"
2 #ifdef USE_ESP_IDF
3 
4 #include "ota_backend_esp_idf.h"
5 #include "ota_component.h"
6 #include <esp_ota_ops.h>
8 
9 namespace esphome {
10 namespace ota {
11 
13  this->partition_ = esp_ota_get_next_update_partition(nullptr);
14  if (this->partition_ == nullptr) {
16  }
17  esp_err_t err = esp_ota_begin(this->partition_, image_size, &this->update_handle_);
18  if (err != ESP_OK) {
19  esp_ota_abort(this->update_handle_);
20  this->update_handle_ = 0;
21  if (err == ESP_ERR_INVALID_SIZE) {
23  } else if (err == ESP_ERR_FLASH_OP_TIMEOUT || err == ESP_ERR_FLASH_OP_FAIL) {
25  }
27  }
28  this->md5_.init();
29  return OTA_RESPONSE_OK;
30 }
31 
32 void IDFOTABackend::set_update_md5(const char *expected_md5) { memcpy(this->expected_bin_md5_, expected_md5, 32); }
33 
34 OTAResponseTypes IDFOTABackend::write(uint8_t *data, size_t len) {
35  esp_err_t err = esp_ota_write(this->update_handle_, data, len);
36  this->md5_.add(data, len);
37  if (err != ESP_OK) {
38  if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
40  } else if (err == ESP_ERR_FLASH_OP_TIMEOUT || err == ESP_ERR_FLASH_OP_FAIL) {
42  }
44  }
45  return OTA_RESPONSE_OK;
46 }
47 
49  this->md5_.calculate();
50  if (!this->md5_.equals_hex(this->expected_bin_md5_)) {
51  this->abort();
53  }
54  esp_err_t err = esp_ota_end(this->update_handle_);
55  this->update_handle_ = 0;
56  if (err == ESP_OK) {
57  err = esp_ota_set_boot_partition(this->partition_);
58  if (err == ESP_OK) {
59  return OTA_RESPONSE_OK;
60  }
61  }
62  if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
64  }
65  if (err == ESP_ERR_FLASH_OP_TIMEOUT || err == ESP_ERR_FLASH_OP_FAIL) {
67  }
69 }
70 
72  esp_ota_abort(this->update_handle_);
73  this->update_handle_ = 0;
74 }
75 
76 } // namespace ota
77 } // namespace esphome
78 #endif
void init()
Initialize a new MD5 digest computation.
Definition: md5.cpp:10
bool equals_hex(const char *expected)
Compare the digest against a provided hex-encoded digest (32 bytes).
Definition: md5.cpp:48
OTAResponseTypes begin(size_t image_size) override
void add(const uint8_t *data, size_t len)
Add bytes of data for the digest.
Definition: md5.cpp:15
std::string size_t len
Definition: helpers.h:278
Definition: a4988.cpp:4
OTAResponseTypes write(uint8_t *data, size_t len) override
void calculate()
Compute the digest, based on the provided data.
Definition: md5.cpp:17
void set_update_md5(const char *md5) override
OTAResponseTypes end() override