Compare commits
6 Commits
3a71b00198
...
038755e56e
Author | SHA1 | Date | |
---|---|---|---|
038755e56e | |||
33d78292c6 | |||
8cf635d2fa | |||
3420bcbeba | |||
6554241ed7 | |||
283f9ff495 |
@ -22,8 +22,7 @@ Currently, this includes the following components:
|
||||
|
||||
`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
|
||||
implementations derive the `serde` `Serialize` and `Deserialize` trait. This allows serializing and
|
||||
It also offers optional support for [`serde`](https://serde.rs/). This allows serializing and
|
||||
deserializing them with an appropriate `serde` provider like
|
||||
[`postcard`](https://github.com/jamesmunns/postcard).
|
||||
|
||||
|
38
src/lib.rs
38
src/lib.rs
@ -16,8 +16,7 @@
|
||||
//!
|
||||
//! `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
|
||||
//! implementations derive the `serde` `Serialize` and `Deserialize` trait. This allows serializing and
|
||||
//! It also offers optional support for [`serde`](https://serde.rs/). This allows serializing and
|
||||
//! deserializing them with an appropriate `serde` provider like
|
||||
//! [`postcard`](https://github.com/jamesmunns/postcard).
|
||||
//!
|
||||
@ -40,8 +39,11 @@
|
||||
//!
|
||||
//! ```rust
|
||||
//! 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);
|
||||
//! 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]
|
||||
#![cfg_attr(doc_cfg, feature(doc_cfg))]
|
||||
@ -499,16 +501,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..])
|
||||
}
|
||||
}
|
||||
|
||||
|
14
src/tc.rs
14
src/tc.rs
@ -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)?,
|
||||
|
15
src/tm.rs
15
src/tm.rs
@ -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)?,
|
||||
|
Reference in New Issue
Block a user