@@ -4,6 +4,7 @@ use nusb::{descriptors::language_id::US_ENGLISH, transfer::Buffer, transfer::Bul
44use std:: time:: Duration ;
55
66const ENDPOINT_NUMBER_MASK : u8 = 0x7f ;
7+ const DESC_TIMEOUT : Duration = Duration :: from_millis ( 100 ) ;
78
89fn decode_version ( version : u16 ) -> ( u8 , u8 , u8 ) {
910 let major: u8 = ( version >> 8 ) as u8 ;
@@ -63,7 +64,7 @@ pub struct UsbAlternateInterface {
6364impl UsbAlternateInterface {
6465 pub fn new ( device : & nusb:: Device , iface : nusb:: descriptors:: InterfaceDescriptor ) -> Self {
6566 let interfaceName = match iface. string_index ( ) {
66- Some ( desc_index) => Some ( device. get_string_descriptor ( desc_index, US_ENGLISH , Duration :: from_millis ( 100 ) ) . wait ( ) . unwrap ( ) ) ,
67+ Some ( desc_index) => Some ( device. get_string_descriptor ( desc_index, US_ENGLISH , DESC_TIMEOUT ) . wait ( ) . unwrap ( ) ) ,
6768 None => None ,
6869 } ;
6970
@@ -114,7 +115,7 @@ pub struct UsbConfiguration {
114115impl UsbConfiguration {
115116 pub fn new ( usb_device : & UsbDevice , device : & nusb:: Device , config : nusb:: descriptors:: ConfigurationDescriptor ) -> Self {
116117 let configurationName = match config. string_index ( ) {
117- Some ( desc_index) => Some ( device. get_string_descriptor ( desc_index, US_ENGLISH , Duration :: from_millis ( 100 ) ) . wait ( ) . unwrap ( ) ) ,
118+ Some ( desc_index) => Some ( device. get_string_descriptor ( desc_index, US_ENGLISH , DESC_TIMEOUT ) . wait ( ) . unwrap ( ) ) ,
118119 None => None ,
119120 } ;
120121
@@ -208,7 +209,7 @@ impl UsbDevice {
208209 None => self . _open ( ) . unwrap ( ) ,
209210 } ;
210211 match device. device_descriptor ( ) . manufacturer_string_index ( ) {
211- Some ( desc_index) => Some ( device. get_string_descriptor ( desc_index, US_ENGLISH , Duration :: from_millis ( 100 ) ) . wait ( ) . unwrap ( ) ) ,
212+ Some ( desc_index) => Some ( device. get_string_descriptor ( desc_index, US_ENGLISH , DESC_TIMEOUT ) . wait ( ) . unwrap ( ) ) ,
212213 None => None ,
213214 }
214215 }
@@ -225,7 +226,7 @@ impl UsbDevice {
225226 None => self . _open ( ) . unwrap ( ) ,
226227 } ;
227228 match device. device_descriptor ( ) . product_string_index ( ) {
228- Some ( desc_index) => Some ( device. get_string_descriptor ( desc_index, US_ENGLISH , Duration :: from_millis ( 100 ) ) . wait ( ) . unwrap ( ) ) ,
229+ Some ( desc_index) => Some ( device. get_string_descriptor ( desc_index, US_ENGLISH , DESC_TIMEOUT ) . wait ( ) . unwrap ( ) ) ,
229230 None => None ,
230231 }
231232 }
@@ -242,7 +243,7 @@ impl UsbDevice {
242243 None => self . _open ( ) . unwrap ( ) ,
243244 } ;
244245 match device. device_descriptor ( ) . serial_number_string_index ( ) {
245- Some ( desc_index) => Some ( device. get_string_descriptor ( desc_index, US_ENGLISH , Duration :: from_millis ( 100 ) ) . wait ( ) . unwrap ( ) ) ,
246+ Some ( desc_index) => Some ( device. get_string_descriptor ( desc_index, US_ENGLISH , DESC_TIMEOUT ) . wait ( ) . unwrap ( ) ) ,
246247 None => None ,
247248 }
248249 }
@@ -374,7 +375,7 @@ impl UsbDevice {
374375 }
375376
376377 #[ napi( js_name = "nativeControlTransferIn" ) ]
377- pub async fn controlTransferIn ( & self , setup : UsbControlTransferParameters , length : u16 ) -> Result < Option < Uint8Array > > {
378+ pub async fn controlTransferIn ( & self , setup : UsbControlTransferParameters , timeout : u32 , length : u16 ) -> Result < Option < Uint8Array > > {
378379 let control_type = match setup. requestType . as_str ( ) {
379380 "standard" => nusb:: transfer:: ControlType :: Standard ,
380381 "class" => nusb:: transfer:: ControlType :: Class ,
@@ -400,7 +401,7 @@ impl UsbDevice {
400401 index : setup. index ,
401402 length,
402403 } ,
403- Duration :: from_millis ( 100 ) ,
404+ Duration :: from_millis ( timeout as u64 ) ,
404405 )
405406 . wait ( )
406407 . map_err ( |e| napi:: Error :: from_reason ( format ! ( "controlTransferIn error: {e}" ) ) ) ?;
@@ -411,7 +412,7 @@ impl UsbDevice {
411412 }
412413
413414 #[ napi( js_name = "nativeControlTransferOut" ) ]
414- pub async fn controlTransferOut ( & self , setup : UsbControlTransferParameters , data : Option < Uint8Array > ) -> Result < u32 > {
415+ pub async fn controlTransferOut ( & self , setup : UsbControlTransferParameters , timeout : u32 , data : Option < Uint8Array > ) -> Result < u32 > {
415416 let control_type = match setup. requestType . as_str ( ) {
416417 "standard" => nusb:: transfer:: ControlType :: Standard ,
417418 "class" => nusb:: transfer:: ControlType :: Class ,
@@ -438,7 +439,7 @@ impl UsbDevice {
438439 index : setup. index ,
439440 data : & bytes,
440441 } ,
441- Duration :: from_millis ( 100 ) ,
442+ Duration :: from_millis ( timeout as u64 ) ,
442443 )
443444 . wait ( )
444445 . map_err ( |e| napi:: Error :: from_reason ( format ! ( "controlTransferOut error: {e}" ) ) ) ?;
@@ -449,13 +450,13 @@ impl UsbDevice {
449450 }
450451
451452 #[ napi( js_name = "nativeTransferIn" ) ]
452- pub async fn transferIn ( & self , endpointNumber : u8 , length : u32 ) -> Result < Option < Uint8Array > > {
453+ pub async fn transferIn ( & self , endpointNumber : u8 , timeout : u32 , length : u32 ) -> Result < Option < Uint8Array > > {
453454 match self . get_endpoint :: < nusb:: transfer:: In > ( endpointNumber) {
454455 Some ( mut endpoint) => {
455456 let packet_size = endpoint. max_packet_size ( ) ;
456457 let req = ( ( ( length as usize ) + packet_size - 1 ) / packet_size) * packet_size;
457458 let buf = Buffer :: new ( req) ;
458- let completion = endpoint. transfer_blocking ( buf, Duration :: from_millis ( 100 ) ) ;
459+ let completion = endpoint. transfer_blocking ( buf, Duration :: from_millis ( timeout as u64 ) ) ;
459460 completion. status . map_err ( |e| napi:: Error :: from_reason ( format ! ( "transferIn error: {e:?}" ) ) ) ?;
460461 let mut v = completion. buffer . into_vec ( ) ;
461462 v. truncate ( completion. actual_len . min ( length as usize ) ) ;
@@ -468,12 +469,12 @@ impl UsbDevice {
468469 }
469470
470471 #[ napi( js_name = "nativeTransferOut" ) ]
471- pub async fn transferOut ( & self , endpointNumber : u8 , data : Uint8Array ) -> Result < u32 > {
472+ pub async fn transferOut ( & self , endpointNumber : u8 , timeout : u32 , data : Uint8Array ) -> Result < u32 > {
472473 match self . get_endpoint :: < nusb:: transfer:: Out > ( endpointNumber) {
473474 Some ( mut endpoint) => {
474475 let mut buf = Buffer :: new ( data. len ( ) ) ;
475476 buf. extend_from_slice ( & data) ;
476- let completion = endpoint. transfer_blocking ( buf, Duration :: from_millis ( 100 ) ) ;
477+ let completion = endpoint. transfer_blocking ( buf, Duration :: from_millis ( timeout as u64 ) ) ;
477478 completion. status . map_err ( |e| napi:: Error :: from_reason ( format ! ( "transferOut error: {e:?}" ) ) ) ?;
478479 return Ok ( completion. actual_len as u32 ) ;
479480 }
0 commit comments