ESPHome 2025.9.0-dev
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 APIPlaintextFrameHelper(std::unique_ptr<socket::Socket> socket, const ClientInfo *client_info)
11 : APIFrameHelper(std::move(socket), client_info) {
12 // Plaintext header structure (worst case):
13 // Pos 0: indicator (0x00)
14 // Pos 1-3: payload size varint (up to 3 bytes)
15 // Pos 4-5: message type varint (up to 2 bytes)
16 // Pos 6+: actual payload data
18 }
19 ~APIPlaintextFrameHelper() override = default;
20 APIError init() override;
21 APIError loop() override;
22 APIError read_packet(ReadPacketBuffer *buffer) override;
23 APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) override;
24 APIError write_protobuf_packets(ProtoWriteBuffer buffer, std::span<const PacketInfo> packets) override;
25 uint8_t frame_header_padding() override { return frame_header_padding_; }
26 // Get the frame footer size required by this protocol
27 uint8_t frame_footer_size() override { return frame_footer_size_; }
28
29 protected:
30 APIError try_read_frame_(std::vector<uint8_t> *frame);
31
32 // Group 2-byte aligned types
35
36 // Group 1-byte types together
37 // Fixed-size header buffer for plaintext protocol:
38 // We now store the indicator byte + the two varints.
39 // To match noise protocol's maximum message size (UINT16_MAX = 65535), we need:
40 // 1 byte for indicator + 3 bytes for message size varint (supports up to 2097151) + 2 bytes for message type varint
41 //
42 // While varints could theoretically be up to 10 bytes each for 64-bit values,
43 // attempting to process messages with headers that large would likely crash the
44 // ESP32 due to memory constraints.
45 uint8_t rx_header_buf_[6]; // 1 byte indicator + 5 bytes for varints (3 for size + 2 for type)
46 uint8_t rx_header_buf_pos_ = 0;
47 bool rx_header_parsed_ = false;
48 // 8 bytes total, no padding needed
49};
50
51} // namespace esphome::api
52#endif // USE_API_PLAINTEXT
53#endif // USE_API
APIError try_read_frame_(std::vector< uint8_t > *frame)
Read a packet into the rx_buf_.
APIPlaintextFrameHelper(std::unique_ptr< socket::Socket > socket, const ClientInfo *client_info)
APIError init() override
Initialize the frame helper, returns OK if successful.
APIError write_protobuf_packets(ProtoWriteBuffer buffer, std::span< const PacketInfo > packets) override
~APIPlaintextFrameHelper() override=default
APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) override
APIError read_packet(ReadPacketBuffer *buffer) override
uint8_t type