serialplugin-core 0.2.2

Serial port core of tauri-plugin-serialplugin without Tauri: RX hub, watch streams with native parsers, AT exchange, CMUX, TCP and BLE transports
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
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
//! Unified serial API — `Box<dyn serialport::SerialPort>` on desktop and Android.
#[cfg(target_os = "ios")]
use crate::serialport;

use crate::api::serial_port::{
    configure_at_session as configure_at_session_shared, run_exchange_queued,
};
use crate::at::session::{AtCommandOptions, AtPhase, AtSessionConfig, SendSmsPduOptions};
use crate::cmux::{mux_path, parse_mux_path, CmuxSession};
use crate::error::{CoreError, Error, ErrorKind};
use crate::events::{
    CoreEvent, CoreSink, EventSink, ExchangeOptions, PortListSink, PortStatus, SerialEvent,
    SubscriptionId, WatchOptions,
};
use crate::hub::delivery::CoreQueue;
#[cfg(target_os = "android")]
use crate::log_error;
use crate::port::tx_queue::PortTxQueue;
use crate::state::{
    ClearBuffer, ConnectedPort, ConnectedPortHandle, DataBits, FlowControl, Parity, PortState,
    SerialportInfo, StopBits,
};
use crate::{log_debug, log_info};
use serialport::SerialPort as SerialPortTrait;
use std::collections::HashMap;
use std::io::Write;
use std::sync::atomic::{AtomicU32, AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, Weak};
use std::thread;
use std::time::{Duration, Instant};

type PortArc = Arc<Mutex<Box<dyn SerialPortTrait>>>;

/// Ids handed out by [`SerialManager::watch`] / [`SerialManager::watch_ports`].
/// The Tauri plugin passes its IPC channel ids through the `*_with_id` variants instead;
/// both share the manager's registry, so an id already taken there is skipped (and
/// registering a duplicate id is rejected outright).
fn next_watch_id(taken: impl Fn(u32) -> bool) -> u32 {
    static NEXT: AtomicU32 = AtomicU32::new(1);
    loop {
        let id = NEXT.fetch_add(1, Ordering::Relaxed);
        if !taken(id) {
            return id;
        }
    }
}

/// Ids of `watch_core` streams and `subscribe_core` subscriptions.
fn next_subscription_id() -> u64 {
    static NEXT: AtomicU64 = AtomicU64::new(1);
    NEXT.fetch_add(1, Ordering::Relaxed)
}

/// Platform seam: enumerate available ports.
fn platform_available_ports(
    single_port_per_device: bool,
) -> Result<HashMap<String, HashMap<String, String>>, Error> {
    #[cfg(not(target_os = "android"))]
    {
        crate::port::list::enumerate_available_ports(single_port_per_device)
    }
    #[cfg(target_os = "android")]
    {
        let json = crate::android::fd_bridge::call_enumerate_json()?;
        log_info!(
            "[SerialEnumerate] raw json ({} bytes): {}",
            json.len(),
            json
        );
        #[derive(serde::Deserialize)]
        struct AvailablePortsResponse {
            #[serde(default)]
            ports: HashMap<String, crate::android::enumerate::DeviceEntry>,
        }
        let response: AvailablePortsResponse = serde_json::from_str(&json).map_err(|e| {
            log_error!("[SerialEnumerate] Invalid enumerate JSON: {e}; body={json}");
            Error::new(format!("Invalid enumerate JSON: {e}"))
        })?;
        let filtered =
            crate::android::enumerate::expand_devices(response.ports, single_port_per_device)?;
        let keys: Vec<&str> = filtered.keys().map(|s| s.as_str()).collect();
        log_info!(
            "[SerialEnumerate] parsed {} port(s) single={} keys={:?}",
            filtered.len(),
            single_port_per_device,
            keys
        );
        Ok(filtered)
    }
}

/// Platform seam: open physical port as `Box<dyn serialport::SerialPort>`.
#[allow(clippy::too_many_arguments)]
fn platform_open(
    path: &str,
    baud_rate: u32,
    data_bits: DataBits,
    flow_control: FlowControl,
    parity: Parity,
    stop_bits: StopBits,
    timeout: Duration,
    lines: crate::state::ModemLinesOnOpen,
) -> Result<(String, Box<dyn SerialPortTrait>), CoreError> {
    if path.starts_with("ble://") {
        #[cfg(feature = "ble")]
        {
            #[cfg(not(target_os = "android"))]
            use crate::port::ble::BleSerialPort as Ble;
            #[cfg(target_os = "android")]
            use crate::port::ble_android::AndroidBleSerialPort as Ble;
            let port = Ble::connect(
                path,
                baud_rate,
                data_bits.into(),
                flow_control.into(),
                parity.into(),
                stop_bits.into(),
                timeout,
            )
            .map_err(|e| CoreError::io(format!("Failed to open {path}"), e))?;
            return Ok((path.to_string(), Box::new(port)));
        }
        #[cfg(not(feature = "ble"))]
        return Err(CoreError::new(
            ErrorKind::Other,
            Error::String(format!(
                "{path}: BLE needs the plugin's `ble` cargo feature"
            )),
        ));
    }
    if crate::port::tcp::tcp_address(path).is_some() {
        let port = crate::port::tcp::TcpSerialPort::connect(
            path,
            baud_rate,
            data_bits.into(),
            flow_control.into(),
            parity.into(),
            stop_bits.into(),
            timeout,
        )
        .map_err(|e| CoreError::io(format!("Failed to open {path}"), e))?;
        return Ok((path.to_string(), Box::new(port)));
    }
    // iOS has no local serial devices; serialport's open/enumerate code would also
    // pull in IOKit, which iOS apps cannot link.
    #[cfg(target_os = "ios")]
    {
        let _ = (
            baud_rate,
            data_bits,
            flow_control,
            parity,
            stop_bits,
            timeout,
            lines,
        );
        Err(CoreError::new(
            ErrorKind::NotFound,
            Error::String(format!(
                "{path}: iOS has no local serial ports; use a tcp://host:port bridge"
            )),
        ))
    }
    #[cfg(not(any(target_os = "android", target_os = "ios")))]
    {
        let mut builder = serialport::new(path, baud_rate)
            .data_bits(data_bits.into())
            .flow_control(flow_control.into())
            .parity(parity.into())
            .stop_bits(stop_bits.into())
            .timeout(timeout);
        if let Some(dtr) = lines.dtr {
            builder = builder.dtr_on_open(dtr);
        }
        let mut port = builder.open().map_err(|e| {
            let err = CoreError::serialport("Failed to open serial port".into(), e);
            // Listed but not openable: in use (Windows says "Access is denied", in the
            // system language; serialport reports that and flock failures as NoDevice).
            let listed = || {
                serialport::available_ports().is_ok_and(|l| l.iter().any(|p| p.port_name == path))
            };
            let kind = open_error_kind(err.kind(), listed);
            err.with_kind(kind)
        })?;
        if let Some(rts) = lines.rts {
            port.write_request_to_send(rts)
                .map_err(|e| CoreError::serialport("Failed to set RTS on open".into(), e))?;
        }
        Ok((path.to_string(), port))
    }
    #[cfg(target_os = "android")]
    {
        let _ = timeout;
        crate::android::driver_host::global_host()
            .open(
                path,
                baud_rate,
                data_bits,
                flow_control,
                parity,
                stop_bits,
                lines,
            )
            .map_err(CoreError::from)
    }
}

/// A second handle for writes and control calls, so they never wait for the RX hub's
/// read (and the hub's poll timeout never touches the write timeout). `timeout` is
/// the port timeout from `open` / `set_timeout`.
///
/// Local serial ports on Unix only. TCP, BLE and Android keep one handle (no real
/// clone there), and so does Windows: its duplicated handles share one COMMTIMEOUTS,
/// so the hub's short read timeout would cut writes short.
fn split_writer(
    path: &str,
    port: &dyn SerialPortTrait,
    timeout: Duration,
) -> Option<Box<dyn SerialPortTrait>> {
    if !cfg!(all(
        unix,
        not(any(target_os = "android", target_os = "ios"))
    )) || path.starts_with("ble://")
        || crate::port::tcp::tcp_address(path).is_some()
    {
        return None;
    }
    let mut writer = port.try_clone().ok()?;
    writer.set_timeout(timeout).ok()?;
    Some(writer)
}

/// Default read/write timeout (ms) when opening a port.
const DEFAULT_PORT_TIMEOUT_MS: u64 = 1000;
const PORT_LOCK_TIMEOUT_MS: u64 = 250;
const PORT_IO_TIMEOUT_MS: u64 = 100;
/// Pause between the `AT+CMUX` OK and the first frame (see `enable_mux`). The M26
/// needs well under 20 ms; 100 ms leaves room for slower modems.
const CMUX_SWITCH_SETTLE: Duration = Duration::from_millis(100);
/// Watch batch window when the caller sets none: short enough for interactive
/// terminals, long enough to coalesce a burst into one event.
const DEFAULT_WATCH_FLUSH_MS: u64 = 20;

/// Acquire the port mutex with a deadline (hub reader uses try_lock; avoid blocking forever).
/// `io_timeout`: set the shared 100 ms I/O timeout first (single-handle ports only).
fn lock_with_deadline<T, E, F>(
    port: &PortArc,
    lock_timeout: Duration,
    io_timeout: bool,
    f: F,
) -> Result<T, E>
where
    E: From<Error>,
    F: FnOnce(&mut Box<dyn serialport::SerialPort>) -> Result<T, E>,
{
    let deadline = Instant::now() + lock_timeout;
    loop {
        match port.try_lock() {
            Ok(mut guard) => {
                if io_timeout {
                    let _ = guard.set_timeout(Duration::from_millis(PORT_IO_TIMEOUT_MS));
                }
                return f(&mut guard);
            }
            Err(_) if Instant::now() >= deadline => {
                return Err(Error::String(format!(
                    "serial port lock timeout after {} ms",
                    lock_timeout.as_millis()
                ))
                .into());
            }
            Err(_) => thread::sleep(Duration::from_millis(1)),
        }
    }
}

fn with_port_try_lock<T, E, F>(port: &PortArc, lock_timeout: Duration, f: F) -> Result<T, E>
where
    E: From<Error>,
    F: FnOnce(&mut Box<dyn serialport::SerialPort>) -> Result<T, E>,
{
    lock_with_deadline(port, lock_timeout, true, f)
}

/// Kind of a failed open: a device that is listed but "not found" is in use.
#[cfg(not(any(target_os = "android", target_os = "ios")))]
fn open_error_kind(kind: ErrorKind, listed: impl FnOnce() -> bool) -> ErrorKind {
    if kind == ErrorKind::NotFound && listed() {
        ErrorKind::Busy
    } else {
        kind
    }
}

/// Time `len` bytes take on the wire at the port's baud rate (11 bits a character).
fn wire_ms(port: &dyn SerialPortTrait, len: usize) -> u64 {
    let baud = u64::from(port.baud_rate().unwrap_or(9600).max(1));
    (len as u64).saturating_mul(11_000) / baud
}

