diff --git a/satrs-core/src/pus/verification.rs b/satrs-core/src/pus/verification.rs index 5d2880e..bdfe8b2 100644 --- a/satrs-core/src/pus/verification.rs +++ b/satrs-core/src/pus/verification.rs @@ -248,17 +248,17 @@ impl VerificationToken { } /// Composite helper struct to pass failure parameters to the [VerificationReporter] -pub struct FailParams<'a> { - time_stamp: &'a [u8], - failure_code: &'a dyn EcssEnumeration, - failure_data: Option<&'a [u8]>, +pub struct FailParams<'stamp, 'fargs> { + time_stamp: &'stamp [u8], + failure_code: &'fargs dyn EcssEnumeration, + failure_data: Option<&'fargs [u8]>, } -impl<'a> FailParams<'a> { +impl<'stamp, 'fargs> FailParams<'stamp, 'fargs> { pub fn new( - time_stamp: &'a [u8], - failure_code: &'a impl EcssEnumeration, - failure_data: Option<&'a [u8]>, + time_stamp: &'stamp [u8], + failure_code: &'fargs impl EcssEnumeration, + failure_data: Option<&'fargs [u8]>, ) -> Self { Self { time_stamp, @@ -269,17 +269,17 @@ impl<'a> FailParams<'a> { } /// Composite helper struct to pass step failure parameters to the [VerificationReporter] -pub struct FailParamsWithStep<'a> { - bp: FailParams<'a>, - step: &'a dyn EcssEnumeration, +pub struct FailParamsWithStep<'stamp, 'fargs> { + bp: FailParams<'stamp, 'fargs>, + step: &'fargs dyn EcssEnumeration, } -impl<'a> FailParamsWithStep<'a> { +impl<'stamp, 'fargs> FailParamsWithStep<'stamp, 'fargs> { pub fn new( - time_stamp: &'a [u8], - step: &'a impl EcssEnumeration, - failure_code: &'a impl EcssEnumeration, - failure_data: Option<&'a [u8]>, + time_stamp: &'stamp [u8], + step: &'fargs impl EcssEnumeration, + failure_code: &'fargs impl EcssEnumeration, + failure_data: Option<&'fargs [u8]>, ) -> Self { Self { bp: FailParams::new(time_stamp, failure_code, failure_data), @@ -294,6 +294,84 @@ pub struct VerificationReporterCore { apid: u16, } +pub struct VerificationSendable<'slice, State> { + token: Option>, + pus_tm: Option>, +} + +impl<'slice, State> VerificationSendable<'slice, State> { + pub fn pus_tm(&self) -> &PusTm<'slice> { + self.pus_tm.as_ref().unwrap() + } + + pub fn pus_tm_mut(&mut self) -> &mut PusTm<'slice> { + self.pus_tm.as_mut().unwrap() + } + + pub(crate) fn take_tm(&mut self) -> PusTm<'slice> { + self.pus_tm.take().unwrap() + } + + pub(crate) fn increment_seq_counter( + &self, + seq_counter: Option<&(impl SequenceCountProviderCore + ?Sized)>, + ) { + if let Some(seq_counter) = seq_counter { + seq_counter.increment(); + } + } + pub fn send_success_verif_failure( + self, + seq_counter: Option<&(impl SequenceCountProviderCore + ?Sized)>, + ) { + self.increment_seq_counter(seq_counter); + } +} + +impl<'slice> VerificationSendable<'slice, TcStateNone> { + pub fn send_success_acceptance_success( + self, + seq_counter: Option<&(impl SequenceCountProviderCore + ?Sized)>, + ) -> VerificationToken { + self.increment_seq_counter(seq_counter); + VerificationToken { + state: PhantomData, + req_id: self.token.unwrap().req_id(), + } + } + pub fn send_failure(self) -> (PusTm<'slice>, VerificationToken) { + (self.pus_tm.unwrap(), self.token.unwrap()) + } +} + +impl<'slice> VerificationSendable<'slice, TcStateAccepted> { + pub fn send_success_start_success( + self, + seq_counter: Option<&(impl SequenceCountProviderCore + ?Sized)>, + ) -> VerificationToken { + self.increment_seq_counter(seq_counter); + VerificationToken { + state: PhantomData, + req_id: self.token.unwrap().req_id(), + } + } + pub fn send_failure(self) -> (PusTm<'slice>, VerificationToken) { + (self.pus_tm.unwrap(), self.token.unwrap()) + } +} + +impl<'slice> VerificationSendable<'slice, TcStateStarted> { + pub fn send_success_step_or_completion_success( + self, + seq_counter: Option<&(impl SequenceCountProviderCore + ?Sized)>, + ) { + self.increment_seq_counter(seq_counter); + } + pub fn send_failure(self) -> (PusTm<'slice>, Option>) { + (self.pus_tm.unwrap(), self.token) + } +} + impl VerificationReporterCore { pub fn new(apid: u16) -> Option { if apid > MAX_APID { @@ -334,245 +412,241 @@ impl VerificationReporterCore { VerificationToken::::new(req_id) } - /// Package and send a PUS TM\[1, 1\] packet, see 8.1.2.1 of the PUS standard - pub fn acceptance_success( + fn sendable_success_no_step<'slice, E, State: Copy>( &mut self, - buf: &mut [u8], - token: VerificationToken, - sender: &mut (impl EcssTmSenderCore + ?Sized), + buf: &'slice mut [u8], + subservice: u8, + token: VerificationToken, seq_counter: &(impl SequenceCountProviderCore + ?Sized), - time_stamp: &[u8], - ) -> Result, VerificationErrorWithToken> - { - let tm = self - .create_pus_verif_success_tm( - buf, - Subservice::TmAcceptanceSuccess.into(), - seq_counter.get(), - &token.req_id, - time_stamp, - None::<&dyn EcssEnumeration>, - ) - .map_err(|e| VerificationErrorWithToken(e, token))?; - sender - .send_tm(tm) - .map_err(|e| VerificationErrorWithToken(e, token))?; - seq_counter.increment(); - //seq_counter.get_and_increment() - Ok(VerificationToken { - state: PhantomData, - req_id: token.req_id, + time_stamp: &'slice [u8], + ) -> Result, VerificationErrorWithToken> { + Ok(VerificationSendable { + pus_tm: Some( + self.create_pus_verif_success_tm( + buf, + subservice, + seq_counter.get(), + &token.req_id, + time_stamp, + None::<&dyn EcssEnumeration>, + ) + .map_err(|e| VerificationErrorWithToken(e, token))?, + ), + token: Some(token), }) } - /// Package and send a PUS TM\[1, 2\] packet, see 8.1.2.2 of the PUS standard - pub fn acceptance_failure( + /// Package a PUS TM\[1, 1\] packet, see 8.1.2.1 of the PUS standard. + pub fn acceptance_success<'slice, E>( &mut self, - buf: &mut [u8], + buf: &'slice mut [u8], token: VerificationToken, - sender: &mut (impl EcssTmSenderCore + ?Sized), seq_counter: &(impl SequenceCountProviderCore + ?Sized), - params: FailParams, - ) -> Result<(), VerificationErrorWithToken> { - let tm = self - .create_pus_verif_fail_tm( - buf, - Subservice::TmAcceptanceFailure.into(), - seq_counter.get(), - &token.req_id, - None::<&dyn EcssEnumeration>, - ¶ms, - ) - .map_err(|e| VerificationErrorWithToken(e, token))?; - sender - .send_tm(tm) - .map_err(|e| VerificationErrorWithToken(e, token))?; - seq_counter.increment(); - Ok(()) + time_stamp: &'slice [u8], + ) -> Result, VerificationErrorWithToken> + { + self.sendable_success_no_step( + buf, + Subservice::TmAcceptanceSuccess.into(), + token, + seq_counter, + time_stamp, + ) + } + + /// Package a PUS TM\[1, 2\] packet, see 8.1.2.2 of the PUS standard. + pub fn acceptance_failure<'slice, E>( + &mut self, + buf: &'slice mut [u8], + token: VerificationToken, + seq_counter: &(impl SequenceCountProviderCore + ?Sized), + params: FailParams<'slice, '_>, + ) -> Result, VerificationErrorWithToken> + { + Ok(VerificationSendable { + pus_tm: Some( + self.create_pus_verif_fail_tm( + buf, + Subservice::TmAcceptanceFailure.into(), + seq_counter.get(), + &token.req_id, + None::<&dyn EcssEnumeration>, + ¶ms, + ) + .map_err(|e| VerificationErrorWithToken(e, token))?, + ), + token: Some(token), + }) } /// Package and send a PUS TM\[1, 3\] packet, see 8.1.2.3 of the PUS standard. /// /// Requires a token previously acquired by calling [Self::acceptance_success]. - pub fn start_success( + pub fn start_success<'slice, E>( &mut self, - buf: &mut [u8], + buf: &'slice mut [u8], token: VerificationToken, - sender: &mut (impl EcssTmSenderCore + ?Sized), seq_counter: &(impl SequenceCountProviderCore + ?Sized), - time_stamp: &[u8], - ) -> Result, VerificationErrorWithToken> - { - let tm = self - .create_pus_verif_success_tm( - buf, - Subservice::TmStartSuccess.into(), - seq_counter.get(), - &token.req_id, - time_stamp, - None::<&dyn EcssEnumeration>, - ) - .map_err(|e| VerificationErrorWithToken(e, token))?; - sender - .send_tm(tm) - .map_err(|e| VerificationErrorWithToken(e, token))?; - seq_counter.increment(); - Ok(VerificationToken { - state: PhantomData, - req_id: token.req_id, - }) + time_stamp: &'slice [u8], + ) -> Result< + VerificationSendable<'slice, TcStateAccepted>, + VerificationErrorWithToken, + > { + self.sendable_success_no_step( + buf, + Subservice::TmStartSuccess.into(), + token, + seq_counter, + time_stamp, + ) } /// Package and send a PUS TM\[1, 4\] packet, see 8.1.2.4 of the PUS standard. /// /// Requires a token previously acquired by calling [Self::acceptance_success]. It consumes /// the token because verification handling is done. - pub fn start_failure( + pub fn start_failure<'slice, E>( &mut self, - buf: &mut [u8], + buf: &'slice mut [u8], token: VerificationToken, - sender: &mut (impl EcssTmSenderCore + ?Sized), seq_counter: &(impl SequenceCountProviderCore + ?Sized), - params: FailParams, - ) -> Result<(), VerificationErrorWithToken> { - let tm = self - .create_pus_verif_fail_tm( - buf, - Subservice::TmStartFailure.into(), - seq_counter.get(), - &token.req_id, - None::<&dyn EcssEnumeration>, - ¶ms, - ) - .map_err(|e| VerificationErrorWithToken(e, token))?; - sender - .send_tm(tm) - .map_err(|e| VerificationErrorWithToken(e, token))?; - seq_counter.increment(); - Ok(()) + params: FailParams<'slice, '_>, + ) -> Result< + VerificationSendable<'slice, TcStateAccepted>, + VerificationErrorWithToken, + > { + Ok(VerificationSendable { + pus_tm: Some( + self.create_pus_verif_fail_tm( + buf, + Subservice::TmStartFailure.into(), + seq_counter.get(), + &token.req_id, + None::<&dyn EcssEnumeration>, + ¶ms, + ) + .map_err(|e| VerificationErrorWithToken(e, token))?, + ), + token: Some(token), + }) } /// Package and send a PUS TM\[1, 5\] packet, see 8.1.2.5 of the PUS standard. /// /// Requires a token previously acquired by calling [Self::start_success]. - pub fn step_success( + pub fn step_success<'slice, E>( &mut self, - buf: &mut [u8], + buf: &'slice mut [u8], token: &VerificationToken, - sender: &mut (impl EcssTmSenderCore + ?Sized), seq_counter: &(impl SequenceCountProviderCore + ?Sized), - time_stamp: &[u8], + time_stamp: &'slice [u8], step: impl EcssEnumeration, - ) -> Result<(), EcssTmError> { - let tm = self.create_pus_verif_success_tm( - buf, - Subservice::TmStepSuccess.into(), - seq_counter.get(), - &token.req_id, - time_stamp, - Some(&step), - )?; - sender.send_tm(tm)?; - seq_counter.increment(); - Ok(()) + ) -> Result, EcssTmError> { + Ok(VerificationSendable { + pus_tm: Some(self.create_pus_verif_success_tm( + buf, + Subservice::TmStepSuccess.into(), + seq_counter.get(), + &token.req_id, + time_stamp, + Some(&step), + )?), + token: None, + }) } /// Package and send a PUS TM\[1, 6\] packet, see 8.1.2.6 of the PUS standard. /// /// Requires a token previously acquired by calling [Self::start_success]. It consumes the /// token because verification handling is done. - pub fn step_failure( + pub fn step_failure<'slice, E>( &mut self, - buf: &mut [u8], + buf: &'slice mut [u8], token: VerificationToken, - sender: &mut (impl EcssTmSenderCore + ?Sized), seq_counter: &(impl SequenceCountProviderCore + ?Sized), - params: FailParamsWithStep, - ) -> Result<(), VerificationErrorWithToken> { - let tm = self - .create_pus_verif_fail_tm( - buf, - Subservice::TmStepFailure.into(), - seq_counter.get(), - &token.req_id, - Some(params.step), - ¶ms.bp, - ) - .map_err(|e| VerificationErrorWithToken(e, token))?; - sender - .send_tm(tm) - .map_err(|e| VerificationErrorWithToken(e, token))?; - seq_counter.increment(); - Ok(()) + params: FailParamsWithStep<'slice, '_>, + ) -> Result< + VerificationSendable<'slice, TcStateStarted>, + VerificationErrorWithToken, + > { + Ok(VerificationSendable { + pus_tm: Some( + self.create_pus_verif_fail_tm( + buf, + Subservice::TmStepFailure.into(), + seq_counter.get(), + &token.req_id, + Some(params.step), + ¶ms.bp, + ) + .map_err(|e| VerificationErrorWithToken(e, token))?, + ), + token: Some(token), + }) } /// Package and send a PUS TM\[1, 7\] packet, see 8.1.2.7 of the PUS standard. /// /// Requires a token previously acquired by calling [Self::start_success]. It consumes the /// token because verification handling is done. - pub fn completion_success( + pub fn completion_success<'slice, E>( &mut self, - buf: &mut [u8], + buf: &'slice mut [u8], token: VerificationToken, - sender: &mut (impl EcssTmSenderCore + ?Sized), seq_counter: &(impl SequenceCountProviderCore + ?Sized), - time_stamp: &[u8], - ) -> Result<(), VerificationErrorWithToken> { - let tm = self - .create_pus_verif_success_tm( - buf, - Subservice::TmCompletionSuccess.into(), - seq_counter.get(), - &token.req_id, - time_stamp, - None::<&dyn EcssEnumeration>, - ) - .map_err(|e| VerificationErrorWithToken(e, token))?; - sender - .send_tm(tm) - .map_err(|e| VerificationErrorWithToken(e, token))?; - seq_counter.increment(); - Ok(()) + time_stamp: &'slice [u8], + ) -> Result< + VerificationSendable<'slice, TcStateStarted>, + VerificationErrorWithToken, + > { + self.sendable_success_no_step( + buf, + Subservice::TmCompletionSuccess.into(), + token, + seq_counter, + time_stamp, + ) } /// Package and send a PUS TM\[1, 8\] packet, see 8.1.2.8 of the PUS standard. /// /// Requires a token previously acquired by calling [Self::start_success]. It consumes the /// token because verification handling is done. - pub fn completion_failure( + pub fn completion_failure<'slice, E>( &mut self, - buf: &mut [u8], + buf: &'slice mut [u8], token: VerificationToken, - sender: &mut (impl EcssTmSenderCore + ?Sized), seq_counter: &(impl SequenceCountProviderCore + ?Sized), - params: FailParams, - ) -> Result<(), VerificationErrorWithToken> { - let tm = self - .create_pus_verif_fail_tm( - buf, - Subservice::TmCompletionFailure.into(), - seq_counter.get(), - &token.req_id, - None::<&dyn EcssEnumeration>, - ¶ms, - ) - .map_err(|e| VerificationErrorWithToken(e, token))?; - sender - .send_tm(tm) - .map_err(|e| VerificationErrorWithToken(e, token))?; - seq_counter.increment(); - Ok(()) + params: FailParams<'slice, '_>, + ) -> Result< + VerificationSendable<'slice, TcStateStarted>, + VerificationErrorWithToken, + > { + Ok(VerificationSendable { + pus_tm: Some( + self.create_pus_verif_fail_tm( + buf, + Subservice::TmCompletionFailure.into(), + seq_counter.get(), + &token.req_id, + None::<&dyn EcssEnumeration>, + ¶ms, + ) + .map_err(|e| VerificationErrorWithToken(e, token))?, + ), + token: Some(token), + }) } - fn create_pus_verif_success_tm<'a, E>( - &'a mut self, - buf: &'a mut [u8], + fn create_pus_verif_success_tm<'slice, E>( + &mut self, + buf: &'slice mut [u8], subservice: u8, msg_counter: u16, req_id: &RequestId, - time_stamp: &'a [u8], + time_stamp: &'slice [u8], step: Option<&(impl EcssEnumeration + ?Sized)>, - ) -> Result> { + ) -> Result, EcssTmError> { let mut source_data_len = size_of::(); if let Some(step) = step { source_data_len += step.byte_width(); @@ -597,15 +671,15 @@ impl VerificationReporterCore { )) } - fn create_pus_verif_fail_tm<'a, E>( - &'a mut self, - buf: &'a mut [u8], + fn create_pus_verif_fail_tm<'slice, E>( + &mut self, + buf: &'slice mut [u8], subservice: u8, msg_counter: u16, req_id: &RequestId, step: Option<&(impl EcssEnumeration + ?Sized)>, - params: &'a FailParams, - ) -> Result> { + params: &FailParams<'slice, '_>, + ) -> Result, EcssTmError> { let mut idx = 0; let mut source_data_len = RequestId::SIZE_AS_BYTES + params.failure_code.byte_width(); if let Some(step) = step { @@ -641,15 +715,15 @@ impl VerificationReporterCore { )) } - fn create_pus_verif_tm_base<'a>( - &'a mut self, - buf: &'a mut [u8], + fn create_pus_verif_tm_base<'slice>( + &mut self, + buf: &'slice mut [u8], subservice: u8, msg_counter: u16, sp_header: &mut SpHeader, - time_stamp: &'a [u8], + time_stamp: &'slice [u8], source_data_len: usize, - ) -> PusTm { + ) -> PusTm<'slice> { let tm_sec_header = PusTmSecondaryHeader::new(1, subservice, msg_counter, self.dest_id, time_stamp); PusTm::new( @@ -748,13 +822,17 @@ mod alloc_mod { time_stamp: &[u8], ) -> Result, VerificationErrorWithToken> { - self.reporter.acceptance_success( + let mut sendable = self.reporter.acceptance_success( self.source_data_buf.as_mut_slice(), token, - sender, - self.seq_counter.as_mut(), + self.seq_counter.as_ref(), time_stamp, - ) + )?; + sender + .send_tm(sendable.take_tm()) + .map_err(|e| VerificationErrorWithToken(e, token))?; + let token = sendable.send_success_acceptance_success(Some(self.seq_counter.as_ref())); + Ok(token) } /// Package and send a PUS TM\[1, 2\] packet, see 8.1.2.2 of the PUS standard @@ -764,13 +842,17 @@ mod alloc_mod { sender: &mut (impl EcssTmSenderCore + ?Sized), params: FailParams, ) -> Result<(), VerificationErrorWithToken> { - self.reporter.acceptance_failure( + let mut sendable = self.reporter.acceptance_failure( self.source_data_buf.as_mut_slice(), token, - sender, - self.seq_counter.as_mut(), + self.seq_counter.as_ref(), params, - ) + )?; + sender + .send_tm(sendable.take_tm()) + .map_err(|e| VerificationErrorWithToken(e, token))?; + sendable.send_success_verif_failure(Some(self.seq_counter.as_ref())); + Ok(()) } /// Package and send a PUS TM\[1, 3\] packet, see 8.1.2.3 of the PUS standard. @@ -783,13 +865,17 @@ mod alloc_mod { time_stamp: &[u8], ) -> Result, VerificationErrorWithToken> { - self.reporter.start_success( + let mut sendable = self.reporter.start_success( self.source_data_buf.as_mut_slice(), token, - sender, self.seq_counter.as_mut(), time_stamp, - ) + )?; + sender + .send_tm(sendable.take_tm()) + .map_err(|e| VerificationErrorWithToken(e, token))?; + let token = sendable.send_success_start_success(Some(self.seq_counter.as_ref())); + Ok(token) } /// Package and send a PUS TM\[1, 4\] packet, see 8.1.2.4 of the PUS standard. @@ -802,13 +888,17 @@ mod alloc_mod { sender: &mut (impl EcssTmSenderCore + ?Sized), params: FailParams, ) -> Result<(), VerificationErrorWithToken> { - self.reporter.start_failure( + let mut sendable = self.reporter.start_failure( self.source_data_buf.as_mut_slice(), token, - sender, self.seq_counter.as_mut(), params, - ) + )?; + sender + .send_tm(sendable.take_tm()) + .map_err(|e| VerificationErrorWithToken(e, token))?; + sendable.send_success_verif_failure(Some(self.seq_counter.as_ref())); + Ok(()) } /// Package and send a PUS TM\[1, 5\] packet, see 8.1.2.5 of the PUS standard. @@ -821,14 +911,16 @@ mod alloc_mod { time_stamp: &[u8], step: impl EcssEnumeration, ) -> Result<(), EcssTmError> { - self.reporter.step_success( + let mut sendable = self.reporter.step_success( self.source_data_buf.as_mut_slice(), token, - sender, self.seq_counter.as_mut(), time_stamp, step, - ) + )?; + sender.send_tm(sendable.take_tm())?; + sendable.send_success_step_or_completion_success(Some(self.seq_counter.as_ref())); + Ok(()) } /// Package and send a PUS TM\[1, 6\] packet, see 8.1.2.6 of the PUS standard. @@ -841,13 +933,17 @@ mod alloc_mod { sender: &mut (impl EcssTmSenderCore + ?Sized), params: FailParamsWithStep, ) -> Result<(), VerificationErrorWithToken> { - self.reporter.step_failure( + let mut sendable = self.reporter.step_failure( self.source_data_buf.as_mut_slice(), token, - sender, self.seq_counter.as_mut(), params, - ) + )?; + sender + .send_tm(sendable.take_tm()) + .map_err(|e| VerificationErrorWithToken(e, token))?; + sendable.send_success_step_or_completion_success(Some(self.seq_counter.as_ref())); + Ok(()) } /// Package and send a PUS TM\[1, 7\] packet, see 8.1.2.7 of the PUS standard. @@ -860,13 +956,17 @@ mod alloc_mod { sender: &mut (impl EcssTmSenderCore + ?Sized), time_stamp: &[u8], ) -> Result<(), VerificationErrorWithToken> { - self.reporter.completion_success( + let mut sendable = self.reporter.completion_success( self.source_data_buf.as_mut_slice(), token, - sender, self.seq_counter.as_mut(), time_stamp, - ) + )?; + sender + .send_tm(sendable.take_tm()) + .map_err(|e| VerificationErrorWithToken(e, token))?; + sendable.send_success_step_or_completion_success(Some(self.seq_counter.as_ref())); + Ok(()) } /// Package and send a PUS TM\[1, 8\] packet, see 8.1.2.8 of the PUS standard. @@ -879,13 +979,17 @@ mod alloc_mod { sender: &mut (impl EcssTmSenderCore + ?Sized), params: FailParams, ) -> Result<(), VerificationErrorWithToken> { - self.reporter.completion_failure( + let mut sendable = self.reporter.completion_failure( self.source_data_buf.as_mut_slice(), token, - sender, self.seq_counter.as_mut(), params, - ) + )?; + sender + .send_tm(sendable.take_tm()) + .map_err(|e| VerificationErrorWithToken(e, token))?; + sendable.send_success_step_or_completion_success(Some(self.seq_counter.as_ref())); + Ok(()) } }