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