11use error:: ChallengeResponseError ;
2- use rusb:: {
3- request_type, Context , Device as RUSBDevice , DeviceHandle , Direction , Recipient , RequestType , UsbContext ,
4- } ;
5- use sec:: crc16;
2+ use rusb:: { request_type, Context , DeviceHandle , Direction , Recipient , RequestType , UsbContext } ;
63use std:: time:: Duration ;
7- use usb:: {
8- Backend , Command , Device , Flags , Frame , CHALLENGE_SIZE , HID_GET_REPORT , HID_SET_REPORT , PRODUCT_ID ,
9- REPORT_TYPE_FEATURE , RESPONSE_SIZE , STATUS_UPDATE_PAYLOAD_SIZE , VENDOR_ID ,
10- } ;
4+ use usb:: { Backend , Device , HID_GET_REPORT , HID_SET_REPORT , PRODUCT_ID , REPORT_TYPE_FEATURE , VENDOR_ID } ;
115
126pub struct RUSBBackend {
137 context : Context ,
@@ -144,7 +138,9 @@ impl Backend<DeviceHandle<Context>, u8> for RUSBBackend {
144138 }
145139
146140 let name = device. open ( ) ?. read_product_string_ascii ( & descr) . ok ( ) ;
147- let serial = self . read_serial_from_device ( device. clone ( ) ) . ok ( ) ;
141+ let serial = self
142+ . read_serial_from_device ( device. bus_number ( ) , device. address ( ) )
143+ . ok ( ) ;
148144 let device = Device {
149145 name,
150146 serial,
@@ -174,7 +170,10 @@ impl Backend<DeviceHandle<Context>, u8> for RUSBBackend {
174170 }
175171
176172 let name = device. open ( ) ?. read_product_string_ascii ( & descr) . ok ( ) ;
177- let fetched_serial = match self . read_serial_from_device ( device. clone ( ) ) . ok ( ) {
173+ let fetched_serial = match self
174+ . read_serial_from_device ( device. bus_number ( ) , device. address ( ) )
175+ . ok ( )
176+ {
178177 Some ( s) => s,
179178 None => 0 ,
180179 } ;
@@ -210,7 +209,9 @@ impl Backend<DeviceHandle<Context>, u8> for RUSBBackend {
210209 }
211210
212211 let name = device. open ( ) ?. read_product_string_ascii ( & descr) . ok ( ) ;
213- let serial = self . read_serial_from_device ( device. clone ( ) ) . ok ( ) ;
212+ let serial = self
213+ . read_serial_from_device ( device. bus_number ( ) , device. address ( ) )
214+ . ok ( ) ;
214215 let device = Device {
215216 name,
216217 serial,
@@ -229,31 +230,3 @@ impl Backend<DeviceHandle<Context>, u8> for RUSBBackend {
229230 Err ( ChallengeResponseError :: DeviceNotFound )
230231 }
231232}
232-
233- impl RUSBBackend {
234- fn read_serial_from_device ( & mut self , device : RUSBDevice < Context > ) -> Result < u32 , ChallengeResponseError > {
235- let ( mut handle, interfaces) = self . open_device ( device. bus_number ( ) , device. address ( ) ) ?;
236- let challenge = [ 0 ; CHALLENGE_SIZE ] ;
237- let command = Command :: DeviceSerial ;
238-
239- let d = Frame :: new ( challenge, command) ; // FixMe: do not need a challange
240- let mut buf = [ 0 ; STATUS_UPDATE_PAYLOAD_SIZE ] ;
241- self . wait ( & mut handle, |f| !f. contains ( Flags :: SLOT_WRITE_FLAG ) , & mut buf) ?;
242-
243- self . write_frame ( & mut handle, & d) ?;
244-
245- // Read the response.
246- let mut response = [ 0 ; RESPONSE_SIZE ] ;
247- self . read_response ( & mut handle, & mut response) ?;
248- self . close_device ( handle, interfaces) ?;
249-
250- // Check response.
251- if crc16 ( & response[ ..6 ] ) != crate :: sec:: CRC_RESIDUAL_OK {
252- return Err ( ChallengeResponseError :: WrongCRC ) ;
253- }
254-
255- let serial = structure ! ( "2I" ) . unpack ( response[ ..8 ] . to_vec ( ) ) ?;
256-
257- Ok ( serial. 0 )
258- }
259- }
0 commit comments