canboat 9.1.0

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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
// (C) 2009-2026, Kees Verruijt, Harlingen, The Netherlands.

//! Actisense NGT-1 gateway codec.
//!
//! Wraps [`crate::engine::format::ngt1`]: received bytes go through
//! `Ngt1Decoder`, frames for the bus go out as `N2K_MSG_SEND` (0x94)
//! messages. On connect the codec asks for the gateway's product
//! information, and logs it, then sets its operating mode (BEM Set
//! Operating Mode, [`Config::operating_mode`]); the gateway answers with the
//! mode in force, which is logged, and a mismatch is warned about.
//!
//! There is no keepalive. canboat C's `actisense-serial` long set the mode
//! again every 20 s, added in 2020 against NGT-1 hangs; the Actisense SDK
//! has no keepalive, and the gateway keeps the mode in non-volatile memory.
//! An NGT-1 with firmware 2.690 (2026-10) ran 15 minutes without one,
//! through 5 minutes of nothing written and stalls of up to 60 s in reading
//! with commands written meanwhile, and never hung.
//!
//! Setting the mode is not free: that NGT-1 then drops what is written to
//! it, BEM commands for 50 to 200 ms and N2K messages for 400 to 450 ms. So
//! frames for the bus that [`Codec::send`] is given in the 500 ms after
//! the mode is set come back empty, held, and go out as [`Event::Send`]
//! from [`Codec::receive`] or [`Codec::tick`] once that time is over.
//!
//! The gateway's own BEM messages come out as canboat's `Actisense: …`
//! PGNs (`0x40000` + the BEM id), as canboat C's `actisense-serial` gives
//! them. A Startup Status (the gateway restarted) makes the codec set the
//! operating mode and the transmit list again; an Error Report or a
//! Negative Ack is logged with the Actisense SDK's name for its error. Synthetic PGNs (`>= 0x40000`) are refused, matching
//! canboat's behaviour.
//!
//! Alongside the bus traffic the codec emits the synthetic `NMEA 2000
//! gateway: network status` (PGN 262400), as canboat C's `actisense-serial`
//! does. After startup it reads the gateway's Transmit PGN Enable list,
//! and with [`Config::pgn_lists`] naming Transmit PGNs brings it up to date
//! (see [`super::ngt1_tx_list`]). Without, a frame for a PGN not on it is
//! sent all the same, and dropped by the gateway, so the first of each is
//! warned about.
//!
//! A gateway that answers no BEM command within [`PROBE_AFTER_MS`] is asked
//! whether it is an Across Ocean Systems simulator ([`crate::engine::format::aos`]),
//! which speaks this framing too. One that says so is logged, nothing more
//! is waited for from it, and [`Ngt1::aos`] tells the application. One that
//! answers neither is warned about.

use std::sync::{Arc, Mutex};
use std::time::Duration;

use crate::engine::RawFrame;
use crate::engine::format::aos::{self, Answers, AosInfo};
use crate::engine::format::ikonvert::{NetworkStatus, build_network_status};
use crate::engine::format::ngt1::{
    BEM_ERROR_REPORT, BEM_NEGATIVE_ACK, BEM_OPERATING_MODE, BEM_PRODUCT_INFO, BEM_STARTUP_STATUS,
    BEM_SYSTEM_STATUS, BemResponse, Ngt1Decoder, NgtEvent, OperatingMode, ProductInfo,
    describe_error, encode_get_product_info, encode_n2k_send_frame, encode_set_operating_mode,
    startup_status,
};
use crate::engine::pgn_list::{self, PgnListStatus, PgnListSupport, PgnLists, TxGate};

use super::ngt1_tx_list::{NGT_MSG_RECEIVED, TxListRecord, TxListSync};
use super::{Codec, Event, Refused, SYNTHETIC_PGN_START};
use crate::engine::format_iso_ms;

/// The interval of the keepalive the codec no longer sends: the C
/// `actisense-serial` sets the operating mode again every 20 s.
#[deprecated(note = "the NGT-1 codec sends no keepalive; `Ngt1::keepalive` returns `None`")]
pub const KEEPALIVE_INTERVAL: Duration = Duration::from_secs(20);

/// `Actisense: System status` — `ACTISENSE_BEM + 0xf2`. The NGT-1 sends
/// it about once a second, but only when P-codes are enabled for the
/// port, so its two fields may never arrive.
const ACTISENSE_SYSTEM_STATUS_PGN: u32 = ACTISENSE_BEM_PGN + BEM_SYSTEM_STATUS as u32;

/// The canboat PGN a BEM message comes out as, less the BEM id:
/// `Actisense: …`.
const ACTISENSE_BEM_PGN: u32 = SYNTHETIC_PGN_START;

/// How often to emit the synthetic gateway network status.
const NETWORK_STATUS_INTERVAL_MS: u64 = 5_000;

/// How long after the start a gateway that has answered no BEM command is
/// asked whether it is an AOS simulator. An NGT-1 answers Product Info
/// within 20 ms.
pub const PROBE_AFTER_MS: u64 = 2_000;

/// How long the AOS simulator gets to answer; it takes about 1 ms.
const PROBE_ANSWER_MS: u64 = 3_000;

/// NGT-1 settings. `Config::default()` puts the gateway in NGT Transfer Rx
/// All Mode and leaves its lists alone.
#[derive(Debug, Clone)]
pub struct Config {
    /// The operating mode to set: [`OperatingMode::NgTransferRxAll`] (the
    /// default) forwards every received PGN,
    /// [`OperatingMode::NgTransferNormal`] only those on the gateway's
    /// Receive PGN Enable list.
    pub operating_mode: OperatingMode,
    /// PGNs the application sends and reads. The Transmit PGNs are added
    /// to the gateway's Transmit PGN Enable list when missing from it —
    /// the NGT-1 does not transmit a PGN that is not there. The Receive
    /// PGNs are not used: the gateway runs in receive-all mode.
    ///
    /// **⚠️ ONCE `pgn_lists.tx` NAMES ANY PGN, EVERY PGN NOT ON IT IS
    /// REFUSED: THE CODEC DOES NOT SEND IT.** The NGT-1 only transmits the
    /// PGNs in its list, and the codec sets that list once, at startup, so
    /// name every PGN you will send before opening the device.
    /// Network-management PGNs are always sent. With `tx` empty the
    /// gateway's list is left alone and nothing is refused.
    pub pgn_lists: PgnLists,
    /// PGNs canboat itself transmits through the gateway (a quirk's, such
    /// as `wmm`'s 127258). Once `pgn_lists.tx` names a PGN they are enabled
    /// with it and allowed; on their own they neither write the gateway's
    /// list nor make the codec refuse anything.
    pub extra_tx_pgns: Vec<u32>,
    /// What this run has learned about the gateway's transmit list. Share
    /// one across reconnects (clone the `Arc` into each session's
    /// `Config`), so a reconnect does not write again what is known.
    pub tx_list_record: Arc<Mutex<TxListRecord>>,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            operating_mode: OperatingMode::NgTransferRxAll,
            pgn_lists: PgnLists::default(),
            extra_tx_pgns: Vec::new(),
            tx_list_record: Arc::default(),
        }
    }
}