/// Write the full buffer, retrying until all bytes are sent or an error occurs
/// (single-handle ports: the hub shares the handle and its timeout).
fn write_all_port(
    port: &mut Box<dyn serialport::SerialPort>,
    buf: &[u8],
    operation: &str,
) -> Result<usize, CoreError> {
    // The shared 100 ms I/O timeout is too short for big or slow writes (8 KB at
    // 9600 baud needs ~9 s), so give writes room for the wire time plus a margin.
    let wire_ms = wire_ms(port.as_ref(), buf.len());
    let _ = port.set_timeout(Duration::from_millis(wire_ms.saturating_mul(2) + 1000));
    let result = port
        .write_all(buf)
        .map_err(|e| CoreError::io(format!("Failed to {operation}"), e));
    let _ = port.set_timeout(Duration::from_millis(PORT_IO_TIMEOUT_MS));
    result.map(|()| buf.len())
}

/// Write on a split write handle: its own timeout, no lock shared with the hub. A
/// write that needs longer on the wire than the port timeout gets twice its wire
/// time (as 4.x writes did), so a big write is not cut short by a short timeout.
fn write_all_split(port: &PortArc, buf: &[u8], operation: &str) -> Result<usize, CoreError> {
    lock_with_deadline(
        port,
        Duration::from_millis(PORT_LOCK_TIMEOUT_MS),
        false,
        |p| {
            let own = p.timeout();
            let need = Duration::from_millis(wire_ms(p.as_ref(), buf.len()).saturating_mul(2));
            let raise = need > own;
            if raise {
                let _ = p.set_timeout(need);
            }
            let result = p
                .write_all(buf)
                .map(|()| buf.len())
                .map_err(|e| CoreError::io(format!("Failed to {operation}"), e));
            if raise {
                let _ = p.set_timeout(own);
            }
            result
        },
    )
}

/// Tear down resources held by a [`SerialportInfo`] (RX hub + port).
fn finish_serialport_info(path: &str, info: SerialportInfo) -> Result<(), Error> {
    #[cfg(not(target_os = "android"))]
    let _ = path;
    match info.state {
        PortState::Connected(cp) => {
            if let Ok(mut hub_guard) = cp.rx_hub.lock() {
                if let Some(hub) = hub_guard.take() {
                    #[cfg(target_os = "android")]
                    crate::android::registry::global_registry().unregister(path);
                    // The hub flushes what it holds into the watches; no `Disconnect`.
                    hub.shared().mark_closing();
                    match Arc::try_unwrap(hub) {
                        Ok(hub) => hub.stop(),
                        Err(hub) => hub.request_stop(),
                    }
                }
            }
            #[cfg(target_os = "android")]
            {
                let _ = crate::android::driver_host::global_host().close(Some(path));
            }
            Ok(())
        }
        PortState::Opening | PortState::Closed => Ok(()),
    }
}

fn register_mobile_hub(path: &str, cp: &ConnectedPortHandle, hub: Arc<crate::hub::PortRxHub>) {
    #[cfg(target_os = "android")]
    {
        let hub_handle: Arc<dyn crate::hub::RxHubHandle> = hub;
        crate::android::registry::global_registry().register(path, hub_handle, cp.clone());
    }
    #[cfg(not(target_os = "android"))]
    {
        let _ = (path, cp, hub);
    }
}

/// Cloned write handle of a port, tied to the reader it was cloned from.
struct Writer {
    reader: Weak<Mutex<Box<dyn SerialPortTrait>>>,
    port: PortArc,
}

/// Per-manager state of the 4.2 core (registries, write handles, subscriptions).
#[derive(Default)]
struct CoreState {
    /// Old `watch` ids → path (one per port).
    watches: Mutex<HashMap<u32, String>>,
    /// `watch_core` ids → path (any number per port).
    core_watches: Mutex<HashMap<u64, String>>,
    writers: Mutex<HashMap<String, Writer>>,
    /// Ports the core released after a disconnect, with the reason (4.x behaviour until
    /// `close`: still listed, a late watch gets `Disconnect`, calls fail with it).
    disconnected: Mutex<HashMap<String, String>>,
    subscribers: Mutex<Vec<(u64, Arc<CoreQueue>)>>,
    /// Bytes per core receiver queue; 0 = [`crate::events::DEFAULT_CORE_QUEUE_BYTES`].
    queue_capacity: AtomicUsize,
}

/// A manager without keeping it alive (the hub threads' disconnect hooks).
struct WeakManager {
    serialports: Weak<Mutex<HashMap<String, SerialportInfo>>>,
    virtual_ports: Weak<Mutex<HashMap<String, crate::state::VirtualPortRef>>>,
    core: Weak<CoreState>,
}

impl WeakManager {
    fn upgrade(&self) -> Option<SerialManager> {
        Some(SerialManager {
            serialports: self.serialports.upgrade()?,
            virtual_ports: self.virtual_ports.upgrade()?,
            core: self.core.upgrade()?,
        })
    }
}

