devices/usb/backend/fido_backend/
fido_provider.rs

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
5use std::fs::File;
6use std::sync::Arc;
7
8use sync::Mutex;
9
10use crate::usb::backend::device::BackendDeviceType;
11use crate::usb::backend::device::DeviceState;
12use crate::usb::backend::error::Error;
13use crate::usb::backend::error::Result;
14use crate::usb::backend::fido_backend::fido_device::FidoDevice;
15use crate::usb::backend::fido_backend::fido_passthrough::FidoPassthroughDevice;
16use crate::usb::backend::utils::UsbUtilEventHandler;
17use crate::utils::EventHandler;
18use 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.
22pub 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}