41 Commits

Author SHA1 Message Date
d972dd5223 cargo fmt
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2023-01-10 16:59:13 +01:00
481de83fdb move lifetime docs further up
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2023-01-10 16:20:50 +01:00
09b305f529 better names for lifetimes
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2023-01-10 16:19:21 +01:00
78c5787e07 add some more basic docs
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2023-01-10 16:12:20 +01:00
e9e33b0335 release this tomorrow
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2023-01-10 00:00:10 +01:00
455be77f4a more docs 2023-01-09 23:59:55 +01:00
c748657499 allow minor release bump for serde and crc dependency
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2023-01-09 23:49:17 +01:00
2e90cba5bd put some ASCII includes behind alloc feature
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2023-01-09 23:47:47 +01:00
f290d2a54e re-remove Default impl for CDS time provider
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2023-01-09 23:46:31 +01:00
da695e4705 v0.4.0 preparation
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2023-01-09 11:21:07 +01:00
5c222735d4 Merge branch 'main' of https://egit.irs.uni-stuttgart.de/rust/spacepackets
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2023-01-09 11:19:16 +01:00
1432c298b3 feature list update 2023-01-09 11:19:10 +01:00
1b45082ace Merge pull request 'Add CUC impl' (#4) from add_cuc_time_impl into main
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Reviewed-on: #4
Reviewed-by: Paul Nehlich <nehlichp@irs.uni-stuttgart.de>
2023-01-09 11:14:40 +01:00
4c20158dcc Merge branch 'main' into add_cuc_time_impl
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Rust/spacepackets/pipeline/pr-main This commit looks good
2023-01-09 11:12:47 +01:00
c879181093 update changelog
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2023-01-09 11:12:07 +01:00
6c88e94742 Merge remote-tracking branch 'origin/main' into add_cuc_time_impl
Some checks failed
Rust/spacepackets/pipeline/head This commit looks good
Rust/spacepackets/pipeline/pr-main There was a failure building this commit
2023-01-09 11:10:44 +01:00
3fb2fbd20c Merge pull request 'Improve CDS timecode implementation' (#3) from improve_cds_short_impl into main
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Reviewed-on: #3
2023-01-09 11:08:11 +01:00
ec8a2e1d24 rename pfield preamble constant, add for CUC
Some checks failed
Rust/spacepackets/pipeline/head This commit looks good
Rust/spacepackets/pipeline/pr-main There was a failure building this commit
2023-01-09 11:07:43 +01:00
192e2f2c76 make pfield public
All checks were successful
Rust/spacepackets/pipeline/pr-main This commit looks good
Rust/spacepackets/pipeline/head This commit looks good
2023-01-09 11:06:51 +01:00
5df221759f conversion from now bugfix
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Rust/spacepackets/pipeline/pr-main This commit looks good
2022-12-22 23:55:05 +01:00
f137bd2549 improve tests
All checks were successful
Rust/spacepackets/pipeline/pr-main This commit looks good
Rust/spacepackets/pipeline/head This commit looks good
2022-12-22 23:45:15 +01:00
630bffec51 Merge branch 'main' into improve_cds_short_impl
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Rust/spacepackets/pipeline/pr-main This commit looks good
2022-12-22 23:12:35 +01:00
a14ae37cac Merge pull request 'added sp header getter function' (#6) from sp_header_getter into main
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Reviewed-on: #6
2022-12-22 09:06:33 +01:00
2758699601 added sp header getter function
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2022-12-21 22:09:45 +01:00
b07cec28ea update changelog
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Rust/spacepackets/pipeline/pr-main This commit looks good
2022-12-21 10:14:58 +01:00
51963d0f72 remove default impl for cds TimeProvider 2022-12-21 10:14:13 +01:00
83e2cad753 cargo fmt
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Rust/spacepackets/pipeline/pr-main This commit looks good
2022-12-21 09:47:09 +01:00
472bfa9964 add floating point division code
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Rust/spacepackets/pipeline/pr-main This commit looks good
2022-12-21 01:17:36 +01:00
14fa1bad92 add TODO
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Rust/spacepackets/pipeline/pr-main This commit looks good
2022-12-21 01:03:26 +01:00
3828a98c76 important bugfix
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Rust/spacepackets/pipeline/pr-main This commit looks good
2022-12-21 01:01:05 +01:00
f641248ac2 add PR link
Some checks failed
Rust/spacepackets/pipeline/head There was a failure building this commit
Rust/spacepackets/pipeline/pr-main This commit looks good
2022-12-20 16:28:10 +01:00
884de647ad add PR link
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Rust/spacepackets/pipeline/pr-main This commit looks good
2022-12-20 16:27:23 +01:00
0d8074c6b9 clippy fixes
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Rust/spacepackets/pipeline/pr-main This commit looks good
2022-12-20 16:26:04 +01:00
6798e3a6f5 Merge remote-tracking branch 'origin/main' into improve_cds_short_impl
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Rust/spacepackets/pipeline/pr-main This commit looks good
2022-12-20 16:23:24 +01:00
7e763fe055 Merge remote-tracking branch 'origin/main' into add_cuc_time_impl
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Rust/spacepackets/pipeline/pr-main This commit looks good
2022-12-20 16:22:23 +01:00
4410ee7eec bump changelog
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2022-12-20 16:21:30 +01:00
2895d7645a Merge pull request 'PTC and PFC extensions' (#5) from ptc_pfc_extension into main
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Reviewed-on: #5
2022-12-20 16:19:21 +01:00
692d12e5a5 Merge branch 'main' into ptc_pfc_extension
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Rust/spacepackets/pipeline/pr-main This commit looks good
2022-12-20 16:15:46 +01:00
9e57ce3872 cargo fmt
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2022-12-19 17:02:19 +01:00
f964342556 removed unnecessary casts
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2022-12-18 23:54:13 +01:00
5a878ef6a3 Merge remote-tracking branch 'origin/main' into improve_cds_short_impl
Some checks failed
Rust/spacepackets/pipeline/head This commit looks good
Rust/spacepackets/pipeline/pr-main There was a failure building this commit
2022-12-12 11:56:08 +01:00
10 changed files with 129 additions and 53 deletions

View File

@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
# [unreleased]
# [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.
@ -19,12 +25,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
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.

View File

@ -1,6 +1,6 @@
[package]
name = "spacepackets"
version = "0.3.1"
version = "0.4.0"
edition = "2021"
rust-version = "1.60"
authors = ["Robin Mueller <muellerr@irs.uni-stuttgart.de>"]
@ -14,11 +14,11 @@ 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"]

View File

@ -15,7 +15,9 @@ 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
- 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)

View File

@ -18,10 +18,15 @@ 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,
}
@ -48,6 +53,7 @@ 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 {
@ -67,6 +73,7 @@ pub enum PacketTypeCodes {
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 {
@ -82,6 +89,7 @@ pub enum UnsignedPfc {
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 {
@ -152,6 +160,8 @@ 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 {
const PUS_VERSION: PusVersion = PusVersion::PusC;

View File

@ -9,9 +9,11 @@
//! [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)
//! - Some helper types and functions to support ASCII timecodes ad specified in
//! - 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
@ -127,6 +129,7 @@ impl Display for ByteConversionError {
#[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 {
@ -175,6 +178,8 @@ 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)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PacketId {
@ -258,6 +263,8 @@ 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)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PacketSequenceCtrl {

View File

@ -215,19 +215,19 @@ impl PusTcSecondaryHeader {
/// There is no spare bytes support yet.
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PusTc<'slice> {
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,
#[cfg_attr(feature = "serde", serde(skip))]
raw_data: Option<&'slice [u8]>,
app_data: Option<&'slice [u8]>,
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
@ -243,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);
@ -268,7 +268,7 @@ impl<'slice> PusTc<'slice> {
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(
@ -279,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 {
@ -401,7 +405,7 @@ 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> {
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));
@ -431,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
}
}

View File

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

View File

@ -6,7 +6,8 @@ use super::*;
use crate::private::Sealed;
use core::fmt::Debug;
const CDS_SHORT_P_FIELD: u8 = (CcsdsTimeCodes::Cds as u8) << 4;
/// 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
@ -124,7 +125,7 @@ pub fn precision_from_pfield(pfield: u8) -> SubmillisPrecision {
/// assert_eq!(stamp_deserialized.len_as_bytes(), 7);
/// }
/// ```
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TimeProvider<DaysLen: ProvidesDaysLength = DaysLen16Bits> {
pfield: u8,
@ -391,7 +392,7 @@ impl<ProvidesDaysLen: ProvidesDaysLength> TimeProvider<ProvidesDaysLen> {
day_seg_len: LengthOfDaySegment,
submillis_prec: Option<SubmillisPrecision>,
) -> u8 {
let mut pfield = CDS_SHORT_P_FIELD | ((day_seg_len as u8) << 2);
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,

View File

@ -6,6 +6,9 @@ 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;
@ -64,10 +67,20 @@ pub fn fractional_part_from_subsec_ns(
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. An integer division floors
// which is what we want here.
let fractional_part = ns / (sec_as_ns / fractional_res_to_div(res) as u64);
// 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))
}
@ -300,7 +313,7 @@ impl TimeProviderCcsdsEpoch {
}
fn build_p_field(counter_width: u8, fractions_width: Option<FractionalResolution>) -> u8 {
let mut pfield = (CcsdsTimeCodes::CucCcsdsEpoch as u8) << 4;
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
@ -867,29 +880,39 @@ mod tests {
#[test]
fn fractional_part_formula() {
let fractional_part =
7843137 / (10_u64.pow(9) / fractional_res_to_div(FractionalResolution::FourMs) as u64);
assert_eq!(fractional_part, 2);
fractional_part_from_subsec_ns(FractionalResolution::FourMs, 7843138).unwrap();
assert_eq!(fractional_part.1, 2);
}
#[test]
fn fractional_part_formula_2() {
let fractional_part =
12000000 / (10_u64.pow(9) / fractional_res_to_div(FractionalResolution::FourMs) as u64);
assert_eq!(fractional_part, 3);
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) / (2_u32.pow(8 * 2) - 1) as u64;
assert_eq!(one_fraction_with_width_two_in_ns, 15259);
let hundred_fractions_and_some = 100 * one_fraction_with_width_two_in_ns + 7000;
let fractional_part = hundred_fractions_and_some
/ (10_u64.pow(9) / fractional_res_to_div(FractionalResolution::FifteenUs) as u64);
assert_eq!(fractional_part, 100);
let hundred_and_one_fractions = 101 * one_fraction_with_width_two_in_ns;
let fractional_part = hundred_and_one_fractions
/ (10_u64.pow(9) / fractional_res_to_div(FractionalResolution::FifteenUs) as u64);
assert_eq!(fractional_part, 101);
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]
@ -914,4 +937,14 @@ mod tests {
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);
}
}

View File

@ -108,18 +108,18 @@ pub mod zc {
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PusTmSecondaryHeader<'slice> {
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,
@ -136,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,
@ -201,21 +201,26 @@ impl<'slice> TryFrom<zc::PusTmSecHeader<'slice>> for PusTmSecondaryHeader<'slice
/// provider like [postcard](https://docs.rs/postcard/latest/postcard/).
///
/// 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)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PusTm<'slice> {
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,
#[cfg_attr(feature = "serde", serde(skip))]
raw_data: Option<&'slice [u8]>,
source_data: Option<&'slice [u8]>,
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
@ -230,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);
@ -259,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
}
@ -390,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();