6 Commits

Author SHA1 Message Date
038755e56e set minimal data len in example
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2022-12-05 01:44:53 +01:00
33d78292c6 better example 2022-12-05 01:44:28 +01:00
8cf635d2fa additional useful functions 2022-12-05 01:41:35 +01:00
3420bcbeba Merge branch 'main' of https://egit.irs.uni-stuttgart.de/rust/spacepackets
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
2022-12-05 00:18:16 +01:00
6554241ed7 doc correction 2022-12-05 00:17:37 +01:00
283f9ff495 Merge pull request 'Various improvements' (#1) from various_improvements into main
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Reviewed-on: #1
2022-12-05 00:16:07 +01:00
4 changed files with 41 additions and 29 deletions

View File

@ -22,8 +22,7 @@ Currently, this includes the following components:
`spacepackets` supports various runtime environments and is also suitable for `no_std` environments. `spacepackets` supports various runtime environments and is also suitable for `no_std` environments.
It offers support for [`serde`](https://serde.rs/). The Space Packet, PUS TM and TC It also offers optional support for [`serde`](https://serde.rs/). This allows serializing and
implementations derive the `serde` `Serialize` and `Deserialize` trait. This allows serializing and
deserializing them with an appropriate `serde` provider like deserializing them with an appropriate `serde` provider like
[`postcard`](https://github.com/jamesmunns/postcard). [`postcard`](https://github.com/jamesmunns/postcard).

View File

@ -16,8 +16,7 @@
//! //!
//! `spacepackets` supports various runtime environments and is also suitable for `no_std` environments. //! `spacepackets` supports various runtime environments and is also suitable for `no_std` environments.
//! //!
//! It also offers support for [`serde`](https://serde.rs/). The Space Packet, PUS TM and TC //! It also offers optional support for [`serde`](https://serde.rs/). This allows serializing and
//! implementations derive the `serde` `Serialize` and `Deserialize` trait. This allows serializing and
//! deserializing them with an appropriate `serde` provider like //! deserializing them with an appropriate `serde` provider like
//! [`postcard`](https://github.com/jamesmunns/postcard). //! [`postcard`](https://github.com/jamesmunns/postcard).
//! //!
@ -40,8 +39,11 @@
//! //!
//! ```rust //! ```rust
//! use spacepackets::SpHeader; //! use spacepackets::SpHeader;
//! let sp_header = SpHeader::tc_unseg(0x42, 12, 0).expect("Error creating SP header"); //! let sp_header = SpHeader::tc_unseg(0x42, 12, 1).expect("Error creating CCSDS TC header");
//! println!("{:?}", sp_header); //! println!("{:?}", sp_header);
//! let mut ccsds_buf: [u8; 32] = [0; 32];
//! sp_header.write_to_be_bytes(&mut ccsds_buf).expect("Writing CCSDS TC header failed");
//! println!("{:x?}", &ccsds_buf[0..6]);
//! ``` //! ```
#![no_std] #![no_std]
#![cfg_attr(doc_cfg, feature(doc_cfg))] #![cfg_attr(doc_cfg, feature(doc_cfg))]
@ -499,16 +501,38 @@ impl SpHeader {
self.packet_id.ptype = packet_type; self.packet_id.ptype = packet_type;
} }
pub fn from_raw_slice(buf: &[u8]) -> Result<Self, ByteConversionError> { /// Create a struct from a raw slice where the fields have network endianness (big).
if buf.len() < CCSDS_HEADER_LEN + 1 { /// 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 { return Err(ByteConversionError::FromSliceTooSmall(SizeMissmatch {
found: buf.len(), found: buf.len(),
expected: CCSDS_HEADER_LEN + 1, expected: CCSDS_HEADER_LEN,
})); }));
} }
let zc_header = zc::SpHeader::from_bytes(&buf[0..CCSDS_HEADER_LEN]) let zc_header = zc::SpHeader::from_bytes(&buf[0..CCSDS_HEADER_LEN])
.ok_or(ByteConversionError::ZeroCopyFromError)?; .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. /// Write the raw PUS byte representation to a provided buffer.
pub fn write_to_bytes(&self, slice: &mut [u8]) -> Result<usize, PusError> { pub fn write_to_bytes(&self, slice: &mut [u8]) -> Result<usize, PusError> {
let mut curr_idx = 0; let mut curr_idx = 0;
let sph_zc = crate::zc::SpHeader::from(self.sp_header);
let tc_header_len = size_of::<zc::PusTcSecondaryHeader>(); let tc_header_len = size_of::<zc::PusTcSecondaryHeader>();
let total_size = self.len_packed(); let total_size = self.len_packed();
if total_size > slice.len() { if total_size > slice.len() {
@ -345,10 +344,7 @@ impl<'slice> PusTc<'slice> {
}) })
.into()); .into());
} }
sph_zc self.sp_header.write_to_be_bytes(slice)?;
.to_bytes(&mut slice[curr_idx..curr_idx + CCSDS_HEADER_LEN])
.ok_or(ByteConversionError::ZeroCopyToError)?;
curr_idx += CCSDS_HEADER_LEN; curr_idx += CCSDS_HEADER_LEN;
let sec_header = zc::PusTcSecondaryHeader::try_from(self.sec_header).unwrap(); let sec_header = zc::PusTcSecondaryHeader::try_from(self.sec_header).unwrap();
sec_header sec_header
@ -411,11 +407,9 @@ impl<'slice> PusTc<'slice> {
return Err(PusError::RawDataTooShort(raw_data_len)); return Err(PusError::RawDataTooShort(raw_data_len));
} }
let mut current_idx = 0; let mut current_idx = 0;
let sph = let (sp_header, _) = SpHeader::from_be_bytes(&slice[0..CCSDS_HEADER_LEN])?;
crate::zc::SpHeader::from_bytes(&slice[current_idx..current_idx + CCSDS_HEADER_LEN])
.ok_or(ByteConversionError::ZeroCopyFromError)?;
current_idx += 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 { if raw_data_len < total_len || total_len < PUS_TC_MIN_LEN_WITHOUT_APP_DATA {
return Err(PusError::RawDataTooShort(raw_data_len)); return Err(PusError::RawDataTooShort(raw_data_len));
} }
@ -426,7 +420,7 @@ impl<'slice> PusTc<'slice> {
current_idx += PUC_TC_SECONDARY_HEADER_LEN; current_idx += PUC_TC_SECONDARY_HEADER_LEN;
let raw_data = &slice[0..total_len]; let raw_data = &slice[0..total_len];
let pus_tc = PusTc { let pus_tc = PusTc {
sp_header: SpHeader::from(sph), sp_header,
sec_header: PusTcSecondaryHeader::try_from(sec_header).unwrap(), sec_header: PusTcSecondaryHeader::try_from(sec_header).unwrap(),
raw_data: Some(raw_data), raw_data: Some(raw_data),
app_data: user_data_from_raw(current_idx, total_len, raw_data_len, slice)?, 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. /// Write the raw PUS byte representation to a provided buffer.
pub fn write_to_bytes(&self, slice: &mut [u8]) -> Result<usize, PusError> { pub fn write_to_bytes(&self, slice: &mut [u8]) -> Result<usize, PusError> {
let mut curr_idx = 0; let mut curr_idx = 0;
let sph_zc = crate::zc::SpHeader::from(self.sp_header);
let total_size = self.len_packed(); let total_size = self.len_packed();
if total_size > slice.len() { if total_size > slice.len() {
return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch { return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
@ -324,10 +323,8 @@ impl<'slice> PusTm<'slice> {
}) })
.into()); .into());
} }
sph_zc self.sp_header
.to_bytes(&mut slice[curr_idx..curr_idx + CCSDS_HEADER_LEN]) .write_to_be_bytes(&mut slice[0..CCSDS_HEADER_LEN])?;
.ok_or(ByteConversionError::ZeroCopyToError)?;
curr_idx += CCSDS_HEADER_LEN; curr_idx += CCSDS_HEADER_LEN;
let sec_header_len = size_of::<zc::PusTmSecHeaderWithoutTimestamp>(); let sec_header_len = size_of::<zc::PusTmSecHeaderWithoutTimestamp>();
let sec_header = zc::PusTmSecHeaderWithoutTimestamp::try_from(self.sec_header).unwrap(); 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)); return Err(PusError::RawDataTooShort(raw_data_len));
} }
let mut current_idx = 0; let mut current_idx = 0;
let sph = let (sp_header, _) = SpHeader::from_be_bytes(&slice[0..CCSDS_HEADER_LEN])?;
crate::zc::SpHeader::from_bytes(&slice[current_idx..current_idx + CCSDS_HEADER_LEN])
.ok_or(ByteConversionError::ZeroCopyFromError)?;
current_idx += 6; 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 { if raw_data_len < total_len || total_len < PUS_TM_MIN_LEN_WITHOUT_SOURCE_DATA {
return Err(PusError::RawDataTooShort(raw_data_len)); return Err(PusError::RawDataTooShort(raw_data_len));
} }
@ -421,7 +416,7 @@ impl<'slice> PusTm<'slice> {
current_idx += timestamp_len; current_idx += timestamp_len;
let raw_data = &slice[0..total_len]; let raw_data = &slice[0..total_len];
let pus_tm = PusTm { let pus_tm = PusTm {
sp_header: SpHeader::from(sph), sp_header,
sec_header: PusTmSecondaryHeader::try_from(zc_sec_header_wrapper).unwrap(), sec_header: PusTmSecondaryHeader::try_from(zc_sec_header_wrapper).unwrap(),
raw_data: Some(&slice[0..total_len]), raw_data: Some(&slice[0..total_len]),
source_data: user_data_from_raw(current_idx, total_len, raw_data_len, slice)?, source_data: user_data_from_raw(current_idx, total_len, raw_data_len, slice)?,