From 736a0bc093d9dba3fecafb2eac1bb70d5124abbb Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Wed, 7 Feb 2024 22:08:19 +0100 Subject: [PATCH] Add initial messagepack support --- Cargo.lock | 83 ++++++++++++++++++++++++++++++++++++++++++++---- Cargo.toml | 2 ++ src/main.rs | 91 ++++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 155 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a73a38a..1bd719f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -80,6 +80,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "bitflags" version = "1.3.2" @@ -92,6 +98,12 @@ version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "487f1e0fcbe47deb8b0574e646def1c903389d95241dd1bbcc6ce4a715dfc0c1" +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "cfg-if" version = "1.0.0" @@ -155,6 +167,8 @@ name = "heliox-cli" version = "0.1.0" dependencies = [ "clap", + "rmp-serde", + "serde", "serialport", ] @@ -219,6 +233,21 @@ dependencies = [ "libc", ] +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "paste" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" + [[package]] name = "pkg-config" version = "0.3.27" @@ -227,18 +256,18 @@ checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" [[package]] name = "proc-macro2" -version = "1.0.69" +version = "1.0.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.33" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -272,12 +301,54 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3cbb081b9784b07cceb8824c8583f86db4814d172ab043f3c23f7dc600bf83d" +[[package]] +name = "rmp" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f9860a6cc38ed1da53456442089b4dfa35e7cedaa326df63017af88385e6b20" +dependencies = [ + "byteorder", + "num-traits", + "paste", +] + +[[package]] +name = "rmp-serde" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bffea85eea980d8a74453e5d02a8d93028f3c34725de143085a844ebe953258a" +dependencies = [ + "byteorder", + "rmp", + "serde", +] + [[package]] name = "scopeguard" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "serde" +version = "1.0.196" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "870026e60fa08c69f064aa766c10f10b1d62db9ccd4d0abb206472bee0ce3b32" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.196" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33c85360c95e7d137454dc81d9a4ed2b8efd8fbe19cee57357b32b9771fccb67" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "serialport" version = "4.2.2" @@ -304,9 +375,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "2.0.38" +version = "2.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 4750e48..4a1fdf8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,6 @@ edition = "2021" [dependencies] clap = { version = "4.4.7", features = ["derive"] } +rmp-serde = "1.1.2" +serde = {version ="1.0.188", features = ["derive"]} serialport = "4.2.2" diff --git a/src/main.rs b/src/main.rs index a7b0798..cf282db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,60 @@ use clap::Parser; -use std::time::Duration; +use std::{process::exit, time::Duration}; +use serde::{Deserialize, Serialize}; +use rmp_serde::{Deserializer, Serializer}; + +const NUM_LIGHTS: usize = 4; +type LightIndex = u8; +type LightStatus = u8; #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] struct Args { mode: String, #[arg(short, long)] + index: Option, + #[arg(short, long)] port: Option, } +#[derive(Debug, PartialEq, Deserialize, Serialize)] +enum MessageType{ + Info, + Warning, + Error, + Success, +} + +#[derive(Debug, PartialEq, Deserialize, Serialize)] +struct StatusMessagePayload{ + kind: MessageType, + content: Option, +} + +#[derive(Debug, PartialEq, Deserialize, Serialize)] +struct CommandPayload{ + content: String, +} + + +#[derive(Debug, PartialEq, Deserialize, Serialize)] +enum LightControlCommand{ + Toggle(LightIndex), + On, + Off, + Increase(LightIndex), + Decrease(LightIndex), + Set((LightIndex,LightStatus)), +} + +#[derive(Debug, PartialEq, Deserialize, Serialize)] +enum Packet{ + StatusMessage(StatusMessagePayload), + Command(CommandPayload), + LightControl(LightControlCommand), + LightStatus([LightStatus;NUM_LIGHTS]), +} + fn main() { let args = Args::parse(); let default_address = "/dev/heliox".to_string(); @@ -18,29 +64,44 @@ fn main() { .open() .expect("Failed to open port"); - let on_cmd = "HX\x02\x02\x00".as_bytes(); - let off_cmd = "HX\x02\x02\x01".as_bytes(); - let son1_cmd = "HX\x03\x02\x02\x01".as_bytes(); - let son2_cmd = "HX\x03\x02\x02\x00".as_bytes(); + let packet:Option; + let mut serialized_packet = Vec::new(); match args.mode.as_str() { "on" => { - println!("Turning on..."); - port.write(on_cmd).expect("Write failed!"); + packet = Option::Some(Packet::LightControl(LightControlCommand::On)); } "off" => { - println!("Turning off..."); - port.write(off_cmd).expect("Write failed!"); + packet = Option::Some(Packet::LightControl(LightControlCommand::Off)); } - "son" => { - println!("Turning semi-on..."); - port.write(on_cmd).expect("Write failed!"); - port.write(son1_cmd).expect("Write failed!"); - port.write(son2_cmd).expect("Write failed!"); + "toggle" => { + packet = match args.index{ + Some(index)=>Option::Some(Packet::LightControl(LightControlCommand::Toggle(index))), + None=>{ + eprintln!("You must supply an index to toggle a specific light!"); + exit(1); + } + } } _ => { eprintln!("Invalid mode value"); - std::process::exit(1); + exit(1); } + } + + match packet{ + Option::Some(packet)=>{ + packet.serialize(&mut Serializer::new(&mut serialized_packet)).unwrap(); + port.write_all(&serialized_packet).expect("Serial port write failed!"); + + println!("{:?}",serialized_packet); + let mut in_buf = serialized_packet.as_slice(); + let d2: Packet = Deserialize::deserialize(&mut Deserializer::new(&mut in_buf)).unwrap(); + println!("{:?}",d2); + }, + Option::None=>{ + eprintln!("No valid packet was specified, so nothing was serialized!"); + exit(1); + }, } }