rax25 0.2.6

AX.25 connected mode implementation
Documentation
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)]
    /// KISS endpoint, such as serial:///dev/rfcomm0 or tcp://localhost:8000.
    #[clap(short = 'p', default_value = "serial:///dev/null")]
    port: String,

    /// Source callsign and SSID.
    #[clap(short = 's')]
    src: String,

    /// Use CR instead of NL.
    #[clap(short = 'r')]
    cr: bool,

    /// Verbosity level.
    #[clap(short = 'v', default_value = "info")]
    v: LogLevel,

    /// Capture packets in/out to pcap.
    #[clap(long)]
    capture: Option<std::path::PathBuf>,

    /// Initial SRT value.
    #[clap(long, value_parser = parse_duration)]
    srt: Option<std::time::Duration>,

    /// T3 (idle timer) value.
    #[clap(long, value_parser = parse_duration)]
    t3v: Option<std::time::Duration>,

    /// Experiments to enable.
    #[clap(long, value_enum, value_delimiter = ',')]
    experiments: Vec<rax25::Experiment>,

    /// MTU for outgoing frames.
    #[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(())
}