spacepackets/src/time/cuc.rs

1417 lines
51 KiB
Rust
Raw Normal View History

2022-12-09 17:33:04 +01:00
//! Module to generate or read CCSDS Unsegmented (CUC) timestamps as specified in
//! [CCSDS 301.0-B-4](https://public.ccsds.org/Pubs/301x0b4e1.pdf) section 3.2 .
//!
2024-03-18 15:14:40 +01:00
//! The core data structure to do this is the [CucTime] struct which provides a CUC time object
//! using the CCSDS Epoch.
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use core::fmt::{Debug, Display, Formatter};
use core::ops::{Add, AddAssign};
use core::time::Duration;
2023-12-05 15:05:00 +01:00
use core::u64;
2022-12-08 15:22:19 +01:00
2024-03-18 15:14:40 +01:00
use crate::ByteConversionError;
#[cfg(feature = "std")]
use super::StdTimestampError;
use super::{
ccsds_epoch_to_unix_epoch, ccsds_time_code_from_p_field, unix_epoch_to_ccsds_epoch,
2024-03-25 13:42:18 +01:00
CcsdsTimeCode, CcsdsTimeProvider, DateBeforeCcsdsEpochError, TimeReader, TimeWriter,
TimestampError, UnixTime,
2024-03-18 15:14:40 +01:00
};
#[cfg(feature = "std")]
use std::error::Error;
#[cfg(feature = "std")]
use std::time::SystemTime;
#[cfg(feature = "chrono")]
use chrono::Datelike;
2022-12-08 15:22:19 +01:00
const MIN_CUC_LEN: usize = 2;
/// Base value for the preamble field for a time field parser to determine the time field type.
2024-03-18 15:14:40 +01:00
pub const P_FIELD_BASE: u8 = (CcsdsTimeCode::CucCcsdsEpoch as u8) << 4;
2022-12-09 17:33:04 +01:00
/// Maximum length if the preamble field is not extended.
pub const MAX_CUC_LEN_SMALL_PREAMBLE: usize = 8;
2022-12-08 15:22:19 +01:00
2022-12-09 13:50:04 +01:00
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum FractionalResolution {
/// No fractional part, only second resolution
Seconds = 0,
2024-03-18 15:14:40 +01:00
/// 255 fractional parts, resulting in 1/255 ~= 4 ms fractional resolution
2022-12-09 13:50:04 +01:00
FourMs = 1,
/// 65535 fractional parts, resulting in 1/65535 ~= 15 us fractional resolution
FifteenUs = 2,
/// 16777215 fractional parts, resulting in 1/16777215 ~= 60 ns fractional resolution
SixtyNs = 3,
}
impl TryFrom<u8> for FractionalResolution {
type Error = ();
fn try_from(v: u8) -> Result<Self, Self::Error> {
match v {
0 => Ok(FractionalResolution::Seconds),
1 => Ok(FractionalResolution::FourMs),
2 => Ok(FractionalResolution::FifteenUs),
3 => Ok(FractionalResolution::SixtyNs),
_ => Err(()),
}
}
}
2024-03-18 15:14:40 +01:00
/// Please note that this function will panic if the fractional counter is not smaller than
2022-12-09 13:50:04 +01:00
/// the maximum number of fractions allowed for the particular resolution.
/// (e.g. passing 270 when the resolution only allows 255 values).
2023-01-15 17:42:23 +01:00
#[inline]
2022-12-09 13:50:04 +01:00
pub fn convert_fractional_part_to_ns(fractional_part: FractionalPart) -> u64 {
2024-03-18 15:14:40 +01:00
let div = fractional_res_to_div(fractional_part.resolution);
assert!(fractional_part.counter <= div);
10_u64.pow(9) * fractional_part.counter as u64 / div as u64
2022-12-09 13:50:04 +01:00
}
2023-01-15 17:42:23 +01:00
#[inline(always)]
2022-12-09 13:50:04 +01:00
pub const fn fractional_res_to_div(res: FractionalResolution) -> u32 {
2024-03-18 15:14:40 +01:00
// We do not use the full possible range for a given resolution. This is because if we did
// that, the largest value would be equal to the counter being incremented by one. Thus, the
// smallest allowed fractions value is 0 while the largest allowed fractions value is the
// closest fractions value to the next counter increment.
2022-12-09 13:50:04 +01:00
2_u32.pow(8 * res as u32) - 1
}
2022-12-09 16:51:48 +01:00
/// Calculate the fractional part for a given resolution and subsecond nanoseconds.
/// Please note that this function will panic if the passed nanoseconds exceeds 1 second
/// as a nanosecond (10 to the power of 9). Furthermore, it will return [None] if the
/// given resolution is [FractionalResolution::Seconds].
2024-03-18 15:14:40 +01:00
pub fn fractional_part_from_subsec_ns(res: FractionalResolution, ns: u64) -> FractionalPart {
2022-12-09 16:51:48 +01:00
if res == FractionalResolution::Seconds {
2024-03-18 15:14:40 +01:00
return FractionalPart::new_with_seconds_resolution();
2022-12-09 16:51:48 +01:00
}
let sec_as_ns = 10_u64.pow(9);
if ns > sec_as_ns {
panic!("passed nanosecond value larger than 1 second");
}
2024-03-18 15:14:40 +01:00
let resolution_divisor = fractional_res_to_div(res) as u64;
// This is probably the cheapest way to calculate the fractional part without using expensive
// floating point division.
let fractional_counter = ns * (resolution_divisor + 1) / sec_as_ns;
FractionalPart {
resolution: res,
counter: fractional_counter as u32,
}
2022-12-09 16:51:48 +01:00
}
2022-12-08 15:22:19 +01:00
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum CucError {
InvalidCounterWidth(u8),
2023-01-17 01:16:07 +01:00
/// Invalid counter supplied.
2023-12-05 15:05:00 +01:00
InvalidCounter {
width: u8,
counter: u64,
},
InvalidFractions {
resolution: FractionalResolution,
value: u64,
},
2024-03-18 15:14:40 +01:00
LeapSecondCorrectionError,
2024-03-25 13:42:18 +01:00
DateBeforeCcsdsEpoch(DateBeforeCcsdsEpochError),
2022-12-08 15:22:19 +01:00
}
impl Display for CucError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
CucError::InvalidCounterWidth(w) => {
2023-01-26 21:57:45 +01:00
write!(f, "invalid cuc counter byte width {w}")
2022-12-08 15:22:19 +01:00
}
2023-12-05 15:05:00 +01:00
CucError::InvalidCounter { width, counter } => {
write!(f, "invalid cuc counter {counter} for width {width}")
2022-12-08 15:22:19 +01:00
}
2023-12-05 15:05:00 +01:00
CucError::InvalidFractions { resolution, value } => {
write!(
f,
"invalid cuc fractional part {value} for resolution {resolution:?}"
)
2022-12-08 15:22:19 +01:00
}
2024-03-18 15:14:40 +01:00
CucError::LeapSecondCorrectionError => {
write!(f, "error while correcting for leap seconds")
}
2024-03-25 13:42:18 +01:00
CucError::DateBeforeCcsdsEpoch(e) => {
write!(f, "date before ccsds epoch: {e}")
}
2022-12-08 15:22:19 +01:00
}
}
}
#[cfg(feature = "std")]
2024-03-25 13:42:18 +01:00
impl Error for CucError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
CucError::DateBeforeCcsdsEpoch(e) => Some(e),
_ => None,
}
}
}
impl From<DateBeforeCcsdsEpochError> for CucError {
fn from(e: DateBeforeCcsdsEpochError) -> Self {
Self::DateBeforeCcsdsEpoch(e)
}
}
2022-12-08 15:22:19 +01:00
2024-03-18 15:23:26 +01:00
/// Tuple object where the first value is the width of the counter and the second value
/// is the counter value.
2022-12-08 15:22:19 +01:00
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2024-03-18 15:23:26 +01:00
pub struct WidthCounterPair(pub u8, pub u32);
2024-03-18 15:14:40 +01:00
2022-12-09 13:50:04 +01:00
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2024-03-18 15:14:40 +01:00
pub struct FractionalPart {
pub resolution: FractionalResolution,
pub counter: u32,
}
impl FractionalPart {
pub const fn new(resolution: FractionalResolution, counter: u32) -> Self {
let div = fractional_res_to_div(resolution);
assert!(counter <= div, "invalid counter for resolution");
Self {
resolution,
counter,
}
}
/// An empty fractional part for second resolution only.
pub const fn new_with_seconds_resolution() -> Self {
Self::new(FractionalResolution::Seconds, 0)
}
/// Helper method which simply calls [Self::new_with_seconds_resolution].
pub const fn new_empty() -> Self {
Self::new_with_seconds_resolution()
}
pub fn new_checked(resolution: FractionalResolution, counter: u32) -> Option<Self> {
let div = fractional_res_to_div(resolution);
if counter > div {
return None;
}
Some(Self {
resolution,
counter,
})
}
pub fn resolution(&self) -> FractionalResolution {
self.resolution
}
pub fn counter(&self) -> u32 {
self.counter
}
pub fn no_fractional_part(&self) -> bool {
self.resolution == FractionalResolution::Seconds
}
}
2022-12-08 15:22:19 +01:00
2022-12-09 17:33:04 +01:00
/// This object is the abstraction for the CCSDS Unsegmented Time Code (CUC) using the CCSDS epoch
/// and a small preamble field.
2022-12-08 15:22:19 +01:00
///
2022-12-09 17:33:04 +01:00
/// It has the capability to generate and read timestamps as specified in the CCSDS 301.0-B-4
/// section 3.2 . The preamble field only has one byte, which allows a time code representation
/// through the year 2094. The time is represented as a simple binary counter starting from the
2024-03-18 15:14:40 +01:00
/// fixed CCSDS epoch 1958-01-01T00:00:00+00:00 using the TAI reference time scale. This time
/// code provides the advantage of being truly monotonic.
/// It is possible to provide subsecond accuracy using the fractional field with various available
/// [resolutions][FractionalResolution].
2022-12-09 17:33:04 +01:00
///
/// Having a preamble field of one byte limits the width of the counter
/// type (generally seconds) to 4 bytes and the width of the fractions type to 3 bytes. This limits
/// the maximum time stamp size to [MAX_CUC_LEN_SMALL_PREAMBLE] (8 bytes).
2022-12-10 18:09:00 +01:00
///
2024-03-18 15:14:40 +01:00
/// Please note that this object does not implement the [CcsdsTimeProvider] trait by itself because
/// leap seconds corrections need to be applied to support the trait methods. Instead, it needs
/// to be converted to a [CucTimeWithLeapSecs] object using the [Self::to_leap_sec_helper] method.
///
/// This time code is not UTC based. Conversion to UTC based times, for example a UNIX timestamp,
/// can be performed by subtracting the current number of leap seconds.
///
2022-12-10 18:09:00 +01:00
/// # Example
///
/// ```
2024-03-18 15:14:40 +01:00
/// use spacepackets::time::cuc::{FractionalResolution, CucTime};
/// use spacepackets::time::{TimeWriter, CcsdsTimeCode, TimeReader, CcsdsTimeProvider};
///
/// const LEAP_SECONDS: u32 = 37;
2022-12-10 18:09:00 +01:00
///
/// // Highest fractional resolution
/// let timestamp_now = CucTime::now(FractionalResolution::SixtyNs, LEAP_SECONDS)
2024-03-18 15:14:40 +01:00
/// .expect("creating cuc stamp failed");
2022-12-10 18:09:00 +01:00
/// let mut raw_stamp = [0; 16];
/// {
/// let written = timestamp_now.write_to_bytes(&mut raw_stamp).expect("writing timestamp failed");
2024-03-18 15:14:40 +01:00
/// assert_eq!((raw_stamp[0] >> 4) & 0b111, CcsdsTimeCode::CucCcsdsEpoch as u8);
2022-12-10 18:09:00 +01:00
/// // 1 byte preamble + 4 byte counter + 3 byte fractional part
/// assert_eq!(written, 8);
/// }
/// {
2024-03-18 15:14:40 +01:00
/// let read_result = CucTime::from_bytes(&raw_stamp);
2022-12-10 18:09:00 +01:00
/// assert!(read_result.is_ok());
/// let stamp_deserialized = read_result.unwrap();
/// assert_eq!(stamp_deserialized, timestamp_now);
/// }
/// ```
2022-12-08 15:22:19 +01:00
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2024-03-18 15:14:40 +01:00
pub struct CucTime {
2022-12-08 15:22:19 +01:00
pfield: u8,
counter: WidthCounterPair,
2024-03-18 15:14:40 +01:00
fractions: FractionalPart,
}
/// This object is a wrapper object around the [CucTime] object which also tracks
/// the leap seconds. This is necessary to implement the [CcsdsTimeProvider] trait.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CucTimeWithLeapSecs {
pub time: CucTime,
pub leap_seconds: u32,
}
impl CucTimeWithLeapSecs {
pub fn new(time: CucTime, leap_seconds: u32) -> Self {
Self { time, leap_seconds }
}
2022-12-08 15:22:19 +01:00
}
#[inline]
pub fn pfield_len(pfield: u8) -> usize {
if ((pfield >> 7) & 0b1) == 1 {
return 2;
}
1
}
2024-03-18 15:14:40 +01:00
impl CucTime {
2022-12-10 18:18:41 +01:00
/// Create a time provider with a four byte counter and no fractional part.
2022-12-09 16:51:48 +01:00
pub fn new(counter: u32) -> Self {
2022-12-08 15:22:19 +01:00
// These values are definitely valid, so it is okay to unwrap here.
2024-03-18 15:14:40 +01:00
Self::new_generic(
WidthCounterPair(4, counter),
FractionalPart::new_with_seconds_resolution(),
)
.unwrap()
2022-12-09 16:51:48 +01:00
}
2024-03-18 15:14:40 +01:00
/// Like [CucTime::new] but allow to supply a fractional part as well.
2022-12-10 18:18:41 +01:00
pub fn new_with_fractions(counter: u32, fractions: FractionalPart) -> Result<Self, CucError> {
2024-03-18 15:14:40 +01:00
Self::new_generic(WidthCounterPair(4, counter), fractions)
2022-12-10 18:18:41 +01:00
}
2022-12-10 16:35:00 +01:00
/// Fractions with a resolution of ~ 4 ms
2022-12-09 16:51:48 +01:00
pub fn new_with_coarse_fractions(counter: u32, subsec_fractions: u8) -> Self {
// These values are definitely valid, so it is okay to unwrap here.
Self::new_generic(
WidthCounterPair(4, counter),
2024-03-18 15:14:40 +01:00
FractionalPart {
resolution: FractionalResolution::FourMs,
counter: subsec_fractions as u32,
},
2022-12-09 16:51:48 +01:00
)
.unwrap()
}
2022-12-10 16:35:00 +01:00
/// Fractions with a resolution of ~ 16 us
pub fn new_with_medium_fractions(counter: u32, subsec_fractions: u16) -> Self {
2022-12-09 16:51:48 +01:00
// These values are definitely valid, so it is okay to unwrap here.
Self::new_generic(
WidthCounterPair(4, counter),
2024-03-18 15:14:40 +01:00
FractionalPart {
resolution: FractionalResolution::FifteenUs,
counter: subsec_fractions as u32,
},
2022-12-09 16:51:48 +01:00
)
.unwrap()
}
2022-12-10 18:23:47 +01:00
/// Fractions with a resolution of ~ 60 ns. The fractional part value is limited by the
/// 24 bits of the fractional field, so this function will fail with
/// [CucError::InvalidFractions] if the fractional value exceeds the value.
2022-12-09 16:51:48 +01:00
pub fn new_with_fine_fractions(counter: u32, subsec_fractions: u32) -> Result<Self, CucError> {
Self::new_generic(
WidthCounterPair(4, counter),
2024-03-18 15:14:40 +01:00
FractionalPart {
resolution: FractionalResolution::SixtyNs,
counter: subsec_fractions,
},
2022-12-09 16:51:48 +01:00
)
}
2022-12-09 13:50:04 +01:00
/// This function will return the current time as a CUC timestamp.
/// The counter width will always be set to 4 bytes because the normal CCSDS epoch will overflow
/// when using less than that.
2024-03-18 15:14:40 +01:00
///
/// The CUC timestamp uses TAI as the reference time system. Therefore, leap second corrections
/// must be applied on top of the UTC based time retrieved from the system in addition to the
/// conversion to the CCSDS epoch.
2022-12-09 13:50:04 +01:00
#[cfg(feature = "std")]
pub fn now(
2024-03-18 15:14:40 +01:00
fraction_resolution: FractionalResolution,
leap_seconds: u32,
) -> Result<Self, StdTimestampError> {
2022-12-09 13:50:04 +01:00
let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
2024-03-18 15:14:40 +01:00
let mut counter =
u32::try_from(unix_epoch_to_ccsds_epoch(now.as_secs() as i64)).map_err(|_| {
TimestampError::Cuc(CucError::InvalidCounter {
width: 4,
counter: now.as_secs(),
})
})?;
counter = counter
.checked_add(leap_seconds)
.ok_or(TimestampError::Cuc(CucError::LeapSecondCorrectionError))?;
2022-12-10 18:18:41 +01:00
if fraction_resolution == FractionalResolution::Seconds {
2024-03-18 15:14:40 +01:00
return Ok(Self::new(counter));
2022-12-10 18:18:41 +01:00
}
2022-12-09 16:51:48 +01:00
let fractions =
fractional_part_from_subsec_ns(fraction_resolution, now.subsec_nanos() as u64);
2024-03-18 15:14:40 +01:00
Self::new_with_fractions(counter, fractions)
2023-07-09 16:46:25 +02:00
.map_err(|e| StdTimestampError::Timestamp(e.into()))
2022-12-09 13:50:04 +01:00
}
2022-12-10 18:23:47 +01:00
/// Updates the current time stamp from the current time. The fractional field width remains
/// the same and will be updated accordingly.
2024-03-18 15:14:40 +01:00
///
/// The CUC timestamp uses TAI as the reference time system. Therefore, leap second corrections
/// must be applied on top of the UTC based time retrieved from the system in addition to the
/// conversion to the CCSDS epoch.
2022-12-09 16:51:48 +01:00
#[cfg(feature = "std")]
2024-03-18 15:14:40 +01:00
pub fn update_from_now(&mut self, leap_seconds: u32) -> Result<(), StdTimestampError> {
2022-12-09 16:51:48 +01:00
let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
2023-01-17 01:16:07 +01:00
self.counter.1 = unix_epoch_to_ccsds_epoch(now.as_secs() as i64) as u32;
2024-03-18 15:14:40 +01:00
self.counter.1 = self
.counter
.1
.checked_add(leap_seconds)
.ok_or(TimestampError::Cuc(CucError::LeapSecondCorrectionError))?;
if let FractionalResolution::Seconds = self.fractions.resolution {
2022-12-09 16:51:48 +01:00
self.fractions = fractional_part_from_subsec_ns(
2024-03-18 15:14:40 +01:00
self.fractions.resolution,
2022-12-09 16:51:48 +01:00
now.subsec_nanos() as u64,
);
2024-03-18 15:14:40 +01:00
return Ok(());
2022-12-09 16:51:48 +01:00
}
Ok(())
}
2024-03-18 15:14:40 +01:00
#[cfg(feature = "chrono")]
pub fn from_chrono_date_time(
dt: &chrono::DateTime<chrono::Utc>,
2023-01-17 01:16:07 +01:00
res: FractionalResolution,
2024-03-18 15:14:40 +01:00
leap_seconds: u32,
2024-03-25 13:42:18 +01:00
) -> Result<Self, CucError> {
2023-01-17 01:16:07 +01:00
// Year before CCSDS epoch is invalid.
if dt.year() < 1958 {
2024-03-25 13:42:18 +01:00
return Err(DateBeforeCcsdsEpochError(UnixTime::from(*dt)).into());
2023-01-17 01:16:07 +01:00
}
2024-03-18 15:14:40 +01:00
let counter = dt
.timestamp()
.checked_add(i64::from(leap_seconds))
2024-03-25 13:42:18 +01:00
.ok_or(CucError::LeapSecondCorrectionError)?;
2023-01-17 01:16:07 +01:00
Self::new_generic(
2024-03-18 15:14:40 +01:00
WidthCounterPair(4, counter as u32),
2023-01-17 01:16:07 +01:00
fractional_part_from_subsec_ns(res, dt.timestamp_subsec_nanos() as u64),
)
}
2023-12-06 01:12:33 +01:00
/// Generates a CUC timestamp from a UNIX timestamp with a width of 4. This width is able
/// to accomodate all possible UNIX timestamp values.
pub fn from_unix_time(
unix_time: &UnixTime,
2023-01-17 01:16:07 +01:00
res: FractionalResolution,
2024-03-18 15:14:40 +01:00
leap_seconds: u32,
2024-03-25 13:42:18 +01:00
) -> Result<Self, CucError> {
let counter = unix_epoch_to_ccsds_epoch(unix_time.secs);
2023-01-17 01:16:07 +01:00
// Negative CCSDS epoch is invalid.
2024-03-18 15:14:40 +01:00
if counter < 0 {
return Err(DateBeforeCcsdsEpochError(*unix_time).into());
2023-01-17 01:16:07 +01:00
}
2024-03-18 15:14:40 +01:00
// We already excluded negative values, so the conversion to u64 should always work.
let mut counter = u32::try_from(counter).map_err(|_| CucError::InvalidCounter {
width: 4,
counter: counter as u64,
})?;
counter = counter
.checked_add(leap_seconds)
2024-03-25 13:42:18 +01:00
.ok_or(CucError::LeapSecondCorrectionError)?;
2024-03-18 15:14:40 +01:00
let fractions =
fractional_part_from_subsec_ns(res, unix_time.subsec_millis() as u64 * 10_u64.pow(6));
2024-03-25 13:42:18 +01:00
Self::new_generic(WidthCounterPair(4, counter as u32), fractions)
2023-01-17 01:16:07 +01:00
}
2024-03-18 15:23:26 +01:00
/// Most generic constructor which allows full configurability for the counter and for the
/// fractions.
pub fn new_generic(
width_and_counter: WidthCounterPair,
fractions: FractionalPart,
) -> Result<Self, CucError> {
Self::verify_counter_width(width_and_counter.0)?;
if width_and_counter.1 > (2u64.pow(width_and_counter.0 as u32 * 8) - 1) as u32 {
return Err(CucError::InvalidCounter {
width: width_and_counter.0,
counter: width_and_counter.1.into(),
});
}
Self::verify_fractions_value(fractions)?;
Ok(Self {
pfield: Self::build_p_field(width_and_counter.0, fractions.resolution),
counter: width_and_counter,
fractions,
})
2024-03-18 15:14:40 +01:00
}
pub fn ccsds_time_code(&self) -> CcsdsTimeCode {
CcsdsTimeCode::CucCcsdsEpoch
2022-12-10 18:18:41 +01:00
}
2022-12-09 13:50:04 +01:00
pub fn width_counter_pair(&self) -> WidthCounterPair {
self.counter
}
2024-03-18 15:23:26 +01:00
pub fn counter_width(&self) -> u8 {
2023-12-05 15:26:34 +01:00
self.counter.0
}
pub fn counter(&self) -> u32 {
self.counter.1
}
2024-03-18 15:14:40 +01:00
/// Subsecond fractional part of the CUC time.
pub fn fractions(&self) -> FractionalPart {
2022-12-09 13:50:04 +01:00
self.fractions
}
2024-03-18 15:14:40 +01:00
pub fn to_leap_sec_helper(&self, leap_seconds: u32) -> CucTimeWithLeapSecs {
CucTimeWithLeapSecs::new(*self, leap_seconds)
}
2022-12-09 13:50:04 +01:00
pub fn set_fractions(&mut self, fractions: FractionalPart) -> Result<(), CucError> {
2022-12-08 15:22:19 +01:00
Self::verify_fractions_value(fractions)?;
2024-03-18 15:14:40 +01:00
self.fractions = fractions;
2022-12-08 15:22:19 +01:00
self.update_p_field_fractions();
Ok(())
}
2022-12-09 16:51:48 +01:00
/// Set a fractional resolution. Please note that this function will reset the fractional value
/// to 0 if the resolution changes.
pub fn set_fractional_resolution(&mut self, res: FractionalResolution) {
if res == FractionalResolution::Seconds {
2024-03-18 15:14:40 +01:00
self.fractions = FractionalPart::new_with_seconds_resolution();
2022-12-09 16:51:48 +01:00
}
2024-03-18 15:14:40 +01:00
if res != self.fractions().resolution() {
self.fractions = FractionalPart::new(res, 0);
2022-12-09 16:51:48 +01:00
}
}
2024-03-18 15:14:40 +01:00
fn build_p_field(counter_width: u8, resolution: FractionalResolution) -> u8 {
let mut pfield = P_FIELD_BASE;
2022-12-10 18:23:47 +01:00
if !(1..=4).contains(&counter_width) {
// Okay to panic here, this function is private and all input values should
// have been sanitized
panic!("invalid counter width {} for cuc timestamp", counter_width);
}
pfield |= (counter_width - 1) << 2;
2024-03-18 15:14:40 +01:00
if resolution != FractionalResolution::Seconds {
if !(1..=3).contains(&(resolution as u8)) {
2022-12-10 18:23:47 +01:00
// Okay to panic here, this function is private and all input values should
// have been sanitized
2024-03-18 15:14:40 +01:00
panic!("invalid fractions width {:?} for cuc timestamp", resolution);
2022-12-10 18:23:47 +01:00
}
2024-03-18 15:14:40 +01:00
pfield |= resolution as u8;
2022-12-10 18:23:47 +01:00
}
pfield
}
fn update_p_field_fractions(&mut self) {
self.pfield &= !(0b11);
2024-03-18 15:14:40 +01:00
self.pfield |= self.fractions.resolution() as u8;
2022-12-10 18:23:47 +01:00
}
#[inline]
pub fn len_cntr_from_pfield(pfield: u8) -> u8 {
((pfield >> 2) & 0b11) + 1
}
#[inline]
pub fn len_fractions_from_pfield(pfield: u8) -> u8 {
pfield & 0b11
}
2023-01-15 17:42:23 +01:00
#[inline]
2024-03-18 15:14:40 +01:00
pub fn unix_secs(&self, leap_seconds: u32) -> i64 {
2023-01-17 01:16:07 +01:00
ccsds_epoch_to_unix_epoch(self.counter.1 as i64)
2024-03-18 15:14:40 +01:00
.checked_sub(leap_seconds as i64)
.unwrap()
}
#[inline]
pub fn subsec_millis(&self) -> u16 {
(self.subsec_nanos() / 1_000_000) as u16
2023-01-15 17:42:23 +01:00
}
2022-12-10 18:23:47 +01:00
/// This returns the length of the individual components of the CUC timestamp in addition
/// to the total size.
///
/// This function will return a tuple where the first value is the byte width of the
/// counter, the second value is the byte width of the fractional part, and the third
/// components is the total size.
pub fn len_components_and_total_from_pfield(pfield: u8) -> (u8, u8, usize) {
let base_len: usize = 1;
let cntr_len = Self::len_cntr_from_pfield(pfield);
let fractions_len = Self::len_fractions_from_pfield(pfield);
(
cntr_len,
fractions_len,
base_len + cntr_len as usize + fractions_len as usize,
)
}
pub fn len_packed_from_pfield(pfield: u8) -> usize {
let mut base_len: usize = 1;
base_len += Self::len_cntr_from_pfield(pfield) as usize;
base_len += Self::len_fractions_from_pfield(pfield) as usize;
base_len
}
/// Verifies the raw width parameter.
fn verify_counter_width(width: u8) -> Result<(), CucError> {
if width == 0 || width > 4 {
return Err(CucError::InvalidCounterWidth(width));
}
Ok(())
}
fn verify_fractions_value(val: FractionalPart) -> Result<(), CucError> {
2024-03-18 15:14:40 +01:00
if val.counter > 2u32.pow((val.resolution as u32) * 8) - 1 {
2023-12-05 15:05:00 +01:00
return Err(CucError::InvalidFractions {
2024-03-18 15:14:40 +01:00
resolution: val.resolution,
value: val.counter as u64,
2023-12-05 15:05:00 +01:00
});
2022-12-10 18:23:47 +01:00
}
Ok(())
}
2024-03-18 15:14:40 +01:00
fn len_as_bytes(&self) -> usize {
Self::len_packed_from_pfield(self.pfield)
}
fn subsec_nanos(&self) -> u32 {
if self.fractions.resolution() == FractionalResolution::Seconds {
return 0;
}
// Rounding down here is the correct approach.
convert_fractional_part_to_ns(self.fractions) as u32
}
2022-12-08 15:22:19 +01:00
}
2024-03-18 15:14:40 +01:00
impl TimeReader for CucTime {
fn from_bytes(buf: &[u8]) -> Result<Self, TimestampError> {
2022-12-08 15:22:19 +01:00
if buf.len() < MIN_CUC_LEN {
2023-07-09 16:46:25 +02:00
return Err(TimestampError::ByteConversion(
2023-08-18 10:09:32 +02:00
ByteConversionError::FromSliceTooSmall {
2022-12-08 15:22:19 +01:00
expected: MIN_CUC_LEN,
found: buf.len(),
2023-08-18 10:09:32 +02:00
},
2022-12-08 15:22:19 +01:00
));
}
2022-12-10 16:35:00 +01:00
match ccsds_time_code_from_p_field(buf[0]) {
Ok(code) => {
2024-03-18 15:14:40 +01:00
if code != CcsdsTimeCode::CucCcsdsEpoch {
2023-08-28 17:10:45 +02:00
return Err(TimestampError::InvalidTimeCode {
2024-03-18 15:14:40 +01:00
expected: CcsdsTimeCode::CucCcsdsEpoch,
2023-08-28 17:10:45 +02:00
found: code as u8,
});
2022-12-10 16:35:00 +01:00
}
}
Err(raw) => {
2023-08-28 17:10:45 +02:00
return Err(TimestampError::InvalidTimeCode {
2024-03-18 15:14:40 +01:00
expected: CcsdsTimeCode::CucCcsdsEpoch,
2023-08-28 17:10:45 +02:00
found: raw,
});
2022-12-10 16:35:00 +01:00
}
}
2022-12-08 15:22:19 +01:00
let (cntr_len, fractions_len, total_len) =
Self::len_components_and_total_from_pfield(buf[0]);
if buf.len() < total_len {
2023-07-09 16:46:25 +02:00
return Err(TimestampError::ByteConversion(
2023-08-18 10:09:32 +02:00
ByteConversionError::FromSliceTooSmall {
2022-12-08 15:22:19 +01:00
expected: total_len,
found: buf.len(),
2023-08-18 10:09:32 +02:00
},
2022-12-08 15:22:19 +01:00
));
}
let mut current_idx = 1;
let counter = match cntr_len {
1 => buf[current_idx] as u32,
2 => u16::from_be_bytes(buf[current_idx..current_idx + 2].try_into().unwrap()) as u32,
3 => {
let mut tmp_buf: [u8; 4] = [0; 4];
tmp_buf[1..4].copy_from_slice(&buf[current_idx..current_idx + 3]);
2022-12-19 00:01:07 +01:00
u32::from_be_bytes(tmp_buf)
2022-12-08 15:22:19 +01:00
}
2022-12-19 00:01:07 +01:00
4 => u32::from_be_bytes(buf[current_idx..current_idx + 4].try_into().unwrap()),
2022-12-08 15:22:19 +01:00
_ => panic!("unreachable match arm"),
};
current_idx += cntr_len as usize;
2024-03-18 15:14:40 +01:00
let mut fractions = FractionalPart::new_with_seconds_resolution();
2022-12-08 15:22:19 +01:00
if fractions_len > 0 {
match fractions_len {
2022-12-09 13:50:04 +01:00
1 => {
2024-03-18 15:14:40 +01:00
fractions = FractionalPart::new(
2022-12-09 13:50:04 +01:00
fractions_len.try_into().unwrap(),
buf[current_idx] as u32,
2024-03-18 15:14:40 +01:00
)
2022-12-09 13:50:04 +01:00
}
2022-12-08 15:22:19 +01:00
2 => {
2024-03-18 15:14:40 +01:00
fractions = FractionalPart::new(
2022-12-09 13:50:04 +01:00
fractions_len.try_into().unwrap(),
2022-12-08 15:22:19 +01:00
u16::from_be_bytes(buf[current_idx..current_idx + 2].try_into().unwrap())
as u32,
2024-03-18 15:14:40 +01:00
)
2022-12-08 15:22:19 +01:00
}
3 => {
let mut tmp_buf: [u8; 4] = [0; 4];
tmp_buf[1..4].copy_from_slice(&buf[current_idx..current_idx + 3]);
2024-03-18 15:14:40 +01:00
fractions = FractionalPart::new(
2022-12-09 13:50:04 +01:00
fractions_len.try_into().unwrap(),
2022-12-19 00:01:07 +01:00
u32::from_be_bytes(tmp_buf),
2024-03-18 15:14:40 +01:00
)
2022-12-08 15:22:19 +01:00
}
_ => panic!("unreachable match arm"),
}
}
2022-12-09 16:51:48 +01:00
let provider = Self::new_generic(WidthCounterPair(cntr_len, counter), fractions)?;
2022-12-08 15:22:19 +01:00
Ok(provider)
}
}
2024-03-18 15:14:40 +01:00
impl TimeWriter for CucTime {
2022-12-08 15:22:19 +01:00
fn write_to_bytes(&self, bytes: &mut [u8]) -> Result<usize, TimestampError> {
// Cross check the sizes of the counters against byte widths in the ctor
2022-12-10 16:42:24 +01:00
if bytes.len() < self.len_as_bytes() {
2023-07-09 16:46:25 +02:00
return Err(TimestampError::ByteConversion(
2023-08-18 10:09:32 +02:00
ByteConversionError::ToSliceTooSmall {
2022-12-08 15:22:19 +01:00
found: bytes.len(),
2022-12-10 16:42:24 +01:00
expected: self.len_as_bytes(),
2023-08-18 10:09:32 +02:00
},
2022-12-08 15:22:19 +01:00
));
}
bytes[0] = self.pfield;
let mut current_idx: usize = 1;
match self.counter.0 {
1 => {
bytes[current_idx] = self.counter.1 as u8;
}
2 => {
bytes[current_idx..current_idx + 2]
.copy_from_slice(&(self.counter.1 as u16).to_be_bytes());
}
3 => {
bytes[current_idx..current_idx + 3]
.copy_from_slice(&self.counter.1.to_be_bytes()[1..4]);
}
4 => {
bytes[current_idx..current_idx + 4].copy_from_slice(&self.counter.1.to_be_bytes());
}
// Should never happen
_ => panic!("invalid counter width value"),
}
current_idx += self.counter.0 as usize;
2024-03-18 15:14:40 +01:00
match self.fractions.resolution() {
FractionalResolution::FourMs => bytes[current_idx] = self.fractions.counter as u8,
FractionalResolution::FifteenUs => bytes[current_idx..current_idx + 2]
.copy_from_slice(&(self.fractions.counter as u16).to_be_bytes()),
FractionalResolution::SixtyNs => bytes[current_idx..current_idx + 3]
.copy_from_slice(&self.fractions.counter.to_be_bytes()[1..4]),
_ => (),
2022-12-08 15:22:19 +01:00
}
2024-03-18 15:14:40 +01:00
current_idx += self.fractions.resolution as usize;
2022-12-08 15:22:19 +01:00
Ok(current_idx)
}
2024-02-05 15:04:29 +01:00
fn len_written(&self) -> usize {
self.len_as_bytes()
}
2022-12-08 15:22:19 +01:00
}
2022-12-09 13:50:04 +01:00
2024-03-18 15:14:40 +01:00
impl CcsdsTimeProvider for CucTimeWithLeapSecs {
2022-12-09 17:21:45 +01:00
fn len_as_bytes(&self) -> usize {
2024-03-18 15:14:40 +01:00
self.time.len_as_bytes()
2022-12-09 17:21:45 +01:00
}
fn p_field(&self) -> (usize, [u8; 2]) {
2024-03-18 15:14:40 +01:00
(1, [self.time.pfield, 0])
2022-12-09 17:21:45 +01:00
}
2024-03-18 15:14:40 +01:00
fn ccdsd_time_code(&self) -> CcsdsTimeCode {
self.time.ccsds_time_code()
2022-12-09 17:21:45 +01:00
}
2024-03-18 15:14:40 +01:00
fn unix_secs(&self) -> i64 {
self.time.unix_secs(self.leap_seconds)
2023-01-15 17:42:23 +01:00
}
2024-03-18 15:14:40 +01:00
fn subsec_nanos(&self) -> u32 {
self.time.subsec_nanos()
2022-12-09 17:21:45 +01:00
}
}
2024-03-18 15:14:40 +01:00
// TODO: Introduce more overflow checks here.
fn get_time_values_after_duration_addition(
time: &CucTime,
duration: Duration,
2024-03-18 15:14:40 +01:00
) -> (u32, FractionalPart) {
let mut new_counter = time.counter.1;
let subsec_nanos = duration.subsec_nanos();
let mut increment_counter = |amount: u32| {
let mut sum: u64 = 0;
let mut counter_inc_handler = |max_val: u64| {
sum = new_counter as u64 + amount as u64;
if sum >= max_val {
new_counter = (sum % max_val) as u32;
return;
}
new_counter = sum as u32;
};
2024-03-18 15:14:40 +01:00
match time.counter.0 {
1 => counter_inc_handler(u8::MAX as u64),
2 => counter_inc_handler(u16::MAX as u64),
3 => counter_inc_handler((2_u32.pow(24) - 1) as u64),
4 => counter_inc_handler(u32::MAX as u64),
_ => {
// Should never happen
panic!("invalid counter width")
}
}
};
2024-03-18 15:14:40 +01:00
let resolution = time.fractions().resolution();
let fractional_increment = fractional_part_from_subsec_ns(resolution, subsec_nanos as u64);
let mut fractional_part = FractionalPart::new_with_seconds_resolution();
if resolution != FractionalResolution::Seconds {
let mut new_fractions = time.fractions().counter() + fractional_increment.counter;
let max_fractions = fractional_res_to_div(resolution);
if new_fractions > max_fractions {
increment_counter(1);
new_fractions -= max_fractions;
}
2024-03-18 15:14:40 +01:00
fractional_part = FractionalPart {
resolution,
counter: new_fractions,
}
}
2023-01-17 01:16:07 +01:00
increment_counter(duration.as_secs() as u32);
(new_counter, fractional_part)
}
2024-03-18 15:14:40 +01:00
impl AddAssign<Duration> for CucTime {
fn add_assign(&mut self, duration: Duration) {
let (new_counter, new_fractional_part) =
2024-03-18 15:14:40 +01:00
get_time_values_after_duration_addition(self, duration);
self.counter.1 = new_counter;
2024-03-18 15:14:40 +01:00
self.fractions = new_fractional_part;
}
}
2024-03-18 15:14:40 +01:00
impl Add<Duration> for CucTime {
type Output = Self;
fn add(self, duration: Duration) -> Self::Output {
let (new_counter, new_fractional_part) =
2024-03-18 15:14:40 +01:00
get_time_values_after_duration_addition(&self, duration);
// The generated fractional part should always be valid, so its okay to unwrap here.
Self::new_with_fractions(new_counter, new_fractional_part).unwrap()
}
}
2024-03-18 15:14:40 +01:00
impl Add<Duration> for &CucTime {
type Output = CucTime;
2023-01-21 14:50:35 +01:00
fn add(self, duration: Duration) -> Self::Output {
let (new_counter, new_fractional_part) =
2024-03-18 15:14:40 +01:00
get_time_values_after_duration_addition(self, duration);
// The generated fractional part should always be valid, so its okay to unwrap here.
Self::Output::new_with_fractions(new_counter, new_fractional_part).unwrap()
2023-01-21 14:50:35 +01:00
}
}
2022-12-09 13:50:04 +01:00
#[cfg(test)]
mod tests {
2024-03-18 15:14:40 +01:00
use crate::time::{UnixTime, DAYS_CCSDS_TO_UNIX, SECONDS_PER_DAY};
2022-12-09 16:51:48 +01:00
use super::*;
2023-12-05 15:05:00 +01:00
use alloc::string::ToString;
2024-03-18 15:14:40 +01:00
use chrono::{Datelike, TimeZone, Timelike};
2022-12-09 17:21:45 +01:00
#[allow(unused_imports)]
use std::println;
2022-12-09 13:50:04 +01:00
2024-03-18 15:14:40 +01:00
const LEAP_SECONDS: u32 = 37;
2022-12-09 13:50:04 +01:00
#[test]
2022-12-09 17:21:45 +01:00
fn test_basic_zero_epoch() {
2024-03-18 15:14:40 +01:00
// Do not include leap second corrections, which do not apply to dates before 1972.
let zero_cuc = CucTime::new(0);
2022-12-10 17:39:15 +01:00
assert_eq!(zero_cuc.len_as_bytes(), 5);
2024-03-18 15:23:26 +01:00
assert_eq!(zero_cuc.counter_width(), zero_cuc.width_counter_pair().0);
2023-12-05 15:26:34 +01:00
assert_eq!(zero_cuc.counter(), zero_cuc.width_counter_pair().1);
2024-03-18 15:14:40 +01:00
let ccsds_cuc = zero_cuc.to_leap_sec_helper(0);
assert_eq!(ccsds_cuc.ccdsd_time_code(), CcsdsTimeCode::CucCcsdsEpoch);
2022-12-09 17:21:45 +01:00
let counter = zero_cuc.width_counter_pair();
assert_eq!(counter.0, 4);
assert_eq!(counter.1, 0);
2024-03-18 15:14:40 +01:00
let fractions = zero_cuc.fractions();
assert_eq!(fractions, FractionalPart::new_with_seconds_resolution());
let dt = ccsds_cuc.chrono_date_time();
if let chrono::LocalResult::Single(dt) = dt {
assert_eq!(dt.year(), 1958);
assert_eq!(dt.month(), 1);
assert_eq!(dt.day(), 1);
assert_eq!(dt.hour(), 0);
assert_eq!(dt.minute(), 0);
assert_eq!(dt.second(), 0);
}
2022-12-09 17:21:45 +01:00
}
#[test]
2022-12-10 16:35:00 +01:00
fn test_write_no_fractions() {
2022-12-09 17:21:45 +01:00
let mut buf: [u8; 16] = [0; 16];
2024-03-18 15:14:40 +01:00
let zero_cuc =
CucTime::new_generic(WidthCounterPair(4, 0x20102030), FractionalPart::new_empty());
2022-12-09 17:21:45 +01:00
assert!(zero_cuc.is_ok());
let zero_cuc = zero_cuc.unwrap();
let res = zero_cuc.write_to_bytes(&mut buf);
assert!(res.is_ok());
2024-03-18 15:14:40 +01:00
assert_eq!(zero_cuc.subsec_nanos(), 0);
2022-12-10 17:39:15 +01:00
assert_eq!(zero_cuc.len_as_bytes(), 5);
assert_eq!(pfield_len(buf[0]), 1);
2022-12-09 17:21:45 +01:00
let written = res.unwrap();
assert_eq!(written, 5);
assert_eq!((buf[0] >> 7) & 0b1, 0);
let time_code = ccsds_time_code_from_p_field(buf[0]);
assert!(time_code.is_ok());
2024-03-18 15:14:40 +01:00
assert_eq!(time_code.unwrap(), CcsdsTimeCode::CucCcsdsEpoch);
2022-12-09 17:21:45 +01:00
assert_eq!((buf[0] >> 2) & 0b11, 0b11);
assert_eq!(buf[0] & 0b11, 0);
let raw_counter = u32::from_be_bytes(buf[1..5].try_into().unwrap());
assert_eq!(raw_counter, 0x20102030);
2022-12-10 16:35:00 +01:00
assert_eq!(buf[5], 0);
}
2022-12-10 17:39:15 +01:00
#[test]
fn test_datetime_now() {
2024-03-18 15:14:40 +01:00
let now = chrono::Utc::now();
let cuc_now = CucTime::now(FractionalResolution::SixtyNs, LEAP_SECONDS);
2022-12-10 17:39:15 +01:00
assert!(cuc_now.is_ok());
let cuc_now = cuc_now.unwrap();
2024-03-18 15:14:40 +01:00
let ccsds_cuc = cuc_now.to_leap_sec_helper(LEAP_SECONDS);
let dt_opt = ccsds_cuc.chrono_date_time();
if let chrono::LocalResult::Single(dt) = dt_opt {
let diff = dt - now;
assert!(diff.num_milliseconds() < 1000);
println!("datetime from cuc: {}", dt);
println!("datetime now: {}", now);
} else {
panic!("date time creation from now failed")
}
2022-12-10 17:39:15 +01:00
}
2022-12-10 16:35:00 +01:00
#[test]
fn test_read_no_fractions() {
let mut buf: [u8; 16] = [0; 16];
2024-03-18 15:14:40 +01:00
let zero_cuc = CucTime::new_generic(
WidthCounterPair(4, 0x20102030),
FractionalPart::new_with_seconds_resolution(),
)
.unwrap();
2022-12-10 16:35:00 +01:00
zero_cuc.write_to_bytes(&mut buf).unwrap();
2024-03-18 15:14:40 +01:00
let cuc_read_back = CucTime::from_bytes(&buf).expect("reading cuc timestamp failed");
2022-12-10 16:35:00 +01:00
assert_eq!(cuc_read_back, zero_cuc);
assert_eq!(cuc_read_back.width_counter_pair().1, 0x20102030);
2024-03-18 15:14:40 +01:00
assert_eq!(cuc_read_back.fractions(), FractionalPart::new_empty());
2022-12-10 16:35:00 +01:00
}
#[test]
fn invalid_read_len() {
2022-12-10 17:39:15 +01:00
let mut buf: [u8; 16] = [0; 16];
2022-12-10 16:35:00 +01:00
for i in 0..2 {
2024-03-18 15:14:40 +01:00
let res = CucTime::from_bytes(&buf[0..i]);
2022-12-10 16:35:00 +01:00
assert!(res.is_err());
let err = res.unwrap_err();
2023-08-18 10:09:32 +02:00
if let TimestampError::ByteConversion(ByteConversionError::FromSliceTooSmall {
found,
expected,
}) = err
{
assert_eq!(found, i);
assert_eq!(expected, 2);
2022-12-10 16:35:00 +01:00
}
}
2024-03-18 15:14:40 +01:00
let large_stamp = CucTime::new_with_fine_fractions(22, 300).unwrap();
2022-12-10 17:39:15 +01:00
large_stamp.write_to_bytes(&mut buf).unwrap();
for i in 2..large_stamp.len_as_bytes() - 1 {
2024-03-18 15:14:40 +01:00
let res = CucTime::from_bytes(&buf[0..i]);
2022-12-10 17:39:15 +01:00
assert!(res.is_err());
let err = res.unwrap_err();
2023-08-18 10:09:32 +02:00
if let TimestampError::ByteConversion(ByteConversionError::FromSliceTooSmall {
found,
expected,
}) = err
{
assert_eq!(found, i);
assert_eq!(expected, large_stamp.len_as_bytes());
2022-12-10 17:39:15 +01:00
}
}
}
#[test]
fn write_and_read_tiny_stamp() {
let mut buf = [0; 2];
2024-03-18 15:14:40 +01:00
let cuc = CucTime::new_generic(WidthCounterPair(1, 200), FractionalPart::new_empty());
2022-12-10 17:39:15 +01:00
assert!(cuc.is_ok());
let cuc = cuc.unwrap();
assert_eq!(cuc.len_as_bytes(), 2);
let res = cuc.write_to_bytes(&mut buf);
assert!(res.is_ok());
let written = res.unwrap();
assert_eq!(written, 2);
assert_eq!(buf[1], 200);
2024-03-18 15:14:40 +01:00
let cuc_read_back = CucTime::from_bytes(&buf);
2022-12-10 17:39:15 +01:00
assert!(cuc_read_back.is_ok());
let cuc_read_back = cuc_read_back.unwrap();
assert_eq!(cuc_read_back, cuc);
}
#[test]
fn write_slightly_larger_stamp() {
let mut buf = [0; 4];
2024-03-18 15:14:40 +01:00
let cuc = CucTime::new_generic(WidthCounterPair(2, 40000), FractionalPart::new_empty());
2022-12-10 17:39:15 +01:00
assert!(cuc.is_ok());
let cuc = cuc.unwrap();
assert_eq!(cuc.len_as_bytes(), 3);
let res = cuc.write_to_bytes(&mut buf);
assert!(res.is_ok());
let written = res.unwrap();
assert_eq!(written, 3);
assert_eq!(u16::from_be_bytes(buf[1..3].try_into().unwrap()), 40000);
2024-03-18 15:14:40 +01:00
let cuc_read_back = CucTime::from_bytes(&buf);
2022-12-10 17:39:15 +01:00
assert!(cuc_read_back.is_ok());
let cuc_read_back = cuc_read_back.unwrap();
assert_eq!(cuc_read_back, cuc);
}
#[test]
fn invalid_buf_len_for_read() {}
2023-12-05 15:05:00 +01:00
2022-12-10 17:39:15 +01:00
#[test]
fn write_read_three_byte_cntr_stamp() {
let mut buf = [0; 4];
2024-03-18 15:14:40 +01:00
let cuc = CucTime::new_generic(
WidthCounterPair(3, 2_u32.pow(24) - 2),
FractionalPart::new_empty(),
);
2022-12-10 17:39:15 +01:00
assert!(cuc.is_ok());
let cuc = cuc.unwrap();
assert_eq!(cuc.len_as_bytes(), 4);
let res = cuc.write_to_bytes(&mut buf);
assert!(res.is_ok());
let written = res.unwrap();
assert_eq!(written, 4);
let mut temp_buf = [0; 4];
temp_buf[1..4].copy_from_slice(&buf[1..4]);
assert_eq!(u32::from_be_bytes(temp_buf), 2_u32.pow(24) - 2);
2024-03-18 15:14:40 +01:00
let cuc_read_back = CucTime::from_bytes(&buf);
2022-12-10 17:39:15 +01:00
assert!(cuc_read_back.is_ok());
let cuc_read_back = cuc_read_back.unwrap();
assert_eq!(cuc_read_back, cuc);
2022-12-09 17:21:45 +01:00
}
2022-12-09 13:50:04 +01:00
2022-12-10 17:39:15 +01:00
#[test]
fn test_write_invalid_buf() {
let mut buf: [u8; 16] = [0; 16];
2024-03-18 15:14:40 +01:00
let res = CucTime::new_with_fine_fractions(0, 0);
2022-12-10 17:39:15 +01:00
let cuc = res.unwrap();
for i in 0..cuc.len_as_bytes() - 1 {
let err = cuc.write_to_bytes(&mut buf[0..i]);
assert!(err.is_err());
let err = err.unwrap_err();
2023-08-18 10:09:32 +02:00
if let TimestampError::ByteConversion(ByteConversionError::ToSliceTooSmall {
found,
expected,
}) = err
{
assert_eq!(expected, cuc.len_as_bytes());
assert_eq!(found, i);
2022-12-10 17:39:15 +01:00
} else {
panic!("unexpected error: {}", err);
}
}
}
2022-12-10 16:35:00 +01:00
#[test]
fn invalid_ccsds_stamp_type() {
let mut buf: [u8; 16] = [0; 16];
2024-03-18 15:14:40 +01:00
buf[0] |= (CcsdsTimeCode::CucAgencyEpoch as u8) << 4;
let res = CucTime::from_bytes(&buf);
2022-12-10 16:35:00 +01:00
assert!(res.is_err());
let err = res.unwrap_err();
2023-08-28 17:24:54 +02:00
if let TimestampError::InvalidTimeCode { expected, found } = err {
2024-03-18 15:14:40 +01:00
assert_eq!(expected, CcsdsTimeCode::CucCcsdsEpoch);
assert_eq!(found, CcsdsTimeCode::CucAgencyEpoch as u8);
2022-12-10 16:35:00 +01:00
} else {
panic!("unexpected error: {}", err);
}
}
#[test]
fn test_write_with_coarse_fractions() {
let mut buf: [u8; 16] = [0; 16];
2024-03-18 15:14:40 +01:00
let cuc = CucTime::new_with_coarse_fractions(0x30201060, 120);
assert_eq!(cuc.fractions().counter(), 120);
assert_eq!(cuc.fractions().resolution(), FractionalResolution::FourMs);
2022-12-10 16:35:00 +01:00
let res = cuc.write_to_bytes(&mut buf);
assert!(res.is_ok());
let written = res.unwrap();
assert_eq!(written, 6);
assert_eq!(buf[5], 120);
assert_eq!(buf[6], 0);
2022-12-10 17:39:15 +01:00
assert_eq!(
u32::from_be_bytes(buf[1..5].try_into().unwrap()),
0x30201060
);
2022-12-10 16:35:00 +01:00
}
#[test]
fn test_read_with_coarse_fractions() {
let mut buf: [u8; 16] = [0; 16];
2024-03-18 15:14:40 +01:00
let cuc = CucTime::new_with_coarse_fractions(0x30201060, 120);
2022-12-10 16:35:00 +01:00
let res = cuc.write_to_bytes(&mut buf);
assert!(res.is_ok());
2024-03-18 15:14:40 +01:00
let res = CucTime::from_bytes(&buf);
2022-12-10 16:35:00 +01:00
assert!(res.is_ok());
let read_back = res.unwrap();
assert_eq!(read_back, cuc);
}
#[test]
fn test_write_with_medium_fractions() {
let mut buf: [u8; 16] = [0; 16];
2024-03-18 15:14:40 +01:00
let cuc = CucTime::new_with_medium_fractions(0x30303030, 30000);
2022-12-10 16:35:00 +01:00
let res = cuc.write_to_bytes(&mut buf);
assert!(res.is_ok());
let written = res.unwrap();
assert_eq!(written, 7);
assert_eq!(u16::from_be_bytes(buf[5..7].try_into().unwrap()), 30000);
assert_eq!(buf[7], 0);
}
2022-12-10 16:42:24 +01:00
#[test]
fn test_read_with_medium_fractions() {
let mut buf: [u8; 16] = [0; 16];
2024-03-18 15:14:40 +01:00
let cuc = CucTime::new_with_medium_fractions(0x30303030, 30000);
2022-12-10 16:42:24 +01:00
let res = cuc.write_to_bytes(&mut buf);
assert!(res.is_ok());
2024-03-18 15:14:40 +01:00
let res = CucTime::from_bytes(&buf);
2022-12-10 16:42:24 +01:00
assert!(res.is_ok());
let cuc_read_back = res.unwrap();
assert_eq!(cuc_read_back, cuc);
}
2022-12-10 16:35:00 +01:00
#[test]
fn test_write_with_fine_fractions() {
let mut buf: [u8; 16] = [0; 16];
2024-03-18 15:14:40 +01:00
let cuc = CucTime::new_with_fine_fractions(0x30303030, u16::MAX as u32 + 60000);
2022-12-10 16:35:00 +01:00
assert!(cuc.is_ok());
let cuc = cuc.unwrap();
let res = cuc.write_to_bytes(&mut buf);
let written = res.unwrap();
assert_eq!(written, 8);
let mut dummy_buf: [u8; 4] = [0; 4];
dummy_buf[1..4].copy_from_slice(&buf[5..8]);
assert_eq!(u32::from_be_bytes(dummy_buf), u16::MAX as u32 + 60000);
assert_eq!(buf[8], 0);
}
2022-12-10 16:42:24 +01:00
#[test]
fn test_read_with_fine_fractions() {
let mut buf: [u8; 16] = [0; 16];
2024-03-18 15:14:40 +01:00
let cuc = CucTime::new_with_fine_fractions(0x30303030, u16::MAX as u32 + 60000);
2022-12-10 16:42:24 +01:00
assert!(cuc.is_ok());
let cuc = cuc.unwrap();
let res = cuc.write_to_bytes(&mut buf);
assert!(res.is_ok());
2024-03-18 15:14:40 +01:00
let res = CucTime::from_bytes(&buf);
2022-12-10 16:42:24 +01:00
assert!(res.is_ok());
let cuc_read_back = res.unwrap();
assert_eq!(cuc_read_back, cuc);
}
2022-12-10 16:35:00 +01:00
2022-12-09 13:50:04 +01:00
#[test]
fn test_fractional_converter() {
2024-03-18 15:14:40 +01:00
let ns = convert_fractional_part_to_ns(FractionalPart {
resolution: FractionalResolution::FourMs,
counter: 2,
});
2022-12-09 13:50:04 +01:00
// The formula for this is 2/255 * 10e9 = 7.843.137.
assert_eq!(ns, 7843137);
// This is the largest value we should be able to pass without this function panicking.
2024-03-18 15:14:40 +01:00
let ns = convert_fractional_part_to_ns(FractionalPart {
resolution: FractionalResolution::SixtyNs,
counter: 2_u32.pow(24) - 2,
});
2022-12-09 13:50:04 +01:00
assert_eq!(ns, 999999940);
}
#[test]
#[should_panic]
fn test_fractional_converter_invalid_input() {
2024-03-18 15:14:40 +01:00
convert_fractional_part_to_ns(FractionalPart {
resolution: FractionalResolution::FourMs,
counter: 256,
});
2022-12-09 13:50:04 +01:00
}
#[test]
#[should_panic]
fn test_fractional_converter_invalid_input_2() {
2024-03-18 15:14:40 +01:00
convert_fractional_part_to_ns(FractionalPart {
resolution: FractionalResolution::SixtyNs,
counter: 2_u32.pow(32) - 1,
});
2022-12-09 13:50:04 +01:00
}
2022-12-09 16:51:48 +01:00
#[test]
fn fractional_part_formula() {
2024-03-18 15:14:40 +01:00
let fractional_part = fractional_part_from_subsec_ns(FractionalResolution::FourMs, 7843138);
assert_eq!(fractional_part.counter, 2);
2022-12-09 16:51:48 +01:00
}
#[test]
fn fractional_part_formula_2() {
let fractional_part =
2024-03-18 15:14:40 +01:00
fractional_part_from_subsec_ns(FractionalResolution::FourMs, 12000000);
assert_eq!(fractional_part.counter, 3);
2022-12-09 16:51:48 +01:00
}
#[test]
fn fractional_part_formula_3() {
2022-12-22 23:45:15 +01:00
let one_fraction_with_width_two_in_ns =
10_u64.pow(9) as f64 / (2_u32.pow(8 * 2) - 1) as f64;
assert_eq!(one_fraction_with_width_two_in_ns.ceil(), 15260.0);
let hundred_fractions_and_some =
(100.0 * one_fraction_with_width_two_in_ns).floor() as u64 + 7000;
let fractional_part = fractional_part_from_subsec_ns(
FractionalResolution::FifteenUs,
hundred_fractions_and_some,
2024-03-18 15:14:40 +01:00
);
assert_eq!(fractional_part.counter, 100);
2022-12-22 23:45:15 +01:00
// Using exactly 101.0 can yield values which will later be rounded down to 100
let hundred_and_one_fractions =
(101.001 * one_fraction_with_width_two_in_ns).floor() as u64;
let fractional_part = fractional_part_from_subsec_ns(
FractionalResolution::FifteenUs,
hundred_and_one_fractions,
2024-03-18 15:14:40 +01:00
);
assert_eq!(fractional_part.counter, 101);
2022-12-09 16:51:48 +01:00
}
2022-12-10 18:09:00 +01:00
#[test]
fn update_fractions() {
2024-03-18 15:14:40 +01:00
let mut stamp = CucTime::new(2000);
let res = stamp.set_fractions(FractionalPart {
resolution: FractionalResolution::SixtyNs,
counter: 5000,
});
2022-12-10 18:09:00 +01:00
assert!(res.is_ok());
2024-03-18 15:14:40 +01:00
assert_eq!(
stamp.fractions().resolution(),
FractionalResolution::SixtyNs
);
assert_eq!(stamp.fractions().counter(), 5000);
2022-12-10 18:09:00 +01:00
}
#[test]
fn set_fract_resolution() {
2024-03-18 15:14:40 +01:00
let mut stamp = CucTime::new(2000);
2022-12-10 18:09:00 +01:00
stamp.set_fractional_resolution(FractionalResolution::SixtyNs);
2024-03-18 15:14:40 +01:00
assert_eq!(
stamp.fractions().resolution(),
FractionalResolution::SixtyNs
);
assert_eq!(stamp.fractions().counter(), 0);
let res = stamp.update_from_now(LEAP_SECONDS);
2022-12-10 18:09:00 +01:00
assert!(res.is_ok());
}
2022-12-21 01:01:05 +01:00
#[test]
2024-03-18 15:14:40 +01:00
fn test_small_fraction_floored_to_zero() {
let fractions = fractional_part_from_subsec_ns(FractionalResolution::SixtyNs, 59);
assert_eq!(fractions.counter, 0);
}
#[test]
fn test_small_fraction_becomes_fractional_part() {
let fractions = fractional_part_from_subsec_ns(FractionalResolution::SixtyNs, 61);
assert_eq!(fractions.counter, 1);
}
#[test]
fn test_smallest_resolution_small_nanoseconds_floored_to_zero() {
2022-12-21 09:47:09 +01:00
let fractions =
2024-03-18 15:14:40 +01:00
fractional_part_from_subsec_ns(FractionalResolution::FourMs, 3800 * 1e3 as u64);
assert_eq!(fractions.counter, 0);
}
#[test]
fn test_smallest_resolution_small_nanoseconds_becomes_one_fraction() {
let fractions =
fractional_part_from_subsec_ns(FractionalResolution::FourMs, 4000 * 1e3 as u64);
assert_eq!(fractions.counter, 1);
}
#[test]
fn test_smallest_resolution_large_nanoseconds_becomes_largest_fraction() {
let fractions =
fractional_part_from_subsec_ns(FractionalResolution::FourMs, 10u64.pow(9) - 1);
assert_eq!(fractions.counter, 2_u32.pow(8) - 1);
}
#[test]
fn test_largest_fractions_with_largest_resolution() {
let fractions =
fractional_part_from_subsec_ns(FractionalResolution::SixtyNs, 10u64.pow(9) - 1);
2022-12-21 01:01:05 +01:00
// The value can not be larger than representable by 3 bytes
// Assert that the maximum resolution can be reached
2024-03-18 15:14:40 +01:00
assert_eq!(fractions.counter, 2_u32.pow(3 * 8) - 1);
2022-12-21 01:01:05 +01:00
}
2023-01-17 01:16:07 +01:00
2024-03-18 15:14:40 +01:00
fn check_stamp_after_addition(cuc_stamp: &CucTime) {
let cuc_with_leaps = cuc_stamp.to_leap_sec_helper(LEAP_SECONDS);
assert_eq!(
cuc_with_leaps.ccdsd_time_code(),
CcsdsTimeCode::CucCcsdsEpoch
);
2023-01-17 01:16:07 +01:00
assert_eq!(cuc_stamp.width_counter_pair().1, 202);
2024-03-18 15:14:40 +01:00
let fractions = cuc_stamp.fractions().counter();
2023-01-17 01:16:07 +01:00
let expected_val =
2024-03-18 15:14:40 +01:00
(0.5 * fractional_res_to_div(FractionalResolution::FifteenUs) as f64).ceil() as u32;
2023-01-17 01:16:07 +01:00
assert_eq!(fractions, expected_val);
let cuc_stamp2 = cuc_stamp + Duration::from_millis(501);
// What I would roughly expect
assert_eq!(cuc_stamp2.counter.1, 203);
2024-03-18 15:14:40 +01:00
assert!(cuc_stamp2.fractions().counter() < 100);
assert!(cuc_stamp2.subsec_millis() <= 1);
2023-01-17 01:16:07 +01:00
}
2023-12-06 01:12:33 +01:00
#[test]
fn add_duration_basic() {
2024-03-18 15:14:40 +01:00
let mut cuc_stamp = CucTime::new(200);
2023-12-06 01:12:33 +01:00
cuc_stamp.set_fractional_resolution(FractionalResolution::FifteenUs);
let duration = Duration::from_millis(2500);
cuc_stamp += duration;
check_stamp_after_addition(&cuc_stamp);
}
#[test]
fn add_duration_basic_on_ref() {
2024-03-18 15:14:40 +01:00
let mut cuc_stamp = CucTime::new(200);
2023-12-06 01:12:33 +01:00
cuc_stamp.set_fractional_resolution(FractionalResolution::FifteenUs);
let duration = Duration::from_millis(2500);
let new_stamp = cuc_stamp + duration;
check_stamp_after_addition(&new_stamp);
}
#[test]
fn add_duration_basic_no_fractions() {
2024-03-18 15:14:40 +01:00
let mut cuc_stamp = CucTime::new(200);
2023-12-06 01:12:33 +01:00
let duration = Duration::from_millis(2000);
cuc_stamp += duration;
assert_eq!(cuc_stamp.counter(), 202);
2024-03-18 15:14:40 +01:00
assert_eq!(cuc_stamp.fractions(), FractionalPart::new_empty());
2023-12-06 01:12:33 +01:00
}
#[test]
fn add_duration_basic_on_ref_no_fractions() {
2024-03-18 15:14:40 +01:00
let cuc_stamp = CucTime::new(200);
2023-12-06 01:12:33 +01:00
let duration = Duration::from_millis(2000);
let new_stamp = cuc_stamp + duration;
assert_eq!(new_stamp.counter(), 202);
2024-03-18 15:14:40 +01:00
assert_eq!(new_stamp.fractions(), FractionalPart::new_empty());
2023-12-06 01:12:33 +01:00
}
2023-01-17 01:16:07 +01:00
#[test]
fn add_duration_overflow() {
let mut cuc_stamp =
2024-03-18 15:14:40 +01:00
CucTime::new_generic(WidthCounterPair(1, 255), FractionalPart::new_empty()).unwrap();
2023-01-17 01:16:07 +01:00
let duration = Duration::from_secs(10);
cuc_stamp += duration;
assert_eq!(cuc_stamp.counter.1, 10);
}
2023-12-05 15:05:00 +01:00
#[test]
fn test_invalid_width_param() {
2024-03-18 15:14:40 +01:00
let error = CucTime::new_generic(WidthCounterPair(8, 0), FractionalPart::new_empty());
2023-12-05 15:05:00 +01:00
assert!(error.is_err());
let error = error.unwrap_err();
if let CucError::InvalidCounterWidth(width) = error {
assert_eq!(width, 8);
assert_eq!(error.to_string(), "invalid cuc counter byte width 8");
} else {
panic!("unexpected error: {}", error);
}
}
2023-12-05 15:26:34 +01:00
#[test]
fn test_from_dt() {
2024-03-18 15:14:40 +01:00
let dt = chrono::Utc.with_ymd_and_hms(2021, 1, 1, 0, 0, 0).unwrap();
let cuc = CucTime::from_chrono_date_time(&dt, FractionalResolution::Seconds, LEAP_SECONDS)
.unwrap();
assert_eq!(cuc.counter(), dt.timestamp() as u32 + LEAP_SECONDS);
2023-12-05 15:26:34 +01:00
}
#[test]
fn from_unix_stamp() {
2024-03-18 15:14:40 +01:00
let unix_stamp = UnixTime::new(0, 0);
let cuc = CucTime::from_unix_time(&unix_stamp, FractionalResolution::Seconds, LEAP_SECONDS)
.expect("failed to create cuc from unix stamp");
2023-12-05 15:26:34 +01:00
assert_eq!(
cuc.counter(),
2024-03-18 15:14:40 +01:00
(-DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32) as u32 + LEAP_SECONDS
2023-12-05 15:26:34 +01:00
);
}
2023-12-06 01:12:33 +01:00
#[test]
fn test_invalid_counter() {
2024-03-18 15:14:40 +01:00
let cuc_error = CucTime::new_generic(WidthCounterPair(1, 256), FractionalPart::new_empty());
2023-12-06 01:12:33 +01:00
assert!(cuc_error.is_err());
let cuc_error = cuc_error.unwrap_err();
if let CucError::InvalidCounter { width, counter } = cuc_error {
assert_eq!(width, 1);
assert_eq!(counter, 256);
assert_eq!(cuc_error.to_string(), "invalid cuc counter 256 for width 1");
} else {
panic!("unexpected error: {}", cuc_error);
}
}
2024-02-05 15:04:29 +01:00
#[test]
fn test_stamp_to_vec() {
2024-03-18 15:23:26 +01:00
let stamp = CucTime::new(100);
2024-02-05 15:04:29 +01:00
let stamp_vec = stamp.to_vec().unwrap();
let mut buf: [u8; 16] = [0; 16];
stamp.write_to_bytes(&mut buf).unwrap();
assert_eq!(stamp_vec, buf[..stamp.len_written()]);
}
2022-12-09 13:50:04 +01:00
}