this is really annoying

This commit is contained in:
Robin Müller 2022-08-13 19:44:52 +02:00
parent 946ced2a49
commit a45bc074a0
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
6 changed files with 84 additions and 54 deletions

30
Cargo.lock generated
View File

@ -124,6 +124,12 @@ dependencies = [
"bitflags", "bitflags",
] ]
[[package]]
name = "cobs"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67ba02a97a2bd10f4b59b25c7973101c79642302776489e030cd13cdab09ed15"
[[package]] [[package]]
name = "cortex-m" name = "cortex-m"
version = "0.7.5" version = "0.7.5"
@ -211,8 +217,11 @@ version = "0.1.0"
dependencies = [ dependencies = [
"bus", "bus",
"num", "num",
"postcard 1.0.1",
"serde",
"spacepackets", "spacepackets",
"thiserror", "thiserror",
"zerocopy",
] ]
[[package]] [[package]]
@ -431,6 +440,17 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "postcard"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41f5465c5e5a38e04552d8fb53ebcf4f58124ab3bbd0c02add043b33f82792e5"
dependencies = [
"cobs",
"heapless",
"serde",
]
[[package]] [[package]]
name = "postcard-cobs" name = "postcard-cobs"
version = "0.1.5-pre" version = "0.1.5-pre"
@ -546,18 +566,18 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.142" version = "1.0.143"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e590c437916fb6b221e1d00df6e3294f3fccd70ca7e92541c475d6ed6ef5fee2" checksum = "53e8e5d5b70924f74ff5c6d64d9a5acd91422117c60f48c4e07855238a254553"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.142" version = "1.0.143"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34b5b8d809babe02f538c2cfec6f2c1ed10804c0e5a6a041a049a4f5588ccc2e" checksum = "d3d8e8de557aee63c26b85b947f5e59b690d0454c753f3adeb5cd7835ab88391"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -578,7 +598,7 @@ dependencies = [
"crc", "crc",
"delegate", "delegate",
"num", "num",
"postcard", "postcard 0.7.3",
"serde", "serde",
"zerocopy", "zerocopy",
] ]

View File

@ -11,6 +11,11 @@ bus = "2.2.3"
num = "0.4" num = "0.4"
spacepackets = { path = "../spacepackets"} spacepackets = { path = "../spacepackets"}
[dev-dependencies]
postcard = { version = "1.0.1", features = ["use-std"] }
serde = "1.0.143"
zerocopy = "0.6.1"
[features] [features]
default = ["use_std"] default = ["use_std"]
use_std = [] use_std = []

View File

@ -23,7 +23,7 @@
//! // Add new data to the pool //! // Add new data to the pool
//! let mut example_data = [0; 4]; //! let mut example_data = [0; 4];
//! example_data[0] = 42; //! example_data[0] = 42;
//! let res = local_pool.add(example_data); //! let res = local_pool.add(&example_data);
//! assert!(res.is_ok()); //! assert!(res.is_ok());
//! addr = res.unwrap(); //! addr = res.unwrap();
//! } //! }

View File

