Compare commits
115 Commits
Author | SHA1 | Date | |
---|---|---|---|
8fddaefab1 | |||
6e593e4e27 | |||
ef5a4e2924 | |||
aa5c206c1c | |||
1c702f933f | |||
6989558f93 | |||
33e9b40c39 | |||
008359ec71 | |||
2385e7812b | |||
76ea418711 | |||
b68f1c3752 | |||
7c9bdb4512 | |||
8ffa7efa4d | |||
d972dd5223 | |||
481de83fdb | |||
09b305f529 | |||
78c5787e07 | |||
e9e33b0335 | |||
455be77f4a | |||
c748657499 | |||
2e90cba5bd | |||
f290d2a54e | |||
da695e4705 | |||
5c222735d4 | |||
1432c298b3 | |||
1b45082ace | |||
4c20158dcc | |||
c879181093 | |||
6c88e94742 | |||
3fb2fbd20c | |||
ec8a2e1d24 | |||
192e2f2c76 | |||
5df221759f | |||
f137bd2549 | |||
630bffec51 | |||
a14ae37cac | |||
2758699601 | |||
b07cec28ea | |||
51963d0f72 | |||
83e2cad753 | |||
472bfa9964 | |||
14fa1bad92 | |||
3828a98c76 | |||
f641248ac2 | |||
884de647ad | |||
0d8074c6b9 | |||
6798e3a6f5 | |||
7e763fe055 | |||
4410ee7eec | |||
2895d7645a | |||
692d12e5a5 | |||
9e57ce3872 | |||
fd13694904 | |||
976fe9c49b | |||
fbeea41e8f | |||
177ddba9c5 | |||
f964342556 | |||
46e2af41d2 | |||
fc05eaa925 | |||
5a878ef6a3 | |||
66d77fda36 | |||
ef963187ac | |||
93159dae45 | |||
f73edd71fd | |||
6341cf35d3 | |||
1d9329ad63 | |||
d889826b79 | |||
e155ddbcb0 | |||
7615e40e43 | |||
bccbdf65a3 | |||
8d0de0dce4 | |||
5958d19eb4 | |||
4d64394637 | |||
c4f8eee8da | |||
f7f500ea48 | |||
7f607be5f3 | |||
c60ebc2a6e | |||
48e5b22c27 | |||
04073a29af | |||
66b4bf6013 | |||
f5397b8352 | |||
180134c05d | |||
3ee237865c | |||
afd59b14fc | |||
3d5829b9a8 | |||
1b4934e865 | |||
3fa43d46f9 | |||
4cd8f143df | |||
878f072e9a | |||
25695b39ea | |||
8c0b78c698 | |||
4e1df03729 | |||
f13a2f73eb | |||
62c1462930 | |||
8358d5ed1c | |||
0c77a04059 | |||
e8571866e8 | |||
038755e56e | |||
33d78292c6 | |||
8cf635d2fa | |||
3420bcbeba | |||
6554241ed7 | |||
283f9ff495 | |||
3a71b00198 | |||
13be7ca1e7 | |||
098a534199 | |||
322a56335e | |||
f7c688d8db | |||
54bb4bdaaa | |||
1969a26f14 | |||
d28ea7d4da | |||
aeb2e806b8 | |||
938c4ba770 | |||
97c70bf03b | |||
dc6d726e61 |
30
.github/workflows/ci.yml
vendored
30
.github/workflows/ci.yml
vendored
@ -20,6 +20,21 @@ jobs:
|
||||
command: check
|
||||
args: --release
|
||||
|
||||
msrv:
|
||||
name: Check with MSRV
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: 1.60.0
|
||||
override: true
|
||||
profile: minimal
|
||||
- uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: check
|
||||
args: --release
|
||||
|
||||
cross-check:
|
||||
name: Check Cross
|
||||
runs-on: ubuntu-latest
|
||||
@ -58,6 +73,21 @@ jobs:
|
||||
command: fmt
|
||||
args: --all -- --check
|
||||
|
||||
check-doc:
|
||||
name: Check Documentation Build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: nightly
|
||||
override: true
|
||||
profile: minimal
|
||||
- uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: doc
|
||||
args: --all-features
|
||||
|
||||
clippy:
|
||||
name: Clippy
|
||||
runs-on: ubuntu-latest
|
||||
|
61
CHANGELOG.md
61
CHANGELOG.md
@ -8,6 +8,67 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
# [unreleased]
|
||||
|
||||
# [v0.4.2] 14.01.2023
|
||||
|
||||
## Fixed
|
||||
|
||||
- CDS timestamp: Fixed another small logic error for stamp creation from the current
|
||||
time with picosecond precision.
|
||||
PR: https://egit.irs.uni-stuttgart.de/rust/spacepackets/pulls/8
|
||||
|
||||
# [v0.4.1] 14.01.2023
|
||||
|
||||
## Fixed
|
||||
|
||||
- CDS timestamp: The conversion function from the current time were buggy
|
||||
when specifying picoseconds precision, which could lead to overflow
|
||||
multiplications and/or incorrect precision fields.
|
||||
PR: https://egit.irs.uni-stuttgart.de/rust/spacepackets/pulls/7
|
||||
|
||||
# [v0.4.0] 10.01.2023
|
||||
|
||||
## Fixed
|
||||
|
||||
- Remove `Default` derive on CDS time provider. This can lead to uninitialized preamble fields.
|
||||
|
||||
## Changed
|
||||
|
||||
- `serde` support is now optional and behind the `serde` feature.
|
||||
- `PusTcSecondaryHeaderT` trait renamed to `GenericPusTcSecondaryHeader`.
|
||||
- `PusTmSecondaryHeaderT` trait renamed to `GenericPusTmSecondaryHeader`.
|
||||
- `SpHeader`: Former `tc` and `tm` methods now named `tc_unseg` and `tm_unseg`.
|
||||
Former `new` method now called `new_from_single_fields`.
|
||||
- `SpHeader`: Renamed `from_bytes` to `from_be_bytes`.
|
||||
The function now returns the remaining slice as well.
|
||||
- All CDS specific functionality was moved into the `cds` submodule of the `time`
|
||||
module. `CdsShortTimeProvider` was renamed to `TimeProvider`.
|
||||
PR: https://egit.irs.uni-stuttgart.de/rust/spacepackets/pulls/3
|
||||
|
||||
## Added
|
||||
|
||||
- `SpHeader` getter function `sp_header` added for `PusTc`
|
||||
PR: https://egit.irs.uni-stuttgart.de/rust/spacepackets/pulls/6
|
||||
- Added PFC enumerations: `ecss::UnsignedPfc` and `ecss::RealPfc`.
|
||||
PR: https://egit.irs.uni-stuttgart.de/rust/spacepackets/pulls/5
|
||||
- Added `std::error::Error` implementation for all error enumerations if the `std` feature
|
||||
is enabled.
|
||||
- CUC timestamp implementation as specified in CCSDS 301.0-B-4 section 3.2.
|
||||
PR: https://egit.irs.uni-stuttgart.de/rust/spacepackets/pulls/4/files
|
||||
- ACII timestamps as specified in CCSDS 301.0-B-4 section 3.5.
|
||||
- Added MSRV in `Cargo.toml` with the `rust-version` field set to Rust 1.60.
|
||||
- `serde` `Serialize` and `Deserialize` added to all types.
|
||||
- Added `const` constructors for `PacketId`, `PacketSeqCtrl` and
|
||||
`SpHeader`.
|
||||
- Added `PartialEq` and `Eq` `derive`s to `TimeProvider`.
|
||||
- `SpHeader`: Added serialization function into raw format `write_to_be_bytes`.
|
||||
- Added 24-bit day field support for CDS short. The bit width is configured at type level
|
||||
via a generic parameter type passed to the `cds::TimeProvider`
|
||||
- Added submillisecond precision support for `cds::TimeProvider`
|
||||
|
||||
# [v0.3.1] 03.12.2022
|
||||
|
||||
- Small fix for faulty docs.rs build
|
||||
|
||||
# [v0.3.0] 01.12.2022
|
||||
|
||||
## Added
|
||||
|
11
Cargo.toml
11
Cargo.toml
@ -1,7 +1,8 @@
|
||||
[package]
|
||||
name = "spacepackets"
|
||||
version = "0.3.0"
|
||||
version = "0.4.2"
|
||||
edition = "2021"
|
||||
rust-version = "1.60"
|
||||
authors = ["Robin Mueller <muellerr@irs.uni-stuttgart.de>"]
|
||||
description = "Generic implementations for various CCSDS and ECSS packet standards"
|
||||
homepage = "https://egit.irs.uni-stuttgart.de/rust/spacepackets"
|
||||
@ -13,11 +14,12 @@ categories = ["aerospace", "aerospace::space-protocols", "no-std", "hardware-sup
|
||||
|
||||
[dependencies]
|
||||
zerocopy = "0.6"
|
||||
crc = "3.0"
|
||||
crc = "3"
|
||||
delegate = "0.8"
|
||||
|
||||
[dependencies.serde]
|
||||
version = "1.0"
|
||||
version = "1"
|
||||
optional = true
|
||||
default-features = false
|
||||
features = ["derive"]
|
||||
|
||||
@ -35,7 +37,8 @@ version = "1.0"
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = ["chrono/std", "chrono/clock", "alloc"]
|
||||
alloc = ["postcard/alloc"]
|
||||
serde = ["dep:serde", "chrono/serde"]
|
||||
alloc = ["postcard/alloc", "chrono/alloc"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
|
19
README.md
19
README.md
@ -15,25 +15,32 @@ Currently, this includes the following components:
|
||||
[CCSDS Blue Book 133.0-B-2](https://public.ccsds.org/Pubs/133x0b2e1.pdf)
|
||||
- PUS Telecommand and PUS Telemetry implementation according to the
|
||||
[ECSS-E-ST-70-41C standard](https://ecss.nl/standard/ecss-e-st-70-41c-space-engineering-telemetry-and-telecommand-packet-utilization-15-april-2016/).
|
||||
- CDS Short Time Code implementation according to
|
||||
[CCSDS CCSDS 301.0-B-4](https://public.ccsds.org/Pubs/301x0b4e1.pdf)
|
||||
|
||||
- CUC (CCSDS Unsegmented Time Code) implementation according to
|
||||
[CCSDS 301.0-B-4 3.2](https://public.ccsds.org/Pubs/301x0b4e1.pdf)
|
||||
- CDS (CCSDS Day Segmented Time Code) implementation according to
|
||||
[CCSDS 301.0-B-4 3.3](https://public.ccsds.org/Pubs/301x0b4e1.pdf)
|
||||
- Some helper types to support ASCII timecodes ad specified in
|
||||
[CCSDS 301.0-B-4 3.5](https://public.ccsds.org/Pubs/301x0b4e1.pdf)
|
||||
|
||||
# Features
|
||||
|
||||
`spacepackets` supports various runtime environments and is also suitable for `no_std` environments.
|
||||
|
||||
It offers support for [`serde`](https://serde.rs/). The Space Packet, PUS TM and TC
|
||||
implementations derive the `serde` `Serialize` and `Deserialize` trait. This allows serializing and
|
||||
It also offers optional support for [`serde`](https://serde.rs/). This allows serializing and
|
||||
deserializing them with an appropriate `serde` provider like
|
||||
[`postcard`](https://github.com/jamesmunns/postcard).
|
||||
|
||||
Default features:
|
||||
## Default features
|
||||
|
||||
- [`std`](https://doc.rust-lang.org/std/): Enables functionality relying on the standard library.
|
||||
- [`alloc`](https://doc.rust-lang.org/alloc/): Enables features which operate on containers
|
||||
like [`alloc::vec::Vec`](https://doc.rust-lang.org/beta/alloc/vec/struct.Vec.html).
|
||||
Enabled by the `std` feature.
|
||||
|
||||
## Optional Features
|
||||
|
||||
- [`serde`](https://serde.rs/): Adds `serde` support for most types by adding `Serialize` and `Deserialize` `derive`s
|
||||
|
||||
# Examples
|
||||
|
||||
You can check the [documentation](https://docs.rs/spacepackets) of individual modules for various
|
||||
|
@ -10,6 +10,6 @@ ARG DEBIAN_FRONTEND=noninteractive
|
||||
# set CROSS_CONTAINER_IN_CONTAINER to inform `cross` that it is executed from within a container
|
||||
ENV CROSS_CONTAINER_IN_CONTAINER=true
|
||||
|
||||
# TODO: installing cross is problematic, permission issues
|
||||
RUN rustup target add thumbv7em-none-eabihf armv7-unknown-linux-gnueabihf && \
|
||||
RUN rustup install nightly && \
|
||||
rustup target add thumbv7em-none-eabihf armv7-unknown-linux-gnueabihf && \
|
||||
rustup component add rustfmt clippy
|
||||
|
18
automation/Jenkinsfile
vendored
18
automation/Jenkinsfile
vendored
@ -13,19 +13,29 @@ pipeline {
|
||||
sh 'cargo clippy'
|
||||
}
|
||||
}
|
||||
stage('Docs') {
|
||||
steps {
|
||||
sh 'cargo +nightly doc --all-features'
|
||||
}
|
||||
}
|
||||
stage('Rustfmt') {
|
||||
steps {
|
||||
sh 'cargo fmt'
|
||||
sh 'cargo fmt --all --check'
|
||||
}
|
||||
}
|
||||
stage('Test') {
|
||||
steps {
|
||||
sh 'cargo test'
|
||||
sh 'cargo test --all-features'
|
||||
}
|
||||
}
|
||||
stage('Check') {
|
||||
stage('Check with all features') {
|
||||
steps {
|
||||
sh 'cargo check'
|
||||
sh 'cargo check --all-features'
|
||||
}
|
||||
}
|
||||
stage('Check with no features') {
|
||||
steps {
|
||||
sh 'cargo check --no-default-features'
|
||||
}
|
||||
}
|
||||
stage('Check Cross Embedded Bare Metal') {
|
||||
|
114
src/ecss.rs
114
src/ecss.rs
@ -1,10 +1,13 @@
|
||||
//! 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::fmt::{Debug, Display, Formatter};
|
||||
use core::mem::size_of;
|
||||
use crc::{Crc, CRC_16_IBM_3740};
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[cfg(feature = "std")]
|
||||
use std::error::Error;
|
||||
|
||||
pub type CrcType = u16;
|
||||
|
||||
@ -12,8 +15,24 @@ pub type CrcType = u16;
|
||||
pub const CRC_CCITT_FALSE: Crc<u16> = Crc::<u16>::new(&CRC_16_IBM_3740);
|
||||
pub const CCSDS_HEADER_LEN: usize = size_of::<crate::zc::SpHeader>();
|
||||
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub enum PusServiceId {
|
||||
/// Service 1
|
||||
Verification = 1,
|
||||
/// Service 3
|
||||
Housekeeping = 3,
|
||||
/// Service 5
|
||||
Event = 5,
|
||||
/// Service 8
|
||||
Action = 8,
|
||||
/// Service 17
|
||||
Test = 17,
|
||||
}
|
||||
|
||||
/// All PUS versions. Only PUS C is supported by this library.
|
||||
#[derive(PartialEq, Eq, Copy, Clone, Serialize, Deserialize, Debug)]
|
||||
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub enum PusVersion {
|
||||
EsaPus = 0,
|
||||
PusA = 1,
|
||||
@ -34,7 +53,9 @@ impl TryFrom<u8> for PusVersion {
|
||||
}
|
||||
}
|
||||
|
||||
/// ECSS Packet Type Codes (PTC)s.
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub enum PacketTypeCodes {
|
||||
Boolean = 1,
|
||||
Enumerated = 2,
|
||||
@ -50,7 +71,40 @@ pub enum PacketTypeCodes {
|
||||
Packet = 12,
|
||||
}
|
||||
|
||||
pub type Ptc = PacketTypeCodes;
|
||||
|
||||
/// ECSS Packet Field Codes (PFC)s for the unsigned [Ptc].
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub enum UnsignedPfc {
|
||||
OneByte = 4,
|
||||
TwelveBits = 8,
|
||||
TwoBytes = 12,
|
||||
ThreeBytes = 13,
|
||||
FourBytes = 14,
|
||||
SixBytes = 15,
|
||||
EightBytes = 16,
|
||||
OneBit = 17,
|
||||
TwoBits = 18,
|
||||
ThreeBits = 19,
|
||||
}
|
||||
|
||||
/// ECSS Packet Field Codes (PFC)s for the real (floating point) [Ptc].
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub enum RealPfc {
|
||||
/// 4 octets simple precision format (IEEE)
|
||||
Float = 1,
|
||||
/// 8 octets simple precision format (IEEE)
|
||||
Double = 2,
|
||||
/// 4 octets simple precision format (MIL-STD)
|
||||
FloatMilStd = 3,
|
||||
/// 8 octets simple precision format (MIL-STD)
|
||||
DoubleMilStd = 4,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub enum PusError {
|
||||
VersionNotSupported(PusVersion),
|
||||
IncorrectCrc(u16),
|
||||
@ -61,12 +115,53 @@ pub enum PusError {
|
||||
ByteConversionError(ByteConversionError),
|
||||
}
|
||||
|
||||
impl Display for PusError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
|
||||
match self {
|
||||
PusError::VersionNotSupported(v) => {
|
||||
write!(f, "PUS version {:?} not supported", v)
|
||||
}
|
||||
PusError::IncorrectCrc(crc) => {
|
||||
write!(f, "crc16 {:#04x} is incorrect", crc)
|
||||
}
|
||||
PusError::RawDataTooShort(size) => {
|
||||
write!(
|
||||
f,
|
||||
"deserialization error, provided raw data with size {} too short",
|
||||
size
|
||||
)
|
||||
}
|
||||
PusError::NoRawData => {
|
||||
write!(f, "no raw data provided")
|
||||
}
|
||||
PusError::CrcCalculationMissing => {
|
||||
write!(f, "crc16 was not calculated")
|
||||
}
|
||||
PusError::ByteConversionError(e) => {
|
||||
write!(f, "low level byte conversion error: {}", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl Error for PusError {
|
||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||
if let PusError::ByteConversionError(e) = self {
|
||||
return Some(e);
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ByteConversionError> for PusError {
|
||||
fn from(e: ByteConversionError) -> Self {
|
||||
PusError::ByteConversionError(e)
|
||||
}
|
||||
}
|
||||
|
||||
/// Generic trait to describe common attributes for both PUS Telecommands (TC) and PUS Telemetry
|
||||
/// (TM) packets. All PUS packets are also a special type of [CcsdsPacket]s.
|
||||
pub trait PusPacket: CcsdsPacket {
|
||||
const PUS_VERSION: PusVersion = PusVersion::PusC;
|
||||
|
||||
@ -179,6 +274,14 @@ pub trait ToBeBytes {
|
||||
fn to_be_bytes(&self) -> Self::ByteArray;
|
||||
}
|
||||
|
||||
impl ToBeBytes for () {
|
||||
type ByteArray = [u8; 0];
|
||||
|
||||
fn to_be_bytes(&self) -> Self::ByteArray {
|
||||
[]
|
||||
}
|
||||
}
|
||||
|
||||
impl ToBeBytes for u8 {
|
||||
type ByteArray = [u8; 1];
|
||||
|
||||
@ -212,6 +315,7 @@ impl ToBeBytes for u64 {
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct GenericEcssEnumWrapper<TYPE> {
|
||||
val: TYPE,
|
||||
}
|
||||
@ -232,13 +336,13 @@ impl<TYPE: ToBeBytes> EcssEnumeration for GenericEcssEnumWrapper<TYPE> {
|
||||
}
|
||||
|
||||
fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<(), ByteConversionError> {
|
||||
if buf.len() < self.byte_width() as usize {
|
||||
if buf.len() < self.byte_width() {
|
||||
return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
|
||||
found: buf.len(),
|
||||
expected: self.byte_width() as usize,
|
||||
expected: self.byte_width(),
|
||||
}));
|
||||
}
|
||||
buf[0..self.byte_width() as usize].copy_from_slice(self.val.to_be_bytes().as_ref());
|
||||
buf[0..self.byte_width()].copy_from_slice(self.val.to_be_bytes().as_ref());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
429
src/lib.rs
429
src/lib.rs
@ -9,47 +9,63 @@
|
||||
//! [CCSDS Blue Book 133.0-B-2](https://public.ccsds.org/Pubs/133x0b2e1.pdf)
|
||||
//! - PUS Telecommand and PUS Telemetry implementation according to the
|
||||
//! [ECSS-E-ST-70-41C standard](https://ecss.nl/standard/ecss-e-st-70-41c-space-engineering-telemetry-and-telecommand-packet-utilization-15-april-2016/).
|
||||
//! - CDS Short Time Code implementation according to
|
||||
//! [CCSDS CCSDS 301.0-B-4](https://public.ccsds.org/Pubs/301x0b4e1.pdf)
|
||||
//! - CUC (CCSDS Unsegmented Time Code) implementation according to
|
||||
//! [CCSDS 301.0-B-4 3.2](https://public.ccsds.org/Pubs/301x0b4e1.pdf)
|
||||
//! - CDS (CCSDS Day Segmented Time Code) implementation according to
|
||||
//! [CCSDS 301.0-B-4 3.3](https://public.ccsds.org/Pubs/301x0b4e1.pdf)
|
||||
//! - Some helper types to support ASCII timecodes ad specified in
|
||||
//! [CCSDS 301.0-B-4 3.5](https://public.ccsds.org/Pubs/301x0b4e1.pdf)
|
||||
//!
|
||||
//! ## Features
|
||||
//!
|
||||
//! `spacepackets` supports various runtime environments and is also suitable for `no_std` environments.
|
||||
//!
|
||||
//! It also offers support for [`serde`](https://serde.rs/). The Space Packet, PUS TM and TC
|
||||
//! implementations derive the `serde` `Serialize` and `Deserialize` trait. This allows serializing and
|
||||
//! It also offers optional support for [`serde`](https://serde.rs/). This allows serializing and
|
||||
//! deserializing them with an appropriate `serde` provider like
|
||||
//! [`postcard`](https://github.com/jamesmunns/postcard).
|
||||
//!
|
||||
//! Default features:
|
||||
//! ### Default features
|
||||
//!
|
||||
//! - [`std`](https://doc.rust-lang.org/std/): Enables functionality relying on the standard library.
|
||||
//! - [`alloc`](https://doc.rust-lang.org/alloc/): Enables features which operate on containers
|
||||
//! like [`alloc::vec::Vec`](https://doc.rust-lang.org/beta/alloc/vec/struct.Vec.html).
|
||||
//! Enabled by the `std` feature.
|
||||
//!
|
||||
//! ### Optional features
|
||||
//!
|
||||
//! - [`serde`](https://serde.rs/): Adds `serde` support for most types by adding `Serialize` and
|
||||
//! `Deserialize` `derive`s
|
||||
//!
|
||||
//! ## Module
|
||||
//!
|
||||
//! This module contains helpers and data structures to generate Space Packets according to the
|
||||
//! [CCSDS 133.0-B-2](https://public.ccsds.org/Pubs/133x0b2e1.pdf). This includes the
|
||||
//! [SpHeader] class to generate the Space Packet Header component common to all space packets
|
||||
//! [SpHeader] class to generate the Space Packet Header component common to all space packets.
|
||||
//!
|
||||
//! ## Example
|
||||
//!
|
||||
//! ```rust
|
||||
//! use spacepackets::SpHeader;
|
||||
//! let sp_header = SpHeader::tc(0x42, 12, 0).expect("Error creating SP header");
|
||||
//! let sp_header = SpHeader::tc_unseg(0x42, 12, 1).expect("Error creating CCSDS TC header");
|
||||
//! println!("{:?}", sp_header);
|
||||
//! let mut ccsds_buf: [u8; 32] = [0; 32];
|
||||
//! sp_header.write_to_be_bytes(&mut ccsds_buf).expect("Writing CCSDS TC header failed");
|
||||
//! println!("{:x?}", &ccsds_buf[0..6]);
|
||||
//! ```
|
||||
#![no_std]
|
||||
#![cfg_attr(doc_cfg, feature(doc_cfg))]
|
||||
#[cfg(feature = "alloc")]
|
||||
extern crate alloc;
|
||||
#[cfg(any(feature = "std", test))]
|
||||
extern crate std;
|
||||
|
||||
use crate::ecss::CCSDS_HEADER_LEN;
|
||||
use core::fmt::{Display, Formatter};
|
||||
use delegate::delegate;
|
||||
#[cfg(feature = "std")]
|
||||
use std::error::Error;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub mod ecss;
|
||||
@ -57,16 +73,22 @@ pub mod tc;
|
||||
pub mod time;
|
||||
pub mod tm;
|
||||
|
||||
mod private {
|
||||
pub trait Sealed {}
|
||||
}
|
||||
|
||||
pub const MAX_APID: u16 = 2u16.pow(11) - 1;
|
||||
pub const MAX_SEQ_COUNT: u16 = 2u16.pow(14) - 1;
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct SizeMissmatch {
|
||||
pub found: usize,
|
||||
pub expected: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub enum ByteConversionError {
|
||||
/// The passed slice is too small. Returns the passed slice length and expected minimum size
|
||||
ToSliceTooSmall(SizeMissmatch),
|
||||
@ -77,7 +99,39 @@ pub enum ByteConversionError {
|
||||
ZeroCopyFromError,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Copy, Clone)]
|
||||
impl Display for ByteConversionError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
|
||||
match self {
|
||||
ByteConversionError::ToSliceTooSmall(missmatch) => {
|
||||
write!(
|
||||
f,
|
||||
"target slice with size {} is too small, expected size of at least {}",
|
||||
missmatch.found, missmatch.expected
|
||||
)
|
||||
}
|
||||
ByteConversionError::FromSliceTooSmall(missmatch) => {
|
||||
write!(
|
||||
f,
|
||||
"source slice with size {} too small, expected at least {} bytes",
|
||||
missmatch.found, missmatch.expected
|
||||
)
|
||||
}
|
||||
ByteConversionError::ZeroCopyToError => {
|
||||
write!(f, "zerocopy serialization error")
|
||||
}
|
||||
ByteConversionError::ZeroCopyFromError => {
|
||||
write!(f, "zerocopy deserialization error")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl Error for ByteConversionError {}
|
||||
|
||||
/// CCSDS packet type enumeration.
|
||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub enum PacketType {
|
||||
Tm = 0,
|
||||
Tc = 1,
|
||||
@ -99,7 +153,8 @@ pub fn packet_type_in_raw_packet_id(packet_id: u16) -> PacketType {
|
||||
PacketType::try_from((packet_id >> 12) as u8 & 0b1).unwrap()
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Copy, Clone)]
|
||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub enum SequenceFlags {
|
||||
ContinuationSegment = 0b00,
|
||||
FirstSegment = 0b01,
|
||||
@ -123,21 +178,59 @@ impl TryFrom<u8> for SequenceFlags {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Copy, Clone)]
|
||||
/// Abstraction for the CCSDS Packet ID, which forms the last thirteen bits
|
||||
/// of the first two bytes in the CCSDS primary header.
|
||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct PacketId {
|
||||
pub ptype: PacketType,
|
||||
pub sec_header_flag: bool,
|
||||
apid: u16,
|
||||
}
|
||||
|
||||
impl PacketId {
|
||||
pub fn new(ptype: PacketType, sec_header_flag: bool, apid: u16) -> Option<PacketId> {
|
||||
let mut pid = PacketId {
|
||||
ptype,
|
||||
sec_header_flag,
|
||||
impl Default for PacketId {
|
||||
fn default() -> Self {
|
||||
PacketId {
|
||||
ptype: PacketType::Tm,
|
||||
sec_header_flag: false,
|
||||
apid: 0,
|
||||
};
|
||||
pid.set_apid(apid).then_some(pid)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PacketId {
|
||||
pub const fn const_tc(sec_header: bool, apid: u16) -> Self {
|
||||
Self::const_new(PacketType::Tc, sec_header, apid)
|
||||
}
|
||||
|
||||
pub const fn const_tm(sec_header: bool, apid: u16) -> Self {
|
||||
Self::const_new(PacketType::Tm, sec_header, apid)
|
||||
}
|
||||
|
||||
pub fn tc(sec_header: bool, apid: u16) -> Option<Self> {
|
||||
Self::new(PacketType::Tc, sec_header, apid)
|
||||
}
|
||||
|
||||
pub fn tm(sec_header: bool, apid: u16) -> Option<Self> {
|
||||
Self::new(PacketType::Tm, sec_header, apid)
|
||||
}
|
||||
|
||||
pub const fn const_new(ptype: PacketType, sec_header: bool, apid: u16) -> Self {
|
||||
if apid > MAX_APID {
|
||||
panic!("APID too large");
|
||||
}
|
||||
PacketId {
|
||||
ptype,
|
||||
sec_header_flag: sec_header,
|
||||
apid,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(ptype: PacketType, sec_header_flag: bool, apid: u16) -> Option<PacketId> {
|
||||
if apid > MAX_APID {
|
||||
return None;
|
||||
}
|
||||
Some(PacketId::const_new(ptype, sec_header_flag, apid))
|
||||
}
|
||||
|
||||
/// Set a new Application Process ID (APID). If the passed number is invalid, the APID will
|
||||
@ -170,27 +263,41 @@ impl From<u16> for PacketId {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Copy, Clone)]
|
||||
/// Abstraction for the CCSDS Packet Sequence Control (PSC) field which is the
|
||||
/// third and the fourth byte in the CCSDS primary header.
|
||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct PacketSequenceCtrl {
|
||||
pub seq_flags: SequenceFlags,
|
||||
seq_count: u16,
|
||||
}
|
||||
|
||||
impl PacketSequenceCtrl {
|
||||
/// Returns [None] if the passed sequence count exceeds [MAX_SEQ_COUNT]
|
||||
pub fn new(seq_flags: SequenceFlags, seq_count: u16) -> Option<PacketSequenceCtrl> {
|
||||
let mut psc = PacketSequenceCtrl {
|
||||
/// const variant of [PacketSequenceCtrl::new], but panics if the sequence count exceeds
|
||||
/// [MAX_SEQ_COUNT].
|
||||
const fn const_new(seq_flags: SequenceFlags, seq_count: u16) -> PacketSequenceCtrl {
|
||||
if seq_count > MAX_SEQ_COUNT {
|
||||
panic!("Sequence count too large");
|
||||
}
|
||||
PacketSequenceCtrl {
|
||||
seq_flags,
|
||||
seq_count: 0,
|
||||
};
|
||||
psc.set_seq_count(seq_count).then_some(psc)
|
||||
seq_count,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns [None] if the passed sequence count exceeds [MAX_SEQ_COUNT].
|
||||
pub fn new(seq_flags: SequenceFlags, seq_count: u16) -> Option<PacketSequenceCtrl> {
|
||||
if seq_count > MAX_SEQ_COUNT {
|
||||
return None;
|
||||
}
|
||||
Some(PacketSequenceCtrl::const_new(seq_flags, seq_count))
|
||||
}
|
||||
pub fn raw(&self) -> u16 {
|
||||
((self.seq_flags as u16) << 14) | self.seq_count
|
||||
}
|
||||
|
||||
/// Set a new sequence count. If the passed number is invalid, the sequence count will not be
|
||||
/// set and false will be returned. The maximum allowed value for the 14-bit field is 16383
|
||||
/// set and false will be returned. The maximum allowed value for the 14-bit field is 16383.
|
||||
pub fn set_seq_count(&mut self, ssc: u16) -> bool {
|
||||
if ssc > MAX_SEQ_COUNT {
|
||||
return false;
|
||||
@ -231,7 +338,7 @@ macro_rules! sph_from_other {
|
||||
const SSC_MASK: u16 = 0x3FFF;
|
||||
const VERSION_MASK: u16 = 0xE000;
|
||||
|
||||
/// Generic trait to access fields of a CCSDS space packet header according to CCSDS 133.0-B-2
|
||||
/// Generic trait to access fields of a CCSDS space packet header according to CCSDS 133.0-B-2.
|
||||
pub trait CcsdsPacket {
|
||||
fn ccsds_version(&self) -> u8;
|
||||
fn packet_id(&self) -> PacketId;
|
||||
@ -245,7 +352,7 @@ pub trait CcsdsPacket {
|
||||
}
|
||||
|
||||
/// Retrieve 13 bit Packet Identification field. Can usually be retrieved with a bitwise AND
|
||||
/// of the first 2 bytes with 0x1FFF
|
||||
/// of the first 2 bytes with 0x1FFF.
|
||||
#[inline]
|
||||
fn packet_id_raw(&self) -> u16 {
|
||||
self.packet_id().raw()
|
||||
@ -256,8 +363,8 @@ pub trait CcsdsPacket {
|
||||
self.psc().raw()
|
||||
}
|
||||
|
||||
/// Retrieve Packet Type (TM: 0, TC: 1).
|
||||
#[inline]
|
||||
/// Retrieve Packet Type (TM: 0, TC: 1)
|
||||
fn ptype(&self) -> PacketType {
|
||||
// This call should never fail because only 0 and 1 can be passed to the try_from call
|
||||
self.packet_id().ptype
|
||||
@ -274,13 +381,13 @@ pub trait CcsdsPacket {
|
||||
}
|
||||
|
||||
/// Retrieve the secondary header flag. Returns true if a secondary header is present
|
||||
/// and false if it is not
|
||||
/// and false if it is not.
|
||||
#[inline]
|
||||
fn sec_header_flag(&self) -> bool {
|
||||
self.packet_id().sec_header_flag
|
||||
}
|
||||
|
||||
/// Retrieve Application Process ID
|
||||
/// Retrieve Application Process ID.
|
||||
#[inline]
|
||||
fn apid(&self) -> u16 {
|
||||
self.packet_id().apid
|
||||
@ -308,16 +415,18 @@ pub trait CcsdsPrimaryHeader {
|
||||
) -> Self;
|
||||
}
|
||||
|
||||
/// Space Packet Primary Header according to CCSDS 133.0-B-2
|
||||
/// Space Packet Primary Header according to CCSDS 133.0-B-2.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `version` - CCSDS version field, occupies the first 3 bits of the raw header
|
||||
/// * `version` - CCSDS version field, occupies the first 3 bits of the raw header. Will generally
|
||||
/// be set to 0b000 in all constructors provided by this crate.
|
||||
/// * `packet_id` - Packet Identifier, which can also be used as a start marker. Occupies the last
|
||||
/// 13 bits of the first two bytes of the raw header
|
||||
/// * `psc` - Packet Sequence Control, occupies the third and fourth byte of the raw header
|
||||
/// * `data_len` - Data length field occupies the fifth and the sixth byte of the raw header
|
||||
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Eq, Copy, Clone)]
|
||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct SpHeader {
|
||||
pub version: u8,
|
||||
pub packet_id: PacketId,
|
||||
@ -326,63 +435,104 @@ pub struct SpHeader {
|
||||
}
|
||||
|
||||
impl Default for SpHeader {
|
||||
/// The default function sets the sequence flag field to [SequenceFlags::Unsegmented]. The data
|
||||
/// length field is set to 1, which denotes an empty space packets.
|
||||
fn default() -> Self {
|
||||
SpHeader {
|
||||
version: 0,
|
||||
packet_id: PacketId {
|
||||
ptype: PacketType::Tm,
|
||||
apid: 0,
|
||||
sec_header_flag: false,
|
||||
},
|
||||
packet_id: PacketId::default(),
|
||||
psc: PacketSequenceCtrl {
|
||||
seq_flags: SequenceFlags::Unsegmented,
|
||||
seq_count: 0,
|
||||
},
|
||||
data_len: 0,
|
||||
data_len: 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SpHeader {
|
||||
/// Create a new Space Packet Header instance which can be used to create generic
|
||||
/// Space Packets. This will return [None] if the APID or sequence count argument
|
||||
/// exceed [MAX_APID] or [MAX_SEQ_COUNT] respectively.
|
||||
pub fn new(
|
||||
pub const fn new(packet_id: PacketId, psc: PacketSequenceCtrl, data_len: u16) -> Self {
|
||||
Self {
|
||||
version: 0,
|
||||
packet_id,
|
||||
psc,
|
||||
data_len,
|
||||
}
|
||||
}
|
||||
|
||||
/// const variant of the [SpHeader::new_fron_single_fields] function. Panics if the passed
|
||||
/// APID exceeds [MAX_APID] or the passed packet sequence count exceeds [MAX_SEQ_COUNT].
|
||||
const fn const_new_from_single_fields(
|
||||
ptype: PacketType,
|
||||
sec_header: bool,
|
||||
apid: u16,
|
||||
seq_flags: SequenceFlags,
|
||||
seq_count: u16,
|
||||
data_len: u16,
|
||||
) -> Self {
|
||||
if seq_count > MAX_SEQ_COUNT {
|
||||
panic!("Sequence count is too large");
|
||||
}
|
||||
if apid > MAX_APID {
|
||||
panic!("APID is too large");
|
||||
}
|
||||
Self {
|
||||
psc: PacketSequenceCtrl::const_new(seq_flags, seq_count),
|
||||
packet_id: PacketId::const_new(ptype, sec_header, apid),
|
||||
data_len,
|
||||
version: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new Space Packet Header instance which can be used to create generic
|
||||
/// Space Packets. This will return [None] if the APID or sequence count argument
|
||||
/// exceed [MAX_APID] or [MAX_SEQ_COUNT] respectively. The version field is set to 0b000.
|
||||
pub fn new_from_single_fields(
|
||||
ptype: PacketType,
|
||||
sec_header: bool,
|
||||
apid: u16,
|
||||
seq_flags: SequenceFlags,
|
||||
seq_count: u16,
|
||||
data_len: u16,
|
||||
) -> Option<Self> {
|
||||
if seq_count > MAX_SEQ_COUNT || apid > MAX_APID {
|
||||
return None;
|
||||
}
|
||||
let mut header = SpHeader::default();
|
||||
header.packet_id.sec_header_flag = sec_header;
|
||||
header.packet_id.apid = apid;
|
||||
header.packet_id.ptype = ptype;
|
||||
header.psc.seq_count = seq_count;
|
||||
header.data_len = data_len;
|
||||
Some(header)
|
||||
Some(SpHeader::const_new_from_single_fields(
|
||||
ptype, sec_header, apid, seq_flags, seq_count, data_len,
|
||||
))
|
||||
}
|
||||
|
||||
/// Helper function for telemetry space packet headers. The packet type field will be
|
||||
/// set accordingly.
|
||||
pub fn tm(apid: u16, seq_count: u16, data_len: u16) -> Option<Self> {
|
||||
Self::new(PacketType::Tm, false, apid, seq_count, data_len)
|
||||
/// Helper function for telemetry space packet headers. The packet type field will be
|
||||
/// set accordingly. The secondary header flag field is set to false.
|
||||
pub fn tm(apid: u16, seq_flags: SequenceFlags, seq_count: u16, data_len: u16) -> Option<Self> {
|
||||
Self::new_from_single_fields(PacketType::Tm, false, apid, seq_flags, seq_count, data_len)
|
||||
}
|
||||
|
||||
/// Helper function for telecommand space packet headers. The packet type field will be
|
||||
/// set accordingly.
|
||||
pub fn tc(apid: u16, seq_count: u16, data_len: u16) -> Option<Self> {
|
||||
Self::new(PacketType::Tc, false, apid, seq_count, data_len)
|
||||
/// Helper function for telemetry space packet headers. The packet type field will be
|
||||
/// set accordingly. The secondary header flag field is set to false.
|
||||
pub fn tc(apid: u16, seq_flags: SequenceFlags, seq_count: u16, data_len: u16) -> Option<Self> {
|
||||
Self::new_from_single_fields(PacketType::Tc, false, apid, seq_flags, seq_count, data_len)
|
||||
}
|
||||
|
||||
/// Variant of [SpHeader::tm] which sets the sequence flag field to [SequenceFlags::Unsegmented]
|
||||
pub fn tm_unseg(apid: u16, seq_count: u16, data_len: u16) -> Option<Self> {
|
||||
Self::tm(apid, SequenceFlags::Unsegmented, seq_count, data_len)
|
||||
}
|
||||
|
||||
/// Variant of [SpHeader::tc] which sets the sequence flag field to [SequenceFlags::Unsegmented]
|
||||
pub fn tc_unseg(apid: u16, seq_count: u16, data_len: u16) -> Option<Self> {
|
||||
Self::tc(apid, SequenceFlags::Unsegmented, seq_count, data_len)
|
||||
}
|
||||
|
||||
//noinspection RsTraitImplementation
|
||||
delegate!(to self.packet_id {
|
||||
/// Returns [false] and fails if the APID exceeds [MAX_APID]
|
||||
pub fn set_apid(&mut self, apid: u16) -> bool;
|
||||
});
|
||||
|
||||
delegate!(to self.psc {
|
||||
/// Returns [false] and fails if the sequence count exceeds [MAX_SEQ_COUNT]
|
||||
pub fn set_seq_count(&mut self, seq_count: u16) -> bool;
|
||||
});
|
||||
|
||||
@ -402,16 +552,38 @@ impl SpHeader {
|
||||
self.packet_id.ptype = packet_type;
|
||||
}
|
||||
|
||||
pub fn from_raw_slice(buf: &[u8]) -> Result<Self, ByteConversionError> {
|
||||
if buf.len() < CCSDS_HEADER_LEN + 1 {
|
||||
/// Create a struct from a raw slice where the fields have network endianness (big).
|
||||
/// This function also returns the remaining part of the passed slice starting past the read
|
||||
/// CCSDS header.
|
||||
pub fn from_be_bytes(buf: &[u8]) -> Result<(Self, &[u8]), ByteConversionError> {
|
||||
if buf.len() < CCSDS_HEADER_LEN {
|
||||
return Err(ByteConversionError::FromSliceTooSmall(SizeMissmatch {
|
||||
found: buf.len(),
|
||||
expected: CCSDS_HEADER_LEN + 1,
|
||||
expected: CCSDS_HEADER_LEN,
|
||||
}));
|
||||
}
|
||||
let zc_header = zc::SpHeader::from_bytes(&buf[0..CCSDS_HEADER_LEN])
|
||||
.ok_or(ByteConversionError::ZeroCopyFromError)?;
|
||||
Ok(Self::from(zc_header))
|
||||
Ok((Self::from(zc_header), &buf[CCSDS_HEADER_LEN..]))
|
||||
}
|
||||
|
||||
/// Write the header to a raw buffer using big endian format. This function returns the
|
||||
/// remaining part of the passed slice starting past the written CCSDS header.
|
||||
pub fn write_to_be_bytes<'a>(
|
||||
&self,
|
||||
buf: &'a mut [u8],
|
||||
) -> Result<&'a mut [u8], ByteConversionError> {
|
||||
if buf.len() < CCSDS_HEADER_LEN {
|
||||
return Err(ByteConversionError::FromSliceTooSmall(SizeMissmatch {
|
||||
found: buf.len(),
|
||||
expected: CCSDS_HEADER_LEN,
|
||||
}));
|
||||
}
|
||||
let zc_header: zc::SpHeader = zc::SpHeader::from(*self);
|
||||
zc_header
|
||||
.to_bytes(&mut buf[0..CCSDS_HEADER_LEN])
|
||||
.ok_or(ByteConversionError::ZeroCopyToError)?;
|
||||
Ok(&mut buf[CCSDS_HEADER_LEN..])
|
||||
}
|
||||
}
|
||||
|
||||
@ -540,23 +712,75 @@ pub mod zc {
|
||||
sph_from_other!(SpHeader, crate::SpHeader);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(all(test, feature = "std"))]
|
||||
mod tests {
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(feature = "serde")]
|
||||
use crate::CcsdsPrimaryHeader;
|
||||
use crate::SpHeader;
|
||||
use crate::{
|
||||
packet_type_in_raw_packet_id, zc, CcsdsPacket, PacketId, PacketSequenceCtrl, PacketType,
|
||||
SequenceFlags,
|
||||
};
|
||||
use crate::{SequenceFlags, SpHeader};
|
||||
use alloc::vec;
|
||||
#[cfg(not(feature = "std"))]
|
||||
use num::pow;
|
||||
#[cfg(feature = "std")]
|
||||
use num_traits::pow;
|
||||
use postcard::from_bytes;
|
||||
#[cfg(feature = "alloc")]
|
||||
use postcard::to_allocvec;
|
||||
#[cfg(feature = "serde")]
|
||||
use postcard::{from_bytes, to_allocvec};
|
||||
|
||||
const CONST_SP: SpHeader = SpHeader::new(
|
||||
PacketId::const_tc(true, 0x36),
|
||||
PacketSequenceCtrl::const_new(SequenceFlags::ContinuationSegment, 0x88),
|
||||
0x90,
|
||||
);
|
||||
|
||||
const PACKET_ID_TM: PacketId = PacketId::const_tm(true, 0x22);
|
||||
|
||||
#[test]
|
||||
fn verify_const_packet_id() {
|
||||
assert_eq!(PACKET_ID_TM.apid(), 0x22);
|
||||
assert_eq!(PACKET_ID_TM.sec_header_flag, true);
|
||||
assert_eq!(PACKET_ID_TM.ptype, PacketType::Tm);
|
||||
let const_tc_id = PacketId::const_tc(true, 0x23);
|
||||
assert_eq!(const_tc_id.ptype, PacketType::Tc);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_packet_id() {
|
||||
let id_default = PacketId::default();
|
||||
assert_eq!(id_default.ptype, PacketType::Tm);
|
||||
assert_eq!(id_default.apid, 0x000);
|
||||
assert_eq!(id_default.sec_header_flag, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_packet_id_ctors() {
|
||||
let packet_id = PacketId::new(PacketType::Tc, true, 0x1ff);
|
||||
assert!(packet_id.is_some());
|
||||
let packet_id = packet_id.unwrap();
|
||||
assert_eq!(packet_id.apid(), 0x1ff);
|
||||
assert_eq!(packet_id.ptype, PacketType::Tc);
|
||||
assert_eq!(packet_id.sec_header_flag, true);
|
||||
let packet_id_tc = PacketId::tc(true, 0x1ff);
|
||||
assert!(packet_id_tc.is_some());
|
||||
let packet_id_tc = packet_id_tc.unwrap();
|
||||
assert_eq!(packet_id_tc, packet_id);
|
||||
let packet_id_tm = PacketId::tm(true, 0x2ff);
|
||||
assert!(packet_id_tm.is_some());
|
||||
let packet_id_tm = packet_id_tm.unwrap();
|
||||
assert_eq!(packet_id_tm.sec_header_flag, true);
|
||||
assert_eq!(packet_id_tm.ptype, PacketType::Tm);
|
||||
assert_eq!(packet_id_tm.apid, 0x2ff);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_const_sp_header() {
|
||||
assert_eq!(CONST_SP.sec_header_flag(), true);
|
||||
assert_eq!(CONST_SP.apid(), 0x36);
|
||||
assert_eq!(
|
||||
CONST_SP.sequence_flags(),
|
||||
SequenceFlags::ContinuationSegment
|
||||
);
|
||||
assert_eq!(CONST_SP.seq_count(), 0x88);
|
||||
assert_eq!(CONST_SP.data_len, 0x90);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_seq_flag_helpers() {
|
||||
@ -639,9 +863,9 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(feature = "serde")]
|
||||
fn test_serde_sph() {
|
||||
let sp_header = SpHeader::tc(0x42, 12, 0).expect("Error creating SP header");
|
||||
let sp_header = SpHeader::tc_unseg(0x42, 12, 0).expect("Error creating SP header");
|
||||
assert_eq!(sp_header.ccsds_version(), 0b000);
|
||||
assert!(sp_header.is_tc());
|
||||
assert!(!sp_header.sec_header_flag());
|
||||
@ -663,7 +887,7 @@ mod tests {
|
||||
assert_eq!(sp_header.ccsds_version(), 0b000);
|
||||
assert_eq!(sp_header.data_len, 0);
|
||||
|
||||
let sp_header = SpHeader::tm(0x7, 22, 36).expect("Error creating SP header");
|
||||
let sp_header = SpHeader::tm_unseg(0x7, 22, 36).expect("Error creating SP header");
|
||||
assert_eq!(sp_header.ccsds_version(), 0b000);
|
||||
assert!(sp_header.is_tm());
|
||||
assert!(!sp_header.sec_header_flag());
|
||||
@ -694,28 +918,69 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sp_header_setters() {
|
||||
let mut sp_header = SpHeader::tc(0x42, 12, 0).expect("Error creating SP header");
|
||||
assert_eq!(sp_header.apid(), 0x42);
|
||||
fn test_setters() {
|
||||
let sp_header = SpHeader::tc(0x42, SequenceFlags::Unsegmented, 25, 0);
|
||||
assert!(sp_header.is_some());
|
||||
let mut sp_header = sp_header.unwrap();
|
||||
sp_header.set_apid(0x12);
|
||||
assert_eq!(sp_header.apid(), 0x12);
|
||||
|
||||
sp_header.set_sec_header_flag();
|
||||
assert!(sp_header.sec_header_flag());
|
||||
sp_header.clear_sec_header_flag();
|
||||
assert!(!sp_header.sec_header_flag());
|
||||
sp_header.set_seq_count(0x45);
|
||||
assert_eq!(sp_header.seq_count(), 0x45);
|
||||
assert_eq!(sp_header.ptype(), PacketType::Tc);
|
||||
sp_header.set_packet_type(PacketType::Tm);
|
||||
assert_eq!(sp_header.ptype(), PacketType::Tm);
|
||||
sp_header.set_seq_count(0x45);
|
||||
assert_eq!(sp_header.seq_count(), 0x45);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tc_ctor() {
|
||||
let sp_header = SpHeader::tc(0x42, SequenceFlags::Unsegmented, 25, 0);
|
||||
assert!(sp_header.is_some());
|
||||
let sp_header = sp_header.unwrap();
|
||||
verify_sp_fields(PacketType::Tc, &sp_header);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tc_ctor_unseg() {
|
||||
let sp_header = SpHeader::tc_unseg(0x42, 25, 0);
|
||||
assert!(sp_header.is_some());
|
||||
let sp_header = sp_header.unwrap();
|
||||
verify_sp_fields(PacketType::Tc, &sp_header);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tm_ctor() {
|
||||
let sp_header = SpHeader::tm(0x42, SequenceFlags::Unsegmented, 25, 0);
|
||||
assert!(sp_header.is_some());
|
||||
let sp_header = sp_header.unwrap();
|
||||
verify_sp_fields(PacketType::Tm, &sp_header);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tm_ctor_unseg() {
|
||||
let sp_header = SpHeader::tm_unseg(0x42, 25, 0);
|
||||
assert!(sp_header.is_some());
|
||||
let sp_header = sp_header.unwrap();
|
||||
verify_sp_fields(PacketType::Tm, &sp_header);
|
||||
}
|
||||
|
||||
fn verify_sp_fields(ptype: PacketType, sp_header: &SpHeader) {
|
||||
assert_eq!(sp_header.ptype(), ptype);
|
||||
assert_eq!(sp_header.sequence_flags(), SequenceFlags::Unsegmented);
|
||||
assert_eq!(sp_header.apid(), 0x42);
|
||||
assert_eq!(sp_header.seq_count(), 25);
|
||||
assert_eq!(sp_header.data_len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zc_sph() {
|
||||
use zerocopy::AsBytes;
|
||||
|
||||
let sp_header = SpHeader::tc(0x7FF, pow(2, 14) - 1, 0).expect("Error creating SP header");
|
||||
let sp_header =
|
||||
SpHeader::tc_unseg(0x7FF, pow(2, 14) - 1, 0).expect("Error creating SP header");
|
||||
assert_eq!(sp_header.ptype(), PacketType::Tc);
|
||||
assert_eq!(sp_header.apid(), 0x7FF);
|
||||
assert_eq!(sp_header.data_len(), 0);
|
||||
|
86
src/tc.rs
86
src/tc.rs
@ -9,7 +9,7 @@
|
||||
//! use spacepackets::ecss::PusPacket;
|
||||
//!
|
||||
//! // Create a ping telecommand with no user application data
|
||||
//! let mut sph = SpHeader::tc(0x02, 0x34, 0).unwrap();
|
||||
//! let mut sph = SpHeader::tc_unseg(0x02, 0x34, 0).unwrap();
|
||||
//! let tc_header = PusTcSecondaryHeader::new_simple(17, 1);
|
||||
//! let pus_tc = PusTc::new(&mut sph, tc_header, None, true);
|
||||
//! println!("{:?}", pus_tc);
|
||||
@ -41,6 +41,7 @@ use crate::{
|
||||
};
|
||||
use core::mem::size_of;
|
||||
use delegate::delegate;
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
use zerocopy::AsBytes;
|
||||
|
||||
@ -66,7 +67,7 @@ pub const ACK_ALL: u8 = AckOpts::Acceptance as u8
|
||||
| AckOpts::Progress as u8
|
||||
| AckOpts::Completion as u8;
|
||||
|
||||
pub trait PusTcSecondaryHeaderT {
|
||||
pub trait GenericPusTcSecondaryHeader {
|
||||
fn pus_version(&self) -> PusVersion;
|
||||
fn ack_flags(&self) -> u8;
|
||||
fn service(&self) -> u8;
|
||||
@ -76,7 +77,7 @@ pub trait PusTcSecondaryHeaderT {
|
||||
|
||||
pub mod zc {
|
||||
use crate::ecss::{PusError, PusVersion};
|
||||
use crate::tc::PusTcSecondaryHeaderT;
|
||||
use crate::tc::GenericPusTcSecondaryHeader;
|
||||
use zerocopy::{AsBytes, FromBytes, NetworkEndian, Unaligned, U16};
|
||||
|
||||
#[derive(FromBytes, AsBytes, Unaligned)]
|
||||
@ -103,7 +104,7 @@ pub mod zc {
|
||||
}
|
||||
}
|
||||
|
||||
impl PusTcSecondaryHeaderT for PusTcSecondaryHeader {
|
||||
impl GenericPusTcSecondaryHeader for PusTcSecondaryHeader {
|
||||
fn pus_version(&self) -> PusVersion {
|
||||
PusVersion::try_from(self.version_ack >> 4 & 0b1111).unwrap_or(PusVersion::Invalid)
|
||||
}
|
||||
@ -136,7 +137,8 @@ pub mod zc {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Copy, Clone, Serialize, Deserialize, Debug)]
|
||||
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct PusTcSecondaryHeader {
|
||||
pub service: u8,
|
||||
pub subservice: u8,
|
||||
@ -145,7 +147,7 @@ pub struct PusTcSecondaryHeader {
|
||||
pub version: PusVersion,
|
||||
}
|
||||
|
||||
impl PusTcSecondaryHeaderT for PusTcSecondaryHeader {
|
||||
impl GenericPusTcSecondaryHeader for PusTcSecondaryHeader {
|
||||
fn pus_version(&self) -> PusVersion {
|
||||
self.version
|
||||
}
|
||||
@ -206,25 +208,26 @@ impl PusTcSecondaryHeader {
|
||||
/// This class models a PUS telecommand. It is the primary data structure to generate the raw byte
|
||||
/// representation of a PUS telecommand or to deserialize from one from raw bytes.
|
||||
///
|
||||
/// This class also derives the [serde::Serialize] and [serde::Deserialize] trait which allows
|
||||
/// to send around TC packets in a raw byte format using a serde provider like
|
||||
/// [postcard](https://docs.rs/postcard/latest/postcard/).
|
||||
/// This class also derives the [serde::Serialize] and [serde::Deserialize] trait if the
|
||||
/// [serde] feature is used, which allows to send around TC packets in a raw byte format using a
|
||||
/// serde provider like [postcard](https://docs.rs/postcard/latest/postcard/).
|
||||
///
|
||||
/// There is no spare bytes support yet.
|
||||
#[derive(PartialEq, Eq, Copy, Clone, Serialize, Deserialize, Debug)]
|
||||
pub struct PusTc<'slice> {
|
||||
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct PusTc<'app_data> {
|
||||
sp_header: SpHeader,
|
||||
pub sec_header: PusTcSecondaryHeader,
|
||||
/// If this is set to false, a manual call to [PusTc::calc_own_crc16] or
|
||||
/// [PusTc::update_packet_fields] is necessary for the serialized or cached CRC16 to be valid.
|
||||
pub calc_crc_on_serialization: bool,
|
||||
#[serde(skip)]
|
||||
raw_data: Option<&'slice [u8]>,
|
||||
app_data: Option<&'slice [u8]>,
|
||||
#[cfg_attr(feature = "serde", serde(skip))]
|
||||
raw_data: Option<&'app_data [u8]>,
|
||||
app_data: Option<&'app_data [u8]>,
|
||||
crc16: Option<u16>,
|
||||
}
|
||||
|
||||
impl<'slice> PusTc<'slice> {
|
||||
impl<'app_data> PusTc<'app_data> {
|
||||
/// Generates a new struct instance.
|
||||
///
|
||||
/// # Arguments
|
||||
@ -240,7 +243,7 @@ impl<'slice> PusTc<'slice> {
|
||||
pub fn new(
|
||||
sp_header: &mut SpHeader,
|
||||
sec_header: PusTcSecondaryHeader,
|
||||
app_data: Option<&'slice [u8]>,
|
||||
app_data: Option<&'app_data [u8]>,
|
||||
set_ccsds_len: bool,
|
||||
) -> Self {
|
||||
sp_header.set_packet_type(PacketType::Tc);
|
||||
@ -260,12 +263,12 @@ impl<'slice> PusTc<'slice> {
|
||||
}
|
||||
|
||||
/// Simplified version of the [PusTc::new] function which allows to only specify service and
|
||||
/// subservice instead of the full PUS TC secondary header
|
||||
/// subservice instead of the full PUS TC secondary header.
|
||||
pub fn new_simple(
|
||||
sph: &mut SpHeader,
|
||||
service: u8,
|
||||
subservice: u8,
|
||||
app_data: Option<&'slice [u8]>,
|
||||
app_data: Option<&'app_data [u8]>,
|
||||
set_ccsds_len: bool,
|
||||
) -> Self {
|
||||
Self::new(
|
||||
@ -276,6 +279,10 @@ impl<'slice> PusTc<'slice> {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn sp_header(&self) -> &SpHeader {
|
||||
&self.sp_header
|
||||
}
|
||||
|
||||
pub fn len_packed(&self) -> usize {
|
||||
let mut length = PUS_TC_MIN_LEN_WITHOUT_APP_DATA;
|
||||
if let Some(app_data) = self.app_data {
|
||||
@ -303,7 +310,7 @@ impl<'slice> PusTc<'slice> {
|
||||
/// used.
|
||||
/// If this was not done or the application data is set or changed after construction,
|
||||
/// this function needs to be called to ensure that the data length field of the CCSDS header
|
||||
/// is set correctly
|
||||
/// is set correctly.
|
||||
pub fn update_ccsds_data_len(&mut self) {
|
||||
self.sp_header.data_len =
|
||||
self.len_packed() as u16 - size_of::<crate::zc::SpHeader>() as u16 - 1;
|
||||
@ -323,7 +330,7 @@ impl<'slice> PusTc<'slice> {
|
||||
self.crc16 = Some(digest.finalize())
|
||||
}
|
||||
|
||||
/// This helper function calls both [PusTc.update_ccsds_data_len] and [PusTc.calc_own_crc16]
|
||||
/// This helper function calls both [PusTc::update_ccsds_data_len] and [PusTc::calc_own_crc16].
|
||||
pub fn update_packet_fields(&mut self) {
|
||||
self.update_ccsds_data_len();
|
||||
self.calc_own_crc16();
|
||||
@ -332,7 +339,6 @@ impl<'slice> PusTc<'slice> {
|
||||
/// Write the raw PUS byte representation to a provided buffer.
|
||||
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() {
|
||||
@ -342,10 +348,7 @@ impl<'slice> PusTc<'slice> {
|
||||
})
|
||||
.into());
|
||||
}
|
||||
sph_zc
|
||||
.to_bytes(&mut slice[curr_idx..curr_idx + CCSDS_HEADER_LEN])
|
||||
.ok_or(ByteConversionError::ZeroCopyToError)?;
|
||||
|
||||
self.sp_header.write_to_be_bytes(slice)?;
|
||||
curr_idx += CCSDS_HEADER_LEN;
|
||||
let sec_header = zc::PusTcSecondaryHeader::try_from(self.sec_header).unwrap();
|
||||
sec_header
|
||||
@ -401,18 +404,16 @@ impl<'slice> PusTc<'slice> {
|
||||
}
|
||||
|
||||
/// Create a [PusTc] instance from a raw slice. On success, it returns a tuple containing
|
||||
/// the instance and the found byte length of the packet
|
||||
pub fn from_bytes(slice: &'slice [u8]) -> Result<(Self, usize), PusError> {
|
||||
/// the instance and the found byte length of the packet.
|
||||
pub fn from_bytes(slice: &'app_data [u8]) -> Result<(Self, usize), PusError> {
|
||||
let raw_data_len = slice.len();
|
||||
if raw_data_len < PUS_TC_MIN_LEN_WITHOUT_APP_DATA {
|
||||
return Err(PusError::RawDataTooShort(raw_data_len));
|
||||
}
|
||||
let mut current_idx = 0;
|
||||
let sph =
|
||||
crate::zc::SpHeader::from_bytes(&slice[current_idx..current_idx + CCSDS_HEADER_LEN])
|
||||
.ok_or(ByteConversionError::ZeroCopyFromError)?;
|
||||
let (sp_header, _) = SpHeader::from_be_bytes(&slice[0..CCSDS_HEADER_LEN])?;
|
||||
current_idx += CCSDS_HEADER_LEN;
|
||||
let total_len = sph.total_len();
|
||||
let total_len = sp_header.total_len();
|
||||
if raw_data_len < total_len || total_len < PUS_TC_MIN_LEN_WITHOUT_APP_DATA {
|
||||
return Err(PusError::RawDataTooShort(raw_data_len));
|
||||
}
|
||||
@ -423,7 +424,7 @@ impl<'slice> PusTc<'slice> {
|
||||
current_idx += PUC_TC_SECONDARY_HEADER_LEN;
|
||||
let raw_data = &slice[0..total_len];
|
||||
let pus_tc = PusTc {
|
||||
sp_header: SpHeader::from(sph),
|
||||
sp_header,
|
||||
sec_header: PusTcSecondaryHeader::try_from(sec_header).unwrap(),
|
||||
raw_data: Some(raw_data),
|
||||
app_data: user_data_from_raw(current_idx, total_len, raw_data_len, slice)?,
|
||||
@ -434,7 +435,7 @@ impl<'slice> PusTc<'slice> {
|
||||
Ok((pus_tc, total_len))
|
||||
}
|
||||
|
||||
pub fn raw(&self) -> Option<&'slice [u8]> {
|
||||
pub fn raw(&self) -> Option<&'app_data [u8]> {
|
||||
self.raw_data
|
||||
}
|
||||
}
|
||||
@ -462,7 +463,7 @@ impl PusPacket for PusTc<'_> {
|
||||
}
|
||||
|
||||
//noinspection RsTraitImplementation
|
||||
impl PusTcSecondaryHeaderT for PusTc<'_> {
|
||||
impl GenericPusTcSecondaryHeader for PusTc<'_> {
|
||||
delegate!(to self.sec_header {
|
||||
fn pus_version(&self) -> PusVersion;
|
||||
fn service(&self) -> u8;
|
||||
@ -472,29 +473,29 @@ impl PusTcSecondaryHeaderT for PusTc<'_> {
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(all(test, feature = "std"))]
|
||||
mod tests {
|
||||
use crate::ecss::PusVersion::PusC;
|
||||
use crate::ecss::{PusError, PusPacket};
|
||||
use crate::tc::ACK_ALL;
|
||||
use crate::tc::{PusTc, PusTcSecondaryHeader, PusTcSecondaryHeaderT};
|
||||
use crate::tc::{GenericPusTcSecondaryHeader, PusTc, PusTcSecondaryHeader};
|
||||
use crate::{ByteConversionError, SpHeader};
|
||||
use crate::{CcsdsPacket, SequenceFlags};
|
||||
use alloc::vec::Vec;
|
||||
|
||||
fn base_ping_tc_full_ctor() -> PusTc<'static> {
|
||||
let mut sph = SpHeader::tc(0x02, 0x34, 0).unwrap();
|
||||
let mut sph = SpHeader::tc_unseg(0x02, 0x34, 0).unwrap();
|
||||
let tc_header = PusTcSecondaryHeader::new_simple(17, 1);
|
||||
PusTc::new(&mut sph, tc_header, None, true)
|
||||
}
|
||||
|
||||
fn base_ping_tc_simple_ctor() -> PusTc<'static> {
|
||||
let mut sph = SpHeader::tc(0x02, 0x34, 0).unwrap();
|
||||
let mut sph = SpHeader::tc_unseg(0x02, 0x34, 0).unwrap();
|
||||
PusTc::new_simple(&mut sph, 17, 1, None, true)
|
||||
}
|
||||
|
||||
fn base_ping_tc_simple_ctor_with_app_data(app_data: &'static [u8]) -> PusTc<'static> {
|
||||
let mut sph = SpHeader::tc(0x02, 0x34, 0).unwrap();
|
||||
let mut sph = SpHeader::tc_unseg(0x02, 0x34, 0).unwrap();
|
||||
PusTc::new_simple(&mut sph, 17, 1, Some(app_data), true)
|
||||
}
|
||||
|
||||
@ -534,7 +535,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_update_func() {
|
||||
let mut sph = SpHeader::tc(0x02, 0x34, 0).unwrap();
|
||||
let mut sph = SpHeader::tc_unseg(0x02, 0x34, 0).unwrap();
|
||||
let mut tc = PusTc::new_simple(&mut sph, 17, 1, None, false);
|
||||
tc.calc_crc_on_serialization = false;
|
||||
assert_eq!(tc.data_len(), 0);
|
||||
@ -560,7 +561,6 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "alloc")]
|
||||
fn test_vec_ser_deser() {
|
||||
let pus_tc = base_ping_tc_simple_ctor();
|
||||
let mut test_vec = Vec::new();
|
||||
@ -625,7 +625,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_write_buf_too_msall() {
|
||||
fn test_write_buf_too_small() {
|
||||
let pus_tc = base_ping_tc_simple_ctor();
|
||||
let mut test_buf = [0; 12];
|
||||
let res = pus_tc.write_to_bytes(test_buf.as_mut_slice());
|
||||
@ -698,7 +698,7 @@ mod tests {
|
||||
assert_eq!(tc.apid(), 0x02);
|
||||
assert_eq!(tc.ack_flags(), ACK_ALL);
|
||||
assert_eq!(tc.len_packed(), exp_full_len);
|
||||
let mut comp_header = SpHeader::tc(0x02, 0x34, exp_full_len as u16 - 7).unwrap();
|
||||
let mut comp_header = SpHeader::tc_unseg(0x02, 0x34, exp_full_len as u16 - 7).unwrap();
|
||||
comp_header.set_sec_header_flag();
|
||||
assert_eq!(tc.sp_header, comp_header);
|
||||
}
|
||||
|
469
src/time.rs
469
src/time.rs
@ -1,469 +0,0 @@
|
||||
//! CCSDS Time Code Formats according to [CCSDS 301.0-B-4](https://public.ccsds.org/Pubs/301x0b4e1.pdf)
|
||||
use crate::{ByteConversionError, SizeMissmatch};
|
||||
use chrono::{DateTime, LocalResult, TimeZone, Utc};
|
||||
|
||||
#[allow(unused_imports)]
|
||||
#[cfg(not(feature = "std"))]
|
||||
use num_traits::float::FloatCore;
|
||||
|
||||
use crate::time::CcsdsTimeCodes::Cds;
|
||||
#[cfg(feature = "std")]
|
||||
use std::time::{SystemTime, SystemTimeError};
|
||||
|
||||
pub const CDS_SHORT_LEN: usize = 7;
|
||||
pub const DAYS_CCSDS_TO_UNIX: i32 = -4383;
|
||||
pub const SECONDS_PER_DAY: u32 = 86400;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||
pub enum CcsdsTimeCodes {
|
||||
None = 0,
|
||||
CucCcsdsEpoch = 0b001,
|
||||
CucAgencyEpoch = 0b010,
|
||||
Cds = 0b100,
|
||||
Ccs = 0b101,
|
||||
}
|
||||
|
||||
const CDS_SHORT_P_FIELD: u8 = (CcsdsTimeCodes::Cds as u8) << 4;
|
||||
|
||||
impl TryFrom<u8> for CcsdsTimeCodes {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
x if x == CcsdsTimeCodes::None as u8 => Ok(CcsdsTimeCodes::None),
|
||||
x if x == CcsdsTimeCodes::CucCcsdsEpoch as u8 => Ok(CcsdsTimeCodes::CucCcsdsEpoch),
|
||||
x if x == CcsdsTimeCodes::CucAgencyEpoch as u8 => Ok(CcsdsTimeCodes::CucAgencyEpoch),
|
||||
x if x == CcsdsTimeCodes::Cds as u8 => Ok(CcsdsTimeCodes::Cds),
|
||||
x if x == CcsdsTimeCodes::Ccs as u8 => Ok(CcsdsTimeCodes::Ccs),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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(ByteConversionError),
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
||||
pub fn seconds_since_epoch() -> f64 {
|
||||
SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.expect("System time generation failed")
|
||||
.as_secs_f64()
|
||||
}
|
||||
|
||||
/// Convert UNIX days to CCSDS days
|
||||
///
|
||||
/// - CCSDS epoch: 1958 January 1
|
||||
/// - UNIX Epoch: 1970 January 1
|
||||
pub const fn unix_to_ccsds_days(unix_days: i32) -> i32 {
|
||||
unix_days - DAYS_CCSDS_TO_UNIX
|
||||
}
|
||||
|
||||
/// Convert CCSDS days to UNIX days
|
||||
///
|
||||
/// - CCSDS epoch: 1958 January 1
|
||||
/// - UNIX Epoch: 1970 January 1
|
||||
pub const fn ccsds_to_unix_days(ccsds_days: i32) -> i32 {
|
||||
ccsds_days + DAYS_CCSDS_TO_UNIX
|
||||
}
|
||||
|
||||
pub trait TimeWriter {
|
||||
fn write_to_bytes(&self, bytes: &mut [u8]) -> Result<(), TimestampError>;
|
||||
}
|
||||
|
||||
pub trait TimeReader {
|
||||
fn from_bytes(buf: &[u8]) -> Result<Self, TimestampError>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
/// Trait for generic CCSDS time providers
|
||||
pub trait CcsdsTimeProvider {
|
||||
fn len_as_bytes(&self) -> usize;
|
||||
|
||||
/// Returns the pfield of the time provider. The pfield can have one or two bytes depending
|
||||
/// on the extension bit (first bit). The time provider should returns a tuple where the first
|
||||
/// entry denotes the length of the pfield and the second entry is the value of the pfield
|
||||
/// in big endian format.
|
||||
fn p_field(&self) -> (usize, [u8; 2]);
|
||||
fn ccdsd_time_code(&self) -> CcsdsTimeCodes;
|
||||
fn unix_seconds(&self) -> i64;
|
||||
fn date_time(&self) -> Option<DateTime<Utc>>;
|
||||
}
|
||||
|
||||
/// This object is the abstraction for the CCSDS Day Segmented Time Code (CDS).
|
||||
///
|
||||
/// It has the capability to generate and read timestamps as specified in the CCSDS 301.0-B-4
|
||||
/// section 3.3
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use spacepackets::time::{CdsShortTimeProvider, TimeWriter};
|
||||
/// use spacepackets::time::CcsdsTimeCodes::Cds;
|
||||
/// let timestamp_now = CdsShortTimeProvider::from_now().unwrap();
|
||||
/// let mut raw_stamp = [0; 7];
|
||||
/// timestamp_now.write_to_bytes(&mut raw_stamp).unwrap();
|
||||
/// assert_eq!((raw_stamp[0] >> 4) & 0b111, Cds as u8);
|
||||
/// ```
|
||||
#[derive(Debug, Copy, Clone, Default)]
|
||||
pub struct CdsShortTimeProvider {
|
||||
ccsds_days: u16,
|
||||
ms_of_day: u32,
|
||||
unix_seconds: i64,
|
||||
date_time: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
impl CdsShortTimeProvider {
|
||||
pub fn new(ccsds_days: u16, ms_of_day: u32) -> Self {
|
||||
let provider = Self {
|
||||
ccsds_days,
|
||||
ms_of_day,
|
||||
unix_seconds: 0,
|
||||
date_time: None,
|
||||
};
|
||||
let unix_days_seconds =
|
||||
ccsds_to_unix_days(ccsds_days as i32) as i64 * SECONDS_PER_DAY as i64;
|
||||
provider.setup(unix_days_seconds as i64, ms_of_day.into())
|
||||
}
|
||||
|
||||
#[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();
|
||||
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;
|
||||
let provider = Self {
|
||||
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,
|
||||
unix_seconds: 0,
|
||||
date_time: None,
|
||||
};
|
||||
Ok(provider.setup(unix_days_seconds as i64, ms_of_day))
|
||||
}
|
||||
|
||||
#[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();
|
||||
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);
|
||||
self
|
||||
}
|
||||
|
||||
#[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())
|
||||
}
|
||||
|
||||
pub fn ms_of_day(seconds_since_epoch: f64) -> u32 {
|
||||
let fraction_ms = seconds_since_epoch - seconds_since_epoch.floor();
|
||||
let ms_of_day: u32 =
|
||||
(((seconds_since_epoch.floor() as u32 % SECONDS_PER_DAY) * 1000) as f64 + fraction_ms)
|
||||
.floor() as u32;
|
||||
ms_of_day
|
||||
}
|
||||
|
||||
fn calc_unix_seconds(&mut self, unix_days_seconds: i64, ms_of_day: u64) {
|
||||
self.unix_seconds = unix_days_seconds;
|
||||
let seconds_of_day = (ms_of_day / 1000) as i64;
|
||||
if self.unix_seconds < 0 {
|
||||
self.unix_seconds -= seconds_of_day;
|
||||
} else {
|
||||
self.unix_seconds += seconds_of_day;
|
||||
}
|
||||
}
|
||||
|
||||
fn calc_date_time(&mut self, ms_since_last_second: u32) {
|
||||
assert!(ms_since_last_second < 1000, "Invalid MS since last second");
|
||||
let ns_since_last_sec = ms_since_last_second * 1e6 as u32;
|
||||
if let LocalResult::Single(val) = Utc.timestamp_opt(self.unix_seconds, ns_since_last_sec) {
|
||||
self.date_time = Some(val);
|
||||
} else {
|
||||
self.date_time = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CcsdsTimeProvider for CdsShortTimeProvider {
|
||||
fn len_as_bytes(&self) -> usize {
|
||||
CDS_SHORT_LEN
|
||||
}
|
||||
|
||||
fn p_field(&self) -> (usize, [u8; 2]) {
|
||||
(1, [CDS_SHORT_P_FIELD, 0])
|
||||
}
|
||||
|
||||
fn ccdsd_time_code(&self) -> CcsdsTimeCodes {
|
||||
Cds
|
||||
}
|
||||
|
||||
fn unix_seconds(&self) -> i64 {
|
||||
self.unix_seconds
|
||||
}
|
||||
|
||||
fn date_time(&self) -> Option<DateTime<Utc>> {
|
||||
self.date_time
|
||||
}
|
||||
}
|
||||
|
||||
impl TimeWriter for CdsShortTimeProvider {
|
||||
fn write_to_bytes(&self, buf: &mut [u8]) -> Result<(), TimestampError> {
|
||||
if buf.len() < self.len_as_bytes() {
|
||||
return Err(TimestampError::OtherPacketError(
|
||||
ByteConversionError::ToSliceTooSmall(SizeMissmatch {
|
||||
expected: self.len_as_bytes(),
|
||||
found: buf.len(),
|
||||
}),
|
||||
));
|
||||
}
|
||||
buf[0] = CDS_SHORT_P_FIELD;
|
||||
buf[1..3].copy_from_slice(self.ccsds_days.to_be_bytes().as_slice());
|
||||
buf[3..7].copy_from_slice(self.ms_of_day.to_be_bytes().as_slice());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl TimeReader for CdsShortTimeProvider {
|
||||
fn from_bytes(buf: &[u8]) -> Result<Self, TimestampError> {
|
||||
if buf.len() < CDS_SHORT_LEN {
|
||||
return Err(TimestampError::OtherPacketError(
|
||||
ByteConversionError::FromSliceTooSmall(SizeMissmatch {
|
||||
expected: CDS_SHORT_LEN,
|
||||
found: buf.len(),
|
||||
}),
|
||||
));
|
||||
}
|
||||
let pfield = buf[0];
|
||||
match CcsdsTimeCodes::try_from(pfield >> 4 & 0b111) {
|
||||
Ok(cds_type) => match cds_type {
|
||||
Cds => (),
|
||||
_ => return Err(TimestampError::InvalidTimeCode(Cds, cds_type as u8)),
|
||||
},
|
||||
_ => return Err(TimestampError::InvalidTimeCode(Cds, pfield >> 4 & 0b111)),
|
||||
};
|
||||
let ccsds_days: u16 = u16::from_be_bytes(buf[1..3].try_into().unwrap());
|
||||
let ms_of_day: u32 = u32::from_be_bytes(buf[3..7].try_into().unwrap());
|
||||
Ok(Self::new(ccsds_days, ms_of_day))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::time::TimestampError::{InvalidTimeCode, OtherPacketError};
|
||||
use crate::ByteConversionError::{FromSliceTooSmall, ToSliceTooSmall};
|
||||
use alloc::format;
|
||||
use chrono::{Datelike, Timelike};
|
||||
|
||||
#[test]
|
||||
fn test_creation() {
|
||||
assert_eq!(unix_to_ccsds_days(DAYS_CCSDS_TO_UNIX), 0);
|
||||
assert_eq!(ccsds_to_unix_days(0), DAYS_CCSDS_TO_UNIX);
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[test]
|
||||
fn test_get_current_time() {
|
||||
let sec_floats = seconds_since_epoch();
|
||||
assert!(sec_floats > 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_time_stamp_zero_args() {
|
||||
let time_stamper = CdsShortTimeProvider::new(0, 0);
|
||||
assert_eq!(
|
||||
time_stamper.unix_seconds(),
|
||||
(DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32) as i64
|
||||
);
|
||||
assert_eq!(time_stamper.ccdsd_time_code(), Cds);
|
||||
assert_eq!(time_stamper.p_field(), (1, [(Cds as u8) << 4, 0]));
|
||||
let date_time = time_stamper.date_time().unwrap();
|
||||
assert_eq!(date_time.year(), 1958);
|
||||
assert_eq!(date_time.month(), 1);
|
||||
assert_eq!(date_time.day(), 1);
|
||||
assert_eq!(date_time.hour(), 0);
|
||||
assert_eq!(date_time.minute(), 0);
|
||||
assert_eq!(date_time.second(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_time_stamp_unix_epoch() {
|
||||
let time_stamper = CdsShortTimeProvider::new((-DAYS_CCSDS_TO_UNIX) as u16, 0);
|
||||
assert_eq!(time_stamper.unix_seconds(), 0);
|
||||
let date_time = time_stamper.date_time().unwrap();
|
||||
assert_eq!(date_time.year(), 1970);
|
||||
assert_eq!(date_time.month(), 1);
|
||||
assert_eq!(date_time.day(), 1);
|
||||
assert_eq!(date_time.hour(), 0);
|
||||
assert_eq!(date_time.minute(), 0);
|
||||
assert_eq!(date_time.second(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_write() {
|
||||
let mut buf = [0; 16];
|
||||
let time_stamper_0 = CdsShortTimeProvider::new(0, 0);
|
||||
let mut res = time_stamper_0.write_to_bytes(&mut buf);
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(buf[0], (CcsdsTimeCodes::Cds as u8) << 4);
|
||||
assert_eq!(
|
||||
u16::from_be_bytes(buf[1..3].try_into().expect("Byte conversion failed")),
|
||||
0
|
||||
);
|
||||
assert_eq!(
|
||||
u32::from_be_bytes(buf[3..7].try_into().expect("Byte conversion failed")),
|
||||
0
|
||||
);
|
||||
let time_stamper_1 = CdsShortTimeProvider::new(u16::MAX - 1, u32::MAX - 1);
|
||||
res = time_stamper_1.write_to_bytes(&mut buf);
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(buf[0], (CcsdsTimeCodes::Cds as u8) << 4);
|
||||
assert_eq!(
|
||||
u16::from_be_bytes(buf[1..3].try_into().expect("Byte conversion failed")),
|
||||
u16::MAX - 1
|
||||
);
|
||||
assert_eq!(
|
||||
u32::from_be_bytes(buf[3..7].try_into().expect("Byte conversion failed")),
|
||||
u32::MAX - 1
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_faulty_write_buf_too_small() {
|
||||
let mut buf = [0; 7];
|
||||
let time_stamper = CdsShortTimeProvider::new(u16::MAX - 1, u32::MAX - 1);
|
||||
for i in 0..6 {
|
||||
let res = time_stamper.write_to_bytes(&mut buf[0..i]);
|
||||
assert!(res.is_err());
|
||||
match res.unwrap_err() {
|
||||
OtherPacketError(ToSliceTooSmall(missmatch)) => {
|
||||
assert_eq!(missmatch.found, i);
|
||||
assert_eq!(missmatch.expected, 7);
|
||||
}
|
||||
_ => panic!(
|
||||
"{}",
|
||||
format!("Invalid error {:?} detected", res.unwrap_err())
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_faulty_read_buf_too_small() {
|
||||
let buf = [0; 7];
|
||||
for i in 0..6 {
|
||||
let res = CdsShortTimeProvider::from_bytes(&buf[0..i]);
|
||||
assert!(res.is_err());
|
||||
match res.unwrap_err() {
|
||||
InvalidTimeCode(_, _) => {
|
||||
panic!("Unexpected error");
|
||||
}
|
||||
OtherPacketError(e) => match e {
|
||||
FromSliceTooSmall(missmatch) => {
|
||||
assert_eq!(missmatch.found, i);
|
||||
assert_eq!(missmatch.expected, 7);
|
||||
}
|
||||
_ => panic!("{}", format!("Invalid error {:?} detected", e)),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_faulty_invalid_pfield() {
|
||||
let mut buf = [0; 16];
|
||||
let time_stamper_0 = CdsShortTimeProvider::new(0, 0);
|
||||
let res = time_stamper_0.write_to_bytes(&mut buf);
|
||||
assert!(res.is_ok());
|
||||
buf[0] = 0;
|
||||
let res = CdsShortTimeProvider::from_bytes(&buf);
|
||||
assert!(res.is_err());
|
||||
let err = res.unwrap_err();
|
||||
match err {
|
||||
InvalidTimeCode(code, raw) => {
|
||||
assert_eq!(code, Cds);
|
||||
assert_eq!(raw, 0);
|
||||
}
|
||||
OtherPacketError(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_reading() {
|
||||
let mut buf = [0; 16];
|
||||
let time_stamper = CdsShortTimeProvider::new(u16::MAX - 1, u32::MAX - 1);
|
||||
let res = time_stamper.write_to_bytes(&mut buf);
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(buf[0], (CcsdsTimeCodes::Cds as u8) << 4);
|
||||
assert_eq!(
|
||||
u16::from_be_bytes(buf[1..3].try_into().expect("Byte conversion failed")),
|
||||
u16::MAX - 1
|
||||
);
|
||||
assert_eq!(
|
||||
u32::from_be_bytes(buf[3..7].try_into().expect("Byte conversion failed")),
|
||||
u32::MAX - 1
|
||||
);
|
||||
|
||||
let read_stamp = CdsShortTimeProvider::from_bytes(&buf).expect("Reading timestamp failed");
|
||||
assert_eq!(read_stamp.ccsds_days, u16::MAX - 1);
|
||||
assert_eq!(read_stamp.ms_of_day, u32::MAX - 1);
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[test]
|
||||
fn test_time_now() {
|
||||
let timestamp_now = CdsShortTimeProvider::from_now().unwrap();
|
||||
let compare_stamp = Utc::now();
|
||||
let dt = timestamp_now.date_time().unwrap();
|
||||
if compare_stamp.year() > dt.year() {
|
||||
assert_eq!(compare_stamp.year() - dt.year(), 1);
|
||||
} else {
|
||||
assert_eq!(dt.year(), compare_stamp.year());
|
||||
}
|
||||
generic_dt_property_equality_check(dt.month(), compare_stamp.month(), 1, 12);
|
||||
|
||||
assert_eq!(dt.day(), compare_stamp.day());
|
||||
if compare_stamp.day() < dt.day() {
|
||||
assert!(dt.day() >= 28);
|
||||
assert_eq!(compare_stamp.day(), 1);
|
||||
} else if compare_stamp.day() > dt.day() {
|
||||
assert_eq!(compare_stamp.day() - dt.day(), 1);
|
||||
} else {
|
||||
assert_eq!(compare_stamp.day(), dt.day());
|
||||
}
|
||||
generic_dt_property_equality_check(dt.hour(), compare_stamp.hour(), 0, 23);
|
||||
generic_dt_property_equality_check(dt.minute(), compare_stamp.minute(), 0, 59);
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
fn generic_dt_property_equality_check(first: u32, second: u32, start: u32, end: u32) {
|
||||
if second < first {
|
||||
assert_eq!(second, start);
|
||||
assert_eq!(first, end);
|
||||
} else if second > first {
|
||||
assert_eq!(second - first, 1);
|
||||
} else {
|
||||
assert_eq!(first, second);
|
||||
}
|
||||
}
|
||||
}
|
130
src/time/ascii.rs
Normal file
130
src/time/ascii.rs
Normal file
@ -0,0 +1,130 @@
|
||||
//! Module to generate the ASCII timecodes specified in
|
||||
//! [CCSDS 301.0-B-4](https://public.ccsds.org/Pubs/301x0b4e1.pdf) section 3.5 .
|
||||
//! See [chrono::DateTime::format] for a usage example of the generated
|
||||
//! [chrono::format::DelayedFormat] structs.
|
||||
#[cfg(feature = "alloc")]
|
||||
use chrono::{
|
||||
format::{DelayedFormat, StrftimeItems},
|
||||
DateTime, Utc,
|
||||
};
|
||||
|
||||
/// Tuple of format string and formatted size for time code A.
|
||||
///
|
||||
/// Format: YYYY-MM-DDThh:mm:ss.ddd
|
||||
///
|
||||
/// Three digits are used for the decimal fraction
|
||||
pub const FMT_STR_CODE_A_WITH_SIZE: (&str, usize) = ("%FT%T%.3f", 23);
|
||||
/// Tuple of format string and formatted size for time code A.
|
||||
///
|
||||
/// Format: YYYY-MM-DDThh:mm:ss.dddZ
|
||||
///
|
||||
/// Three digits are used for the decimal fraction and a terminator is added at the end.
|
||||
pub const FMT_STR_CODE_A_TERMINATED_WITH_SIZE: (&str, usize) = ("%FT%T%.3fZ", 24);
|
||||
|
||||
/// Tuple of format string and formatted size for time code A.
|
||||
///
|
||||
/// Format: YYYY-DDDThh:mm:ss.ddd
|
||||
///
|
||||
/// Three digits are used for the decimal fraction
|
||||
pub const FMT_STR_CODE_B_WITH_SIZE: (&str, usize) = ("%Y-%jT%T%.3f", 21);
|
||||
/// Tuple of format string and formatted size for time code A.
|
||||
///
|
||||
/// Format: YYYY-DDDThh:mm:ss.dddZ
|
||||
///
|
||||
/// Three digits are used for the decimal fraction and a terminator is added at the end.
|
||||
pub const FMT_STR_CODE_B_TERMINATED_WITH_SIZE: (&str, usize) = ("%Y-%jT%T%.3fZ", 22);
|
||||
|
||||
/// Generates a time code formatter using the [FMT_STR_CODE_A_WITH_SIZE] format.
|
||||
#[cfg(feature = "alloc")]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
|
||||
pub fn generate_time_code_a(date: &DateTime<Utc>) -> DelayedFormat<StrftimeItems<'static>> {
|
||||
date.format(FMT_STR_CODE_A_WITH_SIZE.0)
|
||||
}
|
||||
|
||||
/// Generates a time code formatter using the [FMT_STR_CODE_A_TERMINATED_WITH_SIZE] format.
|
||||
#[cfg(feature = "alloc")]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
|
||||
pub fn generate_time_code_a_terminated(
|
||||
date: &DateTime<Utc>,
|
||||
) -> DelayedFormat<StrftimeItems<'static>> {
|
||||
date.format(FMT_STR_CODE_A_TERMINATED_WITH_SIZE.0)
|
||||
}
|
||||
|
||||
/// Generates a time code formatter using the [FMT_STR_CODE_B_WITH_SIZE] format.
|
||||
#[cfg(feature = "alloc")]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
|
||||
pub fn generate_time_code_b(date: &DateTime<Utc>) -> DelayedFormat<StrftimeItems<'static>> {
|
||||
date.format(FMT_STR_CODE_B_WITH_SIZE.0)
|
||||
}
|
||||
|
||||
/// Generates a time code formatter using the [FMT_STR_CODE_B_TERMINATED_WITH_SIZE] format.
|
||||
#[cfg(feature = "alloc")]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
|
||||
pub fn generate_time_code_b_terminated(
|
||||
date: &DateTime<Utc>,
|
||||
) -> DelayedFormat<StrftimeItems<'static>> {
|
||||
date.format(FMT_STR_CODE_B_TERMINATED_WITH_SIZE.0)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use chrono::Utc;
|
||||
use std::format;
|
||||
|
||||
#[test]
|
||||
fn test_ascii_timestamp_a_unterminated() {
|
||||
let date = Utc::now();
|
||||
let stamp_formatter = generate_time_code_a(&date);
|
||||
let stamp = format!("{}", stamp_formatter);
|
||||
let t_sep = stamp.find("T");
|
||||
assert!(t_sep.is_some());
|
||||
assert_eq!(t_sep.unwrap(), 10);
|
||||
assert_eq!(stamp.len(), FMT_STR_CODE_A_WITH_SIZE.1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ascii_timestamp_a_terminated() {
|
||||
let date = Utc::now();
|
||||
let stamp_formatter = generate_time_code_a_terminated(&date);
|
||||
let stamp = format!("{}", stamp_formatter);
|
||||
let t_sep = stamp.find("T");
|
||||
assert!(t_sep.is_some());
|
||||
assert_eq!(t_sep.unwrap(), 10);
|
||||
let z_terminator = stamp.find("Z");
|
||||
assert!(z_terminator.is_some());
|
||||
assert_eq!(
|
||||
z_terminator.unwrap(),
|
||||
FMT_STR_CODE_A_TERMINATED_WITH_SIZE.1 - 1
|
||||
);
|
||||
assert_eq!(stamp.len(), FMT_STR_CODE_A_TERMINATED_WITH_SIZE.1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ascii_timestamp_b_unterminated() {
|
||||
let date = Utc::now();
|
||||
let stamp_formatter = generate_time_code_b(&date);
|
||||
let stamp = format!("{}", stamp_formatter);
|
||||
let t_sep = stamp.find("T");
|
||||
assert!(t_sep.is_some());
|
||||
assert_eq!(t_sep.unwrap(), 8);
|
||||
assert_eq!(stamp.len(), FMT_STR_CODE_B_WITH_SIZE.1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ascii_timestamp_b_terminated() {
|
||||
let date = Utc::now();
|
||||
let stamp_formatter = generate_time_code_b_terminated(&date);
|
||||
let stamp = format!("{}", stamp_formatter);
|
||||
let t_sep = stamp.find("T");
|
||||
assert!(t_sep.is_some());
|
||||
assert_eq!(t_sep.unwrap(), 8);
|
||||
let z_terminator = stamp.find("Z");
|
||||
assert!(z_terminator.is_some());
|
||||
assert_eq!(
|
||||
z_terminator.unwrap(),
|
||||
FMT_STR_CODE_B_TERMINATED_WITH_SIZE.1 - 1
|
||||
);
|
||||
assert_eq!(stamp.len(), FMT_STR_CODE_B_TERMINATED_WITH_SIZE.1);
|
||||
}
|
||||
}
|
956
src/time/cds.rs
Normal file
956
src/time/cds.rs
Normal file
@ -0,0 +1,956 @@
|
||||
//! Module to generate or read CCSDS Day Segmented (CDS) timestamps as specified in
|
||||
//! [CCSDS 301.0-B-4](https://public.ccsds.org/Pubs/301x0b4e1.pdf) section 3.3 .
|
||||
//!
|
||||
//! The core data structure to do this is the [cds::TimeProvider] struct.
|
||||
use super::*;
|
||||
use crate::private::Sealed;
|
||||
use core::fmt::Debug;
|
||||
|
||||
/// Base value for the preamble field for a time field parser to determine the time field type.
|
||||
pub const P_FIELD_BASE: u8 = (CcsdsTimeCodes::Cds as u8) << 4;
|
||||
pub const MIN_CDS_FIELD_LEN: usize = 7;
|
||||
|
||||
/// Generic trait implemented by token structs to specify the length of day field at type
|
||||
/// level. This trait is only meant to be implemented in this crate and therefore sealed.
|
||||
pub trait ProvidesDaysLength: Sealed {
|
||||
type FieldType: Copy + Clone + TryFrom<i32>;
|
||||
}
|
||||
|
||||
/// Type level token to be used as a generic parameter to [TimeProvider].
|
||||
#[derive(Debug, PartialEq, Eq, Default)]
|
||||
pub struct DaysLen16Bits {}
|
||||
|
||||
impl Sealed for DaysLen16Bits {}
|
||||
impl ProvidesDaysLength for DaysLen16Bits {
|
||||
type FieldType = u16;
|
||||
}
|
||||
|
||||
/// Type level token to be used as a generic parameter to [TimeProvider].
|
||||
#[derive(Debug, PartialEq, Eq, Default)]
|
||||
pub struct DaysLen24Bits {}
|
||||
impl Sealed for DaysLen24Bits {}
|
||||
impl ProvidesDaysLength for DaysLen24Bits {
|
||||
type FieldType = u32;
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub enum LengthOfDaySegment {
|
||||
Short16Bits = 0,
|
||||
Long24Bits = 1,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub enum SubmillisPrecision {
|
||||
Absent,
|
||||
Microseconds(u16),
|
||||
Picoseconds(u32),
|
||||
Reserved,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub enum CdsError {
|
||||
/// CCSDS days value exceeds maximum allowed size or is negative
|
||||
InvalidCcsdsDays(i64),
|
||||
/// There are distinct constructors depending on the days field width detected in the preamble
|
||||
/// field. This error will be returned if there is a missmatch.
|
||||
InvalidCtorForDaysOfLenInPreamble(LengthOfDaySegment),
|
||||
}
|
||||
|
||||
impl Display for CdsError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
|
||||
match self {
|
||||
CdsError::InvalidCcsdsDays(days) => {
|
||||
write!(f, "invalid ccsds days {}", days)
|
||||
}
|
||||
CdsError::InvalidCtorForDaysOfLenInPreamble(length_of_day) => {
|
||||
write!(
|
||||
f,
|
||||
"wrong constructor for length of day {:?} detected in preamble",
|
||||
length_of_day
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl Error for CdsError {}
|
||||
pub fn length_of_day_segment_from_pfield(pfield: u8) -> LengthOfDaySegment {
|
||||
if (pfield >> 2) & 0b1 == 1 {
|
||||
return LengthOfDaySegment::Long24Bits;
|
||||
}
|
||||
LengthOfDaySegment::Short16Bits
|
||||
}
|
||||
pub fn precision_from_pfield(pfield: u8) -> SubmillisPrecision {
|
||||
match pfield & 0b11 {
|
||||
0b01 => SubmillisPrecision::Microseconds(0),
|
||||
0b10 => SubmillisPrecision::Picoseconds(0),
|
||||
0b00 => SubmillisPrecision::Absent,
|
||||
0b11 => SubmillisPrecision::Reserved,
|
||||
_ => panic!("pfield to SubmillisPrecision failed"),
|
||||
}
|
||||
}
|
||||
|
||||
/// This object is the abstraction for the CCSDS Day Segmented Time Code (CDS).
|
||||
///
|
||||
/// It has the capability to generate and read timestamps as specified in the CCSDS 301.0-B-4
|
||||
/// section 3.3 . The width of the days field is configured at compile time via the generic
|
||||
/// [ProvidesDaysLength] trait which is implemented by [DaysLen16Bits] and [DaysLen24Bits].
|
||||
///
|
||||
/// Custom epochs are not supported yet.
|
||||
/// Furthermore, the preamble field (p-field) is explicitly conveyed.
|
||||
/// That means it will always be present when writing the time stamp to a raw buffer, and it
|
||||
/// must be present when reading a CDS timestamp from a raw buffer.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use spacepackets::time::cds::{TimeProvider, DaysLen16Bits};
|
||||
/// use spacepackets::time::{TimeWriter, CcsdsTimeCodes, TimeReader, CcsdsTimeProvider};
|
||||
///
|
||||
/// let timestamp_now = TimeProvider::from_now_with_u16_days().unwrap();
|
||||
/// let mut raw_stamp = [0; 7];
|
||||
/// {
|
||||
/// let written = timestamp_now.write_to_bytes(&mut raw_stamp).unwrap();
|
||||
/// assert_eq!((raw_stamp[0] >> 4) & 0b111, CcsdsTimeCodes::Cds as u8);
|
||||
/// assert_eq!(written, 7);
|
||||
/// }
|
||||
/// {
|
||||
/// let read_result = TimeProvider::<DaysLen16Bits>::from_bytes(&raw_stamp);
|
||||
/// assert!(read_result.is_ok());
|
||||
/// let stamp_deserialized = read_result.unwrap();
|
||||
/// assert_eq!(stamp_deserialized.len_as_bytes(), 7);
|
||||
/// }
|
||||
/// ```
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct TimeProvider<DaysLen: ProvidesDaysLength = DaysLen16Bits> {
|
||||
pfield: u8,
|
||||
ccsds_days: DaysLen::FieldType,
|
||||
ms_of_day: u32,
|
||||
submillis_precision: Option<SubmillisPrecision>,
|
||||
unix_seconds: i64,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
struct ConversionFromNow {
|
||||
ccsds_days: i32,
|
||||
ms_of_day: u64,
|
||||
unix_days_seconds: u64,
|
||||
submillis_prec: Option<SubmillisPrecision>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl ConversionFromNow {
|
||||
fn new() -> Result<Self, SystemTimeError> {
|
||||
Self::new_generic(None)
|
||||
}
|
||||
|
||||
fn new_with_submillis_us_prec() -> Result<Self, SystemTimeError> {
|
||||
Self::new_generic(Some(SubmillisPrecision::Microseconds(0)))
|
||||
}
|
||||
|
||||
fn new_with_submillis_ps_prec() -> Result<Self, SystemTimeError> {
|
||||
Self::new_generic(Some(SubmillisPrecision::Picoseconds(0)))
|
||||
}
|
||||
|
||||
fn new_generic(mut prec: Option<SubmillisPrecision>) -> Result<Self, 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;
|
||||
if let Some(submilli_prec) = prec {
|
||||
match submilli_prec {
|
||||
SubmillisPrecision::Microseconds(_) => {
|
||||
prec = Some(SubmillisPrecision::Microseconds(
|
||||
(now.subsec_micros() % 1000) as u16,
|
||||
));
|
||||
}
|
||||
SubmillisPrecision::Picoseconds(_) => {
|
||||
prec = Some(SubmillisPrecision::Picoseconds(
|
||||
(now.subsec_nanos() % 10_u32.pow(6)) * 1000,
|
||||
));
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
Ok(Self {
|
||||
ms_of_day: secs_of_day * 1000 + now.subsec_millis() as u64,
|
||||
ccsds_days: unix_to_ccsds_days((unix_days_seconds / SECONDS_PER_DAY as u64) as i64)
|
||||
as i32,
|
||||
unix_days_seconds,
|
||||
submillis_prec: prec,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<ProvidesDaysLen: ProvidesDaysLength> TimeProvider<ProvidesDaysLen> {
|
||||
pub fn set_submillis_precision(&mut self, prec: SubmillisPrecision) {
|
||||
self.pfield &= !(0b11);
|
||||
if let SubmillisPrecision::Absent = prec {
|
||||
self.submillis_precision = None;
|
||||
return;
|
||||
}
|
||||
self.submillis_precision = Some(prec);
|
||||
match prec {
|
||||
SubmillisPrecision::Microseconds(_) => {
|
||||
self.pfield |= 0b01;
|
||||
}
|
||||
SubmillisPrecision::Picoseconds(_) => {
|
||||
self.pfield |= 0b10;
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear_submillis_precision(&mut self) {
|
||||
self.pfield &= !(0b11);
|
||||
self.submillis_precision = None;
|
||||
}
|
||||
|
||||
pub fn ccsds_days(&self) -> ProvidesDaysLen::FieldType {
|
||||
self.ccsds_days
|
||||
}
|
||||
|
||||
pub fn submillis_precision(&self) -> Option<SubmillisPrecision> {
|
||||
self.submillis_precision
|
||||
}
|
||||
|
||||
pub fn ms_of_day(&self) -> u32 {
|
||||
self.ms_of_day
|
||||
}
|
||||
|
||||
fn generic_raw_read_checks(
|
||||
buf: &[u8],
|
||||
days_len: LengthOfDaySegment,
|
||||
) -> Result<SubmillisPrecision, TimestampError> {
|
||||
if buf.len() < MIN_CDS_FIELD_LEN {
|
||||
return Err(TimestampError::ByteConversionError(
|
||||
ByteConversionError::FromSliceTooSmall(SizeMissmatch {
|
||||
expected: MIN_CDS_FIELD_LEN,
|
||||
found: buf.len(),
|
||||
}),
|
||||
));
|
||||
}
|
||||
let pfield = buf[0];
|
||||
match CcsdsTimeCodes::try_from(pfield >> 4 & 0b111) {
|
||||
Ok(cds_type) => match cds_type {
|
||||
CcsdsTimeCodes::Cds => (),
|
||||
_ => {
|
||||
return Err(TimestampError::InvalidTimeCode(
|
||||
CcsdsTimeCodes::Cds,
|
||||
cds_type as u8,
|
||||
))
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
return Err(TimestampError::InvalidTimeCode(
|
||||
CcsdsTimeCodes::Cds,
|
||||
pfield >> 4 & 0b111,
|
||||
))
|
||||
}
|
||||
};
|
||||
if ((pfield >> 3) & 0b1) == 1 {
|
||||
return Err(TimestampError::CustomEpochNotSupported);
|
||||
}
|
||||
let days_len_from_pfield = length_of_day_segment_from_pfield(pfield);
|
||||
if days_len_from_pfield != days_len {
|
||||
return Err(CdsError::InvalidCtorForDaysOfLenInPreamble(days_len_from_pfield).into());
|
||||
}
|
||||
let stamp_len = Self::calc_stamp_len(pfield);
|
||||
if buf.len() < stamp_len {
|
||||
return Err(TimestampError::ByteConversionError(
|
||||
ByteConversionError::FromSliceTooSmall(SizeMissmatch {
|
||||
expected: stamp_len,
|
||||
found: buf.len(),
|
||||
}),
|
||||
));
|
||||
}
|
||||
Ok(precision_from_pfield(pfield))
|
||||
}
|
||||
|
||||
fn calc_stamp_len(pfield: u8) -> usize {
|
||||
let mut init_len = 7;
|
||||
if length_of_day_segment_from_pfield(pfield) == LengthOfDaySegment::Long24Bits {
|
||||
init_len += 1
|
||||
}
|
||||
match pfield & 0b11 {
|
||||
0b01 => {
|
||||
init_len += 2;
|
||||
}
|
||||
0b10 => {
|
||||
init_len += 4;
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
init_len
|
||||
}
|
||||
|
||||
fn setup(&mut self, unix_days_seconds: i64, ms_of_day: u64) {
|
||||
self.calc_unix_seconds(unix_days_seconds, ms_of_day);
|
||||
}
|
||||
|
||||
fn calc_unix_seconds(&mut self, unix_days_seconds: i64, ms_of_day: u64) {
|
||||
self.unix_seconds = unix_days_seconds;
|
||||
let seconds_of_day = (ms_of_day / 1000) as i64;
|
||||
if self.unix_seconds < 0 {
|
||||
self.unix_seconds -= seconds_of_day;
|
||||
} else {
|
||||
self.unix_seconds += seconds_of_day;
|
||||
}
|
||||
}
|
||||
|
||||
fn calc_date_time(&self, ms_since_last_second: u32) -> Option<DateTime<Utc>> {
|
||||
assert!(ms_since_last_second < 1000, "Invalid MS since last second");
|
||||
let ns_since_last_sec = ms_since_last_second * 1e6 as u32;
|
||||
if let LocalResult::Single(val) = Utc.timestamp_opt(self.unix_seconds, ns_since_last_sec) {
|
||||
return Some(val);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn length_check(&self, buf: &[u8], len_as_bytes: usize) -> Result<(), TimestampError> {
|
||||
if buf.len() < len_as_bytes {
|
||||
return Err(TimestampError::ByteConversionError(
|
||||
ByteConversionError::ToSliceTooSmall(SizeMissmatch {
|
||||
expected: len_as_bytes,
|
||||
found: buf.len(),
|
||||
}),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn generic_new(
|
||||
days_len: LengthOfDaySegment,
|
||||
ccsds_days: ProvidesDaysLen::FieldType,
|
||||
ms_of_day: u32,
|
||||
) -> Result<Self, CdsError>
|
||||
where
|
||||
i64: From<ProvidesDaysLen::FieldType>,
|
||||
{
|
||||
let mut provider = Self {
|
||||
pfield: Self::generate_p_field(days_len, None),
|
||||
ccsds_days,
|
||||
ms_of_day,
|
||||
unix_seconds: 0,
|
||||
submillis_precision: None,
|
||||
};
|
||||
let unix_days_seconds = ccsds_to_unix_days(ccsds_days.into()) * SECONDS_PER_DAY as i64;
|
||||
provider.setup(unix_days_seconds, ms_of_day.into());
|
||||
Ok(provider)
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
fn generic_from_now(
|
||||
days_len: LengthOfDaySegment,
|
||||
conversion_from_now: ConversionFromNow,
|
||||
) -> Result<Self, StdTimestampError>
|
||||
where
|
||||
<ProvidesDaysLen::FieldType as TryFrom<i32>>::Error: Debug,
|
||||
{
|
||||
let ccsds_days: ProvidesDaysLen::FieldType =
|
||||
conversion_from_now.ccsds_days.try_into().map_err(|_| {
|
||||
StdTimestampError::TimestampError(
|
||||
CdsError::InvalidCcsdsDays(conversion_from_now.ccsds_days.into()).into(),
|
||||
)
|
||||
})?;
|
||||
let mut provider = Self {
|
||||
pfield: Self::generate_p_field(days_len, conversion_from_now.submillis_prec),
|
||||
ccsds_days,
|
||||
ms_of_day: conversion_from_now.ms_of_day as u32,
|
||||
unix_seconds: 0,
|
||||
submillis_precision: conversion_from_now.submillis_prec,
|
||||
};
|
||||
provider.setup(
|
||||
conversion_from_now.unix_days_seconds as i64,
|
||||
conversion_from_now.ms_of_day,
|
||||
);
|
||||
Ok(provider)
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
fn generic_conversion_from_now(&self) -> Result<ConversionFromNow, SystemTimeError> {
|
||||
Ok(match self.submillis_precision {
|
||||
None => ConversionFromNow::new()?,
|
||||
Some(prec) => match prec {
|
||||
SubmillisPrecision::Microseconds(_) => {
|
||||
ConversionFromNow::new_with_submillis_us_prec()?
|
||||
}
|
||||
SubmillisPrecision::Picoseconds(_) => {
|
||||
ConversionFromNow::new_with_submillis_ps_prec()?
|
||||
}
|
||||
_ => ConversionFromNow::new()?,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
fn generate_p_field(
|
||||
day_seg_len: LengthOfDaySegment,
|
||||
submillis_prec: Option<SubmillisPrecision>,
|
||||
) -> u8 {
|
||||
let mut pfield = P_FIELD_BASE | ((day_seg_len as u8) << 2);
|
||||
if let Some(submillis_prec) = submillis_prec {
|
||||
match submillis_prec {
|
||||
SubmillisPrecision::Microseconds(_) => pfield |= 0b01,
|
||||
SubmillisPrecision::Picoseconds(_) => pfield |= 0b10,
|
||||
SubmillisPrecision::Reserved => pfield |= 0b11,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
pfield
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
||||
pub fn update_from_now(&mut self) -> Result<(), StdTimestampError>
|
||||
where
|
||||
<ProvidesDaysLen::FieldType as TryFrom<i32>>::Error: Debug,
|
||||
{
|
||||
let conversion_from_now = self.generic_conversion_from_now()?;
|
||||
let ccsds_days: ProvidesDaysLen::FieldType =
|
||||
conversion_from_now.ccsds_days.try_into().map_err(|_| {
|
||||
StdTimestampError::TimestampError(
|
||||
CdsError::InvalidCcsdsDays(conversion_from_now.ccsds_days as i64).into(),
|
||||
)
|
||||
})?;
|
||||
self.ccsds_days = ccsds_days;
|
||||
self.ms_of_day = conversion_from_now.ms_of_day as u32;
|
||||
self.setup(
|
||||
conversion_from_now.unix_days_seconds as i64,
|
||||
conversion_from_now.ms_of_day,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl TimeProvider<DaysLen24Bits> {
|
||||
/// Generate a new timestamp provider with the days field width set to 24 bits
|
||||
pub fn new_with_u24_days(ccsds_days: u32, ms_of_day: u32) -> Result<Self, CdsError> {
|
||||
if ccsds_days > 2_u32.pow(24) {
|
||||
return Err(CdsError::InvalidCcsdsDays(ccsds_days.into()));
|
||||
}
|
||||
Self::generic_new(LengthOfDaySegment::Long24Bits, ccsds_days, ms_of_day)
|
||||
}
|
||||
|
||||
/// Generate a time stamp from the current time using the system clock.
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
||||
pub fn from_now_with_u24_days() -> Result<Self, StdTimestampError> {
|
||||
let conversion_from_now = ConversionFromNow::new()?;
|
||||
Self::generic_from_now(LengthOfDaySegment::Long24Bits, conversion_from_now)
|
||||
}
|
||||
|
||||
/// Like [Self::from_now_with_u24_days] but with microsecond sub-millisecond precision.
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
||||
pub fn from_now_with_u24_days_and_us_prec() -> Result<Self, StdTimestampError> {
|
||||
let conversion_from_now = ConversionFromNow::new_with_submillis_us_prec()?;
|
||||
Self::generic_from_now(LengthOfDaySegment::Long24Bits, conversion_from_now)
|
||||
}
|
||||
|
||||
/// Like [Self::from_now_with_u24_days] but with picoseconds sub-millisecond precision.
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
||||
pub fn from_now_with_u24_days_ps_submillis_prec() -> Result<Self, StdTimestampError> {
|
||||
let conversion_from_now = ConversionFromNow::new_with_submillis_ps_prec()?;
|
||||
Self::generic_from_now(LengthOfDaySegment::Long24Bits, conversion_from_now)
|
||||
}
|
||||
|
||||
fn from_bytes_24_bit_days(buf: &[u8]) -> Result<Self, TimestampError> {
|
||||
let submillis_precision =
|
||||
Self::generic_raw_read_checks(buf, LengthOfDaySegment::Long24Bits)?;
|
||||
let mut temp_buf: [u8; 4] = [0; 4];
|
||||
temp_buf[1..4].copy_from_slice(&buf[1..4]);
|
||||
let cccsds_days: u32 = u32::from_be_bytes(temp_buf);
|
||||
let ms_of_day: u32 = u32::from_be_bytes(buf[4..8].try_into().unwrap());
|
||||
let mut provider = Self::new_with_u24_days(cccsds_days, ms_of_day)?;
|
||||
match submillis_precision {
|
||||
SubmillisPrecision::Microseconds(_) => {
|
||||
provider.set_submillis_precision(SubmillisPrecision::Microseconds(
|
||||
u16::from_be_bytes(buf[8..10].try_into().unwrap()),
|
||||
))
|
||||
}
|
||||
SubmillisPrecision::Picoseconds(_) => provider.set_submillis_precision(
|
||||
SubmillisPrecision::Picoseconds(u32::from_be_bytes(buf[8..12].try_into().unwrap())),
|
||||
),
|
||||
_ => (),
|
||||
}
|
||||
Ok(provider)
|
||||
}
|
||||
}
|
||||
|
||||
impl TimeProvider<DaysLen16Bits> {
|
||||
/// Generate a new timestamp provider with the days field width set to 16 bits
|
||||
pub fn new_with_u16_days(ccsds_days: u16, ms_of_day: u32) -> Self {
|
||||
// This should never fail, type system ensures CCSDS can not be negative or too large
|
||||
Self::generic_new(LengthOfDaySegment::Short16Bits, ccsds_days, ms_of_day).unwrap()
|
||||
}
|
||||
|
||||
/// Generate a time stamp from the current time using the system clock.
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
||||
pub fn from_now_with_u16_days() -> Result<Self, StdTimestampError> {
|
||||
let conversion_from_now = ConversionFromNow::new()?;
|
||||
Self::generic_from_now(LengthOfDaySegment::Short16Bits, conversion_from_now)
|
||||
}
|
||||
|
||||
/// Like [Self::from_now_with_u16_days] but with microsecond sub-millisecond precision.
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
||||
pub fn from_now_with_u16_days_and_us_prec() -> Result<Self, StdTimestampError> {
|
||||
let conversion_from_now = ConversionFromNow::new_with_submillis_us_prec()?;
|
||||
Self::generic_from_now(LengthOfDaySegment::Short16Bits, conversion_from_now)
|
||||
}
|
||||
|
||||
/// Like [Self::from_now_with_u16_days] but with picosecond sub-millisecond precision.
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
||||
pub fn from_now_with_u16_days_and_ps_prec() -> Result<Self, StdTimestampError> {
|
||||
let conversion_from_now = ConversionFromNow::new_with_submillis_ps_prec()?;
|
||||
Self::generic_from_now(LengthOfDaySegment::Short16Bits, conversion_from_now)
|
||||
}
|
||||
|
||||
fn from_bytes_16_bit_days(buf: &[u8]) -> Result<Self, TimestampError> {
|
||||
let submillis_precision =
|
||||
Self::generic_raw_read_checks(buf, LengthOfDaySegment::Short16Bits)?;
|
||||
let ccsds_days: u16 = u16::from_be_bytes(buf[1..3].try_into().unwrap());
|
||||
let ms_of_day: u32 = u32::from_be_bytes(buf[3..7].try_into().unwrap());
|
||||
let mut provider = Self::new_with_u16_days(ccsds_days, ms_of_day);
|
||||
provider.pfield = buf[0];
|
||||
match submillis_precision {
|
||||
SubmillisPrecision::Microseconds(_) => provider.set_submillis_precision(
|
||||
SubmillisPrecision::Microseconds(u16::from_be_bytes(buf[7..9].try_into().unwrap())),
|
||||
),
|
||||
SubmillisPrecision::Picoseconds(_) => provider.set_submillis_precision(
|
||||
SubmillisPrecision::Picoseconds(u32::from_be_bytes(buf[7..11].try_into().unwrap())),
|
||||
),
|
||||
_ => (),
|
||||
}
|
||||
Ok(provider)
|
||||
}
|
||||
}
|
||||
|
||||
impl<ProvidesDaysLen: ProvidesDaysLength> CcsdsTimeProvider for TimeProvider<ProvidesDaysLen> {
|
||||
fn len_as_bytes(&self) -> usize {
|
||||
Self::calc_stamp_len(self.pfield)
|
||||
}
|
||||
|
||||
fn p_field(&self) -> (usize, [u8; 2]) {
|
||||
(1, [self.pfield, 0])
|
||||
}
|
||||
|
||||
fn ccdsd_time_code(&self) -> CcsdsTimeCodes {
|
||||
CcsdsTimeCodes::Cds
|
||||
}
|
||||
|
||||
fn unix_seconds(&self) -> i64 {
|
||||
self.unix_seconds
|
||||
}
|
||||
|
||||
fn date_time(&self) -> Option<DateTime<Utc>> {
|
||||
self.calc_date_time(self.ms_of_day % 1000)
|
||||
}
|
||||
}
|
||||
|
||||
impl TimeReader for TimeProvider<DaysLen16Bits> {
|
||||
fn from_bytes(buf: &[u8]) -> Result<Self, TimestampError> {
|
||||
Self::from_bytes_16_bit_days(buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl TimeReader for TimeProvider<DaysLen24Bits> {
|
||||
fn from_bytes(buf: &[u8]) -> Result<Self, TimestampError> {
|
||||
Self::from_bytes_24_bit_days(buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl TimeWriter for TimeProvider<DaysLen16Bits> {
|
||||
fn write_to_bytes(&self, buf: &mut [u8]) -> Result<usize, TimestampError> {
|
||||
self.length_check(buf, self.len_as_bytes())?;
|
||||
buf[0] = self.pfield;
|
||||
buf[1..3].copy_from_slice(self.ccsds_days.to_be_bytes().as_slice());
|
||||
buf[3..7].copy_from_slice(self.ms_of_day.to_be_bytes().as_slice());
|
||||
if let Some(submillis_prec) = self.submillis_precision {
|
||||
match submillis_prec {
|
||||
SubmillisPrecision::Microseconds(ms) => {
|
||||
buf[7..9].copy_from_slice(ms.to_be_bytes().as_slice());
|
||||
}
|
||||
SubmillisPrecision::Picoseconds(ps) => {
|
||||
buf[7..11].copy_from_slice(ps.to_be_bytes().as_slice());
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
Ok(self.len_as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
impl TimeWriter for TimeProvider<DaysLen24Bits> {
|
||||
fn write_to_bytes(&self, buf: &mut [u8]) -> Result<usize, TimestampError> {
|
||||
self.length_check(buf, self.len_as_bytes())?;
|
||||
buf[0] = self.pfield;
|
||||
let be_days = self.ccsds_days.to_be_bytes();
|
||||
buf[1..4].copy_from_slice(&be_days[1..4]);
|
||||
buf[4..8].copy_from_slice(self.ms_of_day.to_be_bytes().as_slice());
|
||||
if let Some(submillis_prec) = self.submillis_precision {
|
||||
match submillis_prec {
|
||||
SubmillisPrecision::Microseconds(ms) => {
|
||||
buf[8..10].copy_from_slice(ms.to_be_bytes().as_slice());
|
||||
}
|
||||
SubmillisPrecision::Picoseconds(ps) => {
|
||||
buf[8..12].copy_from_slice(ps.to_be_bytes().as_slice());
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
Ok(self.len_as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::time::TimestampError::{ByteConversionError, InvalidTimeCode};
|
||||
use crate::ByteConversionError::{FromSliceTooSmall, ToSliceTooSmall};
|
||||
use chrono::{Datelike, Timelike};
|
||||
#[cfg(feature = "serde")]
|
||||
use postcard::{from_bytes, to_allocvec};
|
||||
use std::format;
|
||||
|
||||
#[test]
|
||||
fn test_time_stamp_zero_args() {
|
||||
let time_stamper = TimeProvider::new_with_u16_days(0, 0);
|
||||
assert_eq!(
|
||||
time_stamper.unix_seconds(),
|
||||
(DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32) as i64
|
||||
);
|
||||
assert_eq!(time_stamper.submillis_precision(), None);
|
||||
assert_eq!(time_stamper.ccdsd_time_code(), CcsdsTimeCodes::Cds);
|
||||
assert_eq!(
|
||||
time_stamper.p_field(),
|
||||
(1, [(CcsdsTimeCodes::Cds as u8) << 4, 0])
|
||||
);
|
||||
let date_time = time_stamper.date_time().unwrap();
|
||||
assert_eq!(date_time.year(), 1958);
|
||||
assert_eq!(date_time.month(), 1);
|
||||
assert_eq!(date_time.day(), 1);
|
||||
assert_eq!(date_time.hour(), 0);
|
||||
assert_eq!(date_time.minute(), 0);
|
||||
assert_eq!(date_time.second(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_time_stamp_unix_epoch() {
|
||||
let time_stamper = TimeProvider::new_with_u16_days((-DAYS_CCSDS_TO_UNIX) as u16, 0);
|
||||
assert_eq!(time_stamper.unix_seconds(), 0);
|
||||
assert_eq!(time_stamper.submillis_precision(), None);
|
||||
let date_time = time_stamper.date_time().unwrap();
|
||||
assert_eq!(date_time.year(), 1970);
|
||||
assert_eq!(date_time.month(), 1);
|
||||
assert_eq!(date_time.day(), 1);
|
||||
assert_eq!(date_time.hour(), 0);
|
||||
assert_eq!(date_time.minute(), 0);
|
||||
assert_eq!(date_time.second(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_large_days_field_write() {
|
||||
let time_stamper = TimeProvider::new_with_u24_days(0x108020, 0);
|
||||
assert!(time_stamper.is_ok());
|
||||
let time_stamper = time_stamper.unwrap();
|
||||
assert_eq!(time_stamper.len_as_bytes(), 8);
|
||||
let mut buf = [0; 16];
|
||||
let written = time_stamper.write_to_bytes(&mut buf);
|
||||
assert!(written.is_ok());
|
||||
let written = written.unwrap();
|
||||
assert_eq!(written, 8);
|
||||
assert_eq!(buf[1], 0x10);
|
||||
assert_eq!(buf[2], 0x80);
|
||||
assert_eq!(buf[3], 0x20);
|
||||
let ms = u32::from_be_bytes(buf[4..8].try_into().unwrap());
|
||||
assert_eq!(ms, 0);
|
||||
assert_eq!((buf[0] >> 2) & 0b1, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_large_days_field_read() {
|
||||
let time_stamper = TimeProvider::new_with_u24_days(0x108020, 0);
|
||||
assert!(time_stamper.is_ok());
|
||||
let time_stamper = time_stamper.unwrap();
|
||||
let mut buf = [0; 16];
|
||||
let written = time_stamper.write_to_bytes(&mut buf);
|
||||
assert!(written.is_ok());
|
||||
let provider = TimeProvider::<DaysLen24Bits>::from_bytes(&buf);
|
||||
assert!(provider.is_ok());
|
||||
let provider = provider.unwrap();
|
||||
assert_eq!(provider.ccsds_days(), 0x108020);
|
||||
assert_eq!(provider.ms_of_day(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_large_days_field_read_invalid_ctor() {
|
||||
let time_stamper = TimeProvider::new_with_u24_days(0x108020, 0);
|
||||
assert!(time_stamper.is_ok());
|
||||
let time_stamper = time_stamper.unwrap();
|
||||
let mut buf = [0; 16];
|
||||
let written = time_stamper.write_to_bytes(&mut buf);
|
||||
assert!(written.is_ok());
|
||||
let faulty_ctor = TimeProvider::<DaysLen16Bits>::from_bytes(&buf);
|
||||
assert!(faulty_ctor.is_err());
|
||||
let error = faulty_ctor.unwrap_err();
|
||||
if let TimestampError::CdsError(cds::CdsError::InvalidCtorForDaysOfLenInPreamble(
|
||||
len_of_day,
|
||||
)) = error
|
||||
{
|
||||
assert_eq!(len_of_day, LengthOfDaySegment::Long24Bits);
|
||||
} else {
|
||||
panic!("Wrong error type");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_write() {
|
||||
let mut buf = [0; 16];
|
||||
let time_stamper_0 = TimeProvider::new_with_u16_days(0, 0);
|
||||
let mut res = time_stamper_0.write_to_bytes(&mut buf);
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(buf[0], (CcsdsTimeCodes::Cds as u8) << 4);
|
||||
assert_eq!(
|
||||
u16::from_be_bytes(buf[1..3].try_into().expect("Byte conversion failed")),
|
||||
0
|
||||
);
|
||||
assert_eq!(
|
||||
u32::from_be_bytes(buf[3..7].try_into().expect("Byte conversion failed")),
|
||||
0
|
||||
);
|
||||
let time_stamper_1 = TimeProvider::new_with_u16_days(u16::MAX - 1, u32::MAX - 1);
|
||||
res = time_stamper_1.write_to_bytes(&mut buf);
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(buf[0], (CcsdsTimeCodes::Cds as u8) << 4);
|
||||
assert_eq!(
|
||||
u16::from_be_bytes(buf[1..3].try_into().expect("Byte conversion failed")),
|
||||
u16::MAX - 1
|
||||
);
|
||||
assert_eq!(
|
||||
u32::from_be_bytes(buf[3..7].try_into().expect("Byte conversion failed")),
|
||||
u32::MAX - 1
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_faulty_write_buf_too_small() {
|
||||
let mut buf = [0; 7];
|
||||
let time_stamper = TimeProvider::new_with_u16_days(u16::MAX - 1, u32::MAX - 1);
|
||||
for i in 0..6 {
|
||||
let res = time_stamper.write_to_bytes(&mut buf[0..i]);
|
||||
assert!(res.is_err());
|
||||
match res.unwrap_err() {
|
||||
ByteConversionError(ToSliceTooSmall(missmatch)) => {
|
||||
assert_eq!(missmatch.found, i);
|
||||
assert_eq!(missmatch.expected, 7);
|
||||
}
|
||||
_ => panic!(
|
||||
"{}",
|
||||
format!("Invalid error {:?} detected", res.unwrap_err())
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_faulty_read_buf_too_small() {
|
||||
let buf = [0; 7];
|
||||
for i in 0..6 {
|
||||
let res = TimeProvider::<DaysLen16Bits>::from_bytes(&buf[0..i]);
|
||||
assert!(res.is_err());
|
||||
let err = res.unwrap_err();
|
||||
match err {
|
||||
ByteConversionError(e) => match e {
|
||||
FromSliceTooSmall(missmatch) => {
|
||||
assert_eq!(missmatch.found, i);
|
||||
assert_eq!(missmatch.expected, 7);
|
||||
}
|
||||
_ => panic!("{}", format!("Invalid error {:?} detected", e)),
|
||||
},
|
||||
_ => {
|
||||
panic!("Unexpected error {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_faulty_invalid_pfield() {
|
||||
let mut buf = [0; 16];
|
||||
let time_stamper_0 = TimeProvider::new_with_u16_days(0, 0);
|
||||
let res = time_stamper_0.write_to_bytes(&mut buf);
|
||||
assert!(res.is_ok());
|
||||
buf[0] = 0;
|
||||
let res = TimeProvider::<DaysLen16Bits>::from_bytes(&buf);
|
||||
assert!(res.is_err());
|
||||
let err = res.unwrap_err();
|
||||
match err {
|
||||
InvalidTimeCode(code, raw) => {
|
||||
assert_eq!(code, CcsdsTimeCodes::Cds);
|
||||
assert_eq!(raw, 0);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_reading() {
|
||||
let mut buf = [0; 16];
|
||||
let time_stamper = TimeProvider::new_with_u16_days(u16::MAX - 1, u32::MAX - 1);
|
||||
let res = time_stamper.write_to_bytes(&mut buf);
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(buf[0], (CcsdsTimeCodes::Cds as u8) << 4);
|
||||
assert_eq!(
|
||||
u16::from_be_bytes(buf[1..3].try_into().expect("Byte conversion failed")),
|
||||
u16::MAX - 1
|
||||
);
|
||||
assert_eq!(
|
||||
u32::from_be_bytes(buf[3..7].try_into().expect("Byte conversion failed")),
|
||||
u32::MAX - 1
|
||||
);
|
||||
|
||||
let read_stamp: TimeProvider<DaysLen16Bits> =
|
||||
TimeProvider::from_bytes(&buf).expect("Reading timestamp failed");
|
||||
assert_eq!(read_stamp.ccsds_days(), u16::MAX - 1);
|
||||
assert_eq!(read_stamp.ms_of_day(), u32::MAX - 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_time_now() {
|
||||
let timestamp_now = TimeProvider::from_now_with_u16_days().unwrap();
|
||||
let compare_stamp = Utc::now();
|
||||
let dt = timestamp_now.date_time().unwrap();
|
||||
if compare_stamp.year() > dt.year() {
|
||||
assert_eq!(compare_stamp.year() - dt.year(), 1);
|
||||
} else {
|
||||
assert_eq!(dt.year(), compare_stamp.year());
|
||||
}
|
||||
generic_dt_property_equality_check(dt.month(), compare_stamp.month(), 1, 12);
|
||||
|
||||
assert_eq!(dt.day(), compare_stamp.day());
|
||||
if compare_stamp.day() < dt.day() {
|
||||
assert!(dt.day() >= 28);
|
||||
assert_eq!(compare_stamp.day(), 1);
|
||||
} else if compare_stamp.day() > dt.day() {
|
||||
assert_eq!(compare_stamp.day() - dt.day(), 1);
|
||||
} else {
|
||||
assert_eq!(compare_stamp.day(), dt.day());
|
||||
}
|
||||
generic_dt_property_equality_check(dt.hour(), compare_stamp.hour(), 0, 23);
|
||||
generic_dt_property_equality_check(dt.minute(), compare_stamp.minute(), 0, 59);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_submillis_precision_micros() {
|
||||
let mut time_stamper = TimeProvider::new_with_u16_days(0, 0);
|
||||
time_stamper.set_submillis_precision(SubmillisPrecision::Microseconds(500));
|
||||
assert!(time_stamper.submillis_precision().is_some());
|
||||
if let SubmillisPrecision::Microseconds(micros) =
|
||||
time_stamper.submillis_precision().unwrap()
|
||||
{
|
||||
assert_eq!(micros, 500);
|
||||
} else {
|
||||
panic!("Submillis precision was not set properly");
|
||||
}
|
||||
let mut write_buf: [u8; 16] = [0; 16];
|
||||
let written = time_stamper
|
||||
.write_to_bytes(&mut write_buf)
|
||||
.expect("Writing timestamp failed");
|
||||
assert_eq!(written, 9);
|
||||
let cross_check: u16 = 500;
|
||||
assert_eq!(write_buf[7..9], cross_check.to_be_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_submillis_precision_picos() {
|
||||
let mut time_stamper = TimeProvider::new_with_u16_days(0, 0);
|
||||
time_stamper.set_submillis_precision(SubmillisPrecision::Picoseconds(5e8 as u32));
|
||||
assert!(time_stamper.submillis_precision().is_some());
|
||||
if let SubmillisPrecision::Picoseconds(ps) = time_stamper.submillis_precision().unwrap() {
|
||||
assert_eq!(ps, 5e8 as u32);
|
||||
} else {
|
||||
panic!("Submillis precision was not set properly");
|
||||
}
|
||||
let mut write_buf: [u8; 16] = [0; 16];
|
||||
let written = time_stamper
|
||||
.write_to_bytes(&mut write_buf)
|
||||
.expect("Writing timestamp failed");
|
||||
assert_eq!(written, 11);
|
||||
let cross_check: u32 = 5e8 as u32;
|
||||
assert_eq!(write_buf[7..11], cross_check.to_be_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_stamp_with_ps_submillis_precision() {
|
||||
let mut time_stamper = TimeProvider::new_with_u16_days(0, 0);
|
||||
time_stamper.set_submillis_precision(SubmillisPrecision::Picoseconds(5e8 as u32));
|
||||
let mut write_buf: [u8; 16] = [0; 16];
|
||||
let written = time_stamper
|
||||
.write_to_bytes(&mut write_buf)
|
||||
.expect("Writing timestamp failed");
|
||||
assert_eq!(written, 11);
|
||||
let stamp_deserialized = TimeProvider::<DaysLen16Bits>::from_bytes(&write_buf);
|
||||
assert!(stamp_deserialized.is_ok());
|
||||
let stamp_deserialized = stamp_deserialized.unwrap();
|
||||
assert_eq!(stamp_deserialized.len_as_bytes(), 11);
|
||||
assert!(stamp_deserialized.submillis_precision().is_some());
|
||||
let submillis_rec = stamp_deserialized.submillis_precision().unwrap();
|
||||
if let SubmillisPrecision::Picoseconds(ps) = submillis_rec {
|
||||
assert_eq!(ps, 5e8 as u32);
|
||||
} else {
|
||||
panic!("Wrong precision field detected");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_stamp_with_us_submillis_precision() {
|
||||
let mut time_stamper = TimeProvider::new_with_u16_days(0, 0);
|
||||
time_stamper.set_submillis_precision(SubmillisPrecision::Microseconds(500));
|
||||
let mut write_buf: [u8; 16] = [0; 16];
|
||||
let written = time_stamper
|
||||
.write_to_bytes(&mut write_buf)
|
||||
.expect("Writing timestamp failed");
|
||||
assert_eq!(written, 9);
|
||||
let stamp_deserialized = TimeProvider::<DaysLen16Bits>::from_bytes(&write_buf);
|
||||
assert!(stamp_deserialized.is_ok());
|
||||
let stamp_deserialized = stamp_deserialized.unwrap();
|
||||
assert_eq!(stamp_deserialized.len_as_bytes(), 9);
|
||||
assert!(stamp_deserialized.submillis_precision().is_some());
|
||||
let submillis_rec = stamp_deserialized.submillis_precision().unwrap();
|
||||
if let SubmillisPrecision::Microseconds(us) = submillis_rec {
|
||||
assert_eq!(us, 500);
|
||||
} else {
|
||||
panic!("Wrong precision field detected");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "serde")]
|
||||
fn test_serialization() {
|
||||
let stamp_now = TimeProvider::from_now_with_u16_days().expect("Error retrieving time");
|
||||
let val = to_allocvec(&stamp_now).expect("Serializing timestamp failed");
|
||||
assert!(val.len() > 0);
|
||||
let stamp_deser: TimeProvider = from_bytes(&val).expect("Stamp deserialization failed");
|
||||
assert_eq!(stamp_deser, stamp_now);
|
||||
}
|
||||
|
||||
fn generic_dt_property_equality_check(first: u32, second: u32, start: u32, end: u32) {
|
||||
if second < first {
|
||||
assert_eq!(second, start);
|
||||
assert_eq!(first, end);
|
||||
} else if second > first {
|
||||
assert_eq!(second - first, 1);
|
||||
} else {
|
||||
assert_eq!(first, second);
|
||||
}
|
||||
}
|
||||
}
|
950
src/time/cuc.rs
Normal file
950
src/time/cuc.rs
Normal file
@ -0,0 +1,950 @@
|
||||
//! 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 .
|
||||
//!
|
||||
//! The core data structure to do this is the [TimeProviderCcsdsEpoch] struct.
|
||||
use super::*;
|
||||
use core::fmt::Debug;
|
||||
|
||||
const MIN_CUC_LEN: usize = 2;
|
||||
|
||||
/// Base value for the preamble field for a time field parser to determine the time field type.
|
||||
pub const P_FIELD_BASE: u8 = (CcsdsTimeCodes::CucCcsdsEpoch as u8) << 4;
|
||||
/// Maximum length if the preamble field is not extended.
|
||||
pub const MAX_CUC_LEN_SMALL_PREAMBLE: usize = 8;
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub enum FractionalResolution {
|
||||
/// No fractional part, only second resolution
|
||||
Seconds = 0,
|
||||
/// 256 fractional parts, resulting in 1/255 ~= 4 ms fractional resolution
|
||||
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(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Please note that this function will panic if the fractional value is not smaller than
|
||||
/// the maximum number of fractions allowed for the particular resolution.
|
||||
/// (e.g. passing 270 when the resolution only allows 255 values).
|
||||
pub fn convert_fractional_part_to_ns(fractional_part: FractionalPart) -> u64 {
|
||||
let div = fractional_res_to_div(fractional_part.0);
|
||||
assert!(fractional_part.1 < div);
|
||||
10_u64.pow(9) * fractional_part.1 as u64 / div as u64
|
||||
}
|
||||
|
||||
pub const fn fractional_res_to_div(res: FractionalResolution) -> u32 {
|
||||
2_u32.pow(8 * res as u32) - 1
|
||||
}
|
||||
|
||||
/// 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].
|
||||
pub fn fractional_part_from_subsec_ns(
|
||||
res: FractionalResolution,
|
||||
ns: u64,
|
||||
) -> Option<FractionalPart> {
|
||||
if res == FractionalResolution::Seconds {
|
||||
return None;
|
||||
}
|
||||
let sec_as_ns = 10_u64.pow(9);
|
||||
if ns > sec_as_ns {
|
||||
panic!("passed nanosecond value larger than 1 second");
|
||||
}
|
||||
let resolution = fractional_res_to_div(res) as u64;
|
||||
// Use integer division because this can reduce code size of really small systems.
|
||||
// First determine the nanoseconds for the smallest segment given the resolution.
|
||||
// Then divide by that to find out the fractional part. For the calculation of the smallest
|
||||
// fraction, we perform a ceiling division. This is because if we would use the default
|
||||
// flooring division, we would divide by a smaller value, thereby allowing the calculation to
|
||||
// invalid fractional parts which are too large. For the division of the nanoseconds by the
|
||||
// smallest fraction, a flooring division is correct.
|
||||
// The multiplication with 100000 is necessary to avoid precision loss during integer division.
|
||||
// TODO: Floating point division might actually be faster option, but requires additional
|
||||
// code on small embedded systems..
|
||||
let fractional_part = ns * 100000 / ((sec_as_ns * 100000 + resolution) / resolution);
|
||||
// Floating point division.
|
||||
//let fractional_part = (ns as f64 / ((sec_as_ns as f64) / resolution as f64)).floor() as u32;
|
||||
Some(FractionalPart(res, fractional_part as u32))
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub enum CucError {
|
||||
InvalidCounterWidth(u8),
|
||||
InvalidFractionResolution(FractionalResolution),
|
||||
InvalidCounter(u8, u64),
|
||||
InvalidFractions(FractionalResolution, u64),
|
||||
}
|
||||
|
||||
impl Display for CucError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
|
||||
match self {
|
||||
CucError::InvalidCounterWidth(w) => {
|
||||
write!(f, "invalid cuc counter byte width {}", w)
|
||||
}
|
||||
CucError::InvalidFractionResolution(w) => {
|
||||
write!(f, "invalid cuc fractional part byte width {:?}", w)
|
||||
}
|
||||
CucError::InvalidCounter(w, c) => {
|
||||
write!(f, "invalid cuc counter {} for width {}", c, w)
|
||||
}
|
||||
CucError::InvalidFractions(w, c) => {
|
||||
write!(f, "invalid cuc fractional part {} for width {:?}", c, w)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl Error for CucError {}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct WidthCounterPair(u8, u32);
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct FractionalPart(FractionalResolution, u32);
|
||||
|
||||
/// This object is the abstraction for the CCSDS Unsegmented Time Code (CUC) using the CCSDS epoch
|
||||
/// and a small preamble field.
|
||||
///
|
||||
/// 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
|
||||
/// fixed CCSDS epoch (1958-01-01 00:00:00). It is possible to provide subsecond accuracy using the
|
||||
/// fractional field with various available [resolutions][FractionalResolution].
|
||||
///
|
||||
/// 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).
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use spacepackets::time::cuc::{FractionalResolution, TimeProviderCcsdsEpoch};
|
||||
/// use spacepackets::time::{TimeWriter, CcsdsTimeCodes, TimeReader, CcsdsTimeProvider};
|
||||
///
|
||||
/// // Highest fractional resolution
|
||||
/// let timestamp_now = TimeProviderCcsdsEpoch::from_now(FractionalResolution::SixtyNs).expect("creating cuc stamp failed");
|
||||
/// let mut raw_stamp = [0; 16];
|
||||
/// {
|
||||
/// let written = timestamp_now.write_to_bytes(&mut raw_stamp).expect("writing timestamp failed");
|
||||
/// assert_eq!((raw_stamp[0] >> 4) & 0b111, CcsdsTimeCodes::CucCcsdsEpoch as u8);
|
||||
/// // 1 byte preamble + 4 byte counter + 3 byte fractional part
|
||||
/// assert_eq!(written, 8);
|
||||
/// }
|
||||
/// {
|
||||
/// let read_result = TimeProviderCcsdsEpoch::from_bytes(&raw_stamp);
|
||||
/// assert!(read_result.is_ok());
|
||||
/// let stamp_deserialized = read_result.unwrap();
|
||||
/// assert_eq!(stamp_deserialized, timestamp_now);
|
||||
/// }
|
||||
/// ```
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct TimeProviderCcsdsEpoch {
|
||||
pfield: u8,
|
||||
counter: WidthCounterPair,
|
||||
fractions: Option<FractionalPart>,
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn pfield_len(pfield: u8) -> usize {
|
||||
if ((pfield >> 7) & 0b1) == 1 {
|
||||
return 2;
|
||||
}
|
||||
1
|
||||
}
|
||||
|
||||
impl TimeProviderCcsdsEpoch {
|
||||
/// Create a time provider with a four byte counter and no fractional part.
|
||||
pub fn new(counter: u32) -> Self {
|
||||
// These values are definitely valid, so it is okay to unwrap here.
|
||||
Self::new_generic(WidthCounterPair(4, counter), None).unwrap()
|
||||
}
|
||||
|
||||
/// Like [TimeProviderCcsdsEpoch::new] but allow to supply a fractional part as well.
|
||||
pub fn new_with_fractions(counter: u32, fractions: FractionalPart) -> Result<Self, CucError> {
|
||||
Self::new_generic(WidthCounterPair(4, counter), Some(fractions))
|
||||
}
|
||||
|
||||
/// Fractions with a resolution of ~ 4 ms
|
||||
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),
|
||||
Some(FractionalPart(
|
||||
FractionalResolution::FourMs,
|
||||
subsec_fractions as u32,
|
||||
)),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Fractions with a resolution of ~ 16 us
|
||||
pub fn new_with_medium_fractions(counter: u32, subsec_fractions: u16) -> Self {
|
||||
// These values are definitely valid, so it is okay to unwrap here.
|
||||
Self::new_generic(
|
||||
WidthCounterPair(4, counter),
|
||||
Some(FractionalPart(
|
||||
FractionalResolution::FifteenUs,
|
||||
subsec_fractions as u32,
|
||||
)),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// 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.
|
||||
pub fn new_with_fine_fractions(counter: u32, subsec_fractions: u32) -> Result<Self, CucError> {
|
||||
Self::new_generic(
|
||||
WidthCounterPair(4, counter),
|
||||
Some(FractionalPart(
|
||||
FractionalResolution::SixtyNs,
|
||||
subsec_fractions,
|
||||
)),
|
||||
)
|
||||
}
|
||||
|
||||
/// 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.
|
||||
#[cfg(feature = "std")]
|
||||
pub fn from_now(fraction_resolution: FractionalResolution) -> Result<Self, StdTimestampError> {
|
||||
let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
|
||||
let ccsds_epoch = unix_epoch_to_ccsds_epoch(now.as_secs());
|
||||
if fraction_resolution == FractionalResolution::Seconds {
|
||||
return Ok(Self::new(ccsds_epoch as u32));
|
||||
}
|
||||
let fractions =
|
||||
fractional_part_from_subsec_ns(fraction_resolution, now.subsec_nanos() as u64);
|
||||
Self::new_with_fractions(ccsds_epoch as u32, fractions.unwrap())
|
||||
.map_err(|e| StdTimestampError::TimestampError(e.into()))
|
||||
}
|
||||
|
||||
/// Updates the current time stamp from the current time. The fractional field width remains
|
||||
/// the same and will be updated accordingly.
|
||||
#[cfg(feature = "std")]
|
||||
pub fn update_from_now(&mut self) -> Result<(), StdTimestampError> {
|
||||
let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
|
||||
self.counter.1 = unix_epoch_to_ccsds_epoch(now.as_secs()) as u32;
|
||||
if self.fractions.is_some() {
|
||||
self.fractions = fractional_part_from_subsec_ns(
|
||||
self.fractions.unwrap().0,
|
||||
now.subsec_nanos() as u64,
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn new_u16_counter(counter: u16) -> Self {
|
||||
// These values are definitely valid, so it is okay to unwrap here.
|
||||
Self::new_generic(WidthCounterPair(2, counter as u32), None).unwrap()
|
||||
}
|
||||
|
||||
pub fn width_counter_pair(&self) -> WidthCounterPair {
|
||||
self.counter
|
||||
}
|
||||
|
||||
pub fn width_fractions_pair(&self) -> Option<FractionalPart> {
|
||||
self.fractions
|
||||
}
|
||||
|
||||
pub fn set_fractions(&mut self, fractions: FractionalPart) -> Result<(), CucError> {
|
||||
Self::verify_fractions_width(fractions.0)?;
|
||||
Self::verify_fractions_value(fractions)?;
|
||||
self.fractions = Some(fractions);
|
||||
self.update_p_field_fractions();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
self.fractions = None;
|
||||
}
|
||||
let mut update_fractions = true;
|
||||
if let Some(existing_fractions) = self.fractions {
|
||||
if existing_fractions.0 == res {
|
||||
update_fractions = false;
|
||||
}
|
||||
};
|
||||
if update_fractions {
|
||||
self.fractions = Some(FractionalPart(res, 0));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_generic(
|
||||
counter: WidthCounterPair,
|
||||
fractions: Option<FractionalPart>,
|
||||
) -> Result<Self, CucError> {
|
||||
Self::verify_counter_width(counter.0)?;
|
||||
if counter.1 > (2u64.pow(counter.0 as u32 * 8) - 1) as u32 {
|
||||
return Err(CucError::InvalidCounter(counter.0, counter.1 as u64));
|
||||
}
|
||||
if let Some(fractions) = fractions {
|
||||
Self::verify_fractions_width(fractions.0)?;
|
||||
Self::verify_fractions_value(fractions)?;
|
||||
}
|
||||
Ok(Self {
|
||||
pfield: Self::build_p_field(counter.0, fractions.map(|v| v.0)),
|
||||
counter,
|
||||
fractions,
|
||||
})
|
||||
}
|
||||
|
||||
fn build_p_field(counter_width: u8, fractions_width: Option<FractionalResolution>) -> u8 {
|
||||
let mut pfield = P_FIELD_BASE;
|
||||
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;
|
||||
if let Some(fractions_width) = fractions_width {
|
||||
if !(1..=3).contains(&(fractions_width as u8)) {
|
||||
// Okay to panic here, this function is private and all input values should
|
||||
// have been sanitized
|
||||
panic!(
|
||||
"invalid fractions width {:?} for cuc timestamp",
|
||||
fractions_width
|
||||
);
|
||||
}
|
||||
pfield |= fractions_width as u8;
|
||||
}
|
||||
pfield
|
||||
}
|
||||
|
||||
fn update_p_field_fractions(&mut self) {
|
||||
self.pfield &= !(0b11);
|
||||
if let Some(fractions) = self.fractions {
|
||||
self.pfield |= fractions.0 as u8;
|
||||
}
|
||||
}
|
||||
|
||||
#[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
|
||||
}
|
||||
|
||||
/// 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_width(width: FractionalResolution) -> Result<(), CucError> {
|
||||
if width as u8 > 3 {
|
||||
return Err(CucError::InvalidFractionResolution(width));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn verify_fractions_value(val: FractionalPart) -> Result<(), CucError> {
|
||||
if val.1 > 2u32.pow((val.0 as u32) * 8) - 1 {
|
||||
return Err(CucError::InvalidFractions(val.0, val.1 as u64));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl TimeReader for TimeProviderCcsdsEpoch {
|
||||
fn from_bytes(buf: &[u8]) -> Result<Self, TimestampError>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if buf.len() < MIN_CUC_LEN {
|
||||
return Err(TimestampError::ByteConversionError(
|
||||
ByteConversionError::FromSliceTooSmall(SizeMissmatch {
|
||||
expected: MIN_CUC_LEN,
|
||||
found: buf.len(),
|
||||
}),
|
||||
));
|
||||
}
|
||||
match ccsds_time_code_from_p_field(buf[0]) {
|
||||
Ok(code) => {
|
||||
if code != CcsdsTimeCodes::CucCcsdsEpoch {
|
||||
return Err(TimestampError::InvalidTimeCode(
|
||||
CcsdsTimeCodes::CucCcsdsEpoch,
|
||||
code as u8,
|
||||
));
|
||||
}
|
||||
}
|
||||
Err(raw) => {
|
||||
return Err(TimestampError::InvalidTimeCode(
|
||||
CcsdsTimeCodes::CucCcsdsEpoch,
|
||||
raw,
|
||||
))
|
||||
}
|
||||
}
|
||||
let (cntr_len, fractions_len, total_len) =
|
||||
Self::len_components_and_total_from_pfield(buf[0]);
|
||||
if buf.len() < total_len {
|
||||
return Err(TimestampError::ByteConversionError(
|
||||
ByteConversionError::FromSliceTooSmall(SizeMissmatch {
|
||||
expected: total_len,
|
||||
found: buf.len(),
|
||||
}),
|
||||
));
|
||||
}
|
||||
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]);
|
||||
u32::from_be_bytes(tmp_buf)
|
||||
}
|
||||
4 => u32::from_be_bytes(buf[current_idx..current_idx + 4].try_into().unwrap()),
|
||||
_ => panic!("unreachable match arm"),
|
||||
};
|
||||
current_idx += cntr_len as usize;
|
||||
let mut fractions = None;
|
||||
if fractions_len > 0 {
|
||||
match fractions_len {
|
||||
1 => {
|
||||
fractions = Some(FractionalPart(
|
||||
fractions_len.try_into().unwrap(),
|
||||
buf[current_idx] as u32,
|
||||
))
|
||||
}
|
||||
2 => {
|
||||
fractions = Some(FractionalPart(
|
||||
fractions_len.try_into().unwrap(),
|
||||
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]);
|
||||
fractions = Some(FractionalPart(
|
||||
fractions_len.try_into().unwrap(),
|
||||
u32::from_be_bytes(tmp_buf),
|
||||
))
|
||||
}
|
||||
_ => panic!("unreachable match arm"),
|
||||
}
|
||||
}
|
||||
let provider = Self::new_generic(WidthCounterPair(cntr_len, counter), fractions)?;
|
||||
Ok(provider)
|
||||
}
|
||||
}
|
||||
|
||||
impl TimeWriter for TimeProviderCcsdsEpoch {
|
||||
fn write_to_bytes(&self, bytes: &mut [u8]) -> Result<usize, TimestampError> {
|
||||
// Cross check the sizes of the counters against byte widths in the ctor
|
||||
if bytes.len() < self.len_as_bytes() {
|
||||
return Err(TimestampError::ByteConversionError(
|
||||
ByteConversionError::ToSliceTooSmall(SizeMissmatch {
|
||||
found: bytes.len(),
|
||||
expected: self.len_as_bytes(),
|
||||
}),
|
||||
));
|
||||
}
|
||||
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;
|
||||
if let Some(fractions) = self.fractions {
|
||||
match fractions.0 {
|
||||
FractionalResolution::FourMs => bytes[current_idx] = fractions.1 as u8,
|
||||
FractionalResolution::FifteenUs => bytes[current_idx..current_idx + 2]
|
||||
.copy_from_slice(&(fractions.1 as u16).to_be_bytes()),
|
||||
FractionalResolution::SixtyNs => bytes[current_idx..current_idx + 3]
|
||||
.copy_from_slice(&fractions.1.to_be_bytes()[1..4]),
|
||||
// Should also never happen
|
||||
_ => panic!("invalid fractions value"),
|
||||
}
|
||||
current_idx += fractions.0 as usize;
|
||||
}
|
||||
Ok(current_idx)
|
||||
}
|
||||
}
|
||||
|
||||
impl CcsdsTimeProvider for TimeProviderCcsdsEpoch {
|
||||
fn len_as_bytes(&self) -> usize {
|
||||
Self::len_packed_from_pfield(self.pfield)
|
||||
}
|
||||
|
||||
fn p_field(&self) -> (usize, [u8; 2]) {
|
||||
(1, [self.pfield, 0])
|
||||
}
|
||||
|
||||
fn ccdsd_time_code(&self) -> CcsdsTimeCodes {
|
||||
CcsdsTimeCodes::CucCcsdsEpoch
|
||||
}
|
||||
|
||||
/// Please note that this function only works as intended if the time counter resolution
|
||||
/// is one second.
|
||||
fn unix_seconds(&self) -> i64 {
|
||||
ccsds_epoch_to_unix_epoch(self.counter.1 as u64) as i64
|
||||
}
|
||||
|
||||
fn date_time(&self) -> Option<DateTime<Utc>> {
|
||||
let unix_seconds = self.unix_seconds();
|
||||
let ns = if let Some(fractional_part) = self.fractions {
|
||||
convert_fractional_part_to_ns(fractional_part)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
if let LocalResult::Single(res) = Utc.timestamp_opt(unix_seconds, ns as u32) {
|
||||
return Some(res);
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use chrono::{Datelike, Timelike};
|
||||
#[allow(unused_imports)]
|
||||
use std::println;
|
||||
|
||||
#[test]
|
||||
fn test_basic_zero_epoch() {
|
||||
let zero_cuc = TimeProviderCcsdsEpoch::new(0);
|
||||
assert_eq!(zero_cuc.len_as_bytes(), 5);
|
||||
assert_eq!(zero_cuc.ccdsd_time_code(), CcsdsTimeCodes::CucCcsdsEpoch);
|
||||
let counter = zero_cuc.width_counter_pair();
|
||||
assert_eq!(counter.0, 4);
|
||||
assert_eq!(counter.1, 0);
|
||||
let fractions = zero_cuc.width_fractions_pair();
|
||||
assert!(fractions.is_none());
|
||||
let dt = zero_cuc.date_time();
|
||||
assert!(dt.is_some());
|
||||
let dt = dt.unwrap();
|
||||
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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_write_no_fractions() {
|
||||
let mut buf: [u8; 16] = [0; 16];
|
||||
let zero_cuc = TimeProviderCcsdsEpoch::new_generic(WidthCounterPair(4, 0x20102030), None);
|
||||
assert!(zero_cuc.is_ok());
|
||||
let zero_cuc = zero_cuc.unwrap();
|
||||
let res = zero_cuc.write_to_bytes(&mut buf);
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(zero_cuc.len_as_bytes(), 5);
|
||||
assert_eq!(pfield_len(buf[0]), 1);
|
||||
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());
|
||||
assert_eq!(time_code.unwrap(), CcsdsTimeCodes::CucCcsdsEpoch);
|
||||
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);
|
||||
assert_eq!(buf[5], 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_datetime_now() {
|
||||
let now = Utc::now();
|
||||
let cuc_now = TimeProviderCcsdsEpoch::from_now(FractionalResolution::SixtyNs);
|
||||
assert!(cuc_now.is_ok());
|
||||
let cuc_now = cuc_now.unwrap();
|
||||
let dt_opt = cuc_now.date_time();
|
||||
assert!(dt_opt.is_some());
|
||||
let dt = dt_opt.unwrap();
|
||||
let diff = dt - now;
|
||||
assert!(diff.num_milliseconds() < 1000);
|
||||
println!("datetime from cuc: {}", dt);
|
||||
println!("datetime now: {}", now);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_no_fractions() {
|
||||
let mut buf: [u8; 16] = [0; 16];
|
||||
let zero_cuc =
|
||||
TimeProviderCcsdsEpoch::new_generic(WidthCounterPair(4, 0x20102030), None).unwrap();
|
||||
zero_cuc.write_to_bytes(&mut buf).unwrap();
|
||||
let cuc_read_back =
|
||||
TimeProviderCcsdsEpoch::from_bytes(&buf).expect("reading cuc timestamp failed");
|
||||
assert_eq!(cuc_read_back, zero_cuc);
|
||||
assert_eq!(cuc_read_back.width_counter_pair().1, 0x20102030);
|
||||
assert_eq!(cuc_read_back.width_fractions_pair(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_read_len() {
|
||||
let mut buf: [u8; 16] = [0; 16];
|
||||
for i in 0..2 {
|
||||
let res = TimeProviderCcsdsEpoch::from_bytes(&buf[0..i]);
|
||||
assert!(res.is_err());
|
||||
let err = res.unwrap_err();
|
||||
if let TimestampError::ByteConversionError(ByteConversionError::FromSliceTooSmall(e)) =
|
||||
err
|
||||
{
|
||||
assert_eq!(e.found, i);
|
||||
assert_eq!(e.expected, 2);
|
||||
}
|
||||
}
|
||||
let large_stamp = TimeProviderCcsdsEpoch::new_with_fine_fractions(22, 300).unwrap();
|
||||
large_stamp.write_to_bytes(&mut buf).unwrap();
|
||||
for i in 2..large_stamp.len_as_bytes() - 1 {
|
||||
let res = TimeProviderCcsdsEpoch::from_bytes(&buf[0..i]);
|
||||
assert!(res.is_err());
|
||||
let err = res.unwrap_err();
|
||||
if let TimestampError::ByteConversionError(ByteConversionError::FromSliceTooSmall(e)) =
|
||||
err
|
||||
{
|
||||
assert_eq!(e.found, i);
|
||||
assert_eq!(e.expected, large_stamp.len_as_bytes());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_and_read_tiny_stamp() {
|
||||
let mut buf = [0; 2];
|
||||
let cuc = TimeProviderCcsdsEpoch::new_generic(WidthCounterPair(1, 200), None);
|
||||
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);
|
||||
let cuc_read_back = TimeProviderCcsdsEpoch::from_bytes(&buf);
|
||||
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];
|
||||
let cuc = TimeProviderCcsdsEpoch::new_generic(WidthCounterPair(2, 40000), None);
|
||||
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);
|
||||
let cuc_read_back = TimeProviderCcsdsEpoch::from_bytes(&buf);
|
||||
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() {}
|
||||
#[test]
|
||||
fn write_read_three_byte_cntr_stamp() {
|
||||
let mut buf = [0; 4];
|
||||
let cuc = TimeProviderCcsdsEpoch::new_generic(WidthCounterPair(3, 2_u32.pow(24) - 2), None);
|
||||
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);
|
||||
let cuc_read_back = TimeProviderCcsdsEpoch::from_bytes(&buf);
|
||||
assert!(cuc_read_back.is_ok());
|
||||
let cuc_read_back = cuc_read_back.unwrap();
|
||||
assert_eq!(cuc_read_back, cuc);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_write_invalid_buf() {
|
||||
let mut buf: [u8; 16] = [0; 16];
|
||||
let res = TimeProviderCcsdsEpoch::new_with_fine_fractions(0, 0);
|
||||
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();
|
||||
if let TimestampError::ByteConversionError(ByteConversionError::ToSliceTooSmall(e)) =
|
||||
err
|
||||
{
|
||||
assert_eq!(e.expected, cuc.len_as_bytes());
|
||||
assert_eq!(e.found, i);
|
||||
} else {
|
||||
panic!("unexpected error: {}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn invalid_ccsds_stamp_type() {
|
||||
let mut buf: [u8; 16] = [0; 16];
|
||||
buf[0] |= (CcsdsTimeCodes::CucAgencyEpoch as u8) << 4;
|
||||
let res = TimeProviderCcsdsEpoch::from_bytes(&buf);
|
||||
assert!(res.is_err());
|
||||
let err = res.unwrap_err();
|
||||
if let TimestampError::InvalidTimeCode(code, raw) = err {
|
||||
assert_eq!(code, CcsdsTimeCodes::CucCcsdsEpoch);
|
||||
assert_eq!(raw, CcsdsTimeCodes::CucAgencyEpoch as u8);
|
||||
} else {
|
||||
panic!("unexpected error: {}", err);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_write_with_coarse_fractions() {
|
||||
let mut buf: [u8; 16] = [0; 16];
|
||||
let cuc = TimeProviderCcsdsEpoch::new_with_coarse_fractions(0x30201060, 120);
|
||||
assert!(cuc.fractions.is_some());
|
||||
assert_eq!(cuc.fractions.unwrap().1, 120);
|
||||
assert_eq!(cuc.fractions.unwrap().0, FractionalResolution::FourMs);
|
||||
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);
|
||||
assert_eq!(
|
||||
u32::from_be_bytes(buf[1..5].try_into().unwrap()),
|
||||
0x30201060
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_with_coarse_fractions() {
|
||||
let mut buf: [u8; 16] = [0; 16];
|
||||
let cuc = TimeProviderCcsdsEpoch::new_with_coarse_fractions(0x30201060, 120);
|
||||
let res = cuc.write_to_bytes(&mut buf);
|
||||
assert!(res.is_ok());
|
||||
let res = TimeProviderCcsdsEpoch::from_bytes(&buf);
|
||||
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];
|
||||
let cuc = TimeProviderCcsdsEpoch::new_with_medium_fractions(0x30303030, 30000);
|
||||
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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_with_medium_fractions() {
|
||||
let mut buf: [u8; 16] = [0; 16];
|
||||
let cuc = TimeProviderCcsdsEpoch::new_with_medium_fractions(0x30303030, 30000);
|
||||
let res = cuc.write_to_bytes(&mut buf);
|
||||
assert!(res.is_ok());
|
||||
let res = TimeProviderCcsdsEpoch::from_bytes(&buf);
|
||||
assert!(res.is_ok());
|
||||
let cuc_read_back = res.unwrap();
|
||||
assert_eq!(cuc_read_back, cuc);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_write_with_fine_fractions() {
|
||||
let mut buf: [u8; 16] = [0; 16];
|
||||
let cuc =
|
||||
TimeProviderCcsdsEpoch::new_with_fine_fractions(0x30303030, u16::MAX as u32 + 60000);
|
||||
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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_with_fine_fractions() {
|
||||
let mut buf: [u8; 16] = [0; 16];
|
||||
let cuc =
|
||||
TimeProviderCcsdsEpoch::new_with_fine_fractions(0x30303030, u16::MAX as u32 + 60000);
|
||||
assert!(cuc.is_ok());
|
||||
let cuc = cuc.unwrap();
|
||||
let res = cuc.write_to_bytes(&mut buf);
|
||||
assert!(res.is_ok());
|
||||
let res = TimeProviderCcsdsEpoch::from_bytes(&buf);
|
||||
assert!(res.is_ok());
|
||||
let cuc_read_back = res.unwrap();
|
||||
assert_eq!(cuc_read_back, cuc);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fractional_converter() {
|
||||
let ns = convert_fractional_part_to_ns(FractionalPart(FractionalResolution::FourMs, 2));
|
||||
// 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.
|
||||
let ns = convert_fractional_part_to_ns(FractionalPart(
|
||||
FractionalResolution::SixtyNs,
|
||||
2_u32.pow(24) - 2,
|
||||
));
|
||||
assert_eq!(ns, 999999940);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_fractional_converter_invalid_input() {
|
||||
convert_fractional_part_to_ns(FractionalPart(FractionalResolution::FourMs, 256));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_fractional_converter_invalid_input_2() {
|
||||
convert_fractional_part_to_ns(FractionalPart(
|
||||
FractionalResolution::SixtyNs,
|
||||
2_u32.pow(32) - 1,
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fractional_part_formula() {
|
||||
let fractional_part =
|
||||
fractional_part_from_subsec_ns(FractionalResolution::FourMs, 7843138).unwrap();
|
||||
assert_eq!(fractional_part.1, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fractional_part_formula_2() {
|
||||
let fractional_part =
|
||||
fractional_part_from_subsec_ns(FractionalResolution::FourMs, 12000000).unwrap();
|
||||
assert_eq!(fractional_part.1, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fractional_part_formula_3() {
|
||||
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,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(fractional_part.1, 100);
|
||||
// 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,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(fractional_part.1, 101);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_fractions() {
|
||||
let mut stamp = TimeProviderCcsdsEpoch::new(2000);
|
||||
let res = stamp.set_fractions(FractionalPart(FractionalResolution::SixtyNs, 5000));
|
||||
assert!(res.is_ok());
|
||||
assert!(stamp.fractions.is_some());
|
||||
let fractions = stamp.fractions.unwrap();
|
||||
assert_eq!(fractions.0, FractionalResolution::SixtyNs);
|
||||
assert_eq!(fractions.1, 5000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_fract_resolution() {
|
||||
let mut stamp = TimeProviderCcsdsEpoch::new(2000);
|
||||
stamp.set_fractional_resolution(FractionalResolution::SixtyNs);
|
||||
assert!(stamp.fractions.is_some());
|
||||
let fractions = stamp.fractions.unwrap();
|
||||
assert_eq!(fractions.0, FractionalResolution::SixtyNs);
|
||||
assert_eq!(fractions.1, 0);
|
||||
let res = stamp.update_from_now();
|
||||
assert!(res.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn assert_largest_fractions() {
|
||||
let fractions =
|
||||
fractional_part_from_subsec_ns(FractionalResolution::SixtyNs, 10u64.pow(9) - 1)
|
||||
.unwrap();
|
||||
// The value can not be larger than representable by 3 bytes
|
||||
// Assert that the maximum resolution can be reached
|
||||
assert_eq!(fractions.1, 2_u32.pow(3 * 8) - 2);
|
||||
}
|
||||
}
|
243
src/time/mod.rs
Normal file
243
src/time/mod.rs
Normal file
@ -0,0 +1,243 @@
|
||||
//! CCSDS Time Code Formats according to [CCSDS 301.0-B-4](https://public.ccsds.org/Pubs/301x0b4e1.pdf)
|
||||
use crate::{ByteConversionError, SizeMissmatch};
|
||||
use chrono::{DateTime, LocalResult, TimeZone, Utc};
|
||||
use core::fmt::{Display, Formatter};
|
||||
|
||||
#[allow(unused_imports)]
|
||||
#[cfg(not(feature = "std"))]
|
||||
use num_traits::float::FloatCore;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[cfg(feature = "std")]
|
||||
use std::error::Error;
|
||||
#[cfg(feature = "std")]
|
||||
use std::time::{SystemTime, SystemTimeError};
|
||||
|
||||
pub mod ascii;
|
||||
pub mod cds;
|
||||
pub mod cuc;
|
||||
|
||||
pub const DAYS_CCSDS_TO_UNIX: i32 = -4383;
|
||||
pub const SECONDS_PER_DAY: u32 = 86400;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub enum CcsdsTimeCodes {
|
||||
CucCcsdsEpoch = 0b001,
|
||||
CucAgencyEpoch = 0b010,
|
||||
Cds = 0b100,
|
||||
Ccs = 0b101,
|
||||
AgencyDefined = 0b110,
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for CcsdsTimeCodes {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
x if x == CcsdsTimeCodes::CucCcsdsEpoch as u8 => Ok(CcsdsTimeCodes::CucCcsdsEpoch),
|
||||
x if x == CcsdsTimeCodes::CucAgencyEpoch as u8 => Ok(CcsdsTimeCodes::CucAgencyEpoch),
|
||||
x if x == CcsdsTimeCodes::Cds as u8 => Ok(CcsdsTimeCodes::Cds),
|
||||
x if x == CcsdsTimeCodes::Ccs as u8 => Ok(CcsdsTimeCodes::Ccs),
|
||||
x if x == CcsdsTimeCodes::AgencyDefined as u8 => Ok(CcsdsTimeCodes::AgencyDefined),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieve the CCSDS time code from the p-field. If no valid time code identifier is found, the
|
||||
/// value of the raw time code identification field is returned.
|
||||
pub fn ccsds_time_code_from_p_field(pfield: u8) -> Result<CcsdsTimeCodes, u8> {
|
||||
let raw_bits = (pfield >> 4) & 0b111;
|
||||
CcsdsTimeCodes::try_from(raw_bits).map_err(|_| raw_bits)
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
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),
|
||||
ByteConversionError(ByteConversionError),
|
||||
CdsError(cds::CdsError),
|
||||
CucError(cuc::CucError),
|
||||
CustomEpochNotSupported,
|
||||
}
|
||||
|
||||
impl From<cds::CdsError> for TimestampError {
|
||||
fn from(e: cds::CdsError) -> Self {
|
||||
TimestampError::CdsError(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<cuc::CucError> for TimestampError {
|
||||
fn from(e: cuc::CucError) -> Self {
|
||||
TimestampError::CucError(e)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum StdTimestampError {
|
||||
SystemTimeError(SystemTimeError),
|
||||
TimestampError(TimestampError),
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl From<TimestampError> for StdTimestampError {
|
||||
fn from(v: TimestampError) -> Self {
|
||||
Self::TimestampError(v)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl From<SystemTimeError> for StdTimestampError {
|
||||
fn from(v: SystemTimeError) -> Self {
|
||||
Self::SystemTimeError(v)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for TimestampError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
|
||||
match self {
|
||||
TimestampError::InvalidTimeCode(time_code, raw_val) => {
|
||||
write!(
|
||||
f,
|
||||
"invalid raw time code value {} for time code {:?}",
|
||||
raw_val, time_code
|
||||
)
|
||||
}
|
||||
TimestampError::CdsError(e) => {
|
||||
write!(f, "cds error {}", e)
|
||||
}
|
||||
TimestampError::CucError(e) => {
|
||||
write!(f, "cuc error {}", e)
|
||||
}
|
||||
TimestampError::ByteConversionError(e) => {
|
||||
write!(f, "byte conversion error {}", e)
|
||||
}
|
||||
TimestampError::CustomEpochNotSupported => {
|
||||
write!(f, "custom epochs are not supported")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl Error for TimestampError {
|
||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||
match self {
|
||||
TimestampError::ByteConversionError(e) => Some(e),
|
||||
TimestampError::CdsError(e) => Some(e),
|
||||
TimestampError::CucError(e) => Some(e),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
||||
pub fn seconds_since_epoch() -> f64 {
|
||||
SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.expect("System time generation failed")
|
||||
.as_secs_f64()
|
||||
}
|
||||
|
||||
/// Convert UNIX days to CCSDS days
|
||||
///
|
||||
/// - CCSDS epoch: 1958 January 1
|
||||
/// - UNIX Epoch: 1970 January 1
|
||||
pub const fn unix_to_ccsds_days(unix_days: i64) -> i64 {
|
||||
unix_days - DAYS_CCSDS_TO_UNIX as i64
|
||||
}
|
||||
|
||||
/// Convert CCSDS days to UNIX days
|
||||
///
|
||||
/// - CCSDS epoch: 1958 January 1
|
||||
/// - UNIX Epoch: 1970 January 1
|
||||
pub const fn ccsds_to_unix_days(ccsds_days: i64) -> i64 {
|
||||
ccsds_days + DAYS_CCSDS_TO_UNIX as i64
|
||||
}
|
||||
|
||||
/// Similar to [unix_to_ccsds_days] but converts the epoch instead, which is the number of elpased
|
||||
/// seconds since the CCSDS and UNIX epoch times.
|
||||
pub const fn unix_epoch_to_ccsds_epoch(unix_epoch: u64) -> u64 {
|
||||
(unix_epoch as i64 - (DAYS_CCSDS_TO_UNIX as i64 * SECONDS_PER_DAY as i64)) as u64
|
||||
}
|
||||
|
||||
pub const fn ccsds_epoch_to_unix_epoch(ccsds_epoch: u64) -> u64 {
|
||||
(ccsds_epoch as i64 + (DAYS_CCSDS_TO_UNIX as i64 * SECONDS_PER_DAY as i64)) as u64
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
||||
pub fn ms_of_day_using_sysclock() -> u32 {
|
||||
ms_of_day(seconds_since_epoch())
|
||||
}
|
||||
|
||||
pub fn ms_of_day(seconds_since_epoch: f64) -> u32 {
|
||||
let fraction_ms = seconds_since_epoch - seconds_since_epoch.floor();
|
||||
let ms_of_day: u32 = (((seconds_since_epoch.floor() as u32 % SECONDS_PER_DAY) * 1000) as f64
|
||||
+ fraction_ms)
|
||||
.floor() as u32;
|
||||
ms_of_day
|
||||
}
|
||||
|
||||
pub trait TimeWriter {
|
||||
/// Generic function to convert write a timestamp into a raw buffer.
|
||||
/// Returns the number of written bytes on success.
|
||||
fn write_to_bytes(&self, bytes: &mut [u8]) -> Result<usize, TimestampError>;
|
||||
}
|
||||
|
||||
pub trait TimeReader {
|
||||
fn from_bytes(buf: &[u8]) -> Result<Self, TimestampError>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
/// Trait for generic CCSDS time providers.
|
||||
pub trait CcsdsTimeProvider {
|
||||
fn len_as_bytes(&self) -> usize;
|
||||
|
||||
/// Returns the pfield of the time provider. The pfield can have one or two bytes depending
|
||||
/// on the extension bit (first bit). The time provider should returns a tuple where the first
|
||||
/// entry denotes the length of the pfield and the second entry is the value of the pfield
|
||||
/// in big endian format.
|
||||
fn p_field(&self) -> (usize, [u8; 2]);
|
||||
fn ccdsd_time_code(&self) -> CcsdsTimeCodes;
|
||||
fn unix_seconds(&self) -> i64;
|
||||
fn date_time(&self) -> Option<DateTime<Utc>>;
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "std"))]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_days_conversion() {
|
||||
assert_eq!(unix_to_ccsds_days(DAYS_CCSDS_TO_UNIX.into()), 0);
|
||||
assert_eq!(ccsds_to_unix_days(0), DAYS_CCSDS_TO_UNIX.into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_current_time() {
|
||||
let sec_floats = seconds_since_epoch();
|
||||
assert!(sec_floats > 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ccsds_epoch() {
|
||||
let now = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap();
|
||||
let unix_epoch = now.as_secs();
|
||||
let ccsds_epoch = unix_epoch_to_ccsds_epoch(now.as_secs());
|
||||
assert!(ccsds_epoch > unix_epoch);
|
||||
assert_eq!((ccsds_epoch - unix_epoch) % SECONDS_PER_DAY as u64, 0);
|
||||
let days_diff = (ccsds_epoch - unix_epoch) / SECONDS_PER_DAY as u64;
|
||||
assert_eq!(days_diff, -DAYS_CCSDS_TO_UNIX as u64);
|
||||
}
|
||||
}
|
80
src/tm.rs
80
src/tm.rs
@ -9,6 +9,7 @@ use crate::{
|
||||
CCSDS_HEADER_LEN,
|
||||
};
|
||||
use core::mem::size_of;
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
use zerocopy::AsBytes;
|
||||
|
||||
@ -21,7 +22,7 @@ pub const PUC_TM_MIN_SEC_HEADER_LEN: usize = 7;
|
||||
pub const PUS_TM_MIN_LEN_WITHOUT_SOURCE_DATA: usize =
|
||||
CCSDS_HEADER_LEN + PUC_TM_MIN_SEC_HEADER_LEN + size_of::<CrcType>();
|
||||
|
||||
pub trait PusTmSecondaryHeaderT {
|
||||
pub trait GenericPusTmSecondaryHeader {
|
||||
fn pus_version(&self) -> PusVersion;
|
||||
fn sc_time_ref_status(&self) -> u8;
|
||||
fn service(&self) -> u8;
|
||||
@ -31,6 +32,7 @@ pub trait PusTmSecondaryHeaderT {
|
||||
}
|
||||
|
||||
pub mod zc {
|
||||
use super::GenericPusTmSecondaryHeader;
|
||||
use crate::ecss::{PusError, PusVersion};
|
||||
use zerocopy::{AsBytes, FromBytes, NetworkEndian, Unaligned, U16};
|
||||
|
||||
@ -76,7 +78,7 @@ pub mod zc {
|
||||
}
|
||||
}
|
||||
|
||||
impl super::PusTmSecondaryHeaderT for PusTmSecHeaderWithoutTimestamp {
|
||||
impl GenericPusTmSecondaryHeader for PusTmSecHeaderWithoutTimestamp {
|
||||
fn pus_version(&self) -> PusVersion {
|
||||
PusVersion::try_from(self.pus_version_and_sc_time_ref_status >> 4 & 0b1111)
|
||||
.unwrap_or(PusVersion::Invalid)
|
||||
@ -104,19 +106,20 @@ pub mod zc {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Serialize, Deserialize, Copy, Clone, Debug)]
|
||||
pub struct PusTmSecondaryHeader<'slice> {
|
||||
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct PusTmSecondaryHeader<'stamp> {
|
||||
pus_version: PusVersion,
|
||||
pub sc_time_ref_status: u8,
|
||||
pub service: u8,
|
||||
pub subservice: u8,
|
||||
pub msg_counter: u16,
|
||||
pub dest_id: u16,
|
||||
pub time_stamp: &'slice [u8],
|
||||
pub time_stamp: &'stamp [u8],
|
||||
}
|
||||
|
||||
impl<'slice> PusTmSecondaryHeader<'slice> {
|
||||
pub fn new_simple(service: u8, subservice: u8, time_stamp: &'slice [u8]) -> Self {
|
||||
impl<'stamp> PusTmSecondaryHeader<'stamp> {
|
||||
pub fn new_simple(service: u8, subservice: u8, time_stamp: &'stamp [u8]) -> Self {
|
||||
PusTmSecondaryHeader {
|
||||
pus_version: PusVersion::PusC,
|
||||
sc_time_ref_status: 0,
|
||||
@ -133,7 +136,7 @@ impl<'slice> PusTmSecondaryHeader<'slice> {
|
||||
subservice: u8,
|
||||
msg_counter: u16,
|
||||
dest_id: u16,
|
||||
time_stamp: &'slice [u8],
|
||||
time_stamp: &'stamp [u8],
|
||||
) -> Self {
|
||||
PusTmSecondaryHeader {
|
||||
pus_version: PusVersion::PusC,
|
||||
@ -147,7 +150,7 @@ impl<'slice> PusTmSecondaryHeader<'slice> {
|
||||
}
|
||||
}
|
||||
|
||||
impl PusTmSecondaryHeaderT for PusTmSecondaryHeader<'_> {
|
||||
impl GenericPusTmSecondaryHeader for PusTmSecondaryHeader<'_> {
|
||||
fn pus_version(&self) -> PusVersion {
|
||||
self.pus_version
|
||||
}
|
||||
@ -193,25 +196,31 @@ impl<'slice> TryFrom<zc::PusTmSecHeader<'slice>> for PusTmSecondaryHeader<'slice
|
||||
/// structure to generate the raw byte representation of PUS telemetry or to
|
||||
/// deserialize from one from raw bytes.
|
||||
///
|
||||
/// This class also derives the [serde::Serialize] and [serde::Deserialize] trait which allows
|
||||
/// to send around TM packets in a raw byte format using a serde provider like
|
||||
/// [postcard](https://docs.rs/postcard/latest/postcard/).
|
||||
/// This class also derives the [serde::Serialize] and [serde::Deserialize] trait if the [serde]
|
||||
/// feature is used which allows to send around TM packets in a raw byte format using a serde
|
||||
/// provider like [postcard](https://docs.rs/postcard/latest/postcard/).
|
||||
///
|
||||
/// There is no spare bytes support yet.
|
||||
#[derive(PartialEq, Eq, Serialize, Deserialize, Debug, Copy, Clone)]
|
||||
pub struct PusTm<'slice> {
|
||||
///
|
||||
/// # Lifetimes
|
||||
///
|
||||
/// * `'src_data` - Life time of a buffer where the user provided time stamp and source data will
|
||||
/// be serialized into.
|
||||
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub struct PusTm<'src_data> {
|
||||
pub sp_header: SpHeader,
|
||||
pub sec_header: PusTmSecondaryHeader<'slice>,
|
||||
pub sec_header: PusTmSecondaryHeader<'src_data>,
|
||||
/// If this is set to false, a manual call to [PusTm::calc_own_crc16] or
|
||||
/// [PusTm::update_packet_fields] is necessary for the serialized or cached CRC16 to be valid.
|
||||
pub calc_crc_on_serialization: bool,
|
||||
#[serde(skip)]
|
||||
raw_data: Option<&'slice [u8]>,
|
||||
source_data: Option<&'slice [u8]>,
|
||||
#[cfg_attr(feature = "serde", serde(skip))]
|
||||
raw_data: Option<&'src_data [u8]>,
|
||||
source_data: Option<&'src_data [u8]>,
|
||||
crc16: Option<u16>,
|
||||
}
|
||||
|
||||
impl<'slice> PusTm<'slice> {
|
||||
impl<'src_data> PusTm<'src_data> {
|
||||
/// Generates a new struct instance.
|
||||
///
|
||||
/// # Arguments
|
||||
@ -226,8 +235,8 @@ impl<'slice> PusTm<'slice> {
|
||||
/// the correct value to this field manually
|
||||
pub fn new(
|
||||
sp_header: &mut SpHeader,
|
||||
sec_header: PusTmSecondaryHeader<'slice>,
|
||||
source_data: Option<&'slice [u8]>,
|
||||
sec_header: PusTmSecondaryHeader<'src_data>,
|
||||
source_data: Option<&'src_data [u8]>,
|
||||
set_ccsds_len: bool,
|
||||
) -> Self {
|
||||
sp_header.set_packet_type(PacketType::Tm);
|
||||
@ -255,11 +264,11 @@ impl<'slice> PusTm<'slice> {
|
||||
length
|
||||
}
|
||||
|
||||
pub fn time_stamp(&self) -> &'slice [u8] {
|
||||
pub fn time_stamp(&self) -> &'src_data [u8] {
|
||||
self.sec_header.time_stamp
|
||||
}
|
||||
|
||||
pub fn source_data(&self) -> Option<&'slice [u8]> {
|
||||
pub fn source_data(&self) -> Option<&'src_data [u8]> {
|
||||
self.source_data
|
||||
}
|
||||
|
||||
@ -311,7 +320,6 @@ impl<'slice> PusTm<'slice> {
|
||||
/// Write the raw PUS byte representation to a provided buffer.
|
||||
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(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
|
||||
@ -320,10 +328,8 @@ impl<'slice> PusTm<'slice> {
|
||||
})
|
||||
.into());
|
||||
}
|
||||
sph_zc
|
||||
.to_bytes(&mut slice[curr_idx..curr_idx + CCSDS_HEADER_LEN])
|
||||
.ok_or(ByteConversionError::ZeroCopyToError)?;
|
||||
|
||||
self.sp_header
|
||||
.write_to_be_bytes(&mut slice[0..CCSDS_HEADER_LEN])?;
|
||||
curr_idx += CCSDS_HEADER_LEN;
|
||||
let sec_header_len = size_of::<zc::PusTmSecHeaderWithoutTimestamp>();
|
||||
let sec_header = zc::PusTmSecHeaderWithoutTimestamp::try_from(self.sec_header).unwrap();
|
||||
@ -389,7 +395,7 @@ impl<'slice> PusTm<'slice> {
|
||||
/// the instance and the found byte length of the packet. The timestamp length needs to be
|
||||
/// known beforehand.
|
||||
pub fn from_bytes(
|
||||
slice: &'slice [u8],
|
||||
slice: &'src_data [u8],
|
||||
timestamp_len: usize,
|
||||
) -> Result<(Self, usize), PusError> {
|
||||
let raw_data_len = slice.len();
|
||||
@ -397,11 +403,9 @@ impl<'slice> PusTm<'slice> {
|
||||
return Err(PusError::RawDataTooShort(raw_data_len));
|
||||
}
|
||||
let mut current_idx = 0;
|
||||
let sph =
|
||||
crate::zc::SpHeader::from_bytes(&slice[current_idx..current_idx + CCSDS_HEADER_LEN])
|
||||
.ok_or(ByteConversionError::ZeroCopyFromError)?;
|
||||
let (sp_header, _) = SpHeader::from_be_bytes(&slice[0..CCSDS_HEADER_LEN])?;
|
||||
current_idx += 6;
|
||||
let total_len = sph.total_len();
|
||||
let total_len = sp_header.total_len();
|
||||
if raw_data_len < total_len || total_len < PUS_TM_MIN_LEN_WITHOUT_SOURCE_DATA {
|
||||
return Err(PusError::RawDataTooShort(raw_data_len));
|
||||
}
|
||||
@ -417,7 +421,7 @@ impl<'slice> PusTm<'slice> {
|
||||
current_idx += timestamp_len;
|
||||
let raw_data = &slice[0..total_len];
|
||||
let pus_tm = PusTm {
|
||||
sp_header: SpHeader::from(sph),
|
||||
sp_header,
|
||||
sec_header: PusTmSecondaryHeader::try_from(zc_sec_header_wrapper).unwrap(),
|
||||
raw_data: Some(&slice[0..total_len]),
|
||||
source_data: user_data_from_raw(current_idx, total_len, raw_data_len, slice)?,
|
||||
@ -452,7 +456,7 @@ impl PusPacket for PusTm<'_> {
|
||||
}
|
||||
|
||||
//noinspection RsTraitImplementation
|
||||
impl PusTmSecondaryHeaderT for PusTm<'_> {
|
||||
impl GenericPusTmSecondaryHeader for PusTm<'_> {
|
||||
delegate!(to self.sec_header {
|
||||
fn pus_version(&self) -> PusVersion;
|
||||
fn service(&self) -> u8;
|
||||
@ -470,13 +474,13 @@ mod tests {
|
||||
use crate::SpHeader;
|
||||
|
||||
fn base_ping_reply_full_ctor(time_stamp: &[u8]) -> PusTm {
|
||||
let mut sph = SpHeader::tm(0x123, 0x234, 0).unwrap();
|
||||
let mut sph = SpHeader::tm_unseg(0x123, 0x234, 0).unwrap();
|
||||
let tc_header = PusTmSecondaryHeader::new_simple(17, 2, &time_stamp);
|
||||
PusTm::new(&mut sph, tc_header, None, true)
|
||||
}
|
||||
|
||||
fn base_hk_reply<'a>(time_stamp: &'a [u8], src_data: &'a [u8]) -> PusTm<'a> {
|
||||
let mut sph = SpHeader::tm(0x123, 0x234, 0).unwrap();
|
||||
let mut sph = SpHeader::tm_unseg(0x123, 0x234, 0).unwrap();
|
||||
let tc_header = PusTmSecondaryHeader::new_simple(3, 5, &time_stamp);
|
||||
PusTm::new(&mut sph, tc_header, Some(src_data), true)
|
||||
}
|
||||
@ -548,7 +552,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_manual_field_update() {
|
||||
let mut sph = SpHeader::tm(0x123, 0x234, 0).unwrap();
|
||||
let mut sph = SpHeader::tm_unseg(0x123, 0x234, 0).unwrap();
|
||||
let tc_header = PusTmSecondaryHeader::new_simple(17, 2, dummy_time_stamp());
|
||||
let mut tm = PusTm::new(&mut sph, tc_header, None, false);
|
||||
tm.calc_crc_on_serialization = false;
|
||||
|
Reference in New Issue
Block a user