/// Owns every open port (desktop serial, `tcp://`, `ble://`, Android USB, CMUX channels).
///
/// Cheap to clone (all clones share the same ports). Methods block: call them from a
/// plain thread or `tokio::task::spawn_blocking`, not straight from an async task.
///
/// Lock order: `serialports` before `virtual_ports` and the disconnect tombstones;
/// never hold a map lock across device I/O, thread spawn, or `JoinHandle` join.
#[derive(Clone, Default)]
pub struct SerialManager {
    #[doc(hidden)]
    pub serialports: Arc<Mutex<HashMap<String, SerialportInfo>>>,
    #[doc(hidden)]
    pub virtual_ports: Arc<Mutex<HashMap<String, crate::state::VirtualPortRef>>>,
    core: Arc<CoreState>,
}

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

    /// Android only: hook USB detach / port-list callbacks from the Kotlin side into this manager.
    #[cfg(target_os = "android")]
    pub fn setup_teardown(&self) {
        let manager = self.downgrade();
        // USB failure: released like a desktop disconnect (state event, the path stays
        // listed until `close`, watches stay registered as on desktop and in 4.x).
        crate::android::registry::set_rust_state_fail(Box::new(move |path, reason| {
            if let Some(m) = manager.upgrade() {
                let removed = {
                    let mut map = crate::sync_util::lock_or_recover(&m.serialports);
                    let removed = map.remove(path);
                    if removed.is_some() {
                        // As on desktop: the tombstone lands under the map lock.
                        crate::sync_util::lock_or_recover(&m.core.disconnected)
                            .insert(path.to_string(), reason.to_string());
                    }
                    removed
                };
                if let Some(info) = removed {
                    m.release(path, info, reason);
                }
            }
        }));
        crate::port::list_monitor::set_android_enumerator(move |single| {
            platform_available_ports(single)
        });
        crate::android::registry::init_registry();
    }

    /// Get serial port list
    pub fn available_ports(
        &self,
        single_port_per_device: bool,
    ) -> Result<HashMap<String, HashMap<String, String>>, Error> {
        platform_available_ports(single_port_per_device)
    }

    /// Subscribe to available-port attach/detach events. `sink` first gets a
    /// [`Snapshot`](crate::events::PortListEvent::Snapshot), then `added` / `removed`.
    /// Returns the id for [`Self::unwatch_ports`].
    pub fn watch_ports(
        &self,
        options: crate::events::WatchPortsOptions,
        sink: PortListSink,
    ) -> Result<u32, Error> {
        self.watch_ports_with_id(
            next_watch_id(crate::port::list_monitor::is_subscribed),
            options,
            sink,
        )
    }

    /// [`Self::watch_ports`] under a caller-chosen id (the plugin uses its IPC channel id).
    pub fn watch_ports_with_id(
        &self,
        id: u32,
        options: crate::events::WatchPortsOptions,
        sink: PortListSink,
    ) -> Result<u32, Error> {
        crate::port::list_monitor::subscribe_sink(id, sink, options)?;
        Ok(id)
    }

    /// Stop a port-list watch session.
    pub fn unwatch_ports(&self, channel_id: u32) -> Result<(), Error> {
        crate::port::list_monitor::unsubscribe(channel_id);
        Ok(())
    }

    /// Get a list of managed serial ports.
    pub fn managed_ports(&self) -> Result<Vec<String>, Error> {
        // Lock the Mutex to safely access the data inside `self.serialports`.
        let ports = self
            .serialports
            .lock()
            .map_err(|_| Error::String("Failed to lock serialports mutex".to_string()))?;

        let mut port_list: Vec<String> = ports.keys().cloned().collect();
        drop(ports);
        if let Ok(virtuals) = self.virtual_ports.lock() {
            port_list.extend(virtuals.keys().cloned());
        }
        port_list.extend(
            crate::sync_util::lock_or_recover(&self.core.disconnected)
                .keys()
                .cloned(),
        );
        port_list.sort();
        port_list.dedup();
        Ok(port_list)
    }

    /// Cancel an in-flight poll [`Self::read`] (does not stop an active [`Self::watch`]).
    pub fn cancel_read(&self, path: String) -> Result<(), Error> {
        let cp = self.resolve_connected_port(&path)?;
        if let Ok(guard) = cp.rx_hub.lock() {
            if let Some(hub) = guard.as_ref() {
                hub.shared().cancel_pending_read();
            }
        }
        Ok(())
    }

    /// Close the specified serial port. What the hub still holds (the watch batch, a
    /// parser's remainder) reaches the watches first, in order; no `Disconnect` is
    /// sent. Succeeds once on a port the core already released after a disconnect.
    pub fn close(&self, path: String) -> Result<(), Error> {
        self.close_core(path).map_err(Error::from)
    }

    /// [`Self::close`] with a [`CoreError`].
    pub fn close_core(&self, path: String) -> Result<(), CoreError> {
        log_debug!("close {}", path);

        if crate::sync_util::lock_or_recover(&self.virtual_ports).contains_key(&path) {
            for channel_id in self.watch_ids_for(&path) {
                let _ = self.unwatch(channel_id);
            }
            for id in self.core_watch_ids_for(&path) {
                let _ = self.unwatch_core(SubscriptionId(id));
            }
            let removed = crate::sync_util::lock_or_recover(&self.virtual_ports).remove(&path);
            if let Some(vp) = removed {
                vp.tx_queue.cancel_all();
                if let Ok(cp) = self.resolve_connected_port(&vp.physical_path) {
                    if let Some(session) = crate::sync_util::lock_or_recover(&cp.mux).clone() {
                        session.unregister_dlci(vp.dlci);
                    }
                }
                self.emit_port_state(&path, PortStatus::Closed);
                return Ok(());
            }
        }

        // Physical port: its virtual channels and CMUX session go with it.
        if self.resolve_connected_port(&path).is_ok() {
            let _ = self.disable_mux(path.clone());
        }

        let removed = match self.serialports.lock() {
            Ok(mut serialports) => serialports.remove(&path),
            Err(error) => {
                return Err(Error::String(format!("Failed to acquire lock: {}", error)).into());
            }
        };

        if let Some(port_info) = removed {
            log_debug!("stop {}", path);
            self.drop_writer(&path);
            let finished = finish_serialport_info(&path, port_info);
            self.forget_watches(&path);
            finished?;
            self.emit_port_state(&path, PortStatus::Closed);
            log_debug!("end {}", path);
            Ok(())
        } else {
            self.forget_watches(&path);
            if crate::sync_util::lock_or_recover(&self.core.disconnected)
                .remove(&path)
                .is_some()
            {
                return Ok(());
            }
            Err(CoreError::new(
                ErrorKind::NotFound,
                Error::String(format!("Serial port {} is not open!", path)),
            ))
        }
    }

    /// Close all open serial ports
    pub fn close_all(&self) -> Result<(), Error> {
        let paths = self.managed_ports()?;
        let mut errors = vec![];

        for path in paths {
            if let Err(e) = self.close(path) {
                errors.push(e.to_string());
            }
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(Error::String(errors.join(", ")))
        }
    }

    /// Force close a serial port
    pub fn force_close(&self, path: String) -> Result<(), Error> {
        if crate::sync_util::lock_or_recover(&self.virtual_ports).contains_key(&path) {
            return self.close(path);
        }
        self.close_virtual_channels_of(&path);
        if let Ok(cp) = self.resolve_connected_port(&path) {
            if let Some(session) = crate::sync_util::lock_or_recover(&cp.mux).take() {
                session.close_down();
            }
        }

        // Remove under the lock, join the hub outside it (lock order, see the struct docs).
        let removed = match self.serialports.lock() {
            Ok(mut map) => map.remove(&path),
            Err(error) => return Err(Error::String(format!("Failed to acquire lock: {}", error))),
        };
        self.drop_writer(&path);
        crate::sync_util::lock_or_recover(&self.core.disconnected).remove(&path);
        if let Some(serial) = removed {
            let finished = finish_serialport_info(&path, serial);
            self.forget_watches(&path);
            finished?;
            self.emit_port_state(&path, PortStatus::Closed);
        } else {
            self.forget_watches(&path);
        }
        Ok(())
    }

    #[allow(clippy::too_many_arguments)]
    pub fn open(
        &self,
        path: String,
        baud_rate: u32,
        data_bits: Option<DataBits>,
        flow_control: Option<FlowControl>,
        parity: Option<Parity>,
        stop_bits: Option<StopBits>,
        timeout: Option<u64>,
    ) -> Result<String, Error> {
        self.open_with_lines(
            path,
            baud_rate,
            data_bits,
            flow_control,
            parity,
            stop_bits,
            timeout,
            crate::state::ModemLinesOnOpen::default(),
        )
    }

    /// [`Self::open`] with explicit DTR / RTS levels on open.
    #[allow(clippy::too_many_arguments)]
    pub fn open_with_lines(
        &self,
        path: String,
        baud_rate: u32,
        data_bits: Option<DataBits>,
        flow_control: Option<FlowControl>,
        parity: Option<Parity>,
        stop_bits: Option<StopBits>,
        timeout: Option<u64>,
        lines: crate::state::ModemLinesOnOpen,
    ) -> Result<String, Error> {
        self.open_core(
            path,
            baud_rate,
            data_bits,
            flow_control,
            parity,
            stop_bits,
            timeout,
            lines,
        )
        .map_err(Error::from)
    }

    /// [`Self::open_with_lines`] with a [`CoreError`] (kind `NotFound`, `PermissionDenied`,
    /// `Busy`, … and the platform error as its source). `timeout` (ms, default 1000)
    /// is the port's read / write timeout; [`Self::set_timeout`] changes it later.
    ///
    /// Opening a path that is already open (or disconnected and not yet closed)
    /// replaces that session, as `open` does in 4.x: it is closed first, with its
    /// watches, `watch_core` streams, CMUX channels and RX hub.
    #[allow(clippy::too_many_arguments)]
    pub fn open_core(
        &self,
        path: String,
        baud_rate: u32,
        data_bits: Option<DataBits>,
        flow_control: Option<FlowControl>,
        parity: Option<Parity>,
        stop_bits: Option<StopBits>,
        timeout: Option<u64>,
        lines: crate::state::ModemLinesOnOpen,
    ) -> Result<String, CoreError> {
        self.open_core_with(
            path,
            baud_rate,
            data_bits,
            flow_control,
            parity,
            stop_bits,
            timeout,
            lines,
            None,
        )
    }

    /// [`Self::open_core`] plus a [`Self::watch_core`] stream that exists before the
    /// port's first read: no byte and no event is lost (a `RDY` / URC sent right after
    /// open, or a `Disconnect` of a port that dies at once, which still returns `Ok`).
    /// Returns the session path and the stream's id (for [`Self::unwatch_core`]). An
    /// invalid `parser` fails before the port is opened.
    #[allow(clippy::too_many_arguments)]
    pub fn open_watch_core(
        &self,
        path: String,
        baud_rate: u32,
        data_bits: Option<DataBits>,
        flow_control: Option<FlowControl>,
        parity: Option<Parity>,
        stop_bits: Option<StopBits>,
        timeout: Option<u64>,
        lines: crate::state::ModemLinesOnOpen,
        options: WatchOptions,
        parser: Option<serial_codec::framing::FramerConfig>,
        sink: CoreSink,
    ) -> Result<(String, SubscriptionId), CoreError> {
        let (id, queue, slot) = self.first_core_watch(&options, parser, sink)?;
        match self.open_core_with(
            path,
            baud_rate,
            data_bits,
            flow_control,
            parity,
            stop_bits,
            timeout,
            lines,
            Some(slot),
        ) {
            Ok(session) => {
                crate::sync_util::lock_or_recover(&self.core.core_watches)
                    .insert(id, session.clone());
                Ok((session, SubscriptionId(id)))
            }
            Err(e) => {
                queue.close();
                Err(e)
            }
        }
    }

    /// [`Self::insert_port`] plus a [`Self::watch_core`] stream that exists before the
    /// port's first read, as [`Self::open_watch_core`].
    pub fn insert_port_watch_core(
        &self,
        path: String,
        port: Box<dyn SerialPortTrait>,
        options: WatchOptions,
        parser: Option<serial_codec::framing::FramerConfig>,
        sink: CoreSink,
    ) -> Result<SubscriptionId, CoreError> {
        let (id, queue, slot) = self.first_core_watch(&options, parser, sink)?;
        match self.insert_port_with(path.clone(), port, Some(slot)) {
            Ok(()) => {
                crate::sync_util::lock_or_recover(&self.core.core_watches).insert(id, path);
                Ok(SubscriptionId(id))
            }
            Err(e) => {
                queue.close();
                Err(e)
            }
        }
    }

    /// The stream of `open_watch_core` / `insert_port_watch_core`, not attached yet.
    fn first_core_watch(
        &self,
        options: &WatchOptions,
        parser: Option<serial_codec::framing::FramerConfig>,
        sink: CoreSink,
    ) -> Result<(u64, Arc<CoreQueue>, crate::hub::shared::CoreWatchSlot), CoreError> {
        let framer = parser
            .as_ref()
            .map(|p| p.build())
            .transpose()
            .map_err(|e| CoreError::new(ErrorKind::Other, Error::String(e.to_string())))?;
        let (batch_timeout, read_size, route_urc) = watch_params(options);
        let id = next_subscription_id();
        let queue = CoreQueue::spawn(sink, self.core_queue_capacity());
        let slot = crate::hub::shared::CoreWatchSlot::new(
            id,
            queue.event_sink(),
            queue.alive_flag(),
            batch_timeout,
            read_size,
            route_urc,
            framer,
        );
        Ok((id, queue, slot))
    }

    #[allow(clippy::too_many_arguments)]
    fn open_core_with(
        &self,
        path: String,
        baud_rate: u32,
        data_bits: Option<DataBits>,
        flow_control: Option<FlowControl>,
        parity: Option<Parity>,
        stop_bits: Option<StopBits>,
        timeout: Option<u64>,
        lines: crate::state::ModemLinesOnOpen,
        first_watch: Option<crate::hub::shared::CoreWatchSlot>,
    ) -> Result<String, CoreError> {
        // Re-open (e.g. after a webview reload): tear the old session down the same
        // way `close` does, so its watches, CMUX channels and hub go with it.
        let already_open = matches!(
            crate::sync_util::lock_or_recover(&self.serialports)
                .get(&path)
                .map(|i| &i.state),
            Some(PortState::Connected(_))
        );
        let released =
            crate::sync_util::lock_or_recover(&self.core.disconnected).contains_key(&path);
        if already_open || released {
            log_info!("Replacing existing port {}", path);
            let _ = self.close(path.clone());
        }
        let stale = {
            let mut serialports = self
                .serialports
                .lock()
                .map_err(|e| Error::String(format!("Failed to acquire lock: {}", e)))?;
            if matches!(
                serialports.get(&path).map(|i| &i.state),
                Some(PortState::Opening)
            ) {
                return Err(Error::String(format!("open already in progress for {}", path)).into());
            }
            let stale = serialports.remove(&path);
            serialports.insert(
                path.clone(),
                SerialportInfo {
                    state: PortState::Opening,
                },
            );
            stale
        };

        if let Some(existing) = stale {
            log_info!("Replacing existing port {}", path);
            finish_serialport_info(&path, existing)?;
        }

        let timeout = Duration::from_millis(timeout.unwrap_or(DEFAULT_PORT_TIMEOUT_MS));
        let port_result = platform_open(
            &path,
            baud_rate,
            data_bits.unwrap_or(DataBits::Eight),
            flow_control.unwrap_or(FlowControl::None),
            parity.unwrap_or(Parity::None),
            stop_bits.unwrap_or(StopBits::One),
            timeout,
            lines,
        );

        let mut serialports = self
            .serialports
            .lock()
            .map_err(|e| Error::String(format!("Failed to acquire lock: {}", e)))?;

        match port_result {
            Ok((session_path, port)) => {
                if session_path != path {
                    serialports.remove(&path);
                    if !serialports.contains_key(&session_path) {
                        serialports.insert(
                            session_path.clone(),
                            SerialportInfo {
                                state: PortState::Opening,
                            },
                        );
                    }
                }

                let (cp, hub) = self.connect(&session_path, port, timeout, first_watch);
                let reserved = matches!(
                    serialports.get(&session_path).map(|i| &i.state),
                    Some(PortState::Opening)
                );
                if let (Ok(()), true) = (&hub, reserved) {
                    if let Some(entry) = serialports.get_mut(&session_path) {
                        entry.state = PortState::Connected(cp);
                    }
                    // Under the map lock: a disconnect hook (it waits for the lock)
                    // reports `Disconnected` after this `Opened`.
                    self.emit_port_state(&session_path, PortStatus::Opened);
                    drop(serialports);
                    return Ok(session_path);
                }
                // Never leave an `Opening` entry or a running hub thread behind.
                if reserved {
                    serialports.remove(&session_path);
                }
                drop(serialports);
                self.drop_writer(&session_path);
                let _ = finish_serialport_info(
                    &session_path,
                    SerialportInfo {
                        state: PortState::Connected(cp),
                    },
                );
                hub?;
                Err(
                    Error::String(format!("Port '{}' state changed during open", session_path))
                        .into(),
                )
            }
            Err(e) => {
                if matches!(
                    serialports.get(&path).map(|i| &i.state),
                    Some(PortState::Opening)
                ) {
                    serialports.remove(&path);
                }
                Err(e)
            }
        }
    }

    /// Manage `port` under `path` as if [`Self::open`] had opened it (tests, custom
    /// transports, a fake device). An open port under `path` is closed first. Its
    /// current `timeout()` is the port timeout.
    ///
    /// On Unix (not TCP / BLE) writes and control calls go to `port.try_clone()` when
    /// it succeeds, reads to `port`: a clone must share the device (like a dup'ed fd),
    /// or `try_clone` must return `Err` to keep one handle for everything.
    pub fn insert_port(
        &self,
        path: String,
        port: Box<dyn SerialPortTrait>,
    ) -> Result<(), CoreError> {
        self.insert_port_with(path, port, None)
    }

    fn insert_port_with(
        &self,
        path: String,
        port: Box<dyn SerialPortTrait>,
        first_watch: Option<crate::hub::shared::CoreWatchSlot>,
    ) -> Result<(), CoreError> {
        let known = crate::sync_util::lock_or_recover(&self.serialports).contains_key(&path)
            || crate::sync_util::lock_or_recover(&self.core.disconnected).contains_key(&path);
        if known {
            let _ = self.close(path.clone());
        }
        let timeout = port.timeout();
        // Under the map lock (as `open_core`): a device failing at once is reported by
        // its disconnect hook, which waits for the record.
        let mut serialports = crate::sync_util::lock_or_recover(&self.serialports);
        let (cp, hub) = self.connect(&path, port, timeout, first_watch);
        if let Err(e) = hub {
            drop(serialports);
            self.drop_writer(&path);
            let _ = finish_serialport_info(
                &path,
                SerialportInfo {
                    state: PortState::Connected(cp),
                },
            );
            return Err(e.into());
        }
        serialports.insert(
            path.clone(),
            SerialportInfo {
                state: PortState::Connected(cp),
            },
        );
        self.emit_port_state(&path, PortStatus::Opened);
        drop(serialports);
        Ok(())
    }

    /// Stream port events into `sink` (one watch per port and manager). Returns the id
    /// for [`Self::unwatch`]. As in 4.x, `sink` runs on the RX side: the port's hub
    /// thread, or the caller of `unwatch` / `close` for the tail they deliver. It never
    /// runs under a manager lock, but a slow sink holds up RX (keep it short, e.g.
    /// forward into a channel), and calling `at()` / `exchange` on the same port from
    /// it waits for the RX it blocks. Nothing reaches it after `close()` returns.
    /// [`Self::watch_core`] has its own delivery thread instead.
    pub fn watch(
        &self,
        path: String,
        options: WatchOptions,
        sink: EventSink,
    ) -> Result<u32, Error> {
        self.watch_framed(path, options, None, sink)
    }

    /// [`Self::watch`] under a caller-chosen id (the plugin uses its IPC channel id).
    pub fn watch_with_id(
        &self,
        channel_id: u32,
        path: String,
        options: WatchOptions,
        sink: EventSink,
    ) -> Result<u32, Error> {
        self.watch_framed_with_id(channel_id, path, options, None, sink)
    }

    /// [`Self::watch`] with a native parser: each complete frame becomes exactly one
    /// [`SerialEvent::Data`](crate::events::SerialEvent::Data) (the batch timer no longer
    /// applies). An invalid config fails before the watch starts. With
    /// [`WatchOptions::route_urc`] the URC split runs first and only non-URC lines reach
    /// the parser. On unwatch, close or disconnect the parser's buffered remainder is
    /// sent as a final `data` event (before `disconnect`), like Node serialport emits
    /// on stream end.
    pub fn watch_framed(
        &self,
        path: String,
        options: WatchOptions,
        parser: Option<serial_codec::framing::FramerConfig>,
        sink: EventSink,
    ) -> Result<u32, Error> {
        let id = next_watch_id(|id| {
            crate::sync_util::lock_or_recover(&self.core.watches).contains_key(&id)
                || crate::port::watch_registry::contains(id)
        });
        self.watch_framed_with_id(id, path, options, parser, sink)
    }

    /// [`Self::watch_framed`] under a caller-chosen id.
    pub fn watch_framed_with_id(
        &self,
        channel_id: u32,
        path: String,
        options: WatchOptions,
        parser: Option<serial_codec::framing::FramerConfig>,
        channel: EventSink,
    ) -> Result<u32, Error> {
        // Validate the parser before anything is registered or attached.
        let mut framer = parser
            .as_ref()
            .map(|p| p.build())
            .transpose()
            .map_err(|e| Error::String(e.to_string()))?;
        self.register_watch(channel_id, &path)?;
        log_debug!("Starting watch on port: {} (channel {})", path, channel_id);

        // Released after a disconnect: the late watch gets that `Disconnect` (4.x).
        if let Some(reason) = self.released_reason(&path) {
            crate::port::watch_registry::send_event_to(
                &channel,
                SerialEvent::Disconnect { path, reason },
            );
            return Ok(channel_id);
        }

        let (batch_timeout, read_size, route_urc) = watch_params(&options);

        if let Ok(virtuals) = self.virtual_ports.lock() {
            if let Some(vp) = virtuals.get(&path).cloned() {
                drop(virtuals);
                let session = self
                    .resolve_connected_port(&vp.physical_path)
                    .and_then(|cp| {
                        crate::sync_util::lock_or_recover(&cp.mux)
                            .clone()
                            .ok_or_else(|| Error::String("CMUX not enabled".into()))
                    });
                let session = match session {
                    Ok(session) => session,
                    Err(e) => {
                        self.unregister_watch(channel_id);
                        return Err(e);
                    }
                };
                if let Err(e) = self.ensure_rx_hub_on_physical(&vp.physical_path) {
                    self.unregister_watch(channel_id);
                    return Err(e);
                }
                session.set_watch_with(vp.dlci, channel, batch_timeout, framer);
                return Ok(channel_id);
            }
        }

        // A replaced watch's remainder is queued under the locks, delivered after.
        let mut replaced = None;
        let attached = self.with_connected_port(path.clone(), |cp| {
            if cp.virtual_dlci.is_some() {
                return Err(Error::String("legacy virtual ConnectedPort".into()));
            }
            if let Some(shared) = cp
                .rx_hub
                .lock()
                .ok()
                .and_then(|g| g.as_ref().map(|h| h.shared()))
                .filter(|s| s.has_watch())
            {
                shared.detach_watch_queued();
                replaced = Some(shared);
            }
            self.ensure_rx_hub(&cp.handle(), &path)?;
            let guard = cp
                .rx_hub
                .lock()
                .map_err(|e| Error::String(format!("Mutex lock failed: {}", e)))?;
            let hub = guard
                .as_ref()
                .ok_or_else(|| Error::String("RX hub missing after start".into()))?;
            hub.attach_watch_with(channel, batch_timeout, read_size, route_urc, framer.take());
            Ok(())
        });
        if let Some(shared) = replaced {
            shared.deliver_queued();
        }
        if let Err(e) = attached {
            self.unregister_watch(channel_id);
            return Err(e);
        }

        Ok(channel_id)
    }

    pub fn unwatch(&self, channel_id: u32) -> Result<(), Error> {
        let path = self
            .unregister_watch(channel_id)
            .ok_or_else(|| Error::new(format!("No active watch for channel {}", channel_id)))?;
        log_debug!("Stopping watch on port: {} (channel {})", path, channel_id);
        self.stop_watch_thread(&path)
    }

    /// Detach the watch of `path`. The remainder goes to the sink after every lock
    /// here is released, so a sink calling back into the manager cannot deadlock.
    fn stop_watch_thread(&self, path: &str) -> Result<(), Error> {
        let virtual_port = self
            .virtual_ports
            .lock()
            .ok()
            .and_then(|v| v.get(path).cloned());
        if let Some(vp) = virtual_port {
            if let Ok(cp) = self.resolve_connected_port(&vp.physical_path) {
                let session = crate::sync_util::lock_or_recover(&cp.mux).clone();
                if let Some(session) = session {
                    session.clear_watch(vp.dlci);
                }
            }
            return Ok(());
        }
        enum Detach {
            Mux(Arc<CmuxSession>, u8),
            Hub(Arc<crate::hub::RxHubShared>),
            Nothing,
        }
        // A port the core already released (disconnect) has nothing left to detach.
        let detach = self
            .with_connected_port(path.to_string(), |cp| {
                if let Some(dlci) = cp.virtual_dlci {
                    let session = crate::sync_util::lock_or_recover(&cp.mux).clone();
                    return Ok(session.map_or(Detach::Nothing, |s| Detach::Mux(s, dlci)));
                }
                let shared = cp
                    .rx_hub
                    .lock()
                    .ok()
                    .and_then(|g| g.as_ref().map(|h| h.shared()));
                Ok(shared.map_or(Detach::Nothing, Detach::Hub))
            })
            .unwrap_or(Detach::Nothing);
        match detach {
            Detach::Mux(session, dlci) => session.clear_watch(dlci),
            Detach::Hub(shared) => shared.detach_watch(),
            Detach::Nothing => {}
        }
        Ok(())
    }

    pub fn read(
        &self,
        path: String,
        timeout: Option<u64>,
        size: Option<usize>,
    ) -> Result<String, Error> {
        let bytes = self.read_via_hub(path, timeout, size, false)?;
        Ok(String::from_utf8_lossy(&bytes).to_string())
    }

    pub fn read_binary(
        &self,
        path: String,
        timeout: Option<u64>,
        size: Option<usize>,
    ) -> Result<Vec<u8>, Error> {
        self.read_via_hub(path, timeout, size, true)
    }

    fn read_via_hub(
        &self,
        path: String,
        timeout: Option<u64>,
        size: Option<usize>,
        fill: bool,
    ) -> Result<Vec<u8>, Error> {
        let cp = self.resolve_connected_port(&path)?;
        self.ensure_rx_hub(&cp, &path)?;
        let hub_shared = {
            let guard = cp
                .rx_hub
                .lock()
                .map_err(|e| Error::String(format!("Mutex lock failed: {}", e)))?;
            guard
                .as_ref()
                .ok_or_else(|| Error::String("RX hub missing".into()))?
                .shared()
        };
        hub_shared
            .read_request(size.unwrap_or(1024), timeout.unwrap_or(1000), fill)
            .map_err(Error::String)
    }

    /// Write data to the serial port (or a CMUX channel `path#dlci=N`).
    pub fn write(&self, path: String, value: String) -> Result<usize, Error> {
        self.write_bytes(&path, value.as_bytes(), "write data")
            .map_err(Error::from)
    }

    /// Write binary data to the serial port (or a CMUX channel `path#dlci=N`).
    pub fn write_binary(&self, path: String, value: Vec<u8>) -> Result<usize, Error> {
        self.write_bytes(&path, &value, "write binary data")
            .map_err(Error::from)
    }

    /// [`Self::write_binary`] with a [`CoreError`] (the I/O error as its source).
    pub fn write_core(&self, path: String, data: Vec<u8>) -> Result<usize, CoreError> {
        self.write_bytes(&path, &data, "write binary data")
    }

    fn write_bytes(&self, path: &str, data: &[u8], operation: &str) -> Result<usize, CoreError> {
        let virtual_port = crate::sync_util::lock_or_recover(&self.virtual_ports)
            .get(path)
            .cloned();
        if let Some(vp) = virtual_port {
            let cp = self.resolve_connected_port(&vp.physical_path)?;
            let session = crate::sync_util::lock_or_recover(&cp.mux)
                .clone()
                .ok_or_else(|| Error::String("CMUX not enabled".into()))?;
            return session
                .send_uih(vp.dlci, data)
                .map_err(|e| Error::String(format!("Failed to {operation}: {e}")).into());
        }
        let cp = self.resolve_connected_port(path)?;
        match self.writer_of(path, &cp) {
            Some(writer) => write_all_split(&writer, data, operation),
            None => with_port_try_lock(
                &cp.port,
                Duration::from_millis(PORT_LOCK_TIMEOUT_MS),
                |port| write_all_port(port, data, operation),
            ),
        }
    }

    /// Set the baud rate
    pub fn set_baud_rate(&self, path: String, baud_rate: u32) -> Result<(), Error> {
        self.get_serialport(path, |port| {
            port.set_baud_rate(baud_rate)
                .map_err(|e| Error::String(format!("Failed to set baud rate: {}", e)))
        })
    }

    /// Set the data bits
    pub fn set_data_bits(&self, path: String, data_bits: DataBits) -> Result<(), Error> {
        self.get_serialport(path, |port| {
            port.set_data_bits(data_bits.into()).map_err(Error::from)
        })
    }

    /// Set the flow control
    pub fn set_flow_control(&self, path: String, flow_control: FlowControl) -> Result<(), Error> {
        self.get_serialport(path, |port| {
            port.set_flow_control(flow_control.into())
                .map_err(Error::from)
        })
    }

    /// Set the parity
    pub fn set_parity(&self, path: String, parity: Parity) -> Result<(), Error> {
        self.get_serialport(path, |port| {
            port.set_parity(parity.into()).map_err(Error::from)
        })
    }

    /// Set the stop bits
    pub fn set_stop_bits(&self, path: String, stop_bits: StopBits) -> Result<(), Error> {
        self.get_serialport(path, |port| {
            port.set_stop_bits(stop_bits.into()).map_err(Error::from)
        })
    }

    /// Set the timeout
    pub fn set_timeout(&self, path: String, timeout: Duration) -> Result<(), Error> {
        self.get_serialport(path, |port| port.set_timeout(timeout).map_err(Error::from))
    }

    /// Set the RTS (Request To Send) control signal
    pub fn write_request_to_send(&self, path: String, level: bool) -> Result<(), Error> {
        self.get_serialport(path, |port| {
            port.write_request_to_send(level).map_err(Error::from)
        })
    }

    /// Set the DTR (Data Terminal Ready) control signal
    pub fn write_data_terminal_ready(&self, path: String, level: bool) -> Result<(), Error> {
        self.get_serialport(path, |port| {
            port.write_data_terminal_ready(level).map_err(Error::from)
        })
    }

    /// Read the CTS (Clear To Send) control signal state
    pub fn read_clear_to_send(&self, path: String) -> Result<bool, Error> {
        self.get_serialport(path, |port| port.read_clear_to_send().map_err(Error::from))
    }

    /// Read the DSR (Data Set Ready) control signal state
    pub fn read_data_set_ready(&self, path: String) -> Result<bool, Error> {
        self.get_serialport(path, |port| port.read_data_set_ready().map_err(Error::from))
    }

    /// Read the RI (Ring Indicator) control signal state
    pub fn read_ring_indicator(&self, path: String) -> Result<bool, Error> {
        self.get_serialport(path, |port| port.read_ring_indicator().map_err(Error::from))
    }

    /// Read the CD (Carrier Detect) control signal state
    pub fn read_carrier_detect(&self, path: String) -> Result<bool, Error> {
        self.get_serialport(path, |port| port.read_carrier_detect().map_err(Error::from))
    }

    /// Get the number of bytes available to read
    pub fn bytes_to_read(&self, path: String) -> Result<u32, Error> {
        if let Some(reason) = self.released_reason(&path) {
            return Err(Error::String(reason));
        }
        let hub_buffered = if let Ok(cp) = self.resolve_connected_port(&path) {
            cp.rx_hub
                .lock()
                .ok()
                .and_then(|guard| guard.as_ref().map(|hub| hub.shared().buffered_len() as u32))
                .unwrap_or(0)
        } else {
            0
        };
        // Wait for the handle (the hub holds the reader during a poll) instead of
        // reporting 0 whenever it is busy.
        let port_buffered = if let Ok(cp) = self.resolve_connected_port(&path) {
            let port = self.writer_of(&path, &cp).unwrap_or(cp.port);
            lock_with_deadline(
                &port,
                Duration::from_millis(PORT_LOCK_TIMEOUT_MS),
                false,
                |p| p.bytes_to_read().map_err(Error::from),
            )
            .unwrap_or(0)
        } else {
            0
        };
        Ok(hub_buffered.saturating_add(port_buffered))
    }

    /// Get the number of bytes waiting to be written
    pub fn bytes_to_write(&self, path: String) -> Result<u32, Error> {
        self.get_serialport(path, |port| port.bytes_to_write().map_err(Error::from))
    }

    /// Clear input/output buffers
    /// `Input` / `All` also drop what the RX hub holds for no one yet.
    pub fn clear_buffer(&self, path: String, buffer_to_clear: ClearBuffer) -> Result<(), Error> {
        let input = !matches!(buffer_to_clear, ClearBuffer::Output);
        self.get_serialport(path.clone(), |port| {
            port.clear(buffer_to_clear.into()).map_err(Error::from)
        })?;
        if input {
            if let Ok(cp) = self.resolve_connected_port(&path) {
                if let Some(hub) = crate::sync_util::lock_or_recover(&cp.rx_hub).as_ref() {
                    hub.shared().purge_buffers();
                }
            }
        }
        Ok(())
    }

    /// Start break signal transmission
    pub fn set_break(&self, path: String) -> Result<(), Error> {
        self.get_serialport(path, |port| port.set_break().map_err(Error::from))
    }

    /// Stop break signal transmission
    pub fn clear_break(&self, path: String) -> Result<(), Error> {
        self.get_serialport(path, |port| port.clear_break().map_err(Error::from))
    }

    /// Write payload and read until terminators, idle silence, or timeout (AT-style exchange).
    pub fn exchange(
        &self,
        path: String,
        value: String,
        options: ExchangeOptions,
    ) -> Result<crate::at::parse::ExchangeResponse, Error> {
        self.exchange_bytes(path, value.into_bytes(), options)
    }

    /// Binary variant of [`Self::exchange`].
    pub fn exchange_binary(
        &self,
        path: String,
        value: Vec<u8>,
        options: ExchangeOptions,
    ) -> Result<crate::at::parse::ExchangeResponse, Error> {
        self.exchange_bytes(path, value, options)
    }

    /// [`Self::exchange_binary`] with a [`CoreError`] (`Timeout`, `Cancelled`, …).
    pub fn exchange_core(
        &self,
        path: String,
        value: Vec<u8>,
        options: ExchangeOptions,
    ) -> Result<crate::at::parse::ExchangeResponse, CoreError> {
        self.exchange_bytes(path, value, options)
            .map_err(CoreError::from)
    }

    fn get_tx_queue(&self, path: &str) -> Result<Arc<PortTxQueue>, Error> {
        if let Ok(virtuals) = self.virtual_ports.lock() {
            if let Some(vp) = virtuals.get(path) {
                return Ok(vp.tx_queue.clone());
            }
        }
        let ports = self
            .serialports
            .lock()
            .map_err(|e| Error::String(format!("Mutex lock failed: {}", e)))?;
        let info = ports.get(path).ok_or_else(|| self.not_open_error(path))?;
        match &info.state {
            PortState::Connected(cp) => Ok(cp.tx_queue.clone()),
            other => Err(Error::String(other.not_connected_reason())),
        }
    }

    fn exchange_bytes(
        &self,
        path: String,
        payload: Vec<u8>,
        options: ExchangeOptions,
    ) -> Result<crate::at::parse::ExchangeResponse, Error> {
        run_exchange_queued(
            path,
            options,
            |p| self.get_tx_queue(p),
            |physical_path,
             dlci,
             virtual_path,
             payload,
             options|
             -> Result<crate::at::parse::ExchangeResponse, Error> {
                self.exchange_bytes_mux_direct(physical_path, dlci, virtual_path, payload, options)
            },
            |path, payload, options| -> Result<crate::at::parse::ExchangeResponse, Error> {
                self.exchange_bytes_direct(path, payload, options)
            },
            payload,
        )
    }

    fn exchange_bytes_direct(
        &self,
        path: String,
        payload: Vec<u8>,
        options: ExchangeOptions,
    ) -> Result<crate::at::parse::ExchangeResponse, Error> {
        let command = options
            .command
            .clone()
            .unwrap_or_else(|| String::from_utf8_lossy(&payload).into_owned());
        let user_solicited = options.solicited_prefixes.clone().unwrap_or_default();
        let resolved_timeout = options.resolve().timeout_ms;

        let cp = self.resolve_connected_port(&path)?;
        self.ensure_rx_hub(&cp, &path)?;

        struct DesktopExchangeIo<'a> {
            port: &'a PortArc,
            writer: Option<PortArc>,
            timeout_ms: u64,
        }
        impl crate::exchange::io::ExchangeIo for DesktopExchangeIo<'_> {
            fn purge_rx(&self) -> Result<(), Error> {
                let (port, io_timeout) = match &self.writer {
                    Some(w) => (w, false),
                    None => (self.port, true),
                };
                lock_with_deadline(
                    port,
                    Duration::from_millis(self.timeout_ms.min(5000)),
                    io_timeout,
                    |port| port.clear(ClearBuffer::Input.into()).map_err(Error::from),
                )
            }
            fn write_payload(&self, payload: &[u8]) -> Result<(), Error> {
                if let Some(w) = &self.writer {
                    return write_all_split(w, payload, "exchange write")
                        .map(|_| ())
                        .map_err(Error::from);
                }
                with_port_try_lock(
                    self.port,
                    Duration::from_millis(self.timeout_ms.min(5000)),
                    |port| {
                        write_all_port(port, payload, "exchange write")
                            .map(|_| ())
                            .map_err(Error::from)
                    },
                )
            }
        }

        let hub = {
            let guard = crate::sync_util::lock_or_recover(&cp.rx_hub);
            guard
                .as_ref()
                .ok_or_else(|| Error::String("RX hub missing".into()))?
                .clone()
        };

        crate::exchange::run::run_physical_exchange(
            hub.as_ref(),
            &DesktopExchangeIo {
                port: &cp.port,
                writer: self.writer_of(&path, &cp),
                timeout_ms: resolved_timeout,
            },
            &command,
            &user_solicited,
            payload,
            options,
            cp.exchange_cancel.clone(),
        )
    }

    fn exchange_bytes_mux_direct(
        &self,
        physical_path: String,
        _dlci: u8,
        virtual_path: String,
        payload: Vec<u8>,
        options: ExchangeOptions,
    ) -> Result<crate::at::parse::ExchangeResponse, Error> {
        let vp = self
            .virtual_ports
            .lock()
            .map_err(|e| Error::String(format!("Mutex lock failed: {}", e)))?
            .get(&virtual_path)
            .cloned()
            .ok_or_else(|| Error::String(format!("Virtual port '{}' not open", virtual_path)))?;
        self.exchange_bytes_mux_via_ref(physical_path, &vp, payload, options)
    }

    fn exchange_bytes_mux_via_ref(
        &self,
        physical_path: String,
        vp: &crate::state::VirtualPortRef,
        payload: Vec<u8>,
        options: ExchangeOptions,
    ) -> Result<crate::at::parse::ExchangeResponse, Error> {
        let dlci = vp.dlci;
        let command = options
            .command
            .clone()
            .unwrap_or_else(|| String::from_utf8_lossy(&payload).into_owned());
        let user_solicited = options.solicited_prefixes.clone().unwrap_or_default();

        let session = {
            let ports = self
                .serialports
                .lock()
                .map_err(|e| Error::String(format!("Mutex lock failed: {}", e)))?;
            let info = ports.get(&physical_path).ok_or_else(|| {
                Error::String(format!("Physical port '{}' not found", physical_path))
            })?;
            match &info.state {
                PortState::Connected(cp) => cp
                    .mux
                    .lock()
                    .map_err(|e| Error::String(format!("Mutex lock failed: {}", e)))?
                    .clone()
                    .ok_or_else(|| Error::String("CMUX not enabled on physical port".into()))?,
                other => return Err(Error::String(other.not_connected_reason())),
            }
        };

        self.ensure_rx_hub_on_physical(&physical_path)?;

        crate::exchange::run::run_mux_exchange(
            &session,
            dlci,
            &command,
            &user_solicited,
            payload,
            options,
            vp.exchange_cancel.clone(),
        )
    }

    fn run_exchange_unqueued(
        &self,
        path: String,
        payload: Vec<u8>,
        options: ExchangeOptions,
    ) -> Result<crate::at::parse::ExchangeResponse, Error> {
        if let Some((physical, dlci)) = parse_mux_path(path.as_str()) {
            return self.exchange_bytes_mux_direct(
                physical.to_string(),
                dlci,
                path,
                payload,
                options,
            );
        }
        self.exchange_bytes_direct(path, payload, options)
    }

    pub fn at(
        &self,
        path: String,
        command: String,
        options: Option<AtCommandOptions>,
    ) -> Result<crate::at::parse::AtCommandResult, Error> {
        let tx_queue = self.get_tx_queue(&path)?;
        crate::at::commands::queue_at(self, &tx_queue, path, command, options)
    }

    /// [`Self::at`] with a [`CoreError`].
    pub fn at_core(
        &self,
        path: String,
        command: String,
        options: Option<AtCommandOptions>,
    ) -> Result<crate::at::parse::AtCommandResult, CoreError> {
        self.at(path, command, options).map_err(CoreError::from)
    }

    /// Multi-phase AT flow (e.g. CMGS) — phases run atomically without interleaving other jobs.
    pub fn at_phases(
        &self,
        path: String,
        phases: Vec<AtPhase>,
    ) -> Result<Vec<crate::at::parse::AtCommandResult>, Error> {
        let tx_queue = self.get_tx_queue(&path)?;
        crate::at::commands::queue_at_phases(self, &tx_queue, path, phases)
    }

    /// Built-in CMGS recipe: `AT+CMGS=n` → `>` → PDU + Ctrl+Z → final line.
    pub fn send_sms_pdu(
        &self,
        path: String,
        length: u32,
        pdu: Vec<u8>,
        options: Option<SendSmsPduOptions>,
    ) -> Result<Vec<crate::at::parse::AtCommandResult>, Error> {
        let tx_queue = self.get_tx_queue(&path)?;
        crate::at::commands::queue_send_sms_pdu(self, &tx_queue, path, length, pdu, options)
    }

    /// Configure AT session defaults for native `at` jobs on this port.
    pub fn configure_at_session(
        &self,
        path: String,
        session: AtSessionConfig,
    ) -> Result<(), Error> {
        let tx_queue = self
            .resolve_connected_port(&path)
            .map(|cp| cp.tx_queue.clone())
            .ok();
        configure_at_session_shared(&self.virtual_ports, tx_queue, &path, session)
    }

    /// Enter GSM 07.10 CMUX mode (`AT+CMUX=…` then attach deframer to the RX hub).
    pub fn enable_mux(&self, path: String, command: String, timeout_ms: u64) -> Result<(), Error> {
        let response = self.exchange(
            path.clone(),
            format!("{command}\r"),
            ExchangeOptions {
                timeout_ms: Some(timeout_ms),
                rx_prepare: Some(crate::events::RxPrepareMode::None),
                ..Default::default()
            },
        )?;
        if response.status != crate::at::parse::AtParseStatus::Ok {
            return Err(Error::String(format!(
                "modem rejected {command}: {}",
                String::from_utf8_lossy(&response.raw).trim()
            )));
        }
        // The modem switches to multiplexer mode a few ms after its OK. A frame sent
        // inside that window is lost, and the Quectel M26 then ignores SABM (retries
        // included) for about 2 s: wait the switch out before the first frame.
        thread::sleep(CMUX_SWITCH_SETTLE);

        let cp = self.resolve_connected_port(&path)?;
        if cp.virtual_dlci.is_some() {
            return Err(Error::String("enable_mux on virtual port".into()));
        }
        if crate::sync_util::lock_or_recover(&cp.mux).is_some() {
            return Err(Error::String("CMUX already enabled".into()));
        }
        let io_port = self
            .writer_of(&path, &cp)
            .unwrap_or_else(|| cp.port.clone());
        io_port
            .lock()
            .map_err(|e| Error::String(format!("Mutex lock failed: {}", e)))?
            .clear(ClearBuffer::Input.into())
            .map_err(Error::from)?;
        let session = CmuxSession::for_command(
            path.clone(),
            Arc::new(crate::cmux::SerialPortIo(io_port)),
            &command,
        );
        // The modem closed channels (DISC / DM / CLD): their state goes out, and when it
        // left multiplexer mode the session is detached (the port is plain again).
        let manager = self.downgrade();
        let weak = Arc::downgrade(&session);
        let physical = path.clone();
        session.set_on_closed(Box::new(move |paths, reason, whole| {
            let Some(m) = manager.upgrade() else {
                return;
            };
            for p in paths {
                m.emit_port_state(
                    p,
                    PortStatus::Disconnected {
                        kind: ErrorKind::Disconnected,
                        reason: reason.to_string(),
                    },
                );
            }
            if whole {
                m.detach_mux_session(&physical, &weak);
            }
        }));
        self.ensure_rx_hub(&cp, &path)?;
        if let Some(hub) = crate::sync_util::lock_or_recover(&cp.rx_hub).as_ref() {
            hub.attach_cmux(session.clone());
        }
        // Control channel first; without its UA the modem is not multiplexing.
        if let Err(e) = session.open_dlci(0, Duration::from_millis(timeout_ms.max(500))) {
            // The modem said OK to AT+CMUX, so it may be multiplexing with the UA lost:
            // CLD takes it back to AT mode.
            session.close_down();
            if let Some(hub) = crate::sync_util::lock_or_recover(&cp.rx_hub).as_ref() {
                hub.detach_cmux();
            }
            return Err(Error::String(e));
        }
        *crate::sync_util::lock_or_recover(&cp.mux) = Some(session);
        Ok(())
    }

    /// Register a virtual DLCI channel on an CMUX-enabled physical port.
    pub fn open_mux_channel(&self, physical_path: String, dlci: u8) -> Result<String, Error> {
        if !crate::cmux::is_data_dlci(dlci) {
            return Err(Error::String(format!(
                "invalid CMUX DLCI {dlci}: data channels are 1..=63"
            )));
        }
        let virtual_path = mux_path(&physical_path, dlci);
        let session = {
            let ports = self
                .serialports
                .lock()
                .map_err(|e| Error::String(format!("Mutex lock failed: {}", e)))?;
            let info = ports
                .get(&physical_path)
                .ok_or_else(|| self.not_open_error(&physical_path))?;
            match &info.state {
                PortState::Connected(cp) => cp
                    .mux
                    .lock()
                    .map_err(|e| Error::String(format!("Mutex lock failed: {}", e)))?
                    .clone()
                    .ok_or_else(|| Error::String("CMUX not enabled".into())),
                other => Err(Error::String(other.not_connected_reason())),
            }
        }?;

        // Already open on *this* session. An entry left from an earlier session (port
        // closed and re-opened) is stale and gets replaced below.
        let known = self
            .virtual_ports
            .lock()
            .map_err(|e| Error::String(format!("Mutex lock failed: {}", e)))?
            .contains_key(&virtual_path);
        if known && session.has_dlci(dlci) {
            return Ok(virtual_path);
        }
        // Closed by the modem or left from an earlier session: its watches are dead.
        if known {
            self.forget_watches(&virtual_path);
        }

        session.register_dlci(dlci, virtual_path.clone());
        if let Err(e) = session.open_dlci(dlci, Duration::from_secs(3)) {
            session.unregister_dlci(dlci);
            return Err(Error::String(e));
        }
        self.virtual_ports
            .lock()
            .map_err(|e| Error::String(format!("Mutex lock failed: {}", e)))?
            .insert(virtual_path.clone(), {
                let tx_queue = Arc::new(PortTxQueue::new());
                crate::state::VirtualPortRef {
                    physical_path,
                    dlci,
                    exchange_cancel: tx_queue.cancel_flag(),
                    tx_queue,
                }
            });
        self.emit_port_state(&virtual_path, PortStatus::Opened);
        Ok(virtual_path)
    }

    /// Tear down CMUX: close virtual channels and detach deframer.
    /// The CLD wait (up to T2) runs without the manager's map lock.
    pub fn disable_mux(&self, path: String) -> Result<(), Error> {
        self.close_virtual_channels_of(&path);
        let cp = self.resolve_connected_port(&path)?;
        let session = crate::sync_util::lock_or_recover(&cp.mux).take();
        if let Some(session) = session {
            session.close_down();
        }
        if let Some(hub) = crate::sync_util::lock_or_recover(&cp.rx_hub).as_ref() {
            hub.detach_cmux();
        }
        Ok(())
    }

    /// The modem left multiplexer mode: drop `session` from `physical` (if it is still
    /// that port's session) and its deframer from the hub.
    fn detach_mux_session(&self, physical: &str, session: &Weak<CmuxSession>) {
        let Ok(cp) = self.resolve_connected_port(physical) else {
            return;
        };
        let mut mux = crate::sync_util::lock_or_recover(&cp.mux);
        let current = mux
            .as_ref()
            .zip(session.upgrade())
            .is_some_and(|(a, b)| Arc::ptr_eq(a, &b));
        if !current {
            return;
        }
        mux.take();
        drop(mux);
        if let Some(hub) = crate::sync_util::lock_or_recover(&cp.rx_hub).as_ref() {
            hub.detach_cmux();
        };
    }

    /// Close every CMUX virtual channel that rides on `physical`.
    fn close_virtual_channels_of(&self, physical: &str) {
        let virtual_paths: Vec<String> = crate::sync_util::lock_or_recover(&self.virtual_ports)
            .iter()
            .filter(|(_, vp)| vp.physical_path == physical)
            .map(|(p, _)| p.clone())
            .collect();
        for vp in virtual_paths {
            let _ = self.close(vp);
        }
    }

    /// Cancel an in-flight exchange and reject queued waiters on `path`.
    pub fn cancel_exchange(&self, path: String) -> Result<(), Error> {
        if let Ok(virtuals) = self.virtual_ports.lock() {
            if let Some(vp) = virtuals.get(&path).cloned() {
                drop(virtuals);
                let session = {
                    let ports = self
                        .serialports
                        .lock()
                        .map_err(|e| Error::String(format!("Mutex lock failed: {}", e)))?;
                    let info = ports.get(&vp.physical_path).ok_or_else(|| {
                        Error::String(format!("Physical port '{}' not found", vp.physical_path))
                    })?;
                    match &info.state {
                        PortState::Connected(cp) => cp
                            .mux
                            .lock()
                            .map_err(|e| Error::String(format!("Mutex lock failed: {}", e)))?
                            .clone(),
                        other => return Err(Error::String(other.not_connected_reason())),
                    }
                };
                if let Some(session) = session {
                    crate::exchange::cancel::cancel_virtual_exchange(
                        &vp.exchange_cancel,
                        &vp.tx_queue,
                        &session,
                        vp.dlci,
                    );
                } else {
                    vp.exchange_cancel.store(true, Ordering::SeqCst);
                    vp.tx_queue.cancel_all();
                }
                return Ok(());
            }
        }
        let cp = self.resolve_connected_port(&path)?;
        crate::exchange::cancel::cancel_physical_exchange(
            &cp.exchange_cancel,
            &cp.tx_queue,
            || {
                if let Ok(guard) = cp.rx_hub.lock() {
                    if let Some(hub) = guard.as_ref() {
                        hub.cancel_active_exchange();
                    }
                }
            },
        );
        Ok(())
    }

    /// Resolve a connected port's shared handles (brief global lock only).
    fn resolve_connected_port(&self, path: &str) -> Result<ConnectedPortHandle, Error> {
        let ports = self
            .serialports
            .lock()
            .map_err(|e| Error::String(format!("Mutex lock failed: {}", e)))?;
        let info = ports.get(path).ok_or_else(|| self.not_open_error(path))?;
        match &info.state {
            PortState::Connected(cp) => Ok(cp.handle()),
            other => Err(Error::String(other.not_connected_reason())),
        }
    }

    /// Run `f` only when the port is [`PortState::Connected`] (holds global lock for `f`).
    fn with_connected_port<T, F>(&self, path: String, f: F) -> Result<T, Error>
    where
        F: FnOnce(&mut ConnectedPort) -> Result<T, Error>,
    {
        let mut ports = self
            .serialports
            .lock()
            .map_err(|e| Error::String(format!("Mutex lock failed: {}", e)))?;

        let info = ports
            .get_mut(&path)
            .ok_or_else(|| self.not_open_error(&path))?;

        match &mut info.state {
            PortState::Connected(cp) => f(cp),
            other => Err(Error::String(other.not_connected_reason())),
        }
    }

    /// The error for a path with no record: the disconnect reason of a port the core
    /// released (4.x: calls on a dead port fail with a disconnect error), else
    /// "Port 'X' not found".
    fn not_open_error(&self, path: &str) -> Error {
        Error::String(
            self.released_reason(path)
                .unwrap_or_else(|| format!("Port '{}' not found", path)),
        )
    }

    fn released_reason(&self, path: &str) -> Option<String> {
        crate::sync_util::lock_or_recover(&self.core.disconnected)
            .get(path)
            .cloned()
    }

    /// Settings, lines, buffers and `set_timeout` go to the write handle when the port
    /// has one (no wait for the hub's read, no timeout override), else to the port.
    fn get_serialport<T, F>(&self, path: String, f: F) -> Result<T, Error>
    where
        F: FnOnce(&mut Box<dyn serialport::SerialPort>) -> Result<T, Error>,
    {
        let cp = self.resolve_connected_port(&path)?;
        let lock = Duration::from_millis(PORT_LOCK_TIMEOUT_MS);
        match self.writer_of(&path, &cp) {
            Some(writer) => lock_with_deadline(&writer, lock, false, f),
            None => with_port_try_lock(&cp.port, lock, f),
        }
    }
}

