ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
api_frame_helper_noise.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_NOISE
5#include "noise/protocol.h"
6#include "api_noise_context.h"
7
8namespace esphome::api {
9
10class APINoiseFrameHelper final : public APIFrameHelper {
11 public:
12 APINoiseFrameHelper(std::unique_ptr<socket::Socket> socket, APINoiseContext &ctx, const ClientInfo *client_info)
13 : APIFrameHelper(std::move(socket), client_info), ctx_(ctx) {
14 // Noise header structure:
15 // Pos 0: indicator (0x01)
16 // Pos 1-2: encrypted payload size (16-bit big-endian)
17 // Pos 3-6: encrypted type (16-bit) + data_len (16-bit)
18 // Pos 7+: actual payload data
20 }
21 ~APINoiseFrameHelper() override;
22 APIError init() override;
23 APIError loop() override;
24 APIError read_packet(ReadPacketBuffer *buffer) override;
25 APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) override;
26 APIError write_protobuf_packets(ProtoWriteBuffer buffer, std::span<const PacketInfo> packets) override;
27
28 protected:
31 APIError write_frame_(const uint8_t *data, uint16_t len);
34 void send_explicit_handshake_reject_(const LogString *reason);
36 APIError handle_noise_error_(int err, const LogString *func_name, APIError api_err);
37
38 // Pointers first (4 bytes each)
39 NoiseHandshakeState *handshake_{nullptr};
40 NoiseCipherState *send_cipher_{nullptr};
41 NoiseCipherState *recv_cipher_{nullptr};
42
43 // Reference to noise context (4 bytes on 32-bit)
45
46 // Vector (12 bytes on 32-bit)
47 std::vector<uint8_t> prologue_;
48
49 // NoiseProtocolId (size depends on implementation)
50 NoiseProtocolId nid_;
51
52 // Group small types together
53 // Fixed-size header buffer for noise protocol:
54 // 1 byte for indicator + 2 bytes for message size (16-bit value, not varint)
55 // Note: Maximum message size is UINT16_MAX (65535), with a limit of 128 bytes during handshake phase
56 uint8_t rx_header_buf_[3];
57 uint8_t rx_header_buf_len_ = 0;
58 // 4 bytes total, no padding
59};
60
61} // namespace esphome::api
62#endif // USE_API_NOISE
63#endif // USE_API
APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) override
APIError read_packet(ReadPacketBuffer *buffer) override
APIError write_protobuf_packets(ProtoWriteBuffer buffer, std::span< const PacketInfo > packets) override
APIError handle_noise_error_(int err, const LogString *func_name, APIError api_err)
APIError state_action_()
To be called from read/write methods.
APIError try_read_frame_()
Read a packet into the rx_buf_.
APIError loop() override
Run through handshake messages (if in that phase)
APIError handle_handshake_frame_error_(APIError aerr)
void send_explicit_handshake_reject_(const LogString *reason)
APIError write_frame_(const uint8_t *data, uint16_t len)
APIError init() override
Initialize the frame helper, returns OK if successful.
APINoiseFrameHelper(std::unique_ptr< socket::Socket > socket, APINoiseContext &ctx, const ClientInfo *client_info)
APIError init_handshake_()
Initiate the data structures for the handshake.
uint16_t type
std::string size_t len
Definition helpers.h:533