ch32rv 0.51.0

Flashing and debugging tool for WCH CH32 RISC-V microcontrollers
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
//! en: `arduino discovery` / `arduino monitor` (docs/cli.ja.md §4.11): the Arduino Pluggable
//! Discovery and Monitor protocols (line-based stdio JSON). Discovery exposes each WCH probe as a
//! `wchlink://<serial>` port; Monitor serves the IDE's Serial Monitor from the probe's CDC (uart /
//! sdi) or the DMI sources (dmdata / dmseq / rtt), both ways (what the user types reaches the
//! sketch). Machine-facing: never mixes human text onto stdout.
//! ja: `arduino discovery`/`monitor`。Arduino の Pluggable Discovery/Monitor プロトコル(行単位の
//! stdio JSON)。discovery は各 WCH probe を `wchlink://<serial>` port として公開、monitor は DMI
//! IDE の Serial Monitor を probe の CDC(uart / sdi)か DMI の source(dmdata / dmseq / rtt)で
//! 双方向に提供する。

use std::io::{BufRead, Write};
use std::process::ExitCode;

use serde_json::{Value, json};

use crate::args::Cli;

/// `arduino discovery`: the Pluggable Discovery protocol over stdio.
pub fn discovery(_cli: &Cli) -> ExitCode {
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::{Arc, Mutex};
    let out = Arc::new(Mutex::new(std::io::stdout()));
    let say = |v: &Value| {
        if let Ok(mut o) = out.lock() {
            emit(&mut *o, v);
        }
    };
    // The sync watcher's stop flag. Not joined: a round may be reading a probe for a few seconds,
    // and the answer to STOP / QUIT must not wait for it; the round checks the flag before it
    // says anything.
    let mut sync: Option<Arc<AtomicBool>> = None;
    let stop_sync = |sync: &mut Option<Arc<AtomicBool>>| {
        if let Some(stop) = sync.take() {
            stop.store(true, Ordering::SeqCst);
        }
    };
    for line in stdin_lines_or_exit() {
        let word = line
            .split_whitespace()
            .next()
            .unwrap_or("")
            .to_ascii_uppercase();
        match word.as_str() {
            "" => {}
            "HELLO" => say(&json!({"eventType":"hello","protocolVersion":1,"message":"OK"})),
            "START" => say(&json!({"eventType":"start","message":"OK"})),
            "STOP" => {
                stop_sync(&mut sync);
                say(&json!({"eventType":"stop","message":"OK"}));
            }
            "LIST" => say(&json!({"eventType":"list","ports": list_ports()})),
            "START_SYNC" => {
                stop_sync(&mut sync);
                say(&json!({"eventType":"start_sync","message":"OK"}));
                let stop = Arc::new(AtomicBool::new(false));
                let (o, s) = (out.clone(), stop.clone());
                std::thread::spawn(move || sync_watch(o, s));
                sync = Some(stop);
            }
            "QUIT" => {
                stop_sync(&mut sync);
                say(&json!({"eventType":"quit","message":"OK"}));
                return ExitCode::SUCCESS;
            }
            other => say(
                &json!({"eventType": other.to_ascii_lowercase(), "error": true, "message": format!("unknown command {other}")}),
            ),
        }
    }
    ExitCode::SUCCESS
}

/// en: How long one command may still run after stdin closed before the process ends anyway.
/// A LIST over many probes takes seconds (6 s on the bench); a stuck one never ends.
/// ja: stdin が閉じた後、1 つのコマンドをどれだけ待ってから終えるか。
/// en: `hid` is the board's own USB, not something a probe's port serves.
/// ja: `hid` は board 自身の USB で、probe の口が出すものではない。
const HID_NOT_A_PROBE: &str =
    "hid is the board's own USB, not a probe's source: use `ch32rv monitor --source hid`";

const ORPHAN_GRACE: std::time::Duration = std::time::Duration::from_secs(30);

/// en: stdin's lines, read on a thread of their own, so that stdin closing (arduino-cli quit or
/// died) is seen even while a command is stuck - a LIST waiting on a probe or on USB. The lines
/// already read are still answered; but when one command is still running [`ORPHAN_GRACE`] after
/// stdin closed, the process ends, so a discovery never outlives its IDE holding a port.
/// ja: stdin の行を別の thread で読む。stdin が閉じても読んだ行には答えるが、閉じた後 1 つの
/// コマンドが 30 秒を超えて終わらなければプロセスを終える(IDE より長く port を持って残らない)。
struct StdinLines {
    rx: std::sync::mpsc::Receiver<String>,
    /// Milliseconds since `epoch` when the command now running started; 0 while waiting.
    busy_since: std::sync::Arc<std::sync::atomic::AtomicU64>,
    epoch: std::time::Instant,
}

impl Iterator for StdinLines {
    type Item = String;
    fn next(&mut self) -> Option<String> {
        use std::sync::atomic::Ordering;
        self.busy_since.store(0, Ordering::Relaxed);
        let line = self.rx.recv().ok()?;
        let now = (self.epoch.elapsed().as_millis() as u64).max(1);
        self.busy_since.store(now, Ordering::Relaxed);
        Some(line)
    }
}

fn stdin_lines_or_exit() -> StdinLines {
    use std::sync::atomic::{AtomicU64, Ordering};
    let (tx, rx) = std::sync::mpsc::channel();
    let busy_since = std::sync::Arc::new(AtomicU64::new(0));
    let epoch = std::time::Instant::now();
    let busy = busy_since.clone();
    std::thread::spawn(move || {
        for line in std::io::stdin().lock().lines() {
            let Ok(line) = line else { break };
            if tx.send(line).is_err() {
                return;
            }
        }
        drop(tx);
        // stdin closed: the lines read are answered, a command stuck past the grace is not.
        let closed = epoch.elapsed();
        loop {
            std::thread::sleep(std::time::Duration::from_millis(200));
            let since = busy.load(Ordering::Relaxed);
            if since == 0 {
                continue;
            }
            let started = std::time::Duration::from_millis(since).max(closed);
            if epoch.elapsed().saturating_sub(started) > ORPHAN_GRACE {
                std::process::exit(0);
            }
        }
    });
    StdinLines {
        rx,
        busy_since,
        epoch,
    }
}

/// Enumerate WCH probes as Pluggable-Discovery ports (from USB descriptors only - no AttachChip,
/// so it never disturbs an in-flight upload/monitor on the same probe).
pub(crate) fn list_ports() -> Vec<Value> {
    // One USB enumeration for the whole listing (it asked for 49, 6-30 s on a bench of ~200
    // devices, past arduino-cli's 10 s for a LIST). ja: 一覧全体で USB の列挙を 1 回に。
    ch32rv_usb::with_one_enumeration(|| {
        let mut ports = wchlink_ports();
        ports.extend(oep_ports());
        ports.extend(hid_ports());
        ports.extend(isp_route_ports());
        ports.push(waiting_port());
        ports
    })
}

/// en: The port that is always listed (docs/freeze-decisions.ja.md §2): uploading through it waits
/// for the board's own USB bootloader - the factory ISP, or the HID bootloader on a board that has
/// one (boards.txt picks the tool) - which the user brings up after pressing Upload. It is there
/// with nothing connected, so the IDE can keep it selected; a probe is never written through it.
/// ja: いつも一覧にある待つポート。押してから BOOT で入れたボードのブートローダーに書く。
fn waiting_port() -> Value {
    json!({
        "address": format!("{}{}", crate::cmd_isp::BOOTLOADER_SCHEME, crate::cmd_isp::WAIT_FOR_BOOT),
        "label": "USB bootloader (waits for BOOT)",
        "protocol": "ch32rv-bootloader",
        "protocolLabel": "CH32RV USB bootloader",
        "properties": {},
    })
}

/// en: OEP probes (`crate::oep::is_oep_device`) as one `oep://<probe>[/<port>]` port per port its
/// labels name. The labels are read lock-free - through the probe's broker when one runs (never started
/// here), else on its serial port opened briefly - and when the probe cannot be read (another tool
/// holds the port), its last listing stands in for a short while. Plain serial ports are never opened.
/// ja: OEP の probe を、ラベルが名指すポートごとに `oep://<probe>[/<port>]` で出す。ラベルは lock 無しで読む
/// (ブローカーが動いていればそこ経由、ここでは起動しない。無ければ serial port を短く開く)。読めなければ
/// 少しの間だけ前回の一覧。普通の serial port は開かない。
/// en: Whether this user may not read and write the serial port `path` (checked without opening
/// it: an open would toggle its modem lines).
/// ja: この利用者が serial port `path` を読み書きできないか(開かずに調べる。開くと modem 線が動く)。
#[cfg(unix)]
fn no_access(path: &str) -> bool {
    use rustix::fs::{Access, access};
    matches!(
        access(path, Access::READ_OK | Access::WRITE_OK),
        Err(e) if e == rustix::io::Errno::ACCESS || e == rustix::io::Errno::PERM
    )
}

#[cfg(not(unix))]
fn no_access(_path: &str) -> bool {
    false
}

fn oep_ports() -> Vec<Value> {
    let mut out = Vec::new();
    let devices = crate::oep::oep_devices();
    let on_usb: Vec<String> = devices.iter().map(crate::oep::probe_id).collect();
    for dev in devices {
        out.extend(oep_device_ports(dev));
    }
    out.extend(net_oep_ports(&on_usb));
    out
}

/// One USB OEP probe's ports (its labels read), or the unit when this user may not open it.
fn oep_device_ports(dev: ch32rv_usb::UsbDeviceInfo) -> Vec<Value> {
    let mut out = Vec::new();
    let Some(path) = crate::oep::oep_port(&dev) else {
        return out;
    };
    let id = crate::oep::probe_id(&dev);
    let denied = no_access(&path);
    let read = if denied {
        None
    } else {
        read_oep_listing_within(dev.clone(), path.clone(), OEP_LISTING_WITHIN)
    };
    let listed = listing_or_recent(read, &id);
    // en: A probe this user may not open (no udev rule) is still shown, as the unit, with the
    // reason in its label: its ports cannot be read, and leaving it out made the probe look
    // absent with nothing saying why. Opening it fails with the exact fix.
    // ja: 開く権限の無い probe(udev のルールが無い)も、unit として理由を label に添えて出す。
    // 口は読めず、出さないと probe が無いように見えて理由も分からなかった。開けば直し方が出る。
    if listed.is_empty() && denied {
        out.push(json!({
            "address": format!("oep://{id}"),
            "label": format!("OEP probe {id} (no permission: udev rules missing)"),
            "protocol": "oep",
            "protocolLabel": "OEP probe",
            "hardwareId": id,
            "properties": {
                "vid": format!("0x{:04x}", dev.vid()),
                "pid": format!("0x{:04x}", dev.pid()),
                "port": path,
                "error": "permission",
                "fix": crate::session::access_denied_fix(),
            },
        }));
    }
    out.extend(listed);
    out
}

