mirror of
https://github.com/esphome/esphome.git
synced 2026-11-19 19:45:58 +00:00
Compare commits
42
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a8c95ace2d | ||
|
|
ec04273cb9 | ||
|
|
8b744ca59c | ||
|
|
590ed71434 | ||
|
|
19d2f90a2a | ||
|
|
bee80eef56 | ||
|
|
04bb736511 | ||
|
|
94e670de12 | ||
|
|
03990bf424 | ||
|
|
d3dd1b27da | ||
|
|
05436f0b15 | ||
|
|
c90cf2c987 | ||
|
|
87be3320a3 | ||
|
|
39fa5b1dd6 | ||
|
|
d983659f32 | ||
|
|
837f2706b4 | ||
|
|
d01835e824 | ||
|
|
0b0bb23517 | ||
|
|
4f8ca50ffe | ||
|
|
e009b526aa | ||
|
|
29f751a8aa | ||
|
|
c1b0e3b7b6 | ||
|
|
5a46ae13d8 | ||
|
|
b338e0a2e3 | ||
|
|
fd330d8e23 | ||
|
|
3d067b9484 | ||
|
|
4d28203063 | ||
|
|
f72ba983e9 | ||
|
|
dcda93d7ca | ||
|
|
0436eda0f8 | ||
|
|
bf79439362 | ||
|
|
a460fb50b4 | ||
|
|
06ac99fe7b | ||
|
|
61b99d91bf | ||
|
|
69a7f7a408 | ||
|
|
9fb6cb4867 | ||
|
|
e0a5fad2de | ||
|
|
d76c655553 | ||
|
|
9bdd466e51 | ||
|
|
03880d5ea6 | ||
|
|
4a04c61ab5 | ||
|
|
2cd9ddf246 |
No files matched your search
@@ -120,7 +120,7 @@ helper; `AGENTS.md` section 4 and the `component-codegen` skill have the detail.
|
||||
| hand-written `set_x_sensor()` plus `x_sensor_` member | `SUB_SENSOR(x)`, `SUB_BINARY_SENSOR`, `SUB_TEXT_SENSOR`, `SUB_SWITCH`, ... |
|
||||
| `if CONF_X in config: s = await sensor.new_sensor(config[CONF_X])` plus setter | `sensors = sensor.sub_sensors(config)` then `await sensors(CONF_X, hub.set_x_sensor)` |
|
||||
| hand-written `{"a": MyEnum.MY_ENUM_A, ...}` dict | `MyEnum.options("a", "b", prefix="MY_ENUM_")` |
|
||||
| loops on `available()` / `read_byte()` | UART `read_available(on_byte)`, `discard_available()`, `LineReader<N>`, `FrameReader<N, HEADER...>` |
|
||||
| loops on `available()` / `read_byte()` | UART `read_available(on_byte)`, `discard_available()`, `LineReader<N>`, `FrameReader<N, HEADER...>`, `LengthFrameReader<N, FrameFormat<...>...>` |
|
||||
| hand-rolled CRC, checksum or bit loop | `crc8`, `crc16`, `xor_bytes`, `sum16_bytes`, `encode_uint16`, ... (`esphome/core/helpers.h`) |
|
||||
| integer split into buffer bytes by hand (`buf[0] = v >> 8; buf[1] = v;`) | `put_be16`/`24`/`32`, `put_le16`, `put_le32` (`esphome/core/helpers.h`) |
|
||||
| bare `mark_failed()`, or `ESP_LOGE` then `mark_failed()` | `this->mark_failed(LOG_STR("reason"))` or `LOG_STR(ESP_LOG_MSG_COMM_FAIL)` |
|
||||
|
||||
@@ -246,7 +246,9 @@ file does, and it is the authority when they disagree. The most useful starting
|
||||
`time::decode_bcd_calendar<LAYOUT>` and `encode_bcd_calendar<LAYOUT>` with `EPSON_BCD_CALENDAR`,
|
||||
`DS_BCD_CALENDAR` or `PCF_BCD_CALENDAR`.
|
||||
* **Bus helpers:** UART drains with `read_available(on_byte)` and `discard_available()`, text lines with
|
||||
`LineReader<N>`, fixed-length binary frames with `FrameReader<N, HEADER...>`; I2C has `update_register`,
|
||||
`LineReader<N>`, fixed-length binary frames with `FrameReader<N, HEADER...>`, frames that give their own length
|
||||
with `LengthFrameReader<N, FrameFormat<FrameHeader<...>, PayloadLengthU16LE<LengthField{...}>>...>` fed by
|
||||
`read_frames(reader, handler, now, last_rx)`, which frees frames a bad length holds; I2C has `update_register`,
|
||||
`update_register16`, `read_byte_reg16` and `write_byte_reg16` for 16-bit register addresses,
|
||||
`write_registers` (PROGMEM `I2CRegValue` table), `check_chip_id` and `read_bytes_16_le`.
|
||||
`if (!this->status_comm_result(ok)) return;` sets or clears the warning; an earlier step of a multi-step
|
||||
|
||||
@@ -90,6 +90,19 @@ class UARTDevice {
|
||||
count);
|
||||
}
|
||||
|
||||
/// Feed the waiting bytes to a LengthFrameReader; on a pass with none, gap_ms after last_rx, flush a stalled frame.
|
||||
/// Returns false when a UART read failed.
|
||||
template<size_t N = 64, typename R, typename F>
|
||||
bool read_frames(R &reader, F &&handle, uint32_t now, uint32_t &last_rx, uint32_t gap_ms = 250) {
|
||||
if (const size_t count = this->available()) {
|
||||
last_rx = now;
|
||||
return this->read_available<N>([&reader, &handle](uint8_t c) { reader.feed(c, handle); }, count);
|
||||
}
|
||||
if (reader.length() != 0 && now - last_rx >= gap_ms)
|
||||
reader.flush(handle);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Read and drop every byte waiting in the receive buffer.
|
||||
void discard_available();
|
||||
|
||||
|
||||
+320
-13
@@ -1,11 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <type_traits>
|
||||
|
||||
// Out of line, one copy: the frame readers' rare paths call each other, and GCC would add specialised clones
|
||||
#if defined(__clang__)
|
||||
#define ESPHOME_FRAME_READER_COLD __attribute__((noinline))
|
||||
#else
|
||||
#define ESPHOME_FRAME_READER_COLD __attribute__((noinline, noclone))
|
||||
#endif
|
||||
|
||||
namespace esphome {
|
||||
|
||||
namespace frame_reader_detail {
|
||||
// A fold, not an array, so no header table lands in RAM on ESP8266
|
||||
template<uint8_t... HEADER> constexpr uint8_t header_byte(size_t i) {
|
||||
size_t j = 0;
|
||||
uint8_t r = 0;
|
||||
((r = (j++ == i) ? HEADER : r), ...);
|
||||
return r;
|
||||
}
|
||||
} // namespace frame_reader_detail
|
||||
|
||||
/// Collects fixed length binary frames of N bytes that start with the header bytes HEADER...; the binary twin of
|
||||
/// LineReader. Bytes before a header are skipped; checksums are left to the caller.
|
||||
template<size_t N, uint8_t... HEADER> class FrameReader {
|
||||
@@ -18,11 +37,12 @@ template<size_t N, uint8_t... HEADER> class FrameReader {
|
||||
/// Feed one byte; returns the completed frame, header included, valid until the next call, or nullptr.
|
||||
const uint8_t *feed(uint8_t c) {
|
||||
pos_t pos = this->pos_;
|
||||
if (pos < HEADER_LEN && c != header_byte(pos)) {
|
||||
if (pos < HEADER_LEN && c != frame_reader_detail::header_byte<HEADER...>(pos)) {
|
||||
// A later byte of the broken header, or c, may start the real one
|
||||
if (pos == 0)
|
||||
return nullptr;
|
||||
this->buf_[pos] = c;
|
||||
this->replay_(pos);
|
||||
return nullptr;
|
||||
return this->replay_(pos);
|
||||
}
|
||||
this->buf_[pos++] = c;
|
||||
if (pos < N) {
|
||||
@@ -44,24 +64,311 @@ template<size_t N, uint8_t... HEADER> class FrameReader {
|
||||
void resync() { this->replay_(N - 1); }
|
||||
|
||||
protected:
|
||||
// A fold, not an array, so no header table lands in RAM on ESP8266
|
||||
static constexpr uint8_t header_byte(size_t i) {
|
||||
size_t j = 0;
|
||||
uint8_t r = 0;
|
||||
((r = (j++ == i) ? HEADER : r), ...);
|
||||
return r;
|
||||
}
|
||||
|
||||
// Start over and feed buf_[1..last] again; never completes a frame as last < N
|
||||
void replay_(size_t last) {
|
||||
// Start over and feed buf_[1..last] again; never completes a frame as last < N, so it returns nullptr, which lets
|
||||
// feed() end in a tail call. Out of line, as feed() and replay_() call each other.
|
||||
const uint8_t *ESPHOME_FRAME_READER_COLD replay_(size_t last) {
|
||||
this->pos_ = 0;
|
||||
for (size_t i = 1; i <= last; i++) {
|
||||
this->feed(this->buf_[i]);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
uint8_t buf_[N];
|
||||
pos_t pos_{0};
|
||||
};
|
||||
|
||||
// Length rules: get() gives the whole frame length once KNOWN_AT bytes are in, or 0 to reject the header.
|
||||
|
||||
/// offset: where the length field starts, counted from the first header byte; trailer: the bytes after the payload
|
||||
/// (checksum, footer), for a payload length only.
|
||||
struct LengthField {
|
||||
size_t offset;
|
||||
size_t trailer = 0;
|
||||
};
|
||||
|
||||
/// Whole-frame length: the field counts every byte of the frame, header included.
|
||||
template<LengthField F> struct FrameLengthU8 {
|
||||
static_assert(F.trailer == 0, "A whole-frame length has no trailer");
|
||||
static constexpr size_t KNOWN_AT = F.offset + 1;
|
||||
static constexpr size_t get(const uint8_t *buf) { return buf[F.offset]; }
|
||||
};
|
||||
template<LengthField F> struct FrameLengthU16BE {
|
||||
static_assert(F.trailer == 0, "A whole-frame length has no trailer");
|
||||
static constexpr size_t KNOWN_AT = F.offset + 2;
|
||||
static constexpr size_t get(const uint8_t *buf) { return (size_t(buf[F.offset]) << 8) | buf[F.offset + 1]; }
|
||||
};
|
||||
template<LengthField F> struct FrameLengthU16LE {
|
||||
static_assert(F.trailer == 0, "A whole-frame length has no trailer");
|
||||
static constexpr size_t KNOWN_AT = F.offset + 2;
|
||||
static constexpr size_t get(const uint8_t *buf) { return buf[F.offset] | (size_t(buf[F.offset + 1]) << 8); }
|
||||
};
|
||||
/// Payload length: the field counts the payload right after it.
|
||||
template<LengthField F> struct PayloadLengthU8 {
|
||||
static constexpr size_t KNOWN_AT = F.offset + 1;
|
||||
static constexpr size_t get(const uint8_t *buf) { return KNOWN_AT + buf[F.offset] + F.trailer; }
|
||||
};
|
||||
template<LengthField F> struct PayloadLengthU16BE {
|
||||
static constexpr size_t KNOWN_AT = F.offset + 2;
|
||||
static constexpr size_t get(const uint8_t *buf) {
|
||||
return KNOWN_AT + FrameLengthU16BE<LengthField{.offset = F.offset}>::get(buf) + F.trailer;
|
||||
}
|
||||
};
|
||||
template<LengthField F> struct PayloadLengthU16LE {
|
||||
static constexpr size_t KNOWN_AT = F.offset + 2;
|
||||
static constexpr size_t get(const uint8_t *buf) {
|
||||
return KNOWN_AT + FrameLengthU16LE<LengthField{.offset = F.offset}>::get(buf) + F.trailer;
|
||||
}
|
||||
};
|
||||
/// A frame type that always has LEN bytes.
|
||||
template<size_t LEN> struct FrameFixedLength {
|
||||
static_assert(LEN >= 2, "A fixed length frame has at least 2 bytes");
|
||||
static constexpr size_t KNOWN_AT = 1;
|
||||
static constexpr size_t get(const uint8_t * /*buf*/) { return LEN; }
|
||||
};
|
||||
|
||||
/// Wraps a length rule: CHECK(buf) runs once AT bytes are in, and a header it fails gives length 0 (rejected).
|
||||
template<class LENGTH, size_t AT, bool (*CHECK)(const uint8_t *)> struct CheckedLength {
|
||||
static_assert(AT >= LENGTH::KNOWN_AT, "AT must cover the length field");
|
||||
static constexpr size_t KNOWN_AT = AT;
|
||||
static size_t get(const uint8_t *buf) { return CHECK(buf) ? LENGTH::get(buf) : 0; }
|
||||
};
|
||||
|
||||
/// Wraps a length rule: a frame longer than MAX calls WARN(length) and is rejected; a replay can warn again.
|
||||
template<class LENGTH, size_t MAX, void (*WARN)(size_t)> struct WarnIfLonger {
|
||||
static constexpr size_t KNOWN_AT = LENGTH::KNOWN_AT;
|
||||
static size_t get(const uint8_t *buf) {
|
||||
const size_t len = LENGTH::get(buf);
|
||||
if (len <= MAX)
|
||||
return len;
|
||||
WARN(len);
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
/// The shortest frame of a FrameFormat, when that is more than its header and length field.
|
||||
template<size_t LEN> struct FrameMinLength {
|
||||
static constexpr size_t VALUE = LEN;
|
||||
};
|
||||
|
||||
namespace frame_reader_detail {
|
||||
// The length of a FrameFixedLength rule, 0 for every other rule
|
||||
template<class LENGTH> constexpr size_t FIXED_LENGTH = 0;
|
||||
template<size_t LEN> constexpr size_t FIXED_LENGTH<FrameFixedLength<LEN>> = LEN;
|
||||
template<class LENGTH, size_t AT, bool (*CHECK)(const uint8_t *)>
|
||||
constexpr size_t FIXED_LENGTH<CheckedLength<LENGTH, AT, CHECK>> = FIXED_LENGTH<LENGTH>;
|
||||
template<class LENGTH, size_t MAX, void (*WARN)(size_t)>
|
||||
constexpr size_t FIXED_LENGTH<WarnIfLonger<LENGTH, MAX, WARN>> = FIXED_LENGTH<LENGTH>;
|
||||
// The MAX of a WarnIfLonger rule, 0 for every other rule
|
||||
template<class LENGTH> constexpr size_t WARN_MAX = 0;
|
||||
template<class LENGTH, size_t MAX, void (*WARN)(size_t)>
|
||||
constexpr size_t WARN_MAX<WarnIfLonger<LENGTH, MAX, WARN>> = MAX;
|
||||
template<class LENGTH, size_t AT, bool (*CHECK)(const uint8_t *)>
|
||||
constexpr size_t WARN_MAX<CheckedLength<LENGTH, AT, CHECK>> = WARN_MAX<LENGTH>;
|
||||
} // namespace frame_reader_detail
|
||||
|
||||
/// The bytes every frame of a FrameFormat starts with.
|
||||
template<uint8_t... BYTES> struct FrameHeader {
|
||||
static constexpr size_t LEN = sizeof...(BYTES);
|
||||
static_assert(LEN >= 1 && LEN <= 8, "A FrameHeader has 1 to 8 bytes");
|
||||
static constexpr uint8_t byte(size_t i) { return frame_reader_detail::header_byte<BYTES...>(i); }
|
||||
};
|
||||
|
||||
/// One frame type for LengthFrameReader: a FrameHeader, a length rule from above and an optional FrameMinLength.
|
||||
template<class HEADER, class LENGTH, class MIN = FrameMinLength<0>> struct FrameFormat {
|
||||
using Length = LENGTH;
|
||||
static constexpr size_t HEADER_LEN = HEADER::LEN;
|
||||
static constexpr uint8_t FIRST = HEADER::byte(0);
|
||||
// Bytes collected when the length is checked
|
||||
static constexpr size_t MIN_LEN = std::max<size_t>({LENGTH::KNOWN_AT, HEADER_LEN, 2});
|
||||
// A shorter length is not a frame
|
||||
static constexpr size_t SHORTEST = std::max(MIN_LEN, MIN::VALUE);
|
||||
static_assert(LENGTH::KNOWN_AT == 1 || LENGTH::KNOWN_AT > HEADER_LEN, "The length field must follow the header");
|
||||
static constexpr uint8_t header_byte(size_t i) { return HEADER::byte(i); }
|
||||
};
|
||||
|
||||
/// 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.
|
||||
template<size_t N, class... FORMATS> class LengthFrameReader {
|
||||
using pos_t = std::conditional_t<(N <= 0xFF), uint8_t, uint16_t>;
|
||||
static_assert(sizeof...(FORMATS) >= 1, "LengthFrameReader takes at least one FrameFormat");
|
||||
// flush_() copies a partial frame on the stack
|
||||
static_assert(N >= 2 && N <= 1024, "LengthFrameReader size out of range");
|
||||
static constexpr bool distinct_first() {
|
||||
const uint8_t first[] = {FORMATS::FIRST...};
|
||||
for (size_t i = 0; i < sizeof...(FORMATS); i++) {
|
||||
for (size_t j = i + 1; j < sizeof...(FORMATS); j++) {
|
||||
if (first[i] == first[j])
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
static_assert(distinct_first(), "Each FrameFormat needs a different first header byte");
|
||||
static_assert(((FORMATS::SHORTEST <= N) && ...), "N must hold the shortest frame of every FrameFormat");
|
||||
static_assert(((frame_reader_detail::FIXED_LENGTH<typename FORMATS::Length> == 0 ||
|
||||
(frame_reader_detail::FIXED_LENGTH<typename FORMATS::Length> >= FORMATS::SHORTEST &&
|
||||
frame_reader_detail::FIXED_LENGTH<typename FORMATS::Length> <= N)) &&
|
||||
...),
|
||||
"A fixed length must hold the header and fit in N");
|
||||
static_assert(((frame_reader_detail::WARN_MAX<typename FORMATS::Length> <= N) && ...),
|
||||
"A WarnIfLonger MAX above N lets longer frames be skipped without a warning");
|
||||
|
||||
public:
|
||||
/// 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.
|
||||
template<class F> void __attribute__((always_inline)) feed(uint8_t c, F &&handle) {
|
||||
// Most bytes are in the middle of a frame whose length is known, and not its last byte
|
||||
const pos_t pos = this->pos_;
|
||||
if (pos + 1u < this->end_) {
|
||||
this->buf_[pos] = c;
|
||||
this->pos_ = pos + 1;
|
||||
} else if (const size_t len = this->feed_edge_(c)) {
|
||||
this->drain_(len, 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. UARTDevice::read_frames() calls this.
|
||||
template<class F> void flush(F &&handle) {
|
||||
// held_end_ marks a partial frame already searched; new bytes move pos_ past it
|
||||
if (this->pos_ != 0 && this->held_end_ != this->pos_)
|
||||
this->flush_(handle);
|
||||
}
|
||||
|
||||
/// Number of bytes of the partial frame; 0 after a frame is returned and while looking for a header.
|
||||
size_t length() const { return this->pos_; }
|
||||
|
||||
/// Whether a frame is coming in: a partial frame that an idle flush has not searched and kept.
|
||||
bool receiving() const { return this->pos_ != 0 && this->held_end_ != this->pos_; }
|
||||
|
||||
/// Forget a partial frame an idle flush kept, before a request starts a new exchange; safe inside a frame handler.
|
||||
void drop_kept() {
|
||||
if (this->pos_ != 0 && this->held_end_ == this->pos_)
|
||||
this->reset();
|
||||
}
|
||||
|
||||
/// Forget the partial frame, for a request/response protocol starting a new exchange.
|
||||
void reset() {
|
||||
this->pos_ = 0;
|
||||
this->end_ = 0;
|
||||
this->held_end_ = 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
// Hands each frame (len bytes at buf_) to handle, then goes on after a handled frame or from the second byte of a
|
||||
// rejected one
|
||||
template<class F> bool ESPHOME_FRAME_READER_COLD drain_(size_t len, F &handle) {
|
||||
bool handled = false;
|
||||
while (len != 0) {
|
||||
const bool ok = handle(this->buf_, len);
|
||||
handled |= ok;
|
||||
len = this->replay_(ok ? len : 1, this->held_end_);
|
||||
}
|
||||
this->held_end_ = 0;
|
||||
return handled;
|
||||
}
|
||||
|
||||
// Searches the partial frame for frames; when handle takes none, puts the partial frame back and marks it searched
|
||||
template<class F> void ESPHOME_FRAME_READER_COLD flush_(F &handle) {
|
||||
const pos_t pos = this->pos_;
|
||||
const pos_t end = this->end_;
|
||||
uint8_t saved[N];
|
||||
std::memcpy(saved, this->buf_, pos);
|
||||
while (this->pos_ != 0) {
|
||||
// A partial frame left behind the handed over ones is searched on the next idle pass
|
||||
if (this->drain_(this->replay_(1, this->pos_), handle))
|
||||
return;
|
||||
}
|
||||
std::memcpy(this->buf_, saved, pos);
|
||||
this->pos_ = pos;
|
||||
this->end_ = end;
|
||||
this->held_end_ = pos;
|
||||
}
|
||||
|
||||
// Every byte but the middle of a frame; returns the length of a completed frame, or 0
|
||||
size_t ESPHOME_FRAME_READER_COLD feed_edge_(uint8_t c) {
|
||||
// A byte that starts or ends a frame clears the searched mark, so it cannot match a later partial frame
|
||||
this->held_end_ = 0;
|
||||
const pos_t pos = this->pos_;
|
||||
if (pos == 0) {
|
||||
const bool header = ((c == FORMATS::FIRST) || ...);
|
||||
if (header) {
|
||||
this->buf_[0] = c;
|
||||
this->pos_ = 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if constexpr (sizeof...(FORMATS) == 1) {
|
||||
return this->feed_as_<FORMATS...>(pos, c);
|
||||
} else {
|
||||
const uint8_t first = this->buf_[0];
|
||||
size_t len = 0;
|
||||
(void) ((first == FORMATS::FIRST && ((len = this->feed_as_<FORMATS>(pos, c)), true)) || ...);
|
||||
return len;
|
||||
}
|
||||
}
|
||||
|
||||
template<class F> size_t feed_as_(pos_t pos, uint8_t c) {
|
||||
if (pos < F::HEADER_LEN && c != F::header_byte(pos)) {
|
||||
// A later byte of the broken header, or c, may start the real one
|
||||
this->buf_[pos] = c;
|
||||
return this->replay_(1, pos + 1);
|
||||
}
|
||||
this->buf_[pos++] = c;
|
||||
if (pos == F::MIN_LEN) {
|
||||
const size_t len = F::Length::get(this->buf_);
|
||||
if (len < F::SHORTEST || len > N)
|
||||
return this->replay_(1, pos);
|
||||
this->end_ = len;
|
||||
}
|
||||
if (pos == this->end_) {
|
||||
this->pos_ = 0;
|
||||
this->end_ = 0;
|
||||
this->held_end_ = pos;
|
||||
return pos;
|
||||
}
|
||||
this->pos_ = pos;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Start over and feed buf_[from..to) again; writes stay behind the read position. On a completed frame, moves the
|
||||
// bytes not fed yet right behind it, for drain_(), and returns its length; 0 otherwise.
|
||||
size_t ESPHOME_FRAME_READER_COLD replay_(size_t from, size_t to) {
|
||||
this->pos_ = 0;
|
||||
this->end_ = 0;
|
||||
for (size_t i = from; i < to; i++) {
|
||||
const size_t len = this->feed_edge_(this->buf_[i]);
|
||||
if (len == 0)
|
||||
continue;
|
||||
// A nested replay may already have put bytes of its own behind the frame
|
||||
const size_t held = this->held_end_;
|
||||
const size_t rest = to - i - 1;
|
||||
std::memmove(this->buf_ + held, this->buf_ + i + 1, rest);
|
||||
this->held_end_ = held + rest;
|
||||
return len;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Before buf_, so they stay within a short load offset of the reader when N is large
|
||||
pos_t pos_{0};
|
||||
// Length of the frame being collected once its length is known; 0 otherwise
|
||||
pos_t end_{0};
|
||||
// End of a held frame plus the bytes after it not fed yet; or, after flush() kept a partial frame, the pos_ it
|
||||
// searched
|
||||
pos_t held_end_{0};
|
||||
uint8_t buf_[N];
|
||||
};
|
||||
|
||||
} // namespace esphome
|
||||
|
||||
#undef ESPHOME_FRAME_READER_COLD
|
||||
@@ -20,6 +20,12 @@ static constexpr uint8_t FRAME_30[30] = {0xAA, 0xFF, 0x03, 0x00, 0x0E, 0x03, 0xB
|
||||
static constexpr uint8_t ULTRASONIC_NOISY[] = {0x12, 0xFF, 0x07, 0xA1, 0xA7, 0x34, 0xFF, 0xFF, 0x07, 0xA1, 0xA7,
|
||||
0x00, 0xFF, 0x07, 0xA1, 0xA7, 0x56, 0xFF, 0x07, 0xA1, 0xA7};
|
||||
|
||||
// Pioneer WT shaped frames (A5 01 header, whole-frame length at byte 7) of 12, 20 and 12 bytes among noise
|
||||
static constexpr uint8_t LENGTH_FRAMES[] = {0x00, 0xA5, 0x01, 0x01, 0x23, 0x00, 0xA5, 0x00, 0x0C, 0x11, 0x22, 0x33,
|
||||
0x44, 0xA5, 0x01, 0x01, 0x23, 0x00, 0xA5, 0x00, 0x14, 0x11, 0x22, 0x33,
|
||||
0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0x7E, 0xA5, 0x01,
|
||||
0x01, 0x23, 0x00, 0xA5, 0x00, 0x0C, 0x11, 0x22, 0x33, 0x44};
|
||||
|
||||
// --- LineReader: one GGA sentence fed byte by byte ---
|
||||
|
||||
static void LineReader_NmeaLine(benchmark::State &state) {
|
||||
@@ -93,4 +99,32 @@ static void FrameReader_UltrasonicNoisy(benchmark::State &state) {
|
||||
}
|
||||
BENCHMARK(FrameReader_UltrasonicNoisy);
|
||||
|
||||
// --- LengthFrameReader: frames of different lengths among noise ---
|
||||
|
||||
// A5 01, whole-frame length at 7
|
||||
using PioneerFormat = FrameFormat<FrameHeader<0xA5, 0x01>, FrameLengthU8<LengthField{.offset = 7}>>;
|
||||
|
||||
static void LengthFrameReader_MixedFrames(benchmark::State &state) {
|
||||
LengthFrameReader<255, PioneerFormat> reader;
|
||||
const uint8_t *bytes = LENGTH_FRAMES;
|
||||
uint32_t frames = 0;
|
||||
|
||||
auto handle = [&frames](const uint8_t *frame, size_t /*len*/) {
|
||||
benchmark::DoNotOptimize(frame);
|
||||
frames++;
|
||||
return true;
|
||||
};
|
||||
|
||||
for (auto _ : state) {
|
||||
benchmark::DoNotOptimize(bytes);
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
for (size_t j = 0; j < sizeof(LENGTH_FRAMES); j++)
|
||||
reader.feed(bytes[j], handle);
|
||||
}
|
||||
benchmark::DoNotOptimize(frames);
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
BENCHMARK(LengthFrameReader_MixedFrames);
|
||||
|
||||
} // namespace esphome::benchmarks
|
||||
@@ -140,4 +140,372 @@ TEST(FrameReaderTest, SixteenBitIndex) {
|
||||
EXPECT_EQ(frames[0][299], 0x42);
|
||||
}
|
||||
|
||||
// Feeds bytes and collects every frame the reader hands over, rejecting those reject() picks.
|
||||
template<size_t N, class... F, class R>
|
||||
static Frames feed_all(LengthFrameReader<N, F...> &reader, const Bytes &input, R reject) {
|
||||
Frames frames;
|
||||
auto handle = [&](const uint8_t *frame, size_t len) {
|
||||
frames.emplace_back(frame, frame + len);
|
||||
return !reject(frames.back());
|
||||
};
|
||||
for (uint8_t c : input)
|
||||
reader.feed(c, handle);
|
||||
return frames;
|
||||
}
|
||||
template<size_t N, class... F> static Frames feed_all(LengthFrameReader<N, F...> &reader, const Bytes &input) {
|
||||
return feed_all(reader, input, [](const Bytes & /*frame*/) { return false; });
|
||||
}
|
||||
|
||||
// Takes every frame and keeps a copy
|
||||
struct Collect {
|
||||
Frames seen;
|
||||
bool operator()(const uint8_t *frame, size_t len) {
|
||||
this->seen.emplace_back(frame, frame + len);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
// pioneer_wt: A5 01, one byte at 7 holds the whole length
|
||||
using OneByte = FrameFormat<FrameHeader<0xA5, 0x01>, FrameLengthU8<LengthField{.offset = 7}>>;
|
||||
// tuya: 55 AA, version, command, payload length (2, BE), payload, checksum
|
||||
using TwoByteBE = FrameFormat<FrameHeader<0x55, 0xAA>, PayloadLengthU16BE<LengthField{.offset = 4, .trailer = 1}>>;
|
||||
// ld24xx command and data frames: header (4), payload length (2, LE), payload, footer (4)
|
||||
using LdCmd =
|
||||
FrameFormat<FrameHeader<0xFD, 0xFC, 0xFB, 0xFA>, PayloadLengthU16LE<LengthField{.offset = 4, .trailer = 4}>>;
|
||||
using LdData =
|
||||
FrameFormat<FrameHeader<0xF4, 0xF3, 0xF2, 0xF1>, PayloadLengthU16LE<LengthField{.offset = 4, .trailer = 4}>>;
|
||||
// ld2410s short frame: 6E, always 5 bytes
|
||||
using LdShort = FrameFormat<FrameHeader<0x6E>, FrameFixedLength<5>>;
|
||||
// Small format for the recovery tests: A5, one byte at 1 holds the whole length
|
||||
using Small = FrameFormat<FrameHeader<0xA5>, FrameLengthU8<LengthField{.offset = 1}>>;
|
||||
|
||||
static_assert(sizeof(LengthFrameReader<255, OneByte>) == 258);
|
||||
static_assert(sizeof(LengthFrameReader<300, TwoByteBE>) == 306);
|
||||
|
||||
TEST(LengthFrameReaderTest, OneByteLength) {
|
||||
LengthFrameReader<16, OneByte> reader;
|
||||
EXPECT_EQ(feed_all(reader, {0x00, 0xA5, 0x01, 0x10, 0x11, 0x12, 0x13, 0x00, 0x09, 0x42}),
|
||||
(Frames{{0xA5, 0x01, 0x10, 0x11, 0x12, 0x13, 0x00, 0x09, 0x42}}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, TwoByteBigEndianPayloadLength) {
|
||||
LengthFrameReader<300, TwoByteBE> reader;
|
||||
// Payload of 0x0102 = 258 bytes: 265 in all, past an 8-bit index
|
||||
Bytes input{0x55, 0xAA, 0x03, 0x07, 0x01, 0x02};
|
||||
input.resize(265, 0x33);
|
||||
Frames frames = feed_all(reader, input);
|
||||
ASSERT_EQ(frames.size(), 1u);
|
||||
EXPECT_EQ(frames[0].size(), 265u);
|
||||
// Too long for N: not a frame
|
||||
EXPECT_EQ(feed_all(reader, {0x55, 0xAA, 0x03, 0x07, 0x01, 0x30}), Frames{});
|
||||
EXPECT_EQ(reader.length(), 0u);
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, LittleEndianLengthsAboveAByte) {
|
||||
// Whole-frame length 0x0102 = 258, low byte first
|
||||
LengthFrameReader<300, FrameFormat<FrameHeader<0xA5>, FrameLengthU16LE<LengthField{.offset = 1}>>> whole;
|
||||
Bytes input{0xA5, 0x02, 0x01};
|
||||
input.resize(258, 0x33);
|
||||
Frames frames = feed_all(whole, input);
|
||||
ASSERT_EQ(frames.size(), 1u);
|
||||
EXPECT_EQ(frames[0].size(), 258u);
|
||||
// Payload length 0x0100 = 256 after the field, then a 1 byte trailer: 260 in all
|
||||
LengthFrameReader<300, FrameFormat<FrameHeader<0xA5>, PayloadLengthU16LE<LengthField{.offset = 1, .trailer = 1}>>>
|
||||
payload;
|
||||
input = {0xA5, 0x00, 0x01};
|
||||
input.resize(260, 0x44);
|
||||
frames = feed_all(payload, input);
|
||||
ASSERT_EQ(frames.size(), 1u);
|
||||
EXPECT_EQ(frames[0].size(), 260u);
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, TwoFormatsPickedByFirstByte) {
|
||||
LengthFrameReader<32, LdCmd, LdData> reader;
|
||||
const Bytes cmd{0xFD, 0xFC, 0xFB, 0xFA, 0x02, 0x00, 0xA0, 0x01, 0x04, 0x03, 0x02, 0x01};
|
||||
const Bytes data{0xF4, 0xF3, 0xF2, 0xF1, 0x01, 0x00, 0x55, 0xF8, 0xF7, 0xF6, 0xF5};
|
||||
Bytes input{0x00};
|
||||
input.insert(input.end(), data.begin(), data.end());
|
||||
input.insert(input.end(), cmd.begin(), cmd.end());
|
||||
EXPECT_EQ(feed_all(reader, input), (Frames{data, cmd}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, FixedAndLengthFormatsTogether) {
|
||||
LengthFrameReader<32, LdShort, LdData> reader;
|
||||
const Bytes small{0x6E, 0x01, 0x02, 0x03, 0x62};
|
||||
const Bytes data{0xF4, 0xF3, 0xF2, 0xF1, 0x00, 0x00, 0xF8, 0xF7, 0xF6, 0xF5};
|
||||
Bytes input = small;
|
||||
input.insert(input.end(), data.begin(), data.end());
|
||||
input.insert(input.end(), small.begin(), small.end());
|
||||
EXPECT_EQ(feed_all(reader, input), (Frames{small, data, small}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, BrokenHeaderRealigns) {
|
||||
LengthFrameReader<32, LdCmd, LdData> reader;
|
||||
// FD FC then F4 breaks the command header and starts a data header
|
||||
const Bytes data{0xF4, 0xF3, 0xF2, 0xF1, 0x00, 0x00, 0xF8, 0xF7, 0xF6, 0xF5};
|
||||
Bytes input{0xFD, 0xFC};
|
||||
input.insert(input.end(), data.begin(), data.end());
|
||||
EXPECT_EQ(feed_all(reader, input), (Frames{data}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, BrokenHeaderHoldsAShorterFrameOfAnotherFormat) {
|
||||
// FD FC FB then 77 breaks the command header; its replay finds a whole 2 byte FC frame and keeps the 77 behind it
|
||||
LengthFrameReader<32, LdCmd, FrameFormat<FrameHeader<0xFC>, FrameFixedLength<2>>> reader;
|
||||
EXPECT_EQ(feed_all(reader, {0xFD, 0xFC, 0xFB, 0x77}), (Frames{{0xFC, 0xFB}}));
|
||||
EXPECT_EQ(reader.length(), 0u);
|
||||
// The held 77 was noise, so the next frame is found normally
|
||||
EXPECT_EQ(feed_all(reader, {0xFC, 0x01}), (Frames{{0xFC, 0x01}}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, RejectedFrameHoldsFramesOfBothFormats) {
|
||||
LengthFrameReader<32, LdShort, Small> reader;
|
||||
// A rejected 12 byte A5 frame holds a whole short frame, a whole A5 frame and the start of another
|
||||
auto reject_12 = [](const Bytes &frame) { return frame.size() == 12; };
|
||||
EXPECT_EQ(feed_all(reader, {0xA5, 0x0C, 0x6E, 0x01, 0x02, 0x03, 0x62, 0xA5, 0x03, 0x09, 0xA5, 0x05}, reject_12),
|
||||
(Frames{{0xA5, 0x0C, 0x6E, 0x01, 0x02, 0x03, 0x62, 0xA5, 0x03, 0x09, 0xA5, 0x05},
|
||||
{0x6E, 0x01, 0x02, 0x03, 0x62},
|
||||
{0xA5, 0x03, 0x09}}));
|
||||
EXPECT_EQ(reader.length(), 2u);
|
||||
EXPECT_EQ(feed_all(reader, {0x11, 0x22, 0x33}), (Frames{{0xA5, 0x05, 0x11, 0x22, 0x33}}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, BadLengthRealignsAfterFirstByte) {
|
||||
LengthFrameReader<8, Small> reader;
|
||||
// 0x01 ends the frame before its length byte and 0x09 is longer than 8: neither is a frame.
|
||||
// The second A5 inside the bad one starts the real frame.
|
||||
EXPECT_EQ(feed_all(reader, {0xA5, 0x01, 0xA5, 0x09, 0xA5, 0x04, 0x01, 0x02}), (Frames{{0xA5, 0x04, 0x01, 0x02}}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, LengthByteCanStartHeader) {
|
||||
LengthFrameReader<8, FrameFormat<FrameHeader<0x09>, FrameLengthU8<LengthField{.offset = 1}>>> reader;
|
||||
// Length 9 is longer than 8, so the length byte becomes the next header
|
||||
EXPECT_EQ(feed_all(reader, {0x09, 0x09, 0x03, 0x07}), (Frames{{0x09, 0x03, 0x07}}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, LengthTracksPartialFrame) {
|
||||
LengthFrameReader<8, Small> reader;
|
||||
EXPECT_EQ(feed_all(reader, {0x01, 0xA5, 0x05}), Frames{});
|
||||
EXPECT_EQ(reader.length(), 2u);
|
||||
EXPECT_EQ(feed_all(reader, {0x10, 0x20, 0x30}), (Frames{{0xA5, 0x05, 0x10, 0x20, 0x30}}));
|
||||
EXPECT_EQ(reader.length(), 0u);
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, RejectedFrameIsSearchedForAHeader) {
|
||||
LengthFrameReader<8, Small> reader;
|
||||
// The A5 taken as a header was payload; the real frame starts at the second A5
|
||||
auto reject_long = [](const Bytes &frame) { return frame.size() == 5; };
|
||||
EXPECT_EQ(feed_all(reader, {0xA5, 0x05, 0x00, 0xA5, 0x04}, reject_long), (Frames{{0xA5, 0x05, 0x00, 0xA5, 0x04}}));
|
||||
EXPECT_EQ(reader.length(), 2u);
|
||||
EXPECT_EQ(feed_all(reader, {0x10, 0x20}, reject_long), (Frames{{0xA5, 0x04, 0x10, 0x20}}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, RejectionRecoversEveryFrameInside) {
|
||||
LengthFrameReader<16, Small> reader;
|
||||
// The 9 byte frame is rejected; the two frames inside it are handed over too, and a partial one is kept
|
||||
auto reject_9 = [](const Bytes &frame) { return frame.size() == 9; };
|
||||
EXPECT_EQ(feed_all(reader, {0xA5, 0x09, 0xA5, 0x03, 0x01, 0xA5, 0x02, 0xA5, 0x04}, reject_9),
|
||||
(Frames{{0xA5, 0x09, 0xA5, 0x03, 0x01, 0xA5, 0x02, 0xA5, 0x04}, {0xA5, 0x03, 0x01}, {0xA5, 0x02}}));
|
||||
EXPECT_EQ(reader.length(), 2u);
|
||||
EXPECT_EQ(feed_all(reader, {0x01, 0x02}), (Frames{{0xA5, 0x04, 0x01, 0x02}}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, RecoveredFrameRejectedTooIsSearched) {
|
||||
LengthFrameReader<16, Small> reader;
|
||||
// The 9 byte frame holds a 4 byte candidate, which holds a 3 byte one; both outer ones are rejected
|
||||
auto reject_outer = [](const Bytes &frame) { return frame.size() != 3; };
|
||||
EXPECT_EQ(
|
||||
feed_all(reader, {0xA5, 0x09, 0xA5, 0x04, 0xA5, 0x03, 0x01, 0x02, 0x03}, reject_outer),
|
||||
(Frames{{0xA5, 0x09, 0xA5, 0x04, 0xA5, 0x03, 0x01, 0x02, 0x03}, {0xA5, 0x04, 0xA5, 0x03}, {0xA5, 0x03, 0x01}}));
|
||||
EXPECT_EQ(reader.length(), 0u);
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, FlushHandsOverFramesHeldBack) {
|
||||
LengthFrameReader<16, Small> reader;
|
||||
Collect handle;
|
||||
// A corrupt length of 15 holds back two complete frames until flush()
|
||||
for (uint8_t c : {0xA5, 0x0F, 0xA5, 0x04, 0x01, 0x02, 0xA5, 0x03, 0x09})
|
||||
reader.feed(c, handle);
|
||||
EXPECT_EQ(handle.seen, Frames{});
|
||||
EXPECT_EQ(reader.length(), 9u);
|
||||
reader.flush(handle);
|
||||
EXPECT_EQ(handle.seen, (Frames{{0xA5, 0x04, 0x01, 0x02}, {0xA5, 0x03, 0x09}}));
|
||||
EXPECT_EQ(reader.length(), 0u);
|
||||
reader.flush(handle); // nothing collected: nothing to do
|
||||
EXPECT_EQ(handle.seen.size(), 2u);
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, FlushKeepsASlowFrameUntilTheRestArrives) {
|
||||
LengthFrameReader<16, Small> reader;
|
||||
Collect handle;
|
||||
for (uint8_t c : {0xA5, 0x06, 0xA5, 0x03})
|
||||
reader.feed(c, handle);
|
||||
reader.flush(handle);
|
||||
EXPECT_EQ(handle.seen, Frames{});
|
||||
EXPECT_EQ(reader.length(), 4u);
|
||||
for (uint8_t c : {0x01, 0x02})
|
||||
reader.feed(c, handle);
|
||||
EXPECT_EQ(handle.seen, (Frames{{0xA5, 0x06, 0xA5, 0x03, 0x01, 0x02}}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, FlushKeepsASlowFrameBehindTheFramesItHandsOver) {
|
||||
LengthFrameReader<16, Small> reader;
|
||||
Collect handle;
|
||||
// A corrupt length holds a whole frame, then the start of a slow one
|
||||
for (uint8_t c : {0xA5, 0x0F, 0xA5, 0x04, 0x01, 0x02, 0xA5, 0x05, 0x11})
|
||||
reader.feed(c, handle);
|
||||
reader.flush(handle);
|
||||
reader.flush(handle);
|
||||
EXPECT_EQ(handle.seen, (Frames{{0xA5, 0x04, 0x01, 0x02}}));
|
||||
EXPECT_EQ(reader.length(), 3u);
|
||||
for (uint8_t c : {0x22, 0x33})
|
||||
reader.feed(c, handle);
|
||||
EXPECT_EQ(handle.seen, (Frames{{0xA5, 0x04, 0x01, 0x02}, {0xA5, 0x05, 0x11, 0x22, 0x33}}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, ReceivingUntilAnIdleFlushKeepsTheFrame) {
|
||||
LengthFrameReader<16, Small> reader;
|
||||
Collect handle;
|
||||
EXPECT_FALSE(reader.receiving());
|
||||
for (uint8_t c : {0xA5, 0x06, 0x01})
|
||||
reader.feed(c, handle);
|
||||
EXPECT_TRUE(reader.receiving());
|
||||
reader.flush(handle);
|
||||
EXPECT_FALSE(reader.receiving());
|
||||
reader.feed(0x02, handle);
|
||||
EXPECT_TRUE(reader.receiving());
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, NewBytesAtTheSearchedLengthAreSearchedAgain) {
|
||||
// A5 with a whole-frame length at 5, and a fixed two-byte 6E frame
|
||||
LengthFrameReader<16, FrameFormat<FrameHeader<0xA5>, FrameLengthU8<LengthField{.offset = 5}>>,
|
||||
FrameFormat<FrameHeader<0x6E>, FrameFixedLength<2>>>
|
||||
reader;
|
||||
Collect handle;
|
||||
for (uint8_t c : {0xA5, 0xA5, 0x00, 0x00, 0x6E})
|
||||
reader.feed(c, handle);
|
||||
reader.flush(handle);
|
||||
reader.feed(0x01, handle);
|
||||
reader.flush(handle);
|
||||
EXPECT_EQ(handle.seen, (Frames{{0x6E, 0x01}}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, ResyncAfterASearchedFrameIsSearchedAgain) {
|
||||
// A5 with a whole-frame length at 3, and a fixed two-byte AA frame
|
||||
LengthFrameReader<6, FrameFormat<FrameHeader<0xA5>, FrameLengthU8<LengthField{.offset = 3}>>,
|
||||
FrameFormat<FrameHeader<0xAA>, FrameFixedLength<2>>>
|
||||
reader;
|
||||
Collect handle;
|
||||
for (uint8_t c : {0xA5, 0xA5, 0xAA})
|
||||
reader.feed(c, handle);
|
||||
reader.flush(handle);
|
||||
reader.feed(0x07, handle);
|
||||
reader.flush(handle);
|
||||
EXPECT_EQ(handle.seen, (Frames{{0xAA, 0x07}}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, FlushKeepsTheFrameWhenEveryFrameInsideIsRejected) {
|
||||
LengthFrameReader<16, Small> reader;
|
||||
size_t calls = 0;
|
||||
auto reject = [&calls](const uint8_t *, size_t) {
|
||||
calls++;
|
||||
return false;
|
||||
};
|
||||
for (uint8_t c : {0xA5, 0x0F, 0xA5, 0x03, 0x09})
|
||||
reader.feed(c, reject);
|
||||
reader.flush(reject);
|
||||
EXPECT_EQ(calls, 1u);
|
||||
EXPECT_EQ(reader.length(), 5u);
|
||||
// Searched once until a new byte comes
|
||||
reader.flush(reject);
|
||||
EXPECT_EQ(calls, 1u);
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, ResetForgetsThePartialFrame) {
|
||||
LengthFrameReader<16, Small> reader;
|
||||
EXPECT_EQ(feed_all(reader, {0xA5, 0x05, 0x11}), Frames{});
|
||||
reader.reset();
|
||||
EXPECT_EQ(reader.length(), 0u);
|
||||
EXPECT_EQ(feed_all(reader, {0xA5, 0x03, 0x22}), (Frames{{0xA5, 0x03, 0x22}}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, DropKeptLeavesAFrameStillComingIn) {
|
||||
LengthFrameReader<16, Small> reader;
|
||||
Collect handle;
|
||||
for (uint8_t c : {0xA5, 0x06, 0x01})
|
||||
reader.feed(c, handle);
|
||||
reader.drop_kept();
|
||||
EXPECT_EQ(reader.length(), 3u);
|
||||
reader.flush(handle);
|
||||
reader.drop_kept();
|
||||
EXPECT_EQ(reader.length(), 0u);
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, DropKeptInsideAHandlerKeepsTheHeldFrames) {
|
||||
LengthFrameReader<16, Small> reader;
|
||||
Frames seen;
|
||||
auto handle = [&reader, &seen](const uint8_t *frame, size_t len) {
|
||||
seen.emplace_back(frame, frame + len);
|
||||
reader.drop_kept();
|
||||
return frame[1] != 0x09;
|
||||
};
|
||||
for (uint8_t c : {0xA5, 0x09, 0xA5, 0x03, 0x01, 0xA5, 0x03, 0x02, 0x00})
|
||||
reader.feed(c, handle);
|
||||
EXPECT_EQ(seen,
|
||||
(Frames{{0xA5, 0x09, 0xA5, 0x03, 0x01, 0xA5, 0x03, 0x02, 0x00}, {0xA5, 0x03, 0x01}, {0xA5, 0x03, 0x02}}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, IdleAfterHandledFrameFindsNothingInIt) {
|
||||
LengthFrameReader<16, Small> reader;
|
||||
Collect handle;
|
||||
// A handled frame whose payload looks like a frame of its own, then noise
|
||||
for (uint8_t c : {0xA5, 0x06, 0xA5, 0x03, 0x01, 0x02, 0x00})
|
||||
reader.feed(c, handle);
|
||||
reader.flush(handle);
|
||||
EXPECT_EQ(handle.seen, (Frames{{0xA5, 0x06, 0xA5, 0x03, 0x01, 0x02}}));
|
||||
}
|
||||
|
||||
// A custom rule known at the first byte that reads the buffer builds (only FrameFixedLength is checked up front)
|
||||
struct FirstByteLength {
|
||||
static constexpr size_t KNOWN_AT = 1;
|
||||
static constexpr size_t get(const uint8_t *buf) { return buf[0]; }
|
||||
};
|
||||
|
||||
TEST(LengthFrameReaderTest, CustomRuleKnownAtTheFirstByte) {
|
||||
LengthFrameReader<8, FrameFormat<FrameHeader<0x03>, FirstByteLength>> reader;
|
||||
EXPECT_EQ(feed_all(reader, {0x03, 0x01, 0x02, 0x03}), (Frames{{0x03, 0x01, 0x02}}));
|
||||
}
|
||||
|
||||
// A5, payload length at 1, payload, checksum; a frame has at least 2 payload bytes
|
||||
using MinTwo =
|
||||
FrameFormat<FrameHeader<0xA5>, PayloadLengthU8<LengthField{.offset = 1, .trailer = 1}>, FrameMinLength<5>>;
|
||||
|
||||
TEST(LengthFrameReaderTest, LengthBelowTheMinimumIsRejectedAtTheHeader) {
|
||||
LengthFrameReader<16, MinTwo> reader;
|
||||
EXPECT_EQ(feed_all(reader, {0xA5, 0x00, 0xA5, 0x02, 0x10, 0x20, 0x30}), (Frames{{0xA5, 0x02, 0x10, 0x20, 0x30}}));
|
||||
}
|
||||
|
||||
static bool second_byte_not_ff(const uint8_t *buf) { return buf[1] != 0xFF; }
|
||||
// A5, a byte that must not be FF, payload length at 2, payload
|
||||
using Checked =
|
||||
FrameFormat<FrameHeader<0xA5>, CheckedLength<PayloadLengthU8<LengthField{.offset = 2}>, 3, second_byte_not_ff>>;
|
||||
|
||||
static size_t &warned_len() {
|
||||
static size_t len = 0;
|
||||
return len;
|
||||
}
|
||||
static void note_too_long(size_t len) { warned_len() = len; }
|
||||
|
||||
TEST(LengthFrameReaderTest, WarnIfLongerRejectsAndReports) {
|
||||
LengthFrameReader<
|
||||
8, FrameFormat<FrameHeader<0xA5>, WarnIfLonger<FrameLengthU8<LengthField{.offset = 1}>, 8, note_too_long>>>
|
||||
reader;
|
||||
EXPECT_EQ(feed_all(reader, {0xA5, 0x09, 0xA5, 0x03, 0x42}), (Frames{{0xA5, 0x03, 0x42}}));
|
||||
EXPECT_EQ(warned_len(), 9u);
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, CheckedLengthRejectsABadHeader) {
|
||||
LengthFrameReader<16, Checked> reader;
|
||||
EXPECT_EQ(feed_all(reader, {0xA5, 0xFF, 0x01, 0xA5, 0x00, 0x01, 0x42}), (Frames{{0xA5, 0x00, 0x01, 0x42}}));
|
||||
}
|
||||
|
||||
} // namespace esphome::core::testing
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <algorithm>
|
||||
#include "common.h"
|
||||
#include "esphome/components/uart/uart.h"
|
||||
#include "esphome/core/frame_reader.h"
|
||||
|
||||
namespace esphome::uart::testing {
|
||||
|
||||
@@ -179,4 +180,66 @@ TEST(UARTReadAvailableTest, DiscardStopsOnReadFailure) {
|
||||
EXPECT_EQ(uart.read_sizes.size(), 3u);
|
||||
}
|
||||
|
||||
// Keeps a copy of each frame; takes every frame except those whose length byte is reject_len
|
||||
struct Collect {
|
||||
std::vector<std::vector<uint8_t>> frames;
|
||||
uint8_t reject_len{0};
|
||||
bool operator()(const uint8_t *f, size_t len) {
|
||||
this->frames.emplace_back(f, f + len);
|
||||
return f[1] != this->reject_len;
|
||||
}
|
||||
};
|
||||
|
||||
// AA, length byte, then that many bytes
|
||||
using GapReader = LengthFrameReader<16, FrameFormat<FrameHeader<0xAA>, PayloadLengthU8<LengthField{.offset = 1}>>>;
|
||||
|
||||
TEST(UARTReadFramesTest, FeedsTheReaderAndStampsTheTime) {
|
||||
::testing::NiceMock<FakeRxUART> uart(std::vector<uint8_t>{0xAA, 0x01, 0x07});
|
||||
UARTDevice dev(&uart);
|
||||
GapReader reader;
|
||||
Collect handle;
|
||||
uint32_t last_rx = 0;
|
||||
EXPECT_TRUE(dev.read_frames(reader, handle, 500, last_rx));
|
||||
EXPECT_EQ(last_rx, 500u);
|
||||
EXPECT_EQ(handle.frames, (std::vector<std::vector<uint8_t>>{{0xAA, 0x01, 0x07}}));
|
||||
}
|
||||
|
||||
TEST(UARTReadFramesTest, IdlePassAfterTheGapFlushesAStalledFrame) {
|
||||
// A corrupt length of 9 holds the whole frame AA 01 07 behind it
|
||||
::testing::NiceMock<FakeRxUART> uart(std::vector<uint8_t>{0xAA, 0x09, 0xAA, 0x01, 0x07});
|
||||
UARTDevice dev(&uart);
|
||||
GapReader reader;
|
||||
Collect handle{.reject_len = 0x09};
|
||||
uint32_t last_rx = 0;
|
||||
dev.read_frames(reader, handle, 1000, last_rx, 100);
|
||||
dev.read_frames(reader, handle, 1099, last_rx, 100);
|
||||
EXPECT_TRUE(handle.frames.empty());
|
||||
dev.read_frames(reader, handle, 1100, last_rx, 100);
|
||||
EXPECT_EQ(handle.frames, (std::vector<std::vector<uint8_t>>{{0xAA, 0x01, 0x07}}));
|
||||
EXPECT_EQ(reader.length(), 0u);
|
||||
}
|
||||
|
||||
TEST(UARTReadFramesTest, BytesAfterASlowLoopAreFedNotFlushed) {
|
||||
::testing::NiceMock<FakeRxUART> uart(std::vector<uint8_t>{0xAA, 0x02, 0x01});
|
||||
UARTDevice dev(&uart);
|
||||
GapReader reader;
|
||||
Collect handle;
|
||||
uint32_t last_rx = 0;
|
||||
dev.read_frames(reader, handle, 1000, last_rx, 100);
|
||||
// The rest of the frame waited out a 500 ms loop
|
||||
uart.append({0x02});
|
||||
dev.read_frames(reader, handle, 1500, last_rx, 100);
|
||||
EXPECT_EQ(handle.frames, (std::vector<std::vector<uint8_t>>{{0xAA, 0x02, 0x01, 0x02}}));
|
||||
}
|
||||
|
||||
TEST(UARTReadFramesTest, ReportsAFailedRead) {
|
||||
::testing::NiceMock<FakeRxUART> uart(std::vector<uint8_t>{0xAA, 0x01, 0x07});
|
||||
uart.fail_after_reads = 0;
|
||||
UARTDevice dev(&uart);
|
||||
GapReader reader;
|
||||
Collect handle;
|
||||
uint32_t last_rx = 0;
|
||||
EXPECT_FALSE(dev.read_frames(reader, handle, 500, last_rx));
|
||||
}
|
||||
|
||||
} // namespace esphome::uart::testing
|
||||
Reference in new issue
Block a user