9 Commits

Author SHA1 Message Date
638e4cda62 bump version
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2022-09-13 10:34:31 +02:00
1cc4771a53 cross-ref docs for examples
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2022-09-13 10:31:47 +02:00
427b368057 cargo fmt
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2022-09-13 10:29:09 +02:00
795abc57fa better names
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2022-09-13 10:28:20 +02:00
7da7e5329c typos
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2022-09-13 10:21:52 +02:00
5631372e58 update changelog
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2022-09-13 10:18:21 +02:00
28ba4f887d added some auto-conversion
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2022-09-13 09:52:59 +02:00
d559646d80 better naming/docs. new const for MAX_SEQ_COUNT 2022-09-13 09:41:21 +02:00
fe1a30327b work on uniform API
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2022-09-11 20:50:46 +02:00
8 changed files with 161 additions and 114 deletions

View File

@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
# [unreleased]
# [v0.2.0] 13.09.2022
## Added
- Basic support for ECSS enumeration types for u8, u16, u32 and u64
@ -15,6 +17,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## Changed
- Better names for generic error enumerations: `PacketError` renamed to `ByteConversionError`
- CCSDS module: `ssc` abbreviations fully replaced by better name `seq_count`
- Time module: `CcsdsTimeProvider::date_time` now has `Option<DateTime<Utc>>` as
a returnvalue instead of `DateTime<Utc>`
- `PusTc` and `PusTm`: `new_from_raw_slice` renamed to simpler `from_bytes`
# [v0.1.0] 16.08.2022

View File

@ -1,6 +1,6 @@
[package]
name = "spacepackets"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
authors = ["Robin Mueller <muellerr@irs.uni-stuttgart.de>"]
description = "Generic implementations for various CCSDS and ECSS packet standards"

View File

