From 99fb12d7250a4f2ac0366c2e0176312e3ce37f71 Mon Sep 17 00:00:00 2001 From: Noah <2+noah@noreply.gitea.bluefla.re> Date: Thu, 22 Oct 2026 10:13:18 -0600 Subject: [PATCH] Add chain probing policy to the detection core Define the backend contract for identification (ChainProbe) with a bounded ProbeBudget and a ProbeOutcome that distinguishes an identified chain from a silent port and from unframed bytes. probe_chain retries a chain until it identifies or the budget is spent, and reports an exhausted chain as an unknown chip instead of dropping it; detect_chains walks every declared port. Framing and baud switching stay in the per-family SDK backend. Tests cover retry-until-identified, budget exhaustion, EEPROM state retention and the refusal path when a chain never identifies. --- src/detect.rs | 185 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) diff --git a/src/detect.rs b/src/detect.rs index 05e66c1..ffa5c31 100644 --- a/src/detect.rs +++ b/src/detect.rs @@ -201,6 +201,78 @@ pub fn plan( }) } +/// Outcome of one chip-identification attempt on a chain. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum ProbeOutcome { + /// Chips answered: the model and how many responded. + Identified { chip: ChipModel, chips: u16 }, + /// The port is electrically silent (no signal at all). + Silent, + /// Bytes arrived but did not frame as a chip response: wrong baud, noise, or a dead TX. + Garbled, +} + +/// Backend contract for identification. +/// +/// 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 { + /// 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; + /// Read the hashboard EEPROM, or report that it is absent or unreadable. + fn eeprom(&mut self, chain: u8) -> EepromState; +} + +/// How many identification attempts to spend per chain before declaring it unidentified. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct ProbeBudget { + pub attempts: u8, +} + +impl Default for ProbeBudget { + fn default() -> Self { + Self { attempts: 4 } + } +} + +/// Probe one chain until it identifies or the budget is spent. +/// +/// 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 { + let mut outcome = ProbeOutcome::Silent; + for attempt in 0..budget.attempts.max(1) { + outcome = backend.probe(chain, attempt); + if matches!(outcome, ProbeOutcome::Identified { .. }) { + break; + } + } + let (chip, chips_found) = match outcome { + ProbeOutcome::Identified { chip, chips } => (chip, chips), + _ => (ChipModel::Unknown, 0), + }; + ChainReport { + index: chain, + chip, + chips_found, + eeprom: backend.eeprom(chain), + response_per_mille: None, + } +} + +/// Probe every declared chain port. +pub fn detect_chains( + backend: &mut impl ChainProbe, + chains: &[u8], + budget: ProbeBudget, +) -> Vec { + chains + .iter() + .map(|&chain| probe_chain(backend, chain, budget)) + .collect() +} + #[cfg(test)] mod tests { use super::*; @@ -342,4 +414,117 @@ mod tests { let plan = plan(&chains, &profile(), Some(&psu()), zones.clone()).expect("plan"); assert_eq!(plan.fan_zones, zones); } + + struct ScriptedProbe { + plan: std::collections::VecDeque, + eeprom: EepromState, + calls: usize, + } + + impl ScriptedProbe { + fn new(plan: &[ProbeOutcome], eeprom: EepromState) -> Self { + Self { + plan: plan.iter().copied().collect(), + eeprom, + calls: 0, + } + } + } + + impl ChainProbe for ScriptedProbe { + fn probe(&mut self, _chain: u8, _attempt: u8) -> ProbeOutcome { + self.calls += 1; + self.plan.pop_front().unwrap_or(ProbeOutcome::Silent) + } + + fn eeprom(&mut self, _chain: u8) -> EepromState { + self.eeprom.clone() + } + } + + #[test] + fn probe_retries_until_the_chain_identifies() { + let mut backend = ScriptedProbe::new( + &[ + ProbeOutcome::Silent, + ProbeOutcome::Garbled, + ProbeOutcome::Identified { + chip: ChipModel::Bm1362, + chips: 126, + }, + ], + EepromState::Absent, + ); + let report = probe_chain(&mut backend, 0, ProbeBudget { attempts: 4 }); + assert_eq!(backend.calls, 3); + assert_eq!(report.chip, ChipModel::Bm1362); + assert_eq!(report.chips_found, 126); + assert!(report.is_identified()); + } + + #[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 }); + assert_eq!(backend.calls, 4); + assert_eq!(report.chip, ChipModel::Unknown); + assert_eq!(report.chips_found, 0); + assert!(!report.is_identified()); + } + + #[test] + fn probe_keeps_the_eeprom_state_even_when_unidentified() { + let mut backend = ScriptedProbe::new( + &[ProbeOutcome::Garbled; 2], + EepromState::Present { + model: "BHB42601".into(), + }, + ); + let report = probe_chain(&mut backend, 2, ProbeBudget { attempts: 2 }); + assert_eq!(report.chip, ChipModel::Unknown); + assert_eq!( + report.eeprom, + EepromState::Present { + model: "BHB42601".into() + } + ); + } + + #[test] + fn detection_covers_every_declared_chain_port() { + let mut backend = ScriptedProbe::new( + &[ + ProbeOutcome::Identified { + chip: ChipModel::Bm1362, + chips: 126, + }, + ProbeOutcome::Identified { + chip: ChipModel::Bm1362, + chips: 126, + }, + ProbeOutcome::Identified { + chip: ChipModel::Bm1362, + chips: 126, + }, + ], + EepromState::Absent, + ); + let reports = detect_chains(&mut backend, &[0, 1, 2], ProbeBudget::default()); + assert_eq!(reports.len(), 3); + assert_eq!( + reports.iter().map(|r| r.index).collect::>(), + vec![0, 1, 2] + ); + assert!(reports.iter().all(ChainReport::is_identified)); + } + + #[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()); + assert_eq!( + plan(&reports, &profile(), Some(&psu()), Vec::new()), + Err(DetectionError::UnknownChip) + ); + } }