From 6b7a10f532e22b40c4ca587eed92349d73763c14 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Mon, 18 Nov 2024 07:57:13 +0100 Subject: [PATCH] add readmes and cmd-cli --- Cargo.lock | 381 +++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 +- README.md | 17 ++ cmd-cli/Cargo.toml | 11 ++ cmd-cli/README.md | 22 +++ cmd-cli/src/cli.rs | 39 +++++ cmd-cli/src/main.rs | 25 +++ controller/README.md | 6 +- controller/src/main.rs | 1 + controller/src/wifi.rs | 2 +- flake.nix | 1 + 11 files changed, 502 insertions(+), 5 deletions(-) create mode 100644 README.md create mode 100644 cmd-cli/Cargo.toml create mode 100644 cmd-cli/README.md create mode 100644 cmd-cli/src/cli.rs create mode 100644 cmd-cli/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index ce5c413..f611848 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,10 +2,391 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" +dependencies = [ + "anstyle", + "windows-sys", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clap" +version = "4.5.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb3b4b9e5a7c7514dfa52869339ee98b3156b0bfb4e8a77c4ff4babb64b1604f" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b17a95aa67cc7b5ebd32aa5370189aa0d79069ef1c64ce893bd30fb24bff20ec" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afb84c814227b90d6895e01398aee0d8033c00e7466aca416fb6a8e0eb19d8a7" + +[[package]] +name = "cmd" +version = "0.1.0" +dependencies = [ + "clap", + "serial-comm", + "serialport", +] + +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + +[[package]] +name = "core-foundation" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b55271e5c8c478ad3f38ad24ef34923091e0548492a266d19b3c0b4d82574c63" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "io-kit-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "617ee6cf8e3f66f3b4ea67a4058564628cde41901316e19f559e14c7c72c5e7b" +dependencies = [ + "core-foundation-sys", + "mach2", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "libc" +version = "0.2.164" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f" + +[[package]] +name = "mach2" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b955cdeb2a02b9117f121ce63aa52d08ade45de53e48fe6a38b39c10f6f709" +dependencies = [ + "libc", +] + [[package]] name = "mqtt-protocol" version = "0.1.0" +[[package]] +name = "nix" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b" +dependencies = [ + "bitflags 1.3.2", + "cfg-if", + "libc", +] + +[[package]] +name = "proc-macro2" +version = "1.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "serial-comm" version = "0.1.0" + +[[package]] +name = "serialport" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7331eefcaafaa382c0df95bcd84068f0b3e3c215c300750dde2316e9b8806ed5" +dependencies = [ + "bitflags 2.6.0", + "cfg-if", + "core-foundation", + "core-foundation-sys", + "io-kit-sys", + "mach2", + "nix", + "scopeguard", + "unescaper", + "winapi", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unescaper" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c878a167baa8afd137494101a688ef8c67125089ff2249284bd2b5f9bfedb815" +dependencies = [ + "thiserror", +] + +[[package]] +name = "unicode-ident" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index a70ebac..e278f88 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["serial-comm", "mqtt-protocol"] +members = ["cmd-cli", "serial-comm", "mqtt-protocol"] resolver = "2" [workspace.package] diff --git a/README.md b/README.md new file mode 100644 index 0000000..d222627 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# rp2040 Temperatures + +A wip project to read temperatures on a rp2040 and publish readings to mqtt. + +## Projects + +- [cmd-cli](./cmd-cli/): Send COBS encoded commands across a serial connection. + Used to configure the microcontroller. +- [controller](./controller/): Code running on the rp2040. Because of + limitations in cargo, it is not part of the workspace. +- [mqtt-protocol](./mqtt-protocol/): Very simple implementation of just enough + of MQTT to publish data. There was no MQTT crate that compiles for the rp2040. +- [serial-comm](./serial-comm/): Implementation of a (too) simple serial + protocol, leveraging + [COBS](https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing). + +Take a look at the controller subproject to run stuff. diff --git a/cmd-cli/Cargo.toml b/cmd-cli/Cargo.toml new file mode 100644 index 0000000..ef3528d --- /dev/null +++ b/cmd-cli/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "cmd" +version.workspace = true +edition.workspace = true +description.workspace = true +license.workspace = true + +[dependencies] +clap = { version = "4.5.21", features = ["derive"] } +serial-comm = { path = "../serial-comm" } +serialport = { version = "4.6.0", default-features = false } diff --git a/cmd-cli/README.md b/cmd-cli/README.md new file mode 100644 index 0000000..25eb918 --- /dev/null +++ b/cmd-cli/README.md @@ -0,0 +1,22 @@ +# Cmd-Cli + +Send COBS encoded commands across a serial connection. + +``` +Usage: cmd [OPTIONS] --port + +Commands: + set-config Set a key-value configuration + help Print this message or the help of the given subcommand(s) + +Options: + -p, --port Serial port to connect to + -b, --baud Baud rate for serial connection [default: 9600] + -h, --help Print help +``` + +Example for running directly from the workspace: + +``` +cargo run --release -p cmd -- --port /dev/ttyACM1 -b 9600 set-config -k mqtt -v 5.196.78.28 +``` diff --git a/cmd-cli/src/cli.rs b/cmd-cli/src/cli.rs new file mode 100644 index 0000000..d26e566 --- /dev/null +++ b/cmd-cli/src/cli.rs @@ -0,0 +1,39 @@ +//! Cli interface. + +use clap::Parser; + +/// Send COBS encoded commands across a serial connection +#[derive(Parser)] +#[command(name = "cmd")] +#[command(bin_name = "cmd")] +pub struct Cli { + /// Serial port to connect to + #[arg(short, long)] + pub port: String, + + /// Baud rate for serial connection + #[arg(short, long, default_value = "9600")] + pub baud: u32, + + #[command(subcommand)] + pub command: Commands, +} + +#[derive(clap::Subcommand)] +pub enum Commands { + /// Set a key-value configuration + SetConfig(SetConfigArgs), +} + +/// Set a key-value configuration +#[derive(clap::Args)] +#[command(version, about, long_about = None)] +pub struct SetConfigArgs { + /// Configuration key + #[arg(short, long)] + pub key: String, + + /// Configuration value + #[arg(short, long)] + pub value: String, +} diff --git a/cmd-cli/src/main.rs b/cmd-cli/src/main.rs new file mode 100644 index 0000000..735c239 --- /dev/null +++ b/cmd-cli/src/main.rs @@ -0,0 +1,25 @@ +use clap::Parser; +use serial_comm::protocol::{cmd::Cobs, set_config::SetConfig}; + +mod cli; + +fn main() { + let args = cli::Cli::parse(); + + match args.command { + cli::Commands::SetConfig(cmd) => { + let set_config = SetConfig::new(&cmd.key, &cmd.value); + let (size, set_config): (usize, [u8; 64]) = set_config.as_cobs(); + + let builder = serialport::new(&args.port, args.baud); + let mut port = builder.open().unwrap_or_else(|e| { + eprintln!("Failed to open {}: {}", &args.port, e); + ::std::process::exit(1); + }); + port.write_all(&set_config[..size + 1]).unwrap_or_else(|e| { + eprintln!("Failed to write command: {}", e); + ::std::process::exit(1); + }); + } + } +} diff --git a/controller/README.md b/controller/README.md index 8abdb86..bbc35cb 100644 --- a/controller/README.md +++ b/controller/README.md @@ -3,8 +3,9 @@ Read temperature values and write them to an MQTT topic. The Raspberry Pi Pico is well supported by [embassy](https://embassy.dev), apart -from the bluetooth stack (for [legal]() reasons). As we do not use bluetooth for -communication we can ignore that. +from the bluetooth stack (_2024/11/18: +[might not be true anymore](https://github.com/embassy-rs/embassy/blob/main/examples/rp/src/bin/bluetooth.rs)_). +As we do not use bluetooth for communication we can ignore that. ## Flashing @@ -107,4 +108,3 @@ All configuration is read on startup of the microcontroller. ## Outlook - Pull the flash/config handling into separate library for testability -- Merge different ways of running tasks (join and tasks) diff --git a/controller/src/main.rs b/controller/src/main.rs index 348d6fc..5249780 100644 --- a/controller/src/main.rs +++ b/controller/src/main.rs @@ -13,6 +13,7 @@ use embassy_executor::Spawner; use embassy_net::Stack; use embassy_time::Timer; use flash::{Config, FlashError}; +use mqtt_protocol::publish::MAX_TOPIC_LENGTH; use static_cell::StaticCell; use temperature::{temperature_task, Temperature}; use usb_serial::{init_usb, usb_task}; diff --git a/controller/src/wifi.rs b/controller/src/wifi.rs index 7a34580..ca7249d 100644 --- a/controller/src/wifi.rs +++ b/controller/src/wifi.rs @@ -35,7 +35,7 @@ async fn net_task(stack: &'static Stack>) -> ! { static STATE: StaticCell = StaticCell::new(); static STACK: StaticCell>> = StaticCell::new(); -const STACK_RESOURCES_COUNT: usize = 2; +const STACK_RESOURCES_COUNT: usize = 5; static STACK_RESOURCES: StaticCell> = StaticCell::new(); // Initialise the wwifi chip of the RP2040 and return the control interface as well as the network diff --git a/flake.nix b/flake.nix index 2f5ce10..e1e8386 100644 --- a/flake.nix +++ b/flake.nix @@ -26,6 +26,7 @@ pkgs.cargo-deny pkgs.rust-analyzer pkgs.probe-rs + pkgs.mosquitto ]; in {