2022-09-03 13:47:25 +02:00
|
|
|
mod ccsds;
|
2022-12-19 17:03:26 +01:00
|
|
|
mod hk;
|
2022-08-29 01:33:32 +02:00
|
|
|
mod pus;
|
2022-12-19 17:03:26 +01:00
|
|
|
mod requests;
|
2022-08-29 01:33:32 +02:00
|
|
|
mod tmtc;
|
|
|
|
|
2022-12-22 09:26:00 +01:00
|
|
|
use crate::hk::{AcsHkIds, HkRequest};
|
|
|
|
use crate::requests::{Request, RequestWithToken};
|
2022-12-21 22:14:42 +01:00
|
|
|
use crate::tmtc::{
|
|
|
|
core_tmtc_task, OtherArgs, PusTcSource, TcArgs, TcStore, TmArgs, TmFunnel, TmStore, PUS_APID,
|
|
|
|
};
|
2022-11-20 19:54:14 +01:00
|
|
|
use satrs_core::event_man::{
|
2022-11-19 02:42:11 +01:00
|
|
|
EventManagerWithMpscQueue, MpscEventReceiver, MpscEventU32SendProvider, SendEventProvider,
|
|
|
|
};
|
2022-11-20 19:54:14 +01:00
|
|
|
use satrs_core::events::EventU32;
|
2022-12-21 22:14:42 +01:00
|
|
|
use satrs_core::pool::{LocalPool, PoolCfg, StoreAddr};
|
2022-11-20 19:54:14 +01:00
|
|
|
use satrs_core::pus::event_man::{
|
2022-11-13 21:07:16 +01:00
|
|
|
DefaultPusMgmtBackendProvider, EventReporter, EventRequest, EventRequestWithToken,
|
|
|
|
PusEventDispatcher,
|
|
|
|
};
|
2022-12-22 09:15:59 +01:00
|
|
|
use satrs_core::pus::hk::Subservice;
|
2022-11-21 10:28:31 +01:00
|
|
|
use satrs_core::pus::verification::{
|
|
|
|
MpscVerifSender, VerificationReporterCfg, VerificationReporterWithSender,
|
|
|
|
};
|
2022-11-20 19:54:14 +01:00
|
|
|
use satrs_core::pus::{EcssTmError, EcssTmSender};
|
2022-11-21 10:28:31 +01:00
|
|
|
use satrs_core::seq_count::SimpleSeqCountProvider;
|
2022-12-21 10:15:41 +01:00
|
|
|
use satrs_example::{RequestTargetId, OBSW_SERVER_ADDR, SERVER_PORT};
|
2022-12-08 15:26:49 +01:00
|
|
|
use spacepackets::time::cds::TimeProvider;
|
|
|
|
use spacepackets::time::TimeWriter;
|
2022-12-22 09:15:59 +01:00
|
|
|
use spacepackets::tm::{PusTm, PusTmSecondaryHeader};
|
|
|
|
use spacepackets::{SequenceFlags, SpHeader};
|
2022-12-19 17:03:26 +01:00
|
|
|
use std::collections::HashMap;
|
2022-08-29 01:33:32 +02:00
|
|
|
use std::net::{IpAddr, SocketAddr};
|
2022-12-21 09:47:27 +01:00
|
|
|
use std::sync::mpsc::{channel, TryRecvError};
|
2022-11-27 01:19:34 +01:00
|
|
|
use std::sync::{mpsc, Arc, RwLock};
|
2022-08-29 01:33:32 +02:00
|
|
|
use std::thread;
|
2022-12-20 15:33:00 +01:00
|
|
|
use std::time::Duration;
|
2022-08-29 01:33:32 +02:00
|
|
|
|
2022-11-21 10:28:31 +01:00
|
|
|
#[derive(Clone)]
|
2022-11-13 21:07:16 +01:00
|
|
|
struct EventTmSender {
|
|
|
|
store_helper: TmStore,
|
|
|
|
sender: mpsc::Sender<StoreAddr>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EventTmSender {
|
|
|
|
fn new(store_helper: TmStore, sender: mpsc::Sender<StoreAddr>) -> Self {
|
|
|
|
Self {
|
|
|
|
store_helper,
|
|
|
|
sender,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EcssTmSender for EventTmSender {
|
|
|
|
type Error = mpsc::SendError<StoreAddr>;
|
|
|
|
|
|
|
|
fn send_tm(&mut self, tm: PusTm) -> Result<(), EcssTmError<Self::Error>> {
|
|
|
|
let addr = self.store_helper.add_pus_tm(&tm);
|
|
|
|
self.sender.send(addr).map_err(EcssTmError::SendError)
|
|
|
|
}
|
|
|
|
}
|
2022-12-21 19:50:31 +01:00
|
|
|
|
2022-08-29 01:33:32 +02:00
|
|
|
fn main() {
|
2022-09-03 13:47:25 +02:00
|
|
|
println!("Running OBSW example");
|
2022-12-21 22:14:42 +01:00
|
|
|
let tm_pool = LocalPool::new(PoolCfg::new(vec![
|
|
|
|
(30, 32),
|
|
|
|
(15, 64),
|
|
|
|
(15, 128),
|
|
|
|
(15, 256),
|
|
|
|
(15, 1024),
|
|
|
|
(15, 2048),
|
|
|
|
]));
|
|
|
|
let tm_store = TmStore {
|
|
|
|
pool: Arc::new(RwLock::new(Box::new(tm_pool))),
|
2022-09-10 13:34:04 +02:00
|
|
|
};
|
2022-12-21 22:14:42 +01:00
|
|
|
let tc_pool = LocalPool::new(PoolCfg::new(vec![
|
|
|
|
(30, 32),
|
|
|
|
(15, 64),
|
|
|
|
(15, 128),
|
|
|
|
(15, 256),
|
|
|
|
(15, 1024),
|
|
|
|
(15, 2048),
|
|
|
|
]));
|
|
|
|
let tc_store = TcStore {
|
|
|
|
pool: Arc::new(RwLock::new(Box::new(tc_pool))),
|
|
|
|
};
|
2022-12-22 09:15:59 +01:00
|
|
|
|
2022-12-21 22:14:42 +01:00
|
|
|
let sock_addr = SocketAddr::new(IpAddr::V4(OBSW_SERVER_ADDR), SERVER_PORT);
|
|
|
|
let (tc_source_tx, tc_source_rx) = channel();
|
2022-11-13 21:07:16 +01:00
|
|
|
let (tm_funnel_tx, tm_funnel_rx) = channel();
|
|
|
|
let (tm_server_tx, tm_server_rx) = channel();
|
2022-12-22 09:15:59 +01:00
|
|
|
let verif_sender = MpscVerifSender::new(tm_store.pool.clone(), tm_funnel_tx.clone());
|
2022-11-21 10:28:31 +01:00
|
|
|
let verif_cfg = VerificationReporterCfg::new(
|
|
|
|
PUS_APID,
|
2022-12-20 17:23:11 +01:00
|
|
|
#[allow(clippy::box_default)]
|
2022-11-21 10:28:31 +01:00
|
|
|
Box::new(SimpleSeqCountProvider::default()),
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
8,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-12-22 09:15:59 +01:00
|
|
|
let verif_reporter = VerificationReporterWithSender::new(&verif_cfg, Box::new(verif_sender));
|
2022-11-13 21:07:16 +01:00
|
|
|
|
|
|
|
// Create event handling components
|
|
|
|
let (event_request_tx, event_request_rx) = channel::<EventRequestWithToken>();
|
2022-11-12 23:37:19 +01:00
|
|
|
let (event_sender, event_man_rx) = channel();
|
|
|
|
let event_recv = MpscEventReceiver::<EventU32>::new(event_man_rx);
|
2022-11-19 02:42:11 +01:00
|
|
|
let mut event_man = EventManagerWithMpscQueue::new(Box::new(event_recv));
|
2022-11-12 23:37:19 +01:00
|
|
|
let event_reporter = EventReporter::new(PUS_APID, 128).unwrap();
|
|
|
|
let pus_tm_backend = DefaultPusMgmtBackendProvider::<EventU32>::default();
|
2022-11-13 21:07:16 +01:00
|
|
|
let mut pus_event_dispatcher =
|
|
|
|
PusEventDispatcher::new(event_reporter, Box::new(pus_tm_backend));
|
2022-11-12 23:37:19 +01:00
|
|
|
let (pus_event_man_tx, pus_event_man_rx) = channel();
|
|
|
|
let pus_event_man_send_provider = MpscEventU32SendProvider::new(1, pus_event_man_tx);
|
2022-12-21 22:14:42 +01:00
|
|
|
let mut reporter_event_handler = verif_reporter.clone();
|
|
|
|
let mut reporter_aocs = verif_reporter.clone();
|
2022-11-19 02:42:11 +01:00
|
|
|
event_man.subscribe_all(pus_event_man_send_provider.id());
|
2022-11-13 21:07:16 +01:00
|
|
|
|
2022-12-19 17:03:26 +01:00
|
|
|
let mut request_map = HashMap::new();
|
2022-12-21 10:23:32 +01:00
|
|
|
let (acs_thread_tx, acs_thread_rx) = channel::<RequestWithToken>();
|
2022-12-19 17:03:26 +01:00
|
|
|
request_map.insert(RequestTargetId::AcsSubsystem as u32, acs_thread_tx);
|
|
|
|
|
2022-12-21 22:14:42 +01:00
|
|
|
let tc_source = PusTcSource {
|
|
|
|
tc_store,
|
|
|
|
tc_source: tc_source_tx,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create clones here to allow moving the values
|
|
|
|
let core_args = OtherArgs {
|
|
|
|
sock_addr,
|
|
|
|
verif_reporter,
|
2022-11-13 21:07:16 +01:00
|
|
|
event_sender,
|
|
|
|
event_request_tx,
|
2022-12-19 17:03:26 +01:00
|
|
|
request_map,
|
2022-11-13 21:07:16 +01:00
|
|
|
};
|
2022-12-21 22:14:42 +01:00
|
|
|
let tc_args = TcArgs {
|
|
|
|
tc_source,
|
|
|
|
tc_receiver: tc_source_rx,
|
|
|
|
};
|
|
|
|
let tm_args = TmArgs {
|
|
|
|
tm_store: tm_store.clone(),
|
|
|
|
tm_sink_sender: tm_funnel_tx.clone(),
|
|
|
|
tm_server_rx,
|
|
|
|
};
|
2022-11-20 18:45:11 +01:00
|
|
|
|
2022-12-22 09:15:59 +01:00
|
|
|
let aocs_to_funnel = tm_funnel_tx.clone();
|
|
|
|
let mut aocs_tm_store = tm_store.clone();
|
|
|
|
|
2022-11-20 18:45:11 +01:00
|
|
|
println!("Starting TMTC task");
|
2022-08-29 01:33:32 +02:00
|
|
|
let jh0 = thread::spawn(move || {
|
2022-12-21 22:14:42 +01:00
|
|
|
core_tmtc_task(core_args, tc_args, tm_args);
|
2022-08-29 01:33:32 +02:00
|
|
|
});
|
|
|
|
|
2022-11-20 18:45:11 +01:00
|
|
|
println!("Starting TM funnel task");
|
2022-08-29 01:33:32 +02:00
|
|
|
let jh1 = thread::spawn(move || {
|
|
|
|
let tm_funnel = TmFunnel {
|
|
|
|
tm_server_tx,
|
|
|
|
tm_funnel_rx,
|
|
|
|
};
|
|
|
|
loop {
|
|
|
|
if let Ok(addr) = tm_funnel.tm_funnel_rx.recv() {
|
|
|
|
tm_funnel
|
|
|
|
.tm_server_tx
|
|
|
|
.send(addr)
|
|
|
|
.expect("Sending TM to server failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-11-12 23:37:19 +01:00
|
|
|
|
2022-11-20 18:45:11 +01:00
|
|
|
println!("Starting event handling task");
|
2022-11-13 21:07:16 +01:00
|
|
|
let jh2 = thread::spawn(move || {
|
|
|
|
let mut timestamp: [u8; 7] = [0; 7];
|
2022-12-21 22:14:42 +01:00
|
|
|
let mut sender = EventTmSender::new(tm_store, tm_funnel_tx);
|
2022-12-08 15:26:49 +01:00
|
|
|
let mut time_provider = TimeProvider::new_with_u16_days(0, 0);
|
2022-11-27 01:19:34 +01:00
|
|
|
let mut report_completion = |event_req: EventRequestWithToken, timestamp: &[u8]| {
|
2022-12-21 10:23:32 +01:00
|
|
|
reporter_event_handler
|
2022-11-13 21:07:16 +01:00
|
|
|
.completion_success(event_req.token, timestamp)
|
|
|
|
.expect("Sending completion success failed");
|
|
|
|
};
|
|
|
|
loop {
|
|
|
|
if let Ok(event_req) = event_request_rx.try_recv() {
|
|
|
|
match event_req.request {
|
|
|
|
EventRequest::Enable(event) => {
|
|
|
|
pus_event_dispatcher
|
|
|
|
.enable_tm_for_event(&event)
|
|
|
|
.expect("Enabling TM failed");
|
|
|
|
update_time(&mut time_provider, &mut timestamp);
|
|
|
|
report_completion(event_req, ×tamp);
|
|
|
|
}
|
|
|
|
EventRequest::Disable(event) => {
|
|
|
|
pus_event_dispatcher
|
|
|
|
.disable_tm_for_event(&event)
|
|
|
|
.expect("Disabling TM failed");
|
|
|
|
update_time(&mut time_provider, &mut timestamp);
|
|
|
|
report_completion(event_req, ×tamp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Ok((event, _param)) = pus_event_man_rx.try_recv() {
|
|
|
|
update_time(&mut time_provider, &mut timestamp);
|
|
|
|
pus_event_dispatcher
|
|
|
|
.generate_pus_event_tm_generic(&mut sender, ×tamp, event, None)
|
|
|
|
.expect("Sending TM as event failed");
|
|
|
|
}
|
2022-12-22 09:15:59 +01:00
|
|
|
thread::sleep(Duration::from_millis(400));
|
2022-11-12 23:37:19 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-12-20 15:33:00 +01:00
|
|
|
println!("Starting AOCS thread");
|
2022-12-21 10:23:32 +01:00
|
|
|
let jh3 = thread::spawn(move || {
|
|
|
|
let mut timestamp: [u8; 7] = [0; 7];
|
|
|
|
let mut time_provider = TimeProvider::new_with_u16_days(0, 0);
|
|
|
|
loop {
|
|
|
|
match acs_thread_rx.try_recv() {
|
|
|
|
Ok(request) => {
|
|
|
|
println!("ACS thread: Received HK request {:?}", request.0);
|
|
|
|
update_time(&mut time_provider, &mut timestamp);
|
2022-12-22 09:26:00 +01:00
|
|
|
match request.0 {
|
|
|
|
Request::HkRequest(hk_req) => match hk_req {
|
|
|
|
HkRequest::OneShot(one_shot_req) => {
|
|
|
|
assert_eq!(
|
|
|
|
one_shot_req.unique_id,
|
|
|
|
RequestTargetId::AcsSubsystem as u32
|
|
|
|
);
|
|
|
|
if one_shot_req.unique_id == AcsHkIds::TestMgmSet as u32 {
|
|
|
|
let mut sp_header =
|
|
|
|
SpHeader::tm(PUS_APID, SequenceFlags::Unsegmented, 0, 0)
|
|
|
|
.unwrap();
|
|
|
|
let sec_header = PusTmSecondaryHeader::new_simple(
|
|
|
|
3,
|
|
|
|
Subservice::TmHkPacket as u8,
|
|
|
|
×tamp,
|
|
|
|
);
|
|
|
|
let pus_tm = PusTm::new(&mut sp_header, sec_header, None, true);
|
|
|
|
let addr = aocs_tm_store.add_pus_tm(&pus_tm);
|
|
|
|
aocs_to_funnel.send(addr).expect("Sending HK TM failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HkRequest::Enable(_) => {}
|
|
|
|
HkRequest::Disable(_) => {}
|
|
|
|
HkRequest::ModifyCollectionInterval(_, _) => {}
|
|
|
|
},
|
|
|
|
}
|
2022-12-21 10:24:18 +01:00
|
|
|
let started_token = reporter_aocs
|
|
|
|
.start_success(request.1, ×tamp)
|
|
|
|
.expect("Sending start success failed");
|
2022-12-21 10:23:32 +01:00
|
|
|
reporter_aocs
|
|
|
|
.completion_success(started_token, ×tamp)
|
2022-12-21 10:24:18 +01:00
|
|
|
.expect("Sending completion success failed");
|
2022-12-21 09:47:27 +01:00
|
|
|
}
|
2022-12-21 10:23:32 +01:00
|
|
|
Err(e) => match e {
|
|
|
|
TryRecvError::Empty => {}
|
|
|
|
TryRecvError::Disconnected => {
|
|
|
|
println!("ACS thread: Message Queue TX disconnected!")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
thread::sleep(Duration::from_millis(500));
|
2022-12-20 15:33:00 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-08-29 01:33:32 +02:00
|
|
|
jh0.join().expect("Joining UDP TMTC server thread failed");
|
|
|
|
jh1.join().expect("Joining TM Funnel thread failed");
|
2022-11-12 23:37:19 +01:00
|
|
|
jh2.join().expect("Joining Event Manager thread failed");
|
2022-12-20 15:33:00 +01:00
|
|
|
jh3.join().expect("Joining AOCS thread failed");
|
2022-08-29 01:33:32 +02:00
|
|
|
}
|
2022-11-13 21:07:16 +01:00
|
|
|
|
2022-12-08 15:26:49 +01:00
|
|
|
pub fn update_time(time_provider: &mut TimeProvider, timestamp: &mut [u8]) {
|
2022-11-13 21:07:16 +01:00
|
|
|
time_provider
|
|
|
|
.update_from_now()
|
|
|
|
.expect("Could not get current time");
|
|
|
|
time_provider
|
|
|
|
.write_to_bytes(timestamp)
|
|
|
|
.expect("Writing timestamp failed");
|
|
|
|
}
|