ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
proto.h
Go to the documentation of this file.
1#pragma once
2
3#include "api_pb2_defines.h"
4#include "api_buffer.h"
7#include "esphome/core/log.h"
10
11#include <cassert>
12#include <cstring>
13#include <vector>
14
15#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
16#define HAS_PROTO_MESSAGE_DUMP
17#endif
18
19namespace esphome::api {
20
21// Protocol Buffer wire type constants
22// See https://protobuf.dev/programming-guides/encoding/#structure
23constexpr uint8_t WIRE_TYPE_VARINT = 0; // int32, int64, uint32, uint64, sint32, sint64, bool, enum
24constexpr uint8_t WIRE_TYPE_LENGTH_DELIMITED = 2; // string, bytes, embedded messages, packed repeated fields
25constexpr uint8_t WIRE_TYPE_FIXED32 = 5; // fixed32, sfixed32, float
26constexpr uint8_t WIRE_TYPE_MASK = 0b111; // Mask to extract wire type from tag
27
28// Reinterpret float bits as uint32_t without floating-point comparison.
29// Used by both encode_float() and calc_float() to ensure identical zero checks.
30// Uses union type-punning which is a GCC/Clang extension (not standard C++),
31// but bit_cast/memcpy don't optimize to a no-op on xtensa-gcc (ESP8266).
32inline uint32_t float_to_raw(float value) {
33 union {
34 float f;
35 uint32_t u;
36 } v;
37 v.f = value;
38 return v.u;
39}
40
41// Helper functions for ZigZag encoding/decoding
42inline constexpr uint32_t encode_zigzag32(int32_t value) {
43 return (static_cast<uint32_t>(value) << 1) ^ (static_cast<uint32_t>(value >> 31));
44}
45
46inline constexpr uint64_t encode_zigzag64(int64_t value) {
47 return (static_cast<uint64_t>(value) << 1) ^ (static_cast<uint64_t>(value >> 63));
48}
49
50inline constexpr int32_t decode_zigzag32(uint32_t value) {
51 return (value & 1) ? static_cast<int32_t>(~(value >> 1)) : static_cast<int32_t>(value >> 1);
52}
53
54inline constexpr int64_t decode_zigzag64(uint64_t value) {
55 return (value & 1) ? static_cast<int64_t>(~(value >> 1)) : static_cast<int64_t>(value >> 1);
56}
57
59inline uint16_t count_packed_varints(const uint8_t *data, size_t len) {
60 uint16_t count = 0;
61 while (len > 0) {
62 // Skip varint bytes until we find one without continuation bit
63 while (len > 0 && (*data & 0x80)) {
64 data++;
65 len--;
66 }
67 if (len > 0) {
68 data++;
69 len--;
70 count++;
71 }
72 }
73 return count;
74}
75
78inline void encode_varint_to_buffer(uint32_t val, uint8_t *buffer) {
79 while (val > 0x7F) {
80 *buffer++ = static_cast<uint8_t>(val | 0x80);
81 val >>= 7;
82 }
83 *buffer = static_cast<uint8_t>(val);
84}
85
86/*
87 * StringRef Ownership Model for API Protocol Messages
88 * ===================================================
89 *
90 * StringRef is used for zero-copy string handling in outgoing (SOURCE_SERVER) messages.
91 * It holds a pointer and length to existing string data without copying.
92 *
93 * CRITICAL: The referenced string data MUST remain valid until message encoding completes.
94 *
95 * Safe StringRef Patterns:
96 * 1. String literals: StringRef("literal") - Always safe (static storage duration)
97 * 2. Member variables: StringRef(this->member_string_) - Safe if object outlives encoding
98 * 3. Global/static strings: StringRef(GLOBAL_CONSTANT) - Always safe
99 * 4. Local variables: Safe ONLY if encoding happens before function returns:
100 * std::string temp = compute_value();
101 * msg.field = StringRef(temp);
102 * return this->send_message(msg); // temp is valid during encoding
103 *
104 * Unsafe Patterns (WILL cause crashes/corruption):
105 * 1. Temporaries: msg.field = StringRef(obj.get_string()) // get_string() returns by value
106 * 2. Concatenation: msg.field = StringRef(str1 + str2) // Result is temporary
107 *
108 * For unsafe patterns, store in a local variable first:
109 * std::string temp = get_string(); // or str1 + str2
110 * msg.field = StringRef(temp);
111 *
112 * The send_*_response pattern ensures proper lifetime management by encoding
113 * within the same function scope where temporaries are created.
114 */
115
117#ifdef USE_API_VARINT64
118using proto_varint_value_t = uint64_t;
119#else
121#endif
122
125
130 uint32_t consumed; // PROTO_VARINT_PARSE_FAILED = parse failed
131
132 constexpr bool has_value() const { return this->consumed != PROTO_VARINT_PARSE_FAILED; }
133};
134
137 public:
140 static inline ProtoVarIntResult ESPHOME_ALWAYS_INLINE parse_non_empty(const uint8_t *buffer, uint32_t len) {
141#ifdef ESPHOME_DEBUG_API
142 assert(len > 0);
143#endif
144 // Fast path: single-byte varints (0-127) are the most common case
145 // (booleans, small enums, field tags, small message sizes/types).
146 if ((buffer[0] & 0x80) == 0) [[likely]]
147 return {buffer[0], 1};
148 return parse_slow(buffer, len);
149 }
150
153 static inline ProtoVarIntResult ESPHOME_ALWAYS_INLINE parse(const uint8_t *buffer, uint32_t len) {
154 if (len == 0)
155 return {0, PROTO_VARINT_PARSE_FAILED};
156 return parse_non_empty(buffer, len);
157 }
158
159 protected:
160 // Slow path for multi-byte varints (>= 128), outlined to keep fast path small
161 static ProtoVarIntResult parse_slow(const uint8_t *buffer, uint32_t len) __attribute__((noinline));
162
163#ifdef USE_API_VARINT64
165 static ProtoVarIntResult parse_wide(const uint8_t *buffer, uint32_t len, uint32_t result32) __attribute__((noinline));
166#endif
167};
168
169// Forward declarations for encoding helpers
170class ProtoMessage;
171class ProtoSize;
172
174 public:
175 explicit ProtoLengthDelimited(const uint8_t *value, size_t length) : value_(value), length_(length) {}
176 std::string as_string() const { return std::string(reinterpret_cast<const char *>(this->value_), this->length_); }
177
178 // Direct access to raw data without string allocation
179 const uint8_t *data() const { return this->value_; }
180 size_t size() const { return this->length_; }
181
184 template<typename T> void decode_to_message(T &msg) const;
185
186 protected:
187 const uint8_t *const value_;
188 const size_t length_;
189};
190
192 public:
193 explicit Proto32Bit(uint32_t value) : value_(value) {}
194 uint32_t as_fixed32() const { return this->value_; }
195 int32_t as_sfixed32() const { return static_cast<int32_t>(this->value_); }
196 float as_float() const {
197 union {
199 float value;
200 } s{};
201 s.raw = this->value_;
202 return s.value;
203 }
204
205 protected:
207};
208
209// NOTE: Proto64Bit class removed - wire type 1 (64-bit fixed) not supported
210
211// Debug bounds checking for proto encode functions.
212// In debug mode (ESPHOME_DEBUG_API), an extra end-of-buffer pointer is threaded
213// through the entire encode chain. In production, these expand to nothing.
214#ifdef ESPHOME_DEBUG_API
215#define PROTO_ENCODE_DEBUG_PARAM , uint8_t *proto_debug_end_
216#define PROTO_ENCODE_DEBUG_ARG , proto_debug_end_
217#define PROTO_ENCODE_DEBUG_INIT(buf) , (buf)->data() + (buf)->size()
218#define PROTO_ENCODE_CHECK_BOUNDS(pos, n) \
219 do { \
220 if ((pos) + (n) > proto_debug_end_) \
221 proto_check_bounds_failed(pos, n, proto_debug_end_, __builtin_FUNCTION()); \
222 } while (0)
223void proto_check_bounds_failed(const uint8_t *pos, size_t bytes, const uint8_t *end, const char *caller);
224#else
225#define PROTO_ENCODE_DEBUG_PARAM
226#define PROTO_ENCODE_DEBUG_ARG
227#define PROTO_ENCODE_DEBUG_INIT(buf)
228#define PROTO_ENCODE_CHECK_BOUNDS(pos, n)
229#endif
230
232 public:
233 ProtoWriteBuffer(APIBuffer *buffer) : buffer_(buffer), pos_(buffer->data() + buffer->size()) {}
234 ProtoWriteBuffer(APIBuffer *buffer, size_t write_pos) : buffer_(buffer), pos_(buffer->data() + write_pos) {}
235 inline void ESPHOME_ALWAYS_INLINE encode_varint_raw(uint32_t value) {
236 if (value < 128) [[likely]] {
237 this->debug_check_bounds_(1);
238 *this->pos_++ = static_cast<uint8_t>(value);
239 return;
240 }
241 this->encode_varint_raw_slow_(value);
242 }
255 void encode_field_raw(uint32_t field_id, uint32_t type) { this->encode_varint_raw((field_id << 3) | type); }
258 template<typename T> void encode_sub_message(uint32_t field_id, const T &value);
261 template<typename T> void encode_optional_sub_message(uint32_t field_id, const T &value);
262
263 // NOLINTBEGIN(readability-identifier-naming)
264 // Non-template core for encode_sub_message — backpatch approach.
265 void encode_sub_message(uint32_t field_id, const void *value,
266 uint8_t *(*encode_fn)(const void *, ProtoWriteBuffer &PROTO_ENCODE_DEBUG_PARAM));
267 // Non-template core for encode_optional_sub_message.
268 void encode_optional_sub_message(uint32_t field_id, uint32_t nested_size, const void *value,
269 uint8_t *(*encode_fn)(const void *, ProtoWriteBuffer &PROTO_ENCODE_DEBUG_PARAM));
270 // NOLINTEND(readability-identifier-naming)
271 APIBuffer *get_buffer() const { return buffer_; }
272 uint8_t *get_pos() const { return pos_; }
273 void set_pos(uint8_t *pos) { pos_ = pos; }
274
275 protected:
276 // Slow path for encode_varint_raw values >= 128, outlined to keep fast path small
277 void encode_varint_raw_slow_(uint32_t value) __attribute__((noinline));
278
279#ifdef ESPHOME_DEBUG_API
280 void debug_check_bounds_(size_t bytes, const char *caller = __builtin_FUNCTION());
281 void debug_check_encode_size_(uint32_t field_id, uint32_t expected, ptrdiff_t actual);
282#else
283 void debug_check_bounds_([[maybe_unused]] size_t bytes) {}
284#endif
285
287 uint8_t *pos_;
288};
289
290// Varint encoding thresholds — used by both proto_encode_* free functions and ProtoSize.
291constexpr uint32_t VARINT_MAX_1_BYTE = 1 << 7; // 128
292constexpr uint32_t VARINT_MAX_2_BYTE = 1 << 14; // 16384
293
299 public:
301 template<typename T>
302 static inline void encode_varint_raw_loop(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, T value) {
303 do {
304 PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
305 *pos++ = static_cast<uint8_t>(value | 0x80);
306 value >>= 7;
307 } while (value > 0x7F);
308 PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
309 *pos++ = static_cast<uint8_t>(value);
310 }
311 static inline void ESPHOME_ALWAYS_INLINE encode_varint_raw(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM,
312 uint32_t value) {
313 if (value < VARINT_MAX_1_BYTE) [[likely]] {
314 PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
315 *pos++ = static_cast<uint8_t>(value);
316 return;
317 }
318 encode_varint_raw_loop(pos PROTO_ENCODE_DEBUG_ARG, value);
319 }
321 static inline void ESPHOME_ALWAYS_INLINE encode_varint_raw_short(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM,
322 uint32_t value) {
323 if (value < VARINT_MAX_1_BYTE) [[likely]] {
324 PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
325 *pos++ = static_cast<uint8_t>(value);
326 return;
327 }
328 if (value < VARINT_MAX_2_BYTE) [[likely]] {
329 PROTO_ENCODE_CHECK_BOUNDS(pos, 2);
330 *pos++ = static_cast<uint8_t>(value | 0x80);
331 *pos++ = static_cast<uint8_t>(value >> 7);
332 return;
333 }
334 encode_varint_raw_loop(pos PROTO_ENCODE_DEBUG_ARG, value);
335 }
336 static inline void ESPHOME_ALWAYS_INLINE encode_varint_raw_64(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM,
337 uint64_t value) {
338 if (value < VARINT_MAX_1_BYTE) [[likely]] {
339 PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
340 *pos++ = static_cast<uint8_t>(value);
341 return;
342 }
343 encode_varint_raw_loop(pos PROTO_ENCODE_DEBUG_ARG, value);
344 }
345 static inline void ESPHOME_ALWAYS_INLINE encode_field_raw(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM,
346 uint32_t field_id, uint32_t type) {
347 encode_varint_raw(pos PROTO_ENCODE_DEBUG_ARG, (field_id << 3) | type);
348 }
350 static inline void ESPHOME_ALWAYS_INLINE write_raw_byte(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM,
351 uint8_t b) {
352 PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
353 *pos++ = b;
354 }
357 static inline void ESPHOME_ALWAYS_INLINE reserve_byte(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM) {
358 PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
359 pos++;
360 }
362 static inline void ESPHOME_ALWAYS_INLINE encode_raw(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM,
363 const void *data, size_t len) {
364 PROTO_ENCODE_CHECK_BOUNDS(pos, len);
365 std::memcpy(pos, data, len);
366 pos += len;
367 }
370 static inline void encode_short_string_force(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint8_t tag,
371 const StringRef &ref) {
372#ifdef ESPHOME_DEBUG_API
373 assert(ref.size() < 128 && "encode_short_string_force: string exceeds max_data_length < 128");
374#endif
375 PROTO_ENCODE_CHECK_BOUNDS(pos, 2 + ref.size());
376 pos[0] = tag;
377 pos[1] = static_cast<uint8_t>(ref.size());
378 std::memcpy(pos + 2, ref.c_str(), ref.size());
379 pos += 2 + ref.size();
380 }
382 static inline void ESPHOME_ALWAYS_INLINE write_tag_and_fixed32(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM,
383 uint8_t tag, uint32_t value) {
384 PROTO_ENCODE_CHECK_BOUNDS(pos, 5);
385 pos[0] = tag;
386#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
387 std::memcpy(pos + 1, &value, 4);
388#else
389 pos[1] = static_cast<uint8_t>(value & 0xFF);
390 pos[2] = static_cast<uint8_t>((value >> 8) & 0xFF);
391 pos[3] = static_cast<uint8_t>((value >> 16) & 0xFF);
392 pos[4] = static_cast<uint8_t>((value >> 24) & 0xFF);
393#endif
394 pos += 5;
395 }
396 static inline void encode_string(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id,
397 const char *string, size_t len, bool force = false) {
398 if (len == 0 && !force)
399 return;
400 encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 2); // type 2: Length-delimited string
401 if (len < VARINT_MAX_1_BYTE) [[likely]] {
402 PROTO_ENCODE_CHECK_BOUNDS(pos, 1 + len);
403 *pos++ = static_cast<uint8_t>(len);
404 } else {
405 encode_varint_raw_loop(pos PROTO_ENCODE_DEBUG_ARG, len);
406 PROTO_ENCODE_CHECK_BOUNDS(pos, len);
407 }
408 std::memcpy(pos, string, len);
409 pos += len;
410 }
411 static inline void encode_string(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id,
412 const std::string &value, bool force = false) {
413 encode_string(pos PROTO_ENCODE_DEBUG_ARG, field_id, value.data(), value.size(), force);
414 }
415 static inline void encode_string(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id,
416 const StringRef &ref, bool force = false) {
417 encode_string(pos PROTO_ENCODE_DEBUG_ARG, field_id, ref.c_str(), ref.size(), force);
418 }
419 static inline void encode_bytes(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id,
420 const uint8_t *data, size_t len, bool force = false) {
421 encode_string(pos PROTO_ENCODE_DEBUG_ARG, field_id, reinterpret_cast<const char *>(data), len, force);
422 }
423 static inline void encode_uint32(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id,
424 uint32_t value, bool force = false) {
425 if (value == 0 && !force)
426 return;
427 encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 0);
428 encode_varint_raw(pos PROTO_ENCODE_DEBUG_ARG, value);
429 }
430 static inline void encode_uint64(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id,
431 uint64_t value, bool force = false) {
432 if (value == 0 && !force)
433 return;
434 encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 0);
435 encode_varint_raw_64(pos PROTO_ENCODE_DEBUG_ARG, value);
436 }
437 static inline void encode_bool(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, bool value,
438 bool force = false) {
439 if (!value && !force)
440 return;
441 encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 0);
442 PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
443 *pos++ = value ? 0x01 : 0x00;
444 }
445 static inline void encode_fixed32(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id,
446 uint32_t value, bool force = false) {
447 if (value == 0 && !force)
448 return;
449 encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 5);
450 PROTO_ENCODE_CHECK_BOUNDS(pos, 4);
451#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
452 std::memcpy(pos, &value, 4);
453 pos += 4;
454#else
455 *pos++ = (value >> 0) & 0xFF;
456 *pos++ = (value >> 8) & 0xFF;
457 *pos++ = (value >> 16) & 0xFF;
458 *pos++ = (value >> 24) & 0xFF;
459#endif
460 }
461 // NOTE: Wire type 1 (64-bit fixed: double, fixed64, sfixed64) is intentionally
462 // not supported to reduce overhead on embedded systems. All ESPHome devices are
463 // 32-bit microcontrollers where 64-bit operations are expensive. If 64-bit support
464 // is needed in the future, the necessary encoding/decoding functions must be added.
465 static inline void encode_float(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, float value,
466 bool force = false) {
467 uint32_t raw = float_to_raw(value);
468 if (raw == 0 && !force)
469 return;
470 encode_fixed32(pos PROTO_ENCODE_DEBUG_ARG, field_id, raw);
471 }
472 static inline void encode_int32(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, int32_t value,
473 bool force = false) {
474 if (value < 0) {
475 // negative int32 is always 10 byte long
476 encode_uint64(pos PROTO_ENCODE_DEBUG_ARG, field_id, static_cast<uint64_t>(value), force);
477 return;
478 }
479 encode_uint32(pos PROTO_ENCODE_DEBUG_ARG, field_id, static_cast<uint32_t>(value), force);
480 }
481 static inline void encode_int64(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, int64_t value,
482 bool force = false) {
483 encode_uint64(pos PROTO_ENCODE_DEBUG_ARG, field_id, static_cast<uint64_t>(value), force);
484 }
485 static inline void encode_sint32(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id,
486 int32_t value, bool force = false) {
487 encode_uint32(pos PROTO_ENCODE_DEBUG_ARG, field_id, encode_zigzag32(value), force);
488 }
489 static inline void encode_sint64(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id,
490 int64_t value, bool force = false) {
491 encode_uint64(pos PROTO_ENCODE_DEBUG_ARG, field_id, encode_zigzag64(value), force);
492 }
494 template<typename T>
495 static inline void encode_sub_message(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, ProtoWriteBuffer &buffer,
496 uint32_t field_id, const T &value) {
497 buffer.set_pos(pos);
498 buffer.encode_sub_message(field_id, value);
499 pos = buffer.get_pos();
500 }
501 template<typename T>
502 static inline void encode_optional_sub_message(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM,
503 ProtoWriteBuffer &buffer, uint32_t field_id, const T &value) {
504 buffer.set_pos(pos);
505 buffer.encode_optional_sub_message(field_id, value);
506 pos = buffer.get_pos();
507 }
508};
509
510#ifdef HAS_PROTO_MESSAGE_DUMP
517 public:
518 // Matches default tx_buffer_size in logger component
519 static constexpr size_t CAPACITY = 512;
520
521 DumpBuffer() : pos_(0) { buf_[0] = '\0'; }
522
523 DumpBuffer &append(const char *str) {
524 if (str) {
525 append_impl_(str, strlen(str));
526 }
527 return *this;
528 }
529
530 DumpBuffer &append(const char *str, size_t len) {
531 append_impl_(str, len);
532 return *this;
533 }
534
535 DumpBuffer &append(size_t n, char c) {
536 size_t space = CAPACITY - 1 - pos_;
537 if (n > space)
538 n = space;
539 if (n > 0) {
540 memset(buf_ + pos_, c, n);
541 pos_ += n;
542 buf_[pos_] = '\0';
543 }
544 return *this;
545 }
546
548 DumpBuffer &append_p(const char *str) {
549 if (str) {
550#ifdef USE_ESP8266
551 append_p_esp8266(str);
552#else
553 append_impl_(str, strlen(str));
554#endif
555 }
556 return *this;
557 }
558
559#ifdef USE_ESP8266
561 void append_p_esp8266(const char *str);
562#endif
563
564 const char *c_str() const { return buf_; }
565 size_t size() const { return pos_; }
566
568 char *data() { return buf_; }
570 size_t pos() const { return pos_; }
572 void set_pos(size_t pos) {
573 if (pos >= CAPACITY) {
574 pos_ = CAPACITY - 1;
575 } else {
576 pos_ = pos;
577 }
578 buf_[pos_] = '\0';
579 }
580
581 private:
582 void append_impl_(const char *str, size_t len) {
583 size_t space = CAPACITY - 1 - pos_;
584 if (len > space)
585 len = space;
586 if (len > 0) {
587 memcpy(buf_ + pos_, str, len);
588 pos_ += len;
589 buf_[pos_] = '\0';
590 }
591 }
592
593 char buf_[CAPACITY];
594 size_t pos_;
595};
596#endif
597
599 public:
600 // Non-virtual defaults for messages with no fields.
601 // Concrete message classes hide these with their own implementations.
602 // All call sites use templates to preserve the concrete type, so virtual
603 // dispatch is not needed. This eliminates per-message vtable entries for
604 // encode/calculate_size, saving ~1.3 KB of flash across all message types.
605 uint8_t *encode(ProtoWriteBuffer &buffer PROTO_ENCODE_DEBUG_PARAM) const { return buffer.get_pos(); }
606 uint32_t calculate_size() const { return 0; }
607#ifdef HAS_PROTO_MESSAGE_DUMP
608 virtual const char *dump_to(DumpBuffer &out) const = 0;
609 virtual const LogString *message_name() const { return LOG_STR("unknown"); }
610#endif
611
612#ifndef USE_HOST
613 protected:
614#endif
615 // Non-virtual destructor is protected to prevent polymorphic deletion.
616 // On host platform, made public to allow value-initialization of std::array
617 // members (e.g. DeviceInfoResponse::devices) without clang errors.
618 ~ProtoMessage() = default;
619};
620
621// Base class for messages that support decoding
623 public:
624 void decode(const uint8_t *buffer, size_t length);
625
635 static uint32_t count_repeated_field(const uint8_t *buffer, size_t length, uint32_t target_field_id);
636
637 protected:
639 virtual bool decode_varint(uint32_t field_id, proto_varint_value_t value) { return false; }
640 virtual bool decode_length(uint32_t field_id, ProtoLengthDelimited value) { return false; }
641 virtual bool decode_32bit(uint32_t field_id, Proto32Bit value) { return false; }
642 // NOTE: decode_64bit removed - wire type 1 not supported
643};
644
646 public:
647 // Varint encoding thresholds — use namespace-level constants for 1/2 byte,
648 // class-level for 3/4 byte (only used within ProtoSize).
651 static constexpr uint32_t VARINT_THRESHOLD_3_BYTE = 1 << 21; // 2097152
652 static constexpr uint32_t VARINT_THRESHOLD_4_BYTE = 1 << 28; // 268435456
653
654 // Varint encoded length for a 16-bit value (1, 2, or 3 bytes).
655 // Fully inline — no slow path call for values >= 128.
656 static constexpr inline uint8_t ESPHOME_ALWAYS_INLINE varint16(uint16_t value) {
657 return value < VARINT_THRESHOLD_1_BYTE ? 1 : (value < VARINT_THRESHOLD_2_BYTE ? 2 : 3);
658 }
659
660 // Varint encoded length for an 8-bit value (1 or 2 bytes).
661 static constexpr inline uint8_t ESPHOME_ALWAYS_INLINE varint8(uint8_t value) {
662 return value < VARINT_THRESHOLD_1_BYTE ? 1 : 2;
663 }
664
671 static constexpr inline uint32_t ESPHOME_ALWAYS_INLINE varint(uint32_t value) {
672 if (value < VARINT_THRESHOLD_1_BYTE) [[likely]]
673 return 1; // Fast path: 7 bits, most common case
674 if (__builtin_is_constant_evaluated())
675 return varint_wide(value);
676 return varint_slow(value);
677 }
680 static constexpr inline uint32_t ESPHOME_ALWAYS_INLINE varint_short(uint32_t value) {
681 if (value < VARINT_THRESHOLD_1_BYTE) [[likely]]
682 return 1;
683 if (value < VARINT_THRESHOLD_2_BYTE) [[likely]]
684 return 2;
685 if (__builtin_is_constant_evaluated())
686 return varint_wide(value);
687 return varint_slow(value);
688 }
689
690 private:
691 // Slow path for varint >= 128, outlined to keep fast path small
692 static uint32_t varint_slow(uint32_t value) __attribute__((noinline));
693 // Shared cascade for values >= 128 (used by both constexpr and noinline paths)
694 static constexpr inline uint32_t ESPHOME_ALWAYS_INLINE varint_wide(uint32_t value) {
695 if (value < VARINT_THRESHOLD_2_BYTE)
696 return 2;
697 if (value < VARINT_THRESHOLD_3_BYTE)
698 return 3;
699 if (value < VARINT_THRESHOLD_4_BYTE)
700 return 4;
701 return 5;
702 }
703
704 public:
711 static constexpr uint32_t varint(uint64_t value) {
712 // Handle common case of values fitting in uint32_t (vast majority of use cases)
713 if (value <= UINT32_MAX) {
714 return varint(static_cast<uint32_t>(value));
715 }
716
717 // For larger values, determine size based on highest bit position
718 if (value < (1ULL << 35)) {
719 return 5; // 35 bits
720 } else if (value < (1ULL << 42)) {
721 return 6; // 42 bits
722 } else if (value < (1ULL << 49)) {
723 return 7; // 49 bits
724 } else if (value < (1ULL << 56)) {
725 return 8; // 56 bits
726 } else if (value < (1ULL << 63)) {
727 return 9; // 63 bits
728 } else {
729 return 10; // 64 bits (maximum for uint64_t)
730 }
731 }
732
742 static constexpr uint32_t varint(int32_t value) {
743 // Negative values are sign-extended to 64 bits in protocol buffers,
744 // which always results in a 10-byte varint for negative int32
745 if (value < 0) {
746 return 10; // Negative int32 is always 10 bytes long
747 }
748 // For non-negative values, use the uint32_t implementation
749 return varint(static_cast<uint32_t>(value));
750 }
751
758 static constexpr uint32_t varint(int64_t value) {
759 // For int64_t, we convert to uint64_t and calculate the size
760 // This works because the bit pattern determines the encoding size,
761 // and we've handled negative int32 values as a special case above
762 return varint(static_cast<uint64_t>(value));
763 }
764
772 static constexpr uint32_t field(uint32_t field_id, uint32_t type) {
773 uint32_t tag = (field_id << 3) | (type & WIRE_TYPE_MASK);
774 return varint(tag);
775 }
776
777 // Static methods that RETURN size contribution (no ProtoSize object needed).
778 // Used by generated calculate_size() methods to accumulate into a plain uint32_t register.
779 static constexpr uint32_t calc_int32(uint32_t field_id_size, int32_t value) {
780 return value ? field_id_size + (value < 0 ? 10 : varint(static_cast<uint32_t>(value))) : 0;
781 }
782 static constexpr uint32_t calc_int32_force(uint32_t field_id_size, int32_t value) {
783 return field_id_size + (value < 0 ? 10 : varint(static_cast<uint32_t>(value)));
784 }
785 static constexpr uint32_t calc_uint32(uint32_t field_id_size, uint32_t value) {
786 return value ? field_id_size + varint(value) : 0;
787 }
788 static constexpr uint32_t calc_uint32_force(uint32_t field_id_size, uint32_t value) {
789 return field_id_size + varint(value);
790 }
791 static constexpr uint32_t calc_bool(uint32_t field_id_size, bool value) { return value ? field_id_size + 1 : 0; }
792 static constexpr uint32_t calc_bool_force(uint32_t field_id_size) { return field_id_size + 1; }
793 static uint32_t calc_float(uint32_t field_id_size, float value) {
794 return float_to_raw(value) != 0 ? field_id_size + 4 : 0;
795 }
796 static constexpr uint32_t calc_fixed32(uint32_t field_id_size, uint32_t value) {
797 return value ? field_id_size + 4 : 0;
798 }
799 static constexpr uint32_t calc_sfixed32(uint32_t field_id_size, int32_t value) {
800 return value ? field_id_size + 4 : 0;
801 }
802 static constexpr uint32_t calc_sint32(uint32_t field_id_size, int32_t value) {
803 return value ? field_id_size + varint_short(encode_zigzag32(value)) : 0;
804 }
805 static constexpr inline uint32_t ESPHOME_ALWAYS_INLINE calc_sint32_force(uint32_t field_id_size, int32_t value) {
806 return field_id_size + varint_short(encode_zigzag32(value));
807 }
808 static constexpr uint32_t calc_int64(uint32_t field_id_size, int64_t value) {
809 return value ? field_id_size + varint(value) : 0;
810 }
811 static constexpr uint32_t calc_int64_force(uint32_t field_id_size, int64_t value) {
812 return field_id_size + varint(value);
813 }
814 static constexpr uint32_t calc_uint64(uint32_t field_id_size, uint64_t value) {
815 return value ? field_id_size + varint(value) : 0;
816 }
817 static constexpr inline uint32_t ESPHOME_ALWAYS_INLINE calc_uint64_force(uint32_t field_id_size, uint64_t value) {
818 return field_id_size + varint(value);
819 }
820 static constexpr uint32_t calc_length(uint32_t field_id_size, size_t len) {
821 return len ? field_id_size + varint(static_cast<uint32_t>(len)) + static_cast<uint32_t>(len) : 0;
822 }
823 static constexpr inline uint32_t ESPHOME_ALWAYS_INLINE calc_length_force(uint32_t field_id_size, size_t len) {
824 return field_id_size + varint(static_cast<uint32_t>(len)) + static_cast<uint32_t>(len);
825 }
826 static constexpr uint32_t calc_sint64(uint32_t field_id_size, int64_t value) {
827 return value ? field_id_size + varint(encode_zigzag64(value)) : 0;
828 }
829 static constexpr uint32_t calc_sint64_force(uint32_t field_id_size, int64_t value) {
830 return field_id_size + varint(encode_zigzag64(value));
831 }
832 static constexpr uint32_t calc_fixed64(uint32_t field_id_size, uint64_t value) {
833 return value ? field_id_size + 8 : 0;
834 }
835 static constexpr uint32_t calc_sfixed64(uint32_t field_id_size, int64_t value) {
836 return value ? field_id_size + 8 : 0;
837 }
838 static constexpr uint32_t calc_message(uint32_t field_id_size, uint32_t nested_size) {
839 return nested_size ? field_id_size + varint(nested_size) + nested_size : 0;
840 }
841 static constexpr inline uint32_t ESPHOME_ALWAYS_INLINE calc_message_force(uint32_t field_id_size,
842 uint32_t nested_size) {
843 return field_id_size + varint(nested_size) + nested_size;
844 }
845};
846
847// Implementation of methods that depend on ProtoSize being fully defined
848
849// Encode thunk — converts void* back to concrete type for direct encode() call
850template<typename T> uint8_t *proto_encode_msg(const void *msg, ProtoWriteBuffer &buf PROTO_ENCODE_DEBUG_PARAM) {
851 return static_cast<const T *>(msg)->encode(buf PROTO_ENCODE_DEBUG_ARG);
852}
853
854// Thin template wrapper; delegates to non-template core in proto.cpp.
855template<typename T> inline void ProtoWriteBuffer::encode_sub_message(uint32_t field_id, const T &value) {
856 this->encode_sub_message(field_id, &value, &proto_encode_msg<T>);
857}
858
859// Thin template wrapper; delegates to non-template core.
860template<typename T> inline void ProtoWriteBuffer::encode_optional_sub_message(uint32_t field_id, const T &value) {
861 this->encode_optional_sub_message(field_id, value.calculate_size(), &value, &proto_encode_msg<T>);
862}
863
864// Template decode_to_message - preserves concrete type so decode() resolves statically
865template<typename T> void ProtoLengthDelimited::decode_to_message(T &msg) const {
866 msg.decode(this->value_, this->length_);
867}
868
869template<typename T> const char *proto_enum_to_string(T value);
870
871// ProtoService removed — its methods were inlined into APIConnection.
872// APIConnection is the concrete server-side implementation; the extra virtual layer was unnecessary.
873
874} // namespace esphome::api
uint8_t raw[35]
Definition bl0939.h:0
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
constexpr const char * c_str() const
Definition string_ref.h:73
constexpr size_type size() const
Definition string_ref.h:74
Byte buffer that skips zero-initialization on resize().
Definition api_buffer.h:36
Fixed-size buffer for message dumps - avoids heap allocation.
Definition proto.h:516
const char * c_str() const
Definition proto.h:564
DumpBuffer & append(size_t n, char c)
Definition proto.h:535
size_t size() const
Definition proto.h:565
void append_p_esp8266(const char *str)
Out-of-line ESP8266 PROGMEM append to avoid inlining strlen_P/memcpy_P at every call site.
DumpBuffer & append_p(const char *str)
Append a PROGMEM string (flash-safe on ESP8266, regular append on other platforms)
Definition proto.h:548
size_t pos() const
Get current position for use with buf_append_printf.
Definition proto.h:570
static constexpr size_t CAPACITY
Definition proto.h:519
DumpBuffer & append(const char *str, size_t len)
Definition proto.h:530
DumpBuffer & append(const char *str)
Definition proto.h:523
char * data()
Get writable buffer pointer for use with buf_append_printf.
Definition proto.h:568
void set_pos(size_t pos)
Update position after buf_append_printf call.
Definition proto.h:572
uint32_t as_fixed32() const
Definition proto.h:194
int32_t as_sfixed32() const
Definition proto.h:195
float as_float() const
Definition proto.h:196
const uint32_t value_
Definition proto.h:206
Proto32Bit(uint32_t value)
Definition proto.h:193
virtual bool decode_32bit(uint32_t field_id, Proto32Bit value)
Definition proto.h:641
virtual bool decode_varint(uint32_t field_id, proto_varint_value_t value)
Definition proto.h:639
void decode(const uint8_t *buffer, size_t length)
Definition proto.cpp:213
virtual bool decode_length(uint32_t field_id, ProtoLengthDelimited value)
Definition proto.h:640
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.
Definition proto.cpp:60
Static encode helpers for generated encode() functions.
Definition proto.h:298
static void encode_bool(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, bool value, bool force=false)
Definition proto.h:437
static void encode_string(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, const char *string, size_t len, bool force=false)
Definition proto.h:396
static void encode_sint32(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, int32_t value, bool force=false)
Definition proto.h:485
static void ESPHOME_ALWAYS_INLINE encode_varint_raw_64(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint64_t value)
Definition proto.h:336
static void ESPHOME_ALWAYS_INLINE encode_varint_raw_short(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t value)
Encode a varint that is expected to be 1-2 bytes (e.g. zigzag RSSI, small lengths).
Definition proto.h:321
static void encode_string(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, const StringRef &ref, bool force=false)
Definition proto.h:415
static void encode_int64(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, int64_t value, bool force=false)
Definition proto.h:481
static void encode_varint_raw_loop(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, T value)
Write a multi-byte varint directly through a pos pointer.
Definition proto.h:302
static void ESPHOME_ALWAYS_INLINE write_raw_byte(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint8_t b)
Write a single precomputed tag byte. Tag must be < 128.
Definition proto.h:350
static void encode_float(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, float value, bool force=false)
Definition proto.h:465
static void encode_short_string_force(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint8_t tag, const StringRef &ref)
Encode tag + 1-byte length + raw string data.
Definition proto.h:370
static void ESPHOME_ALWAYS_INLINE write_tag_and_fixed32(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint8_t tag, uint32_t value)
Write a precomputed tag byte + 32-bit value in one operation.
Definition proto.h:382
static void encode_optional_sub_message(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, ProtoWriteBuffer &buffer, uint32_t field_id, const T &value)
Definition proto.h:502
static void encode_sub_message(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, ProtoWriteBuffer &buffer, uint32_t field_id, const T &value)
Sub-message encoding: sync pos to buffer, delegate, get pos from return value.
Definition proto.h:495
static void ESPHOME_ALWAYS_INLINE reserve_byte(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM)
Reserve one byte for later backpatch (e.g., sub-message length).
Definition proto.h:357
static void encode_sint64(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, int64_t value, bool force=false)
Definition proto.h:489
static void ESPHOME_ALWAYS_INLINE encode_raw(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, const void *data, size_t len)
Write raw bytes to the buffer (no tag, no length prefix).
Definition proto.h:362
static void encode_fixed32(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, uint32_t value, bool force=false)
Definition proto.h:445
static void ESPHOME_ALWAYS_INLINE encode_field_raw(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, uint32_t type)
Definition proto.h:345
static void ESPHOME_ALWAYS_INLINE encode_varint_raw(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t value)
Definition proto.h:311
static void encode_uint64(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, uint64_t value, bool force=false)
Definition proto.h:430
static void encode_int32(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, int32_t value, bool force=false)
Definition proto.h:472
static void encode_bytes(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, const uint8_t *data, size_t len, bool force=false)
Definition proto.h:419
static void encode_uint32(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, uint32_t value, bool force=false)
Definition proto.h:423
static void encode_string(uint8_t *__restrict__ &pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, const std::string &value, bool force=false)
Definition proto.h:411
void decode_to_message(T &msg) const
Decode the length-delimited data into a message instance.
Definition proto.h:865
const uint8_t *const value_
Definition proto.h:187
const uint8_t * data() const
Definition proto.h:179
ProtoLengthDelimited(const uint8_t *value, size_t length)
Definition proto.h:175
std::string as_string() const
Definition proto.h:176
virtual const LogString * message_name() const
Definition proto.h:609
uint8_t * encode(ProtoWriteBuffer &buffer PROTO_ENCODE_DEBUG_PARAM) const
Definition proto.h:605
uint32_t calculate_size() const
Definition proto.h:606
virtual const char * dump_to(DumpBuffer &out) const =0
static constexpr uint32_t calc_uint64(uint32_t field_id_size, uint64_t value)
Definition proto.h:814
static constexpr uint32_t calc_int64(uint32_t field_id_size, int64_t value)
Definition proto.h:808
static constexpr uint8_t ESPHOME_ALWAYS_INLINE varint8(uint8_t value)
Definition proto.h:661
static constexpr uint32_t calc_bool(uint32_t field_id_size, bool value)
Definition proto.h:791
static constexpr uint32_t calc_message(uint32_t field_id_size, uint32_t nested_size)
Definition proto.h:838
static constexpr uint32_t calc_sint32(uint32_t field_id_size, int32_t value)
Definition proto.h:802
static constexpr uint32_t calc_sint64_force(uint32_t field_id_size, int64_t value)
Definition proto.h:829
static constexpr uint32_t ESPHOME_ALWAYS_INLINE calc_uint64_force(uint32_t field_id_size, uint64_t value)
Definition proto.h:817
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.
Definition proto.h:772
static constexpr uint32_t ESPHOME_ALWAYS_INLINE varint(uint32_t value)
Calculates the size in bytes needed to encode a uint32_t value as a varint.
Definition proto.h:671
static constexpr uint32_t calc_uint32_force(uint32_t field_id_size, uint32_t value)
Definition proto.h:788
static constexpr uint32_t varint(int32_t value)
Calculates the size in bytes needed to encode an int32_t value as a varint.
Definition proto.h:742
static constexpr uint32_t calc_int64_force(uint32_t field_id_size, int64_t value)
Definition proto.h:811
static constexpr uint32_t calc_sfixed64(uint32_t field_id_size, int64_t value)
Definition proto.h:835
static constexpr uint32_t calc_uint32(uint32_t field_id_size, uint32_t value)
Definition proto.h:785
static constexpr uint32_t VARINT_THRESHOLD_3_BYTE
Definition proto.h:651
static constexpr uint8_t ESPHOME_ALWAYS_INLINE varint16(uint16_t value)
Definition proto.h:656
static constexpr uint32_t calc_length(uint32_t field_id_size, size_t len)
Definition proto.h:820
static constexpr uint32_t calc_int32(uint32_t field_id_size, int32_t value)
Definition proto.h:779
static constexpr uint32_t calc_fixed64(uint32_t field_id_size, uint64_t value)
Definition proto.h:832
static constexpr uint32_t ESPHOME_ALWAYS_INLINE varint_short(uint32_t value)
Size of a varint expected to be 1-2 bytes (e.g.
Definition proto.h:680
static constexpr uint32_t varint(int64_t value)
Calculates the size in bytes needed to encode an int64_t value as a varint.
Definition proto.h:758
static constexpr uint32_t ESPHOME_ALWAYS_INLINE calc_length_force(uint32_t field_id_size, size_t len)
Definition proto.h:823
static constexpr uint32_t calc_bool_force(uint32_t field_id_size)
Definition proto.h:792
static constexpr uint32_t calc_sfixed32(uint32_t field_id_size, int32_t value)
Definition proto.h:799
static constexpr uint32_t varint(uint64_t value)
Calculates the size in bytes needed to encode a uint64_t value as a varint.
Definition proto.h:711
static uint32_t calc_float(uint32_t field_id_size, float value)
Definition proto.h:793
static constexpr uint32_t calc_sint64(uint32_t field_id_size, int64_t value)
Definition proto.h:826
static constexpr uint32_t calc_int32_force(uint32_t field_id_size, int32_t value)
Definition proto.h:782
static constexpr uint32_t calc_fixed32(uint32_t field_id_size, uint32_t value)
Definition proto.h:796
static constexpr uint32_t VARINT_THRESHOLD_2_BYTE
Definition proto.h:650
static constexpr uint32_t ESPHOME_ALWAYS_INLINE calc_message_force(uint32_t field_id_size, uint32_t nested_size)
Definition proto.h:841
static constexpr uint32_t ESPHOME_ALWAYS_INLINE calc_sint32_force(uint32_t field_id_size, int32_t value)
Definition proto.h:805
static constexpr uint32_t VARINT_THRESHOLD_1_BYTE
Definition proto.h:649
static constexpr uint32_t VARINT_THRESHOLD_4_BYTE
Definition proto.h:652
Static varint parsing methods for the protobuf wire format.
Definition proto.h:136
static ProtoVarIntResult ESPHOME_ALWAYS_INLINE parse_non_empty(const uint8_t *buffer, uint32_t len)
Parse a varint from buffer.
Definition proto.h:140
static ProtoVarIntResult parse_wide(const uint8_t *buffer, uint32_t len, uint32_t result32) __attribute__((noinline))
Continue parsing varint bytes 4-9 with 64-bit arithmetic.
Definition proto.cpp:46
static ProtoVarIntResult parse_slow(const uint8_t *buffer, uint32_t len) __attribute__((noinline))
Definition proto.cpp:23
static ProtoVarIntResult ESPHOME_ALWAYS_INLINE parse(const uint8_t *buffer, uint32_t len)
Parse a varint from buffer (safe for empty buffers).
Definition proto.h:153
void set_pos(uint8_t *pos)
Definition proto.h:273
void debug_check_encode_size_(uint32_t field_id, uint32_t expected, ptrdiff_t actual)
Definition proto.cpp:205
void debug_check_bounds_(size_t bytes)
Definition proto.h:283
void encode_varint_raw_slow_(uint32_t value) __attribute__((noinline))
Definition proto.cpp:13
void encode_field_raw(uint32_t field_id, uint32_t type)
Encode a field key (tag/wire type combination).
Definition proto.h:255
void encode_optional_sub_message(uint32_t field_id, const T &value)
Encode an optional singular submessage field — skips if empty.
Definition proto.h:860
void encode_sub_message(uint32_t field_id, const T &value)
Single-pass encode for repeated submessage elements.
Definition proto.h:855
void debug_check_bounds_(size_t bytes, const char *caller=__builtin_FUNCTION())
Definition proto.cpp:198
uint8_t * get_pos() const
Definition proto.h:272
APIBuffer * get_buffer() const
Definition proto.h:271
void ESPHOME_ALWAYS_INLINE encode_varint_raw(uint32_t value)
Definition proto.h:235
ProtoWriteBuffer(APIBuffer *buffer, size_t write_pos)
Definition proto.h:234
ProtoWriteBuffer(APIBuffer *buffer)
Definition proto.h:233
struct @65::@66 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:32
uint16_t type
mopeka_std_values val[3]
constexpr uint32_t encode_zigzag32(int32_t value)
Definition proto.h:42
constexpr uint32_t VARINT_MAX_2_BYTE
Definition proto.h:292
constexpr uint8_t WIRE_TYPE_VARINT
Definition proto.h:23
uint64_t proto_varint_value_t
Type used for decoded varint values - uint64_t when BLE needs 64-bit addresses, uint32_t otherwise.
Definition proto.h:118
uint8_t * proto_encode_msg(const void *msg, ProtoWriteBuffer &buf PROTO_ENCODE_DEBUG_PARAM)
Definition proto.h:850
const char * proto_enum_to_string(T value)
constexpr uint32_t VARINT_MAX_1_BYTE
Definition proto.h:291
constexpr uint64_t encode_zigzag64(int64_t value)
Definition proto.h:46
constexpr uint8_t WIRE_TYPE_MASK
Definition proto.h:26
constexpr uint32_t PROTO_VARINT_PARSE_FAILED
Sentinel value for consumed field indicating parse failure.
Definition proto.h:124
constexpr uint8_t WIRE_TYPE_LENGTH_DELIMITED
Definition proto.h:24
constexpr int32_t decode_zigzag32(uint32_t value)
Definition proto.h:50
constexpr uint8_t WIRE_TYPE_FIXED32
Definition proto.h:25
uint32_t float_to_raw(float value)
Definition proto.h:32
constexpr int64_t decode_zigzag64(uint64_t value)
Definition proto.h:54
void encode_varint_to_buffer(uint32_t val, uint8_t *buffer)
Encode a varint directly into a pre-allocated buffer.
Definition proto.h:78
uint16_t count_packed_varints(const uint8_t *data, size_t len)
Count number of varints in a packed buffer.
Definition proto.h:59
void proto_check_bounds_failed(const uint8_t *pos, size_t bytes, const uint8_t *end, const char *caller)
Definition proto.cpp:194
const char * tag
Definition log.h:74
std::string size_t len
Definition helpers.h:1045
uint16_t size
Definition helpers.cpp:25
size_t size_t pos
Definition helpers.h:1082
static void uint32_t
Result of parsing a varint: value + number of bytes consumed.
Definition proto.h:128
constexpr bool has_value() const
Definition proto.h:132
proto_varint_value_t value
Definition proto.h:129
uint8_t end[39]
Definition sun_gtil2.cpp:17
uint16_t length
Definition tt21100.cpp:0