/// The Transmit PGNs to enable: the named ones and canboat's own — none
/// when nothing is named, so the gateway's list is left alone.
fn wanted_tx_pgns(named: &[u32], extra: &[u32]) -> (Vec<u32>, Vec<u32>) {
    if named.is_empty() {
        return (Vec::new(), Vec::new());
    }
    pgn_list::merge(&[], &[named, extra].concat())
}

/// How the gateway takes the `named` lists (and canboat's own `extra`
/// PGNs): a named Transmit list is written into it
/// ([`PgnListSupport::Pushed`]), otherwise its list is left
/// ([`PgnListSupport::Untouched`]); there is no known way to set what it
/// advertises as received. `dropped` names the PGNs that do not fit.
pub fn pgn_list_status(named: &PgnLists, extra: &[u32]) -> PgnListStatus {
    PgnListStatus {
        tx: if named.tx.is_empty() {
            PgnListSupport::Untouched
        } else {
            PgnListSupport::Pushed
        },
        rx: if named.rx.is_empty() {
            PgnListSupport::Untouched
        } else {
            PgnListSupport::Unsupported
        },
        dropped: wanted_tx_pgns(&named.tx, extra).1,
    }
}

/// The NGT-1 protocol; see the [module docs](self).
pub struct Ngt1 {
    inner: Ngt1Decoder,
    /// State for the synthetic network status, so a consumer sees the same
    /// per-gateway record whichever gateway is feeding it.
    net: NetworkStatusState,
    /// Brings the gateway's Transmit PGN Enable list up to date.
    tx_list: TxListSync,
    gate: Option<TxGate>,
    /// The operating mode to set.
    mode: OperatingMode,
    /// The mode the gateway last reported, to log only a change.
    reported_mode: Option<OperatingMode>,
    /// The gateway's product information as its parts arrive.
    product_info: ProductInfo,
    /// Whether the gateway has sent any BEM message.
    bem_seen: bool,
    probe: Probe,
    /// The AOS simulator's answers, out of the text between frames.
    answers: Answers,
    /// The AOS simulator this gateway turned out to be.
    aos: Option<AosInfo>,
    /// Whether frames for the bus wait for a Set Operating Mode to settle.
    settle: Settle,
    /// Frames for the bus held meanwhile, encoded.
    held: Vec<Vec<u8>>,
    /// PGNs sent that the gateway's list lacks, each warned about once.
    warned: Vec<u32>,
}

/// Asking a gateway that answers no BEM command what it is.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Probe {
    NotYet,
    Sent { at: u64 },
    Done,
}

/// How long after Set Operating Mode frames for the bus are held: an NGT-1
/// (firmware 2.690) drops everything written in the 400 to 450 ms after
/// it, N2K messages and BEM commands alike.
const MODE_SETTLE_MS: u64 = 500;

/// Holding frames for the bus after a Set Operating Mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Settle {
    /// Nothing held.
    Settled,
    /// Set Operating Mode was written by [`Codec::open`], which has no
    /// clock; the hold runs from the first time the codec is given.
    Opened,
    /// Held until then.
    Until(u64),
}

struct NetworkStatusState {
    /// When the codec first saw the time: the uptime's zero, and the
    /// wall-clock status timer's base. `None` until then.
    start_ms: Option<u64>,
    next_ms: u64,
    /// Distinct source addresses seen, for the device count.
    seen: [bool; 256],
    /// Ch1 Rx Load and Error ID, cached from the last System Status.
    /// Stay `None` — and ride the canboat sentinel — until one arrives.
    load_pct: Option<u8>,
    errors: Option<u32>,
}

impl Ngt1 {
    /// A codec with [`Config`].
    pub fn new(config: Config) -> Self {
        let (tx_pgns, dropped) = wanted_tx_pgns(&config.pgn_lists.tx, &config.extra_tx_pgns);
        if !dropped.is_empty() {
            log::warn!(
                "ngt1: not enabling transmit PGNs {dropped:?} (invalid, or the list is full)"
            );
        }
        Self {
            inner: Ngt1Decoder::new(),
            net: NetworkStatusState {
                start_ms: None,
                next_ms: 0,
                seen: [false; 256],
                load_pct: None,
                errors: None,
            },
            tx_list: TxListSync::new(tx_pgns, config.tx_list_record.clone()),
            gate: TxGate::new("ngt1", &config.pgn_lists.tx, &config.extra_tx_pgns),
            mode: config.operating_mode,
            reported_mode: None,
            product_info: ProductInfo::default(),
            bem_seen: false,
            probe: Probe::NotYet,
            answers: Answers::default(),
            aos: None,
            settle: Settle::Settled,
            held: Vec::new(),
            warned: Vec::new(),
        }
    }

    /// The AOS simulator this gateway is, once it has said so. It answers
    /// no BEM command and transmits every PGN, from the source address of
    /// an `N2K_MSG_RECEIVED` message (see
    /// [`crate::engine::format::ngt1::encode_n2k_received_frame`]), or
    /// from a fixed address it never claims.
    pub fn aos(&self) -> Option<&AosInfo> {
        self.aos.as_ref()
    }

    /// Whether the gateway has answered a BEM command, as an Actisense
    /// NGT-1 does and the AOS simulator does not.
    pub fn answers_bem(&self) -> bool {
        self.bem_seen
    }

