diff --git a/satrs-core/src/hk.rs b/satrs-core/src/hk.rs new file mode 100644 index 0000000..727e887 --- /dev/null +++ b/satrs-core/src/hk.rs @@ -0,0 +1,11 @@ +use crate::tmtc::AddressableId; + +pub type CollectionIntervalFactor = u32; + +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum HkRequest { + OneShot(AddressableId), + Enable(AddressableId), + Disable(AddressableId), + ModifyCollectionInterval(AddressableId, CollectionIntervalFactor), +} diff --git a/satrs-core/src/lib.rs b/satrs-core/src/lib.rs index 39d0bca..d1b25d2 100644 --- a/satrs-core/src/lib.rs +++ b/satrs-core/src/lib.rs @@ -25,6 +25,7 @@ pub mod events; #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] pub mod executable; pub mod hal; +pub mod hk; pub mod objects; pub mod params; #[cfg(feature = "alloc")] @@ -32,6 +33,7 @@ pub mod params; pub mod pool; pub mod power; pub mod pus; +pub mod request; pub mod res_code; pub mod seq_count; pub mod tmtc; diff --git a/satrs-core/src/mode.rs b/satrs-core/src/mode.rs new file mode 100644 index 0000000..e69de29 diff --git a/satrs-core/src/power.rs b/satrs-core/src/power.rs index 0875b87..82063a3 100644 --- a/satrs-core/src/power.rs +++ b/satrs-core/src/power.rs @@ -27,7 +27,7 @@ pub enum SwitchState { pub type SwitchId = u16; /// Generic trait for a device capable of turning on and off switches. -pub trait PowerSwitcher { +pub trait PowerSwitcherCommandSender { type Error; fn send_switch_on_cmd(&mut self, switch_id: SwitchId) -> Result<(), Self::Error>; @@ -45,12 +45,16 @@ pub trait PowerSwitcher { ) -> Result<(), ::Error> { switch.switch_off() } +} + +pub trait PowerSwitchInfo { + type Error; /// Retrieve the switch state - fn get_switch_state(&mut self, switch_id: SwitchId) -> SwitchState; + fn get_switch_state(&mut self, switch_id: SwitchId) -> Result; - fn get_is_switch_on(&mut self, switch_id: SwitchId) -> bool { - self.get_switch_state(switch_id) == SwitchState::On + fn get_is_switch_on(&mut self, switch_id: SwitchId) -> Result { + Ok(self.get_switch_state(switch_id)? == SwitchState::On) } /// The maximum delay it will take to change a switch. @@ -59,3 +63,54 @@ pub trait PowerSwitcher { /// see the switch changed. fn switch_delay_ms(&self) -> u32; } + +pub trait PowerSwitchProvider: PowerSwitcherCommandSender + PowerSwitchInfo { + type Error; +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::power::PowerSwitcherCommandSender; + use std::boxed::Box; + + struct Pcdu { + switch_rx: std::sync::mpsc::Receiver<(SwitchId, u16)>, + } + enum DeviceState { + OFF, + SwitchingPower, + ON, + SETUP, + IDLE, + } + struct MyComplexDevice { + power_switcher: Box>, + switch_id: SwitchId, + some_state: u16, + dev_state: DeviceState, + mode: u32, + submode: u16, + } + + impl MyComplexDevice { + pub fn periodic_op(&mut self) { + // .. mode command coming in + let mode = 1; + if mode == 1 { + if self.dev_state == DeviceState::OFF { + self.power_switcher + .send_switch_on_cmd(self.switch_id) + .expect("sending siwthc cmd failed"); + self.dev_state = DeviceState::SwitchingPower; + } + if self.dev_state == DeviceState::SwitchingPower { + if self.power_switcher.get_is_switch_on() { + self.dev_state = DeviceState::ON; + self.mode = 1; + } + } + } + } + } +} diff --git a/satrs-core/src/request.rs b/satrs-core/src/request.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/satrs-core/src/request.rs @@ -0,0 +1 @@ + diff --git a/satrs-example/src/hk.rs b/satrs-example/src/hk.rs index d27c0f0..2e4727a 100644 --- a/satrs-example/src/hk.rs +++ b/satrs-example/src/hk.rs @@ -1,16 +1,4 @@ -use satrs_core::tmtc::AddressableId; - -pub type CollectionIntervalFactor = u32; - #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum AcsHkIds { TestMgmSet = 1, } - -#[derive(Debug, Copy, Clone, PartialEq, Eq)] -pub enum HkRequest { - OneShot(AddressableId), - Enable(AddressableId), - Disable(AddressableId), - ModifyCollectionInterval(AddressableId, CollectionIntervalFactor), -} diff --git a/satrs-example/src/lib.rs b/satrs-example/src/lib.rs index 83a095b..e28ece5 100644 --- a/satrs-example/src/lib.rs +++ b/satrs-example/src/lib.rs @@ -1,3 +1,4 @@ +use satrs_core::events::{EventU32TypedSev, SeverityInfo}; use std::net::Ipv4Addr; use satrs_mib::res_code::{ResultU16, ResultU16Info}; @@ -17,6 +18,9 @@ pub enum GroupId { pub const OBSW_SERVER_ADDR: Ipv4Addr = Ipv4Addr::UNSPECIFIED; pub const SERVER_PORT: u16 = 7301; +pub const TEST_EVENT: EventU32TypedSev = + EventU32TypedSev::::const_new(0, 0); + pub mod tmtc_err { use super::*; diff --git a/satrs-example/src/main.rs b/satrs-example/src/main.rs index dfe78aa..a070109 100644 --- a/satrs-example/src/main.rs +++ b/satrs-example/src/main.rs @@ -4,7 +4,7 @@ mod pus; mod requests; mod tmtc; -use crate::hk::{AcsHkIds, HkRequest}; +use crate::hk::AcsHkIds; use crate::requests::{Request, RequestWithToken}; use crate::tmtc::{ core_tmtc_task, OtherArgs, PusTcSource, TcArgs, TcStore, TmArgs, TmFunnel, TmStore, PUS_APID, @@ -13,6 +13,7 @@ use satrs_core::event_man::{ EventManagerWithMpscQueue, MpscEventReceiver, MpscEventU32SendProvider, SendEventProvider, }; use satrs_core::events::EventU32; +use satrs_core::hk::HkRequest; use satrs_core::pool::{LocalPool, PoolCfg, StoreAddr}; use satrs_core::pus::event_man::{ DefaultPusMgmtBackendProvider, EventReporter, EventRequest, EventRequestWithToken, diff --git a/satrs-example/src/pus.rs b/satrs-example/src/pus.rs index 68774fa..fb228bb 100644 --- a/satrs-example/src/pus.rs +++ b/satrs-example/src/pus.rs @@ -1,7 +1,8 @@ -use crate::hk::{CollectionIntervalFactor, HkRequest}; use crate::requests::{Request, RequestWithToken}; use crate::tmtc::{PusTcSource, TmStore}; use satrs_core::events::EventU32; +use satrs_core::hk::{CollectionIntervalFactor, HkRequest}; +use satrs_core::params::Params; use satrs_core::pool::StoreAddr; use satrs_core::pus::event; use satrs_core::pus::event_man::{EventRequest, EventRequestWithToken}; @@ -17,7 +18,7 @@ use satrs_core::{ spacepackets::ecss::PusPacket, spacepackets::tc::PusTc, spacepackets::time::cds::TimeProvider, spacepackets::time::TimeWriter, spacepackets::SpHeader, }; -use satrs_example::{hk_err, tmtc_err}; +use satrs_example::{hk_err, tmtc_err, TEST_EVENT}; use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; @@ -31,6 +32,7 @@ pub struct PusReceiver { #[allow(dead_code)] tc_source: PusTcSource, event_request_tx: Sender, + event_sender: Sender<(EventU32, Option)>, request_map: HashMap>, stamper: TimeProvider, time_stamp: [u8; 7], @@ -52,6 +54,7 @@ pub struct PusTcArgs { pub request_map: HashMap>, /// Required for scheduling of telecommands. pub tc_source: PusTcSource, + pub event_sender: Sender<(EventU32, Option)>, pub scheduler: Rc>, } @@ -64,6 +67,7 @@ impl PusReceiver { verif_reporter: tm_arguments.verif_reporter, tc_source: tc_arguments.tc_source, event_request_tx: tc_arguments.event_request_tx, + event_sender: tc_arguments.event_sender, request_map: tc_arguments.request_map, stamper: TimeProvider::new_with_u16_days(0, 0), time_stamp: [0; 7], @@ -110,33 +114,49 @@ impl PusServiceProvider for PusReceiver { impl PusReceiver { fn handle_test_service(&mut self, pus_tc: &PusTc, token: VerificationToken) { - if PusPacket::subservice(pus_tc) == 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); - let addr = self.tm_store.add_pus_tm(&ping_reply); - let start_token = self - .verif_reporter - .start_success(token, Some(&self.time_stamp)) - .expect("Error sending start success"); - self.tm_tx - .send(addr) - .expect("Sending TM to TM funnel failed"); - self.verif_reporter - .completion_success(start_token, Some(&self.time_stamp)) - .expect("Error sending completion success"); - } else { - self.update_time_stamp(); - self.verif_reporter - .start_failure( - token, - FailParams::new( - Some(&self.time_stamp), - &tmtc_err::INVALID_PUS_SUBSERVICE, - None, - ), - ) - .expect("Sending start failure TM failed"); + match PusPacket::subservice(pus_tc) { + 1 => { + println!("Received PUS ping command TC[17,1]"); + println!("Sending ping reply PUS TM[17,2]"); + let start_token = self + .verif_reporter + .start_success(token, Some(&self.time_stamp)) + .expect("Error sending start success"); + let ping_reply = self.tm_helper.create_pus_tm_timestamp_now(17, 2, None); + let addr = self.tm_store.add_pus_tm(&ping_reply); + self.tm_tx + .send(addr) + .expect("Sending TM to TM funnel failed"); + self.verif_reporter + .completion_success(start_token, Some(&self.time_stamp)) + .expect("Error sending completion success"); + } + 128 => { + self.update_time_stamp(); + self.event_sender + .send((TEST_EVENT.into(), None)) + .expect("Sending test event failed"); + let start_token = self + .verif_reporter + .start_success(token, Some(&self.time_stamp)) + .expect("Error sending start success"); + self.verif_reporter + .completion_success(start_token, Some(&self.time_stamp)) + .expect("Error sending completion success"); + } + _ => { + self.update_time_stamp(); + self.verif_reporter + .start_failure( + token, + FailParams::new( + Some(&self.time_stamp), + &tmtc_err::INVALID_PUS_SUBSERVICE, + None, + ), + ) + .expect("Sending start failure TM failed"); + } } } diff --git a/satrs-example/src/requests.rs b/satrs-example/src/requests.rs index cac2277..efb5732 100644 --- a/satrs-example/src/requests.rs +++ b/satrs-example/src/requests.rs @@ -1,4 +1,4 @@ -use crate::hk::HkRequest; +use satrs_core::hk::HkRequest; use satrs_core::pus::verification::{TcStateAccepted, VerificationToken}; #[derive(Copy, Clone, Eq, PartialEq, Debug)] diff --git a/satrs-example/src/tmtc.rs b/satrs-example/src/tmtc.rs index 753d1a4..81ad712 100644 --- a/satrs-example/src/tmtc.rs +++ b/satrs-example/src/tmtc.rs @@ -177,6 +177,7 @@ pub fn core_tmtc_task(args: OtherArgs, mut tc_args: TcArgs, tm_args: TmArgs) { event_request_tx: args.event_request_tx, request_map: args.request_map, tc_source: tc_args.tc_source.clone(), + event_sender: args.event_sender, scheduler: sched_clone, }; let mut pus_receiver = PusReceiver::new(PUS_APID, pus_tm_args, pus_tc_args);