Skip to content

Commit 1e86250

Browse files
committed
fix: bump nusb
1 parent 99b93d3 commit 1e86250

File tree

4 files changed

+64
-44
lines changed

4 files changed

+64
-44
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ aes = "0.8"
3333
block-modes = "0.9"
3434
hmac = "0.12"
3535
sha-1 = "0.10"
36+
tokio = { version = "1", features = ["rt"] }
3637

3738
[target.'cfg(windows)'.dependencies]
3839
rusb = { version = "0.9" }
3940

4041
[target.'cfg(not(windows))'.dependencies]
4142
rusb = { version = "0.9", optional = true }
42-
nusb = { version = "0.1", optional = true }
43+
nusb = { version = "0.2", optional = true }
4344

4445
[dev-dependencies]
4546
hex = "0.4"

src/error.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#[cfg(any(feature = "rusb", target_os = "windows"))]
22
use rusb::Error as usbError;
3+
#[cfg(all(feature = "nusb", not(target_os = "windows")))]
4+
use nusb::{Error as nusbError, transfer::TransferError as nusbTransferError};
35
use std::error;
46
use std::fmt;
57
use std::io::Error as ioError;
@@ -9,6 +11,10 @@ pub enum ChallengeResponseError {
911
IOError(ioError),
1012
#[cfg(any(feature = "rusb", target_os = "windows"))]
1113
UsbError(usbError),
14+
#[cfg(all(feature = "nusb", not(target_os = "windows")))]
15+
NusbError(nusbError),
16+
#[cfg(all(feature = "nusb", not(target_os = "windows")))]
17+
NusbTransferError(nusbTransferError),
1218
CommandNotSupported,
1319
DeviceNotFound,
1420
OpenDeviceError,
@@ -25,6 +31,10 @@ impl fmt::Display for ChallengeResponseError {
2531
ChallengeResponseError::IOError(ref err) => write!(f, "IO error: {}", err),
2632
#[cfg(any(feature = "rusb", target_os = "windows"))]
2733
ChallengeResponseError::UsbError(ref err) => write!(f, "USB error: {}", err),
34+
#[cfg(all(feature = "nusb", not(target_os = "windows")))]
35+
ChallengeResponseError::NusbError(ref err) => write!(f, "NUSB error: {}", err),
36+
#[cfg(all(feature = "nusb", not(target_os = "windows")))]
37+
ChallengeResponseError::NusbTransferError(ref err) => write!(f, "NUSB transfer error: {}", err),
2838
ChallengeResponseError::DeviceNotFound => write!(f, "Device not found"),
2939
ChallengeResponseError::OpenDeviceError => write!(f, "Can not open device"),
3040
ChallengeResponseError::CommandNotSupported => write!(f, "Command Not Supported"),
@@ -42,6 +52,10 @@ impl error::Error for ChallengeResponseError {
4252
match *self {
4353
#[cfg(any(feature = "rusb", target_os = "windows"))]
4454
ChallengeResponseError::UsbError(ref err) => Some(err),
55+
#[cfg(all(feature = "nusb", not(target_os = "windows")))]
56+
ChallengeResponseError::NusbError(ref err) => Some(err),
57+
#[cfg(all(feature = "nusb", not(target_os = "windows")))]
58+
ChallengeResponseError::NusbTransferError(ref err) => Some(err),
4559
_ => None,
4660
}
4761
}
@@ -59,3 +73,17 @@ impl From<usbError> for ChallengeResponseError {
5973
ChallengeResponseError::UsbError(err)
6074
}
6175
}
76+
77+
#[cfg(all(feature = "nusb", not(target_os = "windows")))]
78+
impl From<nusbError> for ChallengeResponseError {
79+
fn from(err: nusbError) -> ChallengeResponseError {
80+
ChallengeResponseError::NusbError(err)
81+
}
82+
}
83+
84+
#[cfg(all(feature = "nusb", not(target_os = "windows")))]
85+
impl From<nusbTransferError> for ChallengeResponseError {
86+
fn from(err: nusbTransferError) -> ChallengeResponseError {
87+
ChallengeResponseError::NusbTransferError(err)
88+
}
89+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ compile_error!("Either the rusb or nusb feature must be enabled for this crate")
55

66
#[cfg(all(feature = "nusb", not(feature = "rusb"), not(target_os = "windows")))]
77
extern crate nusb;
8+
#[cfg(all(feature = "nusb", not(feature = "rusb"), not(target_os = "windows")))]
9+
extern crate tokio;
810
#[cfg(any(feature = "rusb", target_os = "windows"))]
911
extern crate rusb;
1012

src/usb/nusb.rs

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
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

35
use error::ChallengeResponseError;
46
use std::time::Duration;
57
use 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

913
impl 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

Comments
 (0)