ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
api_frame_helper_plaintext.cpp
Go to the documentation of this file.
2#ifdef USE_API
3#ifdef USE_API_PLAINTEXT
4#include "api_connection.h" // For ClientInfo struct
6#include "esphome/core/hal.h"
8#include "esphome/core/log.h"
9#include "proto.h"
10#include <cstring>
11#include <cinttypes>
12
13namespace esphome::api {
14
15static const char *const TAG = "api.plaintext";
16
17#define HELPER_LOG(msg, ...) ESP_LOGVV(TAG, "%s: " msg, this->client_info_->get_combined_info().c_str(), ##__VA_ARGS__)
18
19#ifdef HELPER_LOG_PACKETS
20#define LOG_PACKET_RECEIVED(buffer) ESP_LOGVV(TAG, "Received frame: %s", format_hex_pretty(buffer).c_str())
21#define LOG_PACKET_SENDING(data, len) ESP_LOGVV(TAG, "Sending raw: %s", format_hex_pretty(data, len).c_str())
22#else
23#define LOG_PACKET_RECEIVED(buffer) ((void) 0)
24#define LOG_PACKET_SENDING(data, len) ((void) 0)
25#endif
26
29 APIError err = init_common_();
30 if (err != APIError::OK) {
31 return err;
32 }
33
35 return APIError::OK;
36}
38 if (state_ != State::DATA) {
40 }
41 // Use base class implementation for buffer sending
42 return APIFrameHelper::loop();
43}
44
55 if (frame == nullptr) {
56 HELPER_LOG("Bad argument for try_read_frame_");
57 return APIError::BAD_ARG;
58 }
59
60 // read header
61 while (!rx_header_parsed_) {
62 // Now that we know when the socket is ready, we can read up to 3 bytes
63 // into the rx_header_buf_ before we have to switch back to reading
64 // one byte at a time to ensure we don't read past the message and
65 // into the next one.
66
67 // Read directly into rx_header_buf_ at the current position
68 // Try to get to at least 3 bytes total (indicator + 2 varint bytes), then read one byte at a time
69 ssize_t received =
70 this->socket_->read(&rx_header_buf_[rx_header_buf_pos_], rx_header_buf_pos_ < 3 ? 3 - rx_header_buf_pos_ : 1);
72 if (err != APIError::OK) {
73 return err;
74 }
75
76 // If this was the first read, validate the indicator byte
77 if (rx_header_buf_pos_ == 0 && received > 0) {
78 if (rx_header_buf_[0] != 0x00) {
80 HELPER_LOG("Bad indicator byte %u", rx_header_buf_[0]);
82 }
83 }
84
85 rx_header_buf_pos_ += received;
86
87 // Check for buffer overflow
88 if (rx_header_buf_pos_ >= sizeof(rx_header_buf_)) {
90 HELPER_LOG("Header buffer overflow");
92 }
93
94 // Need at least 3 bytes total (indicator + 2 varint bytes) before trying to parse
95 if (rx_header_buf_pos_ < 3) {
96 continue;
97 }
98
99 // At this point, we have at least 3 bytes total:
100 // - Validated indicator byte (0x00) stored at position 0
101 // - At least 2 bytes in the buffer for the varints
102 // Buffer layout:
103 // [0]: indicator byte (0x00)
104 // [1-3]: Message size varint (variable length)
105 // - 2 bytes would only allow up to 16383, which is less than noise's UINT16_MAX (65535)
106 // - 3 bytes allows up to 2097151, ensuring we support at least as much as noise
107 // [2-5]: Message type varint (variable length)
108 // We now attempt to parse both varints. If either is incomplete,
109 // we'll continue reading more bytes.
110
111 // Skip indicator byte at position 0
112 uint8_t varint_pos = 1;
113 uint32_t consumed = 0;
114
115 auto msg_size_varint = ProtoVarInt::parse(&rx_header_buf_[varint_pos], rx_header_buf_pos_ - varint_pos, &consumed);
116 if (!msg_size_varint.has_value()) {
117 // not enough data there yet
118 continue;
119 }
120
121 if (msg_size_varint->as_uint32() > std::numeric_limits<uint16_t>::max()) {
123 HELPER_LOG("Bad packet: message size %" PRIu32 " exceeds maximum %u", msg_size_varint->as_uint32(),
124 std::numeric_limits<uint16_t>::max());
126 }
127 rx_header_parsed_len_ = msg_size_varint->as_uint16();
128
129 // Move to next varint position
130 varint_pos += consumed;
131
132 auto msg_type_varint = ProtoVarInt::parse(&rx_header_buf_[varint_pos], rx_header_buf_pos_ - varint_pos, &consumed);
133 if (!msg_type_varint.has_value()) {
134 // not enough data there yet
135 continue;
136 }
137 if (msg_type_varint->as_uint32() > std::numeric_limits<uint16_t>::max()) {
139 HELPER_LOG("Bad packet: message type %" PRIu32 " exceeds maximum %u", msg_type_varint->as_uint32(),
140 std::numeric_limits<uint16_t>::max());
142 }
143 rx_header_parsed_type_ = msg_type_varint->as_uint16();
144 rx_header_parsed_ = true;
145 }
146 // header reading done
147
148 // reserve space for body
149 if (rx_buf_.size() != rx_header_parsed_len_) {
151 }
152
154 // more data to read
155 uint16_t to_read = rx_header_parsed_len_ - rx_buf_len_;
156 ssize_t received = this->socket_->read(&rx_buf_[rx_buf_len_], to_read);
157 APIError err = handle_socket_read_result_(received);
158 if (err != APIError::OK) {
159 return err;
160 }
161 rx_buf_len_ += static_cast<uint16_t>(received);
162 if (static_cast<uint16_t>(received) != to_read) {
163 // not all read
165 }
166 }
167
168 LOG_PACKET_RECEIVED(rx_buf_);
169 *frame = std::move(rx_buf_);
170 // consume msg
171 rx_buf_ = {};
172 rx_buf_len_ = 0;
174 rx_header_parsed_ = false;
175 return APIError::OK;
176}
178 APIError aerr;
179
180 if (state_ != State::DATA) {
182 }
183
184 std::vector<uint8_t> frame;
185 aerr = try_read_frame_(&frame);
186 if (aerr != APIError::OK) {
187 if (aerr == APIError::BAD_INDICATOR) {
188 // Make sure to tell the remote that we don't
189 // understand the indicator byte so it knows
190 // we do not support it.
191 struct iovec iov[1];
192 // The \x00 first byte is the marker for plaintext.
193 //
194 // The remote will know how to handle the indicator byte,
195 // but it likely won't understand the rest of the message.
196 //
197 // We must send at least 3 bytes to be read, so we add
198 // a message after the indicator byte to ensures its long
199 // enough and can aid in debugging.
200 const char msg[] = "\x00"
201 "Bad indicator byte";
202 iov[0].iov_base = (void *) msg;
203 iov[0].iov_len = 19;
204 this->write_raw_(iov, 1, 19);
205 }
206 return aerr;
207 }
208
209 buffer->container = std::move(frame);
210 buffer->data_offset = 0;
213 return APIError::OK;
214}
216 PacketInfo packet{type, 0, static_cast<uint16_t>(buffer.get_buffer()->size() - frame_header_padding_)};
217 return write_protobuf_packets(buffer, std::span<const PacketInfo>(&packet, 1));
218}
219
221 if (state_ != State::DATA) {
222 return APIError::BAD_STATE;
223 }
224
225 if (packets.empty()) {
226 return APIError::OK;
227 }
228
229 std::vector<uint8_t> *raw_buffer = buffer.get_buffer();
230 uint8_t *buffer_data = raw_buffer->data(); // Cache buffer pointer
231
232 this->reusable_iovs_.clear();
233 this->reusable_iovs_.reserve(packets.size());
234 uint16_t total_write_len = 0;
235
236 for (const auto &packet : packets) {
237 // Calculate varint sizes for header layout
238 uint8_t size_varint_len = api::ProtoSize::varint(static_cast<uint32_t>(packet.payload_size));
239 uint8_t type_varint_len = api::ProtoSize::varint(static_cast<uint32_t>(packet.message_type));
240 uint8_t total_header_len = 1 + size_varint_len + type_varint_len;
241
242 // Calculate where to start writing the header
243 // The header starts at the latest possible position to minimize unused padding
244 //
245 // Example 1 (small values): total_header_len = 3, header_offset = 6 - 3 = 3
246 // [0-2] - Unused padding
247 // [3] - 0x00 indicator byte
248 // [4] - Payload size varint (1 byte, for sizes 0-127)
249 // [5] - Message type varint (1 byte, for types 0-127)
250 // [6...] - Actual payload data
251 //
252 // Example 2 (medium values): total_header_len = 4, header_offset = 6 - 4 = 2
253 // [0-1] - Unused padding
254 // [2] - 0x00 indicator byte
255 // [3-4] - Payload size varint (2 bytes, for sizes 128-16383)
256 // [5] - Message type varint (1 byte, for types 0-127)
257 // [6...] - Actual payload data
258 //
259 // Example 3 (large values): total_header_len = 6, header_offset = 6 - 6 = 0
260 // [0] - 0x00 indicator byte
261 // [1-3] - Payload size varint (3 bytes, for sizes 16384-2097151)
262 // [4-5] - Message type varint (2 bytes, for types 128-32767)
263 // [6...] - Actual payload data
264 //
265 // The message starts at offset + frame_header_padding_
266 // So we write the header starting at offset + frame_header_padding_ - total_header_len
267 uint8_t *buf_start = buffer_data + packet.offset;
268 uint32_t header_offset = frame_header_padding_ - total_header_len;
269
270 // Write the plaintext header
271 buf_start[header_offset] = 0x00; // indicator
272
273 // Encode varints directly into buffer
274 ProtoVarInt(packet.payload_size).encode_to_buffer_unchecked(buf_start + header_offset + 1, size_varint_len);
275 ProtoVarInt(packet.message_type)
276 .encode_to_buffer_unchecked(buf_start + header_offset + 1 + size_varint_len, type_varint_len);
277
278 // Add iovec for this packet (header + payload)
279 size_t packet_len = static_cast<size_t>(total_header_len + packet.payload_size);
280 this->reusable_iovs_.push_back({buf_start + header_offset, packet_len});
281 total_write_len += packet_len;
282 }
283
284 // Send all packets in one writev call
285 return write_raw_(this->reusable_iovs_.data(), this->reusable_iovs_.size(), total_write_len);
286}
287
288} // namespace esphome::api
289#endif // USE_API_PLAINTEXT
290#endif // USE_API
APIError handle_socket_read_result_(ssize_t received)
std::vector< uint8_t > rx_buf_
std::vector< struct iovec > reusable_iovs_
APIError write_raw_(const struct iovec *iov, int iovcnt, uint16_t total_write_len)
APIError try_read_frame_(std::vector< uint8_t > *frame)
Read a packet into the rx_buf_.
APIError init() override
Initialize the frame helper, returns OK if successful.
APIError write_protobuf_packets(ProtoWriteBuffer buffer, std::span< const PacketInfo > packets) override
APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) override
APIError read_packet(ReadPacketBuffer *buffer) override
static constexpr uint32_t varint(uint32_t value)
Calculates the size in bytes needed to encode a uint32_t value as a varint.
Definition proto.h:395
Representation of a VarInt - in ProtoBuf should be 64bit but we only use 32bit.
Definition proto.h:66
void encode_to_buffer_unchecked(uint8_t *buffer, size_t len)
Encode the varint value to a pre-allocated buffer without bounds checking.
Definition proto.h:138
static optional< ProtoVarInt > parse(const uint8_t *buffer, uint32_t len, uint32_t *consumed)
Definition proto.h:71
std::vector< uint8_t > * get_buffer() const
Definition proto.h:327
virtual ssize_t read(void *buf, size_t len)=0
uint8_t type
__int64 ssize_t
Definition httplib.h:175
std::vector< uint8_t > container
void * iov_base
Definition headers.h:101
size_t iov_len
Definition headers.h:102