/// Batch window, read size and URC routing of a watch.
fn watch_params(options: &WatchOptions) -> (u64, usize, bool) {
    let batch_timeout = options
        .serial_data_flush_interval_ms
        .or(options.timeout)
        .unwrap_or(DEFAULT_WATCH_FLUSH_MS);
    (
        batch_timeout,
        options.size.unwrap_or(1024),
        options.route_urc,
    )
}

/// The 4.2 core API: [`CoreEvent`] streams, port-state subscriptions, queue limits.
impl SerialManager {
    /// Stream one port's events into `sink` as [`CoreEvent::Event`] (the final
    /// `Disconnect` included). Any number of these may run on a port, beside the old
    /// [`Self::watch`]: each gets its own copy of the stream and its own `parser` /
    /// `route_urc` state, and URCs caught during `at()` / `exchange` reach every one
    /// with `route_urc`. See [`CoreSink`] for threading, the queue and overflow.
    pub fn watch_core(
        &self,
        path: String,
        options: WatchOptions,
        parser: Option<serial_codec::framing::FramerConfig>,
        sink: CoreSink,
    ) -> Result<SubscriptionId, CoreError> {
        let framer = parser
            .as_ref()
            .map(|p| p.build())
            .transpose()
            .map_err(|e| CoreError::new(ErrorKind::Other, Error::String(e.to_string())))?;
        let (batch_timeout, read_size, route_urc) = watch_params(&options);
        let id = next_subscription_id();
        let queue = CoreQueue::spawn(sink, self.core_queue_capacity());
        if let Some(reason) = self.released_reason(&path) {
            queue.push(CoreEvent::Event(SerialEvent::Disconnect { path, reason }));
            queue.close();
            return Ok(SubscriptionId(id));
        }
        let virtual_port = crate::sync_util::lock_or_recover(&self.virtual_ports)
            .get(&path)
            .cloned();
        if let Some(vp) = virtual_port {
            let cp = self.resolve_connected_port(&vp.physical_path)?;
            let session = crate::sync_util::lock_or_recover(&cp.mux)
                .clone()
                .ok_or_else(|| Error::String("CMUX not enabled".into()))?;
            self.ensure_rx_hub(&cp, &vp.physical_path)?;
            if !session.add_core_watch(
                vp.dlci,
                id,
                queue.event_sink(),
                queue.alive_flag(),
                batch_timeout,
                framer,
            ) {
                return Err(Error::String(format!("Port '{}' not found", path)).into());
            }
        } else {
            let cp = self.resolve_connected_port(&path)?;
            self.ensure_rx_hub(&cp, &path)?;
            let shared = crate::sync_util::lock_or_recover(&cp.rx_hub)
                .as_ref()
                .map(|h| h.shared())
                .ok_or_else(|| Error::String("RX hub missing after start".into()))?;
            shared.attach_core_watch(crate::hub::shared::CoreWatchSlot::new(
                id,
                queue.event_sink(),
                queue.alive_flag(),
                batch_timeout,
                read_size,
                route_urc,
                framer,
            ));
        }
        crate::sync_util::lock_or_recover(&self.core.core_watches).insert(id, path);
        Ok(SubscriptionId(id))
    }

