Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
nickray committed Jan 24, 2023
1 parent 51d2d8e commit 2f29e01
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 127 deletions.
113 changes: 69 additions & 44 deletions src/class.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// use core::convert::TryInto as _;
// use core::convert::TryFrom as _;

use interchange::Requester;
use embedded_time::duration::Extensions;
use interchange::Requester;

use crate::{
types::Status,
constants::{INTERRUPT_POLL_MILLISECONDS, PACKET_SIZE},
pipe::Pipe,
types::Status,
};

use ctaphid_dispatch::types::HidInterchange;
Expand All @@ -16,7 +16,7 @@ use usb_device::{
bus::{InterfaceNumber, UsbBus, UsbBusAllocator},
class::{ControlIn, ControlOut, UsbClass},
control,
descriptor::{DescriptorWriter},
descriptor::DescriptorWriter,
endpoint::{EndpointAddress, EndpointIn, EndpointOut},
Result as UsbResult,
};
Expand All @@ -29,19 +29,26 @@ pub struct CtapHid<'alloc, Bus: UsbBus> {

impl<'alloc, Bus> CtapHid<'alloc, Bus>
where
Bus: UsbBus
Bus: UsbBus,
{
pub fn new(allocate: &'alloc UsbBusAllocator<Bus>, interchange: Requester<HidInterchange>, initial_milliseconds: u32)
-> Self
{
pub fn new(
allocate: &'alloc UsbBusAllocator<Bus>,
interchange: Requester<HidInterchange>,
initial_milliseconds: u32,
) -> Self {
// 64 bytes, interrupt endpoint polled every 5 milliseconds
let read_endpoint: EndpointOut<'alloc, Bus> =
allocate.interrupt(PACKET_SIZE as u16, INTERRUPT_POLL_MILLISECONDS);
// 64 bytes, interrupt endpoint polled every 5 milliseconds
let write_endpoint: EndpointIn<'alloc, Bus> =
allocate.interrupt(PACKET_SIZE as u16, INTERRUPT_POLL_MILLISECONDS);

let pipe = Pipe::new(read_endpoint, write_endpoint, interchange, initial_milliseconds);
let pipe = Pipe::new(
read_endpoint,
write_endpoint,
interchange,
initial_milliseconds,
);

Self {
interface: allocate.interface(),
Expand Down Expand Up @@ -104,7 +111,6 @@ where
Status::Idle
}
}

}

const HID_INTERFACE_CLASS: u8 = 0x03;
Expand All @@ -124,35 +130,49 @@ const HID_REPORT_DESCRIPTOR: u8 = 0x22;
const FIDO_HID_REPORT_DESCRIPTOR_LENGTH: usize = 34;
const FIDO_HID_REPORT_DESCRIPTOR: [u8; FIDO_HID_REPORT_DESCRIPTOR_LENGTH] = [
// Usage page (vendor defined): 0xF1D0 (FIDO_USAGE_PAGE)
0x06, 0xD0, 0xF1,
0x06,
0xD0,
0xF1,
// Usage ID (vendor defined): 0x1 (FIDO_USAGE_CTAPHID)
0x09, 0x01,

0x09,
0x01,
// Collection (application)
0xA1, 0x01,

// The Input report
0x09, 0x03, // Usage ID - vendor defined: FIDO_USAGE_DATA_IN
0x15, 0x00, // Logical Minimum (0)
0x26, 0xFF, 0x00, // Logical Maximum (255)
0x75, 0x08, // Report Size (8 bits)
0x95, PACKET_SIZE as u8, // Report Count (64 fields)
0x81, 0x08, // Input (Data, Variable, Absolute)

// The Output report
0x09, 0x04, // Usage ID - vendor defined: FIDO_USAGE_DATA_OUT
0x15, 0x00, // Logical Minimum (0)
0x26, 0xFF, 0x00, // Logical Maximum (255)
0x75, 0x08, // Report Size (8 bits)
0x95, PACKET_SIZE as u8, // Report Count (64 fields)
0x91, 0x08, // Output (Data, Variable, Absolute)

0xA1,
0x01,
// The Input report
0x09,
0x03, // Usage ID - vendor defined: FIDO_USAGE_DATA_IN
0x15,
0x00, // Logical Minimum (0)
0x26,
0xFF,
0x00, // Logical Maximum (255)
0x75,
0x08, // Report Size (8 bits)
0x95,
PACKET_SIZE as u8, // Report Count (64 fields)
0x81,
0x08, // Input (Data, Variable, Absolute)
// The Output report
0x09,
0x04, // Usage ID - vendor defined: FIDO_USAGE_DATA_OUT
0x15,
0x00, // Logical Minimum (0)
0x26,
0xFF,
0x00, // Logical Maximum (255)
0x75,
0x08, // Report Size (8 bits)
0x95,
PACKET_SIZE as u8, // Report Count (64 fields)
0x91,
0x08, // Output (Data, Variable, Absolute)
// EndCollection
0xC0,
];

// see hid1_11.pdf, section 7.2, p. 50
#[derive(Copy,Clone,Eq,Debug,PartialEq)]
#[derive(Copy, Clone, Eq, Debug, PartialEq)]
pub enum ClassRequests {
/// mandatory, allow host to receive report via control pipe.
/// intention: initialization
Expand All @@ -167,10 +187,10 @@ pub enum ClassRequests {
}

impl<'alloc, Bus> UsbClass<Bus> for CtapHid<'alloc, Bus>
where Bus: UsbBus
where
Bus: UsbBus,
{
fn get_configuration_descriptors(&self, writer: &mut DescriptorWriter) -> UsbResult<()> {

writer.interface(
self.interface,
HID_INTERFACE_CLASS,
Expand All @@ -179,13 +199,18 @@ where Bus: UsbBus
)?;

// little-endian integers
writer.write(HID_DESCRIPTOR, &[
0x11, 0x01, // bcdHID (le)
0x00, // country code: universal
0x01, // number of HID report descriptors
HID_REPORT_DESCRIPTOR, // 1st HID report descriptor type
FIDO_HID_REPORT_DESCRIPTOR_LENGTH as u8, 0x00, // 1st HID report descriptor length in bytes as u16-be
])?;
writer.write(
HID_DESCRIPTOR,
&[
0x11,
0x01, // bcdHID (le)
0x00, // country code: universal
0x01, // number of HID report descriptors
HID_REPORT_DESCRIPTOR, // 1st HID report descriptor type
FIDO_HID_REPORT_DESCRIPTOR_LENGTH as u8,
0x00, // 1st HID report descriptor length in bytes as u16-be
],
)?;

writer.endpoint(self.pipe.read_endpoint())?;
writer.endpoint(self.pipe.write_endpoint())?;
Expand Down Expand Up @@ -232,7 +257,7 @@ where Bus: UsbBus
// while its current report remains unchanged
r if r == ClassRequests::SetIdle as u8 => {
xfer.accept().ok();
},
}
_ => (),
};
}
Expand All @@ -249,15 +274,15 @@ where Bus: UsbBus
// wValue: 0x2200,
// wIndex: 0x0,
// wLength: 0x22, (34 bytes)
if req.request == control::Request::GET_DESCRIPTOR {
if req.request == control::Request::GET_DESCRIPTOR {
xfer.accept(|data| {
assert!(data.len() >= FIDO_HID_REPORT_DESCRIPTOR_LENGTH);
data[..FIDO_HID_REPORT_DESCRIPTOR_LENGTH]
.copy_from_slice(&FIDO_HID_REPORT_DESCRIPTOR);
Ok(FIDO_HID_REPORT_DESCRIPTOR_LENGTH)
}).ok();
})
.ok();
}
}
}

}
1 change: 0 additions & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

pub const INTERRUPT_POLL_MILLISECONDS: u8 = 5;

pub const PACKET_SIZE: usize = 64;
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ generate_macros!();

// pub mod authenticator;

pub mod constants;
pub mod class;
pub mod constants;
pub use class::CtapHid;
pub mod pipe;
pub mod types;
Expand All @@ -29,4 +29,3 @@ pub struct Version {
pub minor: u8,
pub build: u8,
}

Loading

0 comments on commit 2f29e01

Please sign in to comment.