ESPHome  2022.9.1
Data Structures | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes
esphome::api::APINoiseFrameHelper Class Reference

#include <api_frame_helper.h>

Inheritance diagram for esphome::api::APINoiseFrameHelper:
esphome::api::APIFrameHelper

Data Structures

struct  ParsedFrame
 

Public Member Functions

 APINoiseFrameHelper (std::unique_ptr< socket::Socket > socket, std::shared_ptr< APINoiseContext > ctx)
 
 ~APINoiseFrameHelper () override
 
APIError init () override
 Initialize the frame helper, returns OK if successful. More...
 
APIError loop () override
 Run through handshake messages (if in that phase) More...
 
APIError read_packet (ReadPacketBuffer *buffer) override
 
bool can_write_without_blocking () override
 
APIError write_packet (uint16_t type, const uint8_t *payload, size_t len) override
 
std::string getpeername () override
 
APIError close () override
 
APIError shutdown (int how) override
 
void set_log_info (std::string info) override
 
- Public Member Functions inherited from esphome::api::APIFrameHelper
virtual ~APIFrameHelper ()=default
 

Protected Types

enum  State {
  State::INITIALIZE = 1, State::CLIENT_HELLO = 2, State::SERVER_HELLO = 3, State::HANDSHAKE = 4,
  State::DATA = 5, State::CLOSED = 6, State::FAILED = 7, State::EXPLICIT_REJECT = 8
}
 

Protected Member Functions

APIError state_action_ ()
 To be called from read/write methods. More...
 
APIError try_read_frame_ (ParsedFrame *frame)
 Read a packet into the rx_buf_. More...
 
APIError try_send_tx_buf_ ()
 
APIError write_frame_ (const uint8_t *data, size_t len)
 
APIError write_raw_ (const struct iovec *iov, int iovcnt)
 Write the data to the socket, or buffer it a write would block. More...
 
APIError init_handshake_ ()
 Initiate the data structures for the handshake. More...
 
APIError check_handshake_finished_ ()
 
void send_explicit_handshake_reject_ (const std::string &reason)
 

Protected Attributes

std::unique_ptr< socket::Socketsocket_
 
std::string info_
 
uint8_t rx_header_buf_ [3]
 
size_t rx_header_buf_len_ = 0
 
std::vector< uint8_t > rx_buf_
 
size_t rx_buf_len_ = 0
 
std::vector< uint8_t > tx_buf_
 
std::vector< uint8_t > prologue_
 
std::shared_ptr< APINoiseContextctx_
 
NoiseHandshakeState * handshake_ {nullptr}
 
NoiseCipherState * send_cipher_ {nullptr}
 
NoiseCipherState * recv_cipher_ {nullptr}
 
NoiseProtocolId nid_
 
enum esphome::api::APINoiseFrameHelper::State state_ = State::INITIALIZE
 

Detailed Description

Definition at line 77 of file api_frame_helper.h.

Member Enumeration Documentation

◆ State

Enumerator
INITIALIZE 
CLIENT_HELLO 
SERVER_HELLO 
HANDSHAKE 
DATA 
CLOSED 
FAILED 
EXPLICIT_REJECT 

Definition at line 124 of file api_frame_helper.h.

Constructor & Destructor Documentation

◆ APINoiseFrameHelper()

esphome::api::APINoiseFrameHelper::APINoiseFrameHelper ( std::unique_ptr< socket::Socket socket,
std::shared_ptr< APINoiseContext ctx 
)
inline

Definition at line 79 of file api_frame_helper.h.

◆ ~APINoiseFrameHelper()

esphome::api::APINoiseFrameHelper::~APINoiseFrameHelper ( )
override

Definition at line 700 of file api_frame_helper.cpp.

Member Function Documentation

◆ can_write_without_blocking()

bool esphome::api::APINoiseFrameHelper::can_write_without_blocking ( )
overridevirtual

Implements esphome::api::APIFrameHelper.

Definition at line 469 of file api_frame_helper.cpp.

◆ check_handshake_finished_()

APIError esphome::api::APINoiseFrameHelper::check_handshake_finished_ ( )
protected

Definition at line 675 of file api_frame_helper.cpp.

◆ close()

APIError esphome::api::APINoiseFrameHelper::close ( )
overridevirtual

Implements esphome::api::APIFrameHelper.

Definition at line 715 of file api_frame_helper.cpp.

◆ getpeername()

std::string esphome::api::APINoiseFrameHelper::getpeername ( )
inlineoverridevirtual

Implements esphome::api::APIFrameHelper.

Definition at line 87 of file api_frame_helper.h.

◆ init()

APIError esphome::api::APINoiseFrameHelper::init ( )
overridevirtual

Initialize the frame helper, returns OK if successful.

Implements esphome::api::APIFrameHelper.

Definition at line 122 of file api_frame_helper.cpp.

◆ init_handshake_()

APIError esphome::api::APINoiseFrameHelper::init_handshake_ ( )
protected

Initiate the data structures for the handshake.

Returns
0 on success, -1 on error (check errno)

Definition at line 629 of file api_frame_helper.cpp.

◆ loop()

APIError esphome::api::APINoiseFrameHelper::loop ( )
overridevirtual

Run through handshake messages (if in that phase)

Implements esphome::api::APIFrameHelper.

Definition at line 149 of file api_frame_helper.cpp.

◆ read_packet()

APIError esphome::api::APINoiseFrameHelper::read_packet ( ReadPacketBuffer buffer)
overridevirtual

