Compare commits
9 Commits
v0.1.0
...
fe1a30327b
Author | SHA1 | Date | |
---|---|---|---|
fe1a30327b | |||
94489da003 | |||
c72c5ad4aa | |||
bb83e67e54 | |||
a4e297f0c0 | |||
96d389a651 | |||
cc680dba46 | |||
42d3487c19 | |||
3970061ca1 |
12
CHANGELOG.md
12
CHANGELOG.md
@ -6,9 +6,17 @@ All notable changes to this project will be documented in this file.
|
||||
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
## [unreleased]
|
||||
# [unreleased]
|
||||
|
||||
## [v0.1.0] 16.08.2022
|
||||
## Added
|
||||
|
||||
- Basic support for ECSS enumeration types for u8, u16, u32 and u64
|
||||
|
||||
## Changed
|
||||
|
||||
- Better names for generic error enumerations: `PacketError` renamed to `ByteConversionError`
|
||||
|
||||
# [v0.1.0] 16.08.2022
|
||||
|
||||
Initial release with CCSDS Space Packet Primary Header implementation and basic PUS TC and TM
|
||||
implementations.
|
||||
|
157
src/ecss.rs
157
src/ecss.rs
@ -1,6 +1,6 @@
|
||||
//! 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::{CcsdsPacket, PacketError};
|
||||
use crate::{ByteConversionError, CcsdsPacket, SizeMissmatch};
|
||||
use core::mem::size_of;
|
||||
use crc::{Crc, CRC_16_IBM_3740};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -41,7 +41,7 @@ pub enum PusError {
|
||||
NoRawData,
|
||||
/// CRC16 needs to be calculated first
|
||||
CrcCalculationMissing,
|
||||
PacketError(PacketError),
|
||||
PacketError(ByteConversionError),
|
||||
}
|
||||
|
||||
pub trait PusPacket: CcsdsPacket {
|
||||
@ -135,3 +135,156 @@ macro_rules! sp_header_impls {
|
||||
|
||||
pub(crate) use ccsds_impl;
|
||||
pub(crate) use sp_header_impls;
|
||||
|
||||
pub trait EcssEnumeration {
|
||||
fn pfc(&self) -> u8;
|
||||
fn byte_width(&self) -> usize {
|
||||
(self.pfc() / 8) as usize
|
||||
}
|
||||
fn write_to_bytes(&self, buf: &mut [u8]) -> Result<(), ByteConversionError>;
|
||||
}
|
||||
|
||||
trait ToBeBytes {
|
||||
type ByteArray: AsRef<[u8]>;
|
||||
fn to_be_bytes(&self) -> Self::ByteArray;
|
||||
}
|
||||
|
||||
impl ToBeBytes for u8 {
|
||||
type ByteArray = [u8; 1];
|
||||
|
||||
fn to_be_bytes(&self) -> Self::ByteArray {
|
||||
u8::to_be_bytes(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToBeBytes for u16 {
|
||||
type ByteArray = [u8; 2];
|
||||
|
||||
fn to_be_bytes(&self) -> Self::ByteArray {
|
||||
u16::to_be_bytes(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToBeBytes for u32 {
|
||||
type ByteArray = [u8; 4];
|
||||
|
||||
fn to_be_bytes(&self) -> Self::ByteArray {
|
||||
u32::to_be_bytes(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToBeBytes for u64 {
|
||||
type ByteArray = [u8; 8];
|
||||
|
||||
fn to_be_bytes(&self) -> Self::ByteArray {
|
||||
u64::to_be_bytes(*self)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GenericEcssEnumWrapper<TYPE> {
|
||||
val: TYPE,
|
||||
}
|
||||
|
||||
impl<TYPE> GenericEcssEnumWrapper<TYPE> {
|
||||
pub fn new(val: TYPE) -> Self {
|
||||
Self { val }
|
||||
}
|
||||
}
|
||||
|
||||
impl<TYPE: ToBeBytes> EcssEnumeration for GenericEcssEnumWrapper<TYPE> {
|
||||
fn pfc(&self) -> u8 {
|
||||
size_of::<TYPE>() as u8 * 8_u8
|
||||
}
|
||||
|
||||
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(),
|
||||
expected: self.byte_width() as usize,
|
||||
}));
|
||||
}
|
||||
buf[0..self.byte_width() as usize].copy_from_slice(self.val.to_be_bytes().as_ref());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub type EcssEnumU8 = GenericEcssEnumWrapper<u8>;
|
||||
pub type EcssEnumU16 = GenericEcssEnumWrapper<u16>;
|
||||
pub type EcssEnumU32 = GenericEcssEnumWrapper<u32>;
|
||||
pub type EcssEnumU64 = GenericEcssEnumWrapper<u64>;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::ecss::{EcssEnumU16, EcssEnumU32, EcssEnumU8, EcssEnumeration};
|
||||
use crate::ByteConversionError;
|
||||
|
||||
#[test]
|
||||
fn test_enum_u8() {
|
||||
let mut buf = [0, 0, 0];
|
||||
let my_enum = EcssEnumU8::new(1);
|
||||
my_enum
|
||||
.write_to_bytes(&mut buf[1..2])
|
||||
.expect("To byte conversion of u8 failed");
|
||||
assert_eq!(buf[1], 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_enum_u16() {
|
||||
let mut buf = [0, 0, 0];
|
||||
let my_enum = EcssEnumU16::new(0x1f2f);
|
||||
my_enum
|
||||
.write_to_bytes(&mut buf[1..3])
|
||||
.expect("To byte conversion of u8 failed");
|
||||
assert_eq!(buf[1], 0x1f);
|
||||
assert_eq!(buf[2], 0x2f);
|
||||
}
|
||||
|
||||
#[test]
|
||||
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]);
|
||||
assert!(res.is_err());
|
||||
let error = res.unwrap_err();
|
||||
match error {
|
||||
ByteConversionError::ToSliceTooSmall(missmatch) => {
|
||||
assert_eq!(missmatch.expected, 2);
|
||||
assert_eq!(missmatch.found, 1);
|
||||
}
|
||||
_ => {
|
||||
panic!("Unexpected error {:?}", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_enum_u32() {
|
||||
let mut buf = [0, 0, 0, 0, 0];
|
||||
let my_enum = EcssEnumU32::new(0x1f2f3f4f);
|
||||
my_enum
|
||||
.write_to_bytes(&mut buf[1..5])
|
||||
.expect("To byte conversion of u8 failed");
|
||||
assert_eq!(buf[1], 0x1f);
|
||||
assert_eq!(buf[2], 0x2f);
|
||||
assert_eq!(buf[3], 0x3f);
|
||||
assert_eq!(buf[4], 0x4f);
|
||||
}
|
||||
|
||||
#[test]
|
||||
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]);
|
||||
assert!(res.is_err());
|
||||
let error = res.unwrap_err();
|
||||
match error {
|
||||
ByteConversionError::ToSliceTooSmall(missmatch) => {
|
||||
assert_eq!(missmatch.expected, 4);
|
||||
assert_eq!(missmatch.found, 3);
|
||||
}
|
||||
_ => {
|
||||
panic!("Unexpected error {:?}", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
24
src/lib.rs
24
src/lib.rs
@ -46,7 +46,7 @@
|
||||
#![no_std]
|
||||
#[cfg(feature = "alloc")]
|
||||
extern crate alloc;
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(any(feature = "std", test))]
|
||||
extern crate std;
|
||||
|
||||
use crate::ecss::CCSDS_HEADER_LEN;
|
||||
@ -59,20 +59,22 @@ pub mod tc;
|
||||
pub mod time;
|
||||
pub mod tm;
|
||||
|
||||
pub const MAX_APID: u16 = 2u16.pow(11) - 1;
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub struct SizeMissmatch {
|
||||
pub found: usize,
|
||||
pub expected: usize,
|
||||
}
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub enum PacketError {
|
||||
pub enum ByteConversionError {
|
||||
/// The passed slice is too small. Returns the found and expected minimum size
|
||||
ToBytesSliceTooSmall(SizeMissmatch),
|
||||
ToSliceTooSmall(SizeMissmatch),
|
||||
/// The provider buffer it soo small. Returns the found and expected minimum size
|
||||
FromBytesSliceTooSmall(SizeMissmatch),
|
||||
FromSliceTooSmall(SizeMissmatch),
|
||||
/// The [zerocopy] library failed to write to bytes
|
||||
ToBytesZeroCopyError,
|
||||
FromBytesZeroCopyError,
|
||||
ZeroCopyToError,
|
||||
ZeroCopyFromError,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Copy, Clone)]
|
||||
@ -142,7 +144,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;
|
||||
@ -346,7 +348,7 @@ impl SpHeader {
|
||||
ssc: u16,
|
||||
data_len: u16,
|
||||
) -> Option<Self> {
|
||||
if ssc > 2u16.pow(14) - 1 || apid > 2u16.pow(11) - 1 {
|
||||
if ssc > 2u16.pow(14) - 1 || apid > MAX_APID {
|
||||
return None;
|
||||
}
|
||||
let mut header = SpHeader::default();
|
||||
@ -391,15 +393,15 @@ impl SpHeader {
|
||||
self.packet_id.ptype = packet_type;
|
||||
}
|
||||
|
||||
pub fn from_raw_slice(buf: &[u8]) -> Result<Self, PacketError> {
|
||||
pub fn from_raw_slice(buf: &[u8]) -> Result<Self, ByteConversionError> {
|
||||
if buf.len() < CCSDS_HEADER_LEN + 1 {
|
||||
return Err(PacketError::FromBytesSliceTooSmall(SizeMissmatch {
|
||||
return Err(ByteConversionError::FromSliceTooSmall(SizeMissmatch {
|
||||
found: buf.len(),
|
||||
expected: CCSDS_HEADER_LEN + 1,
|
||||
}));
|
||||
}
|
||||
let zc_header = zc::SpHeader::from_bytes(&buf[0..CCSDS_HEADER_LEN])
|
||||
.ok_or(PacketError::FromBytesZeroCopyError)?;
|
||||
.ok_or(ByteConversionError::ZeroCopyFromError)?;
|
||||
Ok(Self::from(zc_header))
|
||||
}
|
||||
}
|
||||
|
54
src/tc.rs
54
src/tc.rs
@ -20,7 +20,7 @@
|
||||
//! // 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]);
|
||||
@ -36,7 +36,9 @@ use crate::ecss::{
|
||||
verify_crc16_from_raw, CrcType, PusError, PusPacket, PusVersion, CRC_CCITT_FALSE,
|
||||
};
|
||||
use crate::SpHeader;
|
||||
use crate::{CcsdsPacket, PacketError, PacketType, SequenceFlags, SizeMissmatch, CCSDS_HEADER_LEN};
|
||||
use crate::{
|
||||
ByteConversionError, CcsdsPacket, PacketType, SequenceFlags, SizeMissmatch, CCSDS_HEADER_LEN,
|
||||
};
|
||||
use core::mem::size_of;
|
||||
use delegate::delegate;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -124,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)
|
||||
}
|
||||
|
||||
@ -328,13 +330,13 @@ 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(PacketError::ToBytesSliceTooSmall(
|
||||
return Err(PusError::PacketError(ByteConversionError::ToSliceTooSmall(
|
||||
SizeMissmatch {
|
||||
found: slice.len(),
|
||||
expected: total_size,
|
||||
@ -343,13 +345,13 @@ impl<'slice> PusTc<'slice> {
|
||||
}
|
||||
sph_zc
|
||||
.to_bytes(&mut slice[curr_idx..curr_idx + CCSDS_HEADER_LEN])
|
||||
.ok_or(PusError::PacketError(PacketError::ToBytesZeroCopyError))?;
|
||||
.ok_or(PusError::PacketError(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(PacketError::ToBytesZeroCopyError))?;
|
||||
.write_to_bytes(&mut slice[curr_idx..curr_idx + tc_header_len])
|
||||
.ok_or(PusError::PacketError(ByteConversionError::ZeroCopyToError))?;
|
||||
|
||||
curr_idx += tc_header_len;
|
||||
if let Some(app_data) = self.app_data {
|
||||
@ -408,16 +410,20 @@ 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(PacketError::FromBytesZeroCopyError))?;
|
||||
.ok_or(PusError::PacketError(
|
||||
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 {
|
||||
return Err(PusError::RawDataTooShort(raw_data_len));
|
||||
}
|
||||
let sec_header = crate::tc::zc::PusTcSecondaryHeader::from_bytes(
|
||||
let sec_header = zc::PusTcSecondaryHeader::from_bytes(
|
||||
&slice[current_idx..current_idx + PUC_TC_SECONDARY_HEADER_LEN],
|
||||
)
|
||||
.ok_or(PusError::PacketError(PacketError::FromBytesZeroCopyError))?;
|
||||
.ok_or(PusError::PacketError(
|
||||
ByteConversionError::ZeroCopyFromError,
|
||||
))?;
|
||||
current_idx += PUC_TC_SECONDARY_HEADER_LEN;
|
||||
let raw_data = &slice[0..total_len];
|
||||
let pus_tc = PusTc {
|
||||
@ -431,6 +437,10 @@ impl<'slice> PusTc<'slice> {
|
||||
verify_crc16_from_raw(raw_data, pus_tc.crc16.expect("CRC16 invalid"))?;
|
||||
Ok((pus_tc, total_len))
|
||||
}
|
||||
|
||||
pub fn raw(&self) -> Option<&'slice [u8]> {
|
||||
self.raw_data
|
||||
}
|
||||
}
|
||||
|
||||
//noinspection RsTraitImplementation
|
||||
@ -472,8 +482,8 @@ mod tests {
|
||||
use crate::ecss::{PusError, PusPacket};
|
||||
use crate::tc::ACK_ALL;
|
||||
use crate::tc::{PusTc, PusTcSecondaryHeader, PusTcSecondaryHeaderT};
|
||||
use crate::{ByteConversionError, SpHeader};
|
||||
use crate::{CcsdsPacket, SequenceFlags};
|
||||
use crate::{PacketError, SpHeader};
|
||||
use alloc::vec::Vec;
|
||||
|
||||
fn base_ping_tc_full_ctor() -> PusTc<'static> {
|
||||
@ -504,7 +514,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);
|
||||
}
|
||||
@ -514,7 +524,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);
|
||||
let (tc_from_raw, size) = PusTc::new_from_raw_slice(&test_buf)
|
||||
@ -540,7 +550,7 @@ 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)
|
||||
@ -571,7 +581,7 @@ 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);
|
||||
@ -587,7 +597,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);
|
||||
@ -598,7 +608,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 { .. }));
|
||||
@ -622,12 +632,12 @@ 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 {
|
||||
PacketError::ToBytesSliceTooSmall(missmatch) => {
|
||||
ByteConversionError::ToSliceTooSmall(missmatch) => {
|
||||
assert_eq!(missmatch.expected, pus_tc.len_packed());
|
||||
assert_eq!(missmatch.found, 12);
|
||||
}
|
||||
@ -643,7 +653,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);
|
||||
@ -667,7 +677,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);
|
||||
|
43
src/time.rs
43
src/time.rs
@ -1,5 +1,5 @@
|
||||
//! CCSDS Time Code Formats according to [CCSDS 301.0-B-4](https://public.ccsds.org/Pubs/301x0b4e1.pdf)
|
||||
use crate::{PacketError, SizeMissmatch};
|
||||
use crate::{ByteConversionError, SizeMissmatch};
|
||||
use chrono::{DateTime, TimeZone, Utc};
|
||||
|
||||
#[allow(unused_imports)]
|
||||
@ -38,12 +38,12 @@ impl TryFrom<u8> for CcsdsTimeCodes {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||
pub enum TimestampError {
|
||||
/// Contains tuple where first value is the expected time code and the second
|
||||
/// value is the found raw value
|
||||
InvalidTimeCode(CcsdsTimeCodes, u8),
|
||||
OtherPacketError(PacketError),
|
||||
OtherPacketError(ByteConversionError),
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
@ -71,7 +71,7 @@ pub const fn ccsds_to_unix_days(ccsds_days: i32) -> i32 {
|
||||
}
|
||||
|
||||
pub trait TimeWriter {
|
||||
fn write_to_bytes(&self, bytes: &mut [u8]) -> Result<(), PacketError>;
|
||||
fn write_to_bytes(&self, bytes: &mut [u8]) -> Result<(), TimestampError>;
|
||||
}
|
||||
|
||||
pub trait TimeReader {
|
||||
@ -94,7 +94,7 @@ pub trait CcsdsTimeProvider {
|
||||
fn date_time(&self) -> DateTime<Utc>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
#[derive(Debug, Copy, Clone, Default)]
|
||||
pub struct CdsShortTimeProvider {
|
||||
pfield: u8,
|
||||
ccsds_days: u16,
|
||||
@ -125,7 +125,7 @@ impl CdsShortTimeProvider {
|
||||
let unix_days_seconds = epoch - secs_of_day;
|
||||
let ms_of_day = secs_of_day * 1000 + now.subsec_millis() as u64;
|
||||
let provider = Self {
|
||||
pfield: (CcsdsTimeCodes::Cds as u8) << 4,
|
||||
pfield: (Cds as u8) << 4,
|
||||
ccsds_days: unix_to_ccsds_days((unix_days_seconds / SECONDS_PER_DAY as u64) as i32)
|
||||
as u16,
|
||||
ms_of_day: ms_of_day as u32,
|
||||
@ -135,6 +135,17 @@ impl CdsShortTimeProvider {
|
||||
Ok(provider.setup(unix_days_seconds as i64, ms_of_day))
|
||||
}
|
||||
|
||||
#[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();
|
||||
let secs_of_day = epoch % SECONDS_PER_DAY as u64;
|
||||
let unix_days_seconds = epoch - secs_of_day;
|
||||
let ms_of_day = secs_of_day * 1000 + now.subsec_millis() as u64;
|
||||
self.setup(unix_days_seconds as i64, ms_of_day);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn setup(mut self, unix_days_seconds: i64, ms_of_day: u64) -> Self {
|
||||
self.calc_unix_seconds(unix_days_seconds, ms_of_day);
|
||||
self.calc_date_time((ms_of_day % 1000) as u32);
|
||||
@ -194,12 +205,14 @@ impl CcsdsTimeProvider for CdsShortTimeProvider {
|
||||
}
|
||||
|
||||
impl TimeWriter for CdsShortTimeProvider {
|
||||
fn write_to_bytes(&self, buf: &mut [u8]) -> Result<(), PacketError> {
|
||||
fn write_to_bytes(&self, buf: &mut [u8]) -> Result<(), TimestampError> {
|
||||
if buf.len() < self.len_as_bytes() {
|
||||
return Err(PacketError::ToBytesSliceTooSmall(SizeMissmatch {
|
||||
expected: self.len_as_bytes(),
|
||||
found: buf.len(),
|
||||
}));
|
||||
return Err(TimestampError::OtherPacketError(
|
||||
ByteConversionError::ToSliceTooSmall(SizeMissmatch {
|
||||
expected: self.len_as_bytes(),
|
||||
found: buf.len(),
|
||||
}),
|
||||
));
|
||||
}
|
||||
buf[0] = self.pfield;
|
||||
buf[1..3].copy_from_slice(self.ccsds_days.to_be_bytes().as_slice());
|
||||
@ -212,7 +225,7 @@ impl TimeReader for CdsShortTimeProvider {
|
||||
fn from_bytes(buf: &[u8]) -> Result<Self, TimestampError> {
|
||||
if buf.len() < CDS_SHORT_LEN {
|
||||
return Err(TimestampError::OtherPacketError(
|
||||
PacketError::FromBytesSliceTooSmall(SizeMissmatch {
|
||||
ByteConversionError::FromSliceTooSmall(SizeMissmatch {
|
||||
expected: CDS_SHORT_LEN,
|
||||
found: buf.len(),
|
||||
}),
|
||||
@ -246,7 +259,7 @@ impl TimeReader for CdsShortTimeProvider {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::time::TimestampError::{InvalidTimeCode, OtherPacketError};
|
||||
use crate::PacketError::{FromBytesSliceTooSmall, ToBytesSliceTooSmall};
|
||||
use crate::ByteConversionError::{FromSliceTooSmall, ToSliceTooSmall};
|
||||
use alloc::format;
|
||||
use chrono::{Datelike, Timelike};
|
||||
|
||||
@ -334,7 +347,7 @@ mod tests {
|
||||
let res = time_stamper.write_to_bytes(&mut buf[0..i]);
|
||||
assert!(res.is_err());
|
||||
match res.unwrap_err() {
|
||||
ToBytesSliceTooSmall(missmatch) => {
|
||||
OtherPacketError(ToSliceTooSmall(missmatch)) => {
|
||||
assert_eq!(missmatch.found, i);
|
||||
assert_eq!(missmatch.expected, 7);
|
||||
}
|
||||
@ -357,7 +370,7 @@ mod tests {
|
||||
panic!("Unexpected error");
|
||||
}
|
||||
OtherPacketError(e) => match e {
|
||||
FromBytesSliceTooSmall(missmatch) => {
|
||||
FromSliceTooSmall(missmatch) => {
|
||||
assert_eq!(missmatch.found, i);
|
||||
assert_eq!(missmatch.expected, 7);
|
||||
}
|
||||
|
47
src/tm.rs
47
src/tm.rs
@ -5,7 +5,8 @@ use crate::ecss::{
|
||||
verify_crc16_from_raw, CrcType, PusError, PusPacket, PusVersion, CRC_CCITT_FALSE,
|
||||
};
|
||||
use crate::{
|
||||
CcsdsPacket, PacketError, PacketType, SequenceFlags, SizeMissmatch, SpHeader, CCSDS_HEADER_LEN,
|
||||
ByteConversionError, CcsdsPacket, PacketType, SequenceFlags, SizeMissmatch, SpHeader,
|
||||
CCSDS_HEADER_LEN,
|
||||
};
|
||||
use core::mem::size_of;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -66,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)
|
||||
}
|
||||
|
||||
@ -258,6 +259,10 @@ impl<'slice> PusTm<'slice> {
|
||||
self.sec_header.time_stamp
|
||||
}
|
||||
|
||||
pub fn source_data(&self) -> Option<&'slice [u8]> {
|
||||
self.source_data
|
||||
}
|
||||
|
||||
pub fn set_dest_id(&mut self, dest_id: u16) {
|
||||
self.sec_header.dest_id = dest_id;
|
||||
}
|
||||
@ -304,12 +309,12 @@ 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(PacketError::ToBytesSliceTooSmall(
|
||||
return Err(PusError::PacketError(ByteConversionError::ToSliceTooSmall(
|
||||
SizeMissmatch {
|
||||
found: slice.len(),
|
||||
expected: total_size,
|
||||
@ -318,14 +323,14 @@ impl<'slice> PusTm<'slice> {
|
||||
}
|
||||
sph_zc
|
||||
.to_bytes(&mut slice[curr_idx..curr_idx + CCSDS_HEADER_LEN])
|
||||
.ok_or(PusError::PacketError(PacketError::ToBytesZeroCopyError))?;
|
||||
.ok_or(PusError::PacketError(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(PacketError::ToBytesZeroCopyError))?;
|
||||
.write_to_bytes(&mut slice[curr_idx..curr_idx + sec_header_len])
|
||||
.ok_or(PusError::PacketError(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);
|
||||
@ -394,7 +399,9 @@ 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(PacketError::FromBytesZeroCopyError))?;
|
||||
.ok_or(PusError::PacketError(
|
||||
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 {
|
||||
@ -403,7 +410,9 @@ 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(PacketError::FromBytesZeroCopyError))?;
|
||||
.ok_or(PusError::PacketError(
|
||||
ByteConversionError::ZeroCopyFromError,
|
||||
))?;
|
||||
current_idx += PUC_TM_MIN_SEC_HEADER_LEN;
|
||||
let zc_sec_header_wrapper = zc::PusTmSecHeader {
|
||||
zc_header: sec_header_zc,
|
||||
@ -492,7 +501,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);
|
||||
}
|
||||
@ -502,7 +513,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);
|
||||
@ -528,7 +541,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);
|
||||
let (tm_deserialized, size) =
|
||||
PusTm::new_from_raw_slice(&buf, 7).expect("Deserialization failed");
|
||||
@ -544,13 +559,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();
|
||||
@ -562,13 +577,13 @@ 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 { .. }));
|
||||
match error {
|
||||
PusError::PacketError(err) => match err {
|
||||
PacketError::ToBytesSliceTooSmall(size_missmatch) => {
|
||||
ByteConversionError::ToSliceTooSmall(size_missmatch) => {
|
||||
assert_eq!(size_missmatch.expected, 22);
|
||||
assert_eq!(size_missmatch.found, 16);
|
||||
}
|
||||
|
Reference in New Issue
Block a user