    /// Ask a gateway that has answered no BEM command whether it is an AOS
    /// simulator, and give up on one that does not answer that either.
    fn probe(&mut self, now_ms: u64, events: &mut Vec<Event>) {
        if self.bem_seen {
            self.probe = Probe::Done;
            return;
        }
        match self.probe {
            Probe::NotYet if now_ms >= self.net.start_ms.unwrap_or(now_ms) + PROBE_AFTER_MS => {
                self.inner.collect_text();
                events.push(Event::Send(aos::GET_INFO.to_vec()));
                self.probe = Probe::Sent { at: now_ms };
            }
            Probe::Sent { at } if now_ms >= at + PROBE_ANSWER_MS => {
                log::warn!(
                    "ngt1: the gateway answers no BEM command: no product information, operating mode or transmit list"
                );
                self.probe = Probe::Done;
            }
            _ => {}
        }
    }

    /// Look for the AOS simulator's answer in the text between frames.
    fn on_text(&mut self) {
        let text = self.inner.take_text();
        if text.is_empty() {
            return;
        }
        for answer in self.answers.push(&text) {
            let Some(info) = aos::parse_get_info(&answer) else {
                continue;
            };
            if info.product_type != aos::SIMULATOR_TYPE {
                log::info!(
                    "ngt1: the gateway is an AOS product of type {}, not the simulator",
                    info.product_type
                );
                continue;
            }
            log::info!(
                "ngt1: gateway AOS NMEA 2000/0183 Simulator, serial {}, hardware {}, firmware {}; it takes no BEM command and transmits every PGN",
                info.serial,
                info.hardware_version,
                info.firmware_version
            );
            self.tx_list.abandon();
            self.gate = None;
            self.aos = Some(info);
            self.probe = Probe::Done;
        }
    }

    /// Say so, once per PGN, when the gateway's list lacks a PGN sent: the
    /// gateway drops it without a word. Only without a transmit list of
    /// our own ([`Config::pgn_lists`]): with one, the gate refuses what is
    /// not on it, and the sync has put on it what is.
    fn warn_if_dropped(&mut self, pgn: u32) {
        if self.gate.is_some() || self.warned.contains(&pgn) {
            return;
        }
        let Some(complete) = self.tx_list.lacks(pgn) else {
            return;
        };
        self.warned.push(pgn);
        let fix = "name it, and every PGN you send, with --tx-pgn (pgn_lists.tx in the library)";
        if complete {
            log::warn!(
                "ngt1: PGN {pgn} is not on the NGT-1's transmit list, which drops it; {fix}"
            );
        } else {
            log::warn!(
                "ngt1: PGN {pgn} is not on the NGT-1's transmit list as read (Format 1, which \
                 can miss some), so it may be dropped; {fix}"
            );
        }
    }

    /// Start or end holding frames for the bus, as the time goes by.
    fn settle(&mut self, now_ms: u64, events: &mut Vec<Event>) {
        match self.settle {
            Settle::Opened => self.settle = Settle::Until(now_ms + MODE_SETTLE_MS),
            Settle::Until(until) if now_ms >= until => {
                self.settle = Settle::Settled;
                if !self.held.is_empty() {
                    log::debug!(
                        "ngt1: sending {} frames held after Set Operating Mode",
                        self.held.len()
                    );
                }
                send_all(events, std::mem::take(&mut self.held));
            }
            _ => {}
        }
    }

    /// Act on a BEM message from the gateway: pass it on as an `Actisense:
    /// …` PGN, and log or answer it.
    fn on_bem(&mut self, payload: &[u8], now_ms: u64, events: &mut Vec<Event>) {
        let Some(response) = BemResponse::parse(payload) else {
            return;
        };
        match response.bem {
            BEM_OPERATING_MODE => self.on_operating_mode(&response),
            BEM_PRODUCT_INFO => {
                if self.product_info.add(&response) {
                    let info = &self.product_info;
                    log::info!(
                        "ngt1: gateway {} serial {}, software {}, hardware {}, product code {}, NMEA 2000 {}.{:03}",
                        info.model,
                        info.serial_number,
                        info.software_version,
                        info.hardware_version,
                        info.product_code,
                        info.nmea2000_version / 1000,
                        info.nmea2000_version % 1000
                    );
                    if let Some(firmware) = info.firmware() {
                        self.tx_list.set_firmware(firmware);
                    }
                }
            }
            BEM_STARTUP_STATUS => {
                if let Some((firmware, reset)) = startup_status(&response) {
                    log::warn!(
                        "ngt1: the gateway restarted (firmware {}.{:03}, reset status {reset:#x}); setting it up again",
                        firmware / 1000,
                        firmware % 1000
                    );
                }
                self.reported_mode = None;
                self.product_info = ProductInfo::default();
                self.tx_list.restart();
                // Product Info first, as in `open`.
                events.push(Event::Send(encode_get_product_info()));
                events.push(Event::Send(encode_set_operating_mode(self.mode)));
                // Frames already held keep their place, ahead of new ones.
                self.settle = Settle::Until(now_ms + MODE_SETTLE_MS);
            }
            BEM_ERROR_REPORT => log::warn!(
                "ngt1: the gateway reports error {}",
                describe_error(response.error)
            ),
            BEM_NEGATIVE_ACK => log::warn!(
                "ngt1: the gateway refused a command: error {}",
                describe_error(response.error)
            ),
            _ => {}
        }
        let frame = RawFrame::new(
            Some(format_iso_ms(now_ms)),
            0,
            ACTISENSE_BEM_PGN + u32::from(response.bem),
            0,
            0,
            payload[1..].iter().copied(),
        );
        self.note_frame(frame, now_ms, events);
    }

    /// Check the gateway's answer to Set Operating Mode: log the mode it
    /// reports when that changes, and warn when it is not the one set.
    fn on_operating_mode(&mut self, response: &BemResponse<'_>) {
        let Some(mode) = response.operating_mode() else {
            return;
        };
        if response.error != 0 {
            log::warn!(
                "ngt1: the gateway refused {} (error {}); it stays in {mode}",
                self.mode,
                describe_error(response.error)
            );
        }
        if self.reported_mode == Some(mode) {
            return;
        }
        self.reported_mode = Some(mode);
        if mode == self.mode {
            log::info!(
                "ngt1: gateway model {:#06x}, serial {}, in {mode}",
                response.model_id,
                response.serial
            );
        } else {
            log::warn!(
                "ngt1: gateway model {:#06x}, serial {}, is in {mode}, not {}",
                response.model_id,
                response.serial,
                self.mode
            );
        }
    }

