From 14e2c85028b49f11840f39ee92afdc6c903be698 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 20 Jul 2025 18:05:21 -1000 Subject: [PATCH 1/2] [api] Remove unused add_fixed_field template function --- esphome/components/api/api_connection.cpp | 7 +++---- esphome/components/api/proto.h | 19 ------------------- 2 files changed, 3 insertions(+), 23 deletions(-) diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 602a0256cf..ebcec54518 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -225,11 +225,10 @@ void APIConnection::loop() { if (this->image_reader_ && this->image_reader_->available() && this->helper_->can_write_without_blocking()) { uint32_t to_send = std::min((size_t) MAX_BATCH_PACKET_SIZE, this->image_reader_->available()); bool done = this->image_reader_->available() == to_send; - uint32_t msg_size = 0; - ProtoSize::add_fixed_field<4>(msg_size, 1, true); // partial message size calculated manually since its a special case - // 1 for the data field, varint for the data size, and the data itself - msg_size += 1 + ProtoSize::varint(to_send) + to_send; + // fixed32 key = 1 (1 byte for field header + 4 bytes for fixed32) + // bytes data = 2 (1 byte for field header + varint for data size + data itself) + uint32_t msg_size = 1 + 4 + 1 + ProtoSize::varint(to_send) + to_send; ProtoSize::add_bool_field(msg_size, 1, done); auto buffer = this->create_buffer(msg_size); diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index a2c31100bf..1eafee0711 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -527,25 +527,6 @@ class ProtoSize { total_size += field_id_size + 1; } - /** - * @brief Calculates and adds the size of a fixed field to the total message size - * - * Fixed fields always take exactly N bytes (4 for fixed32/float, 8 for fixed64/double). - * - * @tparam NumBytes The number of bytes for this fixed field (4 or 8) - * @param is_nonzero Whether the value is non-zero - */ - template - static inline void add_fixed_field(uint32_t &total_size, uint32_t field_id_size, bool is_nonzero) { - // Skip calculation if value is zero - if (!is_nonzero) { - return; // No need to update total_size - } - - // Fixed fields always take exactly NumBytes - total_size += field_id_size + NumBytes; - } - /** * @brief Calculates and adds the size of a float field to the total message size */ From ffaba916d7f613960a48efc3c199a779550ca5fe Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 20 Jul 2025 20:39:13 -1000 Subject: [PATCH 2/2] cleanup --- script/api_protobuf/api_protobuf.py | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 96471d33b2..69ce1809ea 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -609,6 +609,9 @@ class BytesType(TypeInfo): super().__init__(field) self.needs_decode = needs_decode self.needs_encode = needs_encode + # Only set decode_length if we need decoding + if needs_decode: + self.decode_length = "value.as_string()" @property def public_content(self) -> list[str]: @@ -640,18 +643,6 @@ class BytesType(TypeInfo): return content - @property - def decode_length_content(self) -> str: - if not self.needs_decode: - return "" # No decode needed for SOURCE_SERVER messages - - # Decode into storage only - pointer/length are only needed for encoding - return ( - f"case {self.number}:\n" - f" this->{self.field_name} = value.as_string();\n" - f" break;" - ) - @property def encode_content(self) -> str: return f"buffer.encode_bytes({self.number}, this->{self.field_name}_ptr_, this->{self.field_name}_len_);"