fsrc-core no_std now

This commit is contained in:
2022-08-20 23:21:36 +02:00
parent efb9f757ce
commit 1001700411
13 changed files with 145 additions and 106 deletions

View File

@ -1,6 +1,9 @@
//! [Event][crate::events::Event] management and forwarding
use crate::events::{Event, EventRaw, GroupId};
use std::collections::HashMap;
use alloc::boxed::Box;
use alloc::vec;
use alloc::vec::Vec;
use hashbrown::HashMap;
#[derive(PartialEq, Eq, Hash, Copy, Clone)]
enum ListenerType {
@ -62,11 +65,14 @@ impl<E> EventManager<E> {
key: ListenerType,
dest: impl EventListener<Error = E> + 'static,
) {
if let std::collections::hash_map::Entry::Vacant(e) = self.listeners.entry(key) {
e.insert(vec![Listener {
ltype: key,
dest: Box::new(dest),
}]);
if !self.listeners.contains_key(&key) {
self.listeners.insert(
key,
vec![Listener {
ltype: key,
dest: Box::new(dest),
}],
);
} else {
let vec = self.listeners.get_mut(&key).unwrap();
// To prevent double insertions
@ -117,6 +123,7 @@ mod tests {
use super::{EventListener, HandlerResult, ReceivesAllEvent};
use crate::event_man::EventManager;
use crate::events::{Event, Severity};
use alloc::boxed::Box;
use std::sync::mpsc::{channel, Receiver, SendError, Sender};
use std::thread;
use std::time::Duration;

View File

@ -1,5 +1,4 @@
//! Event support module
use num::pow;
pub type GroupId = u16;
pub type UniqueId = u16;
@ -47,7 +46,7 @@ impl Event {
/// * `unique_id`: Each event has a unique 16 bit ID occupying the last 16 bits of the
/// raw event ID
pub fn new(severity: Severity, group_id: GroupId, unique_id: UniqueId) -> Option<Event> {
if group_id > (pow::pow(2u8 as u16, 13) - 1) {
if group_id > (2u16.pow(13) - 1) {
return None;
}
Some(Event {

View File

@ -1,10 +1,13 @@
//! Task scheduling module
use bus::BusReader;
use std::boxed::Box;
use std::error::Error;
use std::sync::mpsc::TryRecvError;
use std::thread;
use std::thread::JoinHandle;
use std::time::Duration;
use std::vec;
use std::vec::Vec;
#[derive(Debug, PartialEq, Eq)]
pub enum OpResult {
@ -138,10 +141,13 @@ pub fn exec_sched_multi<
mod tests {
use super::{exec_sched_multi, exec_sched_single, Executable, ExecutionType, OpResult};
use bus::Bus;
use std::boxed::Box;
use std::error::Error;
use std::string::{String, ToString};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use std::{fmt, thread};
use std::vec::Vec;
use std::{fmt, thread, vec};
struct TestInfo {
exec_num: u32,

View File

@ -7,12 +7,21 @@
//! The crates can generally be used in a `no_std` environment, but some crates expect that the
//! [alloc](https://doc.rust-lang.org/alloc) crate is available to allow boxed trait objects.
//! These are used to supply user code to the crates.
#![no_std]
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(any(feature = "std", test))]
extern crate std;
pub mod error;
#[cfg(feature = "alloc")]
pub mod event_man;
pub mod events;
#[cfg(feature = "std")]
pub mod executable;
pub mod hal;
pub mod objects;
#[cfg(feature = "alloc")]
pub mod pool;
pub mod tmtc;

View File

@ -29,12 +29,12 @@
//! }
//!
//! impl SystemObject for ExampleSysObj {
//!
//! type Error = ();
//! fn get_object_id(&self) -> &ObjectId {
//! &self.id
//! }
//!
//! fn initialize(&mut self) -> Result<(), Box<dyn Error>> {
//! fn initialize(&mut self) -> Result<(), Self::Error> {
//! self.was_initialized = true;
//! Ok(())
//! }
@ -51,9 +51,12 @@
//! assert_eq!(example_obj.id, obj_id);
//! assert_eq!(example_obj.dummy, 42);
//! ```
use alloc::boxed::Box;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use downcast_rs::Downcast;
use std::collections::HashMap;
use hashbrown::HashMap;
#[cfg(feature = "std")]
use std::error::Error;
#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
@ -65,33 +68,37 @@ pub struct ObjectId {
/// Each object which is stored inside the [object manager][ObjectManager] needs to implemented
/// this trait
pub trait SystemObject: Downcast {
type Error;
fn get_object_id(&self) -> &ObjectId;
fn initialize(&mut self) -> Result<(), Box<dyn Error>>;
fn initialize(&mut self) -> Result<(), Self::Error>;
}
downcast_rs::impl_downcast!(SystemObject);
downcast_rs::impl_downcast!(SystemObject assoc Error);
pub trait ManagedSystemObject: SystemObject + Send {}
downcast_rs::impl_downcast!(ManagedSystemObject);
downcast_rs::impl_downcast!(ManagedSystemObject assoc Error);
/// Helper module to manage multiple [ManagedSystemObjects][ManagedSystemObject] by mapping them
/// using an [object ID][ObjectId]
pub struct ObjectManager {
obj_map: HashMap<ObjectId, Box<dyn ManagedSystemObject>>,
#[cfg(feature = "alloc")]
pub struct ObjectManager<E> {
obj_map: HashMap<ObjectId, Box<dyn ManagedSystemObject<Error = E>>>,
}
impl Default for ObjectManager {
#[cfg(feature = "alloc")]
impl<E: 'static> Default for ObjectManager<E> {
fn default() -> Self {
Self::new()
}
}
impl ObjectManager {
pub fn new() -> ObjectManager {
#[cfg(feature = "alloc")]
impl<E: 'static> ObjectManager<E> {
pub fn new() -> Self {
ObjectManager {
obj_map: HashMap::new(),
}
}
pub fn insert(&mut self, sys_obj: Box<dyn ManagedSystemObject>) -> bool {
pub fn insert(&mut self, sys_obj: Box<dyn ManagedSystemObject<Error = E>>) -> bool {
let obj_id = sys_obj.get_object_id();
if self.obj_map.contains_key(obj_id) {
return false;
@ -114,14 +121,14 @@ impl ObjectManager {
/// Retrieve a reference to an object stored inside the manager. The type to retrieve needs to
/// be explicitly passed as a generic parameter or specified on the left hand side of the
/// expression.
pub fn get_ref<T: ManagedSystemObject>(&self, key: &ObjectId) -> Option<&T> {
pub fn get_ref<T: ManagedSystemObject<Error = E>>(&self, key: &ObjectId) -> Option<&T> {
self.obj_map.get(key).and_then(|o| o.downcast_ref::<T>())
}
/// Retrieve a mutable reference to an object stored inside the manager. The type to retrieve
/// needs to be explicitly passed as a generic parameter or specified on the left hand side
/// of the expression.
pub fn get_mut<T: ManagedSystemObject>(&mut self, key: &ObjectId) -> Option<&mut T> {
pub fn get_mut<T: ManagedSystemObject<Error = E>>(&mut self, key: &ObjectId) -> Option<&mut T> {
self.obj_map
.get_mut(key)
.and_then(|o| o.downcast_mut::<T>())
@ -131,7 +138,8 @@ impl ObjectManager {
#[cfg(test)]
mod tests {
use crate::objects::{ManagedSystemObject, ObjectId, ObjectManager, SystemObject};
use std::error::Error;
use std::boxed::Box;
use std::string::String;
use std::sync::{Arc, Mutex};
use std::thread;
@ -152,11 +160,12 @@ mod tests {
}
impl SystemObject for ExampleSysObj {
type Error = ();
fn get_object_id(&self) -> &ObjectId {
&self.id
}
fn initialize(&mut self) -> Result<(), Box<dyn Error>> {
fn initialize(&mut self) -> Result<(), Self::Error> {
self.was_initialized = true;
Ok(())
}
@ -171,11 +180,12 @@ mod tests {
}
impl SystemObject for OtherExampleObject {
type Error = ();
fn get_object_id(&self) -> &ObjectId {
&self.id
}
fn initialize(&mut self) -> Result<(), Box<dyn Error>> {
fn initialize(&mut self) -> Result<(), Self::Error> {
self.was_initialized = true;
Ok(())
}

View File

@ -73,6 +73,10 @@
//! assert_eq!(buf_read_back[0], 7);
//! }
//! ```
use alloc::format;
use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
type NumBlocks = u16;
/// Configuration structure of the [local pool][LocalPool]
@ -215,7 +219,8 @@ impl LocalPool {
self.addr_check(&addr)?;
let block_size = self.pool_cfg.cfg.get(addr.pool_idx as usize).unwrap().1;
let raw_pos = self.raw_pos(&addr).unwrap();
let block = &mut self.pool.get_mut(addr.pool_idx as usize).unwrap()[raw_pos..raw_pos + block_size];
let block =
&mut self.pool.get_mut(addr.pool_idx as usize).unwrap()[raw_pos..raw_pos + block_size];
let size_list = self.sizes_lists.get_mut(addr.pool_idx as usize).unwrap();
size_list[addr.packet_idx as usize] = Self::STORE_FREE;
block.fill(0);
@ -228,7 +233,7 @@ impl LocalPool {
let size_list = self.sizes_lists.get(pool_idx).unwrap();
let curr_size = size_list[addr.packet_idx as usize];
if curr_size == Self::STORE_FREE {
return Ok(false)
return Ok(false);
}
Ok(true)
}
@ -327,7 +332,7 @@ pub struct PoolGuard<'a> {
pool: &'a mut LocalPool,
pub addr: StoreAddr,
no_deletion: bool,
deletion_failed_error: Option<StoreError>
deletion_failed_error: Option<StoreError>,
}
impl<'a> PoolGuard<'a> {
@ -336,7 +341,7 @@ impl<'a> PoolGuard<'a> {
pool,
addr,
no_deletion: false,
deletion_failed_error: None
deletion_failed_error: None,
}
}
@ -352,9 +357,8 @@ impl<'a> PoolGuard<'a> {
impl Drop for PoolGuard<'_> {
fn drop(&mut self) {
if !self.no_deletion {
let res = self.pool.delete(self.addr);
if res.is_err() {
self.deletion_failed_error = Some(res.unwrap_err());
if let Err(e) = self.pool.delete(self.addr) {
self.deletion_failed_error = Some(e);
}
}
}
@ -363,6 +367,7 @@ impl Drop for PoolGuard<'_> {
#[cfg(test)]
mod tests {
use crate::pool::{LocalPool, PoolCfg, StoreAddr, StoreError, StoreIdError};
use alloc::vec;
#[test]
fn test_cfg() {

View File

@ -85,6 +85,7 @@
//! mutable_ref.mutable_foo();
//! ```
use crate::tmtc::{ReceivesCcsdsTc, ReceivesTc};
use alloc::boxed::Box;
use downcast_rs::Downcast;
use spacepackets::{CcsdsPacket, PacketError, SpHeader};
@ -187,6 +188,7 @@ pub(crate) mod tests {
use spacepackets::CcsdsPacket;
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use std::vec::Vec;
pub fn generate_ping_tc(buf: &mut [u8]) -> &[u8] {
let mut sph = SpHeader::tc(0x002, 0x34, 0).unwrap();

View File

@ -9,7 +9,9 @@ use crate::error::{FsrcErrorRaw, FsrcGroupIds};
use spacepackets::tc::PusTc;
use spacepackets::SpHeader;
#[cfg(feature = "alloc")]
pub mod ccsds_distrib;
#[cfg(feature = "alloc")]
pub mod pus_distrib;
const _RAW_PACKET_ERROR: &str = "raw-tmtc";

View File

@ -61,6 +61,7 @@
//! assert_eq!(concrete_handler_ref.handler_call_count, 1);
//! ```
use crate::tmtc::{ReceivesCcsdsTc, ReceivesEcssPusTc, ReceivesTc};
use alloc::boxed::Box;
use downcast_rs::Downcast;
use spacepackets::ecss::{PusError, PusPacket};
use spacepackets::tc::PusTc;
@ -138,10 +139,13 @@ mod tests {
generate_ping_tc, BasicApidHandlerOwnedQueue, BasicApidHandlerSharedQueue,
};
use crate::tmtc::ccsds_distrib::{ApidPacketHandler, CcsdsDistributor};
use alloc::vec::Vec;
use spacepackets::ecss::PusError;
use spacepackets::tc::PusTc;
use spacepackets::CcsdsPacket;
#[cfg(feature = "std")]
use std::collections::VecDeque;
#[cfg(feature = "std")]
use std::sync::{Arc, Mutex};
struct PusHandlerSharedQueue {
@ -244,6 +248,7 @@ mod tests {
}
#[test]
#[cfg(feature = "std")]
fn test_pus_distribution() {
let known_packet_queue = Arc::new(Mutex::default());
let unknown_packet_queue = Arc::new(Mutex::default());