1 Commits

Author SHA1 Message Date
251fcdd6c8 Merge branch 'add_cuc_time_impl' into all_features
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2022-12-18 16:23:25 +01:00
13 changed files with 76 additions and 224 deletions

View File

@@ -20,21 +20,6 @@ jobs:
command: check command: check
args: --release 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: cross-check:
name: Check Cross name: Check Cross
runs-on: ubuntu-latest runs-on: ubuntu-latest
@@ -73,21 +58,6 @@ jobs:
command: fmt command: fmt
args: --all -- --check 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: clippy:
name: Clippy name: Clippy
runs-on: ubuntu-latest runs-on: ubuntu-latest

View File

@@ -8,29 +8,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
# [unreleased] # [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 ## Changed
- `serde` support is now optional and behind the `serde` feature. - `serde` support is now optional and behind the `serde` feature.
@@ -42,18 +19,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
The function now returns the remaining slice as well. The function now returns the remaining slice as well.
- All CDS specific functionality was moved into the `cds` submodule of the `time` - All CDS specific functionality was moved into the `cds` submodule of the `time`
module. `CdsShortTimeProvider` was renamed to `TimeProvider`. module. `CdsShortTimeProvider` was renamed to `TimeProvider`.
PR: https://egit.irs.uni-stuttgart.de/rust/spacepackets/pulls/3
## Added ## 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 - Added `std::error::Error` implementation for all error enumerations if the `std` feature
is enabled. is enabled.
- CUC timestamp implementation as specified in CCSDS 301.0-B-4 section 3.2. - 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. - 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. - Added MSRV in `Cargo.toml` with the `rust-version` field set to Rust 1.60.
- `serde` `Serialize` and `Deserialize` added to all types. - `serde` `Serialize` and `Deserialize` added to all types.

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "spacepackets" name = "spacepackets"
version = "0.4.2" version = "0.3.1"
edition = "2021" edition = "2021"
rust-version = "1.60" rust-version = "1.60"
authors = ["Robin Mueller <muellerr@irs.uni-stuttgart.de>"] authors = ["Robin Mueller <muellerr@irs.uni-stuttgart.de>"]
@@ -14,11 +14,11 @@ categories = ["aerospace", "aerospace::space-protocols", "no-std", "hardware-sup
[dependencies] [dependencies]
zerocopy = "0.6" zerocopy = "0.6"
crc = "3" crc = "3.0"
delegate = "0.8" delegate = "0.8"
[dependencies.serde] [dependencies.serde]
version = "1" version = "1.0"
optional = true optional = true
default-features = false default-features = false
features = ["derive"] features = ["derive"]
@@ -35,9 +35,9 @@ default-features = false
version = "1.0" version = "1.0"
[features] [features]
default = ["std"] default = ["std", "dep:serde"]
std = ["chrono/std", "chrono/clock", "alloc"] std = ["chrono/std", "chrono/clock", "alloc"]
serde = ["dep:serde", "chrono/serde"] serde = ["chrono/serde"]
alloc = ["postcard/alloc", "chrono/alloc"] alloc = ["postcard/alloc", "chrono/alloc"]
[package.metadata.docs.rs] [package.metadata.docs.rs]

View File

@@ -15,9 +15,7 @@ Currently, this includes the following components:
[CCSDS Blue Book 133.0-B-2](https://public.ccsds.org/Pubs/133x0b2e1.pdf) [CCSDS Blue Book 133.0-B-2](https://public.ccsds.org/Pubs/133x0b2e1.pdf)
- PUS Telecommand and PUS Telemetry implementation according to the - 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/). [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/).
- CUC (CCSDS Unsegmented Time Code) implementation according to - CDS Short 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) [CCSDS 301.0-B-4 3.3](https://public.ccsds.org/Pubs/301x0b4e1.pdf)
- Some helper types to support ASCII timecodes ad specified in - Some helper types to support ASCII timecodes ad specified in
[CCSDS 301.0-B-4 3.5](https://public.ccsds.org/Pubs/301x0b4e1.pdf) [CCSDS 301.0-B-4 3.5](https://public.ccsds.org/Pubs/301x0b4e1.pdf)
@@ -30,15 +28,12 @@ It also offers optional support for [`serde`](https://serde.rs/). This allows se
deserializing them with an appropriate `serde` provider like deserializing them with an appropriate `serde` provider like
[`postcard`](https://github.com/jamesmunns/postcard). [`postcard`](https://github.com/jamesmunns/postcard).
## Default features Default features:
- [`std`](https://doc.rust-lang.org/std/): Enables functionality relying on the standard library. - [`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 - [`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). like [`alloc::vec::Vec`](https://doc.rust-lang.org/beta/alloc/vec/struct.Vec.html).
Enabled by the `std` feature. Enabled by the `std` feature.
## Optional Features
- [`serde`](https://serde.rs/): Adds `serde` support for most types by adding `Serialize` and `Deserialize` `derive`s - [`serde`](https://serde.rs/): Adds `serde` support for most types by adding `Serialize` and `Deserialize` `derive`s
# Examples # Examples

View File

@@ -10,6 +10,6 @@ ARG DEBIAN_FRONTEND=noninteractive
# set CROSS_CONTAINER_IN_CONTAINER to inform `cross` that it is executed from within a container # set CROSS_CONTAINER_IN_CONTAINER to inform `cross` that it is executed from within a container
ENV CROSS_CONTAINER_IN_CONTAINER=true ENV CROSS_CONTAINER_IN_CONTAINER=true
RUN rustup install nightly && \ # TODO: installing cross is problematic, permission issues
rustup target add thumbv7em-none-eabihf armv7-unknown-linux-gnueabihf && \ RUN rustup target add thumbv7em-none-eabihf armv7-unknown-linux-gnueabihf && \
rustup component add rustfmt clippy rustup component add rustfmt clippy

View File

@@ -13,29 +13,19 @@ pipeline {
sh 'cargo clippy' sh 'cargo clippy'
} }
} }
stage('Docs') {
steps {
sh 'cargo +nightly doc --all-features'
}
}
stage('Rustfmt') { stage('Rustfmt') {
steps { steps {
sh 'cargo fmt --all --check' sh 'cargo fmt'
} }
} }
stage('Test') { stage('Test') {
steps { steps {
sh 'cargo test --all-features' sh 'cargo test'
} }
} }
stage('Check with all features') { stage('Check') {
steps { steps {
sh 'cargo check --all-features' sh 'cargo check'
}
}
stage('Check with no features') {
steps {
sh 'cargo check --no-default-features'
} }
} }
stage('Check Cross Embedded Bare Metal') { stage('Check Cross Embedded Bare Metal') {

View File

@@ -15,21 +15,6 @@ pub type CrcType = u16;
pub const CRC_CCITT_FALSE: Crc<u16> = Crc::<u16>::new(&CRC_16_IBM_3740); 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>(); 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. /// All PUS versions. Only PUS C is supported by this library.
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -53,7 +38,6 @@ impl TryFrom<u8> for PusVersion {
} }
} }
/// ECSS Packet Type Codes (PTC)s.
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum PacketTypeCodes { pub enum PacketTypeCodes {
@@ -73,7 +57,6 @@ pub enum PacketTypeCodes {
pub type Ptc = PacketTypeCodes; pub type Ptc = PacketTypeCodes;
/// ECSS Packet Field Codes (PFC)s for the unsigned [Ptc].
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum UnsignedPfc { pub enum UnsignedPfc {
@@ -89,7 +72,6 @@ pub enum UnsignedPfc {
ThreeBits = 19, ThreeBits = 19,
} }
/// ECSS Packet Field Codes (PFC)s for the real (floating point) [Ptc].
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum RealPfc { pub enum RealPfc {
@@ -160,8 +142,6 @@ impl From<ByteConversionError> for PusError {
} }
} }
/// 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 { pub trait PusPacket: CcsdsPacket {
const PUS_VERSION: PusVersion = PusVersion::PusC; const PUS_VERSION: PusVersion = PusVersion::PusC;
@@ -336,13 +316,13 @@ impl<TYPE: ToBeBytes> EcssEnumeration for GenericEcssEnumWrapper<TYPE> {
} }
fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<(), ByteConversionError> { fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<(), ByteConversionError> {
if buf.len() < self.byte_width() { if buf.len() < self.byte_width() as usize {
return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch { return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
found: buf.len(), found: buf.len(),
expected: self.byte_width(), expected: self.byte_width() as usize,
})); }));
} }
buf[0..self.byte_width()].copy_from_slice(self.val.to_be_bytes().as_ref()); buf[0..self.byte_width() as usize].copy_from_slice(self.val.to_be_bytes().as_ref());
Ok(()) Ok(())
} }
} }

