Skip to content

Commit

Permalink
unicore: fix reset after one message
Browse files Browse the repository at this point in the history
Signed-off-by: Julian Oes <[email protected]>
  • Loading branch information
julianoes authored and bkueng committed Mar 2, 2023
1 parent 82b78c9 commit 9e27229
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
22 changes: 22 additions & 0 deletions gps-parser-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,35 @@ 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();
test_garbage();
test_too_long();
test_uniheadinga_wrong_crc();
test_uniheadinga();
test_uniheadinga_twice();
}

int main(int, char **)
Expand Down
16 changes: 12 additions & 4 deletions src/unicore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -58,7 +56,7 @@ UnicoreParser::Result UnicoreParser::parseChar(char c)

} else {
if (_buffer_pos >= sizeof(_buffer)) {
_state = State::Uninit;
reset();
return Result::None;
}

Expand All @@ -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';
Expand All @@ -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;
}
}
Expand All @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/unicore.h
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit 9e27229

Please sign in to comment.