ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
esphome::LengthFrameReader< N, FORMATS > Class Template Reference

Collects binary frames that give their own length, of up to N bytes, in one or more FrameFormats; the first byte picks the format, so each format needs a different first header byte. More...

#include <frame_reader.h>

Public Member Functions

template<class F >
void __attribute__ ((always_inline)) feed(uint8_t c
 Feed one byte; calls handle(frame, length) for each frame it completes, header included.
 
 if (pos+1u< this->end_)
 
else if (const size_t len=this->feed_edge_(c))
 
template<class F >
void flush (F &&handle)
 For a partial frame idle too long: hands over the frames a corrupt length holds back inside it, else keeps it for a slow UART.
 
size_t length () const
 Number of bytes of the partial frame; 0 after a frame is returned and while looking for a header.
 
bool receiving () const
 Whether a frame is coming in: a partial frame that an idle flush has not searched and kept.
 
void drop_kept ()
 Forget a partial frame an idle flush kept, before a request starts a new exchange; safe inside a frame handler.
 
void reset ()
 Forget the partial frame, for a request/response protocol starting a new exchange.
 

Data Fields

void F && handle
 

Protected Member Functions

template<class F >
bool ESPHOME_FRAME_READER_COLD drain_ (size_t len, F &handle)
 
template<class F >
void ESPHOME_FRAME_READER_COLD flush_ (F &handle)
 
size_t ESPHOME_FRAME_READER_COLD feed_edge_ (uint8_t c)
 
template<class F >
size_t feed_as_ (pos_t pos, uint8_t c)
 
size_t ESPHOME_FRAME_READER_COLD replay_ (size_t from, size_t to)
 

Protected Attributes

pos_t pos_ {0}
 
pos_t end_ {0}
 
pos_t held_end_ {0}
 
uint8_t buf_ [N]
 

Detailed Description

template<size_t N, class... FORMATS>
class esphome::LengthFrameReader< N, FORMATS >

Collects binary frames that give their own length, of up to N bytes, in one or more FrameFormats; the first byte picks the format, so each format needs a different first header byte.

Bytes before a header are skipped. A length that ends the frame before its length field, or is longer than N, is not a frame, and the reader looks for a header after its first byte. Checksums and footers are left to the caller, which passes a handler that returns false for a frame it rejects:

this->reader_.feed(c, [this](const uint8_t *frame, size_t len) { return this->handle_frame_(frame, len); });

For frames of one fixed size, FrameReader is smaller.

Definition at line 221 of file frame_reader.h.

Member Function Documentation

◆ __attribute__()

template<size_t N, class... FORMATS>
template<class F >
void esphome::LengthFrameReader< N, FORMATS >::__attribute__ ( (always_inline) )

Feed one byte; calls handle(frame, length) for each frame it completes, header included.

A rejected frame (handle returns false) is searched for frames inside it and after it, which go to handle the same way. handle must not feed this reader again: the frame and the bytes held behind it live in its buffer.

◆ drain_()

template<size_t N, class... FORMATS>
template<class F >
bool ESPHOME_FRAME_READER_COLD esphome::LengthFrameReader< N, FORMATS >::drain_ ( size_t len,
F & handle )
inlineprotected

Definition at line 290 of file frame_reader.h.

◆ drop_kept()

template<size_t N, class... FORMATS>
void esphome::LengthFrameReader< N, FORMATS >::drop_kept ( )
inline

Forget a partial frame an idle flush kept, before a request starts a new exchange; safe inside a frame handler.

Definition at line 275 of file frame_reader.h.

◆ feed_as_()

template<size_t N, class... FORMATS>
template<class F >
size_t esphome::LengthFrameReader< N, FORMATS >::feed_as_ ( pos_t pos,
uint8_t c )
inlineprotected

Definition at line 341 of file frame_reader.h.

◆ feed_edge_()

template<size_t N, class... FORMATS>
size_t ESPHOME_FRAME_READER_COLD esphome::LengthFrameReader< N, FORMATS >::feed_edge_ ( uint8_t c)
inlineprotected

Definition at line 319 of file frame_reader.h.

◆ flush()

template<size_t N, class... FORMATS>
template<class F >
void esphome::LengthFrameReader< N, FORMATS >::flush ( F && handle)
inline

For a partial frame idle too long: hands over the frames a corrupt length holds back inside it, else keeps it for a slow UART.

UARTDevice::read_frames() calls this.

Definition at line 262 of file frame_reader.h.

◆ flush_()

template<size_t N, class... FORMATS>
template<class F >
void ESPHOME_FRAME_READER_COLD esphome::LengthFrameReader< N, FORMATS >::flush_ ( F & handle)
inlineprotected

Definition at line 302 of file frame_reader.h.

◆ if() [1/2]

template<size_t N, class... FORMATS>
else esphome::LengthFrameReader< N, FORMATS >::if ( const size_t len = this->feed_edge_(c))
inline

Definition at line 255 of file frame_reader.h.

◆ if() [2/2]

template<size_t N, class... FORMATS>
esphome::LengthFrameReader< N, FORMATS >::if ( pos+1u< this-> end_)
inline

Definition at line 252 of file frame_reader.h.

◆ length()

template<size_t N, class... FORMATS>
size_t esphome::LengthFrameReader< N, FORMATS >::length ( ) const
inline

Number of bytes of the partial frame; 0 after a frame is returned and while looking for a header.

Definition at line 269 of file frame_reader.h.

◆ receiving()

template<size_t N, class... FORMATS>
bool esphome::LengthFrameReader< N, FORMATS >::receiving ( ) const
inline

Whether a frame is coming in: a partial frame that an idle flush has not searched and kept.

Definition at line 272 of file frame_reader.h.

◆ replay_()

template<size_t N, class... FORMATS>
size_t ESPHOME_FRAME_READER_COLD esphome::LengthFrameReader< N, FORMATS >::replay_ ( size_t from,
size_t to )
inlineprotected

Definition at line 366 of file frame_reader.h.

◆ reset()

template<size_t N, class... FORMATS>
void esphome::LengthFrameReader< N, FORMATS >::reset ( )
inline

Forget the partial frame, for a request/response protocol starting a new exchange.

Definition at line 281 of file frame_reader.h.

Field Documentation

◆ buf_

template<size_t N, class... FORMATS>
uint8_t esphome::LengthFrameReader< N, FORMATS >::buf_[N]
protected

Definition at line 390 of file frame_reader.h.

◆ end_

template<size_t N, class... FORMATS>
pos_t esphome::LengthFrameReader< N, FORMATS >::end_ {0}
protected

Definition at line 386 of file frame_reader.h.

◆ handle

template<size_t N, class... FORMATS>
void F&& esphome::LengthFrameReader< N, FORMATS >::handle
Initial value:
{
const pos_t pos = this->pos_
size_t size_t pos
Definition helpers.h:1530

Definition at line 249 of file frame_reader.h.

◆ held_end_

template<size_t N, class... FORMATS>
pos_t esphome::LengthFrameReader< N, FORMATS >::held_end_ {0}
protected

Definition at line 389 of file frame_reader.h.

◆ pos_

template<size_t N, class... FORMATS>
pos_t esphome::LengthFrameReader< N, FORMATS >::pos_ {0}
protected

Definition at line 384 of file frame_reader.h.


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