    /// Stop a [`Self::watch_core`] stream; its batch and parser remainder arrive last.
    /// `NotFound` also for a stream that already ended (overflow, sink returned
    /// `false`, port gone).
    pub fn unwatch_core(&self, id: SubscriptionId) -> Result<(), CoreError> {
        let not_found = || {
            CoreError::new(
                ErrorKind::NotFound,
                Error::new(format!("No active watch for id {}", id.0)),
            )
        };
        let path = crate::sync_util::lock_or_recover(&self.core.core_watches)
            .remove(&id.0)
            .ok_or_else(not_found)?;
        let virtual_port = crate::sync_util::lock_or_recover(&self.virtual_ports)
            .get(&path)
            .cloned();
        let detached = if let Some(vp) = virtual_port {
            self.resolve_connected_port(&vp.physical_path)
                .ok()
                .and_then(|cp| crate::sync_util::lock_or_recover(&cp.mux).clone())
                .is_some_and(|session| session.remove_core_watch(vp.dlci, id.0))
        } else {
            self.resolve_connected_port(&path)
                .ok()
                .and_then(|cp| {
                    crate::sync_util::lock_or_recover(&cp.rx_hub)
                        .as_ref()
                        .map(|h| h.shared())
                })
                .is_some_and(|shared| shared.detach_core_watch(id.0))
        };
        if detached {
            Ok(())
        } else {
            Err(not_found())
        }
    }

