Skip to content

Commit

Permalink
ioctl/g_ext_ctrls: also return the control index in case of error
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnurou committed Nov 15, 2023
1 parent ee74d36 commit 28a6d62
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions lib/src/ioctl/g_ext_ctrls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,37 @@ mod ioctl {
}

#[derive(Debug, Error)]
pub enum ExtControlError {
pub enum ExtControlErrorType {
#[error("Unexpected ioctl error: {0}")]
IoctlError(nix::Error),
}

impl From<ExtControlError> for Errno {
fn from(err: ExtControlError) -> Self {
impl From<ExtControlErrorType> for Errno {
fn from(err: ExtControlErrorType) -> Self {
match err {
ExtControlError::IoctlError(e) => e,
ExtControlErrorType::IoctlError(e) => e,
}
}
}

#[derive(Debug, Error)]
pub struct ExtControlError {
pub error_idx: u32,
pub error: ExtControlErrorType,
}

impl std::fmt::Display for ExtControlError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} at index {}", self.error, self.error_idx)
}
}

impl From<ExtControlError> for Errno {
fn from(err: ExtControlError) -> Self {
Self::from(err.error)
}
}

/// Safe wrapper around the `VIDIOC_G_EXT_CTRLS` to get the value of extended controls.
///
/// If successful, values for the controls will be written in the `controls` parameter.
Expand All @@ -165,7 +183,10 @@ pub fn g_ext_ctrls<I: AsV4l2ControlSlice>(
// SAFETY: the 'controls' argument is properly set up above
match unsafe { ioctl::vidioc_g_ext_ctrls(fd.as_raw_fd(), &mut v4l2_controls) } {
Ok(_) => Ok(()),
Err(e) => Err(ExtControlError::IoctlError(e)),
Err(e) => Err(ExtControlError {
error_idx: v4l2_controls.error_idx,
error: ExtControlErrorType::IoctlError(e),
}),
}
}

Expand Down Expand Up @@ -194,7 +215,10 @@ pub fn s_ext_ctrls<I: AsV4l2ControlSlice>(
// SAFETY: the 'controls' argument is properly set up above
match unsafe { ioctl::vidioc_s_ext_ctrls(fd.as_raw_fd(), &mut v4l2_controls) } {
Ok(_) => Ok(()),
Err(e) => Err(ExtControlError::IoctlError(e)),
Err(e) => Err(ExtControlError {
error_idx: v4l2_controls.error_idx,
error: ExtControlErrorType::IoctlError(e),
}),
}
}

Expand Down Expand Up @@ -223,7 +247,10 @@ pub fn try_ext_ctrls<I: AsV4l2ControlSlice>(
// SAFETY: the 'controls' argument is properly set up above
match unsafe { ioctl::vidioc_try_ext_ctrls(fd.as_raw_fd(), &mut v4l2_controls) } {
Ok(_) => Ok(()),
Err(e) => Err(ExtControlError::IoctlError(e)),
Err(e) => Err(ExtControlError {
error_idx: v4l2_controls.error_idx,
error: ExtControlErrorType::IoctlError(e),
}),
}
}

Expand Down

0 comments on commit 28a6d62

Please sign in to comment.