Skip to content

Commit

Permalink
Adds a CopperList holder + some tests checking if the serialization w…
Browse files Browse the repository at this point in the history
…ould work
  • Loading branch information
gbin committed Jun 30, 2024
1 parent c8b65bf commit 2852134
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
2 changes: 2 additions & 0 deletions copper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 10 additions & 3 deletions copper/src/copperlist.rs
Original file line number Diff line number Diff line change
@@ -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<P: Serialize> {
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)]
Expand Down
2 changes: 0 additions & 2 deletions copper_log_test/build.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
56 changes: 56 additions & 0 deletions copper_unifiedlog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: Encode> {
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(&section[..]);
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);
}
}

0 comments on commit 2852134

Please sign in to comment.