7 Commits

Author SHA1 Message Date
f8199ca87a better docs 2022-11-02 00:38:59 +01:00
4c1101f65f better naming 2022-11-02 00:36:18 +01:00
38b789ca6d cargo fmt 2022-10-31 00:23:13 +01:00
d391891991 add EcssEnumerationExt trait extension 2022-10-31 00:22:28 +01:00
65e85f20e0 doc cfg support
Some checks failed
Rust/spacepackets/pipeline/head There was a failure building this commit
2022-10-26 00:22:56 +02:00
a2673c9870 make ToBeBytes trait public
Some checks failed
Rust/spacepackets/pipeline/head There was a failure building this commit
2022-10-23 18:35:56 +02:00
603f688ac3 small clippy fix
Some checks failed
Rust/spacepackets/pipeline/head There was a failure building this commit
2022-10-15 19:56:17 +02:00
6 changed files with 33 additions and 13 deletions

View File

@ -36,3 +36,7 @@ version = "1.0.1"
default = ["std"]
std = ["chrono/std", "chrono/clock", "alloc"]
alloc = ["postcard/alloc"]
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "doc_cfg"]

View File

@ -1,6 +1,7 @@
//! Common definitions and helpers required to create PUS TMTC packets according to
//! [ECSS-E-ST-70-41C](https://ecss.nl/standard/ecss-e-st-70-41c-space-engineering-telemetry-and-telecommand-packet-utilization-15-april-2016/)
use crate::{ByteConversionError, CcsdsPacket, SizeMissmatch};
use core::fmt::Debug;
use core::mem::size_of;
use crc::{Crc, CRC_16_IBM_3740};
use serde::{Deserialize, Serialize};
@ -158,7 +159,7 @@ 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
/// Generic trait for ECSS enumeration which consist of a PFC field denoting their bit 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.
@ -168,10 +169,12 @@ pub trait EcssEnumeration {
fn byte_width(&self) -> usize {
(self.pfc() / 8) as usize
}
fn write_to_bytes(&self, buf: &mut [u8]) -> Result<(), ByteConversionError>;
fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<(), ByteConversionError>;
}
trait ToBeBytes {
pub trait EcssEnumerationExt: EcssEnumeration + Debug + Copy + Clone + PartialEq + Eq {}
pub trait ToBeBytes {
type ByteArray: AsRef<[u8]>;
fn to_be_bytes(&self) -> Self::ByteArray;
}
@ -208,6 +211,7 @@ impl ToBeBytes for u64 {
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct GenericEcssEnumWrapper<TYPE> {
val: TYPE,
}
@ -227,7 +231,7 @@ impl<TYPE: ToBeBytes> EcssEnumeration for GenericEcssEnumWrapper<TYPE> {
size_of::<TYPE>() as u8 * 8_u8
}
fn write_to_bytes(&self, buf: &mut [u8]) -> Result<(), ByteConversionError> {
fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<(), ByteConversionError> {
if buf.len() < self.byte_width() as usize {
return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
found: buf.len(),
@ -239,6 +243,11 @@ impl<TYPE: ToBeBytes> EcssEnumeration for GenericEcssEnumWrapper<TYPE> {
}
}
impl<TYPE: Debug + Copy + Clone + PartialEq + Eq + ToBeBytes> EcssEnumerationExt
for GenericEcssEnumWrapper<TYPE>
{
}
pub type EcssEnumU8 = GenericEcssEnumWrapper<u8>;
pub type EcssEnumU16 = GenericEcssEnumWrapper<u16>;
pub type EcssEnumU32 = GenericEcssEnumWrapper<u32>;
@ -254,7 +263,7 @@ mod tests {
let mut buf = [0, 0, 0];
let my_enum = EcssEnumU8::new(1);
my_enum
.write_to_bytes(&mut buf[1..2])
.write_to_be_bytes(&mut buf[1..2])
.expect("To byte conversion of u8 failed");
assert_eq!(buf[1], 1);
}
@ -264,7 +273,7 @@ mod tests {
let mut buf = [0, 0, 0];
let my_enum = EcssEnumU16::new(0x1f2f);
my_enum
.write_to_bytes(&mut buf[1..3])
.write_to_be_bytes(&mut buf[1..3])
.expect("To byte conversion of u8 failed");
assert_eq!(buf[1], 0x1f);
assert_eq!(buf[2], 0x2f);
@ -274,7 +283,7 @@ mod tests {
fn test_slice_u16_too_small() {
let mut buf = [0];
let my_enum = EcssEnumU16::new(0x1f2f);
let res = my_enum.write_to_bytes(&mut buf[0..1]);
let res = my_enum.write_to_be_bytes(&mut buf[0..1]);
assert!(res.is_err());
let error = res.unwrap_err();
match error {
@ -293,7 +302,7 @@ mod tests {
let mut buf = [0, 0, 0, 0, 0];
let my_enum = EcssEnumU32::new(0x1f2f3f4f);
my_enum
.write_to_bytes(&mut buf[1..5])
.write_to_be_bytes(&mut buf[1..5])
.expect("To byte conversion of u8 failed");
assert_eq!(buf[1], 0x1f);
assert_eq!(buf[2], 0x2f);
@ -305,7 +314,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.write_to_bytes(&mut buf[0..3]);
let res = my_enum.write_to_be_bytes(&mut buf[0..3]);
assert!(res.is_err());
let error = res.unwrap_err();
match error {

View File

@ -65,11 +65,12 @@ pub struct SizeMissmatch {
pub found: usize,
pub expected: usize,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ByteConversionError {
/// The passed slice is too small. Returns the found and expected minimum size
/// The passed slice is too small. Returns the passed slice length and expected minimum size
ToSliceTooSmall(SizeMissmatch),
/// The provider buffer it soo small. Returns the found and expected minimum size
/// The provider buffer is too small. Returns the passed slice length and expected minimum size
FromSliceTooSmall(SizeMissmatch),
/// The [zerocopy] library failed to write to bytes
ZeroCopyToError,
@ -136,7 +137,7 @@ impl PacketId {
sec_header_flag,
apid: 0,
};
pid.set_apid(apid).then(|| pid)
pid.set_apid(apid).then_some(pid)
}
/// Set a new Application Process ID (APID). If the passed number is invalid, the APID will
@ -182,7 +183,7 @@ impl PacketSequenceCtrl {
seq_flags,
seq_count: 0,
};
psc.set_seq_count(seq_count).then(|| psc)
psc.set_seq_count(seq_count).then_some(psc)
}
pub fn raw(&self) -> u16 {
((self.seq_flags as u16) << 14) | self.seq_count

View File

@ -370,6 +370,7 @@ impl<'slice> PusTc<'slice> {
}
#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
pub fn append_to_vec(&self, vec: &mut Vec<u8>) -> Result<usize, PusError> {
let sph_zc = crate::zc::SpHeader::from(self.sp_header);
let mut appended_len = PUS_TC_MIN_LEN_WITHOUT_APP_DATA;

View File

@ -47,6 +47,7 @@ pub enum TimestampError {
}
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
pub fn seconds_since_epoch() -> f64 {
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
@ -133,6 +134,7 @@ impl CdsShortTimeProvider {
}
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
pub fn from_now() -> Result<Self, SystemTimeError> {
let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
let epoch = now.as_secs();
@ -151,6 +153,7 @@ impl CdsShortTimeProvider {
}
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
pub fn update_from_now(&mut self) -> Result<(), SystemTimeError> {
let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
let epoch = now.as_secs();
@ -168,6 +171,7 @@ impl CdsShortTimeProvider {
}
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
pub fn ms_of_day_using_sysclock() -> u32 {
Self::ms_of_day(seconds_since_epoch())
}

View File

@ -352,6 +352,7 @@ impl<'slice> PusTm<'slice> {
/// Append the raw PUS byte representation to a provided [alloc::vec::Vec]
#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
pub fn append_to_vec(&self, vec: &mut Vec<u8>) -> Result<usize, PusError> {
let sph_zc = crate::zc::SpHeader::from(self.sp_header);
let mut appended_len =