canboat 8.2.1

NMEA 2000 decoding, encoding and live-bus access: the CANboat PGN database as a library, plus the canboat CLI (convert / interface / server / tui).
Documentation
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
// (C) 2009-2026, Kees Verruijt, Harlingen, The Netherlands.

//! Actisense NGT-1 binary protocol — sans-I/O byte framer.
//!
//! Frames on the wire look like:
//!
//! ```text
//!   DLE STX <cmd> <len> <payload...> <checksum> DLE ETX
//! ```
//!
//! Where:
//!   - DLE = 0x10, STX = 0x02, ETX = 0x03
//!   - Inside the framed region, every literal `DLE` is doubled
//!     (`DLE DLE`) so the receiver can unambiguously detect frame
//!     boundaries.
//!   - `len` is the unescaped payload length.
//!   - The 8-bit sum of cmd + len + payload + checksum is zero
//!     modulo 256.
//!
//! Commands we care about for v0:
//!   - `N2K_MSG_RECEIVED` (0x93) — an incoming N2K frame from the bus.
//!     The payload is:
//!     `prio(1) pgn(3, LE) dst(1) src(1) ts(4, LE ms) dlen(1) data(dlen)`.
//!
//! The framer emits [`NgtEvent`]s as soon as it finishes (or rejects)
//! a frame. It performs no I/O — the caller decides how bytes get fed
//! in (sync `Read::read`, tokio `AsyncReadExt::read_buf`, …).

use crate::engine::frame::RawFrame;

// Framing bytes are shared with the `.ebl` encoder in
// [`crate::engine::format::ebl`], hence `pub(crate)`. EBL framing is a superset
// of NGT-1 framing (it adds `ESC SOH … ESC LF` header records), so the
// decoder state machine stays here while the encoder lives next door.
pub(crate) const DLE: u8 = 0x10;
pub(crate) const STX: u8 = 0x02;
pub(crate) const ETX: u8 = 0x03;
/// Actisense `.ebl` log files frame each record with `ESC SOH ... ESC LF`
/// (alongside the usual `DLE STX ... DLE ETX` NGT-1 frames inside). Inside
/// any framed region ESC also doubles as an escape byte (`ESC ESC` for a
/// literal 0x1b), so the framer can keep them distinguished from frame
/// markers. ESC is only special in EBL mode.
pub(crate) const ESC: u8 = 0x1b;
pub(crate) const SOH: u8 = 0x01;
pub(crate) const LF: u8 = 0x0a;

/// Difference between the Windows FILETIME epoch (1601-01-01) and the
/// Unix epoch (1970-01-01), in milliseconds. EBL timestamp records carry
/// a FILETIME (100-ns ticks since 1601); divide by 10_000 to get ms and
/// subtract this to land on Unix ms.
pub(crate) const FILETIME_TO_UNIX_MS: u64 = 11_644_473_600_000;
/// EBL header record types. Only `0x03` (timestamp) is currently emitted
/// by Actisense's W2K-1 logger.
pub(crate) const EBL_TIMESTAMP: u8 = 0x03;

/// Receive an N2K frame off the bus.
pub const N2K_MSG_RECEIVED: u8 = 0x93;
/// Send an N2K frame onto the bus.
pub const N2K_MSG_SEND: u8 = 0x94;
/// Send an NGT-specific message (0xA0 is the matching "received" code).
pub const NGT_MSG_SEND: u8 = 0xA1;

/// One fully-decoded NGT-1 protocol message (cmd + unescaped payload).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NgtMessage {
    pub command: u8,
    pub payload: Vec<u8>,
}

#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]
pub enum NgtError {
    #[error("NGT message shorter than 3 bytes (cmd + len + checksum)")]
    ShortMessage,
    #[error("NGT checksum mismatch")]
    BadChecksum,
    #[error("NGT declared payload length {declared} does not match received {actual}")]
    LengthMismatch { declared: u8, actual: usize },
    #[error("NGT escape byte 0x{0:02x} not followed by STX/ETX/DLE")]
    BadEscape(u8),
}

/// EBL header records (only emitted when the decoder is in EBL mode).
/// Actisense's `.ebl` logger writes a timestamp record before every NGT-1
/// frame; the format reserves room for other record types, none of which
/// are documented, so an unrecognised type is preserved verbatim.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EblHeader {
    /// Wall-clock time of the record that follows, in Unix milliseconds.
    Timestamp(u64),
    /// Header type byte + payload, when the type isn't `EBL_TIMESTAMP`.
    Unknown { kind: u8, payload: Vec<u8> },
}