    /// Start the clock on the first time seen.
    fn started(&mut self, now_ms: u64) {
        if self.net.start_ms.is_none() {
            self.net.start_ms = Some(now_ms);
            self.net.next_ms = now_ms + NETWORK_STATUS_INTERVAL_MS;
        }
    }

    /// Per-frame bookkeeping for the synthetic network status: note the
    /// source address, and harvest the two fields `Actisense: System
    /// status` carries. Pushes the frame itself, plus a fresh status
    /// record when that message is what arrived.
    fn note_frame(&mut self, frame: RawFrame, now_ms: u64, events: &mut Vec<Event>) {
        // The gateway's own messages come from no bus device.
        if frame.pgn < SYNTHETIC_PGN_START {
            self.net.seen[frame.src as usize] = true;
        }
        // Frame data drops the subcommand byte, so the C's msg[8..11]
        // and msg[14] are data[7..10] and data[13] here.
        if frame.pgn == ACTISENSE_SYSTEM_STATUS_PGN {
            if frame.data.len() >= 11 {
                self.net.errors = Some(u32::from_le_bytes([
                    frame.data[7],
                    frame.data[8],
                    frame.data[9],
                    frame.data[10],
                ]));
            }
            if frame.data.len() >= 14 {
                self.net.load_pct = Some(frame.data[13]);
            }
            events.push(Event::Frame(frame));
            // A System Status arrival is the authoritative trigger for
            // a fresh record, as it is in the C — which also makes this
            // work during a file replay, where the wall-clock fallback
            // never fires.
            self.emit_network_status(now_ms, events);
            return;
        }
        events.push(Event::Frame(frame));
    }

    /// Emit the synthetic PGN 262400 record and re-arm the timer.
    fn emit_network_status(&mut self, now_ms: u64, events: &mut Vec<Event>) {
        self.started(now_ms);
        let start_ms = self.net.start_ms.unwrap_or(now_ms);
        let device_count = self.net.seen.iter().filter(|s| **s).count().min(255) as u8;
        let frame = build_network_status(
            NetworkStatus {
                load_pct: self.net.load_pct,
                errors: self.net.errors,
                device_count: Some(device_count),
                uptime_s: Some(now_ms.saturating_sub(start_ms).div_euclid(1000) as u32),
                // The NGT-1 reports neither its own address nor any
                // rejected-TX count, so both stay at the canboat
                // "no data" sentinel.
                gateway_addr: 0xff,
                rejected_tx: None,
            },
            Some(format_iso_ms(now_ms)),
        );
        events.push(Event::Frame(frame));
        self.net.next_ms = now_ms + NETWORK_STATUS_INTERVAL_MS;
    }
}

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

/// Queue each command for the gateway.
fn send_all(events: &mut Vec<Event>, commands: Vec<Vec<u8>>) {
    events.extend(commands.into_iter().map(Event::Send));
}

impl Codec for Ngt1 {
    /// Product Info goes first: an NGT-1 (firmware 2.690) ignores what
    /// follows Set Operating Mode for a while. For the same reason frames
    /// for the bus are held until [`MODE_SETTLE_MS`] after the first
    /// [`Codec::receive`] or [`Codec::tick`].
    fn open(&mut self) -> Vec<u8> {
        self.settle = Settle::Opened;
        let mut out = encode_get_product_info();
        out.extend(encode_set_operating_mode(self.mode));
        out
    }

    fn receive(&mut self, bytes: &[u8], now_ms: u64, events: &mut Vec<Event>) {
        self.tick(now_ms, events);
        for ev in self.inner.push_bytes(bytes) {
            match ev {
                NgtEvent::Message(msg) if msg.command == NGT_MSG_RECEIVED => {
                    self.bem_seen = true;
                    send_all(events, self.tx_list.on_message(&msg.payload, now_ms));
                    self.on_bem(&msg.payload, now_ms, events);
                }
                NgtEvent::Message(msg) => {
                    if let Some(mut frame) = msg.to_raw_frame() {
                        // The message carries the NGT-1's ms-since-power-up
                        // clock; a live stream wants wall-clock time, as
                        // canboat C's actisense-serial and the iKonvert
                        // codec give it.
                        frame.timestamp = Some(format_iso_ms(now_ms));
                        self.note_frame(frame, now_ms, events);
                    }
                }
                NgtEvent::Error(e) => events.push(Event::Error(e.to_string())),
            }
        }
        if self.probe != Probe::Done {
            self.on_text();
        }
    }

    /// The codec's deadlines, run on every receive too, so they advance
    /// on a quiet bus.
    fn tick(&mut self, now_ms: u64, events: &mut Vec<Event>) {
        self.started(now_ms);
        self.settle(now_ms, events);
        // Wall-clock fallback, for a gateway whose P-codes are off and
        // so never sends a System Status to trigger on.
        if now_ms >= self.net.next_ms {
            self.emit_network_status(now_ms, events);
        }
        if self.probe != Probe::Done {
            self.probe(now_ms, events);
        }
        if !self.tx_list.is_done() {
            send_all(events, self.tx_list.on_tick(now_ms));
        }
    }

    fn send(&mut self, frame: &RawFrame) -> Result<Vec<u8>, Refused> {
        if frame.pgn >= SYNTHETIC_PGN_START {
            log::debug!("ngt1: skipping synthetic PGN {}", frame.pgn);
            return Err(Refused::Synthetic);
        }
        if let Some(gate) = &self.gate
            && !gate.allows(frame.pgn)
        {
            return Err(Refused::NotOnTxList);
        }
        // Six header bytes + data must fit the one-byte message length.
        if 6 + frame.data.len() > u8::MAX as usize {
            return Err(Refused::TooLarge);
        }
        self.warn_if_dropped(frame.pgn);
        let bytes = encode_n2k_send_frame(frame);
        if self.settle == Settle::Settled {
            Ok(bytes)
        } else {
            // Written now it would be lost; `tick` sends it later.
            self.held.push(bytes);
            Ok(Vec::new())
        }
    }

