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