1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-28 16:12:24 +01:00
This commit is contained in:
J. Nick Koston
2025-09-26 22:01:44 -05:00
parent e7b9f17bbe
commit 10c5a19503
2 changed files with 32 additions and 40 deletions

View File

@@ -156,33 +156,9 @@ void ESPHomeOTAComponent::handle_handshake_() {
while (true) { while (true) {
switch (this->ota_state_) { switch (this->ota_state_) {
case OTAState::MAGIC_READ: { case OTAState::MAGIC_READ: {
// Try to read remaining magic bytes // Try to read remaining magic bytes (5 total)
if (this->handshake_buf_pos_ < 5) { if (!this->try_read_(5, LOG_STR("reading magic bytes"), LOG_STR("handshake"))) {
// Read as many bytes as available return;
uint8_t bytes_to_read = 5 - 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; // No data yet, try again next loop
}
if (read <= 0) {
// Error or connection closed
if (read == -1) {
this->log_socket_error_(LOG_STR("reading magic bytes"));
} else {
this->log_remote_closed_(LOG_STR("handshake"));
}
this->cleanup_connection_();
return;
}
this->handshake_buf_pos_ += read;
}
// Check if we have all 5 magic bytes
if (this->handshake_buf_pos_ != 5) {
break;
} }
// Validate magic bytes // Validate magic bytes
@@ -237,19 +213,7 @@ void ESPHomeOTAComponent::handle_handshake_() {
case OTAState::FEATURE_READ: { case OTAState::FEATURE_READ: {
// Read features - 1 byte // Read features - 1 byte
ssize_t read = this->client_->read(this->handshake_buf_, 1); if (!this->try_read_(1, LOG_STR("reading features"), LOG_STR("feature read"))) {
if (read == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
return; // No data yet, try again next loop
}
if (read <= 0) {
if (read == -1) {
this->log_socket_error_(LOG_STR("reading features"));
} else {
this->log_remote_closed_(LOG_STR("feature read"));
}
this->cleanup_connection_();
return; return;
} }
@@ -594,6 +558,31 @@ 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) {
// 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 (read == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
return false; // No data yet, try again next loop
}
if (read <= 0) {
// Error or connection closed
if (read == -1) {
this->log_socket_error_(error_desc);
} else {
this->log_remote_closed_(close_desc);
}
this->cleanup_connection_();
return false;
}
this->handshake_buf_pos_ += read;
// Return true only if we have all the requested bytes
return this->handshake_buf_pos_ >= to_read;
}
void ESPHomeOTAComponent::cleanup_connection_() { void ESPHomeOTAComponent::cleanup_connection_() {
this->client_->close(); this->client_->close();
this->client_ = nullptr; this->client_ = nullptr;

View File

@@ -46,6 +46,9 @@ class ESPHomeOTAComponent : public ota::OTAComponent {
#endif // USE_OTA_PASSWORD #endif // USE_OTA_PASSWORD
bool readall_(uint8_t *buf, size_t len); bool readall_(uint8_t *buf, size_t len);
bool writeall_(const uint8_t *buf, size_t len); bool writeall_(const uint8_t *buf, size_t len);
bool try_read_(size_t to_read, const LogString *error_desc, const LogString *close_desc);
void log_socket_error_(const LogString *msg); void log_socket_error_(const LogString *msg);
void log_read_error_(const LogString *what); void log_read_error_(const LogString *what);
void log_start_(const LogString *phase); void log_start_(const LogString *phase);