    /// None: the NGT-1 needs no keepalive (see the module documentation).
    fn keepalive(&self) -> Option<(Duration, Vec<u8>)> {
        None
    }
}

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

    const NOW: u64 = 1_780_082_164_826;

    fn frame(pgn: u32) -> RawFrame {
        RawFrame::new(None, 6, pgn, 0, 21, vec![0x14, 0xf0, 0x01])
    }

    fn sent(events: &[Event]) -> Vec<&Vec<u8>> {
        events
            .iter()
            .filter_map(|e| match e {
                Event::Send(b) => Some(b),
                _ => None,
            })
            .collect()
    }

    /// Without `open` nothing was set, so nothing is held.
    #[test]
    fn frames_go_straight_out_without_open() {
        let mut d = Ngt1::default();
        assert_eq!(
            d.send(&frame(59904)),
            Ok(encode_n2k_send_frame(&frame(59904)))
        );
    }

    /// Frames given before and in the 500 ms after the first tick are
    /// held, then sent in order.
    #[test]
    fn frames_after_open_are_held_until_the_mode_settles() {
        let mut d = Ngt1::default();
        d.open();
        assert_eq!(d.send(&frame(59904)), Ok(Vec::new()));
        let mut events = Vec::new();
        d.tick(NOW, &mut events);
        assert!(sent(&events).is_empty());
        assert_eq!(d.send(&frame(126208)), Ok(Vec::new()));
        d.tick(NOW + MODE_SETTLE_MS - 1, &mut events);
        assert!(sent(&events).is_empty(), "still settling");
        d.tick(NOW + MODE_SETTLE_MS, &mut events);
        assert_eq!(
            sent(&events),
            [
                &encode_n2k_send_frame(&frame(59904)),
                &encode_n2k_send_frame(&frame(126208))
            ]
        );
        assert_eq!(
            d.send(&frame(59904)),
            Ok(encode_n2k_send_frame(&frame(59904)))
        );
    }

    /// A refused frame is refused at once, not held.
    #[test]
    fn a_refused_frame_is_not_held() {
        let mut d = Ngt1::default();
        d.open();
        assert_eq!(d.send(&frame(SYNTHETIC_PGN_START)), Err(Refused::Synthetic));
        assert!(d.held.is_empty());
    }

    /// The gateway restarting sets the mode again, and holds frames again.
    #[test]
    fn a_startup_status_holds_frames_again() {
        let mut d = Ngt1::default();
        let mut payload = vec![BEM_STARTUP_STATUS, 1, 0x0e, 0];
        payload.extend_from_slice(&[0; 8]);
        payload.extend_from_slice(&[0x8e, 0x0a, 0]);
        let mut wire = Vec::new();
        crate::engine::format::ngt1::encode_ngt_message(NGT_MSG_RECEIVED, &payload, &mut wire);
        let mut events = Vec::new();
        d.receive(&wire, NOW, &mut events);
        assert_eq!(d.send(&frame(59904)), Ok(Vec::new()));
        events.clear();
        d.tick(NOW + MODE_SETTLE_MS, &mut events);
        assert_eq!(sent(&events), [&encode_n2k_send_frame(&frame(59904))]);
    }
}

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

    fn frame(pgn: u32) -> RawFrame {
        RawFrame::new(None, 2, pgn, 0, 255, vec![1, 2, 3])
    }

    /// A PGN the gateway's list lacks is still sent, and warned about
    /// once; one on it, or sent before the list is read, is not.
    #[test]
    fn a_pgn_the_gateway_drops_is_warned_about_once() {
        let mut d = Ngt1::new(Config::default());
        assert!(d.send(&frame(127250)).is_ok());
        assert!(d.warned.is_empty(), "the list is not read yet");
        d.tx_list.read_for_test(&[59904, 127508], true);
        assert!(d.send(&frame(127508)).is_ok());
        assert!(d.send(&frame(127250)).is_ok());
        assert!(d.send(&frame(127250)).is_ok());
        assert!(d.send(&frame(130306)).is_ok());
        assert_eq!(d.warned, [127250, 130306]);
    }

    /// With a transmit list of our own the gate refuses what is not on it,
    /// and the sync put the rest on the gateway's: no warning.
    #[test]
    fn a_named_transmit_list_warns_nothing() {
        let mut d = Ngt1::new(Config {
            pgn_lists: PgnLists {
                tx: vec![127250],
                rx: Vec::new(),
            },
            ..Default::default()
        });
        d.tx_list.read_for_test(&[59904], true);
        assert!(d.send(&frame(127250)).is_ok());
        assert_eq!(d.send(&frame(130306)), Err(Refused::NotOnTxList));
        assert!(d.warned.is_empty());
    }
}

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

    const NOW: u64 = 1_780_082_164_826;

    /// The NGT-1 codec must produce the same synthetic PGN 262400
    /// record the iKonvert and SocketCAN drivers do — canboat C's
    /// `actisense-serial` emits it, and a consumer should see one
    /// uniform per-gateway status whichever gateway is feeding it.
    #[test]
    fn system_status_yields_a_network_status_record() {
        let mut d = Ngt1::default();
        let mut events = Vec::new();

        // `Actisense: System status` (ACTISENSE_BEM + 0xf2) as the
        // decoder hands it over: the subcommand byte is stripped, so
        // Error ID sits at data[7..10] and Ch1 Rx Load at data[13].
        let mut data = vec![0u8; 16];
        data[7..11].copy_from_slice(&1234u32.to_le_bytes());
        data[13] = 42; // 42 % bus load
        d.net.seen[3] = true;
        d.net.seen[9] = true;
        d.note_frame(
            RawFrame {
                timestamp: None,
                prio: 0,
                pgn: ACTISENSE_SYSTEM_STATUS_PGN,
                src: 0,
                dst: 0,
                data: data.into(),
            },
            NOW,
            &mut events,
        );

        let status = events
            .iter()
            .filter_map(|e| match e {
                Event::Frame(f) if f.pgn == 0x40100 => Some(f),
                _ => None,
            })
            .next()
            .expect("a network status frame");
        assert_eq!(status.data.len(), 15);
        assert_eq!(status.data[0], 42, "Ch1 Rx Load");
        assert_eq!(
            u32::from_le_bytes([
                status.data[1],
                status.data[2],
                status.data[3],
                status.data[4]
            ]),
            1234,
            "Error ID"
        );
        // src 3 and 9 seen by now; the gateway's own message is no device.
        assert_eq!(status.data[5], 2, "device count");
        // The NGT-1 knows neither of these.
        assert_eq!(status.data[10], 0xff, "gateway address sentinel");
        assert_eq!(&status.data[11..15], &[0xff; 4], "rejected TX sentinel");
        assert_eq!(
            status.timestamp.as_deref(),
            Some("2026-05-29T19:16:04.826Z")
        );
    }

    /// The status timer and the uptime run on the caller's clock.
    #[test]
    fn network_status_runs_on_the_callers_clock() {
        let mut d = Ngt1::default();
        let mut events = Vec::new();
        // The question to a gateway that answers no BEM command aside.
        let frames = |events: &mut Vec<Event>| {
            events.retain(|e| matches!(e, Event::Frame(_)));
            events.len()
        };
        d.tick(NOW, &mut events);
        assert_eq!(frames(&mut events), 0, "the first status is 5 s out");
        d.tick(NOW + 4_999, &mut events);
        assert_eq!(frames(&mut events), 0);
        d.tick(NOW + 7_000, &mut events);
        frames(&mut events);
        let [Event::Frame(f)] = &events[..] else {
            panic!("expected one status frame, got {events:?}")
        };
        assert_eq!(f.pgn, 0x40100);
        assert_eq!(
            u32::from_le_bytes([f.data[6], f.data[7], f.data[8], f.data[9]]),
            7,
            "uptime in seconds"
        );
    }

    /// A received bus frame is stamped with the caller's time, not the
    /// NGT-1's ms-since-power-up clock that rides in the message.
    #[test]
    fn received_frames_carry_the_callers_time() {
        let frame = RawFrame::new(
            None,
            2,
            127250,
            52,
            255,
            [0xff, 0xac, 0xc9, 0xff, 0x7f, 0xff, 0x7f, 0xfd],
        );
        let wire =
            crate::engine::format::ngt1::encode_n2k_received_frame(&frame, 148).expect("fits");
        let mut d = Ngt1::default();
        let mut events = Vec::new();
        d.receive(&wire, NOW, &mut events);
        let [Event::Frame(f)] = &events[..] else {
            panic!("expected one frame, got {events:?}")
        };
        assert_eq!(f.pgn, 127250);
        assert_eq!(f.timestamp.as_deref(), Some("2026-05-29T19:16:04.826Z"));
    }

    /// The gateway's own answers (`NGT_MSG_RECEIVED`) feed the transmit
    /// list sync, and come out as `Actisense: …` PGNs, never as bus frames.
    #[test]
    fn gateway_answers_are_actisense_pgns() {
        let mut d = Ngt1::new(Config {
            pgn_lists: PgnLists {
                tx: vec![127508],
                rx: vec![],
            },
            ..Default::default()
        });
        let mut payload = vec![0x11, 1, 0x0e, 0];
        payload.extend_from_slice(&[0; 8]);
        payload.extend_from_slice(&[2, 0]);
        let mut wire = Vec::new();
        crate::engine::format::ngt1::encode_ngt_message(NGT_MSG_RECEIVED, &payload, &mut wire);
        let mut events = Vec::new();
        d.receive(&wire, NOW, &mut events);
        let pgns: Vec<u32> = events
            .iter()
            .filter_map(|e| match e {
                Event::Frame(f) => Some(f.pgn),
                _ => None,
            })
            .collect();
        assert_eq!(pgns, [262161], "Actisense: Operating mode");
        assert!(!d.tx_list.is_done(), "startup confirmed, list read pending");
    }

    /// canboat's own PGNs alone (the wmm quirk) do not write the gateway's
    /// list, and a status says so.
    #[test]
    fn extra_pgns_alone_leave_the_list_alone() {
        let d = Ngt1::new(Config {
            extra_tx_pgns: vec![127258],
            ..Default::default()
        });
        assert!(
            d.tx_list.lacks(127258).is_none(),
            "nothing written, nor read yet"
        );
        let status = pgn_list_status(&PgnLists::default(), &[127258]);
        assert_eq!(status.tx, PgnListSupport::Untouched);
        let status = pgn_list_status(
            &PgnLists {
                tx: vec![],
                rx: vec![129025],
            },
            &[],
        );
        assert_eq!(status.tx, PgnListSupport::Untouched);
        assert_eq!(status.rx, PgnListSupport::Unsupported);
    }

    /// With a named transmit list, only its PGNs, canboat's own and the
    /// network-management PGNs go out; with only canboat's own, nothing is
    /// refused.
    #[test]
    fn a_named_transmit_list_refuses_the_rest() {
        let frame = |pgn| RawFrame {
            timestamp: None,
            prio: 6,
            pgn,
            src: 0,
            dst: 255,
            data: vec![0; 8].into(),
        };
        let mut strict = Ngt1::new(Config {
            pgn_lists: PgnLists {
                tx: vec![127508],
                rx: vec![],
            },
            extra_tx_pgns: vec![127258],
            ..Default::default()
        });
        for pgn in [127508, 127258, 59904] {
            assert!(strict.send(&frame(pgn)).is_ok(), "{pgn}");
        }
        assert_eq!(strict.send(&frame(127506)), Err(Refused::NotOnTxList));
        assert_eq!(strict.send(&frame(0x40100)), Err(Refused::Synthetic));
        let mut long = frame(127508);
        long.data = std::iter::repeat_n(0, 250).collect();
        assert_eq!(strict.send(&long), Err(Refused::TooLarge));
        long.data.pop();
        assert!(strict.send(&long).is_ok(), "249 bytes of data fit");
        let mut open = Ngt1::new(Config {
            extra_tx_pgns: vec![127258],
            ..Default::default()
        });
        assert!(open.send(&frame(127506)).is_ok());
    }

    /// With no System Status ever seen — P-codes off — the two fields
    /// it carries must ride the sentinel rather than read as zero.
    #[test]
    fn without_system_status_load_and_errors_are_sentinel() {
        let mut d = Ngt1::default();
        let mut events = Vec::new();
        d.emit_network_status(NOW, &mut events);
        let Event::Frame(f) = &events[0] else {
            panic!("expected a frame")
        };
        assert_eq!(f.data[0], 0xff, "load sentinel");
        assert_eq!(&f.data[1..5], &[0xff; 4], "errors sentinel");
    }
}

