smon 0.1.3

Minimalistic TUI serial monitor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! The standalone TUI: pick a port, take it over, and watch it.
//!
//! This is the path with no daemon involved. It builds one console of its own,
//! runs a server for it so an agent can still reach the port, and hands the
//! screen to the session.

use std::{
    io::stdout,
    net::SocketAddr,
    sync::{Arc, mpsc::channel},
    thread,
    time::Duration,
};

use anyhow::{Context, Result, anyhow, bail};
use crossterm::{
    event::{DisableMouseCapture, EnableMouseCapture},
    execute,
};
use ratatui::DefaultTerminal;
use serde::Serialize;
use serialport::{SerialPortType, available_ports};

use crate::{
    attached::Local,
    client,
    config::Config,
    console::{Console, ConsoleSpec},
    control::{Control, Role},
    log::{ConsoleLog, DEFAULT_RETENTION_DAYS},
    mcp, picker, probe,
    registry::Registry,
    remote,
    ring::DEFAULT_RING_CAP,
    runner::Runner,
    session, wsl,
};

const ATTACH_SENTINEL: &str = "\0usbipd-attach:";
// A console a daemon already holds, as opposed to a raw device path.
const CONSOLE_SENTINEL: &str = "\0console:";
// What a viewer is shown of the past when it attaches.
const BACKLOG_LINES: usize = 2000;
const DEFAULT_BAUD: u32 = 115200;
const BAUDS: [u32; 8] = [9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600];
// The server binds on its own thread, so the session waits this long to hear
// where it landed before giving up on it.
const READY_WAIT: Duration = Duration::from_secs(5);

/// `remote` means `mcp_bind` is the near end of an ssh tunnel. Then the only
/// consoles that exist are the ones on the far side, and this machine's own
/// serial ports have nothing to do with it.
pub fn run(eol: Vec<u8>, mcp_bind: SocketAddr, remote: bool) -> Result<()> {
    // Probed before the screen is taken over, so a failure to reach the far end
    // prints plainly instead of flashing past an alternate screen.
    let daemon = client::find_daemon(mcp_bind);
    if remote && daemon.is_none() {
        bail!("no smon answering through the tunnel at {mcp_bind}");
    }
    let control = Arc::new(Control::new(Role::Tui));
    let mut terminal = ratatui::init();
    // Without capture the terminal turns the wheel into arrow keys, which land
    // in command history instead of the scrollback.
    let result = execute!(stdout(), EnableMouseCapture)
        .context("enabling mouse capture")
        .and_then(|()| pick_and_attach(&mut terminal, &eol, mcp_bind, daemon.as_ref(), remote, &control));
    // Released before restore, while the alternate screen is still up, so the
    // terminal is back to normal by the time the shell prompt returns.
    let released = execute!(stdout(), DisableMouseCapture).context("releasing mouse capture");
    ratatui::restore();
    if control.stopping() {
        println!("smon: stopped by an update, start it again to use the new version");
    }
    result.and(released)
}

fn pick_and_attach(
    terminal: &mut DefaultTerminal,
    eol: &[u8],
    mcp_bind: SocketAddr,
    daemon: Option<&client::Daemon>,
    remote: bool,
    control: &Arc<Control>,
) -> Result<()> {
    loop {
        let Some(choice) = select_port(terminal, daemon, remote)? else {
            return Ok(());
        };
        if let Some(console) = choice.strip_prefix(CONSOLE_SENTINEL) {
            let Some(daemon) = daemon else {
                bail!("no smon to attach {console} to");
            };
            let mut attached = remote::attach(&daemon.addr.to_string(), console)?;
            return session::run(terminal, &mut attached, control);
        }
        let device = choice;
        let Some(baud) = pick_baud(terminal, &device)? else {
            continue; // cancelling the baud picker returns to port selection
        };
        let mut config = Config::load();
        config.baud.insert(device.clone(), baud);
        config.save()?;

        // With a daemon running, the port goes to it rather than being opened
        // here. Then it stays up after this viewer quits, and it is logged and
        // reachable like every other console instead of only existing while
        // this window does.
        if let Some(daemon) = daemon {
            let name = adopt(daemon, &device, baud, eol)?;
            let mut attached = remote::attach(&daemon.addr.to_string(), &name)?;
            return session::run(terminal, &mut attached, control);
        }
        return attach(terminal, &device, baud, eol, mcp_bind, control);
    }
}

