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);
80 if (consumed !=
nullptr)
86 if ((buffer[0] & 0x80) == 0) {
87 if (consumed !=
nullptr)
94 uint64_t result = buffer[0] & 0x7F;
98 for (uint32_t i = 1; i <
len; i++) {
99 uint8_t
val = buffer[i];
100 result |= uint64_t(
val & 0x7F) << uint64_t(bitpos);
102 if ((
val & 0x80) == 0) {
103 if (consumed !=
nullptr)
109 if (consumed !=
nullptr)
120 return static_cast<int32_t
>(this->
as_int64());
124 return static_cast<int64_t
>(this->
value_);
153 uint8_t temp =
val & 0x7F;
156 buffer[i++] = temp | 0x80;
169 uint8_t temp =
val & 0x7F;
172 out.push_back(temp | 0x80);
254 void encode_string(uint32_t field_id,
const char *
string,
size_t len,
bool force =
false) {
255 if (
len == 0 && !force)
264 size_t old_size = this->
buffer_->size();
266 std::memcpy(this->
buffer_->data() + old_size,
string,
len);
268 void encode_string(uint32_t field_id,
const std::string &value,
bool force =
false) {
269 this->
encode_string(field_id, value.data(), value.size(), force);
274 void encode_bytes(uint32_t field_id,
const uint8_t *data,
size_t len,
bool force =
false) {
275 this->
encode_string(field_id,
reinterpret_cast<const char *
>(data),
len, force);
278 if (value == 0 && !force)
284 if (value == 0 && !force)
289 void encode_bool(uint32_t field_id,
bool value,
bool force =
false) {
290 if (!value && !force)
296 if (value == 0 && !force)
300 this->
write((value >> 0) & 0xFF);
301 this->
write((value >> 8) & 0xFF);
302 this->
write((value >> 16) & 0xFF);
303 this->
write((value >> 24) & 0xFF);
309 void encode_float(uint32_t field_id,
float value,
bool force =
false) {
310 if (value == 0.0f && !force)
320 void encode_int32(uint32_t field_id, int32_t value,
bool force =
false) {
326 this->
encode_uint32(field_id,
static_cast<uint32_t
>(value), force);
328 void encode_int64(uint32_t field_id, int64_t value,
bool force =
false) {
329 this->
encode_uint64(field_id,
static_cast<uint64_t
>(value), force);
354#ifdef HAS_PROTO_MESSAGE_DUMP
355 std::string
dump()
const;
356 virtual void dump_to(std::string &out)
const = 0;
364 virtual void decode(
const uint8_t *buffer,
size_t length);
386 uint32_t total_size_ = 0;
417 static constexpr uint32_t
varint(uint32_t value) {
426 }
else if (value < 2097152) {
428 }
else if (value < 268435456) {
441 static constexpr uint32_t
varint(uint64_t value) {
443 if (value <= UINT32_MAX) {
444 return varint(
static_cast<uint32_t
>(value));
448 if (value < (1ULL << 35)) {
450 }
else if (value < (1ULL << 42)) {
452 }
else if (value < (1ULL << 49)) {
454 }
else if (value < (1ULL << 56)) {
456 }
else if (value < (1ULL << 63)) {
472 static constexpr uint32_t
varint(int32_t value) {
479 return varint(
static_cast<uint32_t
>(value));
488 static constexpr uint32_t
varint(int64_t value) {
492 return varint(
static_cast<uint64_t
>(value));
502 static constexpr uint32_t
field(uint32_t field_id, uint32_t
type) {
524 inline void add_int32(uint32_t field_id_size, int32_t value) {
536 total_size_ += field_id_size + (value < 0 ? 10 : varint(static_cast<uint32_t>(value)));
542 inline void add_uint32(uint32_t field_id_size, uint32_t value) {
553 total_size_ += field_id_size +
varint(value);
559 inline void add_bool(uint32_t field_id_size,
bool value) {
562 total_size_ += field_id_size + 1;
572 total_size_ += field_id_size + 1;
578 inline void add_float(uint32_t field_id_size,
float value) {
580 total_size_ += field_id_size + 4;
592 total_size_ += field_id_size + 4;
604 total_size_ += field_id_size + 4;
616 inline void add_sint32(uint32_t field_id_size, int32_t value) {
636 inline void add_int64(uint32_t field_id_size, int64_t value) {
647 total_size_ += field_id_size +
varint(value);
653 inline void add_uint64(uint32_t field_id_size, uint64_t value) {
664 total_size_ += field_id_size +
varint(value);
686 total_size_ += field_id_size +
varint(
static_cast<uint32_t
>(
len)) +
static_cast<uint32_t
>(
len);
708 if (nested_size != 0) {
721 total_size_ += field_id_size +
varint(nested_size) + nested_size;
736 message.calculate_size(nested_calc);
737 uint32_t nested_size = nested_calc.
get_size();
751 message.calculate_size(nested_calc);
752 uint32_t nested_size = nested_calc.
get_size();
767 template<
typename MessageType>
770 if (!messages.empty()) {
772 for (
const auto &
message : messages) {
785 template<
typename MessageType>
788 if (!messages.
empty()) {
790 for (
const auto &
message : messages) {
804 uint32_t msg_length_bytes = msg_size.
get_size();
810 size_t begin = this->
buffer_->size();
811 this->
buffer_->resize(this->
buffer_->size() + varint_length_bytes);
820 assert(this->
buffer_->size() == begin + varint_length_bytes + msg_length_bytes);
836#ifdef USE_API_PASSWORD
849 virtual void read_message(uint32_t msg_size, uint32_t msg_type, uint8_t *msg_data) = 0;
855 uint32_t msg_size = size.
get_size();
877#ifdef USE_API_PASSWORD
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
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 void dump_to(std::string &out) const =0
virtual void read_message(uint32_t msg_size, uint32_t msg_type, uint8_t *msg_data)=0
virtual void on_unauthenticated_access()=0
virtual ProtoWriteBuffer create_buffer(uint32_t reserve_size)=0
Create a buffer with a reserved size.
virtual void on_fatal_error()=0
virtual bool is_connection_setup()=0
virtual bool is_authenticated()=0
bool check_authenticated_()
bool check_connection_setup_()
virtual void on_no_setup_connection()=0
bool send_message_(const ProtoMessage &msg, uint8_t message_type)
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_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
void encode_to_buffer_unchecked(uint8_t *buffer, size_t len)
Encode the varint value to a pre-allocated buffer without bounds checking.
void encode(std::vector< uint8_t > &out)
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)
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_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, bool force=false)
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)
void encode_varint_raw(ProtoVarInt value)
std::vector< uint8_t > * buffer_
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)