/// en: How often sync mode looks at USB again, and how often it browses the network for OEP probes
/// (a browse takes 600 ms).
/// ja: sync mode が USB を見直す間隔と、ネットワークの OEP の probe を探す間隔(1 回 600 ms)。
const SYNC_EVERY: std::time::Duration = std::time::Duration::from_secs(1);
const SYNC_NET_EVERY: std::time::Duration = std::time::Duration::from_secs(15);

/// en: Sync mode's view of the ports. USB is enumerated every round (it opens nothing); a USB OEP
/// probe's labels are read once when it appears (a read opens its serial port) and kept while it
/// stays; the network is browsed now and then.
/// ja: sync mode の一覧。USB は毎回列挙(何も開かない)。USB の OEP の probe のラベルは現れたときに
/// 1 回読み(serial port を開くので)、居る間は覚えておく。ネットワークは時々探す。
#[derive(Default)]
struct SyncView {
    oep: std::collections::HashMap<String, Vec<Value>>,
    net: Vec<Value>,
    net_at: Option<std::time::Instant>,
}

/// en: The ports that need nothing opened (WCH-Links, HID / ISP bootloaders, CH32RV applications,
/// the waiting port): sync mode says these first, so that a port is known within arduino-cli's 1 s
/// wait at an upload - the whole first round (OEP labels read) took 6 s on the bench, and an
/// upload to a port not yet known goes out with the protocol `serial`.
/// ja: 何も開かずに分かるポート。sync mode はこれを先に出す(書き込みの時の arduino-cli の待ちは
/// 1 秒。OEP のラベルを読む最初の 1 周は台で 6 秒かかり、まだ知らない port への書き込みは
/// protocol `serial` で出ていった)。
fn quick_ports() -> Vec<Value> {
    ch32rv_usb::with_one_enumeration(|| {
        let mut ports = wchlink_ports();
        ports.extend(hid_ports());
        ports.extend(isp_route_ports());
        ports.push(waiting_port());
        ports
    })
}

impl SyncView {
    fn ports(&mut self) -> Vec<Value> {
        ch32rv_usb::with_one_enumeration(|| {
            let mut ports = wchlink_ports();
            let devices = crate::oep::oep_devices();
            let on_usb: Vec<String> = devices.iter().map(crate::oep::probe_id).collect();
            self.oep.retain(|id, _| on_usb.contains(id));
            for dev in devices {
                let id = crate::oep::probe_id(&dev);
                if !self.oep.contains_key(&id) {
                    let listed = oep_device_ports(dev);
                    self.oep.insert(id.clone(), listed);
                }
                ports.extend(self.oep[&id].iter().cloned());
            }
            if self.net_at.is_none_or(|t| t.elapsed() >= SYNC_NET_EVERY) {
                self.net = net_oep_ports(&on_usb);
                self.net_at = Some(std::time::Instant::now());
            }
            ports.extend(self.net.iter().cloned());
            ports.extend(hid_ports());
            ports.extend(isp_route_ports());
            ports.push(waiting_port());
            ports
        })
    }
}

/// en: The events that take the IDE from `known` to `now`: `remove` for a port gone or changed,
/// then `add` for a port new or changed. ja: `known` から `now` への event。
fn sync_events(known: &[Value], now: &[Value]) -> Vec<Value> {
    let same =
        |a: &Value, b: &Value| a["address"] == b["address"] && a["protocol"] == b["protocol"];
    let mut events = Vec::new();
    for old in known {
        match now.iter().find(|n| same(n, old)) {
            Some(n) if n == old => {}
            _ => events.push(json!({"eventType": "remove", "port": {
                "address": old["address"], "protocol": old["protocol"],
            }})),
        }
    }
    for new in now {
        if !known.iter().any(|o| o == new) {
            events.push(json!({"eventType": "add", "port": new}));
        }
    }
    events
}

/// en: Sync mode (START_SYNC): the ports now as `add` events, then every [`SYNC_EVERY`] the ports
/// that came (`add`) and went (`remove`; a port whose description changed is removed and added
/// again), until `stop` is set. arduino-cli and IDE 2 do not LIST again in sync mode: before this,
/// a device that appeared after the start (a bootloader brought up with BOOT, a probe plugged in)
/// never showed.
/// ja: sync mode。今のポートを `add` で出し、その後 [`SYNC_EVERY`] ごとに、来たもの(`add`)と
/// 消えたもの(`remove`。説明が変わったものは remove して add)を知らせる。arduino-cli と IDE 2 は
/// sync mode では LIST し直さないので、以前は開始の後に現れた device(BOOT で出した bootloader、
/// 挿した probe)が出なかった。
fn sync_watch(
    out: std::sync::Arc<std::sync::Mutex<std::io::Stdout>>,
    stop: std::sync::Arc<std::sync::atomic::AtomicBool>,
) {
    use std::sync::atomic::Ordering;
    let mut view = SyncView::default();
    let mut known: Vec<Value> = Vec::new();
    // The quick ones first; the next round adds the OEP probes' ports.
    let first = quick_ports();
    if !stop.load(Ordering::SeqCst) {
        let Ok(mut o) = out.lock() else { return };
        for e in sync_events(&known, &first) {
            emit(&mut *o, &e);
        }
        drop(o);
        known = first;
    }
    loop {
        let now = view.ports();
        if stop.load(Ordering::SeqCst) {
            return;
        }
        let events = sync_events(&known, &now);
        if !events.is_empty() {
            let Ok(mut o) = out.lock() else { return };
            for e in &events {
                emit(&mut *o, e);
            }
        }
        known = now;
        let until = std::time::Instant::now() + SYNC_EVERY;
        while std::time::Instant::now() < until {
            if stop.load(Ordering::SeqCst) {
                return;
            }
            std::thread::sleep(std::time::Duration::from_millis(100));
        }
    }
}

/// en: How long one USB OEP probe may take to give its labels in a LIST (one takes well under 1 s on the
/// bench). A probe whose USB stalls can hold the open of its serial port in the kernel for about
/// a minute (an ESP32-P4 probe on the bench, 2026-11-20: 55 s), past arduino-cli's 10 s for
/// the whole LIST.
/// ja: LIST で USB の OEP の probe 1 台がラベルを返すまでの時間(台では 1 台 1 秒未満)。USB が
/// 止まった probe は serial port の open をカーネルの中で 1 分近く止める(台の ESP32-P4 の probe、
/// 2026-11-20、55 秒)。
const OEP_LISTING_WITHIN: std::time::Duration = std::time::Duration::from_secs(3);

/// en: [`read_oep_listing`] on a thread of its own: a probe that has not answered `within` counts
/// as unreadable (its recent listing stands in), and its thread is left to finish or stay stuck on
/// its own. The thread uses this one's enumeration ([`ch32rv_usb::with_one_enumeration`]).
/// ja: [`read_oep_listing`] を別の thread で。`within` までに返らなければ読めないものとし(前回の
/// 一覧で代える)、その thread は待たずに放っておく。
fn read_oep_listing_within(
    dev: ch32rv_usb::UsbDeviceInfo,
    path: String,
    within: std::time::Duration,
) -> Option<Vec<Value>> {
    let snapshot = ch32rv_usb::current_enumeration();
    let (tx, rx) = std::sync::mpsc::channel();
    std::thread::spawn(move || {
        let read = || read_oep_listing(&dev, &path);
        let _ = tx.send(match snapshot {
            Some(devs) => ch32rv_usb::with_enumeration(devs, read),
            None => read(),
        });
    });
    rx.recv_timeout(within).ok().flatten()
}

/// How long a probe's last listing stands in for it while it cannot be read (another tool holds
/// it): enough for a short tussle such as one upload, not for a probe that changed meanwhile.
const LISTING_LIFE: std::time::Duration = std::time::Duration::from_secs(30);

/// en: The probe's ports: `read` when it could be read (kept as its last listing, by unit id),
/// else that last listing while it is younger than [`LISTING_LIFE`], only its entries that name
/// a port of this very probe, or the probe itself, in today's form (`oep://<unit>[/<port>]`).
/// ja: 読めればそれ(unit id ごとに前回の一覧として残す)。読めなければ 30 秒以内の前回の一覧を、
/// 同じ probe の今の形のアドレスの分だけ。
fn listing_or_recent(read: Option<Vec<Value>>, unit: &str) -> Vec<Value> {
    let cache = crate::broker::listing_cache(&format!("oep-{}", unit.to_ascii_lowercase()));
    if let Some(ports) = read {
        let _ = ch32rv_usb::create_runtime_dir();
        let _ = std::fs::write(&cache, Value::Array(ports.clone()).to_string());
        return ports;
    }
    let fresh = std::fs::metadata(&cache)
        .and_then(|m| m.modified())
        .ok()
        .and_then(|t| t.elapsed().ok())
        .is_some_and(|age| age < LISTING_LIFE);
    let Some(Value::Array(ports)) = fresh
        .then(|| std::fs::read_to_string(&cache).ok())
        .flatten()
        .and_then(|t| serde_json::from_str(&t).ok())
    else {
        return Vec::new();
    };
    let whole = format!("oep://{unit}");
    let prefix = format!("{whole}/");
    ports
        .into_iter()
        .filter(|p| {
            p["address"].as_str().is_some_and(|a| {
                a.eq_ignore_ascii_case(&whole)
                    || a.get(..prefix.len())
                        .is_some_and(|h| h.eq_ignore_ascii_case(&prefix))
                        && port_name_ok(&a[prefix.len()..])
            })
        })
        .collect()
}

/// A port name as an address carries it: the `S` of an `S.N` label.
fn port_name_ok(name: &str) -> bool {
    ch32rv_oep::config::port_line(&format!("{name}.x")).is_some()
}

