remove mut bound for seq counter impl

This commit is contained in:
Robin Müller 2023-01-03 17:20:17 +01:00
parent 328a060e9c
commit 245388fafa
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
3 changed files with 54 additions and 28 deletions

View File

@ -340,7 +340,7 @@ impl VerificationReporterCore {
buf: &mut [u8], buf: &mut [u8],
token: VerificationToken<TcStateNone>, token: VerificationToken<TcStateNone>,
sender: &mut (impl EcssTmSenderCore<Error = E> + ?Sized), sender: &mut (impl EcssTmSenderCore<Error = E> + ?Sized),
seq_counter: &mut (impl SequenceCountProviderCore<u16> + ?Sized), seq_counter: &(impl SequenceCountProviderCore<u16> + ?Sized),
time_stamp: &[u8], time_stamp: &[u8],
) -> Result<VerificationToken<TcStateAccepted>, VerificationErrorWithToken<E, TcStateNone>> ) -> Result<VerificationToken<TcStateAccepted>, VerificationErrorWithToken<E, TcStateNone>>
{ {
@ -371,7 +371,7 @@ impl VerificationReporterCore {
buf: &mut [u8], buf: &mut [u8],
token: VerificationToken<TcStateNone>, token: VerificationToken<TcStateNone>,
sender: &mut (impl EcssTmSenderCore<Error = E> + ?Sized), sender: &mut (impl EcssTmSenderCore<Error = E> + ?Sized),
seq_counter: &mut (impl SequenceCountProviderCore<u16> + ?Sized), seq_counter: &(impl SequenceCountProviderCore<u16> + ?Sized),
params: FailParams, params: FailParams,
) -> Result<(), VerificationErrorWithToken<E, TcStateNone>> { ) -> Result<(), VerificationErrorWithToken<E, TcStateNone>> {
let tm = self let tm = self
@ -399,7 +399,7 @@ impl VerificationReporterCore {
buf: &mut [u8], buf: &mut [u8],
token: VerificationToken<TcStateAccepted>, token: VerificationToken<TcStateAccepted>,
sender: &mut (impl EcssTmSenderCore<Error = E> + ?Sized), sender: &mut (impl EcssTmSenderCore<Error = E> + ?Sized),
seq_counter: &mut (impl SequenceCountProviderCore<u16> + ?Sized), seq_counter: &(impl SequenceCountProviderCore<u16> + ?Sized),
time_stamp: &[u8], time_stamp: &[u8],
) -> Result<VerificationToken<TcStateStarted>, VerificationErrorWithToken<E, TcStateAccepted>> ) -> Result<VerificationToken<TcStateStarted>, VerificationErrorWithToken<E, TcStateAccepted>>
{ {
@ -432,7 +432,7 @@ impl VerificationReporterCore {
buf: &mut [u8], buf: &mut [u8],
token: VerificationToken<TcStateAccepted>, token: VerificationToken<TcStateAccepted>,
sender: &mut (impl EcssTmSenderCore<Error = E> + ?Sized), sender: &mut (impl EcssTmSenderCore<Error = E> + ?Sized),
seq_counter: &mut (impl SequenceCountProviderCore<u16> + ?Sized), seq_counter: &(impl SequenceCountProviderCore<u16> + ?Sized),
params: FailParams, params: FailParams,
) -> Result<(), VerificationErrorWithToken<E, TcStateAccepted>> { ) -> Result<(), VerificationErrorWithToken<E, TcStateAccepted>> {
let tm = self let tm = self
@ -460,7 +460,7 @@ impl VerificationReporterCore {
buf: &mut [u8], buf: &mut [u8],
token: &VerificationToken<TcStateStarted>, token: &VerificationToken<TcStateStarted>,
sender: &mut (impl EcssTmSenderCore<Error = E> + ?Sized), sender: &mut (impl EcssTmSenderCore<Error = E> + ?Sized),
seq_counter: &mut (impl SequenceCountProviderCore<u16> + ?Sized), seq_counter: &(impl SequenceCountProviderCore<u16> + ?Sized),
time_stamp: &[u8], time_stamp: &[u8],
step: impl EcssEnumeration, step: impl EcssEnumeration,
) -> Result<(), EcssTmError<E>> { ) -> Result<(), EcssTmError<E>> {
@ -486,7 +486,7 @@ impl VerificationReporterCore {
buf: &mut [u8], buf: &mut [u8],
token: VerificationToken<TcStateStarted>, token: VerificationToken<TcStateStarted>,
sender: &mut (impl EcssTmSenderCore<Error = E> + ?Sized), sender: &mut (impl EcssTmSenderCore<Error = E> + ?Sized),
seq_counter: &mut (impl SequenceCountProviderCore<u16> + ?Sized), seq_counter: &(impl SequenceCountProviderCore<u16> + ?Sized),
params: FailParamsWithStep, params: FailParamsWithStep,
) -> Result<(), VerificationErrorWithToken<E, TcStateStarted>> { ) -> Result<(), VerificationErrorWithToken<E, TcStateStarted>> {
let tm = self let tm = self
@ -515,7 +515,7 @@ impl VerificationReporterCore {
buf: &mut [u8], buf: &mut [u8],
token: VerificationToken<TcStateStarted>, token: VerificationToken<TcStateStarted>,
sender: &mut (impl EcssTmSenderCore<Error = E> + ?Sized), sender: &mut (impl EcssTmSenderCore<Error = E> + ?Sized),
seq_counter: &mut (impl SequenceCountProviderCore<u16> + ?Sized), seq_counter: &(impl SequenceCountProviderCore<u16> + ?Sized),
time_stamp: &[u8], time_stamp: &[u8],
) -> Result<(), VerificationErrorWithToken<E, TcStateStarted>> { ) -> Result<(), VerificationErrorWithToken<E, TcStateStarted>> {
let tm = self let tm = self
@ -544,7 +544,7 @@ impl VerificationReporterCore {
buf: &mut [u8], buf: &mut [u8],
token: VerificationToken<TcStateStarted>, token: VerificationToken<TcStateStarted>,
sender: &mut (impl EcssTmSenderCore<Error = E> + ?Sized), sender: &mut (impl EcssTmSenderCore<Error = E> + ?Sized),
seq_counter: &mut (impl SequenceCountProviderCore<u16> + ?Sized), seq_counter: &(impl SequenceCountProviderCore<u16> + ?Sized),
params: FailParams, params: FailParams,
) -> Result<(), VerificationErrorWithToken<E, TcStateStarted>> { ) -> Result<(), VerificationErrorWithToken<E, TcStateStarted>> {
let tm = self let tm = self

View File

@ -1,17 +1,35 @@
use core::cell::Cell;
use core::sync::atomic::{AtomicU16, Ordering};
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
use dyn_clone::DynClone; use dyn_clone::DynClone;
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub use stdmod::*; pub use stdmod::*;
/// Core trait for objects which can provide a sequence count. /// Core trait for objects which can provide a sequence count.
///
/// The core functions are not mutable on purpose to allow easier usage with
/// static structs when using the interior mutability pattern. This can be achieved by using
/// [Cell], [RefCell] or atomic types.
pub trait SequenceCountProviderCore<Raw> { pub trait SequenceCountProviderCore<Raw> {
fn get(&self) -> Raw; fn get(&self) -> Raw;
fn increment(&mut self);
fn get_and_increment(&mut self) -> Raw { fn increment(&self);
// TODO: Maybe remove this?
fn increment_mut(&mut self) {
self.increment();
}
fn get_and_increment(&self) -> Raw {
let val = self.get(); let val = self.get();
self.increment(); self.increment();
val val
} }
// TODO: Maybe remove this?
fn get_and_increment_mut(&mut self) -> Raw {
self.get_and_increment()
}
} }
/// Extension trait which allows cloning a sequence count provider after it was turned into /// Extension trait which allows cloning a sequence count provider after it was turned into
@ -25,25 +43,30 @@ impl<T, Raw> SequenceCountProvider<Raw> for T where T: SequenceCountProviderCore
#[derive(Default, Clone)] #[derive(Default, Clone)]
pub struct SeqCountProviderSimple { pub struct SeqCountProviderSimple {
seq_count: u16, seq_count: Cell<u16>,
} }
impl SequenceCountProviderCore<u16> for SeqCountProviderSimple { impl SequenceCountProviderCore<u16> for SeqCountProviderSimple {
fn get(&self) -> u16 { fn get(&self) -> u16 {
self.seq_count self.seq_count.get()
} }
fn increment(&mut self) { fn increment(&self) {
if self.seq_count == u16::MAX { self.get_and_increment();
self.seq_count = 0;
return;
} }
self.seq_count += 1;
fn get_and_increment(&self) -> u16 {
let curr_count = self.seq_count.get();
if curr_count == u16::MAX {
self.seq_count.set(0);
} else {
self.seq_count.set(curr_count + 1);
}
curr_count
} }
} }
use core::sync::atomic::{AtomicU16, Ordering};
pub struct SeqCountProviderAtomicRef { pub struct SeqCountProviderAtomicRef {
atomic: AtomicU16, atomic: AtomicU16,
ordering: Ordering, ordering: Ordering,
@ -51,7 +74,10 @@ pub struct SeqCountProviderAtomicRef {
impl SeqCountProviderAtomicRef { impl SeqCountProviderAtomicRef {
pub const fn new(ordering: Ordering) -> Self { pub const fn new(ordering: Ordering) -> Self {
Self { atomic: AtomicU16::new(0), ordering } Self {
atomic: AtomicU16::new(0),
ordering,
}
} }
} }
@ -60,11 +86,11 @@ impl SequenceCountProviderCore<u16> for SeqCountProviderAtomicRef {
self.atomic.load(self.ordering) self.atomic.load(self.ordering)
} }
fn increment(&mut self) { fn increment(&self) {
self.atomic.fetch_add(1, self.ordering); self.atomic.fetch_add(1, self.ordering);
} }
fn get_and_increment(&mut self) -> u16 { fn get_and_increment(&self) -> u16 {
self.atomic.fetch_add(1, self.ordering) self.atomic.fetch_add(1, self.ordering)
} }
} }
@ -75,20 +101,20 @@ pub mod stdmod {
use std::sync::Arc; use std::sync::Arc;
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct SeqCountProviderSync { pub struct SeqCountProviderSyncClonable {
seq_count: Arc<AtomicU16>, seq_count: Arc<AtomicU16>,
} }
impl SequenceCountProviderCore<u16> for SeqCountProviderSync { impl SequenceCountProviderCore<u16> for SeqCountProviderSyncClonable {
fn get(&self) -> u16 { fn get(&self) -> u16 {
self.seq_count.load(Ordering::SeqCst) self.seq_count.load(Ordering::SeqCst)
} }
fn increment(&mut self) { fn increment(&self) {
self.seq_count.fetch_add(1, Ordering::SeqCst); self.seq_count.fetch_add(1, Ordering::SeqCst);
} }
fn get_and_increment(&mut self) -> u16 { fn get_and_increment(&self) -> u16 {
self.seq_count.fetch_add(1, Ordering::SeqCst) self.seq_count.fetch_add(1, Ordering::SeqCst)
} }
} }

View File

@ -7,7 +7,7 @@ pub mod crossbeam_test {
CrossbeamVerifSender, FailParams, RequestId, VerificationReporterCfg, CrossbeamVerifSender, FailParams, RequestId, VerificationReporterCfg,
VerificationReporterWithSender, VerificationReporterWithSender,
}; };
use satrs_core::seq_count::SeqCountProviderSync; use satrs_core::seq_count::SeqCountProviderSyncClonable;
use spacepackets::ecss::{EcssEnumU16, EcssEnumU8, PusPacket}; use spacepackets::ecss::{EcssEnumU16, EcssEnumU8, PusPacket};
use spacepackets::tc::{PusTc, PusTcSecondaryHeader}; use spacepackets::tc::{PusTc, PusTcSecondaryHeader};
use spacepackets::tm::PusTm; use spacepackets::tm::PusTm;
@ -34,7 +34,7 @@ pub mod crossbeam_test {
// each reporter have an own sequence count provider. // each reporter have an own sequence count provider.
let cfg = VerificationReporterCfg::new( let cfg = VerificationReporterCfg::new(
TEST_APID, TEST_APID,
Box::new(SeqCountProviderSync::default()), Box::new(SeqCountProviderSyncClonable::default()),
1, 1,
2, 2,
8, 8,