ESPHome 2026.5.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
5#include "esphome/core/hal.h"
7#include "esphome/core/log.h"
8#include "proto.h"
9#include <cstring>
10#include <cinttypes>
11
12#ifdef USE_ESP8266
13#include <pgmspace.h>
14#endif
15
16namespace esphome::api {
17
18static const char *const TAG = "api.plaintext";
19
20// Maximum bytes to log in hex format (168 * 3 = 504, under TX buffer size of 512)
21static constexpr size_t API_MAX_LOG_BYTES = 168;
22
23#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
24#define HELPER_LOG(msg, ...) \
25 do { \
26 char peername_buf[socket::SOCKADDR_STR_LEN]; \
27 this->get_peername_to(peername_buf); \
28 ESP_LOGVV(TAG, "%s (%s): " msg, this->client_name_, peername_buf, ##__VA_ARGS__); \
29 } while (0)
30#else
31#define HELPER_LOG(msg, ...) ((void) 0)
32#endif
33
34#ifdef HELPER_LOG_PACKETS
35#define LOG_PACKET_RECEIVED(buffer) \
36 do { \
37 char hex_buf_[format_hex_pretty_size(API_MAX_LOG_BYTES)]; \
38 ESP_LOGVV(TAG, "Received frame: %s", \
39 format_hex_pretty_to(hex_buf_, (buffer).data(), \
40 (buffer).size() < API_MAX_LOG_BYTES ? (buffer).size() : API_MAX_LOG_BYTES)); \
41 } while (0)
42#else
43#define LOG_PACKET_RECEIVED(buffer) ((void) 0)
44#endif
45
48 APIError err = init_common_();
49 if (err != APIError::OK) {
50 return err;
51 }
52
54 return APIError::OK;
55}
57 if (state_ != State::DATA) {
59 }
60 if (!this->overflow_buf_.empty()) [[unlikely]] {
62 }
63 return APIError::OK;
64}
65
73 // read header
74 while (!rx_header_parsed_) {
75 // Now that we know when the socket is ready, we can read up to 3 bytes
76 // into the rx_header_buf_ before we have to switch back to reading
77 // one byte at a time to ensure we don't read past the message and
78 // into the next one.
79
80 // Read directly into rx_header_buf_ at the current position
81 // Try to get to at least 3 bytes total (indicator + 2 varint bytes), then read one byte at a time
82 ssize_t received =
83 this->socket_->read(&rx_header_buf_[rx_header_buf_pos_], rx_header_buf_pos_ < 3 ? 3 - rx_header_buf_pos_ : 1);
85 if (err != APIError::OK) {
86 return err;
87 }
88
89 // If this was the first read, validate the indicator byte
90 if (rx_header_buf_pos_ == 0 && received > 0) {
91 if (rx_header_buf_[0] != 0x00) {
93 HELPER_LOG("Bad indicator byte %u", rx_header_buf_[0]);
95 }
96 }
97
98 rx_header_buf_pos_ += received;
99
100 // Check for buffer overflow
101 if (rx_header_buf_pos_ >= sizeof(rx_header_buf_)) {
103 HELPER_LOG("Header buffer overflow");
105 }
106
107 // Need at least 3 bytes total (indicator + 2 varint bytes) before trying to parse
108 if (rx_header_buf_pos_ < 3) {
109 continue;
110 }
111
112 // At this point, we have at least 3 bytes total:
113 // - Validated indicator byte (0x00) stored at position 0
114 // - At least 2 bytes in the buffer for the varints
115 // Buffer layout:
116 // [0]: indicator byte (0x00)
117 // [1-3]: Message size varint (variable length)
118 // - 2 bytes would only allow up to 16383, which is less than noise's UINT16_MAX (65535)
119 // - 3 bytes allows up to 2097151, ensuring we support at least as much as noise
120 // [2-5]: Message type varint (variable length)
121 // We now attempt to parse both varints. If either is incomplete,
122 // we'll continue reading more bytes.
123
124 // Skip indicator byte at position 0
125 uint8_t varint_pos = 1;
126
127 // rx_header_buf_pos_ >= 3 and varint_pos == 1, so len >= 2
128 auto msg_size_varint = ProtoVarInt::parse_non_empty(&rx_header_buf_[varint_pos], rx_header_buf_pos_ - varint_pos);
129 if (!msg_size_varint.has_value()) {
130 // not enough data there yet
131 continue;
132 }
133
134 if (msg_size_varint.value > MAX_MESSAGE_SIZE) {
136 HELPER_LOG("Bad packet: message size %" PRIu32 " exceeds maximum %u",
137 static_cast<uint32_t>(msg_size_varint.value), MAX_MESSAGE_SIZE);
139 }
140 rx_header_parsed_len_ = static_cast<uint16_t>(msg_size_varint.value);
141
142 // Move to next varint position
143 varint_pos += msg_size_varint.consumed;
144
145 auto msg_type_varint = ProtoVarInt::parse(&rx_header_buf_[varint_pos], rx_header_buf_pos_ - varint_pos);
146 if (!msg_type_varint.has_value()) {
147 // not enough data there yet
148 continue;
149 }
150 if (msg_type_varint.value > std::numeric_limits<uint16_t>::max()) {
152 HELPER_LOG("Bad packet: message type %" PRIu32 " exceeds maximum %u",
153 static_cast<uint32_t>(msg_type_varint.value), std::numeric_limits<uint16_t>::max());
155 }
156 rx_header_parsed_type_ = static_cast<uint16_t>(msg_type_varint.value);
157 rx_header_parsed_ = true;
158 }
159 // header reading done
160
161 // Reserve space for body (+ null terminator so protobuf StringRef fields
162 // can be safely null-terminated in-place after decode)
163 this->rx_buf_.resize(this->rx_header_parsed_len_ + RX_BUF_NULL_TERMINATOR);
164
166 // more data to read
167 uint16_t to_read = rx_header_parsed_len_ - rx_buf_len_;
168 ssize_t received = this->socket_->read(&rx_buf_[rx_buf_len_], to_read);
169 APIError err = handle_socket_read_result_(received);
170 if (err != APIError::OK) {
171 return err;
172 }
173 rx_buf_len_ += static_cast<uint16_t>(received);
174 if (static_cast<uint16_t>(received) != to_read) {
175 // not all read
177 }
178 }
179
180 LOG_PACKET_RECEIVED(this->rx_buf_);
181
182 // Clear state for next frame (rx_buf_ still contains data for caller)
183 this->rx_buf_len_ = 0;
184 this->rx_header_buf_pos_ = 0;
185 this->rx_header_parsed_ = false;
186
187 return APIError::OK;
188}
189
191 APIError aerr = this->check_data_state_();
192 if (aerr != APIError::OK)
193 return aerr;
194
195 aerr = this->try_read_frame_();
196 if (aerr != APIError::OK) {
197 if (aerr == APIError::BAD_INDICATOR) {
198 // Make sure to tell the remote that we don't
199 // understand the indicator byte so it knows
200 // we do not support it.
201 // The \x00 first byte is the marker for plaintext.
202 //
203 // The remote will know how to handle the indicator byte,
204 // but it likely won't understand the rest of the message.
205 //
206 // We must send at least 3 bytes to be read, so we add
207 // a message after the indicator byte to ensures its long
208 // enough and can aid in debugging.
209 static constexpr uint8_t INDICATOR_MSG_SIZE = 19;
210#ifdef USE_ESP8266
211 static const char MSG_PROGMEM[] PROGMEM = "\x00"
212 "Bad indicator byte";
213 char msg[INDICATOR_MSG_SIZE];
214 memcpy_P(msg, MSG_PROGMEM, INDICATOR_MSG_SIZE);
215 this->write_raw_buf_(msg, INDICATOR_MSG_SIZE);
216#else
217 static const char MSG[] = "\x00"
218 "Bad indicator byte";
219 this->write_raw_buf_(MSG, INDICATOR_MSG_SIZE);
220#endif
221 }
222 return aerr;
223 }
224
225 buffer->data = this->rx_buf_.data();
226 buffer->data_len = this->rx_header_parsed_len_;
227 buffer->type = this->rx_header_parsed_type_;
228 return APIError::OK;
229}
230
231// Encode a 16-bit varint (1-3 bytes) using pre-computed length.
232ESPHOME_ALWAYS_INLINE static inline void encode_varint_16(uint16_t value, uint8_t varint_len, uint8_t *p) {
233 if (varint_len >= 2) {
234 *p++ = static_cast<uint8_t>(value | 0x80);
235 value >>= 7;
236 if (varint_len == 3) {
237 *p++ = static_cast<uint8_t>(value | 0x80);
238 value >>= 7;
239 }
240 }
241 *p = static_cast<uint8_t>(value);
242}
243
244// Encode an 8-bit varint (1-2 bytes) using pre-computed length.
245ESPHOME_ALWAYS_INLINE static inline void encode_varint_8(uint8_t value, uint8_t varint_len, uint8_t *p) {
246 if (varint_len == 2) {
247 *p++ = static_cast<uint8_t>(value | 0x80);
248 *p = static_cast<uint8_t>(value >> 7);
249 } else {
250 *p = value;
251 }
252}
253
254// Write plaintext header into pre-allocated padding before payload.
255// padding_size: bytes reserved before payload (HEADER_PADDING for first/single msg,
256// actual header size for contiguous batch messages).
257// Returns the total header length (indicator + varints).
258ESPHOME_ALWAYS_INLINE static inline uint8_t write_plaintext_header(uint8_t *buf_start, uint16_t payload_size,
259 uint8_t message_type, uint8_t padding_size) {
260 uint8_t size_varint_len = ProtoSize::varint16(payload_size);
261 uint8_t type_varint_len = ProtoSize::varint8(message_type);
262 uint8_t total_header_len = 1 + size_varint_len + type_varint_len;
263
264 // The header is right-justified within the padding so it sits immediately before payload.
265 //
266 // Single/first message (padding_size = HEADER_PADDING = 6):
267 // Example (small, header=3): [0-2] unused | [3] 0x00 | [4] size | [5] type | [6...] payload
268 // Example (medium, header=4): [0-1] unused | [2] 0x00 | [3-4] size | [5] type | [6...] payload
269 // Example (large, header=6): [0] 0x00 | [1-3] size | [4-5] type | [6...] payload
270 //
271 // Batch messages 2+ (padding_size = actual header size, no unused bytes):
272 // Example (small, header=3): [0] 0x00 | [1] size | [2] type | [3...] payload
273 // Example (medium, header=4): [0] 0x00 | [1-2] size | [3] type | [4...] payload
274#ifdef ESPHOME_DEBUG_API
275 assert(padding_size >= total_header_len);
276#endif
277 uint32_t header_offset = padding_size - total_header_len;
278
279 // Write the plaintext header
280 buf_start[header_offset] = 0x00; // indicator
281
282 // Encode varints directly into buffer using pre-computed lengths
283 encode_varint_16(payload_size, size_varint_len, buf_start + header_offset + 1);
284 encode_varint_8(message_type, type_varint_len, buf_start + header_offset + 1 + size_varint_len);
285
286 return total_header_len;
287}
288
290#ifdef ESPHOME_DEBUG_API
291 assert(this->state_ == State::DATA);
292#endif
293
294 uint16_t payload_size = static_cast<uint16_t>(buffer.get_buffer()->size() - HEADER_PADDING);
295 uint8_t *buffer_data = buffer.get_buffer()->data();
296 uint8_t header_len = write_plaintext_header(buffer_data, payload_size, type, HEADER_PADDING);
297 return this->write_raw_fast_buf_(buffer_data + HEADER_PADDING - header_len,
298 static_cast<uint16_t>(header_len + payload_size));
299}
300
302 std::span<const MessageInfo> messages) {
303#ifdef ESPHOME_DEBUG_API
304 assert(this->state_ == State::DATA);
305 assert(!messages.empty());
306#endif
307 uint8_t *buffer_data = buffer.get_buffer()->data();
308
309 // First message has max padding (header_size = HEADER_PADDING), may have unused leading bytes.
310 // Subsequent messages were encoded with exact header sizes (header_size = actual header len).
311 // write_plaintext_header right-justifies the header within header_size bytes of padding.
312 const auto &first = messages[0];
313 uint8_t *first_start = buffer_data + first.offset;
314 uint8_t header_len = write_plaintext_header(first_start, first.payload_size, first.message_type, HEADER_PADDING);
315 uint8_t *write_start = first_start + HEADER_PADDING - header_len;
316 uint16_t total_len = header_len + first.payload_size;
317
318 for (size_t i = 1; i < messages.size(); i++) {
319 const auto &msg = messages[i];
320 header_len = write_plaintext_header(buffer_data + msg.offset, msg.payload_size, msg.message_type, msg.header_size);
321 total_len += header_len + msg.payload_size;
322 }
323
324 return this->write_raw_fast_buf_(write_start, total_len);
325}
326
327} // namespace esphome::api
328#endif // USE_API_PLAINTEXT
329#endif // USE_API
void resize(size_t n) ESPHOME_ALWAYS_INLINE
Definition api_buffer.h:43
APIError handle_socket_read_result_(ssize_t received)
APIError ESPHOME_ALWAYS_INLINE write_raw_fast_buf_(const void *data, uint16_t len)
APIError write_raw_buf_(const void *data, uint16_t len, ssize_t sent=WRITE_NOT_ATTEMPTED)
std::unique_ptr< socket::Socket > socket_
APIError ESPHOME_ALWAYS_INLINE check_data_state_() const
bool empty() const
True when no backlogged data is waiting.
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_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
static constexpr uint8_t ESPHOME_ALWAYS_INLINE varint8(uint8_t value)
Definition proto.h:661
static constexpr uint8_t ESPHOME_ALWAYS_INLINE varint16(uint16_t value)
Definition proto.h:656
static ProtoVarIntResult ESPHOME_ALWAYS_INLINE parse_non_empty(const uint8_t *buffer, uint32_t len)
Parse a varint from buffer.
Definition proto.h:140
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:153
APIBuffer * get_buffer() const
Definition proto.h:271
uint16_t type
__int64 ssize_t
Definition httplib.h:178
uint16_t size
Definition helpers.cpp:25
static void uint32_t
uint32_t payload_size()