mirror of
https://github.com/esphome/esphome.git
synced 2026-11-18 19:15:59 +00:00
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d966ec6f71 | ||
|
|
1feda1bcaa | ||
|
|
3b302d3821 | ||
|
|
cd0258944d | ||
|
|
512d143a3a |
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...>`, `LengthFrameReader<N, FrameFormat<...>...>` |
|
||||
| loops on `available()` / `read_byte()` | UART `read_available(on_byte)`, `discard_available()`, `LineReader<N>`, `FrameReader<N, HEADER...>` |
|
||||
| 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,8 +246,7 @@ 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...>`, frames that give their own length
|
||||
with `LengthFrameReader<N, FrameFormat<FrameLengthU16LE<POS, ADD>, HEADER...>...>`; I2C has `update_register`,
|
||||
`LineReader<N>`, fixed-length binary frames with `FrameReader<N, HEADER...>`; 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
|
||||
|
||||
+13
-227
@@ -1,33 +1,11 @@
|
||||
#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;
|
||||
}
|
||||
template<class FIRST, class... REST> struct First {
|
||||
using type = FIRST;
|
||||
};
|
||||
} // 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 {
|
||||
@@ -40,12 +18,11 @@ 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 != frame_reader_detail::header_byte<HEADER...>(pos)) {
|
||||
if (pos < HEADER_LEN && c != header_byte(pos)) {
|
||||
// A later byte of the broken header, or c, may start the real one
|
||||
if (pos == 0)
|
||||
return nullptr;
|
||||
this->buf_[pos] = c;
|
||||
return this->replay_(pos);
|
||||
this->replay_(pos);
|
||||
return nullptr;
|
||||
}
|
||||
this->buf_[pos++] = c;
|
||||
if (pos < N) {
|
||||
@@ -67,215 +44,24 @@ template<size_t N, uint8_t... HEADER> class FrameReader {
|
||||
void resync() { this->replay_(N - 1); }
|
||||
|
||||
protected:
|
||||
// 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) {
|
||||
// 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) {
|
||||
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 for FrameFormat: the frame length is known once KNOWN_AT bytes are collected, and get() returns it
|
||||
/// for the whole frame, header included. A component can pass its own type with these two members.
|
||||
/// One length byte at POS; ADD is added to give the whole frame length.
|
||||
template<size_t POS, size_t ADD = 0> struct FrameLengthU8 {
|
||||
static constexpr size_t KNOWN_AT = POS + 1;
|
||||
static constexpr size_t get(const uint8_t *buf) { return buf[POS] + ADD; }
|
||||
};
|
||||
/// Two length bytes at POS, high byte first.
|
||||
template<size_t POS, size_t ADD = 0> struct FrameLengthU16BE {
|
||||
static constexpr size_t KNOWN_AT = POS + 2;
|
||||
static constexpr size_t get(const uint8_t *buf) { return ((size_t(buf[POS]) << 8) | buf[POS + 1]) + ADD; }
|
||||
};
|
||||
/// Two length bytes at POS, low byte first.
|
||||
template<size_t POS, size_t ADD = 0> struct FrameLengthU16LE {
|
||||
static constexpr size_t KNOWN_AT = POS + 2;
|
||||
static constexpr size_t get(const uint8_t *buf) { return (buf[POS] | (size_t(buf[POS + 1]) << 8)) + ADD; }
|
||||
};
|
||||
/// A frame type that always has LEN bytes.
|
||||
template<size_t LEN> struct FrameFixedLength {
|
||||
static constexpr size_t KNOWN_AT = 1;
|
||||
static constexpr size_t get(const uint8_t * /*buf*/) { return LEN; }
|
||||
};
|
||||
|
||||
/// One frame type for LengthFrameReader: frames that start with the header bytes HEADER... and whose length follows
|
||||
/// LENGTH, one of the FrameLength types above.
|
||||
template<class LENGTH, uint8_t... HEADER> struct FrameFormat {
|
||||
using Length = LENGTH;
|
||||
static constexpr size_t HEADER_LEN = sizeof...(HEADER);
|
||||
static constexpr uint8_t FIRST = frame_reader_detail::header_byte<HEADER...>(0);
|
||||
// Bytes collected when the length is checked; also the shortest frame
|
||||
static constexpr size_t MIN_LEN = std::max<size_t>({LENGTH::KNOWN_AT, HEADER_LEN, 2});
|
||||
static_assert(HEADER_LEN >= 1 && HEADER_LEN <= 8, "FrameFormat takes 1 to 8 header bytes");
|
||||
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 frame_reader_detail::header_byte<HEADER...>(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, footers and a minimum length that covers them 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); });
|
||||
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");
|
||||
static_assert(N >= 2 && N <= 0xFFFF, "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");
|
||||
|
||||
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.
|
||||
template<class F> inline void __attribute__((always_inline)) feed(uint8_t c, F &&handle) {
|
||||
if (const uint8_t *frame = this->feed(c))
|
||||
this->drain_(frame, handle);
|
||||
}
|
||||
|
||||
/// For a partial frame left too long without a byte, which a corrupt length can cause: give up on it, and pass the
|
||||
/// frames it held back to handle.
|
||||
template<class F> void flush(F &&handle) {
|
||||
while (this->pos_ != 0)
|
||||
this->drain_(this->resync(), handle);
|
||||
}
|
||||
|
||||
/// Lower level: feed one byte; returns the completed frame, valid until the next call, or nullptr. After it, call
|
||||
/// next() when the frame is handled or resync() when it is rejected, until they return nullptr.
|
||||
inline const uint8_t *__attribute__((always_inline)) feed(uint8_t c) {
|
||||
// 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;
|
||||
return nullptr;
|
||||
}
|
||||
return this->feed_edge_(c);
|
||||
}
|
||||
|
||||
/// Length of the frame feed(), resync() or next() returned; valid until the next call on the reader.
|
||||
size_t frame_length() const {
|
||||
if constexpr (sizeof...(FORMATS) == 1) {
|
||||
return frame_reader_detail::First<FORMATS...>::type::Length::get(this->buf_);
|
||||
} else {
|
||||
const uint8_t first = this->buf_[0];
|
||||
size_t len = 0;
|
||||
(void) ((first == FORMATS::FIRST && ((len = FORMATS::Length::get(this->buf_)), true)) || ...);
|
||||
return len;
|
||||
}
|
||||
}
|
||||
|
||||
/// 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_; }
|
||||
|
||||
/// Forget the partial frame and any held bytes, and look for a header from the next byte.
|
||||
void reset() {
|
||||
this->pos_ = 0;
|
||||
this->end_ = 0;
|
||||
this->held_end_ = 0;
|
||||
}
|
||||
|
||||
/// Lower level: for a rejected frame, or a partial one: drop its first byte and look for a frame in the rest.
|
||||
/// Returns the next frame found, or nullptr. Does nothing when no frame is held and nothing is collected.
|
||||
const uint8_t *resync() { return this->replay_(1, this->pos_ != 0 ? this->pos_ : this->held_end_); }
|
||||
|
||||
/// Lower level: after handling a frame, look for the next one in the bytes that followed it in a resync().
|
||||
/// Returns that frame, or nullptr once they are used up.
|
||||
const uint8_t *next() { return this->replay_(this->frame_length(), this->held_end_); }
|
||||
|
||||
protected:
|
||||
template<class F> void ESPHOME_FRAME_READER_COLD drain_(const uint8_t *frame, F &handle) {
|
||||
while (frame != nullptr)
|
||||
frame = handle(frame, this->frame_length()) ? this->next() : this->resync();
|
||||
}
|
||||
|
||||
// A header byte, the bytes up to the length, the last byte of a frame, or a byte between frames
|
||||
const uint8_t *ESPHOME_FRAME_READER_COLD feed_edge_(uint8_t c) {
|
||||
const pos_t pos = this->pos_;
|
||||
if (pos == 0) {
|
||||
this->held_end_ = 0;
|
||||
const bool header = ((c == FORMATS::FIRST) || ...);
|
||||
if (header) {
|
||||
this->buf_[0] = c;
|
||||
this->pos_ = 1;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
if constexpr (sizeof...(FORMATS) == 1) {
|
||||
return this->feed_as_<FORMATS...>(pos, c);
|
||||
} else {
|
||||
const uint8_t first = this->buf_[0];
|
||||
const uint8_t *frame = nullptr;
|
||||
(void) ((first == FORMATS::FIRST && ((frame = this->feed_as_<FORMATS>(pos, c)), true)) || ...);
|
||||
return frame;
|
||||
}
|
||||
}
|
||||
|
||||
template<class F> const uint8_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::MIN_LEN || 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 this->buf_;
|
||||
}
|
||||
this->pos_ = pos;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// 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 next() and resync(), and returns it.
|
||||
const uint8_t *ESPHOME_FRAME_READER_COLD replay_(size_t from, size_t to) {
|
||||
this->pos_ = 0;
|
||||
this->end_ = 0;
|
||||
this->held_end_ = 0;
|
||||
for (size_t i = from; i < to; i++) {
|
||||
if (this->feed_edge_(this->buf_[i]) == nullptr)
|
||||
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 this->buf_;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
uint8_t buf_[N];
|
||||
pos_t pos_{0};
|
||||
// Length of the frame being collected once its length is known; 0 otherwise
|
||||
pos_t end_{0};
|
||||
// While a frame is held: end of the frame plus the bytes after it that are not fed yet; 0 otherwise
|
||||
pos_t held_end_{0};
|
||||
};
|
||||
|
||||
} // namespace esphome
|
||||
|
||||
#undef ESPHOME_FRAME_READER_COLD
|
||||
@@ -20,12 +20,6 @@ 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) {
|
||||
@@ -99,28 +93,4 @@ static void FrameReader_UltrasonicNoisy(benchmark::State &state) {
|
||||
}
|
||||
BENCHMARK(FrameReader_UltrasonicNoisy);
|
||||
|
||||
// --- LengthFrameReader: frames of different lengths among noise ---
|
||||
|
||||
static void LengthFrameReader_MixedFrames(benchmark::State &state) {
|
||||
LengthFrameReader<255, FrameFormat<FrameLengthU8<7>, 0xA5, 0x01>> reader;
|
||||
const uint8_t *bytes = LENGTH_FRAMES;
|
||||
uint32_t frames = 0;
|
||||
|
||||
for (auto _ : state) {
|
||||
benchmark::DoNotOptimize(bytes);
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
for (size_t j = 0; j < sizeof(LENGTH_FRAMES); j++) {
|
||||
const uint8_t *frame = reader.feed(bytes[j]);
|
||||
if (frame == nullptr)
|
||||
continue;
|
||||
benchmark::DoNotOptimize(frame);
|
||||
frames++;
|
||||
}
|
||||
}
|
||||
benchmark::DoNotOptimize(frames);
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kInnerIterations);
|
||||
}
|
||||
BENCHMARK(LengthFrameReader_MixedFrames);
|
||||
|
||||
} // namespace esphome::benchmarks
|
||||
@@ -140,211 +140,4 @@ TEST(FrameReaderTest, SixteenBitIndex) {
|
||||
EXPECT_EQ(frames[0][299], 0x42);
|
||||
}
|
||||
|
||||
// Feeds bytes and collects every completed frame of a LengthFrameReader, taking each one with next().
|
||||
template<size_t N, class... F> static Frames feed_all(LengthFrameReader<N, F...> &reader, const Bytes &input) {
|
||||
Frames frames;
|
||||
for (uint8_t c : input) {
|
||||
for (const uint8_t *frame = reader.feed(c); frame != nullptr; frame = reader.next())
|
||||
frames.emplace_back(frame, frame + reader.frame_length());
|
||||
}
|
||||
return frames;
|
||||
}
|
||||
|
||||
// Feeds bytes and returns the last frame feed() completed, or nullptr.
|
||||
template<size_t N, class... F> static const uint8_t *feed_last(LengthFrameReader<N, F...> &reader, const Bytes &input) {
|
||||
const uint8_t *last = nullptr;
|
||||
for (uint8_t c : input) {
|
||||
if (const uint8_t *frame = reader.feed(c))
|
||||
last = frame;
|
||||
}
|
||||
return last;
|
||||
}
|
||||
|
||||
template<size_t N, class... F> static Bytes held(const LengthFrameReader<N, F...> &reader, const uint8_t *frame) {
|
||||
return Bytes(frame, frame + reader.frame_length());
|
||||
}
|
||||
|
||||
// pioneer_wt: A5 01, one byte at 7 holds the whole length
|
||||
using OneByte = FrameFormat<FrameLengthU8<7>, 0xA5, 0x01>;
|
||||
// tuya: 55 AA, payload length big-endian at 4, header, length and checksum add 7
|
||||
using TwoByteBE = FrameFormat<FrameLengthU16BE<4, 7>, 0x55, 0xAA>;
|
||||
// ld24xx command and data frames: payload length little-endian at 4, header, length and footer add 10
|
||||
using LdCmd = FrameFormat<FrameLengthU16LE<4, 10>, 0xFD, 0xFC, 0xFB, 0xFA>;
|
||||
using LdData = FrameFormat<FrameLengthU16LE<4, 10>, 0xF4, 0xF3, 0xF2, 0xF1>;
|
||||
// ld2410s short frame: 6E, always 5 bytes
|
||||
using LdShort = FrameFormat<FrameFixedLength<5>, 0x6E>;
|
||||
// Small formats for the recovery tests: A5, one byte at 1 holds the whole length
|
||||
using Small = FrameFormat<FrameLengthU8<1>, 0xA5>;
|
||||
|
||||
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
|
||||
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, 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, 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<FrameLengthU8<1>, 0x09>> 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;
|
||||
feed_all(reader, {0xA5, 0x05});
|
||||
EXPECT_EQ(reader.length(), 2u);
|
||||
reader.reset();
|
||||
EXPECT_EQ(reader.length(), 0u);
|
||||
EXPECT_EQ(feed_all(reader, {0x01, 0xA5, 0x03, 0x07}), (Frames{{0xA5, 0x03, 0x07}}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, ResyncFindsHeaderInRejectedFrame) {
|
||||
LengthFrameReader<8, Small> reader;
|
||||
// The A5 taken as a header was payload; the real frame starts at the second A5
|
||||
const uint8_t *frame = feed_last(reader, {0xA5, 0x05, 0x00, 0xA5, 0x04});
|
||||
ASSERT_NE(frame, nullptr);
|
||||
EXPECT_EQ(reader.frame_length(), 5u);
|
||||
EXPECT_EQ(reader.resync(), nullptr);
|
||||
EXPECT_EQ(reader.length(), 2u);
|
||||
EXPECT_EQ(feed_all(reader, {0x10, 0x20}), (Frames{{0xA5, 0x04, 0x10, 0x20}}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, ResyncReturnsEveryFrameInsideRejectedOne) {
|
||||
LengthFrameReader<16, Small> reader;
|
||||
// A bad checksum rejects the 9 byte frame; two complete frames sit inside it, and a partial one after them
|
||||
const uint8_t *frame = feed_last(reader, {0xA5, 0x09, 0xA5, 0x03, 0x01, 0xA5, 0x02, 0xA5, 0x04});
|
||||
ASSERT_NE(frame, nullptr);
|
||||
frame = reader.resync();
|
||||
ASSERT_NE(frame, nullptr);
|
||||
EXPECT_EQ(held(reader, frame), (Bytes{0xA5, 0x03, 0x01}));
|
||||
frame = reader.next();
|
||||
ASSERT_NE(frame, nullptr);
|
||||
EXPECT_EQ(held(reader, frame), (Bytes{0xA5, 0x02}));
|
||||
EXPECT_EQ(reader.next(), nullptr);
|
||||
EXPECT_EQ(reader.length(), 2u);
|
||||
EXPECT_EQ(feed_all(reader, {0x01, 0x02}), (Frames{{0xA5, 0x04, 0x01, 0x02}}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, ResyncAgainAfterRecoveredFrameIsRejected) {
|
||||
LengthFrameReader<16, Small> reader;
|
||||
// The 9 byte frame holds a 4 byte candidate, which holds a 3 byte one, followed by two more bytes
|
||||
const uint8_t *frame = feed_last(reader, {0xA5, 0x09, 0xA5, 0x04, 0xA5, 0x03, 0x01, 0x02, 0x03});
|
||||
ASSERT_NE(frame, nullptr);
|
||||
frame = reader.resync();
|
||||
ASSERT_NE(frame, nullptr);
|
||||
EXPECT_EQ(held(reader, frame), (Bytes{0xA5, 0x04, 0xA5, 0x03}));
|
||||
// Rejected too: the 3 byte frame inside it comes back, and the bytes after the 4 byte one are kept
|
||||
frame = reader.resync();
|
||||
ASSERT_NE(frame, nullptr);
|
||||
EXPECT_EQ(held(reader, frame), (Bytes{0xA5, 0x03, 0x01}));
|
||||
EXPECT_EQ(reader.next(), nullptr);
|
||||
EXPECT_EQ(reader.length(), 0u);
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, ResyncPartialFrameAfterSilence) {
|
||||
LengthFrameReader<16, Small> reader;
|
||||
// A corrupt length of 15 holds back two complete frames until the caller gives up on the partial one
|
||||
EXPECT_EQ(feed_all(reader, {0xA5, 0x0F, 0xA5, 0x04, 0x01, 0x02, 0xA5, 0x03, 0x09}), Frames{});
|
||||
EXPECT_EQ(reader.length(), 9u);
|
||||
Frames found;
|
||||
for (const uint8_t *frame = reader.resync(); frame != nullptr; frame = reader.next())
|
||||
found.push_back(held(reader, frame));
|
||||
EXPECT_EQ(found, (Frames{{0xA5, 0x04, 0x01, 0x02}, {0xA5, 0x03, 0x09}}));
|
||||
EXPECT_EQ(reader.length(), 0u);
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, ResyncWhileIdleIsHarmless) {
|
||||
LengthFrameReader<8, Small> reader;
|
||||
EXPECT_EQ(reader.resync(), nullptr);
|
||||
// A handled frame, then noise: nothing is held any more
|
||||
EXPECT_EQ(feed_all(reader, {0xA5, 0x03, 0x01, 0x00}), (Frames{{0xA5, 0x03, 0x01}}));
|
||||
EXPECT_EQ(reader.resync(), nullptr);
|
||||
// A rejected length leaves nothing collected
|
||||
EXPECT_EQ(feed_all(reader, {0xA5, 0x09}), Frames{});
|
||||
EXPECT_EQ(reader.resync(), nullptr);
|
||||
EXPECT_EQ(feed_all(reader, {0xA5, 0x03, 0x01}), (Frames{{0xA5, 0x03, 0x01}}));
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, HandlerRejectionRecoversFramesInside) {
|
||||
LengthFrameReader<16, Small> reader;
|
||||
Frames seen;
|
||||
// The handler rejects the 9 byte frame; the two frames inside it reach the handler too
|
||||
auto handle = [&seen](const uint8_t *frame, size_t len) {
|
||||
seen.emplace_back(frame, frame + len);
|
||||
return len != 9;
|
||||
};
|
||||
for (uint8_t c : {0xA5, 0x09, 0xA5, 0x03, 0x01, 0xA5, 0x02, 0xA5, 0x04})
|
||||
reader.feed(c, handle);
|
||||
EXPECT_EQ(seen, (Frames{{0xA5, 0x09, 0xA5, 0x03, 0x01, 0xA5, 0x02, 0xA5, 0x04}, {0xA5, 0x03, 0x01}, {0xA5, 0x02}}));
|
||||
EXPECT_EQ(reader.length(), 2u);
|
||||
}
|
||||
|
||||
TEST(LengthFrameReaderTest, FlushHandsOverFramesHeldBack) {
|
||||
LengthFrameReader<16, Small> reader;
|
||||
Frames seen;
|
||||
auto handle = [&seen](const uint8_t *frame, size_t len) {
|
||||
seen.emplace_back(frame, frame + len);
|
||||
return true;
|
||||
};
|
||||
for (uint8_t c : {0xA5, 0x0F, 0xA5, 0x04, 0x01, 0x02, 0xA5, 0x03, 0x09})
|
||||
reader.feed(c, handle);
|
||||
EXPECT_EQ(seen, Frames{});
|
||||
reader.flush(handle);
|
||||
EXPECT_EQ(seen, (Frames{{0xA5, 0x04, 0x01, 0x02}, {0xA5, 0x03, 0x09}}));
|
||||
EXPECT_EQ(reader.length(), 0u);
|
||||
reader.flush(handle); // nothing collected: nothing to do
|
||||
EXPECT_EQ(seen.size(), 2u);
|
||||
}
|
||||
|
||||
} // namespace esphome::core::testing
|
||||
Reference in new issue
Block a user