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§
- json
- Encode/decode SBP messages as JSON.
- link
- Callback based message handler.
- messages
- SBP message definitions.
- sbp_
iter_ ext - Extra adaptors for iterators of Sbp messages.
- sbp_
string - time
- Types representing GPS times embedded in messsages.
Structs§
- CrcError
- Decoder
- Frame
- The wire representation of an SBP message.
- Framer
- SbpEncoder
- Writes Sbp messages into a writer.
- SbpString
- Fixed or variable length string and its encoding.
Enums§
- Deserialize
Error - All errors that can occur while reading messages.
- Error
- All errors that can occur while reading messages.
- Sbp
- Represents any SBP message.
- Serialize
Error - All errors that can occur while writing messages.
Constants§
- CRC_LEN
- Length of the crc of the payload.
- HEADER_
LEN - Length of the header section.
- MAX_
FRAME_ LEN - Max length of a frame (header + payload + crc).
- MAX_
PAYLOAD_ LEN - Max length of the variable-sized payload field.
- MIN_
FRAME_ LEN - Min length of a frame (e.g. when payload is empty)
- PAYLOAD_
INDEX - Position of payload
- PREAMBLE
- Denotes the start of frame transmission.
Traits§
- Handle
Parse Error - 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 - SbpIter
Ext - An Iterator blanket implementation that provides extra adaptors for iterators of Sbp messages.
- SbpMessage
- Common functionality available to all SBP messages.
Functions§
- iter_
frames - Deserialize IO stream into iterator of raw frames
- iter_
frames_ with_ timeout - Deserialize the IO stream into an iterator of frames. Provide a timeout for the maximum allowed duration without a successful message.
- iter_
messages - Deserialize the IO stream into an iterator of messages.
- iter_
messages_ with_ timeout - Deserialize the IO stream into an iterator of messages. Provide a timeout for the maximum allowed duration without a successful message.
- stream_
frames - Deserialize the async IO stream into stream of frames
- stream_
frames_ with_ timeout - stream_
messages - Deserialize the async IO stream into an stream of messages.
- stream_
messages_ with_ timeout - to_vec
- Serialize the given message as a byte vector.
- to_
writer - Serialize the given message into the IO stream.