fn read_oep_listing(dev: &ch32rv_usb::UsbDeviceInfo, path: &str) -> Option<Vec<Value>> {
    // A LIST must stay quick: a probe that does not answer (older firmware, another framing) costs
    // two short waits, not the usual reply timeout twice.
    let mut p = crate::oep::probe_for_listing(dev)?;
    let props = json!({
        "vid": format!("0x{:04x}", dev.vid()),
        "pid": format!("0x{:04x}", dev.pid()),
        "port": path,
    });
    port_entries(&mut p, &crate::oep::probe_id(dev), &props)
}

/// en: OEP probes on the local network that are not on USB (DNS-SD `_oep._tcp`, oep-spec
/// transports §3), listed like the USB ones: one `oep://<unit_id>[/<port>]` per port, read over TCP
/// (lock-free) after describe confirmed the unit id; its recent listing for a short while when it cannot be read.
/// ja: USB に無い、ネットワークの OEP の probe(DNS-SD)。USB のものと同じくポートごとに出す。
fn net_oep_ports(on_usb: &[String]) -> Vec<Value> {
    let mut out = Vec::new();
    for f in crate::mdns::browse(std::time::Duration::from_millis(600)) {
        let (Some(unit), Some(addr)) = (f.unit_id.clone(), f.endpoint()) else {
            continue;
        };
        if on_usb.iter().any(|u| u.eq_ignore_ascii_case(&unit)) {
            continue;
        }
        // Through its broker when one runs (never started here), else a brief direct look.
        let target = crate::broker::BrokerTarget::Net {
            addr: addr.clone(),
            unit: unit.clone(),
            via: None,
        };
        let opened = match crate::broker::existing_link_for(&target) {
            Some(l) => ch32rv_oep::session::Probe::connect(l).map_err(|e| e.to_string()),
            None => crate::oep::open_net(&addr, &unit),
        };
        let read = opened.ok().and_then(|mut p| {
            p.link().set_timeout(std::time::Duration::from_millis(500));
            // Not on USB: `oep://` without `via` too (host guide §5.3: one line per place; the
            // address reaches it over the network whatever its IP).
            port_entries(
                &mut p,
                &unit,
                &json!({"tcp": addr, "instance": f.instance, "topology": "network"}),
            )
        });
        out.extend(listing_or_recent(read, &unit));
    }
    out
}

/// en: The probe's ports as discovery ports (host guide §5.3): one `oep://<id>/<port>` per port
/// its labels name (the `S` of the `S.N` labels, as written); a probe naming none is listed as
/// itself, `oep://<id>` (its pins then come from `--pins`). `props` added to each one's
/// properties. Read lock-free, without attaching: the target is not known until a tool attaches.
/// ja: probe の口を discovery の port として出す。ラベルが名指す口ごとに 1 つ。名前つきの口が
/// 無い probe は `oep://<id>` で出す。attach しないので target は分からない。
fn port_entries(p: &mut ch32rv_oep::session::Probe, id: &str, props: &Value) -> Option<Vec<Value>> {
    let labels = ch32rv_oep::config::labels(p).ok()?;
    let ports = ch32rv_oep::config::ports_named(&labels);
    let entry = |port: Option<&str>| {
        let mut properties = json!({ "port_name": port });
        if let (Some(m), Some(extra)) = (properties.as_object_mut(), props.as_object()) {
            for (k, v) in extra {
                m.insert(k.clone(), v.clone());
            }
        }
        let (address, label, hardware) = match port {
            Some(port) => (
                format!("oep://{id}/{port}"),
                format!("{port} on OEP probe {id}"),
                format!("{id}/{port}"),
            ),
            None => (
                format!("oep://{id}"),
                format!("OEP probe {id}"),
                id.to_owned(),
            ),
        };
        json!({
            "address": address,
            "label": label,
            "protocol": "oep",
            "protocolLabel": "OEP probe",
            "hardwareId": hardware,
            "properties": properties,
        })
    };
    if ports.is_empty() {
        return Some(vec![entry(None)]);
    }
    Some(ports.iter().map(|s| entry(Some(s))).collect())
}

/// WCH-Links as `wchlink://<serial>`, or `wchlink://usb-<bus>-<ports>` for one without a serial.
fn wchlink_ports() -> Vec<Value> {
    let entries = crate::cmd_probe::wch_devices().unwrap_or_default();
    entries
        .iter()
        // A Link with neither a serial nor a stable position has no address that would still name
        // it after a replug; it is left out (select it with --probe).
        .filter_map(|e| e.dev.port_id().map(|id| (e, id)))
        .map(|(e, id)| {
            // en: Still listed when its driver keeps ch32rv out (the user sees it and why), with
            // the driver named in the label and the properties.
            // ja: driver のために開けない Link も出す(見えて、理由が分かるように)。label と properties に
            // driver の名前。
            let driver = e.dev.foreign_driver();
            let label = match &driver {
                Some(d) => format!("WCH-Link {id} (cannot open: driver {d})"),
                None => format!("WCH-Link {id}"),
            };
            let mut v = json!({
                "address": format!("wchlink://{id}"),
                "label": label,
                "protocol": "wchlink",
                "protocolLabel": "WCH-Link (RISC-V debug)",
                "hardwareId": id,
                "properties": {
                    "serial": e.dev.serial(),
                    "vid": format!("0x{:04x}", e.dev.vid()),
                    "pid": format!("0x{:04x}", e.dev.pid()),
                    "mode": crate::cmd_probe::mode_str(e.mode),
                },
            });
            if let Some(d) = driver {
                v["properties"]["driver"] = json!(d);
            }
            v
        })
        .collect()
}

/// en: The board's own HID port, by USB position: `ch32rv-hid://<topology>`, protocol
/// `ch32rv-hid`. While the sketch runs it is the application's HID serial (usage page 0xFF32 /
/// usage 0x52; label `HID serial <topology>`), while the board is in its bootloader the rv003usb /
/// UIAPduino HID bootloader (`1209:b803`, `1209:b003`, or one known by its report descriptor;
/// label `HID bootloader <topology>`). Both come up at the same position, so the port the IDE
/// selected stays while an upload swaps the one for the other. `properties.mode` is `app` or
/// `bootloader`; `vid` / `pid` are the device's now (boards.txt's `upload_port.N.vid/pid` name a
/// board by them). A bootloader is listed from the USB device list too (no hidraw access needed).
/// ja: board 自身の HID の port を位置で `ch32rv-hid://<topology>` に出す。sketch の間はアプリの
/// HID シリアル、bootloader の間は HID bootloader(VID:PID の表か報告記述子で)。同じ位置なので、
/// 書き込みで入れ替わっても IDE の選んだ port は残る。`mode` は `app` / `bootloader`。
fn hid_ports() -> Vec<Value> {
    use crate::hid_serial::Kind;
    let port = |topology: &str, kind: Kind, vid: u16, pid: u16| {
        let (label, mode) = match kind {
            Kind::App => (format!("HID serial {topology}"), "app"),
            Kind::Bootloader => (format!("HID bootloader {topology}"), "bootloader"),
        };
        json!({
            "address": format!("{}{topology}", crate::cmd_boot::HID_SCHEME),
            "label": label,
            "protocol": "ch32rv-hid",
            "protocolLabel": "USB HID (HID serial / rv003usb bootloader)",
            "hardwareId": topology,
            "properties": {
                "vid": format!("0x{vid:04x}"),
                "pid": format!("0x{pid:04x}"),
                "topology": topology,
                "mode": mode,
            },
        })
    };
    let mut seen: Vec<String> = Vec::new();
    let mut out = Vec::new();
    for d in ch32rv_usb::enumerate().unwrap_or_default() {
        if ch32rv_boot::DEFAULT_HID_IDS.contains(&(d.vid(), d.pid())) {
            let t = d.topology();
            out.push(port(&t, Kind::Bootloader, d.vid(), d.pid()));
            seen.push(t);
        }
    }
    for f in crate::hid_serial::list() {
        let Some(t) = f.topology else { continue };
        if !seen.contains(&t) {
            out.push(port(&t, f.kind, f.vid, f.pid));
            seen.push(t);
        }
    }
    out
}

/// en: The factory ISP route's ports. A running CH32RV application (a DFU run-time interface named
/// `CH32RV`, ArduinoCore-CH32RV's default) as `ch32rv-app://<topology>`, also when it has a CDC
/// port - a serial port alone does not say the device is a CH32RV, so the application port is
/// the one to upload through, and its monitor opens that CDC port. A factory ISP bootloader
/// (`1a86:55e0`) as `ch32rv-isp://<topology>`, while it is up (it returns to the application
/// after ~10 s unattended). `4348:55e0` is not listed: a WCH-Link in IAP mode shares it. Nothing
/// is opened. ja: factory ISP 経路の port。CH32RV のアプリ(名前 `CH32RV` の DFU run-time)を
/// `ch32rv-app://`(CDC があっても。serial port だけでは CH32RV と分からない)、ISP bootloader
/// (`1a86:55e0`)を `ch32rv-isp://` で出す。`4348:55e0` は LinkE の IAP と同じなので出さない。
fn isp_route_ports() -> Vec<Value> {
    let devs = ch32rv_usb::enumerate().unwrap_or_default();
    let props = |d: &ch32rv_usb::UsbDeviceInfo| {
        json!({
            "vid": format!("0x{:04x}", d.vid()),
            "pid": format!("0x{:04x}", d.pid()),
            "serialNumber": d.serial().unwrap_or(""),
            "topology": d.topology(),
        })
    };
    let apps: Vec<ch32rv_usb::UsbDeviceInfo> = ch32rv_usb::enumerate()
        .unwrap_or_default()
        .into_iter()
        .filter(crate::cmd_isp::is_ch32rv_app)
        .collect();
    let mut ports = Vec::new();
    for d in &devs {
        let topology = d.topology();
        if crate::cmd_isp::is_ch32rv_app(d) {
            let id = crate::cmd_isp::app_id(d, &apps);
            ports.push(json!({
                "address": format!("{}{id}", crate::cmd_isp::APP_SCHEME),
                "label": app_label(d.product(), &id, d.serial_ports().first().map(String::as_str)),
                "protocol": "ch32rv-app",
                "protocolLabel": "CH32RV application (USB)",
                "hardwareId": d.serial().filter(|s| !s.is_empty()).map_or_else(|| topology.clone(), str::to_owned),
                "properties": props(d),
            }));
        } else if (d.vid(), d.pid()) == (0x1a86, 0x55e0) {
            ports.push(json!({
                "address": format!("{}{topology}", crate::cmd_isp::ISP_SCHEME),
                "label": format!("WCH ISP bootloader (USB {})", short_position(&topology)),
                "protocol": "ch32rv-isp",
                "protocolLabel": "WCH factory ISP bootloader (USB)",
                "hardwareId": topology,
                "properties": props(d),
            }));
        }
    }
    ports
}

