linux_video_core/private/
helpers.rs

1use crate::{ContentType, Result};
2use std::{
3    fs::{File, OpenOptions},
4    os::unix::fs::{FileTypeExt, OpenOptionsExt},
5    path::Path,
6};
7
8/// Open device by path or name
9pub fn open(path: impl AsRef<Path>, nonblock: bool) -> Result<File> {
10    let path = path.as_ref();
11
12    #[allow(unused)]
13    let mut full_path = None;
14
15    let path = if path.is_absolute() {
16        path
17    } else {
18        full_path = Some(Path::new("dev").join(path));
19        full_path.as_ref().unwrap()
20    };
21
22    if !path.metadata()?.file_type().is_char_device() {
23        return Err(crate::utils::invalid_input("No character device"));
24    }
25
26    pub const O_NONBLOCK: i32 = 2048;
27
28    OpenOptions::new()
29        .read(true)
30        .write(true)
31        .custom_flags(if nonblock { O_NONBLOCK } else { 0 })
32        .open(path)
33}
34
35/// Check video device name prefix
36pub fn check_dev_name(name: impl AsRef<str>) -> Option<ContentType> {
37    let name = name.as_ref();
38    if name.starts_with("video") {
39        Some(ContentType::Video)
40    } else if name.starts_with("vbi") {
41        Some(ContentType::Vbi)
42    } else if name.starts_with("radio") || name.starts_with("swradio") {
43        Some(ContentType::Sdr)
44    } else {
45        None
46    }
47}