add additional API to set APID
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good

This commit is contained in:
Robin Müller 2023-07-05 19:25:05 +02:00
parent c85177ece9
commit 929590ecb0
Signed by: muellerr
GPG Key ID: A649FB78196E3849
2 changed files with 20 additions and 5 deletions

View File

@ -6,7 +6,7 @@
//! ```rust //! ```rust
//! use spacepackets::{CcsdsPacket, SpHeader}; //! use spacepackets::{CcsdsPacket, SpHeader};
//! use spacepackets::tc::{PusTc, PusTcSecondaryHeader}; //! use spacepackets::tc::{PusTc, PusTcSecondaryHeader};
//! use spacepackets::ecss::PusPacket; //! use spacepackets::ecss::{PusPacket, SerializablePusPacket};
//! //!
//! // Create a ping telecommand with no user application data //! // Create a ping telecommand with no user application data
//! let mut sph = SpHeader::tc_unseg(0x02, 0x34, 0).unwrap(); //! let mut sph = SpHeader::tc_unseg(0x02, 0x34, 0).unwrap();

View File

@ -7,7 +7,7 @@ use crate::ecss::{
}; };
use crate::{ use crate::{
ByteConversionError, CcsdsPacket, PacketType, SequenceFlags, SizeMissmatch, SpHeader, ByteConversionError, CcsdsPacket, PacketType, SequenceFlags, SizeMissmatch, SpHeader,
CCSDS_HEADER_LEN, CRC_CCITT_FALSE, MAX_SEQ_COUNT, CCSDS_HEADER_LEN, CRC_CCITT_FALSE, MAX_APID, MAX_SEQ_COUNT,
}; };
use core::mem::size_of; use core::mem::size_of;
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
@ -427,6 +427,19 @@ impl<'raw> PusTmZeroCopyWriter<'raw> {
Some(Self { raw_tm }) Some(Self { raw_tm })
} }
/// Set the sequence count. Returns false and does not update the value if the passed value
/// exceeds [MAX_APID].
pub fn set_apid(&mut self, apid: u16) -> bool {
if apid > MAX_APID {
return false;
}
// Clear APID part of the raw packet ID
let updated_apid =
((((self.raw_tm[0] as u16) << 8) | self.raw_tm[1] as u16) & !MAX_APID) | apid;
self.raw_tm[0..2].copy_from_slice(&updated_apid.to_be_bytes());
true
}
/// This function sets the message counter in the PUS TM secondary header. /// This function sets the message counter in the PUS TM secondary header.
pub fn set_msg_count(&mut self, msg_count: u16) { pub fn set_msg_count(&mut self, msg_count: u16) {
self.raw_tm[9..11].copy_from_slice(&msg_count.to_be_bytes()); self.raw_tm[9..11].copy_from_slice(&msg_count.to_be_bytes());
@ -785,14 +798,16 @@ mod tests {
.expect("Creating zero copy writer failed"); .expect("Creating zero copy writer failed");
writer.set_destination_id(55); writer.set_destination_id(55);
writer.set_msg_count(100); writer.set_msg_count(100);
writer.set_seq_count_in_place(MAX_SEQ_COUNT - 1); writer.set_seq_count_in_place(MAX_SEQ_COUNT);
writer.finalize(); writer.set_apid(MAX_APID);
writer.finish();
// This performs all necessary checks, including the CRC check. // This performs all necessary checks, including the CRC check.
let (tm_read_back, tm_size_read_back) = let (tm_read_back, tm_size_read_back) =
PusTm::from_bytes(&buf, 7).expect("Re-creating PUS TM failed"); PusTm::from_bytes(&buf, 7).expect("Re-creating PUS TM failed");
assert_eq!(tm_size_read_back, tm_size); assert_eq!(tm_size_read_back, tm_size);
assert_eq!(tm_read_back.msg_counter(), 100); assert_eq!(tm_read_back.msg_counter(), 100);
assert_eq!(tm_read_back.dest_id(), 55); assert_eq!(tm_read_back.dest_id(), 55);
assert_eq!(tm_read_back.seq_count(), MAX_SEQ_COUNT - 1); assert_eq!(tm_read_back.seq_count(), MAX_SEQ_COUNT);
assert_eq!(tm_read_back.apid(), MAX_APID);
} }
} }