/// en: A CH32RV application's label in the IDE's port menu: its product name, its id and its serial
/// port, so that two boards running the same sketch (one product name) are told apart there; the
/// menu shows only the label. ja: IDE のポートのメニューに出るアプリの表示名。製品名・id・serial port
/// (同じスケッチの板が 2 台あっても見分けられるように。メニューは label しか出さない)。
fn app_label(product: Option<&str>, id: &str, port: Option<&str>) -> String {
    let name = product.unwrap_or("CH32RV application");
    match port {
        Some(p) => {
            // `/dev/ttyACM5` as `ttyACM5`; `COM6` as it is.
            let short = p.rsplit('/').next().unwrap_or(p);
            format!("{name} {id} ({short})")
        }
        None => format!("{name} {id}"),
    }
}

/// en: The tail of a USB position for a label: `1.3.2` of `PCIROOT(0)#PCI(1400)#USBROOT(0)-1.3.2`
/// (Windows) or of `3-1.3.2`. ja: 表示名のための USB の位置の末尾。
fn short_position(topology: &str) -> &str {
    topology.rsplit('-').next().unwrap_or(topology)
}

fn emit(out: &mut impl Write, v: &Value) {
    // One compact JSON object per line; flush so the IDE sees it immediately.
    let _ = writeln!(out, "{v}");
    let _ = out.flush();
}

// ---- arduino monitor (Pluggable Monitor protocol) ----

/// en: `CH32RV_MONITOR_LOG=<file>`: the monitor appends what it is told, what it answers and what
/// its session does, one timestamped line each - for "which command went unanswered" when an IDE
/// times out (arduino-cli shows a tool's stderr to nobody).
/// ja: `CH32RV_MONITOR_LOG=<file>` に、受けた命令・返事・session の出来事を時刻付きで追記する。
pub(crate) fn mlog(msg: std::fmt::Arguments<'_>) {
    static LOG: std::sync::OnceLock<Option<Mutex<std::fs::File>>> = std::sync::OnceLock::new();
    let log = LOG.get_or_init(|| {
        let path = std::env::var_os("CH32RV_MONITOR_LOG")?;
        std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(path)
            .ok()
            .map(Mutex::new)
    });
    if let Some(f) = log {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default();
        let mut f = match f.lock() {
            Ok(g) => g,
            Err(p) => p.into_inner(),
        };
        let _ = writeln!(
            f,
            "{}.{:03} [{}] {msg}",
            now.as_secs(),
            now.subsec_millis(),
            std::process::id()
        );
    }
}

/// [`emit`] for the monitor: logged too.
fn memit(out: &mut impl Write, v: &Value) {
    mlog(format_args!("> {v}"));
    emit(out, v);
}

use std::net::{Shutdown, TcpStream};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::Receiver;
use std::sync::{Arc, Condvar, Mutex};
use std::thread::JoinHandle;
use std::time::Duration;

use ch32rv_contract::policy::MonitorSource;
use serialport::SerialPort;

use crate::cmd_probe::Entry;
use crate::session::Session;
use crate::source::{self, DmiSource};

/// en: The sources a serial port offers, in the order the IDE lists them; the first (`uart`,
/// the port as it is) is the default. A plain serial port (no WCH probe behind it) can only do
/// `uart`, which is what the IDE would do anyway.
/// ja: serial port の source。先頭(`uart` = port をそのまま)が既定。WCH probe の無い素の serial
/// port は `uart` だけ。
const SERIAL_SOURCES: [MonitorSource; 7] = [
    MonitorSource::Auto,
    MonitorSource::Uart,
    MonitorSource::Sdi,
    MonitorSource::Dmdata,
    MonitorSource::Dmseq,
    MonitorSource::Rtt,
    MonitorSource::FixtureUart,
];

/// en: The sources an `oep://<probe>[/<port>]` port offers: the target's console (dmseq, the default,
/// dmdata, sdi), the probe's fixture UART, and RTT (run by ch32rv over the probe's riscv-dm).
/// ja: `oep://` の port の source。target の console(dmseq 既定 / dmdata / sdi)、fixture の UART、RTT
/// (ch32rv が probe の riscv-dm の上で行う)。
/// The board's own HID port (`ch32rv-hid://`): its HID serial only.
const HID_SOURCES: [MonitorSource; 1] = [MonitorSource::Hid];
const OEP_SOURCES: [MonitorSource; 6] = [
    MonitorSource::Auto,
    MonitorSource::Dmseq,
    MonitorSource::Dmdata,
    MonitorSource::Sdi,
    MonitorSource::FixtureUart,
    MonitorSource::Rtt,
];

/// en: The sources a `wchlink://` port offers. `uart` (the default) is the Link's CDC, found as
/// `probe list`'s `ports` finds it, opened as a serial port's `uart` is (no attach, no probe lock):
/// what the IDE picks for a `wchlink://` upload port then shows the sketch's `Serial` without
/// touching the target (an attach re-clocks it, and breaks a CH32V205's s0).
/// ja: `wchlink://` の source。既定の `uart` は Link の CDC(`probe list` の `ports` と同じに引く)を
/// serial の `uart` と同じに開く(attach しない、lock なし)。IDE が書き込みの `wchlink://` のまま
/// モニターを開いても、target に触れずにスケッチの `Serial` が見える(attach はクロックを組み替え、
/// CH32V205 の s0 を壊す)。
const WCHLINK_SOURCES: [MonitorSource; 6] = [
    MonitorSource::Auto,
    MonitorSource::Uart,
    MonitorSource::Dmdata,
    MonitorSource::Dmseq,
    MonitorSource::Sdi,
    MonitorSource::Rtt,
];

/// The builtin serial-monitor's baud list, so a board's `monitor_port.serial.baudrate` means the
/// same here.
const BAUDS: [u32; 20] = [
    300, 600, 750, 1200, 2400, 4800, 9600, 19200, 31250, 38400, 57600, 74880, 115200, 230400,
    250000, 460800, 500000, 921600, 1_000_000, 2_000_000,
];

/// How long a backend waits for data before it looks at `stop` / the settings again.
const TICK: Duration = Duration::from_millis(50);

/// en: How long an OEP console waits between empty reads (each read is one lock-free request
/// through the broker, about a millisecond): short, so the target's output shows up within a few
/// ms; the wait also ends at once when input comes.
/// ja: OEP の console の、空の読み出しの間の待ち(読み出し 1 回はブローカー越しの lock 無しの要求で
/// 約 1 ms)。短くして target の出力が数 ms で届くように。入力が来たらすぐ終わる。
const OEP_IDLE: Duration = Duration::from_millis(5);

/// en: The monitor's settings (the DESCRIBE parameters). `baudrate`, `dtr` and `rts` apply to
/// `uart` and are accepted (and ignored) with the other sources. `chip` is `--chip`: the board's
/// family (`monitor_port.serial.chip`), checked fail-closed against the target wherever the
/// source attaches anyway (`uart` does not attach, since a WCH-Link attach re-clocks the target).
/// ja: DESCRIBE の設定。`baudrate` / `dtr` / `rts` は `uart` 用で、他の source でも受けて無視する。
/// `chip` は `--chip` と同じ(板の家系)。source が attach するところで fail-closed に照合する
/// (`uart` は attach しない。WCH-Link の attach は target のクロックを組み替えるため)。
#[derive(Debug, Clone, PartialEq, Eq)]
struct Settings {
    sources: &'static [MonitorSource],
    source: MonitorSource,
    baud: u32,
    dtr: bool,
    rts: bool,
    chip: Option<String>,
    /// The debug speed (`--speed`'s values) the sources that attach use (wchlink / oep).
    speed: String,
}

impl Settings {
    fn new(protocol: &str, chip: Option<String>, speed: String) -> Self {
        let sources: &'static [MonitorSource] = match protocol {
            "wchlink" => &WCHLINK_SOURCES,
            "oep" => &OEP_SOURCES,
            "ch32rv-hid" => &HID_SOURCES,
            // serial / ch32rv-app (its CDC) / ch32rv-isp / ch32rv-bootloader: the
            // UART source only.
            _ => &SERIAL_SOURCES,
        };
        // Line settings default to the builtin serial-monitor's.
        Settings {
            sources,
            source: sources[0],
            baud: 9600,
            dtr: true,
            rts: true,
            chip,
            speed,
        }
    }

    /// Apply `CONFIGURE <key> <value>`; `Err` is the message the IDE shows.
    fn set(&mut self, key: &str, value: &str) -> Result<(), String> {
        let on_off = |v: &str| match v {
            "on" => Ok(true),
            "off" => Ok(false),
            _ => Err(format!("{key} is on or off, not `{value}`")),
        };
        match key {
            "source" => {
                self.source = *self
                    .sources
                    .iter()
                    .find(|s| s.as_str() == value)
                    .ok_or_else(|| format!("unknown source `{value}`"))?;
            }
            "baudrate" => {
                self.baud = value
                    .parse()
                    .ok()
                    .filter(|b| BAUDS.contains(b))
                    .ok_or_else(|| format!("unsupported baudrate `{value}`"))?;
            }
            "dtr" => self.dtr = on_off(value)?,
            "rts" => self.rts = on_off(value)?,
            // en: No longer offered (0.35.1 and before did): IDE 2 keeps every setting it was once
            // offered in pluggable-monitor-settings.json and sends it back on each open, and a saved
            // part number made OPEN fail after the board's Part Number changed. Taken and ignored,
            // so such a file still opens; `--chip` / `--speed` on the command line still apply.
            // ja: もう出さない(0.35.1 までは出していた)。IDE 2 は一度出された設定を保存し、開くたびに
            // 送り返す。保存された型番は Part Number を変えた後の OPEN を失敗させた。受けて無視する
            // (そのファイルでも開ける)。コマンド行の `--chip` / `--speed` は効く。
            "chip" | "speed" => {}
            _ => return Err(format!("unknown setting `{key}`")),
        }
        Ok(())
    }

    fn describe(&self, protocol: &str) -> Value {
        let on_off = |b: bool| if b { "on" } else { "off" };
        // en: The HID serial has no line settings: only its source. (A baudrate / dtr / rts the
        // IDE saved earlier is still taken and ignored by `set`.)
        // ja: HID シリアルに線の設定は無い(source だけ)。保存済みの baudrate などは `set` が受けて無視。
        if self.sources == HID_SOURCES {
            return json!({
                "protocol": protocol,
                "configuration_parameters": {
                    "source": {
                        "label": "Monitor Source", "type": "enum",
                        "value": ["hid"], "selected": "hid",
                    },
                }
            });
        }
        json!({
            "protocol": protocol,
            "configuration_parameters": {
                "source": {
                    "label": "Monitor Source", "type": "enum",
                    "value": self.sources.iter().map(|s| s.as_str()).collect::<Vec<_>>(),
                    "selected": self.source.as_str(),
                },
                "baudrate": {
                    "label": "Baud Rate (UART)", "type": "enum",
                    "value": BAUDS.iter().map(u32::to_string).collect::<Vec<_>>(),
                    "selected": self.baud.to_string(),
                },
                "dtr": {
                    "label": "DTR (UART)", "type": "enum",
                    "value": ["on", "off"], "selected": on_off(self.dtr),
                },
                "rts": {
                    "label": "RTS (UART)", "type": "enum",
                    "value": ["on", "off"], "selected": on_off(self.rts),
                },
            }
        })
    }
}

