12#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
13#define HAS_PROTO_MESSAGE_DUMP
20 return (
static_cast<uint32_t
>(value) << 1) ^ (
static_cast<uint32_t
>(value >> 31));
24 return (
static_cast<uint64_t
>(value) << 1) ^ (
static_cast<uint64_t
>(value >> 63));
28 return (value & 1) ?
static_cast<int32_t
>(~(value >> 1)) :
static_cast<int32_t
>(value >> 1);
32 return (value & 1) ?
static_cast<int64_t
>(~(value >> 1)) :
static_cast<int64_t
>(value >> 1);
73 if (consumed !=
nullptr)
79 if ((buffer[0] & 0x80) == 0) {
80 if (consumed !=
nullptr)
87 uint64_t result = buffer[0] & 0x7F;
91 for (uint32_t i = 1; i <
len; i++) {
92 uint8_t
val = buffer[i];
93 result |= uint64_t(
val & 0x7F) << uint64_t(bitpos);
95 if ((
val & 0x80) == 0) {
96 if (consumed !=
nullptr)
102 if (consumed !=
nullptr)
113 return static_cast<int32_t
>(this->
as_int64());
117 return static_cast<int64_t
>(this->
value_);
146 uint8_t temp =
val & 0x7F;
149 buffer[i++] = temp | 0x80;
162 uint8_t temp =
val & 0x7F;
165 out.push_back(temp | 0x80);
244 uint32_t
val = (field_id << 3) | (
type & 0b111);
247 void encode_string(uint32_t field_id,
const char *
string,
size_t len,
bool force =
false) {
248 if (
len == 0 && !force)
257 size_t old_size = this->
buffer_->size();
259 std::memcpy(this->
buffer_->data() + old_size,
string,
len);
261 void encode_string(uint32_t field_id,
const std::string &value,
bool force =
false) {
262 this->
encode_string(field_id, value.data(), value.size(), force);
267 void encode_bytes(uint32_t field_id,
const uint8_t *data,
size_t len,
bool force =
false) {
268 this->
encode_string(field_id,
reinterpret_cast<const char *
>(data),
len, force);
271 if (value == 0 && !force)
277 if (value == 0 && !force)
282 void encode_bool(uint32_t field_id,
bool value,
bool force =
false) {
283 if (!value && !force)
289 if (value == 0 && !force)
293 this->
write((value >> 0) & 0xFF);
294 this->
write((value >> 8) & 0xFF);
295 this->
write((value >> 16) & 0xFF);
296 this->
write((value >> 24) & 0xFF);
302 void encode_float(uint32_t field_id,
float value,
bool force =
false) {
303 if (value == 0.0f && !force)
313 void encode_int32(uint32_t field_id, int32_t value,
bool force =
false) {
319 this->
encode_uint32(field_id,
static_cast<uint32_t
>(value), force);
321 void encode_int64(uint32_t field_id, int64_t value,
bool force =
false) {
322 this->
encode_uint64(field_id,
static_cast<uint64_t
>(value), force);
347#ifdef HAS_PROTO_MESSAGE_DUMP
348 std::string
dump()
const;
349 virtual void dump_to(std::string &out)
const = 0;
368 uint32_t total_size_ = 0;
399 static constexpr uint32_t
varint(uint32_t value) {
408 }
else if (value < 2097152) {
410 }
else if (value < 268435456) {
423 static constexpr uint32_t
varint(uint64_t value) {
425 if (value <= UINT32_MAX) {
426 return varint(
static_cast<uint32_t
>(value));
430 if (value < (1ULL << 35)) {
432 }
else if (value < (1ULL << 42)) {
434 }
else if (value < (1ULL << 49)) {
436 }
else if (value < (1ULL << 56)) {
438 }
else if (value < (1ULL << 63)) {
454 static constexpr uint32_t
varint(int32_t value) {
461 return varint(
static_cast<uint32_t
>(value));
470 static constexpr uint32_t
varint(int64_t value) {
474 return varint(
static_cast<uint64_t
>(value));
484 static constexpr uint32_t
field(uint32_t field_id, uint32_t
type) {
485 uint32_t tag = (field_id << 3) | (
type & 0b111);
506 inline void add_int32(uint32_t field_id_size, int32_t value) {
518 total_size_ += field_id_size + (value < 0 ? 10 : varint(static_cast<uint32_t>(value)));
524 inline void add_uint32(uint32_t field_id_size, uint32_t value) {
535 total_size_ += field_id_size +
varint(value);
541 inline void add_bool(uint32_t field_id_size,
bool value) {
544 total_size_ += field_id_size + 1;
554 total_size_ += field_id_size + 1;
560 inline void add_float(uint32_t field_id_size,
float value) {
562 total_size_ += field_id_size + 4;
574 total_size_ += field_id_size + 4;
586 total_size_ += field_id_size + 4;
598 inline void add_sint32(uint32_t field_id_size, int32_t value) {
618 inline void add_int64(uint32_t field_id_size, int64_t value) {
629 total_size_ += field_id_size +
varint(value);
635 inline void add_uint64(uint32_t field_id_size, uint64_t value) {
646 total_size_ += field_id_size +
varint(value);
668 total_size_ += field_id_size +
varint(
static_cast<uint32_t
>(
len)) +
static_cast<uint32_t
>(
len);
690 if (nested_size != 0) {
703 total_size_ += field_id_size +
varint(nested_size) + nested_size;
719 uint32_t nested_size = nested_calc.
get_size();
734 uint32_t nested_size = nested_calc.
get_size();
749 template<
typename MessageType>
752 if (messages.empty()) {
757 for (
const auto &message : messages) {
770 uint32_t msg_length_bytes = msg_size.
get_size();
776 size_t begin = this->
buffer_->size();
777 this->
buffer_->resize(this->
buffer_->size() + varint_length_bytes);
786 assert(this->
buffer_->size() == begin + varint_length_bytes + msg_length_bytes);
802#ifdef USE_API_PASSWORD
815 virtual void read_message(uint32_t msg_size, uint32_t msg_type, uint8_t *msg_data) = 0;
821 uint32_t msg_size = size.
get_size();
843#ifdef USE_API_PASSWORD
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)
void decode(const uint8_t *buffer, size_t length)
virtual bool decode_length(uint32_t field_id, ProtoLengthDelimited value)
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)
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)
const char * proto_enum_to_string(T value)
constexpr uint64_t encode_zigzag64(int64_t value)
constexpr int32_t decode_zigzag32(uint32_t value)
constexpr int64_t decode_zigzag64(uint64_t value)