From 7f6eb97569dadad676248c297ff4929781a2b729 Mon Sep 17 00:00:00 2001 From: David Hoppenbrouwers Date: Thu, 8 Sep 2022 23:57:33 +0200 Subject: [PATCH] Make Request constants public Manually constructing a RawRequest is more convenient sometimes. --- usb_request/src/lib.rs | 72 ++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 44 deletions(-) diff --git a/usb_request/src/lib.rs b/usb_request/src/lib.rs index ffc00e6..d3ed2e8 100644 --- a/usb_request/src/lib.rs +++ b/usb_request/src/lib.rs @@ -3,27 +3,6 @@ pub mod descriptor; -#[allow(dead_code)] -const GET_STATUS: u8 = 0; -#[allow(dead_code)] -const CLEAR_FEATURE: u8 = 1; -#[allow(dead_code)] -const SET_FEATURE: u8 = 3; -#[allow(dead_code)] -const SET_ADDRESS: u8 = 5; -const GET_DESCRIPTOR: u8 = 6; -#[allow(dead_code)] -const SET_DESCRIPTOR: u8 = 7; -#[allow(dead_code)] -const GET_CONFIGURATION: u8 = 8; -const SET_CONFIGURATION: u8 = 9; -#[allow(dead_code)] -const GET_INTERFACE: u8 = 10; -#[allow(dead_code)] -const SET_INTERFACE: u8 = 11; -#[allow(dead_code)] -const SYNC_FRAME: u8 = 12; - #[derive(Debug)] pub enum Request { GetDescriptor { ty: descriptor::GetDescriptor }, @@ -44,44 +23,49 @@ pub struct RawRequest { } impl RawRequest { - pub fn direction_in(&self) -> bool { - self.request_type & request_type::DIR_IN != 0 - } -} - -mod request_type { pub const DIR_OUT: u8 = 0 << 7; pub const DIR_IN: u8 = 1 << 7; pub const TYPE_STANDARD: u8 = 0 << 5; - #[allow(dead_code)] pub const TYPE_CLASS: u8 = 1 << 5; - #[allow(dead_code)] pub const TYPE_VENDOR: u8 = 2 << 5; pub const RECIPIENT_DEVICE: u8 = 0; pub const RECIPIENT_INTERFACE: u8 = 1; - #[allow(dead_code)] pub const RECIPIENT_ENDPOINT: u8 = 2; - #[allow(dead_code)] pub const RECIPIENT_OTHER: u8 = 3; + + pub const GET_STATUS: u8 = 0; + pub const CLEAR_FEATURE: u8 = 1; + pub const SET_FEATURE: u8 = 3; + pub const SET_ADDRESS: u8 = 5; + pub const GET_DESCRIPTOR: u8 = 6; + pub const SET_DESCRIPTOR: u8 = 7; + pub const GET_CONFIGURATION: u8 = 8; + pub const SET_CONFIGURATION: u8 = 9; + pub const GET_INTERFACE: u8 = 10; + pub const SET_INTERFACE: u8 = 11; + pub const SYNC_FRAME: u8 = 12; + + pub fn direction_in(&self) -> bool { + self.request_type & Self::DIR_IN != 0 + } } -impl Request { - pub fn into_raw(self) -> RawRequest { - use request_type::*; +impl From for RawRequest { + fn from(r: Request) -> Self { let w_value = |ty, i| u16::from(ty) << 8 | u16::from(i); - match self { - Self::GetDescriptor { ty } => { + match r { + Request::GetDescriptor { ty } => { use descriptor::GetDescriptor::*; RawRequest { - request_type: DIR_IN - | TYPE_STANDARD + request_type: Self::DIR_IN + | Self::TYPE_STANDARD | match ty { - Device | Configuration { .. } | String { .. } => RECIPIENT_DEVICE, - Report => RECIPIENT_INTERFACE, + Device | Configuration { .. } | String { .. } => Self::RECIPIENT_DEVICE, + Report => Self::RECIPIENT_INTERFACE, }, - request: GET_DESCRIPTOR, + request: Self::GET_DESCRIPTOR, value: match ty { Device => w_value(descriptor::DEVICE, 0), Configuration { index } => w_value(descriptor::CONFIGURATION, index), @@ -91,9 +75,9 @@ impl Request { index: 0, } } - Self::SetConfiguration { value } => RawRequest { - request_type: DIR_OUT | TYPE_STANDARD | RECIPIENT_DEVICE, - request: SET_CONFIGURATION, + Request::SetConfiguration { value } => RawRequest { + request_type: Self::DIR_OUT | Self::TYPE_STANDARD | Self::RECIPIENT_DEVICE, + request: Self::SET_CONFIGURATION, value: value.into(), index: 0, },