Expand description
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);
}
}
Bitfield Types
A number of messages have fields that encode multiple values using a bitfield. This crate provides getters and setters on messages with these values. The getters and setters have slightly different semantics that can be grouped into three categories.
Enumerations
Most bitfield members are mapped to a dataless enum. The setter accepts a variant of that enum.
The getter will return a variant of this enum wrapped in a Result
. The getter returns Ok
if the bitfield member contains a known variant of the enum. Otherwise, the integral value of the
bitfield member is returned in the Err
variant. This may be because of a malformed message,
or because you’re SBP client is outdated and new variants of the enum were added.
let mut msg = MsgPosLlh::from(msg);
msg.set_fix_mode(FixMode::DeadReckoning);
assert_eq!(msg.fix_mode(), Ok(FixMode::DeadReckoning));
Integral
Some bitfield members are represented by an integral type. In this case, the getter accepts the
smallest integer type that can contain the bitfield member. For example, if the bitfield member
spans 6 bits, the setter will accept a u8
, and mask off any extra bits when setting the value.
The getter will return the integer value, again using the smallest integer type that will contain
the bitfield member.
let mut msg = MsgStatusReport::from(msg);
msg.set_sbp_major_protocol_version_number(3);
assert_eq!(msg.sbp_major_protocol_version_number(), 3);
Boolean
If the bitfield members is represented by a single bit, getters and setters use bool
s.
let mut msg = MsgDops::from(msg);
msg.set_raim_repair_flag(true);
assert!(msg.raim_repair_flag());
Modules
- Encode/decode SBP messages as JSON.
- Callback based message handler.
- SBP message definitions.
- Extra adaptors for iterators of Sbp messages.
- Types representing GPS times embedded in messsages.
Structs
- The wire representation of an SBP message.
- Writes Sbp messages into a writer.
- Fixed or variable length string and its encoding.
Enums
- All errors that can occur while reading messages.
- All errors that can occur while reading messages.
- Represents any SBP message.
- All errors that can occur while writing messages.
Constants
- Length of the crc of the payload.
- Length of the header section.
- Max length of a frame (header + payload + crc).
- Max length of the variable-sized payload field.
- Min length of a frame (e.g. when payload is empty)
- Position of payload
- Denotes the start of frame transmission.
Traits
- A trait that is used for converting an error in parsing of a messages into a message. A separate trait other than the
Into
trait is defined because it makes it clear that it is ok to panic inside of the handle_error function if it is not recoverable… e.g. if the error is from an I/O error while parsing a message - Common functionality available to all SBP messages.
Functions
- Deserialize IO stream into iterator of raw frames
- Deserialize the IO stream into an iterator of frames. Provide a timeout for the maximum allowed duration without a successful message.
- Deserialize the IO stream into an iterator of messages.
- Deserialize the IO stream into an iterator of messages. Provide a timeout for the maximum allowed duration without a successful message.
- Deserialize the async IO stream into stream of frames
- Deserialize the async IO stream into an stream of messages.
- Serialize the given message as a byte vector.
- Serialize the given message into the IO stream.