1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-29 16:42:19 +01:00
This commit is contained in:
J. Nick Koston
2025-09-26 23:52:48 -05:00
parent 20cbc48ad4
commit dba680a748
2 changed files with 36 additions and 33 deletions

View File

@@ -344,7 +344,7 @@ void ESPHomeOTAComponent::handle_data_() {
size_t requested = std::min(sizeof(buf), ota_size - total); size_t requested = std::min(sizeof(buf), ota_size - total);
ssize_t read = this->client_->read(buf, requested); ssize_t read = this->client_->read(buf, requested);
if (read == -1) { if (read == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (this->would_block_(errno)) {
this->yield_and_feed_watchdog_(); this->yield_and_feed_watchdog_();
continue; continue;
} }
@@ -442,7 +442,7 @@ bool ESPHomeOTAComponent::readall_(uint8_t *buf, size_t len) {
ssize_t read = this->client_->read(buf + at, len - at); ssize_t read = this->client_->read(buf + at, len - at);
if (read == -1) { if (read == -1) {
if (errno != EAGAIN && errno != EWOULDBLOCK) { if (!this->would_block_(errno)) {
ESP_LOGW(TAG, "Error reading %d bytes, errno %d", len, errno); ESP_LOGW(TAG, "Error reading %d bytes, errno %d", len, errno);
return false; return false;
} }
@@ -469,7 +469,7 @@ bool ESPHomeOTAComponent::writeall_(const uint8_t *buf, size_t len) {
ssize_t written = this->client_->write(buf + at, len - at); ssize_t written = this->client_->write(buf + at, len - at);
if (written == -1) { if (written == -1) {
if (errno != EAGAIN && errno != EWOULDBLOCK) { if (!this->would_block_(errno)) {
ESP_LOGW(TAG, "Error writing %d bytes, errno %d", len, errno); ESP_LOGW(TAG, "Error writing %d bytes, errno %d", len, errno);
return false; return false;
} }
@@ -499,12 +499,8 @@ void ESPHomeOTAComponent::log_remote_closed_(const LogString *during) {
ESP_LOGW(TAG, "Remote closed during %s", LOG_STR_ARG(during)); ESP_LOGW(TAG, "Remote closed during %s", LOG_STR_ARG(during));
} }
bool ESPHomeOTAComponent::try_read_(size_t to_read, const LogString *error_desc, const LogString *close_desc) { bool ESPHomeOTAComponent::handle_read_error_(ssize_t read, const LogString *error_desc, const LogString *close_desc) {
// Read bytes into handshake buffer, starting at handshake_buf_pos_ if (read == -1 && this->would_block_(errno)) {
size_t bytes_to_read = to_read - this->handshake_buf_pos_;
ssize_t read = this->client_->read(this->handshake_buf_ + this->handshake_buf_pos_, bytes_to_read);
if (read == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
return false; // No data yet, try again next loop return false; // No data yet, try again next loop
} }
@@ -513,6 +509,29 @@ bool ESPHomeOTAComponent::try_read_(size_t to_read, const LogString *error_desc,
this->cleanup_connection_(); this->cleanup_connection_();
return false; return false;
} }
return true;
}
bool ESPHomeOTAComponent::handle_write_error_(ssize_t written, const LogString *error_desc) {
if (written == -1) {
if (this->would_block_(errno)) {
return false; // Try again next loop
}
this->log_socket_error_(error_desc);
this->cleanup_connection_();
return false;
}
return true;
}
bool ESPHomeOTAComponent::try_read_(size_t to_read, const LogString *error_desc, const LogString *close_desc) {
// Read bytes into handshake buffer, starting at handshake_buf_pos_
size_t bytes_to_read = to_read - this->handshake_buf_pos_;
ssize_t read = this->client_->read(this->handshake_buf_ + this->handshake_buf_pos_, bytes_to_read);
if (!this->handle_read_error_(read, error_desc, close_desc)) {
return false;
}
this->handshake_buf_pos_ += read; this->handshake_buf_pos_ += read;
// Return true only if we have all the requested bytes // Return true only if we have all the requested bytes
@@ -524,12 +543,7 @@ bool ESPHomeOTAComponent::try_write_(size_t to_write, const LogString *error_des
size_t bytes_to_write = to_write - this->handshake_buf_pos_; size_t bytes_to_write = to_write - this->handshake_buf_pos_;
ssize_t written = this->client_->write(this->handshake_buf_ + this->handshake_buf_pos_, bytes_to_write); ssize_t written = this->client_->write(this->handshake_buf_ + this->handshake_buf_pos_, bytes_to_write);
if (written == -1) { if (!this->handle_write_error_(written, error_desc)) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return false; // Try again next loop
}
this->log_socket_error_(error_desc);
this->cleanup_connection_();
return false; return false;
} }
@@ -649,12 +663,7 @@ bool ESPHomeOTAComponent::handle_auth_send_() {
size_t remaining = to_write - this->auth_buf_pos_; size_t remaining = to_write - this->auth_buf_pos_;
ssize_t written = this->client_->write(this->auth_buf_.get() + this->auth_buf_pos_, remaining); ssize_t written = this->client_->write(this->auth_buf_.get() + this->auth_buf_pos_, remaining);
if (written == -1) { if (!this->handle_write_error_(written, LOG_STR("auth write"))) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return false; // Try again next loop
}
this->log_auth_warning_(LOG_STR("Writing auth type and nonce failed"));
this->cleanup_connection_();
return false; return false;
} }
@@ -680,18 +689,8 @@ bool ESPHomeOTAComponent::handle_auth_read_() {
size_t remaining = to_read - this->auth_buf_pos_; size_t remaining = to_read - this->auth_buf_pos_;
ssize_t read = this->client_->read(this->auth_buf_.get() + cnonce_offset + this->auth_buf_pos_, remaining); ssize_t read = this->client_->read(this->auth_buf_.get() + cnonce_offset + this->auth_buf_pos_, remaining);
if (read == -1) { auto *auth_read_desc = LOG_STR("auth read");
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (!this->handle_read_error_(read, auth_read_desc, auth_read_desc)) {
return false; // Try again next loop
}
this->log_auth_warning_(LOG_STR("Reading cnonce response failed"));
this->cleanup_connection_();
return false;
}
if (read == 0) {
this->log_auth_warning_(LOG_STR("Remote closed during auth read"));
this->cleanup_connection_();
return false; return false;
} }

View File

@@ -58,6 +58,10 @@ class ESPHomeOTAComponent : public ota::OTAComponent {
bool try_read_(size_t to_read, const LogString *error_desc, const LogString *close_desc); bool try_read_(size_t to_read, const LogString *error_desc, const LogString *close_desc);
bool try_write_(size_t to_write, const LogString *error_desc); bool try_write_(size_t to_write, const LogString *error_desc);
bool would_block_(int error_code) const { return error_code == EAGAIN || error_code == EWOULDBLOCK; }
bool handle_read_error_(ssize_t read, const LogString *error_desc, const LogString *close_desc);
bool handle_write_error_(ssize_t written, const LogString *error_desc);
void transition_ota_state_(OTAState next_state); void transition_ota_state_(OTAState next_state);
void log_socket_error_(const LogString *msg); void log_socket_error_(const LogString *msg);