diff --git a/copper/Cargo.toml b/copper/Cargo.toml index d0dcdffe1..feab0c3b9 100644 --- a/copper/Cargo.toml +++ b/copper/Cargo.toml @@ -8,6 +8,8 @@ keywords = ["robotics", "middleware", "copper", "real-time"] categories = ["science::robotics"] [dependencies] +bincode = { version = "2.0.0-rc.3", features = ["serde"] } +bincode_derive = "2.0.0-rc.3" lazy_static = "1.4.0" zerocopy = "0.7.34" zerocopy-derive = "0.7.34" diff --git a/copper/src/copperlist.rs b/copper/src/copperlist.rs index 3bc9a280c..5c5e4e31a 100644 --- a/copper/src/copperlist.rs +++ b/copper/src/copperlist.rs @@ -1,24 +1,31 @@ extern crate alloc; +use bincode_derive::{Decode, Encode}; + use std::iter::{Chain, Rev}; use std::slice::{Iter as SliceIter, IterMut as SliceIterMut}; const MAX_TASKS: usize = 512; -#[derive(Debug)] +#[derive(Debug, Encode, Decode)] struct CopperLiskMask { #[allow(dead_code)] mask: [u128; MAX_TASKS / 128 + 1], } -#[derive(Debug)] -#[allow(dead_code)] +#[derive(Debug, Encode, Decode)] enum CopperListState { Free, ProcessingTasks(CopperLiskMask), BeingSerialized, } +#[derive(Debug, Encode, Decode)] +struct CopperList { + state: CopperListState, + payload: P, // This is generated from the runtime. +} + /// This structure maintains the entire memory needed by Copper for one loop for the inter tasks communication within a process. /// T is typically a Tuple of various types of messages that are exchanged between tasks. #[derive(Debug)] diff --git a/copper_log_test/build.rs b/copper_log_test/build.rs index a1c0c5fa6..e7a39b7af 100644 --- a/copper_log_test/build.rs +++ b/copper_log_test/build.rs @@ -1,7 +1,5 @@ use std::env; -const INDEX_DIR_NAME: &str = "copper_log_index"; - fn main() { let out_dir = env::var("OUT_DIR").expect("OUT_DIR is not defined"); println!("cargo:rustc-env=OUT_DIR={}", out_dir); diff --git a/copper_unifiedlog/src/lib.rs b/copper_unifiedlog/src/lib.rs index 2bcefd479..6683bfa4a 100644 --- a/copper_unifiedlog/src/lib.rs +++ b/copper_unifiedlog/src/lib.rs @@ -619,4 +619,60 @@ mod tests { assert_eq!(v2, 2); assert_eq!(v3, 3); } + + /// Mimic a basic CopperList implementation. + + #[derive(Debug, dEncode, dDecode)] + enum CopperListStateMock { + Free, + ProcessingTasks, + BeingSerialized, + } + + #[derive(dEncode, dDecode)] + struct CopperList { + state: CopperListStateMock, + payload: P, // This is generated from the runtime. + } + + #[test] + fn test_copperlist_list_like_logging() { + let tmp_dir = TempDir::new().expect("could not create a tmp dir"); + let (logger, f) = make_a_logger(&tmp_dir); + let p = f.as_path(); + println!("Path : {:?}", p); + { + let mut stream = stream_write(logger.clone(), UnifiedLogType::CopperList, 1024); + let cl0 = CopperList { + state: CopperListStateMock::Free, + payload: (1u32, 2u32, 3u32), + }; + let cl1 = CopperList { + state: CopperListStateMock::ProcessingTasks, + payload: (4u32, 5u32, 6u32), + }; + stream.log(&cl0).unwrap(); + stream.log(&cl1).unwrap(); + } + drop(logger); + + let UnifiedLogger::Read(mut dl) = UnifiedLoggerBuilder::new() + .file_path(&f.to_path_buf()) + .build() + .expect("Failed to build logger") + else { + panic!("Failed to build logger"); + }; + let section = dl + .read_next_section_type(UnifiedLogType::CopperList) + .expect("Failed to read section"); + assert!(section.is_some()); + let section = section.unwrap(); + + let mut reader = BufReader::new(§ion[..]); + let cl0: CopperList<(u32, u32, u32)> = decode_from_reader(&mut reader, standard()).unwrap(); + let cl1: CopperList<(u32, u32, u32)> = decode_from_reader(&mut reader, standard()).unwrap(); + assert_eq!(cl0.payload.1, 2); + assert_eq!(cl1.payload.2, 6); + } }