View File

@@ -9,11 +9,9 @@
//! [CCSDS Blue Book 133.0-B-2](https://public.ccsds.org/Pubs/133x0b2e1.pdf) //! [CCSDS Blue Book 133.0-B-2](https://public.ccsds.org/Pubs/133x0b2e1.pdf)
//! - PUS Telecommand and PUS Telemetry implementation according to the //! - 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/). //! [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/).
//! - CUC (CCSDS Unsegmented Time Code) implementation according to //! - CDS Short Time Code implementation according to
//! [CCSDS 301.0-B-4 3.2](https://public.ccsds.org/Pubs/301x0b4e1.pdf) //! [CCSDS CCSDS 301.0-B-4](https://public.ccsds.org/Pubs/301x0b4e1.pdf)
//! - CDS (CCSDS Day Segmented Time Code) implementation according to //! - Some helper types and functions to support ASCII timecodes ad specified in
//! [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) //! [CCSDS 301.0-B-4 3.5](https://public.ccsds.org/Pubs/301x0b4e1.pdf)
//! //!
//! ## Features //! ## Features
@@ -24,15 +22,12 @@
//! deserializing them with an appropriate `serde` provider like //! deserializing them with an appropriate `serde` provider like
//! [`postcard`](https://github.com/jamesmunns/postcard). //! [`postcard`](https://github.com/jamesmunns/postcard).
//! //!
//! ### Default features //! Default features:
//! //!
//! - [`std`](https://doc.rust-lang.org/std/): Enables functionality relying on the standard library. //! - [`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 //! - [`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). //! like [`alloc::vec::Vec`](https://doc.rust-lang.org/beta/alloc/vec/struct.Vec.html).
//! Enabled by the `std` feature. //! Enabled by the `std` feature.
//!
//! ### Optional features
//!
//! - [`serde`](https://serde.rs/): Adds `serde` support for most types by adding `Serialize` and //! - [`serde`](https://serde.rs/): Adds `serde` support for most types by adding `Serialize` and
//! `Deserialize` `derive`s //! `Deserialize` `derive`s
//! //!
@@ -129,7 +124,6 @@ impl Display for ByteConversionError {
#[cfg(feature = "std")] #[cfg(feature = "std")]
impl Error for ByteConversionError {} impl Error for ByteConversionError {}
/// CCSDS packet type enumeration.
#[derive(Debug, PartialEq, Eq, Copy, Clone)] #[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum PacketType { pub enum PacketType {
@@ -178,8 +172,6 @@ impl TryFrom<u8> for SequenceFlags {
} }
} }
/// 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)] #[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PacketId { pub struct PacketId {
@@ -263,8 +255,6 @@ impl From<u16> for PacketId {
} }
} }
/// 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)] #[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PacketSequenceCtrl { pub struct PacketSequenceCtrl {

