1- use nusb:: { Device as NUSBDevice , Interface } ;
1+ use nusb:: { transfer:: { ControlIn , ControlOut , ControlType , Recipient } , Device as NUSBDevice , Interface } ;
2+ use std:: future:: IntoFuture ;
3+ use tokio:: runtime:: Runtime ;
24
35use error:: ChallengeResponseError ;
46use std:: time:: Duration ;
57use usb:: { Backend , Device , HID_GET_REPORT , HID_SET_REPORT , PRODUCT_ID , REPORT_TYPE_FEATURE , VENDOR_ID } ;
68
7- pub struct NUSBBackend { }
9+ pub struct NUSBBackend {
10+ rt : Runtime ,
11+ }
812
913impl Backend < NUSBDevice , Interface > for NUSBBackend {
1014 fn new ( ) -> Result < Self , ChallengeResponseError > {
11- Ok ( Self { } )
15+ let rt = tokio:: runtime:: Builder :: new_current_thread ( ) . enable_all ( ) . build ( ) ?;
16+ Ok ( Self { rt } )
1217 }
1318
1419 fn open_device (
1520 & mut self ,
1621 bus_id : u8 ,
1722 address_id : u8 ,
1823 ) -> Result < ( NUSBDevice , Vec < Interface > ) , ChallengeResponseError > {
19- let nusb_devices = match nusb:: list_devices ( ) {
20- Ok ( d) => d,
21- Err ( e) => return Err ( e. into ( ) ) ,
22- } ;
24+ let nusb_devices = self . rt . block_on ( nusb:: list_devices ( ) . into_future ( ) ) ?;
2325 for device_info in nusb_devices {
24- if device_info. bus_number ( ) != bus_id || device_info. device_address ( ) != address_id {
26+ if device_info. busnum ( ) != bus_id || device_info. device_address ( ) != address_id {
2527 continue ;
2628 }
2729
28- let device = match device_info. open ( ) {
29- Ok ( d) => d,
30- Err ( _) => {
31- return Err ( ChallengeResponseError :: OpenDeviceError ) ;
32- }
33- } ;
30+ let device = self . rt . block_on ( device_info. open ( ) . into_future ( ) ) ?;
3431
3532 let mut interfaces: Vec < Interface > = Vec :: new ( ) ;
3633 for interface in device_info. interfaces ( ) {
37- let interface = match device. detach_and_claim_interface ( interface. interface_number ( ) ) {
34+ let interface = match self . rt . block_on ( device. detach_and_claim_interface ( interface. interface_number ( ) ) . into_future ( ) ) {
3835 Ok ( interface) => interface,
3936 Err ( _) => continue ,
4037 } ;
@@ -58,41 +55,32 @@ impl Backend<NUSBDevice, Interface> for NUSBBackend {
5855 fn read ( & self , handle : & mut NUSBDevice , buf : & mut [ u8 ] ) -> Result < usize , ChallengeResponseError > {
5956 assert_eq ! ( buf. len( ) , 8 ) ;
6057
61- let control_type = nusb:: transfer:: ControlType :: Class ;
62- let control_in = nusb:: transfer:: Control {
63- control_type,
64- recipient : nusb:: transfer:: Recipient :: Interface ,
58+ let control_in = ControlIn {
59+ control_type : ControlType :: Class ,
60+ recipient : Recipient :: Interface ,
6561 request : HID_GET_REPORT ,
6662 value : REPORT_TYPE_FEATURE << 8 ,
6763 index : 0 ,
64+ length : buf. len ( ) as u16 ,
6865 } ;
6966
70- match handle. control_in_blocking ( control_in, buf, Duration :: new ( 2 , 0 ) ) {
71- Ok ( r) => Ok ( r) ,
72- Err ( _e) => Err ( ChallengeResponseError :: CanNotReadFromDevice ) ,
73- }
67+ let data = self . rt . block_on ( handle. control_in ( control_in, Duration :: new ( 2 , 0 ) ) . into_future ( ) ) ?;
68+ buf. copy_from_slice ( & data) ;
69+ Ok ( data. len ( ) )
7470 }
7571
7672 fn raw_write ( & self , handle : & mut NUSBDevice , packet : & [ u8 ] ) -> Result < ( ) , ChallengeResponseError > {
77- let control_type = nusb:: transfer:: ControlType :: Class ;
78- let control_out = nusb:: transfer:: Control {
79- control_type,
80- recipient : nusb:: transfer:: Recipient :: Interface ,
73+ let control_out = ControlOut {
74+ control_type : ControlType :: Class ,
75+ recipient : Recipient :: Interface ,
8176 request : HID_SET_REPORT ,
8277 value : REPORT_TYPE_FEATURE << 8 ,
8378 index : 0 ,
79+ data : packet,
8480 } ;
8581
86- match handle. control_out_blocking ( control_out, packet, Duration :: new ( 2 , 0 ) ) {
87- Ok ( bytes_written) => {
88- if bytes_written != 8 {
89- Err ( ChallengeResponseError :: CanNotWriteToDevice )
90- } else {
91- Ok ( ( ) )
92- }
93- }
94- Err ( _) => Err ( ChallengeResponseError :: CanNotWriteToDevice ) ,
95- }
82+ self . rt . block_on ( handle. control_out ( control_out, Duration :: new ( 2 , 0 ) ) . into_future ( ) ) ?;
83+ Ok ( ( ) )
9684 }
9785
9886 fn find_device ( & mut self ) -> Result < Device , ChallengeResponseError > {
@@ -109,7 +97,7 @@ impl Backend<NUSBDevice, Interface> for NUSBBackend {
10997 }
11098
11199 fn find_device_from_serial ( & mut self , serial : u32 ) -> Result < Device , ChallengeResponseError > {
112- let nusb_devices = nusb:: list_devices ( ) ?;
100+ let nusb_devices = self . rt . block_on ( nusb:: list_devices ( ) . into_future ( ) ) ?;
113101 for device_info in nusb_devices {
114102 let product_id = device_info. product_id ( ) ;
115103 let vendor_id = device_info. vendor_id ( ) ;
@@ -119,7 +107,7 @@ impl Backend<NUSBDevice, Interface> for NUSBBackend {
119107 }
120108
121109 let device_serial =
122- match self . read_serial_from_device ( device_info. bus_number ( ) , device_info. device_address ( ) ) {
110+ match self . read_serial_from_device ( device_info. busnum ( ) , device_info. device_address ( ) ) {
123111 Ok ( s) => s,
124112 Err ( _) => continue ,
125113 } ;
@@ -133,7 +121,7 @@ impl Backend<NUSBDevice, Interface> for NUSBBackend {
133121 serial : Some ( serial) ,
134122 product_id,
135123 vendor_id,
136- bus_id : device_info. bus_number ( ) ,
124+ bus_id : device_info. busnum ( ) ,
137125 address_id : device_info. device_address ( ) ,
138126 } ) ;
139127 }
@@ -143,7 +131,7 @@ impl Backend<NUSBDevice, Interface> for NUSBBackend {
143131
144132 fn find_all_devices ( & mut self ) -> Result < Vec < Device > , ChallengeResponseError > {
145133 let mut devices: Vec < Device > = Vec :: new ( ) ;
146- let nusb_devices = nusb:: list_devices ( ) ?;
134+ let nusb_devices = self . rt . block_on ( nusb:: list_devices ( ) . into_future ( ) ) ?;
147135 for device_info in nusb_devices {
148136 let product_id = device_info. product_id ( ) ;
149137 let vendor_id = device_info. vendor_id ( ) ;
@@ -153,7 +141,7 @@ impl Backend<NUSBDevice, Interface> for NUSBBackend {
153141 }
154142
155143 let device_serial = self
156- . read_serial_from_device ( device_info. bus_number ( ) , device_info. device_address ( ) )
144+ . read_serial_from_device ( device_info. busnum ( ) , device_info. device_address ( ) )
157145 . ok ( ) ;
158146
159147 devices. push ( Device {
@@ -164,10 +152,11 @@ impl Backend<NUSBDevice, Interface> for NUSBBackend {
164152 serial : device_serial,
165153 product_id,
166154 vendor_id,
167- bus_id : device_info. bus_number ( ) ,
155+ bus_id : device_info. busnum ( ) ,
168156 address_id : device_info. device_address ( ) ,
169157 } ) ;
170158 }
171159 Ok ( devices)
172160 }
173161}
162+
0 commit comments