add crc serialization for all packets
All checks were successful
Rust/spacepackets/pipeline/pr-main This commit looks good
Rust/spacepackets/pipeline/head This commit looks good

This commit is contained in:
Robin Müller 2023-06-12 13:00:58 +02:00
parent e48c2fe368
commit 895080bbc0
Signed by: muellerr
GPG Key ID: A649FB78196E3849
5 changed files with 28 additions and 13 deletions

View File

@ -1,9 +1,9 @@
use crate::cfdp::pdu::{
generic_length_checks_pdu_deserialization, read_fss_field, write_fss_field, FileDirectiveType,
PduError, PduHeader,
add_pdu_crc, generic_length_checks_pdu_deserialization, read_fss_field, write_fss_field,
FileDirectiveType, PduError, PduHeader,
};
use crate::cfdp::tlv::EntityIdTlv;
use crate::cfdp::{ConditionCode, LargeFileFlag};
use crate::cfdp::{ConditionCode, CrcFlag, LargeFileFlag};
use crate::{ByteConversionError, SizeMissmatch};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
@ -87,6 +87,9 @@ impl EofPdu {
if let Some(fault_location) = self.fault_location {
current_idx += fault_location.write_to_be_bytes(buf)?;
}
if self.pdu_header.pdu_conf.crc_flag == CrcFlag::WithCrc {
current_idx = add_pdu_crc(buf, current_idx);
}
Ok(current_idx)
}

View File

@ -1,5 +1,6 @@
use crate::cfdp::pdu::{
generic_length_checks_pdu_deserialization, read_fss_field, write_fss_field, PduError, PduHeader,
add_pdu_crc, generic_length_checks_pdu_deserialization, read_fss_field, write_fss_field,
PduError, PduHeader,
};
use crate::cfdp::{CrcFlag, LargeFileFlag, PduType, SegmentMetadataFlag};
use crate::{ByteConversionError, SizeMissmatch};
@ -185,6 +186,9 @@ impl<'seg_meta, 'file_data> FileDataPdu<'seg_meta, 'file_data> {
)?;
buf[current_idx..current_idx + self.file_data.len()].copy_from_slice(self.file_data);
current_idx += self.file_data.len();
if self.pdu_header.pdu_conf.crc_flag == CrcFlag::WithCrc {
current_idx = add_pdu_crc(buf, current_idx);
}
Ok(current_idx)
}

View File

@ -1,8 +1,8 @@
use crate::cfdp::pdu::{
generic_length_checks_pdu_deserialization, FileDirectiveType, PduError, PduHeader,
add_pdu_crc, generic_length_checks_pdu_deserialization, FileDirectiveType, PduError, PduHeader,
};
use crate::cfdp::tlv::{EntityIdTlv, Tlv, TlvType, TlvTypeField};
use crate::cfdp::{ConditionCode, PduType, TlvLvError};
use crate::cfdp::{ConditionCode, CrcFlag, PduType, TlvLvError};
use crate::{ByteConversionError, SizeMissmatch};
use num_enum::{IntoPrimitive, TryFromPrimitive};
#[cfg(feature = "serde")]
@ -157,6 +157,9 @@ impl<'fs_responses> FinishedPdu<'fs_responses> {
if let Some(fault_location) = self.fault_location {
current_idx += fault_location.write_to_be_bytes(&mut buf[current_idx..])?;
}
if self.pdu_header.pdu_conf.crc_flag == CrcFlag::WithCrc {
current_idx = add_pdu_crc(buf, current_idx);
}
Ok(current_idx)
}

View File

@ -1,11 +1,11 @@
use crate::cfdp::lv::Lv;
use crate::cfdp::pdu::{
generic_length_checks_pdu_deserialization, read_fss_field, write_fss_field, FileDirectiveType,
PduError, PduHeader,
add_pdu_crc, generic_length_checks_pdu_deserialization, read_fss_field, write_fss_field,
FileDirectiveType, PduError, PduHeader,
};
use crate::cfdp::tlv::Tlv;
use crate::cfdp::{ChecksumType, CrcFlag, LargeFileFlag, PduType};
use crate::{ByteConversionError, SizeMissmatch, CRC_CCITT_FALSE};
use crate::{ByteConversionError, SizeMissmatch};
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
#[cfg(feature = "serde")]
@ -226,10 +226,7 @@ impl<'src_name, 'dest_name, 'opts> MetadataPdu<'src_name, 'dest_name, 'opts> {
current_idx += opts.len();
}
if self.pdu_header.pdu_conf.crc_flag == CrcFlag::WithCrc {
let mut digest = CRC_CCITT_FALSE.digest();
digest.update(&buf[..current_idx]);
buf[current_idx..current_idx + 2].copy_from_slice(&digest.finalize().to_be_bytes());
current_idx += 2;
current_idx = add_pdu_crc(buf, current_idx);
}
Ok(current_idx)
}

View File

@ -549,6 +549,14 @@ pub(crate) fn generic_length_checks_pdu_deserialization(
Ok(())
}
pub(crate) fn add_pdu_crc(buf: &mut [u8], mut current_idx: usize) -> usize {
let mut digest = CRC_CCITT_FALSE.digest();
digest.update(&buf[..current_idx]);
buf[current_idx..current_idx + 2].copy_from_slice(&digest.finalize().to_be_bytes());
current_idx += 2;
current_idx
}
#[cfg(test)]
mod tests {
use crate::cfdp::pdu::{CommonPduConfig, PduError, PduHeader, FIXED_HEADER_LEN};