ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
ota_backend_arduino_esp8266.cpp
Go to the documentation of this file.
1#ifdef USE_ARDUINO
2#ifdef USE_ESP8266
4#include "ota_backend.h"
5
8#include "esphome/core/log.h"
9
10#include <Updater.h>
11
12namespace esphome {
13namespace ota {
14
15static const char *const TAG = "ota.arduino_esp8266";
16
17std::unique_ptr<ota::OTABackend> make_ota_backend() { return make_unique<ota::ArduinoESP8266OTABackend>(); }
18
20 // Handle UPDATE_SIZE_UNKNOWN (0) by calculating available space
21 if (image_size == 0) {
22 // NOLINTNEXTLINE(readability-static-accessed-through-instance)
23 image_size = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
24 }
25 bool ret = Update.begin(image_size, U_FLASH);
26 if (ret) {
28 return OTA_RESPONSE_OK;
29 }
30
31 uint8_t error = Update.getError();
32 if (error == UPDATE_ERROR_BOOTSTRAP)
34 if (error == UPDATE_ERROR_NEW_FLASH_CONFIG)
36 if (error == UPDATE_ERROR_FLASH_CONFIG)
38 if (error == UPDATE_ERROR_SPACE)
40
41 ESP_LOGE(TAG, "Begin error: %d", error);
42
44}
45
47 Update.setMD5(md5);
48 this->md5_set_ = true;
49}
50
52 size_t written = Update.write(data, len);
53 if (written == len) {
54 return OTA_RESPONSE_OK;
55 }
56
57 uint8_t error = Update.getError();
58 ESP_LOGE(TAG, "Write error: %d", error);
59
61}
62
64 // Use strict validation (false) when MD5 is set, lenient validation (true) when no MD5
65 // This matches the behavior of the old web_server OTA implementation
66 bool success = Update.end(!this->md5_set_);
67
68 // On ESP8266, Update.end() might return false even with error code 0
69 // Check the actual error code to determine success
70 uint8_t error = Update.getError();
71
72 if (success || error == UPDATE_ERROR_OK) {
73 return OTA_RESPONSE_OK;
74 }
75
76 ESP_LOGE(TAG, "End error: %d", error);
78}
79
84
85} // namespace ota
86} // namespace esphome
87
88#endif
89#endif
OTAResponseTypes begin(size_t image_size) override
OTAResponseTypes write(uint8_t *data, size_t len) override
void preferences_prevent_write(bool prevent)
std::unique_ptr< ota::OTABackend > make_ota_backend()
@ OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG
Definition ota_backend.h:34
@ OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG
Definition ota_backend.h:33
@ OTA_RESPONSE_ERROR_WRITING_FLASH
Definition ota_backend.h:30
@ OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE
Definition ota_backend.h:35
@ OTA_RESPONSE_ERROR_UPDATE_END
Definition ota_backend.h:31
@ OTA_RESPONSE_ERROR_UNKNOWN
Definition ota_backend.h:40
@ OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING
Definition ota_backend.h:32
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:279