From 52063320bee15fa67584fb0f1d11810ec9a10747 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sun, 3 Dec 2023 20:25:32 +0100 Subject: [PATCH] more --- CHANGELOG.md | 5 +++-- src/cfdp/mod.rs | 18 +++++++----------- src/cfdp/tlv/mod.rs | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae42428..88862a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,11 +28,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Removed the `PusError::RawDataTooShort` variant which is already covered by `PusError::ByteConversionError` variant. - Ranamed `TlvLvError::ByteConversionError` to `TlvLvError::ByteConversion`. +- Renamed `PusError::IncorrectCrc` to `PusError::ChecksumFailure`. ## Removed -- Removed `PusError::NoRawData` and renamed `PusError::IncorrectCrc` to - `PusError::ChecksumFailure`. +- `PusError::NoRawData` variant. +- `cfdp::LenInBytes` which was not used. # [v0.7.0-beta.2] 2023-09-26 diff --git a/src/cfdp/mod.rs b/src/cfdp/mod.rs index cff6a56..6fd796c 100644 --- a/src/cfdp/mod.rs +++ b/src/cfdp/mod.rs @@ -94,17 +94,6 @@ pub enum FaultHandlerCode { AbandonTransaction = 0b0100, } -#[derive(Debug, Copy, Clone, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[repr(u8)] -pub enum LenInBytes { - ZeroOrNone = 0, - OneByte = 1, - TwoBytes = 2, - ThreeBytes = 4, - FourBytes = 8, -} - #[derive(Debug, Copy, Clone, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[repr(u8)] @@ -264,4 +253,11 @@ mod tests { let checksum = ChecksumType::default(); assert_eq!(checksum, ChecksumType::NullChecksum); } + + #[test] + fn test_fault_handler_code_from_u8() { + let fault_handler_code_raw = FaultHandlerCode::NoticeOfSuspension as u8; + let fault_handler_code = FaultHandlerCode::try_from(fault_handler_code_raw).unwrap(); + assert_eq!(fault_handler_code, FaultHandlerCode::NoticeOfSuspension); + } } diff --git a/src/cfdp/tlv/mod.rs b/src/cfdp/tlv/mod.rs index d232d03..1c93a3d 100644 --- a/src/cfdp/tlv/mod.rs +++ b/src/cfdp/tlv/mod.rs @@ -208,6 +208,10 @@ impl EntityIdTlv { Ok(()) } + pub fn entity_id(&self) -> &UnsignedByteField { + &self.entity_id + } + pub fn len_value(&self) -> usize { self.entity_id.size() } @@ -220,7 +224,7 @@ impl EntityIdTlv { Self::len_check(buf)?; buf[0] = TlvType::EntityId as u8; buf[1] = self.entity_id.size() as u8; - self.entity_id.write_to_be_bytes(&mut buf[2..]) + Ok(2 + self.entity_id.write_to_be_bytes(&mut buf[2..])?) } pub fn from_bytes(buf: &[u8]) -> Result { @@ -481,10 +485,13 @@ impl<'first_name, 'second_name> FilestoreRequestTlv<'first_name, 'second_name> { #[cfg(test)] mod tests { + use std::println; + + use super::*; use crate::cfdp::lv::Lv; use crate::cfdp::tlv::{FilestoreActionCode, FilestoreRequestTlv, Tlv, TlvType, TlvTypeField}; use crate::cfdp::TlvLvError; - use crate::util::{UbfU8, UnsignedEnum}; + use crate::util::{UbfU16, UbfU8, UnsignedEnum}; const TLV_TEST_STR_0: &str = "hello.txt"; const TLV_TEST_STR_1: &str = "hello2.txt"; @@ -544,6 +551,30 @@ mod tests { assert_eq!(tlv_from_raw.value()[0], 5); } + #[test] + fn test_entity_id_tlv() { + let entity_id = UbfU16::new(0x0102); + let entity_id_tlv = EntityIdTlv::new(entity_id.into()); + let mut buf: [u8; 16] = [0; 16]; + let written_len = entity_id_tlv.write_to_be_bytes(&mut buf).unwrap(); + assert_eq!(written_len, entity_id_tlv.len_full()); + assert_eq!(buf[0], TlvType::EntityId as u8); + assert_eq!(buf[1], 2); + assert_eq!(u16::from_be_bytes(buf[2..4].try_into().unwrap()), 0x0102); + } + + #[test] + fn test_entity_id_from_raw() { + let entity_id = UbfU16::new(0x0102); + let entity_id_tlv = EntityIdTlv::new(entity_id.into()); + let mut buf: [u8; 16] = [0; 16]; + let _ = entity_id_tlv.write_to_be_bytes(&mut buf).unwrap(); + let entity_tlv_from_raw = + EntityIdTlv::from_bytes(&buf).expect("creating entity ID TLV failed"); + assert_eq!(entity_tlv_from_raw, entity_id_tlv); + assert_eq!(entity_tlv_from_raw.entity_id(), &entity_id.into()); + } + #[test] fn test_empty() { let tlv_empty = Tlv::new_empty(TlvType::MsgToUser);