use core::fmt;
use hdmi_hal::HdmiForumFrl;
use hdmi_hal::phy::TmdsPhyConfig;
use super::Report;
use crate::bench::Bench;
use crate::line::{Line, RATIO_40_ABOVE_KHZ};
pub trait OutputProbe<P: ?Sized> {
fn line(&self, phy: &P) -> Option<Line>;
}
impl<P: ?Sized, const LOG: usize, const FAULTS: usize> OutputProbe<P> for Bench<'_, LOG, FAULTS> {
fn line(&self, _phy: &P) -> Option<Line> {
Some(self.line())
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct NoProbe;
impl<P: ?Sized> OutputProbe<P> for NoProbe {
fn line(&self, _phy: &P) -> Option<Line> {
None
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum PhyPoint {
StopFresh,
StartUnconfigured,
Configure,
ScramblerOffStopped,
ScramblerOnStopped,
Start,
ScramblerOffRunning,
ScramblerOnRunning,
ConfigureRunning,
Stop,
StopStopped,
PowerOff,
StopPoweredOff,
ConfigurePoweredOff,
PowerOn,
PowerOnAgain,
StartAfterPowerOn,
EnterFrl,
StartInFrl,
ConfigureLeavesFrl,
StopLeavesFrl,
FrlOff,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum PhyFailure {
Failed {
point: PhyPoint,
},
Succeeded {
point: PhyPoint,
},
Running {
point: PhyPoint,
expected: bool,
found: bool,
},
Line {
point: PhyPoint,
found: Line,
},
}
impl fmt::Display for PhyFailure {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Failed { point } => write!(f, "{point:?}: the call failed"),
Self::Succeeded { point } => write!(f, "{point:?}: the call should have failed"),
Self::Running {
point,
expected,
found,
} => {
let state = |running: &bool| if *running { "running" } else { "stopped" };
write!(
f,
"{point:?}: the output was {}, not {}",
state(found),
state(expected)
)
}
Self::Line { point, found } => write!(
f,
"{point:?}: transmits {found:?}, not what the configuration and the calls ask"
),
}
}
}
pub struct PhyCheck<'p, R: ?Sized> {
config: TmdsPhyConfig,
probe: &'p R,
frl_rate: Option<HdmiForumFrl>,
}
impl<R: ?Sized> Clone for PhyCheck<'_, R> {
fn clone(&self) -> Self {
*self
}
}
impl<R: ?Sized> Copy for PhyCheck<'_, R> {}
impl<R: ?Sized> fmt::Debug for PhyCheck<'_, R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PhyCheck")
.field("config", &self.config)
.field("frl_rate", &self.frl_rate)
.finish_non_exhaustive()
}
}
impl<'p, R: ?Sized> PhyCheck<'p, R> {
pub const fn new(config: TmdsPhyConfig, probe: &'p R) -> Self {
Self {
config,
probe,
frl_rate: None,
}
}
#[must_use]
pub const fn with_frl_rate(self, rate: HdmiForumFrl) -> Self {
Self {
frl_rate: Some(rate),
..self
}
}
}
struct Points<'r> {
report: &'r mut Report<PhyFailure>,
}
impl Points<'_> {
fn ok<E>(&mut self, point: PhyPoint, result: Result<(), E>) -> bool {
let ok = result.is_ok();
self.report
.record((!ok).then_some(PhyFailure::Failed { point }));
ok
}
fn refused<E>(&mut self, point: PhyPoint, result: Result<(), E>) {
self.report
.record(result.is_ok().then_some(PhyFailure::Succeeded { point }));
}
fn running(&mut self, point: PhyPoint, line: Option<Line>, expected: bool) {
if let Some(line) = line {
let found = line != Line::Off;
self.report
.record((found != expected).then_some(PhyFailure::Running {
point,
expected,
found,
}));
}
}
fn tmds(
&mut self,
point: PhyPoint,
line: Option<Line>,
config: TmdsPhyConfig,
scrambled: Option<bool>,
) {
let Some(line) = line else { return };
self.report.record(match line {
Line::Off => Some(PhyFailure::Running {
point,
expected: true,
found: false,
}),
Line::Tmds {
character_rate_khz,
ratio,
scrambled: found,
} if character_rate_khz == config.character_rate_khz
&& ratio == config.clock_ratio
&& scrambled.is_none_or(|expected| expected == found) =>
{
None
}
found => Some(PhyFailure::Line { point, found }),
});
}
fn frl(&mut self, point: PhyPoint, line: Option<Line>, rate: HdmiForumFrl) {
let Some(line) = line else { return };
self.report.record(match line {
Line::Off => Some(PhyFailure::Running {
point,
expected: true,
found: false,
}),
Line::Frl { rate: found, .. } if found == rate => None,
found => Some(PhyFailure::Line { point, found }),
});
}
}
fn scrambles(config: TmdsPhyConfig, caps: hdmi_hal::phy::TmdsPhyCapabilities) -> bool {
caps.scrambling
&& (config.character_rate_khz > RATIO_40_ABOVE_KHZ || caps.scrambles_at_low_rate())
}
macro_rules! tmds {
($check:ident, $points:ident, $phy:ident, $($await:tt)*) => {{
use PhyPoint as P;
let config = $check.config;
let caps = $phy.capabilities();
$points.ok(P::StopFresh, $phy.stop_output() $($await)*);
$points.running(P::StopFresh, $check.probe.line(&*$phy), false);
$points.refused(P::StartUnconfigured, $phy.start_output() $($await)*);
$points.running(P::StartUnconfigured, $check.probe.line(&*$phy), false);
if $points.ok(P::Configure, $phy.configure(config) $($await)*) {
$points.running(P::Configure, $check.probe.line(&*$phy), false);
$points.ok(P::ScramblerOffStopped, $phy.set_scrambling(false) $($await)*);
if scrambles(config, caps) {
$points.ok(P::ScramblerOnStopped, $phy.set_scrambling(true) $($await)*);
} else if !caps.scrambling {
$points.refused(P::ScramblerOnStopped, $phy.set_scrambling(true) $($await)*);
}
if $points.ok(P::Start, $phy.start_output() $($await)*) {
$points.tmds(P::Start, $check.probe.line(&*$phy), config, None);
if $points.ok(P::ScramblerOffRunning, $phy.set_scrambling(false) $($await)*) {
let line = $check.probe.line(&*$phy);
$points.tmds(P::ScramblerOffRunning, line, config, Some(false));
}
if scrambles(config, caps)
&& $points.ok(P::ScramblerOnRunning, $phy.set_scrambling(true) $($await)*)
{
let line = $check.probe.line(&*$phy);
$points.tmds(P::ScramblerOnRunning, line, config, Some(true));
}
if $phy.configure(config) $($await)*.is_ok() {
$points.running(P::ConfigureRunning, $check.probe.line(&*$phy), false);
}
}
}
$points.ok(P::Stop, $phy.stop_output() $($await)*);
$points.running(P::Stop, $check.probe.line(&*$phy), false);
$points.ok(P::StopStopped, $phy.stop_output() $($await)*);
}};
}
macro_rules! power {
($check:ident, $points:ident, $phy:ident, $($await:tt)*) => {{
use PhyPoint as P;
let config = $check.config;
if $points.ok(P::Configure, $phy.configure(config) $($await)*)
&& $points.ok(P::Start, $phy.start_output() $($await)*)
{
$points.tmds(P::Start, $check.probe.line(&*$phy), config, None);
$points.ok(P::PowerOnAgain, $phy.power_on() $($await)*);
$points.tmds(P::PowerOnAgain, $check.probe.line(&*$phy), config, None);
}
$points.ok(P::PowerOff, $phy.power_off() $($await)*);
$points.running(P::PowerOff, $check.probe.line(&*$phy), false);
$points.ok(P::StopPoweredOff, $phy.stop_output() $($await)*);
$points.refused(P::ConfigurePoweredOff, $phy.configure(config) $($await)*);
$points.ok(P::PowerOn, $phy.power_on() $($await)*);
$points.running(P::PowerOn, $check.probe.line(&*$phy), false);
$points.refused(P::StartAfterPowerOn, $phy.start_output() $($await)*);
}};
}
macro_rules! frl {
($check:ident, $points:ident, $phy:ident, $($await:tt)*) => {{
use PhyPoint as P;
if let Some(rate) = $check.frl_rate {
let config = $check.config;
if $points.ok(P::EnterFrl, $phy.set_frl_rate(rate) $($await)*) {
$points.frl(P::EnterFrl, $check.probe.line(&*$phy), rate);
$points.refused(P::StartInFrl, $phy.start_output() $($await)*);
$points.ok(P::ConfigureLeavesFrl, $phy.configure(config) $($await)*);
$points.running(P::ConfigureLeavesFrl, $check.probe.line(&*$phy), false);
}
if $points.ok(P::EnterFrl, $phy.set_frl_rate(rate) $($await)*) {
$points.ok(P::StopLeavesFrl, $phy.stop_output() $($await)*);
$points.running(P::StopLeavesFrl, $check.probe.line(&*$phy), false);
}
if $points.ok(P::EnterFrl, $phy.set_frl_rate(rate) $($await)*) {
$points.ok(P::FrlOff, $phy.set_frl_rate(HdmiForumFrl::NotSupported) $($await)*);
$points.running(P::FrlOff, $check.probe.line(&*$phy), false);
$points.refused(P::StartInFrl, $phy.start_output() $($await)*);
}
}
}};
}
impl<R: ?Sized> PhyCheck<'_, R> {
pub fn run_tmds<P>(&self, phy: &mut P) -> Report<PhyFailure>
where
P: hdmi_hal::phy::TmdsPhy + ?Sized,
R: OutputProbe<P>,
{
let mut report = Report::new();
let mut points = Points {
report: &mut report,
};
let check = self;
tmds!(check, points, phy,);
report
}
pub fn run_powered<P>(&self, phy: &mut P) -> Report<PhyFailure>
where
P: hdmi_hal::phy::TmdsPhy + hdmi_hal::phy::PhyPower + ?Sized,
R: OutputProbe<P>,
{
let mut report = Report::new();
let mut points = Points {
report: &mut report,
};
let check = self;
points.ok(PhyPoint::PowerOff, phy.power_off());
points.ok(PhyPoint::PowerOn, phy.power_on());
tmds!(check, points, phy,);
power!(check, points, phy,);
report
}
pub fn run_frl<P>(&self, phy: &mut P) -> Report<PhyFailure>
where
P: hdmi_hal::phy::TmdsPhy + hdmi_hal::phy::PhyPower + hdmi_hal::phy::FrlPhy + ?Sized,
R: OutputProbe<P>,
{
let mut report = self.run_powered(phy);
let mut points = Points {
report: &mut report,
};
let check = self;
frl!(check, points, phy,);
report
}
#[cfg(feature = "async")]
pub async fn run_tmds_async<P>(&self, phy: &mut P) -> Report<PhyFailure>
where
P: hdmi_hal_async::phy::TmdsPhy + ?Sized,
R: OutputProbe<P>,
{
let mut report = Report::new();
let mut points = Points {
report: &mut report,
};
let check = self;
tmds!(check, points, phy, .await);
report
}
#[cfg(feature = "async")]
pub async fn run_powered_async<P>(&self, phy: &mut P) -> Report<PhyFailure>
where
P: hdmi_hal_async::phy::TmdsPhy + hdmi_hal_async::phy::PhyPower + ?Sized,
R: OutputProbe<P>,
{
let mut report = Report::new();
let mut points = Points {
report: &mut report,
};
let check = self;
points.ok(PhyPoint::PowerOff, phy.power_off().await);
points.ok(PhyPoint::PowerOn, phy.power_on().await);
tmds!(check, points, phy, .await);
power!(check, points, phy, .await);
report
}
#[cfg(feature = "async")]
pub async fn run_frl_async<P>(&self, phy: &mut P) -> Report<PhyFailure>
where
P: hdmi_hal_async::phy::TmdsPhy
+ hdmi_hal_async::phy::PhyPower
+ hdmi_hal_async::phy::FrlPhy
+ ?Sized,
R: OutputProbe<P>,
{
let mut report = self.run_powered_async(phy).await;
let mut points = Points {
report: &mut report,
};
let check = self;
frl!(check, points, phy, .await);
report
}
}