fn attach(
    terminal: &mut DefaultTerminal,
    device: &str,
    baud: u32,
    eol: &[u8],
    mcp_bind: SocketAddr,
    control: &Arc<Control>,
) -> Result<()> {
    let log = ConsoleLog::open(device, DEFAULT_RETENTION_DAYS, None)?;
    let (inject_tx, inject_rx) = channel();
    let console = Console::new(
        ConsoleSpec {
            device: device.to_string(),
            label: None,
            baud,
            eol: eol.to_vec(),
            ring_cap: DEFAULT_RING_CAP,
            bridge: None,
        },
        log,
        inject_tx,
    );
    // The first open must succeed so a bad pick fails fast back at the picker.
    // Failures after that go through the runner's reconnect loop instead of
    // ending the session and losing the scrollback.
    let runner = Runner::start(Arc::clone(&console), inject_rx, true)?;

    let server = match start_server(&console, mcp_bind, Arc::clone(control)) {
        Ok(server) => server,
        Err(e) => {
            runner.stop();
            return Err(e);
        }
    };

    let mut local = Local::new(Arc::clone(&console), BACKLOG_LINES);
    let result = session::run(terminal, &mut local, control);

    if !server.stop() {
        console.note("mcp server had already stopped");
    }
    runner.stop();
    result
}

// Hand a device to the daemon and return the name it answers to.
fn adopt(daemon: &client::Daemon, device: &str, baud: u32, eol: &[u8]) -> Result<String> {
    let request = AdoptRequest {
        device,
        baud,
        eol: eol_name(eol),
    };
    let body = client::call(daemon.addr, "console_adopt", &serde_json::to_string(&request)?)?;
    let adopted: mcp::StatusResult =
        serde_json::from_str(&body).with_context(|| format!("bad console_adopt reply: {body}"))?;
    Ok(adopted.label.unwrap_or(adopted.port))
}

#[derive(Serialize)]
struct AdoptRequest<'a> {
    device: &'a str,
    baud:   u32,
    eol:    &'static str,
}

// The daemon takes the end-of-line by name, the session already holds it as
// bytes, so it is named back here rather than parsed twice.
fn eol_name(eol: &[u8]) -> &'static str {
    match eol {
        b"\r" => "cr",
        b"\n" => "lf",
        b"\r\n" => "crlf",
        _ => "none",
    }
}

struct Server {
    control: Arc<Control>,
    thread:  thread::JoinHandle<()>,
}

impl Server {
    // Whether the server was still there to be told. The thread is detached
    // rather than joined: a client holding a stream open can keep graceful
    // shutdown from returning, so quitting must not wait on a client.
    fn stop(self) -> bool {
        let told = self.control.release();
        drop(self.thread);
        told
    }
}

// An agent must always be able to reach a running smon, so a failed bind ends
// the session instead of degrading to a monitor without an endpoint.
fn start_server(console: &Arc<Console>, bind: SocketAddr, control: Arc<Control>) -> Result<Server> {
    let (ready_tx, ready_rx) = channel();
    let registry = Registry::new(vec![Arc::clone(console)], 0);
    let thread = mcp::spawn(bind, registry, Arc::clone(&control), ready_tx);

    match ready_rx.recv_timeout(READY_WAIT) {
        Ok(Ok(addr)) => {
            console.note(&format!("mcp serving http://{addr}/mcp"));
            Ok(Server { control, thread })
        }
        Ok(Err(e)) => Err(anyhow!("mcp bind failed: {e}")),
        Err(e) => Err(anyhow!("mcp server did not start: {e}")),
    }
}

fn pick_baud(terminal: &mut DefaultTerminal, device: &str) -> Result<Option<u32>> {
    let config = Config::load();
    let saved = config.baud.get(device).copied();

    let baud_items: Vec<picker::Item> = BAUDS
        .iter()
        .map(|b| picker::Item {
            value: b.to_string(),
            label: b.to_string(),
            busy:  false,
        })
        .collect();

    let Some(choice) = picker::pick(
        terminal,
        "Select baud rate",
        || baud_items.clone(),
        default_baud_index(&BAUDS, saved),
        false,
    )?
    else {
        return Ok(None);
    };

    Ok(Some(choice.parse::<u32>().context("parsing baud rate")?))
}

