smon 0.1.4

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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! One serial console: its rolling buffer, its log, and everything a client can
//! ask of it.
//!
//! A console does not own the device. The runner does, and it feeds received
//! bytes in here. Input goes the other way, queued on the inject channel for
//! the runner to write, so the port is only ever written from one place.

use std::{
    path::PathBuf,
    sync::{
        Arc, Mutex,
        atomic::{AtomicBool, Ordering},
        mpsc::Sender,
    },
    time::{Duration, Instant},
};

use anyhow::Result;
use serde::{Deserialize, Serialize};
use tokio::sync::{Notify, broadcast, oneshot};

use crate::{
    log::{ConsoleLog, LogInfo},
    ring::{Matcher, Ring},
};

// How many events a viewer can fall behind before it is told it missed some.
// A viewer redraws every 16ms, so this is generous.
const EVENT_QUEUE: usize = 1024;

// Hard ceiling on a single expect() wait, so one call cannot block a client
// forever. Longer waits are done by the client re-calling.
const MAX_EXPECT_MS: u64 = 120_000;

// How long a release waits for the runner to actually close the port. The
// runner notices within its own tick, so this is generous.
const RELEASE_POLL: Duration = Duration::from_millis(50);
const RELEASE_TICKS: usize = 40;

/// Who produced a piece of input.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Origin {
    Typed,
    Key,
    Agent,
    /// Input from a raw byte bridge, where the writer is some other program
    /// speaking to the board directly.
    Bridge,
}

/// What a viewer attached to this console sees.
#[derive(Clone)]
pub enum ConsoleEvent {
    Rx(Vec<u8>),
    Echo {
        origin: Origin,
        text:   String,
    },
    System(String),
    /// The port came or went. A viewer reads the new state off the console, so
    /// this only says that it changed.
    Connected,
}

/// Input queued for the runner to write to the port. `echo` is the display form
/// shown to viewers, and `resp` reports the write result back when a caller is
/// waiting on it.
pub struct Inject {
    pub bytes:  Vec<u8>,
    pub echo:   String,
    pub origin: Origin,
    pub resp:   Option<oneshot::Sender<Result<(), String>>>,
}

/// The result of an [`Console::expect`] wait.
pub struct Expect {
    pub matched:   bool,
    pub data:      String,
    pub cursor:    u64,
    pub timed_out: bool,
}

/// Everything a console is made of, apart from its log and its input queue.
pub struct ConsoleSpec {
    pub device:   String,
    pub label:    Option<String>,
    pub baud:     u32,
    pub eol:      Vec<u8>,
    pub ring_cap: usize,
    /// Where to offer this console as raw bytes, when it is offered at all.
    pub bridge:   Option<u16>,
}

pub struct Console {
    device:    String,
    label:     Option<String>,
    baud:      u32,
    eol:       Vec<u8>,
    ring:      Mutex<Ring>,
    notify:    Notify,
    // A std channel, not a tokio one. Sending never blocks, so async callers and
    // the keyboard both queue input without a runtime, and the runner thread can
    // block on it instead of polling.
    inject:    Sender<Inject>,
    events:    broadcast::Sender<ConsoleEvent>,
    log:       Mutex<ConsoleLog>,
    connected: AtomicBool,
    // Set while some other program needs the device node itself. The runner
    // closes the port and stays off it until this clears.
    released:  AtomicBool,
    // Where this console is offered as a raw byte stream, when it is.
    bridge:    Option<u16>,
}

impl Console {
    pub fn new(spec: ConsoleSpec, log: ConsoleLog, inject: Sender<Inject>) -> Arc<Self> {
        let ConsoleSpec {
            device,
            label,
            baud,
            eol,
            ring_cap,
            bridge,
        } = spec;
        Arc::new(Self {
            device,
            label,
            baud,
            eol,
            ring: Mutex::new(Ring::new(ring_cap)),
            notify: Notify::new(),
            inject,
            events: broadcast::Sender::new(EVENT_QUEUE),
            log: Mutex::new(log),
            connected: AtomicBool::new(false),
            released: AtomicBool::new(false),
            bridge,
        })
    }

    pub fn device(&self) -> &str {
        &self.device
    }

    pub fn label(&self) -> Option<&str> {
        self.label.as_deref()
    }

    /// How this console is named to a person, its label when it has one.
    pub fn name(&self) -> &str {
        self.label.as_deref().unwrap_or(&self.device)
    }

