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

View File

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

View File

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

View File

@ -1,26 +1,25 @@
use crate::error::FsrcErrorHandler;
use crate::tmtc::{
ReceivesCcsds, ReceivesTc, FROM_BYTES_SLICE_TOO_SMALL_ERROR, FROM_BYTES_ZEROCOPY_ERROR,
};
use crate::tmtc::{ReceivesTc, FROM_BYTES_SLICE_TOO_SMALL_ERROR, FROM_BYTES_ZEROCOPY_ERROR};
use spacepackets::{CcsdsPacket, PacketError, SpHeader};
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]);
}
pub struct CcsdsDistributor {
apid_handlers: Box<dyn HandlesPacketForApid>,
apid_handler: Box<dyn HandlesPacketForApid>,
error_handler: Box<dyn FsrcErrorHandler>,
}
impl CcsdsDistributor {
impl<'a> CcsdsDistributor {
pub fn new(
apid_handlers: Box<dyn HandlesPacketForApid>,
apid_handler: Box<dyn HandlesPacketForApid>,
error_handler: Box<dyn FsrcErrorHandler>,
) -> Self {
CcsdsDistributor {
apid_handlers,
apid_handler,
error_handler,
}
}
@ -50,13 +49,12 @@ impl ReceivesTc for CcsdsDistributor {
}
};
let apid = sp_header.apid();
match self.apid_handlers.get_apid_handler(apid) {
None => {
self.apid_handlers.handle_unknown_apid(&sp_header, tm_raw);
}
Some(handler) => {
handler.pass_ccsds(&sp_header, tm_raw).ok();
let valid_apids = self.apid_handler.valid_apids();
for &valid_apid in valid_apids {
if valid_apid == apid {
self.apid_handler.handle_known_apid(&sp_header, tm_raw);
}
}
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]);
}
pub trait ReceivesCcsds {
fn pass_ccsds(&mut self, header: &SpHeader, tm_raw: &[u8]) -> Result<(), PacketError>;
pub trait ReceivesCcsdsTc {
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>;
}
@ -43,54 +43,61 @@ mod tests {
use crate::error::SimpleStdErrorHandler;
use crate::tmtc::ccsds_distrib::{CcsdsDistributor, HandlesPacketForApid};
use spacepackets::CcsdsPacket;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
#[derive(Copy, Clone)]
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(())
}
}
#[derive(Default)]
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 {
fn get_apid_handler(&mut self, apid: u16) -> Option<&mut Box<dyn ReceivesCcsds>> {
self.handler_map.get_mut(&apid)
fn valid_apids(&self) -> &'static [u16] {
&[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]) {
println!("Packet with unknown APID {} received", sp_header.apid());
println!("Packet with len {}: {:x?}", tc_raw.len(), tc_raw);
let mut vec = Vec::new();
vec.extend_from_slice(tc_raw);
self.unknown_packet_queue
.lock()
.unwrap()
.push_back((sp_header.apid(), vec));
}
}
#[test]
fn test_distribs() {
let ccsds_handler = DummyCcsdsHandler {};
let mut apid_handler = ApidHandler {
handler_map: HashMap::new(),
fn test_distribs_known_apid() {
let known_packet_queue = Arc::new(Mutex::default());
let unknown_packet_queue = Arc::new(Mutex::default());
let apid_handler = ApidHandler {
known_packet_queue: known_packet_queue.clone(),
unknown_packet_queue: unknown_packet_queue.clone(),
};
let error_handler = SimpleStdErrorHandler {};
apid_handler.add_ccsds_handler(0, Box::new(ccsds_handler));
let mut ccsds_distrib =
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 mut test_buf: [u8; 32] = [0; 32];
pus_tc
.write_to(test_buf.as_mut_slice())
.expect("Error writing TC to buffer");
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::tmtc::{ReceivesCcsds, ReceivesPus, ReceivesTc};
use crate::tmtc::{ReceivesCcsdsTc, ReceivesPusTc, ReceivesTc};
use spacepackets::ecss::{PusError, PusPacket};
use spacepackets::tc::PusTc;
use spacepackets::{CcsdsPacket, PacketError, SpHeader};
pub trait PusServiceProvider {
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 {
@ -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> {
// TODO: Better error handling
let (tc, _) = match PusTc::new_from_raw_slice(tm_raw) {