Skip to content

Commit

Permalink
Add serde option
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <patrickelectric@gmail.com>
  • Loading branch information
patrickelectric committed Jul 21, 2020
1 parent 11947bf commit 2201395
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ edition = "2018"
derivative = "1.0.3"
libc = "0.2"
thiserror = "1.0.9"
serde = { version = "1.0.104", features = ["derive"], optional = true }

[features]
no_wrapper = []
default = ["serde"]
17 changes: 16 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ mod v4l2;

pub type Result<T> = result::Result<T, Error>;

#[cfg(feature = "serde")]
extern crate serde;

#[macro_use]
extern crate derivative;

Expand All @@ -62,7 +65,8 @@ pub enum Error {
BadField,
}

#[derive(Debug)]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Config<'a> {
/// The mix of numerator and denominator. v4l2 uses frame intervals instead of frame rates.
/// Default is `(1, 10)`.
Expand Down Expand Up @@ -94,6 +98,7 @@ impl<'a> Default for Config<'a> {
}
}

#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct FormatInfo {
/// FourCC of format (e.g. `b"H264"`).
pub format: [u8; 4],
Expand Down Expand Up @@ -143,6 +148,7 @@ impl fmt::Debug for FormatInfo {
}
}

#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ResolutionInfo {
Discretes(Vec<(u32, u32)>),
Stepwise {
Expand Down Expand Up @@ -173,6 +179,7 @@ impl fmt::Debug for ResolutionInfo {
}
}

#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum IntervalInfo {
Discretes(Vec<(u32, u32)>),
Stepwise {
Expand Down Expand Up @@ -242,18 +249,21 @@ impl Drop for Frame {
}

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
enum State {
Idle,
Streaming,
Aborted,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Camera {
fd: RawFd,
state: State,
resolution: (u32, u32),
format: [u8; 4],
#[cfg_attr(feature = "serde", serde(skip_serializing))]
buffers: Vec<Arc<MappedRegion>>,
}

Expand Down Expand Up @@ -661,6 +671,7 @@ impl Drop for Camera {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct FormatIter<'a> {
camera: &'a Camera,
index: u32,
Expand Down Expand Up @@ -752,6 +763,7 @@ impl Settable for String {
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Control {
pub id: u32,
pub name: String,
Expand All @@ -761,6 +773,7 @@ pub struct Control {
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum CtrlData {
Integer {
value: i32,
Expand Down Expand Up @@ -807,12 +820,14 @@ pub enum CtrlData {
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct CtrlMenuItem {
pub index: u32,
pub name: String,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct CtrlIntMenuItem {
pub index: u32,
pub value: i64,
Expand Down
34 changes: 30 additions & 4 deletions src/v4l2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub struct Format {
#[cfg(target_pointer_width = "64")]
padding: u32,
pub fmt: PixFormat,
#[derivative(Debug="ignore")]
#[derivative(Debug = "ignore")]
space: [u8; 156],
}

Expand All @@ -166,6 +166,7 @@ impl Format {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(C)]
pub struct PixFormat {
pub width: u32,
Expand Down Expand Up @@ -193,6 +194,7 @@ impl PixFormat {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(C)]
pub struct RequestBuffers {
pub count: u32,
Expand Down Expand Up @@ -221,7 +223,7 @@ pub struct Buffer {
pub bytesused: u32,
pub flags: u32,
pub field: u32,
#[derivative(Debug="ignore")]
#[derivative(Debug = "ignore")]
pub timestamp: Timeval,
pub timecode: TimeCode,
pub sequence: u32,
Expand All @@ -242,6 +244,7 @@ impl Buffer {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(C)]
pub struct TimeCode {
pub ttype: u32,
Expand All @@ -254,6 +257,7 @@ pub struct TimeCode {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(C)]
pub struct FmtDesc {
pub index: u32,
Expand All @@ -278,7 +282,7 @@ impl FmtDesc {
pub struct StreamParm {
pub ptype: u32,
pub parm: CaptureParm,
#[derivative(Debug="ignore")]
#[derivative(Debug = "ignore")]
space: [u8; 160],
}

Expand All @@ -293,6 +297,7 @@ impl StreamParm {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(C)]
pub struct CaptureParm {
pub capability: u32,
Expand All @@ -304,13 +309,15 @@ pub struct CaptureParm {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(C)]
pub struct Fract {
pub numerator: u32,
pub denominator: u32,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(C)]
pub struct Frmsizeenum {
pub index: u32,
Expand All @@ -337,13 +344,15 @@ impl Frmsizeenum {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(C)]
pub struct FrmsizeDiscrete {
pub width: u32,
pub height: u32,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(C)]
pub struct FrmsizeStepwise {
pub min_width: u32,
Expand All @@ -355,6 +364,7 @@ pub struct FrmsizeStepwise {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(C)]
pub struct Frmivalenum {
pub index: u32,
Expand Down Expand Up @@ -385,6 +395,7 @@ impl Frmivalenum {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(C)]
pub struct FrmivalStepwise {
pub min: Fract,
Expand All @@ -393,6 +404,7 @@ pub struct FrmivalStepwise {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(C)]
pub struct QueryCtrl {
pub id: u32,
Expand All @@ -415,6 +427,7 @@ impl QueryCtrl {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(C)]
pub struct QueryExtCtrl {
pub id: u32,
Expand Down Expand Up @@ -442,11 +455,15 @@ impl QueryExtCtrl {
}
}

#[derive(Debug, Copy, Clone)]
#[derive(Derivative, Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(C, packed)]
pub struct QueryMenu {
pub id: u32,
pub index: u32,
//#[derivative(Debug="ignore")]
#[serde(skip_serializing)]
#[serde(skip_deserializing)]
pub data: QueryMenuData,
reserved: u32,
}
Expand All @@ -458,6 +475,12 @@ pub union QueryMenuData {
value: i64,
}

impl Default for QueryMenuData {
fn default() -> Self {
QueryMenuData { value: 0 }
}
}

impl fmt::Debug for QueryMenuData {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.name())
Expand All @@ -483,6 +506,7 @@ impl QueryMenuData {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(C)]
pub struct Control {
pub id: u32,
Expand All @@ -496,6 +520,7 @@ impl Control {
}

#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(C, packed)]
pub struct ExtControl {
pub id: u32,
Expand All @@ -516,6 +541,7 @@ impl ExtControl {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct ExtControls<'a> {
pub ctrl_class: u32,
Expand Down

0 comments on commit 2201395

Please sign in to comment.