PUS event management backend provider

This commit is contained in:
Robin Müller 2022-10-22 17:16:28 +02:00
parent 0f21203d27
commit e99ce711a5
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
8 changed files with 86 additions and 3 deletions

1
Cargo.lock generated
View File

@ -297,6 +297,7 @@ dependencies = [
"delegate 0.8.0",
"downcast-rs",
"hashbrown",
"heapless",
"num-traits",
"once_cell",
"postcard",

View File

@ -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 = []

View File

@ -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<u8> for Severity {
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Event {
severity: Severity,
group_id: GroupId,

View File

@ -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<bool, Self::Error>;
fn disable_event_reporting(&mut self, event: &Event) -> Result<bool, Self::Error>;
}
/// 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<Event>,
}
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<bool, Self::Error> {
Ok(self.disabled.remove(event))
}
fn disable_event_reporting(&mut self, event: &Event) -> Result<bool, Self::Error> {
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<const N: usize> {
disabled: heapless::FnvIndexSet<EventRaw, N>,
}
impl<const N: usize> PusEventMgmtBackendProvider for HeaplessPusMgmtBckendProvider<N> {
type Error = ();
fn event_enabled(&self, event: &Event) -> bool {
self.disabled.contains(&event.raw())
}
fn enable_event_reporting(&mut self, event: &Event) -> Result<bool, Self::Error> {
self.disabled.insert(event.raw()).map_err(|_| ())
}
fn disable_event_reporting(&mut self, event: &Event) -> Result<bool, Self::Error> {
Ok(self.disabled.remove(&event.raw()))
}
}
}
pub struct PusEventManager {}

View File

@ -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.

View File

@ -0,0 +1 @@