/// What the backends need from the command line (the `Cli` itself stays on the main thread).
#[derive(Clone)]
struct Env {
    lock_timeout: Duration,
}

/// State shared between the protocol loop and the session thread.
struct Shared {
    settings: Mutex<Settings>,
    stop: AtomicBool,
}

impl Shared {
    fn settings(&self) -> Settings {
        match self.settings.lock() {
            Ok(g) => g.clone(),
            Err(p) => p.into_inner().clone(),
        }
    }
}

/// `arduino monitor`: the Pluggable Monitor protocol over stdio. OPEN resolves the port address
/// (a probe's serial port such as `/dev/ttyACM0` / `COM3`, or `wchlink://<serial>`), opens the
/// selected source, connects (TCP client) to the IDE-provided address and pipes the target's
/// output to it and the IDE's input back. The process ends when stdin closes (arduino-cli may die
/// without sending CLOSE / QUIT), and when the session fails after OPEN, having first sent the
/// reason as a data line (arduino-cli shows the tool's stderr to nobody).
pub fn monitor(cli: &Cli, protocol: &str) -> ExitCode {
    let stdin = std::io::stdin();
    let mut out = std::io::stdout();
    let env = Env {
        lock_timeout: Duration::from_secs(cli.lock_timeout),
    };
    let shared = Arc::new(Shared {
        settings: Mutex::new(Settings::new(protocol, cli.chip.clone(), cli.speed.clone())),
        stop: AtomicBool::new(false),
    });
    let mut running: Option<(JoinHandle<()>, TcpStream)> = None;

    // CLOSE / QUIT / EOF: stop the session thread and shut the socket so its blocked reads end.
    // Every backend waits in `TICK` steps, so the join is short.
    let close = |running: &mut Option<(JoinHandle<()>, TcpStream)>| {
        shared.stop.store(true, Ordering::SeqCst);
        if let Some((h, sock)) = running.take() {
            let _ = sock.shutdown(Shutdown::Both);
            let _ = h.join();
        }
    };

    for line in stdin.lock().lines() {
        let Ok(line) = line else { break };
        mlog(format_args!("< {line}"));
        let parts: Vec<&str> = line.split_whitespace().collect();
        let word = parts.first().copied().unwrap_or("").to_ascii_uppercase();
        match word.as_str() {
            "" => {}
            "HELLO" => memit(
                &mut out,
                &json!({"eventType":"hello","protocolVersion":1,"message":"OK"}),
            ),
            "DESCRIBE" => memit(
                &mut out,
                &json!({
                    "eventType":"describe","message":"OK",
                    "port_description": shared.settings().describe(protocol),
                }),
            ),
            "CONFIGURE" => {
                let result = match (parts.get(1), parts.get(2)) {
                    (Some(k), Some(v)) => match shared.settings.lock() {
                        Ok(mut g) => g.set(k, v),
                        Err(p) => p.into_inner().set(k, v),
                    },
                    _ => Err("CONFIGURE <key> <value>".to_owned()),
                };
                match result {
                    Ok(()) => memit(&mut out, &json!({"eventType":"configure","message":"OK"})),
                    Err(m) => memit(
                        &mut out,
                        &json!({"eventType":"configure","error":true,"message":m}),
                    ),
                }
            }
            "OPEN" => {
                // OPEN <client-host:port> <port-address>
                let (Some(&client), Some(&address)) = (parts.get(1), parts.get(2)) else {
                    memit(
                        &mut out,
                        &json!({"eventType":"open","error":true,"message":"OPEN needs <host:port> <port>"}),
                    );
                    continue;
                };
                if running.is_some() {
                    memit(
                        &mut out,
                        &json!({"eventType":"open","error":true,"message":"already open"}),
                    );
                    continue;
                }
                shared.stop.store(false, Ordering::SeqCst);
                let slot: Arc<OpenSlot> = Arc::default();
                let (client, address) = (client.to_owned(), address.to_owned());
                let (shared2, env2, slot2) = (shared.clone(), env.clone(), slot.clone());
                let handle = std::thread::spawn(move || {
                    session(&client, &address, &shared2, &env2, &slot2);
                    // Whatever happened, the OPEN is not left waiting.
                    slot2.finish(Err("monitor session ended before it opened".to_owned()));
                });
                match slot.wait(OPEN_ANSWER_WITHIN) {
                    Opened::Ready(sock) => {
                        running = Some((handle, sock));
                        memit(&mut out, &json!({"eventType":"open","message":"OK"}));
                    }
                    Opened::Still(mut sock) => {
                        mlog(format_args!("open: still opening, answered OK"));
                        let _ = write!(
                            sock,
                            "[ch32rv monitor] still connecting to the target; its output follows when it answers\r\n"
                        );
                        running = Some((handle, sock));
                        memit(&mut out, &json!({"eventType":"open","message":"OK"}));
                    }
                    Opened::Failed(m) => {
                        let _ = handle.join();
                        memit(
                            &mut out,
                            &json!({"eventType":"open","error":true,"message":m}),
                        );
                    }
                }
            }
            "CLOSE" => {
                close(&mut running);
                memit(&mut out, &json!({"eventType":"close","message":"OK"}));
            }
            "QUIT" => {
                // en: Answer first: arduino-cli waits 250 ms for it and then kills the process
                // whatever came back (internal/arduino/monitor/monitor.go, 1.3.1), so a session
                // still open (no CLOSE before) gets its clean-up only while it is not killed.
                // CLOSE, which arduino-cli waits 5 s for, is where the clean-up is done.
                // ja: 先に返事をする(arduino-cli は 250 ms 待ち、返事の有無によらず process を止める)。
                // 後始末は 5 秒待つ CLOSE で済ませる。
                memit(&mut out, &json!({"eventType":"quit","message":"OK"}));
                close(&mut running);
                return ExitCode::SUCCESS;
            }
            other => memit(
                &mut out,
                &json!({
                "eventType": other.to_ascii_lowercase(),"error":true,
                "message":format!("unknown command {other}")}),
            ),
        }
    }
    // stdin closed: arduino-cli is gone (or going). Never outlive it.
    mlog(format_args!("stdin closed"));
    close(&mut running);
    ExitCode::SUCCESS
}

/// A resolved port address: the WCH probe behind it (if any) and the serial port to use for
/// `uart` / `sdi` (if any).
struct Resolved {
    entry: Option<Entry>,
    port: Option<String>,
    /// An OEP probe's address (`oep://` or a serial port no WCH-Link owns).
    oep: Option<crate::oep::OepAddr>,
    /// `ch32rv-hid://<topology>`: the board's own HID port at that USB position.
    hid: Option<String>,
}

/// en: `wchlink://<serial>` names a probe (its first CDC serves uart / sdi). Anything else is a
/// serial port path: the WCH probe that owns it, if one does (found through `probe list`'s
/// `ports`, so a Link without a USB serial number is found by position), else a plain serial
/// port, which only `uart` can use.
/// ja: `wchlink://<serial>` は probe(uart / sdi は最初の CDC)。それ以外は serial port の path で、
/// それを持つ WCH probe があればそれ(`probe list` の `ports` で引くので serial 番号の無い Link も
/// 位置で見つかる)、無ければ素の serial port(uart だけが使える)。
fn resolve_address(address: &str) -> Result<Resolved, String> {
    // en: Enumerated only for the addresses that name a WCH-Link (an enumeration waits on any USB
    // device the kernel is still probing). ja: WCH-Link を指すアドレスでだけ列挙する。
    let entries = || crate::cmd_probe::wch_devices().map_err(|e| format!("USB enumeration: {e}"));
    if let Some(id) = address.strip_prefix("wchlink://") {
        let entry = entries()?
            .into_iter()
            .find(|e| e.dev.port_id().as_deref() == Some(id))
            .ok_or_else(|| format!("no WCH-Link {id} is connected"))?;
        let port = entry.dev.serial_ports().into_iter().next();
        return Ok(Resolved {
            entry: Some(entry),
            port,
            oep: None,
            hid: None,
        });
    }
    // A CH32RV application: its CDC port, if it has one. The bootloader has no console.
    if let Some(id) = address.strip_prefix(crate::cmd_isp::APP_SCHEME) {
        let dev = crate::cmd_isp::find_app(id).map_err(|(_, m)| m)?;
        let port = dev.serial_ports().into_iter().next().ok_or_else(|| {
            "this CH32RV application has no serial (CDC) port to monitor".to_owned()
        })?;
        return Ok(Resolved {
            entry: None,
            port: Some(port),
            oep: None,
            hid: None,
        });
    }
    if address.starts_with(crate::cmd_isp::ISP_SCHEME) {
        return Err("the ISP bootloader has no console to monitor".to_owned());
    }
    // en: The board's own HID port: its HID serial, waited for while the bootloader is there.
    // ja: board 自身の HID の port。HID シリアルを開く(bootloader の間は待つ)。
    if address.starts_with(crate::cmd_boot::HID_SCHEME) {
        let t = crate::cmd_boot::hid_position(address).map_err(|(m, _)| m)?;
        return Ok(Resolved {
            entry: None,
            port: None,
            oep: None,
            hid: Some(t.to_owned()),
        });
    }
    if address.starts_with(crate::cmd_isp::BOOTLOADER_SCHEME) {
        return Err(
            "this port waits for a bootloader to upload through; it has no console to monitor (pick the board's own port)"
                .to_owned(),
        );
    }
    // `oep://` with any `via`: the same resolution as `--probe`, so the monitor meets the broker
    // the upload used.
    if address.starts_with("oep://") {
        let a = crate::oep::resolve_oep_url(address)?;
        return Ok(Resolved {
            entry: None,
            port: None,
            oep: Some(a),
            hid: None,
        });
    }
    let sel = ch32rv_usb::Selector::Port(address.to_owned());
    let mut owners: Vec<Entry> = entries()?
        .into_iter()
        .enumerate()
        .filter(|(i, e)| sel.matches(&e.dev, *i))
        .map(|(_, e)| e)
        .collect();
    if owners.len() > 1 {
        return Err(format!(
            "{} WCH-Links could own {address} (without a USB serial number they cannot be told \
             apart on this OS); pick the Link's wchlink://<serial> port, or name it with --probe",
            owners.len()
        ));
    }
    let entry = owners.pop();
    let oep = entry
        .is_none()
        .then(|| crate::oep::OepAddr::Serial(address.to_owned()));
    Ok(Resolved {
        entry,
        port: Some(address.to_owned()),
        oep,
        hid: None,
    })
}