#[cfg(test)]
mod operating_mode_tests {
    use super::*;
    use crate::engine::format::ngt1::{NGT_MSG_SEND, encode_ngt_message};

    /// The gateway's answer to Operating Mode, reporting `mode`.
    fn answer(mode: u16, error: u32) -> Vec<u8> {
        let mut payload = vec![BEM_OPERATING_MODE, 0, 0x0e, 0x00];
        payload.extend_from_slice(&1234u32.to_le_bytes());
        payload.extend_from_slice(&error.to_le_bytes());
        payload.extend_from_slice(&mode.to_le_bytes());
        let mut wire = Vec::new();
        encode_ngt_message(NGT_MSG_RECEIVED, &payload, &mut wire);
        wire
    }

    #[test]
    fn open_sets_the_configured_mode_and_nothing_keeps_it_alive() {
        let mut d = Ngt1::default();
        let mut open = encode_get_product_info();
        open.extend(encode_set_operating_mode(OperatingMode::NgTransferRxAll));
        assert_eq!(d.open(), open);

        let mut d = Ngt1::new(Config {
            operating_mode: OperatingMode::NgTransferNormal,
            ..Config::default()
        });
        let mut wire = Vec::new();
        encode_ngt_message(NGT_MSG_SEND, &[BEM_OPERATING_MODE, 1, 0], &mut wire);
        assert!(d.open().ends_with(&wire));
        assert_eq!(d.keepalive(), None);
    }

