ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
ota_backend_arduino_libretiny.cpp
Go to the documentation of this file.
1#ifdef USE_LIBRETINY
3#include "ota_backend.h"
4
6#include "esphome/core/log.h"
7
8#include <Update.h>
9
10namespace esphome::ota {
11
12ESPHOME_LOG_TAG(TAG, "ota");
13
14std::unique_ptr<ArduinoLibreTinyOTABackend> make_ota_backend() { return make_unique<ArduinoLibreTinyOTABackend>(); }
15
17 if (!this->lock_.try_acquire())
19 if (ota_type != OTA_TYPE_UPDATE_APP) {
21 }
22 // Handle UPDATE_SIZE_UNKNOWN (0) which is used by web server OTA
23 // where the exact firmware size is unknown due to multipart encoding
24 if (image_size == 0) {
25 image_size = UPDATE_SIZE_UNKNOWN;
26 }
27 bool ret = Update.begin(image_size, U_FLASH);
28 if (ret) {
29 return OTA_RESPONSE_OK;
30 }
31
32 uint8_t error = Update.getError();
33 if (error == UPDATE_ERROR_SIZE)
35
36 ESP_LOGE(TAG, "Begin error: %d", error);
37
39}
40
42 Update.setMD5(md5);
43 this->md5_set_ = true;
44}
45
47 size_t written = Update.write(data, len);
48 if (written == len) {
49 return OTA_RESPONSE_OK;
50 }
51
52 uint8_t error = Update.getError();
53 ESP_LOGE(TAG, "Write error: %d", error);
54
56}
57
59 // Use strict validation (false) when MD5 is set, lenient validation (true) when no MD5
60 // This matches the behavior of the old web_server OTA implementation
61 if (Update.end(!this->md5_set_)) {
62 this->lock_.hold_until_reboot();
63 return OTA_RESPONSE_OK;
64 }
65
66 uint8_t error = Update.getError();
67 ESP_LOGE(TAG, "End error: %d", error);
68
70}
71
73 if (!this->lock_.is_held()) // refused in begin()
74 return;
75 Update.abort();
76}
77
78} // namespace esphome::ota
79#endif // USE_LIBRETINY
OTAResponseTypes begin(size_t image_size, OTAType ota_type=OTA_TYPE_UPDATE_APP)
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.
int ret
ESPHOME_LOG_TAG(TAG, "ota")
@ OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE
Definition ota_backend.h:42
@ OTA_RESPONSE_ERROR_WRITING_FLASH
Definition ota_backend.h:36
@ OTA_RESPONSE_ERROR_UNSUPPORTED_OTA_TYPE
Definition ota_backend.h:47
@ OTA_RESPONSE_ERROR_UPDATE_END
Definition ota_backend.h:37
@ OTA_RESPONSE_ERROR_UPDATE_BUSY
Definition ota_backend.h:54
@ OTA_RESPONSE_ERROR_UNKNOWN
Definition ota_backend.h:55
std::unique_ptr< ArduinoLibreTinyOTABackend > make_ota_backend()
const void size_t len
Definition hal.h:68
int written
Definition helpers.h:1262