1 // Copyright 2019 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 use std::fs::File; 6 use std::sync::Arc; 7 8 use sync::Mutex; 9 use usb_util::Device; 10 11 use crate::usb::backend::device::BackendDeviceType; 12 use crate::usb::backend::device::DeviceState; 13 use crate::usb::backend::error::Error; 14 use crate::usb::backend::error::Result; 15 use crate::usb::backend::host_backend::host_device::HostDevice; 16 use crate::usb::backend::utils::UsbUtilEventHandler; 17 use crate::utils::EventHandler; 18 19 /// Attaches a host device to the backend. This creates a host USB device object by opening a 20 /// usbdevfs file and returns a pair of the device and its fd event handler. attach_host_backend_device( usb_file: File, device_state: DeviceState, ) -> Result<(Arc<Mutex<BackendDeviceType>>, Arc<dyn EventHandler>)>21pub fn attach_host_backend_device( 22 usb_file: File, 23 device_state: DeviceState, 24 ) -> Result<(Arc<Mutex<BackendDeviceType>>, Arc<dyn EventHandler>)> { 25 let device = Device::new(usb_file).map_err(Error::CreateHostUsbDevice)?; 26 let host_device = HostDevice::new(Arc::new(Mutex::new(device)), device_state)?; 27 let device_impl = BackendDeviceType::HostDevice(host_device); 28 let arc_mutex_device = Arc::new(Mutex::new(device_impl)); 29 30 let event_handler: Arc<dyn EventHandler> = Arc::new(UsbUtilEventHandler { 31 device: arc_mutex_device.clone(), 32 }); 33 34 Ok((arc_mutex_device, event_handler)) 35 } 36