Skip to main content

usb3_handshake/
usb3_handshake.rs

1//! Open a real board over USB3 and read its handshake — the first ten minutes on Windows:
2//! `usb3_handshake [serial] [driver-dir]`. Identity registers BY NAME, no endpoint number here.
3//! Does NOT flash: `open_usb3` binds what is running, safe on a shared board (`bundled` covers it).
4
5use kdi::Commands;
6
7fn main() -> std::process::ExitCode {
8    let mut args = std::env::args().skip(1);
9    let serial = args.next().unwrap_or_default();
10    let driver_dir = args.next();
11
12    let opened = kdi::Device::open_usb3(&serial, driver_dir.as_deref().map(std::path::Path::new));
13
14    let mut dev = match opened {
15        Ok(d) => d,
16        Err(e) => {
17            // Naming the failure matters more than the exit code: "no board" and "the driver is
18            // present but a symbol did not resolve" are the two outcomes worth telling apart, and
19            // every entry point resolves by name at dlopen so the second one says which symbol.
20            eprintln!("could not open the board: {e}");
21            return std::process::ExitCode::FAILURE;
22        }
23    };
24
25    let (major, minor) = dev.kdi();
26    let caps = dev.caps();
27    println!("serial          {}", dev.info().serial);
28    println!("transport       {:?}", dev.info().transport);
29    println!("contract        {major}.{minor}");
30    println!("caps            0x{:08x}", caps.0);
31    for c in caps.iter() {
32        println!("                  {}", c.token());
33    }
34    println!("gateware_sha    {:08x}", dev.gateware_sha());
35    if caps.has(kdi::Cap::CommandProtocol) {
36        match dev.sys_hello() {
37            Ok(h) => println!("fw_sha          {}", h.fw),
38            Err(e) => {
39                eprintln!("sys.hello failed: {e}");
40                return std::process::ExitCode::FAILURE;
41            }
42        }
43    } else {
44        println!("fw_sha          -");
45    }
46
47    match dev.wait_ready(std::time::Duration::from_millis(kdi::READY_TIMEOUT_MS)) {
48        Ok(()) => println!("contract_ready  yes"),
49        Err(e) => {
50            // Not fatal for a handshake read: a board still calibrating is a normal transient, and
51            // the point of this program is to report what the device says.
52            println!("contract_ready  NO ({e})");
53        }
54    }
55
56    if let Err(e) = dev.close() {
57        eprintln!("close: {e}");
58        return std::process::ExitCode::FAILURE;
59    }
60    std::process::ExitCode::SUCCESS
61}