Files
innesco-rs/crates/innesco-cli/src/main.rs
T
BobBot 78a84ef248
ci / test (push) Canceled after 0s
ci / build-arm64 (push) Canceled after 0s
Initial Rust port of innesco: ECS protocol, frame decoder, CLI
- innesco-protocol: ECS wire protocol client (port of ecs/protocol.py)
- innesco-decoding: 13-byte frame decoder (port of csrc/decoding.c),
  golden tests mirror tests/test_decode_golden.py (14/14 green)
- innesco-cli: ping / decode subcommands
- scripts/build-arm64.sh: rootless cross build for AMD Kria K26
  (aarch64 via cargo-zigbuild + zig, musl static default)
- zero external dependencies (std only)
2026-11-12 07:06:19 +00:00

97 lines
3.6 KiB
Rust

//! `innesco-cli` — minimal CLI for the Rust clone of innesco.
//!
//! Subcommands:
//! * `ping <host> [port]` — open an ECS connection to data_server and ping it.
//! * `decode <file|->` — decode a raw 13-byte-aligned frame stream to TSV.
//! * `help` — usage.
use std::io::Read;
use std::process::ExitCode;
use std::time::Duration;
use innesco_protocol::{Connection, Request, DEFAULT_PORT};
use innesco_decoding::{Decoder, FRAME_SIZE_BYTES};
fn usage() -> String {
"usage: innesco-cli <command>\n\
\n\
commands:\n \
ping <host> [port] ping the IGNITE64 data_server (default port 8888)\n \
decode <file|-> decode raw 13-byte frames to TSV on stdout\n \
help show this message"
.to_string()
}
fn main() -> ExitCode {
let args: Vec<String> = std::env::args().skip(1).collect();
match run(&args) {
Ok(()) => ExitCode::SUCCESS,
Err(msg) => {
eprintln!("innesco-cli: {msg}");
ExitCode::FAILURE
}
}
}
fn run(args: &[String]) -> Result<(), String> {
match args.first().map(String::as_str) {
Some("ping") => {
let host = args.get(1).ok_or_else(|| usage())?;
let port = args
.get(2)
.map(|p| p.parse::<u16>().map_err(|_| format!("bad port: {p}")))
.transpose()?
.unwrap_or(DEFAULT_PORT);
let mut conn = Connection::connect(host, port, Duration::from_secs(5))
.map_err(|e| format!("connect {host}:{port} failed: {e}"))?;
let payload = b"innesco-cli ping".to_vec();
let resp = conn
.send(&Request::Ping { payload: payload.clone() })
.map_err(|e| format!("ping failed: {e}"))?;
if resp.is_error() {
return Err(format!("server error {}: {}", resp.status, resp.message()));
}
if resp.data == payload {
println!("pong from {host}:{port} ({}/{} bytes echoed)", resp.data.len(), payload.len());
} else {
println!("reply from {host}:{port}: {} bytes (payload differs)", resp.data.len());
}
Ok(())
}
Some("decode") => {
let path = args.get(1).ok_or_else(|| usage())?;
let mut bytes = Vec::new();
if path == "-" {
std::io::stdin()
.read_to_end(&mut bytes)
.map_err(|e| format!("stdin read failed: {e}"))?;
} else {
bytes = std::fs::read(path).map_err(|e| format!("read {path} failed: {e}"))?;
}
if bytes.len() % FRAME_SIZE_BYTES != 0 {
return Err(format!(
"input length {} is not a multiple of {FRAME_SIZE_BYTES}",
bytes.len()
));
}
let mut dec = Decoder::new();
let hits = dec.decode(&bytes).map_err(|e| e.to_string())?;
println!("address\tix\tiy\tboard\tbxid\tcnt0\tcnt1\ttot_fs\ttimestamp\tcalibration");
for h in &hits {
println!(
"{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}",
h.address, h.ix, h.iy, h.board, h.bxid, h.cnt0, h.cnt1, h.tot, h.timestamp,
h.calibration as u8
);
}
let stats = dec.stats();
eprintln!(
"# {} hits | data words: {} | timeframes: {} | bad headers: {}",
hits.len(), stats.n_frame_type[0], stats.n_frame_type[1], stats.n_bad_headers
);
Ok(())
}
_ => Err(usage()),
}
}