fix(gui): headless bench driver re-finds the board by MAC across reboots

The A113D can take a different DHCP lease (or lose its v4 lease) across the
install/restore reboots, so assuming the board keeps its initial IP broke the
verify/restore steps. Resolve the board's MAC up front (on stock) and, after
each reboot, re-locate it in the neighbour table (sweeping the /24 when it is
not cached) so verify and the flag-restore target the board's CURRENT address.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B1W6qKwKxq3oUPDKWicM6i
This commit is contained in:
2026-11-16 12:22:40 -06:00
co-authored by Claude Opus 4.8
parent bdd94dad8a
commit 79f9efc3e3
+89 -12
View File
@@ -124,18 +124,87 @@ fn classify(host: &str) -> (String, Option<bool>) {
}
}
/// Poll `classify` until it reaches the wanted stock-ness, or `timeout` elapses.
fn wait_for(host: &str, want_stock: bool, timeout: Duration) -> (String, bool) {
/// The MAC (lladdr) the kernel has cached for `host` (pings first to populate).
fn mac_of(host: &str) -> Option<String> {
let _ = Command::new("ping")
.args(["-c", "1", "-W", "1", host])
.output();
let out = Command::new("ip").args(["neigh", "show", host]).output().ok()?;
let s = String::from_utf8_lossy(&out.stdout);
let mut it = s.split_whitespace();
while let Some(t) = it.next() {
if t == "lladdr" {
return it.next().map(|m| m.to_lowercase());
}
}
None
}
/// Populate the neighbour table by parallel-pinging the /24 of `sample` (a bench
/// board can take a different DHCP lease across a reboot, so we must re-find it).
fn sweep_subnet(sample: &str) {
let base = match sample.rsplit_once('.') {
Some((net, _)) => net.to_string(),
None => return,
};
let mut kids = Vec::new();
for h in 2..=254 {
if let Ok(c) = Command::new("ping")
.args(["-c", "1", "-W", "1", &format!("{base}.{h}")])
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
{
kids.push(c);
}
}
for mut c in kids {
let _ = c.wait();
}
}
/// Current IPv4 for `mac` from the neighbour table, if present.
fn ip_for_mac(mac: &str) -> Option<String> {
let out = Command::new("ip").args(["neigh"]).output().ok()?;
let s = String::from_utf8_lossy(&out.stdout);
let mac = mac.to_lowercase();
for line in s.lines() {
if line.to_lowercase().contains(&mac) {
if let Some(ip) = line.split_whitespace().next() {
if ip.contains('.') {
return Some(ip.to_string());
}
}
}
}
None
}
/// Poll until the board (located by MAC, so a changed DHCP lease is followed)
/// reaches the wanted stock-ness, or `timeout` elapses. Returns the board's
/// current IP, a description, and whether the target state was reached.
fn wait_for(mac: Option<&str>, fallback: &str, want_stock: bool, timeout: Duration) -> (String, String, bool) {
let deadline = Instant::now() + timeout;
let mut last;
let mut ip = fallback.to_string();
let mut swept = Instant::now() - Duration::from_secs(60);
loop {
let (desc, is_stock) = classify(host);
last = desc;
// Re-locate by MAC; sweep the subnet at most every ~25s to catch a new lease.
if let Some(m) = mac {
if ip_for_mac(m).is_none() && swept.elapsed() >= Duration::from_secs(25) {
sweep_subnet(fallback);
swept = Instant::now();
}
if let Some(found) = ip_for_mac(m) {
ip = found;
}
}
let (desc, is_stock) = classify(&ip);
let line = format!("[{ip}] {desc}");
if is_stock == Some(want_stock) {
return (last, true);
return (ip, line, true);
}
if Instant::now() >= deadline {
return (last, false);
return (ip, line, false);
}
std::thread::sleep(Duration::from_secs(5));
}
@@ -176,6 +245,14 @@ pub fn run(spec: &str) -> i32 {
return 2;
}
// Resolve the board's MAC now (on stock) so we can re-find it after each
// reboot even if its DHCP lease changes under alkane.
let mac = mac_of(&host);
println!(
"board MAC: {}",
mac.as_deref().unwrap_or("unknown (will use the fixed host)")
);
let boot_timeout = Duration::from_secs(300);
let mut all_ok = true;
@@ -203,13 +280,13 @@ pub fn run(spec: &str) -> i32 {
"[2/4] verify alkane (awaiting reboot, up to {}s)...",
boot_timeout.as_secs()
);
let (desc, ok) = wait_for(&host, false, boot_timeout);
let (alkane_ip, desc, ok) = wait_for(mac.as_deref(), &host, false, boot_timeout);
println!(" {desc} => {}", if ok { "ALKANE" } else { "NOT ALKANE (fail)" });
all_ok = all_ok && ok;
// 3. restore (alkane -> stock) flag-flip through the GUI operation layer
println!("[3/4] restore: alkane -> stock (flag flip)");
let h = stock_ops::start_restore_flag(vec![host.clone()], password.clone());
// 3. restore (alkane -> stock) flag-flip at the board's CURRENT address
println!("[3/4] restore: alkane -> stock (flag flip) at {alkane_ip}");
let h = stock_ops::start_restore_flag(vec![alkane_ip.clone()], password.clone());
if !drive("restore", h) {
all_ok = false;
println!("[3/4] restore FAILED");
@@ -220,7 +297,7 @@ pub fn run(spec: &str) -> i32 {
"[4/4] verify stock (awaiting reboot, up to {}s)...",
boot_timeout.as_secs()
);
let (desc, ok) = wait_for(&host, true, boot_timeout);
let (_stock_ip, desc, ok) = wait_for(mac.as_deref(), &host, true, boot_timeout);
println!(" {desc} => {}", if ok { "STOCK" } else { "NOT STOCK (fail)" });
all_ok = all_ok && ok;
}