Skip to content

Commit

Permalink
rust: expose crc errors [INFRA-125] (#786)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Mobarak committed Mar 12, 2020
1 parent 58c8ec7 commit 9c7d6b6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
7 changes: 5 additions & 2 deletions rust/sbp/src/lib.rs
Expand Up @@ -16,6 +16,7 @@ pub type Result<T> = result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
ParseError,
CrcError,
NotEnoughData,
UnrecoverableFailure,
IoError(std::io::Error),
Expand All @@ -24,9 +25,10 @@ pub enum Error {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::ParseError => write!(f, "Parse Error"),
Error::ParseError => write!(f, "Parse error"),
Error::CrcError => write!(f, "CRC error"),
Error::NotEnoughData => write!(f, "Not enough data"),
Error::UnrecoverableFailure => write!(f, "Unrecoverage Failure"),
Error::UnrecoverableFailure => write!(f, "Unrecoverage failure"),
Error::IoError(err) => write!(f, "IO Error: {}", err),
}
}
Expand All @@ -36,6 +38,7 @@ impl error::Error for Error {
fn description(&self) -> &str {
match self {
Error::ParseError => "An error occured during parsing",
Error::CrcError => "CRC validation failed",
Error::NotEnoughData => "Not enough data available to parse a message",
Error::UnrecoverableFailure => "An unrecoverage failure was encountered",
Error::IoError(err) => err.description(),
Expand Down
8 changes: 4 additions & 4 deletions rust/sbp/src/parser/mod.rs
Expand Up @@ -30,20 +30,20 @@ pub fn frame(input: &[u8]) -> (Result<SBP>, usize) {
let result = tuple((preamble, le_u16, le_u16, payload, le_u16))(input);

match result {
Ok((o, (_preamble, msg_type, sender_id, payload, _crc))) => {
Ok((o, (_preamble, msg_type, sender_id, payload, crc_in))) => {
let mut crc = crc16::State::<crc16::XMODEM>::new();
crc.update(&msg_type.to_le_bytes());
crc.update(&sender_id.to_le_bytes());
crc.update(&[payload.len() as u8]);
crc.update(payload);
if crc.get() == _crc {
if crc.get() == crc_in {
let bytes_read = original_size - o.len();
(
SBP::parse(msg_type, sender_id, &mut &payload[..]),
bytes_read,
bytes_read
)
} else {
(Err(crate::Error::ParseError), 1)
(Err(crate::Error::CrcError), 1)
}
}
// Act like we didn't read anything
Expand Down

0 comments on commit 9c7d6b6

Please sign in to comment.