sat-rs/satrs-example/src/pus/mod.rs

475 lines
18 KiB
Rust
Raw Normal View History

2023-07-04 15:17:43 +02:00
use crate::tmtc::MpscStoreAndSendError;
2022-11-20 19:54:14 +01:00
use satrs_core::events::EventU32;
2023-02-14 15:53:14 +01:00
use satrs_core::params::Params;
2023-07-05 11:25:23 +02:00
use satrs_core::pool::StoreAddr;
use satrs_core::pus::verification::{FailParams, StdVerifReporterWithSender};
use satrs_core::pus::AcceptedTc;
use satrs_core::spacepackets::ecss::PusServiceId;
use satrs_core::spacepackets::tc::PusTc;
use satrs_core::spacepackets::time::cds::TimeProvider;
use satrs_core::spacepackets::time::TimeWriter;
2023-07-05 14:25:51 +02:00
use satrs_core::tmtc::tm_helper::PusTmWithCdsShortHelper;
2023-07-05 11:25:23 +02:00
use satrs_example::{tmtc_err, CustomPusServiceId};
use std::sync::mpsc::Sender;
2022-08-29 01:33:32 +02:00
2023-07-05 21:08:04 +02:00
pub mod action;
2023-07-05 14:25:51 +02:00
pub mod event;
2023-07-05 21:10:45 +02:00
pub mod hk;
2023-07-04 18:51:54 +02:00
pub mod scheduler;
2023-07-03 18:22:08 +02:00
pub mod test;
2023-07-03 01:33:13 +02:00
pub struct PusTcMpscRouter {
2023-07-03 18:22:08 +02:00
pub test_service_receiver: Sender<AcceptedTc>,
pub event_service_receiver: Sender<AcceptedTc>,
pub sched_service_receiver: Sender<AcceptedTc>,
pub hk_service_receiver: Sender<AcceptedTc>,
pub action_service_receiver: Sender<AcceptedTc>,
2023-07-02 20:49:45 +02:00
}
2023-07-03 01:33:13 +02:00
2022-09-03 13:47:25 +02:00
pub struct PusReceiver {
pub tm_helper: PusTmWithCdsShortHelper,
2023-02-15 11:05:32 +01:00
pub tm_args: PusTmArgs,
2023-02-15 11:26:19 +01:00
pub tc_args: PusTcArgs,
stamp_helper: TimeStampHelper,
2022-09-03 13:47:25 +02:00
}
2023-02-04 15:35:18 +01:00
pub struct PusTmArgs {
/// All verification reporting is done with this reporter.
pub verif_reporter: StdVerifReporterWithSender,
}
2023-02-15 11:26:19 +01:00
impl PusTmArgs {
fn vr(&mut self) -> &mut StdVerifReporterWithSender {
&mut self.verif_reporter
}
}
2023-02-04 15:35:18 +01:00
pub struct PusTcArgs {
2023-07-03 01:33:13 +02:00
/// This routes all telecommands to their respective recipients
pub pus_router: PusTcMpscRouter,
/// Used to send events from within the TC router
2023-02-13 09:20:00 +01:00
pub event_sender: Sender<(EventU32, Option<Params>)>,
2023-02-04 15:35:18 +01:00
}
struct TimeStampHelper {
stamper: TimeProvider,
time_stamp: [u8; 7],
}
impl TimeStampHelper {
pub fn new() -> Self {
Self {
stamper: TimeProvider::new_with_u16_days(0, 0),
time_stamp: [0; 7],
}
}
pub fn stamp(&self) -> &[u8] {
&self.time_stamp
}
pub fn update_from_now(&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-09-03 13:47:25 +02:00
impl PusReceiver {
2023-02-04 15:35:18 +01:00
pub fn new(apid: u16, tm_arguments: PusTmArgs, tc_arguments: PusTcArgs) -> Self {
2022-09-03 13:47:25 +02:00
Self {
tm_helper: PusTmWithCdsShortHelper::new(apid),
2023-02-15 11:05:32 +01:00
tm_args: tm_arguments,
2023-02-15 11:26:19 +01:00
tc_args: tc_arguments,
stamp_helper: TimeStampHelper::new(),
2022-09-03 13:47:25 +02:00
}
}
}
2023-07-03 18:22:08 +02:00
impl PusReceiver {
pub fn handle_tc_packet(
2022-09-03 13:47:25 +02:00
&mut self,
2023-07-03 18:22:08 +02:00
store_addr: StoreAddr,
2022-09-03 13:47:25 +02:00
service: u8,
pus_tc: &PusTc,
2023-07-03 18:22:08 +02:00
) -> Result<(), MpscStoreAndSendError> {
2023-02-15 11:05:32 +01:00
let init_token = self.tm_args.verif_reporter.add_tc(pus_tc);
self.stamp_helper.update_from_now();
2023-02-15 11:19:23 +01:00
let accepted_token = self
.tm_args
2023-02-15 11:26:19 +01:00
.vr()
2023-02-15 11:19:23 +01:00
.acceptance_success(init_token, Some(self.stamp_helper.stamp()))
2022-09-10 20:18:19 +02:00
.expect("Acceptance success failure");
let service = PusServiceId::try_from(service);
match service {
Ok(standard_service) => match standard_service {
2023-07-04 18:51:54 +02:00
PusServiceId::Test => {
let res = self
.tc_args
.pus_router
.test_service_receiver
.send((store_addr, accepted_token));
match res {
Ok(_) => {}
Err(e) => {
println!("Error {e}")
}
}
}
2023-07-03 18:22:08 +02:00
PusServiceId::Housekeeping => self
.tc_args
.pus_router
.hk_service_receiver
.send((store_addr, accepted_token))
.unwrap(),
2023-07-03 01:33:13 +02:00
PusServiceId::Event => self
.tc_args
.pus_router
.event_service_receiver
2023-07-03 18:22:08 +02:00
.send((store_addr, accepted_token))
.unwrap(),
2023-07-03 01:33:13 +02:00
PusServiceId::Scheduling => self
.tc_args
.pus_router
.sched_service_receiver
2023-07-03 18:22:08 +02:00
.send((store_addr, accepted_token))
.unwrap(),
2023-02-15 11:19:23 +01:00
_ => self
.tm_args
.verif_reporter
.start_failure(
accepted_token,
FailParams::new(
2023-02-15 11:19:23 +01:00
Some(self.stamp_helper.stamp()),
&tmtc_err::PUS_SERVICE_NOT_IMPLEMENTED,
Some(&[standard_service as u8]),
),
)
.expect("Start failure verification failed"),
},
Err(e) => {
if let Ok(custom_service) = CustomPusServiceId::try_from(e.number) {
match custom_service {
CustomPusServiceId::Mode => {
2023-07-03 18:22:08 +02:00
//self.handle_mode_service(pus_tc, accepted_token)
}
CustomPusServiceId::Health => {}
}
} else {
2023-02-15 11:19:23 +01:00
self.tm_args
.verif_reporter
.start_failure(
accepted_token,
FailParams::new(
Some(self.stamp_helper.stamp()),
&tmtc_err::INVALID_PUS_SUBSERVICE,
Some(&[e.number]),
),
)
.expect("Start failure verification failed")
}
}
2022-09-03 13:47:25 +02:00
}
Ok(())
}
}
2023-07-03 18:22:08 +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> {
// let init_token = self.tm_args.verif_reporter.add_tc(pus_tc);
// self.stamp_helper.update_from_now();
// let accepted_token = self
// .tm_args
// .vr()
// .acceptance_success(init_token, Some(self.stamp_helper.stamp()))
// .expect("Acceptance success failure");
// let service = PusServiceId::try_from(service);
// match service {
// Ok(standard_service) => match standard_service {
// PusServiceId::Test => self
// .tc_args
// .pus_router
// .test_service_receiver
// .send_tc(*pus_tc),
// PusServiceId::Housekeeping => {
// self.tc_args.pus_router.hk_service_receiver.send_tc(*pus_tc)
// } //self.handle_hk_request(pus_tc, accepted_token),
// PusServiceId::Event => self
// .tc_args
// .pus_router
// .event_service_receiver
// .send_tc(*pus_tc), //self.handle_event_request(pus_tc, accepted_token),
// PusServiceId::Scheduling => self
// .tc_args
// .pus_router
// .sched_service_receiver
// .send_tc(*pus_tc), //self.handle_scheduled_tc(pus_tc, accepted_token),
// _ => self
// .tm_args
// .verif_reporter
// .start_failure(
// accepted_token,
// FailParams::new(
// Some(self.stamp_helper.stamp()),
// &tmtc_err::PUS_SERVICE_NOT_IMPLEMENTED,
// Some(&[standard_service as u8]),
// ),
// )
// .expect("Start failure verification failed"),
// },
// Err(e) => {
// if let Ok(custom_service) = CustomPusServiceId::try_from(e.number) {
// match custom_service {
// CustomPusServiceId::Mode => {
// self.handle_mode_service(pus_tc, accepted_token)
// }
// CustomPusServiceId::Health => {}
// }
// } else {
// self.tm_args
// .verif_reporter
// .start_failure(
// accepted_token,
// FailParams::new(
// Some(self.stamp_helper.stamp()),
// &tmtc_err::INVALID_PUS_SUBSERVICE,
// Some(&[e.number]),
// ),
// )
// .expect("Start failure verification failed")
// }
// }
// }
// Ok(())
// }
// }
2022-09-03 13:47:25 +02:00
2023-07-03 01:33:13 +02:00
// impl PusReceiver {
//
// fn handle_hk_request(&mut self, pus_tc: &PusTc, token: VerificationToken<TcStateAccepted>) {
// if pus_tc.user_data().is_none() {
// self.tm_args
// .verif_reporter
// .start_failure(
// token,
// FailParams::new(
// Some(self.stamp_helper.stamp()),
// &tmtc_err::NOT_ENOUGH_APP_DATA,
// None,
// ),
// )
// .expect("Sending start failure TM failed");
// return;
// }
// let user_data = pus_tc.user_data().unwrap();
// if user_data.len() < 8 {
// let err = if user_data.len() < 4 {
// &hk_err::TARGET_ID_MISSING
// } else {
// &hk_err::UNIQUE_ID_MISSING
// };
// self.tm_args
// .verif_reporter
// .start_failure(
// token,
// FailParams::new(Some(self.stamp_helper.stamp()), err, None),
// )
// .expect("Sending start failure TM failed");
// return;
// }
// let addressable_id = AddressableId::from_raw_be(user_data).unwrap();
// if !self
// .tc_args
// .request_map
// .contains_key(&addressable_id.target_id)
// {
// self.tm_args
// .verif_reporter
// .start_failure(
// token,
// FailParams::new(
// Some(self.stamp_helper.stamp()),
// &hk_err::UNKNOWN_TARGET_ID,
// None,
// ),
// )
// .expect("Sending start failure TM failed");
// return;
// }
// let send_request = |target: TargetId, request: HkRequest| {
// let sender = self
// .tc_args
// .request_map
// .get(&addressable_id.target_id)
// .unwrap();
// sender
// .send(RequestWithToken::new(
// target,
// Request::HkRequest(request),
// token,
// ))
// .unwrap_or_else(|_| panic!("Sending HK request {request:?} failed"));
// };
// if PusPacket::subservice(pus_tc) == hk::Subservice::TcEnableHkGeneration as u8 {
// send_request(
// addressable_id.target_id,
// HkRequest::Enable(addressable_id.unique_id),
// );
// } else if PusPacket::subservice(pus_tc) == hk::Subservice::TcDisableHkGeneration as u8 {
// send_request(
// addressable_id.target_id,
// HkRequest::Disable(addressable_id.unique_id),
// );
// } else if PusPacket::subservice(pus_tc) == hk::Subservice::TcGenerateOneShotHk as u8 {
// send_request(
// addressable_id.target_id,
// HkRequest::OneShot(addressable_id.unique_id),
// );
// } else if PusPacket::subservice(pus_tc)
// == hk::Subservice::TcModifyHkCollectionInterval as u8
// {
// if user_data.len() < 12 {
// self.tm_args
// .verif_reporter
// .start_failure(
// token,
// FailParams::new(
// Some(self.stamp_helper.stamp()),
// &hk_err::COLLECTION_INTERVAL_MISSING,
// None,
// ),
// )
// .expect("Sending start failure TM failed");
// return;
// }
// send_request(
// addressable_id.target_id,
// HkRequest::ModifyCollectionInterval(
// addressable_id.unique_id,
// CollectionIntervalFactor::from_be_bytes(user_data[8..12].try_into().unwrap()),
// ),
// );
// }
// }
//
//
// fn handle_mode_service(&mut self, pus_tc: &PusTc, token: VerificationToken<TcStateAccepted>) {
// let mut app_data_len = 0;
// let app_data = pus_tc.user_data();
// if app_data.is_some() {
// app_data_len = pus_tc.user_data().unwrap().len();
// }
// if app_data_len < 4 {
// self.tm_args
// .verif_reporter
// .start_failure(
// token,
// FailParams::new(
// Some(self.stamp_helper.stamp()),
// &tmtc_err::NOT_ENOUGH_APP_DATA,
// Some(format!("expected {} bytes, found {}", 4, app_data_len).as_bytes()),
// ),
// )
// .expect("Sending start failure TM failed");
// }
// let app_data = app_data.unwrap();
// let mut invalid_subservice_handler = || {
// self.tm_args
// .verif_reporter
// .start_failure(
// token,
// FailParams::new(
// Some(self.stamp_helper.stamp()),
// &tmtc_err::INVALID_PUS_SUBSERVICE,
// Some(&[PusPacket::subservice(pus_tc)]),
// ),
// )
// .expect("Sending start failure TM failed");
// };
// let subservice = mode::Subservice::try_from(PusPacket::subservice(pus_tc));
// if let Ok(subservice) = subservice {
// let forward_mode_request = |target_id, mode_request: ModeRequest| match self
// .tc_args
// .request_map
// .get(&target_id)
// {
// None => warn!("not mode request recipient for target ID {target_id} found"),
// Some(sender_to_recipient) => {
// sender_to_recipient
// .send(RequestWithToken::new(
// target_id,
// Request::ModeRequest(mode_request),
// token,
// ))
// .expect("sending mode request failed");
// }
// };
// let mut valid_subservice = true;
// match subservice {
// Subservice::TcSetMode => {
// let target_id = u32::from_be_bytes(app_data[0..4].try_into().unwrap());
// let min_len = ModeAndSubmode::raw_len() + 4;
// if app_data_len < min_len {
// self.tm_args
// .verif_reporter
// .start_failure(
// token,
// FailParams::new(
// Some(self.stamp_helper.stamp()),
// &tmtc_err::NOT_ENOUGH_APP_DATA,
// Some(
// format!("expected {min_len} bytes, found {app_data_len}")
// .as_bytes(),
// ),
// ),
// )
// .expect("Sending start failure TM failed");
// }
// // Should never fail after size check
// let mode_submode = ModeAndSubmode::from_be_bytes(
// app_data[4..4 + ModeAndSubmode::raw_len()]
// .try_into()
// .unwrap(),
// )
// .unwrap();
// forward_mode_request(target_id, ModeRequest::SetMode(mode_submode));
// }
// Subservice::TcReadMode => {
// let target_id = u32::from_be_bytes(app_data[0..4].try_into().unwrap());
// forward_mode_request(target_id, ModeRequest::ReadMode);
// }
// Subservice::TcAnnounceMode => {
// let target_id = u32::from_be_bytes(app_data[0..4].try_into().unwrap());
// forward_mode_request(target_id, ModeRequest::AnnounceMode);
// }
// Subservice::TcAnnounceModeRecursive => {
// let target_id = u32::from_be_bytes(app_data[0..4].try_into().unwrap());
// forward_mode_request(target_id, ModeRequest::AnnounceModeRecursive);
// }
// _ => {
// warn!("Can not process mode request with subservice {subservice:?}");
// invalid_subservice_handler();
// valid_subservice = false;
// }
// }
// if valid_subservice {
// self.tm_args
// .verif_reporter
// .start_success(token, Some(self.stamp_helper.stamp()))
// .expect("sending start success TM failed");
// }
// } else {
// invalid_subservice_handler();
// }
// }
// }