spacepackets/src/tc.rs

687 lines
23 KiB
Rust
Raw Normal View History

2022-07-31 02:27:27 +02:00
use crate::ecss::{
crc_from_raw_data, crc_procedure, CrcType, PusError, PusPacket, PusVersion, CRC_CCITT_FALSE,
};
2022-07-31 11:38:58 +02:00
use crate::SpHeader;
use crate::{CcsdsPacket, PacketError, PacketType, SequenceFlags, SizeMissmatch, CCSDS_HEADER_LEN};
2022-06-18 22:48:51 +02:00
use core::mem::size_of;
2022-07-24 15:19:05 +02:00
use delegate::delegate;
use serde::{Deserialize, Serialize};
use zerocopy::AsBytes;
2022-06-18 22:48:51 +02:00
2022-07-31 11:38:58 +02:00
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
2022-06-18 22:48:51 +02:00
/// PUS C secondary header length is fixed
2022-07-31 02:27:27 +02:00
pub const PUC_TC_SECONDARY_HEADER_LEN: usize = size_of::<zc::PusTcSecondaryHeader>();
2022-06-18 22:48:51 +02:00
pub const PUS_TC_MIN_LEN_WITHOUT_APP_DATA: usize =
CCSDS_HEADER_LEN + PUC_TC_SECONDARY_HEADER_LEN + size_of::<CrcType>();
const PUS_VERSION: PusVersion = PusVersion::PusC;
#[derive(Copy, Clone, PartialEq, Debug)]
enum AckOpts {
Acceptance = 0b1000,
Start = 0b0100,
Progress = 0b0010,
Completion = 0b0001,
}
pub const ACK_ALL: u8 = AckOpts::Acceptance as u8
| AckOpts::Start as u8
| AckOpts::Progress as u8
| AckOpts::Completion as u8;
2022-07-31 02:27:27 +02:00
pub trait PusTcSecondaryHeaderT {
fn pus_version(&self) -> PusVersion;
2022-06-18 22:48:51 +02:00
fn ack_flags(&self) -> u8;
fn service(&self) -> u8;
fn subservice(&self) -> u8;
fn source_id(&self) -> u16;
}
pub mod zc {
use crate::ecss::{PusError, PusVersion};
2022-07-31 02:27:27 +02:00
use crate::tc::PusTcSecondaryHeaderT;
2022-06-18 22:48:51 +02:00
use zerocopy::{AsBytes, FromBytes, NetworkEndian, Unaligned, U16};
#[derive(FromBytes, AsBytes, Unaligned)]
#[repr(C)]
2022-07-31 02:27:27 +02:00
pub struct PusTcSecondaryHeader {
2022-06-18 22:48:51 +02:00
version_ack: u8,
service: u8,
subservice: u8,
source_id: U16<NetworkEndian>,
}
2022-07-31 02:27:27 +02:00
impl TryFrom<crate::tc::PusTcSecondaryHeader> for PusTcSecondaryHeader {
2022-06-18 22:48:51 +02:00
type Error = PusError;
2022-07-31 02:27:27 +02:00
fn try_from(value: crate::tc::PusTcSecondaryHeader) -> Result<Self, Self::Error> {
2022-06-18 22:48:51 +02:00
if value.version != PusVersion::PusC {
return Err(PusError::VersionNotSupported(value.version));
}
2022-07-31 02:27:27 +02:00
Ok(PusTcSecondaryHeader {
2022-06-18 22:48:51 +02:00
version_ack: ((value.version as u8) << 4) | value.ack,
service: value.service,
subservice: value.subservice,
source_id: U16::from(value.source_id),
})
}
}
2022-07-31 02:27:27 +02:00
impl PusTcSecondaryHeaderT for PusTcSecondaryHeader {
fn pus_version(&self) -> PusVersion {
PusVersion::try_from(self.version_ack >> 4 & 0b1111).unwrap_or(PusVersion::Invalid)
}
2022-06-18 22:48:51 +02:00
fn ack_flags(&self) -> u8 {
self.version_ack & 0b1111
}
fn service(&self) -> u8 {
self.service
}
fn subservice(&self) -> u8 {
self.subservice
}
fn source_id(&self) -> u16 {
self.source_id.get()
}
}
2022-07-31 02:27:27 +02:00
impl PusTcSecondaryHeader {
pub fn to_bytes(&self, slice: &mut [u8]) -> Option<()> {
self.write_to(slice)
2022-06-18 22:48:51 +02:00
}
2022-07-31 02:27:27 +02:00
pub fn from_bytes(slice: &[u8]) -> Option<Self> {
Self::read_from(slice)
2022-06-18 22:48:51 +02:00
}
}
}
2022-07-24 17:23:44 +02:00
#[derive(PartialEq, Copy, Clone, Serialize, Deserialize, Debug)]
2022-07-31 02:27:27 +02:00
pub struct PusTcSecondaryHeader {
2022-07-24 15:19:05 +02:00
pub service: u8,
pub subservice: u8,
pub source_id: u16,
pub ack: u8,
pub version: PusVersion,
}
2022-06-18 22:48:51 +02:00
2022-07-31 02:27:27 +02:00
impl PusTcSecondaryHeaderT for PusTcSecondaryHeader {
fn pus_version(&self) -> PusVersion {
self.version
}
2022-07-24 15:19:05 +02:00
fn ack_flags(&self) -> u8 {
self.ack
2022-06-18 22:48:51 +02:00
}
2022-07-24 15:19:05 +02:00
fn service(&self) -> u8 {
self.service
}
2022-06-18 22:48:51 +02:00
2022-07-24 15:19:05 +02:00
fn subservice(&self) -> u8 {
self.subservice
}
2022-06-18 22:48:51 +02:00
2022-07-24 15:19:05 +02:00
fn source_id(&self) -> u16 {
self.source_id
}
}
2022-07-31 02:27:27 +02:00
impl TryFrom<zc::PusTcSecondaryHeader> for PusTcSecondaryHeader {
2022-07-24 15:19:05 +02:00
type Error = ();
2022-07-31 02:27:27 +02:00
fn try_from(value: zc::PusTcSecondaryHeader) -> Result<Self, Self::Error> {
Ok(PusTcSecondaryHeader {
2022-07-24 15:19:05 +02:00
service: value.service(),
subservice: value.subservice(),
source_id: value.source_id(),
ack: value.ack_flags(),
version: PUS_VERSION,
})
}
}
2022-06-18 22:48:51 +02:00
2022-07-31 02:27:27 +02:00
impl PusTcSecondaryHeader {
2022-07-24 15:19:05 +02:00
pub fn new_simple(service: u8, subservice: u8) -> Self {
2022-07-31 02:27:27 +02:00
PusTcSecondaryHeader {
2022-07-24 15:19:05 +02:00
service,
subservice,
ack: ACK_ALL,
source_id: 0,
version: PusVersion::PusC,
2022-06-18 22:48:51 +02:00
}
}
2022-07-24 15:19:05 +02:00
pub fn new(service: u8, subservice: u8, ack: u8, source_id: u16) -> Self {
2022-07-31 02:27:27 +02:00
PusTcSecondaryHeader {
2022-07-24 15:19:05 +02:00
service,
subservice,
ack: ack & 0b1111,
source_id,
version: PusVersion::PusC,
2022-06-18 22:48:51 +02:00
}
}
2022-07-24 15:19:05 +02:00
}
2022-06-18 22:48:51 +02:00
2022-07-31 02:27:27 +02:00
/// This struct models a PUS telecommand. It is the primary data structure to generate the raw byte
/// representation of a PUS telecommand or to deserialize from one from raw bytes.
2022-07-24 17:33:30 +02:00
///
2022-07-31 02:27:27 +02:00
/// There is no spare bytes support yet
2022-07-24 17:23:44 +02:00
#[derive(PartialEq, Copy, Clone, Serialize, Deserialize, Debug)]
2022-07-24 15:19:05 +02:00
pub struct PusTc<'slice> {
2022-07-31 02:27:27 +02:00
pub sp_header: SpHeader,
pub sec_header: PusTcSecondaryHeader,
/// If this is set to false, a manual call to [PusTc::calc_own_crc16] or
/// [PusTc::update_packet_fields] is necessary for the serialized or cached CRC16 to be valid.
2022-07-24 15:19:05 +02:00
pub calc_crc_on_serialization: bool,
#[serde(skip)]
raw_data: Option<&'slice [u8]>,
app_data: Option<&'slice [u8]>,
crc16: Option<u16>,
}
impl<'slice> PusTc<'slice> {
2022-07-24 17:23:44 +02:00
/// Generates a new struct instance.
2022-07-24 15:19:05 +02:00
///
/// # Arguments
///
2022-07-31 02:27:27 +02:00
/// * `sp_header` - Space packet header information. The correct packet type will be set
2022-07-24 15:19:05 +02:00
/// automatically
2022-07-31 02:27:27 +02:00
/// * `sec_header` - Information contained in the data field header, including the service
2022-07-24 15:19:05 +02:00
/// and subservice type
2022-07-31 02:27:27 +02:00
/// * `app_data` - Custom application data
2022-07-24 15:19:05 +02:00
/// * `set_ccsds_len` - Can be used to automatically update the CCSDS space packet data length
2022-07-24 17:23:44 +02:00
/// field. If this is not set to true, [PusTc::update_ccsds_data_len] can be called to set
/// the correct value to this field manually
2022-07-24 15:19:05 +02:00
pub fn new(
2022-07-31 02:27:27 +02:00
sp_header: &mut SpHeader,
sec_header: PusTcSecondaryHeader,
2022-07-24 15:19:05 +02:00
app_data: Option<&'slice [u8]>,
set_ccsds_len: bool,
) -> Self {
2022-07-31 11:38:58 +02:00
sp_header.set_packet_type(PacketType::Tc);
sp_header.set_sec_header_flag();
2022-07-24 15:19:05 +02:00
let mut pus_tc = PusTc {
2022-07-31 02:27:27 +02:00
sp_header: *sp_header,
2022-07-24 15:19:05 +02:00
raw_data: None,
app_data,
2022-07-31 02:27:27 +02:00
sec_header,
2022-07-24 15:19:05 +02:00
calc_crc_on_serialization: true,
crc16: None,
};
if set_ccsds_len {
2022-07-24 17:23:44 +02:00
pus_tc.update_ccsds_data_len();
2022-06-18 22:48:51 +02:00
}
2022-07-24 15:19:05 +02:00
pus_tc
2022-06-18 22:48:51 +02:00
}
2022-07-24 17:23:44 +02:00
/// Simplified version of the [PusTc::new] function which allows to only specify service and
/// subservice instead of the full PUS TC secondary header
2022-07-24 15:19:05 +02:00
pub fn new_simple(
sph: &mut SpHeader,
service: u8,
subservice: u8,
2022-06-18 22:48:51 +02:00
app_data: Option<&'slice [u8]>,
2022-07-24 15:19:05 +02:00
set_ccsds_len: bool,
) -> Self {
Self::new(
sph,
2022-07-31 02:27:27 +02:00
PusTcSecondaryHeader::new(service, subservice, ACK_ALL, 0),
2022-07-24 15:19:05 +02:00
app_data,
set_ccsds_len,
)
}
2022-06-18 22:48:51 +02:00
2022-07-24 15:19:05 +02:00
pub fn len_packed(&self) -> usize {
let mut length = PUS_TC_MIN_LEN_WITHOUT_APP_DATA;
if let Some(app_data) = self.app_data {
length += app_data.len();
2022-06-18 22:48:51 +02:00
}
2022-07-24 15:19:05 +02:00
length
}
2022-06-18 22:48:51 +02:00
2022-07-24 17:23:44 +02:00
pub fn set_seq_flags(&mut self, seq_flag: SequenceFlags) {
2022-07-31 02:27:27 +02:00
self.sp_header.psc.seq_flags = seq_flag;
2022-07-24 17:23:44 +02:00
}
2022-07-24 15:19:05 +02:00
pub fn set_ack_field(&mut self, ack: u8) -> bool {
if ack > 0b1111 {
return false;
2022-06-18 22:48:51 +02:00
}
2022-07-31 02:27:27 +02:00
self.sec_header.ack = ack & 0b1111;
2022-07-24 15:19:05 +02:00
true
}
2022-06-18 22:48:51 +02:00
2022-07-24 15:19:05 +02:00
pub fn set_source_id(&mut self, source_id: u16) {
2022-07-31 02:27:27 +02:00
self.sec_header.source_id = source_id;
2022-07-24 15:19:05 +02:00
}
2022-07-24 17:23:44 +02:00
/// Forwards the call to [crate::PacketId::set_apid]
2022-07-24 15:19:05 +02:00
pub fn set_apid(&mut self, apid: u16) -> bool {
2022-07-31 02:27:27 +02:00
self.sp_header.packet_id.set_apid(apid)
2022-07-24 15:19:05 +02:00
}
2022-07-24 17:23:44 +02:00
/// Forwards the call to [crate::PacketSequenceCtrl::set_seq_count]
2022-07-24 15:19:05 +02:00
pub fn set_seq_count(&mut self, seq_count: u16) -> bool {
2022-07-31 02:27:27 +02:00
self.sp_header.psc.set_seq_count(seq_count)
2022-07-24 15:19:05 +02:00
}
2022-06-18 22:48:51 +02:00
2022-07-24 15:19:05 +02:00
/// Calculate the CCSDS space packet data length field and sets it
2022-07-31 02:27:27 +02:00
/// This is called automatically if the [set_ccsds_len] argument in the [new] call was used.
/// If this was not done or the application data is set or changed after construction,
/// this function needs to be called to ensure that the data length field of the CCSDS header
/// is set correctly
2022-07-24 17:23:44 +02:00
pub fn update_ccsds_data_len(&mut self) {
2022-07-31 02:27:27 +02:00
self.sp_header.data_len =
self.len_packed() as u16 - size_of::<crate::zc::SpHeader>() as u16 - 1;
2022-07-24 15:19:05 +02:00
}
2022-06-18 22:48:51 +02:00
2022-07-31 02:27:27 +02:00
/// This function should be called before the TC packet is serialized if
/// [calc_crc_on_serialization] is set to False. It will calculate and cache the CRC16.
2022-07-24 15:19:05 +02:00
pub fn calc_own_crc16(&mut self) {
let mut digest = CRC_CCITT_FALSE.digest();
2022-07-31 02:27:27 +02:00
let sph_zc = crate::zc::SpHeader::from(self.sp_header);
2022-07-24 15:19:05 +02:00
digest.update(sph_zc.as_bytes());
2022-07-31 02:27:27 +02:00
let pus_tc_header = zc::PusTcSecondaryHeader::try_from(self.sec_header).unwrap();
2022-07-24 15:19:05 +02:00
digest.update(pus_tc_header.as_bytes());
if let Some(app_data) = self.app_data {
digest.update(app_data);
2022-06-18 22:48:51 +02:00
}
2022-07-24 15:19:05 +02:00
self.crc16 = Some(digest.finalize())
}
2022-06-18 22:48:51 +02:00
2022-07-31 02:27:27 +02:00
/// This helper function calls both [update_ccsds_data_len] and [calc_own_crc16]
2022-07-24 15:19:05 +02:00
pub fn update_packet_fields(&mut self) {
2022-07-24 17:23:44 +02:00
self.update_ccsds_data_len();
2022-07-24 15:19:05 +02:00
self.calc_own_crc16();
}
2022-07-31 02:27:27 +02:00
pub fn write_to(&self, slice: &mut [u8]) -> Result<usize, PusError> {
2022-07-24 15:19:05 +02:00
let mut curr_idx = 0;
2022-07-31 02:27:27 +02:00
let sph_zc = crate::zc::SpHeader::from(self.sp_header);
let tc_header_len = size_of::<zc::PusTcSecondaryHeader>();
let total_size = self.len_packed();
if total_size > slice.len() {
2022-07-24 15:19:05 +02:00
return Err(PusError::OtherPacketError(
PacketError::ToBytesSliceTooSmall(SizeMissmatch {
found: slice.len(),
expected: total_size,
}),
2022-07-24 15:19:05 +02:00
));
2022-06-18 22:48:51 +02:00
}
2022-07-24 15:19:05 +02:00
sph_zc
.to_bytes(&mut slice[curr_idx..curr_idx + 6])
2022-07-24 15:19:05 +02:00
.ok_or(PusError::OtherPacketError(
PacketError::ToBytesZeroCopyError,
))?;
2022-06-18 22:48:51 +02:00
2022-07-31 02:27:27 +02:00
curr_idx += CCSDS_HEADER_LEN;
let sec_header = zc::PusTcSecondaryHeader::try_from(self.sec_header).unwrap();
sec_header
.to_bytes(&mut slice[curr_idx..curr_idx + tc_header_len])
2022-06-18 22:48:51 +02:00
.ok_or(PusError::OtherPacketError(
2022-07-24 15:19:05 +02:00
PacketError::ToBytesZeroCopyError,
2022-06-18 22:48:51 +02:00
))?;
2022-07-31 02:27:27 +02:00
2022-07-24 15:19:05 +02:00
curr_idx += tc_header_len;
if let Some(app_data) = self.app_data {
slice[curr_idx..curr_idx + app_data.len()].copy_from_slice(app_data);
2022-07-24 15:19:05 +02:00
curr_idx += app_data.len();
2022-06-18 22:48:51 +02:00
}
2022-07-31 02:27:27 +02:00
let crc16 = crc_procedure(self.calc_crc_on_serialization, &self.crc16, curr_idx, slice)?;
slice[curr_idx..curr_idx + 2].copy_from_slice(crc16.to_be_bytes().as_slice());
2022-07-24 15:19:05 +02:00
curr_idx += 2;
Ok(curr_idx)
2022-06-18 22:48:51 +02:00
}
2022-07-31 11:38:58 +02:00
#[cfg(feature = "alloc")]
2022-07-24 15:19:05 +02:00
pub fn append_to_vec(&self, vec: &mut Vec<u8>) -> Result<usize, PusError> {
2022-07-31 02:27:27 +02:00
let sph_zc = crate::zc::SpHeader::from(self.sp_header);
2022-07-24 15:19:05 +02:00
let mut appended_len = PUS_TC_MIN_LEN_WITHOUT_APP_DATA;
if let Some(app_data) = self.app_data {
appended_len += app_data.len();
};
2022-07-24 17:23:44 +02:00
let start_idx = vec.len();
let mut curr_idx = vec.len();
2022-07-24 15:19:05 +02:00
vec.extend_from_slice(sph_zc.as_bytes());
2022-07-24 17:23:44 +02:00
curr_idx += sph_zc.as_bytes().len();
2022-07-24 15:19:05 +02:00
// The PUS version is hardcoded to PUS C
2022-07-31 02:27:27 +02:00
let pus_tc_header = zc::PusTcSecondaryHeader::try_from(self.sec_header).unwrap();
2022-07-24 15:19:05 +02:00
vec.extend_from_slice(pus_tc_header.as_bytes());
2022-07-24 17:23:44 +02:00
curr_idx += pus_tc_header.as_bytes().len();
2022-07-24 15:19:05 +02:00
if let Some(app_data) = self.app_data {
vec.extend_from_slice(app_data);
2022-07-24 17:23:44 +02:00
curr_idx += app_data.len();
}
2022-07-31 02:27:27 +02:00
let crc16 = crc_procedure(
self.calc_crc_on_serialization,
&self.crc16,
curr_idx,
&vec[start_idx..curr_idx],
)?;
2022-07-24 17:23:44 +02:00
vec.extend_from_slice(crc16.to_be_bytes().as_slice());
2022-07-24 15:19:05 +02:00
Ok(appended_len)
2022-06-18 22:48:51 +02:00
}
2022-07-24 15:19:05 +02:00
/// Create a PusTc instance from a raw slice. On success, it returns a tuple containing
/// the instance and the found byte length of the packet
pub fn new_from_raw_slice(
2022-07-31 11:38:58 +02:00
slice: &'slice [u8],
2022-07-24 15:19:05 +02:00
) -> Result<(Self, usize), PusError> {
let slice_ref = slice.as_ref();
let raw_data_len = slice_ref.len();
if raw_data_len < PUS_TC_MIN_LEN_WITHOUT_APP_DATA {
return Err(PusError::RawDataTooShort(raw_data_len));
2022-06-18 22:48:51 +02:00
}
2022-07-24 15:19:05 +02:00
let mut current_idx = 0;
let sph = crate::zc::SpHeader::from_bytes(&slice_ref[current_idx..current_idx + 6]).ok_or(
PusError::OtherPacketError(PacketError::FromBytesZeroCopyError),
)?;
current_idx += 6;
let total_len = sph.total_len();
if raw_data_len < total_len || total_len < PUS_TC_MIN_LEN_WITHOUT_APP_DATA {
return Err(PusError::RawDataTooShort(raw_data_len));
}
2022-07-31 02:27:27 +02:00
let sec_header = crate::tc::zc::PusTcSecondaryHeader::from_bytes(
2022-07-24 15:19:05 +02:00
&slice_ref[current_idx..current_idx + PUC_TC_SECONDARY_HEADER_LEN],
)
.ok_or(PusError::OtherPacketError(
PacketError::FromBytesZeroCopyError,
))?;
current_idx += PUC_TC_SECONDARY_HEADER_LEN;
let mut pus_tc = PusTc {
2022-07-31 02:27:27 +02:00
sp_header: SpHeader::from(sph),
sec_header: PusTcSecondaryHeader::try_from(sec_header).unwrap(),
2022-07-24 15:19:05 +02:00
raw_data: Some(slice_ref),
app_data: match current_idx {
_ if current_idx == total_len - 2 => None,
_ if current_idx > total_len - 2 => {
return Err(PusError::RawDataTooShort(raw_data_len))
}
_ => Some(&slice_ref[current_idx..total_len - 2]),
},
calc_crc_on_serialization: false,
crc16: None,
};
2022-07-31 02:27:27 +02:00
crc_from_raw_data(pus_tc.raw_data)?;
2022-07-24 15:19:05 +02:00
pus_tc.verify()?;
Ok((pus_tc, total_len))
}
2022-06-18 22:48:51 +02:00
2022-07-24 15:19:05 +02:00
fn verify(&mut self) -> Result<(), PusError> {
let mut digest = CRC_CCITT_FALSE.digest();
if self.raw_data.is_none() {
return Err(PusError::NoRawData);
2022-06-18 22:48:51 +02:00
}
2022-07-24 15:19:05 +02:00
let raw_data = self.raw_data.unwrap();
digest.update(raw_data.as_ref());
if digest.finalize() == 0 {
return Ok(());
}
2022-07-31 02:27:27 +02:00
let crc16 = crc_from_raw_data(self.raw_data)?;
2022-07-24 15:19:05 +02:00
Err(PusError::IncorrectCrc(crc16))
}
}
//noinspection RsTraitImplementation
impl CcsdsPacket for PusTc<'_> {
2022-07-31 02:27:27 +02:00
delegate!(to self.sp_header {
2022-07-24 15:19:05 +02:00
fn ccsds_version(&self) -> u8;
fn packet_id(&self) -> crate::PacketId;
fn psc(&self) -> crate::PacketSequenceCtrl;
fn data_len(&self) -> u16;
});
}
//noinspection RsTraitImplementation
impl PusPacket for PusTc<'_> {
2022-07-31 02:27:27 +02:00
delegate!(to self.sec_header {
fn pus_version(&self) -> PusVersion;
2022-07-24 15:19:05 +02:00
fn service(&self) -> u8;
fn subservice(&self) -> u8;
});
fn user_data(&self) -> Option<&[u8]> {
self.app_data
2022-06-18 22:48:51 +02:00
}
2022-07-24 15:19:05 +02:00
fn crc16(&self) -> Option<u16> {
self.crc16
2022-06-18 22:48:51 +02:00
}
}
2022-07-24 15:19:05 +02:00
//noinspection RsTraitImplementation
2022-07-31 02:27:27 +02:00
impl PusTcSecondaryHeaderT for PusTc<'_> {
delegate!(to self.sec_header {
fn pus_version(&self) -> PusVersion;
2022-07-24 15:19:05 +02:00
fn service(&self) -> u8;
fn subservice(&self) -> u8;
fn source_id(&self) -> u16;
fn ack_flags(&self) -> u8;
});
}
2022-07-28 02:09:07 +02:00
2022-06-18 22:48:51 +02:00
#[cfg(test)]
mod tests {
2022-07-31 02:27:27 +02:00
use crate::ecss::PusVersion::PusC;
2022-07-24 17:23:44 +02:00
use crate::ecss::{PusError, PusPacket};
2022-07-31 11:38:58 +02:00
use crate::SpHeader;
2022-06-18 22:48:51 +02:00
use crate::tc::ACK_ALL;
2022-07-31 02:27:27 +02:00
use crate::tc::{PusTc, PusTcSecondaryHeader, PusTcSecondaryHeaderT};
2022-07-24 17:23:44 +02:00
use crate::{CcsdsPacket, SequenceFlags};
2022-06-18 22:48:51 +02:00
use alloc::vec::Vec;
2022-07-24 15:19:05 +02:00
fn base_ping_tc_full_ctor() -> PusTc<'static> {
let mut sph = SpHeader::tc(0x02, 0x34, 0).unwrap();
2022-07-31 02:27:27 +02:00
let tc_header = PusTcSecondaryHeader::new_simple(17, 1);
2022-07-24 15:19:05 +02:00
PusTc::new(&mut sph, tc_header, None, true)
}
2022-07-24 17:23:44 +02:00
fn base_ping_tc_simple_ctor() -> PusTc<'static> {
let mut sph = SpHeader::tc(0x02, 0x34, 0).unwrap();
PusTc::new_simple(&mut sph, 17, 1, None, true)
}
fn base_ping_tc_simple_ctor_with_app_data(app_data: &'static [u8]) -> PusTc<'static> {
let mut sph = SpHeader::tc(0x02, 0x34, 0).unwrap();
PusTc::new_simple(&mut sph, 17, 1, Some(app_data), true)
}
2022-06-18 22:48:51 +02:00
#[test]
2022-07-24 15:19:05 +02:00
fn test_tc_fields() {
2022-07-24 17:23:44 +02:00
let pus_tc = base_ping_tc_full_ctor();
2022-06-18 22:48:51 +02:00
assert_eq!(pus_tc.crc16(), None);
2022-07-24 17:23:44 +02:00
verify_test_tc(&pus_tc, false, 13);
2022-07-24 15:19:05 +02:00
}
#[test]
fn test_serialization() {
2022-07-24 17:23:44 +02:00
let pus_tc = base_ping_tc_simple_ctor();
2022-07-24 15:19:05 +02:00
let mut test_buf: [u8; 32] = [0; 32];
let size = pus_tc
2022-07-31 02:27:27 +02:00
.write_to(test_buf.as_mut_slice())
2022-07-24 15:19:05 +02:00
.expect("Error writing TC to buffer");
assert_eq!(size, 13);
}
#[test]
fn test_deserialization() {
2022-07-24 17:23:44 +02:00
let pus_tc = base_ping_tc_simple_ctor();
2022-07-24 15:19:05 +02:00
let mut test_buf: [u8; 32] = [0; 32];
2022-06-18 22:48:51 +02:00
let size = pus_tc
2022-07-31 02:27:27 +02:00
.write_to(test_buf.as_mut_slice())
2022-06-18 22:48:51 +02:00
.expect("Error writing TC to buffer");
assert_eq!(size, 13);
2022-07-24 17:23:44 +02:00
let (tc_from_raw, size) = PusTc::new_from_raw_slice(&test_buf)
2022-06-18 22:48:51 +02:00
.expect("Creating PUS TC struct from raw buffer failed");
assert_eq!(size, 13);
2022-07-24 17:23:44 +02:00
verify_test_tc(&tc_from_raw, false, 13);
verify_test_tc_raw(&test_buf);
verify_crc_no_app_data(&test_buf);
}
2022-06-18 22:48:51 +02:00
2022-07-24 17:23:44 +02:00
#[test]
fn test_vec_ser_deser() {
let pus_tc = base_ping_tc_simple_ctor();
2022-06-18 22:48:51 +02:00
let mut test_vec = Vec::new();
2022-07-24 17:23:44 +02:00
let size = pus_tc
2022-06-18 22:48:51 +02:00
.append_to_vec(&mut test_vec)
.expect("Error writing TC to vector");
assert_eq!(size, 13);
2022-07-24 17:23:44 +02:00
verify_test_tc_raw(&test_vec.as_slice());
verify_crc_no_app_data(&test_vec.as_slice());
}
#[test]
fn test_incorrect_crc() {
let pus_tc = base_ping_tc_simple_ctor();
let mut test_buf: [u8; 32] = [0; 32];
pus_tc
2022-07-31 02:27:27 +02:00
.write_to(test_buf.as_mut_slice())
2022-07-24 17:23:44 +02:00
.expect("Error writing TC to buffer");
test_buf[12] = 0;
let res = PusTc::new_from_raw_slice(&test_buf);
assert!(res.is_err());
let err = res.unwrap_err();
assert!(matches!(err, PusError::IncorrectCrc { .. }));
2022-06-18 22:48:51 +02:00
}
2022-07-24 17:23:44 +02:00
#[test]
fn test_manual_crc_calculation() {
let mut pus_tc = base_ping_tc_simple_ctor();
pus_tc.calc_crc_on_serialization = false;
let mut test_buf: [u8; 32] = [0; 32];
pus_tc.calc_own_crc16();
pus_tc
2022-07-31 02:27:27 +02:00
.write_to(test_buf.as_mut_slice())
2022-07-24 17:23:44 +02:00
.expect("Error writing TC to buffer");
verify_test_tc_raw(&test_buf);
verify_crc_no_app_data(&test_buf);
}
#[test]
fn test_manual_crc_calculation_no_calc_call() {
let mut pus_tc = base_ping_tc_simple_ctor();
pus_tc.calc_crc_on_serialization = false;
let mut test_buf: [u8; 32] = [0; 32];
2022-07-31 02:27:27 +02:00
let res = pus_tc.write_to(test_buf.as_mut_slice());
2022-07-24 17:23:44 +02:00
assert!(res.is_err());
let err = res.unwrap_err();
assert!(matches!(err, PusError::CrcCalculationMissing { .. }));
}
#[test]
fn test_with_application_data_vec() {
let pus_tc = base_ping_tc_simple_ctor_with_app_data(&[1, 2, 3]);
verify_test_tc(&pus_tc, true, 16);
let mut test_vec = Vec::new();
let size = pus_tc
.append_to_vec(&mut test_vec)
.expect("Error writing TC to vector");
assert_eq!(test_vec[11], 1);
assert_eq!(test_vec[12], 2);
assert_eq!(test_vec[13], 3);
assert_eq!(size, 16);
}
#[test]
fn test_with_application_data_buf() {
let pus_tc = base_ping_tc_simple_ctor_with_app_data(&[1, 2, 3]);
verify_test_tc(&pus_tc, true, 16);
let mut test_buf: [u8; 32] = [0; 32];
let size = pus_tc
2022-07-31 02:27:27 +02:00
.write_to(test_buf.as_mut_slice())
2022-07-24 17:23:44 +02:00
.expect("Error writing TC to buffer");
assert_eq!(test_buf[11], 1);
assert_eq!(test_buf[12], 2);
assert_eq!(test_buf[13], 3);
assert_eq!(size, 16);
}
#[test]
fn test_custom_setters() {
let mut pus_tc = base_ping_tc_simple_ctor();
let mut test_buf: [u8; 32] = [0; 32];
pus_tc.set_apid(0x7ff);
pus_tc.set_seq_count(0x3fff);
pus_tc.set_ack_field(0b11);
pus_tc.set_source_id(0xffff);
pus_tc.set_seq_flags(SequenceFlags::Unsegmented);
assert_eq!(pus_tc.source_id(), 0xffff);
assert_eq!(pus_tc.seq_count(), 0x3fff);
assert_eq!(pus_tc.ack_flags(), 0b11);
assert_eq!(pus_tc.apid(), 0x7ff);
assert_eq!(pus_tc.sequence_flags(), SequenceFlags::Unsegmented);
pus_tc.calc_own_crc16();
pus_tc
2022-07-31 02:27:27 +02:00
.write_to(test_buf.as_mut_slice())
2022-07-24 17:23:44 +02:00
.expect("Error writing TC to buffer");
assert_eq!(test_buf[0], 0x1f);
assert_eq!(test_buf[1], 0xff);
assert_eq!(test_buf[2], 0xff);
assert_eq!(test_buf[3], 0xff);
assert_eq!(test_buf[6], 0x23);
// Source ID 0
assert_eq!(test_buf[9], 0xff);
assert_eq!(test_buf[10], 0xff);
}
fn verify_test_tc(tc: &PusTc, has_user_data: bool, exp_full_len: usize) {
2022-06-18 22:48:51 +02:00
assert_eq!(PusPacket::service(tc), 17);
assert_eq!(PusPacket::subservice(tc), 1);
2022-07-31 11:38:58 +02:00
assert!(tc.sec_header_flag());
2022-07-31 02:27:27 +02:00
assert_eq!(PusPacket::pus_version(tc), PusC);
2022-07-24 17:23:44 +02:00
if !has_user_data {
assert_eq!(tc.user_data(), None);
}
2022-07-24 15:19:05 +02:00
assert_eq!(tc.seq_count(), 0x34);
2022-06-18 22:48:51 +02:00
assert_eq!(tc.source_id(), 0);
2022-07-24 15:19:05 +02:00
assert_eq!(tc.apid(), 0x02);
2022-06-18 22:48:51 +02:00
assert_eq!(tc.ack_flags(), ACK_ALL);
2022-07-24 17:23:44 +02:00
assert_eq!(tc.len_packed(), exp_full_len);
2022-07-31 11:38:58 +02:00
let mut comp_header = SpHeader::tc(0x02, 0x34, exp_full_len as u16 - 7).unwrap();
comp_header.set_sec_header_flag();
2022-07-24 17:23:44 +02:00
assert_eq!(
2022-07-31 02:27:27 +02:00
tc.sp_header,
2022-07-31 11:38:58 +02:00
comp_header
2022-07-24 17:23:44 +02:00
);
2022-06-18 22:48:51 +02:00
}
2022-07-24 17:23:44 +02:00
fn verify_test_tc_raw(slice: &impl AsRef<[u8]>) {
2022-06-18 22:48:51 +02:00
// Reference comparison implementation:
2022-07-24 17:23:44 +02:00
// https://github.com/us-irs/py-spacepackets/blob/v0.13.0/tests/ecss/test_pus_tc.py
2022-06-18 22:48:51 +02:00
let slice = slice.as_ref();
// 0x1801 is the generic
2022-07-24 17:23:44 +02:00
assert_eq!(slice[0], 0x18);
2022-06-18 22:48:51 +02:00
// APID is 0x01
2022-07-24 15:19:05 +02:00
assert_eq!(slice[1], 0x02);
2022-06-18 22:48:51 +02:00
// Unsegmented packets
assert_eq!(slice[2], 0xc0);
2022-07-24 15:19:05 +02:00
// Sequence count 0x34
assert_eq!(slice[3], 0x34);
2022-06-18 22:48:51 +02:00
assert_eq!(slice[4], 0x00);
// Space data length of 6 equals total packet length of 13
assert_eq!(slice[5], 0x06);
// PUS Version C 0b0010 and ACK flags 0b1111
assert_eq!(slice[6], 0x2f);
// Service 17
assert_eq!(slice[7], 0x11);
// Subservice 1
assert_eq!(slice[8], 0x01);
// Source ID 0
assert_eq!(slice[9], 0x00);
assert_eq!(slice[10], 0x00);
2022-07-24 17:23:44 +02:00
}
fn verify_crc_no_app_data(slice: &impl AsRef<[u8]>) {
// Reference comparison implementation:
// https://github.com/us-irs/py-spacepackets/blob/v0.13.0/tests/ecss/test_pus_tc.py
let slice = slice.as_ref();
2022-07-24 15:19:05 +02:00
assert_eq!(slice[11], 0xee);
assert_eq!(slice[12], 0x63);
2022-06-18 22:48:51 +02:00
}
}