continue tc write impl

This commit is contained in:
Robin Müller 2022-06-16 18:34:04 +02:00
parent a4234b90b8
commit 3075da0674
No known key found for this signature in database
GPG Key ID: BE6480244DFE612C
5 changed files with 92 additions and 27 deletions

View File

@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Run" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
<option name="command" value="run --package launchpad --bin launchpad" />
<configuration default="false" name="Run spacepackets" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
<option name="command" value="run --package spacepackets --bin spacepackets" />
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
<option name="channel" value="DEFAULT" />
<option name="requiredFeatures" value="true" />

View File

@ -1,4 +1,22 @@
use crate::CcsdsPacket;
use crc::{Crc, CRC_16_IBM_3740};
use serde::{Deserialize, Serialize};
/// CRC algorithm used by the PUS standard
pub const CRC_CCITT_FALSE: Crc<u16> = Crc::<u16>::new(&CRC_16_IBM_3740);
/// All PUS versions. Only PUS C is supported by this library
#[derive(PartialEq, Copy, Clone, Serialize, Deserialize, Debug)]
pub enum PusVersion {
EsaPus = 0,
PusA = 1,
PusC = 2,
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum PusError {
VersionNotSupported(PusVersion),
}
pub trait PusPacket: CcsdsPacket {
fn service(&self) -> u8;

View File

@ -235,8 +235,7 @@ pub trait CcsdsPrimaryHeader {
pub mod srd {
use crate::{
CcsdsPacket, CcsdsPrimaryHeader, PacketId, PacketSequenceCtrl, PacketType,
SequenceFlags,
CcsdsPacket, CcsdsPrimaryHeader, PacketId, PacketSequenceCtrl, PacketType, SequenceFlags,
};
/// Space Packet Primary Header according to CCSDS 133.0-B-2
@ -331,9 +330,7 @@ pub mod srd {
}
pub mod zc {
use crate::{
CcsdsPacket, CcsdsPrimaryHeader, PacketId, PacketSequenceCtrl, VERSION_MASK,
};
use crate::{CcsdsPacket, CcsdsPrimaryHeader, PacketId, PacketSequenceCtrl, VERSION_MASK};
use zerocopy::byteorder::NetworkEndian;
use zerocopy::{AsBytes, FromBytes, Unaligned, U16};

View File

@ -1,4 +1,4 @@
use postcard::to_stdvec;
use postcard::{from_bytes, to_stdvec};
use serde::{Deserialize, Serialize};
use zerocopy::byteorder::{I32, U16};
use zerocopy::{AsBytes, FromBytes, NetworkEndian, Unaligned};
@ -20,6 +20,13 @@ struct PostcardTest {
some_float: f32,
}
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct SliceSerTest<'slice> {
some_u8: u8,
some_u32: u32,
some_slice: &'slice [u8],
}
fn main() {
let pc_test = PostcardTest {
some_bool: true as u8,
@ -40,4 +47,21 @@ fn main() {
let mut slice = [0; 11];
sample_hk.write_to(slice.as_mut_slice());
println!("{:#04x?}", slice);
let ser_vec;
{
let test_buf = [0, 1, 2, 3];
let test_with_slice = SliceSerTest {
some_u8: 12,
some_u32: 1,
some_slice: test_buf.as_slice(),
};
ser_vec = to_stdvec(&test_with_slice).unwrap();
println!("{:#04x?}", out);
}
{
let test_deser: SliceSerTest = from_bytes(ser_vec.as_slice()).unwrap();
println!("{:?}", test_deser);
}
}

View File

@ -1,17 +1,6 @@
use crc::{Crc, CRC_16_IBM_3740};
use serde::{Deserialize, Serialize};
/// All PUS versions. Only PUS C is supported by this library
#[derive(PartialEq, Copy, Clone, Serialize, Deserialize)]
pub enum PusVersion {
EsaPus = 0,
PusA = 1,
PusC = 2,
}
pub const CRC_CCITT_FALSE: Crc<u16> = Crc::<u16>::new(&CRC_16_IBM_3740);
pub mod zc {
use crate::ecss::{PusError, PusVersion};
use crate::tc::srd;
use zerocopy::{AsBytes, FromBytes, NetworkEndian, Unaligned, U16};
#[derive(FromBytes, AsBytes, Unaligned)]
@ -22,12 +11,33 @@ pub mod zc {
subservice: u8,
source_id: U16<NetworkEndian>,
}
impl TryFrom<srd::PusTcDataFieldHeader> for PusTcDataFieldHeader {
type Error = PusError;
fn try_from(value: srd::PusTcDataFieldHeader) -> Result<Self, Self::Error> {
if value.version != PusVersion::PusC {
return Err(PusError::VersionNotSupported(value.version));
}
Ok(PusTcDataFieldHeader {
version_ack: ((value.version as u8) << 4) | value.ack,
service: value.service,
subservice: value.subservice,
source_id: U16::from(value.source_id),
})
}
}
impl PusTcDataFieldHeader {
pub fn to_bytes(&self, slice: &mut (impl AsMut<[u8]> + ?Sized)) -> Option<()> {
self.write_to(slice.as_mut())
}
}
}
pub mod srd {
use crate::ecss::PusPacket;
use crate::ecss::{PusPacket, PusVersion, CRC_CCITT_FALSE};
use crate::srd::SpHeader;
use crate::tc::{PusVersion, CRC_CCITT_FALSE};
use crate::tc::zc;
use crate::{CcsdsPacket, PacketError, PacketId, PacketSequenceCtrl, PacketType};
use serde::{Deserialize, Serialize};
@ -56,6 +66,7 @@ pub mod srd {
pub struct PusTc<'slice> {
pub sph: SpHeader,
pub data_field_header: PusTcDataFieldHeader,
#[serde(skip)]
raw_data: Option<&'slice [u8]>,
app_data: Option<&'slice [u8]>,
crc16: Option<u16>,
@ -78,15 +89,30 @@ pub mod srd {
}
}
pub fn write(&mut self, mut slice: impl AsMut<[u8]>) -> Result<(), PacketError> {
pub fn write(
&mut self,
slice: &mut (impl AsMut<[u8]> + ?Sized),
) -> Result<(), PacketError> {
let mut_slice = slice.as_mut();
let mut curr_idx = 0;
let sph_zc = crate::zc::SpHeader::from(self.sph);
if slice.as_mut().len() < 6 {
if mut_slice.len() < 6 {
return Err(PacketError::ToBytesSliceTooSmall(6));
}
sph_zc
.to_bytes(slice)
.to_bytes(&mut mut_slice[curr_idx..curr_idx + 6])
.ok_or(PacketError::ToBytesZeroCopyError)?;
// TODO: Finish impl
curr_idx += 6;
// The PUS version is hardcoded to PUS C
let pus_tc_header = zc::PusTcDataFieldHeader::try_from(self.data_field_header).unwrap();
let tc_header_len = std::mem::size_of::<PusTcDataFieldHeader>();
pus_tc_header
.to_bytes(&mut mut_slice[curr_idx..curr_idx + tc_header_len])
.ok_or(PacketError::ToBytesZeroCopyError)?;
curr_idx += tc_header_len;
if let Some(app_data) = self.app_data {
mut_slice[curr_idx..curr_idx + app_data.len()].copy_from_slice(app_data);
}
Ok(())
}
}