From 3549c73d13dba858e1013a86ed232e0c411d76ec Mon Sep 17 00:00:00 2001 From: Jack Rubacha Date: Sun, 11 Aug 2024 20:07:52 -0400 Subject: [PATCH] Add Command Line Args, automatic dependency updates (#45) * add cmdline parser * add dependabot --- .github/dependabot.yml | 6 +++ Cargo.lock | 120 +++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + README.md | 3 +- src/main.rs | 64 +++++++++++----------- 5 files changed, 162 insertions(+), 32 deletions(-) create mode 100644 .github/dependabot.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..791a4ef --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: "cargo" + directory: "/" + schedule: + interval: "weekly" \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index ec8ed37..1278e00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,55 @@ dependencies = [ "memchr", ] +[[package]] +name = "anstream" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" + +[[package]] +name = "anstyle-parse" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "anyhow" version = "1.0.79" @@ -69,6 +118,7 @@ name = "calypso" version = "0.1.0" dependencies = [ "bitstream-io", + "clap", "paho-mqtt", "protobuf", "protobuf-codegen", @@ -90,6 +140,46 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11d8838454fda655dafd3accb2b6e2bea645b9e4078abe84a22ceb947235c5cc" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "216aec2b177652e3846684cbfe25c9964d18ec45234f0f5da5157b207ed1aab6" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.39", +] + +[[package]] +name = "clap_lex" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" + [[package]] name = "cmake" version = "0.1.50" @@ -99,6 +189,12 @@ dependencies = [ "cc", ] +[[package]] +name = "colorchoice" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" + [[package]] name = "concurrent-queue" version = "2.3.0" @@ -265,6 +361,12 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hex" version = "0.4.3" @@ -290,6 +392,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "itertools" version = "0.10.5" @@ -606,6 +714,12 @@ dependencies = [ "thiserror", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "syn" version = "1.0.109" @@ -667,6 +781,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "vcpkg" version = "0.2.15" diff --git a/Cargo.toml b/Cargo.toml index c40490f..67e0770 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ paho-mqtt = "0.12.5" protobuf-codegen = "3.3.0" protobuf = "3.3.0" bitstream-io = "2.5.0" +clap = { version = "4.5.15", features = ["derive", "env"] } [build-dependencies] diff --git a/README.md b/README.md index 22756b6..56a75f8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Calypso Custom CAN decoder to translate CAN messages to MQTT protobuf encoded packets with low latency and a YAML configuration structure. +Usage: run `-h` to see the full usage options and defaults. ### Develop setup #### Go to Settings in VSCode @@ -30,7 +31,7 @@ Process for testing: - `sudo ip link add dev vcan0 type vcan` - `sudo ip link set dev vcan0 up` -run ```cargo run localhost:1883 vcan0``` +run ```cargo run -- -u localhost:1883 -c vcan0``` To send a can message: - `cansend vcan0 #` diff --git a/src/main.rs b/src/main.rs index b4e3f1c..ff8d2a2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ use std::{ collections::HashMap, - env, process, sync::{Arc, RwLock}, thread::{self, JoinHandle}, time::Duration, @@ -11,11 +10,39 @@ use calypso::{ encodable_message::EncodableMessage, encode_master_mapping::ENCODABLE_KEY_LIST, mqtt::MqttClient, serverdata, }; +use clap::Parser; use protobuf::Message; use socketcan::{CanFrame, CanSocket, EmbeddedFrame, Id, Socket}; const ENCODER_MAP_SUB: &str = "Calypso/Bidir/Command/#"; +/// Calypso command line arguments +#[derive(Parser, Debug)] +#[command(version)] +struct CalypsoArgs { + /// Whether to enable CAN message encoding + #[arg(short = 'e', long, env = "CALYPSO_CAN_ENCODE")] + encode: bool, + + /// The host url of the siren, including port and excluding protocol prefix + #[arg( + short = 'u', + long, + env = "CALYPSO_SIREN_HOST_URL", + default_value = "localhost:1883" + )] + siren_host_url: String, + + /// The SocketCAN interface port + #[arg( + short = 'c', + long, + env = "CALYPSO_SOCKETCAN_IFACE", + default_value = "vcan0" + )] + socketcan_iface: String, +} + /** * Reads the can socket and publishes the data to the given client. */ @@ -173,48 +200,23 @@ fn send_out( }) } -/** - * Parses the command line arguments. - */ -fn parse_args() -> (String, String, bool) { - let args: Vec = env::args().collect(); - println!("{:?}", args); - if args.len() < 3 { - println!("Not enough arguments!"); - println!("Siren (MQTT) URL and can interface are required"); - process::exit(1); - } - let path = &args[1]; - let can_interface = &args[2]; - let encode_en = args.len() >= 4 && args[3] == "encode_en"; - - println!("Siren URL: {}", path); - println!("Can interface: {}", can_interface); - - (String::from(path), String::from(can_interface), encode_en) -} - /** * Main Function * Configures the can network, retrieves the client based on the command line arguments, * connects the client and then reads the can socket from specified interface. * - * Args: - * 1 -> IP address:port of Siren mqtt server (ex. localhost:1883) - * 2 -> Socketcan interface name (ex. vcan0) - * 3 -> encode_en or blank, whether to enable encoding */ fn main() { - let (path, can_interface, encoding) = parse_args(); - let can_handle = read_can(&path, &can_interface); + let cli = CalypsoArgs::parse(); + let can_handle = read_can(&cli.siren_host_url, &cli.socketcan_iface); // use a arc for mutlithread, and a rwlock to enforce one writer - if encoding { + if cli.encode { let send_map: Arc>> = Arc::new(RwLock::new(HashMap::new())); - let siren_handle = read_siren(&path, Arc::clone(&send_map)); + let siren_handle = read_siren(&cli.siren_host_url, Arc::clone(&send_map)); - let send_handle = send_out(&can_interface, Arc::clone(&send_map)); + let send_handle = send_out(&cli.socketcan_iface, Arc::clone(&send_map)); siren_handle.join().expect("Encoder failed with "); println!("Encoder ended");