lesavka/server/src/usb_gadget.rs

39 lines
1.2 KiB
Rust
Raw Normal View History

2025-06-24 23:48:06 -05:00
use std::{fs::OpenOptions, io::Write, thread, time::Duration};
use anyhow::{Result, Context};
use tracing::{info, warn};
#[derive(Clone)]
pub struct UsbGadget {
udc_file: &'static str,
}
impl UsbGadgetManager {
pub fn new(gadget_name: &'static str) -> Self {
// /sys/kernel/config/usb_gadget/<name>/UDC
Self { udc_file: Box::leak(
format!("/sys/kernel/config/usb_gadget/{gadget_name}/UDC").into_boxed_str())
}
}
/// Force the host to reenumerate our HID gadget.
pub fn cycle(&self) -> Result<()> {
// 1. detach
info!("UDCcycle: detaching gadget");
OpenOptions::new().write(true).open(self.udc_file)?
.write_all(b"")?;
// 2. wait ≥ 100 ms so host sees a disconnect
std::thread::sleep(Duration::from_millis(200));
// 3. reattach to **first** UDC (dwc2)
let udc = std::fs::read_dir("/sys/class/udc")?
.next().context("no UDC present")?
.file_name();
let name = udc.to_string_lossy();
info!("UDCcycle: reattaching to {name}");
OpenOptions::new().write(true).open(self.udc_file)?
.write_all(name.as_bytes())?;
Ok(())
}
}