    /// Does `query` address this console? A label or a device path both work,
    /// and so does the last element of the path, so `ttyUSB2` finds
    /// `/dev/ttyUSB2` without typing the whole thing.
    pub fn matches(&self, query: &str) -> bool {
        if self.label.as_deref().is_some_and(|l| l.eq_ignore_ascii_case(query)) {
            return true;
        }
        if self.device.eq_ignore_ascii_case(query) {
            return true;
        }
        self.device
            .rsplit(['/', '\\'])
            .next()
            .is_some_and(|file| file.eq_ignore_ascii_case(query))
    }

    pub fn subscribe(&self) -> broadcast::Receiver<ConsoleEvent> {
        self.events.subscribe()
    }

    /// Called from the runner when the port delivers bytes.
    pub fn push_rx(&self, bytes: &[u8]) {
        self.ring.lock().unwrap().append(bytes);
        self.record(self.log.lock().unwrap().rx(bytes));
        self.notify.notify_waiters();
        self.emit(ConsoleEvent::Rx(bytes.to_vec()));
    }

    /// Called from the runner when it takes input off the inject queue.
    pub fn push_echo(&self, origin: Origin, bytes: &[u8], echo: &str) {
        self.record(self.log.lock().unwrap().tx(origin, bytes, echo));
        self.emit(ConsoleEvent::Echo {
            origin,
            text: echo.to_string(),
        });
    }

    /// A line for the log only. Where the MCP endpoint bound is for agents, so
    /// it belongs in the log and not on the person's screen.
    pub fn note(&self, text: &str) {
        self.record(self.log.lock().unwrap().system(text));
    }

    pub fn push_system(&self, text: &str) {
        self.record(self.log.lock().unwrap().system(text));
        self.emit(ConsoleEvent::System(text.to_string()));
    }

    pub fn set_connected(&self, connected: bool) {
        self.connected.store(connected, Ordering::Relaxed);
        self.emit(ConsoleEvent::Connected);
    }

    pub fn connected(&self) -> bool {
        self.connected.load(Ordering::Relaxed)
    }

    /// Let go of the device so another program can open it. The console keeps
    /// its buffer, its log and its viewers, it just stops holding the port.
    ///
    /// The runner does the closing, so this waits for it. Returning before the
    /// port was actually closed would hand the caller a device that is still
    /// held, which is the one thing this exists to avoid. Reports whether it
    /// let go in time.
    pub async fn release(&self) -> bool {
        self.released.store(true, Ordering::Relaxed);
        for _ in 0..RELEASE_TICKS {
            if !self.connected() {
                return true;
            }
            tokio::time::sleep(RELEASE_POLL).await;
        }
        false
    }

    /// Take the device back.
    pub fn hold(&self) {
        self.released.store(false, Ordering::Relaxed);
    }

    pub fn released(&self) -> bool {
        self.released.load(Ordering::Relaxed)
    }

    pub fn bridge_port(&self) -> Option<u16> {
        self.bridge
    }

    /// Queue bytes exactly as given, with no line ending added. This is the
    /// raw bridge path, where the client is already speaking the board's own
    /// protocol and nothing may be inserted into it.
    pub fn queue_raw(&self, bytes: Vec<u8>) -> Result<(), String> {
        let echo = String::from_utf8_lossy(&bytes).into_owned();
        self.queue(bytes, echo, Origin::Bridge)
    }

    pub fn baud(&self) -> u32 {
        self.baud
    }

    pub fn total(&self) -> u64 {
        self.ring.lock().unwrap().total()
    }

    /// Bytes received since `cursor`, or the whole retained buffer if `None`,
    /// with the new cursor to pass next time.
    pub fn read(&self, cursor: Option<u64>) -> (String, u64) {
        let ring = self.ring.lock().unwrap();
        let start = cursor.unwrap_or(ring.base());
        let (abs, hay) = ring.slice_from(start);
        (String::from_utf8_lossy(hay).into_owned(), abs + hay.len() as u64)
    }

    pub fn snapshot(&self, lines: usize) -> String {
        self.ring.lock().unwrap().tail_lines(lines)
    }

    pub fn log_info(&self) -> LogInfo {
        self.log.lock().unwrap().info()
    }

    /// Start a new log segment and report where it went.
    ///
    /// # Errors
    /// Returns an error if the new segment cannot be created.
    pub fn log_roll(&self, tag: Option<&str>) -> Result<LogInfo> {
        self.log.lock().unwrap().roll(tag)
    }

    /// This console's log segment files, newest first.
    ///
    /// # Errors
    /// Returns an error if the log directory cannot be read.
    pub fn log_segments(&self, days: Option<i64>) -> Result<Vec<PathBuf>> {
        self.log.lock().unwrap().segments(days)
    }

