1 // Copyright 2024 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
10 use crate::usb::backend::device::BackendDeviceType;
11 use crate::usb::backend::device::DeviceState;
12 use crate::usb::backend::error::Error;
13 use crate::usb::backend::error::Result;
14 use crate::usb::backend::fido_backend::fido_device::FidoDevice;
15 use crate::usb::backend::fido_backend::fido_passthrough::FidoPassthroughDevice;
16 use crate::usb::backend::utils::UsbUtilEventHandler;
17 use crate::utils::EventHandler;
18 use crate::utils::EventLoop;
19
20 /// Utility function to attach a security key device to the backend provider. It initializes a
21 /// `FidoPassthroughDevice` and returns it with its `EventHandler` to the backend.
attach_security_key( hidraw: File, event_loop: Arc<EventLoop>, device_state: DeviceState, ) -> Result<(Arc<Mutex<BackendDeviceType>>, Arc<dyn EventHandler>)>22 pub fn attach_security_key(
23 hidraw: File,
24 event_loop: Arc<EventLoop>,
25 device_state: DeviceState,
26 ) -> Result<(Arc<Mutex<BackendDeviceType>>, Arc<dyn EventHandler>)> {
27 let device =
28 FidoDevice::new(hidraw, event_loop.clone()).map_err(Error::CreateFidoBackendDevice)?;
29 let passthrough_device =
30 FidoPassthroughDevice::new(Arc::new(Mutex::new(device)), device_state, event_loop)
31 .map_err(Error::CreateFidoBackendDevice)?;
32 let device_impl = BackendDeviceType::FidoDevice(passthrough_device);
33 let arc_mutex_device = Arc::new(Mutex::new(device_impl));
34
35 let event_handler: Arc<dyn EventHandler> = Arc::new(UsbUtilEventHandler {
36 device: arc_mutex_device.clone(),
37 });
38
39 Ok((arc_mutex_device, event_handler))
40 }
41