ESPHome 2025.9.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
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 // Get the frame header padding required by this protocol
29 uint8_t frame_header_padding() override { return frame_header_padding_; }
30 // Get the frame footer size required by this protocol
31 uint8_t frame_footer_size() override { return frame_footer_size_; }
32
33 protected:
35 APIError try_read_frame_(std::vector<uint8_t> *frame);
36 APIError write_frame_(const uint8_t *data, uint16_t len);
39 void send_explicit_handshake_reject_(const std::string &reason);
41 APIError handle_noise_error_(int err, const char *func_name, APIError api_err);
42
43 // Pointers first (4 bytes each)
44 NoiseHandshakeState *handshake_{nullptr};
45 NoiseCipherState *send_cipher_{nullptr};
46 NoiseCipherState *recv_cipher_{nullptr};
47
48 // Shared pointer (8 bytes on 32-bit = 4 bytes control block pointer + 4 bytes object pointer)
49 std::shared_ptr<APINoiseContext> ctx_;
50
51 // Vector (12 bytes on 32-bit)
52 std::vector<uint8_t> prologue_;
53
54 // NoiseProtocolId (size depends on implementation)
55 NoiseProtocolId nid_;
56
57 // Group small types together
58 // Fixed-size header buffer for noise protocol:
59 // 1 byte for indicator + 2 bytes for message size (16-bit value, not varint)
60 // Note: Maximum message size is UINT16_MAX (65535), with a limit of 128 bytes during handshake phase
61 uint8_t rx_header_buf_[3];
62 uint8_t rx_header_buf_len_ = 0;
63 // 4 bytes total, no padding
64};
65
66} // namespace esphome::api
67#endif // USE_API_NOISE
68#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 state_action_()
To be called from read/write methods.
APIError handle_noise_error_(int err, const char *func_name, APIError api_err)
APIError loop() override
Run through handshake messages (if in that phase)
APIError handle_handshake_frame_error_(APIError aerr)
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.
void send_explicit_handshake_reject_(const std::string &reason)
APIError init_handshake_()
Initiate the data structures for the handshake.
uint8_t type
std::string size_t len
Definition helpers.h:279