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