diff --git a/fsrc-core/src/event_man.rs b/fsrc-core/src/event_man.rs index 27d3b04..381ccd7 100644 --- a/fsrc-core/src/event_man.rs +++ b/fsrc-core/src/event_man.rs @@ -27,7 +27,8 @@ //! events to package them as telemetry or start actions where applicable. //! Other components might only be interested in certain events. For example, a thermal system //! handler might only be interested in temperature events generated by a thermal sensor component. -use crate::events::{GenericEvent, LargestEventRaw, LargestGroupIdRaw}; +use crate::events::{EventU16, EventU32, GenericEvent, LargestEventRaw, LargestGroupIdRaw}; +use crate::util::{AuxData, AuxDataHeapless}; use alloc::boxed::Box; use alloc::vec; use alloc::vec::Vec; @@ -43,48 +44,69 @@ enum ListenerType { All, } -pub trait SendEventProvider { +pub type EventWithHeaplessAuxData = (Event, Option); +pub type EventU32WithHeaplessAuxData = EventWithHeaplessAuxData; +pub type EventU16WithHeaplessAuxData = EventWithHeaplessAuxData; + +pub type EventWithAuxData = (Event, Option); +pub type EventU32WithAuxData = EventWithAuxData; +pub type EventU16WithAuxData = EventWithAuxData; + +pub trait SendEventProvider { type Error; fn id(&self) -> u32; fn send_no_data(&mut self, event: Provider) -> Result<(), Self::Error> { self.send(event, None) } - fn send(&mut self, event: Provider, aux_data: Option<&[u8]>) -> Result<(), Self::Error>; + fn send( + &mut self, + event: Provider, + aux_data: Option, + ) -> Result<(), Self::Error>; } -struct Listener { +struct Listener { ltype: ListenerType, - send_provider: Box>, + send_provider: Box>, } /// Generic abstraction for an event receiver. -pub trait EventReceiver { +pub trait EventReceiver { /// This function has to be provided by any event receiver. A receive call may or may not return /// an event. /// /// To allow returning arbitrary additional auxiliary data, a mutable slice is passed to the /// [Self::receive] call as well. Receivers can write data to this slice, but care must be taken /// to avoid panics due to size missmatches or out of bound writes. - fn receive(&mut self, aux_data: &mut [u8]) -> Option; + fn receive(&mut self) -> Option<(Event, Option)>; } /// Generic event manager implementation. -pub struct EventManager { - aux_data_buf: Vec, - listeners: HashMap>>, - event_receiver: Box>, +/// +/// # Generics +/// +/// * `SendProviderError`: [SendEventProvider] error type +/// * `Event`: Concrete event provider, currently either [EventU32] or [EventU16] +/// * `AuxDataProvider`: Concrete auxiliary data provder, currently either [AuxData] or +/// [AuxDataHeapless] +pub struct EventManager< + SendProviderError, + Event: GenericEvent = EventU32, + AuxDataProvider = AuxData, +> { + listeners: HashMap>>, + event_receiver: Box>, } -pub enum HandlerResult { +pub enum HandlerResult { Empty, - Handled(u32, Provider), + Handled(u32, Provider, Option), } impl EventManager { - pub fn new(event_receiver: Box>, buf_len_aux_data: usize) -> Self { + pub fn new(event_receiver: Box>) -> Self { EventManager { - aux_data_buf: vec![0; buf_len_aux_data], listeners: HashMap::new(), event_receiver, } @@ -105,6 +127,10 @@ impl EventManager { self.update_listeners(ListenerType::Group(group_id), dest); } + /// Subscribe for all events received by the manager. + /// + /// For example, this can be useful for a handler component which sends every event as + /// a telemetry packet. pub fn subscribe_all(&mut self, dest: impl SendEventProvider + 'static) { self.update_listeners(ListenerType::All, dest); } @@ -126,11 +152,13 @@ impl EventManager { } } -impl EventManager { +impl + EventManager +{ fn update_listeners( &mut self, key: ListenerType, - dest: impl SendEventProvider + 'static, + dest: impl SendEventProvider + 'static, ) { if !self.listeners.contains_key(&key) { self.listeners.insert( @@ -155,28 +183,27 @@ impl EventManager { } } - pub fn try_event_handling(&mut self) -> Result, E> { + pub fn try_event_handling(&mut self) -> Result, E> { let mut err_status = None; let mut num_recipients = 0; let mut send_handler = - |event: Provider, aux_data: Option<&[u8]>, llist: &mut Vec>| { + |event: Event, + aux_data: Option, + llist: &mut Vec>| { for listener in llist.iter_mut() { - if let Err(e) = listener.send_provider.send(event, aux_data) { + if let Err(e) = listener.send_provider.send(event, aux_data.clone()) { err_status = Some(Err(e)); } else { num_recipients += 1; } } }; - if let Some(event) = self - .event_receiver - .receive(self.aux_data_buf.as_mut_slice()) - { + if let Some((event, aux_data)) = self.event_receiver.receive() { let single_key = ListenerType::Single(event.raw_as_largest_type()); if self.listeners.contains_key(&single_key) { send_handler( event, - Some(self.aux_data_buf.as_slice()), + aux_data.clone(), self.listeners.get_mut(&single_key).unwrap(), ); } @@ -184,17 +211,17 @@ impl EventManager { if self.listeners.contains_key(&group_key) { send_handler( event, - Some(self.aux_data_buf.as_slice()), + aux_data.clone(), self.listeners.get_mut(&group_key).unwrap(), ); } if let Some(all_receivers) = self.listeners.get_mut(&ListenerType::All) { - send_handler(event, Some(self.aux_data_buf.as_slice()), all_receivers); + send_handler(event, aux_data.clone(), all_receivers); } if let Some(err) = err_status { return err; } - return Ok(HandlerResult::Handled(num_recipients, event)); + return Ok(HandlerResult::Handled(num_recipients, event, aux_data)); } Ok(HandlerResult::Empty) } @@ -202,31 +229,26 @@ impl EventManager { #[cfg(feature = "std")] pub mod stdmod { - use crate::event_man::EventReceiver; + use crate::event_man::{EventReceiver, EventWithAuxData}; use crate::events::{EventU16, EventU32, GenericEvent}; + use crate::util::AuxData; use std::sync::mpsc::Receiver; - use std::vec::Vec; pub struct MpscEventReceiver { - mpsc_receiver: Receiver<(Event, Option>)>, + mpsc_receiver: Receiver<(Event, Option)>, } impl MpscEventReceiver { - pub fn new(receiver: Receiver<(Event, Option>)>) -> Self { + pub fn new(receiver: Receiver<(Event, Option)>) -> Self { Self { mpsc_receiver: receiver, } } } impl EventReceiver for MpscEventReceiver { - fn receive(&mut self, aux_data: &mut [u8]) -> Option { - if let Some((event, params)) = self.mpsc_receiver.try_recv().ok() { - if let Some(params) = params { - if params.len() < aux_data.len() { - aux_data[0..params.len()].copy_from_slice(params.as_slice()) - } - } - return Some(event); + fn receive(&mut self) -> Option> { + if let Ok(event_and_data) = self.mpsc_receiver.try_recv() { + return Some(event_and_data); } None } @@ -241,62 +263,76 @@ mod tests { use super::*; use crate::event_man::EventManager; use crate::events::{EventU32, GenericEvent, Severity}; + use crate::util::AuxDataRaw; use alloc::boxed::Box; + use std::format; use std::sync::mpsc::{channel, Receiver, SendError, Sender}; - type EventAndParams<'a> = (EventU32, Option<&'a [u8]>); - #[derive(Clone)] - struct MpscEventSenderQueue<'a> { + struct MpscEventSenderQueue { id: u32, - mpsc_sender: Sender>, + mpsc_sender: Sender, } - impl<'a> SendEventProvider for MpscEventSenderQueue<'a> { - type Error = SendError>; + impl MpscEventSenderQueue { + fn new(id: u32, mpsc_sender: Sender) -> Self { + Self { id, mpsc_sender } + } + } + + impl SendEventProvider for MpscEventSenderQueue { + type Error = SendError; fn id(&self) -> u32 { self.id } - fn send(&mut self, event: EventU32, aux_data: Option<&'a [u8]>) -> Result<(), Self::Error> { + fn send(&mut self, event: EventU32, aux_data: Option) -> Result<(), Self::Error> { self.mpsc_sender.send((event, aux_data)) } } - fn check_next_event(expected: EventU32, receiver: &Receiver) { - for _ in 0..5 { - if let Ok(event) = receiver.try_recv() { - assert_eq!(event.0, expected); - break; - } + fn check_next_event( + expected: EventU32, + receiver: &Receiver, + ) -> Option { + if let Ok(event) = receiver.try_recv() { + assert_eq!(event.0, expected); + return event.1; } + None } fn check_handled_event( - res: HandlerResult, + res: HandlerResult, expected: EventU32, expected_num_sent: u32, ) { assert!(matches!(res, HandlerResult::Handled { .. })); - if let HandlerResult::Handled(num_recipients, event) = res { + if let HandlerResult::Handled(num_recipients, event, _aux_data) = res { assert_eq!(event, expected); assert_eq!(num_recipients, expected_num_sent); } } - #[test] - fn test_basic() { + fn generic_event_man() -> ( + Sender, + EventManager>, + ) { let (event_sender, manager_queue) = channel(); let event_man_receiver = MpscEventReceiver::new(manager_queue); - let mut event_man: EventManager, EventU32> = - EventManager::new(Box::new(event_man_receiver), 128); + ( + event_sender, + EventManager::new(Box::new(event_man_receiver)), + ) + } + + #[test] + fn test_basic() { + let (event_sender, mut event_man) = generic_event_man(); let event_grp_0 = EventU32::new(Severity::INFO, 0, 0).unwrap(); let event_grp_1_0 = EventU32::new(Severity::HIGH, 1, 0).unwrap(); let (single_event_sender, single_event_receiver) = channel(); - let single_event_listener = MpscEventSenderQueue { - id: 0, - mpsc_sender: single_event_sender, - }; + let single_event_listener = MpscEventSenderQueue::new(0, single_event_sender); event_man.subscribe_single(event_grp_0, single_event_listener); let (group_event_sender_0, group_event_receiver_0) = channel(); let group_event_listener = MpscEventSenderQueue { @@ -324,13 +360,34 @@ mod tests { check_next_event(event_grp_1_0, &group_event_receiver_0); } + #[test] + fn test_with_basic_aux_data() { + let (event_sender, mut event_man) = generic_event_man(); + let event_grp_0 = EventU32::new(Severity::INFO, 0, 0).unwrap(); + let (single_event_sender, single_event_receiver) = channel(); + let single_event_listener = MpscEventSenderQueue::new(0, single_event_sender); + event_man.subscribe_single(event_grp_0, single_event_listener); + event_sender + .send((event_grp_0, Some(AuxData::Heapless((2_u32, 3_u32).into())))) + .expect("Sending group error failed"); + let res = event_man.try_event_handling(); + assert!(res.is_ok()); + check_handled_event(res.unwrap(), event_grp_0, 1); + let aux = check_next_event(event_grp_0, &single_event_receiver); + assert!(aux.is_some()); + let aux = aux.unwrap(); + if let AuxData::Heapless(AuxDataHeapless::Raw(AuxDataRaw::U32Pair(pair))) = aux { + assert_eq!(pair.0, 2); + assert_eq!(pair.1, 3); + } else { + panic!("{}", format!("Unexpected auxiliary value type {:?}", aux)); + } + } + /// Test listening for multiple groups #[test] fn test_multi_group() { - let (event_sender, manager_queue) = channel(); - let event_man_receiver = MpscEventReceiver::new(manager_queue); - let mut event_man: EventManager, EventU32> = - EventManager::new(Box::new(event_man_receiver), 128); + let (event_sender, mut event_man) = generic_event_man(); let res = event_man.try_event_handling(); assert!(res.is_ok()); let hres = res.unwrap(); @@ -367,10 +424,7 @@ mod tests { /// to both group and single events from one listener #[test] fn test_listening_to_same_event_and_multi_type() { - let (event_sender, manager_queue) = channel(); - let event_man_receiver = MpscEventReceiver::new(manager_queue); - let mut event_man: EventManager, EventU32> = - EventManager::new(Box::new(event_man_receiver), 128); + let (event_sender, mut event_man) = generic_event_man(); let event_0 = EventU32::new(Severity::INFO, 0, 5).unwrap(); let event_1 = EventU32::new(Severity::HIGH, 1, 0).unwrap(); let (event_0_tx_0, event_0_rx_0) = channel(); @@ -426,8 +480,8 @@ mod tests { fn test_all_events_listener() { let (event_sender, manager_queue) = channel(); let event_man_receiver = MpscEventReceiver::new(manager_queue); - let mut event_man: EventManager, EventU32> = - EventManager::new(Box::new(event_man_receiver), 128); + let mut event_man: EventManager> = + EventManager::new(Box::new(event_man_receiver)); let event_0 = EventU32::new(Severity::INFO, 0, 5).unwrap(); let event_1 = EventU32::new(Severity::HIGH, 1, 0).unwrap(); let (event_0_tx_0, all_events_rx) = channel(); diff --git a/fsrc-core/src/lib.rs b/fsrc-core/src/lib.rs index e8349da..c5178d4 100644 --- a/fsrc-core/src/lib.rs +++ b/fsrc-core/src/lib.rs @@ -28,5 +28,6 @@ pub mod objects; pub mod pool; pub mod pus; pub mod tmtc; +pub mod util; extern crate downcast_rs; diff --git a/fsrc-core/src/util.rs b/fsrc-core/src/util.rs new file mode 100644 index 0000000..ef8033c --- /dev/null +++ b/fsrc-core/src/util.rs @@ -0,0 +1,97 @@ +use crate::pool::StoreAddr; +#[cfg(feature = "alloc")] +use alloc::string::String; +#[cfg(feature = "alloc")] +use alloc::string::ToString; +#[cfg(feature = "alloc")] +use alloc::vec::Vec; + +#[derive(Debug, Copy, Clone)] +pub enum AuxDataRaw { + U8(u8), + U8Pair((u8, u8)), + U8Triplet((u8, u8, u8)), + I8(i8), + I8Pair((i8, i8)), + I8Triplet((i8, i8, i8)), + U16(u16), + U16Pair((u16, u16)), + U16Triplet((u16, u16, u16)), + I16(i16), + I16Pair((i16, i16)), + I16Triplet((i16, i16, i16)), + U32(u32), + U32Pair((u32, u32)), + U32Triplet((u32, u32, u32)), + I32(i32), + I32Tuple((i32, i32)), + I32Triplet((i32, i32, i32)), + F32(f32), + F32Pair((f32, f32)), + F32Triplet((f32, f32, f32)), + U64(u64), + F64(f64), +} + +#[derive(Debug, Copy, Clone)] +pub enum AuxDataHeapless { + Raw(AuxDataRaw), + Store(StoreAddr), +} + +impl From for AuxDataHeapless { + fn from(x: StoreAddr) -> Self { + Self::Store(x) + } +} + +impl From<(u32, u32)> for AuxDataRaw { + fn from(val: (u32, u32)) -> Self { + Self::U32Pair(val) + } +} + +impl From<(u32, u32)> for AuxDataHeapless { + fn from(val: (u32, u32)) -> Self { + AuxDataHeapless::Raw(val.into()) + } +} + +#[cfg(feature = "alloc")] +#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] +#[derive(Debug, Clone)] +pub enum AuxData { + Heapless(AuxDataHeapless), + Vec(Vec), + String(String), +} + +impl From for AuxData { + fn from(x: AuxDataHeapless) -> Self { + Self::Heapless(x) + } +} + +impl From> for AuxData { + fn from(val: Vec) -> Self { + Self::Vec(val) + } +} + +impl From<&[u8]> for AuxData { + fn from(val: &[u8]) -> Self { + Self::Vec(val.to_vec()) + } +} + +impl From for AuxData { + fn from(val: String) -> Self { + Self::String(val) + } +} + +impl From<&str> for AuxData { + fn from(val: &str) -> Self { + Self::String(val.to_string()) + } +}