@ -20,11 +20,9 @@ Currently, this includes the following components:
# Features
`spacepackets` supports various runtime environments and is also suitable
for suitable for `no_std` environments. It has several features which may be enabled
for disabled.
`spacepackets` supports various runtime environments and is also suitable for `no_std` environments.
It also offers support for [`serde`](https://serde.rs/). The Space Paccket, PUS TM and TC
It offers support for [`serde`](https://serde.rs/). The Space Packet, PUS TM and TC
implementations derive the `serde` `Serialize` and `Deserialize` trait. This allows serializing and
deserializing them with an appropriate `serde` provider like
[`postcard`](https://github.com/jamesmunns/postcard).
@ -35,3 +33,8 @@ Default features:
- [`alloc`](https://doc.rust-lang.org/alloc/): Enables features which operate on containers
like [`alloc::vec::Vec`](https://doc.rust-lang.org/beta/alloc/vec/struct.Vec.html).
Enabled by the `std` feature.
# Examples
You can check the [documentation](https://docs.rs/spacepackets) of individual modules for various
usage examples.

View File

@ -33,6 +33,22 @@ impl TryFrom<u8> for PusVersion {
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum PacketTypeCodes {
Boolean = 1,
Enumerated = 2,
UnsignedInt = 3,
SignedInt = 4,
Real = 5,
BitString = 6,
OctetString = 7,
CharString = 8,
AbsoluteTime = 9,
RelativeTime = 10,
Deduced = 11,
Packet = 12,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum PusError {
VersionNotSupported(PusVersion),
@ -41,7 +57,13 @@ pub enum PusError {
NoRawData,
/// CRC16 needs to be calculated first
CrcCalculationMissing,
PacketError(ByteConversionError),
ByteConversionError(ByteConversionError),
}
impl From<ByteConversionError> for PusError {
fn from(e: ByteConversionError) -> Self {
PusError::ByteConversionError(e)
}
}
pub trait PusPacket: CcsdsPacket {
@ -136,12 +158,17 @@ macro_rules! sp_header_impls {
pub(crate) use ccsds_impl;
pub(crate) use sp_header_impls;
/// Generic trait for ECSS enumeration which consist of a PFC field denoting their length
/// and an unsigned value. The trait makes no assumptions about the actual type of the unsigned
/// value and only requires implementors to implement a function which writes the enumeration into
/// a raw byte format.
pub trait EcssEnumeration {
/// Packet Format Code, which denotes the number of bits of the enumeration
fn pfc(&self) -> u8;
fn byte_width(&self) -> usize {
(self.pfc() / 8) as usize
}
fn to_bytes(&self, buf: &mut [u8]) -> Result<(), ByteConversionError>;
fn write_to_bytes(&self, buf: &mut [u8]) -> Result<(), ByteConversionError>;
}
trait ToBeBytes {
@ -186,6 +213,10 @@ pub struct GenericEcssEnumWrapper<TYPE> {
}
impl<TYPE> GenericEcssEnumWrapper<TYPE> {
pub const fn ptc() -> PacketTypeCodes {
PacketTypeCodes::Enumerated
}
pub fn new(val: TYPE) -> Self {
Self { val }
}
@ -196,7 +227,7 @@ impl<TYPE: ToBeBytes> EcssEnumeration for GenericEcssEnumWrapper<TYPE> {
size_of::<TYPE>() as u8 * 8_u8
}
fn to_bytes(&self, buf: &mut [u8]) -> Result<(), ByteConversionError> {
fn write_to_bytes(&self, buf: &mut [u8]) -> Result<(), ByteConversionError> {
if buf.len() < self.byte_width() as usize {
return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
found: buf.len(),
@ -223,7 +254,7 @@ mod tests {
let mut buf = [0, 0, 0];
let my_enum = EcssEnumU8::new(1);
my_enum
.to_bytes(&mut buf[1..2])
.write_to_bytes(&mut buf[1..2])
.expect("To byte conversion of u8 failed");
assert_eq!(buf[1], 1);
}
@ -233,7 +264,7 @@ mod tests {
let mut buf = [0, 0, 0];
let my_enum = EcssEnumU16::new(0x1f2f);
my_enum
.to_bytes(&mut buf[1..3])
.write_to_bytes(&mut buf[1..3])
.expect("To byte conversion of u8 failed");
assert_eq!(buf[1], 0x1f);
assert_eq!(buf[2], 0x2f);
@ -243,7 +274,7 @@ mod tests {
fn test_slice_u16_too_small() {
let mut buf = [0];
let my_enum = EcssEnumU16::new(0x1f2f);
let res = my_enum.to_bytes(&mut buf[0..1]);
let res = my_enum.write_to_bytes(&mut buf[0..1]);
assert!(res.is_err());
let error = res.unwrap_err();
match error {
@ -262,7 +293,7 @@ mod tests {
let mut buf = [0, 0, 0, 0, 0];
let my_enum = EcssEnumU32::new(0x1f2f3f4f);
my_enum
.to_bytes(&mut buf[1..5])
.write_to_bytes(&mut buf[1..5])
.expect("To byte conversion of u8 failed");
assert_eq!(buf[1], 0x1f);
assert_eq!(buf[2], 0x2f);
@ -274,7 +305,7 @@ mod tests {
fn test_slice_u32_too_small() {
let mut buf = [0, 0, 0, 0, 0];
let my_enum = EcssEnumU32::new(0x1f2f3f4f);
let res = my_enum.to_bytes(&mut buf[0..3]);
let res = my_enum.write_to_bytes(&mut buf[0..3]);
assert!(res.is_err());
let error = res.unwrap_err();
match error {

View File

@ -14,11 +14,9 @@
//!
//! ## Features
//!
//! `spacepackets` supports various runtime environments and is also suitable
//! for suitable for `no_std` environments. It has several features which may be enabled
//! for disabled.
//! `spacepackets` supports various runtime environments and is also suitable for `no_std` environments.
//!
//! It also offers support for [`serde`](https://serde.rs/). The Space Paccket, PUS TM and TC
//! It also offers support for [`serde`](https://serde.rs/). The Space Packet, PUS TM and TC
//! implementations derive the `serde` `Serialize` and `Deserialize` trait. This allows serializing and
//! deserializing them with an appropriate `serde` provider like
//! [`postcard`](https://github.com/jamesmunns/postcard).
@ -59,6 +57,9 @@ pub mod tc;
pub mod time;
pub mod tm;
pub const MAX_APID: u16 = 2u16.pow(11) - 1;
pub const MAX_SEQ_COUNT: u16 = 2u16.pow(14) - 1;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct SizeMissmatch {
pub found: usize,
@ -142,7 +143,7 @@ impl PacketId {
/// not be set and false will be returned. The maximum allowed value for the 11-bit field is
/// 2047
pub fn set_apid(&mut self, apid: u16) -> bool {
if apid > 2u16.pow(11) - 1 {
if apid > MAX_APID {
return false;
}
self.apid = apid;
@ -175,12 +176,13 @@ pub struct PacketSequenceCtrl {
}
impl PacketSequenceCtrl {
pub fn new(seq_flags: SequenceFlags, ssc: u16) -> Option<PacketSequenceCtrl> {
/// Returns [None] if the passed sequence count exceeds [MAX_SEQ_COUNT]
pub fn new(seq_flags: SequenceFlags, seq_count: u16) -> Option<PacketSequenceCtrl> {
let mut psc = PacketSequenceCtrl {
seq_flags,
seq_count: 0,
};
psc.set_seq_count(ssc).then(|| psc)
psc.set_seq_count(seq_count).then(|| psc)
}
pub fn raw(&self) -> u16 {
((self.seq_flags as u16) << 14) | self.seq_count
@ -189,14 +191,14 @@ impl PacketSequenceCtrl {
/// Set a new sequence count. If the passed number is invalid, the sequence count will not be
/// set and false will be returned. The maximum allowed value for the 14-bit field is 16383
pub fn set_seq_count(&mut self, ssc: u16) -> bool {
if ssc > 2u16.pow(14) - 1 {
if ssc > MAX_SEQ_COUNT {
return false;
}
self.seq_count = ssc;
true
}
pub fn ssc(&self) -> u16 {
pub fn seq_count(&self) -> u16 {
self.seq_count
}
}
@ -321,6 +323,7 @@ pub struct SpHeader {
pub psc: PacketSequenceCtrl,
pub data_len: u16,
}
impl Default for SpHeader {
fn default() -> Self {
SpHeader {
@ -339,29 +342,36 @@ impl Default for SpHeader {
}
}
impl SpHeader {
/// Create a new Space Packet Header instance which can be used to create generic
/// Space Packets. This will return [None] if the APID or sequence count argument
/// exceed [MAX_APID] or [MAX_SEQ_COUNT] respectively.
pub fn new(
ptype: PacketType,
sec_header: bool,
apid: u16,
ssc: u16,
seq_count: u16,
data_len: u16,
) -> Option<Self> {
if ssc > 2u16.pow(14) - 1 || apid > 2u16.pow(11) - 1 {
if seq_count > MAX_SEQ_COUNT || apid > MAX_APID {
return None;
}
let mut header = SpHeader::default();
header.packet_id.sec_header_flag = sec_header;
header.packet_id.apid = apid;
header.packet_id.ptype = ptype;
header.psc.seq_count = ssc;
header.psc.seq_count = seq_count;
header.data_len = data_len;
Some(header)
}
/// Helper function for telemetry space packet headers. The packet type field will be
/// set accordingly.
pub fn tm(apid: u16, seq_count: u16, data_len: u16) -> Option<Self> {
Self::new(PacketType::Tm, false, apid, seq_count, data_len)
}
/// Helper function for telecommand space packet headers. The packet type field will be
/// set accordingly.
pub fn tc(apid: u16, seq_count: u16, data_len: u16) -> Option<Self> {
Self::new(PacketType::Tc, false, apid, seq_count, data_len)
}

View File

@ -20,13 +20,13 @@
//! // Serialize TC into a raw buffer
//! let mut test_buf: [u8; 32] = [0; 32];
//! let size = pus_tc
//! .write_to(test_buf.as_mut_slice())
//! .write_to_bytes(test_buf.as_mut_slice())
//! .expect("Error writing TC to buffer");
//! assert_eq!(size, 13);
//! println!("{:?}", &test_buf[0..size]);
//!
//! // Deserialize from the raw byte representation
//! let pus_tc_deserialized = PusTc::new_from_raw_slice(&test_buf).expect("Deserialization failed");
//! let pus_tc_deserialized = PusTc::from_bytes(&test_buf).expect("Deserialization failed");
//! assert_eq!(pus_tc.service(), 17);
//! assert_eq!(pus_tc.subservice(), 1);
//! assert_eq!(pus_tc.apid(), 0x02);
@ -126,7 +126,7 @@ pub mod zc {
}
impl PusTcSecondaryHeader {
pub fn to_bytes(&self, slice: &mut [u8]) -> Option<()> {
pub fn write_to_bytes(&self, slice: &mut [u8]) -> Option<()> {
self.write_to(slice)
}
@ -330,28 +330,27 @@ impl<'slice> PusTc<'slice> {
}
/// Write the raw PUS byte representation to a provided buffer.
pub fn write_to(&self, slice: &mut [u8]) -> Result<usize, PusError> {
pub fn write_to_bytes(&self, slice: &mut [u8]) -> Result<usize, PusError> {
let mut curr_idx = 0;
let sph_zc = crate::zc::SpHeader::from(self.sp_header);
let tc_header_len = size_of::<zc::PusTcSecondaryHeader>();
let total_size = self.len_packed();
if total_size > slice.len() {
return Err(PusError::PacketError(ByteConversionError::ToSliceTooSmall(
SizeMissmatch {
found: slice.len(),
expected: total_size,
},
)));
return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
found: slice.len(),
expected: total_size,
})
.into());
}
sph_zc
.to_bytes(&mut slice[curr_idx..curr_idx + CCSDS_HEADER_LEN])
.ok_or(PusError::PacketError(ByteConversionError::ZeroCopyToError))?;
.ok_or(ByteConversionError::ZeroCopyToError)?;
curr_idx += CCSDS_HEADER_LEN;
let sec_header = zc::PusTcSecondaryHeader::try_from(self.sec_header).unwrap();
sec_header
.to_bytes(&mut slice[curr_idx..curr_idx + tc_header_len])
.ok_or(PusError::PacketError(ByteConversionError::ZeroCopyToError))?;
.write_to_bytes(&mut slice[curr_idx..curr_idx + tc_header_len])
.ok_or(ByteConversionError::ZeroCopyToError)?;
curr_idx += tc_header_len;
if let Some(app_data) = self.app_data {
@ -402,7 +401,7 @@ impl<'slice> PusTc<'slice> {
/// Create a [PusTc] instance from a raw slice. On success, it returns a tuple containing
/// the instance and the found byte length of the packet
pub fn new_from_raw_slice(slice: &'slice [u8]) -> Result<(Self, usize), PusError> {
pub fn from_bytes(slice: &'slice [u8]) -> Result<(Self, usize), PusError> {
let raw_data_len = slice.len();
if raw_data_len < PUS_TC_MIN_LEN_WITHOUT_APP_DATA {
return Err(PusError::RawDataTooShort(raw_data_len));
@ -410,9 +409,7 @@ impl<'slice> PusTc<'slice> {
let mut current_idx = 0;
let sph =
crate::zc::SpHeader::from_bytes(&slice[current_idx..current_idx + CCSDS_HEADER_LEN])
.ok_or(PusError::PacketError(
ByteConversionError::ZeroCopyFromError,
))?;
.ok_or(ByteConversionError::ZeroCopyFromError)?;
current_idx += CCSDS_HEADER_LEN;
let total_len = sph.total_len();
if raw_data_len < total_len || total_len < PUS_TC_MIN_LEN_WITHOUT_APP_DATA {
@ -421,9 +418,7 @@ impl<'slice> PusTc<'slice> {
let sec_header = zc::PusTcSecondaryHeader::from_bytes(
&slice[current_idx..current_idx + PUC_TC_SECONDARY_HEADER_LEN],
)
.ok_or(PusError::PacketError(
ByteConversionError::ZeroCopyFromError,
))?;
.ok_or(ByteConversionError::ZeroCopyFromError)?;
current_idx += PUC_TC_SECONDARY_HEADER_LEN;
let raw_data = &slice[0..total_len];
let pus_tc = PusTc {
@ -514,7 +509,7 @@ mod tests {
let pus_tc = base_ping_tc_simple_ctor();
let mut test_buf: [u8; 32] = [0; 32];
let size = pus_tc
.write_to(test_buf.as_mut_slice())
.write_to_bytes(test_buf.as_mut_slice())
.expect("Error writing TC to buffer");
assert_eq!(size, 13);
}
@ -524,11 +519,11 @@ mod tests {
let pus_tc = base_ping_tc_simple_ctor();
let mut test_buf: [u8; 32] = [0; 32];
let size = pus_tc
.write_to(test_buf.as_mut_slice())
.write_to_bytes(test_buf.as_mut_slice())
.expect("Error writing TC to buffer");
assert_eq!(size, 13);
let (tc_from_raw, size) = PusTc::new_from_raw_slice(&test_buf)
.expect("Creating PUS TC struct from raw buffer failed");
let (tc_from_raw, size) =
PusTc::from_bytes(&test_buf).expect("Creating PUS TC struct from raw buffer failed");
assert_eq!(size, 13);
verify_test_tc(&tc_from_raw, false, 13);
assert!(tc_from_raw.user_data().is_none());
@ -550,11 +545,11 @@ mod tests {
let pus_tc = base_ping_tc_simple_ctor_with_app_data(&[1, 2, 3]);
let mut test_buf: [u8; 32] = [0; 32];
let size = pus_tc
.write_to(test_buf.as_mut_slice())
.write_to_bytes(test_buf.as_mut_slice())
.expect("Error writing TC to buffer");
assert_eq!(size, 16);
let (tc_from_raw, size) = PusTc::new_from_raw_slice(&test_buf)
.expect("Creating PUS TC struct from raw buffer failed");
let (tc_from_raw, size) =
PusTc::from_bytes(&test_buf).expect("Creating PUS TC struct from raw buffer failed");
assert_eq!(size, 16);
verify_test_tc(&tc_from_raw, true, 16);
let user_data = tc_from_raw.user_data().unwrap();
@ -581,10 +576,10 @@ mod tests {
let pus_tc = base_ping_tc_simple_ctor();
let mut test_buf: [u8; 32] = [0; 32];
pus_tc
.write_to(test_buf.as_mut_slice())
.write_to_bytes(test_buf.as_mut_slice())
.expect("Error writing TC to buffer");
test_buf[12] = 0;
let res = PusTc::new_from_raw_slice(&test_buf);
let res = PusTc::from_bytes(&test_buf);
assert!(res.is_err());
let err = res.unwrap_err();
assert!(matches!(err, PusError::IncorrectCrc { .. }));
@ -597,7 +592,7 @@ mod tests {
let mut test_buf: [u8; 32] = [0; 32];
pus_tc.calc_own_crc16();
pus_tc
.write_to(test_buf.as_mut_slice())
.write_to_bytes(test_buf.as_mut_slice())
.expect("Error writing TC to buffer");
verify_test_tc_raw(&test_buf);
verify_crc_no_app_data(&test_buf);
@ -608,7 +603,7 @@ mod tests {
let mut pus_tc = base_ping_tc_simple_ctor();
pus_tc.calc_crc_on_serialization = false;
let mut test_buf: [u8; 32] = [0; 32];
let res = pus_tc.write_to(test_buf.as_mut_slice());
let res = pus_tc.write_to_bytes(test_buf.as_mut_slice());
assert!(res.is_err());
let err = res.unwrap_err();
assert!(matches!(err, PusError::CrcCalculationMissing { .. }));
@ -632,11 +627,11 @@ mod tests {
fn test_write_buf_too_msall() {
let pus_tc = base_ping_tc_simple_ctor();
let mut test_buf = [0; 12];
let res = pus_tc.write_to(test_buf.as_mut_slice());
let res = pus_tc.write_to_bytes(test_buf.as_mut_slice());
assert!(res.is_err());
let err = res.unwrap_err();
match err {
PusError::PacketError(err) => match err {
PusError::ByteConversionError(err) => match err {
ByteConversionError::ToSliceTooSmall(missmatch) => {
assert_eq!(missmatch.expected, pus_tc.len_packed());
assert_eq!(missmatch.found, 12);
@ -653,7 +648,7 @@ mod tests {
verify_test_tc(&pus_tc, true, 16);
let mut test_buf: [u8; 32] = [0; 32];
let size = pus_tc
.write_to(test_buf.as_mut_slice())
.write_to_bytes(test_buf.as_mut_slice())
.expect("Error writing TC to buffer");
assert_eq!(test_buf[11], 1);
assert_eq!(test_buf[12], 2);
@ -677,7 +672,7 @@ mod tests {
assert_eq!(pus_tc.sequence_flags(), SequenceFlags::Unsegmented);
pus_tc.calc_own_crc16();
pus_tc
.write_to(test_buf.as_mut_slice())
.write_to_bytes(test_buf.as_mut_slice())
.expect("Error writing TC to buffer");
assert_eq!(test_buf[0], 0x1f);
assert_eq!(test_buf[1], 0xff);

View File

@ -91,9 +91,24 @@ pub trait CcsdsTimeProvider {
fn p_field(&self) -> (usize, [u8; 2]);
fn ccdsd_time_code(&self) -> CcsdsTimeCodes;
fn unix_seconds(&self) -> i64;
fn date_time(&self) -> DateTime<Utc>;
fn date_time(&self) -> Option<DateTime<Utc>>;
}
/// This object is the abstraction for the CCSDS Day Segmented Time Code (CDS).
///
/// It has the capability to generate and read timestamps as specified in the CCSDS 301.0-B-4
/// section 3.3
///
/// # Example
///
/// ```
/// use spacepackets::time::{CdsShortTimeProvider, TimeWriter};
/// use spacepackets::time::CcsdsTimeCodes::Cds;
/// let timestamp_now = CdsShortTimeProvider::from_now().unwrap();
/// let mut raw_stamp = [0; 7];
/// timestamp_now.write_to_bytes(&mut raw_stamp).unwrap();
/// assert_eq!((raw_stamp[0] >> 4) & 0b111, Cds as u8);
/// ```
#[derive(Debug, Copy, Clone, Default)]
pub struct CdsShortTimeProvider {
pfield: u8,
@ -106,7 +121,7 @@ pub struct CdsShortTimeProvider {
impl CdsShortTimeProvider {
pub fn new(ccsds_days: u16, ms_of_day: u32) -> Self {
let provider = Self {
pfield: (CcsdsTimeCodes::Cds as u8) << 4,
pfield: (Cds as u8) << 4,
ccsds_days,
ms_of_day,
unix_seconds: 0,
@ -192,15 +207,15 @@ impl CcsdsTimeProvider for CdsShortTimeProvider {
}
fn ccdsd_time_code(&self) -> CcsdsTimeCodes {
CcsdsTimeCodes::Cds
Cds
}
fn unix_seconds(&self) -> i64 {
self.unix_seconds
}
fn date_time(&self) -> DateTime<Utc> {
self.date_time.expect("Invalid date time")
fn date_time(&self) -> Option<DateTime<Utc>> {
self.date_time
}
}
@ -235,19 +250,9 @@ impl TimeReader for CdsShortTimeProvider {
match CcsdsTimeCodes::try_from(pfield >> 4 & 0b111) {
Ok(cds_type) => match cds_type {
Cds => (),
_ => {
return Err(TimestampError::InvalidTimeCode(
CcsdsTimeCodes::Cds,
cds_type as u8,
))
}
_ => return Err(TimestampError::InvalidTimeCode(Cds, cds_type as u8)),
},
_ => {
return Err(TimestampError::InvalidTimeCode(
CcsdsTimeCodes::Cds,
pfield >> 4 & 0b111,
))
}
_ => return Err(TimestampError::InvalidTimeCode(Cds, pfield >> 4 & 0b111)),
};
let ccsds_days: u16 = u16::from_be_bytes(buf[1..3].try_into().unwrap());
let ms_of_day: u32 = u32::from_be_bytes(buf[3..7].try_into().unwrap());
@ -283,12 +288,9 @@ mod tests {
time_stamper.unix_seconds(),
(DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32) as i64
);
assert_eq!(time_stamper.ccdsd_time_code(), CcsdsTimeCodes::Cds);
assert_eq!(
time_stamper.p_field(),
(1, [(CcsdsTimeCodes::Cds as u8) << 4, 0])
);
let date_time = time_stamper.date_time();
assert_eq!(time_stamper.ccdsd_time_code(), Cds);
assert_eq!(time_stamper.p_field(), (1, [(Cds as u8) << 4, 0]));
let date_time = time_stamper.date_time().unwrap();
assert_eq!(date_time.year(), 1958);
assert_eq!(date_time.month(), 1);
assert_eq!(date_time.day(), 1);
@ -301,7 +303,7 @@ mod tests {
fn test_time_stamp_unix_epoch() {
let time_stamper = CdsShortTimeProvider::new((-DAYS_CCSDS_TO_UNIX) as u16, 0);
assert_eq!(time_stamper.unix_seconds(), 0);
let date_time = time_stamper.date_time();
let date_time = time_stamper.date_time().unwrap();
assert_eq!(date_time.year(), 1970);
assert_eq!(date_time.month(), 1);
assert_eq!(date_time.day(), 1);
@ -392,7 +394,7 @@ mod tests {
let err = res.unwrap_err();
match err {
InvalidTimeCode(code, raw) => {
assert_eq!(code, CcsdsTimeCodes::Cds);
assert_eq!(code, Cds);
assert_eq!(raw, 0);
}
OtherPacketError(_) => {}
@ -425,7 +427,7 @@ mod tests {
fn test_time_now() {
let timestamp_now = CdsShortTimeProvider::from_now().unwrap();
let compare_stamp = Utc::now();
let dt = timestamp_now.date_time();
let dt = timestamp_now.date_time().unwrap();
if compare_stamp.year() > dt.year() {
assert_eq!(compare_stamp.year() - dt.year(), 1);
} else {

View File

@ -67,7 +67,7 @@ pub mod zc {
}
impl PusTmSecHeaderWithoutTimestamp {
pub fn to_bytes(&self, slice: &mut [u8]) -> Option<()> {
pub fn write_to_bytes(&self, slice: &mut [u8]) -> Option<()> {
self.write_to(slice)
}
@ -309,28 +309,27 @@ impl<'slice> PusTm<'slice> {
}
/// Write the raw PUS byte representation to a provided buffer.
pub fn write_to(&self, slice: &mut [u8]) -> Result<usize, PusError> {
pub fn write_to_bytes(&self, slice: &mut [u8]) -> Result<usize, PusError> {
let mut curr_idx = 0;
let sph_zc = crate::zc::SpHeader::from(self.sp_header);
let total_size = self.len_packed();
if total_size > slice.len() {
return Err(PusError::PacketError(ByteConversionError::ToSliceTooSmall(
SizeMissmatch {
found: slice.len(),
expected: total_size,
},
)));
return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
found: slice.len(),
expected: total_size,
})
.into());
}
sph_zc
.to_bytes(&mut slice[curr_idx..curr_idx + CCSDS_HEADER_LEN])
.ok_or(PusError::PacketError(ByteConversionError::ZeroCopyToError))?;
.ok_or(ByteConversionError::ZeroCopyToError)?;
curr_idx += CCSDS_HEADER_LEN;
let sec_header_len = size_of::<zc::PusTmSecHeaderWithoutTimestamp>();
let sec_header = zc::PusTmSecHeaderWithoutTimestamp::try_from(self.sec_header).unwrap();
sec_header
.to_bytes(&mut slice[curr_idx..curr_idx + sec_header_len])
.ok_or(PusError::PacketError(ByteConversionError::ZeroCopyToError))?;
.write_to_bytes(&mut slice[curr_idx..curr_idx + sec_header_len])
.ok_or(ByteConversionError::ZeroCopyToError)?;
curr_idx += sec_header_len;
let timestamp_len = self.sec_header.time_stamp.len();
slice[curr_idx..curr_idx + timestamp_len].copy_from_slice(self.sec_header.time_stamp);
@ -388,7 +387,7 @@ impl<'slice> PusTm<'slice> {
/// Create a [PusTm] instance from a raw slice. On success, it returns a tuple containing
/// the instance and the found byte length of the packet. The timestamp length needs to be
/// known beforehand.
pub fn new_from_raw_slice(
pub fn from_bytes(
slice: &'slice [u8],
timestamp_len: usize,
) -> Result<(Self, usize), PusError> {
@ -399,9 +398,7 @@ impl<'slice> PusTm<'slice> {
let mut current_idx = 0;
let sph =
crate::zc::SpHeader::from_bytes(&slice[current_idx..current_idx + CCSDS_HEADER_LEN])
.ok_or(PusError::PacketError(
ByteConversionError::ZeroCopyFromError,
))?;
.ok_or(ByteConversionError::ZeroCopyFromError)?;
current_idx += 6;
let total_len = sph.total_len();
if raw_data_len < total_len || total_len < PUS_TM_MIN_LEN_WITHOUT_SOURCE_DATA {
@ -410,9 +407,7 @@ impl<'slice> PusTm<'slice> {
let sec_header_zc = zc::PusTmSecHeaderWithoutTimestamp::from_bytes(
&slice[current_idx..current_idx + PUC_TM_MIN_SEC_HEADER_LEN],
)
.ok_or(PusError::PacketError(
ByteConversionError::ZeroCopyFromError,
))?;
.ok_or(ByteConversionError::ZeroCopyFromError)?;
current_idx += PUC_TM_MIN_SEC_HEADER_LEN;
let zc_sec_header_wrapper = zc::PusTmSecHeader {
zc_header: sec_header_zc,
@ -501,7 +496,9 @@ mod tests {
let time_stamp = dummy_time_stamp();
let pus_tm = base_ping_reply_full_ctor(&time_stamp);
let mut buf: [u8; 32] = [0; 32];
let ser_len = pus_tm.write_to(&mut buf).expect("Serialization failed");
let ser_len = pus_tm
.write_to_bytes(&mut buf)
.expect("Serialization failed");
assert_eq!(ser_len, 22);
verify_raw_ping_reply(&buf);
}
@ -511,7 +508,9 @@ mod tests {
let src_data = [1, 2, 3];
let hk_reply = base_hk_reply(dummy_time_stamp(), &src_data);
let mut buf: [u8; 32] = [0; 32];
let ser_len = hk_reply.write_to(&mut buf).expect("Serialization failed");
let ser_len = hk_reply
.write_to_bytes(&mut buf)
.expect("Serialization failed");
assert_eq!(ser_len, 25);
assert_eq!(buf[20], 1);
assert_eq!(buf[21], 2);
@ -537,10 +536,11 @@ mod tests {
let time_stamp = dummy_time_stamp();
let pus_tm = base_ping_reply_full_ctor(&time_stamp);
let mut buf: [u8; 32] = [0; 32];
let ser_len = pus_tm.write_to(&mut buf).expect("Serialization failed");
let ser_len = pus_tm
.write_to_bytes(&mut buf)
.expect("Serialization failed");
assert_eq!(ser_len, 22);
let (tm_deserialized, size) =
PusTm::new_from_raw_slice(&buf, 7).expect("Deserialization failed");
let (tm_deserialized, size) = PusTm::from_bytes(&buf, 7).expect("Deserialization failed");
assert_eq!(ser_len, size);
verify_ping_reply(&tm_deserialized, false, 22, dummy_time_stamp());
}
@ -553,13 +553,13 @@ mod tests {
tm.calc_crc_on_serialization = false;
assert_eq!(tm.data_len(), 0x00);
let mut buf: [u8; 32] = [0; 32];
let res = tm.write_to(&mut buf);
let res = tm.write_to_bytes(&mut buf);
assert!(res.is_err());
assert!(matches!(res.unwrap_err(), PusError::CrcCalculationMissing));
tm.update_ccsds_data_len();
assert_eq!(tm.data_len(), 15);
tm.calc_own_crc16();
let res = tm.write_to(&mut buf);
let res = tm.write_to_bytes(&mut buf);
assert!(res.is_ok());
tm.sp_header.data_len = 0;
tm.update_packet_fields();
@ -571,12 +571,12 @@ mod tests {
let time_stamp = dummy_time_stamp();
let pus_tm = base_ping_reply_full_ctor(&time_stamp);
let mut buf: [u8; 16] = [0; 16];
let res = pus_tm.write_to(&mut buf);
let res = pus_tm.write_to_bytes(&mut buf);
assert!(res.is_err());
let error = res.unwrap_err();
assert!(matches!(error, PusError::PacketError { .. }));
assert!(matches!(error, PusError::ByteConversionError { .. }));
match error {
PusError::PacketError(err) => match err {
PusError::ByteConversionError(err) => match err {
ByteConversionError::ToSliceTooSmall(size_missmatch) => {
assert_eq!(size_missmatch.expected, 22);
assert_eq!(size_missmatch.found, 16);