2022-12-19 17:03:26 +01:00
|
|
|
use crate::requests::Request;
|
2022-09-10 13:34:04 +02:00
|
|
|
use crate::tmtc::TmStore;
|
2022-11-20 19:54:14 +01:00
|
|
|
use satrs_core::events::EventU32;
|
|
|
|
use satrs_core::pool::StoreAddr;
|
|
|
|
use satrs_core::pus::event::Subservices;
|
|
|
|
use satrs_core::pus::event_man::{EventRequest, EventRequestWithToken};
|
|
|
|
use satrs_core::pus::verification::{
|
2022-11-27 01:19:34 +01:00
|
|
|
FailParams, StdVerifReporterWithSender, TcStateAccepted, VerificationToken,
|
2022-09-11 14:51:25 +02:00
|
|
|
};
|
2022-11-27 22:05:42 +01:00
|
|
|
use satrs_core::res_code::ResultU16;
|
2022-11-20 19:54:14 +01:00
|
|
|
use satrs_core::tmtc::tm_helper::PusTmWithCdsShortHelper;
|
|
|
|
use satrs_core::tmtc::PusServiceProvider;
|
2022-11-27 22:05:42 +01:00
|
|
|
use satrs_example::{INVALID_PUS_SERVICE, INVALID_PUS_SUBSERVICE, NOT_ENOUGH_APP_DATA};
|
2022-11-27 19:24:18 +01:00
|
|
|
use spacepackets::ecss::PusPacket;
|
2022-11-19 12:16:51 +01:00
|
|
|
use spacepackets::tc::PusTc;
|
2022-12-08 15:26:49 +01:00
|
|
|
use spacepackets::time::cds::TimeProvider;
|
|
|
|
use spacepackets::time::TimeWriter;
|
2022-09-03 13:47:25 +02:00
|
|
|
use spacepackets::SpHeader;
|
2022-12-19 17:03:26 +01:00
|
|
|
use std::collections::HashMap;
|
2022-09-10 13:34:04 +02:00
|
|
|
use std::sync::mpsc;
|
2022-12-19 17:03:26 +01:00
|
|
|
use std::sync::mpsc::Sender;
|
2022-08-29 01:33:32 +02:00
|
|
|
|
2022-09-03 13:47:25 +02:00
|
|
|
pub struct PusReceiver {
|
|
|
|
pub tm_helper: PusTmWithCdsShortHelper,
|
2022-12-19 17:03:26 +01:00
|
|
|
pub tm_tx: Sender<StoreAddr>,
|
2022-09-10 13:34:04 +02:00
|
|
|
pub tm_store: TmStore,
|
2022-11-27 01:19:34 +01:00
|
|
|
pub verif_reporter: StdVerifReporterWithSender,
|
2022-12-19 17:03:26 +01:00
|
|
|
event_request_tx: Sender<EventRequestWithToken>,
|
|
|
|
request_map: HashMap<u32, Sender<Request>>,
|
2022-12-08 15:26:49 +01:00
|
|
|
stamper: TimeProvider,
|
2022-09-10 20:18:19 +02:00
|
|
|
time_stamp: [u8; 7],
|
2022-09-03 13:47:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PusReceiver {
|
2022-09-10 13:34:04 +02:00
|
|
|
pub fn new(
|
|
|
|
apid: u16,
|
2022-12-19 17:03:26 +01:00
|
|
|
tm_tx: Sender<StoreAddr>,
|
2022-09-10 13:34:04 +02:00
|
|
|
tm_store: TmStore,
|
2022-11-27 01:19:34 +01:00
|
|
|
verif_reporter: StdVerifReporterWithSender,
|
2022-12-19 17:03:26 +01:00
|
|
|
event_request_tx: Sender<EventRequestWithToken>,
|
|
|
|
request_map: HashMap<u32, Sender<Request>>,
|
2022-09-10 13:34:04 +02:00
|
|
|
) -> Self {
|
2022-09-03 13:47:25 +02:00
|
|
|
Self {
|
|
|
|
tm_helper: PusTmWithCdsShortHelper::new(apid),
|
|
|
|
tm_tx,
|
|
|
|
tm_store,
|
2022-09-10 13:34:04 +02:00
|
|
|
verif_reporter,
|
2022-11-19 12:16:51 +01:00
|
|
|
event_request_tx,
|
2022-12-19 17:03:26 +01:00
|
|
|
request_map,
|
2022-12-08 15:26:49 +01:00
|
|
|
stamper: TimeProvider::default(),
|
2022-09-10 20:18:19 +02:00
|
|
|
time_stamp: [0; 7],
|
2022-09-03 13:47:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PusServiceProvider for PusReceiver {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn handle_pus_tc_packet(
|
|
|
|
&mut self,
|
|
|
|
service: u8,
|
|
|
|
_header: &SpHeader,
|
|
|
|
pus_tc: &PusTc,
|
|
|
|
) -> Result<(), Self::Error> {
|
2022-11-27 01:19:34 +01:00
|
|
|
let init_token = self.verif_reporter.add_tc(pus_tc);
|
2022-11-27 14:30:29 +01:00
|
|
|
self.update_time_stamp();
|
2022-11-27 01:19:34 +01:00
|
|
|
let accepted_token = self
|
|
|
|
.verif_reporter
|
2022-09-10 20:18:19 +02:00
|
|
|
.acceptance_success(init_token, &self.time_stamp)
|
|
|
|
.expect("Acceptance success failure");
|
2022-09-03 13:47:25 +02:00
|
|
|
if service == 17 {
|
2022-09-11 14:51:25 +02:00
|
|
|
self.handle_test_service(pus_tc, accepted_token);
|
2022-11-12 23:13:55 +01:00
|
|
|
} else if service == 5 {
|
2022-12-19 17:03:26 +01:00
|
|
|
self.handle_event_request(pus_tc, accepted_token);
|
|
|
|
} else if service == 3 {
|
|
|
|
self.handle_hk_request(pus_tc, accepted_token);
|
2022-11-27 01:19:34 +01:00
|
|
|
} else {
|
2022-11-27 19:24:18 +01:00
|
|
|
self.update_time_stamp();
|
2022-11-27 22:05:42 +01:00
|
|
|
self.verif_reporter
|
|
|
|
.start_failure(
|
|
|
|
accepted_token,
|
|
|
|
FailParams::new(&self.time_stamp, &INVALID_PUS_SERVICE, None),
|
|
|
|
)
|
2022-11-27 19:24:18 +01:00
|
|
|
.expect("Start failure verification failed")
|
2022-09-03 13:47:25 +02:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PusReceiver {
|
2022-11-13 21:07:16 +01:00
|
|
|
fn handle_test_service(&mut self, pus_tc: &PusTc, token: VerificationToken<TcStateAccepted>) {
|
2022-09-03 13:47:25 +02:00
|
|
|
if pus_tc.subservice() == 1 {
|
|
|
|
println!("Received PUS ping command TC[17,1]");
|
|
|
|
println!("Sending ping reply PUS TM[17,2]");
|
|
|
|
let ping_reply = self.tm_helper.create_pus_tm_timestamp_now(17, 2, None);
|
2022-09-10 13:34:04 +02:00
|
|
|
let addr = self.tm_store.add_pus_tm(&ping_reply);
|
2022-11-27 01:19:34 +01:00
|
|
|
let start_token = self
|
2022-09-11 14:51:25 +02:00
|
|
|
.verif_reporter
|
|
|
|
.start_success(token, &self.time_stamp)
|
|
|
|
.expect("Error sending start success");
|
2022-09-03 13:47:25 +02:00
|
|
|
self.tm_tx
|
|
|
|
.send(addr)
|
|
|
|
.expect("Sending TM to TM funnel failed");
|
2022-11-27 01:19:34 +01:00
|
|
|
self.verif_reporter
|
2022-09-11 14:51:25 +02:00
|
|
|
.completion_success(start_token, &self.time_stamp)
|
|
|
|
.expect("Error sending completion success");
|
2022-11-27 01:19:34 +01:00
|
|
|
} else {
|
|
|
|
self.update_time_stamp();
|
|
|
|
self.verif_reporter
|
|
|
|
.start_failure(
|
|
|
|
token,
|
2022-11-27 19:24:18 +01:00
|
|
|
FailParams::new(&self.time_stamp, &INVALID_PUS_SUBSERVICE, None),
|
2022-11-27 01:19:34 +01:00
|
|
|
)
|
|
|
|
.expect("Sending start failure TM failed");
|
2022-09-03 13:47:25 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-19 12:16:51 +01:00
|
|
|
|
|
|
|
fn update_time_stamp(&mut self) {
|
|
|
|
self.stamper
|
|
|
|
.update_from_now()
|
|
|
|
.expect("Updating timestamp failed");
|
|
|
|
self.stamper
|
|
|
|
.write_to_bytes(&mut self.time_stamp)
|
|
|
|
.expect("Writing timestamp failed");
|
|
|
|
}
|
|
|
|
|
2022-12-19 17:03:26 +01:00
|
|
|
fn handle_hk_request(&mut self, pus_tc: &PusTc, token: VerificationToken<TcStateAccepted>) {}
|
|
|
|
fn handle_event_request(&mut self, pus_tc: &PusTc, token: VerificationToken<TcStateAccepted>) {
|
2022-11-27 01:19:34 +01:00
|
|
|
let send_start_failure = |verif_reporter: &mut StdVerifReporterWithSender,
|
2022-11-19 12:16:51 +01:00
|
|
|
timestamp: &[u8; 7],
|
2022-11-27 19:24:18 +01:00
|
|
|
failure_code: &ResultU16,
|
2022-11-19 12:16:51 +01:00
|
|
|
failure_data: Option<&[u8]>| {
|
|
|
|
verif_reporter
|
|
|
|
.start_failure(
|
|
|
|
token,
|
2022-11-27 19:24:18 +01:00
|
|
|
FailParams::new(timestamp, failure_code, failure_data),
|
2022-11-19 12:16:51 +01:00
|
|
|
)
|
|
|
|
.expect("Sending start failure TM failed");
|
|
|
|
};
|
2022-11-27 01:19:34 +01:00
|
|
|
let send_start_acceptance = |verif_reporter: &mut StdVerifReporterWithSender,
|
2022-11-19 12:16:51 +01:00
|
|
|
timestamp: &[u8; 7]| {
|
|
|
|
verif_reporter
|
|
|
|
.start_success(token, timestamp)
|
|
|
|
.expect("Sending start success TM failed")
|
|
|
|
};
|
|
|
|
if pus_tc.user_data().is_none() {
|
|
|
|
self.update_time_stamp();
|
|
|
|
send_start_failure(
|
|
|
|
&mut self.verif_reporter,
|
|
|
|
&self.time_stamp,
|
2022-11-27 19:24:18 +01:00
|
|
|
&NOT_ENOUGH_APP_DATA,
|
2022-11-19 12:16:51 +01:00
|
|
|
None,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let app_data = pus_tc.user_data().unwrap();
|
|
|
|
if app_data.len() < 4 {
|
|
|
|
self.update_time_stamp();
|
|
|
|
send_start_failure(
|
|
|
|
&mut self.verif_reporter,
|
|
|
|
&self.time_stamp,
|
2022-11-27 19:24:18 +01:00
|
|
|
&NOT_ENOUGH_APP_DATA,
|
2022-11-19 12:16:51 +01:00
|
|
|
None,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let event_id = EventU32::from(u32::from_be_bytes(app_data.try_into().unwrap()));
|
|
|
|
match PusPacket::subservice(pus_tc).try_into() {
|
|
|
|
Ok(Subservices::TcEnableEventGeneration) => {
|
|
|
|
self.update_time_stamp();
|
|
|
|
let start_token = send_start_acceptance(&mut self.verif_reporter, &self.time_stamp);
|
|
|
|
self.event_request_tx
|
|
|
|
.send(EventRequestWithToken {
|
|
|
|
request: EventRequest::Enable(event_id),
|
|
|
|
token: start_token,
|
|
|
|
})
|
|
|
|
.expect("Sending event request failed");
|
|
|
|
}
|
|
|
|
Ok(Subservices::TcDisableEventGeneration) => {
|
|
|
|
self.update_time_stamp();
|
|
|
|
let start_token = send_start_acceptance(&mut self.verif_reporter, &self.time_stamp);
|
|
|
|
self.event_request_tx
|
|
|
|
.send(EventRequestWithToken {
|
|
|
|
request: EventRequest::Disable(event_id),
|
|
|
|
token: start_token,
|
|
|
|
})
|
|
|
|
.expect("Sending event request failed");
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.update_time_stamp();
|
|
|
|
send_start_failure(
|
|
|
|
&mut self.verif_reporter,
|
|
|
|
&self.time_stamp,
|
2022-11-27 19:24:18 +01:00
|
|
|
&INVALID_PUS_SUBSERVICE,
|
2022-11-19 12:16:51 +01:00
|
|
|
None,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-03 13:47:25 +02:00
|
|
|
}
|