fn default_baud_index(bauds: &[u32], saved: Option<u32>) -> Option<usize> {
    let target = saved.unwrap_or(DEFAULT_BAUD);
    bauds
        .iter()
        .position(|b| *b == target)
        .or_else(|| bauds.iter().position(|b| *b == DEFAULT_BAUD))
}

fn select_port(
    terminal: &mut DefaultTerminal,
    daemon: Option<&client::Daemon>,
    remote: bool,
) -> Result<Option<String>> {
    let usbipd = if remote { None } else { wsl::detect() };
    let mut notice: Option<String> = None;

    loop {
        let title = match &notice {
            Some(msg) => format!("Select serial port  --  {msg}"),
            None => "Select serial port".to_string(),
        };

        let make_items = || {
            let mut items = daemon_items(daemon);
            // A device on this machine is not reachable through a tunnel, so
            // with a remote daemon its consoles are the whole list.
            if !remote {
                items.extend(serial_port_items());
            }
            if let Some(u) = &usbipd {
                for device in u.serial_devices() {
                    items.push(picker::Item {
                        value: format!("{ATTACH_SENTINEL}{}", device.busid),
                        label: device.attach_label(),
                        busy:  false,
                    });
                }
            }
            items
        };

        let value = match picker::pick(terminal, &title, make_items, None, true)? {
            Some(v) => v,
            None => return Ok(None),
        };

        let Some(busid) = value.strip_prefix(ATTACH_SENTINEL) else {
            return Ok(Some(value));
        };

        if let Some(u) = &usbipd {
            match u.attach(busid) {
                Ok(()) => {
                    wait_for_new_ports(serial_port_items().len());
                    notice = Some(format!("attached {busid}"));
                }
                Err(e) => notice = Some(e.to_string()),
            }
        }
    }
}

// The consoles a running smon already holds, offered ahead of raw devices.
fn daemon_items(daemon: Option<&client::Daemon>) -> Vec<picker::Item> {
    let Some(daemon) = daemon else {
        return Vec::new();
    };
    daemon
        .consoles
        .iter()
        .map(|console| {
            let state = if console.connected {
                "connected"
            } else {
                "disconnected"
            };
            picker::Item {
                value: format!("{CONSOLE_SENTINEL}{}", console.name),
                label: format!("{}  (smon on {}, {state})", console.name, daemon.addr),
                busy:  false,
            }
        })
        .collect()
}

fn serial_port_items() -> Vec<picker::Item> {
    let mut items: Vec<picker::Item> = probe::hold(|| {
        available_ports()
            .unwrap_or_default()
            .into_iter()
            .map(|p| {
                let label = match &p.port_type {
                    SerialPortType::UsbPort(info) => {
                        let product = info.product.as_deref().unwrap_or("USB");
                        format!("{}  ({product})", p.port_name)
                    }
                    SerialPortType::BluetoothPort => format!("{}  (Bluetooth)", p.port_name),
                    SerialPortType::PciPort => format!("{}  (PCI)", p.port_name),
                    SerialPortType::Unknown => p.port_name.clone(),
                };
                let busy = probe::is_busy(&p.port_name);
                picker::Item {
                    value: p.port_name,
                    label,
                    busy,
                }
            })
            .collect()
    });
    items.sort_by_key(|a| port_sort_key(&a.value));
    items
}

// Sort COM9 before COM10: split the trailing number off the name so it compares
// numerically instead of lexically.
fn port_sort_key(name: &str) -> (String, Option<u64>) {
    let digits = name.chars().rev().take_while(char::is_ascii_digit).count();
    let (prefix, number) = name.split_at(name.len() - digits);
    (prefix.to_string(), number.parse().ok())
}

fn wait_for_new_ports(baseline: usize) {
    for _ in 0..15 {
        if serial_port_items().len() > baseline {
            return;
        }
        thread::sleep(Duration::from_millis(200));
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn saved_baud_is_preselected() {
        assert_eq!(default_baud_index(&BAUDS, Some(57600)), Some(3));
    }

    #[test]
    fn missing_or_unsaved_baud_falls_back_to_default() {
        assert_eq!(default_baud_index(&BAUDS, None), Some(4)); // 115200
        assert_eq!(default_baud_index(&BAUDS, Some(12345)), Some(4));
    }

    #[test]
    fn ports_sort_numerically_not_lexically() {
        let mut names = vec!["COM10", "COM9", "COM1"];
        names.sort_by_key(|a| port_sort_key(a));
        assert_eq!(names, ["COM1", "COM9", "COM10"]);
    }
}