first basic scheduler impl

This commit is contained in:
Robin Müller 2023-01-21 14:29:40 +01:00
parent 300eabafe2
commit ce6f804eed
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
4 changed files with 41 additions and 10 deletions

4
Cargo.lock generated
View File

@ -568,7 +568,7 @@ dependencies = [
"postcard", "postcard",
"serde", "serde",
"serde_json", "serde_json",
"spacepackets 0.5.0 (git+https://egit.irs.uni-stuttgart.de/rust/spacepackets.git?rev=74e489bd074)", "spacepackets 0.5.0 (git+https://egit.irs.uni-stuttgart.de/rust/spacepackets.git?rev=fc76a975c11c54697a30471ba0e921965af4de12)",
"zerocopy", "zerocopy",
] ]
@ -698,7 +698,7 @@ dependencies = [
[[package]] [[package]]
name = "spacepackets" name = "spacepackets"
version = "0.5.0" version = "0.5.0"
source = "git+https://egit.irs.uni-stuttgart.de/rust/spacepackets.git?rev=74e489bd074#74e489bd074bbd8139a1c8740aa1670a2ef9b671" source = "git+https://egit.irs.uni-stuttgart.de/rust/spacepackets.git?rev=fc76a975c11c54697a30471ba0e921965af4de12#fc76a975c11c54697a30471ba0e921965af4de12"
dependencies = [ dependencies = [
"chrono", "chrono",
"crc", "crc",

View File

@ -53,10 +53,10 @@ default-features = false
optional = true optional = true
[dependencies.spacepackets] [dependencies.spacepackets]
git = "https://egit.irs.uni-stuttgart.de/rust/spacepackets.git"
# path = "../spacepackets" # path = "../spacepackets"
# version = "0.4.0" # version = "0.4.0"
rev = "74e489bd074" git = "https://egit.irs.uni-stuttgart.de/rust/spacepackets.git"
rev = "fc76a975c11c54697a30471ba0e921965af4de12"
default-features = false default-features = false

View File

@ -1,19 +1,25 @@
use crate::pool::StoreAddr; use crate::pool::StoreAddr;
use alloc::collections::btree_map::Range;
use core::time::Duration;
use spacepackets::time::UnixTimestamp; use spacepackets::time::UnixTimestamp;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::vec;
use std::vec::Vec;
#[derive(Debug)] #[derive(Debug)]
pub struct PusScheduler { pub struct PusScheduler {
tc_map: BTreeMap<UnixTimestamp, StoreAddr>, tc_map: BTreeMap<UnixTimestamp, Vec<StoreAddr>>,
current_time: UnixTimestamp, current_time: UnixTimestamp,
time_margin: Duration,
enabled: bool, enabled: bool,
} }
impl PusScheduler { impl PusScheduler {
pub fn new(init_current_time: UnixTimestamp) -> Self { pub fn new(init_current_time: UnixTimestamp, time_margin: Duration) -> Self {
PusScheduler { PusScheduler {
tc_map: Default::default(), tc_map: Default::default(),
current_time: init_current_time, current_time: init_current_time,
time_margin,
enabled: true, enabled: true,
} }
} }
@ -39,8 +45,31 @@ impl PusScheduler {
self.current_time = current_time; self.current_time = current_time;
} }
pub fn insert_tc(&mut self, time_stamp: UnixTimestamp, addr: StoreAddr) { pub fn insert_tc(&mut self, time_stamp: UnixTimestamp, addr: StoreAddr) -> bool {
self.tc_map.insert(time_stamp, addr); if time_stamp > self.current_time + self.time_margin {
return false;
}
if self.tc_map.contains_key(&time_stamp) {
let tc_vec = self.tc_map.get_mut(&time_stamp).unwrap();
tc_vec.push(addr);
return true;
}
self.tc_map.insert(time_stamp, vec![addr]);
true
}
pub fn telecommands_to_release(&self) -> Range<'_, UnixTimestamp, Vec<StoreAddr>> {
self.tc_map.range(..=self.current_time)
}
pub fn release_telecommands<R: FnMut(bool, &StoreAddr)>(&mut self, mut releaser: R) {
let tcs_to_release = self.telecommands_to_release();
for tc in tcs_to_release {
for addr in tc.1 {
releaser(self.enabled, addr);
}
}
self.tc_map.retain(|k, _| k > &self.current_time);
} }
} }
@ -48,10 +77,12 @@ impl PusScheduler {
mod tests { mod tests {
use crate::pus::scheduling::PusScheduler; use crate::pus::scheduling::PusScheduler;
use spacepackets::time::UnixTimestamp; use spacepackets::time::UnixTimestamp;
use std::time::Duration;
#[test] #[test]
fn basic() { fn basic() {
let mut scheduler = PusScheduler::new(UnixTimestamp::new_only_seconds(0)); let mut scheduler =
PusScheduler::new(UnixTimestamp::new_only_seconds(0), Duration::from_secs(5));
assert!(scheduler.is_enabled()); assert!(scheduler.is_enabled());
scheduler.disable(); scheduler.disable();
assert!(!scheduler.is_enabled()); assert!(!scheduler.is_enabled());

@ -1 +1 @@
Subproject commit 74e489bd074bbd8139a1c8740aa1670a2ef9b671 Subproject commit fc76a975c11c54697a30471ba0e921965af4de12