Skip to content
This repository has been archived by the owner on May 26, 2024. It is now read-only.

Commit

Permalink
Bugfix: The old implementation of 'isCompleteMessageInParserBuffer' h…
Browse files Browse the repository at this point in the history
…as suffered from the fact that it did not check for the occurence of both $ and \r\n within the parser buffer. Consequently - when during a power down an incomplete message arrives garbage is fed to the decoder which leads to all sorts of trouble down the line. This commit is fixing this issue.
  • Loading branch information
aentinger committed Jan 21, 2021
1 parent edafe97 commit 4855ab1
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/ArduinoNmeaParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,34 @@ void ArduinoNmeaParser::flushParserBuffer()

bool ArduinoNmeaParser::isCompleteNmeaMessageInParserBuffer()
{
if (_parser_buf_elems < 2)
/* Temporarily terminate string with a '\0' terminator in
* order to be able to use string library functions.
*/
_parser_buf[_parser_buf_elems] = '\0';

/* Determine wether or not there exists a '$' marking
* the start of a NMEA message.
*/
char const * nmea_start = strchr(_parser_buf, '$');
if (!nmea_start)
return false;

/* Now that we've got a '$' marking the start of a NMEA
* message it is time to check if there's also an end
* to it.
*/
char const * nmea_stop_cr = strchr(nmea_start, '\r');
if (!nmea_stop_cr)
return false;

char const prev_last = _parser_buf[_parser_buf_elems - 2];
char const last = _parser_buf[_parser_buf_elems - 1];
char const * nmea_stop_lf = strchr(nmea_start, '\n');
if (!nmea_stop_lf)
return false;

return ((prev_last == '\r') && (last == '\n'));
/* Lastly: check if the LF is really directly following
* the CR within the NMEA message.
*/
return ((nmea_stop_cr + 1) == nmea_stop_lf);
}

void ArduinoNmeaParser::terminateParserBuffer()
Expand Down

0 comments on commit 4855ab1

Please sign in to comment.