    #[test]
    fn the_reported_mode_is_noted_and_never_a_frame() {
        let mut d = Ngt1::default();
        let mut events = Vec::new();
        d.receive(&answer(2, 0), NOW, &mut events);
        assert_eq!(d.reported_mode, Some(OperatingMode::NgTransferRxAll));
        d.receive(&answer(1, 0x8000_0001), NOW, &mut events);
        assert_eq!(d.reported_mode, Some(OperatingMode::NgTransferNormal));
        assert!(
            !events
                .iter()
                .any(|e| matches!(e, Event::Frame(f) if f.pgn < SYNTHETIC_PGN_START)),
            "{events:?}"
        );
    }

    /// A BEM message from the gateway: `bem`, part `sequence`, `data`.
    fn bem(bem: u8, sequence: u8, error: i32, data: &[u8]) -> Vec<u8> {
        let mut payload = vec![bem, sequence, 0x0e, 0x00];
        payload.extend_from_slice(&1234u32.to_le_bytes());
        payload.extend_from_slice(&error.to_le_bytes());
        payload.extend_from_slice(data);
        let mut wire = Vec::new();
        encode_ngt_message(NGT_MSG_RECEIVED, &payload, &mut wire);
        wire
    }

    fn padded(text: &str) -> Vec<u8> {
        let mut v = text.as_bytes().to_vec();
        v.resize(32, 0xff);
        v
    }

    /// Before, every BEM message was dropped, so a live NGT-1's System
    /// Status never reached the network status.
    #[test]
    fn a_live_system_status_reaches_the_network_status() {
        let mut d = Ngt1::default();
        let mut data = vec![0u8; 4];
        data[2] = 37; // Ch1 Rx Load, after channel count and bandwidth
        let mut events = Vec::new();
        d.receive(&bem(BEM_SYSTEM_STATUS, 0, 0, &data), NOW, &mut events);
        assert_eq!(d.net.load_pct, Some(37));
        assert!(
            events
                .iter()
                .any(|e| matches!(e, Event::Frame(f) if f.pgn == 262386))
        );
        assert!(
            events
                .iter()
                .any(|e| matches!(e, Event::Frame(f) if f.pgn == 0x40100))
        );
    }

    #[test]
    fn product_info_in_five_parts() {
        let mut d = Ngt1::default();
        let mut events = Vec::new();
        let mut main = 2100u16.to_le_bytes().to_vec();
        main.extend_from_slice(&1234u16.to_le_bytes());
        main.extend_from_slice(&[0, 1]);
        d.receive(&bem(BEM_PRODUCT_INFO, 1, 0, &main), NOW, &mut events);
        for (part, text) in [(2, "NGT-1"), (3, "2.190"), (4, "Rev B"), (5, "110763")] {
            d.receive(
                &bem(BEM_PRODUCT_INFO, part, 0, &padded(text)),
                NOW,
                &mut events,
            );
        }
        assert_eq!(d.product_info.model, "NGT-1");
        assert_eq!(d.product_info.software_version, "2.190");
        assert_eq!(d.product_info.serial_number, "110763");
        assert_eq!(d.product_info.nmea2000_version, 2100);
        let mut probe = d.product_info.clone();
        let header = [BEM_PRODUCT_INFO, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
        let mut payload = header.to_vec();
        payload.extend(padded("x"));
        assert!(
            probe.add(&BemResponse::parse(&payload).unwrap()),
            "complete"
        );
    }

    #[test]
    fn product_info_in_one_message() {
        let mut data = 17u32.to_le_bytes().to_vec();
        data.extend_from_slice(&2100u16.to_le_bytes());
        data.extend_from_slice(&1234u16.to_le_bytes());
        for text in ["W2K-1", "1.010", "B", "123456"] {
            data.extend(padded(text));
        }
        data.extend_from_slice(&[0, 2]);
        let mut d = Ngt1::default();
        let mut events = Vec::new();
        d.receive(&bem(BEM_PRODUCT_INFO, 6, 0, &data), NOW, &mut events);
        assert_eq!(d.product_info.model, "W2K-1");
        assert_eq!(d.product_info.hardware_version, "B");
        assert_eq!(d.product_info.product_code, 1234);
    }

    /// A gateway that restarted is set up again: the operating mode, the
    /// product information and the transmit list.
    #[test]
    fn a_startup_status_sets_the_gateway_up_again() {
        let mut d = Ngt1::new(Config {
            pgn_lists: PgnLists {
                tx: vec![127508],
                rx: vec![],
            },
            ..Default::default()
        });
        d.tx_list.on_message(&[BEM_OPERATING_MODE, 1], NOW);
        let mut events = Vec::new();
        d.receive(
            &bem(BEM_STARTUP_STATUS, 0, 0, &[0x8e, 0x08, 0]),
            NOW,
            &mut events,
        );
        let sent: Vec<&Vec<u8>> = events
            .iter()
            .filter_map(|e| match e {
                Event::Send(b) => Some(b),
                _ => None,
            })
            .collect();
        assert_eq!(
            sent,
            [
                &encode_get_product_info(),
                &encode_set_operating_mode(OperatingMode::NgTransferRxAll)
            ]
        );
        assert!(
            events
                .iter()
                .any(|e| matches!(e, Event::Frame(f) if f.pgn == 262384))
        );
        assert!(!d.tx_list.is_done());
    }

    #[test]
    fn errors_have_the_sdk_names() {
        assert_eq!(describe_error(-1158), "-1158 (command timeout)");
        assert_eq!(describe_error(-697), "-697");
        let mut d = Ngt1::default();
        let mut events = Vec::new();
        d.receive(
            &bem(BEM_NEGATIVE_ACK, 0, -1158, &[1, 0, 0, 0]),
            NOW,
            &mut events,
        );
        d.receive(
            &bem(BEM_ERROR_REPORT, 0, -1497, &[4, 1, 0, 0, 0]),
            NOW,
            &mut events,
        );
        let pgns: Vec<u32> = events
            .iter()
            .filter_map(|e| match e {
                Event::Frame(f) => Some(f.pgn),
                _ => None,
            })
            .collect();
        assert_eq!(pgns, [262388, 262385]);
    }

    const NOW: u64 = 1_780_082_164_826;
}

#[cfg(test)]
mod aos_probe_tests {
    use super::*;
    use crate::engine::format::ngt1::{
        NGT_MSG_RECEIVED as BEM_RECEIVED, encode_n2k_received_frame, encode_ngt_message,
    };

