new ccsds header trait and zerocopy support

This commit is contained in:
Robin Müller 2022-06-14 14:56:18 +02:00
parent 7e26427058
commit 99c827c507
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814

316
src/sp.rs
View File

@ -1,5 +1,4 @@
//! # Space related components including CCSDS and ECSS packet standards //! # Space related components including CCSDS and ECSS packet standards
use ccsds_spacepacket::PrimaryHeader;
pub use ccsds_spacepacket::PrimaryHeader as DekuSpHeader; pub use ccsds_spacepacket::PrimaryHeader as DekuSpHeader;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -45,159 +44,242 @@ impl TryFrom<u8> for SequenceFlags {
} }
} }
/// Space Packet Primary Header according to CCSDS 133.0-B-2 /// Generic trait to access fields of a CCSDS space packet header according to CCSDS 133.0-B-2
#[derive(Serialize, Deserialize, Debug, PartialEq)] pub trait CcsdsPrimaryHeader {
pub struct SpHeader { const SEQ_FLAG_MASK: u16 = 0xC000;
pub version: u8,
pub ptype: PacketType, fn version(&self) -> u8;
pub apid: u16, /// Retrieve 13 bit Packet Identification field. Can usually be retrieved with a bitwise AND
pub secondary_header_flag: bool, /// of the first 2 bytes with 0x1FFF
pub sequence_flags: SequenceFlags, fn packet_id(&self) -> u16;
pub ssc: u16, /// Retrieve Packet Sequence Count
pub packet_data_len: u16, fn psc(&self) -> u16;
/// Retrieve data length field
fn data_len(&self) -> u16;
/// Retrieve Packet Type (TM: 0, TC: 1)
fn ptype(&self) -> PacketType {
// This call should never fail because only 0 and 1 can be passed to the try_from call
PacketType::try_from((self.packet_id() >> 13) as u8 & 0b1).unwrap()
}
/// Retrieve the secondary header flag. Returns true if a secondary header is present
/// and false if it is not
fn sec_header_flag(&self) -> bool {
(self.packet_id() >> 12) & 0x01 != 0
}
/// Retrieve Application Process ID
fn apid(&self) -> u16 {
self.packet_id() & 0x7FF
}
fn ssc(&self) -> u16 {
self.psc() & (!Self::SEQ_FLAG_MASK)
}
fn sequence_flags(&self) -> SequenceFlags {
// This call should never fail because the mask ensures that only valid values are passed
// into the try_from function
SequenceFlags::try_from(((self.psc() & Self::SEQ_FLAG_MASK) >> 14) as u8).unwrap()
}
} }
impl Default for SpHeader { pub mod srd {
fn default() -> Self { use crate::sp::PacketType;
SpHeader { use crate::sp::SequenceFlags;
version: 0,
ptype: PacketType::Tm, /// Space Packet Primary Header according to CCSDS 133.0-B-2
apid: 0, #[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)]
secondary_header_flag: true, pub struct SpHeader {
sequence_flags: SequenceFlags::Unsegmented, pub version: u8,
ssc: 0, pub ptype: PacketType,
packet_data_len: 0, pub apid: u16,
pub sec_header_flag: bool,
pub seq_flags: SequenceFlags,
pub ssc: u16,
pub data_len: u16,
}
impl Default for SpHeader {
fn default() -> Self {
SpHeader {
version: 0,
ptype: PacketType::Tm,
apid: 0,
sec_header_flag: true,
seq_flags: SequenceFlags::Unsegmented,
ssc: 0,
data_len: 0,
}
}
}
impl SpHeader {
pub fn new(apid: u16, ptype: PacketType, ssc: u16) -> Option<Self> {
if ssc > num::pow(2, 14) || apid > num::pow(2, 11) {
return None;
}
let mut header = SpHeader::default();
header.ptype = ptype;
header.apid = apid;
header.ssc = ssc;
Some(header)
}
pub fn tm(apid: u16, ssc: u16) -> Option<Self> {
Self::new(apid, PacketType::Tm, ssc)
}
pub fn tc(apid: u16, ssc: u16) -> Option<Self> {
Self::new(apid, PacketType::Tc, ssc)
}
/// Function to retrieve the packet sequence control field
#[inline]
pub fn psc(&self) -> u16 {
((self.seq_flags as u16) << 14) | self.sec_header_flag as u16
}
/// Retrieve Packet Identification composite field
#[inline]
pub fn packet_id(&self) -> u16 {
((self.ptype as u16) << 13) | ((self.sec_header_flag as u16) << 12) | self.apid
}
#[inline]
pub fn is_tm(&self) -> bool {
self.ptype == PacketType::Tm
}
#[inline]
pub fn is_tc(&self) -> bool {
self.ptype == PacketType::Tc
} }
} }
} }
impl SpHeader {
pub fn new(apid: u16, ptype: PacketType, ssc: u16) -> Option<Self> { pub mod zc {
if ssc > num::pow(2, 14) || apid > num::pow(2, 11) { use crate::sp::CcsdsPrimaryHeader;
return None; use zerocopy::byteorder::NetworkEndian;
use zerocopy::{AsBytes, FromBytes, Unaligned, U16};
#[derive(FromBytes, AsBytes, Unaligned)]
#[repr(C)]
struct SpHeader {
version_packet_id: U16<NetworkEndian>,
psc: U16<NetworkEndian>,
data_len: U16<NetworkEndian>,
}
impl CcsdsPrimaryHeader for SpHeader {
fn version(&self) -> u8 {
((self.version_packet_id.get() >> 13) as u8) & 0b111
} }
let mut header = SpHeader::default();
header.ptype = ptype;
header.apid = apid;
header.ssc = ssc;
Some(header)
}
pub fn tm(apid: u16, ssc: u16) -> Option<Self> { fn packet_id(&self) -> u16 {
Self::new(apid, PacketType::Tm, ssc) self.version_packet_id.get() & 0x1FFF
} }
pub fn tc(apid: u16, ssc: u16) -> Option<Self> { fn psc(&self) -> u16 {
Self::new(apid, PacketType::Tc, ssc) self.psc.get()
} }
/// Function to retrieve the packet sequence control field fn data_len(&self) -> u16 {
#[inline] self.data_len.get()
pub fn psc(&self) -> u16 { }
((self.sequence_flags as u16) << 14) | self.secondary_header_flag as u16
}
/// Retrieve Packet Identification composite field
#[inline]
pub fn packet_id(&self) -> u16 {
((self.ptype as u16) << 13) | ((self.secondary_header_flag as u16) << 12) | self.apid
}
#[inline]
pub fn is_tm(&self) -> bool {
self.ptype == PacketType::Tm
}
#[inline]
pub fn is_tc(&self) -> bool {
self.ptype == PacketType::Tc
} }
} }
/// The [DekuSpHeader] is very useful to deserialize a packed raw space packet header with 6 bytes. pub mod deku {
/// This function allows converting it to the [SpHeader] which is compatible to the [serde] use crate::sp::srd::SpHeader;
/// framework use crate::sp::{DekuSpHeader, PacketType, SequenceFlags};
impl TryFrom<DekuSpHeader> for SpHeader { use ccsds_spacepacket::PrimaryHeader;
type Error = ();
fn try_from(header: PrimaryHeader) -> Result<Self, Self::Error> { /// The [DekuSpHeader] is very useful to deserialize a packed raw space packet header with 6 bytes.
let seq_num = SequenceFlags::try_from(header.sequence_flags as u8)?; /// This function allows converting it to the [SpHeader] which is compatible to the [serde]
let packet_type = PacketType::try_from(header.packet_type as u8)?; /// framework
let sec_header_flag = header.sec_header_flag as u8 != 0; impl TryFrom<DekuSpHeader> for SpHeader {
Ok(SpHeader { type Error = ();
version: header.version,
sequence_flags: seq_num, fn try_from(header: PrimaryHeader) -> Result<Self, Self::Error> {
packet_data_len: header.data_length, let seq_num = SequenceFlags::try_from(header.sequence_flags as u8)?;
ssc: header.sequence_count, let packet_type = PacketType::try_from(header.packet_type as u8)?;
ptype: packet_type, let sec_header_flag = header.sec_header_flag as u8 != 0;
apid: header.app_proc_id, Ok(SpHeader {
secondary_header_flag: sec_header_flag, version: header.version,
}) seq_flags: seq_num,
data_len: header.data_length,
ssc: header.sequence_count,
ptype: packet_type,
apid: header.app_proc_id,
sec_header_flag,
})
}
} }
}
/// It is possible to convert the [serde] compatible [SpHeader] back into a [DekuSpHeader] /// It is possible to convert the [serde] compatible [SpHeader] back into a [DekuSpHeader]
/// to allow for packed binary serialization /// to allow for packed binary serialization
impl TryFrom<SpHeader> for DekuSpHeader { impl TryFrom<SpHeader> for DekuSpHeader {
type Error = (); type Error = ();
fn try_from(value: SpHeader) -> Result<Self, Self::Error> { fn try_from(value: SpHeader) -> Result<Self, Self::Error> {
use ccsds_spacepacket::types::PacketType as DekuPacketType; use ccsds_spacepacket::types::PacketType as DekuPacketType;
use ccsds_spacepacket::types::SecondaryHeaderFlag as DekuSecHeaderFlag; use ccsds_spacepacket::types::SecondaryHeaderFlag as DekuSecHeaderFlag;
use ccsds_spacepacket::types::SeqFlag as DekuSeqFlag; use ccsds_spacepacket::types::SeqFlag as DekuSeqFlag;
let sequence_flags = match value.sequence_flags as u8 { let sequence_flags = match value.seq_flags as u8 {
x if x == SequenceFlags::Unsegmented as u8 => DekuSeqFlag::Unsegmented, x if x == SequenceFlags::Unsegmented as u8 => DekuSeqFlag::Unsegmented,
x if x == SequenceFlags::FirstSegment as u8 => DekuSeqFlag::FirstSegment, x if x == SequenceFlags::FirstSegment as u8 => DekuSeqFlag::FirstSegment,
x if x == SequenceFlags::LastSegment as u8 => DekuSeqFlag::LastSegment, x if x == SequenceFlags::LastSegment as u8 => DekuSeqFlag::LastSegment,
x if x == SequenceFlags::ContinuationSegment as u8 => DekuSeqFlag::Continuation, x if x == SequenceFlags::ContinuationSegment as u8 => DekuSeqFlag::Continuation,
_ => return Err(()), _ => return Err(()),
}; };
let packet_type = match value.ptype as u8 { let packet_type = match value.ptype as u8 {
x if x == PacketType::Tm as u8 => DekuPacketType::Data, x if x == PacketType::Tm as u8 => DekuPacketType::Data,
x if x == PacketType::Tc as u8 => DekuPacketType::Command, x if x == PacketType::Tc as u8 => DekuPacketType::Command,
_ => return Err(()), _ => return Err(()),
}; };
let sec_header_flag = match value.secondary_header_flag as bool { let sec_header_flag = match value.sec_header_flag as bool {
true => DekuSecHeaderFlag::Present, true => DekuSecHeaderFlag::Present,
false => DekuSecHeaderFlag::NotPresent, false => DekuSecHeaderFlag::NotPresent,
}; };
Ok(DekuSpHeader { Ok(DekuSpHeader {
version: value.version, version: value.version,
packet_type, packet_type,
sec_header_flag, sec_header_flag,
app_proc_id: value.apid, app_proc_id: value.apid,
sequence_flags, sequence_flags,
data_length: value.packet_data_len, data_length: value.data_len,
sequence_count: value.ssc, sequence_count: value.ssc,
}) })
}
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::sp::{DekuSpHeader, PacketType, SequenceFlags, SpHeader}; use crate::sp::srd::SpHeader;
use deku::prelude::*; use crate::sp::{DekuSpHeader, PacketType, SequenceFlags};
use postcard::{from_bytes, to_stdvec}; use postcard::{from_bytes, to_stdvec};
#[test] #[test]
fn test_deser_internally() { fn test_deser_internally() {
let sp_header = SpHeader::tc(0x42, 12).expect("Error creating SP header"); let sp_header = SpHeader::tc(0x42, 12).expect("Error creating SP header");
assert_eq!(sp_header.version, 0b000); assert_eq!(sp_header.version, 0b000);
assert_eq!(sp_header.secondary_header_flag, true); assert_eq!(sp_header.sec_header_flag, true);
assert_eq!(sp_header.ptype, PacketType::Tc); assert_eq!(sp_header.ptype, PacketType::Tc);
assert_eq!(sp_header.ssc, 12); assert_eq!(sp_header.ssc, 12);
assert_eq!(sp_header.apid, 0x42); assert_eq!(sp_header.apid, 0x42);
assert_eq!(sp_header.sequence_flags, SequenceFlags::Unsegmented); assert_eq!(sp_header.seq_flags, SequenceFlags::Unsegmented);
assert_eq!(sp_header.packet_data_len, 0); assert_eq!(sp_header.data_len, 0);
let output = to_stdvec(&sp_header).unwrap(); let output = to_stdvec(&sp_header).unwrap();
println!("Output: {:?} with length {}", output, output.len()); println!("Output: {:?} with length {}", output, output.len());
let sp_header: SpHeader = from_bytes(&output).unwrap(); let sp_header: SpHeader = from_bytes(&output).unwrap();
assert_eq!(sp_header.version, 0b000); assert_eq!(sp_header.version, 0b000);
assert_eq!(sp_header.secondary_header_flag, true); assert_eq!(sp_header.sec_header_flag, true);
assert_eq!(sp_header.ptype, PacketType::Tc); assert_eq!(sp_header.ptype, PacketType::Tc);
assert_eq!(sp_header.ssc, 12); assert_eq!(sp_header.ssc, 12);
assert_eq!(sp_header.apid, 0x42); assert_eq!(sp_header.apid, 0x42);
assert_eq!(sp_header.sequence_flags, SequenceFlags::Unsegmented); assert_eq!(sp_header.seq_flags, SequenceFlags::Unsegmented);
assert_eq!(sp_header.packet_data_len, 0); assert_eq!(sp_header.data_len, 0);
} }
#[test] #[test]