add readmes and cmd-cli

This commit is contained in:
Sebastian Hugentobler 2024-11-18 07:57:13 +01:00
parent 17a8e573c0
commit 6b7a10f532
Signed by: shu
GPG key ID: BB32CF3CA052C2F0
11 changed files with 502 additions and 5 deletions

11
cmd-cli/Cargo.toml Normal file
View file

@ -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 }

22
cmd-cli/README.md Normal file
View file

@ -0,0 +1,22 @@
# Cmd-Cli
Send COBS encoded commands across a serial connection.
```
Usage: cmd [OPTIONS] --port <PORT> <COMMAND>
Commands:
set-config Set a key-value configuration
help Print this message or the help of the given subcommand(s)
Options:
-p, --port <PORT> Serial port to connect to
-b, --baud <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
```

39
cmd-cli/src/cli.rs Normal file
View file

@ -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,
}

25
cmd-cli/src/main.rs Normal file
View file

@ -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);
});
}
}
}