ESPHome  2024.3.1
ota_component.cpp
Go to the documentation of this file.
1 #include "ota_component.h"
2 #include "ota_backend.h"
7 #include "ota_backend_esp_idf.h"
8 
9 #include "esphome/core/log.h"
11 #include "esphome/core/hal.h"
12 #include "esphome/core/util.h"
15 
16 #include <cerrno>
17 #include <cstdio>
18 
19 namespace esphome {
20 namespace ota {
21 
22 static const char *const TAG = "ota";
23 static constexpr u_int16_t OTA_BLOCK_SIZE = 8192;
24 
25 OTAComponent *global_ota_component = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
26 
27 std::unique_ptr<OTABackend> make_ota_backend() {
28 #ifdef USE_ARDUINO
29 #ifdef USE_ESP8266
30  return make_unique<ArduinoESP8266OTABackend>();
31 #endif // USE_ESP8266
32 #ifdef USE_ESP32
33  return make_unique<ArduinoESP32OTABackend>();
34 #endif // USE_ESP32
35 #endif // USE_ARDUINO
36 #ifdef USE_ESP_IDF
37  return make_unique<IDFOTABackend>();
38 #endif // USE_ESP_IDF
39 #ifdef USE_RP2040
40  return make_unique<ArduinoRP2040OTABackend>();
41 #endif // USE_RP2040
42 #ifdef USE_LIBRETINY
43  return make_unique<ArduinoLibreTinyOTABackend>();
44 #endif
45 }
46 
47 OTAComponent::OTAComponent() { global_ota_component = this; }
48 
50  server_ = socket::socket_ip(SOCK_STREAM, 0);
51  if (server_ == nullptr) {
52  ESP_LOGW(TAG, "Could not create socket.");
53  this->mark_failed();
54  return;
55  }
56  int enable = 1;
57  int err = server_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
58  if (err != 0) {
59  ESP_LOGW(TAG, "Socket unable to set reuseaddr: errno %d", err);
60  // we can still continue
61  }
62  err = server_->setblocking(false);
63  if (err != 0) {
64  ESP_LOGW(TAG, "Socket unable to set nonblocking mode: errno %d", err);
65  this->mark_failed();
66  return;
67  }
68 
69  struct sockaddr_storage server;
70 
71  socklen_t sl = socket::set_sockaddr_any((struct sockaddr *) &server, sizeof(server), this->port_);
72  if (sl == 0) {
73  ESP_LOGW(TAG, "Socket unable to set sockaddr: errno %d", errno);
74  this->mark_failed();
75  return;
76  }
77 
78  err = server_->bind((struct sockaddr *) &server, sizeof(server));
79  if (err != 0) {
80  ESP_LOGW(TAG, "Socket unable to bind: errno %d", errno);
81  this->mark_failed();
82  return;
83  }
84 
85  err = server_->listen(4);
86  if (err != 0) {
87  ESP_LOGW(TAG, "Socket unable to listen: errno %d", errno);
88  this->mark_failed();
89  return;
90  }
91 
92  this->dump_config();
93 }
94 
96  ESP_LOGCONFIG(TAG, "Over-The-Air Updates:");
97  ESP_LOGCONFIG(TAG, " Address: %s:%u", network::get_use_address().c_str(), this->port_);
98 #ifdef USE_OTA_PASSWORD
99  if (!this->password_.empty()) {
100  ESP_LOGCONFIG(TAG, " Using Password.");
101  }
102 #endif
103  ESP_LOGCONFIG(TAG, " OTA version: %d.", USE_OTA_VERSION);
104  if (this->has_safe_mode_ && this->safe_mode_rtc_value_ > 1 &&
106  ESP_LOGW(TAG, "Last Boot was an unhandled reset, will proceed to safe mode in %" PRIu32 " restarts",
108  }
109 }
110 
112  this->handle_();
113 
114  if (this->has_safe_mode_ && (millis() - this->safe_mode_start_time_) > this->safe_mode_enable_time_) {
115  this->has_safe_mode_ = false;
116  // successful boot, reset counter
117  ESP_LOGI(TAG, "Boot seems successful, resetting boot loop counter.");
118  this->clean_rtc();
119  }
120 }
121 
122 static const uint8_t FEATURE_SUPPORTS_COMPRESSION = 0x01;
123 
126  bool update_started = false;
127  size_t total = 0;
128  uint32_t last_progress = 0;
129  uint8_t buf[1024];
130  char *sbuf = reinterpret_cast<char *>(buf);
131  size_t ota_size;
132  uint8_t ota_features;
133  std::unique_ptr<OTABackend> backend;
134  (void) ota_features;
135 #if USE_OTA_VERSION == 2
136  size_t size_acknowledged = 0;
137 #endif
138 
139  if (client_ == nullptr) {
140  struct sockaddr_storage source_addr;
141  socklen_t addr_len = sizeof(source_addr);
142  client_ = server_->accept((struct sockaddr *) &source_addr, &addr_len);
143  }
144  if (client_ == nullptr)
145  return;
146 
147  int enable = 1;
148  int err = client_->setsockopt(IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(int));
149  if (err != 0) {
150  ESP_LOGW(TAG, "Socket could not enable tcp nodelay, errno: %d", errno);
151  return;
152  }
153 
154  ESP_LOGD(TAG, "Starting OTA Update from %s...", this->client_->getpeername().c_str());
155  this->status_set_warning();
156 #ifdef USE_OTA_STATE_CALLBACK
157  this->state_callback_.call(OTA_STARTED, 0.0f, 0);
158 #endif
159 
160  if (!this->readall_(buf, 5)) {
161  ESP_LOGW(TAG, "Reading magic bytes failed!");
162  goto error; // NOLINT(cppcoreguidelines-avoid-goto)
163  }
164  // 0x6C, 0x26, 0xF7, 0x5C, 0x45
165  if (buf[0] != 0x6C || buf[1] != 0x26 || buf[2] != 0xF7 || buf[3] != 0x5C || buf[4] != 0x45) {
166  ESP_LOGW(TAG, "Magic bytes do not match! 0x%02X-0x%02X-0x%02X-0x%02X-0x%02X", buf[0], buf[1], buf[2], buf[3],
167  buf[4]);
168  error_code = OTA_RESPONSE_ERROR_MAGIC;
169  goto error; // NOLINT(cppcoreguidelines-avoid-goto)
170  }
171 
172  // Send OK and version - 2 bytes
173  buf[0] = OTA_RESPONSE_OK;
174  buf[1] = USE_OTA_VERSION;
175  this->writeall_(buf, 2);
176 
177  backend = make_ota_backend();
178 
179  // Read features - 1 byte
180  if (!this->readall_(buf, 1)) {
181  ESP_LOGW(TAG, "Reading features failed!");
182  goto error; // NOLINT(cppcoreguidelines-avoid-goto)
183  }
184  ota_features = buf[0]; // NOLINT
185  ESP_LOGV(TAG, "OTA features is 0x%02X", ota_features);
186 
187  // Acknowledge header - 1 byte
188  buf[0] = OTA_RESPONSE_HEADER_OK;
189  if ((ota_features & FEATURE_SUPPORTS_COMPRESSION) != 0 && backend->supports_compression()) {
191  }
192 
193  this->writeall_(buf, 1);
194 
195 #ifdef USE_OTA_PASSWORD
196  if (!this->password_.empty()) {
197  buf[0] = OTA_RESPONSE_REQUEST_AUTH;
198  this->writeall_(buf, 1);
199  md5::MD5Digest md5{};
200  md5.init();
201  sprintf(sbuf, "%08" PRIx32, random_uint32());
202  md5.add(sbuf, 8);
203  md5.calculate();
204  md5.get_hex(sbuf);
205  ESP_LOGV(TAG, "Auth: Nonce is %s", sbuf);
206 
207  // Send nonce, 32 bytes hex MD5
208  if (!this->writeall_(reinterpret_cast<uint8_t *>(sbuf), 32)) {
209  ESP_LOGW(TAG, "Auth: Writing nonce failed!");
210  goto error; // NOLINT(cppcoreguidelines-avoid-goto)
211  }
212 
213  // prepare challenge
214  md5.init();
215  md5.add(this->password_.c_str(), this->password_.length());
216  // add nonce
217  md5.add(sbuf, 32);
218 
219  // Receive cnonce, 32 bytes hex MD5
220  if (!this->readall_(buf, 32)) {
221  ESP_LOGW(TAG, "Auth: Reading cnonce failed!");
222  goto error; // NOLINT(cppcoreguidelines-avoid-goto)
223  }
224  sbuf[32] = '\0';
225  ESP_LOGV(TAG, "Auth: CNonce is %s", sbuf);
226  // add cnonce
227  md5.add(sbuf, 32);
228 
229  // calculate result
230  md5.calculate();
231  md5.get_hex(sbuf);
232  ESP_LOGV(TAG, "Auth: Result is %s", sbuf);
233 
234  // Receive result, 32 bytes hex MD5
235  if (!this->readall_(buf + 64, 32)) {
236  ESP_LOGW(TAG, "Auth: Reading response failed!");
237  goto error; // NOLINT(cppcoreguidelines-avoid-goto)
238  }
239  sbuf[64 + 32] = '\0';
240  ESP_LOGV(TAG, "Auth: Response is %s", sbuf + 64);
241 
242  bool matches = true;
243  for (uint8_t i = 0; i < 32; i++)
244  matches = matches && buf[i] == buf[64 + i];
245 
246  if (!matches) {
247  ESP_LOGW(TAG, "Auth failed! Passwords do not match!");
248  error_code = OTA_RESPONSE_ERROR_AUTH_INVALID;
249  goto error; // NOLINT(cppcoreguidelines-avoid-goto)
250  }
251  }
252 #endif // USE_OTA_PASSWORD
253 
254  // Acknowledge auth OK - 1 byte
255  buf[0] = OTA_RESPONSE_AUTH_OK;
256  this->writeall_(buf, 1);
257 
258  // Read size, 4 bytes MSB first
259  if (!this->readall_(buf, 4)) {
260  ESP_LOGW(TAG, "Reading size failed!");
261  goto error; // NOLINT(cppcoreguidelines-avoid-goto)
262  }
263  ota_size = 0;
264  for (uint8_t i = 0; i < 4; i++) {
265  ota_size <<= 8;
266  ota_size |= buf[i];
267  }
268  ESP_LOGV(TAG, "OTA size is %u bytes", ota_size);
269 
270  error_code = backend->begin(ota_size);
271  if (error_code != OTA_RESPONSE_OK)
272  goto error; // NOLINT(cppcoreguidelines-avoid-goto)
273  update_started = true;
274 
275  // Acknowledge prepare OK - 1 byte
277  this->writeall_(buf, 1);
278 
279  // Read binary MD5, 32 bytes
280  if (!this->readall_(buf, 32)) {
281  ESP_LOGW(TAG, "Reading binary MD5 checksum failed!");
282  goto error; // NOLINT(cppcoreguidelines-avoid-goto)
283  }
284  sbuf[32] = '\0';
285  ESP_LOGV(TAG, "Update: Binary MD5 is %s", sbuf);
286  backend->set_update_md5(sbuf);
287 
288  // Acknowledge MD5 OK - 1 byte
289  buf[0] = OTA_RESPONSE_BIN_MD5_OK;
290  this->writeall_(buf, 1);
291 
292  while (total < ota_size) {
293  // TODO: timeout check
294  size_t requested = std::min(sizeof(buf), ota_size - total);
295  ssize_t read = this->client_->read(buf, requested);
296  if (read == -1) {
297  if (errno == EAGAIN || errno == EWOULDBLOCK) {
298  App.feed_wdt();
299  delay(1);
300  continue;
301  }
302  ESP_LOGW(TAG, "Error receiving data for update, errno: %d", errno);
303  goto error; // NOLINT(cppcoreguidelines-avoid-goto)
304  } else if (read == 0) {
305  // $ man recv
306  // "When a stream socket peer has performed an orderly shutdown, the return value will
307  // be 0 (the traditional "end-of-file" return)."
308  ESP_LOGW(TAG, "Remote end closed connection");
309  goto error; // NOLINT(cppcoreguidelines-avoid-goto)
310  }
311 
312  error_code = backend->write(buf, read);
313  if (error_code != OTA_RESPONSE_OK) {
314  ESP_LOGW(TAG, "Error writing binary data to flash!, error_code: %d", error_code);
315  goto error; // NOLINT(cppcoreguidelines-avoid-goto)
316  }
317  total += read;
318 #if USE_OTA_VERSION == 2
319  while (size_acknowledged + OTA_BLOCK_SIZE <= total || (total == ota_size && size_acknowledged < ota_size)) {
320  buf[0] = OTA_RESPONSE_CHUNK_OK;
321  this->writeall_(buf, 1);
322  size_acknowledged += OTA_BLOCK_SIZE;
323  }
324 #endif
325 
326  uint32_t now = millis();
327  if (now - last_progress > 1000) {
328  last_progress = now;
329  float percentage = (total * 100.0f) / ota_size;
330  ESP_LOGD(TAG, "OTA in progress: %0.1f%%", percentage);
331 #ifdef USE_OTA_STATE_CALLBACK
332  this->state_callback_.call(OTA_IN_PROGRESS, percentage, 0);
333 #endif
334  // feed watchdog and give other tasks a chance to run
335  App.feed_wdt();
336  yield();
337  }
338  }
339 
340  // Acknowledge receive OK - 1 byte
341  buf[0] = OTA_RESPONSE_RECEIVE_OK;
342  this->writeall_(buf, 1);
343 
344  error_code = backend->end();
345  if (error_code != OTA_RESPONSE_OK) {
346  ESP_LOGW(TAG, "Error ending OTA!, error_code: %d", error_code);
347  goto error; // NOLINT(cppcoreguidelines-avoid-goto)
348  }
349 
350  // Acknowledge Update end OK - 1 byte
352  this->writeall_(buf, 1);
353 
354  // Read ACK
355  if (!this->readall_(buf, 1) || buf[0] != OTA_RESPONSE_OK) {
356  ESP_LOGW(TAG, "Reading back acknowledgement failed!");
357  // do not go to error, this is not fatal
358  }
359 
360  this->client_->close();
361  this->client_ = nullptr;
362  delay(10);
363  ESP_LOGI(TAG, "OTA update finished!");
364  this->status_clear_warning();
365 #ifdef USE_OTA_STATE_CALLBACK
366  this->state_callback_.call(OTA_COMPLETED, 100.0f, 0);
367 #endif
368  delay(100); // NOLINT
369  App.safe_reboot();
370 
371 error:
372  buf[0] = static_cast<uint8_t>(error_code);
373  this->writeall_(buf, 1);
374  this->client_->close();
375  this->client_ = nullptr;
376 
377  if (backend != nullptr && update_started) {
378  backend->abort();
379  }
380 
381  this->status_momentary_error("onerror", 5000);
382 #ifdef USE_OTA_STATE_CALLBACK
383  this->state_callback_.call(OTA_ERROR, 0.0f, static_cast<uint8_t>(error_code));
384 #endif
385 }
386 
387 bool OTAComponent::readall_(uint8_t *buf, size_t len) {
388  uint32_t start = millis();
389  uint32_t at = 0;
390  while (len - at > 0) {
391  uint32_t now = millis();
392  if (now - start > 1000) {
393  ESP_LOGW(TAG, "Timed out reading %d bytes of data", len);
394  return false;
395  }
396 
397  ssize_t read = this->client_->read(buf + at, len - at);
398  if (read == -1) {
399  if (errno == EAGAIN || errno == EWOULDBLOCK) {
400  App.feed_wdt();
401  delay(1);
402  continue;
403  }
404  ESP_LOGW(TAG, "Failed to read %d bytes of data, errno: %d", len, errno);
405  return false;
406  } else if (read == 0) {
407  ESP_LOGW(TAG, "Remote closed connection");
408  return false;
409  } else {
410  at += read;
411  }
412  App.feed_wdt();
413  delay(1);
414  }
415 
416  return true;
417 }
418 bool OTAComponent::writeall_(const uint8_t *buf, size_t len) {
419  uint32_t start = millis();
420  uint32_t at = 0;
421  while (len - at > 0) {
422  uint32_t now = millis();
423  if (now - start > 1000) {
424  ESP_LOGW(TAG, "Timed out writing %d bytes of data", len);
425  return false;
426  }
427 
428  ssize_t written = this->client_->write(buf + at, len - at);
429  if (written == -1) {
430  if (errno == EAGAIN || errno == EWOULDBLOCK) {
431  App.feed_wdt();
432  delay(1);
433  continue;
434  }
435  ESP_LOGW(TAG, "Failed to write %d bytes of data, errno: %d", len, errno);
436  return false;
437  } else {
438  at += written;
439  }
440  App.feed_wdt();
441  delay(1);
442  }
443  return true;
444 }
445 
447 uint16_t OTAComponent::get_port() const { return this->port_; }
448 void OTAComponent::set_port(uint16_t port) { this->port_ = port; }
449 
450 void OTAComponent::set_safe_mode_pending(const bool &pending) {
451  if (!this->has_safe_mode_)
452  return;
453 
454  uint32_t current_rtc = this->read_rtc_();
455 
456  if (pending && current_rtc != esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC) {
457  ESP_LOGI(TAG, "Device will enter safe mode on next boot.");
459  }
460 
461  if (!pending && current_rtc == esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC) {
462  ESP_LOGI(TAG, "Safe mode pending has been cleared");
463  this->clean_rtc();
464  }
465 }
468 }
469 
470 bool OTAComponent::should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time) {
471  this->has_safe_mode_ = true;
472  this->safe_mode_start_time_ = millis();
473  this->safe_mode_enable_time_ = enable_time;
474  this->safe_mode_num_attempts_ = num_attempts;
475  this->rtc_ = global_preferences->make_preference<uint32_t>(233825507UL, false);
476  this->safe_mode_rtc_value_ = this->read_rtc_();
477 
478  bool is_manual_safe_mode = this->safe_mode_rtc_value_ == esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC;
479 
480  if (is_manual_safe_mode) {
481  ESP_LOGI(TAG, "Safe mode has been entered manually");
482  } else {
483  ESP_LOGCONFIG(TAG, "There have been %" PRIu32 " suspected unsuccessful boot attempts.", this->safe_mode_rtc_value_);
484  }
485 
486  if (this->safe_mode_rtc_value_ >= num_attempts || is_manual_safe_mode) {
487  this->clean_rtc();
488 
489  if (!is_manual_safe_mode) {
490  ESP_LOGE(TAG, "Boot loop detected. Proceeding to safe mode.");
491  }
492 
493  this->status_set_error();
494  this->set_timeout(enable_time, []() {
495  ESP_LOGE(TAG, "No OTA attempt made, restarting.");
496  App.reboot();
497  });
498 
499  // Delay here to allow power to stabilise before Wi-Fi/Ethernet is initialised.
500  delay(300); // NOLINT
501  App.setup();
502 
503  ESP_LOGI(TAG, "Waiting for OTA attempt.");
504 
505  return true;
506  } else {
507  // increment counter
508  this->write_rtc_(this->safe_mode_rtc_value_ + 1);
509  return false;
510  }
511 }
513  this->rtc_.save(&val);
515 }
517  uint32_t val;
518  if (!this->rtc_.load(&val))
519  return 0;
520  return val;
521 }
525  this->clean_rtc();
526 }
527 
528 #ifdef USE_OTA_STATE_CALLBACK
529 void OTAComponent::add_on_state_callback(std::function<void(OTAState, float, uint8_t)> &&callback) {
530  this->state_callback_.add(std::move(callback));
531 }
532 #endif
533 
534 } // namespace ota
535 } // namespace esphome
void init()
Initialize a new MD5 digest computation.
Definition: md5.cpp:10
std::unique_ptr< Socket > socket_ip(int type, int protocol)
Create a socket in the newest available IP domain (IPv6 or IPv4) of the given type and protocol...
Definition: socket.cpp:12
uint32_t safe_mode_enable_time_
The time safe mode should be on for.
Definition: ota_component.h:96
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition: component.cpp:26
socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port)
Set a sockaddr to the any address and specified port for the IP version used by socket_ip().
Definition: socket.cpp:53
std::string get_use_address()
Get the active network hostname.
Definition: util.cpp:52
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:146
ESPPreferenceObject rtc_
Definition: ota_component.h:99
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition: helpers.cpp:193
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:69
void add_on_state_callback(std::function< void(OTAState, float, uint8_t)> &&callback)
uint32_t socklen_t
Definition: headers.h:97
mopeka_std_values val[4]
void setup()
Set up all the registered components. Call this at the end of your setup() function.
Definition: application.cpp:28
uint32_t safe_mode_start_time_
stores when safe mode was enabled.
Definition: ota_component.h:95
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
void status_momentary_error(const std::string &name, uint32_t length=5000)
Definition: component.cpp:177
bool readall_(uint8_t *buf, size_t len)
bool save(const T *src)
Definition: preferences.h:21
static const uint32_t ENTER_SAFE_MODE_MAGIC
a magic number to indicate that safe mode should be entered on next boot
void status_set_error(const char *message="unspecified")
Definition: component.cpp:154
void set_port(uint16_t port)
Manually set the port OTA should listen on.
ESPPreferences * global_preferences
void status_clear_warning()
Definition: component.cpp:161
std::unique_ptr< socket::Socket > client_
Definition: ota_component.h:92
float get_setup_priority() const override
bool has_safe_mode_
stores whether safe mode can be enabled.
Definition: ota_component.h:94
std::unique_ptr< OTABackend > make_ota_backend()
void write_rtc_(uint32_t val)
Application App
Global storage of Application pointer - only one Application can exist.
void on_safe_shutdown() override
OTAComponent * global_ota_component
bool should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time)
std::string size_t len
Definition: helpers.h:292
void IRAM_ATTR HOT yield()
Definition: core.cpp:24
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:113
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
virtual bool sync()=0
Commit pending writes to flash.
void set_safe_mode_pending(const bool &pending)
Set to true if the next startup will enter safe mode.
bool writeall_(const uint8_t *buf, size_t len)
std::unique_ptr< socket::Socket > server_
Definition: ota_component.h:91
CallbackManager< void(OTAState, float, uint8_t)> state_callback_
OTAComponent provides a simple way to integrate Over-the-Air updates into your app using ArduinoOTA...
Definition: ota_component.h:44
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26