From 83db7109500adfbe59ddf77b011a1c8abb0de9c2 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 16 Aug 2023 16:27:10 +0200 Subject: [PATCH 1/9] update LV and TLV code --- CHANGELOG.md | 9 +++++ src/cfdp/lv.rs | 87 ++++++++++++++++++++++++++------------------- src/cfdp/pdu/mod.rs | 2 ++ src/cfdp/tlv.rs | 42 +++++++++++++--------- 4 files changed, 88 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec5652d..08dadaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/). # [unreleased] +## Changed + +- The `Tlv` and `Lv` API return `&[u8]` instead of `Option<&[u8]>`. + +## Added + +- Added `raw_data` API for `Tlv` and `Lv` to retrieve the whole `Lv`/`Tlv` slice if the object + was created from a raw bytestream. + # [v0.7.0-beta.0] 2023-08-16 - Moved MSRV from v1.60 to v1.61. diff --git a/src/cfdp/lv.rs b/src/cfdp/lv.rs index 74f0f17..4d91094 100644 --- a/src/cfdp/lv.rs +++ b/src/cfdp/lv.rs @@ -15,10 +15,19 @@ pub const MIN_LV_LEN: usize = 1; /// * `data`: If the LV is generated from a raw bytestream, this will be the lifetime of /// the raw bytestream. If the LV is generated from a raw slice or a similar data reference, /// this will be the lifetime of that data reference. -#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[derive(Debug, Copy, Clone, Eq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Lv<'data> { - data: Option<&'data [u8]>, + data: &'data [u8], + // If the LV was generated from a raw bytestream, this will contain the start of the + // full LV. + pub(crate) raw_data: Option<&'data [u8]>, +} + +impl PartialEq for Lv<'_> { + fn eq(&self, other: &Self) -> bool { + self.data == other.data + } } pub(crate) fn generic_len_check_data_serialization( @@ -53,12 +62,18 @@ impl<'data> Lv<'data> { if data.len() > u8::MAX as usize { return Err(TlvLvError::DataTooLarge(data.len())); } - Ok(Lv { data: Some(data) }) + Ok(Lv { + data, + raw_data: None, + }) } /// Creates a LV with an empty value field. pub fn new_empty() -> Lv<'data> { - Lv { data: None } + Lv { + data: &[], + raw_data: None, + } } /// Helper function to build a string LV. This is especially useful for the file or directory @@ -77,10 +92,7 @@ impl<'data> Lv<'data> { /// Returns the length of the value part, not including the length byte. pub fn len_value(&self) -> usize { - if self.data.is_none() { - return 0; - } - self.data.unwrap().len() + self.data.len() } /// Returns the full raw length, including the length byte. @@ -90,18 +102,26 @@ impl<'data> Lv<'data> { /// Checks whether the value field is empty. pub fn is_empty(&self) -> bool { - self.data.is_none() + self.data.len() == 0 } - pub fn value(&self) -> Option<&[u8]> { + pub fn value(&self) -> &[u8] { self.data } + /// If the LV was generated from a raw bytestream using [Self::from_bytes], the raw start + /// of the LV can be retrieved with this method. + pub fn raw_data(&self) -> Option<&[u8]> { + self.raw_data + } + /// Convenience function to extract the value as a [str]. This is useful if the LV is /// known to contain a [str], for example being a file name. pub fn value_as_str(&self) -> Option> { - self.data?; - Some(core::str::from_utf8(self.data.unwrap())) + if self.is_empty() { + return None; + } + Some(core::str::from_utf8(self.data)) } /// Writes the LV to a raw buffer. Please note that the first byte will contain the length @@ -118,15 +138,14 @@ impl<'data> Lv<'data> { } pub(crate) fn write_to_be_bytes_no_len_check(&self, buf: &mut [u8]) -> usize { - if self.data.is_none() { + if self.is_empty() { buf[0] = 0; return MIN_LV_LEN; } - let data = self.data.unwrap(); // Length check in constructor ensures the length always has a valid value. - buf[0] = data.len() as u8; - buf[MIN_LV_LEN..data.len() + MIN_LV_LEN].copy_from_slice(data); - MIN_LV_LEN + data.len() + buf[0] = self.data.len() as u8; + buf[MIN_LV_LEN..self.data.len() + MIN_LV_LEN].copy_from_slice(self.data); + MIN_LV_LEN + self.data.len() } pub(crate) fn from_be_bytes_no_len_check( @@ -134,11 +153,10 @@ impl<'data> Lv<'data> { ) -> Result, ByteConversionError> { let value_len = buf[0] as usize; generic_len_check_deserialization(buf, value_len + MIN_LV_LEN)?; - let mut data = None; - if value_len > 0 { - data = Some(&buf[MIN_LV_LEN..MIN_LV_LEN + value_len]) - } - Ok(Self { data }) + Ok(Self { + data: &buf[MIN_LV_LEN..MIN_LV_LEN + value_len], + raw_data: Some(buf), + }) } } @@ -155,8 +173,8 @@ pub mod tests { let lv_res = Lv::new(&lv_data); assert!(lv_res.is_ok()); let lv = lv_res.unwrap(); - assert!(lv.value().is_some()); - let val = lv.value().unwrap(); + assert!(lv.value().len() > 0); + let val = lv.value(); assert_eq!(val[0], 1); assert_eq!(val[1], 2); assert_eq!(val[2], 3); @@ -172,7 +190,6 @@ pub mod tests { assert_eq!(lv_empty.len_value(), 0); assert_eq!(lv_empty.len_full(), 1); assert!(lv_empty.is_empty()); - assert_eq!(lv_empty.value(), None); let mut buf: [u8; 4] = [0xff; 4]; let res = lv_empty.write_to_be_bytes(&mut buf); assert!(res.is_ok()); @@ -211,10 +228,9 @@ pub mod tests { assert!(lv.is_ok()); let lv = lv.unwrap(); assert!(!lv.is_empty()); - assert!(lv.value().is_some()); assert_eq!(lv.len_value(), 4); assert_eq!(lv.len_full(), 5); - let val = lv.value().unwrap(); + let val = lv.value(); assert_eq!(val[0], 1); assert_eq!(val[1], 2); assert_eq!(val[2], 3); @@ -228,7 +244,6 @@ pub mod tests { assert!(lv_empty.is_ok()); let lv_empty = lv_empty.unwrap(); assert!(lv_empty.is_empty()); - assert!(lv_empty.value().is_none()); } #[test] @@ -282,14 +297,14 @@ pub mod tests { let res = res.unwrap(); assert_eq!(res, 8 + 1); assert_eq!(buf[0], 8); - assert_eq!(buf[1], 't' as u8); - assert_eq!(buf[2], 'e' as u8); - assert_eq!(buf[3], 's' as u8); - assert_eq!(buf[4], 't' as u8); - assert_eq!(buf[5], '.' as u8); - assert_eq!(buf[6], 'b' as u8); - assert_eq!(buf[7], 'i' as u8); - assert_eq!(buf[8], 'n' as u8); + assert_eq!(buf[1], b't'); + assert_eq!(buf[2], b'e'); + assert_eq!(buf[3], b's'); + assert_eq!(buf[4], b't'); + assert_eq!(buf[5], b'.'); + assert_eq!(buf[6], b'b'); + assert_eq!(buf[7], b'i'); + assert_eq!(buf[8], b'n'); } #[test] fn test_str_helper() { diff --git a/src/cfdp/pdu/mod.rs b/src/cfdp/pdu/mod.rs index c0e1e20..6d93da9 100644 --- a/src/cfdp/pdu/mod.rs +++ b/src/cfdp/pdu/mod.rs @@ -718,6 +718,8 @@ mod tests { assert_eq!(default_conf.crc_flag, CrcFlag::NoCrc); assert_eq!(default_conf.file_flag, LargeFileFlag::Normal); } + + #[test] fn test_pdu_header_setter() { let src_id = UnsignedByteFieldU8::new(1); let dest_id = UnsignedByteFieldU8::new(2); diff --git a/src/cfdp/tlv.rs b/src/cfdp/tlv.rs index 4ec2580..b0e3e24 100644 --- a/src/cfdp/tlv.rs +++ b/src/cfdp/tlv.rs @@ -109,7 +109,7 @@ impl<'data> Tlv<'data> { Ok(self.len_full()) } - pub fn value(&self) -> Option<&[u8]> { + pub fn value(&self) -> &[u8] { self.lv.value() } @@ -134,10 +134,20 @@ impl<'data> Tlv<'data> { /// [Self::len_full]. pub fn from_bytes(buf: &'data [u8]) -> Result, TlvLvError> { generic_len_check_deserialization(buf, MIN_TLV_LEN)?; - Ok(Self { + let mut tlv = Self { tlv_type_field: TlvTypeField::from(buf[0]), lv: Lv::from_bytes(&buf[MIN_LV_LEN..])?, - }) + }; + // We re-use this field so we do not need an additional struct field to store the raw start + // of the TLV. + tlv.lv.raw_data = Some(buf); + Ok(tlv) + } + + /// If the TLV was generated from a raw bytestream using [Self::from_bytes], the raw start + /// of the TLV can be retrieved with this method. + pub fn raw_data(&self) -> Option<&[u8]> { + self.lv.raw_data() } } @@ -242,13 +252,14 @@ impl<'data> TryFrom> for EntityIdTlv { return Err(TlvLvError::InvalidValueLength(value.len_value())); } Ok(Self::new( - UnsignedByteField::new_from_be_bytes(value.len_value(), value.value().unwrap()) - .map_err(|e| match e { + UnsignedByteField::new_from_be_bytes(value.len_value(), value.value()).map_err( + |e| match e { UnsignedByteFieldError::ByteConversionError(e) => e, // This can not happen, we checked for the length validity, and the data is always smaller than // 255 bytes. _ => panic!("unexpected error"), - })?, + }, + )?, )) } } @@ -454,8 +465,8 @@ mod tests { use crate::cfdp::TlvLvError; use crate::util::{UbfU8, UnsignedEnum}; - const TLV_TEST_STR_0: &'static str = "hello.txt"; - const TLV_TEST_STR_1: &'static str = "hello2.txt"; + const TLV_TEST_STR_0: &str = "hello.txt"; + const TLV_TEST_STR_1: &str = "hello2.txt"; #[test] fn test_basic() { @@ -472,8 +483,7 @@ mod tests { assert_eq!(tlv_res.len_full(), 3); assert_eq!(tlv_res.len_value(), 1); assert!(!tlv_res.is_empty()); - assert!(tlv_res.value().is_some()); - assert_eq!(tlv_res.value().unwrap()[0], 5); + assert_eq!(tlv_res.value()[0], 5); } #[test] @@ -507,14 +517,14 @@ mod tests { ); assert_eq!(tlv_from_raw.len_value(), 1); assert_eq!(tlv_from_raw.len_full(), 3); - assert!(tlv_from_raw.value().is_some()); - assert_eq!(tlv_from_raw.value().unwrap()[0], 5); + assert!(tlv_from_raw.value().len() > 0); + assert_eq!(tlv_from_raw.value()[0], 5); } #[test] fn test_empty() { let tlv_empty = Tlv::new_empty(TlvType::MsgToUser); - assert!(tlv_empty.value().is_none()); + assert_eq!(tlv_empty.value().len(), 0); assert!(tlv_empty.is_empty()); assert_eq!(tlv_empty.len_full(), 2); assert_eq!(tlv_empty.len_value(), 0); @@ -538,11 +548,11 @@ mod tests { let mut buf: [u8; 4] = [0; 4]; buf[0] = TlvType::MsgToUser as u8; buf[1] = 0; - let tlv_empty = Tlv::from_bytes(&mut buf); + let tlv_empty = Tlv::from_bytes(&buf); assert!(tlv_empty.is_ok()); let tlv_empty = tlv_empty.unwrap(); assert!(tlv_empty.is_empty()); - assert!(tlv_empty.value().is_none()); + assert_eq!(tlv_empty.value().len(), 0); assert_eq!( tlv_empty.tlv_type_field(), TlvTypeField::Standard(TlvType::MsgToUser) @@ -570,7 +580,7 @@ mod tests { buf[0] = 3; buf[1] = 1; buf[2] = 5; - let tlv = Tlv::from_bytes(&mut buf); + let tlv = Tlv::from_bytes(&buf); assert!(tlv.is_ok()); let tlv = tlv.unwrap(); assert_eq!(tlv.tlv_type_field(), TlvTypeField::Custom(3)); From 4e2c0f1aa70da8d0bfcaaffaf3ad8b1f781f2c3d Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 16 Aug 2023 16:36:15 +0200 Subject: [PATCH 2/9] added a few additional tests --- src/cfdp/lv.rs | 2 ++ src/cfdp/tlv.rs | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/cfdp/lv.rs b/src/cfdp/lv.rs index 4d91094..67c2269 100644 --- a/src/cfdp/lv.rs +++ b/src/cfdp/lv.rs @@ -230,6 +230,8 @@ pub mod tests { assert!(!lv.is_empty()); assert_eq!(lv.len_value(), 4); assert_eq!(lv.len_full(), 5); + assert!(lv.raw_data().is_some()); + assert_eq!(lv.raw_data().unwrap(), buf); let val = lv.value(); assert_eq!(val[0], 1); assert_eq!(val[1], 2); diff --git a/src/cfdp/tlv.rs b/src/cfdp/tlv.rs index b0e3e24..e9015ab 100644 --- a/src/cfdp/tlv.rs +++ b/src/cfdp/tlv.rs @@ -508,16 +508,17 @@ mod tests { assert!(entity_id.write_to_be_bytes(&mut buf[2..]).is_ok()); buf[0] = TlvType::EntityId as u8; buf[1] = 1; - let tlv_from_raw = Tlv::from_bytes(&mut buf); + let tlv_from_raw = Tlv::from_bytes(&buf); assert!(tlv_from_raw.is_ok()); let tlv_from_raw = tlv_from_raw.unwrap(); + assert!(tlv_from_raw.raw_data().is_some()); + assert_eq!(tlv_from_raw.raw_data().unwrap(), buf); assert_eq!( tlv_from_raw.tlv_type_field(), TlvTypeField::Standard(TlvType::EntityId) ); assert_eq!(tlv_from_raw.len_value(), 1); assert_eq!(tlv_from_raw.len_full(), 3); - assert!(tlv_from_raw.value().len() > 0); assert_eq!(tlv_from_raw.value()[0], 5); } From 3cb19298c829c4c9f070f01854646ab9fd528229 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 16 Aug 2023 17:27:02 +0200 Subject: [PATCH 3/9] some restructuring --- src/cfdp/{tlv.rs => tlv/mod.rs} | 48 +++++++++++++----- src/cfdp/tlv/msg_to_user.rs | 90 +++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 13 deletions(-) rename src/cfdp/{tlv.rs => tlv/mod.rs} (95%) create mode 100644 src/cfdp/tlv/msg_to_user.rs diff --git a/src/cfdp/tlv.rs b/src/cfdp/tlv/mod.rs similarity index 95% rename from src/cfdp/tlv.rs rename to src/cfdp/tlv/mod.rs index e9015ab..d378d4d 100644 --- a/src/cfdp/tlv.rs +++ b/src/cfdp/tlv/mod.rs @@ -9,6 +9,8 @@ use num_enum::{IntoPrimitive, TryFromPrimitive}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; +pub mod msg_to_user; + pub const MIN_TLV_LEN: usize = 2; #[derive(Debug, Copy, Clone, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)] @@ -98,12 +100,30 @@ impl<'data> Tlv<'data> { } } + /// Checks whether the type field contains one of the standard types specified in the CFDP + /// standard and is part of the [TlvType] enum. + pub fn is_standard_tlv(&self) -> bool { + if let TlvTypeField::Standard(_) = self.tlv_type_field { + return true; + } + false + } + + /// Returns the standard TLV type if the TLV field is not a custom field + pub fn tlv_type(&self) -> Option { + if let TlvTypeField::Standard(tlv_type) = self.tlv_type_field { + Some(tlv_type) + } else { + None + } + } + pub fn tlv_type_field(&self) -> TlvTypeField { self.tlv_type_field } pub fn write_to_bytes(&self, buf: &mut [u8]) -> Result { - generic_len_check_data_serialization(buf, self.len_value(), MIN_TLV_LEN)?; + generic_len_check_data_serialization(buf, self.value().len(), MIN_TLV_LEN)?; buf[0] = self.tlv_type_field.into(); self.lv.write_to_be_bytes_no_len_check(&mut buf[1..]); Ok(self.len_full()) @@ -113,7 +133,8 @@ impl<'data> Tlv<'data> { self.lv.value() } - /// Returns the length of the value part, not including the length byte. + /// Helper method to retrieve the length of the value. Simply calls the [slice::len] method of + /// [Self::value] pub fn len_value(&self) -> usize { self.lv.len_value() } @@ -244,15 +265,15 @@ impl<'data> TryFrom> for EntityIdTlv { ))); } } - if value.len_value() != 1 - && value.len_value() != 2 - && value.len_value() != 4 - && value.len_value() != 8 - { - return Err(TlvLvError::InvalidValueLength(value.len_value())); + let len_value = value.value().len(); + if len_value != 1 + && len_value != 2 + && len_value != 4 + && len_value != 8 { + return Err(TlvLvError::InvalidValueLength(len_value)); } Ok(Self::new( - UnsignedByteField::new_from_be_bytes(value.len_value(), value.value()).map_err( + UnsignedByteField::new_from_be_bytes(len_value, value.value()).map_err( |e| match e { UnsignedByteFieldError::ByteConversionError(e) => e, // This can not happen, we checked for the length validity, and the data is always smaller than @@ -481,6 +502,7 @@ mod tests { TlvTypeField::Standard(TlvType::EntityId) ); assert_eq!(tlv_res.len_full(), 3); + assert_eq!(tlv_res.value().len(), 1); assert_eq!(tlv_res.len_value(), 1); assert!(!tlv_res.is_empty()); assert_eq!(tlv_res.value()[0], 5); @@ -517,7 +539,7 @@ mod tests { tlv_from_raw.tlv_type_field(), TlvTypeField::Standard(TlvType::EntityId) ); - assert_eq!(tlv_from_raw.len_value(), 1); + assert_eq!(tlv_from_raw.value().len(), 1); assert_eq!(tlv_from_raw.len_full(), 3); assert_eq!(tlv_from_raw.value()[0], 5); } @@ -528,7 +550,7 @@ mod tests { assert_eq!(tlv_empty.value().len(), 0); assert!(tlv_empty.is_empty()); assert_eq!(tlv_empty.len_full(), 2); - assert_eq!(tlv_empty.len_value(), 0); + assert!(tlv_empty.value().is_empty()); assert_eq!( tlv_empty.tlv_type_field(), TlvTypeField::Standard(TlvType::MsgToUser) @@ -559,7 +581,7 @@ mod tests { TlvTypeField::Standard(TlvType::MsgToUser) ); assert_eq!(tlv_empty.len_full(), 2); - assert_eq!(tlv_empty.len_value(), 0); + assert!(tlv_empty.value().is_empty()); } #[test] @@ -585,7 +607,7 @@ mod tests { assert!(tlv.is_ok()); let tlv = tlv.unwrap(); assert_eq!(tlv.tlv_type_field(), TlvTypeField::Custom(3)); - assert_eq!(tlv.len_value(), 1); + assert_eq!(tlv.value().len(), 1); assert_eq!(tlv.len_full(), 3); } diff --git a/src/cfdp/tlv/msg_to_user.rs b/src/cfdp/tlv/msg_to_user.rs new file mode 100644 index 0000000..463deac --- /dev/null +++ b/src/cfdp/tlv/msg_to_user.rs @@ -0,0 +1,90 @@ +use delegate::delegate; + +use crate::ByteConversionError; + +use super::{TlvLvError, Tlv, TlvType, TlvTypeField}; + +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub struct MsgToUserTlv<'data> { + pub tlv: Tlv<'data>, +} + +impl<'data> MsgToUserTlv<'data> { + + /// Create a new message to user TLV where the type field is set correctly. + pub fn new(value: &'data [u8]) -> Result, TlvLvError> { + Ok(Self { + tlv: Tlv::new(TlvType::MsgToUser, value)? + }) + } + + delegate! { + to self.tlv { + pub fn tlv_type_field(&self) -> TlvTypeField; + pub fn write_to_bytes(&self, buf: &mut [u8]) -> Result; + pub fn value(&self) -> &[u8]; + /// Helper method to retrieve the length of the value. Simply calls the [slice::len] method of + /// [Self::value] + pub fn len_value(&self) -> usize; + /// Returns the full raw length, including the length byte. + pub fn len_full(&self) -> usize; + /// Checks whether the value field is empty. + pub fn is_empty(&self) -> bool; + /// If the TLV was generated from a raw bytestream using [Self::from_bytes], the raw start + /// of the TLV can be retrieved with this method. + pub fn raw_data(&self) -> Option<&[u8]>; + } + } + + pub fn is_standard_tlv(&self) -> bool { + true + } + + pub fn tlv_type(&self) -> Option { + Some(TlvType::MsgToUser) + } + + /// This is a thin wrapper around [Tlv::from_bytes] with the additional type check. + pub fn from_bytes(buf: &'data [u8]) -> Result, TlvLvError> { + let msg_to_user = Self { + tlv: Tlv::from_bytes(buf)?, + }; + match msg_to_user.tlv_type_field() { + TlvTypeField::Standard(tlv_type) => { + if tlv_type != TlvType::MsgToUser { + return Err(TlvLvError::InvalidTlvTypeField(( + tlv_type as u8, + Some(TlvType::MsgToUser as u8), + ))); + } + } + TlvTypeField::Custom(raw) => { + return Err(TlvLvError::InvalidTlvTypeField(( + raw, + Some(TlvType::MsgToUser as u8), + ))); + } + } + Ok(msg_to_user) + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_basic() { + let custom_value: [u8; 4] = [1, 2, 3, 4]; + let msg_to_user = MsgToUserTlv::new(&custom_value); + assert!(msg_to_user.is_ok()); + let msg_to_user = msg_to_user.unwrap(); + assert!(msg_to_user.is_standard_tlv()); + assert_eq!(msg_to_user.tlv_type().unwrap(), TlvType::MsgToUser); + assert_eq!(msg_to_user.value(), custom_value); + assert_eq!(msg_to_user.value().len(), 4); + assert_eq!(msg_to_user.len_value(), 4); + assert_eq!(msg_to_user.len_full(), 5); + assert!(!msg_to_user.is_empty()); + assert!(msg_to_user.raw_data().is_none()); + } +} From 081f6e840f21cc1e3b5b6b87e60e7b6fc09b5a5b Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 16 Aug 2023 17:58:19 +0200 Subject: [PATCH 4/9] added additional API --- src/cfdp/tlv/msg_to_user.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/cfdp/tlv/msg_to_user.rs b/src/cfdp/tlv/msg_to_user.rs index 463deac..82b25eb 100644 --- a/src/cfdp/tlv/msg_to_user.rs +++ b/src/cfdp/tlv/msg_to_user.rs @@ -1,7 +1,6 @@ +//! Abstractions for the Message to User CFDP TLV subtype. use delegate::delegate; - use crate::ByteConversionError; - use super::{TlvLvError, Tlv, TlvType, TlvTypeField}; #[derive(Debug, Copy, Clone, PartialEq, Eq)] @@ -44,6 +43,18 @@ impl<'data> MsgToUserTlv<'data> { Some(TlvType::MsgToUser) } + /// Check whether this message is a reserved CFDP message like a Proxy Operation Message. + pub fn is_reserved_cfdp_msg(&self) -> bool { + if self.value().len() < 4 { + return false; + } + let value = self.value(); + if value[0] == b'c' && value[1] == b'f' && value[2] == b'd' && value[3] == b'p' { + return true; + } + false + } + /// This is a thin wrapper around [Tlv::from_bytes] with the additional type check. pub fn from_bytes(buf: &'data [u8]) -> Result, TlvLvError> { let msg_to_user = Self { @@ -83,8 +94,9 @@ mod tests { assert_eq!(msg_to_user.value(), custom_value); assert_eq!(msg_to_user.value().len(), 4); assert_eq!(msg_to_user.len_value(), 4); - assert_eq!(msg_to_user.len_full(), 5); + assert_eq!(msg_to_user.len_full(), 6); assert!(!msg_to_user.is_empty()); assert!(msg_to_user.raw_data().is_none()); + assert!(!msg_to_user.is_reserved_cfdp_msg()); } } From 81db36d15979e783a341192fa9b3c99c436b7e07 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 16 Aug 2023 18:01:09 +0200 Subject: [PATCH 5/9] additional docs --- src/cfdp/lv.rs | 3 +++ src/cfdp/tlv/mod.rs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/cfdp/lv.rs b/src/cfdp/lv.rs index 67c2269..18329ef 100644 --- a/src/cfdp/lv.rs +++ b/src/cfdp/lv.rs @@ -11,6 +11,9 @@ pub const MIN_LV_LEN: usize = 1; /// Generic CFDP length-value (LV) abstraction as specified in CFDP 5.1.8. /// +/// Please note that this class is zero-copy and does not generate a copy of the value data for +/// both the regular [new] constructor and the [Self::from_bytes] constructor. +/// /// # Lifetimes /// * `data`: If the LV is generated from a raw bytestream, this will be the lifetime of /// the raw bytestream. If the LV is generated from a raw slice or a similar data reference, diff --git a/src/cfdp/tlv/mod.rs b/src/cfdp/tlv/mod.rs index d378d4d..d6d2f4b 100644 --- a/src/cfdp/tlv/mod.rs +++ b/src/cfdp/tlv/mod.rs @@ -72,6 +72,9 @@ impl From for u8 { /// Generic CFDP type-length-value (TLV) abstraction as specified in CFDP 5.1.9. /// +/// Please note that this class is zero-copy and does not generate a copy of the value data for +/// both the regular [new] constructor and the [Self::from_bytes] constructor. +/// /// # Lifetimes /// * `data`: If the TLV is generated from a raw bytestream, this will be the lifetime of /// the raw bytestream. If the TLV is generated from a raw slice or a similar data reference, From 3ba575aac179f57f052ebb0c5d3035662a13d770 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 16 Aug 2023 18:01:54 +0200 Subject: [PATCH 6/9] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08dadaa..001847f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Added `raw_data` API for `Tlv` and `Lv` to retrieve the whole `Lv`/`Tlv` slice if the object was created from a raw bytestream. +- Added `MsgToUserTlv` helper class which wraps a regular `Tlv` and adds some useful functionality. # [v0.7.0-beta.0] 2023-08-16 From 407d1e115445e197eb301dd3d1ea9ef6e4e3f536 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 16 Aug 2023 18:10:39 +0200 Subject: [PATCH 7/9] additional test for new method --- src/cfdp/tlv/msg_to_user.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/cfdp/tlv/msg_to_user.rs b/src/cfdp/tlv/msg_to_user.rs index 82b25eb..4ea4573 100644 --- a/src/cfdp/tlv/msg_to_user.rs +++ b/src/cfdp/tlv/msg_to_user.rs @@ -99,4 +99,13 @@ mod tests { assert!(msg_to_user.raw_data().is_none()); assert!(!msg_to_user.is_reserved_cfdp_msg()); } + + #[test] + fn test_reserved_msg() { + let reserved_str = "cfdp"; + let msg_to_user = MsgToUserTlv::new(reserved_str.as_bytes()); + assert!(msg_to_user.is_ok()); + let msg_to_user = msg_to_user.unwrap(); + assert!(msg_to_user.is_reserved_cfdp_msg()); + } } From 6ab05e2d8333d439bfc341542a700314efd49a2f Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 16 Aug 2023 18:19:15 +0200 Subject: [PATCH 8/9] fix for docs --- src/cfdp/lv.rs | 2 +- src/cfdp/tlv/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cfdp/lv.rs b/src/cfdp/lv.rs index 18329ef..2e56794 100644 --- a/src/cfdp/lv.rs +++ b/src/cfdp/lv.rs @@ -12,7 +12,7 @@ pub const MIN_LV_LEN: usize = 1; /// Generic CFDP length-value (LV) abstraction as specified in CFDP 5.1.8. /// /// Please note that this class is zero-copy and does not generate a copy of the value data for -/// both the regular [new] constructor and the [Self::from_bytes] constructor. +/// both the regular [Self::new] constructor and the [Self::from_bytes] constructor. /// /// # Lifetimes /// * `data`: If the LV is generated from a raw bytestream, this will be the lifetime of diff --git a/src/cfdp/tlv/mod.rs b/src/cfdp/tlv/mod.rs index d6d2f4b..3cad599 100644 --- a/src/cfdp/tlv/mod.rs +++ b/src/cfdp/tlv/mod.rs @@ -73,7 +73,7 @@ impl From for u8 { /// Generic CFDP type-length-value (TLV) abstraction as specified in CFDP 5.1.9. /// /// Please note that this class is zero-copy and does not generate a copy of the value data for -/// both the regular [new] constructor and the [Self::from_bytes] constructor. +/// both the regular [Self::new] constructor and the [Self::from_bytes] constructor. /// /// # Lifetimes /// * `data`: If the TLV is generated from a raw bytestream, this will be the lifetime of From 9a52066314e8c531fe6f0aacea0ef95642b3a244 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 16 Aug 2023 18:19:41 +0200 Subject: [PATCH 9/9] fmt --- src/cfdp/tlv/mod.rs | 5 +---- src/cfdp/tlv/msg_to_user.rs | 7 +++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/cfdp/tlv/mod.rs b/src/cfdp/tlv/mod.rs index 3cad599..6dd4f00 100644 --- a/src/cfdp/tlv/mod.rs +++ b/src/cfdp/tlv/mod.rs @@ -269,10 +269,7 @@ impl<'data> TryFrom> for EntityIdTlv { } } let len_value = value.value().len(); - if len_value != 1 - && len_value != 2 - && len_value != 4 - && len_value != 8 { + if len_value != 1 && len_value != 2 && len_value != 4 && len_value != 8 { return Err(TlvLvError::InvalidValueLength(len_value)); } Ok(Self::new( diff --git a/src/cfdp/tlv/msg_to_user.rs b/src/cfdp/tlv/msg_to_user.rs index 4ea4573..205d2d8 100644 --- a/src/cfdp/tlv/msg_to_user.rs +++ b/src/cfdp/tlv/msg_to_user.rs @@ -1,7 +1,7 @@ //! Abstractions for the Message to User CFDP TLV subtype. -use delegate::delegate; +use super::{Tlv, TlvLvError, TlvType, TlvTypeField}; use crate::ByteConversionError; -use super::{TlvLvError, Tlv, TlvType, TlvTypeField}; +use delegate::delegate; #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct MsgToUserTlv<'data> { @@ -9,11 +9,10 @@ pub struct MsgToUserTlv<'data> { } impl<'data> MsgToUserTlv<'data> { - /// Create a new message to user TLV where the type field is set correctly. pub fn new(value: &'data [u8]) -> Result, TlvLvError> { Ok(Self { - tlv: Tlv::new(TlvType::MsgToUser, value)? + tlv: Tlv::new(TlvType::MsgToUser, value)?, }) }