From e5a912c6369a8456954ea7f77b35c69d179f2812 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 1 Nov 2022 22:01:27 +0100 Subject: [PATCH] this works --- fsrc-core/src/util.rs | 97 +++++++++++++++++++++++++++++++++++ fsrc-core/tests/pus_events.rs | 34 +++++++++++- 2 files changed, 129 insertions(+), 2 deletions(-) diff --git a/fsrc-core/src/util.rs b/fsrc-core/src/util.rs index 27afd18..6622eb9 100644 --- a/fsrc-core/src/util.rs +++ b/fsrc-core/src/util.rs @@ -33,6 +33,36 @@ use core::mem::size_of; use paste::paste; pub use spacepackets::ecss::ToBeBytes; use spacepackets::ecss::{EcssEnumU16, EcssEnumU32, EcssEnumU64, EcssEnumU8}; +use spacepackets::ByteConversionError; +use spacepackets::SizeMissmatch; + +pub trait WritableAsBeBytes { + fn raw_len(&self) -> usize; + fn write_be(&self, buf: &mut [u8]) -> Result; +} + +macro_rules! param_to_be_bytes_impl { + ($Newtype: ident) => { + impl WritableAsBeBytes for $Newtype { + #[inline] + fn raw_len(&self) -> usize { + size_of::<::ByteArray>() + } + + fn write_be(&self, buf: &mut [u8]) -> Result { + let raw_len = self.raw_len(); + if buf.len() < raw_len { + return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch { + found: buf.len(), + expected: raw_len, + })); + } + buf[0..raw_len].copy_from_slice(&self.to_be_bytes()); + Ok(raw_len) + } + } + }; +} macro_rules! primitive_newtypes_with_eq { ($($ty: ty,)+) => { @@ -45,6 +75,10 @@ macro_rules! primitive_newtypes_with_eq { #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct [<$ty:upper Triplet>](pub $ty, pub $ty, pub $ty); + param_to_be_bytes_impl!([<$ty:upper>]); + param_to_be_bytes_impl!([<$ty:upper Pair>]); + param_to_be_bytes_impl!([<$ty:upper Triplet>]); + impl From<$ty> for [<$ty:upper>] { fn from(v: $ty) -> Self { Self(v) @@ -76,6 +110,10 @@ macro_rules! primitive_newtypes { #[derive(Debug, Copy, Clone, PartialEq)] pub struct [<$ty:upper Triplet>](pub $ty, pub $ty, pub $ty); + param_to_be_bytes_impl!([<$ty:upper>]); + param_to_be_bytes_impl!([<$ty:upper Pair>]); + param_to_be_bytes_impl!([<$ty:upper Triplet>]); + impl From<$ty> for [<$ty:upper>] { fn from(v: $ty) -> Self { Self(v) @@ -237,6 +275,65 @@ pub enum ParamsRaw { F64(F64), } +impl WritableAsBeBytes for ParamsRaw { + fn raw_len(&self) -> usize { + match self { + ParamsRaw::U8(v) => v.raw_len(), + ParamsRaw::U8Pair(v) => v.raw_len(), + ParamsRaw::U8Triplet(v) => v.raw_len(), + ParamsRaw::I8(v) => v.raw_len(), + ParamsRaw::I8Pair(v) => v.raw_len(), + ParamsRaw::I8Triplet(v) => v.raw_len(), + ParamsRaw::U16(v) => v.raw_len(), + ParamsRaw::U16Pair(v) => v.raw_len(), + ParamsRaw::U16Triplet(v) => v.raw_len(), + ParamsRaw::I16(v) => v.raw_len(), + ParamsRaw::I16Pair(v) => v.raw_len(), + ParamsRaw::I16Triplet(v) => v.raw_len(), + ParamsRaw::U32(v) => v.raw_len(), + ParamsRaw::U32Pair(v) => v.raw_len(), + ParamsRaw::U32Triplet(v) => v.raw_len(), + ParamsRaw::I32(v) => v.raw_len(), + ParamsRaw::I32Pair(v) => v.raw_len(), + ParamsRaw::I32Triplet(v) => v.raw_len(), + ParamsRaw::F32(v) => v.raw_len(), + ParamsRaw::F32Pair(v) => v.raw_len(), + ParamsRaw::F32Triplet(v) => v.raw_len(), + ParamsRaw::U64(v) => v.raw_len(), + ParamsRaw::I64(v) => v.raw_len(), + ParamsRaw::F64(v) => v.raw_len(), + } + } + + fn write_be(&self, buf: &mut [u8]) -> Result { + match self { + ParamsRaw::U8(v) => v.write_be(buf), + ParamsRaw::U8Pair(v) => v.write_be(buf), + ParamsRaw::U8Triplet(v) => v.write_be(buf), + ParamsRaw::I8(v) => v.write_be(buf), + ParamsRaw::I8Pair(v) => v.write_be(buf), + ParamsRaw::I8Triplet(v) => v.write_be(buf), + ParamsRaw::U16(v) => v.write_be(buf), + ParamsRaw::U16Pair(v) => v.write_be(buf), + ParamsRaw::U16Triplet(v) => v.write_be(buf), + ParamsRaw::I16(v) => v.write_be(buf), + ParamsRaw::I16Pair(v) => v.write_be(buf), + ParamsRaw::I16Triplet(v) => v.write_be(buf), + ParamsRaw::U32(v) => v.write_be(buf), + ParamsRaw::U32Pair(v) => v.write_be(buf), + ParamsRaw::U32Triplet(v) => v.write_be(buf), + ParamsRaw::I32(v) => v.write_be(buf), + ParamsRaw::I32Pair(v) => v.write_be(buf), + ParamsRaw::I32Triplet(v) => v.write_be(buf), + ParamsRaw::F32(v) => v.write_be(buf), + ParamsRaw::F32Pair(v) => v.write_be(buf), + ParamsRaw::F32Triplet(v) => v.write_be(buf), + ParamsRaw::U64(v) => v.write_be(buf), + ParamsRaw::I64(v) => v.write_be(buf), + ParamsRaw::F64(v) => v.write_be(buf), + } + } +} macro_rules! params_raw_from_newtype { ($($newtype: ident,)+) => { $( diff --git a/fsrc-core/tests/pus_events.rs b/fsrc-core/tests/pus_events.rs index 251f585..5d4f1c8 100644 --- a/fsrc-core/tests/pus_events.rs +++ b/fsrc-core/tests/pus_events.rs @@ -2,7 +2,7 @@ use fsrc_core::event_man::{EventManager, MpscEventReceiver, MpscEventU32SendProv use fsrc_core::events::{EventU32, EventU32TypedSev, Severity, SeverityInfo}; use fsrc_core::pus::event_man::{DefaultPusMgmtBackendProvider, EventReporter, PusEventTmManager}; use fsrc_core::pus::{EcssTmError, EcssTmSender}; -use fsrc_core::util::Params; +use fsrc_core::util::{Params, ParamsHeapless, ParamsRaw}; use spacepackets::tm::PusTm; use std::sync::mpsc::{channel, SendError, TryRecvError}; use std::thread; @@ -43,6 +43,7 @@ fn test_threaded_usage() { let jh0 = thread::spawn(move || { let mut sender = EventTmSender { sender: event_tx }; let mut event_cnt = 0; + let _params_array: [u8; 256] = [0; 256]; loop { let res = event_man.try_event_handling(); assert!(res.is_ok()); @@ -51,7 +52,36 @@ fn test_threaded_usage() { // TODO: Convert auxiliary data into raw byte format if let Some(aux_data) = aux_data { match aux_data { - Params::Heapless(_) => {} + Params::Heapless(heapless) => match heapless { + ParamsHeapless::Raw(raw) => match raw { + ParamsRaw::U8(_) => {} + ParamsRaw::U8Pair(_) => {} + ParamsRaw::U8Triplet(_) => {} + ParamsRaw::I8(_) => {} + ParamsRaw::I8Pair(_) => {} + ParamsRaw::I8Triplet(_) => {} + ParamsRaw::U16(_) => {} + ParamsRaw::U16Pair(_) => {} + ParamsRaw::U16Triplet(_) => {} + ParamsRaw::I16(_) => {} + ParamsRaw::I16Pair(_) => {} + ParamsRaw::I16Triplet(_) => {} + ParamsRaw::U32(_) => {} + ParamsRaw::U32Pair(_) => {} + ParamsRaw::U32Triplet(_) => {} + ParamsRaw::I32(_) => {} + ParamsRaw::I32Pair(_) => {} + ParamsRaw::I32Triplet(_) => {} + ParamsRaw::F32(_) => {} + ParamsRaw::F32Pair(_) => {} + ParamsRaw::F32Triplet(_) => {} + ParamsRaw::U64(_) => {} + ParamsRaw::I64(_) => {} + ParamsRaw::F64(_) => {} + }, + ParamsHeapless::EcssEnum(_) => {} + ParamsHeapless::Store(_) => {} + }, Params::Vec(_) => {} Params::String(_) => {} }