    /// Queue input without waiting for the write to land. This is the path a
    /// keystroke takes, where there is nobody to report a failure to.
    pub fn queue_line(&self, text: &str) -> Result<(), String> {
        let mut bytes = text.as_bytes().to_vec();
        bytes.extend_from_slice(&self.eol);
        self.queue(bytes, text.to_string(), Origin::Typed)
    }

    pub fn queue_ctrl(&self, ctrl: char) -> Result<(), String> {
        let byte = ctrl_byte(ctrl).ok_or_else(|| format!("no control byte for '{ctrl}'"))?;
        let echo = format!("Ctrl+{}", ctrl.to_ascii_uppercase());
        self.queue(vec![byte], echo, Origin::Key)
    }

    fn queue(&self, bytes: Vec<u8>, echo: String, origin: Origin) -> Result<(), String> {
        self.inject
            .send(Inject {
                bytes,
                echo,
                origin,
                resp: None,
            })
            .map_err(|_| "serial session closed".to_string())
    }

    /// Write `text` to the port, appending the console end-of-line when
    /// `newline`. Returns the cursor just before the write, so a following
    /// read or expect captures the echo and reply.
    ///
    /// # Errors
    /// Returns an error if the port is disconnected or the write fails.
    pub async fn send(&self, text: String, newline: bool) -> Result<u64, String> {
        let cursor = self.total();
        let echo = text.clone();
        let mut bytes = text.into_bytes();
        if newline {
            bytes.extend_from_slice(&self.eol);
        }
        self.inject_and_wait(bytes, echo).await?;
        Ok(cursor)
    }

    /// Returns the cursor before the write.
    ///
    /// # Errors
    /// Returns an error if `ctrl` has no control byte, or the write fails.
    pub async fn send_ctrl(&self, ctrl: char) -> Result<u64, String> {
        let byte = ctrl_byte(ctrl).ok_or_else(|| format!("no control byte for '{ctrl}'"))?;
        let cursor = self.total();
        let echo = format!("Ctrl+{}", ctrl.to_ascii_uppercase());
        self.inject_and_wait_as(vec![byte], echo, Origin::Key).await?;
        Ok(cursor)
    }

    async fn inject_and_wait(&self, bytes: Vec<u8>, echo: String) -> Result<(), String> {
        self.inject_and_wait_as(bytes, echo, Origin::Agent).await
    }

    async fn inject_and_wait_as(&self, bytes: Vec<u8>, echo: String, origin: Origin) -> Result<(), String> {
        let (resp_tx, resp_rx) = oneshot::channel();
        self.inject
            .send(Inject {
                bytes,
                echo,
                origin,
                resp: Some(resp_tx),
            })
            .map_err(|_| "serial session closed".to_string())?;
        match resp_rx.await {
            Ok(result) => result,
            Err(_) => Err("serial session closed".to_string()),
        }
    }

    /// Wait until `pattern` appears in received data, or `timeout_ms` elapses.
    /// Scans from `cursor` if given, else from the current end for new data
    /// only. Returns the text from the start point up to and including the
    /// match, or up to the end on timeout, plus the cursor to continue
    /// from.
    ///
    /// # Errors
    /// Returns an error if `pattern` is not a valid regular expression.
    pub async fn expect(
        &self,
        pattern: &str,
        timeout_ms: u64,
        regex: bool,
        cursor: Option<u64>,
    ) -> Result<Expect, String> {
        let matcher = Matcher::build(pattern, regex)?;
        let start = cursor.unwrap_or_else(|| self.total());
        let deadline = Instant::now() + Duration::from_millis(timeout_ms.min(MAX_EXPECT_MS));
        // Each scan resumes near where the previous one ended instead of
        // rescanning everything from `start` for every received chunk.
        let mut scan_from = start;

        loop {
            // Register interest before scanning so a byte that arrives between
            // the scan and the wait cannot be missed.
            let notified = self.notify.notified();
            tokio::pin!(notified);
            notified.as_mut().enable();

            {
                let ring = self.ring.lock().unwrap();
                let (scan_abs, scan_hay) = ring.slice_from(scan_from);
                if let Some(end) = matcher.find_end(scan_hay) {
                    let match_end = scan_abs + end as u64;
                    let (abs, hay) = ring.slice_from(start);
                    let data = &hay[..(match_end - abs) as usize];
                    return Ok(Expect {
                        matched:   true,
                        data:      String::from_utf8_lossy(data).into_owned(),
                        cursor:    match_end,
                        timed_out: false,
                    });
                }
                scan_from = matcher.resume_from(start, ring.total());
            }

            let now = Instant::now();
            if now >= deadline {
                let ring = self.ring.lock().unwrap();
                let (abs, hay) = ring.slice_from(start);
                return Ok(Expect {
                    matched:   false,
                    data:      String::from_utf8_lossy(hay).into_owned(),
                    cursor:    abs + hay.len() as u64,
                    timed_out: true,
                });
            }

            tokio::select! {
                () = &mut notified => {}
                () = tokio::time::sleep(deadline - now) => {}
            }
        }
    }