/// One open source. Each holds what keeps it alive (the probe lock, the attach) until dropped.
enum Backend {
    Uart {
        port: Box<dyn SerialPort>,
        applied: Settings,
    },
    Dmi {
        src: Box<DmiSource>,
        session: Box<Session>,
    },
    /// `auto`: every path the target talks on, merged.
    Auto {
        auto: Box<crate::auto_console::Auto>,
        applied: Settings,
    },
    /// The board's own HID serial at a USB position (`ch32rv-hid://`), waited for (again)
    /// while it is not there - the bootloader up during an upload, or unplugged.
    Hid {
        at: String,
        link: Option<Box<crate::hid_serial::Link>>,
        /// The waiting line has been sent since the link was last up.
        said: bool,
    },
    /// A console stream on an OEP probe, through its broker.
    Oep {
        console: Box<crate::oep::ConsoleSession>,
        source: MonitorSource,
        /// The fixture UART's baud as last configured.
        baud: u32,
    },
}

/// en: One turn of the HID port's monitor: poll the HID serial when it is open (dropping it when
/// it stops answering), else look for it at `at` again every 300 ms, telling the IDE once that
/// it waits (input typed meanwhile is dropped: there is no one to take it).
/// ja: HID の port の monitor の 1 回。開いていれば poll(応えなくなれば落とす)、無ければ 300 ms
/// ごとに `at` を探し直し、待っていることを 1 回だけ IDE に伝える(その間の入力は捨てる)。
fn hid_poll(
    at: &str,
    link: &mut Option<Box<crate::hid_serial::Link>>,
    said: &mut bool,
    pending: &mut Vec<u8>,
    input: &Receiver<Vec<u8>>,
) -> Vec<u8> {
    if let Some(l) = link {
        match l.step(pending) {
            Ok(st) => {
                if st.idle
                    && let Ok(chunk) = input.recv_timeout(Duration::from_millis(2))
                {
                    pending.extend_from_slice(&chunk);
                }
                return st.data;
            }
            Err(_) => *link = None,
        }
    }
    match crate::hid_serial::Link::open(&crate::hid_serial::Which::At(at.to_owned())) {
        Ok(Some(l)) => {
            *link = Some(Box::new(l));
            if std::mem::take(said) {
                return format!("[ch32rv monitor] HID serial {at} is back\n").into_bytes();
            }
            Vec::new()
        }
        _ => {
            pending.clear();
            std::thread::sleep(Duration::from_millis(300));
            if *said {
                Vec::new()
            } else {
                *said = true;
                format!("[ch32rv monitor] waiting for the HID serial at {at} (the sketch is not running there)\n")
                    .into_bytes()
            }
        }
    }
}

/// Why a backend returned.
#[derive(Debug)]
enum Leave {
    /// CLOSE / QUIT / stdin EOF.
    Stop,
    /// `source` changed while open: set up the new one on the same socket.
    Switch,
    /// The session is lost; the string is the reason for the user.
    Failed(String),
}

impl Backend {
    fn open(r: &Resolved, s: Settings, env: &Env) -> Result<Self, String> {
        if s.source == MonitorSource::Auto {
            return Self::open_auto(r, s, env);
        }
        if s.source == MonitorSource::Hid {
            let Some(at) = r.hid.clone() else {
                return Err(HID_NOT_A_PROBE.to_owned());
            };
            let link = crate::hid_serial::Link::open(&crate::hid_serial::Which::At(at.clone()))
                .ok()
                .flatten()
                .map(Box::new);
            return Ok(Backend::Hid {
                at,
                link,
                said: false,
            });
        }
        // en: A debug source on a serial port no WCH-Link owns: an OEP probe, through its broker.
        // (uart stays the port as it is - opening an unknown port to send an OEP confirm would put
        // bytes on someone's device; an OEP UART-bridge probe streams its bound console raw there.)
        // ja: WCH-Link のものでない serial port で debug の source なら OEP の probe(ブローカー経由)。
        // uart は port の素通しのまま(知らない port に OEP の confirm を送ると誰かの device に
        // バイトが入る。OEP の UART bridge の probe は bind した console をそこに生で流す)。
        // en: A WCH-Link's dmdata / dmseq also go through the Link's broker, so a gdb or a flash
        // can use the same Link meanwhile (the broker polls the mailbox as the Link's console).
        // rtt and sdi still open the Link themselves.
        // ja: WCH-Link の dmdata / dmseq もブローカー経由(同じ Link を gdb や flash が同時に使える)。
        let wch_via_broker = r.entry.as_ref().and_then(|e| {
            matches!(
                s.source,
                MonitorSource::Dmdata | MonitorSource::Dmseq | MonitorSource::Sdi
            )
            .then(|| crate::oep::OepAddr::Wch(crate::broker::BrokerTarget::wch(e)))
        });
        if let Some(oep) = wch_via_broker.as_ref().or(r.oep.as_ref())
            && (s.source != MonitorSource::Uart || matches!(oep, crate::oep::OepAddr::Named { .. }))
        {
            use crate::oep::StreamWanted;
            use ch32rv_oep::stream::Mechanism;
            let wanted = match s.source {
                MonitorSource::Sdi => StreamWanted::Console(Mechanism::Sdi),
                MonitorSource::Dmdata => StreamWanted::Console(Mechanism::Dmdata),
                MonitorSource::Dmseq => StreamWanted::Console(Mechanism::Dmseq),
                MonitorSource::FixtureUart => StreamWanted::FixtureUart(s.baud),
                MonitorSource::Rtt => StreamWanted::Rtt,
                MonitorSource::Auto => unreachable!("auto opens in open_auto"),
                MonitorSource::Hid => return Err(HID_NOT_A_PROBE.to_owned()),
                MonitorSource::Uart => {
                    return Err(
                        "uart is not available on an OEP probe (it offers dmseq, dmdata, sdi, fixture-uart and rtt)"
                            .to_owned(),
                    );
                }
            };
            let (speed, _) = crate::parse::speed(&s.speed)?;
            let c = crate::oep::ConsoleSession::open(
                oep,
                wanted,
                s.chip.as_deref(),
                Some(crate::oep::speed_hz(speed)),
            )?;
            return Ok(Backend::Oep {
                console: Box::new(c),
                source: s.source,
                baud: s.baud,
            });
        }
        match s.source {
            MonitorSource::Auto => unreachable!("auto opens in open_auto"),
            MonitorSource::Hid => Err(HID_NOT_A_PROBE.to_owned()),
            MonitorSource::FixtureUart => {
                Err("fixture-uart is an OEP probe's source (this port is not one)".to_owned())
            }
            MonitorSource::Uart => {
                let path = r
                    .port
                    .as_deref()
                    .ok_or("this probe has no serial port for uart")?;
                // en: No probe lock: uart uses only the CDC (a separate interface from the
                // debug one), and the tty itself is exclusive. A gdb or flash on the same probe
                // may run meanwhile.
                // ja: probe の lock は取らない。uart は CDC(debug の口とは別)だけを使い、tty は排他。
                // The serialport crate opens exclusively (TIOCEXCL) on unix; Windows always does.
                let mut port = serialport::new(path, s.baud)
                    .timeout(TICK)
                    .open()
                    .map_err(|e| format!("open {path}: {e}"))?;
                let _ = port.write_data_terminal_ready(s.dtr);
                let _ = port.write_request_to_send(s.rts);
                Ok(Backend::Uart { port, applied: s })
            }
            MonitorSource::Dmdata
            | MonitorSource::Dmseq
            | MonitorSource::Sdi
            | MonitorSource::Rtt => {
                let entry = r.entry.as_ref().ok_or_else(|| {
                    format!(
                        "{} needs a WCH-Link (this serial port is not one)",
                        s.source.as_str()
                    )
                })?;
                let (speed, _) = crate::parse::speed(&s.speed)?;
                let mut warnings = Vec::new();
                let mut session = Session::attach(
                    entry,
                    speed,
                    Duration::from_millis(1000),
                    env.lock_timeout,
                    s.chip.as_deref(),
                    None,
                    &mut warnings,
                )
                .map_err(|e| format!("attach: {}", e.with_fix()))?;
                // CH32V103: the attach overwrote s1; restart the program before watching it.
                session.keep_running();
                let src = DmiSource::open(&mut session, s.source, &mut warnings)
                    .map_err(|e| format!("open {}: {e}", s.source.as_str()))?;
                // The sources only move while the core runs.
                let _ = session.dm().resume();
                Ok(Backend::Dmi {
                    src: Box::new(src),
                    session: Box::new(session),
                })
            }
        }
    }

