additional useful functions

This commit is contained in:
Robin Müller 2022-12-05 01:41:35 +01:00
parent 3420bcbeba
commit 8cf635d2fa
No known key found for this signature in database
GPG Key ID: BE6480244DFE612C
3 changed files with 35 additions and 24 deletions

View File

@ -498,16 +498,38 @@ impl SpHeader {
self.packet_id.ptype = packet_type;
}
pub fn from_raw_slice(buf: &[u8]) -> Result<Self, ByteConversionError> {
if buf.len() < CCSDS_HEADER_LEN + 1 {
/// Create a struct from a raw slice where the fields have network endianness (big).
/// This function will also returns the remaining part of the passed slice starting starting
/// past the read CCSDS header.
pub fn from_be_bytes(buf: &[u8]) -> Result<(Self, &[u8]), ByteConversionError> {
if buf.len() < CCSDS_HEADER_LEN {
return Err(ByteConversionError::FromSliceTooSmall(SizeMissmatch {
found: buf.len(),
expected: CCSDS_HEADER_LEN + 1,
expected: CCSDS_HEADER_LEN,
}));
}
let zc_header = zc::SpHeader::from_bytes(&buf[0..CCSDS_HEADER_LEN])
.ok_or(ByteConversionError::ZeroCopyFromError)?;
Ok(Self::from(zc_header))
Ok((Self::from(zc_header), &buf[CCSDS_HEADER_LEN..]))
}
/// Write the header to a raw buffer using big endian format. This function returns the
/// remaining part of the passed slice starting past the written CCSDS header.
pub fn write_to_be_bytes<'a>(
&self,
buf: &'a mut [u8],
) -> Result<&'a mut [u8], ByteConversionError> {
if buf.len() < CCSDS_HEADER_LEN {
return Err(ByteConversionError::FromSliceTooSmall(SizeMissmatch {
found: buf.len(),
expected: CCSDS_HEADER_LEN,
}));
}
let zc_header: zc::SpHeader = zc::SpHeader::from(*self);
zc_header
.to_bytes(&mut buf[0..CCSDS_HEADER_LEN])
.ok_or(ByteConversionError::ZeroCopyToError)?;
Ok(&mut buf[CCSDS_HEADER_LEN..])
}
}

View File

@ -335,7 +335,6 @@ impl<'slice> PusTc<'slice> {
/// Write the raw PUS byte representation to a provided buffer.
pub fn write_to_bytes(&self, slice: &mut [u8]) -> Result<usize, PusError> {
let mut curr_idx = 0;
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() {
@ -345,10 +344,7 @@ impl<'slice> PusTc<'slice> {
})
.into());
}
sph_zc
.to_bytes(&mut slice[curr_idx..curr_idx + CCSDS_HEADER_LEN])
.ok_or(ByteConversionError::ZeroCopyToError)?;
self.sp_header.write_to_be_bytes(slice)?;
curr_idx += CCSDS_HEADER_LEN;
let sec_header = zc::PusTcSecondaryHeader::try_from(self.sec_header).unwrap();
sec_header
@ -411,11 +407,9 @@ impl<'slice> PusTc<'slice> {
return Err(PusError::RawDataTooShort(raw_data_len));
}
let mut current_idx = 0;
let sph =
crate::zc::SpHeader::from_bytes(&slice[current_idx..current_idx + CCSDS_HEADER_LEN])
.ok_or(ByteConversionError::ZeroCopyFromError)?;
let (sp_header, _) = SpHeader::from_be_bytes(&slice[0..CCSDS_HEADER_LEN])?;
current_idx += CCSDS_HEADER_LEN;
let total_len = sph.total_len();
let total_len = sp_header.total_len();
if raw_data_len < total_len || total_len < PUS_TC_MIN_LEN_WITHOUT_APP_DATA {
return Err(PusError::RawDataTooShort(raw_data_len));
}
@ -426,7 +420,7 @@ impl<'slice> PusTc<'slice> {
current_idx += PUC_TC_SECONDARY_HEADER_LEN;
let raw_data = &slice[0..total_len];
let pus_tc = PusTc {
sp_header: SpHeader::from(sph),
sp_header,
sec_header: PusTcSecondaryHeader::try_from(sec_header).unwrap(),
raw_data: Some(raw_data),
app_data: user_data_from_raw(current_idx, total_len, raw_data_len, slice)?,

View File

@ -315,7 +315,6 @@ impl<'slice> PusTm<'slice> {
/// Write the raw PUS byte representation to a provided buffer.
pub fn write_to_bytes(&self, slice: &mut [u8]) -> Result<usize, PusError> {
let mut curr_idx = 0;
let sph_zc = crate::zc::SpHeader::from(self.sp_header);
let total_size = self.len_packed();
if total_size > slice.len() {
return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
@ -324,10 +323,8 @@ impl<'slice> PusTm<'slice> {
})
.into());
}
sph_zc
.to_bytes(&mut slice[curr_idx..curr_idx + CCSDS_HEADER_LEN])
.ok_or(ByteConversionError::ZeroCopyToError)?;
self.sp_header
.write_to_be_bytes(&mut slice[0..CCSDS_HEADER_LEN])?;
curr_idx += CCSDS_HEADER_LEN;
let sec_header_len = size_of::<zc::PusTmSecHeaderWithoutTimestamp>();
let sec_header = zc::PusTmSecHeaderWithoutTimestamp::try_from(self.sec_header).unwrap();
@ -401,11 +398,9 @@ impl<'slice> PusTm<'slice> {
return Err(PusError::RawDataTooShort(raw_data_len));
}
let mut current_idx = 0;
let sph =
crate::zc::SpHeader::from_bytes(&slice[current_idx..current_idx + CCSDS_HEADER_LEN])
.ok_or(ByteConversionError::ZeroCopyFromError)?;
let (sp_header, _) = SpHeader::from_be_bytes(&slice[0..CCSDS_HEADER_LEN])?;
current_idx += 6;
let total_len = sph.total_len();
let total_len = sp_header.total_len();
if raw_data_len < total_len || total_len < PUS_TM_MIN_LEN_WITHOUT_SOURCE_DATA {
return Err(PusError::RawDataTooShort(raw_data_len));
}
@ -421,7 +416,7 @@ impl<'slice> PusTm<'slice> {
current_idx += timestamp_len;
let raw_data = &slice[0..total_len];
let pus_tm = PusTm {
sp_header: SpHeader::from(sph),
sp_header,
sec_header: PusTmSecondaryHeader::try_from(zc_sec_header_wrapper).unwrap(),
raw_data: Some(&slice[0..total_len]),
source_data: user_data_from_raw(current_idx, total_len, raw_data_len, slice)?,