/// Events emitted by [`Ngt1Decoder::push_byte`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NgtEvent {
    /// A complete NGT-1 message was decoded.
    Message(NgtMessage),
    /// An EBL header record (timestamp or other). EBL-only.
    Header(EblHeader),
    /// The decoder rejected a frame (resync to next DLE STX).
    Error(NgtError),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum State {
    /// Outside any frame, looking for DLE (or ESC in EBL mode).
    Idle,
    /// Inside a `DLE STX … DLE ETX` NGT-1 frame, collecting bytes.
    InFrame,
    /// Inside an `ESC SOH … ESC LF` EBL record, collecting bytes.
    InHeader,
    /// Just consumed an escape byte (DLE always; ESC in EBL mode).
    /// `prev` carries the state we entered Escape from so the literal-
    /// escape branch can restore it.
    Escape { prev: PrevState },
}

/// Restricted shadow of [`State`] — only the states that can precede
/// `State::Escape`. Keeps `State` self-referential noise out.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PrevState {
    Idle,
    InFrame,
    InHeader,
}

/// Streaming NGT-1 byte decoder. Feed bytes; pull events.
///
/// Set EBL mode (via [`Ngt1Decoder::with_ebl`]) to parse Actisense `.ebl`
/// log files, which interleave `ESC SOH … ESC LF` timestamp records with
/// the usual NGT-1 frames and use ESC as a second escape byte inside
/// either kind of framed region. ESC handling is gated on EBL specifically
/// — non-EBL byte streams (live NGT-1, Yacht Device `.dle`, …) can carry
/// 0x1b as ordinary data and must be parsed without ESC escaping.
pub struct Ngt1Decoder {
    state: State,
    /// Accumulating frame payload (command + length + payload + checksum).
    buf: Vec<u8>,
    /// Treat ESC as a second escape byte and recognise ESC SOH / ESC LF
    /// as EBL record framing. Off by default.
    ebl: bool,
}

impl Default for Ngt1Decoder {
    fn default() -> Self {
        Self::new()
    }
}

impl Ngt1Decoder {
    pub fn new() -> Self {
        Self {
            state: State::Idle,
            // Worst-case frame: 1 cmd + 1 len + 255 payload + 1 cksum.
            buf: Vec::with_capacity(258),
            ebl: false,
        }
    }

    /// Build a decoder that also recognises Actisense `.ebl` framing
    /// (ESC SOH … ESC LF records and ESC as a second escape byte).
    pub fn with_ebl() -> Self {
        let mut d = Self::new();
        d.ebl = true;
        d
    }

    /// Feed one byte. Returns `Some(event)` when a frame completes or
    /// fails; `None` while bytes are accumulating.
    ///
    /// Mirrors the single state machine in canboat's `readNGT1Byte`,
    /// which dispatches both NGT-1 (DLE STX … DLE ETX) and EBL
    /// (ESC SOH … ESC LF) framing through a shared "Escape" state.
    pub fn push_byte(&mut self, b: u8) -> Option<NgtEvent> {
        match self.state {
            State::Idle => {
                if self.is_escape(b) {
                    self.state = State::Escape {
                        prev: PrevState::Idle,
                    };
                }
                None
            }
            State::InFrame => {
                if self.is_escape(b) {
                    self.state = State::Escape {
                        prev: PrevState::InFrame,
                    };
                } else {
                    self.buf.push(b);
                }
                None
            }
            State::InHeader => {
                // Per canboat, only ESC counts as escape inside a
                // header record — DLE bytes within a timestamp body
                // are literal data.
                if self.ebl && b == ESC {
                    self.state = State::Escape {
                        prev: PrevState::InHeader,
                    };
                } else {
                    self.buf.push(b);
                }
                None
            }
            State::Escape { prev } => match b {
                STX => {
                    self.buf.clear();
                    self.state = State::InFrame;
                    None
                }
                ETX => {
                    let r = self.finish_frame();
                    self.state = State::Idle;
                    r
                }
                SOH if self.ebl => {
                    self.buf.clear();
                    self.state = State::InHeader;
                    None
                }
                LF if self.ebl => {
                    let r = self.finish_header();
                    self.state = State::Idle;
                    r
                }
                DLE => {
                    if matches!(prev, PrevState::InFrame | PrevState::InHeader) {
                        self.buf.push(DLE);
                    }
                    self.state = restore(prev);
                    None
                }
                ESC if self.ebl => {
                    if matches!(prev, PrevState::InFrame | PrevState::InHeader) {
                        self.buf.push(ESC);
                    }
                    self.state = restore(prev);
                    None
                }
                other => {
                    let was_collecting = matches!(prev, PrevState::InFrame | PrevState::InHeader);
                    self.state = State::Idle;
                    self.buf.clear();
                    // An escape byte that wasn't followed by a frame
                    // delimiter outside any frame is just stray serial
                    // noise — surface no error. Only flag when we were
                    // mid-message and the bad sequence corrupted it.
                    if was_collecting {
                        Some(NgtEvent::Error(NgtError::BadEscape(other)))
                    } else {
                        None
                    }
                }
            },
        }
    }

