I am not sure this is required..
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good

This commit is contained in:
Robin Müller 2023-07-09 12:39:00 +02:00
parent b9774c4c9f
commit defd7609e7
Signed by: muellerr
GPG Key ID: A649FB78196E3849

View File

@ -48,8 +48,8 @@ use zerocopy::AsBytes;
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
use alloc::vec::Vec; use alloc::vec::Vec;
#[cfg(feature = "alloc")] // #[cfg(feature = "alloc")]
pub use alloc_mod::*; // pub use alloc_mod::*;
#[deprecated( #[deprecated(
since = "0.7.0", since = "0.7.0",
@ -734,206 +734,208 @@ impl GenericPusTcSecondaryHeader for PusTcCreator<'_> {
}); });
} }
#[cfg(feature = "alloc")] // TODO: Do we really need an owned variant of the PUS TC creator? I think the regular creator
pub mod alloc_mod { // is perfectly fine..
// #[cfg(feature = "alloc")]
#[cfg(feature = "serde")] // pub mod alloc_mod {
use serde::{Deserialize, Serialize}; //
// #[cfg(feature = "serde")]
use crate::ecss::tc::{ // use serde::{Deserialize, Serialize};
zc, GenericPusTcSecondaryHeader, PusTcSecondaryHeader, ACK_ALL, //
PUS_TC_MIN_LEN_WITHOUT_APP_DATA, // use crate::ecss::tc::{
}; // zc, GenericPusTcSecondaryHeader, PusTcSecondaryHeader, ACK_ALL,
use crate::ecss::PusVersion; // PUS_TC_MIN_LEN_WITHOUT_APP_DATA,
use crate::ecss::{ // };
ccsds_impl, sp_header_impls, PusError, PusPacket, SerializablePusPacket, CCSDS_HEADER_LEN, // use crate::ecss::PusVersion;
}; // use crate::ecss::{
use crate::SequenceFlags; // ccsds_impl, sp_header_impls, PusError, PusPacket, SerializablePusPacket, CCSDS_HEADER_LEN,
use crate::{ // };
ByteConversionError, CcsdsPacket, PacketType, SizeMissmatch, SpHeader, CRC_CCITT_FALSE, // use crate::SequenceFlags;
}; // use crate::{
use alloc::vec::Vec; // ByteConversionError, CcsdsPacket, PacketType, SizeMissmatch, SpHeader, CRC_CCITT_FALSE,
use core::mem::size_of; // };
use delegate::delegate; // use alloc::vec::Vec;
use zerocopy::AsBytes; // use core::mem::size_of;
// use delegate::delegate;
/// This is the owned variant of [super::PusTcCreator] where the application data is copied to // use zerocopy::AsBytes;
/// an internal field and is then an owned field of the creator. //
#[derive(Clone, Debug, PartialEq, Eq)] // /// This is the owned variant of [super::PusTcCreator] where the application data is copied to
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] // /// an internal field and is then an owned field of the creator.
pub struct PusTcCreatorOwned { // #[derive(Clone, Debug, PartialEq, Eq)]
sp_header: SpHeader, // #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub sec_header: PusTcSecondaryHeader, // pub struct PusTcCreatorOwned {
pub app_data: Vec<u8>, // sp_header: SpHeader,
} // pub sec_header: PusTcSecondaryHeader,
// pub app_data: Vec<u8>,
impl PusTcCreatorOwned { // }
/// Generates a new struct instance. //
/// // impl PusTcCreatorOwned {
/// # Arguments // /// Generates a new struct instance.
/// // ///
/// * `sp_header` - Space packet header information. The correct packet type will be set // /// # Arguments
/// automatically // ///
/// * `sec_header` - Information contained in the data field header, including the service // /// * `sp_header` - Space packet header information. The correct packet type will be set
/// and subservice type // /// automatically
/// * `app_data` - Custom application data // /// * `sec_header` - Information contained in the data field header, including the service
/// * `set_ccsds_len` - Can be used to automatically update the CCSDS space packet data length // /// and subservice type
/// field. If this is not set to true, [Self::update_ccsds_data_len] can be called to set // /// * `app_data` - Custom application data
/// the correct value to this field manually // /// * `set_ccsds_len` - Can be used to automatically update the CCSDS space packet data length
pub fn new( // /// field. If this is not set to true, [Self::update_ccsds_data_len] can be called to set
sp_header: &mut SpHeader, // /// the correct value to this field manually
sec_header: PusTcSecondaryHeader, // pub fn new(
app_data: Option<&[u8]>, // sp_header: &mut SpHeader,
set_ccsds_len: bool, // sec_header: PusTcSecondaryHeader,
) -> Self { // app_data: Option<&[u8]>,
sp_header.set_packet_type(PacketType::Tc); // set_ccsds_len: bool,
sp_header.set_sec_header_flag(); // ) -> Self {
let app_data = app_data.map_or(Vec::new(), Vec::from); // sp_header.set_packet_type(PacketType::Tc);
let mut pus_tc = Self { // sp_header.set_sec_header_flag();
sp_header: *sp_header, // let app_data = app_data.map_or(Vec::new(), Vec::from);
app_data, // let mut pus_tc = Self {
sec_header, // sp_header: *sp_header,
}; // app_data,
if set_ccsds_len { // sec_header,
pus_tc.update_ccsds_data_len(); // };
} // if set_ccsds_len {
pus_tc // pus_tc.update_ccsds_data_len();
} // }
// pus_tc
/// Simplified version of the [Self::new] function which allows to only specify service // }
/// and subservice instead of the full PUS TC secondary header. //
pub fn new_simple( // /// Simplified version of the [Self::new] function which allows to only specify service
sph: &mut SpHeader, // /// and subservice instead of the full PUS TC secondary header.
service: u8, // pub fn new_simple(
subservice: u8, // sph: &mut SpHeader,
app_data: Option<&[u8]>, // service: u8,
set_ccsds_len: bool, // subservice: u8,
) -> Self { // app_data: Option<&[u8]>,
Self::new( // set_ccsds_len: bool,
sph, // ) -> Self {
PusTcSecondaryHeader::new(service, subservice, ACK_ALL, 0), // Self::new(
app_data, // sph,
set_ccsds_len, // PusTcSecondaryHeader::new(service, subservice, ACK_ALL, 0),
) // app_data,
} // set_ccsds_len,
// )
pub fn sp_header(&self) -> &SpHeader { // }
&self.sp_header //
} // pub fn sp_header(&self) -> &SpHeader {
// &self.sp_header
pub fn set_ack_field(&mut self, ack: u8) -> bool { // }
if ack > 0b1111 { //
return false; // pub fn set_ack_field(&mut self, ack: u8) -> bool {
} // if ack > 0b1111 {
self.sec_header.ack = ack & 0b1111; // return false;
true // }
} // self.sec_header.ack = ack & 0b1111;
// true
pub fn set_source_id(&mut self, source_id: u16) { // }
self.sec_header.source_id = source_id; //
} // pub fn set_source_id(&mut self, source_id: u16) {
// self.sec_header.source_id = source_id;
sp_header_impls!(); // }
//
/// Calculate the CCSDS space packet data length field and sets it // sp_header_impls!();
/// This is called automatically if the `set_ccsds_len` argument in the [Self::new] call was //
/// used. // /// Calculate the CCSDS space packet data length field and sets it
/// If this was not done or the application data is set or changed after construction, // /// This is called automatically if the `set_ccsds_len` argument in the [Self::new] call was
/// this function needs to be called to ensure that the data length field of the CCSDS header // /// used.
/// is set correctly. // /// If this was not done or the application data is set or changed after construction,
pub fn update_ccsds_data_len(&mut self) { // /// this function needs to be called to ensure that the data length field of the CCSDS header
self.sp_header.data_len = // /// is set correctly.
self.len_packed() as u16 - size_of::<crate::zc::SpHeader>() as u16 - 1; // pub fn update_ccsds_data_len(&mut self) {
} // self.sp_header.data_len =
// self.len_packed() as u16 - size_of::<crate::zc::SpHeader>() as u16 - 1;
/// This function can be called to calculate the CRC16 of the packet before it was // }
/// serialized. //
pub fn calc_own_crc16(&self) -> u16 { // /// This function can be called to calculate the CRC16 of the packet before it was
let mut digest = CRC_CCITT_FALSE.digest(); // /// serialized.
let sph_zc = crate::zc::SpHeader::from(self.sp_header); // pub fn calc_own_crc16(&self) -> u16 {
digest.update(sph_zc.as_bytes()); // let mut digest = CRC_CCITT_FALSE.digest();
let pus_tc_header = zc::PusTcSecondaryHeader::try_from(self.sec_header).unwrap(); // let sph_zc = crate::zc::SpHeader::from(self.sp_header);
digest.update(pus_tc_header.as_bytes()); // digest.update(sph_zc.as_bytes());
if !self.app_data.is_empty() { // let pus_tc_header = zc::PusTcSecondaryHeader::try_from(self.sec_header).unwrap();
digest.update(&self.app_data); // digest.update(pus_tc_header.as_bytes());
} // if !self.app_data.is_empty() {
digest.finalize() // digest.update(&self.app_data);
} // }
} // digest.finalize()
// }
impl SerializablePusPacket for PusTcCreatorOwned { // }
fn len_packed(&self) -> usize { //
let mut length = PUS_TC_MIN_LEN_WITHOUT_APP_DATA; // impl SerializablePusPacket for PusTcCreatorOwned {
if !self.app_data.is_empty() { // fn len_packed(&self) -> usize {
length += self.app_data.len(); // let mut length = PUS_TC_MIN_LEN_WITHOUT_APP_DATA;
} // if !self.app_data.is_empty() {
length // length += self.app_data.len();
} // }
// length
/// Write the raw PUS byte representation to a provided buffer. // }
fn write_to_bytes(&self, slice: &mut [u8]) -> Result<usize, PusError> { //
let mut curr_idx = 0; // /// Write the raw PUS byte representation to a provided buffer.
let tc_header_len = size_of::<zc::PusTcSecondaryHeader>(); // fn write_to_bytes(&self, slice: &mut [u8]) -> Result<usize, PusError> {
let total_size = self.len_packed(); // let mut curr_idx = 0;
if total_size > slice.len() { // let tc_header_len = size_of::<zc::PusTcSecondaryHeader>();
return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch { // let total_size = self.len_packed();
found: slice.len(), // if total_size > slice.len() {
expected: total_size, // return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
}) // found: slice.len(),
.into()); // expected: total_size,
} // })
self.sp_header.write_to_be_bytes(slice)?; // .into());
curr_idx += CCSDS_HEADER_LEN; // }
let sec_header = zc::PusTcSecondaryHeader::try_from(self.sec_header).unwrap(); // self.sp_header.write_to_be_bytes(slice)?;
sec_header // curr_idx += CCSDS_HEADER_LEN;
.write_to_bytes(&mut slice[curr_idx..curr_idx + tc_header_len]) // let sec_header = zc::PusTcSecondaryHeader::try_from(self.sec_header).unwrap();
.ok_or(ByteConversionError::ZeroCopyToError)?; // sec_header
// .write_to_bytes(&mut slice[curr_idx..curr_idx + tc_header_len])
curr_idx += tc_header_len; // .ok_or(ByteConversionError::ZeroCopyToError)?;
if !self.app_data.is_empty() { //
slice[curr_idx..curr_idx + self.app_data.len()].copy_from_slice(&self.app_data); // curr_idx += tc_header_len;
curr_idx += self.app_data.len(); // if !self.app_data.is_empty() {
} // slice[curr_idx..curr_idx + self.app_data.len()].copy_from_slice(&self.app_data);
let mut digest = CRC_CCITT_FALSE.digest(); // curr_idx += self.app_data.len();
digest.update(&slice[0..curr_idx]); // }
slice[curr_idx..curr_idx + 2].copy_from_slice(&digest.finalize().to_be_bytes()); // let mut digest = CRC_CCITT_FALSE.digest();
curr_idx += 2; // digest.update(&slice[0..curr_idx]);
Ok(curr_idx) // slice[curr_idx..curr_idx + 2].copy_from_slice(&digest.finalize().to_be_bytes());
} // curr_idx += 2;
} // Ok(curr_idx)
impl CcsdsPacket for PusTcCreatorOwned { // }
ccsds_impl!(); // }
} // impl CcsdsPacket for PusTcCreatorOwned {
// ccsds_impl!();
impl PusPacket for PusTcCreatorOwned { // }
delegate!(to self.sec_header { //
fn pus_version(&self) -> PusVersion; // impl PusPacket for PusTcCreatorOwned {
fn service(&self) -> u8; // delegate!(to self.sec_header {
fn subservice(&self) -> u8; // fn pus_version(&self) -> PusVersion;
}); // fn service(&self) -> u8;
// fn subservice(&self) -> u8;
fn user_data(&self) -> Option<&[u8]> { // });
if self.app_data.is_empty() { //
return None; // fn user_data(&self) -> Option<&[u8]> {
} // if self.app_data.is_empty() {
Some(self.app_data.as_slice()) // return None;
} // }
// Some(self.app_data.as_slice())
fn crc16(&self) -> Option<u16> { // }
Some(self.calc_own_crc16()) //
} // fn crc16(&self) -> Option<u16> {
} // Some(self.calc_own_crc16())
// }
impl GenericPusTcSecondaryHeader for PusTcCreatorOwned { // }
delegate!(to self.sec_header { //
fn pus_version(&self) -> PusVersion; // impl GenericPusTcSecondaryHeader for PusTcCreatorOwned {
fn service(&self) -> u8; // delegate!(to self.sec_header {
fn subservice(&self) -> u8; // fn pus_version(&self) -> PusVersion;
fn source_id(&self) -> u16; // fn service(&self) -> u8;
fn ack_flags(&self) -> u8; // fn subservice(&self) -> u8;
}); // fn source_id(&self) -> u16;
} // fn ack_flags(&self) -> u8;
} // });
// }
// }
/// This class can be used to read a PUS TC telecommand from raw memory. /// This class can be used to read a PUS TC telecommand from raw memory.
/// ///