From 9e272295491ee91add707566ed5758208f65b7e3 Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Wed, 1 Mar 2023 14:35:59 +1300 Subject: [PATCH] unicore: fix reset after one message Signed-off-by: Julian Oes --- gps-parser-test.cpp | 22 ++++++++++++++++++++++ src/unicore.cpp | 16 ++++++++++++---- src/unicore.h | 3 +-- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/gps-parser-test.cpp b/gps-parser-test.cpp index 61723b6..5d94f83 100644 --- a/gps-parser-test.cpp +++ b/gps-parser-test.cpp @@ -91,6 +91,27 @@ void test_uniheadinga() assert(false); } +void test_uniheadinga_twice() +{ + const char str[] = + "#UNIHEADINGA,89,GPS,FINE,2251,168052600,0,0,18,11;SOL_COMPUTED,NARROW_INT,0.3718,67.0255,-0.7974,0.0000,0.8065,3.3818,\"999\",31,21,21,18,3,01,3,f3*ece5bb07" + "#UNIHEADINGA,88,GPS,FINE,2251,175391000,0,0,18,13;SOL_COMPUTED,NARROW_INT,0.3500,88.4714,-1.5969,0.0000,1.0175,2.8484,\"999\",28,18,18,12,3,01,3,f3*2888f737"; + + UnicoreParser unicore_parser; + + unsigned num_parsed = 0; + + for (unsigned i = 0; i < sizeof(str); ++i) { + auto result = unicore_parser.parseChar(str[i]); + + if (result == UnicoreParser::Result::GotHeading) { + ++num_parsed; + } + } + + assert(num_parsed == 2); +} + void test_unicore() { test_empty(); @@ -98,6 +119,7 @@ void test_unicore() test_too_long(); test_uniheadinga_wrong_crc(); test_uniheadinga(); + test_uniheadinga_twice(); } int main(int, char **) diff --git a/src/unicore.cpp b/src/unicore.cpp index 1e4fa3a..c7da300 100644 --- a/src/unicore.cpp +++ b/src/unicore.cpp @@ -43,8 +43,6 @@ UnicoreParser::Result UnicoreParser::parseChar(char c) case State::Uninit: if (c == '#') { _state = State::GotHashtag; - _buffer_pos = 0; - _buffer_crc_pos = 0; } break; @@ -58,7 +56,7 @@ UnicoreParser::Result UnicoreParser::parseChar(char c) } else { if (_buffer_pos >= sizeof(_buffer)) { - _state = State::Uninit; + reset(); return Result::None; } @@ -70,7 +68,7 @@ UnicoreParser::Result UnicoreParser::parseChar(char c) case State::GotStar: _buffer_crc[_buffer_crc_pos++] = c; - if (_buffer_crc_pos == 8) { + if (_buffer_crc_pos >= 8) { // Make sure the CRC buffer is zero terminated. _buffer_crc[_buffer_crc_pos] = '\0'; @@ -81,13 +79,16 @@ UnicoreParser::Result UnicoreParser::parseChar(char c) if (isHeading()) { if (extractHeading()) { + reset(); return Result::GotHeading; } else { + reset(); return Result::WrongStructure; } } else { + reset(); return Result::UnknownSentence; } } @@ -98,6 +99,13 @@ UnicoreParser::Result UnicoreParser::parseChar(char c) return Result::None; } +void UnicoreParser::reset() +{ + _state = State::Uninit; + _buffer_pos = 0; + _buffer_crc_pos = 0; +} + bool UnicoreParser::crcCorrect() const { const uint32_t crc_calculated = calculateBlockCRC32(_buffer_pos, (uint8_t *)_buffer, 0); diff --git a/src/unicore.h b/src/unicore.h index 187b75d..febefbe 100644 --- a/src/unicore.h +++ b/src/unicore.h @@ -61,12 +61,11 @@ class UnicoreParser private: + void reset(); bool crcCorrect() const; bool isHeading() const; bool extractHeading(); - - // We have seen buffers with 154 bytes. char _buffer[256]; unsigned _buffer_pos {0};