9 #ifdef ESPHOME_LOG_HAS_VERY_VERBOSE 10 #define HAS_PROTO_MESSAGE_DUMP 23 if (consumed !=
nullptr)
32 for (uint32_t i = 0; i <
len; i++) {
33 uint8_t
val = buffer[i];
34 result |= uint64_t(val & 0x7F) << uint64_t(bitpos);
36 if ((val & 0x80) == 0) {
37 if (consumed !=
nullptr)
52 return static_cast<int32_t
>(this->
as_int64());
56 return static_cast<int64_t
>(this->
value_);
61 return static_cast<int32_t
>(~(this->
value_ >> 1));
63 return static_cast<int32_t
>(this->
value_ >> 1);
69 return static_cast<int64_t
>(~(this->
value_ >> 1));
71 return static_cast<int64_t
>(this->
value_ >> 1);
74 void encode(std::vector<uint8_t> &out) {
81 uint8_t temp = val & 0x7F;
84 out.push_back(temp | 0x80);
98 std::string
as_string()
const {
return std::string(reinterpret_cast<const char *>(this->
value_), this->length_); }
101 msg.decode(this->
value_, this->length_);
149 void write(uint8_t value) { this->buffer_->push_back(value); }
153 uint32_t
val = (field_id << 3) | (type & 0b111);
154 this->encode_varint_raw(val);
156 void encode_string(uint32_t field_id,
const char *
string,
size_t len,
bool force =
false) {
157 if (len == 0 && !force)
160 this->encode_field_raw(field_id, 2);
161 this->encode_varint_raw(len);
162 auto *data =
reinterpret_cast<const uint8_t *
>(string);
163 this->buffer_->insert(this->buffer_->end(), data, data +
len);
165 void encode_string(uint32_t field_id,
const std::string &value,
bool force =
false) {
166 this->encode_string(field_id, value.data(), value.size());
168 void encode_bytes(uint32_t field_id,
const uint8_t *data,
size_t len,
bool force =
false) {
169 this->encode_string(field_id, reinterpret_cast<const char *>(data), len, force);
172 if (value == 0 && !force)
174 this->encode_field_raw(field_id, 0);
175 this->encode_varint_raw(value);
178 if (value == 0 && !force)
180 this->encode_field_raw(field_id, 0);
183 void encode_bool(uint32_t field_id,
bool value,
bool force =
false) {
184 if (!value && !force)
186 this->encode_field_raw(field_id, 0);
190 if (value == 0 && !force)
193 this->encode_field_raw(field_id, 5);
194 this->write((value >> 0) & 0xFF);
195 this->write((value >> 8) & 0xFF);
196 this->write((value >> 16) & 0xFF);
197 this->write((value >> 24) & 0xFF);
200 if (value == 0 && !force)
203 this->encode_field_raw(field_id, 5);
204 this->write((value >> 0) & 0xFF);
205 this->write((value >> 8) & 0xFF);
206 this->write((value >> 16) & 0xFF);
207 this->write((value >> 24) & 0xFF);
208 this->write((value >> 32) & 0xFF);
209 this->write((value >> 40) & 0xFF);
210 this->write((value >> 48) & 0xFF);
211 this->write((value >> 56) & 0xFF);
213 template<
typename T>
void encode_enum(uint32_t field_id, T value,
bool force =
false) {
214 this->
encode_uint32(field_id, static_cast<uint32_t>(value), force);
216 void encode_float(uint32_t field_id,
float value,
bool force =
false) {
217 if (value == 0.0f && !force)
225 this->encode_fixed32(field_id,
val.raw);
227 void encode_int32(uint32_t field_id, int32_t value,
bool force =
false) {
230 this->encode_int64(field_id, value, force);
233 this->
encode_uint32(field_id, static_cast<uint32_t>(value), force);
235 void encode_int64(uint32_t field_id, int64_t value,
bool force =
false) {
236 this->encode_uint64(field_id, static_cast<uint64_t>(value), force);
241 uvalue = ~(value << 1);
250 uvalue = ~(value << 1);
254 this->encode_uint64(field_id, uvalue, force);
256 template<
class C>
void encode_message(uint32_t field_id,
const C &value,
bool force =
false) {
257 this->encode_field_raw(field_id, 2);
258 size_t begin = this->buffer_->size();
262 const uint32_t nested_length = this->buffer_->size() - begin;
264 std::vector<uint8_t> var;
266 this->buffer_->insert(this->buffer_->begin() + begin, var.begin(), var.end());
278 void decode(
const uint8_t *buffer,
size_t length);
279 #ifdef HAS_PROTO_MESSAGE_DUMP 280 std::string dump()
const;
281 virtual void dump_to(std::string &out)
const = 0;
296 virtual bool is_authenticated() = 0;
297 virtual bool is_connection_setup() = 0;
298 virtual void on_fatal_error() = 0;
299 virtual void on_unauthenticated_access() = 0;
300 virtual void on_no_setup_connection() = 0;
302 virtual bool send_buffer(
ProtoWriteBuffer buffer, uint32_t message_type) = 0;
303 virtual bool read_message(uint32_t msg_size, uint32_t msg_type, uint8_t *msg_data) = 0;
306 auto buffer = this->create_buffer();
308 return this->send_buffer(buffer, message_type);
void encode_fixed32(uint32_t field_id, uint32_t value, bool force=false)
int32_t as_sint32() const
std::vector< uint8_t > * get_buffer() const
virtual bool decode_32bit(uint32_t field_id, Proto32Bit value)
ProtoWriteBuffer(std::vector< uint8_t > *buffer)
void encode_string(uint32_t field_id, const std::string &value, bool force=false)
virtual bool decode_varint(uint32_t field_id, ProtoVarInt value)
int64_t as_sfixed64() const
Representation of a VarInt - in ProtoBuf should be 64bit but we only use 32bit.
std::vector< uint8_t > * buffer_
virtual bool decode_64bit(uint32_t field_id, Proto64Bit value)
static optional< ProtoVarInt > parse(const uint8_t *buffer, uint32_t len, uint32_t *consumed)
void encode_float(uint32_t field_id, float value, bool force=false)
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
std::string as_string() const
void encode_uint64(uint32_t field_id, uint64_t value, bool force=false)
uint64_t as_fixed64() const
const uint8_t *const value_
Proto32Bit(uint32_t value)
bool send_message_(const C &msg, uint32_t message_type)
void encode_field_raw(uint32_t field_id, uint32_t type)
void encode_sint32(uint32_t field_id, int32_t value, bool force=false)
uint32_t as_fixed32() const
uint64_t as_uint64() const
const char * proto_enum_to_string(T value)
void encode_bool(uint32_t field_id, bool value, bool force=false)
void encode(std::vector< uint8_t > &out)
void encode_uint32(uint32_t field_id, uint32_t value, bool force=false)
virtual bool decode_length(uint32_t field_id, ProtoLengthDelimited value)
void encode_int32(uint32_t field_id, int32_t value, bool force=false)
int64_t as_sint64() const
int32_t as_sfixed32() const
void encode_fixed64(uint32_t field_id, uint64_t value, bool force=false)
Implementation of SPI Controller mode.
void write(uint8_t value)
void encode_varint_raw(ProtoVarInt value)
void encode_string(uint32_t field_id, const char *string, size_t len, bool force=false)
void encode_varint_raw(uint32_t value)
uint32_t as_uint32() const
void encode_sint64(uint32_t field_id, int64_t value, bool force=false)
ProtoLengthDelimited(const uint8_t *value, size_t length)
void encode_enum(uint32_t field_id, T value, bool force=false)
void encode_message(uint32_t field_id, const C &value, bool force=false)
Proto64Bit(uint64_t value)
void encode_bytes(uint32_t field_id, const uint8_t *data, size_t len, bool force=false)
ProtoVarInt(uint64_t value)
void encode_int64(uint32_t field_id, int64_t value, bool force=false)