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);
240 uint32_t
val = (field_id << 3) | (
type & 0b111);
243 void encode_string(uint32_t field_id,
const char *
string,
size_t len,
bool force =
false) {
244 if (
len == 0 && !force)
253 size_t old_size = this->
buffer_->size();
255 std::memcpy(this->
buffer_->data() + old_size,
string,
len);
257 void encode_string(uint32_t field_id,
const std::string &value,
bool force =
false) {
258 this->
encode_string(field_id, value.data(), value.size(), force);
263 void encode_bytes(uint32_t field_id,
const uint8_t *data,
size_t len,
bool force =
false) {
264 this->
encode_string(field_id,
reinterpret_cast<const char *
>(data),
len, force);
267 if (value == 0 && !force)
273 if (value == 0 && !force)
278 void encode_bool(uint32_t field_id,
bool value,
bool force =
false) {
279 if (!value && !force)
285 if (value == 0 && !force)
289 this->
write((value >> 0) & 0xFF);
290 this->
write((value >> 8) & 0xFF);
291 this->
write((value >> 16) & 0xFF);
292 this->
write((value >> 24) & 0xFF);
298 void encode_float(uint32_t field_id,
float value,
bool force =
false) {
299 if (value == 0.0f && !force)
309 void encode_int32(uint32_t field_id, int32_t value,
bool force =
false) {
315 this->
encode_uint32(field_id,
static_cast<uint32_t
>(value), force);
317 void encode_int64(uint32_t field_id, int64_t value,
bool force =
false) {
318 this->
encode_uint64(field_id,
static_cast<uint64_t
>(value), force);
343#ifdef HAS_PROTO_MESSAGE_DUMP
344 std::string
dump()
const;
345 virtual void dump_to(std::string &out)
const = 0;
364 uint32_t total_size_ = 0;
395 static constexpr uint32_t
varint(uint32_t value) {
404 }
else if (value < 2097152) {
406 }
else if (value < 268435456) {
419 static constexpr uint32_t
varint(uint64_t value) {
421 if (value <= UINT32_MAX) {
422 return varint(
static_cast<uint32_t
>(value));
426 if (value < (1ULL << 35)) {
428 }
else if (value < (1ULL << 42)) {
430 }
else if (value < (1ULL << 49)) {
432 }
else if (value < (1ULL << 56)) {
434 }
else if (value < (1ULL << 63)) {
450 static constexpr uint32_t
varint(int32_t value) {
457 return varint(
static_cast<uint32_t
>(value));
466 static constexpr uint32_t
varint(int64_t value) {
470 return varint(
static_cast<uint64_t
>(value));
480 static constexpr uint32_t
field(uint32_t field_id, uint32_t
type) {
481 uint32_t tag = (field_id << 3) | (
type & 0b111);
502 inline void add_int32(uint32_t field_id_size, int32_t value) {
514 total_size_ += field_id_size + (value < 0 ? 10 : varint(static_cast<uint32_t>(value)));
520 inline void add_uint32(uint32_t field_id_size, uint32_t value) {
531 total_size_ += field_id_size +
varint(value);
537 inline void add_bool(uint32_t field_id_size,
bool value) {
540 total_size_ += field_id_size + 1;
550 total_size_ += field_id_size + 1;
556 inline void add_float(uint32_t field_id_size,
float value) {
558 total_size_ += field_id_size + 4;
570 total_size_ += field_id_size + 4;
582 total_size_ += field_id_size + 4;
594 inline void add_sint32(uint32_t field_id_size, int32_t value) {
614 inline void add_int64(uint32_t field_id_size, int64_t value) {
625 total_size_ += field_id_size +
varint(value);
631 inline void add_uint64(uint32_t field_id_size, uint64_t value) {
642 total_size_ += field_id_size +
varint(value);
664 total_size_ += field_id_size +
varint(
static_cast<uint32_t
>(
len)) +
static_cast<uint32_t
>(
len);
686 if (nested_size != 0) {
699 total_size_ += field_id_size +
varint(nested_size) + nested_size;
715 uint32_t nested_size = nested_calc.
get_size();
730 uint32_t nested_size = nested_calc.
get_size();
745 template<
typename MessageType>
748 if (messages.empty()) {
753 for (
const auto &message : messages) {
766 uint32_t msg_length_bytes = msg_size.
get_size();
772 size_t begin = this->
buffer_->size();
773 this->
buffer_->resize(this->
buffer_->size() + varint_length_bytes);
782 assert(this->
buffer_->size() == begin + varint_length_bytes + msg_length_bytes);
798#ifdef USE_API_PASSWORD
811 virtual void read_message(uint32_t msg_size, uint32_t msg_type, uint8_t *msg_data) = 0;
817 uint32_t msg_size = size.
get_size();
839#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_
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)