    fn is_escape(&self, b: u8) -> bool {
        b == DLE || (self.ebl && b == ESC)
    }

    /// Convenience helper to feed a byte slice. Returns every event
    /// produced in order.
    pub fn push_bytes(&mut self, bytes: &[u8]) -> Vec<NgtEvent> {
        let mut out = Vec::new();
        for &b in bytes {
            if let Some(ev) = self.push_byte(b) {
                out.push(ev);
            }
        }
        out
    }

    fn finish_frame(&mut self) -> Option<NgtEvent> {
        let raw = std::mem::take(&mut self.buf);
        self.state = State::Idle;
        if raw.len() < 3 {
            return Some(NgtEvent::Error(NgtError::ShortMessage));
        }
        let cksum: u8 = raw.iter().copied().fold(0u8, u8::wrapping_add);
        if cksum != 0 {
            return Some(NgtEvent::Error(NgtError::BadChecksum));
        }
        let command = raw[0];
        let declared = raw[1];
        let actual_payload_len = raw.len() - 3; // - cmd - len - checksum
        if declared as usize != actual_payload_len {
            return Some(NgtEvent::Error(NgtError::LengthMismatch {
                declared,
                actual: actual_payload_len,
            }));
        }
        let payload = raw[2..raw.len() - 1].to_vec();
        Some(NgtEvent::Message(NgtMessage { command, payload }))
    }

    /// Dispatch a completed EBL header record. Type-0x03 records carry
    /// an 8-byte little-endian Windows FILETIME; unknown types are
    /// surfaced verbatim so a caller can log or skip them.
    fn finish_header(&mut self) -> Option<NgtEvent> {
        let raw = std::mem::take(&mut self.buf);
        if raw.is_empty() {
            return None;
        }
        match raw[0] {
            EBL_TIMESTAMP if raw.len() >= 9 => {
                let ticks = u64::from_le_bytes([
                    raw[1], raw[2], raw[3], raw[4], raw[5], raw[6], raw[7], raw[8],
                ]);
                let unix_ms = (ticks / 10_000).saturating_sub(FILETIME_TO_UNIX_MS);
                Some(NgtEvent::Header(EblHeader::Timestamp(unix_ms)))
            }
            kind => Some(NgtEvent::Header(EblHeader::Unknown {
                kind,
                payload: raw[1..].to_vec(),
            })),
        }
    }
}

fn restore(prev: PrevState) -> State {
    match prev {
        PrevState::Idle => State::Idle,
        PrevState::InFrame => State::InFrame,
        PrevState::InHeader => State::InHeader,
    }
}

/// Wrap `(cmd, payload)` in NGT-1 framing for transmission to the
/// device: `DLE STX <cmd> <len> <payload...> <checksum> DLE ETX`,
/// with DLE-stuffing inside the framed region and an 8-bit checksum
/// such that `(cmd + len + payload + checksum) ≡ 0 (mod 256)`.
///
/// The output is appended to `out`. `payload.len()` must be ≤ 255.
pub fn encode_ngt_message(cmd: u8, payload: &[u8], out: &mut Vec<u8>) {
    assert!(
        payload.len() <= u8::MAX as usize,
        "NGT payload exceeds 255 bytes"
    );
    out.push(DLE);
    out.push(STX);
    write_stuffed(out, cmd);
    write_stuffed(out, payload.len() as u8);
    let mut sum = cmd.wrapping_add(payload.len() as u8);
    for &b in payload {
        write_stuffed(out, b);
        sum = sum.wrapping_add(b);
    }
    let checksum = 0u8.wrapping_sub(sum);
    write_stuffed(out, checksum);
    out.push(DLE);
    out.push(ETX);
}

