use anyhow::Result;
use clap::Parser;
use log::info;
use rax25::{
Addr,
r#async::{ConnectionBuilder, connect_kiss_endpoint},
parse_duration,
};
#[derive(Clone, Copy, Debug, Eq, PartialEq, clap::ValueEnum)]
pub enum LogLevel {
Error,
Warn,
Info,
Debug,
Trace,
}
#[derive(Parser, Debug)]
struct Opt {
#[allow(clippy::doc_markdown)]
#[clap(short = 'p', default_value = "serial:///dev/null")]
port: String,
#[clap(short = 's')]
src: String,
#[clap(short = 'r')]
cr: bool,
#[clap(short = 'v', default_value = "info")]
v: LogLevel,
#[clap(long)]
capture: Option<std::path::PathBuf>,
#[clap(long, value_parser = parse_duration)]
srt: Option<std::time::Duration>,
#[clap(long, value_parser = parse_duration)]
t3v: Option<std::time::Duration>,
#[clap(long, value_enum, value_delimiter = ',')]
experiments: Vec<rax25::Experiment>,
#[clap(long)]
mtu: Option<usize>,
}
#[tokio::main]
async fn main() -> Result<()> {
let opt = Opt::parse();
stderrlog::new()
.module(module_path!())
.module("rax25")
.show_module_names(true)
.verbosity(opt.v as usize)
.init()
.unwrap();
let port = connect_kiss_endpoint(&opt.port).await?;
info!("Awaiting connection");
let mut client = {
let mut builder = ConnectionBuilder::new(Addr::new(&opt.src)?, port)?;
if let Some(capture) = opt.capture {
builder = builder.capture(capture);
}
if let Some(v) = opt.srt {
builder = builder.srt_default(v);
}
if let Some(v) = opt.t3v {
builder = builder.t3v(v);
}
if let Some(v) = opt.mtu {
builder = builder.mtu(v);
}
for ex in &opt.experiments {
builder = builder.enable_experiment(*ex);
}
builder.accept().await?
};
info!("Connected");
client.write(b"Welcome to the server!\n").await?;
loop {
tokio::select! {
data = client.read() => {
let data = data?;
if data.is_empty() {
info!("Got EOF");
break;
}
let s = match String::from_utf8(data.clone()) {
Ok(s) => s,
Err(_) => String::from_utf8(data.iter().map(|&b| b & 0x7F).collect())?,
};
let s = s.trim_end();
client.write(format!("Got <{s}>\n").as_bytes()).await?;
},
}
}
info!("End of main loop");
client.disconnect().await?;
Ok(())
}