Implement block device
This commit is contained in:
parent
dd2563a11b
commit
20b4389459
2 changed files with 34 additions and 9 deletions
|
|
@ -6,10 +6,11 @@ A Rust library to run a USB/IP server to simulate USB devices.
|
|||
|
||||
## How to use
|
||||
|
||||
See examples directory. Two examples are provided:
|
||||
See examples directory. Three examples are provided:
|
||||
|
||||
1. hid_keyboard.rs: simulate a hid keyboard that types something every second.
|
||||
2. cdc_acm_serial.rs : simulate a serial that gets a character every second.
|
||||
1. hid_keyboard: Simulate a hid keyboard that types something every second.
|
||||
2. cdc_acm_serial: Simulate a serial that gets a character every second.
|
||||
3. host: Act like original usb/ip sharing server, sharing one device from one machine to another. Also supports sharing from macOS to Linux!
|
||||
|
||||
To run example, run:
|
||||
|
||||
|
|
|
|||
36
src/host.rs
36
src/host.rs
|
|
@ -32,18 +32,28 @@ impl UsbInterfaceHandler for UsbHostHandler {
|
|||
// control
|
||||
if let Direction::In = ep.direction() {
|
||||
// control in
|
||||
let len = self
|
||||
.handle
|
||||
.read_control(
|
||||
if let Ok(len) = self.handle.read_control(
|
||||
setup.request_type,
|
||||
setup.request,
|
||||
setup.value,
|
||||
setup.index,
|
||||
&mut buffer,
|
||||
timeout,
|
||||
) {
|
||||
return Ok(Vec::from(&buffer[..len]));
|
||||
}
|
||||
} else {
|
||||
// control out
|
||||
self.handle
|
||||
.write_control(
|
||||
setup.request_type,
|
||||
setup.request,
|
||||
setup.value,
|
||||
setup.index,
|
||||
&mut buffer,
|
||||
req,
|
||||
timeout,
|
||||
)
|
||||
.unwrap();
|
||||
return Ok(Vec::from(&buffer[..len]));
|
||||
.ok();
|
||||
}
|
||||
} else if ep.attributes == EndpointAttributes::Interrupt as u8 {
|
||||
// interrupt
|
||||
|
|
@ -52,6 +62,20 @@ impl UsbInterfaceHandler for UsbHostHandler {
|
|||
if let Ok(len) = self.handle.read_interrupt(ep.address, &mut buffer, timeout) {
|
||||
return Ok(Vec::from(&buffer[..len]));
|
||||
}
|
||||
} else {
|
||||
// interrupt out
|
||||
self.handle.write_interrupt(ep.address, req, timeout).ok();
|
||||
}
|
||||
} else if ep.attributes == EndpointAttributes::Bulk as u8 {
|
||||
// bulk
|
||||
if let Direction::In = ep.direction() {
|
||||
// bulk in
|
||||
if let Ok(len) = self.handle.read_bulk(ep.address, &mut buffer, timeout) {
|
||||
return Ok(Vec::from(&buffer[..len]));
|
||||
}
|
||||
} else {
|
||||
// bulk out
|
||||
self.handle.write_bulk(ep.address, req, timeout).ok();
|
||||
}
|
||||
}
|
||||
Ok(vec![])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue