CFDP initial packet support #14
@ -21,7 +21,7 @@ impl MetadataGenericParams {
|
||||
Self {
|
||||
closure_requested,
|
||||
checksum_type,
|
||||
file_size
|
||||
file_size,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -97,6 +97,10 @@ impl<'src_name, 'dest_name, 'opts> MetadataPdu<'src_name, 'dest_name, 'opts> {
|
||||
self.dest_file_name
|
||||
}
|
||||
|
||||
pub fn options(&self) -> Option<&'opts [u8]> {
|
||||
self.options
|
||||
}
|
||||
|
||||
pub fn written_len(&self) -> usize {
|
||||
// One directive type octet, and one byte of the parameter field.
|
||||
let mut len = self.pdu_header.written_len() + 2;
|
||||
@ -139,8 +143,12 @@ impl<'src_name, 'dest_name, 'opts> MetadataPdu<'src_name, 'dest_name, 'opts> {
|
||||
.copy_from_slice(&(self.metadata_params.file_size as u32).to_be_bytes());
|
||||
current_idx += core::mem::size_of::<u32>()
|
||||
}
|
||||
current_idx += self.src_file_name.write_to_be_bytes(buf)?;
|
||||
current_idx += self.dest_file_name.write_to_be_bytes(buf)?;
|
||||
current_idx += self
|
||||
.src_file_name
|
||||
.write_to_be_bytes(&mut buf[current_idx..])?;
|
||||
current_idx += self
|
||||
.dest_file_name
|
||||
.write_to_be_bytes(&mut buf[current_idx..])?;
|
||||
if let Some(opts) = self.options {
|
||||
buf[current_idx..current_idx + opts.len()].copy_from_slice(opts);
|
||||
current_idx += opts.len();
|
||||
@ -151,32 +159,63 @@ impl<'src_name, 'dest_name, 'opts> MetadataPdu<'src_name, 'dest_name, 'opts> {
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use crate::cfdp::ChecksumType;
|
||||
use crate::cfdp::lv::Lv;
|
||||
use crate::cfdp::pdu::metadata::{MetadataGenericParams, MetadataPdu};
|
||||
use crate::cfdp::pdu::tests::verify_raw_header;
|
||||
use crate::cfdp::pdu::{CommonPduConfig, FileDirectiveType, PduHeader};
|
||||
use crate::cfdp::ChecksumType;
|
||||
use crate::util::UbfU8;
|
||||
use crate::cfdp::pdu::{CommonPduConfig, PduHeader};
|
||||
|
||||
#[test]
|
||||
fn test_basic() {
|
||||
fn common_pdu_conf() -> CommonPduConfig {
|
||||
let src_id = UbfU8::new(5);
|
||||
let dest_id = UbfU8::new(10);
|
||||
let transaction_seq_num = UbfU8::new(20);
|
||||
let common_pdu_conf = CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_seq_num).expect("Generating common PDU config");
|
||||
let pdu_header = PduHeader::new_no_file_data(common_pdu_conf, 0);
|
||||
CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_seq_num)
|
||||
.expect("Generating common PDU config")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_basic() {
|
||||
let pdu_header = PduHeader::new_no_file_data(common_pdu_conf(), 0);
|
||||
let metadata_params = MetadataGenericParams::new(false, ChecksumType::Crc32, 10);
|
||||
let src_filename = Lv::new_from_str("hello-world.txt").expect("Generating string LV failed");
|
||||
let src_filename =
|
||||
Lv::new_from_str("hello-world.txt").expect("Generating string LV failed");
|
||||
let src_len = src_filename.len_full();
|
||||
let dest_filename = Lv::new_from_str("hello-world2.txt").expect("Generating destination LV failed");
|
||||
let dest_filename =
|
||||
Lv::new_from_str("hello-world2.txt").expect("Generating destination LV failed");
|
||||
let dest_len = dest_filename.len_full();
|
||||
let metadata_pdu= MetadataPdu::new(pdu_header, metadata_params, src_filename, dest_filename);
|
||||
assert_eq!(metadata_pdu.written_len(), pdu_header.written_len() + 1 + 1 + 4 + src_len + dest_len);
|
||||
let metadata_pdu =
|
||||
MetadataPdu::new(pdu_header, metadata_params, src_filename, dest_filename);
|
||||
assert_eq!(
|
||||
metadata_pdu.written_len(),
|
||||
pdu_header.written_len() + 1 + 1 + 4 + src_len + dest_len
|
||||
);
|
||||
assert_eq!(metadata_pdu.src_file_name(), src_filename);
|
||||
assert_eq!(metadata_pdu.dest_file_name(), dest_filename);
|
||||
assert_eq!(metadata_pdu.options(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serialization() {
|
||||
|
||||
let pdu_header = PduHeader::new_no_file_data(common_pdu_conf(), 0);
|
||||
let metadata_params = MetadataGenericParams::new(false, ChecksumType::Crc32, 10);
|
||||
let src_filename =
|
||||
Lv::new_from_str("hello-world.txt").expect("Generating string LV failed");
|
||||
let src_len = src_filename.len_full();
|
||||
let dest_filename =
|
||||
Lv::new_from_str("hello-world2.txt").expect("Generating destination LV failed");
|
||||
let dest_len = dest_filename.len_full();
|
||||
let metadata_pdu =
|
||||
MetadataPdu::new(pdu_header, metadata_params, src_filename, dest_filename);
|
||||
let mut buf: [u8; 64] = [0; 64];
|
||||
let res = metadata_pdu.write_to_be_bytes(&mut buf);
|
||||
assert!(res.is_ok());
|
||||
let written = res.unwrap();
|
||||
assert_eq!(
|
||||
written,
|
||||
pdu_header.written_len() + 1 + 1 + 4 + src_len + dest_len
|
||||
);
|
||||
verify_raw_header(&pdu_header, &buf);
|
||||
assert_eq!(buf[7], FileDirectiveType::MetadataPdu as u8);
|
||||
}
|
||||
}
|
||||
|
@ -376,10 +376,73 @@ mod tests {
|
||||
CrcFlag, Direction, LargeFileFlag, PduType, SegmentMetadataFlag, SegmentationControl,
|
||||
TransmissionMode, CFDP_VERSION_2,
|
||||
};
|
||||
use crate::util::{UbfU8, UnsignedByteField, UnsignedByteFieldU16, UnsignedByteFieldU8};
|
||||
use crate::util::{
|
||||
UbfU8, UnsignedByteField, UnsignedByteFieldU16, UnsignedByteFieldU8, UnsignedEnum,
|
||||
};
|
||||
use crate::ByteConversionError;
|
||||
use std::format;
|
||||
|
||||
pub(crate) fn verify_raw_header(pdu_conf: &PduHeader, buf: &[u8]) {
|
||||
assert_eq!((buf[0] >> 5) & 0b111, CFDP_VERSION_2);
|
||||
// File directive
|
||||
assert_eq!((buf[0] >> 4) & 1, pdu_conf.pdu_type as u8);
|
||||
// Towards receiver
|
||||
assert_eq!((buf[0] >> 3) & 1, pdu_conf.pdu_conf.direction as u8);
|
||||
// Acknowledged
|
||||
assert_eq!((buf[0] >> 2) & 1, pdu_conf.pdu_conf.trans_mode as u8);
|
||||
// No CRC
|
||||
assert_eq!((buf[0] >> 1) & 1, pdu_conf.pdu_conf.crc_flag as u8);
|
||||
// Regular file size
|
||||
assert_eq!(buf[0] & 1, pdu_conf.pdu_conf.file_flag as u8);
|
||||
let pdu_datafield_len = u16::from_be_bytes(buf[1..3].try_into().unwrap());
|
||||
assert_eq!(pdu_datafield_len, pdu_conf.pdu_datafield_len);
|
||||
// No record boundary preservation
|
||||
assert_eq!((buf[3] >> 7) & 1, pdu_conf.seg_ctrl as u8);
|
||||
// Entity ID length raw value is actual number of octets - 1 => 0
|
||||
let entity_id_len = pdu_conf.pdu_conf.source_entity_id.len();
|
||||
assert_eq!((buf[3] >> 4) & 0b111, entity_id_len as u8 - 1);
|
||||
// No segment metadata
|
||||
assert_eq!((buf[3] >> 3) & 0b1, pdu_conf.seg_metadata_flag as u8);
|
||||
// Transaction Sequence ID length raw value is actual number of octets - 1 => 0
|
||||
let seq_num_len = pdu_conf.pdu_conf.transaction_seq_num.len();
|
||||
assert_eq!(buf[3] & 0b111, seq_num_len as u8 - 1);
|
||||
let mut current_idx = 4;
|
||||
let mut byte_field_check = |field_len: usize, ubf: &UnsignedByteField| {
|
||||
match field_len {
|
||||
1 => assert_eq!(buf[current_idx], ubf.value() as u8),
|
||||
2 => assert_eq!(
|
||||
u16::from_be_bytes(
|
||||
buf[current_idx..current_idx + field_len]
|
||||
.try_into()
|
||||
.unwrap()
|
||||
),
|
||||
ubf.value() as u16
|
||||
),
|
||||
4 => assert_eq!(
|
||||
u32::from_be_bytes(
|
||||
buf[current_idx..current_idx + field_len]
|
||||
.try_into()
|
||||
.unwrap()
|
||||
),
|
||||
ubf.value() as u32
|
||||
),
|
||||
8 => assert_eq!(
|
||||
u64::from_be_bytes(
|
||||
buf[current_idx..current_idx + field_len]
|
||||
.try_into()
|
||||
.unwrap()
|
||||
),
|
||||
ubf.value() as u64
|
||||
),
|
||||
_ => panic!("invalid entity ID length"),
|
||||
}
|
||||
current_idx += field_len
|
||||
};
|
||||
byte_field_check(entity_id_len, &pdu_conf.pdu_conf.source_entity_id);
|
||||
byte_field_check(seq_num_len, &pdu_conf.pdu_conf.transaction_seq_num);
|
||||
byte_field_check(entity_id_len, &pdu_conf.pdu_conf.dest_entity_id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_basic_state() {
|
||||
let src_id = UnsignedByteFieldU8::new(1);
|
||||
@ -401,6 +464,7 @@ mod tests {
|
||||
SegmentationControl::NoRecordBoundaryPreservation
|
||||
);
|
||||
assert_eq!(pdu_header.pdu_datafield_len, 5);
|
||||
assert_eq!(pdu_header.written_len(), 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -416,30 +480,7 @@ mod tests {
|
||||
assert!(res.is_ok());
|
||||
// 4 byte fixed header plus three bytes src, dest ID and transaction ID
|
||||
assert_eq!(res.unwrap(), 7);
|
||||
assert_eq!((buf[0] >> 5) & 0b111, CFDP_VERSION_2);
|
||||
// File directive
|
||||
assert_eq!((buf[0] >> 4) & 1, 0);
|
||||
// Towards receiver
|
||||
assert_eq!((buf[0] >> 3) & 1, 0);
|
||||
// Acknowledged
|
||||
assert_eq!((buf[0] >> 2) & 1, 0);
|
||||
// No CRC
|
||||
assert_eq!((buf[0] >> 1) & 1, 0);
|
||||
// Regular file size
|
||||
assert_eq!(buf[0] & 1, 0);
|
||||
let pdu_datafield_len = u16::from_be_bytes(buf[1..3].try_into().unwrap());
|
||||
assert_eq!(pdu_datafield_len, 5);
|
||||
// No record boundary preservation
|
||||
assert_eq!((buf[3] >> 7) & 1, 0);
|
||||
// Entity ID length raw value is actual number of octets - 1 => 0
|
||||
assert_eq!((buf[3] >> 4) & 0b111, 0);
|
||||
// No segment metadata
|
||||
assert_eq!((buf[3] >> 3) & 0b1, 0);
|
||||
// Transaction Sequence ID length raw value is actual number of octets - 1 => 0
|
||||
assert_eq!(buf[3] & 0b111, 0);
|
||||
assert_eq!(buf[4], 1);
|
||||
assert_eq!(buf[5], 3);
|
||||
assert_eq!(buf[6], 2);
|
||||
verify_raw_header(&pdu_header, &buf);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -478,35 +519,13 @@ mod tests {
|
||||
SegmentMetadataFlag::Present,
|
||||
SegmentationControl::WithRecordBoundaryPreservation,
|
||||
);
|
||||
assert_eq!(pdu_header.written_len(), 10);
|
||||
let mut buf: [u8; 16] = [0; 16];
|
||||
let res = pdu_header.write_to_be_bytes(&mut buf);
|
||||
assert!(res.is_ok(), "{}", format!("Result {res:?} not okay"));
|
||||
// 4 byte fixed header, 6 bytes additional fields
|
||||
assert_eq!(res.unwrap(), 10);
|
||||
assert_eq!((buf[0] >> 5) & 0b111, CFDP_VERSION_2);
|
||||
// File directive
|
||||
assert_eq!((buf[0] >> 4) & 1, 1);
|
||||
// Towards sender
|
||||
assert_eq!((buf[0] >> 3) & 1, 1);
|
||||
// Unacknowledged
|
||||
assert_eq!((buf[0] >> 2) & 1, 1);
|
||||
// With CRC
|
||||
assert_eq!((buf[0] >> 1) & 1, 1);
|
||||
// Large file size
|
||||
assert_eq!(buf[0] & 1, 1);
|
||||
let pdu_datafield_len = u16::from_be_bytes(buf[1..3].try_into().unwrap());
|
||||
assert_eq!(pdu_datafield_len, 5);
|
||||
// With record boundary preservation
|
||||
assert_eq!((buf[3] >> 7) & 1, 1);
|
||||
// Entity ID length raw value is actual number of octets - 1 => 1
|
||||
assert_eq!((buf[3] >> 4) & 0b111, 1);
|
||||
// With segment metadata
|
||||
assert_eq!((buf[3] >> 3) & 0b1, 1);
|
||||
// Transaction Sequence ID length raw value is actual number of octets - 1 => 1
|
||||
assert_eq!(buf[3] & 0b111, 1);
|
||||
assert_eq!(u16::from_be_bytes(buf[4..6].try_into().unwrap()), 0x0001);
|
||||
assert_eq!(u16::from_be_bytes(buf[6..8].try_into().unwrap()), 0x0405);
|
||||
assert_eq!(u16::from_be_bytes(buf[8..10].try_into().unwrap()), 0x0203);
|
||||
verify_raw_header(&pdu_header, &buf);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -232,6 +232,7 @@ mod tests {
|
||||
panic!("unexpected error {:?}", error);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialization_custom_tlv_type() {
|
||||
let mut buf: [u8; 4] = [0; 4];
|
||||
|
@ -126,6 +126,10 @@ impl UnsignedByteField {
|
||||
Self { width, value }
|
||||
}
|
||||
|
||||
pub fn value(&self) -> u64 {
|
||||
self.value
|
||||
}
|
||||
|
||||
pub fn new_from_be_bytes(width: usize, buf: &[u8]) -> Result<Self, UnsignedByteFieldError> {
|
||||
if width > buf.len() {
|
||||
return Err(ByteConversionError::FromSliceTooSmall(SizeMissmatch {
|
||||
|
Loading…
Reference in New Issue
Block a user