    const NOW: u64 = 1_780_082_164_826;

    /// The start of a real answer, with the fields the codec reads.
    const ANSWER: &[u8] = b"{\r\n  \"cmdr\": \"GetInfo\",\r\n  \"Serial\": {\r\n    \"Type\": 24,\r\n    \"Sno\": 2154,\r\n    \"HwVersion\": \"2.0\",\r\n    \"FwVersion\": \"1.6.3.843\"\r\n  }\r\n}\r\n";

    fn probes(events: &[Event]) -> usize {
        events
            .iter()
            .filter(|e| matches!(e, Event::Send(b) if b == aos::GET_INFO))
            .count()
    }

    fn named_tx() -> Ngt1 {
        Ngt1::new(Config {
            pgn_lists: PgnLists {
                tx: vec![127508],
                rx: vec![],
            },
            ..Config::default()
        })
    }

    /// A gateway that answers no BEM command is asked once; the AOS
    /// simulator answers between frames, and then nothing is waited for.
    #[test]
    fn a_silent_gateway_is_asked_and_the_simulator_recognised() {
        let mut d = named_tx();
        let mut events = Vec::new();
        d.tick(NOW, &mut events);
        d.tick(NOW + PROBE_AFTER_MS - 1, &mut events);
        assert_eq!(probes(&events), 0);
        d.tick(NOW + PROBE_AFTER_MS, &mut events);
        d.tick(NOW + PROBE_AFTER_MS + 10, &mut events);
        assert_eq!(probes(&events), 1);
        assert!(d.aos().is_none());

        // A frame, the answer split in two, a frame.
        let frame = RawFrame::new(None, 2, 129025, 21, 255, [1, 2, 3, 4, 5, 6, 7, 8]);
        let wire = encode_n2k_received_frame(&frame, 1).unwrap();
        let mut stream = wire.clone();
        stream.extend_from_slice(ANSWER);
        stream.extend_from_slice(&wire);
        let (a, b) = stream.split_at(wire.len() + 30);
        events.clear();
        d.receive(a, NOW + PROBE_AFTER_MS + 20, &mut events);
        d.receive(b, NOW + PROBE_AFTER_MS + 21, &mut events);
        let info = d.aos().expect("recognised");
        assert_eq!(info.serial, 2154);
        assert_eq!(info.firmware_version, "1.6.3.843");
        assert_eq!(
            events
                .iter()
                .filter(|e| matches!(e, Event::Frame(f) if f.pgn == 129025))
                .count(),
            2
        );
        // It sends every PGN, so none is refused.
        assert!(
            d.send(&RawFrame::new(None, 6, 130306, 0, 255, [0; 8]))
                .is_ok()
        );
        assert!(d.tx_list.is_done());
    }

    /// An NGT-1 answers Product Info at once, and is never asked.
    #[test]
    fn a_gateway_that_answers_bem_is_not_asked() {
        let mut d = Ngt1::default();
        let mut payload = vec![BEM_PRODUCT_INFO, 0, 0x0e, 0x00];
        payload.extend_from_slice(&[0; 8]);
        let mut wire = Vec::new();
        encode_ngt_message(BEM_RECEIVED, &payload, &mut wire);
        let mut events = Vec::new();
        d.receive(&wire, NOW, &mut events);
        d.tick(NOW + 10 * PROBE_AFTER_MS, &mut events);
        assert_eq!(probes(&events), 0);
        assert_eq!(d.probe, Probe::Done);
    }

    /// Another AOS product answers too, but is not the simulator: nothing
    /// changes, and it is given up on as a gateway that does not answer.
    #[test]
    fn another_aos_product_is_not_the_simulator() {
        let mut d = named_tx();
        let mut events = Vec::new();
        d.tick(NOW, &mut events);
        d.tick(NOW + PROBE_AFTER_MS, &mut events);
        let other = String::from_utf8_lossy(ANSWER).replace("\"Type\": 24", "\"Type\": 7");
        d.receive(other.as_bytes(), NOW + PROBE_AFTER_MS + 20, &mut events);
        assert!(d.aos().is_none());
        assert!(d.gate.is_some());
        assert!(!d.tx_list.is_done());
        d.tick(NOW + PROBE_AFTER_MS + PROBE_ANSWER_MS, &mut events);
        assert_eq!(d.probe, Probe::Done);
        assert!(d.aos().is_none());
    }

    /// No answer to the question either: given up on, and asked once only.
    #[test]
    fn a_gateway_that_answers_nothing_is_given_up_on() {
        let mut d = Ngt1::default();
        let mut events = Vec::new();
        d.tick(NOW, &mut events);
        d.tick(NOW + PROBE_AFTER_MS, &mut events);
        d.tick(NOW + PROBE_AFTER_MS + PROBE_ANSWER_MS, &mut events);
        d.tick(NOW + 100 * PROBE_AFTER_MS, &mut events);
        assert_eq!(probes(&events), 1);
        assert_eq!(d.probe, Probe::Done);
        assert!(d.aos().is_none());
    }
}