diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index b0c6299945..a18d5845b6 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -251,8 +251,16 @@ class ProtoWriteBuffer { /// Write a precomputed tag byte + 32-bit value in one operation. /// Tag must be a single-byte varint (< 128). No zero check. inline void write_tag_and_fixed32(uint8_t tag, uint32_t value) ESPHOME_ALWAYS_INLINE { + this->debug_check_bounds_(5); this->pos_[0] = tag; +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ std::memcpy(this->pos_ + 1, &value, 4); +#else + this->pos_[1] = static_cast(value & 0xFF); + this->pos_[2] = static_cast((value >> 8) & 0xFF); + this->pos_[3] = static_cast((value >> 16) & 0xFF); + this->pos_[4] = static_cast((value >> 24) & 0xFF); +#endif this->pos_ += 5; } void encode_string(uint32_t field_id, const char *string, size_t len, bool force = false) {