From 6546a7cf1cae775919c2b21d971e13a1fdf71300 Mon Sep 17 00:00:00 2001 From: Florian Guggi Date: Fri, 3 May 2024 17:20:38 +0200 Subject: [PATCH] Add cli tool to interact run and interact with the scheduler --- .github/workflows/rust.yml | 6 +- Cargo.lock | 292 +++++++++++++++++++++++++++++-- Cargo.toml | 1 + examples/cli.rs | 207 ++++++++++++++++++++++ src/command/execution_context.rs | 11 +- src/communication/cep.rs | 20 +-- src/communication/mod.rs | 27 ++- src/main.rs | 9 +- tests/simulation/timeout.rs | 4 +- 9 files changed, 531 insertions(+), 46 deletions(-) create mode 100644 examples/cli.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 92052f0..d6897a8 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -29,13 +29,15 @@ jobs: run: cargo install cross --git https://github.com/cross-rs/cross - name: Build run: | - cross build --release --target aarch64-unknown-linux-gnu + cross build --examples --bins --release --target aarch64-unknown-linux-gnu mv target/aarch64-unknown-linux-gnu/release/STS1_EDU_Scheduler . + mv target/aarch64-unknown-linux-gnu/release/examples/cli . - name: Upload artifact - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: aarch64 package path: | STS1_EDU_Scheduler config.toml scheduler.service + cli diff --git a/Cargo.lock b/Cargo.lock index 7ba6a1b..f5ef9a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,6 +9,7 @@ dependencies = [ "crc", "file-per-thread-logger", "filevec", + "inquire", "log", "rppal", "serde", @@ -81,6 +82,31 @@ version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5" +[[package]] +name = "crossterm" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e64e6c0fbe2c17357405f7c758c1ef960fce08bdfb2c03d88d2a18d7e09c4b67" +dependencies = [ + "bitflags 1.3.2", + "crossterm_winapi", + "libc", + "mio", + "parking_lot", + "signal-hook", + "signal-hook-mio", + "winapi", +] + +[[package]] +name = "crossterm_winapi" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b" +dependencies = [ + "winapi", +] + [[package]] name = "deranged" version = "0.3.11" @@ -90,6 +116,12 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "dyn-clone" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125" + [[package]] name = "env_logger" version = "0.10.1" @@ -116,7 +148,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -137,8 +169,8 @@ checksum = "1ee447700ac8aa0b2f2bd7bc4462ad686ba06baa6727ac149a2d6277f0d240fd" dependencies = [ "cfg-if", "libc", - "redox_syscall", - "windows-sys", + "redox_syscall 0.4.1", + "windows-sys 0.52.0", ] [[package]] @@ -149,6 +181,24 @@ dependencies = [ "serde", ] +[[package]] +name = "fuzzy-matcher" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54614a3312934d066701a80f20f15fa3b56d67ac7722b39eea5b4c9dd1d66c94" +dependencies = [ + "thread_local", +] + +[[package]] +name = "fxhash" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" +dependencies = [ + "byteorder", +] + [[package]] name = "hashbrown" version = "0.14.3" @@ -183,6 +233,23 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "inquire" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fddf93031af70e75410a2511ec04d49e758ed2f26dad3404a934e0fb45cc12a" +dependencies = [ + "bitflags 2.4.2", + "crossterm", + "dyn-clone", + "fuzzy-matcher", + "fxhash", + "newline-converter", + "once_cell", + "unicode-segmentation", + "unicode-width", +] + [[package]] name = "io-kit-sys" version = "0.4.0" @@ -201,7 +268,7 @@ checksum = "0bad00257d07be169d870ab665980b06cdb366d792ad690bf2e76876dc503455" dependencies = [ "hermit-abi", "rustix", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -242,6 +309,16 @@ version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.20" @@ -263,6 +340,27 @@ version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" +[[package]] +name = "mio" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c" +dependencies = [ + "libc", + "log", + "wasi", + "windows-sys 0.48.0", +] + +[[package]] +name = "newline-converter" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b6b097ecb1cbfed438542d16e84fd7ad9b0c76c8a65b7f9039212a3d14dc7f" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "nix" version = "0.26.4" @@ -292,6 +390,35 @@ dependencies = [ "libc", ] +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "parking_lot" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e4af0ca4f6caed20e900d564c242b8e5d4903fdacf31d3daf527b66fe6f42fb" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall 0.5.1", + "smallvec", + "windows-targets 0.52.0", +] + [[package]] name = "paste" version = "1.0.14" @@ -337,6 +464,15 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "redox_syscall" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "469052894dcb553421e483e4209ee581a45100d31b4018de03e5a7ad86374a7e" +dependencies = [ + "bitflags 2.4.2", +] + [[package]] name = "regex" version = "1.10.2" @@ -407,7 +543,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -470,6 +606,36 @@ dependencies = [ "winapi", ] +[[package]] +name = "signal-hook" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-mio" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af" +dependencies = [ + "libc", + "mio", + "signal-hook", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + [[package]] name = "simplelog" version = "0.12.1" @@ -481,6 +647,12 @@ dependencies = [ "time", ] +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + [[package]] name = "strum" version = "0.25.0" @@ -597,6 +769,16 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "time" version = "0.3.31" @@ -677,6 +859,24 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unicode-segmentation" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" + +[[package]] +name = "unicode-width" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68f5e5f3158ecfd4b8ff6fe086db7c8467a2dfdac97fe420f2b7c4aa97af66d6" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "winapi" version = "0.3.9" @@ -708,13 +908,37 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets", + "windows-targets 0.52.0", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] @@ -723,51 +947,93 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.52.0", + "windows_aarch64_msvc 0.52.0", + "windows_i686_gnu 0.52.0", + "windows_i686_msvc 0.52.0", + "windows_x86_64_gnu 0.52.0", + "windows_x86_64_gnullvm 0.52.0", + "windows_x86_64_msvc 0.52.0", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[package]] name = "windows_aarch64_msvc" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + [[package]] name = "windows_i686_gnu" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[package]] name = "windows_i686_msvc" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + [[package]] name = "windows_x86_64_gnu" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "windows_x86_64_msvc" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index 00e44ea..5088d4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,3 +28,4 @@ rpi = [] [dev-dependencies] file-per-thread-logger = "*" +inquire = "0.7.5" diff --git a/examples/cli.rs b/examples/cli.rs new file mode 100644 index 0000000..9523cf6 --- /dev/null +++ b/examples/cli.rs @@ -0,0 +1,207 @@ +use std::{ + error::Error, io::{Read, Write}, path::Path, process::{Child, ChildStdin, ChildStdout, Stdio}, time::Duration +}; + +use STS1_EDU_Scheduler::communication::{CEPPacket, CommunicationHandle}; + +fn main() { + let scheduler_path = + std::env::args().nth(1).expect("Pass in the directory containing the scheduler binary"); + + let mut serial = SocatSerialPort::new(&format!("{scheduler_path}/virtualserial")); + write_scheduler_config(&scheduler_path); + let _scheduler = PoisonedChild( + std::process::Command::new(format!("{scheduler_path}/STS1_EDU_Scheduler")) + .current_dir(&scheduler_path) + .spawn() + .unwrap(), + ); + + loop { + inquire_and_send_command(&mut serial, &scheduler_path).unwrap(); + println!("------------------------"); + } +} + +pub struct SocatSerialPort { + child: Child, + stdout: T, + stdin: U, +} + +impl SocatSerialPort { + fn new(path: &str) -> Self { + let mut child = std::process::Command::new("socat") + .arg("stdio") + .arg(format!("pty,raw,echo=0,link={},b921600,wait-slave", path)) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .unwrap(); + + loop { + if std::path::Path::new(path).exists() { + break; + } + std::thread::sleep(Duration::from_millis(50)); + } + + let stdout = child.stdout.take().unwrap(); + let stdin = child.stdin.take().unwrap(); + Self { child, stdout, stdin } + } +} + +fn write_scheduler_config(path: &str) { + std::fs::write( + format!("{path}/config.toml"), + " + uart = \"virtualserial\" + baudrate = 921600 + heartbeat_pin = 34 + update_pin = 35 + heartbeat_freq = 10 + log_path = \"log\" + ", + ) + .unwrap(); +} + +const COMMANDS: &[&str] = + &["StoreArchive", "ExecuteProgram", "StopProgram", "GetStatus", "ReturnResult", "UpdateTime"]; + +fn inquire_and_send_command(edu: &mut impl CommunicationHandle, path: &str) -> Result<(), Box> { + let mut select = inquire::Select::new("Select command", COMMANDS.to_vec()); + if Path::new(&format!("{path}/updatepin")).exists() { + select.help_message = Some("Update Pin is high"); + } + let command = select.prompt()?; + + match command { + "StoreArchive" => { + let archive = inquire::Text::new("Path to zipfile:").prompt()?; + let program_id = inquire::Text::new("Program id (must be numerical):").prompt()?; + let archive = std::fs::read(archive)?; + + edu.send_packet(&CEPPacket::Data(store_archive(program_id.parse()?)))?; + edu.send_multi_packet(&archive)?; + println!("Received {:?}", edu.receive_packet()?); + } + "ExecuteProgram" => { + let program_id = inquire::Text::new("Program id:").prompt()?.parse()?; + let timestamp = inquire::Text::new("Timestamp:").prompt()?.parse()?; + let timeout = + inquire::Text::new("Timeout (in seconds):").with_default("1").prompt()?.parse()?; + + edu.send_packet(&CEPPacket::Data(execute_program(program_id, timestamp, timeout)))?; + println!("Received {:?}", edu.receive_packet()?); + } + "StopProgram" => { + edu.send_packet(&CEPPacket::Data(stop_program()))?; + println!("Received {:?}", edu.receive_packet()?); + } + "GetStatus" => { + edu.send_packet(&CEPPacket::Data(get_status()))?; + if let CEPPacket::Data(status) = edu.receive_packet()? { + match status[0] { + 0 => println!("No Event"), + 1 => println!( + "Program Finished with ID: {} Timestamp: {} Exit Code: {}", + u16::from_le_bytes(status[1..3].try_into()?), + u32::from_le_bytes(status[3..7].try_into()?), + status[7] + ), + 2 => println!( + "Result ready for ID: {} Timestamp: {}", + u16::from_le_bytes(status[1..3].try_into()?), + u32::from_le_bytes(status[3..7].try_into()?) + ), + 3 => println!("Enable dosimeter"), + 4 => println!("Disable dosimeter"), + n => println!("Unknown event {n}"), + } + } + }, + "ReturnResult" => { + let program_id = inquire::Text::new("Program id:").prompt()?.parse()?; + let timestamp = inquire::Text::new("Timestamp:").prompt()?.parse()?; + let result_path = inquire::Text::new("File path for returned result:").with_default("./result.tar").prompt()?; + edu.send_packet(&CEPPacket::Data(return_result(program_id, timestamp)))?; + match edu.receive_multi_packet() { + Ok(data) => { + std::fs::write(result_path, data)?; + println!("Wrote result to file"); + }, + Err(e) => println!("Received {:?}", e), + } + } + _ => (), + } + + Ok(()) +} + +impl Read for SocatSerialPort { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + self.stdout.read(buf) + } +} + +impl Write for SocatSerialPort { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + self.stdin.write(buf) + } + + fn flush(&mut self) -> std::io::Result<()> { + self.stdin.flush() + } +} + +impl Drop for SocatSerialPort { + fn drop(&mut self) { + self.child.kill().unwrap(); + } +} + +impl CommunicationHandle for SocatSerialPort { + const INTEGRITY_ACK_TIMEOUT: Duration = Duration::MAX; + const UNLIMITED_TIMEOUT: Duration = Duration::MAX; + + fn set_timeout(&mut self, _timeout: Duration) {} +} + +struct PoisonedChild(pub Child); +impl Drop for PoisonedChild { + fn drop(&mut self) { + self.0.kill().unwrap(); + } +} + +pub fn store_archive(program_id: u16) -> Vec { + let mut vec = vec![1u8]; + vec.extend(program_id.to_le_bytes()); + vec +} + +pub fn execute_program(program_id: u16, timestamp: u32, timeout: u16) -> Vec { + let mut vec = vec![2u8]; + vec.extend(program_id.to_le_bytes()); + vec.extend(timestamp.to_le_bytes()); + vec.extend(timeout.to_le_bytes()); + vec +} + +pub fn stop_program() -> Vec { + vec![3u8] +} + +pub fn get_status() -> Vec { + vec![4u8] +} + +pub fn return_result(program_id: u16, timestamp: u32) -> Vec { + let mut vec = vec![5u8]; + vec.extend(program_id.to_le_bytes()); + vec.extend(timestamp.to_le_bytes()); + vec +} diff --git a/src/command/execution_context.rs b/src/command/execution_context.rs index a2e6197..2a0e920 100644 --- a/src/command/execution_context.rs +++ b/src/command/execution_context.rs @@ -82,23 +82,20 @@ impl UpdatePin { /// This impl is only used when doing tests without hardware #[cfg(feature = "mock")] -pub struct UpdatePin { - pub pin: bool, -} +pub struct UpdatePin; #[cfg(feature = "mock")] impl UpdatePin { pub fn new(_pin: u8) -> Self { - let update_pin = UpdatePin { pin: false }; - return update_pin; + Self } pub fn set_high(&mut self) { - self.pin = true + let _ = std::fs::write("updatepin", b""); } pub fn set_low(&mut self) { - self.pin = false + let _ = std::fs::remove_file("updatepin"); } } diff --git a/src/communication/cep.rs b/src/communication/cep.rs index 5b7ce63..c4ae615 100644 --- a/src/communication/cep.rs +++ b/src/communication/cep.rs @@ -157,21 +157,17 @@ mod tests { #[test] fn invalid_crc_is_rejected() { - assert!( - matches!( - CEPPacket::try_from(vec![0x8B, 4, 0, 0x0a, 0x0b, 0x05, 0x74, 0x52, 0x27, 0x92, 0xf4]), - Err(CEPParseError::InvalidCRC) - ) - ) + assert!(matches!( + CEPPacket::try_from(vec![0x8B, 4, 0, 0x0a, 0x0b, 0x05, 0x74, 0x52, 0x27, 0x92, 0xf4]), + Err(CEPParseError::InvalidCRC) + )) } #[test] fn invalid_length_is_rejected() { - assert!( - matches!( - CEPPacket::try_from(vec![0x8B, 0xff, 0xff]), - Err(CEPParseError::InvalidLength) - ) - ) + assert!(matches!( + CEPPacket::try_from(vec![0x8B, 0xff, 0xff]), + Err(CEPParseError::InvalidLength) + )) } } diff --git a/src/communication/mod.rs b/src/communication/mod.rs index 06ae1a0..944d81a 100644 --- a/src/communication/mod.rs +++ b/src/communication/mod.rs @@ -34,8 +34,8 @@ pub trait CommunicationHandle: Read + Write { if i < Self::DATA_PACKET_RETRIES { self.write_all(&bytes)?; } - }, - Err(e) => return Err(e) + } + Err(e) => return Err(e), } } @@ -74,7 +74,10 @@ pub trait CommunicationHandle: Read + Write { } } - log::error!("Could not receive data packet after {} retries, giving up", Self::DATA_PACKET_RETRIES); + log::error!( + "Could not receive data packet after {} retries, giving up", + Self::DATA_PACKET_RETRIES + ); Err(CommunicationError::PacketInvalidError) } @@ -271,23 +274,33 @@ mod tests { #[test] fn fail_after_retries_receive_packet() { let mut com = TestComHandle::default(); - com.data_to_read.extend([CEPPacketHeader::Data as u8, 1, 0, 2, 1, 1, 1, 1].repeat(TestComHandle::DATA_PACKET_RETRIES)); + com.data_to_read.extend( + [CEPPacketHeader::Data as u8, 1, 0, 2, 1, 1, 1, 1] + .repeat(TestComHandle::DATA_PACKET_RETRIES), + ); let err = com.receive_packet().expect_err("Invalid data packet should fail"); assert!(matches!(err, CommunicationError::PacketInvalidError)); assert!(com.data_to_read.is_empty(), "Not read: {:?}", com.data_to_read); - assert_eq!(com.written_data, CEPPacket::Nack.serialize().repeat(TestComHandle::DATA_PACKET_RETRIES)); + assert_eq!( + com.written_data, + CEPPacket::Nack.serialize().repeat(TestComHandle::DATA_PACKET_RETRIES) + ); } #[test] fn receive_packet_retries_correctly() { let mut com = TestComHandle::default(); - com.data_to_read.extend([CEPPacketHeader::Data as u8, 1, 0, 2, 1, 1, 1, 1].repeat(TestComHandle::DATA_PACKET_RETRIES-1)); + com.data_to_read.extend( + [CEPPacketHeader::Data as u8, 1, 0, 2, 1, 1, 1, 1] + .repeat(TestComHandle::DATA_PACKET_RETRIES - 1), + ); com.data_to_read.append(&mut CEPPacket::Data(vec![2]).serialize()); assert_eq!(com.receive_packet().unwrap(), CEPPacket::Data(vec![2])); assert!(com.data_to_read.is_empty()); - let mut expected = CEPPacket::Nack.serialize().repeat(TestComHandle::DATA_PACKET_RETRIES-1); + let mut expected = + CEPPacket::Nack.serialize().repeat(TestComHandle::DATA_PACKET_RETRIES - 1); expected.append(&mut CEPPacket::Ack.serialize()); assert_eq!(com.written_data, expected); } diff --git a/src/main.rs b/src/main.rs index f0769d2..a61bb77 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,8 +20,10 @@ struct Configuration { } fn main() -> ! { - let config: Configuration = - toml::from_str(&std::fs::read_to_string("./config.toml").expect("Could not open config file")).unwrap(); + let config: Configuration = toml::from_str( + &std::fs::read_to_string("./config.toml").expect("Could not open config file"), + ) + .unwrap(); create_directory_if_not_exists("archives").unwrap(); create_directory_if_not_exists("data").unwrap(); @@ -36,7 +38,8 @@ fn main() -> ! { log::info!("Scheduler started"); // construct a wrapper for UART communication - let mut com = serialport::new(&config.uart, config.baudrate).open().expect("Could not open serial port"); + let mut com = + serialport::new(&config.uart, config.baudrate).open().expect("Could not open serial port"); com.set_timeout( as CommunicationHandle>::UNLIMITED_TIMEOUT); // construct a wrapper for resources that are shared between different commands diff --git a/tests/simulation/timeout.rs b/tests/simulation/timeout.rs index 1d32113..632962f 100644 --- a/tests/simulation/timeout.rs +++ b/tests/simulation/timeout.rs @@ -1,6 +1,6 @@ +use super::{get_status, start_scheduler, SimulationComHandle}; use std::time::Duration; -use STS1_EDU_Scheduler::communication::{CommunicationHandle, CEPPacket}; -use super::{SimulationComHandle, start_scheduler, get_status}; +use STS1_EDU_Scheduler::communication::{CEPPacket, CommunicationHandle}; #[test] fn integrity_ack_timeout_is_honored() {