    // Returns how many viewers got the event. Zero is the normal state for a
    // console nobody is watching, so it is a count and not a failure.
    fn emit(&self, event: ConsoleEvent) -> usize {
        self.events.send(event).unwrap_or(0)
    }

    // A log write can fail on a full disk. That must not take the console down,
    // so it is reported and the console keeps running.
    fn record(&self, outcome: Result<()>) {
        if let Err(e) = outcome {
            eprintln!("smon: log write failed on {}: {e:#}", self.name());
        }
    }
}

/// The control byte for Ctrl+<c>. One shared mapping serves both the TUI
/// keystrokes and the MCP serial_send_ctrl tool, so they cannot drift apart.
pub fn ctrl_byte(c: char) -> Option<u8> {
    let lower = c.to_ascii_lowercase();
    Some(match lower {
        'a'..='z' => (lower as u8) - b'a' + 1,
        '@' => 0,
        '[' => 0x1b,
        '\\' => 0x1c,
        ']' => 0x1d,
        '^' => 0x1e,
        '_' => 0x1f,
        _ => return None,
    })
}

#[cfg(test)]
mod tests {
    use std::{env, fs, sync::mpsc::channel};

    use super::*;
    use crate::ring::DEFAULT_RING_CAP;

    fn console(device: &str, label: Option<&str>) -> Arc<Console> {
        let dir = env::temp_dir().join(format!("smon-console-test-{}", sanitize_for_test(device)));
        if dir.exists() {
            fs::remove_dir_all(&dir).unwrap();
        }
        let log = ConsoleLog::open_in(dir, device, 30, None).unwrap();
        // The inject receiver is dropped, these tests never write to a port.
        Console::new(
            ConsoleSpec {
                device:   device.to_string(),
                label:    label.map(str::to_string),
                baud:     115_200,
                eol:      b"\r\n".to_vec(),
                ring_cap: DEFAULT_RING_CAP,
                bridge:   None,
            },
            log,
            channel().0,
        )
    }

    fn sanitize_for_test(s: &str) -> String {
        s.chars().filter(char::is_ascii_alphanumeric).collect()
    }

    #[test]
    fn snapshot_returns_last_lines() {
        let c = console("COM1", None);
        c.push_rx(b"one\r\ntwo\r\nthree\r\npartial");
        assert_eq!(c.snapshot(2), "three\npartial");
        assert_eq!(c.snapshot(10), "one\ntwo\nthree\npartial");
        assert_eq!(c.snapshot(0), "");
    }

    #[test]
    fn a_label_names_the_console_but_the_device_still_addresses_it() {
        let c = console("/dev/ttyUSB2", Some("board-a"));
        assert_eq!(c.name(), "board-a");
        assert!(c.matches("board-a"));
        assert!(c.matches("BOARD-A"));
        assert!(c.matches("/dev/ttyUSB2"));
        assert!(c.matches("ttyUSB2"));
        assert!(!c.matches("ttyUSB1"));
    }

    #[test]
    fn without_a_label_the_device_is_the_name() {
        let c = console("COM11", None);
        assert_eq!(c.name(), "COM11");
        assert!(c.matches("com11"));
        // COM1 must never address COM11.
        assert!(!c.matches("COM1"));
    }

    #[test]
    fn viewers_see_received_bytes_and_state_changes() {
        let c = console("COM7", None);
        let mut viewer = c.subscribe();
        c.push_rx(b"hello");
        c.set_connected(false);

        assert!(matches!(viewer.try_recv(), Ok(ConsoleEvent::Rx(b)) if b == b"hello"));
        assert!(matches!(viewer.try_recv(), Ok(ConsoleEvent::Connected)));
        assert!(!c.connected());
    }

    #[test]
    fn ctrl_byte_maps_letters_and_symbols() {
        assert_eq!(ctrl_byte('c'), Some(3));
        assert_eq!(ctrl_byte('C'), Some(3));
        assert_eq!(ctrl_byte('['), Some(0x1b));
        assert_eq!(ctrl_byte('1'), None);
    }
}