    /// Port states of every port of this manager ([`CoreEvent::PortState`]: opened,
    /// disconnected with its kind and reason, closed); needs no watch. Same queue and
    /// overflow rules as [`Self::watch_core`].
    pub fn subscribe_core(&self, sink: CoreSink) -> SubscriptionId {
        let id = next_subscription_id();
        let queue = CoreQueue::spawn(sink, self.core_queue_capacity());
        crate::sync_util::lock_or_recover(&self.core.subscribers).push((id, queue));
        SubscriptionId(id)
    }

    pub fn unsubscribe_core(&self, id: SubscriptionId) -> Result<(), CoreError> {
        let mut subs = crate::sync_util::lock_or_recover(&self.core.subscribers);
        let i = subs.iter().position(|(s, _)| *s == id.0).ok_or_else(|| {
            CoreError::new(
                ErrorKind::NotFound,
                Error::new(format!("No subscription {}", id.0)),
            )
        })?;
        subs.remove(i).1.close();
        Ok(())
    }

    /// Queue limit (bytes of event data) of core receivers created from now on.
    /// Default [`crate::events::DEFAULT_CORE_QUEUE_BYTES`].
    pub fn set_core_queue_capacity(&self, bytes: usize) {
        self.core.queue_capacity.store(bytes, Ordering::SeqCst);
    }

