From 3a564633b773552c53769157ef5640e2ad188f2d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 3 Apr 2026 22:20:11 -1000 Subject: [PATCH] clean --- esphome/components/api/api_connection.cpp | 28 +++++++++++------------ 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index a714df67909..a25be074e30 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -2009,27 +2009,27 @@ uint16_t APIConnection::encode_to_buffer(uint32_t calculated_size, MessageEncode } #endif // Cache frame sizes to avoid repeated virtual calls + const uint8_t header_padding = conn->helper_->frame_header_padding(); const uint8_t footer_size = conn->helper_->frame_footer_size(); - size_t to_add; - if (conn->flags_.batch_first_message) { - // First message - buffer already prepared by caller with max header padding. - // Use max padding for header size (actual header computed at write time). - conn->flags_.batch_first_message = false; - conn->batch_header_size_ = conn->helper_->frame_header_padding(); - to_add = calculated_size; // Padding already in buffer - } else { - // Subsequent batch messages use exact header size — no gaps - conn->batch_header_size_ = conn->helper_->frame_header_size(calculated_size, conn->batch_message_type_); - to_add = calculated_size + conn->batch_header_size_ + footer_size; - } + // Calculate total size with padding for buffer allocation + size_t total_calculated_size = calculated_size + header_padding + footer_size; - // total_calculated_size reflects wire size (header + payload + footer) - size_t total_calculated_size = calculated_size + conn->batch_header_size_ + footer_size; + // Check if it fits if (total_calculated_size > remaining_size) return 0; // Doesn't fit auto &shared_buf = conn->parent_->get_shared_buffer_ref(); + + // First message: padding already in buffer, only add payload. + // Subsequent messages: use exact header size for gap-free packing. + bool first = conn->flags_.batch_first_message; + if (first) + conn->flags_.batch_first_message = false; + conn->batch_header_size_ = + first ? header_padding : conn->helper_->frame_header_size(calculated_size, conn->batch_message_type_); + size_t to_add = first ? calculated_size : (calculated_size + conn->batch_header_size_ + footer_size); + shared_buf.resize(shared_buf.size() + to_add); ProtoWriteBuffer buffer{&shared_buf, shared_buf.size() - calculated_size}; encode_fn(msg, buffer);