Skip to main content

linux_media/
media_interface.rs

1use std::path::PathBuf;
2
3use derive_more::{Display, From, Into};
4use linux_media_sys as media;
5use serde::{Deserialize, Serialize};
6
7use crate::media_interface_type::MediaInterfaceType;
8use crate::media_intf_devnode::MediaIntfDevnode;
9
10#[derive(
11    Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, From, Into, Display, Serialize, Deserialize,
12)]
13pub struct InterfaceId(u32);
14
15#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Serialize, Deserialize)]
16pub struct MediaInterface {
17    id: InterfaceId,
18    r#type: MediaInterfaceType,
19    devnode: MediaIntfDevnode,
20}
21
22impl MediaInterface {
23    pub fn new(id: InterfaceId, r#type: MediaInterfaceType, devnode: MediaIntfDevnode) -> Self {
24        Self {
25            id,
26            r#type,
27            devnode,
28        }
29    }
30
31    /// Get ID of this interface
32    pub fn id(&self) -> InterfaceId {
33        self.id
34    }
35
36    /// Get [media interface type][`crate::MediaInterfaceType`] of this interface
37    pub fn r#type(&self) -> MediaInterfaceType {
38        self.r#type
39    }
40
41    /// Get [devnode][`crate::MediaIntfDevnode`] of this interface
42    pub fn devnode(&self) -> MediaIntfDevnode {
43        self.devnode
44    }
45
46    /// Get the path to the charactor device constructed with:
47    /// `/sys/dev/char/{devnode.major}:{devnode.minor}`
48    pub fn path(&self) -> PathBuf {
49        self.devnode.into()
50    }
51}
52
53impl From<media::media_v2_interface> for MediaInterface {
54    fn from(intf: media::media_v2_interface) -> Self {
55        Self {
56            id: intf.id.into(),
57            r#type: intf.intf_type.try_into().unwrap(),
58            devnode: unsafe { intf.__bindgen_anon_1.devnode.into() },
59        }
60    }
61}