Skip to content

Commit

Permalink
Merge pull request #15 from nsforth/12-gpgga-nmea-sentence-is-too-long
Browse files Browse the repository at this point in the history
Added "strict" feature, enabled by default. If disabled NMEA max sent…
  • Loading branch information
nsforth authored Dec 2, 2024
2 parents 2b75536 + 61374cf commit a127c26
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
17 changes: 9 additions & 8 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ name: master

on:
push:
branches: [ "master" ]
branches: ["master"]
pull_request:
branches: [ "master" ]
branches: ["master"]

env:
CARGO_TERM_COLOR: always

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose --features mtk
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose
- name: Run tests strict
run: cargo test --no-default-features -F mtk,strict
- name: Run tests non strict
run: cargo test --no-default-features -F mtk
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ readme = "README.md"
[dependencies]

[features]
default = []
default = ["strict"]
mtk = []
strict = []

[badges]
travis-ci = { repository = "nsforth/nmea0183", branch = "v0.4.0" }
codecov = { repository = "nsforth/nmea0183"}
codecov = { repository = "nsforth/nmea0183" }
is-it-maintained-issue-resolution = { repository = "nsforth/nmea0183" }
is-it-maintained-open-issues = { repository = "nsforth/nmea0183" }
maintenance = { status = "actively-developed" }
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ You do not need to do any preprocessing such as split data to strings or NMEA se
# Optional features

Parser supports Mediatek-related PMTKSPF non-standard sentence. It is disabled by default. Use "mtk" feature if you need it.
If your receives somehow violates NMEA spec, try disable "strict" feature which enabled by default. For example, without "strict" feauture sentence size is set to 120 chars instead of standart NMEA 79 chars.

# Examples

Expand Down Expand Up @@ -77,5 +78,3 @@ Should not panic. If so please report issue on project page.
`NMEA format error!` - Possible data corruption. Parser drops all accumulated data and starts seek new sentences.

It's possible to got other very rare error messages that relates to protocol errors. Receivers nowadays mostly do not violate NMEA specs.


16 changes: 12 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ impl TryFrom<&str> for Source {
"GL" => Ok(Source::GLONASS),
"GA" => Ok(Source::Gallileo),
"BD" => Ok(Source::Beidou),
"GN" => Ok(Source::GNSS),
#[cfg(feature = "mtk")]
"GN" => Ok(Source::GNSS),
#[cfg(feature = "mtk")]
"PM" => Ok(Source::MTK),
_ => Err("Source is not supported!"),
}
Expand Down Expand Up @@ -280,10 +280,18 @@ pub enum ParseResult {
GSA(Option<GSA>),
}

#[cfg(feature = "strict")]
/// Maximum allowed sentence length, according to NMEA 183 docs should be not more than 79 chars. Disable strict feature to parse up to 120 chars.
pub const MAX_SENTENCE_LENGTH: usize = 79usize;

#[cfg(not(feature = "strict"))]
/// Maximum allowed sentence length.
pub const MAX_SENTENCE_LENGTH: usize = 120usize;

/// Parses NMEA sentences and stores intermediate parsing state.
/// Parser is tolerant for errors so you should not reinitialize it after errors.
pub struct Parser {
buffer: [u8; 79],
buffer: [u8; MAX_SENTENCE_LENGTH],
buflen: usize,
chksum: u8,
expected_chksum: u8,
Expand Down Expand Up @@ -334,7 +342,7 @@ impl Parser {
/// Constructs new Parser.
pub fn new() -> Parser {
Parser {
buffer: [0u8; 79],
buffer: [0u8; MAX_SENTENCE_LENGTH],
buflen: 0,
chksum: 0,
expected_chksum: 0,
Expand Down
19 changes: 19 additions & 0 deletions tests/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use nmea0183::VTG;
use nmea0183::{ParseResult, Parser, Source};

#[test]
#[cfg(feature = "strict")]
fn test_too_long_sentence() {
let line = "$01234567890123456789012345678901234567890123456789012345678901234567890123456789";
let mut caught_error = false;
Expand All @@ -33,6 +34,24 @@ fn test_too_long_sentence() {
assert!(caught_error);
}

#[test]
#[cfg(not(feature = "strict"))]
fn test_too_long_sentence_non_strict() {
let line = "$01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890012345678901234567890";
let mut caught_error = false;
for result in Parser::new().parse_from_bytes(line.as_bytes()) {
match result {
Ok(_) => continue,
Err("NMEA sentence is too long!") => {
caught_error = true;
break;
}
Err(_) => panic!("Unexpected error caught in test!"),
}
}
assert!(caught_error);
}

#[test]
fn test_correct_but_unsupported_source() {
let mut p = Parser::new();
Expand Down

0 comments on commit a127c26

Please sign in to comment.