FunctionFS gadget test

This commit is contained in:
Marc Riera 2023-03-16 00:03:31 +01:00
parent c500940f26
commit 6c8783bd93

View file

@ -1,3 +1,8 @@
use std::thread;
use std::fs::File;
use std::io::{Read, Write};
use std::process::Command;
use crate::state::ControllerState;
mod dgoc44u;
mod tcpp20009;
@ -14,6 +19,7 @@ pub enum ControllerModel {
pub fn set_model(state: &ControllerState) -> ControllerModel {
if state.button_right {
println!("Selected controller DGOC44-U.");
init_gadget();
return ControllerModel::DGOC44U;
}
else if state.button_up {
@ -44,3 +50,29 @@ pub fn set_state(state: &mut ControllerState, model: &ControllerModel) {
_ => (),
}
}
fn init_gadget() {
Command::new("modprobe").args(["g_ffs"]).output();
Command::new("mkdir").args(["-p","/tmp/ffs-mascon"]).output();
Command::new("mount").args(["-t","functionfs","mascon","/tmp/ffs-mascon"]).output();
let descriptors = [0x03, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x09, 0x04, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00,
0x07, 0x05, 0x81, 0x03, 0x08, 0x00, 0x00];
let strings = [0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
thread::spawn(move || {
if let Ok(mut ep0) = File::open("/tmp/ffs-mascon/ep0") {
let mut buffer = [0; 100];
loop {
ep0.read(&mut buffer).ok();
}
}
});
if let Ok(mut ep0) = File::create("/tmp/ffs-mascon/ep0") {
ep0.write(&descriptors).ok();
println!("USB Gadget: Descriptors written to EP0");
ep0.write(&strings).ok();
println!("USB Gadget: Strings written to EP0");
}
}