    fn core_queue_capacity(&self) -> usize {
        match self.core.queue_capacity.load(Ordering::SeqCst) {
            0 => crate::events::DEFAULT_CORE_QUEUE_BYTES,
            n => n,
        }
    }

    fn emit_port_state(&self, path: &str, state: PortStatus) {
        let subs: Vec<Arc<CoreQueue>> = crate::sync_util::lock_or_recover(&self.core.subscribers)
            .iter()
            .map(|(_, q)| q.clone())
            .collect();
        for q in subs {
            q.push(CoreEvent::PortState {
                path: path.to_string(),
                state: state.clone(),
            });
        }
        crate::sync_util::lock_or_recover(&self.core.subscribers).retain(|(_, q)| q.alive());
    }

    fn downgrade(&self) -> WeakManager {
        WeakManager {
            serialports: Arc::downgrade(&self.serialports),
            virtual_ports: Arc::downgrade(&self.virtual_ports),
            core: Arc::downgrade(&self.core),
        }
    }

    // --- old watch registry (per manager; mirrored into `port::watch_registry`)

    fn register_watch(&self, id: u32, path: &str) -> Result<(), Error> {
        let mut watches = crate::sync_util::lock_or_recover(&self.core.watches);
        if watches.contains_key(&id) {
            return Err(Error::new(format!("watch id {} is already in use", id)));
        }
        if watches.values().any(|p| p == path) {
            return Err(Error::new(format!(
                "A watch is already active for port {}",
                path
            )));
        }
        // The 4.x global registry is shared by every manager: an id another manager
        // holds is taken too (else one manager's watch would vanish from it).
        if !crate::port::watch_registry::mirror_insert(id, path) {
            return Err(Error::new(format!("watch id {} is already in use", id)));
        }
        watches.insert(id, path.to_string());
        Ok(())
    }

