From 8c8979b9c50fd1aeff2374d4462f544c1c4ce6bb Mon Sep 17 00:00:00 2001 From: Andrey Dyldin Date: Fri, 29 Jan 2021 20:29:19 +0200 Subject: [PATCH] cleaning; FeStats: get_ber(), get_unc() --- README.md | 2 +- src/fe/mod.rs | 24 ++++++++++++------------ src/fe/status.rs | 34 +++++++++++++++++++++++++++++----- src/fe/sys.rs | 25 +------------------------ 4 files changed, 43 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index bdc446f..4a19303 100644 --- a/README.md +++ b/README.md @@ -53,5 +53,5 @@ Frontend status: let fe = FeDevice::open_rd("/dev/dvb/adapter0/frontend0")?; let mut status = FeStatus::default(); status.read(&fe)?; -println!("{}", &status.display(1)); +println!("{}", &status); ``` diff --git a/src/fe/mod.rs b/src/fe/mod.rs index 2f53047..c1bc8f6 100644 --- a/src/fe/mod.rs +++ b/src/fe/mod.rs @@ -125,7 +125,7 @@ impl FeDevice { DtvProperty::new(DTV_TONE, SEC_TONE_OFF), DtvProperty::new(DTV_CLEAR, 0), ]; - self.set_properties(&cmdseq).context("frontend clear")?; + self.set_properties(&cmdseq).context("FE: clear")?; let mut event = FeEvent::default(); @@ -145,7 +145,7 @@ impl FeDevice { ioctl_read!(#[inline] ioctl_call, b'o', 61, FeInfo); unsafe { ioctl_call(self.as_raw_fd(), &mut feinfo as *mut _) - }.context("frontend get info")?; + }.context("FE: get info")?; if let Some(len) = feinfo.name.iter().position(|&b| b == 0) { let name = unsafe { CStr::from_ptr(feinfo.name[.. len + 1].as_ptr()) }; @@ -165,7 +165,7 @@ impl FeDevice { DtvProperty::new(DTV_API_VERSION, 0), DtvProperty::new(DTV_ENUM_DELSYS, 0), ]; - self.get_properties(&mut cmdseq).context("frontend get api version (deprecated driver)")?; + self.get_properties(&mut cmdseq).context("FE: get api version (deprecated driver)")?; // DVB API Version @@ -181,7 +181,7 @@ impl FeDevice { // dev-file metadata - let metadata = self.file.metadata().context("frontend get device metadata")?; + let metadata = self.file.metadata().context("FE: get device metadata")?; ensure!( metadata.file_type().is_char_device(), @@ -197,7 +197,7 @@ impl FeDevice { .write(w) .custom_flags(::nix::libc::O_NONBLOCK) .open(path) - .context("fe open")?; + .context("FE: open")?; let mut fe = FeDevice { file, @@ -292,7 +292,7 @@ impl FeDevice { /// Sets properties on frontend device pub fn set_properties(&self, cmdseq: &[DtvProperty]) -> Result<()> { - self.check_properties(cmdseq).context("fe property check")?; + self.check_properties(cmdseq).context("FE: property check")?; #[repr(C)] pub struct DtvProperties { @@ -309,7 +309,7 @@ impl FeDevice { ioctl_write_ptr!(#[inline] ioctl_call, b'o', 82, DtvProperties); unsafe { ioctl_call(self.as_raw_fd(), &cmd as *const _) - }.context("frontend set properties")?; + }.context("FE: set properties")?; Ok(()) } @@ -331,7 +331,7 @@ impl FeDevice { ioctl_read!(#[inline] ioctl_call, b'o', 83, DtvProperties); unsafe { ioctl_call(self.as_raw_fd(), &mut cmd as *mut _) - }.context("frontend get properties")?; + }.context("FE: get properties")?; Ok(()) } @@ -343,7 +343,7 @@ impl FeDevice { unsafe { ioctl_call(self.as_raw_fd(), event as *mut _) - }.context("frontend get event")?; + }.context("FE: get event")?; Ok(()) } @@ -360,7 +360,7 @@ impl FeDevice { unsafe { ioctl_call(self.as_raw_fd(), value as _) - }.context("frontend set tone")?; + }.context("FE: set tone")?; Ok(()) } @@ -389,7 +389,7 @@ impl FeDevice { unsafe { ioctl_call(self.as_raw_fd(), value as _) - }.context("frontend set voltage")?; + }.context("FE: set voltage")?; Ok(()) } @@ -423,7 +423,7 @@ impl FeDevice { ioctl_write_ptr!(ioctl_call, b'o', 63, DiseqcMasterCmd); unsafe { ioctl_call(self.as_raw_fd(), &cmd as *const _) - }.context("frontend diseqc master cmd")?; + }.context("FE: diseqc master cmd")?; Ok(()) } diff --git a/src/fe/status.rs b/src/fe/status.rs index 846918c..743e6b3 100644 --- a/src/fe/status.rs +++ b/src/fe/status.rs @@ -104,15 +104,15 @@ impl fmt::Display for FeStatus { } write!(f, " BER:")?; - if let Some(ber) = self.props[2].get_stats_counter() { - write!(f, "{}", ber & 0xFFFF)?; + if let Some(ber) = self.get_ber() { + write!(f, "{}", ber)?; } else { write!(f, "-")?; } write!(f, " UNC:")?; - if let Some(unc) = self.props[3].get_stats_counter() { - write!(f, "{}", unc & 0xFFFF)?; + if let Some(unc) = self.get_unc() { + write!(f, "{}", unc)?; } else { write!(f, "-")?; } @@ -124,6 +124,8 @@ impl fmt::Display for FeStatus { impl FeStatus { fn get_signal_level(&self) -> Option<(f64, u64)> { + let stats = self.props.get(0)?; + let _stats = unsafe { &stats.u.st }; // TODO: config for lo/hi // let lo: f64 = -85.0; // let hi: f64 = -6.0; @@ -132,10 +134,32 @@ impl FeStatus { } fn get_signal_noise_ratio(&self) -> Option<(f64, u64)> { + let stats = self.props.get(1)?; + let _stats = unsafe { &stats.u.st }; // let relative = 5 * decibel as u32; None } + fn get_stats_counter(&self, u: usize) -> Option { + let stats = self.props.get(u)?; + let stats = unsafe { &stats.u.st }; + if stats.len > 0 { + let s = &stats.stat[0]; + if s.scale == FE_SCALE_COUNTER { + return Some((s.value & 0xFFFF) as u32); + } + } + None + } + + /// Returns BER value if available + #[inline] + pub fn get_ber(&self) -> Option { self.get_stats_counter(2) } + + /// Returns UNC value if available + #[inline] + pub fn get_unc(&self) -> Option { self.get_stats_counter(3) } + /// Reads frontend status pub fn read(&mut self, fe: &FeDevice) -> Result<()> { self.status = FE_NONE; @@ -144,7 +168,7 @@ impl FeStatus { ioctl_read!(#[inline] ioctl_call, b'o', 69, u32); unsafe { ioctl_call(fe.as_raw_fd(), &mut self.status as *mut _) - }.context("frontend read status")?; + }.context("FE: read status")?; if self.status == FE_NONE { return Ok(()); diff --git a/src/fe/sys.rs b/src/fe/sys.rs index 515c366..f7e5cd6 100644 --- a/src/fe/sys.rs +++ b/src/fe/sys.rs @@ -454,7 +454,7 @@ impl fmt::Debug for DtvStats { } FE_SCALE_RELATIVE => { s.field(FIELD_SCALE, &"FE_SCALE_RELATIVE"); - s.field(FIELD_VALUE, &{(self.value as u64) * 100 / 65535}); + s.field(FIELD_VALUE, &{self.value as u64}); } FE_SCALE_COUNTER => { s.field(FIELD_SCALE, &"FE_SCALE_COUNTER"); @@ -729,29 +729,6 @@ impl DtvProperty { result: 0, } } - - pub fn get_stats_counter(&self) -> Option { - match self.cmd { - DTV_STAT_PRE_ERROR_BIT_COUNT | - DTV_STAT_PRE_TOTAL_BIT_COUNT | - DTV_STAT_POST_ERROR_BIT_COUNT | - DTV_STAT_POST_TOTAL_BIT_COUNT | - DTV_STAT_ERROR_BLOCK_COUNT | - DTV_STAT_TOTAL_BLOCK_COUNT => { - let stats = unsafe { &self.u.st }; - if stats.len > 0 { - let s = &stats.stat[0]; - if s.scale == FE_SCALE_COUNTER { - return Some(s.value as u64); - } - } - } - - _ => {} - } - - None - } }