diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 38fd98b489..32f525990d 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -195,6 +195,20 @@ class ProtoWriteBuffer { this->write((value >> 16) & 0xFF); this->write((value >> 24) & 0xFF); } + void encode_fixed64(uint32_t field_id, uint64_t value, bool force = false) { + if (value == 0 && !force) + return; + + this->encode_field_raw(field_id, 5); + this->write((value >> 0) & 0xFF); + this->write((value >> 8) & 0xFF); + this->write((value >> 16) & 0xFF); + this->write((value >> 24) & 0xFF); + this->write((value >> 32) & 0xFF); + this->write((value >> 40) & 0xFF); + this->write((value >> 48) & 0xFF); + this->write((value >> 56) & 0xFF); + } template void encode_enum(uint32_t field_id, T value, bool force = false) { this->encode_uint32(field_id, static_cast(value), force); } @@ -229,6 +243,15 @@ class ProtoWriteBuffer { } this->encode_uint32(field_id, uvalue, force); } + void encode_sint64(uint32_t field_id, int64_t value, bool force = false) { + uint64_t uvalue; + if (value < 0) { + uvalue = ~(value << 1); + } else { + uvalue = value << 1; + } + this->encode_uint64(field_id, uvalue, force); + } template void encode_message(uint32_t field_id, const C &value, bool force = false) { this->encode_field_raw(field_id, 2); size_t begin = this->buffer_->size(); diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 016a0995b9..26bf8647af 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -236,7 +236,7 @@ class Int64Type(TypeInfo): encode_func = "encode_int64" def dump(self, name): - o = f'sprintf(buffer, "%ll", {name});\n' + o = f'sprintf(buffer, "%lld", {name});\n' o += f"out.append(buffer);" return o @@ -249,7 +249,7 @@ class UInt64Type(TypeInfo): encode_func = "encode_uint64" def dump(self, name): - o = f'sprintf(buffer, "%ull", {name});\n' + o = f'sprintf(buffer, "%llu", {name});\n' o += f"out.append(buffer);" return o @@ -275,7 +275,7 @@ class Fixed64Type(TypeInfo): encode_func = "encode_fixed64" def dump(self, name): - o = f'sprintf(buffer, "%ull", {name});\n' + o = f'sprintf(buffer, "%llu", {name});\n' o += f"out.append(buffer);" return o @@ -417,7 +417,7 @@ class SFixed64Type(TypeInfo): encode_func = "encode_sfixed64" def dump(self, name): - o = f'sprintf(buffer, "%ll", {name});\n' + o = f'sprintf(buffer, "%lld", {name});\n' o += f"out.append(buffer);" return o @@ -440,10 +440,10 @@ class SInt64Type(TypeInfo): cpp_type = "int64_t" default_value = "0" decode_varint = "value.as_sint64()" - encode_func = "encode_sin64" + encode_func = "encode_sint64" def dump(self, name): - o = f'sprintf(buffer, "%ll", {name});\n' + o = f'sprintf(buffer, "%lld", {name});\n' o += f"out.append(buffer);" return o @@ -622,13 +622,13 @@ def build_message_type(desc): protected_content.insert(0, prot) if decode_64bit: decode_64bit.append("default:\n return false;") - o = f"bool {desc.name}::decode_64bit(uint32_t field_id, Proto64bit value) {{\n" + o = f"bool {desc.name}::decode_64bit(uint32_t field_id, Proto64Bit value) {{\n" o += " switch (field_id) {\n" o += indent("\n".join(decode_64bit), " ") + "\n" o += " }\n" o += "}\n" cpp += o - prot = "bool decode_64bit(uint32_t field_id, Proto64bit value) override;" + prot = "bool decode_64bit(uint32_t field_id, Proto64Bit value) override;" protected_content.insert(0, prot) o = f"void {desc.name}::encode(ProtoWriteBuffer buffer) const {{"