rax25 0.1.22

AX.25 connected mode implementation
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
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
//! State machine code for AX.25
//!
//! The state machine is documented in https://www.tapr.org/pdf/AX25.2.2.pdf,
//! but it has a few bugs. They're pointed out in the code as they are
//! encountered.
//!
//! There's also the 2017 version, but it quite possibly added more bugs than it
//! fixed:
//! https://wiki.oarc.uk/_media/packet:ax25.2.2.10.pdf
//!
//! All page numbers, unless otherwise specified, are for the 1998 PDF.
//!
//! There's also isomer's useful notes at the top of
//! https://github.com/isomer/ax25embed/blob/main/ax25/ax25_dl.c
use std::collections::VecDeque;

use anyhow::Result;
use log::{debug, error, warn};

use crate::{
    Addr, Disc, Dm, Frmr, Iframe, Packet, PacketType, Rej, Rnr, Rr, Sabm, Sabme, Srej, Test, Ua,
    Ui, Xid,
};

/// Incoming events to the state machine.
///
/// An incoming event is an incoming packet, or a command from the application,
/// like "connect", or "send this data".
#[derive(Debug, PartialEq)]
pub enum Event {
    Connect { addr: Addr, ext: bool },
    Disconnect,
    Data(Vec<u8>),
    T1,
    T3,
    Sabm(Sabm, /* peer */ Addr),
    Sabme(Sabme, /* peer */ Addr),
    Dm(Dm),
    Rr(Rr, /* command */ bool),
    Rnr(Rnr),
    Ui(Ui, /* command */ bool),
    Disc(Disc),
    Iframe(Iframe, /* command */ bool),
    Ua(Ua),
    Frmr(Frmr),
    Rej(Rej),
    Srej(Srej),
    Test(Test),
    Xid(Xid),
}

/// Return events, that the state machine wants to tell the world.
///
/// IOW this excludes state changes, since only the state code needs to know
/// about that.
#[derive(Debug, PartialEq)]
pub enum ReturnEvent {
    Packet(Packet),
    DlError(DlError),
    Data(Res),
}

impl ReturnEvent {
    /// Serialize a return event.
    ///
    /// TODO: Not very clean. Only packets can serialize. Other return events
    /// return None.
    pub fn serialize(&self, ext: bool) -> Option<Vec<u8>> {
        match self {
            ReturnEvent::Packet(p) => Some(p.serialize(ext)),
            ReturnEvent::DlError(e) => {
                eprintln!("TODO: DLERROR: {e}");
                None
            }
            ReturnEvent::Data(d) => {
                debug!("Data received: {d:?}");
                None
            }
        }
    }
}

/// DLErrors (C4.3, page 81)
///
/// Error codes of all kinds.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum DlError {
    A,
    B,
    C,
    D,
    E,
    F,
    G,
    H,
    I,
    J,
    K,
    L,
    M,
    N,
    O,
    P,
    Q,
    R,
    S,
    T,
    U,
    V,
}

// Page 81.
impl std::fmt::Display for DlError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        write!(
            f,
            "{}",
            match self {
                DlError::A => "A: F=1 received but P=1 not outstanding",
                DlError::B => "B: Unexpected DM with F=1 in states 3,4,5",
                DlError::C => "C: Unexpected UA in states 3 (Connected), 4 (TimerRecovery), 5 (Awaiting v2.2 Connection)",
                DlError::D => "D: UA received without F=1 when SABM or DISC was sent P=1",
                DlError::E => "E: DM received in states 3 (Connected), 4 (TimerRecovery), 5 (Awaiting v2.2 Connection)",

                DlError::F => "F: Data link reset; i.e., SABM received in state 3 (Connected), 4 (TimerRecovery), 5 (Awaiting v2.2 Connection)",
                // 1998 Spec bug: Undocumented.
                DlError::G => "G: Connection timed out",
                // 1998 Spec bug: Undocumented.
                DlError::H => "H: Undocumented. May mean connection timed out while disconnecting",
                DlError::I => "I: N2 timeouts; unacknowledged data",
                DlError::J => "J: N(r) sequence error",
                // 1998 Spec bug: Undocumented.
                DlError::K => "K: Undocumented. May mean unexpected frame received",
                DlError::L => "L: Control field invalid or not implemented",
                DlError::M => "M: Information field was received in a U- or S-type frame",
                DlError::N => "N: Length of frame incorrect for frame type",
                DlError::O => "O: I frame exceeded maximum allowed length",
                DlError::P => "P: N(s) out of the window",
                DlError::Q => "Q: UI response received, or UI command with P=1 received",
                DlError::R => "R: UI frame exceeded maximum allowed length",
                DlError::S => "S: I response received",
                DlError::T => "T: N2 timeout; no response to enquiry",
                DlError::U => "U: N2 timeouts; extended pere busy condition",
                DlError::V => "V: No DL machines available to establish connection",
            }
        )
    }
}

/// Actions are like ReturnEvent, except packets are separated.
///
/// Basically anything the state machine wants to do, aside from
/// modifying the `Data` struct, is to produce "Actions".
///
/// TODO: Terminology here is not very great.
pub enum Action {
    State(Box<dyn State>),
    DlError(DlError),
    SendUa { pf: bool },
    SendRr { pf: bool, nr: u8, command: bool },
    SendRej { pf: bool, nr: u8 },
    SendRnr { pf: bool, nr: u8, command: bool },
    SendDisc { pf: bool },
    SendIframe(Iframe),
    SendDm { pf: bool },
    SendSabm { pf: bool },
    Deliver(Vec<u8>),
    EOF,
}

// Spec says 3s.
const DEFAULT_SRT: std::time::Duration = std::time::Duration::from_secs(3);

const DEFAULT_MTU: usize = 200;

// Output buffer size is kept in RAM, so should not grow unbounded.
//
// At the expected speeds, 100MB is way more than what we should expect to
// send in any connection.
const MAX_OBUF_SIZE: usize = 100_000_000;

// TODO: what is the default?
const DEFAULT_T3V: std::time::Duration = std::time::Duration::from_secs(3);

// Max retry count.
const DEFAULT_N2: u8 = 3;

/// Timer object.
///
/// There are two timers, T1 and T3 (4.4.5, page 30).
#[derive(Debug)]
pub struct Timer {
    running: bool,
    expiry: std::time::Instant,
}

impl Default for Timer {
    fn default() -> Self {
        Self {
            running: false,
            expiry: std::time::Instant::now(),
        }
    }
}

impl Timer {
    /// Start timer.
    ///
    /// Called by the state machine.
    fn start(&mut self, v: std::time::Duration) {
        self.expiry = std::time::Instant::now() + v;
        self.running = true;
    }

    /// Return None if timer is not running.
    ///
    /// Returns true if it's expired, alse false.
    pub fn is_expired(&self) -> Option<bool> {
        if !self.running {
            return None;
        }
        Some(std::time::Instant::now() > self.expiry)
    }

    /// Return the remaining time of the timer, if it's running.
    pub fn remaining(&self) -> Option<std::time::Duration> {
        if !self.running {
            return None;
        }
        Some(
            self.expiry
                .saturating_duration_since(std::time::Instant::now()),
        )
    }

    /// Stop timer.
    ///
    /// Called by the state machine.
    fn stop(&mut self) {
        self.running = false;
    }

    /// Restart timer.
    ///
    /// Called by the state machine.
    ///
    /// TODO: the spec doesn't say what the difference is between "start"
    /// and "restart". Maybe there's no difference?
    fn restart(&mut self, v: std::time::Duration) {
        self.start(v);
    }
}

/// Connection (or socket, if you will) extra data.
///
/// The state object only carries the state itself. Further data is in this
/// object.
#[derive(Debug)]
pub struct Data {
    pub(crate) me: Addr,

    pub(crate) peer: Option<Addr>,
    // TODO: double check all types.
    /// True if this client initiated the connection via SABM(E).
    ///
    /// C4.3, page 82.
    layer3_initiated: bool,

