Skip to content

Commit 643b0ce

Browse files
committed
Bug fixing
1 parent 2472c69 commit 643b0ce

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

lib/lpfuart/src/lumpparser.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,20 @@ void LumpParser::feedByte(uint8_t byte) {
8282
}
8383

8484
void LumpParser::feedBytes(const uint8_t *data, int len) {
85+
// Add all bytes to the ring buffer first, then process once.
86+
// This ensures onDataFrameDispatched() fires only after the last
87+
// frame in the batch (count_ == 0), not after each individual frame.
8588
for (int i = 0; i < len; i++) {
86-
feedByte(data[i]);
89+
if (count_ >= ringBufSize) {
90+
stats_.bufferOverflows++;
91+
head_ = (head_ + 1) % ringBufSize;
92+
count_--;
93+
}
94+
uint16_t tail = (head_ + count_) % ringBufSize;
95+
buf_[tail] = data[i];
96+
count_++;
8797
}
98+
processBuffer();
8899
}
89100

90101
auto LumpParser::stats() const -> const LumpParserStats & {

test/test_lumpparser/test_main.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,20 @@ class LumpParserT {
376376
}
377377

378378
void feedBytes(const uint8_t *data, int len) {
379-
for (int i = 0; i < len; i++) feedByte(data[i]);
379+
// Pre-load all bytes into the ring buffer, then process once.
380+
// This ensures onDataFrameDispatched() fires only after the last
381+
// frame in the batch (count_ == 0), not after each individual frame.
382+
for (int i = 0; i < len; i++) {
383+
if (count_ >= RING_BUF_SIZE) {
384+
stats_.bufferOverflows++;
385+
head_ = (head_ + 1) % RING_BUF_SIZE;
386+
count_--;
387+
}
388+
uint16_t tail = (head_ + count_) % RING_BUF_SIZE;
389+
buf_[tail] = data[i];
390+
count_++;
391+
}
392+
processBuffer();
380393
}
381394

382395
const LumpParserStats &stats() const { return stats_; }

0 commit comments

Comments
 (0)