11#define ESPHOME_FRAME_READER_COLD __attribute__((noinline))
13#define ESPHOME_FRAME_READER_COLD __attribute__((noinline, noclone))
18namespace frame_reader_detail {
20template<uint8_t... HEADER>
constexpr uint8_t
header_byte(
size_t i) {
23 ((r = (j++ == i) ? HEADER : r), ...);
31 using pos_t = std::conditional_t<(N <= 0xFF), uint8_t, uint16_t>;
32 static constexpr size_t HEADER_LEN =
sizeof...(HEADER);
33 static_assert(HEADER_LEN >= 1 && HEADER_LEN <= 8,
"FrameReader takes 1 to 8 header bytes");
34 static_assert(HEADER_LEN < N && N <= 0xFFFF,
"FrameReader size out of range");
38 const uint8_t *
feed(uint8_t c) {
47 this->
buf_[pos++] = c;
69 const uint8_t *ESPHOME_FRAME_READER_COLD
replay_(
size_t last) {
71 for (
size_t i = 1; i <= last; i++) {
94 static_assert(F.payload_at == 0 && F.trailer == 0,
"A whole-frame length has no payload_at or trailer");
95 static constexpr size_t KNOWN_AT = F.offset + 1;
96 static constexpr size_t get(
const uint8_t *buf) {
return buf[F.offset]; }
99 static_assert(F.payload_at == 0 && F.trailer == 0,
"A whole-frame length has no payload_at or trailer");
101 static constexpr size_t get(
const uint8_t *buf) {
return (
size_t(buf[F.offset]) << 8) | buf[F.offset + 1]; }
104 static_assert(F.payload_at == 0 && F.trailer == 0,
"A whole-frame length has no payload_at or trailer");
106 static constexpr size_t get(
const uint8_t *buf) {
return buf[F.offset] | (size_t(buf[F.offset + 1]) << 8); }
108namespace frame_reader_detail {
111 static_assert(F.payload_at == 0 || F.payload_at >= FIELD::KNOWN_AT,
"payload_at must follow the length field");
112 static constexpr size_t KNOWN_AT = FIELD::KNOWN_AT;
116 static constexpr size_t get(
const uint8_t *buf) {
return PAYLOAD_AT + FIELD::get(buf) + F.trailer; }
121template<LengthField F>
123template<LengthField F>
125template<LengthField F>
129 static_assert(LEN >= 2,
"A fixed length frame has at least 2 bytes");
131 static constexpr size_t get(
const uint8_t * ) {
return LEN; }
136 static constexpr size_t MIN = 0;
139 static constexpr bool check(
const uint8_t * ) {
return true; }
140 static constexpr bool fits(
size_t ) {
return true; }
144 static constexpr size_t MIN = LEN;
149 static bool check(
const uint8_t *buf) {
return CHECK(buf); }
164 static constexpr size_t LEN =
sizeof...(BYTES);
165 static_assert(
LEN >= 1 &&
LEN <= 8,
"A FrameHeader has 1 to 8 bytes");
169namespace frame_reader_detail {
172 static constexpr size_t KNOWN_AT = std::max<size_t>({LENGTH::KNOWN_AT, OPTIONS::CHECK_AT...});
173 static size_t get(
const uint8_t *buf) {
174 if (!(OPTIONS::check(buf) && ...))
176 const size_t len = LENGTH::get(buf);
177 return (OPTIONS::fits(
len) && ...) ?
len : 0;
181template<
class LENGTH>
constexpr size_t RULE_MIN = 0;
190template<
class HEADER,
class LENGTH,
class... OPTIONS>
struct FrameFormat {
191 static_assert((std::is_base_of_v<FrameOption, OPTIONS> && ...),
192 "FrameFormat options are FrameMinLength, CheckFirstBytes and WarnIfLongerThan");
193 static_assert(((OPTIONS::CHECK_AT == 0 || OPTIONS::CHECK_AT >= LENGTH::KNOWN_AT) && ...),
194 "CheckFirstBytes must cover the length field");
196 static_assert((0 + ... + (OPTIONS::CHECK_AT != 0)) <= 1,
"A FrameFormat takes at most one CheckFirstBytes");
198 static_assert((0 + ... + (OPTIONS::WARN_MAX != 0)) <= 1,
"A FrameFormat takes at most one WarnIfLongerThan");
201 static constexpr uint8_t
FIRST = HEADER::byte(0);
203 static constexpr size_t WARN_MAX = std::max<size_t>({0, OPTIONS::WARN_MAX...});
208 static_assert(LENGTH::KNOWN_AT == 1 || LENGTH::KNOWN_AT >
HEADER_LEN,
"The length field must follow the header");
209 static constexpr uint8_t
header_byte(
size_t i) {
return HEADER::byte(i); }
222 using pos_t = std::conditional_t<(N <= 0xFF), uint8_t, uint16_t>;
223 static_assert(
sizeof...(FORMATS) >= 1,
"LengthFrameReader takes at least one FrameFormat");
225 static_assert(N >= 2 && N <= 1024,
"LengthFrameReader size out of range");
226 static constexpr bool distinct_first() {
227 const uint8_t first[] = {FORMATS::FIRST...};
228 for (
size_t i = 0; i <
sizeof...(FORMATS); i++) {
229 for (
size_t j = i + 1; j <
sizeof...(FORMATS); j++) {
230 if (first[i] == first[j])
236 static_assert(distinct_first(),
"Each FrameFormat needs a different first header byte");
237 static_assert(((FORMATS::SHORTEST <= N) && ...),
"N must hold the shortest frame of every FrameFormat");
238 static_assert(((FORMATS::FIXED_LENGTH == 0 ||
239 (FORMATS::FIXED_LENGTH >= FORMATS::SHORTEST && FORMATS::FIXED_LENGTH <= N)) &&
241 "A fixed length must hold the header and fit in N");
242 static_assert(((FORMATS::WARN_MAX <= N) && ...),
243 "A WarnIfLongerThan MAX above N lets longer frames be skipped without a warning");
254 this->
pos_ = pos + 1;
291 bool handled =
false;
306 std::memcpy(saved, this->
buf_, pos);
307 while (this->
pos_ != 0) {
312 std::memcpy(this->
buf_, saved,
pos);
324 const bool header = ((c == FORMATS::FIRST) || ...);
331 if constexpr (
sizeof...(FORMATS) == 1) {
334 const uint8_t first = this->
buf_[0];
342 if (
pos < F::HEADER_LEN && c != F::header_byte(
pos)) {
345 return this->
replay_(1, pos + 1);
347 this->
buf_[pos++] = c;
348 if (
pos == F::MIN_LEN) {
349 const size_t len = F::Length::get(this->
buf_);
354 if (pos == this->
end_) {
366 size_t ESPHOME_FRAME_READER_COLD
replay_(
size_t from,
size_t to) {
369 for (
size_t i = from; i < to; i++) {
375 const size_t rest = to - i - 1;
376 std::memmove(this->
buf_ + held, this->
buf_ + i + 1, rest);
395#undef ESPHOME_FRAME_READER_COLD
Collects fixed length binary frames of N bytes that start with the header bytes HEADER....
size_t length() const
Number of bytes of the partial frame; 0 after a frame is returned and while looking for a header.
const uint8_t * feed(uint8_t c)
Feed one byte; returns the completed frame, header included, valid until the next call,...
void resync()
For a frame the caller rejected (bad checksum): drop its first byte and realign on a header in the re...
const uint8_t *ESPHOME_FRAME_READER_COLD replay_(size_t last)
void reset()
Forget the partial frame and look for a header from the next byte.
Collects binary frames that give their own length, of up to N bytes, in one or more FrameFormats; the...
void reset()
Forget the partial frame, for a request/response protocol starting a new exchange.
void drop_kept()
Forget a partial frame an idle flush kept, before a request starts a new exchange; safe inside a fram...
void __attribute__((always_inline)) feed(uint8_t c
Feed one byte; calls handle(frame, length) for each frame it completes, header included.
size_t feed_as_(pos_t pos, uint8_t c)
bool ESPHOME_FRAME_READER_COLD drain_(size_t len, F &handle)
void ESPHOME_FRAME_READER_COLD flush_(F &handle)
size_t length() const
Number of bytes of the partial frame; 0 after a frame is returned and while looking for a header.
size_t ESPHOME_FRAME_READER_COLD replay_(size_t from, size_t to)
void flush(F &&handle)
For a partial frame idle too long: hands over the frames a corrupt length holds back inside it,...
size_t ESPHOME_FRAME_READER_COLD feed_edge_(uint8_t c)
bool receiving() const
Whether a frame is coming in: a partial frame that an idle flush has not searched and kept.
constexpr uint8_t header_byte(size_t i)
constexpr size_t RULE_MIN
constexpr size_t FIXED_LENGTH
FrameFormat option: CHECK(frame) runs once the first BYTES bytes are in; a frame it fails is not a fr...
static bool check(const uint8_t *buf)
static constexpr size_t CHECK_AT
A frame type that always has LEN bytes.
static constexpr size_t get(const uint8_t *)
static constexpr size_t KNOWN_AT
static constexpr size_t KNOWN_AT
static constexpr size_t get(const uint8_t *buf)
static constexpr size_t KNOWN_AT
static constexpr size_t get(const uint8_t *buf)
Whole-frame length: the field counts every byte of the frame, header included.
static constexpr size_t KNOWN_AT
static constexpr size_t get(const uint8_t *buf)
FrameFormat option: the shortest frame, when that is more than its header and length field.
static constexpr size_t MIN
Base of the FrameFormat options; each option overrides only its own members.
static constexpr bool check(const uint8_t *)
static constexpr size_t WARN_MAX
static constexpr size_t MIN
static constexpr bool fits(size_t)
static constexpr size_t CHECK_AT
offset: where the length field starts, counted from the first header byte.
FrameFormat option: a frame longer than MAX calls WARN(length) and is skipped; a replay can warn agai...
static constexpr size_t WARN_MAX
static bool fits(size_t len)
static size_t get(const uint8_t *buf)
static constexpr size_t KNOWN_AT
static constexpr size_t get(const uint8_t *buf)
static constexpr size_t KNOWN_AT
static constexpr size_t MIN
static constexpr size_t PAYLOAD_AT