fn write_stuffed(out: &mut Vec<u8>, b: u8) {
    out.push(b);
    if b == DLE {
        out.push(DLE);
    }
}

/// Build the inner payload of a `N2K_MSG_SEND` (0x94) command from a
/// [`RawFrame`]. The NGT-1 transmit format is six header bytes —
/// `prio, pgn[0..2] (LE), dst, dlen` — followed by the data bytes.
/// Unlike the receive direction there is no `src` field (the NGT-1
/// supplies its own claimed source address) and no NGT timestamp.
///
/// `frame.data.len()` must be ≤ 223 (canboat's `FASTPACKET_MAX_SIZE`).
pub fn encode_n2k_send_payload(frame: &RawFrame) -> Vec<u8> {
    let mut out = Vec::with_capacity(6 + frame.data.len());
    out.push(frame.prio);
    out.push((frame.pgn & 0xff) as u8);
    out.push(((frame.pgn >> 8) & 0xff) as u8);
    out.push(((frame.pgn >> 16) & 0xff) as u8);
    out.push(frame.dst);
    out.push(frame.data.len() as u8);
    out.extend_from_slice(&frame.data);
    out
}

/// Build a complete NGT-1 transmit byte string from a `RawFrame`:
/// `encode_ngt_message(N2K_MSG_SEND, encode_n2k_send_payload(...))`.
/// Caller writes the bytes verbatim to the serial port.
pub fn encode_n2k_send_frame(frame: &RawFrame) -> Vec<u8> {
    let payload = encode_n2k_send_payload(frame);
    let mut out = Vec::with_capacity(payload.len() + 8);
    encode_ngt_message(N2K_MSG_SEND, &payload, &mut out);
    out
}

/// The reverse-engineered NGT-1 startup sequence (3 bytes wrapped in an
/// `NGT_MSG_SEND` command). Sent on connect and periodically afterwards
/// to keep the NGT-1's TX queue unlocked. Magic comes from canboat's
/// `actisense-serial.c` (originally from Actisense NMEAreader).
pub const NGT_STARTUP_SEQ: [u8; 3] = [0x11, 0x02, 0x00];

/// Encode the NGT-1 startup / keepalive ping as a ready-to-write byte
/// string.
pub fn encode_startup_ping() -> Vec<u8> {
    let mut out = Vec::with_capacity(NGT_STARTUP_SEQ.len() + 8);
    encode_ngt_message(NGT_MSG_SEND, &NGT_STARTUP_SEQ, &mut out);
    out
}

