From 5c3c9a9bde67223ba2a26af07dd1dcf98b08d330 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 30 May 2023 11:09:41 +0200 Subject: [PATCH] add test run config --- .idea/runConfigurations/Test.xml | 19 +++++++++++++ src/cfdp/pdu/file_data.rs | 34 ++++++++++++++++++++-- src/cfdp/pdu/metadata.rs | 2 +- src/cfdp/pdu/mod.rs | 49 ++++++++++++++++++++++++-------- 4 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 .idea/runConfigurations/Test.xml diff --git a/.idea/runConfigurations/Test.xml b/.idea/runConfigurations/Test.xml new file mode 100644 index 0000000..d051ff1 --- /dev/null +++ b/.idea/runConfigurations/Test.xml @@ -0,0 +1,19 @@ + + + + \ No newline at end of file diff --git a/src/cfdp/pdu/file_data.rs b/src/cfdp/pdu/file_data.rs index 8839375..cece350 100644 --- a/src/cfdp/pdu/file_data.rs +++ b/src/cfdp/pdu/file_data.rs @@ -15,6 +15,8 @@ pub enum RecordContinuationState { StartAndEnd = 0b11, } +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct SegmentMetadata<'seg_meta> { record_continuation_state: RecordContinuationState, seg_metadata_len: u8, @@ -41,8 +43,11 @@ impl SegmentMetadata<'_> { } } +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct FileDataPdu<'seg_meta, 'file_data> { pdu_header: PduHeader, + #[cfg_attr(feature = "serde", serde(borrow))] segment_metadata: Option>, offset: u64, file_data: &'file_data [u8], @@ -101,6 +106,18 @@ impl<'seg_meta, 'file_data> FileDataPdu<'seg_meta, 'file_data> { len } + pub fn offset(&self) -> u64 { + self.offset + } + + pub fn file_data(&self) -> &'file_data [u8] { + self.file_data + } + + pub fn seg_metadata(&self) -> Option<&SegmentMetadata> { + self.segment_metadata.as_ref() + } + pub fn write_to_bytes(&self, buf: &mut [u8]) -> Result { if buf.len() < self.written_len() { return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch { @@ -130,8 +147,21 @@ impl<'seg_meta, 'file_data> FileDataPdu<'seg_meta, 'file_data> { #[cfg(test)] mod tests { + use crate::cfdp::pdu::file_data::FileDataPdu; + use crate::cfdp::pdu::{CommonPduConfig, PduHeader}; + use crate::util::UbfU8; + #[test] fn test_basic() { - + let src_id = UbfU8::new(1); + let dest_id = UbfU8::new(2); + let transaction_seq_num = UbfU8::new(3); + let common_conf = + CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_seq_num).unwrap(); + let pdu_header = PduHeader::new_for_file_data_default(common_conf, 0); + let file_data: [u8; 4] = [1, 2, 3, 4]; + let fd_pdu = FileDataPdu::new_no_seg_metadata(pdu_header, 10, &file_data); + assert_eq!(fd_pdu.file_data(), file_data); + assert_eq!(fd_pdu.offset(), 10); } -} \ No newline at end of file +} diff --git a/src/cfdp/pdu/metadata.rs b/src/cfdp/pdu/metadata.rs index ce0a70a..6278335 100644 --- a/src/cfdp/pdu/metadata.rs +++ b/src/cfdp/pdu/metadata.rs @@ -47,7 +47,7 @@ pub fn build_metadata_opts_from_vec( /// Helper structure to loop through all options of a metadata PDU. It should be noted that /// iterators in Rust are not fallible, but the TLV creation can fail, for example if the raw TLV -/// data in invalid for some reason. In that case, the iterator will yield [None] because there +/// data is invalid for some reason. In that case, the iterator will yield [None] because there /// is no way to recover from this. /// /// The user can accumulate the length of all TLVs yielded by the iterator and compare it against diff --git a/src/cfdp/pdu/mod.rs b/src/cfdp/pdu/mod.rs index 31fafb4..efca589 100644 --- a/src/cfdp/pdu/mod.rs +++ b/src/cfdp/pdu/mod.rs @@ -227,8 +227,43 @@ impl PduHeader { seg_metadata_flag: SegmentMetadataFlag, seg_ctrl: SegmentationControl, ) -> Self { - PduHeader { - pdu_type: PduType::FileData, + Self::new_generic( + PduType::FileData, + pdu_conf, + pdu_datafield_len, + seg_metadata_flag, + seg_ctrl, + ) + } + + pub fn new_for_file_data_default(pdu_conf: CommonPduConfig, pdu_datafield_len: u16) -> Self { + Self::new_generic( + PduType::FileData, + pdu_conf, + pdu_datafield_len, + SegmentMetadataFlag::NotPresent, + SegmentationControl::NoRecordBoundaryPreservation, + ) + } + pub fn new_no_file_data(pdu_conf: CommonPduConfig, pdu_datafield_len: u16) -> Self { + Self::new_generic( + PduType::FileDirective, + pdu_conf, + pdu_datafield_len, + SegmentMetadataFlag::NotPresent, + SegmentationControl::NoRecordBoundaryPreservation, + ) + } + + pub fn new_generic( + pdu_type: PduType, + pdu_conf: CommonPduConfig, + pdu_datafield_len: u16, + seg_metadata_flag: SegmentMetadataFlag, + seg_ctrl: SegmentationControl, + ) -> Self { + Self { + pdu_type, pdu_conf, seg_metadata_flag, seg_ctrl, @@ -236,16 +271,6 @@ impl PduHeader { } } - pub fn new_no_file_data(pdu_conf: CommonPduConfig, pdu_datafield_len: u16) -> Self { - PduHeader { - pdu_type: PduType::FileDirective, - pdu_conf, - seg_metadata_flag: SegmentMetadataFlag::NotPresent, - seg_ctrl: SegmentationControl::NoRecordBoundaryPreservation, - pdu_datafield_len, - } - } - /// Returns only the length of the PDU header when written to a raw buffer. pub fn header_len(&self) -> usize { FIXED_HEADER_LEN