17std::unique_ptr<ArduinoLibreTinyOTABackend>
make_ota_backend() {
return make_unique<ArduinoLibreTinyOTABackend>(); }
27 if (image_size == 0) {
28 image_size = UPDATE_SIZE_UNKNOWN;
30 bool ret = Update.begin(image_size, U_FLASH);
36 uint8_t error = Update.getError();
37 if (error == UPDATE_ERROR_SIZE)
40 ESP_LOGE(TAG,
"Begin error: %d", error);
47 this->md5_set_ =
true;
48 this->md5_parsed_ =
parse_hex(md5, this->expected_md5_, 16);
51bool ArduinoLibreTinyOTABackend::feed_(
const uint8_t *data,
size_t len) {
53 const size_t chunk = std::min(
len, MAX_FEED);
54 if (Update.write(data, chunk) != chunk) {
55 ESP_LOGE(TAG,
"Write error: %d", Update.getError());
70 if ((this->has_pending_ && !this->feed_(&this->pending_, 1)) || !this->feed_(data,
len - 1))
72 this->pending_ = data[
len - 1];
73 this->has_pending_ =
true;
80 if (!this->md5_parsed_ || !this->md5_.
equals_bytes(this->expected_md5_)) {
81 ESP_LOGE(TAG,
"MD5 mismatch");
85 if (this->has_pending_ && !this->feed_(&this->pending_, 1))
89 if (Update.end(!this->md5_set_)) {
94 uint8_t error = Update.getError();
97 if (error == UPDATE_ERROR_ACTIVATE) {
98 ESP_LOGE(TAG,
"End error: %d, image is for another flash layout; check that board: matches this module", error);
102 ESP_LOGE(TAG,
"End error: %d", error);
119 const bool reverted = lt_flash_erase_block(FLASH_OTA_OFFSET);
122 const bool reverted = lt_ota_switch(
true);
125 ESP_LOGE(TAG,
"Revert failed");
bool equals_bytes(const uint8_t *expected)
Compare the hash against a provided byte-encoded hash.
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.
OTAResponseTypes begin(size_t image_size, OTAType ota_type=OTA_TYPE_UPDATE_APP)
void set_update_md5(const char *md5)
OTAResponseTypes write(uint8_t *data, size_t len)
void hold_until_reboot()
Keep the lock set until reboot; is_held() becomes false.
bool try_acquire()
Returns false if held elsewhere.
ESPHOME_LOG_TAG(TAG, "ota")
@ OTA_RESPONSE_ERROR_MD5_MISMATCH
@ OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE
@ OTA_RESPONSE_ERROR_WRITING_FLASH
@ OTA_RESPONSE_ERROR_UNSUPPORTED_OTA_TYPE
@ OTA_RESPONSE_ERROR_UPDATE_END
@ OTA_RESPONSE_ERROR_UPDATE_BUSY
@ OTA_RESPONSE_ERROR_UNKNOWN
std::unique_ptr< ArduinoLibreTinyOTABackend > make_ota_backend()
size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count)
Parse bytes from a hex-encoded string into a byte array.