@ -1,26 +1,25 @@
use crate::error::FsrcErrorHandler; use crate::error::FsrcErrorHandler;
use crate::tmtc::{ use crate::tmtc::{ReceivesTc, FROM_BYTES_SLICE_TOO_SMALL_ERROR, FROM_BYTES_ZEROCOPY_ERROR};
ReceivesCcsds, ReceivesTc, FROM_BYTES_SLICE_TOO_SMALL_ERROR, FROM_BYTES_ZEROCOPY_ERROR,
};
use spacepackets::{CcsdsPacket, PacketError, SpHeader}; use spacepackets::{CcsdsPacket, PacketError, SpHeader};
pub trait HandlesPacketForApid { pub trait HandlesPacketForApid {
fn get_apid_handler(&mut self, apid: u16) -> Option<&mut Box<dyn ReceivesCcsds>>; fn valid_apids(&self) -> &'static [u16];
fn handle_known_apid(&mut self, sp_header: &SpHeader, tc_raw: &[u8]);
fn handle_unknown_apid(&mut self, sp_header: &SpHeader, tc_raw: &[u8]); fn handle_unknown_apid(&mut self, sp_header: &SpHeader, tc_raw: &[u8]);
} }
pub struct CcsdsDistributor { pub struct CcsdsDistributor {
apid_handlers: Box<dyn HandlesPacketForApid>, apid_handler: Box<dyn HandlesPacketForApid>,
error_handler: Box<dyn FsrcErrorHandler>, error_handler: Box<dyn FsrcErrorHandler>,
} }
impl CcsdsDistributor { impl<'a> CcsdsDistributor {
pub fn new( pub fn new(
apid_handlers: Box<dyn HandlesPacketForApid>, apid_handler: Box<dyn HandlesPacketForApid>,
error_handler: Box<dyn FsrcErrorHandler>, error_handler: Box<dyn FsrcErrorHandler>,
) -> Self { ) -> Self {
CcsdsDistributor { CcsdsDistributor {
apid_handlers, apid_handler,
error_handler, error_handler,
} }
} }
@ -50,13 +49,12 @@ impl ReceivesTc for CcsdsDistributor {
} }
}; };
let apid = sp_header.apid(); let apid = sp_header.apid();
match self.apid_handlers.get_apid_handler(apid) { let valid_apids = self.apid_handler.valid_apids();
None => { for &valid_apid in valid_apids {
self.apid_handlers.handle_unknown_apid(&sp_header, tm_raw); if valid_apid == apid {
} self.apid_handler.handle_known_apid(&sp_header, tm_raw);
Some(handler) => {
handler.pass_ccsds(&sp_header, tm_raw).ok();
} }
} }
self.apid_handler.handle_unknown_apid(&sp_header, tm_raw);
} }
} }

View File

@ -29,11 +29,11 @@ pub trait ReceivesTc {
fn pass_tc(&mut self, tc_raw: &[u8]); fn pass_tc(&mut self, tc_raw: &[u8]);
} }
pub trait ReceivesCcsds { pub trait ReceivesCcsdsTc {
fn pass_ccsds(&mut self, header: &SpHeader, tm_raw: &[u8]) -> Result<(), PacketError>; fn pass_ccsds(&mut self, header: &SpHeader, tc_raw: &[u8]) -> Result<(), PacketError>;
} }
pub trait ReceivesPus { pub trait ReceivesPusTc {
fn pass_pus(&mut self, pus_tc: &PusTc) -> Result<(), PusError>; fn pass_pus(&mut self, pus_tc: &PusTc) -> Result<(), PusError>;
} }
@ -43,54 +43,61 @@ mod tests {
use crate::error::SimpleStdErrorHandler; use crate::error::SimpleStdErrorHandler;
use crate::tmtc::ccsds_distrib::{CcsdsDistributor, HandlesPacketForApid}; use crate::tmtc::ccsds_distrib::{CcsdsDistributor, HandlesPacketForApid};
use spacepackets::CcsdsPacket; use spacepackets::CcsdsPacket;
use std::collections::HashMap; use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
#[derive(Copy, Clone)] #[derive(Default)]
struct DummyCcsdsHandler {}
impl ReceivesCcsds for DummyCcsdsHandler {
fn pass_ccsds(&mut self, header: &SpHeader, tm_raw: &[u8]) -> Result<(), PacketError> {
println!("CCSDS packet with header {:?} received", header);
println!("Raw data with len {}: {:x?}", tm_raw.len(), tm_raw);
Ok(())
}
}
struct ApidHandler { struct ApidHandler {
handler_map: HashMap<u16, Box<dyn ReceivesCcsds>>, known_packet_queue: Arc<Mutex<VecDeque<(u16, Vec<u8>)>>>,
unknown_packet_queue: Arc<Mutex<VecDeque<(u16, Vec<u8>)>>>,
} }
impl ApidHandler {
pub fn add_ccsds_handler(&mut self, apid: u16, ccsds_receiver: Box<dyn ReceivesCcsds>) {
// TODO: Error handling
self.handler_map.insert(apid, ccsds_receiver);
}
}
impl HandlesPacketForApid for ApidHandler { impl HandlesPacketForApid for ApidHandler {
fn get_apid_handler(&mut self, apid: u16) -> Option<&mut Box<dyn ReceivesCcsds>> { fn valid_apids(&self) -> &'static [u16] {
self.handler_map.get_mut(&apid) &[0x000, 0x002]
}
fn handle_known_apid(&mut self, sp_header: &SpHeader, tc_raw: &[u8]) {
let mut vec = Vec::new();
vec.extend_from_slice(tc_raw);
self.known_packet_queue
.lock()
.unwrap()
.push_back((sp_header.apid(), vec));
} }
fn handle_unknown_apid(&mut self, sp_header: &SpHeader, tc_raw: &[u8]) { fn handle_unknown_apid(&mut self, sp_header: &SpHeader, tc_raw: &[u8]) {
println!("Packet with unknown APID {} received", sp_header.apid()); let mut vec = Vec::new();
println!("Packet with len {}: {:x?}", tc_raw.len(), tc_raw); vec.extend_from_slice(tc_raw);
self.unknown_packet_queue
.lock()
.unwrap()
.push_back((sp_header.apid(), vec));
} }
} }
#[test] #[test]
fn test_distribs() { fn test_distribs_known_apid() {
let ccsds_handler = DummyCcsdsHandler {}; let known_packet_queue = Arc::new(Mutex::default());
let mut apid_handler = ApidHandler { let unknown_packet_queue = Arc::new(Mutex::default());
handler_map: HashMap::new(), let apid_handler = ApidHandler {
known_packet_queue: known_packet_queue.clone(),
unknown_packet_queue: unknown_packet_queue.clone(),
}; };
let error_handler = SimpleStdErrorHandler {}; let error_handler = SimpleStdErrorHandler {};
apid_handler.add_ccsds_handler(0, Box::new(ccsds_handler));
let mut ccsds_distrib = let mut ccsds_distrib =
CcsdsDistributor::new(Box::new(apid_handler), Box::new(error_handler)); CcsdsDistributor::new(Box::new(apid_handler), Box::new(error_handler));
let mut sph = SpHeader::tc(0, 0x34, 0).unwrap(); let mut sph = SpHeader::tc(0x002, 0x34, 0).unwrap();
let pus_tc = PusTc::new_simple(&mut sph, 17, 1, None, true); let pus_tc = PusTc::new_simple(&mut sph, 17, 1, None, true);
let mut test_buf: [u8; 32] = [0; 32]; let mut test_buf: [u8; 32] = [0; 32];
pus_tc pus_tc
.write_to(test_buf.as_mut_slice()) .write_to(test_buf.as_mut_slice())
.expect("Error writing TC to buffer"); .expect("Error writing TC to buffer");
ccsds_distrib.pass_tc(&test_buf); ccsds_distrib.pass_tc(&test_buf);
let recvd = known_packet_queue.lock().unwrap().pop_front();
assert!(recvd.is_some());
let (apid, packet) = recvd.unwrap();
assert_eq!(apid, 0x002);
assert_eq!(packet.as_slice(), test_buf);
} }
} }