    /// en: `auto`: a WCH-Link's CDC and its debug paths (reset to run), an OEP probe's fixture
    /// UART and consoles, or only the serial port (a port no WCH-Link owns is never sent an OEP
    /// confirm: it may be anyone's device).
    /// ja: `auto`。WCH-Link は CDC とデバッグの経路(reset して走らせる)、OEP は fixture の UART と
    /// console、それ以外は serial の口だけ(WCH-Link のものでない口に OEP の confirm は送らない)。
    fn open_auto(r: &Resolved, s: Settings, env: &Env) -> Result<Self, String> {
        use crate::auto_console::Auto;
        let open_port = |path: &str| -> Result<Box<dyn SerialPort>, String> {
            let mut port = serialport::new(path, s.baud)
                .timeout(Duration::from_millis(1))
                .open()
                .map_err(|e| format!("open {path}: {e}"))?;
            let _ = port.write_data_terminal_ready(s.dtr);
            let _ = port.write_request_to_send(s.rts);
            Ok(port)
        };
        let auto = if let Some(entry) = r.entry.as_ref() {
            let (speed, _) = crate::parse::speed(&s.speed)?;
            // en: A gdb session holds the Link's target: the UART at once, saying so (the debug
            // paths would only wait `--lock-timeout` for it). ja: gdb が持っていれば、すぐ UART だけに。
            let wch = crate::oep::OepAddr::Wch(crate::broker::BrokerTarget::wch(entry));
            let opened = match crate::oep::gdb_holder(&wch) {
                Some(pid) => Err(crate::oep::gdb_busy_text(&pid)),
                None => {
                    // The CDC is the UART group; a Link without one (or with it busy) still has the rest.
                    let port = r.port.as_deref().and_then(|p| open_port(p).ok());
                    Auto::open_wch(entry, port, speed, env.lock_timeout, s.chip.as_deref())
                }
            };
            match opened {
                Ok(a) => a,
                // The debug paths do not open (no USB access, no target): the UART still does.
                Err(why) => match r.port.as_deref() {
                    Some(p) => Auto::open_uart_only(open_port(p)?, &why),
                    None => return Err(why),
                },
            }
        } else if let Some(oep @ crate::oep::OepAddr::Named { .. }) = r.oep.as_ref() {
            let (speed, _) = crate::parse::speed(&s.speed)?;
            Auto::open_oep(
                oep,
                s.chip.as_deref(),
                Some(crate::oep::speed_hz(speed)),
                s.baud,
            )?
        } else {
            let path = r
                .port
                .as_deref()
                .ok_or("this port has no serial port to monitor")?;
            Auto::open_uart(open_port(path)?)
        };
        Ok(Backend::Auto {
            auto: Box::new(auto),
            applied: s,
        })
    }

    /// Pump until stopped, switched or failed.
    fn run(&mut self, shared: &Shared, sock: &mut TcpStream, input: &Receiver<Vec<u8>>) -> Leave {
        let mut pending = Vec::new();
        // en: `auto` on a WCH-Link has just reset the target: what was typed before is dropped.
        // ja: WCH-Link の `auto` は target を reset したところなので、それまでの入力は捨てる。
        if matches!(self, Backend::Auto { .. }) {
            source::drain_input(input, &mut Vec::new());
        }
        let mut buf = [0u8; 512];
        loop {
            if shared.stop.load(Ordering::SeqCst) {
                return Leave::Stop;
            }
            let now = shared.settings();
            source::drain_input(input, &mut pending);
            let got: Result<Vec<u8>, String> = match self {
                Backend::Uart { port, applied, .. } => {
                    if now.source != MonitorSource::Uart {
                        return Leave::Switch;
                    }
                    // Line settings follow the IDE live, like the builtin serial-monitor.
                    if now.baud != applied.baud {
                        let _ = port.set_baud_rate(now.baud);
                    }
                    if now.dtr != applied.dtr {
                        let _ = port.write_data_terminal_ready(now.dtr);
                    }
                    if now.rts != applied.rts {
                        let _ = port.write_request_to_send(now.rts);
                    }
                    *applied = now.clone();
                    if !pending.is_empty() {
                        if let Err(e) = port.write_all(&pending) {
                            return Leave::Failed(format!("serial port write failed: {e}"));
                        }
                        pending.clear();
                    }
                    match port.read(&mut buf) {
                        Ok(n) => Ok(buf[..n].to_vec()),
                        Err(e) if e.kind() == std::io::ErrorKind::TimedOut => Ok(Vec::new()),
                        Err(e) => Err(format!("serial port read failed: {e}")),
                    }
                }
                Backend::Auto { auto, applied } => {
                    if now.source != MonitorSource::Auto {
                        return Leave::Switch;
                    }
                    if (now.baud, now.dtr, now.rts) != (applied.baud, applied.dtr, applied.rts) {
                        if let Err(e) = auto.set_line(now.baud, now.dtr, now.rts) {
                            return Leave::Failed(e);
                        }
                        *applied = now.clone();
                    }
                    auto.input(&pending);
                    pending.clear();
                    match auto.poll() {
                        Ok(b) => {
                            if b.is_empty()
                                && let Ok(chunk) = input.recv_timeout(auto.idle())
                            {
                                pending.extend_from_slice(&chunk);
                            }
                            Ok(b)
                        }
                        Err(e) => Err(e),
                    }
                }
                Backend::Hid { at, link, said } => {
                    if now.source != MonitorSource::Hid {
                        return Leave::Switch;
                    }
                    Ok(hid_poll(at, link, said, &mut pending, input))
                }
                Backend::Oep {
                    console,
                    source,
                    baud,
                } => {
                    if now.source != *source {
                        return Leave::Switch;
                    }
                    // The fixture UART follows the IDE's baud live, like a serial monitor.
                    if *source == MonitorSource::FixtureUart && now.baud != *baud {
                        if let Err(e) = console.set_baud(now.baud) {
                            return Leave::Failed(format!("fixture UART baud: {e}"));
                        }
                        *baud = now.baud;
                    }
                    if !pending.is_empty() {
                        match console.write(&pending) {
                            Ok(n) => {
                                pending.drain(..n.min(pending.len()));
                            }
                            Err(e) => return Leave::Failed(format!("console write failed: {e}")),
                        }
                    }
                    match console.poll() {
                        Ok(b) => {
                            // en: Nothing came: wait a little, but for the IDE's input too, so a
                            // line typed (or a test's command) goes to the probe at once rather
                            // than after the wait (it waited up to 20 ms before).
                            // ja: 何も来なければ少し待つ。ただし IDE の入力も待ち、来たらすぐ probe へ送る
                            // (前は最大 20 ms 待たせていた)。
                            if b.is_empty()
                                && let Ok(chunk) = input.recv_timeout(if console.is_rtt() {
                                    // RTT halts the hart per poll: RTT's pace, as on a WCH-Link.
                                    Duration::from_millis(50)
                                } else {
                                    OEP_IDLE
                                })
                            {
                                pending.extend_from_slice(&chunk);
                            }
                            Ok(b)
                        }
                        Err(e) => Err(format!("console read failed: {e}")),
                    }
                }
                Backend::Dmi { src, session } => {
                    if now.source.as_str() != src.name() {
                        return Leave::Switch;
                    }
                    match src.poll(session, &mut pending) {
                        Ok(b) => {
                            if b.is_empty() {
                                std::thread::sleep(src.idle());
                            }
                            Ok(b)
                        }
                        Err(e) => Err(format!("{} failed: {e}", src.name())),
                    }
                }
            };
            match got {
                Ok(b) if b.is_empty() => {}
                Ok(b) => {
                    if sock.write_all(&b).and_then(|()| sock.flush()).is_err() {
                        // The IDE side went away; nothing left to tell.
                        return Leave::Stop;
                    }
                }
                Err(m) => return Leave::Failed(m),
            }
        }
    }
}

/// en: The session thread: resolve `address`, open the selected source, connect to the IDE and
/// report the socket (or why not) through `ready`, then pump until CLOSE. A `source` change while
/// open swaps the backend on the same socket. A lost session sends its reason to the IDE as a
/// data line and ends the whole process: arduino-cli shows the tool's stderr to nobody, and the
/// monitor has to end rather than sit silent.
/// ja: セッションのスレッド。address を解き、source を開き、IDE へ接続して socket(か理由)を
/// `ready` で返し、CLOSE まで流す。開いたままの source 変更は同じ socket で backend を替える。
/// 失ったときは理由を data の行で IDE に送ってプロセスごと終わる(arduino-cli は tool の stderr を
/// 見せず、黙ったまま残ってはいけない)。
/// en: How long OPEN waits for the source to open before it answers OK anyway: arduino-cli gives
/// OPEN 10 s (1.3.1), and an OEP attach right after an upload, while the target enumerates USB,
/// took longer (2026-11-14). Past it the monitor says so as data and streams once the source is
/// open, or reports why not and ends, as a session lost later does.
/// ja: OPEN が source を開くのを待つ時間。これを過ぎたら OK を返し、開けたら流す(開けなければ理由を
/// data で流して終わる)。arduino-cli は OPEN を 10 秒しか待たない。
const OPEN_ANSWER_WITHIN: Duration = Duration::from_secs(7);

/// en: Between OPEN and its session thread: the socket (connected first, so OPEN can answer
/// before the source is open), the open's result, and whether OPEN has already answered.
/// ja: OPEN と session の間の受け渡し(先につないだ socket、開いた結果、OPEN が返事をしたか)。
#[derive(Default)]
struct OpenSlot {
    state: Mutex<OpenState>,
    changed: Condvar,
}

#[derive(Default)]
struct OpenState {
    sock: Option<TcpStream>,
    result: Option<Result<(), String>>,
    answered: bool,
}

/// What OPEN answers.
enum Opened {
    /// The source is open: OK.
    Ready(TcpStream),
    /// Still opening past [`OPEN_ANSWER_WITHIN`]: OK, and the session goes on.
    Still(TcpStream),
    /// The open failed in time: the error.
    Failed(String),
}

