diff --git a/src/cfdp/pdu/eof.rs b/src/cfdp/pdu/eof.rs index bfbc65e..8451533 100644 --- a/src/cfdp/pdu/eof.rs +++ b/src/cfdp/pdu/eof.rs @@ -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) } diff --git a/src/cfdp/pdu/file_data.rs b/src/cfdp/pdu/file_data.rs index 1207a04..ee6846c 100644 --- a/src/cfdp/pdu/file_data.rs +++ b/src/cfdp/pdu/file_data.rs @@ -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) } diff --git a/src/cfdp/pdu/finished.rs b/src/cfdp/pdu/finished.rs index 5b14a72..29cf747 100644 --- a/src/cfdp/pdu/finished.rs +++ b/src/cfdp/pdu/finished.rs @@ -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) } diff --git a/src/cfdp/pdu/metadata.rs b/src/cfdp/pdu/metadata.rs index 382cd25..fbdb982 100644 --- a/src/cfdp/pdu/metadata.rs +++ b/src/cfdp/pdu/metadata.rs @@ -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) } diff --git a/src/cfdp/pdu/mod.rs b/src/cfdp/pdu/mod.rs index 148e8d4..2ef5771 100644 --- a/src/cfdp/pdu/mod.rs +++ b/src/cfdp/pdu/mod.rs @@ -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};