    /// T1 timer - pending ACK.
    /// 4.4.5.1, page 30.
    ///
    /// When a packet expecting a reply is sent, such as IFRAME (expecting RR or
    /// a returning IFRAME with NR), T1 is started (unless already running).
    ///
    /// T1 stops if a the last sent IFRAME is acknowledged.
    /// T1 is also used to send another SABM(E) if no UA or DM is received.
    ///
    /// If T1 expires, it initiates a retransmit.
    pub(crate) t1: Timer,

    /// T3 timer - Connection idle timer. (4.4.5.2, page 30)
    ///
    /// When no data is pending ACK, T3 is running. If it expires, it'll trigger
    /// RR/RNR, to probe.
    pub(crate) t3: Timer,
    t3v: std::time::Duration, // TODO: is this where the init value should be?

    /// Send state variable.
    ///
    /// This is the sequence number of the next frame that this node will send,
    /// in the NS field.
    ///
    /// In TCP this counts bytes, but this is packets. Value is kept at mod 8
    /// or 128 at all times.
    vs: u8,

    /// Acknowledge state variable.
    ///
    /// This is the most recent sequence number that the remote end
    /// has reported seeing.
    va: u8,

    /// Receive state variable.
    ///
    /// This is the sequence number of the next expected frame to receive
    /// from the remote end.
    vr: u8,

    /// Default SRT.
    ///
    /// SRT should be smoothed round trip time, but it needs an initial value.
    pub(crate) srt_default: std::time::Duration,

    /// Smoothed round trip time.
    ///
    /// TODO: Don't just keep this fixed.
    srt: std::time::Duration,

    /// Next value for T1; default initial value is initial value of SRT.
    t1v: std::time::Duration,

    /// Max packet size.
    ///
    /// Normally like 200 bytes. And setting it too large tends to cause
    /// some implementations to crash.
    n1: usize,

    /// Max retries.
    ///
    /// After T1 timer expires this many times, the connection attempt
    /// (SABM(E)) or connection (other frames) is aborted.
    n2: u8,

    /// Current retry counter.
    ///
    /// The current value counting towards N2.
    rc: u8,

    /// Either 8 or 128, depending on EXTSEQ.
    modulus: u8,

    /// Remote end is busy, and canet receive frames.
    /// Page 82.
    peer_receiver_busy: bool,

    /// A REJ has been sent to the remote end.
    reject_exception: bool,

    /// We are currently waiting for an incoming connection.
    ///
    /// This is false for outgoing connections, and we will not accept SABM(E)
    /// during Disconnected.
    ///
    /// In a modern spec, able to establish and not would be separate states.
    pub(crate) able_to_establish: bool,

    /// An SREJ has been sent to the remote end.
    ///
    /// TODO: this counts outstanding SREJs?
    sreject_exception: u32,

    /// We are busy.
    ///
    /// TODO: check if we'd ever actually set this to true. The receive window
    /// is so small that the NR/NS will wrap around way before we get "full".
    own_receiver_busy: bool,

    /// ACK, like RR, RNR, or IFRAME, pending.
    acknowledge_pending: bool,

    /// This implementation doesn't yet implement SREJ, so this
    /// is always falso for now.
    srej_enabled: bool,

    /// Maximum number of iframes outstanding.
    ///
    /// This is at most 7 (mod-8), or 127 (extseq, mod-128).
    ///
    /// This is a bit of a tunable value, especially if SREJ is not supported
    /// for the connection. Higher value means fewer bigger bursts of packets,
    /// but makes retransmissions worse. It also hogs the transmitter for other
    /// users.
    k: u8,

    // TODO: not the right type. Should be VecDeque<u8> or VecDeque<Iframe>
    //
    // TODO: this is not currently used, but should be. Either as is, or
    // a byte queue maximizing packet size.
    iframe_queue: Vec<Vec<u8>>,

    /// Output buffer of application payload bytes.
    ///
    /// This will be chopped up into frames when sequence numbers and
    /// transmitter business allows.
    obuf: VecDeque<u8>,

    /// MTU for this connection.
    mtu: usize,

    /// When an IFRAME is sent out, it's stared in this queue, until it's been
    /// acked. When a resend is required, it's sent from here.
    iframe_resend_queue: VecDeque<Iframe>,
}

impl Data {
    /// Create new Data with the specified address being the local one.
    pub fn new(me: Addr) -> Self {
        Self {
            me,
            peer: None,
            n1: 65000, // Max number of octets in the information field of a frame.
            layer3_initiated: false,
            t1: Timer::default(),
            t3: Timer::default(),
            vs: 0,
            va: 0,
            vr: 0,
            srt_default: DEFAULT_SRT,
            srt: DEFAULT_SRT,
            t1v: DEFAULT_SRT,
            t3v: DEFAULT_T3V,
            n2: DEFAULT_N2,
            rc: 0,
            k: 7,
            modulus: 8,
            peer_receiver_busy: false,
            reject_exception: false,
            sreject_exception: 0,
            srej_enabled: false,
            acknowledge_pending: false,
            own_receiver_busy: false,
            iframe_queue: Vec::new(),
            mtu: DEFAULT_MTU,
            obuf: VecDeque::new(),
            iframe_resend_queue: VecDeque::new(),
            able_to_establish: false,
        }
    }

    /// Return true if using 128 modulus.
    #[must_use]
    pub fn ext(&self) -> bool {
        self.modulus == 128
    }

    /// Return true if T1 (retry) has expired.
    #[must_use]
    pub fn t1_expired(&self) -> bool {
        self.t1.is_expired().unwrap_or(false)
    }

    /// Return true if T3 (idle timer) has expired.
    #[must_use]
    pub fn t3_expired(&self) -> bool {
        self.t3.is_expired().unwrap_or(false)
    }

    /// Return list of expired timers.
    #[must_use]
    pub fn active_timers(&self) -> Vec<Event> {
        let mut ret = Vec::new();
        if self.t1_expired() {
            ret.push(Event::T1);
        }
        if self.t3_expired() {
            ret.push(Event::T3);
        }
        ret
    }

    /// Return time until next timer expires, or None if no timer is currently
    /// running.
    #[must_use]
    pub fn next_timer_remaining(&self) -> Option<std::time::Duration> {
        match (self.t1.remaining(), self.t3.remaining()) {
            (Some(t1), Some(t3)) => Some(std::cmp::min(t1, t3)),
            (None, Some(t)) => Some(t),
            (Some(t), None) => Some(t),
            (None, None) => None,
        }
    }

    /// Do something with a received UI frame.
    ///
    /// Unnumbered information is pretty uninteresting here, since this crate
    /// handles connected mode.
    ///
    /// But we should probably add some UI support. It wouldn't be much code.
    ///
    /// Page 108.
    #[must_use]
    fn ui_check(&self, command: bool, len: usize) -> Vec<Action> {
        if !command {
            // 1998 Spec bug: error Q says this is also for UI frames with Poll set.
            //
            // But 4.3.3.6 says command+poll is just fine, and should just trigger
            // DM.
            //
            // So probably the code as-is, is correct, and the Q error message
            // should be changed.
            return vec![Action::DlError(DlError::Q)];
        }
        if len > self.n1 {
            return vec![Action::DlError(DlError::K)];
        }
        debug!("DL-UNIT_DATA indication");
        vec![]
    }

    /// NR error recovery.
    ///
    /// A received packet is trying to ACK a sequence number outside of what's
    /// currently in flight, that's an error, and the connection is terminated.
    ///
    /// Page 106.
    #[must_use]
    fn nr_error_recovery(&mut self) -> Vec<Action> {
        self.layer3_initiated = false;
        vec![Action::DlError(DlError::J), self.establish_data_link()]
    }

    /// Check need for response.
    ///
    /// If a packet is a command, and has the poll bit set, then it demands
    /// a response. I guess in theory this could be an IFRAME, if there's
    /// outstanding data. But the state diagram says to send an RR.
    ///
    /// Page 108, and 6.1.2 and 6.2 on pages 35-36.
    #[must_use]
    fn check_need_for_response(&mut self, command: bool, pf: bool) -> Vec<Action> {
        match (command, pf) {
            // A command with poll set demands a response with fin set.
            (true, true) => vec![self.enquiry_response(true)],

            // I believe that this means the state is currently Connected,
            // and therefore we're not waiting for a response.
            (false, true) => vec![Action::DlError(DlError::A)],

            // If it's not a command, or a command but no response required,
            // then no response needed.
            (_, _) => vec![],
        }
    }

    /// Respond to the other end demanding a sequence number report.
    ///
    /// Bug in 1998 spec, fixed in the 2017 doc:
    /// This function is literally called "response", but the spec says
    /// "RR command". I think not.  It breaks against the Linux implementation
    /// if sending a command, since the kernel never gets answered.
    ///
    /// The kernel (M0THC-2) keeps asking (P), but by following the spec keeps
    /// asking right back.
    ///
    ///  9584 18.543153318      M0THC-2 → M0THC-1      AX.25 16 S P, func=RR, N(R)=1
    ///  9585 18.546108006      M0THC-1 → M0THC-2      AX.25 16 S P, func=RR, N(R)=5
    ///  9586 18.546117747      M0THC-2 → M0THC-1      AX.25 16 S F, func=RR, N(R)=1
    ///
    /// Repeats until Linux kernel gives up and sends DM, closing the connection.
    ///
    /// In the parts currently implemented, `pf` is always set to `true`.
    ///
    /// Page 106.
    #[must_use]
    fn enquiry_response(&mut self, pf: bool) -> Action {
        self.acknowledge_pending = false;
        // TODO: 2017 spec has a bit more complex diagram here. Some of it is
        // correct, but other stuff I'm not so sure of.
        //
        // Most of it is SREJ related, but it also says to send RR instead of
        // RNR if not `F==1 && (RR || RNR || I)`.
        if self.own_receiver_busy {
            // 1998 spec doesn't say, but 2017 spec says "Response".
            Action::SendRnr {
                pf,
                nr: self.vr,
                command: false,
            }
        } else {
            // 1998 spec says commmand, which is wrong.
            Action::SendRr {
                pf,
                nr: self.vr,
                command: false,
            }
        }
    }

    /// Retransmit the resend queue.
    ///
    /// If the remote acks something other than our latest packet, then
    /// send everything unacked.
    ///
    /// TODO: Is this a good idea? This seems like it's a bit over eager in
    /// retransmitting.
    ///
    /// Page 107.
    #[must_use]
    fn invoke_retransmission(&mut self, _nr: u8) -> Vec<Action> {
        self.iframe_resend_queue
            .iter()
            .map(|i| Action::SendIframe(i.clone()))
            .collect()
    }

    /// Select a new T1 value based off of the roundtrip time.
    ///
    /// TODO: actually implement this. Maybe the algorithm in the spec, maybe
    /// something better.
    ///
    /// TODO: Is this supposed to set only SRT, or also T1V?
    ///
    /// Page 109.
    fn select_t1_value(&mut self) {
        if self.rc == 0 {
            // TODO: the real formula is stranger.
            self.srt = self.srt_default;
        } else if self.t1_expired() {
            // 1998 spec says:
            // self.srt = self.srt * (2 ** (rc + 1));

            // 2017 spec formula.
            // It's unclear what unit `rc` is supposed to be. It's retry
            // counter. I'll assume seconds, to millisecond resolution.
            // SRT = RC / 4 + SRT*2
            let t = std::time::Duration::from_millis(self.rc as u64 * 250);
            self.srt = t + self.srt + self.srt;
        }
    }

    /// Ask remote end if they're there, what they heard last.
    ///
    /// This is when T1 or T3 expires.
    ///
    /// Page 106.
    #[must_use]
    fn transmit_enquiry(&mut self) -> Action {
        self.acknowledge_pending = false;
        self.t1.start(self.t1v); // TODO: what timer value?
        if self.own_receiver_busy {
            Action::SendRnr {
                pf: true,
                nr: self.vr,
                command: true,
            }
        } else {
            Action::SendRr {
                pf: true,
                nr: self.vr,
                command: true,
            }
        }
    }

    /// When RR is received, incorporate any new info, and potentially
    /// wait for more RRs.
    ///
    /// Page 107.
    #[must_use]
    fn check_iframe_acked(&mut self, nr: u8) -> Vec<Action> {
        // Typo in 1998 spec. Says "peer busy".
        if self.peer_receiver_busy {
            // 1998 spec says start T3, 2017 spec says stop it.
            // It doesn't make much sense to run both T1 and T3, so let's go
            // with 2017.
            self.t3.stop();
            if !self.t1.running {
                self.t1.start(self.srt); // srt or t1v?
            }
            self.update_ack(nr)
        } else if nr == self.vs {
            self.t1.stop();
            self.t3.start(self.t3v);
            self.select_t1_value();
            self.update_ack(nr)
        } else if nr != self.va {
            // 1998 spec says "restart", 2017 spec just "start". They probably
            // mean the same thing, right?
            self.t1.restart(self.srt);
            self.update_ack(nr)
        } else {
            vec![]
        }
    }

    /// Update state based on an an ACK being received.
    ///
    /// As ACK moves forward, the iframe resend queue can be pruned.
    ///
    /// In the 1998/2017 spec this is just `va <- nr`, which hides the
    /// complexity.
    #[must_use]
    fn update_ack(&mut self, nr: u8) -> Vec<Action> {
        // dbg!(self.va, nr);
        // debug!("Updating ack to {} {}", self.va, nr);
        while self.va != nr {
            assert!(!self.iframe_resend_queue.is_empty());
            self.iframe_resend_queue.pop_front();
            self.va = (self.va + 1) % self.modulus;
        }
        self.flush()
    }

    /// Clear iframe queue.
    ///
    /// This probably means connection shutdown.
    fn clear_iframe_queue(&mut self) {
        self.iframe_queue.clear();
        self.iframe_resend_queue.clear();
    }

    /// Clear exception conditions as a new connection is established.
    fn clear_exception_conditions(&mut self) {
        self.peer_receiver_busy = false;
        self.reject_exception = false;
        self.own_receiver_busy = false;
        self.acknowledge_pending = false;

        // The following added in 2017 spec.
        self.sreject_exception = 0;

        // Huh? Clearing the iframe queue inside a subroutine called "clear
        // exception conditions"? That doesn't seem right.
        //
        // This is new in the 2017 spec.
        //
        // I'm going to leave it here because when exception conditions are
        // unconditionally cleared, it's because a connection was just reset in
        // one way or another.
        self.iframe_queue.clear();
    }

    /// Establish data link.
    ///
    /// Some connection initialization.
    ///
    /// Page 106 & "establish extended data link" on page 109.
    #[must_use]
    fn establish_data_link(&mut self) -> Action {
        self.clear_exception_conditions();

        // 1998 spec says to set rc to 0, 2017 says 1.
        // Yeah I think 1 is right.
        self.rc = 1;
        self.t3.stop();
        // Again 1998 spec says restart, 2017 says start.
        self.t1.restart(self.srt); // TODO: srt or t1v?

        // SendSabm actually sends SABME if modulus is 128.
        Action::SendSabm { pf: true }
    }

    /// Set values for extended sequence number connection.
    ///
    /// Page 109.
    pub(crate) fn set_version_2_2(&mut self) {
        // TODO: set half duplex SREJ
        self.modulus = 128;
        // TODO: n1r = 2048

        // 1998 Spec bug: Spec says `kr`. Surely it means `k`?
        self.k = 32;

        // TODO: self.t2.set(3000);
        self.n2 = 10;
    }

    /// Set values for mod-8 connections.
    ///
    /// Page 109.
    fn set_version_2(&mut self) {
        self.modulus = 8;
        // TODO: n1r = 2048

        // 1998 Spec bug: Spec says `kr`. Surely it means `k`?
        self.k = 4;

        // TODO: self.t2.set(3000);
        self.n2 = 10;
    }

    // If sequence numbers allow, write as many packets as possible.
    //
    // Page 92, "I frame pops off queue".
    #[must_use]
    fn flush(&mut self) -> Vec<Action> {
        if self.peer_receiver_busy {
            return vec![];
        }
        let mut act = Vec::new();
        loop {
            if self.obuf.is_empty() {
                break;
            }
            if self.vs == (self.va + self.k) % self.modulus {
                debug!(
                    "tx window full with more data ({} bytes) to send!",
                    self.obuf.len()
                );
                break;
            }
            let payload = self
                .obuf
                .drain(..std::cmp::min(self.mtu, self.obuf.len()))
                .collect::<Vec<_>>();
            let ns = self.vs;
            self.vs = (self.vs + 1) % self.modulus;
            self.acknowledge_pending = false;
            if self.t1.running {
                self.t3.stop();
                self.t1.start(self.srt);
            }
            let i = Iframe {
                ns,
                nr: self.vr,
                poll: false,
                pid: 0xF0,
                payload,
            };
            self.iframe_resend_queue.push_back(i.clone());
            act.push(Action::SendIframe(i));
        }
        act
    }
}

/// State machine for an AX.25 connection.
///
/// Not all events are implemented in all states, but enough.
///
/// TODO: remove default implementations, to make the "default noop" more
/// deliberate.
pub trait State {
    fn name(&self) -> String;
    fn is_state_connected(&self) -> bool {
        false
    }
    fn is_state_disconnected(&self) -> bool {
        false
    }

    /// User initiates a new connection.
    #[must_use]
    fn connect(&self, _data: &mut Data, _addr: &Addr, _ext: bool) -> Vec<Action> {
        eprintln!("TODO: unexpected DLConnect");
        vec![]
    }

    /// User initiates disconnection.
    #[must_use]
    fn disconnect(&self, _data: &mut Data) -> Vec<Action> {
        eprintln!("TODO: unexpected DLDisconnect in state {}", self.name());
        vec![]
    }

    /// User initiates sending data on a connection.
    #[must_use]
    fn data(&self, _data: &mut Data, _payload: &[u8]) -> Vec<Action> {
        eprintln!("writing data while not connected!");
        vec![]
    }

    /// Timer T1 (pending ack) expires.
    #[must_use]
    fn t1(&self, data: &mut Data) -> Vec<Action> {
        data.t1.stop();
        eprintln!("TODO: unexpected T1 expire");
        vec![]
    }

    /// Timer T3 (connection keepalive) expires.
    #[must_use]
    fn t3(&self, data: &mut Data) -> Vec<Action> {
        data.t3.stop();
        eprintln!("TODO: unexpected T3 expire");
        vec![]
    }

    /// RR received from peer.
    #[must_use]
    fn rr(&self, _data: &mut Data, _packet: &Rr, _command: bool) -> Vec<Action> {
        eprintln!("TODO: unexpected RR");
        vec![]
    }

    /// REJ received from peer.
    #[must_use]
    fn rej(&self, _data: &mut Data, _packet: &Rej) -> Vec<Action> {
        eprintln!("TODO: unexpected REJ");
        vec![]
    }

    /// XID received from peer.
    #[must_use]
    fn xid(&self, _data: &mut Data, _packet: &Xid) -> Vec<Action> {
        eprintln!("TODO: unexpected XID");
        vec![]
    }

    /// TEST received from peer.
    #[must_use]
    fn test(&self, _data: &mut Data, _packet: &Test) -> Vec<Action> {
        eprintln!("TODO: unexpected TEST");
        vec![]
    }

    /// SREJ received from peer.
    #[must_use]
    fn srej(&self, _data: &mut Data, _packet: &Srej) -> Vec<Action> {
        eprintln!("TODO: unexpected SREJ");
        vec![]
    }

    /// FRMR received from peer.
    ///
    /// FRMR is deprecated, so we should probably never see this.
    #[must_use]
    fn frmr(&self, _data: &mut Data) -> Vec<Action> {
        eprintln!("TODO: unexpected FRMR");
        vec![]
    }

    /// RNR received from peer.
    #[must_use]
    fn rnr(&self, _data: &mut Data, _packet: &Rnr) -> Vec<Action> {
        eprintln!("TODO: unexpected RNR");
        vec![]
    }

    /// SABM received from peer.
    #[must_use]
    fn sabm(&self, _data: &mut Data, _src: &Addr, _packet: &Sabm) -> Vec<Action> {
        eprintln!("TODO: unexpected SABM");
        vec![]
    }

    /// SABME received from peer.
    #[must_use]
    fn sabme(&self, _data: &mut Data, _src: &Addr, _packet: &Sabme) -> Vec<Action> {
        eprintln!("TODO: unexpected SABME");
        vec![]
    }

    /// IFRAME received from peer.
    #[must_use]
    fn iframe(&self, _data: &mut Data, _packet: &Iframe, _cr: bool) -> Vec<Action> {
        eprintln!("TODO; unexpected iframe");
        vec![]
    }

    /// UI received from peer.
    #[must_use]
    fn ui(&self, _data: &mut Data, _cr: bool, _packet: &Ui) -> Vec<Action> {
        vec![]
    }

    /// UA received from peer.
    #[must_use]
    fn ua(&self, _data: &mut Data, _packet: &Ua) -> Vec<Action> {
        eprintln!("TODO; unexpected UA");
        vec![]
    }

    /// DM received from peer.
    #[must_use]
    fn dm(&self, _data: &mut Data, _packet: &Dm) -> Vec<Action> {
        eprintln!("TODO: unexpected DM");
        vec![]
    }

    /// DISC received from peer.
    #[must_use]
    fn disc(&self, _data: &mut Data, _packet: &Disc) -> Vec<Action> {
        eprintln!("TODO: unexpected DISC");
        vec![]
    }
}

/// Disconnected state.
///
/// I think this is fully implemented for AX.25 2.0. No SABME yet, though.
///
/// This is a state diagram for a connection. Any non-listening socket
/// should in theory cause `SendDm(p.poll)`, but out of scope.
struct Disconnected {}
impl Disconnected {
    #[must_use]
    pub fn new() -> Self {
        Self {}
    }

    // Page 85.
    #[must_use]
    fn sabm_and_sabme(&self, data: &mut Data, src: Addr, pf: bool) -> Vec<Action> {
        debug!("DL-Connect indication");
        if !data.able_to_establish {
            return vec![Action::SendDm { pf }];
        }
        data.clear_exception_conditions();
        data.vs = 0;
        data.va = 0;
        data.vr = 0;
        data.srt = data.srt_default;
        data.t1v = data.srt + data.srt;
        data.t3.start(data.t3v);
        data.rc = 0;
        data.peer = Some(src);
        vec![
            Action::SendUa { pf },
            Action::State(Box::new(Connected::new(ConnectedState::Connected))),
        ]
    }
}

// Page 84-85.
//
// "All other commands" should generate a DM. Does it mean all other incoming packets?
// Other than that, this state should be complete.
impl State for Disconnected {
    fn name(&self) -> String {
        "Disconnected".to_string()
    }

    fn is_state_disconnected(&self) -> bool {
        true
    }

    // Page 85.
    fn connect(&self, data: &mut Data, addr: &Addr, ext: bool) -> Vec<Action> {
        data.modulus = match ext {
            true => 128,
            false => 8,
        };
        // It says "SAT" in the PDF, but surely means SRT?
        data.peer = Some(addr.clone());
        data.srt = data.srt_default;
        data.t1v = 2 * data.srt;
        data.layer3_initiated = true;
        vec![
            Action::State(Box::new(AwaitingConnection::new())),
            data.establish_data_link(),
        ]
    }

    // Page 84.
    fn disconnect(&self, _data: &mut Data) -> Vec<Action> {
        eprintln!("Disconnect while already disconnected");
        vec![]
    }

    // Page 84.
    fn ui(&self, data: &mut Data, cr: bool, packet: &Ui) -> Vec<Action> {
        let mut ret = data.ui_check(cr, packet.payload.len());
        if packet.push {
            ret.push(Action::SendDm { pf: true });
        }
        ret
    }

    // Page 85.
    fn sabm(&self, data: &mut Data, src: &Addr, sabm: &Sabm) -> Vec<Action> {
        data.set_version_2();
        self.sabm_and_sabme(data, src.clone(), sabm.poll)
    }

    // Page 85.
    fn sabme(&self, data: &mut Data, src: &Addr, packet: &Sabme) -> Vec<Action> {
        data.set_version_2_2();
        self.sabm_and_sabme(data, src.clone(), packet.poll)
    }

    // Page 84.
    fn dm(&self, _data: &mut Data, _packet: &Dm) -> Vec<Action> {
        vec![]
    }

    // Page 84.
    fn ua(&self, _data: &mut Data, _packet: &Ua) -> Vec<Action> {
        vec![Action::DlError(DlError::C), Action::DlError(DlError::D)]
    }

    // Page 84.
    fn disc(&self, _data: &mut Data, packet: &Disc) -> Vec<Action> {
        vec![Action::SendDm { pf: packet.poll }]
    }
}

// AwaitingConnection means a SABM(E) has been sent, and we are waiting for the
// UA.
struct AwaitingConnection {}

impl AwaitingConnection {
    #[must_use]
    fn new() -> Self {
        Self {}
    }
}

impl State for AwaitingConnection {
    fn name(&self) -> String {
        "AwaitingConnection".to_string()
    }

    // Page 88.
    fn t1(&self, data: &mut Data) -> Vec<Action> {
        eprintln!("t1 expired while connecting, retrying");
        data.t1.stop();
        if data.rc == data.n2 {
            data.clear_iframe_queue();
            vec![
                // Typo in 1998 spec: G, not g.
                Action::DlError(DlError::G),
                Action::State(Box::new(Disconnected::new())),
            ]
        } else {
            data.rc += 1;
            data.select_t1_value();
            data.t1.start(data.srt);
            vec![Action::SendSabm { pf: true }]
        }
    }

    // Page 88.
    fn ua(&self, data: &mut Data, packet: &Ua) -> Vec<Action> {
        let f = packet.poll;
        if !f {
            return vec![Action::DlError(DlError::D)];
        }
        if data.layer3_initiated {
            debug!("DL-CONNECT CONFIRM");
        } else if data.vs != data.va {
            // 1998 spec:
            // We're awaiting a connection confirmation, but vs!=va? What does
            // that mean?
            //
            // If we're getting an UA after remote end has already acked
            // some packets, what does that even mean?
            //
            //   data.iframe_queue.clear();
            //   debug!("DL-CONNECT indiciation"); // huh?
            //
            // 2017 spec;
            // I still wonder what it means, what the intention is.
            //
            // In addition, there's a bug in the 2017 spec. This path says to
            // start T1, then immediately stop it again.
            data.srt = data.srt_default;
            data.t1v = data.srt + data.srt;
            debug!("DL-CONNECT CONFIRM, vs!=va");
            warn!("Strange state entered: UA received while vs != va");
        }
        data.t1.stop();

        // 1998 spec says "stop T3".
        // 2017 spec says "start T3" (page 89), which makes much more sense.
        data.t3.start(data.t3v);

        data.vs = 0;
        data.va = 0;
        data.vr = 0;
        data.select_t1_value();
        vec![Action::State(Box::new(Connected::new(
            ConnectedState::Connected,
        )))]
    }

    // Page 86.
    fn sabm(&self, _data: &mut Data, _src: &Addr, packet: &Sabm) -> Vec<Action> {
        vec![Action::SendUa { pf: packet.poll }]
    }

    // Page 88.
    fn sabme(&self, _data: &mut Data, _src: &Addr, packet: &Sabme) -> Vec<Action> {
        // TODO: This is supposed to transition to "awaiting connect 2.2".
        vec![Action::SendDm { pf: packet.poll }]
    }
}

/// TODO: document the meaning of this state.
struct AwaitingRelease {}

impl AwaitingRelease {
    #[must_use]
    fn new() -> Self {
        Self {}
    }
}

// Starting on page 89.
impl State for AwaitingRelease {
    fn name(&self) -> String {
        "AwaitingRelease".to_string()
    }

    // Page 91.
    fn dm(&self, data: &mut Data, p: &Dm) -> Vec<Action> {
        if !p.poll {
            return vec![];
        }
        data.t1.stop();
        vec![Action::State(Box::new(Disconnected::new()))]
    }

    // Page 90.
    fn ua(&self, data: &mut Data, p: &Ua) -> Vec<Action> {
        if !p.poll {
            return vec![Action::DlError(DlError::D)];
        }
        debug!("DL-DISCONNECT confirm");
        data.t1.stop();
        vec![Action::State(Box::new(Disconnected::new()))]
    }

    // Page 91.
    fn t1(&self, data: &mut Data) -> Vec<Action> {
        data.t1.stop();
        if data.rc == data.n2 {
            debug!("DL-DISCONNECT confirm");
            // The 1998 spec doesn't say, but if we're going disconnected, then
            // there's no need for timers.
            data.t3.stop();
            return vec![
                Action::DlError(DlError::H),
                Action::State(Box::new(Disconnected::new())),
            ];
        }
        data.rc += 1;
        data.select_t1_value();
        data.t1.start(data.t1v);
        vec![Action::SendDisc { pf: true }]
    }

    // TODO: More handlers.
}

enum ConnectedState {
    Connected,
    TimerRecovery,
}

/// TODO: document the meaning of this state.
struct Connected {
    connected_state: ConnectedState,
}

impl Connected {
    #[must_use]
    fn new(connected_state: ConnectedState) -> Self {
        Self { connected_state }
    }

    // Page 95
    #[must_use]
    fn rr_connected(&self, data: &mut Data, packet: &Rr, cr: bool) -> Vec<Action> {
        data.peer_receiver_busy = false;
        let mut act = data.check_need_for_response(cr, packet.poll);
        if !in_range(data.va, packet.nr, data.vs, data.modulus) {
            act.extend(data.nr_error_recovery());
            act.push(Action::State(Box::new(AwaitingConnection::new())));
        } else {
            act.extend(data.check_iframe_acked(packet.nr));
        }
        act
    }

    // Page 99.
    #[must_use]
    fn rr_timer_recovery(&self, data: &mut Data, packet: &Rr, cr: bool) -> Vec<Action> {
        data.peer_receiver_busy = false;
        if !cr && packet.poll {
            data.t1.stop();
            data.select_t1_value();
            if !in_range(data.va, packet.nr, data.vs, data.modulus) {
                let mut act = data.nr_error_recovery();
                act.push(Action::State(Box::new(AwaitingConnection::new())));
                return act;
            }
            let mut act = data.update_ack(packet.nr);
            if data.vs == data.va {
                data.t3.start(data.t3v);
                data.rc = 0; // Added in 2017 spec, page 95.
                act.push(Action::State(Box::new(Connected::new(
                    ConnectedState::Connected,
                ))));
            } else {
                act.extend(data.invoke_retransmission(packet.nr));

                // The following added in 2017 spec, page 95.
                data.t3.stop();
                data.t1.start(data.t1v);
                data.acknowledge_pending = true;
            }
            return act;
        }
        let mut act = Vec::new();
        // 2017 spec bug on page 95: no 'no' path from this if.
        if cr && packet.poll {
            act.push(data.enquiry_response(true));
        }
        if in_range(data.va, packet.nr, data.vs, data.modulus) {
            act.extend(data.update_ack(packet.nr));
        } else {
            act.extend(data.nr_error_recovery());
            act.push(Action::State(Box::new(AwaitingConnection::new())));
        }
        act
    }

