12#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
13#define HAS_PROTO_MESSAGE_DUMP
27 return (
static_cast<uint32_t
>(value) << 1) ^ (
static_cast<uint32_t
>(value >> 31));
31 return (
static_cast<uint64_t
>(value) << 1) ^ (
static_cast<uint64_t
>(value >> 63));
35 return (value & 1) ?
static_cast<int32_t
>(~(value >> 1)) :
static_cast<int32_t
>(value >> 1);
39 return (value & 1) ?
static_cast<int64_t
>(~(value >> 1)) :
static_cast<int64_t
>(value >> 1);
47 while (
len > 0 && (*data & 0x80)) {
64 *buffer++ =
static_cast<uint8_t
>(
val | 0x80);
67 *buffer =
static_cast<uint8_t
>(
val);
108#ifdef ESPHOME_DEBUG_API
109 assert(consumed !=
nullptr);
115 if ((buffer[0] & 0x80) == 0) {
122 uint64_t result = buffer[0] & 0x7F;
127 uint32_t max_len = std::min(
len, uint32_t(10));
130 for (uint32_t i = 1; i < max_len; i++) {
131 uint8_t
val = buffer[i];
132 result |= uint64_t(
val & 0x7F) << uint64_t(bitpos);
134 if ((
val & 0x80) == 0) {
149 return static_cast<int32_t
>(this->
as_int64());
153 return static_cast<int64_t
>(this->
value_);
223 while (value > 0x7F) {
224 this->
buffer_->push_back(
static_cast<uint8_t
>(value | 0x80));
227 this->
buffer_->push_back(
static_cast<uint8_t
>(value));
230 while (value > 0x7F) {
231 this->
buffer_->push_back(
static_cast<uint8_t
>(value | 0x80));
234 this->
buffer_->push_back(
static_cast<uint8_t
>(value));
252 void encode_string(uint32_t field_id,
const char *
string,
size_t len,
bool force =
false) {
253 if (
len == 0 && !force)
262 size_t old_size = this->
buffer_->size();
264 std::memcpy(this->
buffer_->data() + old_size,
string,
len);
266 void encode_string(uint32_t field_id,
const std::string &value,
bool force =
false) {
267 this->
encode_string(field_id, value.data(), value.size(), force);
272 void encode_bytes(uint32_t field_id,
const uint8_t *data,
size_t len,
bool force =
false) {
273 this->
encode_string(field_id,
reinterpret_cast<const char *
>(data),
len, force);
276 if (value == 0 && !force)
282 if (value == 0 && !force)
287 void encode_bool(uint32_t field_id,
bool value,
bool force =
false) {
288 if (!value && !force)
291 this->
buffer_->push_back(value ? 0x01 : 0x00);
294 if (value == 0 && !force)
298 this->
write((value >> 0) & 0xFF);
299 this->
write((value >> 8) & 0xFF);
300 this->
write((value >> 16) & 0xFF);
301 this->
write((value >> 24) & 0xFF);
307 void encode_float(uint32_t field_id,
float value,
bool force =
false) {
308 if (value == 0.0f && !force)
318 void encode_int32(uint32_t field_id, int32_t value,
bool force =
false) {
324 this->
encode_uint32(field_id,
static_cast<uint32_t
>(value), force);
326 void encode_int64(uint32_t field_id, int64_t value,
bool force =
false) {
327 this->
encode_uint64(field_id,
static_cast<uint64_t
>(value), force);
344#ifdef HAS_PROTO_MESSAGE_DUMP
359 append_impl_(str, strlen(str));
365 append_impl_(str,
len);
374 memset(buf_ + pos_, c, n);
381 const char *
c_str()
const {
return buf_; }
382 size_t size()
const {
return pos_; }
387 size_t pos()
const {
return pos_; }
399 void append_impl_(
const char *str,
size_t len) {
404 memcpy(buf_ + pos_, str,
len);
422#ifdef HAS_PROTO_MESSAGE_DUMP
431 virtual void decode(
const uint8_t *buffer,
size_t length);
453 uint32_t total_size_ = 0;
484 static constexpr uint32_t
varint(uint32_t value) {
493 }
else if (value < 2097152) {
495 }
else if (value < 268435456) {
508 static constexpr uint32_t
varint(uint64_t value) {
510 if (value <= UINT32_MAX) {
511 return varint(
static_cast<uint32_t
>(value));
515 if (value < (1ULL << 35)) {
517 }
else if (value < (1ULL << 42)) {
519 }
else if (value < (1ULL << 49)) {
521 }
else if (value < (1ULL << 56)) {
523 }
else if (value < (1ULL << 63)) {
539 static constexpr uint32_t
varint(int32_t value) {
546 return varint(
static_cast<uint32_t
>(value));
555 static constexpr uint32_t
varint(int64_t value) {
559 return varint(
static_cast<uint64_t
>(value));
569 static constexpr uint32_t
field(uint32_t field_id, uint32_t
type) {
591 inline void add_int32(uint32_t field_id_size, int32_t value) {
603 total_size_ += field_id_size + (value < 0 ? 10 : varint(static_cast<uint32_t>(value)));
609 inline void add_uint32(uint32_t field_id_size, uint32_t value) {
620 total_size_ += field_id_size +
varint(value);
626 inline void add_bool(uint32_t field_id_size,
bool value) {
629 total_size_ += field_id_size + 1;
639 total_size_ += field_id_size + 1;
645 inline void add_float(uint32_t field_id_size,
float value) {
647 total_size_ += field_id_size + 4;
659 total_size_ += field_id_size + 4;
671 total_size_ += field_id_size + 4;
683 inline void add_sint32(uint32_t field_id_size, int32_t value) {
703 inline void add_int64(uint32_t field_id_size, int64_t value) {
714 total_size_ += field_id_size +
varint(value);
720 inline void add_uint64(uint32_t field_id_size, uint64_t value) {
731 total_size_ += field_id_size +
varint(value);
753 total_size_ += field_id_size +
varint(
static_cast<uint32_t
>(
len)) +
static_cast<uint32_t
>(
len);
775 if (nested_size != 0) {
788 total_size_ += field_id_size +
varint(nested_size) + nested_size;
803 message.calculate_size(nested_calc);
804 uint32_t nested_size = nested_calc.
get_size();
818 message.calculate_size(nested_calc);
819 uint32_t nested_size = nested_calc.
get_size();
834 template<
typename MessageType>
837 if (!messages.empty()) {
839 for (
const auto &
message : messages) {
852 template<
typename MessageType>
855 if (!messages.
empty()) {
857 for (
const auto &
message : messages) {
870 size_t packed_size = 0;
871 for (
int value : values) {
876 total_size_ += field_id_size +
varint(
static_cast<uint32_t
>(packed_size)) +
static_cast<uint32_t
>(packed_size);
886 size_t packed_size = 0;
887 for (
int value : values) {
894 for (
int value : values) {
906 uint32_t msg_length_bytes = msg_size.
get_size();
912 size_t begin = this->
buffer_->size();
913 this->
buffer_->resize(this->
buffer_->size() + varint_length_bytes);
921#ifdef ESPHOME_DEBUG_API
923 assert(this->
buffer_->size() == begin + varint_length_bytes + msg_length_bytes);
942 virtual void read_message(uint32_t msg_size, uint32_t msg_type,
const uint8_t *msg_data) = 0;
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
StringRef is a reference to a string owned by something else.
constexpr const char * c_str() const
constexpr size_type size() const
Fixed-size buffer for message dumps - avoids heap allocation.
const char * c_str() const
DumpBuffer & append(size_t n, char c)
size_t pos() const
Get current position for use with buf_append_printf.
static constexpr size_t CAPACITY
DumpBuffer & append(const char *str, size_t len)
DumpBuffer & append(const char *str)
char * data()
Get writable buffer pointer for use with buf_append_printf.
void set_pos(size_t pos)
Update position after buf_append_printf call.
uint32_t as_fixed32() const
int32_t as_sfixed32() const
Proto32Bit(uint32_t value)
virtual bool decode_32bit(uint32_t field_id, Proto32Bit value)
virtual bool decode_varint(uint32_t field_id, ProtoVarInt value)
virtual void decode(const uint8_t *buffer, size_t length)
virtual bool decode_length(uint32_t field_id, ProtoLengthDelimited value)
static uint32_t count_repeated_field(const uint8_t *buffer, size_t length, uint32_t target_field_id)
Count occurrences of a repeated field in a protobuf buffer.
void decode_to_message(ProtoDecodableMessage &msg) const
Decode the length-delimited data into an existing ProtoDecodableMessage instance.
const uint8_t *const value_
const uint8_t * data() const
ProtoLengthDelimited(const uint8_t *value, size_t length)
std::string as_string() const
virtual void encode(ProtoWriteBuffer buffer) const
virtual const char * message_name() const
virtual ~ProtoMessage()=default
virtual void calculate_size(ProtoSize &size) const
virtual const char * dump_to(DumpBuffer &out) const =0
virtual void on_fatal_error()=0
virtual bool send_message_impl(const ProtoMessage &msg, uint8_t message_type)=0
Send a protobuf message by calculating its size, allocating a buffer, encoding, and sending.
virtual bool is_connection_setup()=0
virtual bool is_authenticated()=0
virtual void read_message(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data)=0
bool check_authenticated_()
bool check_connection_setup_()
virtual void on_no_setup_connection()=0
virtual bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type)=0
void add_message_object(uint32_t field_id_size, const ProtoMessage &message)
Calculates and adds the size of a nested message field to the total message size.
static constexpr uint32_t varint(uint32_t value)
Calculates the size in bytes needed to encode a uint32_t value as a varint.
void add_message_field_force(uint32_t field_id_size, uint32_t nested_size)
Calculates and adds the size of a nested message field to the total message size (force version)
void add_message_object_force(uint32_t field_id_size, const ProtoMessage &message)
Calculates and adds the size of a nested message field to the total message size (force version)
void add_float(uint32_t field_id_size, float value)
Calculates and adds the size of a float field to the total message size.
static constexpr uint32_t field(uint32_t field_id, uint32_t type)
Calculates the size in bytes needed to encode a field ID and wire type.
static constexpr uint32_t varint(int32_t value)
Calculates the size in bytes needed to encode an int32_t value as a varint.
void add_sfixed32(uint32_t field_id_size, int32_t value)
Calculates and adds the size of a sfixed32 field to the total message size.
void add_uint32_force(uint32_t field_id_size, uint32_t value)
Calculates and adds the size of a uint32 field to the total message size (force version)
void add_int32_force(uint32_t field_id_size, int32_t value)
Calculates and adds the size of an int32 field to the total message size (force version)
void add_repeated_message(uint32_t field_id_size, const FixedVector< MessageType > &messages)
Calculates and adds the sizes of all messages in a repeated field to the total message size (FixedVec...
static constexpr uint32_t varint(int64_t value)
Calculates the size in bytes needed to encode an int64_t value as a varint.
void add_int64_force(uint32_t field_id_size, int64_t value)
Calculates and adds the size of an int64 field to the total message size (force version)
void add_sint32(uint32_t field_id_size, int32_t value)
Calculates and adds the size of a sint32 field to the total message size.
void add_message_field(uint32_t field_id_size, uint32_t nested_size)
Calculates and adds the size of a nested message field to the total message size.
void add_bool_force(uint32_t field_id_size, bool value)
Calculates and adds the size of a boolean field to the total message size (force version)
void add_int64(uint32_t field_id_size, int64_t value)
Calculates and adds the size of an int64 field to the total message size.
void add_bool(uint32_t field_id_size, bool value)
Calculates and adds the size of a boolean field to the total message size.
uint32_t get_size() const
void add_uint32(uint32_t field_id_size, uint32_t value)
Calculates and adds the size of a uint32 field to the total message size.
static constexpr uint32_t varint(uint64_t value)
Calculates the size in bytes needed to encode a uint64_t value as a varint.
void add_repeated_message(uint32_t field_id_size, const std::vector< MessageType > &messages)
Calculates and adds the sizes of all messages in a repeated field to the total message size.
void add_uint64(uint32_t field_id_size, uint64_t value)
Calculates and adds the size of a uint64 field to the total message size.
void add_length_force(uint32_t field_id_size, size_t len)
Calculates and adds the size of a length-delimited field (string/bytes) to the total message size (re...
void add_int32(uint32_t field_id_size, int32_t value)
Common parameters for all add_*_field methods.
void add_precalculated_size(uint32_t size)
Adds a pre-calculated size directly to the total.
void add_sint32_force(uint32_t field_id_size, int32_t value)
Calculates and adds the size of a sint32 field to the total message size (force version)
void add_uint64_force(uint32_t field_id_size, uint64_t value)
Calculates and adds the size of a uint64 field to the total message size (force version)
void add_fixed32(uint32_t field_id_size, uint32_t value)
Calculates and adds the size of a fixed32 field to the total message size.
ProtoSize()=default
ProtoSize class for Protocol Buffer serialization size calculation.
void add_packed_sint32(uint32_t field_id_size, const std::vector< int32_t > &values)
Calculate size of a packed repeated sint32 field.
void add_length(uint32_t field_id_size, size_t len)
Calculates and adds the size of a length-delimited field (string/bytes) to the total message size.
Representation of a VarInt - in ProtoBuf should be 64bit but we only use 32bit.
constexpr uint16_t as_uint16() const
constexpr uint64_t as_uint64() const
constexpr int32_t as_int32() const
constexpr uint32_t as_uint32() const
constexpr int64_t as_int64() const
constexpr bool as_bool() const
ProtoVarInt(uint64_t value)
constexpr int32_t as_sint32() const
constexpr int64_t as_sint64() const
static optional< ProtoVarInt > parse(const uint8_t *buffer, uint32_t len, uint32_t *consumed)
Parse a varint from buffer. consumed must be a valid pointer (not null).
void encode_varint_raw(uint32_t value)
void encode_string(uint32_t field_id, const std::string &value, bool force=false)
void write(uint8_t value)
void encode_int64(uint32_t field_id, int64_t value, bool force=false)
void encode_float(uint32_t field_id, float value, bool force=false)
void encode_int32(uint32_t field_id, int32_t value, bool force=false)
void encode_sint64(uint32_t field_id, int64_t value, bool force=false)
void encode_string(uint32_t field_id, const char *string, size_t len, bool force=false)
void encode_bool(uint32_t field_id, bool value, bool force=false)
ProtoWriteBuffer(std::vector< uint8_t > *buffer)
void encode_uint64(uint32_t field_id, uint64_t value, bool force=false)
void encode_string(uint32_t field_id, const StringRef &ref, bool force=false)
void encode_uint32(uint32_t field_id, uint32_t value, bool force=false)
void encode_sint32(uint32_t field_id, int32_t value, bool force=false)
void encode_packed_sint32(uint32_t field_id, const std::vector< int32_t > &values)
Encode a packed repeated sint32 field (zero-copy from vector)
void encode_field_raw(uint32_t field_id, uint32_t type)
Encode a field key (tag/wire type combination).
void encode_message(uint32_t field_id, const ProtoMessage &value)
std::vector< uint8_t > * get_buffer() const
void encode_bytes(uint32_t field_id, const uint8_t *data, size_t len, bool force=false)
void encode_fixed32(uint32_t field_id, uint32_t value, bool force=false)
std::vector< uint8_t > * buffer_
void encode_varint_raw_64(uint64_t value)
constexpr uint32_t encode_zigzag32(int32_t value)
constexpr uint8_t WIRE_TYPE_VARINT
const char * proto_enum_to_string(T value)
constexpr uint64_t encode_zigzag64(int64_t value)
constexpr uint8_t WIRE_TYPE_MASK
constexpr uint8_t WIRE_TYPE_LENGTH_DELIMITED
constexpr int32_t decode_zigzag32(uint32_t value)
constexpr uint8_t WIRE_TYPE_FIXED32
constexpr int64_t decode_zigzag64(uint64_t value)
void encode_varint_to_buffer(uint32_t val, uint8_t *buffer)
Encode a varint directly into a pre-allocated buffer.
uint16_t count_packed_varints(const uint8_t *data, size_t len)
Count number of varints in a packed buffer.