From 50b86939a13efb7610752f3c4c00657c38b9aff4 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 4 Apr 2024 12:07:37 +0200 Subject: [PATCH] this API is a bit more ergonomic --- CHANGELOG.md | 2 ++ src/ecss/tc.rs | 32 +++++++++++++++++--------------- src/ecss/tm.rs | 36 ++++++++++++++++++------------------ 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01f78e8..5993f11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Renamed `tc`, `tm`, `tc_unseg` and `tm_unseg` variants for `PacketId` and `SpHeader` to `new_for_tc_checked`, `new_for_tm_checked`, `new_for_unseg_tc_checked` and `new_for_unseg_tm_checked`. +- `PusTmCreator` and `PusTcCreator` now expect a regular instance of `SpHeader` instead of + a mutable reference. ## Added diff --git a/src/ecss/tc.rs b/src/ecss/tc.rs index 7a4f7a7..7aa3069 100644 --- a/src/ecss/tc.rs +++ b/src/ecss/tc.rs @@ -9,9 +9,11 @@ //! use spacepackets::ecss::tc::{PusTcCreator, PusTcReader, PusTcSecondaryHeader}; //! //! // Create a ping telecommand with no user application data -//! let mut sph = SpHeader::new_for_unseg_tc_checked(0x02, 0x34, 0).unwrap(); -//! let tc_header = PusTcSecondaryHeader::new_simple(17, 1); -//! let pus_tc = PusTcCreator::new_no_app_data(&mut sph, tc_header, true); +//! let pus_tc = PusTcCreator::new_no_app_data( +//! SpHeader::new_from_apid(0x02), +//! PusTcSecondaryHeader::new_simple(17, 1), +//! true +//! ); //! println!("{:?}", pus_tc); //! assert_eq!(pus_tc.service(), 17); //! assert_eq!(pus_tc.subservice(), 1); @@ -256,7 +258,7 @@ impl<'app_data> PusTcCreator<'app_data> { /// the correct value to this field manually #[inline] pub fn new( - sp_header: &mut SpHeader, + mut sp_header: SpHeader, sec_header: PusTcSecondaryHeader, app_data: &'app_data [u8], set_ccsds_len: bool, @@ -264,7 +266,7 @@ impl<'app_data> PusTcCreator<'app_data> { sp_header.set_packet_type(PacketType::Tc); sp_header.set_sec_header_flag(); let mut pus_tc = Self { - sp_header: *sp_header, + sp_header, app_data, sec_header, }; @@ -278,7 +280,7 @@ impl<'app_data> PusTcCreator<'app_data> { /// and subservice instead of the full PUS TC secondary header. #[inline] pub fn new_simple( - sph: &mut SpHeader, + sph: SpHeader, service: u8, subservice: u8, app_data: &'app_data [u8], @@ -294,7 +296,7 @@ impl<'app_data> PusTcCreator<'app_data> { #[inline] pub fn new_no_app_data( - sp_header: &mut SpHeader, + sp_header: SpHeader, sec_header: PusTcSecondaryHeader, set_ccsds_len: bool, ) -> Self { @@ -618,19 +620,19 @@ mod tests { use postcard::{from_bytes, to_allocvec}; fn base_ping_tc_full_ctor() -> PusTcCreator<'static> { - let mut sph = SpHeader::new_for_unseg_tc_checked(0x02, 0x34, 0).unwrap(); + let sph = SpHeader::new_for_unseg_tc_checked(0x02, 0x34, 0).unwrap(); let tc_header = PusTcSecondaryHeader::new_simple(17, 1); - PusTcCreator::new_no_app_data(&mut sph, tc_header, true) + PusTcCreator::new_no_app_data(sph, tc_header, true) } fn base_ping_tc_simple_ctor() -> PusTcCreator<'static> { - let mut sph = SpHeader::new_for_unseg_tc_checked(0x02, 0x34, 0).unwrap(); - PusTcCreator::new_simple(&mut sph, 17, 1, &[], true) + let sph = SpHeader::new_for_unseg_tc_checked(0x02, 0x34, 0).unwrap(); + PusTcCreator::new_simple(sph, 17, 1, &[], true) } fn base_ping_tc_simple_ctor_with_app_data(app_data: &'static [u8]) -> PusTcCreator<'static> { - let mut sph = SpHeader::new_for_unseg_tc_checked(0x02, 0x34, 0).unwrap(); - PusTcCreator::new_simple(&mut sph, 17, 1, app_data, true) + let sph = SpHeader::new_for_unseg_tc_checked(0x02, 0x34, 0).unwrap(); + PusTcCreator::new_simple(sph, 17, 1, app_data, true) } #[test] @@ -686,8 +688,8 @@ mod tests { #[test] fn test_update_func() { - let mut sph = SpHeader::new_for_unseg_tc_checked(0x02, 0x34, 0).unwrap(); - let mut tc = PusTcCreator::new_simple(&mut sph, 17, 1, &[], false); + let sph = SpHeader::new_for_unseg_tc_checked(0x02, 0x34, 0).unwrap(); + let mut tc = PusTcCreator::new_simple(sph, 17, 1, &[], false); assert_eq!(tc.data_len(), 0); tc.update_ccsds_data_len(); assert_eq!(tc.data_len(), 6); diff --git a/src/ecss/tm.rs b/src/ecss/tm.rs index 7a360ce..745fd7e 100644 --- a/src/ecss/tm.rs +++ b/src/ecss/tm.rs @@ -254,7 +254,7 @@ impl<'time, 'src_data> PusTmCreator<'time, 'src_data> { /// the correct value to this field manually #[inline] pub fn new( - sp_header: &mut SpHeader, + mut sp_header: SpHeader, sec_header: PusTmSecondaryHeader<'time>, source_data: &'src_data [u8], set_ccsds_len: bool, @@ -262,7 +262,7 @@ impl<'time, 'src_data> PusTmCreator<'time, 'src_data> { sp_header.set_packet_type(PacketType::Tm); sp_header.set_sec_header_flag(); let mut pus_tm = Self { - sp_header: *sp_header, + sp_header, source_data, sec_header, calc_crc_on_serialization: true, @@ -275,7 +275,7 @@ impl<'time, 'src_data> PusTmCreator<'time, 'src_data> { #[inline] pub fn new_simple( - sp_header: &mut SpHeader, + sp_header: SpHeader, service: u8, subservice: u8, time_provider: &impl TimeWriter, @@ -291,7 +291,7 @@ impl<'time, 'src_data> PusTmCreator<'time, 'src_data> { #[inline] pub fn new_no_source_data( - sp_header: &mut SpHeader, + sp_header: SpHeader, sec_header: PusTmSecondaryHeader<'time>, set_ccsds_len: bool, ) -> Self { @@ -840,20 +840,20 @@ mod tests { const DUMMY_DATA: &[u8] = &[0, 1, 2]; fn base_ping_reply_full_ctor(timestamp: &[u8]) -> PusTmCreator { - let mut sph = SpHeader::new_for_unseg_tm_checked(0x123, 0x234, 0).unwrap(); + let sph = SpHeader::new_for_unseg_tm_checked(0x123, 0x234, 0).unwrap(); let tm_header = PusTmSecondaryHeader::new_simple(17, 2, timestamp); - PusTmCreator::new_no_source_data(&mut sph, tm_header, true) + PusTmCreator::new_no_source_data(sph, tm_header, true) } fn ping_reply_with_data(timestamp: &[u8]) -> PusTmCreator { - let mut sph = SpHeader::new_for_unseg_tm_checked(0x123, 0x234, 0).unwrap(); + let sph = SpHeader::new_for_unseg_tm_checked(0x123, 0x234, 0).unwrap(); let tm_header = PusTmSecondaryHeader::new_simple(17, 2, timestamp); - PusTmCreator::new(&mut sph, tm_header, DUMMY_DATA, true) + PusTmCreator::new(sph, tm_header, DUMMY_DATA, true) } fn base_hk_reply<'a, 'b>(timestamp: &'a [u8], src_data: &'b [u8]) -> PusTmCreator<'a, 'b> { - let mut sph = SpHeader::new_for_unseg_tm_checked(0x123, 0x234, 0).unwrap(); + let sph = SpHeader::new_for_unseg_tm_checked(0x123, 0x234, 0).unwrap(); let tc_header = PusTmSecondaryHeader::new_simple(3, 5, timestamp); - PusTmCreator::new(&mut sph, tc_header, src_data, true) + PusTmCreator::new(sph, tc_header, src_data, true) } fn dummy_timestamp() -> &'static [u8] { @@ -868,11 +868,11 @@ mod tests { } #[test] fn test_basic_simple_api() { - let mut sph = SpHeader::new_for_unseg_tm_checked(0x123, 0x234, 0).unwrap(); + let sph = SpHeader::new_for_unseg_tm_checked(0x123, 0x234, 0).unwrap(); let time_provider = CdsTime::new_with_u16_days(0, 0); let mut stamp_buf: [u8; 8] = [0; 8]; let pus_tm = - PusTmCreator::new_simple(&mut sph, 17, 2, &time_provider, &mut stamp_buf, &[], true) + PusTmCreator::new_simple(sph, 17, 2, &time_provider, &mut stamp_buf, &[], true) .unwrap(); verify_ping_reply(&pus_tm, false, 22, &[64, 0, 0, 0, 0, 0, 0]); } @@ -969,9 +969,9 @@ mod tests { #[test] fn test_manual_field_update() { - let mut sph = SpHeader::new_for_unseg_tm_checked(0x123, 0x234, 0).unwrap(); + let sph = SpHeader::new_for_unseg_tm_checked(0x123, 0x234, 0).unwrap(); let tc_header = PusTmSecondaryHeader::new_simple(17, 2, dummy_timestamp()); - let mut tm = PusTmCreator::new_no_source_data(&mut sph, tc_header, false); + let mut tm = PusTmCreator::new_no_source_data(sph, tc_header, false); tm.calc_crc_on_serialization = false; assert_eq!(tm.data_len(), 0x00); let mut buf: [u8; 32] = [0; 32]; @@ -1259,11 +1259,11 @@ mod tests { #[test] #[cfg(feature = "serde")] fn test_serialization_creator_serde() { - let mut sph = SpHeader::new_for_unseg_tm_checked(0x123, 0x234, 0).unwrap(); + let sph = SpHeader::new_for_unseg_tm_checked(0x123, 0x234, 0).unwrap(); let time_provider = CdsTime::new_with_u16_days(0, 0); let mut stamp_buf: [u8; 8] = [0; 8]; let pus_tm = - PusTmCreator::new_simple(&mut sph, 17, 2, &time_provider, &mut stamp_buf, &[], true) + PusTmCreator::new_simple(sph, 17, 2, &time_provider, &mut stamp_buf, &[], true) .unwrap(); let output = to_allocvec(&pus_tm).unwrap(); @@ -1274,11 +1274,11 @@ mod tests { #[test] #[cfg(feature = "serde")] fn test_serialization_reader_serde() { - let mut sph = SpHeader::new_for_unseg_tm_checked(0x123, 0x234, 0).unwrap(); + let sph = SpHeader::new_for_unseg_tm_checked(0x123, 0x234, 0).unwrap(); let time_provider = CdsTime::new_with_u16_days(0, 0); let mut stamp_buf: [u8; 8] = [0; 8]; let pus_tm = - PusTmCreator::new_simple(&mut sph, 17, 2, &time_provider, &mut stamp_buf, &[], true) + PusTmCreator::new_simple(sph, 17, 2, &time_provider, &mut stamp_buf, &[], true) .unwrap(); let pus_tm_vec = pus_tm.to_vec().unwrap(); let (tm_reader, _) = PusTmReader::new(&pus_tm_vec, time_provider.len_as_bytes()).unwrap();