2023-02-14 17:01:15 +01:00
|
|
|
use crate::aocs_handler::{AocsSensorHandler, MGMData, MGMHandler};
|
|
|
|
use crate::requests::Request;
|
|
|
|
use crate::requests::RequestWithToken;
|
|
|
|
use crate::tmtc::TmStore;
|
|
|
|
use eurosim_obsw::{hk_err, tmtc_err};
|
2023-02-13 15:23:50 +01:00
|
|
|
use satrs_core::pool::StoreAddr;
|
|
|
|
use satrs_core::pus::hk::Subservice;
|
2023-02-14 17:01:15 +01:00
|
|
|
use satrs_core::pus::verification::{
|
|
|
|
FailParams, StdVerifSenderError, VerificationReporterWithSender,
|
|
|
|
};
|
2023-02-13 15:23:50 +01:00
|
|
|
use satrs_core::seq_count::{SeqCountProviderSyncClonable, SequenceCountProviderCore};
|
|
|
|
use satrs_core::spacepackets::time::cds::TimeProvider;
|
|
|
|
use satrs_core::spacepackets::time::TimeWriter;
|
|
|
|
use satrs_core::spacepackets::tm::{PusTm, PusTmSecondaryHeader};
|
2023-02-14 17:01:15 +01:00
|
|
|
use satrs_core::spacepackets::SpHeader;
|
2023-01-18 10:04:36 +01:00
|
|
|
use satrs_core::tmtc::AddressableId;
|
2023-02-14 17:01:15 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-02-13 15:23:50 +01:00
|
|
|
use serde_json::json;
|
2023-02-14 17:01:15 +01:00
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
use std::sync::mpsc::{Receiver, Sender};
|
|
|
|
use std::sync::{Arc, Mutex};
|
2023-01-18 10:04:36 +01:00
|
|
|
|
|
|
|
pub type CollectionIntervalFactor = u32;
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
2023-02-10 14:00:14 +01:00
|
|
|
pub enum AocsHkIds {
|
2023-02-13 15:23:50 +01:00
|
|
|
TestAocsSet = 1,
|
|
|
|
TestMgmSet = 2,
|
2023-01-18 10:04:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
pub enum HkRequest {
|
|
|
|
OneShot(AddressableId),
|
|
|
|
Enable(AddressableId),
|
|
|
|
Disable(AddressableId),
|
|
|
|
ModifyCollectionInterval(AddressableId, CollectionIntervalFactor),
|
2023-02-01 11:05:57 +01:00
|
|
|
}
|
2023-02-10 14:00:14 +01:00
|
|
|
|
2023-02-13 15:23:50 +01:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct AocsSensorData {
|
2023-02-14 17:01:15 +01:00
|
|
|
mgm_data: MGMData, // Voltage for 3 axis
|
|
|
|
css_data: [f64; 18], // Voltage for 18 sun sensors
|
|
|
|
str_data: [f64; 4], // Quaternion for position of satellite
|
2023-02-13 15:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AocsSensorData {
|
|
|
|
pub fn new() -> AocsSensorData {
|
2023-02-14 17:01:15 +01:00
|
|
|
let mgm_data = MGMData::default();
|
2023-02-13 15:23:50 +01:00
|
|
|
let css_data = [0.0; 18];
|
|
|
|
let str_data = [0.0; 4];
|
2023-02-14 17:01:15 +01:00
|
|
|
AocsSensorData {
|
|
|
|
mgm_data,
|
|
|
|
css_data,
|
|
|
|
str_data,
|
|
|
|
}
|
2023-02-13 15:23:50 +01:00
|
|
|
}
|
|
|
|
|
2023-02-14 17:01:15 +01:00
|
|
|
pub fn update_mgm_data(&mut self, mgm_data: &Arc<Mutex<MGMData>>) {
|
|
|
|
let data = mgm_data.lock().unwrap();
|
|
|
|
self.mgm_data = *data;
|
2023-02-13 15:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_css_data(&mut self, css_data: [f64; 18]) {
|
|
|
|
self.css_data = css_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_str_data(&mut self, str_data: [f64; 4]) {
|
|
|
|
self.str_data = str_data;
|
|
|
|
}
|
|
|
|
|
2023-02-14 17:01:15 +01:00
|
|
|
pub fn get_mgm_data(&mut self) -> MGMData {
|
2023-02-13 15:23:50 +01:00
|
|
|
self.mgm_data
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_css_data(&mut self) -> [f64; 18] {
|
|
|
|
self.css_data
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_str_data(&mut self) -> [f64; 4] {
|
|
|
|
self.str_data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 17:01:15 +01:00
|
|
|
pub struct AocsHousekeeper {
|
2023-02-13 15:23:50 +01:00
|
|
|
sensor_data_pool: Arc<Mutex<AocsSensorData>>,
|
|
|
|
action_rx: Receiver<RequestWithToken>,
|
|
|
|
seq_count_provider: SeqCountProviderSyncClonable,
|
|
|
|
aocs_tm_store: TmStore,
|
|
|
|
aocs_tm_funnel_tx: Sender<StoreAddr>,
|
2023-02-14 17:01:15 +01:00
|
|
|
verif_reporter: VerificationReporterWithSender<StdVerifSenderError>,
|
2023-02-13 15:23:50 +01:00
|
|
|
}
|
|
|
|
|
2023-02-14 17:01:15 +01:00
|
|
|
impl AocsHousekeeper {
|
|
|
|
pub fn new(
|
|
|
|
sensor_data_pool: Arc<Mutex<AocsSensorData>>,
|
|
|
|
action_rx: Receiver<RequestWithToken>,
|
|
|
|
seq_count_provider: SeqCountProviderSyncClonable,
|
|
|
|
aocs_tm_store: TmStore,
|
|
|
|
aocs_tm_funnel_tx: Sender<StoreAddr>,
|
|
|
|
verif_reporter: VerificationReporterWithSender<StdVerifSenderError>,
|
|
|
|
) -> AocsHousekeeper {
|
|
|
|
AocsHousekeeper {
|
2023-02-13 15:23:50 +01:00
|
|
|
sensor_data_pool,
|
|
|
|
action_rx,
|
|
|
|
seq_count_provider,
|
|
|
|
aocs_tm_store,
|
|
|
|
aocs_tm_funnel_tx,
|
|
|
|
verif_reporter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_hk_request(&mut self) {
|
|
|
|
let mut time_stamp_buf: [u8; 7] = [0; 7];
|
|
|
|
|
|
|
|
if let Ok(request_with_token) = self.action_rx.try_recv() {
|
|
|
|
if let Request::HkRequest(hk_req) = request_with_token.0 {
|
2023-02-14 17:01:15 +01:00
|
|
|
let cds_stamp = TimeProvider::from_now_with_u16_days().unwrap();
|
2023-02-13 15:23:50 +01:00
|
|
|
cds_stamp.write_to_bytes(&mut time_stamp_buf).unwrap();
|
2023-02-14 17:01:15 +01:00
|
|
|
let start_token = self //implement this for verification
|
2023-02-13 15:23:50 +01:00
|
|
|
.verif_reporter
|
|
|
|
.start_success(request_with_token.1, Some(&time_stamp_buf))
|
|
|
|
.expect("Error sending start success");
|
|
|
|
if let Ok(()) = match hk_req {
|
2023-02-14 17:01:15 +01:00
|
|
|
HkRequest::OneShot(id) => self.one_shot_hk(id),
|
|
|
|
HkRequest::Enable(id) => self.enable_hk(id),
|
|
|
|
HkRequest::Disable(id) => self.disable_hk(id),
|
|
|
|
HkRequest::ModifyCollectionInterval(id, collection_interval) => Ok(()),
|
2023-02-13 15:23:50 +01:00
|
|
|
} {
|
2023-02-14 17:01:15 +01:00
|
|
|
let cds_stamp = TimeProvider::from_now_with_u16_days().unwrap();
|
2023-02-13 15:23:50 +01:00
|
|
|
cds_stamp.write_to_bytes(&mut time_stamp_buf).unwrap();
|
|
|
|
self.verif_reporter
|
|
|
|
.completion_success(start_token, Some(&time_stamp_buf))
|
|
|
|
.expect("Error sending completion success");
|
|
|
|
} else {
|
2023-02-14 17:01:15 +01:00
|
|
|
let cds_stamp = TimeProvider::from_now_with_u16_days().unwrap();
|
2023-02-13 15:23:50 +01:00
|
|
|
cds_stamp.write_to_bytes(&mut time_stamp_buf).unwrap();
|
|
|
|
self.verif_reporter
|
2023-02-14 17:01:15 +01:00
|
|
|
.completion_failure(
|
|
|
|
start_token,
|
|
|
|
FailParams::new(
|
|
|
|
Some(&time_stamp_buf),
|
|
|
|
&hk_err::UNKNOWN_TARGET_ID,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
)
|
2023-02-13 15:23:50 +01:00
|
|
|
.expect("Error sending completion success");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 17:01:15 +01:00
|
|
|
pub fn one_shot_hk(&mut self, id: AddressableId) -> Result<(), ()> {
|
2023-02-13 15:23:50 +01:00
|
|
|
let json_string = self.aocs_data_to_str();
|
|
|
|
self.send_hk_packet(id, &json_string);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-14 17:01:15 +01:00
|
|
|
pub fn enable_hk(&mut self, id: AddressableId) -> Result<(), ()> {
|
2023-02-13 15:23:50 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-14 17:01:15 +01:00
|
|
|
pub fn disable_hk(&mut self, id: AddressableId) -> Result<(), ()> {
|
2023-02-13 15:23:50 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn aocs_data_to_str(&mut self) -> String {
|
|
|
|
let pool = self.sensor_data_pool.lock().unwrap();
|
|
|
|
serde_json::to_string(pool.deref()).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn send_hk_packet(&mut self, id: AddressableId, data: &str) {
|
|
|
|
let mut time_stamp_buf: [u8; 7] = [0; 7];
|
|
|
|
let mut huge_buf: [u8; 8192] = [0; 8192];
|
|
|
|
|
2023-02-14 17:01:15 +01:00
|
|
|
let mut sp_header =
|
|
|
|
SpHeader::tm_unseg(0x02, self.seq_count_provider.get_and_increment(), 0).unwrap();
|
|
|
|
let cds_stamp = TimeProvider::from_now_with_u16_days().unwrap();
|
2023-02-13 15:23:50 +01:00
|
|
|
cds_stamp.write_to_bytes(&mut time_stamp_buf).unwrap();
|
|
|
|
let mut len = id.write_to_be_bytes(&mut huge_buf).unwrap();
|
2023-02-14 17:01:15 +01:00
|
|
|
huge_buf[8..data.len() + 8].copy_from_slice(data.as_bytes());
|
2023-02-13 15:23:50 +01:00
|
|
|
len += data.len();
|
2023-02-14 17:01:15 +01:00
|
|
|
let tm_sec_header =
|
|
|
|
PusTmSecondaryHeader::new_simple(3, Subservice::TmHkPacket as u8, &time_stamp_buf);
|
|
|
|
let hk_tm = PusTm::new(&mut sp_header, tm_sec_header, Some(&huge_buf[0..len]), true);
|
2023-02-13 15:23:50 +01:00
|
|
|
let addr = self.aocs_tm_store.add_pus_tm(&hk_tm);
|
|
|
|
self.aocs_tm_funnel_tx.send(addr).expect("sending failed");
|
|
|
|
}
|
|
|
|
}
|