Implements esphome::api::APIFrameHelper.

Definition at line 416 of file api_frame_helper.cpp.

◆ send_explicit_handshake_reject_()

void esphome::api::APINoiseFrameHelper::send_explicit_handshake_reject_ ( const std::string &  reason)
protected

Definition at line 402 of file api_frame_helper.cpp.

◆ set_log_info()

void esphome::api::APINoiseFrameHelper::set_log_info ( std::string  info)
inlineoverridevirtual

Implements esphome::api::APIFrameHelper.

Definition at line 91 of file api_frame_helper.h.

◆ shutdown()

APIError esphome::api::APINoiseFrameHelper::shutdown ( int  how)
overridevirtual

Implements esphome::api::APIFrameHelper.

Definition at line 722 of file api_frame_helper.cpp.

◆ state_action_()

APIError esphome::api::APINoiseFrameHelper::state_action_ ( )
protected

To be called from read/write methods.

This method runs through the internal handshake methods, if in that state.

If the handshake is still active when this method returns and a read/write can't take place at the moment, returns WOULD_BLOCK. If an error occurred, returns that error. Only returns OK if the transport is ready for data traffic.

Definition at line 276 of file api_frame_helper.cpp.

◆ try_read_frame_()

APIError esphome::api::APINoiseFrameHelper::try_read_frame_ ( ParsedFrame frame)
protected

Read a packet into the rx_buf_.

If successful, stores frame data in the frame parameter

Parameters
frameThe struct to hold the frame information in. msg_start: points to the start of the payload - this pointer is only valid until the next try_receive_raw_ call
Returns
0 if a full packet is in rx_buf_
-1 if error, check errno.

errno EWOULDBLOCK: Packet could not be read without blocking. Try again later. errno ENOMEM: Not enough memory for reading packet. errno API_ERROR_BAD_INDICATOR: Bad indicator byte at start of frame. errno API_ERROR_HANDSHAKE_PACKET_LEN: Packet too big for this phase.

Definition at line 178 of file api_frame_helper.cpp.

◆ try_send_tx_buf_()

APIError esphome::api::APINoiseFrameHelper::try_send_tx_buf_ ( )
protected

Definition at line 525 of file api_frame_helper.cpp.

◆ write_frame_()

APIError esphome::api::APINoiseFrameHelper::write_frame_ ( const uint8_t *  data,
size_t  len 
)
protected

Definition at line 610 of file api_frame_helper.cpp.

◆ write_packet()

APIError esphome::api::APINoiseFrameHelper::write_packet ( uint16_t  type,
const uint8_t *  payload,
size_t  len 
)
overridevirtual

Implements esphome::api::APIFrameHelper.

Definition at line 470 of file api_frame_helper.cpp.

◆ write_raw_()

APIError esphome::api::APINoiseFrameHelper::write_raw_ ( const struct iovec iov,
int  iovcnt 
)
protected

Write the data to the socket, or buffer it a write would block.

Parameters
dataThe data to write
lenThe length of data

Definition at line 550 of file api_frame_helper.cpp.

Field Documentation

◆ ctx_

std::shared_ptr<APINoiseContext> esphome::api::APINoiseFrameHelper::ctx_
protected

Definition at line 118 of file api_frame_helper.h.

◆ handshake_

NoiseHandshakeState* esphome::api::APINoiseFrameHelper::handshake_ {nullptr}
protected

Definition at line 119 of file api_frame_helper.h.

◆ info_

std::string esphome::api::APINoiseFrameHelper::info_
protected

Definition at line 109 of file api_frame_helper.h.

◆ nid_

NoiseProtocolId esphome::api::APINoiseFrameHelper::nid_
protected

Definition at line 122 of file api_frame_helper.h.

◆ prologue_

std::vector<uint8_t> esphome::api::APINoiseFrameHelper::prologue_
protected

Definition at line 116 of file api_frame_helper.h.

◆ recv_cipher_

NoiseCipherState* esphome::api::APINoiseFrameHelper::recv_cipher_ {nullptr}
protected

Definition at line 121 of file api_frame_helper.h.

◆ rx_buf_

std::vector<uint8_t> esphome::api::APINoiseFrameHelper::rx_buf_
protected

Definition at line 112 of file api_frame_helper.h.

◆ rx_buf_len_

size_t esphome::api::APINoiseFrameHelper::rx_buf_len_ = 0
protected

Definition at line 113 of file api_frame_helper.h.

◆ rx_header_buf_

uint8_t esphome::api::APINoiseFrameHelper::rx_header_buf_[3]
protected

Definition at line 110 of file api_frame_helper.h.

◆ rx_header_buf_len_

size_t esphome::api::APINoiseFrameHelper::rx_header_buf_len_ = 0
protected

Definition at line 111 of file api_frame_helper.h.

◆ send_cipher_

NoiseCipherState* esphome::api::APINoiseFrameHelper::send_cipher_ {nullptr}
protected

Definition at line 120 of file api_frame_helper.h.

◆ socket_

std::unique_ptr<socket::Socket> esphome::api::APINoiseFrameHelper::socket_
protected

Definition at line 107 of file api_frame_helper.h.

◆ state_

enum esphome::api::APINoiseFrameHelper::State esphome::api::APINoiseFrameHelper::state_ = State::INITIALIZE
protected

◆ tx_buf_

std::vector<uint8_t> esphome::api::APINoiseFrameHelper::tx_buf_
protected

Definition at line 115 of file api_frame_helper.h.


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