esphome-api-client 0.5.0
Modern C++17 client for the ESPHome native API (plaintext + Noise)
Loading...
Searching...
No Matches
Public Member Functions | Static Public Attributes | List of all members
esphome::api::NoiseFrameHelper Class Referencefinal

Encrypted frame codec. More...

#include <noise_frame_helper.hpp>

Inheritance diagram for esphome::api::NoiseFrameHelper:
Inheritance graph
[legend]
Collaboration diagram for esphome::api::NoiseFrameHelper:
Collaboration graph
[legend]

Public Member Functions

 NoiseFrameHelper (const NoiseConfig &config)
 Construct from config.
 
void encode (std::uint32_t msg_type, ByteView payload, ByteBuffer &out) override
 Serialize a message (numeric type id + protobuf payload) to wire bytes, appending them to out.
 
void feed (ByteView data) override
 Append freshly received bytes to the internal reassembly buffer.
 
FrameStatus next (std::uint32_t &out_type, ByteView &out_payload) override
 Try to extract the next complete frame.
 
bool needs_handshake () const override
 True if the transport requires a handshake before application messages (and before the connection should send HelloRequest).
 
ByteBuffer connect_bytes () override
 Bytes to transmit immediately after the TCP connection is established (the client's first handshake flight).
 
void set_handshake_handlers (HandshakeReady on_ready, HandshakeError on_error) override
 Register callbacks fired when the handshake completes or fails.
 
const std::string & server_name () const noexcept
 
- Public Member Functions inherited from esphome::api::FrameHelper
 FrameHelper ()=default
 
virtual ~FrameHelper ()=default
 
 FrameHelper (const FrameHelper &)=delete
 
FrameHelperoperator= (const FrameHelper &)=delete
 
 FrameHelper (FrameHelper &&)=delete
 
FrameHelperoperator= (FrameHelper &&)=delete
 

Static Public Attributes

static constexpr std::uint8_t indicator = 0x01
 Indicator byte prefixing every Noise frame.
 

Additional Inherited Members

- Public Types inherited from esphome::api::FrameHelper
using HandshakeReady = std::function< void()>
 
using HandshakeError = std::function< void(const std::string &what)>
 

Detailed Description

Encrypted frame codec.

Wire frames are [0x01][u16_be len][ciphertext]. It drives the NNpsk0 handshake internally (server-hello then server-handshake), then encrypts/decrypts application messages whose inner plaintext is [u16_be type][u16_be len][protobuf payload].

Constructor & Destructor Documentation

◆ NoiseFrameHelper()

esphome::api::NoiseFrameHelper::NoiseFrameHelper ( const NoiseConfig config)
explicit

Construct from config.

Throws ApiError if the PSK is not valid base64 of exactly 32 bytes.

Member Function Documentation

◆ connect_bytes()

ByteBuffer esphome::api::NoiseFrameHelper::connect_bytes ( )
overridevirtual

Bytes to transmit immediately after the TCP connection is established (the client's first handshake flight).

Empty for plaintext.

Reimplemented from esphome::api::FrameHelper.

◆ encode()

void esphome::api::NoiseFrameHelper::encode ( std::uint32_t  msg_type,
ByteView  payload,
ByteBuffer out 
)
overridevirtual

Serialize a message (numeric type id + protobuf payload) to wire bytes, appending them to out.

Implements esphome::api::FrameHelper.

◆ feed()

void esphome::api::NoiseFrameHelper::feed ( ByteView  data)
overridevirtual

Append freshly received bytes to the internal reassembly buffer.

Implements esphome::api::FrameHelper.

◆ needs_handshake()

bool esphome::api::NoiseFrameHelper::needs_handshake ( ) const
inlineoverridevirtual

True if the transport requires a handshake before application messages (and before the connection should send HelloRequest).

Reimplemented from esphome::api::FrameHelper.

◆ next()

FrameStatus esphome::api::NoiseFrameHelper::next ( std::uint32_t &  out_type,
ByteView out_payload 
)
overridevirtual

Try to extract the next complete frame.

On FrameStatus::Ok, out_type is the message type id and out_payload is a view of the protobuf payload that stays valid only until the next call to feed or next. On NeedMore nothing was consumed.

Throws ProtocolError on a malformed or oversized frame.

Implements esphome::api::FrameHelper.

◆ server_name()

const std::string & esphome::api::NoiseFrameHelper::server_name ( ) const
inlinenoexcept

◆ set_handshake_handlers()

void esphome::api::NoiseFrameHelper::set_handshake_handlers ( HandshakeReady  ,
HandshakeError   
)
overridevirtual

Register callbacks fired when the handshake completes or fails.

next consumes handshake frames internally and invokes these.

Reimplemented from esphome::api::FrameHelper.

Member Data Documentation

◆ indicator

constexpr std::uint8_t esphome::api::NoiseFrameHelper::indicator = 0x01
staticconstexpr

Indicator byte prefixing every Noise frame.


The documentation for this class was generated from the following file: