ESPHome 2026.10.2
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"
6#include "esphome/core/hal.h"
8#include "esphome/core/log.h"
11
12#include <cassert>
13#include <cstring>
14#include <type_traits>
15#include <vector>
16
17#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
18#define HAS_PROTO_MESSAGE_DUMP
19#endif
20
21namespace esphome::api {
22
23// Protocol Buffer wire type constants
24// See https://protobuf.dev/programming-guides/encoding/#structure
25constexpr uint8_t WIRE_TYPE_VARINT = 0; // int32, int64, uint32, uint64, sint32, sint64, bool, enum
26constexpr uint8_t WIRE_TYPE_LENGTH_DELIMITED = 2; // string, bytes, embedded messages, packed repeated fields
27constexpr uint8_t WIRE_TYPE_FIXED32 = 5; // fixed32, sfixed32, float
28constexpr uint8_t WIRE_TYPE_MASK = 0b111; // Mask to extract wire type from tag
29
30// Reinterpret float bits as uint32_t without floating-point comparison.
31// Used by both encode_float() and calc_float() to ensure identical zero checks.
32// Uses union type-punning which is a GCC/Clang extension (not standard C++),
33// but bit_cast/memcpy don't optimize to a no-op on xtensa-gcc (ESP8266).
34inline uint32_t float_to_raw(float value) {
35 union {
36 float f;
37 uint32_t u;
38 } v;
39 v.f = value;
40 return v.u;
41}
42
43// Helper functions for ZigZag encoding/decoding
44inline constexpr uint32_t encode_zigzag32(int32_t value) {
45 return (static_cast<uint32_t>(value) << 1) ^ (static_cast<uint32_t>(value >> 31));
46}
47
48inline constexpr uint64_t encode_zigzag64(int64_t value) {
49 return (static_cast<uint64_t>(value) << 1) ^ (static_cast<uint64_t>(value >> 63));
50}
51
52inline constexpr int32_t decode_zigzag32(uint32_t value) {
53 return (value & 1) ? static_cast<int32_t>(~(value >> 1)) : static_cast<int32_t>(value >> 1);
54}
55
56inline constexpr int64_t decode_zigzag64(uint64_t value) {
57 return (value & 1) ? static_cast<int64_t>(~(value >> 1)) : static_cast<int64_t>(value >> 1);
58}
59
62template<typename Word = size_t> inline uint16_t count_packed_varints(const uint8_t *data, size_t len) {
63 constexpr size_t word_size = sizeof(Word);
64 constexpr Word lane_ones = ~Word{0} / 0xFF; // 0x01..01
65 const uint8_t *end = data + len;
66 size_t continuations = 0;
67 while (data != end) {
68 // Unaligned word loads fault on Xtensa
69 if ((reinterpret_cast<uintptr_t>(data) & (word_size - 1)) == 0 && static_cast<size_t>(end - data) >= word_size) {
70 Word word;
71 memcpy(&word, __builtin_assume_aligned(data, word_size), word_size);
72 continuations += (((word >> 7) & lane_ones) * lane_ones) >> (word_size * 8 - 8);
73 data += word_size;
74 } else {
75 continuations += *data++ >> 7;
76 }
77 }
78 return static_cast<uint16_t>(len - continuations);
79}
80
84 while (val > 0x7F) {
85 *buffer++ = static_cast<uint8_t>(val | 0x80);
86 val >>= 7;
87 }
88 *buffer = static_cast<uint8_t>(val);
89}
90
91/*
92 * StringRef Ownership Model for API Protocol Messages
93 * ===================================================
94 *
95 * StringRef is used for zero-copy string handling in outgoing (SOURCE_SERVER) messages.
96 * It holds a pointer and length to existing string data without copying.
97 *
98 * CRITICAL: The referenced string data MUST remain valid until message encoding completes.
99 *
100 * Safe StringRef Patterns:
101 * 1. String literals: StringRef("literal") - Always safe (static storage duration)
102 * 2. Member variables: StringRef(this->member_string_) - Safe if object outlives encoding
103 * 3. Global/static strings: StringRef(GLOBAL_CONSTANT) - Always safe
104 * 4. Local variables: Safe ONLY if encoding happens before function returns:
105 * std::string temp = compute_value();
106 * msg.field = StringRef(temp);
107 * return this->send_message(msg); // temp is valid during encoding
108 *
109 * Unsafe Patterns (WILL cause crashes/corruption):
110 * 1. Temporaries: msg.field = StringRef(obj.get_string()) // get_string() returns by value
111 * 2. Concatenation: msg.field = StringRef(str1 + str2) // Result is temporary
112 *
113 * For unsafe patterns, store in a local variable first:
114 * std::string temp = get_string(); // or str1 + str2
115 * msg.field = StringRef(temp);
116 *
117 * The send_*_response pattern ensures proper lifetime management by encoding
118 * within the same function scope where temporaries are created.
119 */
120
122#ifdef USE_API_VARINT64
123using proto_varint_value_t = uint64_t;
124#else
126#endif
127
130
135 uint32_t consumed; // PROTO_VARINT_PARSE_FAILED = parse failed
136
137 constexpr bool has_value() const { return this->consumed != PROTO_VARINT_PARSE_FAILED; }
138};
139
142 public:
145 static inline ProtoVarIntResult ESPHOME_ALWAYS_INLINE parse_non_empty(const uint8_t *buffer, uint32_t len) {
146#ifdef ESPHOME_DEBUG_API
147 assert(len > 0);
148#endif
149 // Fast path: single-byte varints (0-127) are the most common case
150 // (booleans, small enums, field tags, small message sizes/types).
151 if ((buffer[0] & 0x80) == 0) [[likely]]
152 return {buffer[0], 1};
153 return parse_slow(buffer, len);
154 }
155
158 static inline ProtoVarIntResult ESPHOME_ALWAYS_INLINE parse(const uint8_t *buffer, uint32_t len) {
159 if (len == 0)
160 return {0, PROTO_VARINT_PARSE_FAILED};
161 return parse_non_empty(buffer, len);
162 }
163
164 protected:
165 // Slow path for multi-byte varints (>= 128), outlined to keep fast path small
166 static ProtoVarIntResult parse_slow(const uint8_t *buffer, uint32_t len) __attribute__((noinline));
167
168#ifdef USE_API_VARINT64
170 static ProtoVarIntResult parse_wide(const uint8_t *buffer, uint32_t len, uint32_t result32) __attribute__((noinline));
171#endif
172};
173
174// Forward declarations for encoding helpers
175class ProtoMessage;
176class ProtoSize;
177
180constexpr uint32_t proto_tag(uint32_t field_id, uint32_t wire_type) { return (field_id << 3) | wire_type; }
181
185 public:
186 ProtoFieldValue(const uint8_t *data, proto_varint_value_t scalar) : data_(data), scalar_(scalar) {}
187
188 proto_varint_value_t as_varint() const { return this->scalar_; }
189 // A bool is sent as 0 or 1, so the low word is enough and saves a second compare with 64 bit varints
190 bool as_bool() const { return static_cast<uint32_t>(this->scalar_) != 0; }
191
192 // Length-delimited accessors
193 const uint8_t *data() const { return this->data_; }
194 size_t size() const { return static_cast<size_t>(this->scalar_); }
195 std::string as_string() const { return std::string(reinterpret_cast<const char *>(this->data_), this->size()); }
198 template<typename T> void decode_to_message(T &msg) const { msg.decode(this->data_, this->size()); }
199
200 // Fixed32 accessors
201 uint32_t as_fixed32() const { return static_cast<uint32_t>(this->scalar_); }
202 int32_t as_sfixed32() const { return static_cast<int32_t>(this->as_fixed32()); }
203 float as_float() const {
204 union {
206 float value;
207 } s{};
208 s.raw = this->as_fixed32();
209 return s.value;
210 }
211
212 private:
213 const uint8_t *data_;
214 proto_varint_value_t scalar_;
215};
216
217// NOTE: Proto64Bit class removed - wire type 1 (64-bit fixed) not supported
218
219// Debug bounds checking for proto encode functions.
220// In debug mode (ESPHOME_DEBUG_API), an extra end-of-buffer pointer is threaded
221// through the entire encode chain. In production, these expand to nothing.
222#ifdef ESPHOME_DEBUG_API
223#define PROTO_ENCODE_DEBUG_PARAM , uint8_t *proto_debug_end_
224#define PROTO_ENCODE_DEBUG_ARG , proto_debug_end_
225#define PROTO_ENCODE_DEBUG_INIT(buf) , (buf)->data() + (buf)->size()
226#define PROTO_ENCODE_CHECK_BOUNDS(pos, n) \
227 do { \
228 if ((pos) + (n) > proto_debug_end_) \
229 proto_check_bounds_failed(pos, n, proto_debug_end_, __builtin_FUNCTION()); \
230 } while (0)
231void proto_check_bounds_failed(const uint8_t *pos, size_t bytes, const uint8_t *end, const char *caller);
234void proto_check_encode_end(const uint8_t *end, const uint8_t *expected);
236void proto_check_sub_message_size(uint32_t field_id, uint32_t expected, const uint8_t *len_pos, const uint8_t *end);
237#else
238#define PROTO_ENCODE_DEBUG_PARAM
239#define PROTO_ENCODE_DEBUG_ARG
240#define PROTO_ENCODE_DEBUG_INIT(buf)
241#define PROTO_ENCODE_CHECK_BOUNDS(pos, n)
242#endif
243
245 public:
247 ProtoWriteBuffer(APIBuffer *buffer, size_t write_pos) : buffer_(buffer), pos_(buffer->data() + write_pos) {}
248 inline void ESPHOME_ALWAYS_INLINE encode_varint_raw(uint32_t value) {
249 if (value < 128) [[likely]] {
250 this->debug_check_bounds_(1);
251 *this->pos_++ = static_cast<uint8_t>(value);
252 return;
253 }
254 this->encode_varint_raw_slow_(value);
255 }
269 APIBuffer *get_buffer() const { return buffer_; }
270 uint8_t *get_pos() const { return pos_; }
271 void set_pos(uint8_t *pos) { pos_ = pos; }
272
273 protected:
274 // Slow path for encode_varint_raw values >= 128, outlined to keep fast path small
275 void encode_varint_raw_slow_(uint32_t value) __attribute__((noinline));
276
277#ifdef ESPHOME_DEBUG_API
278 void debug_check_bounds_(size_t bytes, const char *caller = __builtin_FUNCTION());
279#else
280 void debug_check_bounds_([[maybe_unused]] size_t bytes) {}
281#endif
282
284 uint8_t *pos_;
285};
286
287// A four byte unaligned store is a memcpy call on ESP-IDF (-fno-builtin-memcpy) and on ARM cores without
288// unaligned access (Cortex-M0+, ARM9), so those targets share one outlined byte store helper per fixed32
289// field. Elsewhere the write inlines to a single store, or on ESP8266 to a few stores that measured
290// faster than a call, so it stays inline.
291#if defined(USE_ESP32) || (defined(__arm__) && !defined(__ARM_FEATURE_UNALIGNED))
292#define PROTO_OUTLINE_FOR_SIZE __attribute__((noinline))
293#define PROTO_FIXED32_BYTE_STORES true
294#else
295#define PROTO_OUTLINE_FOR_SIZE inline
296#define PROTO_FIXED32_BYTE_STORES false
297#endif
298
299// Varint encoding thresholds — used by both proto_encode_* free functions and ProtoSize.
300constexpr uint32_t VARINT_MAX_1_BYTE = 1 << 7; // 128
301constexpr uint32_t VARINT_MAX_2_BYTE = 1 << 14; // 16384
302
304using ProtoEncodeFn = uint8_t *(*) (const void *, uint8_t *PROTO_ENCODE_DEBUG_PARAM);
305
310 public:
312 template<typename T>
313 [[nodiscard]] static inline uint8_t *encode_varint_raw_loop(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
314 T value) {
315 do {
316 PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
317 *pos++ = static_cast<uint8_t>(value | 0x80);
318 value >>= 7;
319 } while (value > 0x7F);
320 PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
321 *pos++ = static_cast<uint8_t>(value);
322 return pos;
323 }
324 [[nodiscard]] static inline uint8_t *ESPHOME_ALWAYS_INLINE
325 encode_varint_raw(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t value) {
326 if (value < VARINT_MAX_1_BYTE) [[likely]] {
327 PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
328 *pos++ = static_cast<uint8_t>(value);
329 return pos;
330 }
331 return encode_varint_raw_loop(pos PROTO_ENCODE_DEBUG_ARG, value);
332 }
334 [[nodiscard]] static inline uint8_t *ESPHOME_ALWAYS_INLINE
335 encode_varint_raw_short(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t value) {
336 if (value < VARINT_MAX_1_BYTE) [[likely]] {
337 PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
338 *pos++ = static_cast<uint8_t>(value);
339 return pos;
340 }
341 if (value < VARINT_MAX_2_BYTE) [[likely]] {
342 PROTO_ENCODE_CHECK_BOUNDS(pos, 2);
343 *pos++ = static_cast<uint8_t>(value | 0x80);
344 *pos++ = static_cast<uint8_t>(value >> 7);
345 return pos;
346 }
347 return encode_varint_raw_loop(pos PROTO_ENCODE_DEBUG_ARG, value);
348 }
349 [[nodiscard]] static inline uint8_t *ESPHOME_ALWAYS_INLINE
350 encode_varint_raw_64(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint64_t value) {
351 if (value < VARINT_MAX_1_BYTE) [[likely]] {
352 PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
353 *pos++ = static_cast<uint8_t>(value);
354 return pos;
355 }
356 return encode_varint_raw_loop(pos PROTO_ENCODE_DEBUG_ARG, value);
357 }
363 [[nodiscard]] static inline uint8_t *ESPHOME_ALWAYS_INLINE
364 encode_varint_raw_48bit(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint64_t value) {
365#ifdef ESPHOME_DEBUG_API
366 assert(value < (1ULL << (MAC_ADDRESS_SIZE * 8)) && "encode_varint_raw_48bit: value exceeds 48 bits");
367#endif
368 // 7-byte varint holds 49 bits (7 * 7), so a 48-bit value needs all 7 bytes
369 // whenever bit 42 or higher is set (i.e. value >= 1 << (48 - 6)).
370 if (value >= (1ULL << (MAC_ADDRESS_SIZE * 8 - 6))) [[likely]] {
371 PROTO_ENCODE_CHECK_BOUNDS(pos, 7);
372 pos[0] = static_cast<uint8_t>(value | 0x80);
373 pos[1] = static_cast<uint8_t>((value >> 7) | 0x80);
374 pos[2] = static_cast<uint8_t>((value >> 14) | 0x80);
375 pos[3] = static_cast<uint8_t>((value >> 21) | 0x80);
376 pos[4] = static_cast<uint8_t>((value >> 28) | 0x80);
377 pos[5] = static_cast<uint8_t>((value >> 35) | 0x80);
378 pos[6] = static_cast<uint8_t>(value >> 42);
379 return pos + 7;
380 }
381 return encode_varint_raw_64(pos PROTO_ENCODE_DEBUG_ARG, value);
382 }
383 [[nodiscard]] static inline uint8_t *ESPHOME_ALWAYS_INLINE
384 encode_field_raw(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, uint32_t type) {
385 return encode_varint_raw(pos PROTO_ENCODE_DEBUG_ARG, proto_tag(field_id, type));
386 }
388 [[nodiscard]] static inline uint8_t *ESPHOME_ALWAYS_INLINE
389 write_raw_byte(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint8_t b) {
390 PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
391 *pos++ = b;
392 return pos;
393 }
396 [[nodiscard]] static inline uint8_t *ESPHOME_ALWAYS_INLINE
397 reserve_byte(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM) {
398 PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
399 return pos + 1;
400 }
402 [[nodiscard]] static inline uint8_t *ESPHOME_ALWAYS_INLINE
403 encode_raw(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, const void *data, size_t len) {
404 PROTO_ENCODE_CHECK_BOUNDS(pos, len);
405 std::memcpy(pos, data, len);
406 return pos + len;
407 }
410 [[nodiscard]] static inline uint8_t *encode_short_string_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
411 uint8_t tag, const StringRef &ref) {
412#ifdef ESPHOME_DEBUG_API
413 assert(ref.size() < 128 && "encode_short_string_force: string exceeds max_data_length < 128");
414#endif
415 PROTO_ENCODE_CHECK_BOUNDS(pos, 2 + ref.size());
416 pos[0] = tag;
417 pos[1] = static_cast<uint8_t>(ref.size());
418 std::memcpy(pos + 2, ref.c_str(), ref.size());
419 return pos + 2 + ref.size();
420 }
422 [[nodiscard]] static PROTO_OUTLINE_FOR_SIZE uint8_t *write_tag_and_fixed32(
423 uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint8_t tag, uint32_t value) {
424 PROTO_ENCODE_CHECK_BOUNDS(pos, 5);
425 pos[0] = tag;
426 write_fixed32_le(pos + 1, value);
427 return pos + 5;
428 }
429 [[nodiscard]] static inline uint8_t *encode_string_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
430 uint32_t field_id, const char *string, size_t len) {
431 pos = encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 2); // type 2: Length-delimited string
432 // NOLINTNEXTLINE(readability-inconsistent-ifelse-braces) -- false positive on [[likely]] attribute
433 if (len < VARINT_MAX_1_BYTE) [[likely]] {
434 PROTO_ENCODE_CHECK_BOUNDS(pos, 1 + len);
435 *pos++ = static_cast<uint8_t>(len);
436 } else {
437 pos = encode_varint_raw_loop(pos PROTO_ENCODE_DEBUG_ARG, len);
438 PROTO_ENCODE_CHECK_BOUNDS(pos, len);
439 }
440 std::memcpy(pos, string, len);
441 return pos + len;
442 }
443 [[nodiscard]] static inline uint8_t *encode_string(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
444 uint32_t field_id, const char *string, size_t len) {
445 if (len == 0)
446 return pos;
447 return encode_string_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, string, len);
448 }
449 [[nodiscard]] static inline uint8_t *encode_string_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
450 uint32_t field_id, const std::string &value) {
451 return encode_string_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, value.data(), value.size());
452 }
453 [[nodiscard]] static inline uint8_t *encode_string(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
454 uint32_t field_id, const StringRef &ref) {
455 return encode_string(pos PROTO_ENCODE_DEBUG_ARG, field_id, ref.c_str(), ref.size());
456 }
457 [[nodiscard]] static inline uint8_t *encode_string_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
458 uint32_t field_id, const StringRef &ref) {
459 return encode_string_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, ref.c_str(), ref.size());
460 }
461 [[nodiscard]] static inline uint8_t *encode_bytes(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
462 uint32_t field_id, const uint8_t *data, size_t len) {
463 return encode_string(pos PROTO_ENCODE_DEBUG_ARG, field_id, reinterpret_cast<const char *>(data), len);
464 }
465 [[nodiscard]] static inline uint8_t *encode_bytes_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
466 uint32_t field_id, const uint8_t *data, size_t len) {
467 return encode_string_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, reinterpret_cast<const char *>(data), len);
468 }
470 [[nodiscard]] static inline uint8_t *encode_progmem_bytes(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
471 uint32_t field_id, const uint8_t *data, size_t len) {
472 if (len == 0)
473 return pos;
474 return encode_progmem_bytes_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, data, len);
475 }
476 [[nodiscard]] static inline uint8_t *encode_progmem_bytes_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
477 uint32_t field_id, const uint8_t *data, size_t len) {
478 pos = encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 2); // type 2: Length-delimited
479 pos = encode_varint_raw(pos PROTO_ENCODE_DEBUG_ARG, len);
480 PROTO_ENCODE_CHECK_BOUNDS(pos, len);
481 progmem_memcpy(pos, data, len);
482 return pos + len;
483 }
484 [[nodiscard]] static inline uint8_t *encode_uint32_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
485 uint32_t field_id, uint32_t value) {
486 pos = encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 0);
487 return encode_varint_raw(pos PROTO_ENCODE_DEBUG_ARG, value);
488 }
489 [[nodiscard]] static inline uint8_t *encode_uint32(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
490 uint32_t field_id, uint32_t value) {
491 if (value == 0)
492 return pos;
493 return encode_uint32_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, value);
494 }
495 [[nodiscard]] static inline uint8_t *encode_uint64_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
496 uint32_t field_id, uint64_t value) {
497 pos = encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 0);
498 return encode_varint_raw_64(pos PROTO_ENCODE_DEBUG_ARG, value);
499 }
500 [[nodiscard]] static inline uint8_t *encode_uint64(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
501 uint32_t field_id, uint64_t value) {
502 if (value == 0)
503 return pos;
504 return encode_uint64_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, value);
505 }
506 [[nodiscard]] static inline uint8_t *encode_bool_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
507 uint32_t field_id, bool value) {
508 pos = encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 0);
509 PROTO_ENCODE_CHECK_BOUNDS(pos, 1);
510 *pos++ = value ? 0x01 : 0x00;
511 return pos;
512 }
513 [[nodiscard]] static inline uint8_t *encode_bool(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
514 uint32_t field_id, bool value) {
515 if (!value)
516 return pos;
517 return encode_bool_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, value);
518 }
520 [[nodiscard]] static PROTO_OUTLINE_FOR_SIZE uint8_t *encode_fixed32_force(
521 uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, uint32_t value) {
522 pos = encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 5);
523 PROTO_ENCODE_CHECK_BOUNDS(pos, 4);
524 write_fixed32_le(pos, value);
525 return pos + 4;
526 }
527 [[nodiscard]] static inline uint8_t *encode_fixed32(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
528 uint32_t field_id, uint32_t value) {
529 if (value == 0)
530 return pos;
531 return encode_fixed32_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, value);
532 }
533 // NOTE: Wire type 1 (64-bit fixed: double, fixed64, sfixed64) is intentionally
534 // not supported to reduce overhead on embedded systems. All ESPHome devices are
535 // 32-bit microcontrollers where 64-bit operations are expensive. If 64-bit support
536 // is needed in the future, the necessary encoding/decoding functions must be added.
537 [[nodiscard]] static inline uint8_t *encode_float(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
538 uint32_t field_id, float value) {
539 return encode_fixed32(pos PROTO_ENCODE_DEBUG_ARG, field_id, float_to_raw(value));
540 }
541 [[nodiscard]] static inline uint8_t *encode_float_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
542 uint32_t field_id, float value) {
543 return encode_fixed32_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, float_to_raw(value));
544 }
545 [[nodiscard]] static inline uint8_t *encode_int32_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
546 uint32_t field_id, int32_t value) {
547 if (value < 0) {
548 // negative int32 is always 10 byte long
549 return encode_uint64_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, static_cast<uint64_t>(value));
550 }
551 return encode_uint32_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, static_cast<uint32_t>(value));
552 }
553 [[nodiscard]] static inline uint8_t *encode_int32(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
554 uint32_t field_id, int32_t value) {
555 if (value == 0)
556 return pos;
557 return encode_int32_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, value);
558 }
559 [[nodiscard]] static inline uint8_t *encode_int64(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
560 uint32_t field_id, int64_t value) {
561 return encode_uint64(pos PROTO_ENCODE_DEBUG_ARG, field_id, static_cast<uint64_t>(value));
562 }
563 [[nodiscard]] static inline uint8_t *encode_int64_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
564 uint32_t field_id, int64_t value) {
565 return encode_uint64_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, static_cast<uint64_t>(value));
566 }
567 [[nodiscard]] static inline uint8_t *encode_sint32(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
568 uint32_t field_id, int32_t value) {
569 return encode_uint32(pos PROTO_ENCODE_DEBUG_ARG, field_id, encode_zigzag32(value));
570 }
571 [[nodiscard]] static inline uint8_t *encode_sint32_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
572 uint32_t field_id, int32_t value) {
573 return encode_uint32_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, encode_zigzag32(value));
574 }
575 [[nodiscard]] static inline uint8_t *encode_sint64(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
576 uint32_t field_id, int64_t value) {
577 return encode_uint64(pos PROTO_ENCODE_DEBUG_ARG, field_id, encode_zigzag64(value));
578 }
579 [[nodiscard]] static inline uint8_t *encode_sint64_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
580 uint32_t field_id, int64_t value) {
581 return encode_uint64_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, encode_zigzag64(value));
582 }
584 template<typename T>
585 [[nodiscard]] static inline uint8_t *encode_sub_message(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
586 uint32_t field_id, const T &value) {
587 pos = encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 2);
588 return encode_sub_message_body(pos PROTO_ENCODE_DEBUG_ARG, &value, &T::encode_msg);
589 }
591 template<typename T>
592 [[nodiscard]] static inline uint8_t *encode_optional_sub_message(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
593 uint32_t field_id, const T &value) {
594 uint32_t nested_size = T::calc_size_msg(&value);
595 if (nested_size == 0)
596 return pos;
597 pos = encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 2);
598#ifdef ESPHOME_DEBUG_API
599 uint8_t *end = encode_sized_sub_message_body(pos PROTO_ENCODE_DEBUG_ARG, nested_size, &value, &T::encode_msg);
600 proto_check_sub_message_size(field_id, nested_size, pos, end);
601 return end;
602#else
603 return encode_sized_sub_message_body(pos, nested_size, &value, &T::encode_msg);
604#endif
605 }
607 [[nodiscard]] static uint8_t *encode_sub_message_body(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
608 const void *value, ProtoEncodeFn encode_fn);
610 [[nodiscard]] static uint8_t *encode_sized_sub_message_body(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
611 uint32_t nested_size, const void *value,
612 ProtoEncodeFn encode_fn);
613
614 private:
618 static inline void ESPHOME_ALWAYS_INLINE write_fixed32_le(uint8_t *__restrict__ pos, uint32_t value) {
619 if constexpr (PROTO_FIXED32_BYTE_STORES) {
620 // Spelled out so the outlined helper does not itself become a memcpy call
621 pos[0] = static_cast<uint8_t>(value);
622 pos[1] = static_cast<uint8_t>(value >> 8);
623 pos[2] = static_cast<uint8_t>(value >> 16);
624 pos[3] = static_cast<uint8_t>(value >> 24);
625 } else {
626 const uint32_t le = convert_little_endian(value);
627 __builtin_memcpy(pos, &le, 4);
628 }
629 }
630};
631#undef PROTO_OUTLINE_FOR_SIZE
632#undef PROTO_FIXED32_BYTE_STORES
633
634#ifdef HAS_PROTO_MESSAGE_DUMP
641 public:
642 // Matches default tx_buffer_size in logger component
643 static constexpr size_t CAPACITY = 512;
644
645 DumpBuffer() : pos_(0) { buf_[0] = '\0'; }
646
647 DumpBuffer &append(const char *str) {
648 if (str) {
649 append_impl_(str, strlen(str));
650 }
651 return *this;
652 }
653
654 DumpBuffer &append(const char *str, size_t len) {
655 append_impl_(str, len);
656 return *this;
657 }
658
659 DumpBuffer &append(size_t n, char c) {
660 size_t space = CAPACITY - 1 - pos_;
661 if (n > space)
662 n = space;
663 if (n > 0) {
664 memset(buf_ + pos_, c, n);
665 pos_ += n;
666 buf_[pos_] = '\0';
667 }
668 return *this;
669 }
670
672 DumpBuffer &append_p(const char *str) {
673 if (str) {
674#ifdef USE_ESP8266
675 append_p_esp8266(str);
676#else
677 append_impl_(str, strlen(str));
678#endif
679 }
680 return *this;
681 }
682
683#ifdef USE_ESP8266
685 void append_p_esp8266(const char *str);
686#endif
687
688 const char *c_str() const { return buf_; }
689 size_t size() const { return pos_; }
690
692 char *data() { return buf_; }
694 size_t pos() const { return pos_; }
696 void set_pos(size_t pos) {
697 if (pos >= CAPACITY) {
698 pos_ = CAPACITY - 1;
699 } else {
700 pos_ = pos;
701 }
702 buf_[pos_] = '\0';
703 }
704
705 private:
706 void append_impl_(const char *str, size_t len) {
707 size_t space = CAPACITY - 1 - pos_;
708 if (len > space)
709 len = space;
710 if (len > 0) {
711 memcpy(buf_ + pos_, str, len);
712 pos_ += len;
713 buf_[pos_] = '\0';
714 }
715 }
716
717 char buf_[CAPACITY];
718 size_t pos_;
719};
720#endif
721
723 public:
724 // Non-virtual defaults for messages with no fields; generated classes hide all four. The
725 // static encode_msg/calc_size_msg take const void * so &T::encode_msg needs no thunk.
726 static uint8_t *encode_msg(const void *self, uint8_t *pos PROTO_ENCODE_DEBUG_PARAM) { return pos; }
727 static uint32_t calc_size_msg(const void *self) { return 0; }
728 uint8_t *encode(ProtoWriteBuffer &buffer PROTO_ENCODE_DEBUG_PARAM) const { return buffer.get_pos(); }
729 uint32_t calculate_size() const { return 0; }
730#ifdef HAS_PROTO_MESSAGE_DUMP
731 virtual const char *dump_to(DumpBuffer &out) const = 0;
732 virtual const LogString *message_name() const { return LOG_STR("unknown"); }
733#endif
734
735#ifndef USE_HOST
736 protected:
737#endif
738 // Non-virtual destructor is protected to prevent polymorphic deletion.
739 // On host platform, made public to allow value-initialization of std::array
740 // members (e.g. DeviceInfoResponse::devices) without clang errors.
741 ~ProtoMessage() = default;
742};
743
744// Base class for messages that support decoding
746 public:
750 using DecodeFieldFn = void (*)(void *msg, uint32_t tag, const uint8_t *data, proto_varint_value_t scalar);
753 static void decode_fields(void *msg, const uint8_t *buffer, size_t length, DecodeFieldFn field);
754
764 static uint32_t count_repeated_field(const uint8_t *buffer, size_t length, uint32_t target_field_id);
765 // The destructor stays accessible on purpose: the generated messages are aggregates that brace
766 // initialise sub message members, which copies a base temporary. That trades away the compile time
767 // guard against deleting through this type; messages are stack locals and never owned through a base
768 // pointer. ProtoMessage keeps its guard for the dump builds.
769};
770#ifndef HAS_PROTO_MESSAGE_DUMP
771// decode() passes decode_field explicitly, so nothing here may add a vtable
772static_assert(!std::is_polymorphic_v<ProtoDecodableMessage>, "decodable messages carry no vtable");
773#endif
774
776 public:
777 // Varint encoding thresholds — use namespace-level constants for 1/2 byte,
778 // class-level for 3/4 byte (only used within ProtoSize).
781 static constexpr uint32_t VARINT_THRESHOLD_3_BYTE = 1 << 21; // 2097152
782 static constexpr uint32_t VARINT_THRESHOLD_4_BYTE = 1 << 28; // 268435456
783
784 // Varint encoded length for a 16-bit value (1, 2, or 3 bytes).
785 // Fully inline — no slow path call for values >= 128.
786 static constexpr inline uint8_t ESPHOME_ALWAYS_INLINE varint16(uint16_t value) {
787 return value < VARINT_THRESHOLD_1_BYTE ? 1 : (value < VARINT_THRESHOLD_2_BYTE ? 2 : 3);
788 }
789
796 static constexpr inline uint32_t ESPHOME_ALWAYS_INLINE varint(uint32_t value) {
797 if (value < VARINT_THRESHOLD_1_BYTE) [[likely]]
798 return 1; // Fast path: 7 bits, most common case
799 if (__builtin_is_constant_evaluated())
800 return varint_wide(value);
801 return varint_slow(value);
802 }
805 static constexpr inline uint32_t ESPHOME_ALWAYS_INLINE varint_short(uint32_t value) {
806 if (value < VARINT_THRESHOLD_1_BYTE) [[likely]]
807 return 1;
808 if (value < VARINT_THRESHOLD_2_BYTE) [[likely]]
809 return 2;
810 if (__builtin_is_constant_evaluated())
811 return varint_wide(value);
812 return varint_slow(value);
813 }
814
815 private:
816 // Slow path for varint >= 128, outlined to keep fast path small
817 static uint32_t varint_slow(uint32_t value) __attribute__((noinline));
818 // Shared cascade for values >= 128 (used by both constexpr and noinline paths)
819 static constexpr inline uint32_t ESPHOME_ALWAYS_INLINE varint_wide(uint32_t value) {
820 if (value < VARINT_THRESHOLD_2_BYTE)
821 return 2;
822 if (value < VARINT_THRESHOLD_3_BYTE)
823 return 3;
824 if (value < VARINT_THRESHOLD_4_BYTE)
825 return 4;
826 return 5;
827 }
828
829 public:
836 static constexpr uint32_t varint(uint64_t value) {
837 // Handle common case of values fitting in uint32_t (vast majority of use cases)
838 if (value <= UINT32_MAX) {
839 return varint(static_cast<uint32_t>(value));
840 }
841
842 // For larger values, determine size based on highest bit position
843 if (value < (1ULL << 35)) {
844 return 5; // 35 bits
845 } else if (value < (1ULL << 42)) {
846 return 6; // 42 bits
847 } else if (value < (1ULL << 49)) {
848 return 7; // 49 bits
849 } else if (value < (1ULL << 56)) {
850 return 8; // 56 bits
851 } else if (value < (1ULL << 63)) {
852 return 9; // 63 bits
853 } else {
854 return 10; // 64 bits (maximum for uint64_t)
855 }
856 }
857
867 static constexpr uint32_t varint(int32_t value) {
868 // Negative values are sign-extended to 64 bits in protocol buffers,
869 // which always results in a 10-byte varint for negative int32
870 if (value < 0) {
871 return 10; // Negative int32 is always 10 bytes long
872 }
873 // For non-negative values, use the uint32_t implementation
874 return varint(static_cast<uint32_t>(value));
875 }
876
883 static constexpr uint32_t varint(int64_t value) {
884 // For int64_t, we convert to uint64_t and calculate the size
885 // This works because the bit pattern determines the encoding size,
886 // and we've handled negative int32 values as a special case above
887 return varint(static_cast<uint64_t>(value));
888 }
889
897 static constexpr uint32_t field(uint32_t field_id, uint32_t type) {
898 uint32_t tag = proto_tag(field_id, type & WIRE_TYPE_MASK);
899 return varint(tag);
900 }
901
902 // Static methods that RETURN size contribution (no ProtoSize object needed).
903 // Used by generated calculate_size() methods to accumulate into a plain uint32_t register.
904 static constexpr uint32_t calc_int32(uint32_t field_id_size, int32_t value) {
905 return value ? field_id_size + (value < 0 ? 10 : varint(static_cast<uint32_t>(value))) : 0;
906 }
907 static constexpr uint32_t calc_int32_force(uint32_t field_id_size, int32_t value) {
908 return field_id_size + (value < 0 ? 10 : varint(static_cast<uint32_t>(value)));
909 }
910 static constexpr uint32_t calc_uint32(uint32_t field_id_size, uint32_t value) {
911 return value ? field_id_size + varint(value) : 0;
912 }
913 static constexpr uint32_t calc_uint32_force(uint32_t field_id_size, uint32_t value) {
914 return field_id_size + varint(value);
915 }
916 static constexpr uint32_t calc_bool(uint32_t field_id_size, bool value) { return value ? field_id_size + 1 : 0; }
917 static constexpr uint32_t calc_bool_force(uint32_t field_id_size) { return field_id_size + 1; }
918 static uint32_t calc_float(uint32_t field_id_size, float value) {
919 return float_to_raw(value) != 0 ? field_id_size + 4 : 0;
920 }
921 static constexpr uint32_t calc_fixed32(uint32_t field_id_size, uint32_t value) {
922 return value ? field_id_size + 4 : 0;
923 }
924 static constexpr uint32_t calc_sfixed32(uint32_t field_id_size, int32_t value) {
925 return value ? field_id_size + 4 : 0;
926 }
927 static constexpr uint32_t calc_sint32(uint32_t field_id_size, int32_t value) {
928 return value ? field_id_size + varint_short(encode_zigzag32(value)) : 0;
929 }
930 static constexpr inline uint32_t ESPHOME_ALWAYS_INLINE calc_sint32_force(uint32_t field_id_size, int32_t value) {
931 return field_id_size + varint_short(encode_zigzag32(value));
932 }
933 static constexpr uint32_t calc_int64(uint32_t field_id_size, int64_t value) {
934 return value ? field_id_size + varint(value) : 0;
935 }
936 static constexpr uint32_t calc_int64_force(uint32_t field_id_size, int64_t value) {
937 return field_id_size + varint(value);
938 }
939 static constexpr uint32_t calc_uint64(uint32_t field_id_size, uint64_t value) {
940 return value ? field_id_size + varint(value) : 0;
941 }
942 static constexpr inline uint32_t ESPHOME_ALWAYS_INLINE calc_uint64_force(uint32_t field_id_size, uint64_t value) {
943 return field_id_size + varint(value);
944 }
949 static constexpr inline uint32_t ESPHOME_ALWAYS_INLINE calc_uint64_48bit_force(uint32_t field_id_size,
950 uint64_t value) {
951 return field_id_size + (value >= (1ULL << (MAC_ADDRESS_SIZE * 8 - 6)) ? 7 : varint(value));
952 }
953 static constexpr uint32_t calc_length(uint32_t field_id_size, size_t len) {
954 return len ? field_id_size + varint(static_cast<uint32_t>(len)) + static_cast<uint32_t>(len) : 0;
955 }
956 static constexpr inline uint32_t ESPHOME_ALWAYS_INLINE calc_length_force(uint32_t field_id_size, size_t len) {
957 return field_id_size + varint(static_cast<uint32_t>(len)) + static_cast<uint32_t>(len);
958 }
959 static constexpr uint32_t calc_sint64(uint32_t field_id_size, int64_t value) {
960 return value ? field_id_size + varint(encode_zigzag64(value)) : 0;
961 }
962 static constexpr uint32_t calc_sint64_force(uint32_t field_id_size, int64_t value) {
963 return field_id_size + varint(encode_zigzag64(value));
964 }
965 static constexpr uint32_t calc_fixed64(uint32_t field_id_size, uint64_t value) {
966 return value ? field_id_size + 8 : 0;
967 }
968 static constexpr uint32_t calc_sfixed64(uint32_t field_id_size, int64_t value) {
969 return value ? field_id_size + 8 : 0;
970 }
971 static constexpr uint32_t calc_message(uint32_t field_id_size, uint32_t nested_size) {
972 return nested_size ? field_id_size + varint(nested_size) + nested_size : 0;
973 }
974 static constexpr inline uint32_t ESPHOME_ALWAYS_INLINE calc_message_force(uint32_t field_id_size,
975 uint32_t nested_size) {
976 return field_id_size + varint(nested_size) + nested_size;
977 }
978};
979
980template<typename T> const char *proto_enum_to_string(T value);
981
982// ProtoService removed — its methods were inlined into APIConnection.
983// APIConnection is the concrete server-side implementation; the extra virtual layer was unnecessary.
984
985} // 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:30
constexpr const char * c_str() const
Definition string_ref.h:77
constexpr size_type size() const
Definition string_ref.h:78
Byte buffer that skips zero-initialization on resize().
Definition api_buffer.h:26
Fixed-size buffer for message dumps - avoids heap allocation.
Definition proto.h:640
const char * c_str() const
Definition proto.h:688
DumpBuffer & append(size_t n, char c)
Definition proto.h:659
size_t size() const
Definition proto.h:689
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:672
size_t pos() const
Get current position for use with buf_append_printf.
Definition proto.h:694
static constexpr size_t CAPACITY
Definition proto.h:643
DumpBuffer & append(const char *str, size_t len)
Definition proto.h:654
DumpBuffer & append(const char *str)
Definition proto.h:647
char * data()
Get writable buffer pointer for use with buf_append_printf.
Definition proto.h:692
void set_pos(size_t pos)
Update position after buf_append_printf call.
Definition proto.h:696
static void decode_fields(void *msg, const uint8_t *buffer, size_t length, DecodeFieldFn field)
Walk buffer and hand every field to field.
Definition proto.cpp:205
void(*)(void *msg, uint32_t tag, const uint8_t *data, proto_varint_value_t scalar) DecodeFieldFn
Stores one decoded field into msg; generated per message type.
Definition proto.h:750
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 the generated encode bodies.
Definition proto.h:309
static uint8_t *ESPHOME_ALWAYS_INLINE encode_field_raw(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, uint32_t type)
Definition proto.h:384
static uint8_t * 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:410
static uint8_t * encode_string_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, const StringRef &ref)
Definition proto.h:457
static uint8_t * encode_int64_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, int64_t value)
Definition proto.h:563
static uint8_t *ESPHOME_ALWAYS_INLINE encode_varint_raw_48bit(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint64_t value)
Encode a 48-bit MAC address (stored in a uint64) as varint.
Definition proto.h:364
static uint8_t * encode_progmem_bytes(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, const uint8_t *data, size_t len)
encode_bytes for data in flash (PROGMEM), which ESP8266 can only read with progmem_memcpy.
Definition proto.h:470
static uint8_t * encode_int32_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, int32_t value)
Definition proto.h:545
static uint8_t * encode_string(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, const StringRef &ref)
Definition proto.h:453
static uint8_t * encode_string_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, const std::string &value)
Definition proto.h:449
static uint8_t * encode_sint64_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, int64_t value)
Definition proto.h:579
static uint8_t * encode_sub_message(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, const T &value)
Repeated sub-message element; the constant tag is written inline.
Definition proto.h:585
static uint8_t * encode_uint32_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, uint32_t value)
Definition proto.h:484
static uint8_t * encode_int32(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, int32_t value)
Definition proto.h:553
static uint8_t *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:389
static uint8_t *ESPHOME_ALWAYS_INLINE encode_varint_raw(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t value)
Definition proto.h:325
static PROTO_OUTLINE_FOR_SIZE uint8_t * encode_fixed32_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, uint32_t value)
Tag + fixed32 for multi-byte tags; single-byte tags use write_tag_and_fixed32.
Definition proto.h:520
static uint8_t * encode_sint64(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, int64_t value)
Definition proto.h:575
static uint8_t * encode_string_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, const char *string, size_t len)
Definition proto.h:429
static uint8_t * encode_string(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, const char *string, size_t len)
Definition proto.h:443
static uint8_t * encode_uint32(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, uint32_t value)
Definition proto.h:489
static uint8_t * encode_sint32(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, int32_t value)
Definition proto.h:567
static uint8_t * encode_fixed32(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, uint32_t value)
Definition proto.h:527
static uint8_t * encode_sized_sub_message_body(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t nested_size, const void *value, ProtoEncodeFn encode_fn)
Length and body for a precomputed size.
Definition proto.cpp:170
static uint8_t * encode_optional_sub_message(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, const T &value)
Singular sub-message field, skipped when it encodes to nothing.
Definition proto.h:592
static uint8_t *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:397
static uint8_t * 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:313
static uint8_t * encode_sint32_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, int32_t value)
Definition proto.h:571
static uint8_t * encode_progmem_bytes_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, const uint8_t *data, size_t len)
Definition proto.h:476
static PROTO_OUTLINE_FOR_SIZE uint8_t * 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. Outlined on embedded: one copy beats inline stores per f...
Definition proto.h:422
static uint8_t * encode_float_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, float value)
Definition proto.h:541
static uint8_t * encode_bool(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, bool value)
Definition proto.h:513
static uint8_t * encode_bytes(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, const uint8_t *data, size_t len)
Definition proto.h:461
static uint8_t *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:403
static uint8_t *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:335
static uint8_t *ESPHOME_ALWAYS_INLINE encode_varint_raw_64(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint64_t value)
Definition proto.h:350
static uint8_t * encode_uint64(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, uint64_t value)
Definition proto.h:500
static uint8_t * encode_uint64_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, uint64_t value)
Definition proto.h:495
static uint8_t * encode_int64(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, int64_t value)
Definition proto.h:559
static uint8_t * encode_float(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, float value)
Definition proto.h:537
static uint8_t * encode_sub_message_body(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, const void *value, ProtoEncodeFn encode_fn)
Length and body, length backpatched after the body is written.
Definition proto.cpp:147
static uint8_t * encode_bytes_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, const uint8_t *data, size_t len)
Definition proto.h:465
static uint8_t * encode_bool_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, bool value)
Definition proto.h:506
One decoded field: the payload pointer and a scalar holding the varint or fixed32 value,...
Definition proto.h:184
void decode_to_message(T &msg) const
Decode the length-delimited payload into a message instance.
Definition proto.h:198
std::string as_string() const
Definition proto.h:195
uint32_t as_fixed32() const
Definition proto.h:201
const uint8_t * data() const
Definition proto.h:193
ProtoFieldValue(const uint8_t *data, proto_varint_value_t scalar)
Definition proto.h:186
proto_varint_value_t as_varint() const
Definition proto.h:188
int32_t as_sfixed32() const
Definition proto.h:202
virtual const LogString * message_name() const
Definition proto.h:732
uint8_t * encode(ProtoWriteBuffer &buffer PROTO_ENCODE_DEBUG_PARAM) const
Definition proto.h:728
uint32_t calculate_size() const
Definition proto.h:729
static uint32_t calc_size_msg(const void *self)
Definition proto.h:727
static uint8_t * encode_msg(const void *self, uint8_t *pos PROTO_ENCODE_DEBUG_PARAM)
Definition proto.h:726
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:939
static constexpr uint32_t calc_int64(uint32_t field_id_size, int64_t value)
Definition proto.h:933
static constexpr uint32_t calc_bool(uint32_t field_id_size, bool value)
Definition proto.h:916
static constexpr uint32_t calc_message(uint32_t field_id_size, uint32_t nested_size)
Definition proto.h:971
static constexpr uint32_t calc_sint32(uint32_t field_id_size, int32_t value)
Definition proto.h:927
static constexpr uint32_t calc_sint64_force(uint32_t field_id_size, int64_t value)
Definition proto.h:962
static constexpr uint32_t ESPHOME_ALWAYS_INLINE calc_uint64_force(uint32_t field_id_size, uint64_t value)
Definition proto.h:942
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:897
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:796
static constexpr uint32_t calc_uint32_force(uint32_t field_id_size, uint32_t value)
Definition proto.h:913
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:867
static constexpr uint32_t calc_int64_force(uint32_t field_id_size, int64_t value)
Definition proto.h:936
static constexpr uint32_t ESPHOME_ALWAYS_INLINE calc_uint64_48bit_force(uint32_t field_id_size, uint64_t value)
48-bit MAC address variant: matches encode_varint_raw_48bit's fast path.
Definition proto.h:949
static constexpr uint32_t calc_sfixed64(uint32_t field_id_size, int64_t value)
Definition proto.h:968
static constexpr uint32_t calc_uint32(uint32_t field_id_size, uint32_t value)
Definition proto.h:910
static constexpr uint32_t VARINT_THRESHOLD_3_BYTE
Definition proto.h:781
static constexpr uint8_t ESPHOME_ALWAYS_INLINE varint16(uint16_t value)
Definition proto.h:786
static constexpr uint32_t calc_length(uint32_t field_id_size, size_t len)
Definition proto.h:953
static constexpr uint32_t calc_int32(uint32_t field_id_size, int32_t value)
Definition proto.h:904
static constexpr uint32_t calc_fixed64(uint32_t field_id_size, uint64_t value)
Definition proto.h:965
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:805
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:883
static constexpr uint32_t ESPHOME_ALWAYS_INLINE calc_length_force(uint32_t field_id_size, size_t len)
Definition proto.h:956
static constexpr uint32_t calc_bool_force(uint32_t field_id_size)
Definition proto.h:917
static constexpr uint32_t calc_sfixed32(uint32_t field_id_size, int32_t value)
Definition proto.h:924
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:836
static uint32_t calc_float(uint32_t field_id_size, float value)
Definition proto.h:918
static constexpr uint32_t calc_sint64(uint32_t field_id_size, int64_t value)
Definition proto.h:959
static constexpr uint32_t calc_int32_force(uint32_t field_id_size, int32_t value)
Definition proto.h:907
static constexpr uint32_t calc_fixed32(uint32_t field_id_size, uint32_t value)
Definition proto.h:921
static constexpr uint32_t VARINT_THRESHOLD_2_BYTE
Definition proto.h:780
static constexpr uint32_t ESPHOME_ALWAYS_INLINE calc_message_force(uint32_t field_id_size, uint32_t nested_size)
Definition proto.h:974
static constexpr uint32_t ESPHOME_ALWAYS_INLINE calc_sint32_force(uint32_t field_id_size, int32_t value)
Definition proto.h:930
static constexpr uint32_t VARINT_THRESHOLD_1_BYTE
Definition proto.h:779
static constexpr uint32_t VARINT_THRESHOLD_4_BYTE
Definition proto.h:782
Static varint parsing methods for the protobuf wire format.
Definition proto.h:141
static ProtoVarIntResult ESPHOME_ALWAYS_INLINE parse_non_empty(const uint8_t *buffer, uint32_t len)
Parse a varint from buffer.
Definition proto.h:145
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:158
void set_pos(uint8_t *pos)
Definition proto.h:271
void debug_check_bounds_(size_t bytes)
Definition proto.h:280
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:268
void debug_check_bounds_(size_t bytes, const char *caller=__builtin_FUNCTION())
Definition proto.cpp:195
uint8_t * get_pos() const
Definition proto.h:270
APIBuffer * get_buffer() const
Definition proto.h:269
void ESPHOME_ALWAYS_INLINE encode_varint_raw(uint32_t value)
Definition proto.h:248
ProtoWriteBuffer(APIBuffer *buffer, size_t write_pos)
Definition proto.h:247
ProtoWriteBuffer(APIBuffer *buffer)
Definition proto.h:246
struct @66::@67 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:45
uint16_t type
std::vector< uint8_t > & out
Definition gif.cpp:49
uint32_t buffer
Definition gif.cpp:50
mopeka_std_values val[3]
uint16_t count_packed_varints(const uint8_t *data, size_t len)
Count varints in a packed buffer: len minus bytes with the continuation bit, summed a word at a time.
Definition proto.h:62
constexpr uint32_t encode_zigzag32(int32_t value)
Definition proto.h:44
constexpr uint32_t VARINT_MAX_2_BYTE
Definition proto.h:301
constexpr uint8_t WIRE_TYPE_VARINT
Definition proto.h:25
constexpr uint32_t proto_tag(uint32_t field_id, uint32_t wire_type)
Case label for decode_field(): the wire tag of a field, so a field that arrives with another wire typ...
Definition proto.h:180
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:123
const char * proto_enum_to_string(T value)
void proto_check_encode_end(const uint8_t *end, const uint8_t *expected)
Aborts unless an encode body ended exactly where calculate_size() promised.
Definition proto.cpp:181
constexpr uint32_t VARINT_MAX_1_BYTE
Definition proto.h:300
constexpr uint64_t encode_zigzag64(int64_t value)
Definition proto.h:48
void proto_check_sub_message_size(uint32_t field_id, uint32_t expected, const uint8_t *len_pos, const uint8_t *end)
Aborts unless a sized sub-message (length prefix at len_pos) ended where its calculated size said.
Definition proto.cpp:187
constexpr uint8_t WIRE_TYPE_MASK
Definition proto.h:28
uint8_t *(*)(const void *, uint8_t *PROTO_ENCODE_DEBUG_PARAM) ProtoEncodeFn
Generated encode body: writes the fields at pos, returns the cursor past them.
Definition proto.h:304
constexpr uint32_t PROTO_VARINT_PARSE_FAILED
Sentinel value for consumed field indicating parse failure.
Definition proto.h:129
constexpr uint8_t WIRE_TYPE_LENGTH_DELIMITED
Definition proto.h:26
constexpr int32_t decode_zigzag32(uint32_t value)
Definition proto.h:52
constexpr uint8_t WIRE_TYPE_FIXED32
Definition proto.h:27
uint32_t float_to_raw(float value)
Definition proto.h:34
constexpr int64_t decode_zigzag64(uint64_t value)
Definition proto.h:56
void encode_varint_to_buffer(uint32_t val, uint8_t *buffer)
Encode a varint directly into a pre-allocated buffer.
Definition proto.h:83
void proto_check_bounds_failed(const uint8_t *pos, size_t bytes, const uint8_t *end, const char *caller)
Definition proto.cpp:177
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:48
constexpr T convert_little_endian(T val)
Convert a value between host byte order and little endian (least significant byte first) order.
Definition helpers.h:1043
const void size_t len
Definition hal.h:64
uint16_t size
Definition helpers.cpp:33
size_t size_t pos
Definition helpers.h:1186
static void uint32_t
Result of parsing a varint: value + number of bytes consumed.
Definition proto.h:133
constexpr bool has_value() const
Definition proto.h:137
proto_varint_value_t value
Definition proto.h:134
uint8_t end[39]
Definition sun_gtil2.cpp:17
uint16_t length
Definition tt21100.cpp:0