From e99ce711a5cbb88bac5a8dfbb8b986bb36bd4e46 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sat, 22 Oct 2022 17:16:28 +0200 Subject: [PATCH] PUS event management backend provider --- Cargo.lock | 1 + fsrc-core/Cargo.toml | 3 +- fsrc-core/src/events.rs | 4 +- fsrc-core/src/pus/event_man.rs | 79 +++++++++++++++++++ fsrc-core/src/pus/mod.rs | 1 + fsrc-core/tests/{pool_test.rs => pools.rs} | 0 fsrc-core/tests/pus_events.rs | 1 + ...rification_test.rs => pus_verification.rs} | 0 8 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 fsrc-core/src/pus/event_man.rs rename fsrc-core/tests/{pool_test.rs => pools.rs} (100%) create mode 100644 fsrc-core/tests/pus_events.rs rename fsrc-core/tests/{verification_test.rs => pus_verification.rs} (100%) diff --git a/Cargo.lock b/Cargo.lock index cef630f..9314533 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -297,6 +297,7 @@ dependencies = [ "delegate 0.8.0", "downcast-rs", "hashbrown", + "heapless", "num-traits", "once_cell", "postcard", diff --git a/fsrc-core/Cargo.toml b/fsrc-core/Cargo.toml index e42e4b6..69df909 100644 --- a/fsrc-core/Cargo.toml +++ b/fsrc-core/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] delegate = "0.8" hashbrown = "0.12" +heapless = "0.7" [dependencies.num-traits] version = "0.2" @@ -40,4 +41,4 @@ version = "1.0" default = ["std"] std = ["downcast-rs/std", "alloc", "bus", "postcard/use-std", "crossbeam-channel/std"] alloc = [] - +heapless = [] diff --git a/fsrc-core/src/events.rs b/fsrc-core/src/events.rs index bce6324..02bf7f6 100644 --- a/fsrc-core/src/events.rs +++ b/fsrc-core/src/events.rs @@ -7,7 +7,7 @@ pub type GroupId = u16; pub type UniqueId = u16; pub type EventRaw = u32; -#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum Severity { INFO = 1, LOW = 2, @@ -29,7 +29,7 @@ impl TryFrom for Severity { } } -#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] pub struct Event { severity: Severity, group_id: GroupId, diff --git a/fsrc-core/src/pus/event_man.rs b/fsrc-core/src/pus/event_man.rs new file mode 100644 index 0000000..0a006e1 --- /dev/null +++ b/fsrc-core/src/pus/event_man.rs @@ -0,0 +1,79 @@ +use crate::events::Event; +use hashbrown::HashSet; + +#[cfg(feature = "heapless")] +pub use heapless_mod::*; + +/// This trait allows the PUS event manager implementation to stay generic over various types +/// of backend containers. These backend containers keep track on whether a particular event +/// is enabled or disabled for reporting and also expose a simple API to enable or disable the event +/// reporting. +/// +/// For example, a straight forward implementation for host systems could use a +/// [hash set](https://docs.rs/hashbrown/latest/hashbrown/struct.HashSet.html) +/// structure to track disabled events. A more primitive and embedded friendly +/// solution could track this information in a static or pre-allocated list which contains +/// the disabled events. +pub trait PusEventMgmtBackendProvider { + type Error; + + fn event_enabled(&self, event: &Event) -> bool; + fn enable_event_reporting(&mut self, event: &Event) -> Result; + fn disable_event_reporting(&mut self, event: &Event) -> Result; +} + +/// Default backend provider which uses a hash set as the event reporting status container +/// like mentioned in the example of the [PusEventMgmtBackendProvider] documentation. +/// +/// This provider is a good option for host systems or larger embedded systems where +/// the expected occasional memory allocation performed by the [HashSet] is not an issue. +#[derive(Default)] +pub struct DefaultPusMgmtBackendProvider { + disabled: HashSet, +} + +impl PusEventMgmtBackendProvider for DefaultPusMgmtBackendProvider { + type Error = (); + fn event_enabled(&self, event: &Event) -> bool { + !self.disabled.contains(event) + } + + fn enable_event_reporting(&mut self, event: &Event) -> Result { + Ok(self.disabled.remove(event)) + } + + fn disable_event_reporting(&mut self, event: &Event) -> Result { + Ok(self.disabled.insert(*event)) + } +} + +#[cfg(feature = "heapless")] +pub mod heapless_mod { + use super::*; + use crate::events::EventRaw; + + // TODO: After a new version of heapless is released which uses hash32 version 0.3, try using + // regular Event type again. + #[derive(Default)] + pub struct HeaplessPusMgmtBckendProvider { + disabled: heapless::FnvIndexSet, + } + + impl PusEventMgmtBackendProvider for HeaplessPusMgmtBckendProvider { + type Error = (); + + fn event_enabled(&self, event: &Event) -> bool { + self.disabled.contains(&event.raw()) + } + + fn enable_event_reporting(&mut self, event: &Event) -> Result { + self.disabled.insert(event.raw()).map_err(|_| ()) + } + + fn disable_event_reporting(&mut self, event: &Event) -> Result { + Ok(self.disabled.remove(&event.raw())) + } + } +} + +pub struct PusEventManager {} diff --git a/fsrc-core/src/pus/mod.rs b/fsrc-core/src/pus/mod.rs index 28e1c77..a09b398 100644 --- a/fsrc-core/src/pus/mod.rs +++ b/fsrc-core/src/pus/mod.rs @@ -10,6 +10,7 @@ use spacepackets::tm::PusTm; use spacepackets::{ByteConversionError, SizeMissmatch}; pub mod event; +pub mod event_man; pub mod verification; /// Generic error type which is also able to wrap a user send error with the user supplied type E. diff --git a/fsrc-core/tests/pool_test.rs b/fsrc-core/tests/pools.rs similarity index 100% rename from fsrc-core/tests/pool_test.rs rename to fsrc-core/tests/pools.rs diff --git a/fsrc-core/tests/pus_events.rs b/fsrc-core/tests/pus_events.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/fsrc-core/tests/pus_events.rs @@ -0,0 +1 @@ + diff --git a/fsrc-core/tests/verification_test.rs b/fsrc-core/tests/pus_verification.rs similarity index 100% rename from fsrc-core/tests/verification_test.rs rename to fsrc-core/tests/pus_verification.rs