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

add support for android no-rusb-open workaround #48

Closed
wants to merge 4 commits into from
Closed
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
261 changes: 139 additions & 122 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ pub struct UsbIpServer {
}

impl UsbIpServer {
/// Create a [UsbIpServer] from an rusb DeviceHandle
pub fn new_from_device_handle(device_handle: DeviceHandle<GlobalContext>) -> Self {
let device = device_handle.device();
let device_descriptor = device.device_descriptor().unwrap();
let active_configuration_descriptor = device.active_config_descriptor().unwrap();
let usb_device = Self::rusb_device_to_usbip_device(device, device_handle, device_descriptor, active_configuration_descriptor);
let devices = vec![usb_device];
Self {
available_devices: RwLock::new(devices),
used_devices: RwLock::new(HashMap::new()),
}
}

/// Create a [UsbIpServer] with simulated devices
pub fn new_simulated(devices: Vec<UsbDevice>) -> Self {
Self {
Expand All @@ -54,151 +67,155 @@ impl UsbIpServer {
}
}

fn rusb_device_to_usbip_device(device: Device<GlobalContext>, device_handle: DeviceHandle<GlobalContext>, desc: DeviceDescriptor, cfg: ConfigDescriptor) -> UsbDevice {
let handle = Arc::new(Mutex::new(device_handle));
let mut interfaces = vec![];
handle
.lock()
.unwrap()
.set_auto_detach_kernel_driver(true)
.ok();
for intf in cfg.interfaces() {
// ignore alternate settings
let intf_desc = intf.descriptors().next().unwrap();
handle
.lock()
.unwrap()
.set_auto_detach_kernel_driver(true)
.ok();
let mut endpoints = vec![];

for ep_desc in intf_desc.endpoint_descriptors() {
endpoints.push(UsbEndpoint {
address: ep_desc.address(),
attributes: ep_desc.transfer_type() as u8,
max_packet_size: ep_desc.max_packet_size(),
interval: ep_desc.interval(),
});
}

let handler = Arc::new(Mutex::new(Box::new(UsbHostInterfaceHandler::new(
handle.clone(),
))
as Box<dyn UsbInterfaceHandler + Send>));
interfaces.push(UsbInterface {
interface_class: intf_desc.class_code(),
interface_subclass: intf_desc.sub_class_code(),
interface_protocol: intf_desc.protocol_code(),
endpoints,
string_interface: intf_desc.description_string_index().unwrap_or(0),
class_specific_descriptor: Vec::from(intf_desc.extra()),
handler,
});
}
let mut device = UsbDevice {
path: format!(
"/sys/bus/{}/{}/{}",
device.bus_number(),
device.address(),
device.port_number()
),
bus_id: format!(
"{}-{}-{}",
device.bus_number(),
device.address(),
device.port_number()
),
bus_num: device.bus_number() as u32,
dev_num: device.port_number() as u32,
speed: device.speed() as u32,
vendor_id: desc.vendor_id(),
product_id: desc.product_id(),
device_class: desc.class_code(),
device_subclass: desc.sub_class_code(),
device_protocol: desc.protocol_code(),
device_bcd: desc.device_version().into(),
configuration_value: cfg.number(),
num_configurations: desc.num_configurations(),
ep0_in: UsbEndpoint {
address: 0x80,
attributes: EndpointAttributes::Control as u8,
max_packet_size: desc.max_packet_size() as u16,
interval: 0,
},
ep0_out: UsbEndpoint {
address: 0x00,
attributes: EndpointAttributes::Control as u8,
max_packet_size: desc.max_packet_size() as u16,
interval: 0,
},
interfaces,
device_handler: Some(Arc::new(Mutex::new(Box::new(UsbHostDeviceHandler::new(
handle.clone(),
))))),
usb_version: desc.usb_version().into(),
..UsbDevice::default()
};

// set strings
if let Some(index) = desc.manufacturer_string_index() {
device.string_manufacturer = device.new_string(
&handle
.lock()
.unwrap()
.read_string_descriptor_ascii(index)
.unwrap(),
)
}
if let Some(index) = desc.product_string_index() {
device.string_product = device.new_string(
&handle
.lock()
.unwrap()
.read_string_descriptor_ascii(index)
.unwrap(),
)
}
if let Some(index) = desc.serial_number_string_index() {
device.string_serial = device.new_string(
&handle
.lock()
.unwrap()
.read_string_descriptor_ascii(index)
.unwrap(),
)
}

device
}

fn with_devices(device_list: Vec<Device<GlobalContext>>) -> Vec<UsbDevice> {
let mut devices = vec![];

for dev in device_list {
let open_device = match dev.open() {
Ok(dev) => dev,
for device in device_list {
let device_handle = match device.open() {
Ok(device_handle) => device_handle,
Err(err) => {
warn!("Impossible to share {:?}: {}, ignoring device", dev, err);
warn!("Impossible to share {:?}: {}, ignoring device", device, err);
continue;
}
};
let desc = match dev.device_descriptor() {
let desc = match device.device_descriptor() {
Ok(desc) => desc,
Err(err) => {
warn!(
"Impossible to get device descriptor for {:?}: {}, ignoring device",
dev, err
device, err
);
continue;
}
};
let cfg = match dev.active_config_descriptor() {
let cfg = match device.active_config_descriptor() {
Ok(desc) => desc,
Err(err) => {
warn!(
"Impossible to get config descriptor for {:?}: {}, ignoring device",
dev, err
device, err
);
continue;
}
};

let handle = Arc::new(Mutex::new(open_device));
let mut interfaces = vec![];
handle
.lock()
.unwrap()
.set_auto_detach_kernel_driver(true)
.ok();
for intf in cfg.interfaces() {
// ignore alternate settings
let intf_desc = intf.descriptors().next().unwrap();
handle
.lock()
.unwrap()
.set_auto_detach_kernel_driver(true)
.ok();
let mut endpoints = vec![];

for ep_desc in intf_desc.endpoint_descriptors() {
endpoints.push(UsbEndpoint {
address: ep_desc.address(),
attributes: ep_desc.transfer_type() as u8,
max_packet_size: ep_desc.max_packet_size(),
interval: ep_desc.interval(),
});
}

let handler = Arc::new(Mutex::new(Box::new(UsbHostInterfaceHandler::new(
handle.clone(),
))
as Box<dyn UsbInterfaceHandler + Send>));
interfaces.push(UsbInterface {
interface_class: intf_desc.class_code(),
interface_subclass: intf_desc.sub_class_code(),
interface_protocol: intf_desc.protocol_code(),
endpoints,
string_interface: intf_desc.description_string_index().unwrap_or(0),
class_specific_descriptor: Vec::from(intf_desc.extra()),
handler,
});
}
let mut device = UsbDevice {
path: format!(
"/sys/bus/{}/{}/{}",
dev.bus_number(),
dev.address(),
dev.port_number()
),
bus_id: format!(
"{}-{}-{}",
dev.bus_number(),
dev.address(),
dev.port_number()
),
bus_num: dev.bus_number() as u32,
dev_num: dev.port_number() as u32,
speed: dev.speed() as u32,
vendor_id: desc.vendor_id(),
product_id: desc.product_id(),
device_class: desc.class_code(),
device_subclass: desc.sub_class_code(),
device_protocol: desc.protocol_code(),
device_bcd: desc.device_version().into(),
configuration_value: cfg.number(),
num_configurations: desc.num_configurations(),
ep0_in: UsbEndpoint {
address: 0x80,
attributes: EndpointAttributes::Control as u8,
max_packet_size: desc.max_packet_size() as u16,
interval: 0,
},
ep0_out: UsbEndpoint {
address: 0x00,
attributes: EndpointAttributes::Control as u8,
max_packet_size: desc.max_packet_size() as u16,
interval: 0,
},
interfaces,
device_handler: Some(Arc::new(Mutex::new(Box::new(UsbHostDeviceHandler::new(
handle.clone(),
))))),
usb_version: desc.usb_version().into(),
..UsbDevice::default()
};

// set strings
if let Some(index) = desc.manufacturer_string_index() {
device.string_manufacturer = device.new_string(
&handle
.lock()
.unwrap()
.read_string_descriptor_ascii(index)
.unwrap(),
)
}
if let Some(index) = desc.product_string_index() {
device.string_product = device.new_string(
&handle
.lock()
.unwrap()
.read_string_descriptor_ascii(index)
.unwrap(),
)
}
if let Some(index) = desc.serial_number_string_index() {
device.string_serial = device.new_string(
&handle
.lock()
.unwrap()
.read_string_descriptor_ascii(index)
.unwrap(),
)
}
devices.push(device);
devices.push(Self::rusb_device_to_usbip_device(device, device_handle, desc, cfg));
}
devices
}
Expand Down
Loading