1 unstable release
new 0.1.0 | Dec 16, 2024 |
---|
#1255 in Hardware support
36KB
828 lines
mock-usb-bus
A mock USB bus implementation for use with the usb-device
crate.
See tests/ping.rs
for an example with a custom UsbClass
implementation.
License
This project is dual-licensed under the Apache-2.0 and MIT licenses.
The documentation and examples contained in this repository are licensed under the Creative Commons Zero license.
You can find a copy of the license texts in the LICENSES
directory.
This project complies with version 3.0 of the REUSE specification.
lib.rs
:
A mock USB bus implementation backed by crossbeam
channels.
This crate provides the [Bus
][] struct that implements the usb_device::bus::UsbBus
trait and can be used to test USB class implementations in software.
This crate supports multiple versions of the usb-device
crate behind these feature flags:
usb-device-v0.2
:usb-device
v0.2 (UsbBus
)usb-device-v0.3
:usb-device
v0.3 (UsbBus
)
Therefore some types from usb-device
are duplicated in this crate:
They provide From
implementations for conversion from the usb-device
types.
Example
This example uses [Bus
][] to create a USB device that writes back the data that it receives
(implemented by the Ping
class). For the full code, see tests/ping.rs
.
use mock_usb_bus::Bus;
use usb_device::{bus::UsbBusAllocator, device::{UsbDeviceBuilder, UsbVidPid}};
let bus = UsbBusAllocator::new(Bus::default());
let mut ping = Ping::new(&bus);
let mut device = UsbDeviceBuilder::new(&bus, UsbVidPid(0, 0)).build();
loop {
device.poll(&mut [&mut ping]);
}
A different thread can then use the channels provided by [Bus
][] to send data to and receive
data from the device:
let tx = device.bus().endpoint_tx(ping.endpoint_read().into()).unwrap();
let rx = device.bus().endpoint_rx(ping.endpoint_write().into()).unwrap();
let data = b"test data";
tx.send(data.into()).unwrap();
let reply = rx.recv().unwrap();
assert_eq!(data.as_slice(), reply.as_slice());
Dependencies
~100–390KB