first packet routing components

This commit is contained in:
2022-08-08 01:24:28 +02:00
parent 2843a18867
commit db78b02348
14 changed files with 226 additions and 2 deletions

View File

@ -0,0 +1 @@
pub mod udp_server;

View File

@ -0,0 +1,29 @@
use crate::tmtc::ReceivesTc;
use std::net::{SocketAddr, ToSocketAddrs, UdpSocket};
use std::vec::Vec;
pub struct UdpTmtcServer {
socket: UdpSocket,
recv_buf: Vec<u8>,
tc_receiver: Box<dyn ReceivesTc>,
}
impl UdpTmtcServer {
pub fn new<A: ToSocketAddrs, E>(
addr: A,
max_recv_size: usize,
tc_receiver: Box<dyn ReceivesTc>,
) -> Result<Self, std::io::Error> {
Ok(Self {
socket: UdpSocket::bind(addr)?,
recv_buf: Vec::with_capacity(max_recv_size),
tc_receiver,
})
}
pub fn recv_tc(&mut self) -> Result<(usize, SocketAddr), std::io::Error> {
let res = self.socket.recv_from(&mut self.recv_buf)?;
self.tc_receiver.pass_tc(&self.recv_buf[0..res.0]);
Ok(res)
}
}

2
fsrc-core/src/hal/mod.rs Normal file
View File

@ -0,0 +1,2 @@
#[cfg(feature = "use_std")]
pub mod host;

View File

@ -2,5 +2,7 @@
pub mod event_man;
pub mod events;
pub mod executable;
pub mod hal;
pub mod objects;
pub mod pool;
pub mod tmtc;

View File

@ -171,7 +171,7 @@ impl LocalPool {
/// Add new data to the pool. It will attempt to reserve a memory block with the appropriate
/// size and then copy the given data to the block. Yields a [StoreAddr] which can be used
/// to access the data stored in the pool
pub fn add(&mut self, data: impl AsRef<[u8]>) -> Result<StoreAddr, StoreError> {
pub fn add(&mut self, data: &[u8]) -> Result<StoreAddr, StoreError> {
let data_len = data.as_ref().len();
if data_len > Self::MAX_SIZE {
return Err(StoreError::DataTooLarge(data_len));

View File

@ -0,0 +1,19 @@
use crate::tmtc::{ReceivesCcsds, ReceivesTc};
use spacepackets::{CcsdsPacket, SpHeader};
pub trait ApidHandler {
fn get_apid_handler(&self, apid: u16) -> Box<dyn ReceivesCcsds>;
}
struct CcsdsDistributor {
apid_handlers: Box<dyn ApidHandler>,
}
impl ReceivesTc for CcsdsDistributor {
fn pass_tc(&mut self, tm_raw: &[u8]) {
// TODO: Better error handling
let sp_header = SpHeader::from_raw_slice(tm_raw).unwrap();
let mut handler = self.apid_handlers.get_apid_handler(sp_header.apid());
handler.pass_ccsds(&sp_header, tm_raw).unwrap();
}
}

18
fsrc-core/src/tmtc/mod.rs Normal file
View File

@ -0,0 +1,18 @@
use spacepackets::ecss::PusError;
use spacepackets::tc::PusTc;
use spacepackets::{PacketError, SpHeader};
pub mod ccsds_distrib;
pub mod pus_distrib;
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 ReceivesPus {
fn pass_pus(&mut self, pus_tc: &PusTc) -> Result<(), PusError>;
}

View File

@ -0,0 +1,39 @@
use crate::tmtc::{ReceivesCcsds, ReceivesPus, ReceivesTc};
use spacepackets::ecss::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>;
}
pub struct PusDistributor {
service_provider: Box<dyn PusServiceProvider>,
}
impl ReceivesTc for PusDistributor {
fn pass_tc(&mut self, tm_raw: &[u8]) {
// Convert to ccsds and call pass_ccsds
let sp_header = SpHeader::from_raw_slice(tm_raw).unwrap();
self.pass_ccsds(&sp_header, tm_raw).unwrap();
}
}
impl ReceivesCcsds for PusDistributor {
fn pass_ccsds(&mut self, _header: &SpHeader, tm_raw: &[u8]) -> Result<(), PacketError> {
// TODO: Better error handling
let (tc, _) = PusTc::new_from_raw_slice(tm_raw).unwrap();
let mut srv_provider = self
.service_provider
.get_service_handler(tc.service(), tc.subservice());
let apid = self.service_provider.get_apid(tc.service());
if apid != tc.apid() {
// TODO: Dedicated error
return Ok(());
}
srv_provider.pass_pus(&tc).unwrap();
Ok(())
}
}