    fn unregister_watch(&self, id: u32) -> Option<String> {
        let path = crate::sync_util::lock_or_recover(&self.core.watches).remove(&id);
        if path.is_some() {
            crate::port::watch_registry::mirror_remove(id);
        }
        path
    }

    fn watch_ids_for(&self, path: &str) -> Vec<u32> {
        crate::sync_util::lock_or_recover(&self.core.watches)
            .iter()
            .filter(|(_, p)| p.as_str() == path)
            .map(|(id, _)| *id)
            .collect()
    }

    fn core_watch_ids_for(&self, path: &str) -> Vec<u64> {
        crate::sync_util::lock_or_recover(&self.core.core_watches)
            .iter()
            .filter(|(_, p)| p.as_str() == path)
            .map(|(id, _)| *id)
            .collect()
    }

    /// Drop the registry entries of `path` (the port is gone; its hub ended the streams).
    fn forget_watches(&self, path: &str) {
        for id in self.watch_ids_for(path) {
            self.unregister_watch(id);
        }
        crate::sync_util::lock_or_recover(&self.core.core_watches).retain(|_, p| p != path);
    }

    // --- port handles and hub

    /// Wrap a freshly opened port: write handle (where it can be split) and RX hub.
    fn connect(
        &self,
        path: &str,
        port: Box<dyn SerialPortTrait>,
        timeout: Duration,
        first_watch: Option<crate::hub::shared::CoreWatchSlot>,
    ) -> (ConnectedPort, Result<(), Error>) {
        let writer = split_writer(path, port.as_ref(), timeout);
        let cp = ConnectedPort::new(port);
        if let Some(writer) = writer {
            crate::sync_util::lock_or_recover(&self.core.writers).insert(
                path.to_string(),
                Writer {
                    reader: Arc::downgrade(&cp.port),
                    port: Arc::new(Mutex::new(writer)),
                },
            );
        }
        let hub = self.start_rx_hub(&cp.handle(), path, first_watch);
        (cp, hub)
    }

    /// The write handle of `path`, if it was split from this very port.
    fn writer_of(&self, path: &str, cp: &ConnectedPortHandle) -> Option<PortArc> {
        crate::sync_util::lock_or_recover(&self.core.writers)
            .get(path)
            .filter(|w| {
                w.reader
                    .upgrade()
                    .is_some_and(|r| Arc::ptr_eq(&r, &cp.port))
            })
            .map(|w| w.port.clone())
    }

    fn drop_writer(&self, path: &str) {
        crate::sync_util::lock_or_recover(&self.core.writers).remove(path);
    }

    /// Start the RX hub if it is not running (it is restarted after an intentional
    /// stop; after a disconnect the port is gone, so it never restarts on a dead fd).
    fn ensure_rx_hub(&self, cp: &ConnectedPortHandle, path: &str) -> Result<(), Error> {
        self.start_rx_hub(cp, path, None)
    }

    /// [`Self::ensure_rx_hub`]; `first_watch` joins the hub before its first read.
    fn start_rx_hub(
        &self,
        cp: &ConnectedPortHandle,
        path: &str,
        first_watch: Option<crate::hub::shared::CoreWatchSlot>,
    ) -> Result<(), Error> {
        let mut guard = cp
            .rx_hub
            .lock()
            .map_err(|e| Error::String(format!("Mutex lock failed: {}", e)))?;
        let needs_start = match guard.as_ref() {
            None => true,
            Some(hub) => hub.is_finished(),
        };
        if needs_start {
            let manager = self.downgrade();
            let reader = Arc::downgrade(&cp.port);
            let owner = path.to_string();
            let hook: crate::hub::desktop::DisconnectHook = Box::new(move |reason| {
                if let Some(m) = manager.upgrade() {
                    m.on_port_disconnected(&owner, &reader, reason);
                }
            });
            let hub = Arc::new(crate::hub::PortRxHub::start_with(
                cp.port.clone(),
                path.to_string(),
                Some(hook),
                first_watch,
            ));
            register_mobile_hub(path, cp, hub.clone());
            *guard = Some(hub);
        } else if let (Some(slot), Some(hub)) = (first_watch, guard.as_ref()) {
            hub.shared().attach_core_watch(slot);
        }
        Ok(())
    }

    fn ensure_rx_hub_on_physical(&self, physical_path: &str) -> Result<(), Error> {
        let cp = self.resolve_connected_port(physical_path)?;
        self.ensure_rx_hub(&cp, physical_path)
    }

    /// On the hub thread, after `Disconnect` went out: release the record if it is
    /// still this reader's (see [`Self::release`]). Never joins the hub (its thread).
    fn on_port_disconnected(
        &self,
        path: &str,
        reader: &Weak<Mutex<Box<dyn SerialPortTrait>>>,
        reason: &str,
    ) {
        let removed = {
            let mut map = crate::sync_util::lock_or_recover(&self.serialports);
            let same = match map.get(path).map(|i| &i.state) {
                Some(PortState::Connected(cp)) => {
                    reader.upgrade().is_some_and(|r| Arc::ptr_eq(&r, &cp.port))
                }
                _ => false,
            };
            if same {
                // Tombstone before the record leaves the map: a `close` racing the
                // release must find one of the two, never neither ("not open").
                crate::sync_util::lock_or_recover(&self.core.disconnected)
                    .insert(path.to_string(), reason.to_string());
                map.remove(path)
            } else {
                None
            }
        };
        if let Some(info) = removed {
            self.release(path, info, reason);
        }
    }

    /// A port that disconnected (its record already out of the map, its tombstone in):
    /// drop both handles, the hub and its CMUX channels, give each channel a tombstone
    /// with `reason` (listed, a late watch gets `Disconnect`, calls fail with it, `close`
    /// removes it), and send `PortState::Disconnected`. The physical path's tombstone
    /// is not added here. Watches stay registered until `close` / `unwatch`.
    fn release(&self, path: &str, info: SerialportInfo, reason: &str) {
        let channels: Vec<String> = {
            let mut virtuals = crate::sync_util::lock_or_recover(&self.virtual_ports);
            let keys: Vec<String> = virtuals
                .iter()
                .filter(|(_, vp)| vp.physical_path == path)
                .map(|(k, _)| k.clone())
                .collect();
            // Each channel's tombstone goes in under the same lock that removes it
            // (order virtual_ports -> disconnected): a racing `close("p#dlci=N")` finds
            // one of the two, never neither ("not open"). The physical path's tombstone
            // went in with the record's removal; adding it again would revive it after
            // a racing `close`.
            let mut released = crate::sync_util::lock_or_recover(&self.core.disconnected);
            for k in &keys {
                if let Some(vp) = virtuals.remove(k) {
                    released.insert(k.clone(), reason.to_string());
                    vp.tx_queue.cancel_all();
                }
            }
            keys
        };
        self.drop_writer(path);
        if let PortState::Connected(cp) = info.state {
            // Dropping the hub handle detaches its (ending) thread; the reader goes with it.
            crate::sync_util::lock_or_recover(&cp.rx_hub).take();
            #[cfg(target_os = "android")]
            crate::android::registry::global_registry().unregister(path);
        }
        log_info!("{} released after disconnect: {}", path, reason);
        // The I/O error kind is in `reason`; an unplug is `Disconnected` on every
        // platform (Windows reports it as PermissionDenied).
        for p in channels.iter().map(String::as_str).chain([path]) {
            self.emit_port_state(
                p,
                PortStatus::Disconnected {
                    kind: ErrorKind::Disconnected,
                    reason: reason.to_string(),
                },
            );
        }
    }
}

impl crate::at::commands::ExchangeRunner for SerialManager {
    fn run_exchange_unqueued(
        &self,
        path: String,
        payload: Vec<u8>,
        options: ExchangeOptions,
    ) -> Result<crate::at::parse::ExchangeResponse, Error> {
        SerialManager::run_exchange_unqueued(self, path, payload, options)
    }
}

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

    #[test]
    fn wire_time_is_eleven_bits_a_byte() {
        let mut port = MockSerialPort::new();
        port.baud_rate = 9_600;
        assert_eq!(wire_ms(&port, 96), 110);
        assert_eq!(wire_ms(&port, 0), 0);
        port.baud_rate = 0;
        assert_eq!(wire_ms(&port, 1), 11_000, "baud 0 counts as 1");
    }

    #[cfg(not(any(target_os = "android", target_os = "ios")))]
    #[test]
    fn listed_but_not_found_is_busy() {
        assert_eq!(
            open_error_kind(ErrorKind::NotFound, || true),
            ErrorKind::Busy
        );
        assert_eq!(
            open_error_kind(ErrorKind::NotFound, || false),
            ErrorKind::NotFound
        );
        assert_eq!(
            open_error_kind(ErrorKind::Other, || panic!("only asked for NotFound")),
            ErrorKind::Other
        );
    }

    /// `open` hands back the path it opened and the port is managed under it.
    #[cfg(not(target_os = "android"))]
    #[test]
    fn open_returns_the_managed_path() {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let path = format!("tcp://{}", listener.local_addr().unwrap());
        let m = SerialManager::new();
        assert!(
            m.set_stop_bits(path.clone(), StopBits::Two).is_err(),
            "not open yet"
        );
        let opened = m
            .open(path.clone(), 115_200, None, None, None, None, None)
            .unwrap();
        assert_eq!(opened, path);
        assert_eq!(m.managed_ports().unwrap(), vec![path.clone()]);
        m.set_stop_bits(path.clone(), StopBits::Two).unwrap();
        m.close(path).unwrap();
    }
}