From c4c33e86ad6b7a411ace010a32853ef1c5f9a77d Mon Sep 17 00:00:00 2001 From: Andrey Dyldin Date: Mon, 8 Mar 2021 00:04:58 +0200 Subject: [PATCH] fe: open by adapter and device number --- examples/feinfo.rs | 72 +++++++++------------------------------------ examples/femon.rs | 32 ++++++++++---------- examples/netinfo.rs | 8 +++-- src/fe/mod.rs | 22 +++++++------- 4 files changed, 45 insertions(+), 89 deletions(-) diff --git a/examples/feinfo.rs b/examples/feinfo.rs index 3900196..6c96776 100644 --- a/examples/feinfo.rs +++ b/examples/feinfo.rs @@ -1,9 +1,6 @@ use { - std::{ - path::Path, - }, - anyhow::{ + bail, Context, Result, }, @@ -15,10 +12,20 @@ use { }; -fn check_frontend(path: &Path) -> Result<()> { - println!("Frontend: {}", path.display()); +fn main() -> Result<()> { + let mut args = std::env::args().skip(1); - let fe = FeDevice::open_rd(path)?; + let adapter = match args.next() { + Some(v) => v.parse::().context("adapter number")?, + None => bail!("adapter number not defined"), + }; + + let device = match args.next() { + Some(v) => v.parse::().context("device number")?, + None => 0, + }; + + let fe = FeDevice::open(adapter, device, true)?; println!("{}", &fe); let mut status = FeStatus::default(); @@ -27,54 +34,3 @@ fn check_frontend(path: &Path) -> Result<()> { Ok(()) } - - -fn list_devices(path: &Path) -> Result<()> { - for entry in path.read_dir().context("list adapter directory")? { - let entry = entry?; - let device_path = entry.path(); - - let file_name = device_path.file_name() - .unwrap_or_default() - .to_str() - .unwrap_or_default(); - - if file_name.starts_with("frontend") { - if let Err(e) = check_frontend(&device_path) { - eprintln!("failed to get frontend info"); - for cause in e.chain() { - eprintln!("> {}", &cause); - } - } - println!(""); - } - } - - Ok(()) -} - - -fn start(fepath: Option<&str>) -> Result<()> { - if let Some(fepath) = fepath { - let path = Path::new(fepath); - check_frontend(&path)?; - } else { - let path = Path::new("/dev/dvb"); - - for entry in path.read_dir().context("list dvb directory")? { - let entry = entry?; - let adapter_path = entry.path(); - if let Err(e) = list_devices(&adapter_path) { - eprintln!("failed to list devices in {}\n{}", adapter_path.display(), e); - } - } - } - - Ok(()) -} - - -fn main() -> Result<()> { - let mut args = std::env::args().skip(1); - start(args.next().as_deref()) -} diff --git a/examples/femon.rs b/examples/femon.rs index ca348b1..9afa5fc 100644 --- a/examples/femon.rs +++ b/examples/femon.rs @@ -1,12 +1,12 @@ use { std::{ - path::Path, time::Duration, thread, }, anyhow::{ - anyhow, + bail, + Context, Result, }, @@ -17,28 +17,26 @@ use { }; -pub fn start(fepath: &str) -> Result<()> { - let fepath = Path::new(fepath); - println!("Frontend: {}", fepath.display()); +fn main() -> Result<()> { + let mut args = std::env::args().skip(1); - let fe = FeDevice::open_rd(fepath)?; + let adapter = match args.next() { + Some(v) => v.parse::().context("adapter number")?, + None => bail!("adapter number not defined"), + }; + + let device = match args.next() { + Some(v) => v.parse::().context("device number")?, + None => 0, + }; + + let fe = FeDevice::open(adapter, device, true)?; let mut status = FeStatus::default(); let delay = Duration::from_secs(1); - loop { status.read(&fe)?; println!("{}", &status); thread::sleep(delay); } } - - -fn main() -> Result<()> { - let mut args = std::env::args().skip(1); - if let Some(ref fepath) = args.next() { - start(fepath) - } else { - Err(anyhow!("Path to frontend not defined")) - } -} diff --git a/examples/netinfo.rs b/examples/netinfo.rs index 56fd425..646ffcf 100644 --- a/examples/netinfo.rs +++ b/examples/netinfo.rs @@ -1,7 +1,9 @@ -use anyhow::{Context, bail}; - use { - anyhow::Result, + anyhow::{ + bail, + Context, + Result, + }, libdvb::NetDevice, }; diff --git a/src/fe/mod.rs b/src/fe/mod.rs index b504657..2c282c8 100644 --- a/src/fe/mod.rs +++ b/src/fe/mod.rs @@ -48,6 +48,9 @@ pub use { /// A reference to the frontend device and device information #[derive(Debug)] pub struct FeDevice { + adapter: u32, + device: u32, + file: File, api_version: u16, @@ -166,15 +169,20 @@ impl FeDevice { Ok(()) } - fn open>(path: P, w: bool) -> Result { + /// Attempts to open a frontend device in read-only mode + pub fn open(adapter: u32, device: u32, readonly: bool) -> Result { + let path = format!("/dev/dvb/adapter{}/frontend{}", adapter, device); let file = OpenOptions::new() .read(true) - .write(w) + .write(!readonly) .custom_flags(::nix::libc::O_NONBLOCK) - .open(path) + .open(&path) .context("FE: open")?; let mut fe = FeDevice { + adapter, + device, + file, api_version: 0, @@ -191,14 +199,6 @@ impl FeDevice { Ok(fe) } - /// Attempts to open a frontend device in read-only mode - #[inline] - pub fn open_rd>(path: P) -> Result { Self::open(path, false) } - - /// Attempts to open a frontend device in read-write mode - #[inline] - pub fn open_rw>(path: P) -> Result { Self::open(path, true) } - fn check_properties(&self, cmdseq: &[DtvProperty]) -> Result<()> { for p in cmdseq { match p.cmd {