11// use core::convert::TryInto as _;
22// use core::convert::TryFrom as _;
33
4- use interchange:: Requester ;
54use embedded_time:: duration:: Extensions ;
5+ use interchange:: Requester ;
66
77use crate :: {
8- types:: Status ,
98 constants:: { INTERRUPT_POLL_MILLISECONDS , PACKET_SIZE } ,
109 pipe:: Pipe ,
10+ types:: Status ,
1111} ;
1212
1313use ctaphid_dispatch:: types:: HidInterchange ;
@@ -16,7 +16,7 @@ use usb_device::{
1616 bus:: { InterfaceNumber , UsbBus , UsbBusAllocator } ,
1717 class:: { ControlIn , ControlOut , UsbClass } ,
1818 control,
19- descriptor:: { DescriptorWriter } ,
19+ descriptor:: DescriptorWriter ,
2020 endpoint:: { EndpointAddress , EndpointIn , EndpointOut } ,
2121 Result as UsbResult ,
2222} ;
@@ -29,19 +29,26 @@ pub struct CtapHid<'alloc, Bus: UsbBus> {
2929
3030impl < ' alloc , Bus > CtapHid < ' alloc , Bus >
3131where
32- Bus : UsbBus
32+ Bus : UsbBus ,
3333{
34- pub fn new ( allocate : & ' alloc UsbBusAllocator < Bus > , interchange : Requester < HidInterchange > , initial_milliseconds : u32 )
35- -> Self
36- {
34+ pub fn new (
35+ allocate : & ' alloc UsbBusAllocator < Bus > ,
36+ interchange : Requester < HidInterchange > ,
37+ initial_milliseconds : u32 ,
38+ ) -> Self {
3739 // 64 bytes, interrupt endpoint polled every 5 milliseconds
3840 let read_endpoint: EndpointOut < ' alloc , Bus > =
3941 allocate. interrupt ( PACKET_SIZE as u16 , INTERRUPT_POLL_MILLISECONDS ) ;
4042 // 64 bytes, interrupt endpoint polled every 5 milliseconds
4143 let write_endpoint: EndpointIn < ' alloc , Bus > =
4244 allocate. interrupt ( PACKET_SIZE as u16 , INTERRUPT_POLL_MILLISECONDS ) ;
4345
44- let pipe = Pipe :: new ( read_endpoint, write_endpoint, interchange, initial_milliseconds) ;
46+ let pipe = Pipe :: new (
47+ read_endpoint,
48+ write_endpoint,
49+ interchange,
50+ initial_milliseconds,
51+ ) ;
4552
4653 Self {
4754 interface : allocate. interface ( ) ,
@@ -104,7 +111,6 @@ where
104111 Status :: Idle
105112 }
106113 }
107-
108114}
109115
110116const HID_INTERFACE_CLASS : u8 = 0x03 ;
@@ -124,35 +130,49 @@ const HID_REPORT_DESCRIPTOR: u8 = 0x22;
124130const FIDO_HID_REPORT_DESCRIPTOR_LENGTH : usize = 34 ;
125131const FIDO_HID_REPORT_DESCRIPTOR : [ u8 ; FIDO_HID_REPORT_DESCRIPTOR_LENGTH ] = [
126132 // Usage page (vendor defined): 0xF1D0 (FIDO_USAGE_PAGE)
127- 0x06 , 0xD0 , 0xF1 ,
133+ 0x06 ,
134+ 0xD0 ,
135+ 0xF1 ,
128136 // Usage ID (vendor defined): 0x1 (FIDO_USAGE_CTAPHID)
129- 0x09 , 0x01 ,
130-
137+ 0x09 ,
138+ 0x01 ,
131139 // Collection (application)
132- 0xA1 , 0x01 ,
133-
134- // The Input report
135- 0x09 , 0x03 , // Usage ID - vendor defined: FIDO_USAGE_DATA_IN
136- 0x15 , 0x00 , // Logical Minimum (0)
137- 0x26 , 0xFF , 0x00 , // Logical Maximum (255)
138- 0x75 , 0x08 , // Report Size (8 bits)
139- 0x95 , PACKET_SIZE as u8 , // Report Count (64 fields)
140- 0x81 , 0x08 , // Input (Data, Variable, Absolute)
141-
142- // The Output report
143- 0x09 , 0x04 , // Usage ID - vendor defined: FIDO_USAGE_DATA_OUT
144- 0x15 , 0x00 , // Logical Minimum (0)
145- 0x26 , 0xFF , 0x00 , // Logical Maximum (255)
146- 0x75 , 0x08 , // Report Size (8 bits)
147- 0x95 , PACKET_SIZE as u8 , // Report Count (64 fields)
148- 0x91 , 0x08 , // Output (Data, Variable, Absolute)
149-
140+ 0xA1 ,
141+ 0x01 ,
142+ // The Input report
143+ 0x09 ,
144+ 0x03 , // Usage ID - vendor defined: FIDO_USAGE_DATA_IN
145+ 0x15 ,
146+ 0x00 , // Logical Minimum (0)
147+ 0x26 ,
148+ 0xFF ,
149+ 0x00 , // Logical Maximum (255)
150+ 0x75 ,
151+ 0x08 , // Report Size (8 bits)
152+ 0x95 ,
153+ PACKET_SIZE as u8 , // Report Count (64 fields)
154+ 0x81 ,
155+ 0x08 , // Input (Data, Variable, Absolute)
156+ // The Output report
157+ 0x09 ,
158+ 0x04 , // Usage ID - vendor defined: FIDO_USAGE_DATA_OUT
159+ 0x15 ,
160+ 0x00 , // Logical Minimum (0)
161+ 0x26 ,
162+ 0xFF ,
163+ 0x00 , // Logical Maximum (255)
164+ 0x75 ,
165+ 0x08 , // Report Size (8 bits)
166+ 0x95 ,
167+ PACKET_SIZE as u8 , // Report Count (64 fields)
168+ 0x91 ,
169+ 0x08 , // Output (Data, Variable, Absolute)
150170 // EndCollection
151171 0xC0 ,
152172] ;
153173
154174// see hid1_11.pdf, section 7.2, p. 50
155- #[ derive( Copy , Clone , Eq , Debug , PartialEq ) ]
175+ #[ derive( Copy , Clone , Eq , Debug , PartialEq ) ]
156176pub enum ClassRequests {
157177 /// mandatory, allow host to receive report via control pipe.
158178 /// intention: initialization
@@ -167,10 +187,10 @@ pub enum ClassRequests {
167187}
168188
169189impl < ' alloc , Bus > UsbClass < Bus > for CtapHid < ' alloc , Bus >
170- where Bus : UsbBus
190+ where
191+ Bus : UsbBus ,
171192{
172193 fn get_configuration_descriptors ( & self , writer : & mut DescriptorWriter ) -> UsbResult < ( ) > {
173-
174194 writer. interface (
175195 self . interface ,
176196 HID_INTERFACE_CLASS ,
@@ -179,13 +199,18 @@ where Bus: UsbBus
179199 ) ?;
180200
181201 // little-endian integers
182- writer. write ( HID_DESCRIPTOR , & [
183- 0x11 , 0x01 , // bcdHID (le)
184- 0x00 , // country code: universal
185- 0x01 , // number of HID report descriptors
186- HID_REPORT_DESCRIPTOR , // 1st HID report descriptor type
187- FIDO_HID_REPORT_DESCRIPTOR_LENGTH as u8 , 0x00 , // 1st HID report descriptor length in bytes as u16-be
188- ] ) ?;
202+ writer. write (
203+ HID_DESCRIPTOR ,
204+ & [
205+ 0x11 ,
206+ 0x01 , // bcdHID (le)
207+ 0x00 , // country code: universal
208+ 0x01 , // number of HID report descriptors
209+ HID_REPORT_DESCRIPTOR , // 1st HID report descriptor type
210+ FIDO_HID_REPORT_DESCRIPTOR_LENGTH as u8 ,
211+ 0x00 , // 1st HID report descriptor length in bytes as u16-be
212+ ] ,
213+ ) ?;
189214
190215 writer. endpoint ( self . pipe . read_endpoint ( ) ) ?;
191216 writer. endpoint ( self . pipe . write_endpoint ( ) ) ?;
@@ -232,7 +257,7 @@ where Bus: UsbBus
232257 // while its current report remains unchanged
233258 r if r == ClassRequests :: SetIdle as u8 => {
234259 xfer. accept ( ) . ok ( ) ;
235- } ,
260+ }
236261 _ => ( ) ,
237262 } ;
238263 }
@@ -249,15 +274,15 @@ where Bus: UsbBus
249274 // wValue: 0x2200,
250275 // wIndex: 0x0,
251276 // wLength: 0x22, (34 bytes)
252- if req. request == control:: Request :: GET_DESCRIPTOR {
277+ if req. request == control:: Request :: GET_DESCRIPTOR {
253278 xfer. accept ( |data| {
254279 assert ! ( data. len( ) >= FIDO_HID_REPORT_DESCRIPTOR_LENGTH ) ;
255280 data[ ..FIDO_HID_REPORT_DESCRIPTOR_LENGTH ]
256281 . copy_from_slice ( & FIDO_HID_REPORT_DESCRIPTOR ) ;
257282 Ok ( FIDO_HID_REPORT_DESCRIPTOR_LENGTH )
258- } ) . ok ( ) ;
283+ } )
284+ . ok ( ) ;
259285 }
260286 }
261287 }
262-
263288}
0 commit comments