From ccbbf657743eb656f4e49b0204092b4450ffa7ba Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Mon, 18 Feb 2019 12:07:26 +0000 Subject: Add HidDevice trait for device-specific information MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The HID USB class needs device-specific information – currently, the subclass and the protocol of the device, but also the report descriptors and request handlers once we support these functions. Therefore, this patch introduces the HidDevice trait that provides this data. It also adds a Nitrokey struct that implements this trait for the Nitrokey Pro device. --- src/device.rs | 20 ++++++++++++++++++++ src/hid.rs | 28 ++++++++++++++-------------- src/main.rs | 5 +++-- 3 files changed, 37 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/device.rs b/src/device.rs index ea732e6..31f4293 100644 --- a/src/device.rs +++ b/src/device.rs @@ -4,9 +4,29 @@ use usb_device::bus::{UsbBus, UsbBusAllocator}; use usb_device::device::{UsbDevice, UsbDeviceBuilder, UsbVidPid}; +use crate::hid::{HidDevice, Protocol, Subclass}; + const VID_CLAY_LOGIC: u16 = 0x20a0; const PID_NITROKEY_PRO: u16 = 0x4108; +pub struct Nitrokey {} + +impl Nitrokey { + pub fn new() -> Self { + Nitrokey {} + } +} + +impl HidDevice for Nitrokey { + fn subclass(&self) -> Subclass { + Subclass::BootInterface + } + + fn protocol(&self) -> Protocol { + Protocol::Keyboard + } +} + pub fn create_usb_device(alloc: &UsbBusAllocator) -> UsbDevice<'_, B> { UsbDeviceBuilder::new(alloc, UsbVidPid(VID_CLAY_LOGIC, PID_NITROKEY_PRO)) .manufacturer("Nitrokey/ntw") diff --git a/src/hid.rs b/src/hid.rs index 9de68d6..0bb8a11 100644 --- a/src/hid.rs +++ b/src/hid.rs @@ -26,31 +26,31 @@ enum_u8! { } } -pub struct HidClass<'a, B: UsbBus> { +pub trait HidDevice { + fn subclass(&self) -> Subclass; + + fn protocol(&self) -> Protocol; +} + +pub struct HidClass<'a, B: UsbBus, D: HidDevice> { + device: D, interface: InterfaceNumber, endpoint_interrupt_in: EndpointIn<'a, B>, expect_interrupt_in_complete: bool, - subclass: Subclass, - protocol: Protocol, } -impl HidClass<'_, B> { - pub fn new( - alloc: &UsbBusAllocator, - subclass: Subclass, - protocol: Protocol, - ) -> HidClass<'_, B> { +impl HidClass<'_, B, D> { + pub fn new(device: D, alloc: &UsbBusAllocator) -> HidClass<'_, B, D> { HidClass { + device, interface: alloc.interface(), endpoint_interrupt_in: alloc.interrupt(8, 10), expect_interrupt_in_complete: false, - subclass, - protocol, } } } -impl UsbClass for HidClass<'_, B> { +impl UsbClass for HidClass<'_, B, D> { fn poll(&mut self) {} fn reset(&mut self) { @@ -61,8 +61,8 @@ impl UsbClass for HidClass<'_, B> { writer.interface( self.interface, INTERFACE_CLASS_HID, - self.subclass.into(), - self.protocol.into(), + self.device.subclass().into(), + self.device.protocol().into(), )?; writer.endpoint(&self.endpoint_interrupt_in)?; diff --git a/src/main.rs b/src/main.rs index 3857967..e10f4d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,8 @@ use hal::prelude::*; use stm32f103xx_usb::UsbBus; use usb_device::class::UsbClass; -use crate::hid::{HidClass, Protocol, Subclass}; +use crate::device::Nitrokey; +use crate::hid::HidClass; #[entry] fn main() -> ! { @@ -38,7 +39,7 @@ fn main() -> ! { let mut gpioa = p.GPIOA.split(&mut rcc.apb2); let usb_bus = UsbBus::usb_with_reset(p.USB, &mut rcc.apb1, &clocks, &mut gpioa.crh, gpioa.pa12); - let mut usb_class = HidClass::new(&usb_bus, Subclass::BootInterface, Protocol::Keyboard); + let mut usb_class = HidClass::new(Nitrokey::new(), &usb_bus); let mut usb_dev = device::create_usb_device(&usb_bus); usb_dev.force_reset().expect("USB reset failed"); -- cgit v1.2.3