Skip to content

feat: add support for tag block, integrate UDP, TCP, file and single message parsing #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ default = ["std"]

[dependencies]
nom = { version = "7", default-features = false }
heapless = { version = "0.7" }
heapless = { version = "0.8.0" }
tokio = { version = "1.0", features = ["full"] }
clap = "4.5.11"
tempfile = "3"


[[bin]]
name = "aisparser"
Expand Down
81 changes: 51 additions & 30 deletions src/bin/aisparser.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,56 @@
use ais::lib;
use ais::{decode, decode_from_file, decode_from_tcp, decode_from_udp};
use clap::{Arg, Command};
use std::error::Error;

use ais::sentence::{AisFragments, AisParser};
use lib::std::io::BufRead;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let matches = Command::new("ais-decode")
.version("1.0")
.about("AIS message decoding")
.arg(
Arg::new("udp")
.short('u')
.long("udp")
.value_name("ADDRESS")
.help("Address to listen for UDP messages"),
)
.arg(
Arg::new("tcp")
.short('t')
.long("tcp")
.value_name("ADDRESS")
.help("Address to connect for TCP messages"),
)
.arg(
Arg::new("file")
.short('f')
.long("file")
.value_name("PATH")
.help("Path to the file to read AIS messages from"),
)
.arg(
Arg::new("message")
.short('m')
.long("message")
.value_name("AIS_MESSAGE")
.help("A single AIS message to decode"),
)
.get_matches();

use lib::std::io;

fn parse_nmea_line(parser: &mut AisParser, line: &[u8]) -> Result<(), ais::errors::Error> {
let sentence = parser.parse(line, true)?;
if let AisFragments::Complete(sentence) = sentence {
println!(
"{:?}\t{:?}",
lib::std::str::from_utf8(line).unwrap(),
sentence.message
);
if let Some(address) = matches.get_one::<String>("udp") {
decode_from_udp(address).await?;
} else if let Some(address) = matches.get_one::<String>("tcp") {
decode_from_tcp(address).await?;
} else if let Some(path) = matches.get_one::<String>("file") {
decode_from_file(path).await?;
} else if let Some(message) = matches.get_one::<String>("message") {
match decode(message.as_bytes()) {
Ok(parsed_message) => println!("Parsed AIS Message: {:?}", parsed_message),
Err(e) => eprintln!("Failed to parse AIS message: {}", e),
}
} else {
eprintln!("No valid command provided.");
}
Ok(())
}

fn main() {
let mut parser = AisParser::new();
let stdin = io::stdin();
{
let handle = stdin.lock();

handle
.split(b'\n')
.map(|line| line.unwrap())
.for_each(|line| {
parse_nmea_line(&mut parser, &line).unwrap_or_else(|err| {
eprintln!("{:?}\t{:?}", lib::std::str::from_utf8(&line).unwrap(), err);
});
});
}
Ok(())
}
2 changes: 2 additions & 0 deletions src/decoders/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//! Function utilities
pub mod utils;
Loading
Loading