You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I need to tell the difference between devices using their media device info. I wrote the following, but it'd be great to see something similar in the library:
use std::{
ffi::{c_char,CStr},
os::fd::AsRawFd,
path::Path,};use nix::ioctl_readwrite;/// A struct that contains information about some Linux media device.////// See: https://docs.kernel.org/userspace-api/media/mediactl/media-ioc-device-info.html#[repr(C)]pub(super)structMediaDeviceInfo{driver:[c_char;16],model:[c_char;32],serial:[c_char;40],bus_info:[c_char;32],media_version:u32,hw_revision:u32,driver_version:u32,reserved:[u32;31],}constMEDIA_IOC_DEVICE_INFO:u8 = 0x00;constIOCTL_MEDIA_COMMAND:u8 = b'|';// call `media_ioc_device_info` to execute the `ioctl`ioctl_readwrite!(
media_ioc_device_info,IOCTL_MEDIA_COMMAND,MEDIA_IOC_DEVICE_INFO,MediaDeviceInfo);implMediaDeviceInfo{/// Attempts to get information about the media device at the given path.pubfnget(path:&Path) -> Result<Self, std::io::Error>{// create an uninitialized MediaDeviceInfo//// SAFETY: This is fine since the kernel will write to this zeroed memory.letmut info = unsafe{ std::mem::zeroed::<MediaDeviceInfo>()};// grab the file descriptorlet file = std::fs::File::open(path)?;let fd = file.as_raw_fd();// perform the ioctl//// SAFETY: The kernel will either write to this or fail to do so.//// If it does fail, we return using the question mark operator.unsafe{media_ioc_device_info(fd,&mut info)?;}Ok(info)}pubfnmodel(&self) -> String{unsafe{CStr::from_ptr(self.model.as_ptr()).to_str().unwrap_or_else(|_| {
tracing::error!("`ioctl` to get capture device model contained invalid UTF-8");"Model was not valid UTF-8"}).to_string()}}pubfnserial(&self) -> String{unsafe{CStr::from_ptr(self.serial.as_ptr()).to_str().unwrap_or_else(|_| {
tracing::error!("`ioctl` to get capture device serial contained invalid UTF-8");"Serial was not valid UTF-8"}).to_string()}}}#[cfg(test)]mod tests {use std::path::PathBuf;usesuper::*;#[test]fntest_media_device_info(){let info = MediaDeviceInfo::get(&PathBuf::from("/dev/media0")).unwrap();assert_eq!(String::from("C922 Pro Stream Webcam"), info.model());}}
I need to tell the difference between devices using their media device info. I wrote the following, but it'd be great to see something similar in the library:
https://docs.kernel.org/userspace-api/media/mediactl/media-ioc-device-info.html#c.MC.MEDIA_IOC_DEVICE_INFO
The text was updated successfully, but these errors were encountered: