PDU config tweak #17

Merged
muellerr merged 10 commits from pdu-conf-tweaK into main 2023-08-10 21:40:43 +02:00
5 changed files with 78 additions and 26 deletions

View File

@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
classes. The old `PusTm` class is deprecated now. classes. The old `PusTm` class is deprecated now.
- Implement `Display` and `Error` for `StdTimestampError` properly. - Implement `Display` and `Error` for `StdTimestampError` properly.
- Remove some redundant `Error` suffixes for enum error variants. - Remove some redundant `Error` suffixes for enum error variants.
- `CommonPduConfig`: `new_with_defaults` replaced by `new_with_byte_fields`.
## Added ## Added
@ -29,6 +30,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
call `user_data` but are also in line with the PUS packet standard names for those fields. call `user_data` but are also in line with the PUS packet standard names for those fields.
- Added new marker trait `IsPusTelemetry` implemented by `PusTmCreator` and `PusTmReader`. - Added new marker trait `IsPusTelemetry` implemented by `PusTmCreator` and `PusTmReader`.
- Added new marker trait `IsPusTelecommand` implemented by `PusTcCreator` and `PusTcReader`. - Added new marker trait `IsPusTelecommand` implemented by `PusTcCreator` and `PusTcReader`.
- `metadata_param` getter method for the `MetadataPdu` object.
- `Default` impl for CFDP `ChecksumType`
- `Default` impl for CFDP `CommonPduConfig`
## Fixed
- All `MetadataGenericParam` fields are now public.
- New setter method `set_source_and_dest_id` for `CommonPduConfig`. - New setter method `set_source_and_dest_id` for `CommonPduConfig`.
# [v0.6.0] 2023-07-06 # [v0.6.0] 2023-07-06

View File

@ -134,6 +134,12 @@ pub enum ChecksumType {
NullChecksum = 15, NullChecksum = 15,
} }
impl Default for ChecksumType {
fn default() -> Self {
Self::NullChecksum
}
}
pub const NULL_CHECKSUM_U32: [u8; 4] = [0; 4]; pub const NULL_CHECKSUM_U32: [u8; 4] = [0; 4];
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]

View File

@ -235,7 +235,7 @@ mod tests {
let dest_id = UbfU8::new(2); let dest_id = UbfU8::new(2);
let transaction_seq_num = UbfU8::new(3); let transaction_seq_num = UbfU8::new(3);
let common_conf = let common_conf =
CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_seq_num).unwrap(); CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_seq_num).unwrap();
let pdu_header = PduHeader::new_for_file_data_default(common_conf, 0); let pdu_header = PduHeader::new_for_file_data_default(common_conf, 0);
let file_data: [u8; 4] = [1, 2, 3, 4]; let file_data: [u8; 4] = [1, 2, 3, 4];
let fd_pdu = FileDataPdu::new_no_seg_metadata(pdu_header, 10, &file_data); let fd_pdu = FileDataPdu::new_no_seg_metadata(pdu_header, 10, &file_data);
@ -254,7 +254,7 @@ mod tests {
let dest_id = UbfU8::new(2); let dest_id = UbfU8::new(2);
let transaction_seq_num = UbfU8::new(3); let transaction_seq_num = UbfU8::new(3);
let common_conf = let common_conf =
CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_seq_num).unwrap(); CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_seq_num).unwrap();
let pdu_header = PduHeader::new_for_file_data_default(common_conf, 0); let pdu_header = PduHeader::new_for_file_data_default(common_conf, 0);
let file_data: [u8; 4] = [1, 2, 3, 4]; let file_data: [u8; 4] = [1, 2, 3, 4];
let fd_pdu = FileDataPdu::new_no_seg_metadata(pdu_header, 10, &file_data); let fd_pdu = FileDataPdu::new_no_seg_metadata(pdu_header, 10, &file_data);
@ -289,7 +289,7 @@ mod tests {
let dest_id = UbfU8::new(2); let dest_id = UbfU8::new(2);
let transaction_seq_num = UbfU8::new(3); let transaction_seq_num = UbfU8::new(3);
let common_conf = let common_conf =
CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_seq_num).unwrap(); CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_seq_num).unwrap();
let pdu_header = PduHeader::new_for_file_data_default(common_conf, 0); let pdu_header = PduHeader::new_for_file_data_default(common_conf, 0);
let file_data: [u8; 4] = [1, 2, 3, 4]; let file_data: [u8; 4] = [1, 2, 3, 4];
let fd_pdu = FileDataPdu::new_no_seg_metadata(pdu_header, 10, &file_data); let fd_pdu = FileDataPdu::new_no_seg_metadata(pdu_header, 10, &file_data);
@ -307,7 +307,7 @@ mod tests {
let dest_id = UbfU8::new(2); let dest_id = UbfU8::new(2);
let transaction_seq_num = UbfU8::new(3); let transaction_seq_num = UbfU8::new(3);
let common_conf = let common_conf =
CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_seq_num).unwrap(); CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_seq_num).unwrap();
let pdu_header = PduHeader::new_for_file_data( let pdu_header = PduHeader::new_for_file_data(
common_conf, common_conf,
0, 0,
@ -372,7 +372,7 @@ mod tests {
let dest_id = UbfU8::new(2); let dest_id = UbfU8::new(2);
let transaction_seq_num = UbfU8::new(3); let transaction_seq_num = UbfU8::new(3);
let common_conf = let common_conf =
CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_seq_num).unwrap(); CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_seq_num).unwrap();
let pdu_header = PduHeader::new_for_file_data( let pdu_header = PduHeader::new_for_file_data(
common_conf, common_conf,
0, 0,

View File

@ -11,12 +11,12 @@ use alloc::vec::Vec;
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MetadataGenericParams { pub struct MetadataGenericParams {
closure_requested: bool, pub closure_requested: bool,
checksum_type: ChecksumType, pub checksum_type: ChecksumType,
file_size: u64, pub file_size: u64,
} }
impl MetadataGenericParams { impl MetadataGenericParams {
@ -145,6 +145,10 @@ impl<'src_name, 'dest_name, 'opts> MetadataPdu<'src_name, 'dest_name, 'opts> {
pdu pdu
} }
pub fn metadata_params(&self) -> &MetadataGenericParams {
&self.metadata_params
}
pub fn src_file_name(&self) -> Lv<'src_name> { pub fn src_file_name(&self) -> Lv<'src_name> {
self.src_file_name self.src_file_name
} }

View File

@ -1,6 +1,6 @@
//! CFDP Packet Data Unit (PDU) support. //! CFDP Packet Data Unit (PDU) support.
use crate::cfdp::*; use crate::cfdp::*;
use crate::util::{UnsignedByteField, UnsignedEnum}; use crate::util::{UnsignedByteField, UnsignedByteFieldU8, UnsignedEnum};
use crate::CRC_CCITT_FALSE; use crate::CRC_CCITT_FALSE;
use crate::{ByteConversionError, SizeMissmatch}; use crate::{ByteConversionError, SizeMissmatch};
use core::fmt::{Display, Formatter}; use core::fmt::{Display, Formatter};
@ -190,7 +190,7 @@ impl CommonPduConfig {
}) })
} }
pub fn new_with_defaults( pub fn new_with_byte_fields(
source_id: impl Into<UnsignedByteField>, source_id: impl Into<UnsignedByteField>,
dest_id: impl Into<UnsignedByteField>, dest_id: impl Into<UnsignedByteField>,
transaction_seq_num: impl Into<UnsignedByteField>, transaction_seq_num: impl Into<UnsignedByteField>,
@ -248,6 +248,24 @@ impl CommonPduConfig {
} }
} }
impl Default for CommonPduConfig {
/// The defaults for the source ID, destination ID and the transaction sequence number is the
/// [UnsignedByteFieldU8] with an intitial value of 0
fn default() -> Self {
// The new function can not fail for these input parameters.
Self::new(
UnsignedByteFieldU8::new(0),
UnsignedByteFieldU8::new(0),
UnsignedByteFieldU8::new(0),
TransmissionMode::Acknowledged,
LargeFileFlag::Normal,
CrcFlag::NoCrc,
Direction::TowardsReceiver,
)
.unwrap()
}
}
pub const FIXED_HEADER_LEN: usize = 4; pub const FIXED_HEADER_LEN: usize = 4;
/// Abstraction for the PDU header common to all CFDP PDUs. /// Abstraction for the PDU header common to all CFDP PDUs.
@ -593,7 +611,8 @@ mod tests {
let src_id = UbfU8::new(5); let src_id = UbfU8::new(5);
let dest_id = UbfU8::new(10); let dest_id = UbfU8::new(10);
let transaction_seq_num = UbfU8::new(20); let transaction_seq_num = UbfU8::new(20);
let mut pdu_conf = CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_seq_num) let mut pdu_conf =
CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_seq_num)
.expect("Generating common PDU config"); .expect("Generating common PDU config");
pdu_conf.crc_flag = crc_flag; pdu_conf.crc_flag = crc_flag;
pdu_conf.file_flag = fss; pdu_conf.file_flag = fss;
@ -666,7 +685,7 @@ mod tests {
let src_id = UnsignedByteFieldU8::new(1); let src_id = UnsignedByteFieldU8::new(1);
let dest_id = UnsignedByteFieldU8::new(2); let dest_id = UnsignedByteFieldU8::new(2);
let transaction_id = UnsignedByteFieldU8::new(3); let transaction_id = UnsignedByteFieldU8::new(3);
let common_pdu_cfg = CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id) let common_pdu_cfg = CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_id)
.expect("common config creation failed"); .expect("common config creation failed");
let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5); let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5);
assert_eq!(pdu_header.pdu_type(), PduType::FileDirective); assert_eq!(pdu_header.pdu_type(), PduType::FileDirective);
@ -686,12 +705,25 @@ mod tests {
} }
#[test] #[test]
fn test_basic_state_default() {
let default_conf = CommonPduConfig::default();
assert_eq!(default_conf.source_id(), UnsignedByteFieldU8::new(0).into());
assert_eq!(default_conf.dest_id(), UnsignedByteFieldU8::new(0).into());
assert_eq!(
default_conf.transaction_seq_num,
UnsignedByteFieldU8::new(0).into()
);
assert_eq!(default_conf.trans_mode, TransmissionMode::Acknowledged);
assert_eq!(default_conf.direction, Direction::TowardsReceiver);
assert_eq!(default_conf.crc_flag, CrcFlag::NoCrc);
assert_eq!(default_conf.file_flag, LargeFileFlag::Normal);
}
fn test_pdu_header_setter() { fn test_pdu_header_setter() {
let src_id = UnsignedByteFieldU8::new(1); let src_id = UnsignedByteFieldU8::new(1);
let dest_id = UnsignedByteFieldU8::new(2); let dest_id = UnsignedByteFieldU8::new(2);
let transaction_id = UnsignedByteFieldU8::new(3); let transaction_id = UnsignedByteFieldU8::new(3);
let mut common_pdu_cfg = let mut common_pdu_cfg =
CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id) CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_id)
.expect("common config creation failed"); .expect("common config creation failed");
let other_src_id = UnsignedByteFieldU16::new(5); let other_src_id = UnsignedByteFieldU16::new(5);
let other_dest_id = UnsignedByteFieldU16::new(6); let other_dest_id = UnsignedByteFieldU16::new(6);
@ -706,7 +738,7 @@ mod tests {
let src_id = UnsignedByteFieldU8::new(1); let src_id = UnsignedByteFieldU8::new(1);
let dest_id = UnsignedByteFieldU8::new(2); let dest_id = UnsignedByteFieldU8::new(2);
let transaction_id = UnsignedByteFieldU8::new(3); let transaction_id = UnsignedByteFieldU8::new(3);
let common_pdu_cfg = CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id) let common_pdu_cfg = CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_id)
.expect("common config creation failed"); .expect("common config creation failed");
let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5); let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5);
let mut buf: [u8; 7] = [0; 7]; let mut buf: [u8; 7] = [0; 7];
@ -722,7 +754,7 @@ mod tests {
let src_id = UnsignedByteFieldU8::new(1); let src_id = UnsignedByteFieldU8::new(1);
let dest_id = UnsignedByteFieldU8::new(2); let dest_id = UnsignedByteFieldU8::new(2);
let transaction_id = UnsignedByteFieldU8::new(3); let transaction_id = UnsignedByteFieldU8::new(3);
let common_pdu_cfg = CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id) let common_pdu_cfg = CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_id)
.expect("common config creation failed"); .expect("common config creation failed");
let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5); let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5);
let mut buf: [u8; 7] = [0; 7]; let mut buf: [u8; 7] = [0; 7];
@ -741,7 +773,7 @@ mod tests {
let dest_id = UnsignedByteFieldU16::new(0x0203); let dest_id = UnsignedByteFieldU16::new(0x0203);
let transaction_id = UnsignedByteFieldU16::new(0x0405); let transaction_id = UnsignedByteFieldU16::new(0x0405);
let mut common_pdu_cfg = let mut common_pdu_cfg =
CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id) CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_id)
.expect("common config creation failed"); .expect("common config creation failed");
common_pdu_cfg.crc_flag = CrcFlag::WithCrc; common_pdu_cfg.crc_flag = CrcFlag::WithCrc;
common_pdu_cfg.direction = Direction::TowardsSender; common_pdu_cfg.direction = Direction::TowardsSender;
@ -768,7 +800,7 @@ mod tests {
let dest_id = UnsignedByteFieldU16::new(0x0203); let dest_id = UnsignedByteFieldU16::new(0x0203);
let transaction_id = UnsignedByteFieldU16::new(0x0405); let transaction_id = UnsignedByteFieldU16::new(0x0405);
let mut common_pdu_cfg = let mut common_pdu_cfg =
CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id) CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_id)
.expect("common config creation failed"); .expect("common config creation failed");
common_pdu_cfg.crc_flag = CrcFlag::WithCrc; common_pdu_cfg.crc_flag = CrcFlag::WithCrc;
common_pdu_cfg.direction = Direction::TowardsSender; common_pdu_cfg.direction = Direction::TowardsSender;
@ -795,7 +827,7 @@ mod tests {
let src_id = UnsignedByteFieldU8::new(1); let src_id = UnsignedByteFieldU8::new(1);
let dest_id = UnsignedByteFieldU8::new(2); let dest_id = UnsignedByteFieldU8::new(2);
let transaction_id = UnsignedByteFieldU8::new(3); let transaction_id = UnsignedByteFieldU8::new(3);
let common_pdu_cfg = CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id) let common_pdu_cfg = CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_id)
.expect("common config creation failed"); .expect("common config creation failed");
let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5); let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5);
let mut buf: [u8; 7] = [0; 7]; let mut buf: [u8; 7] = [0; 7];
@ -834,7 +866,7 @@ mod tests {
let src_id = UnsignedByteFieldU8::new(1); let src_id = UnsignedByteFieldU8::new(1);
let dest_id = UnsignedByteFieldU8::new(2); let dest_id = UnsignedByteFieldU8::new(2);
let transaction_id = UnsignedByteFieldU8::new(3); let transaction_id = UnsignedByteFieldU8::new(3);
let common_pdu_cfg = CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id) let common_pdu_cfg = CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_id)
.expect("common config creation failed"); .expect("common config creation failed");
let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5); let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5);
let mut buf: [u8; 7] = [0; 7]; let mut buf: [u8; 7] = [0; 7];
@ -857,7 +889,8 @@ mod tests {
let dest_id = UbfU8::new(2); let dest_id = UbfU8::new(2);
let transaction_seq_id = UbfU8::new(3); let transaction_seq_id = UbfU8::new(3);
let invalid_byte_field = UnsignedByteField::new(3, 5); let invalid_byte_field = UnsignedByteField::new(3, 5);
let pdu_conf_res = CommonPduConfig::new_with_defaults(src_id, dest_id, invalid_byte_field); let pdu_conf_res =
CommonPduConfig::new_with_byte_fields(src_id, dest_id, invalid_byte_field);
assert!(pdu_conf_res.is_err()); assert!(pdu_conf_res.is_err());
let error = pdu_conf_res.unwrap_err(); let error = pdu_conf_res.unwrap_err();
if let PduError::InvalidTransactionSeqNumLen(len) = error { if let PduError::InvalidTransactionSeqNumLen(len) = error {
@ -865,7 +898,7 @@ mod tests {
} else { } else {
panic!("Invalid exception: {}", error) panic!("Invalid exception: {}", error)
} }
let pdu_conf_res = CommonPduConfig::new_with_defaults( let pdu_conf_res = CommonPduConfig::new_with_byte_fields(
invalid_byte_field, invalid_byte_field,
invalid_byte_field, invalid_byte_field,
transaction_seq_id, transaction_seq_id,
@ -883,7 +916,8 @@ mod tests {
let src_id = UnsignedByteField::new(1, 5); let src_id = UnsignedByteField::new(1, 5);
let dest_id = UnsignedByteField::new(2, 5); let dest_id = UnsignedByteField::new(2, 5);
let transaction_seq_id = UbfU8::new(3); let transaction_seq_id = UbfU8::new(3);
let pdu_conf_res = CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_seq_id); let pdu_conf_res =
CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_seq_id);
assert!(pdu_conf_res.is_err()); assert!(pdu_conf_res.is_err());
let error = pdu_conf_res.unwrap_err(); let error = pdu_conf_res.unwrap_err();
if let PduError::SourceDestIdLenMissmatch((src_len, dest_len)) = error { if let PduError::SourceDestIdLenMissmatch((src_len, dest_len)) = error {
@ -897,7 +931,7 @@ mod tests {
let src_id = UnsignedByteFieldU8::new(1); let src_id = UnsignedByteFieldU8::new(1);
let dest_id = UnsignedByteFieldU8::new(2); let dest_id = UnsignedByteFieldU8::new(2);
let transaction_id = UnsignedByteFieldU8::new(3); let transaction_id = UnsignedByteFieldU8::new(3);
let common_pdu_cfg = CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id) let common_pdu_cfg = CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_id)
.expect("common config creation failed"); .expect("common config creation failed");
let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5); let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5);
let mut buf: [u8; 7] = [0; 7]; let mut buf: [u8; 7] = [0; 7];
@ -921,7 +955,7 @@ mod tests {
let src_id = UnsignedByteFieldU8::new(1); let src_id = UnsignedByteFieldU8::new(1);
let dest_id = UnsignedByteFieldU8::new(2); let dest_id = UnsignedByteFieldU8::new(2);
let transaction_id = UnsignedByteFieldU8::new(3); let transaction_id = UnsignedByteFieldU8::new(3);
let common_pdu_cfg = CommonPduConfig::new_with_defaults(src_id, dest_id, transaction_id) let common_pdu_cfg = CommonPduConfig::new_with_byte_fields(src_id, dest_id, transaction_id)
.expect("common config creation failed"); .expect("common config creation failed");
let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5); let pdu_header = PduHeader::new_no_file_data(common_pdu_cfg, 5);
let mut buf: [u8; 7] = [0; 7]; let mut buf: [u8; 7] = [0; 7];