impl NgtMessage {
    /// Convert a `N2K_MSG_RECEIVED` (0x93) message into a [`RawFrame`].
    /// Other commands return `None` — they carry NGT-1-internal state,
    /// not N2K traffic.
    pub fn to_raw_frame(&self) -> Option<RawFrame> {
        if self.command != N2K_MSG_RECEIVED {
            return None;
        }
        // Header: prio(1) pgn(3 LE) dst(1) src(1) ts(4 LE) dlen(1)
        const HEADER: usize = 11;
        if self.payload.len() < HEADER {
            return None;
        }
        let prio = self.payload[0];
        let pgn = u32::from(self.payload[1])
            | (u32::from(self.payload[2]) << 8)
            | (u32::from(self.payload[3]) << 16);
        let dst = self.payload[4];
        let src = self.payload[5];
        // Bytes 6..10 = NGT-1 timestamp (ms since startup). We preserve
        // it as a string for downstream PLAIN/FAST emission.
        let ts_ms = u32::from(self.payload[6])
            | (u32::from(self.payload[7]) << 8)
            | (u32::from(self.payload[8]) << 16)
            | (u32::from(self.payload[9]) << 24);
        let dlen = self.payload[10] as usize;
        if HEADER + dlen > self.payload.len() {
            return None;
        }
        let data = self.payload[HEADER..HEADER + dlen]
            .iter()
            .copied()
            .collect::<smallvec::SmallVec<[u8; 8]>>();
        Some(RawFrame {
            timestamp: Some(ts_ms.to_string()),
            prio,
            pgn,
            src,
            dst,
            data,
        })
    }
}

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

    /// Build a valid NGT-1 frame from command + payload, applying
    /// DLE-stuffing and computing the checksum.
    fn encode_frame(cmd: u8, payload: &[u8]) -> Vec<u8> {
        let mut inner = vec![cmd, payload.len() as u8];
        inner.extend_from_slice(payload);
        let cksum = inner.iter().copied().fold(0u8, u8::wrapping_add);
        inner.push(0u8.wrapping_sub(cksum));
        // Now DLE-stuff and wrap.
        let mut out = vec![DLE, STX];
        for b in &inner {
            out.push(*b);
            if *b == DLE {
                out.push(DLE);
            }
        }
        out.extend_from_slice(&[DLE, ETX]);
        out
    }

    #[test]
    fn decodes_a_simple_frame() {
        let frame = encode_frame(0x42, &[0x01, 0x02, 0x03]);
        let mut d = Ngt1Decoder::new();
        let events = d.push_bytes(&frame);
        assert_eq!(events.len(), 1);
        match &events[0] {
            NgtEvent::Message(m) => {
                assert_eq!(m.command, 0x42);
                assert_eq!(m.payload, vec![0x01, 0x02, 0x03]);
            }
            other => panic!("expected Message, got {other:?}"),
        }
    }

    #[test]
    fn unescapes_dle_in_payload() {
        // Payload contains a literal DLE byte; it must be doubled on
        // the wire and surface as a single 0x10 in the decoded payload.
        let frame = encode_frame(0xA0, &[0xAA, DLE, 0xBB]);
        let mut d = Ngt1Decoder::new();
        let events = d.push_bytes(&frame);
        assert_eq!(events.len(), 1);
        match &events[0] {
            NgtEvent::Message(m) => assert_eq!(m.payload, vec![0xAA, DLE, 0xBB]),
            other => panic!("expected Message, got {other:?}"),
        }
    }

    #[test]
    fn rejects_bad_checksum() {
        // Build a valid frame, then flip the checksum.
        let mut frame = encode_frame(0x42, &[0x01]);
        // Find the checksum byte (just before DLE ETX); flip a bit.
        let n = frame.len();
        frame[n - 3] = frame[n - 3].wrapping_add(1);
        let mut d = Ngt1Decoder::new();
        let events = d.push_bytes(&frame);
        assert_eq!(events.len(), 1);
        assert!(matches!(events[0], NgtEvent::Error(NgtError::BadChecksum)));
    }

    #[test]
    fn split_across_pushes() {
        // Feed one byte at a time — must produce the same single event.
        let frame = encode_frame(0x42, &[0x01, 0x02]);
        let mut d = Ngt1Decoder::new();
        let mut total = Vec::new();
        for b in &frame {
            if let Some(ev) = d.push_byte(*b) {
                total.push(ev);
            }
        }
        assert_eq!(total.len(), 1);
        match &total[0] {
            NgtEvent::Message(m) => assert_eq!(m.command, 0x42),
            other => panic!("expected Message, got {other:?}"),
        }
    }

    #[test]
    fn ignores_garbage_before_frame() {
        // Pre- and post-amble noise must not trip the decoder.
        let mut bytes = vec![0xFF, 0xAA, 0x10, 0x99]; // bogus DLE-no-STX
        bytes.extend_from_slice(&encode_frame(0x42, &[0x01]));
        bytes.extend_from_slice(&[0x55, 0x66]);
        let mut d = Ngt1Decoder::new();
        let events = d.push_bytes(&bytes);
        assert_eq!(events.len(), 1);
        assert!(matches!(events[0], NgtEvent::Message(_)));
    }

    #[test]
    fn encoder_round_trips_through_decoder() {
        // Build a frame with a DLE byte in payload, encode it, then
        // feed back through the decoder and confirm we get the same
        // (cmd, payload) out.
        let payload = vec![0x00, 0x10, 0xff, 0x10, 0xab];
        let mut wire = Vec::new();
        encode_ngt_message(0x42, &payload, &mut wire);

        let mut d = Ngt1Decoder::new();
        let events = d.push_bytes(&wire);
        assert_eq!(events.len(), 1);
        match &events[0] {
            NgtEvent::Message(m) => {
                assert_eq!(m.command, 0x42);
                assert_eq!(m.payload, payload);
            }
            other => panic!("expected Message, got {other:?}"),
        }
    }

    #[test]
    fn encoder_handles_dle_in_length_and_checksum() {
        // Exercise the DLE-stuffing path for the length byte and the
        // checksum byte (both can equal 0x10 for some payload sizes /
        // contents). Pick a payload of 16 bytes — length = 0x10 = DLE.
        let payload = vec![0u8; 0x10];
        let mut wire = Vec::new();
        encode_ngt_message(0x42, &payload, &mut wire);
        let mut d = Ngt1Decoder::new();
        let events = d.push_bytes(&wire);
        assert_eq!(events.len(), 1);
        match &events[0] {
            NgtEvent::Message(m) => {
                assert_eq!(m.command, 0x42);
                assert_eq!(m.payload.len(), 0x10);
            }
            other => panic!("got {other:?}"),
        }
    }

    #[test]
    fn n2k_send_payload_layout_matches_canboat() {
        let frame = RawFrame {
            timestamp: None,
            prio: 6,
            pgn: 60928,
            src: 0, // ignored in send
            dst: 255,
            data: smallvec::smallvec![0xfb, 0x9b, 0x70, 0x22, 0x00, 0x9b, 0x50, 0xc0],
        };
        let payload = encode_n2k_send_payload(&frame);
        // prio (1) + pgn LE (3) + dst (1) + dlen (1) + data (8) = 14 bytes.
        assert_eq!(payload.len(), 14);
        // 60928 = 0xEE00 → LE bytes 0x00, 0xee, 0x00.
        assert_eq!(payload[0], 6);
        assert_eq!(payload[1], 0x00);
        assert_eq!(payload[2], 0xee);
        assert_eq!(payload[3], 0x00);
        assert_eq!(payload[4], 0xff);
        assert_eq!(payload[5], 8);
        assert_eq!(&payload[6..], &frame.data[..]);
    }

    #[test]
    fn startup_ping_decodes_to_ngt_msg_send() {
        let bytes = encode_startup_ping();
        let mut d = Ngt1Decoder::new();
        let events = d.push_bytes(&bytes);
        assert_eq!(events.len(), 1);
        match &events[0] {
            NgtEvent::Message(m) => {
                assert_eq!(m.command, NGT_MSG_SEND);
                assert_eq!(m.payload, NGT_STARTUP_SEQ);
            }
            other => panic!("got {other:?}"),
        }
    }

    #[test]
    fn n2k_msg_received_decodes_to_raw_frame() {
        // Header: prio=3, pgn=129025 (0x1F801), dst=0xFF, src=36,
        //         ts=0, dlen=8, data=[0xE6 .. 0xB3].
        let pgn = 129025u32;
        let mut payload = vec![
            3,                  // prio
            (pgn & 0xff) as u8, // pgn LE
            ((pgn >> 8) & 0xff) as u8,
            ((pgn >> 16) & 0xff) as u8,
            0xff, // dst
            36,   // src
            0,
            0,
            0,
            0, // ts (4 bytes)
            8, // dlen
        ];
        payload.extend_from_slice(&[0xe6, 0xf1, 0x3a, 0x80, 0x9c, 0xc6, 0x0d, 0xb3]);
        let frame = encode_frame(N2K_MSG_RECEIVED, &payload);

        let mut d = Ngt1Decoder::new();
        let events = d.push_bytes(&frame);
        assert_eq!(events.len(), 1);
        let m = match &events[0] {
            NgtEvent::Message(m) => m,
            other => panic!("{other:?}"),
        };
        let raw = m.to_raw_frame().expect("convert");
        assert_eq!(raw.prio, 3);
        assert_eq!(raw.pgn, 129025);
        assert_eq!(raw.src, 36);
        assert_eq!(raw.dst, 0xff);
        assert_eq!(
            &raw.data[..],
            &[0xe6, 0xf1, 0x3a, 0x80, 0x9c, 0xc6, 0x0d, 0xb3]
        );
    }

    /// EBL header record: ESC SOH 0x03 <8-byte FILETIME LE> ESC LF.
    /// FILETIME is 100-ns ticks since 1601-01-01 UTC; subtract the
    /// 1601→1970 ms offset to get Unix ms. The bytes here are from the
    /// first record of `canboat/samples/actisense1.ebl`, captured on
    /// 2025-04-25 17:05:21.993Z (Unix ms 1_745_600_721_993).
    #[test]
    fn ebl_timestamp_header_decodes() {
        let body = [0x90, 0xe3, 0xc8, 0x3a, 0x04, 0xb6, 0xdb, 0x01];
        let mut wire = vec![ESC, SOH, EBL_TIMESTAMP];
        wire.extend_from_slice(&body);
        wire.extend_from_slice(&[ESC, LF]);
        let mut d = Ngt1Decoder::with_ebl();
        let events = d.push_bytes(&wire);
        assert_eq!(events.len(), 1);
        match &events[0] {
            NgtEvent::Header(EblHeader::Timestamp(ms)) => {
                assert_eq!(*ms, 1_745_600_721_993);
            }
            other => panic!("expected Timestamp, got {other:?}"),
        }
    }

    /// ESC must NOT be treated as an escape byte in non-EBL mode — a
    /// payload byte of 0x1b in a regular NGT-1 / Yacht Device `.dle`
    /// stream is ordinary data, not a frame marker.
    #[test]
    fn esc_in_payload_is_literal_when_not_ebl() {
        let payload = vec![0x1b, 0x00, 0x1b, 0xff];
        let mut wire = Vec::new();
        encode_ngt_message(0x42, &payload, &mut wire);
        let mut d = Ngt1Decoder::new();
        let events = d.push_bytes(&wire);
        assert_eq!(events.len(), 1);
        match &events[0] {
            NgtEvent::Message(m) => assert_eq!(m.payload, payload),
            other => panic!("got {other:?}"),
        }
    }

    /// In EBL mode the same wire bytes (no DLE/ESC stuffing applied)
    /// would corrupt the frame — because ESC is now an escape byte —
    /// so the encoder couldn't be used here. Test instead that
    /// `ESC ESC` escapes to a literal 0x1b inside an EBL header.
    #[test]
    fn ebl_escapes_esc_within_header() {
        // Header carries an unrecognised type byte (so we get the
        // verbatim payload back) and a literal 0x1b in the middle.
        let mut wire = vec![ESC, SOH, 0xAA, 0x11, ESC, ESC, 0x22];
        wire.extend_from_slice(&[ESC, LF]);
        let mut d = Ngt1Decoder::with_ebl();
        let events = d.push_bytes(&wire);
        assert_eq!(events.len(), 1);
        match &events[0] {
            NgtEvent::Header(EblHeader::Unknown { kind, payload }) => {
                assert_eq!(*kind, 0xAA);
                assert_eq!(payload, &vec![0x11, ESC, 0x22]);
            }
            other => panic!("got {other:?}"),
        }
    }

    /// End-to-end: a real Actisense `.ebl` sample interleaves timestamp
    /// records with NGT-1 N2K messages. Decoding the canonical opener of
    /// `samples/actisense1.ebl` should yield exactly: timestamp, then
    /// timestamp, then an `N2K_MSG_RECEIVED` frame.
    #[test]
    fn ebl_decodes_two_timestamps_then_n2k_frame() {
        // Bytes copied from samples/actisense1.ebl, offsets 0x00..0x30:
        //   ESC SOH 03 90 e3 c8 3a 04 b6 db 01 ESC LF       (timestamp 1)
        //   ESC SOH 01 01 03 90 e3 c8 3a 04 b6 db 01 ESC LF (timestamp 2)
        //   DLE STX 93 48 04 10 10 fc 01 63 ff e5 03 …      (N2K msg, truncated)
        // For this unit test we just include both headers plus a small
        // synthetic NGT-1 frame so we don't have to ship the sample.
        let mut wire = vec![
            ESC,
            SOH,
            EBL_TIMESTAMP,
            0x90,
            0xe3,
            0xc8,
            0x3a,
            0x04,
            0xb6,
            0xdb,
            0x01,
            ESC,
            LF,
        ];
        wire.extend_from_slice(&[
            ESC,
            SOH,
            EBL_TIMESTAMP,
            0xa0,
            0xe3,
            0xc8,
            0x3a,
            0x04,
            0xb6,
            0xdb,
            0x01,
            ESC,
            LF,
        ]);
        wire.extend_from_slice(&encode_frame(N2K_MSG_RECEIVED, &[0u8; 11]));
        let mut d = Ngt1Decoder::with_ebl();
        let events = d.push_bytes(&wire);
        assert_eq!(events.len(), 3);
        assert!(matches!(
            events[0],
            NgtEvent::Header(EblHeader::Timestamp(_))
        ));
        assert!(matches!(
            events[1],
            NgtEvent::Header(EblHeader::Timestamp(_))
        ));
        assert!(matches!(events[2], NgtEvent::Message(_)));
    }
}