diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index f2c4d1797f..09a1522fb4 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -1891,21 +1891,18 @@ bool BluetoothGATTGetServicesRequest::decode_varint(uint32_t field_id, ProtoVarI return true; } void BluetoothGATTDescriptor::encode(ProtoWriteBuffer buffer) const { - for (const auto &it : this->uuid) { - buffer.encode_uint64(1, it, true); - } + buffer.encode_uint64(1, this->uuid[0], true); + buffer.encode_uint64(1, this->uuid[1], true); buffer.encode_uint32(2, this->handle); } void BluetoothGATTDescriptor::calculate_size(uint32_t &total_size) const { - for (const auto &it : this->uuid) { - ProtoSize::add_uint64_field_repeated(total_size, 1, it); - } + ProtoSize::add_uint64_field_repeated(total_size, 1, this->uuid[0]); + ProtoSize::add_uint64_field_repeated(total_size, 1, this->uuid[1]); ProtoSize::add_uint32_field(total_size, 1, this->handle); } void BluetoothGATTCharacteristic::encode(ProtoWriteBuffer buffer) const { - for (const auto &it : this->uuid) { - buffer.encode_uint64(1, it, true); - } + buffer.encode_uint64(1, this->uuid[0], true); + buffer.encode_uint64(1, this->uuid[1], true); buffer.encode_uint32(2, this->handle); buffer.encode_uint32(3, this->properties); for (auto &it : this->descriptors) { @@ -1913,26 +1910,23 @@ void BluetoothGATTCharacteristic::encode(ProtoWriteBuffer buffer) const { } } void BluetoothGATTCharacteristic::calculate_size(uint32_t &total_size) const { - for (const auto &it : this->uuid) { - ProtoSize::add_uint64_field_repeated(total_size, 1, it); - } + ProtoSize::add_uint64_field_repeated(total_size, 1, this->uuid[0]); + ProtoSize::add_uint64_field_repeated(total_size, 1, this->uuid[1]); ProtoSize::add_uint32_field(total_size, 1, this->handle); ProtoSize::add_uint32_field(total_size, 1, this->properties); ProtoSize::add_repeated_message(total_size, 1, this->descriptors); } void BluetoothGATTService::encode(ProtoWriteBuffer buffer) const { - for (const auto &it : this->uuid) { - buffer.encode_uint64(1, it, true); - } + buffer.encode_uint64(1, this->uuid[0], true); + buffer.encode_uint64(1, this->uuid[1], true); buffer.encode_uint32(2, this->handle); for (auto &it : this->characteristics) { buffer.encode_message(3, it, true); } } void BluetoothGATTService::calculate_size(uint32_t &total_size) const { - for (const auto &it : this->uuid) { - ProtoSize::add_uint64_field_repeated(total_size, 1, it); - } + ProtoSize::add_uint64_field_repeated(total_size, 1, this->uuid[0]); + ProtoSize::add_uint64_field_repeated(total_size, 1, this->uuid[1]); ProtoSize::add_uint32_field(total_size, 1, this->handle); ProtoSize::add_repeated_message(total_size, 1, this->characteristics); } diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index b9d7f1fd6e..ccc87fb5a6 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -936,7 +936,20 @@ class FixedArrayRepeatedType(TypeInfo): else: return f"buffer.{self._ti.encode_func}({self.number}, this->{self.field_name}[0], true);" - # Multiple elements need a loop + # Special case for 2-element arrays - unroll the loop + if self.array_size == 2: + if isinstance(self._ti, EnumType): + return ( + f"buffer.{self._ti.encode_func}({self.number}, static_cast(this->{self.field_name}[0]), true);\n" + f" buffer.{self._ti.encode_func}({self.number}, static_cast(this->{self.field_name}[1]), true);" + ) + else: + return ( + f"buffer.{self._ti.encode_func}({self.number}, this->{self.field_name}[0], true);\n" + f" buffer.{self._ti.encode_func}({self.number}, this->{self.field_name}[1], true);" + ) + + # 3 or more elements need a loop o = f"for (const auto &it : this->{self.field_name}) {{\n" if isinstance(self._ti, EnumType): o += f" buffer.{self._ti.encode_func}({self.number}, static_cast(it), true);\n" @@ -966,6 +979,14 @@ class FixedArrayRepeatedType(TypeInfo): if self.array_size == 1: return self._ti.get_size_calculation(f"{name}[0]", True) + # Special case for 2-element arrays - unroll the calculation + if self.array_size == 2: + return ( + self._ti.get_size_calculation(f"{name}[0]", True) + + "\n " + + self._ti.get_size_calculation(f"{name}[1]", True) + ) + # Check if this is a fixed-size type by seeing if it has a fixed byte count num_bytes = self._ti.get_fixed_size_bytes() if num_bytes is not None: