asyn-rs 0.25.0

Rust port of EPICS asyn - async device I/O framework
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
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
//! COM interpose — RFC 2217 telnet COM-port-option negotiation for remote
//! serial ports reached over TCP (a "terminal server" / serial-to-ethernet
//! converter).
//!
//! Wire-faithful port of C `asynInterposeCom.c` (856 lines). C installs it from
//! `drvAsynIPPort.c:1061` when the configure string's protocol token is `COM`;
//! [`crate::drivers::ip_port::DrvAsynIPPort`] does the same.
//!
//! The C file interposes **two** interfaces on the port, and this module keeps
//! that split rather than fusing them, because the state they touch is
//! disjoint:
//!
//! * the `asynOctet` interface — IAC (0xFF) doubling on write and unstuffing on
//!   read ([`ComInterpose`], C `writeIt`/`readIt`, :136-245). Pure byte
//!   transformation: C's only per-port state here is the `xBuf` scratch buffer.
//! * the `asynOption` interface — the telnet negotiation and the serial-line
//!   settings it carries ([`ComPortOptions`], C `willdo` / `sbComPortOption` /
//!   `setOption` / `getOption` / `restoreSettings`, :327-758).
//!
//! # The negotiation bypasses the octet interpose
//!
//! Every negotiation byte C sends goes to `pinterposePvt->pasynOctetDrv->write`
//! — the driver *below* the interpose (:339, :430) — and every byte it reads
//! comes from `pasynOctetDrv->read` (`nextChar`, :103). So the IAC framing of
//! the negotiation is **not** IAC-stuffed by `writeIt`, and the replies are
//! **not** unstuffed by `readIt`. [`ComPortOptions`] therefore takes the lower
//! [`OctetNext`] link directly; it is not reachable through the interpose
//! stack, exactly as in C.
//!
//! One consequence in C is a real defect, and this port does NOT reproduce it
//! (CBUG-B8): because the negotiation bypasses `writeIt`, C never IAC-stuffs the
//! subnegotiation PAYLOAD, so a payload byte that happens to equal 0xFF — a baud
//! rate whose big-endian encoding contains 0xFF, e.g. `baud=255`; a line state of
//! 0xFF — goes out raw and a compliant RFC-2217 server reads it as a command
//! byte and desynchronises. Here the payload is escaped on the way out
//! (`write_subnegotiation`) and un-escaped on the way in (`next_payload_char`),
//! which is RFC 2217 §3. The IAC bytes of the FRAMING (`IAC SB … IAC SE`) are
//! commands and stay raw, in C and here alike.

use std::time::Duration;

use crate::error::{AsynError, AsynResult, AsynStatus};
use crate::interpose::{EomReason, OctetInterpose, OctetNext, OctetReadResult};
use crate::trace::TraceMask;
use crate::user::AsynUser;

// TELNET special characters — C asynInterposeCom.c:32-38.
/// "Interpret As Command".
pub const IAC: u8 = 255;
pub const DONT: u8 = 254;
pub const DO: u8 = 253;
pub const WONT: u8 = 252;
pub const WILL: u8 = 251;
/// Subnegotiation Begin.
pub const SB: u8 = 250;
/// Subnegotiation End.
pub const SE: u8 = 240;

/// Will/Do transmit binary — C :40.
const WD_TRANSMIT_BINARY: u8 = 0;

/// Subnegotiation command port option — C :42.
const SB_COM_PORT_OPTION: u8 = 44;

// COM-PORT-OPTION subcommands — C :43-62.
const CPO_SET_BAUDRATE: u8 = 1;
const CPO_SET_DATASIZE: u8 = 2;
const CPO_SET_PARITY: u8 = 3;
const CPO_PARITY_NONE: u8 = 1;
const CPO_PARITY_ODD: u8 = 2;
const CPO_PARITY_EVEN: u8 = 3;
const CPO_PARITY_MARK: u8 = 4;
const CPO_PARITY_SPACE: u8 = 5;
const CPO_SET_STOPSIZE: u8 = 4;
const CPO_SET_CONTROL: u8 = 5;
const CPO_CONTROL_NOFLOW: u8 = 1;
const CPO_CONTROL_IXON: u8 = 2;
const CPO_CONTROL_HWFLOW: u8 = 3;
const CPO_CONTROL_BREAK_ON: u8 = 5;
const CPO_CONTROL_BREAK_OFF: u8 = 6;
const CPO_SET_MODEMSTATE_MASK: u8 = 11;
const CPO_SERVER_NOTIFY_LINESTATE: u8 = 106;
const CPO_SERVER_NOTIFY_MODEMSTATE: u8 = 107;

/// A server's reply to COM-PORT-OPTION subcommand `n` is `n + 100` — C :450.
const CPO_REPLY_OFFSET: i32 = 100;

/// C `nextChar`'s `EOF` (:105). `int` in C, so a failing read is distinguishable
/// from the byte 0xFF, which is why this type is `i32` and not `u8`.
const EOF: i32 = -1;

/// The timeout on the interpose's *own* `asynUser` (`asynInterposeCom.c:836`).
///
/// It bounds the two negotiations the interpose runs for itself — `restoreSettings`
/// at configure time and from the reconnect `exceptionHandler` — and nothing else.
/// A negotiation driven by a *caller* (an asynRecord option put, an iocsh
/// `asynSetOption`) runs under that caller's `asynUser`, whose timeout C threads
/// all the way down through `setOption` → `sbComPortOption` → `nextChar` (:475,
/// :417-431, :95-106).
const INTERPOSE_USER_TIMEOUT: Duration = Duration::from_secs(2);

/// The interpose's own `asynUser` — C `asynInterposeCom.c:833-836`.
fn interpose_user() -> AsynUser {
    AsynUser::new(0).with_timeout(INTERPOSE_USER_TIMEOUT)
}

/// The option keys C's COM interpose claims — `setOption` (:481-643) and
/// `getOption` (:664-708) test the same seven with `epicsStrCaseCmp`. Anything
/// else falls through to the option interface of the driver below (:645-652,
/// :710-717).
const COM_OPTION_KEYS: [&str; 7] = ["baud", "bits", "parity", "stop", "crtscts", "ixon", "break"];

/// C `%#x` — lowercase hex with an `0x` prefix. printf's `#` flag adds the
/// prefix only for a nonzero value, so 0 prints bare.
fn hash_hex_lower(v: i32) -> String {
    if v == 0 {
        "0".to_string()
    } else {
        format!("0x{v:x}")
    }
}

/// C `%#X` — the uppercase form, used by `expectChar` (:121) and `getOption`'s
/// unknown-flow-code diagnostic (:689).
fn hash_hex_upper(v: i32) -> String {
    if v == 0 {
        "0".to_string()
    } else {
        format!("0X{v:X}")
    }
}

/// C `sscanf(val, "%d", &x)`: skip leading whitespace, take an optional sign and
/// then a run of decimal digits, stopping at the first character that cannot
/// continue the number. A match of zero digits is `sscanf` returning 0, i.e. C's
/// "Bad number" branch. `"9600 baud"` therefore reads 9600, and `"abc"` reads
/// nothing.
///
/// Deviation: a digit run that overflows `i32` is undefined behaviour in C; here
/// it is rejected as "Bad number" rather than silently wrapping.
fn scan_int(s: &str) -> Option<i32> {
    let b = s.as_bytes();
    let mut i = 0;
    while i < b.len() && b[i].is_ascii_whitespace() {
        i += 1;
    }
    let start = i;
    if i < b.len() && (b[i] == b'+' || b[i] == b'-') {
        i += 1;
    }
    let first_digit = i;
    while i < b.len() && b[i].is_ascii_digit() {
        i += 1;
    }
    if i == first_digit {
        return None;
    }
    s[start..i].parse::<i32>().ok()
}

/// C `sscanf(val, "%u", &x)` — as [`scan_int`], into an unsigned.
///
/// Deviation: C's `%u` accepts a leading `-` and wraps ("-1" becomes
/// 4294967295, which as a break length would sleep for 49 days). A negative run
/// is rejected here as "Bad number".
fn scan_uint(s: &str) -> Option<u32> {
    let b = s.as_bytes();
    let mut i = 0;
    while i < b.len() && b[i].is_ascii_whitespace() {
        i += 1;
    }
    let start = i;
    if i < b.len() && b[i] == b'+' {
        i += 1;
    }
    let first_digit = i;
    while i < b.len() && b[i].is_ascii_digit() {
        i += 1;
    }
    if i == first_digit {
        return None;
    }
    s[start..i].parse::<u32>().ok()
}

/// C `sscanf(val, "%g", &x)` — the longest prefix that forms a float, after
/// leading whitespace. Implemented as C specifies it (longest valid prefix)
/// rather than by re-deriving the grammar, so `"1.5xyz"` reads 1.5 and the
/// `inf` / `nan` forms C's `%g` accepts are accepted here too.
fn scan_float(s: &str) -> Option<f32> {
    let t = s.trim_start();
    let mut end = t.len();
    while end > 0 {
        if t.is_char_boundary(end) {
            if let Ok(v) = t[..end].parse::<f32>() {
                return Some(v);
            }
        }
        end -= 1;
    }
    None
}

fn asyn_error(message: impl Into<String>) -> AsynError {
    AsynError::Status {
        status: AsynStatus::Error,
        message: message.into(),
    }
}

// ---------------------------------------------------------------------------
// asynOctet interface: IAC stuffing / unstuffing
// ---------------------------------------------------------------------------

/// The `asynOctet` half of C's COM interpose — IAC doubling on the way out,
/// IAC unstuffing on the way in (C `writeIt`/`readIt`, :136-245).
///
/// Carries no state: C's `interposePvt` fields that `writeIt` touches are only
/// the `xBuf`/`xBufCapacity` scratch buffer (:84-85), which exists to avoid a
/// per-write `malloc` and has no meaning across calls. The serial-line settings
/// live in [`ComPortOptions`], which the octet path never reads.
pub struct ComInterpose;

impl ComInterpose {
    pub fn new() -> Self {
        Self
    }
}

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

/// C `nextChar` (:94-107) — one byte from the layer below, or `Err(message)` when
/// the read fails. C discards the failing `asynStatus` and keeps only the message
/// the lower layer left in `pasynUser->errorMessage`, which is what the caller's
/// `return asynError` then carries out. The message comes back to the caller here
/// because the two callers put it in different places: the negotiation lands it in
/// the user's message slot (C's buffer), while `readIt` discards it and reports
/// "Missing IAC" instead (:226-230).
///
/// Deviation: C does not check `nbytes`, so a lower read that reports success
/// having transferred nothing leaves `char c` uninitialized and C reads garbage.
/// That is treated as EOF here.
fn next_char(next: &mut dyn OctetNext, user: &AsynUser) -> Result<u8, String> {
    let mut c = [0u8; 1];
    match next.read(user, &mut c) {
        Ok(r) if r.nbytes_transferred >= 1 => Ok(c[0]),
        Ok(_) => Err("no data".into()),
        Err(e) => Err(e.message()),
    }
}

impl OctetInterpose for ComInterpose {
    /// C `readIt` (:197-245) — unstuff doubled IACs in place.
    ///
    /// The loop is C's, index-for-pointer. Two arms:
    ///
    /// * the IAC is *not* the last valid byte: its partner is already in the
    ///   buffer, so the count drops by one and the tail slides down over it.
    /// * the IAC *is* the last valid byte: its partner has not arrived yet, so C
    ///   pulls one more byte straight from the device (`nextChar`, bypassing this
    ///   layer) and steps `iac` back one — which makes the `nCheck` arithmetic
    ///   land on zero and end the loop. The count does **not** drop, because the
    ///   partner was never in the buffer to begin with.
    ///
    /// Either way the partner must be another IAC; C rejects anything else with
    /// "Missing IAC" rather than interpreting it as a telnet command, so an
    /// unsolicited `IAC WILL x` arriving mid-stream is an error, not a
    /// negotiation. That refusal is the contract, quirk and all.
    fn read(
        &mut self,
        user: &AsynUser,
        buf: &mut [u8],
        next: &mut dyn OctetNext,
    ) -> AsynResult<OctetReadResult> {
        let maxchars = buf.len();
        let r = next.read(user, buf)?;
        let mut n_read = r.nbytes_transferred;
        let mut eom = r.eom_reason;

        // C's `data` cursor and `nCheck` span. `iac` goes signed because the
        // last-byte arm decrements it below `data` (:220), and `data` itself can
        // be the start of the buffer.
        let mut d: isize = 0;
        let mut n_check: isize = n_read as isize;
        let mut unstuffed = false;

        while n_check > 0 {
            let span = &buf[d as usize..(d + n_check) as usize];
            let Some(rel) = span.iter().position(|&b| b == IAC) else {
                break;
            };
            let mut iac = d + rel as isize;

            unstuffed = true;
            // C :217 — any unstuffing clears CNT: the count the base layer
            // reported is no longer the count being returned.
            eom.remove(EomReason::CNT);

            let c = if iac == d + n_check - 1 {
                // :218-221 — partner not yet read; pull it from the device. C
                // keeps the lower layer's message in `pasynUser->errorMessage`
                // here, but the only exit from this arm overwrites it with
                // "Missing IAC" (:226-230), so the message is dropped on the
                // floor exactly as C drops it.
                let c = next_char(next, user).map_or(EOF, i32::from);
                iac -= 1;
                c
            } else {
                // :222-225 — partner is in the buffer; it is about to be dropped.
                let c = i32::from(buf[(iac + 1) as usize]);
                n_read -= 1;
                c
            };
            if c != i32::from(IAC) {
                // :226-230 — C overwrites the lower layer's message with this
                // one, including when `c` was EOF.
                return Err(asyn_error("Missing IAC"));
            }

            // :231-232
            n_check -= (iac - d) + 2;
            d = iac + 1;
            if n_check == 0 {
                break;
            }
            // :235 — memmove(data, data + 1, nCheck): slide the tail down over
            // the escape byte. Bounds hold because `d + n_check` is invariant
            // under this update minus one, and started at `n_read <= buf.len()`.
            let dst = d as usize;
            buf.copy_within(dst + 1..dst + 1 + n_check as usize, dst);
        }

        // :237-239 — a read that unstuffed anything is traced at
        // ASYN_TRACEIO_FILTER, with the buffer as it now stands. The layer has no
        // handle on the port; the trace config comes through the asynUser, as it
        // does in C (`asynPrintIO(pasynUser, …)`).
        if unstuffed {
            user.print_io(
                TraceMask::IO_FILTER,
                &buf[..n_read],
                &format!("nRead {n_read} after IAC unstuffing"),
            );
        }

        // :240-241 — restore CNT if, after unstuffing, the buffer is still full.
        if n_read == maxchars {
            eom.insert(EomReason::CNT);
        }
        Ok(OctetReadResult {
            nbytes_transferred: n_read,
            eom_reason: eom,
        })
    }

    /// C `writeIt` (:136-195) — double every IAC, then report the count the
    /// *caller* handed us, not the stuffed count.
    ///
    /// C only subtracts the stuffing back out when the lower layer took the
    /// whole stuffed buffer (`if (*nbytesTransfered == numchars)`, :192-193): a
    /// short write reports a count in stuffed-byte units, which the caller reads
    /// as un-stuffed bytes. That subtraction is unreachable on the failure path
    /// (a lower write that transferred every byte returns `asynSuccess`), so a
    /// failing write propagates its partial count untouched, as C's does.
    fn write(
        &mut self,
        user: &mut AsynUser,
        data: &[u8],
        next: &mut dyn OctetNext,
    ) -> AsynResult<usize> {
        let n_iac = data.iter().filter(|&&b| b == IAC).count();
        if n_iac == 0 {
            // C :146 — no IAC in the payload, so `data` goes down verbatim and
            // `nIAC` stays 0, making the tail adjustment a no-op.
            return next.write(user, data);
        }
        let mut stuffed = Vec::with_capacity(data.len() + n_iac);
        for &b in data {
            stuffed.push(b);
            if b == IAC {
                stuffed.push(IAC);
            }
        }
        let n = next.write(user, &stuffed)?;
        Ok(if n == stuffed.len() { n - n_iac } else { n })
    }

    fn flush(&mut self, user: &mut AsynUser, next: &mut dyn OctetNext) -> AsynResult<()> {
        // C `flushIt` (:247-253) — straight delegation.
        next.flush(user)
    }
}

// ---------------------------------------------------------------------------
// asynOption interface: the telnet negotiation
// ---------------------------------------------------------------------------

/// The negotiation's view of the link below the interpose, and the `asynUser`
/// whose message slot every C layer writes into.
///
/// The slot is [`AsynUser::error_message`] — C's `pasynUser->errorMessage`
/// itself, not a copy: `nextChar` leaves the lower read's message there on EOF
/// (:103-106), `expectChar` overwrites it on a mismatch (:120-122), `setOption`
/// leaves a *non-failing* advisory there (:571-573, :587-589), and each `return
/// asynError` carries out whatever it currently holds. Keeping the negotiation's
/// messages in the caller's user is what lets a failing read surface the *lower
/// driver's* message with C's `asynError` status, and what gives the advisories
/// somewhere to live at all — a `Result` cannot carry a message that does not
/// fail the call.
struct TelnetLink<'a> {
    next: &'a mut dyn OctetNext,
    /// The `asynUser` C threads through the whole negotiation — the *caller's*
    /// on `setOption` (an asynRecord option put carries TMOT; an iocsh
    /// `asynSetOption` carries its own 2 s, `asynShellCommands.c:119`), the
    /// interpose's own on `restoreSettings` ([`interpose_user`]). Its `timeout`
    /// bounds every wire read the handshake performs, so the negotiation has no
    /// timeout of its own to disagree with the caller's.
    user: &'a mut AsynUser,
}

impl<'a> TelnetLink<'a> {
    fn new(next: &'a mut dyn OctetNext, user: &'a mut AsynUser) -> Self {
        Self { next, user }
    }

    fn next_char(&mut self) -> i32 {
        match next_char(self.next, self.user) {
            Ok(b) => i32::from(b),
            Err(msg) => {
                self.user.error_message = msg;
                EOF
            }
        }
    }

    /// C's `epicsSnprintf(pasynUser->errorMessage, …)` on a path that does *not*
    /// return an error (:571-573, :587-589): the message is left for the caller
    /// to find and the call carries on.
    fn advise(&mut self, msg: &str) {
        self.user.error_message = msg.to_string();
    }

    /// C `expectChar` (:112-125) — fetch and compare. Returns false on EOF
    /// *without* touching the message (so the lower read's message survives),
    /// and false-with-message on a mismatch.
    fn expect_char(&mut self, expect: u8) -> bool {
        let c = self.next_char();
        if c == EOF {
            return false;
        }
        if c != i32::from(expect) {
            self.user.error_message = format!(
                "Expected {}, got {}",
                hash_hex_upper(i32::from(expect)),
                hash_hex_upper(c)
            );
            return false;
        }
        true
    }

    /// One PAYLOAD byte of a subnegotiation, with RFC 2217's IAC escape undone:
    /// a data byte equal to 0xFF travels as `IAC IAC`.
    ///
    /// DEVIATION from C, deliberate — CBUG-B8. See [`Self::write_subnegotiation`]
    /// for the write half. C reads every negotiation byte with a bare `nextChar`
    /// (:103), so a compliant server's escaped 0xFF is read as two bytes: the
    /// value byte comes out right but the frame is one byte long and the trailing
    /// `IAC SE` check fails. Reading the payload through this — the same escape
    /// rule the write half applies — is what makes a 0xFF round-trip at all.
    ///
    /// An IAC followed by anything else is a command in the middle of a payload:
    /// a framing error, reported as such rather than silently taken as data.
    fn next_payload_char(&mut self) -> i32 {
        let c = self.next_char();
        if c != i32::from(IAC) {
            return c;
        }
        let c2 = self.next_char();
        if c2 == i32::from(IAC) {
            return i32::from(IAC);
        }
        if c2 != EOF {
            self.user.error_message = format!(
                "Unescaped IAC in a COM-PORT-OPTION payload, followed by {}",
                hash_hex_upper(c2)
            );
        }
        EOF
    }

    /// C's `return asynError` — the status is always `asynError`, the message is
    /// whatever is in the slot.
    fn error(&self) -> AsynError {
        asyn_error(self.user.error_message.clone())
    }

    /// `IAC SB 44 <payload, IAC-stuffed> IAC SE` — RFC 2217 §3 framing.
    ///
    /// DEVIATION from C, deliberate — CBUG-B8. C builds this frame at :424-431
    /// and hands it to `pasynOctetDrv->write`, the driver BELOW the interpose,
    /// so it never passes through the interpose's own `writeIt` (:146-182) —
    /// the function that doubles IAC bytes. The payload is exactly where a 0xFF
    /// can occur: `CPO_SET_BAUDRATE` sends the rate as four big-endian bytes
    /// (:491), so `asynSetOption(port, 0, "baud", "255")` puts a raw 0xFF into
    /// the payload and a compliant terminal server reads it as a command byte
    /// and desynchronises. Only the payload is stuffed here; the IAC bytes of
    /// the framing itself are commands and must stay raw.
    fn write_subnegotiation(&mut self, payload: &[u8]) -> AsynResult<usize> {
        let mut cbuf = Vec::with_capacity(5 + payload.len());
        cbuf.extend_from_slice(&[IAC, SB, SB_COM_PORT_OPTION]);
        for &b in payload {
            cbuf.push(b);
            if b == IAC {
                cbuf.push(IAC);
            }
        }
        cbuf.extend_from_slice(&[IAC, SE]);
        self.write(&cbuf)
    }

    fn write(&mut self, bytes: &[u8]) -> AsynResult<usize> {
        self.next.write(self.user, bytes)
    }

    /// C `willdo` (:327-411). Two shapes depending on `command`:
    ///
    /// 1. `WILL x` — tell the server we will do x, and require it to answer
    ///    `DO x`. A `DONT x` is a refusal; a `WILL`/`WONT x` echoed back is a
    ///    protocol error ("Received response ... in response to WILL").
    /// 2. `DO x` — tell the server to do x, and require it to answer `WILL x`.
    ///    Mirror-image errors.
    ///
    /// Everything else on the wire is skipped: a reply about a *different*
    /// option code is ignored and the scan resumes, a bare `IAC`/`IAC SE` is
    /// ignored, and an `IAC SB 44 <NOTIFY-LINESTATE|NOTIFY-MODEMSTATE> <state>`
    /// is consumed and discarded — the server may volunteer those at any point.
    /// A byte that is none of the above ends the negotiation with "Unexpected
    /// character".
    fn willdo(&mut self, command: u8, code: u8) -> AsynResult<()> {
        // :336-340
        self.write(&[IAC, command, code])?;
        loop {
            // :344-346 — skip to the next IAC.
            loop {
                let c = self.next_char();
                if c == EOF {
                    return Err(self.error());
                }
                if c == i32::from(IAC) {
                    break;
                }
            }
            let c = self.next_char();
            if c == EOF {
                return Err(self.error());
            }
            match c as u8 {
                // :349-350 — a doubled IAC or a stray SE: resume scanning.
                IAC | SE => {}
                DO | DONT => {
                    // :352-370
                    let wd = c as u8;
                    let opt = self.next_char();
                    if opt == EOF {
                        return Err(self.error());
                    }
                    if opt != i32::from(code) {
                        continue;
                    }
                    if command == DO {
                        self.user.error_message = format!(
                            "Received response {} in response to DO.",
                            hash_hex_lower(opt)
                        );
                        return Err(self.error());
                    }
                    if wd == DONT {
                        self.user.error_message =
                            format!("Device says DON'T {}.", hash_hex_lower(opt));
                        return Err(self.error());
                    }
                    return Ok(());
                }
                WILL | WONT => {
                    // :372-390
                    let wd = c as u8;
                    let opt = self.next_char();
                    if opt == EOF {
                        return Err(self.error());
                    }
                    if opt != i32::from(code) {
                        continue;
                    }
                    if command == WILL {
                        self.user.error_message = format!(
                            "Received response {} in response to WILL.",
                            hash_hex_lower(opt)
                        );
                        return Err(self.error());
                    }
                    if wd == WONT {
                        self.user.error_message =
                            format!("Device says WON'T {}.", hash_hex_lower(opt));
                        return Err(self.error());
                    }
                    return Ok(());
                }
                SB => {
                    // :392-403. Note that C does not error out on EOF at :393 —
                    // EOF is simply not SB_COM_PORT_OPTION, so it breaks back to
                    // the scan loop, whose own `nextChar` then returns EOF and
                    // errors. Same outcome, one read later.
                    if self.next_char() != i32::from(SB_COM_PORT_OPTION) {
                        continue;
                    }
                    let c = self.next_char();
                    if c != i32::from(CPO_SERVER_NOTIFY_LINESTATE)
                        && c != i32::from(CPO_SERVER_NOTIFY_MODEMSTATE)
                    {
                        if c == EOF {
                            return Err(self.error());
                        }
                        continue;
                    }
                    // The state byte — payload, so IAC-escaped (CBUG-B8; a line
                    // state of 0xFF is a legal value). C consumes exactly one
                    // raw byte and does *not* consume the trailing IAC SE here —
                    // it lets the scan loop step over them (the IAC is found, and
                    // SE hits the `case C_SE: break` arm).
                    if self.next_payload_char() == EOF {
                        return Err(self.error());
                    }
                }
                _ => {
                    // :405-408
                    self.user.error_message =
                        format!("Unexpected character {} in TELNET reply", hash_hex_lower(c));
                    return Err(self.error());
                }
            }
        }
    }

    /// C `sbComPortOption` (:416-469) — send `IAC SB 44 <x…> IAC SE` and collect
    /// the server's `x.len() - 1` reply bytes into `r`.
    ///
    /// The server acknowledges subcommand `n` with `n + 100` (:450), echoing the
    /// value it actually applied — which may differ from the one requested, and
    /// which the callers below then check. An `IAC SB 44 <NOTIFY-…> <state> IAC
    /// SE` arriving instead is consumed and the wait resumes; any other reply
    /// code is an error.
    ///
    /// The payload `x` is IAC-stuffed and the reply payload is un-stuffed —
    /// CBUG-B8, a deliberate deviation from C, which does neither. See
    /// [`Self::write_subnegotiation`] and [`Self::next_payload_char`].
    fn sb_com_port_option(&mut self, x: &[u8], r: &mut [u8]) -> AsynResult<()> {
        debug_assert!(!x.is_empty() && r.len() >= x.len() - 1);
        // :424-431
        self.write_subnegotiation(x)?;

        loop {
            // :435-438
            loop {
                let c = self.next_char();
                if c == EOF {
                    return Err(self.error());
                }
                if c == i32::from(IAC) {
                    break;
                }
            }
            // :439-441
            if !self.expect_char(SB) || !self.expect_char(SB_COM_PORT_OPTION) {
                return Err(self.error());
            }
            let c = self.next_char();
            if c == i32::from(CPO_SERVER_NOTIFY_LINESTATE)
                || c == i32::from(CPO_SERVER_NOTIFY_MODEMSTATE)
            {
                // :443-449 — an unsolicited line/modem-state notification:
                // <state> IAC SE, discarded, and we keep waiting for our reply.
                // The state is payload, so it carries the IAC escape (CBUG-B8).
                if self.next_payload_char() == EOF
                    || !self.expect_char(IAC)
                    || !self.expect_char(SE)
                {
                    return Err(self.error());
                }
            } else if c == i32::from(x[0]) + CPO_REPLY_OFFSET {
                // :450-459 — `while (--xLen > 0)`: one reply byte per payload
                // byte after the subcommand. These are DATA, so they carry the
                // IAC escape (CBUG-B8) — C reads them raw.
                for slot in r.iter_mut().take(x.len() - 1) {
                    let b = self.next_payload_char();
                    if b == EOF {
                        return Err(self.error());
                    }
                    *slot = b as u8;
                }
                if !self.expect_char(IAC) || !self.expect_char(SE) {
                    return Err(self.error());
                }
                return Ok(());
            } else {
                // :461-465 — C prints `c` with %d, so an EOF here reports -1.
                self.user.error_message =
                    format!("Sent COM-PORT-OPTION {} but got reply {}", x[0], c);
                return Err(self.error());
            }
        }
    }
}

/// The `asynOption` half of C's COM interpose: the serial-line settings the
/// negotiation carries, and the negotiation itself.
///
/// The fields are C's `interposePvt` option state (:77-82) and hold what the
/// **server** last acknowledged, not what was last requested — every setter
/// stores the value echoed back in the reply.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ComPortOptions {
    baud: i32,
    bits: i32,
    /// One of the `CPO_PARITY_*` codes.
    parity: u8,
    stop: i32,
    /// One of the `CPO_CONTROL_*` codes.
    flow: u8,
    break_active: bool,
}

impl Default for ComPortOptions {
    /// C :837-841 — the defaults `asynInterposeCOM` installs before its first
    /// `restoreSettings`.
    fn default() -> Self {
        Self {
            baud: 9600,
            bits: 8,
            parity: CPO_PARITY_NONE,
            stop: 1,
            flow: CPO_CONTROL_NOFLOW,
            break_active: false,
        }
    }
}

impl ComPortOptions {
    pub fn new() -> Self {
        Self::default()
    }

    /// Whether this layer answers `key` itself. A key outside the set falls
    /// through to the option interface of the driver below, which is what C does
    /// at :645-652 (`setOption`) and :710-717 (`getOption`).
    pub fn owns_key(key: &str) -> bool {
        COM_OPTION_KEYS.iter().any(|k| key.eq_ignore_ascii_case(k))
    }

    /// C `getOption` (:657-725). Reads back cached state only — no wire traffic,
    /// so this needs no link.
    ///
    /// Caller must have checked [`Self::owns_key`]; a key outside the set is C's
    /// delegate-to-lower-driver branch and is not this function's to answer.
    pub fn get_option(&self, key: &str) -> AsynResult<String> {
        if key.eq_ignore_ascii_case("baud") {
            Ok(self.baud.to_string())
        } else if key.eq_ignore_ascii_case("bits") {
            Ok(self.bits.to_string())
        } else if key.eq_ignore_ascii_case("parity") {
            // :671-677 — C's switch has no default: a parity code outside the
            // five leaves `val` untouched (`l` stays 0) and still returns
            // asynSuccess, handing the caller back an unwritten buffer. "Nothing
            // written" is the empty string here. It is reachable — `parity` holds
            // whatever the server echoed — and either way the value fails the
            // next `setOption("parity", …)` with "Invalid parity selection".
            Ok(match self.parity {
                CPO_PARITY_NONE => "none",
                CPO_PARITY_EVEN => "even",
                CPO_PARITY_ODD => "odd",
                CPO_PARITY_MARK => "mark",
                CPO_PARITY_SPACE => "space",
                _ => "",
            }
            .to_string())
        } else if key.eq_ignore_ascii_case("stop") {
            Ok(self.stop.to_string())
        } else if key.eq_ignore_ascii_case("crtscts") {
            // :682-691 — XON/XOFF reports as "no hardware flow control".
            match self.flow {
                CPO_CONTROL_NOFLOW | CPO_CONTROL_IXON => Ok("N".to_string()),
                CPO_CONTROL_HWFLOW => Ok("Y".to_string()),
                other => Err(asyn_error(format!(
                    "Unknown flow control code {}",
                    hash_hex_upper(i32::from(other))
                ))),
            }
        } else if key.eq_ignore_ascii_case("ixon") {
            // :693-703 — and hardware flow control reports as "no XON/XOFF".
            match self.flow {
                CPO_CONTROL_NOFLOW | CPO_CONTROL_HWFLOW => Ok("N".to_string()),
                CPO_CONTROL_IXON => Ok("Y".to_string()),
                other => Err(asyn_error(format!(
                    "Unknown flow control code {}",
                    hash_hex_upper(i32::from(other))
                ))),
            }
        } else if key.eq_ignore_ascii_case("break") {
            // :704-708
            Ok(if self.break_active { "on" } else { "off" }.to_string())
        } else {
            Err(AsynError::OptionNotFound(key.to_string()))
        }
    }

    /// The SET-CONTROL byte to transmit when `key_mode` is being turned OFF.
    ///
    /// **DEVIATION from C, deliberate — CBUG-B6.** C's `crtscts` and `ixon`
    /// branches both implement "n" as `xBuf[1] = pinterposePvt->flow`
    /// (`asynInterposeCom.c:575`, `:591`) — the value transmitted for "turn this
    /// off" is the port's **current** flow-control mode. So if RTS/CTS is on and
    /// you send `crtscts N`, C re-transmits SET-CONTROL HWFLOW, the server
    /// confirms HWFLOW, `:578` writes it back into `flow`, and `getOption` still
    /// answers "Y". Flow control can be turned on and never off; recovery needs
    /// an IOC restart or a terminal-server power cycle.
    ///
    /// `CPO_CONTROL_NOFLOW` ("No flow control", `:53`) is defined and is decoded
    /// in `getOption` (`:684`, `:695`) — it is simply never assigned to
    /// `xBuf[1]` anywhere in the file. Transmitting it is the intended
    /// behaviour, and it is what this does.
    ///
    /// The mode byte carries ONE flow-control mode, so the modes are mutually
    /// exclusive (which is why C advises "XON/XOFF already set. Now using
    /// RTS/CTS."). Disabling therefore means: if `key_mode` is what is currently
    /// in effect, turn flow control off; if some other mode is in effect, this
    /// key's mode is already off and the other one is not ours to touch.
    fn flow_mode_off(&self, key_mode: u8) -> u8 {
        if self.flow == key_mode {
            CPO_CONTROL_NOFLOW
        } else {
            self.flow
        }
    }

    /// C `setOption` (:474-655) — negotiate `key`/`val` with the server over
    /// `next`, the octet driver *below* the interpose, bounded by `user`'s
    /// timeout.
    ///
    /// `user` is the caller's `asynUser`, as in C: `setOption(drvPvt, pasynUser,
    /// key, val)` hands it to `sbComPortOption` (:495), which writes and reads the
    /// wire with it (:431, :435-457). An asynRecord option put therefore
    /// negotiates under TMOT and an iocsh `asynSetOption` under its own 2 s
    /// (`asynShellCommands.c:119`) — the layer has no timeout of its own.
    ///
    /// Caller must have checked [`Self::owns_key`].
    pub fn set_option(
        &mut self,
        user: &mut AsynUser,
        next: &mut dyn OctetNext,
        key: &str,
        val: &str,
    ) -> AsynResult<()> {
        let mut link = TelnetLink::new(next, user);
        self.set_option_on(&mut link, key, val)
    }

    /// C `restoreSettings` (:729-758) — the full handshake, run at connect and on
    /// every reconnect (C wires it to `asynExceptionConnect`, :763-774).
    ///
    /// `IAC DO BINARY`, `IAC WILL BINARY`, `IAC WILL COM-PORT-OPTION`, then a
    /// SET-MODEMSTATE-MASK of 0 (asking the server not to volunteer modem-state
    /// notifications), and finally each cached setting is read back out and
    /// pushed to the server — which is how the line comes up configured after a
    /// terminal server reboots. `break` is deliberately not in C's key list: a
    /// break is a momentary line condition, not a setting to restore.
    /// Runs on the interpose's own `asynUser` (C creates it at :833-836 and hands
    /// it to `restoreSettings` from both the configure path (:851) and the
    /// reconnect `exceptionHandler` (:770)), so this is the one negotiation with a
    /// timeout of its own — 2 s — and no caller to inherit one from.
    pub fn restore_settings(&mut self, next: &mut dyn OctetNext) -> AsynResult<()> {
        let mut user = interpose_user();
        let mut link = TelnetLink::new(next, &mut user);
        self.restore_settings_on(&mut link)
    }

    fn restore_settings_on(&mut self, link: &mut TelnetLink) -> AsynResult<()> {
        // :740-747
        link.willdo(DO, WD_TRANSMIT_BINARY)?;
        link.willdo(WILL, WD_TRANSMIT_BINARY)?;
        link.willdo(WILL, SB_COM_PORT_OPTION)?;
        let mut r = [0u8; 1];
        link.sb_com_port_option(&[CPO_SET_MODEMSTATE_MASK, 0], &mut r)?;

        // :749-756 — note `break` is absent from C's list.
        for key in ["baud", "bits", "parity", "stop", "crtscts", "ixon"] {
            let val = self.get_option(key)?;
            self.set_option_on(link, key, &val)?;
        }
        Ok(())
    }

    fn set_option_on(&mut self, link: &mut TelnetLink, key: &str, val: &str) -> AsynResult<()> {
        if key.eq_ignore_ascii_case("baud") {
            // :481-508 — the rate goes out big-endian in four bytes, and the
            // server echoes the rate it actually applied. C fails the call when
            // they differ rather than silently running at the wrong speed.
            let Some(b) = scan_int(val) else {
                return Err(asyn_error("Bad number"));
            };
            let baud = b as u32;
            let x = [
                CPO_SET_BAUDRATE,
                (baud >> 24) as u8,
                (baud >> 16) as u8,
                (baud >> 8) as u8,
                baud as u8,
            ];
            let mut r = [0u8; 4];
            link.sb_com_port_option(&x, &mut r)?;
            self.baud = i32::from_be_bytes(r);
            if self.baud != b {
                return Err(asyn_error(format!(
                    "Tried to set {b} baud, actually set {} baud.",
                    self.baud
                )));
            }
            Ok(())
        } else if key.eq_ignore_ascii_case("bits") {
            // :509-528
            let Some(b) = scan_int(val) else {
                return Err(asyn_error("Bad number"));
            };
            let mut r = [0u8; 1];
            link.sb_com_port_option(&[CPO_SET_DATASIZE, b as u8], &mut r)?;
            self.bits = i32::from(r[0]);
            if self.bits != b {
                return Err(asyn_error(format!(
                    "Tried to set {b} bits, actually set {} bits.",
                    self.bits
                )));
            }
            Ok(())
        } else if key.eq_ignore_ascii_case("parity") {
            // :529-543 — and note C does *not* check the echo here, unlike baud,
            // bits and stop: whatever the server answers becomes the cached
            // parity, even if it is not what was asked for.
            let code = if val.eq_ignore_ascii_case("none") {
                CPO_PARITY_NONE
            } else if val.eq_ignore_ascii_case("even") {
                CPO_PARITY_EVEN
            } else if val.eq_ignore_ascii_case("odd") {
                CPO_PARITY_ODD
            } else if val.eq_ignore_ascii_case("mark") {
                CPO_PARITY_MARK
            } else if val.eq_ignore_ascii_case("space") {
                CPO_PARITY_SPACE
            } else {
                return Err(asyn_error("Invalid parity selection"));
            };
            let mut r = [0u8; 1];
            link.sb_com_port_option(&[CPO_SET_PARITY, code], &mut r)?;
            self.parity = r[0];
            Ok(())
        } else if key.eq_ignore_ascii_case("stop") {
            // :544-568 — parsed as a float (so "1.5" is *read*), then required to
            // be exactly 1 or 2, then truncated to a byte.
            let Some(b) = scan_float(val) else {
                return Err(asyn_error("Bad number"));
            };
            if b != 1.0 && b != 2.0 {
                // C's message has the double space.
                return Err(asyn_error("Bad  stop bit count"));
            }
            let mut r = [0u8; 1];
            link.sb_com_port_option(&[CPO_SET_STOPSIZE, b as u8], &mut r)?;
            self.stop = i32::from(r[0]);
            if self.stop as f32 != b {
                return Err(asyn_error(format!(
                    "Tried to set {b} stop bits, actually set {} stop bits.",
                    self.stop
                )));
            }
            Ok(())
        } else if key.eq_ignore_ascii_case("crtscts") {
            // :569-584 — "y" turns hardware flow control on. C's "n" re-sends the
            // *current* flow mode; see `flow_mode_for` — CBUG-B6.
            //
            // :571-573 — switching to RTS/CTS over a live XON/XOFF setting is
            // announced in the message slot and does *not* fail the call.
            if self.flow == CPO_CONTROL_IXON {
                link.advise("XON/XOFF already set. Now using RTS/CTS.");
            }
            let mode = if val.eq_ignore_ascii_case("n") {
                self.flow_mode_off(CPO_CONTROL_HWFLOW)
            } else if val.eq_ignore_ascii_case("y") {
                CPO_CONTROL_HWFLOW
            } else {
                return Err(asyn_error("Bad  value"));
            };
            let mut r = [0u8; 1];
            link.sb_com_port_option(&[CPO_SET_CONTROL, mode], &mut r)?;
            self.flow = r[0];
            Ok(())
        } else if key.eq_ignore_ascii_case("ixon") {
            // :585-604 — mirror of crtscts, advisory (:587-589) included.
            if self.flow == CPO_CONTROL_HWFLOW {
                link.advise("RTS/CTS already set. Now using XON/XOFF.");
            }
            let mode = if val.eq_ignore_ascii_case("n") {
                self.flow_mode_off(CPO_CONTROL_IXON)
            } else if val.eq_ignore_ascii_case("y") {
                CPO_CONTROL_IXON
            } else {
                // DEVIATION. C writes "Bad option value" into the message and
                // then *falls through* without returning (:593-596), sending a
                // subnegotiation whose payload byte is the uninitialized
                // `xBuf[1]` — undefined behaviour, and an arbitrary flow-control
                // mode on the wire. Refusing the value is the only behaviour that
                // is both defined and safe; there is nothing to be faithful to.
                return Err(asyn_error("Bad option value"));
            };
            let mut r = [0u8; 1];
            match link.sb_com_port_option(&[CPO_SET_CONTROL, mode], &mut r) {
                Ok(()) => {
                    self.flow = r[0];
                    Ok(())
                }
                Err(e) => {
                    // :601-603 — and only on this key does C print the failure to
                    // stdout as well as returning it.
                    println!("XON/XOFF not set.");
                    Err(e)
                }
            }
        } else if key.eq_ignore_ascii_case("break") {
            self.set_break(link, val)
        } else {
            Err(AsynError::OptionNotFound(key.to_string()))
        }
    }

    /// C `setOption`'s `break` arm (:605-643) — assert / release the line break.
    ///
    /// `"on"` and `"off"` are edge-triggered against the cached state (asserting
    /// an already-asserted break sends nothing). Anything else is read as a
    /// millisecond count and runs the whole cycle: assert, sleep, release —
    /// blocking the caller for the duration, as C's `epicsThreadSleep` does. An
    /// empty value takes that path too, with C's 250 ms default.
    ///
    /// The cached flag tracks what the *server* acknowledged: it is set from the
    /// reply byte, not from the request.
    fn set_break(&mut self, link: &mut TelnetLink, val: &str) -> AsynResult<()> {
        let (on, off, sleep_for) = if val.eq_ignore_ascii_case("on") {
            (!self.break_active, false, None)
        } else if val.eq_ignore_ascii_case("off") {
            (false, self.break_active, None)
        } else {
            let break_len = if val.is_empty() {
                0
            } else {
                let Some(n) = scan_uint(val) else {
                    return Err(asyn_error("Bad number"));
                };
                n
            };
            // :632 — a zero/absent length is 250 ms.
            let ms = if break_len == 0 { 250 } else { break_len };
            (!self.break_active, true, Some(ms))
        };

        if on {
            let mut r = [0u8; 1];
            link.sb_com_port_option(&[CPO_SET_CONTROL, CPO_CONTROL_BREAK_ON], &mut r)?;
            self.break_active = r[0] == CPO_CONTROL_BREAK_ON;
        }
        if let Some(ms) = sleep_for {
            std::thread::sleep(Duration::from_millis(u64::from(ms)));
        }
        if off {
            let mut r = [0u8; 1];
            link.sb_com_port_option(&[CPO_SET_CONTROL, CPO_CONTROL_BREAK_OFF], &mut r)?;
            // :640 — yes, C tests the reply against BREAK_ON here too, so a
            // server that echoes BREAK_OFF leaves the flag clear and one that
            // echoes BREAK_ON leaves it set. Same predicate as the assert arm.
            self.break_active = r[0] == CPO_CONTROL_BREAK_ON;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::interpose::OctetInterposeStack;
    use std::collections::VecDeque;

    /// A scripted terminal server: records everything written, and replays a
    /// queued byte stream one byte at a time (which is how `nextChar` reads).
    struct FakeServer {
        written: Vec<u8>,
        to_read: VecDeque<u8>,
    }

    impl FakeServer {
        fn new(reply: &[u8]) -> Self {
            Self {
                written: Vec::new(),
                to_read: reply.iter().copied().collect(),
            }
        }
    }

    impl OctetNext for FakeServer {
        fn read(&mut self, _user: &AsynUser, buf: &mut [u8]) -> AsynResult<OctetReadResult> {
            if buf.is_empty() {
                return Ok(OctetReadResult {
                    nbytes_transferred: 0,
                    eom_reason: EomReason::empty(),
                });
            }
            let mut n = 0;
            while n < buf.len() {
                match self.to_read.pop_front() {
                    Some(b) => {
                        buf[n] = b;
                        n += 1;
                    }
                    None => break,
                }
            }
            if n == 0 {
                return Err(AsynError::Status {
                    status: AsynStatus::Timeout,
                    message: "read timeout".into(),
                });
            }
            Ok(OctetReadResult {
                nbytes_transferred: n,
                eom_reason: if n == buf.len() {
                    EomReason::CNT
                } else {
                    EomReason::empty()
                },
            })
        }

        fn write(&mut self, _user: &mut AsynUser, data: &[u8]) -> AsynResult<usize> {
            self.written.extend_from_slice(data);
            Ok(data.len())
        }

        fn flush(&mut self, _user: &mut AsynUser) -> AsynResult<()> {
            Ok(())
        }
    }

    /// The server's acknowledgement of subcommand `x[0]`: `IAC SB 44 <x[0]+100>
    /// <values…> IAC SE`.
    fn ack(subcmd: u8, values: &[u8]) -> Vec<u8> {
        let mut v = vec![IAC, SB, SB_COM_PORT_OPTION, subcmd + 100];
        v.extend_from_slice(values);
        v.extend_from_slice(&[IAC, SE]);
        v
    }

    // -- asynOctet: IAC stuffing ------------------------------------------

    #[test]
    fn write_doubles_iac_and_reports_the_unstuffed_count() {
        let mut stack = OctetInterposeStack::new(false);
        stack.install(-1, Box::new(ComInterpose::new()));
        let mut base = FakeServer::new(&[]);
        let mut user = AsynUser::default();

        let n = stack
            .dispatch_write(&mut user, &[b'A', IAC, b'B'], &mut base)
            .unwrap();
        // C writeIt :175 — the escape byte goes on the wire...
        assert_eq!(base.written, vec![b'A', IAC, IAC, b'B']);
        // ...but :192-193 subtracts it back out, so the caller sees the count it
        // handed in, not the stuffed count.
        assert_eq!(n, 3);
    }

    #[test]
    fn write_without_iac_is_verbatim_passthrough() {
        let mut stack = OctetInterposeStack::new(false);
        stack.install(-1, Box::new(ComInterpose::new()));
        let mut base = FakeServer::new(&[]);
        let mut user = AsynUser::default();

        let n = stack
            .dispatch_write(&mut user, b"HELLO", &mut base)
            .unwrap();
        assert_eq!(base.written, b"HELLO");
        assert_eq!(n, 5);
    }

    #[test]
    fn write_stuffs_every_iac_in_a_run() {
        let mut stack = OctetInterposeStack::new(false);
        stack.install(-1, Box::new(ComInterpose::new()));
        let mut base = FakeServer::new(&[]);
        let mut user = AsynUser::default();

        let n = stack
            .dispatch_write(&mut user, &[IAC, IAC, IAC], &mut base)
            .unwrap();
        assert_eq!(base.written, vec![IAC, IAC, IAC, IAC, IAC, IAC]);
        assert_eq!(n, 3);
    }

    /// C `writeIt` :192-193 only subtracts the stuffing when the lower layer took
    /// the *whole stuffed buffer*. A short write reports the count in stuffed
    /// bytes — a genuine C wart, pinned here so a "helpful" correction to it
    /// would fail.
    #[test]
    fn short_write_reports_the_stuffed_count_c_reports() {
        struct ShortWrite;
        impl OctetNext for ShortWrite {
            fn read(&mut self, _u: &AsynUser, _b: &mut [u8]) -> AsynResult<OctetReadResult> {
                unreachable!()
            }
            fn write(&mut self, _u: &mut AsynUser, data: &[u8]) -> AsynResult<usize> {
                Ok(data.len() - 1)
            }
            fn flush(&mut self, _u: &mut AsynUser) -> AsynResult<()> {
                Ok(())
            }
        }
        let mut stack = OctetInterposeStack::new(false);
        stack.install(-1, Box::new(ComInterpose::new()));
        let mut base = ShortWrite;
        let mut user = AsynUser::default();

        // 3 caller bytes -> 4 stuffed bytes -> lower takes 3 -> C reports 3, NOT
        // 3 - nIAC = 2.
        let n = stack
            .dispatch_write(&mut user, &[b'A', IAC, b'B'], &mut base)
            .unwrap();
        assert_eq!(n, 3);
    }

    // -- asynOctet: IAC unstuffing ----------------------------------------

    #[test]
    fn read_unstuffs_a_doubled_iac_inside_the_buffer() {
        let mut stack = OctetInterposeStack::new(false);
        stack.install(-1, Box::new(ComInterpose::new()));
        let mut base = FakeServer::new(&[b'A', IAC, IAC, b'B']);
        let user = AsynUser::default();
        let mut buf = [0u8; 8];

        let r = stack.dispatch_read(&user, &mut buf, &mut base).unwrap();
        assert_eq!(r.nbytes_transferred, 3);
        assert_eq!(&buf[..3], &[b'A', IAC, b'B']);
    }

    /// C `readIt` :218-221 — the escape's partner has not arrived yet, so C pulls
    /// one more byte *from the device*, bypassing this layer, and the returned
    /// count does not drop (the partner was never in the buffer).
    #[test]
    fn read_pulls_the_partner_from_the_device_when_iac_lands_last() {
        let mut stack = OctetInterposeStack::new(false);
        stack.install(-1, Box::new(ComInterpose::new()));
        // A 2-byte buffer takes [A, IAC]; the partner IAC is still on the wire.
        let mut base = FakeServer::new(&[b'A', IAC, IAC]);
        let user = AsynUser::default();
        let mut buf = [0u8; 2];

        let r = stack.dispatch_read(&user, &mut buf, &mut base).unwrap();
        assert_eq!(r.nbytes_transferred, 2);
        assert_eq!(&buf[..2], &[b'A', IAC]);
        assert!(base.to_read.is_empty(), "the partner byte was consumed");
    }

    /// The same arm with the IAC at offset 0 — C decrements `iac` to *before* the
    /// buffer (:220), which is exactly what makes the `nCheck` arithmetic land on
    /// zero. Signed cursor, or this underflows.
    #[test]
    fn read_handles_a_lone_iac_as_the_only_byte() {
        let mut stack = OctetInterposeStack::new(false);
        stack.install(-1, Box::new(ComInterpose::new()));
        let mut base = FakeServer::new(&[IAC, IAC]);
        let user = AsynUser::default();
        let mut buf = [0u8; 1];

        let r = stack.dispatch_read(&user, &mut buf, &mut base).unwrap();
        assert_eq!(r.nbytes_transferred, 1);
        assert_eq!(buf[0], IAC);
    }

    #[test]
    fn read_unstuffs_consecutive_escapes() {
        let mut stack = OctetInterposeStack::new(false);
        stack.install(-1, Box::new(ComInterpose::new()));
        let mut base = FakeServer::new(&[IAC, IAC, IAC, IAC, b'Z']);
        let user = AsynUser::default();
        let mut buf = [0u8; 8];

        let r = stack.dispatch_read(&user, &mut buf, &mut base).unwrap();
        assert_eq!(r.nbytes_transferred, 3);
        assert_eq!(&buf[..3], &[IAC, IAC, b'Z']);
    }

    /// C :226-230 — a lone IAC followed by anything other than IAC is an error,
    /// *not* a telnet command to interpret. An unsolicited `IAC WILL x` in the
    /// data stream therefore fails the read.
    #[test]
    fn read_rejects_an_unescaped_iac() {
        let mut stack = OctetInterposeStack::new(false);
        stack.install(-1, Box::new(ComInterpose::new()));
        let mut base = FakeServer::new(&[b'A', IAC, WILL, 0]);
        let user = AsynUser::default();
        let mut buf = [0u8; 8];

        let err = stack.dispatch_read(&user, &mut buf, &mut base).unwrap_err();
        assert_eq!(err.status(), AsynStatus::Error);
        assert_eq!(err.message(), "Missing IAC");
    }

    /// C :217 + :240-241 — unstuffing clears CNT, and it is re-set only if the
    /// *post-unstuffing* count still fills the buffer. Here it does not, so a
    /// full base read comes back without CNT.
    #[test]
    fn unstuffing_clears_the_count_eom_reason() {
        let mut stack = OctetInterposeStack::new(false);
        stack.install(-1, Box::new(ComInterpose::new()));
        let mut base = FakeServer::new(&[b'A', IAC, IAC, b'B']);
        let user = AsynUser::default();
        let mut buf = [0u8; 4];

        let r = stack.dispatch_read(&user, &mut buf, &mut base).unwrap();
        // Base filled all 4 and reported CNT; after unstuffing only 3 remain.
        assert_eq!(r.nbytes_transferred, 3);
        assert!(!r.eom_reason.contains(EomReason::CNT));
    }

    #[test]
    fn read_without_iac_keeps_the_base_eom_reason() {
        let mut stack = OctetInterposeStack::new(false);
        stack.install(-1, Box::new(ComInterpose::new()));
        let mut base = FakeServer::new(b"ABCD");
        let user = AsynUser::default();
        let mut buf = [0u8; 4];

        let r = stack.dispatch_read(&user, &mut buf, &mut base).unwrap();
        assert_eq!(r.nbytes_transferred, 4);
        assert!(r.eom_reason.contains(EomReason::CNT));
    }

    // -- asynOption: negotiation bytes ------------------------------------

    /// The exact bytes C's `restoreSettings` (:729-758) puts on the wire for a
    /// freshly-configured port at its defaults (9600 8N1, no flow control).
    /// This is the byte contract of the whole subsystem.
    #[test]
    fn restore_settings_emits_c_s_exact_handshake() {
        let mut reply = Vec::new();
        // IAC DO 0    -> the server answers WILL 0
        reply.extend_from_slice(&[IAC, WILL, WD_TRANSMIT_BINARY]);
        // IAC WILL 0  -> the server answers DO 0
        reply.extend_from_slice(&[IAC, DO, WD_TRANSMIT_BINARY]);
        // IAC WILL 44 -> the server answers DO 44
        reply.extend_from_slice(&[IAC, DO, SB_COM_PORT_OPTION]);
        // SET-MODEMSTATE-MASK 0, then each setting, each echoed back verbatim.
        reply.extend_from_slice(&ack(CPO_SET_MODEMSTATE_MASK, &[0]));
        reply.extend_from_slice(&ack(CPO_SET_BAUDRATE, &[0x00, 0x00, 0x25, 0x80]));
        reply.extend_from_slice(&ack(CPO_SET_DATASIZE, &[8]));
        reply.extend_from_slice(&ack(CPO_SET_PARITY, &[CPO_PARITY_NONE]));
        reply.extend_from_slice(&ack(CPO_SET_STOPSIZE, &[1]));
        reply.extend_from_slice(&ack(CPO_SET_CONTROL, &[CPO_CONTROL_NOFLOW]));
        reply.extend_from_slice(&ack(CPO_SET_CONTROL, &[CPO_CONTROL_NOFLOW]));

        let mut server = FakeServer::new(&reply);
        let mut com = ComPortOptions::new();
        com.restore_settings(&mut server).unwrap();

        #[rustfmt::skip]
        let expected: Vec<u8> = vec![
            IAC, DO,   WD_TRANSMIT_BINARY,                                  // :740
            IAC, WILL, WD_TRANSMIT_BINARY,                                  // :742
            IAC, WILL, SB_COM_PORT_OPTION,                                  // :744
            IAC, SB, SB_COM_PORT_OPTION, CPO_SET_MODEMSTATE_MASK, 0, IAC, SE, // :738-746
            // 9600 == 0x2580, big-endian in four bytes (:490-494).
            IAC, SB, SB_COM_PORT_OPTION, CPO_SET_BAUDRATE, 0x00, 0x00, 0x25, 0x80, IAC, SE,
            IAC, SB, SB_COM_PORT_OPTION, CPO_SET_DATASIZE, 8, IAC, SE,
            IAC, SB, SB_COM_PORT_OPTION, CPO_SET_PARITY, CPO_PARITY_NONE, IAC, SE,
            IAC, SB, SB_COM_PORT_OPTION, CPO_SET_STOPSIZE, 1, IAC, SE,
            // crtscts "N" re-sends the *current* flow mode (:575), not NOFLOW by
            // name — they coincide at the default, and the ixon "N" that follows
            // sends the same byte for the same reason.
            IAC, SB, SB_COM_PORT_OPTION, CPO_SET_CONTROL, CPO_CONTROL_NOFLOW, IAC, SE,
            IAC, SB, SB_COM_PORT_OPTION, CPO_SET_CONTROL, CPO_CONTROL_NOFLOW, IAC, SE,
        ];
        assert_eq!(server.written, expected);
        assert_eq!(com, ComPortOptions::default());
    }

    #[test]
    fn set_baud_emits_a_big_endian_four_byte_subnegotiation() {
        let mut server = FakeServer::new(&ack(CPO_SET_BAUDRATE, &[0x00, 0x01, 0xC2, 0x00]));
        let mut com = ComPortOptions::new();
        com.set_option(&mut AsynUser::default(), &mut server, "baud", "115200")
            .unwrap();

        // 115200 == 0x0001C200.
        assert_eq!(
            server.written,
            vec![
                IAC,
                SB,
                SB_COM_PORT_OPTION,
                CPO_SET_BAUDRATE,
                0x00,
                0x01,
                0xC2,
                0x00,
                IAC,
                SE
            ]
        );
        assert_eq!(com.get_option("baud").unwrap(), "115200");
    }

    /// CBUG-B8 — a payload byte of 0xFF is IAC-escaped, in both directions.
    ///
    /// DEVIATION from C, deliberate. C writes the subnegotiation straight to the
    /// driver below the interpose (:430), skipping the `writeIt` that doubles
    /// IACs, so `baud=255` (0x000000FF) puts a raw IAC into the payload and a
    /// compliant RFC-2217 server mis-frames the rest of the negotiation.
    #[test]
    fn b8_a_payload_byte_of_0xff_is_escaped_both_ways() {
        // The compliant server escapes its echo of 0xFF as well.
        let reply = vec![
            IAC,
            SB,
            SB_COM_PORT_OPTION,
            CPO_SET_BAUDRATE + 100,
            0x00,
            0x00,
            0x00,
            IAC,
            IAC, // the echoed 0xFF
            IAC,
            SE,
        ];
        let mut server = FakeServer::new(&reply);
        let mut com = ComPortOptions::new();
        com.set_option(&mut AsynUser::default(), &mut server, "baud", "255")
            .unwrap();

        // Written: the 0xFF of 0x000000FF is doubled; the framing IACs are not.
        assert_eq!(
            server.written,
            vec![
                IAC,
                SB,
                SB_COM_PORT_OPTION,
                CPO_SET_BAUDRATE,
                0x00,
                0x00,
                0x00,
                0xFF,
                0xFF, // C emits a single, raw 0xFF here
                IAC,
                SE
            ]
        );
        // And the escaped echo reads back as the one byte it encodes, so the
        // "did the server apply what we asked?" check passes.
        assert_eq!(com.get_option("baud").unwrap(), "255");
    }

    /// The other half of the escape rule: an IAC that is NOT doubled inside a
    /// payload is a command, i.e. a framing error — not a data byte.
    #[test]
    fn b8_an_unescaped_iac_in_a_reply_payload_is_a_framing_error() {
        let reply = vec![
            IAC,
            SB,
            SB_COM_PORT_OPTION,
            CPO_SET_BAUDRATE + 100,
            0x00,
            0x00,
            0x00,
            IAC,
            SE, // an unescaped IAC where a payload byte belongs
        ];
        let mut server = FakeServer::new(&reply);
        let mut com = ComPortOptions::new();
        let err = com
            .set_option(&mut AsynUser::default(), &mut server, "baud", "255")
            .unwrap_err();
        assert_eq!(err.status(), AsynStatus::Error);
        assert_eq!(
            err.message(),
            "Unescaped IAC in a COM-PORT-OPTION payload, followed by 0XF0"
        );
    }

    /// C :501-506 — the server echoes the rate it *actually* applied, and C fails
    /// the call when it is not the one asked for.
    #[test]
    fn set_baud_fails_when_the_server_applies_a_different_rate() {
        // Asked 115200, server says 9600.
        let mut server = FakeServer::new(&ack(CPO_SET_BAUDRATE, &[0x00, 0x00, 0x25, 0x80]));
        let mut com = ComPortOptions::new();
        let err = com
            .set_option(&mut AsynUser::default(), &mut server, "baud", "115200")
            .unwrap_err();
        assert_eq!(err.status(), AsynStatus::Error);
        assert_eq!(
            err.message(),
            "Tried to set 115200 baud, actually set 9600 baud."
        );
        // C still commits the echoed value before erroring (:497-500).
        assert_eq!(com.get_option("baud").unwrap(), "9600");
    }

    #[test]
    fn set_bits_and_stop_check_the_echo() {
        let mut server = FakeServer::new(&ack(CPO_SET_DATASIZE, &[7]));
        let mut com = ComPortOptions::new();
        let err = com
            .set_option(&mut AsynUser::default(), &mut server, "bits", "8")
            .unwrap_err();
        assert_eq!(err.message(), "Tried to set 8 bits, actually set 7 bits.");

        let mut server = FakeServer::new(&ack(CPO_SET_STOPSIZE, &[2]));
        let mut com = ComPortOptions::new();
        let err = com
            .set_option(&mut AsynUser::default(), &mut server, "stop", "1")
            .unwrap_err();
        assert_eq!(
            err.message(),
            "Tried to set 1 stop bits, actually set 2 stop bits."
        );
    }

    /// Negative control for the asymmetry at C :529-543: parity is the one
    /// setting with **no** echo check. Ask for "even", get "odd" back, and C
    /// reports success while caching odd. If someone "fixes" that into a
    /// consistency check, this fails.
    #[test]
    fn set_parity_does_not_check_the_echo() {
        let mut server = FakeServer::new(&ack(CPO_SET_PARITY, &[CPO_PARITY_ODD]));
        let mut com = ComPortOptions::new();
        com.set_option(&mut AsynUser::default(), &mut server, "parity", "even")
            .unwrap();
        assert_eq!(
            server.written,
            vec![
                IAC,
                SB,
                SB_COM_PORT_OPTION,
                CPO_SET_PARITY,
                CPO_PARITY_EVEN,
                IAC,
                SE
            ]
        );
        assert_eq!(com.get_option("parity").unwrap(), "odd");
    }

    #[test]
    fn parity_names_map_to_the_rfc2217_codes() {
        for (name, code) in [
            ("none", CPO_PARITY_NONE),
            ("odd", CPO_PARITY_ODD),
            ("even", CPO_PARITY_EVEN),
            ("mark", CPO_PARITY_MARK),
            ("space", CPO_PARITY_SPACE),
        ] {
            let mut server = FakeServer::new(&ack(CPO_SET_PARITY, &[code]));
            let mut com = ComPortOptions::new();
            com.set_option(&mut AsynUser::default(), &mut server, "parity", name)
                .unwrap();
            assert_eq!(server.written[4], code, "parity {name}");
            assert_eq!(com.get_option("parity").unwrap(), name);
        }
    }

    /// Turning OFF the mode that is *not* the one in effect leaves the other one
    /// alone: XON/XOFF is on, `crtscts n` says "hardware flow control off", and
    /// hardware flow control is already off — so the cached mode is re-sent and
    /// XON/XOFF keeps running. This is C's byte for this case too (:575) and it
    /// is the right one; the mode byte carries a single mutually-exclusive mode,
    /// so disabling RTS/CTS is not a licence to disable XON/XOFF.
    ///
    /// The case C gets WRONG is the other one — see
    /// [`each_key_can_turn_its_own_flow_control_back_off`].
    #[test]
    fn turning_off_the_mode_that_is_not_in_effect_leaves_the_other_running() {
        let mut server = FakeServer::new(&ack(CPO_SET_CONTROL, &[CPO_CONTROL_IXON]));
        let mut com = ComPortOptions::new();
        com.set_option(&mut AsynUser::default(), &mut server, "ixon", "y")
            .unwrap();
        assert_eq!(com.get_option("ixon").unwrap(), "Y");
        assert_eq!(com.get_option("crtscts").unwrap(), "N");

        let mut server = FakeServer::new(&ack(CPO_SET_CONTROL, &[CPO_CONTROL_IXON]));
        com.set_option(&mut AsynUser::default(), &mut server, "crtscts", "n")
            .unwrap();
        assert_eq!(
            server.written,
            vec![
                IAC,
                SB,
                SB_COM_PORT_OPTION,
                CPO_SET_CONTROL,
                CPO_CONTROL_IXON,
                IAC,
                SE
            ]
        );
        assert_eq!(com.get_option("ixon").unwrap(), "Y");
    }

    /// CBUG-B6 — each key can turn ITS OWN flow control back off, by
    /// transmitting `CPO_CONTROL_NOFLOW`.
    ///
    /// In C, `xBuf[1] = pinterposePvt->flow` on both "n" branches (:575, :591),
    /// so `crtscts N` with RTS/CTS on re-transmits HWFLOW, the server confirms
    /// it, `:578` caches it back, and `getOption` still answers "Y" — flow
    /// control can be enabled and never disabled. `CPO_CONTROL_NOFLOW` is
    /// defined and decoded but assigned to `xBuf[1]` nowhere in the file.
    #[test]
    fn each_key_can_turn_its_own_flow_control_back_off() {
        for (key, on_mode) in [("crtscts", CPO_CONTROL_HWFLOW), ("ixon", CPO_CONTROL_IXON)] {
            let mut com = ComPortOptions::new();
            let mut server = FakeServer::new(&ack(CPO_SET_CONTROL, &[on_mode]));
            com.set_option(&mut AsynUser::default(), &mut server, key, "y")
                .unwrap();
            assert_eq!(com.get_option(key).unwrap(), "Y", "{key} on");

            // C would re-send `on_mode` here and stay "Y" forever.
            let mut server = FakeServer::new(&ack(CPO_SET_CONTROL, &[CPO_CONTROL_NOFLOW]));
            com.set_option(&mut AsynUser::default(), &mut server, key, "n")
                .unwrap();
            assert_eq!(
                server.written,
                vec![
                    IAC,
                    SB,
                    SB_COM_PORT_OPTION,
                    CPO_SET_CONTROL,
                    CPO_CONTROL_NOFLOW,
                    IAC,
                    SE
                ],
                "{key} off must transmit NOFLOW"
            );
            assert_eq!(com.get_option(key).unwrap(), "N", "{key} reads back off");
            // And neither key claims the other is on.
            assert_eq!(com.get_option("crtscts").unwrap(), "N");
            assert_eq!(com.get_option("ixon").unwrap(), "N");
        }
    }

    /// "n" when flow control is already off is a no-op: NOFLOW is re-sent.
    #[test]
    fn turning_flow_control_off_when_it_is_already_off_sends_noflow() {
        let mut com = ComPortOptions::new();
        let mut server = FakeServer::new(&ack(CPO_SET_CONTROL, &[CPO_CONTROL_NOFLOW]));
        com.set_option(&mut AsynUser::default(), &mut server, "crtscts", "n")
            .unwrap();
        assert_eq!(server.written[4], CPO_CONTROL_NOFLOW);
        assert_eq!(com.get_option("crtscts").unwrap(), "N");
    }

    #[test]
    fn crtscts_y_sets_hardware_flow_control() {
        let mut server = FakeServer::new(&ack(CPO_SET_CONTROL, &[CPO_CONTROL_HWFLOW]));
        let mut com = ComPortOptions::new();
        com.set_option(&mut AsynUser::default(), &mut server, "crtscts", "y")
            .unwrap();
        assert_eq!(
            server.written,
            vec![
                IAC,
                SB,
                SB_COM_PORT_OPTION,
                CPO_SET_CONTROL,
                CPO_CONTROL_HWFLOW,
                IAC,
                SE
            ]
        );
        assert_eq!(com.get_option("crtscts").unwrap(), "Y");
        assert_eq!(com.get_option("ixon").unwrap(), "N");
    }

    /// C :605-643 — "on" then "off", edge-triggered, and the cached flag comes
    /// from the server's reply byte, not the request.
    #[test]
    fn break_on_and_off_emit_set_control_break() {
        let mut server = FakeServer::new(&ack(CPO_SET_CONTROL, &[CPO_CONTROL_BREAK_ON]));
        let mut com = ComPortOptions::new();
        com.set_option(&mut AsynUser::default(), &mut server, "break", "on")
            .unwrap();
        assert_eq!(
            server.written,
            vec![
                IAC,
                SB,
                SB_COM_PORT_OPTION,
                CPO_SET_CONTROL,
                CPO_CONTROL_BREAK_ON,
                IAC,
                SE
            ]
        );
        assert_eq!(com.get_option("break").unwrap(), "on");

        let mut server = FakeServer::new(&ack(CPO_SET_CONTROL, &[CPO_CONTROL_BREAK_OFF]));
        com.set_option(&mut AsynUser::default(), &mut server, "break", "off")
            .unwrap();
        assert_eq!(
            server.written,
            vec![
                IAC,
                SB,
                SB_COM_PORT_OPTION,
                CPO_SET_CONTROL,
                CPO_CONTROL_BREAK_OFF,
                IAC,
                SE
            ]
        );
        assert_eq!(com.get_option("break").unwrap(), "off");
    }

    /// C :609-612 — asserting an already-asserted break sends nothing at all, and
    /// releasing an already-released one likewise.
    #[test]
    fn break_is_edge_triggered_against_the_cached_state() {
        let mut server = FakeServer::new(&[]);
        let mut com = ComPortOptions::new();
        // Already off; "off" is a no-op that touches neither the wire nor the
        // (empty) reply queue.
        com.set_option(&mut AsynUser::default(), &mut server, "break", "off")
            .unwrap();
        assert!(server.written.is_empty());
    }

    /// C :614-641 — a numeric value runs assert / sleep / release in one call.
    #[test]
    fn break_with_a_duration_asserts_then_releases() {
        let mut reply = ack(CPO_SET_CONTROL, &[CPO_CONTROL_BREAK_ON]);
        reply.extend_from_slice(&ack(CPO_SET_CONTROL, &[CPO_CONTROL_BREAK_OFF]));
        let mut server = FakeServer::new(&reply);
        let mut com = ComPortOptions::new();
        com.set_option(&mut AsynUser::default(), &mut server, "break", "1")
            .unwrap();

        #[rustfmt::skip]
        let expected: Vec<u8> = vec![
            IAC, SB, SB_COM_PORT_OPTION, CPO_SET_CONTROL, CPO_CONTROL_BREAK_ON,  IAC, SE,
            IAC, SB, SB_COM_PORT_OPTION, CPO_SET_CONTROL, CPO_CONTROL_BREAK_OFF, IAC, SE,
        ];
        assert_eq!(server.written, expected);
        assert_eq!(com.get_option("break").unwrap(), "off");
    }

    // -- asynOption: negotiation state machine ----------------------------

    /// C `sbComPortOption` :443-449 — a server may volunteer a modem/line-state
    /// notification at any point; it is consumed and the wait for our own reply
    /// resumes.
    #[test]
    fn a_volunteered_modemstate_notification_is_skipped() {
        let mut reply = vec![
            IAC,
            SB,
            SB_COM_PORT_OPTION,
            CPO_SERVER_NOTIFY_MODEMSTATE,
            0x30,
            IAC,
            SE,
        ];
        reply.extend_from_slice(&ack(CPO_SET_DATASIZE, &[8]));
        let mut server = FakeServer::new(&reply);
        let mut com = ComPortOptions::new();
        com.set_option(&mut AsynUser::default(), &mut server, "bits", "8")
            .unwrap();
        assert_eq!(com.get_option("bits").unwrap(), "8");
    }

    #[test]
    fn a_volunteered_linestate_notification_is_skipped() {
        let mut reply = vec![
            IAC,
            SB,
            SB_COM_PORT_OPTION,
            CPO_SERVER_NOTIFY_LINESTATE,
            0x60,
            IAC,
            SE,
        ];
        reply.extend_from_slice(&ack(CPO_SET_DATASIZE, &[8]));
        let mut server = FakeServer::new(&reply);
        let mut com = ComPortOptions::new();
        com.set_option(&mut AsynUser::default(), &mut server, "bits", "8")
            .unwrap();
        assert_eq!(com.get_option("bits").unwrap(), "8");
    }

    /// C :461-465 — a reply for the wrong subcommand ends the negotiation.
    #[test]
    fn a_reply_for_the_wrong_subcommand_is_an_error() {
        // Asked SET-DATASIZE (2, reply 102); server answers for SET-PARITY (103).
        let mut server = FakeServer::new(&ack(CPO_SET_PARITY, &[CPO_PARITY_NONE]));
        let mut com = ComPortOptions::new();
        let err = com
            .set_option(&mut AsynUser::default(), &mut server, "bits", "8")
            .unwrap_err();
        assert_eq!(err.status(), AsynStatus::Error);
        assert_eq!(err.message(), "Sent COM-PORT-OPTION 2 but got reply 103");
    }

    /// C `expectChar` :120-122 — and its `%#X` formatting, `0X` prefix and all.
    #[test]
    fn a_malformed_subnegotiation_frame_reports_c_s_expected_got() {
        // IAC then a data byte where SB (0xFA) must be.
        let mut server = FakeServer::new(&[IAC, b'A']);
        let mut com = ComPortOptions::new();
        let err = com
            .set_option(&mut AsynUser::default(), &mut server, "bits", "8")
            .unwrap_err();
        assert_eq!(err.message(), "Expected 0XFA, got 0X41");
    }

    /// C `willdo` :372-390 — we said `IAC DO BINARY`, so a `WONT` is the server
    /// refusing.
    #[test]
    fn willdo_reports_a_refusal() {
        let mut server = FakeServer::new(&[IAC, WONT, WD_TRANSMIT_BINARY]);
        let mut com = ComPortOptions::new();
        let err = com.restore_settings(&mut server).unwrap_err();
        assert_eq!(err.status(), AsynStatus::Error);
        // WD_TRANSMIT_BINARY is 0, and printf's `#` flag prints no prefix for 0.
        assert_eq!(err.message(), "Device says WON'T 0.");
    }

    #[test]
    fn willdo_reports_a_dont_refusal_for_a_will_request() {
        // Skip past the first two exchanges to reach `IAC WILL 44`, which the
        // server refuses with DONT 44.
        let mut reply = vec![IAC, WILL, WD_TRANSMIT_BINARY, IAC, DO, WD_TRANSMIT_BINARY];
        reply.extend_from_slice(&[IAC, DONT, SB_COM_PORT_OPTION]);
        let mut server = FakeServer::new(&reply);
        let mut com = ComPortOptions::new();
        let err = com.restore_settings(&mut server).unwrap_err();
        assert_eq!(err.message(), "Device says DON'T 0x2c.");
    }

    /// C :358-362 — we sent `DO`, so a `DO`/`DONT` echo for the same code is a
    /// protocol error, not an acknowledgement. (`restoreSettings` opens with
    /// `IAC DO BINARY`.)
    #[test]
    fn willdo_rejects_a_do_echoed_in_response_to_do() {
        let mut server = FakeServer::new(&[IAC, DO, WD_TRANSMIT_BINARY]);
        let mut com = ComPortOptions::new();
        let err = com.restore_settings(&mut server).unwrap_err();
        assert_eq!(err.message(), "Received response 0 in response to DO.");
    }

    /// C :344-346 / :349-350 — chatter about *other* option codes, and stray
    /// IAC/SE pairs, are skipped until our own code comes back.
    #[test]
    fn willdo_skips_replies_about_other_option_codes() {
        let mut reply = Vec::new();
        // Server volunteers WILL/WONT for unrelated options (ECHO=1, SGA=3).
        reply.extend_from_slice(&[IAC, WILL, 1, IAC, WONT, 3]);
        // ...and only then answers ours.
        reply.extend_from_slice(&[IAC, WILL, WD_TRANSMIT_BINARY]);
        reply.extend_from_slice(&[IAC, DO, WD_TRANSMIT_BINARY]);
        reply.extend_from_slice(&[IAC, DO, SB_COM_PORT_OPTION]);
        reply.extend_from_slice(&ack(CPO_SET_MODEMSTATE_MASK, &[0]));
        reply.extend_from_slice(&ack(CPO_SET_BAUDRATE, &[0x00, 0x00, 0x25, 0x80]));
        reply.extend_from_slice(&ack(CPO_SET_DATASIZE, &[8]));
        reply.extend_from_slice(&ack(CPO_SET_PARITY, &[CPO_PARITY_NONE]));
        reply.extend_from_slice(&ack(CPO_SET_STOPSIZE, &[1]));
        reply.extend_from_slice(&ack(CPO_SET_CONTROL, &[CPO_CONTROL_NOFLOW]));
        reply.extend_from_slice(&ack(CPO_SET_CONTROL, &[CPO_CONTROL_NOFLOW]));

        let mut server = FakeServer::new(&reply);
        let mut com = ComPortOptions::new();
        com.restore_settings(&mut server).unwrap();
        assert_eq!(com, ComPortOptions::default());
    }

    /// C :405-408 — a byte after IAC that is no telnet command at all.
    #[test]
    fn willdo_rejects_an_unexpected_command_byte() {
        let mut server = FakeServer::new(&[IAC, b'A']);
        let mut com = ComPortOptions::new();
        let err = com.restore_settings(&mut server).unwrap_err();
        assert_eq!(err.message(), "Unexpected character 0x41 in TELNET reply");
    }

    /// C `nextChar` :103-106 discards the failing status but keeps the lower
    /// layer's message, which the caller's `return asynError` then carries out.
    /// So a timed-out negotiation surfaces as asynError carrying the read's
    /// message — the status is *not* preserved as asynTimeout.
    #[test]
    fn a_silent_server_surfaces_the_lower_layers_message_as_asyn_error() {
        let mut server = FakeServer::new(&[]);
        let mut com = ComPortOptions::new();
        let err = com.restore_settings(&mut server).unwrap_err();
        assert_eq!(err.status(), AsynStatus::Error);
        assert_eq!(err.message(), "read timeout");
    }

    // -- asynOption: option plumbing --------------------------------------

    #[test]
    fn owns_exactly_c_s_seven_keys_case_insensitively() {
        for key in ["baud", "BITS", "Parity", "stop", "CRTSCTS", "ixon", "Break"] {
            assert!(ComPortOptions::owns_key(key), "{key}");
        }
        // Handled by the driver below, not here — C :645-652 / :710-717.
        for key in ["hostInfo", "disconnectOnReadTimeout", "clocal", "ixoff"] {
            assert!(!ComPortOptions::owns_key(key), "{key}");
        }
    }

    /// R9-55. C threads the *caller's* `pasynUser` through the whole
    /// negotiation: `setOption(drvPvt, pasynUser, key, val)` (:475) hands it to
    /// `sbComPortOption` (:495), which writes (:431) and reads (:435-457) the wire
    /// with it — so an asynRecord option put negotiates under TMOT and an iocsh
    /// `asynSetOption` under its own 2 s (`asynShellCommands.c:119`). The port
    /// pinned every negotiation to a private 2 s instead, so a record with
    /// TMOT=10 gave up on a slow terminal server after 2 s, and one with
    /// TMOT=0.05 sat waiting for 2 s.
    ///
    /// The 2 s is C's only where C's own asynUser drives — `restoreSettings`,
    /// from the configure path and the reconnect exceptionHandler (:836).
    #[test]
    fn the_negotiation_runs_under_the_callers_timeout() {
        /// Records the timeout on the asynUser every wire operation is given.
        struct TimeoutSpy {
            inner: FakeServer,
            seen: Vec<Duration>,
        }
        impl OctetNext for TimeoutSpy {
            fn read(&mut self, user: &AsynUser, buf: &mut [u8]) -> AsynResult<OctetReadResult> {
                self.seen.push(user.timeout);
                self.inner.read(user, buf)
            }
            fn write(&mut self, user: &mut AsynUser, data: &[u8]) -> AsynResult<usize> {
                self.seen.push(user.timeout);
                self.inner.write(user, data)
            }
            fn flush(&mut self, user: &mut AsynUser) -> AsynResult<()> {
                self.inner.flush(user)
            }
        }

        // One setOption("bits", "8"): write the subcommand, read the echo.
        let mut spy = TimeoutSpy {
            inner: FakeServer::new(&ack(CPO_SET_DATASIZE, &[8])),
            seen: Vec::new(),
        };
        let mut com = ComPortOptions::new();
        let caller = Duration::from_millis(250);
        com.set_option(
            &mut AsynUser::default().with_timeout(caller),
            &mut spy,
            "bits",
            "8",
        )
        .unwrap();
        assert!(!spy.seen.is_empty(), "the negotiation reached the wire");
        assert!(
            spy.seen.iter().all(|t| *t == caller),
            "every wire operation runs under the caller's timeout, got {:?}",
            spy.seen
        );

        // restoreSettings is the interpose's own negotiation, and only that one
        // carries C's 2 s (:836).
        let mut replies = Vec::new();
        replies.extend(vec![IAC, WILL, WD_TRANSMIT_BINARY]);
        replies.extend(vec![IAC, DO, WD_TRANSMIT_BINARY]);
        replies.extend(vec![IAC, DO, SB_COM_PORT_OPTION]);
        replies.extend(ack(CPO_SET_MODEMSTATE_MASK, &[0]));
        replies.extend(ack(CPO_SET_BAUDRATE, &9600i32.to_be_bytes()));
        replies.extend(ack(CPO_SET_DATASIZE, &[8]));
        replies.extend(ack(CPO_SET_PARITY, &[CPO_PARITY_NONE]));
        replies.extend(ack(CPO_SET_STOPSIZE, &[1]));
        replies.extend(ack(CPO_SET_CONTROL, &[CPO_CONTROL_NOFLOW]));
        replies.extend(ack(CPO_SET_CONTROL, &[CPO_CONTROL_NOFLOW]));
        let mut spy = TimeoutSpy {
            inner: FakeServer::new(&replies),
            seen: Vec::new(),
        };
        let mut com = ComPortOptions::new();
        com.restore_settings(&mut spy).unwrap();
        assert!(!spy.seen.is_empty());
        assert!(
            spy.seen.iter().all(|t| *t == INTERPOSE_USER_TIMEOUT),
            "restoreSettings runs on the interpose's own 2 s asynUser, got {:?}",
            spy.seen
        );
    }

    /// R9-57, first half. C `readIt` traces every read it unstuffed at
    /// `ASYN_TRACEIO_FILTER` (asynInterposeCom.c:237-239). The interpose has no
    /// handle on the port to trace through — in C it prints through the
    /// `asynUser` (`asynPrintIO(pasynUser, …)` → `findTracePvt`), and the port's
    /// `AsynUser` now carries the same port/trace linkage, stamped by the actor.
    #[test]
    fn an_unstuffed_read_is_traced_at_traceio_filter() {
        use crate::trace::{TraceFile, TraceInfoMask, TraceIoMask, TraceManager};
        use crate::user::UserTrace;
        use std::sync::{Arc, Mutex};

        let mgr = Arc::new(TraceManager::new());
        mgr.set_trace_mask(Some("comtrace"), TraceMask::IO_FILTER);
        mgr.set_trace_info_mask(Some("comtrace"), TraceInfoMask::PORT);
        mgr.set_trace_io_mask(Some("comtrace"), TraceIoMask::ESCAPE);
        let temp = std::env::temp_dir().join("asyn_com_filter_trace.txt");
        let file = std::fs::File::create(&temp).unwrap();
        mgr.set_trace_file(
            Some("comtrace"),
            TraceFile::File(Arc::new(Mutex::new(file))),
        );

        let user = AsynUser {
            trace: Some(UserTrace {
                manager: mgr.clone(),
                port: "comtrace".into(),
            }),
            ..AsynUser::default()
        };

        // "A<IAC><IAC>B" on the wire is "A<IAC>B" to the caller — one unstuffing.
        let mut server = FakeServer::new(&[b'A', IAC, IAC, b'B']);
        let mut com = ComInterpose::new();
        let mut buf = [0u8; 8];
        let r = com.read(&user, &mut buf, &mut server).unwrap();
        assert_eq!(&buf[..r.nbytes_transferred], &[b'A', IAC, b'B']);

        let contents = std::fs::read_to_string(&temp).unwrap();
        assert!(
            contents.contains("nRead 3 after IAC unstuffing"),
            "C :238 label, got {contents:?}"
        );
        assert!(contents.contains("IO_FILTER"), "at ASYN_TRACEIO_FILTER");
        let _ = std::fs::remove_file(&temp);

        // A read with nothing to unstuff prints nothing (C tests `unstuffed`).
        let temp2 = std::env::temp_dir().join("asyn_com_filter_trace_quiet.txt");
        let file = std::fs::File::create(&temp2).unwrap();
        mgr.set_trace_file(
            Some("comtrace"),
            TraceFile::File(Arc::new(Mutex::new(file))),
        );
        let mut server = FakeServer::new(b"AB");
        com.read(&user, &mut buf, &mut server).unwrap();
        assert_eq!(std::fs::read_to_string(&temp2).unwrap(), "");
        let _ = std::fs::remove_file(&temp2);
    }

    /// R9-57, second half. C's `setOption` leaves an advisory in
    /// `pasynUser->errorMessage` when the operator switches flow control over a
    /// live setting of the other kind, and does *not* fail the call (:571-573,
    /// :587-589). A `Result` cannot carry a message that does not fail, so the
    /// port dropped both; they now land in the user's message slot, which is
    /// where C's caller finds them.
    #[test]
    fn switching_flow_control_leaves_c_s_advisory_in_the_users_message() {
        let mut user = AsynUser::default();

        // XON/XOFF is live; the operator turns RTS/CTS on.
        let mut server = FakeServer::new(&ack(CPO_SET_CONTROL, &[CPO_CONTROL_HWFLOW]));
        let mut com = ComPortOptions::new();
        com.flow = CPO_CONTROL_IXON;
        com.set_option(&mut user, &mut server, "crtscts", "y")
            .expect("the advisory does not fail the call");
        assert_eq!(
            user.error_message,
            "XON/XOFF already set. Now using RTS/CTS."
        );
        assert_eq!(com.flow, CPO_CONTROL_HWFLOW);

        // Mirror image: RTS/CTS is live; the operator turns XON/XOFF on.
        let mut user = AsynUser::default();
        let mut server = FakeServer::new(&ack(CPO_SET_CONTROL, &[CPO_CONTROL_IXON]));
        let mut com = ComPortOptions::new();
        com.flow = CPO_CONTROL_HWFLOW;
        com.set_option(&mut user, &mut server, "ixon", "y")
            .expect("the advisory does not fail the call");
        assert_eq!(
            user.error_message,
            "RTS/CTS already set. Now using XON/XOFF."
        );
        assert_eq!(com.flow, CPO_CONTROL_IXON);

        // No live setting of the other kind: no advisory (C only writes it under
        // the flow test).
        let mut user = AsynUser::default();
        let mut server = FakeServer::new(&ack(CPO_SET_CONTROL, &[CPO_CONTROL_HWFLOW]));
        let mut com = ComPortOptions::new();
        com.set_option(&mut user, &mut server, "crtscts", "y")
            .unwrap();
        assert_eq!(user.error_message, "");
    }

    #[test]
    fn get_option_reports_the_defaults_asyn_interpose_com_installs() {
        let com = ComPortOptions::new();
        assert_eq!(com.get_option("baud").unwrap(), "9600");
        assert_eq!(com.get_option("bits").unwrap(), "8");
        assert_eq!(com.get_option("parity").unwrap(), "none");
        assert_eq!(com.get_option("stop").unwrap(), "1");
        assert_eq!(com.get_option("crtscts").unwrap(), "N");
        assert_eq!(com.get_option("ixon").unwrap(), "N");
        assert_eq!(com.get_option("break").unwrap(), "off");
    }

    #[test]
    fn bad_option_values_are_rejected_with_c_s_messages() {
        let mut server = FakeServer::new(&[]);
        let mut com = ComPortOptions::new();
        assert_eq!(
            com.set_option(&mut AsynUser::default(), &mut server, "baud", "fast")
                .unwrap_err()
                .message(),
            "Bad number"
        );
        assert_eq!(
            com.set_option(&mut AsynUser::default(), &mut server, "parity", "sideways")
                .unwrap_err()
                .message(),
            "Invalid parity selection"
        );
        // C's message really does have two spaces (:553).
        assert_eq!(
            com.set_option(&mut AsynUser::default(), &mut server, "stop", "1.5")
                .unwrap_err()
                .message(),
            "Bad  stop bit count"
        );
        assert_eq!(
            com.set_option(&mut AsynUser::default(), &mut server, "crtscts", "maybe")
                .unwrap_err()
                .message(),
            "Bad  value"
        );
        // Nothing reached the wire.
        assert!(server.written.is_empty());
    }

    /// C `sscanf("%d")` semantics: a trailing non-numeric tail is ignored, not an
    /// error.
    #[test]
    fn baud_accepts_a_trailing_tail_the_way_sscanf_does() {
        let mut server = FakeServer::new(&ack(CPO_SET_BAUDRATE, &[0x00, 0x00, 0x25, 0x80]));
        let mut com = ComPortOptions::new();
        com.set_option(&mut AsynUser::default(), &mut server, "baud", "9600 bps")
            .unwrap();
        assert_eq!(com.get_option("baud").unwrap(), "9600");
    }

    #[test]
    fn scan_helpers_match_sscanf() {
        assert_eq!(scan_int("9600"), Some(9600));
        assert_eq!(scan_int("  -12xyz"), Some(-12));
        assert_eq!(scan_int("+7"), Some(7));
        assert_eq!(scan_int("abc"), None);
        assert_eq!(scan_int(""), None);
        assert_eq!(scan_uint("250ms"), Some(250));
        assert_eq!(scan_uint("x"), None);
        assert_eq!(scan_float("2"), Some(2.0));
        assert_eq!(scan_float("1.5abc"), Some(1.5));
        assert_eq!(scan_float("nope"), None);
    }

    #[test]
    fn hash_hex_matches_printf() {
        assert_eq!(hash_hex_lower(0), "0");
        assert_eq!(hash_hex_lower(44), "0x2c");
        assert_eq!(hash_hex_upper(0), "0");
        assert_eq!(hash_hex_upper(250), "0XFA");
        assert_eq!(hash_hex_upper(0x41), "0X41");
    }
}