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