2023-01-11 10:30:03 +01:00
|
|
|
use crate::events::{EventU32, GenericEvent, Severity};
|
|
|
|
#[cfg(feature = "alloc")]
|
|
|
|
use crate::events::{EventU32TypedSev, HasSeverity};
|
|
|
|
#[cfg(feature = "alloc")]
|
|
|
|
use core::hash::Hash;
|
|
|
|
#[cfg(feature = "alloc")]
|
|
|
|
use hashbrown::HashSet;
|
|
|
|
|
|
|
|
#[cfg(feature = "alloc")]
|
|
|
|
pub use crate::pus::event::EventReporter;
|
2023-07-05 14:25:51 +02:00
|
|
|
use crate::pus::verification::TcStateToken;
|
2023-01-11 10:30:03 +01:00
|
|
|
#[cfg(feature = "alloc")]
|
2024-04-16 11:04:22 +02:00
|
|
|
use crate::pus::EcssTmSender;
|
2023-07-09 20:05:45 +02:00
|
|
|
use crate::pus::EcssTmtcError;
|
2023-01-11 10:30:03 +01:00
|
|
|
#[cfg(feature = "alloc")]
|
|
|
|
pub use alloc_mod::*;
|
|
|
|
#[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.
|
2024-04-24 14:30:45 +02:00
|
|
|
pub trait PusEventReportingMapProvider<Event: GenericEvent> {
|
2023-01-11 10:30:03 +01:00
|
|
|
type Error;
|
|
|
|
|
2024-02-23 14:19:30 +01:00
|
|
|
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>;
|
2023-01-11 10:30:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "heapless")]
|
|
|
|
pub mod heapless_mod {
|
|
|
|
use super::*;
|
2024-02-26 11:41:42 +01:00
|
|
|
use crate::events::LargestEventRaw;
|
|
|
|
use core::marker::PhantomData;
|
2023-01-11 10:30:03 +01:00
|
|
|
|
|
|
|
// 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 HeaplessPusMgmtBackendProvider<const N: usize, Provider: GenericEvent> {
|
|
|
|
disabled: heapless::FnvIndexSet<LargestEventRaw, N>,
|
|
|
|
phantom: PhantomData<Provider>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Safety: All contained field are [Send] as well
|
|
|
|
unsafe impl<const N: usize, Event: GenericEvent + Send> Send
|
|
|
|
for HeaplessPusMgmtBackendProvider<N, Event>
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-04-24 14:30:45 +02:00
|
|
|
impl<const N: usize, Provider: GenericEvent> PusEventReportingMapProvider<Provider>
|
2023-01-11 10:30:03 +01:00
|
|
|
for HeaplessPusMgmtBackendProvider<N, Provider>
|
|
|
|
{
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn event_enabled(&self, event: &Provider) -> bool {
|
|
|
|
self.disabled.contains(&event.raw_as_largest_type())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn enable_event_reporting(&mut self, event: &Provider) -> Result<bool, Self::Error> {
|
|
|
|
self.disabled
|
|
|
|
.insert(event.raw_as_largest_type())
|
|
|
|
.map_err(|_| ())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn disable_event_reporting(&mut self, event: &Provider) -> Result<bool, Self::Error> {
|
|
|
|
Ok(self.disabled.remove(&event.raw_as_largest_type()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-03 13:41:51 +01:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2023-01-11 10:30:03 +01:00
|
|
|
pub enum EventRequest<Event: GenericEvent = EventU32> {
|
|
|
|
Enable(Event),
|
|
|
|
Disable(Event),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct EventRequestWithToken<Event: GenericEvent = EventU32> {
|
|
|
|
pub request: EventRequest<Event>,
|
2023-07-05 14:25:51 +02:00
|
|
|
pub token: TcStateToken,
|
2023-01-11 10:30:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2023-07-09 13:04:00 +02:00
|
|
|
pub enum EventManError {
|
2023-07-09 20:05:45 +02:00
|
|
|
EcssTmtcError(EcssTmtcError),
|
2023-01-11 10:30:03 +01:00
|
|
|
SeverityMissmatch(Severity, Severity),
|
|
|
|
}
|
|
|
|
|
2023-07-09 20:05:45 +02:00
|
|
|
impl From<EcssTmtcError> for EventManError {
|
|
|
|
fn from(v: EcssTmtcError) -> Self {
|
2023-07-03 00:42:20 +02:00
|
|
|
Self::EcssTmtcError(v)
|
2023-01-11 10:30:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "alloc")]
|
|
|
|
pub mod alloc_mod {
|
2024-02-23 14:19:30 +01:00
|
|
|
use core::marker::PhantomData;
|
|
|
|
|
2024-04-24 14:30:45 +02:00
|
|
|
use crate::{
|
|
|
|
events::EventU16,
|
|
|
|
pus::event::{DummyEventHook, EventTmHookProvider},
|
|
|
|
};
|
2024-02-23 14:19:30 +01:00
|
|
|
|
2023-01-11 10:30:03 +01:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
/// Default backend provider which uses a hash set as the event reporting status container
|
2024-04-24 14:30:45 +02:00
|
|
|
/// like mentioned in the example of the [PusEventReportingMapProvider] documentation.
|
2023-01-11 10:30:03 +01:00
|
|
|
///
|
|
|
|
/// 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.
|
2024-04-24 14:30:45 +02:00
|
|
|
pub struct DefaultPusEventReportingMap<Event: GenericEvent = EventU32> {
|
2023-01-11 10:30:03 +01:00
|
|
|
disabled: HashSet<Event>,
|
|
|
|
}
|
|
|
|
|
2024-04-24 14:30:45 +02:00
|
|
|
impl<Event: GenericEvent> Default for DefaultPusEventReportingMap<Event> {
|
2023-01-11 10:30:03 +01:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
disabled: HashSet::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 14:30:45 +02:00
|
|
|
impl<Event: GenericEvent + PartialEq + Eq + Hash + Copy + Clone>
|
|
|
|
PusEventReportingMapProvider<Event> for DefaultPusEventReportingMap<Event>
|
2023-01-11 10:30:03 +01:00
|
|
|
{
|
|
|
|
type Error = ();
|
2024-02-23 14:19:30 +01:00
|
|
|
|
2024-04-24 14:30:45 +02:00
|
|
|
fn event_enabled(&self, event: &Event) -> bool {
|
2023-01-11 10:30:03 +01:00
|
|
|
!self.disabled.contains(event)
|
|
|
|
}
|
|
|
|
|
2024-04-24 14:30:45 +02:00
|
|
|
fn enable_event_reporting(&mut self, event: &Event) -> Result<bool, Self::Error> {
|
2023-01-11 10:30:03 +01:00
|
|
|
Ok(self.disabled.remove(event))
|
|
|
|
}
|
|
|
|
|
2024-04-24 14:30:45 +02:00
|
|
|
fn disable_event_reporting(&mut self, event: &Event) -> Result<bool, Self::Error> {
|
2023-01-11 10:30:03 +01:00
|
|
|
Ok(self.disabled.insert(*event))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 14:30:45 +02:00
|
|
|
pub struct PusEventTmCreatorWithMap<
|
|
|
|
ReportingMap: PusEventReportingMapProvider<Event>,
|
|
|
|
Event: GenericEvent,
|
|
|
|
EventTmHook: EventTmHookProvider = DummyEventHook,
|
2024-02-23 14:19:30 +01:00
|
|
|
> {
|
2024-04-24 14:30:45 +02:00
|
|
|
pub reporter: EventReporter<EventTmHook>,
|
|
|
|
reporting_map: ReportingMap,
|
|
|
|
phantom: PhantomData<Event>,
|
2023-01-11 10:30:03 +01:00
|
|
|
}
|
|
|
|
|
2024-04-24 14:30:45 +02:00
|
|
|
impl<
|
|
|
|
ReportingMap: PusEventReportingMapProvider<Event>,
|
|
|
|
Event: GenericEvent,
|
|
|
|
EventTmHook: EventTmHookProvider,
|
|
|
|
> PusEventTmCreatorWithMap<ReportingMap, Event, EventTmHook>
|
2024-02-23 14:19:30 +01:00
|
|
|
{
|
2024-04-24 14:30:45 +02:00
|
|
|
pub fn new(reporter: EventReporter<EventTmHook>, backend: ReportingMap) -> Self {
|
2024-02-23 14:19:30 +01:00
|
|
|
Self {
|
|
|
|
reporter,
|
2024-04-24 14:30:45 +02:00
|
|
|
reporting_map: backend,
|
2024-02-23 14:19:30 +01:00
|
|
|
phantom: PhantomData,
|
|
|
|
}
|
2023-01-11 10:30:03 +01:00
|
|
|
}
|
|
|
|
|
2024-04-24 14:30:45 +02:00
|
|
|
pub fn enable_tm_for_event(&mut self, event: &Event) -> Result<bool, ReportingMap::Error> {
|
|
|
|
self.reporting_map.enable_event_reporting(event)
|
2023-01-11 10:30:03 +01:00
|
|
|
}
|
|
|
|
|
2024-04-24 14:30:45 +02:00
|
|
|
pub fn disable_tm_for_event(&mut self, event: &Event) -> Result<bool, ReportingMap::Error> {
|
|
|
|
self.reporting_map.disable_event_reporting(event)
|
2023-01-11 10:30:03 +01:00
|
|
|
}
|
|
|
|
|
2023-07-09 13:23:34 +02:00
|
|
|
pub fn generate_pus_event_tm_generic(
|
2024-04-04 15:18:53 +02:00
|
|
|
&self,
|
2024-04-16 11:04:22 +02:00
|
|
|
sender: &(impl EcssTmSender + ?Sized),
|
2023-01-11 10:30:03 +01:00
|
|
|
time_stamp: &[u8],
|
2024-04-04 15:18:53 +02:00
|
|
|
event: Event,
|
|
|
|
params: Option<&[u8]>,
|
2023-07-09 13:04:00 +02:00
|
|
|
) -> Result<bool, EventManError> {
|
2024-04-24 14:30:45 +02:00
|
|
|
if !self.reporting_map.event_enabled(&event) {
|
2023-01-11 10:30:03 +01:00
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
match event.severity() {
|
|
|
|
Severity::INFO => self
|
|
|
|
.reporter
|
2024-04-04 15:18:53 +02:00
|
|
|
.event_info(sender, time_stamp, event, params)
|
2023-01-11 10:30:03 +01:00
|
|
|
.map(|_| true)
|
|
|
|
.map_err(|e| e.into()),
|
|
|
|
Severity::LOW => self
|
|
|
|
.reporter
|
2024-04-04 15:18:53 +02:00
|
|
|
.event_low_severity(sender, time_stamp, event, params)
|
2023-01-11 10:30:03 +01:00
|
|
|
.map(|_| true)
|
|
|
|
.map_err(|e| e.into()),
|
|
|
|
Severity::MEDIUM => self
|
|
|
|
.reporter
|
2024-04-04 15:18:53 +02:00
|
|
|
.event_medium_severity(sender, time_stamp, event, params)
|
2023-01-11 10:30:03 +01:00
|
|
|
.map(|_| true)
|
|
|
|
.map_err(|e| e.into()),
|
|
|
|
Severity::HIGH => self
|
|
|
|
.reporter
|
2024-04-04 15:18:53 +02:00
|
|
|
.event_high_severity(sender, time_stamp, event, params)
|
2023-01-11 10:30:03 +01:00
|
|
|
.map(|_| true)
|
|
|
|
.map_err(|e| e.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 14:30:45 +02:00
|
|
|
impl<Event: GenericEvent + Copy + PartialEq + Eq + Hash, EventTmHook: EventTmHookProvider>
|
|
|
|
PusEventTmCreatorWithMap<DefaultPusEventReportingMap<Event>, Event, EventTmHook>
|
2024-02-23 14:19:30 +01:00
|
|
|
{
|
2024-04-24 14:30:45 +02:00
|
|
|
pub fn new_with_default_backend(reporter: EventReporter<EventTmHook>) -> Self {
|
2024-02-23 14:19:30 +01:00
|
|
|
Self {
|
|
|
|
reporter,
|
2024-04-24 14:30:45 +02:00
|
|
|
reporting_map: DefaultPusEventReportingMap::default(),
|
2024-02-23 14:19:30 +01:00
|
|
|
phantom: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 14:30:45 +02:00
|
|
|
impl<ReportingMap: PusEventReportingMapProvider<EventU32>>
|
|
|
|
PusEventTmCreatorWithMap<ReportingMap, EventU32>
|
|
|
|
{
|
2023-01-11 10:30:03 +01:00
|
|
|
pub fn enable_tm_for_event_with_sev<Severity: HasSeverity>(
|
|
|
|
&mut self,
|
|
|
|
event: &EventU32TypedSev<Severity>,
|
2024-04-24 14:30:45 +02:00
|
|
|
) -> Result<bool, ReportingMap::Error> {
|
|
|
|
self.reporting_map.enable_event_reporting(event.as_ref())
|
2023-01-11 10:30:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn disable_tm_for_event_with_sev<Severity: HasSeverity>(
|
|
|
|
&mut self,
|
|
|
|
event: &EventU32TypedSev<Severity>,
|
2024-04-24 14:30:45 +02:00
|
|
|
) -> Result<bool, ReportingMap::Error> {
|
|
|
|
self.reporting_map.disable_event_reporting(event.as_ref())
|
2023-01-11 10:30:03 +01:00
|
|
|
}
|
|
|
|
|
2023-07-10 00:29:31 +02:00
|
|
|
pub fn generate_pus_event_tm<Severity: HasSeverity>(
|
2024-04-04 15:18:53 +02:00
|
|
|
&self,
|
2024-04-16 11:04:22 +02:00
|
|
|
sender: &(impl EcssTmSender + ?Sized),
|
2023-01-11 10:30:03 +01:00
|
|
|
time_stamp: &[u8],
|
|
|
|
event: EventU32TypedSev<Severity>,
|
|
|
|
aux_data: Option<&[u8]>,
|
2023-07-09 13:04:00 +02:00
|
|
|
) -> Result<bool, EventManError> {
|
2023-01-11 10:30:03 +01:00
|
|
|
self.generate_pus_event_tm_generic(sender, time_stamp, event.into(), aux_data)
|
|
|
|
}
|
|
|
|
}
|
2024-02-23 14:19:30 +01:00
|
|
|
|
2024-04-24 14:30:45 +02:00
|
|
|
pub type DefaultPusEventU16TmCreator<EventTmHook = DummyEventHook> =
|
|
|
|
PusEventTmCreatorWithMap<DefaultPusEventReportingMap<EventU16>, EventU16, EventTmHook>;
|
|
|
|
pub type DefaultPusEventU32TmCreator<EventTmHook = DummyEventHook> =
|
|
|
|
PusEventTmCreatorWithMap<DefaultPusEventReportingMap<EventU32>, EventU32, EventTmHook>;
|
2023-01-11 10:30:03 +01:00
|
|
|
}
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2024-04-04 15:18:53 +02:00
|
|
|
use crate::request::UniqueApidTargetId;
|
2024-04-16 11:04:22 +02:00
|
|
|
use crate::{events::SeverityInfo, tmtc::PacketAsVec};
|
2024-02-26 11:41:42 +01:00
|
|
|
use std::sync::mpsc::{self, TryRecvError};
|
2023-01-11 10:30:03 +01:00
|
|
|
|
|
|
|
const INFO_EVENT: EventU32TypedSev<SeverityInfo> =
|
|
|
|
EventU32TypedSev::<SeverityInfo>::const_new(1, 0);
|
|
|
|
const LOW_SEV_EVENT: EventU32 = EventU32::const_new(Severity::LOW, 1, 5);
|
|
|
|
const EMPTY_STAMP: [u8; 7] = [0; 7];
|
2024-04-04 15:18:53 +02:00
|
|
|
const TEST_APID: u16 = 0x02;
|
|
|
|
const TEST_ID: UniqueApidTargetId = UniqueApidTargetId::new(TEST_APID, 0x05);
|
2023-01-11 10:30:03 +01:00
|
|
|
|
2024-04-24 14:30:45 +02:00
|
|
|
fn create_basic_man_1() -> DefaultPusEventU32TmCreator {
|
2024-04-04 15:18:53 +02:00
|
|
|
let reporter = EventReporter::new(TEST_ID.raw(), TEST_APID, 0, 128)
|
|
|
|
.expect("Creating event repoter failed");
|
2024-04-24 14:30:45 +02:00
|
|
|
PusEventTmCreatorWithMap::new_with_default_backend(reporter)
|
2024-02-23 14:19:30 +01:00
|
|
|
}
|
2024-04-24 14:30:45 +02:00
|
|
|
fn create_basic_man_2() -> DefaultPusEventU32TmCreator {
|
2024-04-04 15:18:53 +02:00
|
|
|
let reporter = EventReporter::new(TEST_ID.raw(), TEST_APID, 0, 128)
|
|
|
|
.expect("Creating event repoter failed");
|
2024-04-24 14:30:45 +02:00
|
|
|
let backend = DefaultPusEventReportingMap::default();
|
|
|
|
PusEventTmCreatorWithMap::new(reporter, backend)
|
2023-01-11 10:30:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_basic() {
|
2024-04-04 15:18:53 +02:00
|
|
|
let event_man = create_basic_man_1();
|
2024-04-16 11:04:22 +02:00
|
|
|
let (event_tx, event_rx) = mpsc::channel::<PacketAsVec>();
|
2023-01-11 10:30:03 +01:00
|
|
|
let event_sent = event_man
|
2024-04-04 15:18:53 +02:00
|
|
|
.generate_pus_event_tm(&event_tx, &EMPTY_STAMP, INFO_EVENT, None)
|
2023-01-11 10:30:03 +01:00
|
|
|
.expect("Sending info event failed");
|
|
|
|
|
|
|
|
assert!(event_sent);
|
|
|
|
// Will not check packet here, correctness of packet was tested somewhere else
|
|
|
|
event_rx.try_recv().expect("Receiving event TM failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_disable_event() {
|
2024-02-23 14:19:30 +01:00
|
|
|
let mut event_man = create_basic_man_2();
|
2024-04-16 11:04:22 +02:00
|
|
|
let (event_tx, event_rx) = mpsc::channel::<PacketAsVec>();
|
2024-04-04 15:18:53 +02:00
|
|
|
// let mut sender = TmAsVecSenderWithMpsc::new(0, "test", event_tx);
|
2023-01-11 10:30:03 +01:00
|
|
|
let res = event_man.disable_tm_for_event(&LOW_SEV_EVENT);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert!(res.unwrap());
|
|
|
|
let mut event_sent = event_man
|
2024-04-04 15:18:53 +02:00
|
|
|
.generate_pus_event_tm_generic(&event_tx, &EMPTY_STAMP, LOW_SEV_EVENT, None)
|
2023-01-11 10:30:03 +01:00
|
|
|
.expect("Sending low severity event failed");
|
|
|
|
assert!(!event_sent);
|
|
|
|
let res = event_rx.try_recv();
|
|
|
|
assert!(res.is_err());
|
|
|
|
assert!(matches!(res.unwrap_err(), TryRecvError::Empty));
|
|
|
|
// Check that only the low severity event was disabled
|
|
|
|
event_sent = event_man
|
2024-04-04 15:18:53 +02:00
|
|
|
.generate_pus_event_tm(&event_tx, &EMPTY_STAMP, INFO_EVENT, None)
|
2023-01-11 10:30:03 +01:00
|
|
|
.expect("Sending info event failed");
|
|
|
|
assert!(event_sent);
|
|
|
|
event_rx.try_recv().expect("No info event received");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reenable_event() {
|
2024-02-23 14:19:30 +01:00
|
|
|
let mut event_man = create_basic_man_1();
|
2024-04-16 11:04:22 +02:00
|
|
|
let (event_tx, event_rx) = mpsc::channel::<PacketAsVec>();
|
2023-01-11 10:30:03 +01:00
|
|
|
let mut res = event_man.disable_tm_for_event_with_sev(&INFO_EVENT);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert!(res.unwrap());
|
|
|
|
res = event_man.enable_tm_for_event_with_sev(&INFO_EVENT);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert!(res.unwrap());
|
|
|
|
let event_sent = event_man
|
2024-04-04 15:18:53 +02:00
|
|
|
.generate_pus_event_tm(&event_tx, &EMPTY_STAMP, INFO_EVENT, None)
|
2023-01-11 10:30:03 +01:00
|
|
|
.expect("Sending info event failed");
|
|
|
|
assert!(event_sent);
|
|
|
|
event_rx.try_recv().expect("No info event received");
|
|
|
|
}
|
|
|
|
}
|