Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split usbip protocol de/serialization from server handler #31

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/cdc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,13 @@ impl UsbInterfaceHandler for UsbCdcAcmHandler {

#[cfg(test)]
mod tests {
use crate::util::tests::*;

use super::*;

#[test]
fn desc_verify() {
setup_test_logger();
let handler = UsbCdcAcmHandler::new();
verify_descriptor(&handler.get_class_specific_descriptor());
}
Expand Down
83 changes: 44 additions & 39 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ pub struct UsbDevice {
impl UsbDevice {
pub fn new(index: u32) -> Self {
let mut res = Self {
path: format!("/sys/device/usbip/{}", index),
bus_id: format!("{}", index),
path: "/sys/bus/0/0/0".to_string(),
bus_id: "0-0-0".to_string(),
dev_num: index,
speed: UsbSpeed::High as u32,
ep0_in: UsbEndpoint {
Expand Down Expand Up @@ -167,46 +167,48 @@ impl UsbDevice {
}
}

pub(crate) async fn write_dev<T: AsyncReadExt + AsyncWriteExt + Unpin>(
&self,
socket: &mut T,
) -> Result<()> {
socket_write_fixed_string(socket, &self.path, 256).await?;
socket_write_fixed_string(socket, &self.bus_id, 32).await?;

// fields
socket.write_u32(self.bus_num).await?;
socket.write_u32(self.dev_num).await?;
socket.write_u32(self.speed).await?;
socket.write_u16(self.vendor_id).await?;
socket.write_u16(self.product_id).await?;
socket
.write_u16((self.device_bcd.major as u16) << 8 | self.device_bcd.minor as u16)
.await?;
socket.write_u8(self.device_class).await?;
socket.write_u8(self.device_subclass).await?;
socket.write_u8(self.device_protocol).await?;
socket.write_u8(self.configuration_value).await?;
socket.write_u8(self.num_configurations).await?;
socket.write_u8(self.interfaces.len() as u8).await?;

Ok(())
pub(crate) fn to_bytes(&self) -> Vec<u8> {
let mut result = Vec::with_capacity(312);

let mut path = self.path.as_bytes().to_vec();
debug_assert!(path.len() <= 256);
path.resize(256, 0);
result.extend_from_slice(path.as_slice());

let mut bus_id = self.bus_id.as_bytes().to_vec();
debug_assert!(bus_id.len() <= 32);
bus_id.resize(32, 0);
result.extend_from_slice(bus_id.as_slice());

result.extend_from_slice(&self.bus_num.to_be_bytes());
result.extend_from_slice(&self.dev_num.to_be_bytes());
result.extend_from_slice(&self.speed.to_be_bytes());
result.extend_from_slice(&self.vendor_id.to_be_bytes());
result.extend_from_slice(&self.product_id.to_be_bytes());
result.push(self.device_bcd.major);
result.push(self.device_bcd.minor);
result.push(self.device_class);
result.push(self.device_subclass);
result.push(self.device_protocol);
result.push(self.configuration_value);
result.push(self.num_configurations);
result.push(self.interfaces.len() as u8);

result
}

pub(crate) async fn write_dev_with_interfaces<T: AsyncReadExt + AsyncWriteExt + Unpin>(
&self,
socket: &mut T,
) -> Result<()> {
self.write_dev(socket).await?;

for interface in &self.interfaces {
socket.write_u8(interface.interface_class).await?;
socket.write_u8(interface.interface_subclass).await?;
socket.write_u8(interface.interface_protocol).await?;
// padding
socket.write_u8(0).await?;
pub(crate) fn to_bytes_with_interfaces(&self) -> Vec<u8> {
let mut result = self.to_bytes();
result.reserve(4 * self.interfaces.len());

for intf in &self.interfaces {
result.push(intf.interface_class);
result.push(intf.interface_subclass);
result.push(intf.interface_protocol);
result.push(0); // padding
}
Ok(())

result
}

pub(crate) async fn handle_urb(
Expand Down Expand Up @@ -484,10 +486,13 @@ pub trait UsbDeviceHandler {

#[cfg(test)]
mod test {
use crate::util::tests::*;

use super::*;

#[test]
fn test_set_string_descriptors() {
setup_test_logger();
let mut device = UsbDevice::new(0);

assert_eq!(device.string_pool.len(), 4);
Expand Down
3 changes: 3 additions & 0 deletions src/hid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,13 @@ pub enum HidDescriptorType {

#[cfg(test)]
mod tests {
use crate::util::tests::*;

use super::*;

#[test]
fn desc_verify() {
setup_test_logger();
let handler = UsbHidKeyboardHandler::new_keyboard();
verify_descriptor(&handler.get_class_specific_descriptor());
}
Expand Down
Loading
Loading