From 4bdf44bb78405835bf34ba99d34f1368e12e54bb Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 10 Aug 2025 18:28:27 -0500 Subject: [PATCH] preen --- .../components/esphome/ota/ota_esphome.cpp | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/esphome/components/esphome/ota/ota_esphome.cpp b/esphome/components/esphome/ota/ota_esphome.cpp index cb8cdb4a42..fb862b3925 100644 --- a/esphome/components/esphome/ota/ota_esphome.cpp +++ b/esphome/components/esphome/ota/ota_esphome.cpp @@ -137,25 +137,31 @@ void ESPHomeOTAComponent::handle_handshake_() { // Try to read first byte of magic bytes uint8_t first_byte; ssize_t read = this->client_->read(&first_byte, 1); - if (read == 1) { - // Got the first byte, check if it's the magic byte - if (first_byte != 0x6C) { - ESP_LOGW(TAG, "Invalid initial byte: 0x%02X", first_byte); - this->cleanup_connection_(); - return; - } - // First byte is valid, continue with data handling - this->handle_data_(); - } else if (read == -1) { - if (errno != EAGAIN && errno != EWOULDBLOCK) { - this->log_socket_error_("reading first byte"); - this->cleanup_connection_(); - } - // For EAGAIN/EWOULDBLOCK, just return and try again next loop - } else if (read == 0) { - ESP_LOGW(TAG, "Remote closed during handshake"); - this->cleanup_connection_(); + + 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_("reading first byte"); + } else { + ESP_LOGW(TAG, "Remote closed during handshake"); + } + this->cleanup_connection_(); + return; + } + + // Got first byte, check if it's the magic byte + if (first_byte != 0x6C) { + ESP_LOGW(TAG, "Invalid initial byte: 0x%02X", first_byte); + this->cleanup_connection_(); + return; + } + + // First byte is valid, continue with data handling + this->handle_data_(); } void ESPHomeOTAComponent::handle_data_() {