impl OpenSlot {
    fn lock(&self) -> std::sync::MutexGuard<'_, OpenState> {
        match self.state.lock() {
            Ok(g) => g,
            Err(p) => p.into_inner(),
        }
    }

    /// The session's socket, once connected.
    fn connected(&self, sock: TcpStream) {
        self.lock().sock = Some(sock);
        self.changed.notify_all();
    }

    /// The open's result; true when OPEN has already answered (the session then reports a
    /// failure itself). Only the first result counts.
    fn finish(&self, r: Result<(), String>) -> bool {
        let mut g = self.lock();
        if g.answered {
            return true;
        }
        if g.result.is_none() {
            g.result = Some(r);
        }
        drop(g);
        self.changed.notify_all();
        false
    }

    /// OPEN's side: the result within `within`, else (once the socket is there) answer anyway.
    fn wait(&self, within: Duration) -> Opened {
        let deadline = std::time::Instant::now() + within;
        let mut g = self.lock();
        loop {
            match g.result.take() {
                Some(Ok(())) => {
                    if let Some(sock) = g.sock.take() {
                        g.answered = true;
                        return Opened::Ready(sock);
                    }
                    return Opened::Failed("the monitor's socket went missing".to_owned());
                }
                Some(Err(m)) => {
                    g.answered = true;
                    return Opened::Failed(m);
                }
                None => {}
            }
            let now = std::time::Instant::now();
            if now >= deadline
                && let Some(sock) = g.sock.take()
            {
                g.answered = true;
                return Opened::Still(sock);
            }
            let step = deadline
                .saturating_duration_since(now)
                .max(Duration::from_millis(100));
            g = match self.changed.wait_timeout(g, step) {
                Ok((g, _)) => g,
                Err(p) => p.into_inner().0,
            };
        }
    }
}

fn session(client: &str, address: &str, shared: &Shared, env: &Env, slot: &OpenSlot) {
    mlog(format_args!("session: opening {address}"));
    let fail = |m: String| {
        mlog(format_args!("session: open failed: {m}"));
        slot.finish(Err(m))
    };
    // en: Connected before anything else, so OPEN can answer in time however long the rest takes
    // (arduino-cli listens before it sends OPEN and accepts after the answer): resolving the
    // address enumerates USB, which waits on any device the kernel is still probing - a CH32H417
    // sketch whose HID interface timed out held it 20 s right after an upload (2026-11-14).
    // ja: 何より先につなぐ(残りがどれだけかかっても OPEN が時間内に返事をできるように)。アドレスの
    // 解決は USB を列挙し、kernel が probe 中の機器を待つ(HID が時間切れの H417 の sketch で 20 秒)。
    let mut sock = match TcpStream::connect(client) {
        Ok(s) => s,
        Err(e) => {
            fail(format!("connect {client}: {e}"));
            return;
        }
    };
    let (Ok(keep), Ok(reader)) = (sock.try_clone(), sock.try_clone()) else {
        fail("socket clone failed".to_owned());
        return;
    };
    slot.connected(keep);
    let resolved = resolve_address(address);
    mlog(format_args!(
        "session: address resolved: {}",
        resolved.is_ok()
    ));
    let resolved = match resolved {
        Ok(r) => r,
        Err(m) => {
            if fail(m.clone()) {
                lost(&mut sock, &m);
            }
            return;
        }
    };
    let mut backend = match Backend::open(&resolved, shared.settings(), env) {
        Ok(b) => b,
        Err(m) => {
            if fail(m.clone()) {
                lost(&mut sock, &m);
            }
            return;
        }
    };
    slot.finish(Ok(()));
    mlog(format_args!("session: open"));
    if shared.stop.load(Ordering::SeqCst) {
        return;
    }
    let input = source::spawn_reader(reader);
    let wch = resolved.entry.is_some();
    say_reclock(&mut sock, shared.settings().source, wch);
    loop {
        let leave = backend.run(shared, &mut sock, &input);
        mlog(format_args!("session: backend left: {leave:?}"));
        match leave {
            Leave::Stop => return,
            Leave::Switch => {
                drop(backend);
                let next = shared.settings();
                match Backend::open(&resolved, next.clone(), env) {
                    Ok(b) => {
                        backend = b;
                        say_reclock(&mut sock, next.source, wch);
                    }
                    Err(m) => lost(&mut sock, &m),
                }
            }
            Leave::Failed(m) => {
                drop(backend);
                lost(&mut sock, &m);
            }
        }
    }
}

/// en: Every source but `uart` goes through a WCH-Link attach, which may re-clock the target and
/// leave it that way (docs/cli.ja.md §4.5, `attach-reclocks-target`): say so in the monitor
/// itself, since that is the only place an IDE user reads, and on every target, since which
/// families it happens to is not fully known.
/// ja: `uart` 以外の source は WCH-Link の attach を通り、target のクロックを組み替えたままにする
/// 可能性がある。IDE の利用者が読むのはモニタだけなので、そこに全 target で「可能性」として出す。
fn say_reclock(sock: &mut TcpStream, source: MonitorSource, wch_link: bool) {
    // Only a WCH-Link's attach touches the clock; an OEP probe's does not.
    // `auto` leaves it to ArduinoCore-CH32RV, whose SysTick handler puts its clock back.
    if matches!(source, MonitorSource::Uart | MonitorSource::Auto) || !wch_link {
        return;
    }
    let _ = write!(
        sock,
        "[ch32rv monitor] attaching may have changed the target clock (UART baud rate, millis, \
         timers); reset the board to run at its own clock\r\n"
    );
    let _ = sock.flush();
}

/// Tell the IDE why the monitor stops, then end the process.
fn lost(sock: &mut TcpStream, why: &str) -> ! {
    mlog(format_args!("session: stopped, process exits: {why}"));
    let _ = write!(sock, "\r\n[ch32rv monitor] stopped: {why}\r\n");
    let _ = sock.flush();
    let _ = sock.shutdown(Shutdown::Both);
    std::process::exit(0)
}

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

    #[test]
    #[allow(clippy::unwrap_used)]
    fn open_answers_in_time_however_long_the_source_takes() {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();
        let sock = || TcpStream::connect(addr).unwrap();
        // Opened in time: OK with the socket.
        let slot = OpenSlot::default();
        slot.connected(sock());
        assert!(!slot.finish(Ok(())));
        assert!(matches!(
            slot.wait(Duration::from_millis(50)),
            Opened::Ready(_)
        ));
        // Failed in time: the error, whether or not the socket was connected.
        let slot = OpenSlot::default();
        assert!(!slot.finish(Err("no probe".to_owned())));
        assert!(
            matches!(slot.wait(Duration::from_millis(50)), Opened::Failed(m) if m == "no probe")
        );
        // Still opening: OK once the socket is there, and a later failure is the session's to
        // report (finish says OPEN has answered).
        let slot = OpenSlot::default();
        slot.connected(sock());
        assert!(matches!(
            slot.wait(Duration::from_millis(50)),
            Opened::Still(_)
        ));
        assert!(slot.finish(Err("attach failed".to_owned())));
    }

    #[test]
    fn chip_and_speed_come_from_the_command_line_only() {
        let mut s = Settings::new("oep", Some("CH32V20x".to_owned()), "high".to_owned());
        let d = s.describe("oep");
        // Not offered, so IDE 2 does not save them...
        assert!(d["configuration_parameters"].get("speed").is_none());
        assert!(d["configuration_parameters"].get("chip").is_none());
        // ...and a file that saved them still opens: taken, ignored.
        assert!(s.set("speed", "low").is_ok());
        assert!(s.set("chip", "CH32V305RBT6").is_ok());
        assert_eq!(s.speed, "high");
        assert_eq!(s.chip.as_deref(), Some("CH32V20x"));
        let s = Settings::new("wchlink", None, "4000".to_owned());
        // A `wchlink://` port reads everything the target talks on unless told otherwise.
        assert_eq!(s.source, MonitorSource::Auto);
        assert_eq!(
            s.describe("wchlink")["configuration_parameters"]["source"]["value"],
            json!(["auto", "uart", "dmdata", "dmseq", "sdi", "rtt"])
        );
        assert_eq!(s.speed, "4000");
    }

    #[test]
    fn an_application_is_labelled_with_its_id_and_port() {
        assert_eq!(
            app_label(Some("CH32RV Arduino"), "AFC6ABCD17D0BC3D", Some("COM6")),
            "CH32RV Arduino AFC6ABCD17D0BC3D (COM6)"
        );
        assert_eq!(
            app_label(None, "usb-3-1.2", Some("/dev/ttyACM5")),
            "CH32RV application usb-3-1.2 (ttyACM5)"
        );
        assert_eq!(
            short_position("PCIROOT(0)#PCI(1400)#USBROOT(0)-1.3.2"),
            "1.3.2"
        );
        assert_eq!(short_position("3-3.2"), "3.2");
    }

    #[test]
    fn sync_mode_says_what_came_went_and_changed() {
        let a = json!({"address": "wchlink://A", "protocol": "wchlink", "label": "A"});
        let b = json!({"address": "ch32rv-hid://1-4", "protocol": "ch32rv-hid", "label": "HID"});
        let a2 = json!({"address": "wchlink://A", "protocol": "wchlink", "label": "A (changed)"});
        let one = std::slice::from_ref(&a);
        assert!(sync_events(one, one).is_empty());
        // A bootloader brought up after the start is added; gone, it is removed.
        let e = sync_events(one, &[a.clone(), b.clone()]);
        assert_eq!(e, vec![json!({"eventType": "add", "port": b})]);
        let e = sync_events(&[a.clone(), b.clone()], one);
        assert_eq!(e[0]["eventType"], "remove");
        assert_eq!(e[0]["port"]["address"], "ch32rv-hid://1-4");
        // A changed description: removed, then added again.
        let e = sync_events(one, std::slice::from_ref(&a2));
        assert_eq!(e[0]["eventType"], "remove");
        assert_eq!(e[1], json!({"eventType": "add", "port": a2}));
    }

    #[test]
    fn a_recent_listing_stands_in_only_for_its_own_probe() {
        let unit = format!("t{:x}", std::process::id());
        let mine = json!({"address": format!("oep://{unit}/l103")});
        let other = json!({"address": "oep://ffffffffffff/l103"});
        let bad = json!({"address": format!("oep://{unit}/My Port")});
        let read = vec![mine.clone(), other, bad];
        assert_eq!(listing_or_recent(Some(read.clone()), &unit), read);
        assert_eq!(listing_or_recent(None, &unit), vec![mine]);
        let _ = std::fs::remove_file(crate::broker::listing_cache(&format!("oep-{unit}")));
    }
}