70 lines
2.0 KiB
Rust
Raw Normal View History

2023-08-15 21:07:15 +02:00
use derive_new::new;
use satrs::hk::UniqueId;
use satrs::request::UniqueApidTargetId;
use satrs::spacepackets::ecss::hk;
use satrs::spacepackets::ecss::tm::{PusTmCreator, PusTmSecondaryHeader};
use satrs::spacepackets::{ByteConversionError, SpHeader};
2023-08-15 21:07:15 +02:00
#[derive(Debug, new, Copy, Clone)]
pub struct HkUniqueId {
target_id: UniqueApidTargetId,
set_id: UniqueId,
2023-08-15 21:07:15 +02:00
}
impl HkUniqueId {
2024-01-30 23:48:46 +01:00
#[allow(dead_code)]
pub fn target_id(&self) -> UniqueApidTargetId {
2024-01-30 23:59:29 +01:00
self.target_id
}
#[allow(dead_code)]
pub fn set_id(&self) -> UniqueId {
2024-01-30 23:59:29 +01:00
self.set_id
2023-08-15 21:07:15 +02:00
}
#[allow(dead_code)]
2024-01-30 23:59:29 +01:00
pub fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError> {
2023-08-15 21:07:15 +02:00
if buf.len() < 8 {
2024-01-30 23:59:29 +01:00
return Err(ByteConversionError::ToSliceTooSmall {
found: buf.len(),
expected: 8,
});
2023-08-15 21:07:15 +02:00
}
buf[0..4].copy_from_slice(&self.target_id.unique_id.to_be_bytes());
2024-01-30 23:59:29 +01:00
buf[4..8].copy_from_slice(&self.set_id.to_be_bytes());
2023-08-15 21:07:15 +02:00
2024-01-30 23:59:29 +01:00
Ok(8)
2023-08-15 21:07:15 +02:00
}
}
#[derive(new)]
pub struct PusHkHelper {
component_id: UniqueApidTargetId,
}
impl PusHkHelper {
pub fn generate_hk_report_packet<
'a,
'b,
HkWriter: FnMut(&mut [u8]) -> Result<usize, ByteConversionError>,
>(
&self,
timestamp: &'a [u8],
set_id: u32,
hk_data_writer: &mut HkWriter,
buf: &'b mut [u8],
) -> Result<PusTmCreator<'a, 'b>, ByteConversionError> {
let sec_header =
PusTmSecondaryHeader::new(3, hk::Subservice::TmHkPacket as u8, 0, 0, timestamp);
buf[0..4].copy_from_slice(&self.component_id.unique_id.to_be_bytes());
buf[4..8].copy_from_slice(&set_id.to_be_bytes());
let (_, second_half) = buf.split_at_mut(8);
let hk_data_len = hk_data_writer(second_half)?;
Ok(PusTmCreator::new(
SpHeader::new_from_apid(self.component_id.apid),
sec_header,
&buf[0..8 + hk_data_len],
true,
))
}
}