    // Page 93 and page 99.
    fn sabm_or_sabme(&self, data: &mut Data, poll: bool) -> Vec<Action> {
        data.clear_exception_conditions();
        if data.vs != data.va {
            data.iframe_queue.clear();
            debug!("DL-Connect indication");
        }
        data.t1.stop();

        // 2017 spec says to stop both T1 and T3 in state timer recovery. That
        // can't be right, can it?
        data.t3.start(data.t3v);
        data.va = 0;
        data.vs = 0;
        data.vr = 0; // 1998 spec typos this as another vs=0.
        if let ConnectedState::Connected = self.connected_state {
            // Added in 2017 spec, but only for Connected.
            // TODO: should this be set also for TimerRecovery?
            data.rc = 0;
        }
        vec![
            Action::DlError(DlError::F),
            Action::SendUa { pf: poll },
            Action::State(Box::new(Connected::new(ConnectedState::Connected))),
        ]
    }
}

impl State for Connected {
    fn name(&self) -> String {
        match self.connected_state {
            ConnectedState::Connected => "Connected".to_string(),
            ConnectedState::TimerRecovery => "TimerRecovery".to_string(),
        }
    }
    fn is_state_connected(&self) -> bool {
        true
    }

    // Page 92 & 98.
    fn disconnect(&self, data: &mut Data) -> Vec<Action> {
        data.clear_iframe_queue();
        data.rc = 0;
        data.t1.start(data.srt); // TODO: with what timer?
        data.t3.stop();
        vec![
            Action::SendDisc { pf: true },
            Action::State(Box::new(AwaitingRelease::new())),
        ]
    }

    // Page 92 & 98.
    //
    // This implementation deliberately doesn't preserve the application's
    // frame boundaries.
    //
    // This seems like the right thing to do. But in the future maybe we'll
    // implement the equivalent of SEQPACKET.
    fn data(&self, data: &mut Data, payload: &[u8]) -> Vec<Action> {
        data.obuf.extend(payload);
        if data.obuf.len() > MAX_OBUF_SIZE {
            panic!(
                "TODO: handle better. Output buffer got too large. {} > {}",
                data.obuf.len(),
                MAX_OBUF_SIZE
            );
        }
        data.flush()
    }

    // Page 93.
    //
    // src is ignored, because it's presumed to already have been checked, in
    // this state.
    fn sabm(&self, data: &mut Data, _src: &Addr, packet: &Sabm) -> Vec<Action> {
        data.set_version_2();
        self.sabm_or_sabme(data, packet.poll)
    }

    // Page 93.
    //
    // src is ignored, because it's presumed to already have been checked, in
    // this state.
    fn sabme(&self, data: &mut Data, _src: &Addr, packet: &Sabme) -> Vec<Action> {
        data.set_version_2_2();
        self.sabm_or_sabme(data, packet.poll)
    }

    // Page 93 & 101.
    //
    // Done.
    fn dm(&self, data: &mut Data, _packet: &Dm) -> Vec<Action> {
        debug!("DL-DISCONNECT");
        data.clear_iframe_queue();
        data.t1.stop();
        data.t3.stop();
        vec![
            Action::DlError(DlError::E),
            Action::State(Box::new(Disconnected::new())),
        ]
    }

    // Page 93 & 100.
    //
    // Done.
    fn disc(&self, data: &mut Data, p: &Disc) -> Vec<Action> {
        data.clear_iframe_queue();
        data.t1.stop();
        data.t3.stop();
        vec![
            Action::SendUa { pf: p.poll },
            Action::EOF,
            Action::State(Box::new(Disconnected::new())),
        ]
    }

    // Page 96 & 102.
    fn iframe(&self, data: &mut Data, p: &Iframe, command_response: bool) -> Vec<Action> {
        if !command_response {
            // 2017 spec page 93 says to DlError::O if the iframe *is* a
            // command. That's not even remotely correct, since O means packet
            // too big.
            // The TimerRecovery version on page 96 has the same nonsense, but
            // there the yes/no is not even labelled.
            return vec![Action::DlError(DlError::S)];
        }
        if p.payload.len() > data.n1 {
            data.layer3_initiated = false;
            debug!("Discarding frame for being too big");
            return vec![
                data.establish_data_link(),
                Action::DlError(DlError::O),
                Action::State(Box::new(AwaitingConnection::new())),
            ];
        }
        if !in_range(data.va, p.nr, data.vs, data.modulus) {
            debug!("Discarding frame for being out of range");
            let mut acts = data.nr_error_recovery();
            acts.push(Action::State(Box::new(AwaitingConnection::new())));
            return acts;
        }
        let mut actions = vec![];
        match self.connected_state {
            ConnectedState::Connected => actions.extend(data.check_iframe_acked(p.nr)),
            ConnectedState::TimerRecovery => actions.extend(data.update_ack(p.nr)),
        }
        if data.own_receiver_busy {
            // discord (implicit)
            debug!("Discarding iframe because busy and being polled");
            if p.poll {
                actions.push(Action::SendRnr {
                    pf: true,
                    nr: data.vr,
                    command: false,
                });
                data.acknowledge_pending = false;
            }
            return actions;
        }

        if p.ns == data.vr {
            debug!("iframe in order {}", p.ns);
            // Frame is in order.
            data.vr = (data.vr + 1) % data.modulus;
            data.reject_exception = false;
            if data.sreject_exception > 0 {
                data.sreject_exception -= 1;
            }
            actions.push(Action::Deliver(p.payload.clone()));
            // TODO: check for stored out of order frames
            while
            /* i frame stored */
            false {
                // retrieve stored vr in frame
                // Deliver
                data.vr = (data.vr + 1) % data.modulus;
            }
            if p.poll {
                actions.push(Action::SendRr {
                    pf: true,
                    nr: data.vr,
                    command: false,
                });
                data.acknowledge_pending = false;
                return actions;
            }
            if !data.acknowledge_pending {
                // LM seize request (?).
                data.acknowledge_pending = true;
            }
            return actions;
        }
        debug!("Iframe not in order got={} want={}", p.ns, data.vr);
        if data.reject_exception {
            // discard frame (implicit)
            if p.poll {
                actions.push(Action::SendRr {
                    pf: true,
                    nr: data.vr,
                    command: false,
                });
                data.acknowledge_pending = false;
            }
            return actions;
        }
        if !data.srej_enabled {
            // discard iframe (implicit)
            //
            // TODO: should we maybe wait a bit with sending a REJ?
            // Empirically I got a bunch of duplicated packets, and
            // that just wrecks havoc with retransmits.
            //
            // Maybe skip a duplicate?
            data.reject_exception = true;
            actions.push(Action::SendRej {
                pf: p.poll,
                nr: data.vr,
            });
            data.acknowledge_pending = false;
            return actions;
        }
        // TODO: save contents of iframe
        if data.sreject_exception > 0 {
            data.sreject_exception += 1;
            // TODO: actions.push(Action::SendSrej(final=false, nr=p.ns));
            data.acknowledge_pending = false;
            return actions;
        }
        // if ns > vr + 1
        // TODO: Maybe a version of if in_range(p.ns) {
        if p.ns != (data.vr + 1) % data.modulus {
            // discard iframe (implicit)
            actions.push(Action::SendRej {
                pf: p.poll,
                nr: data.vr,
            });
            data.acknowledge_pending = false;
            return actions;
        }
        data.sreject_exception += 1;
        // TODO: actions.push(Action::SendSrej(final=false, nr=data.vr));
        data.acknowledge_pending = false;
        actions
    }

    // Page 93 & 99.
    fn t1(&self, data: &mut Data) -> Vec<Action> {
        data.t1.stop();
        data.rc = match self.connected_state {
            ConnectedState::Connected => 1,
            ConnectedState::TimerRecovery => data.rc + 1,
        };
        if data.rc != data.n2 {
            return vec![
                data.transmit_enquiry(),
                Action::State(Box::new(Connected::new(ConnectedState::TimerRecovery))),
            ];
        }
        data.clear_iframe_queue(); // Spec says "discard" iframe queue.
        debug!("DL-DISCONNECT request");
        vec![
            Action::DlError(match (data.vs == data.va, data.peer_receiver_busy) {
                (false, _) => DlError::I,
                (true, true) => DlError::U,
                (true, false) => DlError::T,
            }),
            // 1998 spec (page 99) doesn't say if it should be true or false.
            // 2017 spec adds that pf should be false.
            Action::SendDm { pf: false },
            Action::State(Box::new(Disconnected::new())),
        ]
    }

    // Page 93 (Connected only).
    fn t3(&self, data: &mut Data) -> Vec<Action> {
        data.t3.stop();
        if let ConnectedState::TimerRecovery = self.connected_state {
            error!("T3 should not be running in TimerRecovery");
        }
        data.rc = 0;
        vec![
            Action::State(Box::new(Connected::new(ConnectedState::TimerRecovery))),
            data.transmit_enquiry(),
        ]
    }

    // Page 93 & 100.
    //
    // 2017 spec says DlError::K, which is undocumented.
    fn ua(&self, data: &mut Data, _ua: &Ua) -> Vec<Action> {
        data.layer3_initiated = false;
        vec![
            Action::DlError(DlError::C),
            data.establish_data_link(),
            Action::State(Box::new(AwaitingConnection::new())),
        ]
    }

    // Page 94 & 101.
    //
    // For TimerRecovery, see note K.
    fn frmr(&self, data: &mut Data) -> Vec<Action> {
        data.layer3_initiated = false;
        vec![
            Action::DlError(DlError::K),
            data.establish_data_link(),
            Action::State(Box::new(AwaitingConnection::new())),
        ]
    }

    // Page 94 & 100.
    fn ui(&self, data: &mut Data, cr: bool, packet: &Ui) -> Vec<Action> {
        let mut act = data.ui_check(cr, packet.payload.len());
        if packet.push {
            act.push(data.enquiry_response(true));
        }
        act
    }

    fn rr(&self, data: &mut Data, packet: &Rr, cr: bool) -> Vec<Action> {
        match self.connected_state {
            ConnectedState::Connected => self.rr_connected(data, packet, cr),
            ConnectedState::TimerRecovery => self.rr_timer_recovery(data, packet, cr),
        }
    }
}

/// Ugly range checker.
///
/// if va steps forward, will it hit nr before it hits vs?
#[must_use]
fn in_range(va: u8, nr: u8, vs: u8, modulus: u8) -> bool {
    let mut t = va;
    loop {
        if t == nr {
            return true;
        }
        if t == vs {
            return false;
        }
        t = (t + 1) % modulus;
    }
}

/// Create new state machine, starting in state `Disconnected`.
#[must_use]
pub fn new() -> Box<dyn State> {
    Box::new(Disconnected::new())
}

/// Data delivery object.
///
/// None => No data available now.
/// EOF => Connection is closed.
/// Some(_) => Some data, here you go.
///
/// TODO: poorly named.
#[derive(Debug, PartialEq)]
pub enum Res {
    None,
    EOF,
    Some(Vec<u8>),
}