View File

@ -1,12 +1,12 @@
use crate::error::FsrcErrorHandler; use crate::error::FsrcErrorHandler;
use crate::tmtc::{ReceivesCcsds, ReceivesPus, ReceivesTc}; use crate::tmtc::{ReceivesCcsdsTc, ReceivesPusTc, ReceivesTc};
use spacepackets::ecss::{PusError, PusPacket}; use spacepackets::ecss::{PusError, PusPacket};
use spacepackets::tc::PusTc; use spacepackets::tc::PusTc;
use spacepackets::{CcsdsPacket, PacketError, SpHeader}; use spacepackets::{CcsdsPacket, PacketError, SpHeader};
pub trait PusServiceProvider { pub trait PusServiceProvider {
fn get_apid(&self, service: u8) -> u16; fn get_apid(&self, service: u8) -> u16;
fn get_service_handler(&self, service: u8, subservice: u8) -> Box<dyn ReceivesPus>; fn get_service_handler(&self, service: u8, subservice: u8) -> Box<dyn ReceivesPusTc>;
} }
pub struct PusDistributor { pub struct PusDistributor {
@ -22,7 +22,7 @@ impl ReceivesTc for PusDistributor {
} }
} }
impl ReceivesCcsds for PusDistributor { impl ReceivesCcsdsTc for PusDistributor {
fn pass_ccsds(&mut self, _header: &SpHeader, tm_raw: &[u8]) -> Result<(), PacketError> { fn pass_ccsds(&mut self, _header: &SpHeader, tm_raw: &[u8]) -> Result<(), PacketError> {
// TODO: Better error handling // TODO: Better error handling
let (tc, _) = match PusTc::new_from_raw_slice(tm_raw) { let (tc, _) = match PusTc::new_from_raw_slice(tm_raw) {