Skip to content

Commit

Permalink
Return errors for USB/IP setup (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
ia0 authored Aug 21, 2023
1 parent 255a863 commit 7aa7bfd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions crates/runner-host/src/board/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use std::process::{Child, Command};
use std::time::Duration;

use anyhow::{ensure, Result};
use usb_device::class_prelude::UsbBusAllocator;
use usb_device::prelude::{UsbDevice, UsbDeviceBuilder, UsbVidPid};
use usb_device::UsbError;
Expand Down Expand Up @@ -58,14 +59,17 @@ impl Default for Usb {
}

impl Usb {
pub fn init() {
assert_eq!(spawn(&["sudo", "modprobe", "vhci-hcd"]).wait().unwrap().code().unwrap(), 0);
pub fn init() -> Result<()> {
ensure!(
spawn(&["sudo", "modprobe", "vhci-hcd"]).wait().unwrap().code() == Some(0),
"failed to load kernel module for USB/IP"
);
let mut usbip = spawn(&["sudo", "usbip", "attach", "-r", "localhost", "-b", "1-1"]);
loop {
with_state(|state| state.usb.poll());
match usbip.try_wait().unwrap() {
None => continue,
Some(e) => assert_eq!(e.code().unwrap(), 0),
Some(e) => ensure!(e.code() == Some(0), "failed to attach remote USB/IP device"),
}
break;
}
Expand All @@ -85,6 +89,7 @@ impl Usb {
}
}
});
Ok(())
}

pub fn poll(&mut self) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion crates/runner-host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async fn main() -> Result<()> {
storage,
});
#[cfg(feature = "usb")]
board::usb::Usb::init();
board::usb::Usb::init()?;
tokio::spawn({
async move {
for line in std::io::stdin().lock().lines() {
Expand Down

0 comments on commit 7aa7bfd

Please sign in to comment.