diff --git a/CHANGELOG.md b/CHANGELOG.md index 92f6090..eccc14c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/). # [unreleased] +## Added + +- `num_enum` dependency to avoid boilerplate code for primtive to enum conversions, for example + for the PUS subservices. +- `ecss.event` module containing a `Subservice` enum. +- `ecss.verification` module containing a `Subservice` enum. +- `ecss.scheduling` module containing a `Subservice` enum and some other helper enumerations. + +## Changed + +- Added missing Service IDs to `ecss.PusServiceId` and marked in `#[non_exhaustive]`. + # [v0.5.2] 2023-01-26 ## Added diff --git a/Cargo.toml b/Cargo.toml index 035424c..8e9e715 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ categories = ["aerospace", "aerospace::space-protocols", "no-std", "hardware-sup zerocopy = "0.6" crc = "3" delegate = ">=0.8, <0.10" +num_enum = "0.5" [dependencies.serde] version = "1" diff --git a/src/ecss/event.rs b/src/ecss/event.rs new file mode 100644 index 0000000..622ab5b --- /dev/null +++ b/src/ecss/event.rs @@ -0,0 +1,43 @@ +//! PUS Service 5 Events +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; +use num_enum::{IntoPrimitive, TryFromPrimitive}; + +#[derive(Debug, Eq, PartialEq, Copy, Clone, IntoPrimitive, TryFromPrimitive)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[repr(u8)] +pub enum Subservice { + TmInfoReport = 1, + TmLowSeverityReport = 2, + TmMediumSeverityReport = 3, + TmHighSeverityReport = 4, + TcEnableEventGeneration = 5, + TcDisableEventGeneration = 6, + TcReportDisabledList = 7, + TmDisabledEventsReport = 8, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_conv_into_u8() { + let subservice: u8 = Subservice::TmLowSeverityReport.into(); + assert_eq!(subservice, 2); + } + + #[test] + fn test_conv_from_u8() { + let subservice: Subservice = 2.try_into().unwrap(); + assert_eq!(subservice, Subservice::TmLowSeverityReport); + } + + #[test] + fn test_conv_fails() { + let conversion = Subservice::try_from(9); + assert!(conversion.is_err()); + let err = conversion.unwrap_err(); + assert_eq!(err.number, 9); + } +} diff --git a/src/ecss/mod.rs b/src/ecss/mod.rs index dbde721..7ad259c 100644 --- a/src/ecss/mod.rs +++ b/src/ecss/mod.rs @@ -7,12 +7,15 @@ use crate::{ByteConversionError, CcsdsPacket, SizeMissmatch}; use core::fmt::{Debug, Display, Formatter}; use core::mem::size_of; use crc::{Crc, CRC_16_IBM_3740}; +use num_enum::{IntoPrimitive, TryFromPrimitive}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; #[cfg(feature = "std")] use std::error::Error; pub mod scheduling; +pub mod event; +pub mod verification; pub type CrcType = u16; @@ -20,21 +23,51 @@ pub type CrcType = u16; pub const CRC_CCITT_FALSE: Crc = Crc::::new(&CRC_16_IBM_3740); pub const CCSDS_HEADER_LEN: usize = size_of::(); -#[derive(Debug, Copy, Clone, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[repr(u8)] +#[non_exhaustive] pub enum PusServiceId { /// Service 1 Verification = 1, + /// Service 2 + DeviceAccess = 2, /// Service 3 Housekeeping = 3, + /// Service 4 + ParameterStatistics = 4, /// Service 5 Event = 5, + /// Service 6 + MemoryManagement = 6, /// Service 8 Action = 8, + /// Service 9 + TimeManagement = 9, /// Service 11 Scheduling = 11, + /// Service 12 + OnBoardMonitoring = 12, + /// Service 13 + LargePacketTransfer = 13, + /// Service 14 + RealTimeForwardingControl = 14, + /// Service 15 + StorageAndRetrival = 15, /// Service 17 Test = 17, + /// Service 18 + OpsAndProcedures = 18, + /// Service 19 + EventAction = 19, + /// Service 20 + Parameter = 20, + /// Service 21 + RequestSequencing = 21, + /// Service 22 + PositionBasedScheduling = 22, + /// Service 23 + FileManagement = 23 } /// All PUS versions. Only PUS C is supported by this library. diff --git a/src/ecss/scheduling.rs b/src/ecss/scheduling.rs index 09cb4f6..e9f3e1b 100644 --- a/src/ecss/scheduling.rs +++ b/src/ecss/scheduling.rs @@ -1,9 +1,11 @@ //! PUS Service 11 Scheduling #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; +use num_enum::{IntoPrimitive, TryFromPrimitive}; -#[derive(Debug, PartialEq, Eq, Copy, Clone)] +#[derive(Debug, PartialEq, Eq, Copy, Clone, IntoPrimitive, TryFromPrimitive)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[repr(u8)] pub enum Subservice { // Core subservices TcEnableScheduling = 1, @@ -73,7 +75,7 @@ pub enum TimeWindowType { #[cfg(test)] mod tests { - use crate::ecss::scheduling::SchedStatus; + use super::*; #[test] fn test_bool_conv_0() { @@ -88,4 +90,16 @@ mod tests { let status: SchedStatus = enabled.into(); assert_eq!(status, SchedStatus::Disabled) } + + #[test] + fn test_conv_into_u8() { + let subservice: u8 = Subservice::TcCreateScheduleGroup.into(); + assert_eq!(subservice, 22); + } + + #[test] + fn test_conv_from_u8() { + let subservice: Subservice = 22u8.try_into().unwrap(); + assert_eq!(subservice, Subservice::TcCreateScheduleGroup); + } } diff --git a/src/ecss/verification.rs b/src/ecss/verification.rs new file mode 100644 index 0000000..9c0b709 --- /dev/null +++ b/src/ecss/verification.rs @@ -0,0 +1,35 @@ +//! PUS Service 1 Verification +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; +use num_enum::{IntoPrimitive, TryFromPrimitive}; + +#[derive(Debug, Eq, PartialEq, Copy, Clone, IntoPrimitive, TryFromPrimitive)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[repr(u8)] +pub enum Subservice { + TmAcceptanceSuccess = 1, + TmAcceptanceFailure = 2, + TmStartSuccess = 3, + TmStartFailure = 4, + TmStepSuccess = 5, + TmStepFailure = 6, + TmCompletionSuccess = 7, + TmCompletionFailure = 8, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_conv_into_u8() { + let subservice: u8 = Subservice::TmCompletionSuccess.into(); + assert_eq!(subservice, 7); + } + + #[test] + fn test_conv_from_u8() { + let subservice: Subservice = 7.try_into().unwrap(); + assert_eq!(subservice, Subservice::TmCompletionSuccess); + } +} \ No newline at end of file