2024-02-23 14:19:30 +01:00
|
|
|
use std::sync::mpsc::{self};
|
2024-02-07 18:10:47 +01:00
|
|
|
|
2024-04-04 15:18:53 +02:00
|
|
|
use crate::pus::create_verification_reporter;
|
|
|
|
use satrs::event_man::{EventMessageU32, EventRoutingError};
|
|
|
|
use satrs::params::WritableToBeBytes;
|
|
|
|
use satrs::pus::event::EventTmHookProvider;
|
|
|
|
use satrs::pus::verification::VerificationReporter;
|
|
|
|
use satrs::pus::EcssTmSenderCore;
|
|
|
|
use satrs::request::UniqueApidTargetId;
|
2024-02-12 15:51:37 +01:00
|
|
|
use satrs::{
|
2024-02-07 18:10:47 +01:00
|
|
|
event_man::{
|
2024-02-23 14:19:30 +01:00
|
|
|
EventManagerWithBoundedMpsc, EventSendProvider, EventU32SenderMpscBounded,
|
|
|
|
MpscEventReceiver,
|
2024-02-07 18:10:47 +01:00
|
|
|
},
|
|
|
|
pus::{
|
|
|
|
event_man::{
|
2024-02-23 14:19:30 +01:00
|
|
|
DefaultPusEventU32Dispatcher, EventReporter, EventRequest, EventRequestWithToken,
|
2024-02-07 18:10:47 +01:00
|
|
|
},
|
2024-02-26 11:41:42 +01:00
|
|
|
verification::{TcStateStarted, VerificationReportingProvider, VerificationToken},
|
2024-02-07 18:10:47 +01:00
|
|
|
},
|
2024-04-04 15:18:53 +02:00
|
|
|
spacepackets::time::cds::CdsTime,
|
2024-02-07 18:10:47 +01:00
|
|
|
};
|
2024-04-04 15:18:53 +02:00
|
|
|
use satrs_example::config::components::PUS_EVENT_MANAGEMENT;
|
2024-02-07 18:10:47 +01:00
|
|
|
|
|
|
|
use crate::update_time;
|
|
|
|
|
2024-04-04 15:18:53 +02:00
|
|
|
// This helper sets the APID of the event sender for the PUS telemetry.
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct EventApidSetter {
|
|
|
|
pub next_apid: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EventTmHookProvider for EventApidSetter {
|
|
|
|
fn modify_tm(&self, tm: &mut satrs::spacepackets::ecss::tm::PusTmCreator) {
|
|
|
|
tm.set_apid(self.next_apid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The PUS event handler subscribes for all events and converts them into ECSS PUS 5 event
|
|
|
|
/// packets. It also handles the verification completion of PUS event service requests.
|
|
|
|
pub struct PusEventHandler<TmSender: EcssTmSenderCore> {
|
2024-02-07 18:10:47 +01:00
|
|
|
event_request_rx: mpsc::Receiver<EventRequestWithToken>,
|
2024-02-23 14:19:30 +01:00
|
|
|
pus_event_dispatcher: DefaultPusEventU32Dispatcher<()>,
|
2024-04-04 15:18:53 +02:00
|
|
|
pus_event_man_rx: mpsc::Receiver<EventMessageU32>,
|
|
|
|
tm_sender: TmSender,
|
2024-03-29 16:22:40 +01:00
|
|
|
time_provider: CdsTime,
|
2024-02-07 18:10:47 +01:00
|
|
|
timestamp: [u8; 7],
|
2024-02-26 11:41:42 +01:00
|
|
|
verif_handler: VerificationReporter,
|
2024-04-04 15:18:53 +02:00
|
|
|
event_apid_setter: EventApidSetter,
|
2024-02-07 18:10:47 +01:00
|
|
|
}
|
|
|
|
|
2024-04-04 15:18:53 +02:00
|
|
|
impl<TmSender: EcssTmSenderCore> PusEventHandler<TmSender> {
|
2024-02-07 18:10:47 +01:00
|
|
|
pub fn new(
|
2024-04-04 15:18:53 +02:00
|
|
|
tm_sender: TmSender,
|
2024-02-26 11:41:42 +01:00
|
|
|
verif_handler: VerificationReporter,
|
2024-02-23 14:19:30 +01:00
|
|
|
event_manager: &mut EventManagerWithBoundedMpsc,
|
2024-02-07 18:10:47 +01:00
|
|
|
event_request_rx: mpsc::Receiver<EventRequestWithToken>,
|
|
|
|
) -> Self {
|
2024-02-23 14:19:30 +01:00
|
|
|
let event_queue_cap = 30;
|
|
|
|
let (pus_event_man_tx, pus_event_man_rx) = mpsc::sync_channel(event_queue_cap);
|
2024-02-07 18:10:47 +01:00
|
|
|
|
|
|
|
// All events sent to the manager are routed to the PUS event manager, which generates PUS event
|
|
|
|
// telemetry for each event.
|
2024-04-04 15:18:53 +02:00
|
|
|
let event_reporter = EventReporter::new(PUS_EVENT_MANAGEMENT.raw(), 0, 0, 128).unwrap();
|
2024-02-07 18:10:47 +01:00
|
|
|
let pus_event_dispatcher =
|
2024-02-23 14:19:30 +01:00
|
|
|
DefaultPusEventU32Dispatcher::new_with_default_backend(event_reporter);
|
2024-04-04 15:18:53 +02:00
|
|
|
let pus_event_man_send_provider = EventU32SenderMpscBounded::new(
|
|
|
|
PUS_EVENT_MANAGEMENT.raw(),
|
|
|
|
pus_event_man_tx,
|
|
|
|
event_queue_cap,
|
|
|
|
);
|
2024-02-07 18:10:47 +01:00
|
|
|
|
2024-04-04 15:18:53 +02:00
|
|
|
event_manager.subscribe_all(pus_event_man_send_provider.target_id());
|
2024-02-07 18:10:47 +01:00
|
|
|
event_manager.add_sender(pus_event_man_send_provider);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
event_request_rx,
|
|
|
|
pus_event_dispatcher,
|
|
|
|
pus_event_man_rx,
|
2024-04-04 15:18:53 +02:00
|
|
|
time_provider: CdsTime::new_with_u16_days(0, 0),
|
2024-02-07 18:10:47 +01:00
|
|
|
timestamp: [0; 7],
|
|
|
|
verif_handler,
|
2024-04-04 15:18:53 +02:00
|
|
|
tm_sender,
|
|
|
|
event_apid_setter: EventApidSetter::default(),
|
2024-02-07 18:10:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_event_requests(&mut self) {
|
|
|
|
let report_completion = |event_req: EventRequestWithToken, timestamp: &[u8]| {
|
|
|
|
let started_token: VerificationToken<TcStateStarted> = event_req
|
|
|
|
.token
|
|
|
|
.try_into()
|
|
|
|
.expect("expected start verification token");
|
|
|
|
self.verif_handler
|
2024-04-04 15:18:53 +02:00
|
|
|
.completion_success(&self.tm_sender, started_token, timestamp)
|
2024-02-07 18:10:47 +01:00
|
|
|
.expect("Sending completion success failed");
|
|
|
|
};
|
|
|
|
// handle event requests
|
|
|
|
if let Ok(event_req) = self.event_request_rx.try_recv() {
|
|
|
|
match event_req.request {
|
|
|
|
EventRequest::Enable(event) => {
|
|
|
|
self.pus_event_dispatcher
|
|
|
|
.enable_tm_for_event(&event)
|
|
|
|
.expect("Enabling TM failed");
|
|
|
|
update_time(&mut self.time_provider, &mut self.timestamp);
|
|
|
|
report_completion(event_req, &self.timestamp);
|
|
|
|
}
|
|
|
|
EventRequest::Disable(event) => {
|
|
|
|
self.pus_event_dispatcher
|
|
|
|
.disable_tm_for_event(&event)
|
|
|
|
.expect("Disabling TM failed");
|
|
|
|
update_time(&mut self.time_provider, &mut self.timestamp);
|
|
|
|
report_completion(event_req, &self.timestamp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_pus_event_tm(&mut self) {
|
|
|
|
// Perform the generation of PUS event packets
|
2024-04-04 15:18:53 +02:00
|
|
|
if let Ok(event_msg) = self.pus_event_man_rx.try_recv() {
|
2024-02-07 18:10:47 +01:00
|
|
|
update_time(&mut self.time_provider, &mut self.timestamp);
|
2024-04-04 15:18:53 +02:00
|
|
|
let param_vec = event_msg.params().map_or(Vec::new(), |param| {
|
|
|
|
param.to_vec().expect("failed to convert params to vec")
|
|
|
|
});
|
|
|
|
self.event_apid_setter.next_apid = UniqueApidTargetId::from(event_msg.sender_id()).apid;
|
2024-02-07 18:10:47 +01:00
|
|
|
self.pus_event_dispatcher
|
|
|
|
.generate_pus_event_tm_generic(
|
2024-04-04 15:18:53 +02:00
|
|
|
&self.tm_sender,
|
2024-02-07 18:10:47 +01:00
|
|
|
&self.timestamp,
|
2024-04-04 15:18:53 +02:00
|
|
|
event_msg.event(),
|
|
|
|
Some(¶m_vec),
|
2024-02-07 18:10:47 +01:00
|
|
|
)
|
|
|
|
.expect("Sending TM as event failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 15:18:53 +02:00
|
|
|
/// This is a thin wrapper around the event manager which also caches the sender component
|
|
|
|
/// used to send events to the event manager.
|
2024-02-07 18:10:47 +01:00
|
|
|
pub struct EventManagerWrapper {
|
2024-02-23 14:19:30 +01:00
|
|
|
event_manager: EventManagerWithBoundedMpsc,
|
2024-04-04 15:18:53 +02:00
|
|
|
event_sender: mpsc::Sender<EventMessageU32>,
|
2024-02-07 18:10:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EventManagerWrapper {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
// The sender handle is the primary sender handle for all components which want to create events.
|
|
|
|
// The event manager will receive the RX handle to receive all the events.
|
|
|
|
let (event_sender, event_man_rx) = mpsc::channel();
|
2024-04-04 15:18:53 +02:00
|
|
|
let event_recv = MpscEventReceiver::new(event_man_rx);
|
2024-02-07 18:10:47 +01:00
|
|
|
Self {
|
2024-02-23 14:19:30 +01:00
|
|
|
event_manager: EventManagerWithBoundedMpsc::new(event_recv),
|
2024-02-07 18:10:47 +01:00
|
|
|
event_sender,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 15:18:53 +02:00
|
|
|
// Returns a cached event sender to send events to the event manager for routing.
|
|
|
|
pub fn clone_event_sender(&self) -> mpsc::Sender<EventMessageU32> {
|
2024-02-07 18:10:47 +01:00
|
|
|
self.event_sender.clone()
|
|
|
|
}
|
|
|
|
|
2024-02-23 14:19:30 +01:00
|
|
|
pub fn event_manager(&mut self) -> &mut EventManagerWithBoundedMpsc {
|
2024-02-07 18:10:47 +01:00
|
|
|
&mut self.event_manager
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn try_event_routing(&mut self) {
|
2024-04-04 15:18:53 +02:00
|
|
|
let error_handler = |event_msg: &EventMessageU32, error: EventRoutingError| {
|
|
|
|
self.routing_error_handler(event_msg, error)
|
|
|
|
};
|
2024-02-07 18:10:47 +01:00
|
|
|
// Perform the event routing.
|
2024-04-04 15:18:53 +02:00
|
|
|
self.event_manager.try_event_handling(error_handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn routing_error_handler(&self, event_msg: &EventMessageU32, error: EventRoutingError) {
|
|
|
|
log::warn!("event routing error for event {event_msg:?}: {error:?}");
|
2024-02-07 18:10:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 15:18:53 +02:00
|
|
|
pub struct EventHandler<TmSender: EcssTmSenderCore> {
|
2024-02-07 18:10:47 +01:00
|
|
|
pub event_man_wrapper: EventManagerWrapper,
|
2024-04-04 15:18:53 +02:00
|
|
|
pub pus_event_handler: PusEventHandler<TmSender>,
|
2024-02-07 18:10:47 +01:00
|
|
|
}
|
|
|
|
|
2024-04-04 15:18:53 +02:00
|
|
|
impl<TmSender: EcssTmSenderCore> EventHandler<TmSender> {
|
2024-02-07 18:10:47 +01:00
|
|
|
pub fn new(
|
2024-04-04 15:18:53 +02:00
|
|
|
tm_sender: TmSender,
|
2024-02-07 18:10:47 +01:00
|
|
|
event_request_rx: mpsc::Receiver<EventRequestWithToken>,
|
|
|
|
) -> Self {
|
|
|
|
let mut event_man_wrapper = EventManagerWrapper::new();
|
|
|
|
let pus_event_handler = PusEventHandler::new(
|
2024-04-04 15:18:53 +02:00
|
|
|
tm_sender,
|
|
|
|
create_verification_reporter(PUS_EVENT_MANAGEMENT.id(), PUS_EVENT_MANAGEMENT.apid),
|
2024-02-07 18:10:47 +01:00
|
|
|
event_man_wrapper.event_manager(),
|
|
|
|
event_request_rx,
|
|
|
|
);
|
|
|
|
Self {
|
|
|
|
event_man_wrapper,
|
|
|
|
pus_event_handler,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 15:18:53 +02:00
|
|
|
pub fn clone_event_sender(&self) -> mpsc::Sender<EventMessageU32> {
|
2024-02-07 18:10:47 +01:00
|
|
|
self.event_man_wrapper.clone_event_sender()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2024-02-23 14:19:30 +01:00
|
|
|
pub fn event_manager(&mut self) -> &mut EventManagerWithBoundedMpsc {
|
2024-02-07 18:10:47 +01:00
|
|
|
self.event_man_wrapper.event_manager()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn periodic_operation(&mut self) {
|
|
|
|
self.pus_event_handler.handle_event_requests();
|
|
|
|
self.event_man_wrapper.try_event_routing();
|
|
|
|
self.pus_event_handler.generate_pus_event_tm();
|
|
|
|
}
|
|
|
|
}
|