ESPHome 2026.1.4
Loading...
Searching...
No Matches
api_frame_helper_plaintext.h
Go to the documentation of this file.
1#pragma once
2#include "api_frame_helper.h"
3#ifdef USE_API
4#ifdef USE_API_PLAINTEXT
5
6namespace esphome::api {
7
9 public:
10 explicit APIPlaintextFrameHelper(std::unique_ptr<socket::Socket> socket) : APIFrameHelper(std::move(socket)) {
11 // Plaintext header structure (worst case):
12 // Pos 0: indicator (0x00)
13 // Pos 1-3: payload size varint (up to 3 bytes)
14 // Pos 4-5: message type varint (up to 2 bytes)
15 // Pos 6+: actual payload data
17 }
18 ~APIPlaintextFrameHelper() override = default;
19 APIError init() override;
20 APIError loop() override;
21 APIError read_packet(ReadPacketBuffer *buffer) override;
22 APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) override;
23 APIError write_protobuf_messages(ProtoWriteBuffer buffer, std::span<const MessageInfo> messages) override;
24
25 protected:
27
28 // Group 2-byte aligned types
31
32 // Group 1-byte types together
33 // Fixed-size header buffer for plaintext protocol:
34 // We now store the indicator byte + the two varints.
35 // To match noise protocol's maximum message size (UINT16_MAX = 65535), we need:
36 // 1 byte for indicator + 3 bytes for message size varint (supports up to 2097151) + 2 bytes for message type varint
37 //
38 // While varints could theoretically be up to 10 bytes each for 64-bit values,
39 // attempting to process messages with headers that large would likely crash the
40 // ESP32 due to memory constraints.
41 uint8_t rx_header_buf_[6]; // 1 byte indicator + 5 bytes for varints (3 for size + 2 for type)
42 uint8_t rx_header_buf_pos_ = 0;
43 bool rx_header_parsed_ = false;
44 // 8 bytes total, no padding needed
45};
46
47} // namespace esphome::api
48#endif // USE_API_PLAINTEXT
49#endif // USE_API
APIPlaintextFrameHelper(std::unique_ptr< socket::Socket > socket)
APIError try_read_frame_()
Read a packet into the rx_buf_.
APIError init() override
Initialize the frame helper, returns OK if successful.
~APIPlaintextFrameHelper() override=default
APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) override
APIError read_packet(ReadPacketBuffer *buffer) override
APIError write_protobuf_messages(ProtoWriteBuffer buffer, std::span< const MessageInfo > messages) override
uint16_t type