Crate mock_usb_bus

Crate mock_usb_bus 

Source
Expand description

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:

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());

Structs§

Bus
A mock USB bus implementation backed by crossbeam channels.
Endpoint
Information about an allocated endpoint.
EndpointAddress
The address of an endpoint.

Enums§

EndpointType
The type of an endpoint.
UsbDirection
The direction of USB traffic.