From fe1a30327bcc9a914cd2695b6af74b869a8b23a2 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sun, 11 Sep 2022 20:50:46 +0200 Subject: [PATCH] work on uniform API --- src/ecss.rs | 14 +++++++------- src/lib.rs | 6 ++++-- src/tc.rs | 26 +++++++++++++------------- src/tm.rs | 24 +++++++++++++++--------- 4 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/ecss.rs b/src/ecss.rs index 8bbbb91..5eec1d0 100644 --- a/src/ecss.rs +++ b/src/ecss.rs @@ -141,7 +141,7 @@ pub trait EcssEnumeration { fn byte_width(&self) -> usize { (self.pfc() / 8) as usize } - fn to_bytes(&self, buf: &mut [u8]) -> Result<(), ByteConversionError>; + fn write_to_bytes(&self, buf: &mut [u8]) -> Result<(), ByteConversionError>; } trait ToBeBytes { @@ -196,7 +196,7 @@ impl EcssEnumeration for GenericEcssEnumWrapper { size_of::() as u8 * 8_u8 } - fn to_bytes(&self, buf: &mut [u8]) -> Result<(), ByteConversionError> { + fn write_to_bytes(&self, buf: &mut [u8]) -> Result<(), ByteConversionError> { if buf.len() < self.byte_width() as usize { return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch { found: buf.len(), @@ -223,7 +223,7 @@ mod tests { let mut buf = [0, 0, 0]; let my_enum = EcssEnumU8::new(1); my_enum - .to_bytes(&mut buf[1..2]) + .write_to_bytes(&mut buf[1..2]) .expect("To byte conversion of u8 failed"); assert_eq!(buf[1], 1); } @@ -233,7 +233,7 @@ mod tests { let mut buf = [0, 0, 0]; let my_enum = EcssEnumU16::new(0x1f2f); my_enum - .to_bytes(&mut buf[1..3]) + .write_to_bytes(&mut buf[1..3]) .expect("To byte conversion of u8 failed"); assert_eq!(buf[1], 0x1f); assert_eq!(buf[2], 0x2f); @@ -243,7 +243,7 @@ mod tests { fn test_slice_u16_too_small() { let mut buf = [0]; let my_enum = EcssEnumU16::new(0x1f2f); - let res = my_enum.to_bytes(&mut buf[0..1]); + let res = my_enum.write_to_bytes(&mut buf[0..1]); assert!(res.is_err()); let error = res.unwrap_err(); match error { @@ -262,7 +262,7 @@ mod tests { let mut buf = [0, 0, 0, 0, 0]; let my_enum = EcssEnumU32::new(0x1f2f3f4f); my_enum - .to_bytes(&mut buf[1..5]) + .write_to_bytes(&mut buf[1..5]) .expect("To byte conversion of u8 failed"); assert_eq!(buf[1], 0x1f); assert_eq!(buf[2], 0x2f); @@ -274,7 +274,7 @@ mod tests { fn test_slice_u32_too_small() { let mut buf = [0, 0, 0, 0, 0]; let my_enum = EcssEnumU32::new(0x1f2f3f4f); - let res = my_enum.to_bytes(&mut buf[0..3]); + let res = my_enum.write_to_bytes(&mut buf[0..3]); assert!(res.is_err()); let error = res.unwrap_err(); match error { diff --git a/src/lib.rs b/src/lib.rs index 5cccb69..a4bcea3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,6 +59,8 @@ pub mod tc; pub mod time; pub mod tm; +pub const MAX_APID: u16 = 2u16.pow(11) - 1; + #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct SizeMissmatch { pub found: usize, @@ -142,7 +144,7 @@ impl PacketId { /// not be set and false will be returned. The maximum allowed value for the 11-bit field is /// 2047 pub fn set_apid(&mut self, apid: u16) -> bool { - if apid > 2u16.pow(11) - 1 { + if apid > MAX_APID { return false; } self.apid = apid; @@ -346,7 +348,7 @@ impl SpHeader { ssc: u16, data_len: u16, ) -> Option { - if ssc > 2u16.pow(14) - 1 || apid > 2u16.pow(11) - 1 { + if ssc > 2u16.pow(14) - 1 || apid > MAX_APID { return None; } let mut header = SpHeader::default(); diff --git a/src/tc.rs b/src/tc.rs index 19818f9..722627c 100644 --- a/src/tc.rs +++ b/src/tc.rs @@ -20,7 +20,7 @@ //! // Serialize TC into a raw buffer //! let mut test_buf: [u8; 32] = [0; 32]; //! let size = pus_tc -//! .write_to(test_buf.as_mut_slice()) +//! .write_to_bytes(test_buf.as_mut_slice()) //! .expect("Error writing TC to buffer"); //! assert_eq!(size, 13); //! println!("{:?}", &test_buf[0..size]); @@ -126,7 +126,7 @@ pub mod zc { } impl PusTcSecondaryHeader { - pub fn to_bytes(&self, slice: &mut [u8]) -> Option<()> { + pub fn write_to_bytes(&self, slice: &mut [u8]) -> Option<()> { self.write_to(slice) } @@ -330,7 +330,7 @@ impl<'slice> PusTc<'slice> { } /// Write the raw PUS byte representation to a provided buffer. - pub fn write_to(&self, slice: &mut [u8]) -> Result { + pub fn write_to_bytes(&self, slice: &mut [u8]) -> Result { let mut curr_idx = 0; let sph_zc = crate::zc::SpHeader::from(self.sp_header); let tc_header_len = size_of::(); @@ -350,7 +350,7 @@ impl<'slice> PusTc<'slice> { curr_idx += CCSDS_HEADER_LEN; let sec_header = zc::PusTcSecondaryHeader::try_from(self.sec_header).unwrap(); sec_header - .to_bytes(&mut slice[curr_idx..curr_idx + tc_header_len]) + .write_to_bytes(&mut slice[curr_idx..curr_idx + tc_header_len]) .ok_or(PusError::PacketError(ByteConversionError::ZeroCopyToError))?; curr_idx += tc_header_len; @@ -514,7 +514,7 @@ mod tests { let pus_tc = base_ping_tc_simple_ctor(); let mut test_buf: [u8; 32] = [0; 32]; let size = pus_tc - .write_to(test_buf.as_mut_slice()) + .write_to_bytes(test_buf.as_mut_slice()) .expect("Error writing TC to buffer"); assert_eq!(size, 13); } @@ -524,7 +524,7 @@ mod tests { let pus_tc = base_ping_tc_simple_ctor(); let mut test_buf: [u8; 32] = [0; 32]; let size = pus_tc - .write_to(test_buf.as_mut_slice()) + .write_to_bytes(test_buf.as_mut_slice()) .expect("Error writing TC to buffer"); assert_eq!(size, 13); let (tc_from_raw, size) = PusTc::new_from_raw_slice(&test_buf) @@ -550,7 +550,7 @@ mod tests { let pus_tc = base_ping_tc_simple_ctor_with_app_data(&[1, 2, 3]); let mut test_buf: [u8; 32] = [0; 32]; let size = pus_tc - .write_to(test_buf.as_mut_slice()) + .write_to_bytes(test_buf.as_mut_slice()) .expect("Error writing TC to buffer"); assert_eq!(size, 16); let (tc_from_raw, size) = PusTc::new_from_raw_slice(&test_buf) @@ -581,7 +581,7 @@ mod tests { let pus_tc = base_ping_tc_simple_ctor(); let mut test_buf: [u8; 32] = [0; 32]; pus_tc - .write_to(test_buf.as_mut_slice()) + .write_to_bytes(test_buf.as_mut_slice()) .expect("Error writing TC to buffer"); test_buf[12] = 0; let res = PusTc::new_from_raw_slice(&test_buf); @@ -597,7 +597,7 @@ mod tests { let mut test_buf: [u8; 32] = [0; 32]; pus_tc.calc_own_crc16(); pus_tc - .write_to(test_buf.as_mut_slice()) + .write_to_bytes(test_buf.as_mut_slice()) .expect("Error writing TC to buffer"); verify_test_tc_raw(&test_buf); verify_crc_no_app_data(&test_buf); @@ -608,7 +608,7 @@ mod tests { let mut pus_tc = base_ping_tc_simple_ctor(); pus_tc.calc_crc_on_serialization = false; let mut test_buf: [u8; 32] = [0; 32]; - let res = pus_tc.write_to(test_buf.as_mut_slice()); + let res = pus_tc.write_to_bytes(test_buf.as_mut_slice()); assert!(res.is_err()); let err = res.unwrap_err(); assert!(matches!(err, PusError::CrcCalculationMissing { .. })); @@ -632,7 +632,7 @@ mod tests { fn test_write_buf_too_msall() { let pus_tc = base_ping_tc_simple_ctor(); let mut test_buf = [0; 12]; - let res = pus_tc.write_to(test_buf.as_mut_slice()); + let res = pus_tc.write_to_bytes(test_buf.as_mut_slice()); assert!(res.is_err()); let err = res.unwrap_err(); match err { @@ -653,7 +653,7 @@ mod tests { verify_test_tc(&pus_tc, true, 16); let mut test_buf: [u8; 32] = [0; 32]; let size = pus_tc - .write_to(test_buf.as_mut_slice()) + .write_to_bytes(test_buf.as_mut_slice()) .expect("Error writing TC to buffer"); assert_eq!(test_buf[11], 1); assert_eq!(test_buf[12], 2); @@ -677,7 +677,7 @@ mod tests { assert_eq!(pus_tc.sequence_flags(), SequenceFlags::Unsegmented); pus_tc.calc_own_crc16(); pus_tc - .write_to(test_buf.as_mut_slice()) + .write_to_bytes(test_buf.as_mut_slice()) .expect("Error writing TC to buffer"); assert_eq!(test_buf[0], 0x1f); assert_eq!(test_buf[1], 0xff); diff --git a/src/tm.rs b/src/tm.rs index 0a3523b..debc676 100644 --- a/src/tm.rs +++ b/src/tm.rs @@ -67,7 +67,7 @@ pub mod zc { } impl PusTmSecHeaderWithoutTimestamp { - pub fn to_bytes(&self, slice: &mut [u8]) -> Option<()> { + pub fn write_to_bytes(&self, slice: &mut [u8]) -> Option<()> { self.write_to(slice) } @@ -309,7 +309,7 @@ impl<'slice> PusTm<'slice> { } /// Write the raw PUS byte representation to a provided buffer. - pub fn write_to(&self, slice: &mut [u8]) -> Result { + pub fn write_to_bytes(&self, slice: &mut [u8]) -> Result { let mut curr_idx = 0; let sph_zc = crate::zc::SpHeader::from(self.sp_header); let total_size = self.len_packed(); @@ -329,7 +329,7 @@ impl<'slice> PusTm<'slice> { let sec_header_len = size_of::(); let sec_header = zc::PusTmSecHeaderWithoutTimestamp::try_from(self.sec_header).unwrap(); sec_header - .to_bytes(&mut slice[curr_idx..curr_idx + sec_header_len]) + .write_to_bytes(&mut slice[curr_idx..curr_idx + sec_header_len]) .ok_or(PusError::PacketError(ByteConversionError::ZeroCopyToError))?; curr_idx += sec_header_len; let timestamp_len = self.sec_header.time_stamp.len(); @@ -501,7 +501,9 @@ mod tests { let time_stamp = dummy_time_stamp(); let pus_tm = base_ping_reply_full_ctor(&time_stamp); let mut buf: [u8; 32] = [0; 32]; - let ser_len = pus_tm.write_to(&mut buf).expect("Serialization failed"); + let ser_len = pus_tm + .write_to_bytes(&mut buf) + .expect("Serialization failed"); assert_eq!(ser_len, 22); verify_raw_ping_reply(&buf); } @@ -511,7 +513,9 @@ mod tests { let src_data = [1, 2, 3]; let hk_reply = base_hk_reply(dummy_time_stamp(), &src_data); let mut buf: [u8; 32] = [0; 32]; - let ser_len = hk_reply.write_to(&mut buf).expect("Serialization failed"); + let ser_len = hk_reply + .write_to_bytes(&mut buf) + .expect("Serialization failed"); assert_eq!(ser_len, 25); assert_eq!(buf[20], 1); assert_eq!(buf[21], 2); @@ -537,7 +541,9 @@ mod tests { let time_stamp = dummy_time_stamp(); let pus_tm = base_ping_reply_full_ctor(&time_stamp); let mut buf: [u8; 32] = [0; 32]; - let ser_len = pus_tm.write_to(&mut buf).expect("Serialization failed"); + let ser_len = pus_tm + .write_to_bytes(&mut buf) + .expect("Serialization failed"); assert_eq!(ser_len, 22); let (tm_deserialized, size) = PusTm::new_from_raw_slice(&buf, 7).expect("Deserialization failed"); @@ -553,13 +559,13 @@ mod tests { tm.calc_crc_on_serialization = false; assert_eq!(tm.data_len(), 0x00); let mut buf: [u8; 32] = [0; 32]; - let res = tm.write_to(&mut buf); + let res = tm.write_to_bytes(&mut buf); assert!(res.is_err()); assert!(matches!(res.unwrap_err(), PusError::CrcCalculationMissing)); tm.update_ccsds_data_len(); assert_eq!(tm.data_len(), 15); tm.calc_own_crc16(); - let res = tm.write_to(&mut buf); + let res = tm.write_to_bytes(&mut buf); assert!(res.is_ok()); tm.sp_header.data_len = 0; tm.update_packet_fields(); @@ -571,7 +577,7 @@ mod tests { let time_stamp = dummy_time_stamp(); let pus_tm = base_ping_reply_full_ctor(&time_stamp); let mut buf: [u8; 16] = [0; 16]; - let res = pus_tm.write_to(&mut buf); + let res = pus_tm.write_to_bytes(&mut buf); assert!(res.is_err()); let error = res.unwrap_err(); assert!(matches!(error, PusError::PacketError { .. }));