Native implementation of decoding of SBP (Swift Binary Protocol) used by products made by Swift Navigation. For language agnostic description of the protocol please see the protocol specification documentation.
Example: Print log messages
This example shows how to read messages from stdin and print the contents
of each message MsgLog
to stderr.
use std::convert::TryFrom;
use std::error::Error;
use std::io;
use std::process;
use sbp::messages::logging::MsgLog;
fn example() -> Result<(), Box<dyn Error>> {
let messages = sbp::iter_messages(io::stdin());
for msg in messages {
// The iterator yields Result<Sbp, Error>, so we check the error here.
let msg = msg?;
match MsgLog::try_from(msg) {
Ok(msg) => eprintln!("{}", msg.text),
_ => {}
}
}
Ok(())
}
fn main() {
if let Err(err) = example() {
eprintln!("error: {}", err);
process::exit(1);
}
}
Example: Filter by sender id and write to stdout
This example shows how to read messages from stdin and forward messages with a matching sender_id to stdout.
use std::error::Error;
use std::io;
use std::process;
use sbp::{SbpEncoder, SbpMessage};
fn example(sender_id: u16) -> Result<(), Box<dyn Error>> {
let messages = sbp::iter_messages(io::stdin());
let messages = messages.filter_map(|msg| match msg {
Ok(msg) if msg.sender_id() == Some(sender_id) => Some(msg),
_ => None,
});
SbpEncoder::new(io::stdout()).send_all(messages)?;
Ok(())
}
fn main() {
if let Err(err) = example(42) {
eprintln!("error: {}", err);
process::exit(1);
}
}