diff --git a/satrs-core/Cargo.toml b/satrs-core/Cargo.toml index da11060..3781142 100644 --- a/satrs-core/Cargo.toml +++ b/satrs-core/Cargo.toml @@ -36,7 +36,7 @@ optional = true [dependencies.spacepackets] path = "../spacepackets" -features = ["serde"] +default-features = false [dev-dependencies] serde = "1.0" @@ -49,9 +49,9 @@ version = "1.0" [features] default = ["std"] -std = ["downcast-rs/std", "alloc", "bus", "postcard/use-std", "crossbeam-channel/std", "serde/std"] -alloc = ["serde/alloc"] -serde = ["dep:serde"] +std = ["downcast-rs/std", "alloc", "bus", "postcard/use-std", "crossbeam-channel/std", "serde/std", "spacepackets/std"] +alloc = ["serde/alloc", "spacepackets/alloc"] +serde = ["dep:serde", "spacepackets/serde"] heapless = [] doc-images = [] diff --git a/satrs-core/src/event_man.rs b/satrs-core/src/event_man.rs index 530df51..794ce28 100644 --- a/satrs-core/src/event_man.rs +++ b/satrs-core/src/event_man.rs @@ -56,8 +56,11 @@ doc = ::embed_doc_image::embed_image!("event_man_arch", "images/event_man_arch.p //! different threads. use crate::events::{EventU16, EventU32, GenericEvent, LargestEventRaw, LargestGroupIdRaw}; use crate::params::{Params, ParamsHeapless}; +#[cfg(feature = "alloc")] use alloc::boxed::Box; +#[cfg(feature = "alloc")] use alloc::vec; +#[cfg(feature = "alloc")] use alloc::vec::Vec; use core::slice::Iter; use hashbrown::HashMap; diff --git a/satrs-core/src/events.rs b/satrs-core/src/events.rs index ae0452c..d5763a9 100644 --- a/satrs-core/src/events.rs +++ b/satrs-core/src/events.rs @@ -29,10 +29,10 @@ //! ``` use core::fmt::Debug; use core::hash::Hash; +use core::marker::PhantomData; use delegate::delegate; use spacepackets::ecss::{EcssEnumeration, ToBeBytes}; use spacepackets::{ByteConversionError, SizeMissmatch}; -use std::marker::PhantomData; /// Using a type definition allows to change this to u64 in the future more easily pub type LargestEventRaw = u32; diff --git a/satrs-core/src/objects.rs b/satrs-core/src/objects.rs index 266db7a..1b6f4ad 100644 --- a/satrs-core/src/objects.rs +++ b/satrs-core/src/objects.rs @@ -51,10 +51,10 @@ //! assert_eq!(example_obj.id, obj_id); //! assert_eq!(example_obj.dummy, 42); //! ``` +#[cfg(feature = "alloc")] use alloc::boxed::Box; -#[cfg(not(feature = "std"))] -use alloc::vec::Vec; use downcast_rs::Downcast; +#[cfg(feature = "alloc")] use hashbrown::HashMap; #[cfg(feature = "std")] use std::error::Error; diff --git a/satrs-core/src/params.rs b/satrs-core/src/params.rs index f1b6c6a..bd616bd 100644 --- a/satrs-core/src/params.rs +++ b/satrs-core/src/params.rs @@ -43,21 +43,23 @@ //! This includes the [ParamsHeapless] enumeration for contained values which do not require heap //! allocation, and the [Params] which enumerates [ParamsHeapless] and some additional types which //! require [alloc] support but allow for more flexbility. +#[cfg(feature = "alloc")] use crate::pool::StoreAddr; #[cfg(feature = "alloc")] -use alloc::string::String; -#[cfg(feature = "alloc")] -use alloc::string::ToString; +use alloc::string::{String, ToString}; #[cfg(feature = "alloc")] use alloc::vec::Vec; use core::fmt::Debug; use core::mem::size_of; use paste::paste; -pub use spacepackets::ecss::ToBeBytes; use spacepackets::ecss::{EcssEnumU16, EcssEnumU32, EcssEnumU64, EcssEnumU8, EcssEnumeration}; use spacepackets::ByteConversionError; use spacepackets::SizeMissmatch; +#[cfg(feature = "alloc")] +pub use alloc_mod::*; +pub use spacepackets::ecss::ToBeBytes; + /// Generic trait which is used for objects which can be converted into a raw network (big) endian /// byte format. pub trait WritableToBeBytes { @@ -481,13 +483,6 @@ impl WritableToBeBytes for EcssEnumParams { pub enum ParamsHeapless { Raw(ParamsRaw), EcssEnum(EcssEnumParams), - Store(StoreAddr), -} - -impl From for ParamsHeapless { - fn from(x: StoreAddr) -> Self { - Self::Store(x) - } } macro_rules! from_conversions_for_raw { @@ -534,46 +529,56 @@ from_conversions_for_raw!( (f64, Self::F64), ); -/// Generic enumeration for additional parameters, including parameters which rely on heap -/// allocations. #[cfg(feature = "alloc")] -#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] -#[derive(Debug, Clone)] -pub enum Params { - Heapless(ParamsHeapless), - Vec(Vec), - String(String), -} - -impl From for Params { - fn from(x: ParamsHeapless) -> Self { - Self::Heapless(x) +mod alloc_mod { + use super::*; + /// Generic enumeration for additional parameters, including parameters which rely on heap + /// allocations. + #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] + #[derive(Debug, Clone)] + pub enum Params { + Heapless(ParamsHeapless), + Store(StoreAddr), + Vec(Vec), + String(String), } -} -impl From> for Params { - fn from(val: Vec) -> Self { - Self::Vec(val) + impl From for Params { + fn from(x: StoreAddr) -> Self { + Self::Store(x) + } } -} -/// Converts a byte slice into the [Params::Vec] variant -impl From<&[u8]> for Params { - fn from(val: &[u8]) -> Self { - Self::Vec(val.to_vec()) + impl From for Params { + fn from(x: ParamsHeapless) -> Self { + Self::Heapless(x) + } } -} -impl From for Params { - fn from(val: String) -> Self { - Self::String(val) + impl From> for Params { + fn from(val: Vec) -> Self { + Self::Vec(val) + } } -} -/// Converts a string slice into the [Params::String] variant -impl From<&str> for Params { - fn from(val: &str) -> Self { - Self::String(val.to_string()) + /// Converts a byte slice into the [Params::Vec] variant + impl From<&[u8]> for Params { + fn from(val: &[u8]) -> Self { + Self::Vec(val.to_vec()) + } + } + + impl From for Params { + fn from(val: String) -> Self { + Self::String(val) + } + } + + /// Converts a string slice into the [Params::String] variant + impl From<&str> for Params { + fn from(val: &str) -> Self { + Self::String(val.to_string()) + } } } diff --git a/satrs-core/src/pus/event_man.rs b/satrs-core/src/pus/event_man.rs index d747222..380cbc8 100644 --- a/satrs-core/src/pus/event_man.rs +++ b/satrs-core/src/pus/event_man.rs @@ -1,4 +1,7 @@ -use crate::events::{EventU32, EventU32TypedSev, GenericEvent, HasSeverity, Severity}; +use crate::events::{EventU32, GenericEvent, Severity}; +#[cfg(feature = "alloc")] +use crate::events::{EventU32TypedSev, HasSeverity}; +#[cfg(feature = "alloc")] use alloc::boxed::Box; use core::hash::Hash; use hashbrown::HashSet; @@ -6,7 +9,12 @@ use hashbrown::HashSet; #[cfg(feature = "alloc")] pub use crate::pus::event::EventReporter; use crate::pus::verification::{TcStateStarted, VerificationToken}; -use crate::pus::{EcssTmError, EcssTmSender}; +use crate::pus::EcssTmError; +#[cfg(feature = "alloc")] +use crate::pus::EcssTmSender; +#[cfg(feature = "alloc")] +#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] +pub use alloc_mod::*; #[cfg(feature = "heapless")] #[cfg_attr(doc_cfg, doc(cfg(feature = "heapless")))] pub use heapless_mod::*; @@ -133,93 +141,97 @@ impl From> for EventManError { } } -pub struct PusEventDispatcher { - reporter: EventReporter, - backend: Box>, -} +#[cfg(feature = "alloc")] +pub mod alloc_mod { + use super::*; -/// Safety: All contained fields are send as well. -unsafe impl Send for PusEventDispatcher {} - -impl PusEventDispatcher { - pub fn new( + pub struct PusEventDispatcher { reporter: EventReporter, backend: Box>, - ) -> Self { - Self { reporter, backend } - } -} - -impl PusEventDispatcher { - pub fn enable_tm_for_event(&mut self, event: &Event) -> Result { - self.backend.enable_event_reporting(event) } - pub fn disable_tm_for_event(&mut self, event: &Event) -> Result { - self.backend.disable_event_reporting(event) - } + /// Safety: All contained fields are send as well. + unsafe impl Send for PusEventDispatcher {} - pub fn generate_pus_event_tm_generic( - &mut self, - sender: &mut (impl EcssTmSender + ?Sized), - time_stamp: &[u8], - event: Event, - aux_data: Option<&[u8]>, - ) -> Result> { - if !self.backend.event_enabled(&event) { - return Ok(false); + impl PusEventDispatcher { + pub fn new( + reporter: EventReporter, + backend: Box>, + ) -> Self { + Self { reporter, backend } } - match event.severity() { - Severity::INFO => self - .reporter - .event_info(sender, time_stamp, event, aux_data) - .map(|_| true) - .map_err(|e| e.into()), - Severity::LOW => self - .reporter - .event_low_severity(sender, time_stamp, event, aux_data) - .map(|_| true) - .map_err(|e| e.into()), - Severity::MEDIUM => self - .reporter - .event_medium_severity(sender, time_stamp, event, aux_data) - .map(|_| true) - .map_err(|e| e.into()), - Severity::HIGH => self - .reporter - .event_high_severity(sender, time_stamp, event, aux_data) - .map(|_| true) - .map_err(|e| e.into()), + } + + impl PusEventDispatcher { + pub fn enable_tm_for_event(&mut self, event: &Event) -> Result { + self.backend.enable_event_reporting(event) + } + + pub fn disable_tm_for_event(&mut self, event: &Event) -> Result { + self.backend.disable_event_reporting(event) + } + + pub fn generate_pus_event_tm_generic( + &mut self, + sender: &mut (impl EcssTmSender + ?Sized), + time_stamp: &[u8], + event: Event, + aux_data: Option<&[u8]>, + ) -> Result> { + if !self.backend.event_enabled(&event) { + return Ok(false); + } + match event.severity() { + Severity::INFO => self + .reporter + .event_info(sender, time_stamp, event, aux_data) + .map(|_| true) + .map_err(|e| e.into()), + Severity::LOW => self + .reporter + .event_low_severity(sender, time_stamp, event, aux_data) + .map(|_| true) + .map_err(|e| e.into()), + Severity::MEDIUM => self + .reporter + .event_medium_severity(sender, time_stamp, event, aux_data) + .map(|_| true) + .map_err(|e| e.into()), + Severity::HIGH => self + .reporter + .event_high_severity(sender, time_stamp, event, aux_data) + .map(|_| true) + .map_err(|e| e.into()), + } + } + } + + impl PusEventDispatcher { + pub fn enable_tm_for_event_with_sev( + &mut self, + event: &EventU32TypedSev, + ) -> Result { + self.backend.enable_event_reporting(event.as_ref()) + } + + pub fn disable_tm_for_event_with_sev( + &mut self, + event: &EventU32TypedSev, + ) -> Result { + self.backend.disable_event_reporting(event.as_ref()) + } + + pub fn generate_pus_event_tm( + &mut self, + sender: &mut (impl EcssTmSender + ?Sized), + time_stamp: &[u8], + event: EventU32TypedSev, + aux_data: Option<&[u8]>, + ) -> Result> { + self.generate_pus_event_tm_generic(sender, time_stamp, event.into(), aux_data) } } } - -impl PusEventDispatcher { - pub fn enable_tm_for_event_with_sev( - &mut self, - event: &EventU32TypedSev, - ) -> Result { - self.backend.enable_event_reporting(event.as_ref()) - } - - pub fn disable_tm_for_event_with_sev( - &mut self, - event: &EventU32TypedSev, - ) -> Result { - self.backend.disable_event_reporting(event.as_ref()) - } - - pub fn generate_pus_event_tm( - &mut self, - sender: &mut (impl EcssTmSender + ?Sized), - time_stamp: &[u8], - event: EventU32TypedSev, - aux_data: Option<&[u8]>, - ) -> Result> { - self.generate_pus_event_tm_generic(sender, time_stamp, event.into(), aux_data) - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/satrs-core/src/pus/verification.rs b/satrs-core/src/pus/verification.rs index 1da862b..69c189f 100644 --- a/satrs-core/src/pus/verification.rs +++ b/satrs-core/src/pus/verification.rs @@ -77,6 +77,7 @@ use core::fmt::{Display, Formatter}; use core::hash::{Hash, Hasher}; use core::marker::PhantomData; use core::mem::size_of; +#[cfg(feature = "alloc")] use delegate::delegate; use spacepackets::ecss::EcssEnumeration; use spacepackets::tc::PusTc; @@ -87,7 +88,7 @@ use spacepackets::{SpHeader, MAX_APID}; pub use crate::seq_count::SimpleSeqCountProvider; #[cfg(feature = "alloc")] -pub use allocmod::{ +pub use alloc_mod::{ VerificationReporterCfg, VerificationReporterWithBuf, VerificationReporterWithSender, }; @@ -656,7 +657,7 @@ impl VerificationReporterBasic { } #[cfg(feature = "alloc")] -mod allocmod { +mod alloc_mod { use super::*; use alloc::boxed::Box; use alloc::vec; @@ -995,7 +996,7 @@ mod allocmod { #[cfg(feature = "std")] mod stdmod { - use super::allocmod::VerificationReporterWithSender; + use super::alloc_mod::VerificationReporterWithSender; use super::*; use crate::pool::{ShareablePoolProvider, SharedPool, StoreAddr, StoreError}; use delegate::delegate; diff --git a/satrs-core/src/res_code.rs b/satrs-core/src/res_code.rs index 26db165..9918ecf 100644 --- a/satrs-core/src/res_code.rs +++ b/satrs-core/src/res_code.rs @@ -1,8 +1,10 @@ +#[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use spacepackets::ecss::{EcssEnumU16, EcssEnumeration}; use spacepackets::{ByteConversionError, SizeMissmatch}; -#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ResultU16 { group_id: u8, unique_id: u8, diff --git a/satrs-core/src/tmtc/mod.rs b/satrs-core/src/tmtc/mod.rs index 0a31362..2710bb4 100644 --- a/satrs-core/src/tmtc/mod.rs +++ b/satrs-core/src/tmtc/mod.rs @@ -17,7 +17,9 @@ pub mod ccsds_distrib; pub mod pus_distrib; pub mod tm_helper; +#[cfg(feature = "alloc")] pub use ccsds_distrib::{CcsdsDistributor, CcsdsError, CcsdsPacketHandler}; +#[cfg(feature = "alloc")] pub use pus_distrib::{PusDistributor, PusServiceProvider}; #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] diff --git a/satrs-core/tests/pus_events.rs b/satrs-core/tests/pus_events.rs index 82fabd7..51c529c 100644 --- a/satrs-core/tests/pus_events.rs +++ b/satrs-core/tests/pus_events.rs @@ -78,10 +78,10 @@ fn test_threaded_usage() { .expect("Writing ECSS enum failed"); gen_event(Some(¶ms_array[0..e.raw_len()])) } - ParamsHeapless::Store(_) => gen_event(None), }, Params::Vec(vec) => gen_event(Some(vec.as_slice())), Params::String(str) => gen_event(Some(str.as_bytes())), + Params::Store(_) => gen_event(None), } } else { gen_event(None) diff --git a/spacepackets b/spacepackets index 9b091e3..ae55c39 160000 --- a/spacepackets +++ b/spacepackets @@ -1 +1 @@ -Subproject commit 9b091e3a3a6f599b093c96327751bcf1bc911ca1 +Subproject commit ae55c394bb2455eed3b05a5b587048a931d27238