use crate::error::PfcpError;
use crate::ie::IeType;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
const NTP_EPOCH_OFFSET: u32 = 2_208_988_800;
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct RecoveryTimeStamp {
pub timestamp: SystemTime,
}
impl RecoveryTimeStamp {
pub fn new(timestamp: SystemTime) -> Self {
RecoveryTimeStamp { timestamp }
}
pub fn ntp_seconds(&self) -> u32 {
match self.timestamp.duration_since(UNIX_EPOCH) {
Ok(duration) => NTP_EPOCH_OFFSET.wrapping_add(duration.as_secs() as u32),
Err(error) => NTP_EPOCH_OFFSET.wrapping_sub(error.duration().as_secs() as u32),
}
}
pub fn marshal(&self) -> [u8; 4] {
self.ntp_seconds().to_be_bytes()
}
pub fn to_ie(&self) -> crate::ie::Ie {
crate::ie::Ie::new(IeType::RecoveryTimeStamp, self.marshal().to_vec())
}
pub fn unmarshal(data: &[u8]) -> Result<Self, PfcpError> {
if data.len() < 4 {
return Err(PfcpError::invalid_length(
"Recovery Time Stamp",
IeType::RecoveryTimeStamp,
4,
data.len(),
));
}
let ntp_timestamp = u32::from_be_bytes(data[..4].try_into().expect("length checked above"));
let timestamp = if ntp_timestamp >= NTP_EPOCH_OFFSET {
UNIX_EPOCH + Duration::from_secs(u64::from(ntp_timestamp - NTP_EPOCH_OFFSET))
} else {
UNIX_EPOCH
.checked_sub(Duration::from_secs(u64::from(
NTP_EPOCH_OFFSET - ntp_timestamp,
)))
.ok_or_else(|| {
PfcpError::invalid_value(
"Recovery Time Stamp",
ntp_timestamp.to_string(),
"timestamp is outside the SystemTime range",
)
})?
};
Ok(RecoveryTimeStamp { timestamp })
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::{SystemTime, UNIX_EPOCH};
#[test]
fn test_recovery_time_stamp_marshal_unmarshal() {
let now = SystemTime::now();
let rts = RecoveryTimeStamp::new(now);
let marshaled = rts.marshal();
let unmarshaled = RecoveryTimeStamp::unmarshal(&marshaled).unwrap();
let original_secs = rts.timestamp.duration_since(UNIX_EPOCH).unwrap().as_secs();
let unmarshaled_secs = unmarshaled
.timestamp
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
assert_eq!(original_secs, unmarshaled_secs);
}
#[test]
fn test_recovery_time_stamp_unmarshal_empty() {
let result = RecoveryTimeStamp::unmarshal(&[]);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(matches!(err, PfcpError::InvalidLength { .. }));
assert!(err.to_string().contains("Recovery Time Stamp"));
}
#[test]
fn test_recovery_time_stamp_unmarshal_too_short() {
let result = RecoveryTimeStamp::unmarshal(&[0x01, 0x02]);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(matches!(err, PfcpError::InvalidLength { .. }));
}
#[test]
fn test_recovery_time_stamp_preserves_pre_unix_ntp_value() {
let value = 42_u32;
let timestamp = RecoveryTimeStamp::unmarshal(&value.to_be_bytes()).unwrap();
assert_eq!(timestamp.ntp_seconds(), value);
assert_eq!(timestamp.marshal(), value.to_be_bytes());
}
#[test]
fn test_recovery_time_stamp_ignores_extension_octets() {
let timestamp = RecoveryTimeStamp::unmarshal(&[0, 0, 0, 42, 0xff]).unwrap();
assert_eq!(timestamp.ntp_seconds(), 42);
}
}