View File

@@ -215,19 +215,19 @@ impl PusTcSecondaryHeader {
/// There is no spare bytes support yet. /// There is no spare bytes support yet.
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PusTc<'app_data> { pub struct PusTc<'slice> {
sp_header: SpHeader, sp_header: SpHeader,
pub sec_header: PusTcSecondaryHeader, pub sec_header: PusTcSecondaryHeader,
/// If this is set to false, a manual call to [PusTc::calc_own_crc16] or /// 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. /// [PusTc::update_packet_fields] is necessary for the serialized or cached CRC16 to be valid.
pub calc_crc_on_serialization: bool, pub calc_crc_on_serialization: bool,
#[cfg_attr(feature = "serde", serde(skip))] #[cfg_attr(feature = "serde", serde(skip))]
raw_data: Option<&'app_data [u8]>, raw_data: Option<&'slice [u8]>,
app_data: Option<&'app_data [u8]>, app_data: Option<&'slice [u8]>,
crc16: Option<u16>, crc16: Option<u16>,
} }
impl<'app_data> PusTc<'app_data> { impl<'slice> PusTc<'slice> {
/// Generates a new struct instance. /// Generates a new struct instance.
/// ///
/// # Arguments /// # Arguments
@@ -243,7 +243,7 @@ impl<'app_data> PusTc<'app_data> {
pub fn new( pub fn new(
sp_header: &mut SpHeader, sp_header: &mut SpHeader,
sec_header: PusTcSecondaryHeader, sec_header: PusTcSecondaryHeader,
app_data: Option<&'app_data [u8]>, app_data: Option<&'slice [u8]>,
set_ccsds_len: bool, set_ccsds_len: bool,
) -> Self { ) -> Self {
sp_header.set_packet_type(PacketType::Tc); sp_header.set_packet_type(PacketType::Tc);
@@ -268,7 +268,7 @@ impl<'app_data> PusTc<'app_data> {
sph: &mut SpHeader, sph: &mut SpHeader,
service: u8, service: u8,
subservice: u8, subservice: u8,
app_data: Option<&'app_data [u8]>, app_data: Option<&'slice [u8]>,
set_ccsds_len: bool, set_ccsds_len: bool,
) -> Self { ) -> Self {
Self::new( Self::new(
@@ -279,10 +279,6 @@ impl<'app_data> PusTc<'app_data> {
) )
} }
pub fn sp_header(&self) -> &SpHeader {
&self.sp_header
}
pub fn len_packed(&self) -> usize { pub fn len_packed(&self) -> usize {
let mut length = PUS_TC_MIN_LEN_WITHOUT_APP_DATA; let mut length = PUS_TC_MIN_LEN_WITHOUT_APP_DATA;
if let Some(app_data) = self.app_data { if let Some(app_data) = self.app_data {
@@ -405,7 +401,7 @@ impl<'app_data> PusTc<'app_data> {
/// Create a [PusTc] instance from a raw slice. On success, it returns a tuple containing /// 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. /// the instance and the found byte length of the packet.
pub fn from_bytes(slice: &'app_data [u8]) -> Result<(Self, usize), PusError> { pub fn from_bytes(slice: &'slice [u8]) -> Result<(Self, usize), PusError> {
let raw_data_len = slice.len(); let raw_data_len = slice.len();
if raw_data_len < PUS_TC_MIN_LEN_WITHOUT_APP_DATA { if raw_data_len < PUS_TC_MIN_LEN_WITHOUT_APP_DATA {
return Err(PusError::RawDataTooShort(raw_data_len)); return Err(PusError::RawDataTooShort(raw_data_len));
@@ -435,7 +431,7 @@ impl<'app_data> PusTc<'app_data> {
Ok((pus_tc, total_len)) Ok((pus_tc, total_len))
} }
pub fn raw(&self) -> Option<&'app_data [u8]> { pub fn raw(&self) -> Option<&'slice [u8]> {
self.raw_data self.raw_data
} }
} }

View File

@@ -3,10 +3,8 @@
//! See [chrono::DateTime::format] for a usage example of the generated //! See [chrono::DateTime::format] for a usage example of the generated
//! [chrono::format::DelayedFormat] structs. //! [chrono::format::DelayedFormat] structs.
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
use chrono::{ use chrono::format::{DelayedFormat, StrftimeItems};
format::{DelayedFormat, StrftimeItems}, use chrono::{DateTime, Utc};
DateTime, Utc,
};
/// Tuple of format string and formatted size for time code A. /// Tuple of format string and formatted size for time code A.
/// ///

View File

@@ -6,8 +6,7 @@ use super::*;
use crate::private::Sealed; use crate::private::Sealed;
use core::fmt::Debug; use core::fmt::Debug;
/// Base value for the preamble field for a time field parser to determine the time field type. const CDS_SHORT_P_FIELD: u8 = (CcsdsTimeCodes::Cds as u8) << 4;
pub const P_FIELD_BASE: u8 = (CcsdsTimeCodes::Cds as u8) << 4;
pub const MIN_CDS_FIELD_LEN: usize = 7; pub const MIN_CDS_FIELD_LEN: usize = 7;
/// Generic trait implemented by token structs to specify the length of day field at type /// Generic trait implemented by token structs to specify the length of day field at type
@@ -125,7 +124,7 @@ pub fn precision_from_pfield(pfield: u8) -> SubmillisPrecision {
/// assert_eq!(stamp_deserialized.len_as_bytes(), 7); /// assert_eq!(stamp_deserialized.len_as_bytes(), 7);
/// } /// }
/// ``` /// ```
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TimeProvider<DaysLen: ProvidesDaysLength = DaysLen16Bits> { pub struct TimeProvider<DaysLen: ProvidesDaysLength = DaysLen16Bits> {
pfield: u8, pfield: u8,
@@ -170,8 +169,8 @@ impl ConversionFromNow {
)); ));
} }
SubmillisPrecision::Picoseconds(_) => { SubmillisPrecision::Picoseconds(_) => {
prec = Some(SubmillisPrecision::Picoseconds( prec = Some(SubmillisPrecision::Microseconds(
(now.subsec_nanos() % 10_u32.pow(6)) * 1000, (now.subsec_nanos() * 1000) as u16,
)); ));
} }
_ => (), _ => (),
@@ -339,8 +338,9 @@ impl<ProvidesDaysLen: ProvidesDaysLength> TimeProvider<ProvidesDaysLen> {
unix_seconds: 0, unix_seconds: 0,
submillis_precision: None, submillis_precision: None,
}; };
let unix_days_seconds = ccsds_to_unix_days(ccsds_days.into()) * SECONDS_PER_DAY as i64; let unix_days_seconds =
provider.setup(unix_days_seconds, ms_of_day.into()); ccsds_to_unix_days(ccsds_days.into()) as i64 * SECONDS_PER_DAY as i64;
provider.setup(unix_days_seconds as i64, ms_of_day.into());
Ok(provider) Ok(provider)
} }
@@ -392,7 +392,7 @@ impl<ProvidesDaysLen: ProvidesDaysLength> TimeProvider<ProvidesDaysLen> {
day_seg_len: LengthOfDaySegment, day_seg_len: LengthOfDaySegment,
submillis_prec: Option<SubmillisPrecision>, submillis_prec: Option<SubmillisPrecision>,
) -> u8 { ) -> u8 {
let mut pfield = P_FIELD_BASE | ((day_seg_len as u8) << 2); let mut pfield = CDS_SHORT_P_FIELD | ((day_seg_len as u8) << 2);
if let Some(submillis_prec) = submillis_prec { if let Some(submillis_prec) = submillis_prec {
match submillis_prec { match submillis_prec {
SubmillisPrecision::Microseconds(_) => pfield |= 0b01, SubmillisPrecision::Microseconds(_) => pfield |= 0b01,
@@ -552,7 +552,7 @@ impl<ProvidesDaysLen: ProvidesDaysLength> CcsdsTimeProvider for TimeProvider<Pro
} }
fn date_time(&self) -> Option<DateTime<Utc>> { fn date_time(&self) -> Option<DateTime<Utc>> {
self.calc_date_time(self.ms_of_day % 1000) self.calc_date_time((self.ms_of_day % 1000) as u32)
} }
} }

View File

@@ -6,9 +6,6 @@ use super::*;
use core::fmt::Debug; use core::fmt::Debug;
const MIN_CUC_LEN: usize = 2; 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. /// Maximum length if the preamble field is not extended.
pub const MAX_CUC_LEN_SMALL_PREAMBLE: usize = 8; pub const MAX_CUC_LEN_SMALL_PREAMBLE: usize = 8;
@@ -67,20 +64,10 @@ pub fn fractional_part_from_subsec_ns(
if ns > sec_as_ns { if ns > sec_as_ns {
panic!("passed nanosecond value larger than 1 second"); 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. // 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 // Then divide by that to find out the fractional part. An integer division floors
// fraction, we perform a ceiling division. This is because if we would use the default // which is what we want here.
// flooring division, we would divide by a smaller value, thereby allowing the calculation to let fractional_part = ns / (sec_as_ns / fractional_res_to_div(res) as u64);
// 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)) Some(FractionalPart(res, fractional_part as u32))
} }
@@ -219,7 +206,7 @@ impl TimeProviderCcsdsEpoch {
WidthCounterPair(4, counter), WidthCounterPair(4, counter),
Some(FractionalPart( Some(FractionalPart(
FractionalResolution::SixtyNs, FractionalResolution::SixtyNs,
subsec_fractions, subsec_fractions as u32,
)), )),
) )
} }
@@ -313,7 +300,7 @@ impl TimeProviderCcsdsEpoch {
} }
fn build_p_field(counter_width: u8, fractions_width: Option<FractionalResolution>) -> u8 { fn build_p_field(counter_width: u8, fractions_width: Option<FractionalResolution>) -> u8 {
let mut pfield = P_FIELD_BASE; let mut pfield = (CcsdsTimeCodes::CucCcsdsEpoch as u8) << 4;
if !(1..=4).contains(&counter_width) { if !(1..=4).contains(&counter_width) {
// Okay to panic here, this function is private and all input values should // Okay to panic here, this function is private and all input values should
// have been sanitized // have been sanitized
@@ -444,9 +431,9 @@ impl TimeReader for TimeProviderCcsdsEpoch {
3 => { 3 => {
let mut tmp_buf: [u8; 4] = [0; 4]; let mut tmp_buf: [u8; 4] = [0; 4];
tmp_buf[1..4].copy_from_slice(&buf[current_idx..current_idx + 3]); tmp_buf[1..4].copy_from_slice(&buf[current_idx..current_idx + 3]);
u32::from_be_bytes(tmp_buf) u32::from_be_bytes(tmp_buf) as u32
} }
4 => u32::from_be_bytes(buf[current_idx..current_idx + 4].try_into().unwrap()), 4 => u32::from_be_bytes(buf[current_idx..current_idx + 4].try_into().unwrap()) as u32,
_ => panic!("unreachable match arm"), _ => panic!("unreachable match arm"),
}; };
current_idx += cntr_len as usize; current_idx += cntr_len as usize;
@@ -471,7 +458,7 @@ impl TimeReader for TimeProviderCcsdsEpoch {
tmp_buf[1..4].copy_from_slice(&buf[current_idx..current_idx + 3]); tmp_buf[1..4].copy_from_slice(&buf[current_idx..current_idx + 3]);
fractions = Some(FractionalPart( fractions = Some(FractionalPart(
fractions_len.try_into().unwrap(), fractions_len.try_into().unwrap(),
u32::from_be_bytes(tmp_buf), u32::from_be_bytes(tmp_buf) as u32,
)) ))
} }
_ => panic!("unreachable match arm"), _ => panic!("unreachable match arm"),
@@ -880,39 +867,29 @@ mod tests {
#[test] #[test]
fn fractional_part_formula() { fn fractional_part_formula() {
let fractional_part = let fractional_part =
fractional_part_from_subsec_ns(FractionalResolution::FourMs, 7843138).unwrap(); 7843137 / (10_u64.pow(9) / fractional_res_to_div(FractionalResolution::FourMs) as u64);
assert_eq!(fractional_part.1, 2); assert_eq!(fractional_part, 2);
} }
#[test] #[test]
fn fractional_part_formula_2() { fn fractional_part_formula_2() {
let fractional_part = let fractional_part =
fractional_part_from_subsec_ns(FractionalResolution::FourMs, 12000000).unwrap(); 12000000 / (10_u64.pow(9) / fractional_res_to_div(FractionalResolution::FourMs) as u64);
assert_eq!(fractional_part.1, 3); assert_eq!(fractional_part, 3);
} }
#[test] #[test]
fn fractional_part_formula_3() { fn fractional_part_formula_3() {
let one_fraction_with_width_two_in_ns = let one_fraction_with_width_two_in_ns = 10_u64.pow(9) / (2_u32.pow(8 * 2) - 1) as u64;
10_u64.pow(9) as f64 / (2_u32.pow(8 * 2) - 1) as f64; assert_eq!(one_fraction_with_width_two_in_ns, 15259);
assert_eq!(one_fraction_with_width_two_in_ns.ceil(), 15260.0); let hundred_fractions_and_some = 100 * one_fraction_with_width_two_in_ns + 7000;
let hundred_fractions_and_some = let fractional_part = hundred_fractions_and_some
(100.0 * one_fraction_with_width_two_in_ns).floor() as u64 + 7000; / (10_u64.pow(9) / fractional_res_to_div(FractionalResolution::FifteenUs) as u64);
let fractional_part = fractional_part_from_subsec_ns( assert_eq!(fractional_part, 100);
FractionalResolution::FifteenUs, let hundred_and_one_fractions = 101 * one_fraction_with_width_two_in_ns;
hundred_fractions_and_some, let fractional_part = hundred_and_one_fractions
) / (10_u64.pow(9) / fractional_res_to_div(FractionalResolution::FifteenUs) as u64);
.unwrap(); assert_eq!(fractional_part, 101);
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] #[test]
@@ -937,14 +914,4 @@ mod tests {
let res = stamp.update_from_now(); let res = stamp.update_from_now();
assert!(res.is_ok()); 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);
}
} }

View File

@@ -108,18 +108,18 @@ pub mod zc {
#[derive(PartialEq, Eq, Copy, Clone, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PusTmSecondaryHeader<'stamp> { pub struct PusTmSecondaryHeader<'slice> {
pus_version: PusVersion, pus_version: PusVersion,
pub sc_time_ref_status: u8, pub sc_time_ref_status: u8,
pub service: u8, pub service: u8,
pub subservice: u8, pub subservice: u8,
pub msg_counter: u16, pub msg_counter: u16,
pub dest_id: u16, pub dest_id: u16,
pub time_stamp: &'stamp [u8], pub time_stamp: &'slice [u8],
} }
impl<'stamp> PusTmSecondaryHeader<'stamp> { impl<'slice> PusTmSecondaryHeader<'slice> {
pub fn new_simple(service: u8, subservice: u8, time_stamp: &'stamp [u8]) -> Self { pub fn new_simple(service: u8, subservice: u8, time_stamp: &'slice [u8]) -> Self {
PusTmSecondaryHeader { PusTmSecondaryHeader {
pus_version: PusVersion::PusC, pus_version: PusVersion::PusC,
sc_time_ref_status: 0, sc_time_ref_status: 0,
@@ -136,7 +136,7 @@ impl<'stamp> PusTmSecondaryHeader<'stamp> {
subservice: u8, subservice: u8,
msg_counter: u16, msg_counter: u16,
dest_id: u16, dest_id: u16,
time_stamp: &'stamp [u8], time_stamp: &'slice [u8],
) -> Self { ) -> Self {
PusTmSecondaryHeader { PusTmSecondaryHeader {
pus_version: PusVersion::PusC, pus_version: PusVersion::PusC,
@@ -201,26 +201,21 @@ impl<'slice> TryFrom<zc::PusTmSecHeader<'slice>> for PusTmSecondaryHeader<'slice
/// provider like [postcard](https://docs.rs/postcard/latest/postcard/). /// provider like [postcard](https://docs.rs/postcard/latest/postcard/).
/// ///
/// There is no spare bytes support yet. /// There is no spare bytes support yet.
///
/// # 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)] #[derive(PartialEq, Eq, Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PusTm<'src_data> { pub struct PusTm<'slice> {
pub sp_header: SpHeader, pub sp_header: SpHeader,
pub sec_header: PusTmSecondaryHeader<'src_data>, pub sec_header: PusTmSecondaryHeader<'slice>,
/// If this is set to false, a manual call to [PusTm::calc_own_crc16] or /// 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. /// [PusTm::update_packet_fields] is necessary for the serialized or cached CRC16 to be valid.
pub calc_crc_on_serialization: bool, pub calc_crc_on_serialization: bool,
#[cfg_attr(feature = "serde", serde(skip))] #[cfg_attr(feature = "serde", serde(skip))]
raw_data: Option<&'src_data [u8]>, raw_data: Option<&'slice [u8]>,
source_data: Option<&'src_data [u8]>, source_data: Option<&'slice [u8]>,
crc16: Option<u16>, crc16: Option<u16>,
} }
impl<'src_data> PusTm<'src_data> { impl<'slice> PusTm<'slice> {
/// Generates a new struct instance. /// Generates a new struct instance.
/// ///
/// # Arguments /// # Arguments
@@ -235,8 +230,8 @@ impl<'src_data> PusTm<'src_data> {
/// the correct value to this field manually /// the correct value to this field manually
pub fn new( pub fn new(
sp_header: &mut SpHeader, sp_header: &mut SpHeader,
sec_header: PusTmSecondaryHeader<'src_data>, sec_header: PusTmSecondaryHeader<'slice>,
source_data: Option<&'src_data [u8]>, source_data: Option<&'slice [u8]>,
set_ccsds_len: bool, set_ccsds_len: bool,
) -> Self { ) -> Self {
sp_header.set_packet_type(PacketType::Tm); sp_header.set_packet_type(PacketType::Tm);
@@ -264,11 +259,11 @@ impl<'src_data> PusTm<'src_data> {
length length
} }
pub fn time_stamp(&self) -> &'src_data [u8] { pub fn time_stamp(&self) -> &'slice [u8] {
self.sec_header.time_stamp self.sec_header.time_stamp
} }
pub fn source_data(&self) -> Option<&'src_data [u8]> { pub fn source_data(&self) -> Option<&'slice [u8]> {
self.source_data self.source_data
} }
@@ -395,7 +390,7 @@ impl<'src_data> PusTm<'src_data> {
/// the instance and the found byte length of the packet. The timestamp length needs to be /// the instance and the found byte length of the packet. The timestamp length needs to be
/// known beforehand. /// known beforehand.
pub fn from_bytes( pub fn from_bytes(
slice: &'src_data [u8], slice: &'slice [u8],
timestamp_len: usize, timestamp_len: usize,
) -> Result<(Self, usize), PusError> { ) -> Result<(Self, usize), PusError> {
let raw_data_len = slice.len(); let raw_data_len = slice.len();