Skip to content

Commit

Permalink
Merge pull request #19 from h7x4/add-serde-impls
Browse files Browse the repository at this point in the history
Add serde support
  • Loading branch information
jiegec authored Oct 29, 2023
2 parents a2701a7 + e054293 commit 4316e61
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ tokio = { version = "1.22.0", features = ["rt", "net", "io-util", "sync"] }
log = "0.4.17"
num-traits = "0.2.15"
num-derive = "0.3.3"
rusb = "0.9.1"
rusb = "0.9.3"
serde = { version = "1.0", features = ["derive"], optional = true }

[dev-dependencies]
tokio = { version = "1.22.0", features = ["full"] }
env_logger = "0.9.0"

[features]
default = []
serde = ["dep:serde", "rusb/serde"]
5 changes: 5 additions & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::*;

/// A list of known USB speeds
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum UsbSpeed {
Unknown = 0x0,
Low,
Expand All @@ -15,6 +16,7 @@ pub enum UsbSpeed {
/// A list of defined USB class codes
// https://www.usb.org/defined-class-codes
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ClassCode {
SeeInterface = 0,
Audio,
Expand Down Expand Up @@ -42,6 +44,7 @@ pub enum ClassCode {

/// A list of defined USB endpoint attributes
#[derive(Copy, Clone, Debug, FromPrimitive)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum EndpointAttributes {
Control = 0,
Isochronous,
Expand All @@ -59,6 +62,7 @@ pub const EP0_MAX_PACKET_SIZE: u16 = 64;
/// A list of defined USB standard requests
/// from USB 2.0 standard Table 9.4. Standard Request Codes
#[derive(Copy, Clone, Debug, FromPrimitive)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum StandardRequest {
GetStatus = 0,
ClearFeature = 1,
Expand All @@ -76,6 +80,7 @@ pub enum StandardRequest {
/// A list of defined USB descriptor types
/// from USB 2.0 standard Table 9.5. Descriptor Types
#[derive(Copy, Clone, Debug, FromPrimitive)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum DescriptorType {
Device = 1,
Configuration = 2,
Expand Down
5 changes: 5 additions & 0 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::*;
use rusb::Version as rusbVersion;

#[derive(Clone, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Version {
pub major: u8,
pub minor: u8,
Expand All @@ -26,6 +27,7 @@ impl From<Version> for rusbVersion {

/// Represent a USB device
#[derive(Clone, Default)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct UsbDevice {
pub path: String,
pub bus_id: String,
Expand All @@ -41,7 +43,10 @@ pub struct UsbDevice {
pub configuration_value: u8,
pub num_configurations: u8,
pub interfaces: Vec<UsbInterface>,

#[cfg_attr(feature = "serde", serde(skip))]
pub device_handler: Option<Arc<Mutex<Box<dyn UsbDeviceHandler + Send>>>>,

pub usb_version: Version,

pub(crate) ep0_in: UsbEndpoint,
Expand Down
1 change: 1 addition & 0 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::*;

/// Represent a USB endpoint
#[derive(Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct UsbEndpoint {
/// bEndpointAddress
pub address: u8,
Expand Down
4 changes: 4 additions & 0 deletions src/hid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ use super::*;
// HID Usage Tables 1.12: https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf

#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
enum UsbHidKeyboardHandlerState {
Idle,
KeyDown,
}

/// A handler of a HID keyboard
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct UsbHidKeyboardHandler {
pub report_descriptor: Vec<u8>,
pub pending_key_events: VecDeque<UsbHidKeyboardReport>,
Expand All @@ -23,6 +25,7 @@ pub struct UsbHidKeyboardHandler {
///
/// For definition of key codes, see [HID Usage Tables](https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf)
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct UsbHidKeyboardReport {
/// Key modifier
pub modifier: u8,
Expand Down Expand Up @@ -159,6 +162,7 @@ impl UsbInterfaceHandler for UsbHidKeyboardHandler {

/// A list of defined HID descriptor type
#[derive(Copy, Clone, Debug, FromPrimitive)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum HidDescriptorType {
Hid = 0x21,
Report = 0x22,
Expand Down
3 changes: 3 additions & 0 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ use super::*;

/// Represent a USB interface
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct UsbInterface {
pub interface_class: u8,
pub interface_subclass: u8,
pub interface_protocol: u8,
pub endpoints: Vec<UsbEndpoint>,
pub string_interface: u8,
pub class_specific_descriptor: Vec<u8>,

#[cfg_attr(feature = "serde", serde(skip))]
pub handler: Arc<Mutex<Box<dyn UsbInterfaceHandler + Send>>>,
}

Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use tokio::net::TcpListener;
use tokio::sync::RwLock;
use usbip_protocol::UsbIpCommand;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

pub mod cdc;
mod consts;
mod device;
Expand Down
3 changes: 3 additions & 0 deletions src/setup.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::*;

/// Parse the SETUP packet of control transfers
#[derive(Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SetupPacket {
/// bmRequestType
pub request_type: u8,
Expand Down

0 comments on commit 4316e61

Please sign in to comment.