Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ioctl VIDIOC_S_CTRL #13

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub trait EncoderState {}

pub struct Encoder<S: EncoderState> {
// Make sure to keep the device alive as long as we are.
device: Arc<Device>,
pub device: Arc<Device>,
state: S,
}

Expand Down
2 changes: 2 additions & 0 deletions lib/src/ioctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod qbuf;
mod querybuf;
mod querycap;
mod reqbufs;
mod s_ctrl;
mod streamon;
mod subscribe_event;

Expand All @@ -34,6 +35,7 @@ pub use qbuf::*;
pub use querybuf::*;
pub use querycap::*;
pub use reqbufs::*;
pub use s_ctrl::*;
pub use streamon::*;
pub use subscribe_event::*;

Expand Down
79 changes: 79 additions & 0 deletions lib/src/ioctl/s_ctrl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use std::convert::From;
use std::os::unix::io::AsRawFd;

use nix::{self, errno::Errno, Error};
use thiserror::Error;

use crate::bindings;

/// Implementors can receive the result from the `s_ctrl` ioctl.
pub trait Ctrl: Sized {
fn from_v4l2_control(v4l2_control: bindings::v4l2_control) -> Self;
}

/// Information for a V4L2 control. Safe variant of `struct v4l2_control`.
#[derive(Debug, Clone)]
pub struct Control {
v4l2_control: bindings::v4l2_control,
}

impl Control {
pub fn id(&self) -> u32 {
self.v4l2_control.id
}

pub fn value(&self) -> i32 {
self.v4l2_control.value
}
}

impl Ctrl for Control {
fn from_v4l2_control(v4l2_control: bindings::v4l2_control) -> Self {
Self { v4l2_control }
}
}

#[derive(Debug, Error)]
pub enum SCtrlError {
#[error("Invalid v4l2_control ID")]
InvalidId,
#[error("The v4l2_control valueis out of bounds")]
ValueOutOfBounds,
#[error("Device currently busy")]
DeviceBusy,
#[error("Attempted to set a read only control")]
ReadOnly,
#[error("Unexpected ioctl error: {0}")]
IoctlError(nix::Error),
}

impl From<Error> for SCtrlError {
fn from(error: Error) -> Self {
match error {
Errno::EINVAL => Self::InvalidId,
Errno::ERANGE => Self::ValueOutOfBounds,
Errno::EBUSY => Self::DeviceBusy,
Errno::EACCES => Self::ReadOnly,
error => Self::IoctlError(error),
}
}
}

pub type SCtrlResult<T> = Result<T, SCtrlError>;

#[doc(hidden)]
mod ioctl {
use crate::bindings::v4l2_control;
nix::ioctl_readwrite!(vidioc_s_ctrl, b'V', 28, v4l2_control);
}

/// Safe wrapper around the `VIDIOC_S_CTRL` ioctl.
pub fn s_ctrl<T: Ctrl + std::fmt::Debug, F: AsRawFd>(
fd: &mut F,
id: u32,
value: i32,
) -> Result<T, SCtrlError> {
let mut ctrl = bindings::v4l2_control { id, value };
unsafe { ioctl::vidioc_s_ctrl(fd.as_raw_fd(), &mut ctrl)? };
Ok(T::from_v4l2_control(ctrl))
}