Carry detection verbs over the HAL wire contract
source-pr / source (pull_request) Successful in 12s

Add capability negotiation and three detection verbs to the local HAL
contract: CAPABILITIES, PROBE_CHAIN, READ_EEPROM and IDENTIFY_PSU. A backend
that predates them rejects the capability query, which surfaces as a protocol
error instead of a silent zero. HalClient implements ChainProbe, so the OS
detection driver can run against a real board without knowing the family.

The probe contract is now error-aware: a transport failure propagates as an
error and is never reported as a silent port, and probe_chain/detect_chains
are generic over the backend. A wire round-trip test drives a mock backend
over a socket pair and checks the encoding of all three verbs.
This commit is contained in:
2026-10-22 10:15:29 -06:00
parent 99fb12d725
commit 3028ef29fb
2 changed files with 273 additions and 24 deletions
+50 -24
View File
@@ -217,11 +217,13 @@ pub enum ProbeOutcome {
/// Framing, baud switching and electrical detail stay in the SDK image for that family; the OS
/// only decides how many attempts to spend and what the results mean.
pub trait ChainProbe {
type Error;
/// Attempt one identification on `chain`. `attempt` lets the backend cycle its candidate
/// interface/baud set deterministically.
fn probe(&mut self, chain: u8, attempt: u8) -> ProbeOutcome;
/// interface/baud set deterministically. A transport failure is an error, never `Silent`.
fn probe(&mut self, chain: u8, attempt: u8) -> Result<ProbeOutcome, Self::Error>;
/// Read the hashboard EEPROM, or report that it is absent or unreadable.
fn eeprom(&mut self, chain: u8) -> EepromState;
fn eeprom(&mut self, chain: u8) -> Result<EepromState, Self::Error>;
}
/// How many identification attempts to spend per chain before declaring it unidentified.
@@ -240,10 +242,14 @@ impl Default for ProbeBudget {
///
/// A chain that never identifies is reported as `ChipModel::Unknown` with zero chips rather than
/// being dropped, so the plan can refuse explicitly and the API can show why.
pub fn probe_chain(backend: &mut impl ChainProbe, chain: u8, budget: ProbeBudget) -> ChainReport {
pub fn probe_chain<B: ChainProbe>(
backend: &mut B,
chain: u8,
budget: ProbeBudget,
) -> Result<ChainReport, B::Error> {
let mut outcome = ProbeOutcome::Silent;
for attempt in 0..budget.attempts.max(1) {
outcome = backend.probe(chain, attempt);
outcome = backend.probe(chain, attempt)?;
if matches!(outcome, ProbeOutcome::Identified { .. }) {
break;
}
@@ -252,25 +258,26 @@ pub fn probe_chain(backend: &mut impl ChainProbe, chain: u8, budget: ProbeBudget
ProbeOutcome::Identified { chip, chips } => (chip, chips),
_ => (ChipModel::Unknown, 0),
};
ChainReport {
Ok(ChainReport {
index: chain,
chip,
chips_found,
eeprom: backend.eeprom(chain),
eeprom: backend.eeprom(chain)?,
response_per_mille: None,
}
})
}
/// Probe every declared chain port.
pub fn detect_chains(
backend: &mut impl ChainProbe,
pub fn detect_chains<B: ChainProbe>(
backend: &mut B,
chains: &[u8],
budget: ProbeBudget,
) -> Vec<ChainReport> {
chains
.iter()
.map(|&chain| probe_chain(backend, chain, budget))
.collect()
) -> Result<Vec<ChainReport>, B::Error> {
let mut reports = Vec::with_capacity(chains.len());
for &chain in chains {
reports.push(probe_chain(backend, chain, budget)?);
}
Ok(reports)
}
#[cfg(test)]
@@ -419,6 +426,7 @@ mod tests {
plan: std::collections::VecDeque<ProbeOutcome>,
eeprom: EepromState,
calls: usize,
fail_at: u8,
}
impl ScriptedProbe {
@@ -427,18 +435,24 @@ mod tests {
plan: plan.iter().copied().collect(),
eeprom,
calls: 0,
fail_at: u8::MAX,
}
}
}
impl ChainProbe for ScriptedProbe {
fn probe(&mut self, _chain: u8, _attempt: u8) -> ProbeOutcome {
type Error = ();
fn probe(&mut self, _chain: u8, attempt: u8) -> Result<ProbeOutcome, ()> {
self.calls += 1;
self.plan.pop_front().unwrap_or(ProbeOutcome::Silent)
if attempt == self.fail_at {
return Err(());
}
Ok(self.plan.pop_front().unwrap_or(ProbeOutcome::Silent))
}
fn eeprom(&mut self, _chain: u8) -> EepromState {
self.eeprom.clone()
fn eeprom(&mut self, _chain: u8) -> Result<EepromState, ()> {
Ok(self.eeprom.clone())
}
}
@@ -455,7 +469,7 @@ mod tests {
],
EepromState::Absent,
);
let report = probe_chain(&mut backend, 0, ProbeBudget { attempts: 4 });
let report = probe_chain(&mut backend, 0, ProbeBudget { attempts: 4 }).expect("probe");
assert_eq!(backend.calls, 3);
assert_eq!(report.chip, ChipModel::Bm1362);
assert_eq!(report.chips_found, 126);
@@ -465,7 +479,7 @@ mod tests {
#[test]
fn probe_exhaustion_is_reported_not_hidden() {
let mut backend = ScriptedProbe::new(&[ProbeOutcome::Silent; 4], EepromState::Absent);
let report = probe_chain(&mut backend, 1, ProbeBudget { attempts: 4 });
let report = probe_chain(&mut backend, 1, ProbeBudget { attempts: 4 }).expect("probe");
assert_eq!(backend.calls, 4);
assert_eq!(report.chip, ChipModel::Unknown);
assert_eq!(report.chips_found, 0);
@@ -480,7 +494,7 @@ mod tests {
model: "BHB42601".into(),
},
);
let report = probe_chain(&mut backend, 2, ProbeBudget { attempts: 2 });
let report = probe_chain(&mut backend, 2, ProbeBudget { attempts: 2 }).expect("probe");
assert_eq!(report.chip, ChipModel::Unknown);
assert_eq!(
report.eeprom,
@@ -509,7 +523,8 @@ mod tests {
],
EepromState::Absent,
);
let reports = detect_chains(&mut backend, &[0, 1, 2], ProbeBudget::default());
let reports =
detect_chains(&mut backend, &[0, 1, 2], ProbeBudget::default()).expect("detect");
assert_eq!(reports.len(), 3);
assert_eq!(
reports.iter().map(|r| r.index).collect::<Vec<_>>(),
@@ -518,10 +533,21 @@ mod tests {
assert!(reports.iter().all(ChainReport::is_identified));
}
#[test]
fn transport_failure_is_not_reported_as_a_silent_port() {
let mut backend = ScriptedProbe::new(&[ProbeOutcome::Silent; 4], EepromState::Absent);
backend.fail_at = 1;
assert_eq!(
probe_chain(&mut backend, 0, ProbeBudget::default()),
Err(())
);
assert_eq!(backend.calls, 2);
}
#[test]
fn unidentified_chain_makes_the_plan_refuse() {
let mut backend = ScriptedProbe::new(&[ProbeOutcome::Silent; 4], EepromState::Absent);
let reports = detect_chains(&mut backend, &[0], ProbeBudget::default());
let reports = detect_chains(&mut backend, &[0], ProbeBudget::default()).expect("detect");
assert_eq!(
plan(&reports, &profile(), Some(&psu()), Vec::new()),
Err(DetectionError::UnknownChip)