/// Handle an incoming state, by shoving it through the state machine.
///
/// Source and destination address are assumed to be correct, or in the case of
/// SABM(E), the address is passed along.
///
/// A set of return events and possibly a new state is returned.
#[must_use]
pub fn handle(
    state: &dyn State,
    data: &mut Data,
    packet: &Event,
) -> (Option<Box<dyn State>>, Vec<ReturnEvent>) {
    let actions = match packet {
        Event::Connect { addr, ext } => state.connect(data, addr, *ext),
        Event::Disconnect => state.disconnect(data),
        Event::Data(payload) => state.data(data, payload),
        Event::T1 => state.t1(data),
        Event::T3 => state.t3(data),
        Event::Sabm(p, src) => state.sabm(data, src, p),
        Event::Sabme(p, src) => state.sabme(data, src, p),
        Event::Dm(dm) => state.dm(data, dm),
        Event::Ui(p, cr) => state.ui(data, *cr, p),
        Event::Disc(p) => state.disc(data, p),
        Event::Iframe(p, command_response) => state.iframe(data, p, *command_response),
        Event::Ua(p) => state.ua(data, p),
        Event::Rr(p, command) => state.rr(data, p, *command),
        Event::Rnr(p) => state.rnr(data, p),
        Event::Frmr(_) => state.frmr(data),
        Event::Rej(p) => state.rej(data, p),
        Event::Srej(p) => state.srej(data, p),
        Event::Xid(p) => state.xid(data, p),
        Event::Test(p) => state.test(data, p),
    };
    let mut ret = Vec::new();

    // Save non-state actions.
    for act in &actions {
        use Action::*;
        match act {
            Action::State(_) => {} // Ignore state change at this stage.
            DlError(code) => ret.push(ReturnEvent::DlError(*code)),
            SendIframe(iframe) => ret.push(ReturnEvent::Packet(Packet {
                src: data.me.clone(),
                dst: data.peer.clone().unwrap().clone(),
                command_response: true,     // TODO: what value?
                command_response_la: false, // TODO: same
                digipeater: vec![],
                rr_dist1: false,
                rr_extseq: false,
                packet_type: PacketType::Iframe(iframe.clone()),
            })),
            SendDisc { pf } => ret.push(ReturnEvent::Packet(Packet {
                src: data.me.clone(),
                dst: data.peer.clone().unwrap().clone(),
                command_response: true,     // TODO: what value?
                command_response_la: false, // TODO: same
                digipeater: vec![],
                rr_dist1: false,
                rr_extseq: false,
                packet_type: PacketType::Disc(Disc { poll: *pf }),
            })),
            SendUa { pf } => ret.push(ReturnEvent::Packet(Packet {
                src: data.me.clone(),
                dst: data.peer.clone().unwrap().clone(),
                command_response: false,
                command_response_la: true,
                digipeater: vec![],
                rr_dist1: false,
                rr_extseq: false,
                packet_type: PacketType::Ua(Ua { poll: *pf }),
            })),
            SendRej { pf, nr } => ret.push(ReturnEvent::Packet(Packet {
                src: data.me.clone(),
                dst: data.peer.clone().unwrap().clone(),
                command_response: false,
                command_response_la: true,
                digipeater: vec![],
                rr_dist1: false,
                rr_extseq: false,
                packet_type: PacketType::Rej(Rej { poll: *pf, nr: *nr }),
            })),
            SendRr { pf, nr, command } => ret.push(ReturnEvent::Packet(Packet {
                src: data.me.clone(),
                dst: data.peer.clone().unwrap().clone(),
                command_response: *command,
                command_response_la: !*command,
                digipeater: vec![],
                rr_dist1: false,
                rr_extseq: false,
                packet_type: PacketType::Rr(Rr { poll: *pf, nr: *nr }),
            })),
            SendRnr { pf, nr, command } => ret.push(ReturnEvent::Packet(Packet {
                src: data.me.clone(),
                dst: data.peer.clone().unwrap().clone(),
                command_response: *command,
                command_response_la: !*command,
                digipeater: vec![],
                rr_dist1: false,
                rr_extseq: false,
                packet_type: PacketType::Rnr(Rnr { poll: *pf, nr: *nr }),
            })),
            SendDm { pf } => ret.push(ReturnEvent::Packet(Packet {
                src: data.me.clone(),
                dst: data.peer.clone().unwrap().clone(),
                command_response: true,     // TODO: what value?
                command_response_la: false, // TODO: same
                digipeater: vec![],
                rr_dist1: false,
                rr_extseq: false,
                packet_type: PacketType::Dm(Dm { poll: *pf }),
            })),
            SendSabm { pf } => ret.push(ReturnEvent::Packet(Packet {
                src: data.me.clone(),
                dst: data.peer.clone().unwrap().clone(),
                command_response: true,     // TODO: what value?
                command_response_la: false, // TODO: same
                digipeater: vec![],
                rr_dist1: false,
                rr_extseq: false,
                packet_type: PacketType::Sabm(Sabm { poll: *pf }),
            })),
            // TODO: can we avoid the copy?
            Deliver(p) => ret.push(ReturnEvent::Data(Res::Some(p.to_vec()))),
            EOF => ret.push(ReturnEvent::Data(Res::EOF)),
        }
    }
    for act in actions {
        // Non-statechange actions handled above.
        if let Action::State(new_state) = act {
            return (Some(new_state), ret);
        }
    }
    (None, ret)
}

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

    fn assert_all(want: &[ReturnEvent], got: &[ReturnEvent], more: &str) {
        for w in want {
            let mut found = false;
            for g in got {
                if g == w {
                    found = true;
                    break;
                }
            }
            assert!(found, "Did not find {w:?}\ngot: {got:?}");
        }
        assert_eq!(
            want.len(),
            got.len(),
            "got and want different lengths for {more}:\nwant: {want:?}\ngot: {got:?}"
        );
    }

    #[test]
    fn disconnected_outgoing_timeout() -> Result<()> {
        let mut data = Data::new(Addr::new("M0THC-1")?);
        let con = Disconnected::new();

        // First attempt.
        dbg!("First attempt");
        let (con, events) = handle(
            &con,
            &mut data,
            &Event::Connect {
                addr: Addr::new("M0THC-2")?,
                ext: false,
            },
        );
        let con = con.unwrap();
        assert_eq!(con.name(), "AwaitingConnection");
        assert_eq!(data.peer, Some(Addr::new("M0THC-2")?));
        assert_all(
            &[ReturnEvent::Packet(Packet {
                src: Addr::new("M0THC-1")?,
                dst: Addr::new("M0THC-2")?,
                command_response: true,
                command_response_la: false,
                digipeater: vec![],
                rr_dist1: false,
                rr_extseq: false,
                packet_type: PacketType::Sabm(Sabm { poll: true }),
            })],
            &events,
            "connect",
        );

        for retry in 1.. {
            dbg!("Retry", retry);
            let (c2, events) = handle(&*con, &mut data, &Event::T1);
            if retry == 3 {
                assert_eq!(c2.unwrap().name(), "Disconnected");
                break;
            } else {
                assert!(matches![c2, None]);
                assert_eq!(data.peer, Some(Addr::new("M0THC-2")?));
                assert_all(
                    &[ReturnEvent::Packet(Packet {
                        src: Addr::new("M0THC-1")?,
                        dst: Addr::new("M0THC-2")?,
                        command_response: true,
                        command_response_la: false,
                        digipeater: vec![],
                        rr_dist1: false,
                        rr_extseq: false,
                        packet_type: PacketType::Sabm(Sabm { poll: true }),
                    })],
                    &events,
                    "connect",
                );
            }
        }
        Ok(())
    }

    #[test]
    fn disconnected_incoming() -> Result<()> {
        let mut data = Data::new(Addr::new("M0THC-1")?);
        data.able_to_establish = true; // TODO: implement some sort of listen()
        let con = Disconnected::new();

        let (con, events) = handle(
            &con,
            &mut data,
            &Event::Sabm(Sabm { poll: true }, Addr::new("M0THC-2")?),
        );
        let con = con.unwrap();
        assert_eq!(con.name(), "Connected");
        assert_eq!(data.peer, Some(Addr::new("M0THC-2")?));
        assert_all(
            &[ReturnEvent::Packet(Packet {
                src: Addr::new("M0THC-1")?,
                dst: Addr::new("M0THC-2")?,
                command_response: false,
                command_response_la: true,
                digipeater: vec![],
                rr_dist1: false,
                rr_extseq: false,
                packet_type: PacketType::Ua(Ua { poll: true }),
            })],
            &events,
            "connect",
        );
        Ok(())
    }

    #[test]
    fn connected() -> Result<()> {
        let mut data = Data::new(Addr::new("M0THC-1")?);
        data.peer = Some(Addr::new("M0THC-2")?);
        let con = Connected::new(ConnectedState::Connected);

        eprintln!("Receive data packet");
        let (c2, events) = handle(
            &con,
            &mut data,
            &Event::Iframe(
                Iframe {
                    nr: 0,
                    ns: 0,
                    poll: true, // TODO: poll or no?
                    pid: 0xF0,
                    payload: vec![1, 2, 3],
                },
                true,
            ),
        );
        assert!(matches![c2, None]);
        assert_all(
            &[
                ReturnEvent::Data(Res::Some(vec![1, 2, 3])),
                ReturnEvent::Packet(Packet {
                    src: Addr::new("M0THC-1")?,
                    dst: Addr::new("M0THC-2")?,
                    command_response: false,
                    command_response_la: true,
                    digipeater: vec![],
                    rr_dist1: false,
                    rr_extseq: false,
                    packet_type: PacketType::Rr(Rr { poll: true, nr: 1 }),
                }),
            ],
            &events,
            "iframe",
        );

        eprintln!("Receive repeated packet");
        let (c2, events) = handle(
            &con,
            &mut data,
            &Event::Iframe(
                Iframe {
                    nr: 0,
                    ns: 0,
                    poll: true, // TODO: poll or no?
                    pid: 0xF0,
                    payload: vec![1, 2, 3],
                },
                true,
            ),
        );
        assert!(matches![c2, None]);
        assert_all(
            &[ReturnEvent::Packet(Packet {
                src: Addr::new("M0THC-1")?,
                dst: Addr::new("M0THC-2")?,
                command_response: false,
                command_response_la: true,
                digipeater: vec![],
                rr_dist1: false,
                rr_extseq: false,
                packet_type: PacketType::Rej(Rej { poll: true, nr: 1 }),
            })],
            &events,
            "iframe",
        );

        eprintln!("Receive next packet");
        let (c2, events) = handle(
            &con,
            &mut data,
            &Event::Iframe(
                Iframe {
                    nr: 0,
                    ns: 1,
                    poll: true, // TODO: poll or no?
                    pid: 0xF0,
                    payload: vec![11, 22, 33],
                },
                true,
            ),
        );
        assert!(matches![c2, None]);
        assert_all(
            &[
                ReturnEvent::Data(Res::Some(vec![11, 22, 33])),
                ReturnEvent::Packet(Packet {
                    src: Addr::new("M0THC-1")?,
                    dst: Addr::new("M0THC-2")?,
                    command_response: false,
                    command_response_la: true,
                    digipeater: vec![],
                    rr_dist1: false,
                    rr_extseq: false,
                    packet_type: PacketType::Rr(Rr { poll: true, nr: 2 }),
                }),
            ],
            &events,
            "iframe",
        );
        Ok(())
    }

    #[test]
    fn disconnect() -> Result<()> {
        let mut data = Data::new(Addr::new("M0THC-1")?);
        data.peer = Some(Addr::new("M0THC-2")?);
        let con = Connected::new(ConnectedState::Connected);
        let (c2, events) = handle(&con, &mut data, &Event::Disc(Disc { poll: true }));
        assert_eq!(c2.unwrap().name(), "Disconnected");
        assert_all(
            &[
                ReturnEvent::Data(Res::EOF),
                ReturnEvent::Packet(Packet {
                    src: Addr::new("M0THC-1")?,
                    dst: Addr::new("M0THC-2")?,
                    command_response: false,
                    command_response_la: true,
                    digipeater: vec![],
                    rr_dist1: false,
                    rr_extseq: false,
                    packet_type: PacketType::Ua(Ua { poll: true }),
                }),
            ],
            &events,
            "disconnect",
        );
        Ok(())
    }
}