Skip to content
Merged
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
1 change: 1 addition & 0 deletions generator/sbpg/targets/resources/sbp-cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ license = "MIT"
categories = ["parsing"]
edition = "2018"
keywords = ["encoding", "parsing"]
readme = "../../README.md"

[features]
default = []
Expand Down
34 changes: 30 additions & 4 deletions generator/sbpg/targets/resources/sbp_messages_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ mod lib {

pub use crate::wire_format::{WireFormat, PayloadParseError};
pub use crate::sbp_string::{SbpString, Unterminated, NullTerminated, Multipart, DoubleNullTerminated};

#[cfg(feature = "swiftnav")]
pub use crate::time;

pub use super::{ConcreteMessage, Sbp, SbpMessage, TryFromSbpError};

pub use bytes::{Buf, BufMut};
}

use lib::*;
Expand Down Expand Up @@ -91,7 +92,32 @@ pub enum Sbp {
}

impl Sbp {
pub(crate) fn from_frame(mut frame: crate::de::Frame) -> Result<Sbp, PayloadParseError> {
/// Parse a message from a [Frame](crate::Frame).
///
/// # Example
///
/// ```
/// use std::convert::TryInto;
///
/// use sbp::messages::logging::MsgLog;
/// use sbp::{Frame, Sbp};
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // log level 1 and with "hello" as the message
/// let payload: &[u8] = &[1, 104, 101, 108, 108, 111];
/// let frame = Frame {
/// msg_type: 1025,
/// sender_id: 1,
/// payload,
/// };
/// let msg: MsgLog = Sbp::from_frame(frame)?.try_into()?;
/// assert_eq!(msg.sender_id, Some(1));
/// assert_eq!(msg.level, 1);
/// assert_eq!(msg.text.as_bytes(), "hello".as_bytes());
/// Ok(())
/// }
/// ```
pub fn from_frame<B: Buf>(mut frame: crate::Frame<B>) -> Result<Sbp, PayloadParseError> {
match frame.msg_type {
((*- for m in msgs *))
(((m.identifier|camel_case)))::MESSAGE_TYPE => {
Expand Down Expand Up @@ -180,11 +206,11 @@ impl SbpMessage for Sbp {
impl WireFormat for Sbp {
const MIN_ENCODED_LEN: usize = crate::MAX_FRAME_LEN;

fn parse_unchecked(_: &mut bytes::BytesMut) -> Self {
fn parse_unchecked<B: Buf>(_: &mut B) -> Self {
unimplemented!("Sbp must be parsed with Sbp::from_frame");
}

fn write(&self, buf: &mut bytes::BytesMut) {
fn write<B: BufMut>(&self, buf: &mut B) {
match self {
((*- for m in msgs *))
Sbp::(((m.identifier|camel_case)))(msg) => {
Expand Down
4 changes: 2 additions & 2 deletions generator/sbpg/targets/resources/sbp_messages_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ impl WireFormat for (((m.identifier|camel_case))) {
((*- endfor *))
((*- endif *))
}
fn write(&self, ((*- if not m.fields *)) _buf ((*- else *)) buf ((*- endif *)): &mut bytes::BytesMut) {
fn write<B: BufMut>(&self, ((*- if not m.fields *)) _buf ((*- else *)) buf ((*- endif *)): &mut B) {
((*- for f in m.fields *))
WireFormat::write( &self.(((f.identifier|snake_case))), buf);
((*- endfor *))
}
fn parse_unchecked( ((*- if not m.fields *)) _buf ((*- else *)) buf ((*- endif *)): &mut bytes::BytesMut) -> Self {
fn parse_unchecked<B: Buf>( ((*- if not m.fields *)) _buf ((*- else *)) buf ((*- endif *)): &mut B) -> Self {
(((m.identifier|camel_case))) {
((*- if m.is_real_message *))
sender_id: None,
Expand Down
1 change: 1 addition & 0 deletions rust/sbp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ license = "MIT"
categories = ["parsing"]
edition = "2018"
keywords = ["encoding", "parsing"]
readme = "../../README.md"

[features]
default = []
Expand Down
7 changes: 4 additions & 3 deletions rust/sbp/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn stream_messages<R: futures::AsyncRead + Unpin>(
SbpDecoder::framed(input)
}

pub fn parse_frame(buf: &mut BytesMut) -> Option<Result<Frame, CrcError>> {
pub fn parse_frame(buf: &mut BytesMut) -> Option<Result<Frame<BytesMut>, CrcError>> {
if buf.len() < HEADER_LEN {
return None;
}
Expand Down Expand Up @@ -89,11 +89,12 @@ fn check_crc(msg_type: u16, sender_id: u16, payload: &[u8], crc_in: u16) -> bool
crc.get() == crc_in
}

/// The wire representation of an SBP message.
#[derive(Debug)]
pub struct Frame {
pub struct Frame<B> {
pub msg_type: u16,
pub sender_id: u16,
pub payload: BytesMut,
pub payload: B,
}

/// All errors that can occur while reading messages.
Expand Down
2 changes: 1 addition & 1 deletion rust/sbp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub use crate::messages::SbpMessage;
pub use ser::{to_vec, to_writer, Error as SerializeError, SbpEncoder};

#[doc(inline)]
pub use de::{iter_messages, Error as DeserializeError};
pub use de::{iter_messages, Error as DeserializeError, Frame};

#[cfg(feature = "async")]
#[doc(inline)]
Expand Down
32 changes: 16 additions & 16 deletions rust/sbp/src/messages/acquisition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl WireFormat for AcqSvProfile {
+ WireFormat::encoded_len(&self.cf)
+ WireFormat::encoded_len(&self.cp)
}
fn write(&self, buf: &mut bytes::BytesMut) {
fn write<B: BufMut>(&self, buf: &mut B) {
WireFormat::write(&self.job_type, buf);
WireFormat::write(&self.status, buf);
WireFormat::write(&self.cn0, buf);
Expand All @@ -106,7 +106,7 @@ impl WireFormat for AcqSvProfile {
WireFormat::write(&self.cf, buf);
WireFormat::write(&self.cp, buf);
}
fn parse_unchecked(buf: &mut bytes::BytesMut) -> Self {
fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
AcqSvProfile {
job_type: WireFormat::parse_unchecked(buf),
status: WireFormat::parse_unchecked(buf),
Expand Down Expand Up @@ -196,7 +196,7 @@ impl WireFormat for AcqSvProfileDep {
+ WireFormat::encoded_len(&self.cf)
+ WireFormat::encoded_len(&self.cp)
}
fn write(&self, buf: &mut bytes::BytesMut) {
fn write<B: BufMut>(&self, buf: &mut B) {
WireFormat::write(&self.job_type, buf);
WireFormat::write(&self.status, buf);
WireFormat::write(&self.cn0, buf);
Expand All @@ -210,7 +210,7 @@ impl WireFormat for AcqSvProfileDep {
WireFormat::write(&self.cf, buf);
WireFormat::write(&self.cp, buf);
}
fn parse_unchecked(buf: &mut bytes::BytesMut) -> Self {
fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
AcqSvProfileDep {
job_type: WireFormat::parse_unchecked(buf),
status: WireFormat::parse_unchecked(buf),
Expand Down Expand Up @@ -296,13 +296,13 @@ impl WireFormat for MsgAcqResult {
+ WireFormat::encoded_len(&self.cf)
+ WireFormat::encoded_len(&self.sid)
}
fn write(&self, buf: &mut bytes::BytesMut) {
fn write<B: BufMut>(&self, buf: &mut B) {
WireFormat::write(&self.cn0, buf);
WireFormat::write(&self.cp, buf);
WireFormat::write(&self.cf, buf);
WireFormat::write(&self.sid, buf);
}
fn parse_unchecked(buf: &mut bytes::BytesMut) -> Self {
fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
MsgAcqResult {
sender_id: None,
cn0: WireFormat::parse_unchecked(buf),
Expand Down Expand Up @@ -380,13 +380,13 @@ impl WireFormat for MsgAcqResultDepA {
+ WireFormat::encoded_len(&self.cf)
+ WireFormat::encoded_len(&self.prn)
}
fn write(&self, buf: &mut bytes::BytesMut) {
fn write<B: BufMut>(&self, buf: &mut B) {
WireFormat::write(&self.snr, buf);
WireFormat::write(&self.cp, buf);
WireFormat::write(&self.cf, buf);
WireFormat::write(&self.prn, buf);
}
fn parse_unchecked(buf: &mut bytes::BytesMut) -> Self {
fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
MsgAcqResultDepA {
sender_id: None,
snr: WireFormat::parse_unchecked(buf),
Expand Down Expand Up @@ -463,13 +463,13 @@ impl WireFormat for MsgAcqResultDepB {
+ WireFormat::encoded_len(&self.cf)
+ WireFormat::encoded_len(&self.sid)
}
fn write(&self, buf: &mut bytes::BytesMut) {
fn write<B: BufMut>(&self, buf: &mut B) {
WireFormat::write(&self.snr, buf);
WireFormat::write(&self.cp, buf);
WireFormat::write(&self.cf, buf);
WireFormat::write(&self.sid, buf);
}
fn parse_unchecked(buf: &mut bytes::BytesMut) -> Self {
fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
MsgAcqResultDepB {
sender_id: None,
snr: WireFormat::parse_unchecked(buf),
Expand Down Expand Up @@ -545,13 +545,13 @@ impl WireFormat for MsgAcqResultDepC {
+ WireFormat::encoded_len(&self.cf)
+ WireFormat::encoded_len(&self.sid)
}
fn write(&self, buf: &mut bytes::BytesMut) {
fn write<B: BufMut>(&self, buf: &mut B) {
WireFormat::write(&self.cn0, buf);
WireFormat::write(&self.cp, buf);
WireFormat::write(&self.cf, buf);
WireFormat::write(&self.sid, buf);
}
fn parse_unchecked(buf: &mut bytes::BytesMut) -> Self {
fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
MsgAcqResultDepC {
sender_id: None,
cn0: WireFormat::parse_unchecked(buf),
Expand Down Expand Up @@ -613,10 +613,10 @@ impl WireFormat for MsgAcqSvProfile {
fn encoded_len(&self) -> usize {
WireFormat::encoded_len(&self.acq_sv_profile)
}
fn write(&self, buf: &mut bytes::BytesMut) {
fn write<B: BufMut>(&self, buf: &mut B) {
WireFormat::write(&self.acq_sv_profile, buf);
}
fn parse_unchecked(buf: &mut bytes::BytesMut) -> Self {
fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
MsgAcqSvProfile {
sender_id: None,
acq_sv_profile: WireFormat::parse_unchecked(buf),
Expand Down Expand Up @@ -674,10 +674,10 @@ impl WireFormat for MsgAcqSvProfileDep {
fn encoded_len(&self) -> usize {
WireFormat::encoded_len(&self.acq_sv_profile)
}
fn write(&self, buf: &mut bytes::BytesMut) {
fn write<B: BufMut>(&self, buf: &mut B) {
WireFormat::write(&self.acq_sv_profile, buf);
}
fn parse_unchecked(buf: &mut bytes::BytesMut) -> Self {
fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
MsgAcqSvProfileDep {
sender_id: None,
acq_sv_profile: WireFormat::parse_unchecked(buf),
Expand Down
24 changes: 12 additions & 12 deletions rust/sbp/src/messages/bootload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ impl WireFormat for MsgBootloaderHandshakeDepA {
fn encoded_len(&self) -> usize {
WireFormat::encoded_len(&self.handshake)
}
fn write(&self, buf: &mut bytes::BytesMut) {
fn write<B: BufMut>(&self, buf: &mut B) {
WireFormat::write(&self.handshake, buf);
}
fn parse_unchecked(buf: &mut bytes::BytesMut) -> Self {
fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
MsgBootloaderHandshakeDepA {
sender_id: None,
handshake: WireFormat::parse_unchecked(buf),
Expand Down Expand Up @@ -130,8 +130,8 @@ impl WireFormat for MsgBootloaderHandshakeReq {
fn encoded_len(&self) -> usize {
0
}
fn write(&self, _buf: &mut bytes::BytesMut) {}
fn parse_unchecked(_buf: &mut bytes::BytesMut) -> Self {
fn write<B: BufMut>(&self, _buf: &mut B) {}
fn parse_unchecked<B: Buf>(_buf: &mut B) -> Self {
MsgBootloaderHandshakeReq { sender_id: None }
}
}
Expand Down Expand Up @@ -193,11 +193,11 @@ impl WireFormat for MsgBootloaderHandshakeResp {
fn encoded_len(&self) -> usize {
WireFormat::encoded_len(&self.flags) + WireFormat::encoded_len(&self.version)
}
fn write(&self, buf: &mut bytes::BytesMut) {
fn write<B: BufMut>(&self, buf: &mut B) {
WireFormat::write(&self.flags, buf);
WireFormat::write(&self.version, buf);
}
fn parse_unchecked(buf: &mut bytes::BytesMut) -> Self {
fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
MsgBootloaderHandshakeResp {
sender_id: None,
flags: WireFormat::parse_unchecked(buf),
Expand Down Expand Up @@ -256,10 +256,10 @@ impl WireFormat for MsgBootloaderJumpToApp {
fn encoded_len(&self) -> usize {
WireFormat::encoded_len(&self.jump)
}
fn write(&self, buf: &mut bytes::BytesMut) {
fn write<B: BufMut>(&self, buf: &mut B) {
WireFormat::write(&self.jump, buf);
}
fn parse_unchecked(buf: &mut bytes::BytesMut) -> Self {
fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
MsgBootloaderJumpToApp {
sender_id: None,
jump: WireFormat::parse_unchecked(buf),
Expand Down Expand Up @@ -319,8 +319,8 @@ impl WireFormat for MsgNapDeviceDnaReq {
fn encoded_len(&self) -> usize {
0
}
fn write(&self, _buf: &mut bytes::BytesMut) {}
fn parse_unchecked(_buf: &mut bytes::BytesMut) -> Self {
fn write<B: BufMut>(&self, _buf: &mut B) {}
fn parse_unchecked<B: Buf>(_buf: &mut B) -> Self {
MsgNapDeviceDnaReq { sender_id: None }
}
}
Expand Down Expand Up @@ -380,10 +380,10 @@ impl WireFormat for MsgNapDeviceDnaResp {
fn encoded_len(&self) -> usize {
WireFormat::encoded_len(&self.dna)
}
fn write(&self, buf: &mut bytes::BytesMut) {
fn write<B: BufMut>(&self, buf: &mut B) {
WireFormat::write(&self.dna, buf);
}
fn parse_unchecked(buf: &mut bytes::BytesMut) -> Self {
fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
MsgNapDeviceDnaResp {
sender_id: None,
dna: WireFormat::parse_unchecked(buf),
Expand Down
4 changes: 2 additions & 2 deletions rust/sbp/src/messages/ext_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ impl WireFormat for MsgExtEvent {
+ WireFormat::encoded_len(&self.flags)
+ WireFormat::encoded_len(&self.pin)
}
fn write(&self, buf: &mut bytes::BytesMut) {
fn write<B: BufMut>(&self, buf: &mut B) {
WireFormat::write(&self.wn, buf);
WireFormat::write(&self.tow, buf);
WireFormat::write(&self.ns_residual, buf);
WireFormat::write(&self.flags, buf);
WireFormat::write(&self.pin, buf);
}
fn parse_unchecked(buf: &mut bytes::BytesMut) -> Self {
fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
MsgExtEvent {
sender_id: None,
wn: WireFormat::parse_unchecked(buf),
Expand Down
Loading