15static const char *
const TAG =
"ota";
17std::unique_ptr<ArduinoLibreTinyOTABackend>
make_ota_backend() {
return make_unique<ArduinoLibreTinyOTABackend>(); }
25 if (image_size == 0) {
26 image_size = UPDATE_SIZE_UNKNOWN;
28 bool ret = Update.begin(image_size, U_FLASH);
34 uint8_t error = Update.getError();
35 if (error == UPDATE_ERROR_SIZE)
38 ESP_LOGE(TAG,
"Begin error: %d", error);
45 this->md5_set_ =
true;
46 this->md5_parsed_ =
parse_hex(md5, this->expected_md5_, 16);
49bool ArduinoLibreTinyOTABackend::feed_(
const uint8_t *data,
size_t len) {
51 const size_t chunk = std::min(
len, MAX_FEED);
52 if (Update.write(data, chunk) != chunk) {
53 ESP_LOGE(TAG,
"Write error: %d", Update.getError());
68 if ((this->has_pending_ && !this->feed_(&this->pending_, 1)) || !this->feed_(data,
len - 1))
70 this->pending_ = data[
len - 1];
71 this->has_pending_ =
true;
78 if (!this->md5_parsed_ || !this->md5_.
equals_bytes(this->expected_md5_)) {
79 ESP_LOGE(TAG,
"MD5 mismatch");
83 if (this->has_pending_ && !this->feed_(&this->pending_, 1))
87 if (Update.end(!this->md5_set_)) {
93 uint8_t error = Update.getError();
94 ESP_LOGE(TAG,
"End error: %d", error);
109 const bool reverted = lt_flash_erase_block(FLASH_OTA_OFFSET);
112 const bool reverted = lt_ota_switch(
true);
115 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)
@ 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_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.