| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use crate::transport::device_selector::DeviceSelectorEvent; |
| 6 |
use crate::util::from_unix_result; |
| 7 |
use runloop::RunLoop; |
| 8 |
use std::collections::HashMap; |
| 9 |
use std::error::Error; |
| 10 |
use std::ffi::{CString, OsString}; |
| 11 |
use std::os::unix::ffi::OsStrExt; |
| 12 |
use std::os::unix::io::RawFd; |
| 13 |
use std::path::PathBuf; |
| 14 |
use std::sync::{mpsc::Sender, Arc}; |
| 15 |
use std::thread; |
| 16 |
use std::time::Duration; |
| 17 |
|
| 18 |
const POLL_TIMEOUT: u64 = 500; |
| 19 |
|
| 20 |
#[derive(Debug)] |
| 21 |
pub struct WrappedOpenDevice { |
| 22 |
pub fd: RawFd, |
| 23 |
pub os_path: OsString, |
| 24 |
} |
| 25 |
|
| 26 |
pub struct Monitor<F> |
| 27 |
where |
| 28 |
F: Fn( |
| 29 |
WrappedOpenDevice, |
| 30 |
Sender<DeviceSelectorEvent>, |
| 31 |
Sender<crate::StatusUpdate>, |
| 32 |
&dyn Fn() -> bool, |
| 33 |
) + Sync, |
| 34 |
{ |
| 35 |
runloops: HashMap<OsString, RunLoop>, |
| 36 |
new_device_cb: Arc<F>, |
| 37 |
selector_sender: Sender<DeviceSelectorEvent>, |
| 38 |
status_sender: Sender<crate::StatusUpdate>, |
| 39 |
} |
| 40 |
|
| 41 |
impl<F> Monitor<F> |
| 42 |
where |
| 43 |
F: Fn( |
| 44 |
WrappedOpenDevice, |
| 45 |
Sender<DeviceSelectorEvent>, |
| 46 |
Sender<crate::StatusUpdate>, |
| 47 |
&dyn Fn() -> bool, |
| 48 |
) + Send |
| 49 |
+ Sync |
| 50 |
+ 'static, |
| 51 |
{ |
| 52 |
pub fn new( |
| 53 |
new_device_cb: F, |
| 54 |
selector_sender: Sender<DeviceSelectorEvent>, |
| 55 |
status_sender: Sender<crate::StatusUpdate>, |
| 56 |
) -> Self { |
| 57 |
Self { |
| 58 |
runloops: HashMap::new(), |
| 59 |
new_device_cb: Arc::new(new_device_cb), |
| 60 |
selector_sender, |
| 61 |
status_sender, |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
pub fn run(&mut self, alive: &dyn Fn() -> bool) -> Result<(), Box<dyn Error>> { |
| 66 |
// Loop until we're stopped by the controlling thread, or fail. |
| 67 |
while alive() { |
| 68 |
// Iterate the first 10 fido(4) devices. |
| 69 |
for path in (0..10) |
| 70 |
.map(|unit| PathBuf::from(&format!("/dev/fido/{}", unit))) |
| 71 |
.filter(|path| path.exists()) |
| 72 |
{ |
| 73 |
let os_path = path.as_os_str().to_os_string(); |
| 74 |
let cstr = CString::new(os_path.as_bytes())?; |
| 75 |
|
| 76 |
// Try to open the device. |
| 77 |
let fd = unsafe { libc::open(cstr.as_ptr(), libc::O_RDWR) }; |
| 78 |
match from_unix_result(fd) { |
| 79 |
Ok(fd) => { |
| 80 |
// The device is available if it can be opened. |
| 81 |
let _ = self |
| 82 |
.selector_sender |
| 83 |
.send(DeviceSelectorEvent::DevicesAdded(vec![os_path.clone()])); |
| 84 |
self.add_device(WrappedOpenDevice { fd, os_path }); |
| 85 |
} |
| 86 |
Err(ref err) if err.raw_os_error() == Some(libc::EBUSY) => { |
| 87 |
// The device is available but currently in use. |
| 88 |
} |
| 89 |
_ => { |
| 90 |
// libc::ENODEV or any other error. |
| 91 |
self.remove_device(os_path); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
thread::sleep(Duration::from_millis(POLL_TIMEOUT)); |
| 97 |
} |
| 98 |
|
| 99 |
// Remove all tracked devices. |
| 100 |
self.remove_all_devices(); |
| 101 |
|
| 102 |
Ok(()) |
| 103 |
} |
| 104 |
|
| 105 |
fn add_device(&mut self, fido: WrappedOpenDevice) { |
| 106 |
if !self.runloops.contains_key(&fido.os_path) { |
| 107 |
let f = self.new_device_cb.clone(); |
| 108 |
let key = fido.os_path.clone(); |
| 109 |
let selector_sender = self.selector_sender.clone(); |
| 110 |
let status_sender = self.status_sender.clone(); |
| 111 |
let runloop = RunLoop::new(move |alive| { |
| 112 |
if alive() { |
| 113 |
f(fido, selector_sender, status_sender, alive); |
| 114 |
} |
| 115 |
}); |
| 116 |
|
| 117 |
if let Ok(runloop) = runloop { |
| 118 |
self.runloops.insert(key, runloop); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
fn remove_device(&mut self, path: OsString) { |
| 124 |
let _ = self |
| 125 |
.selector_sender |
| 126 |
.send(DeviceSelectorEvent::DeviceRemoved(path.clone())); |
| 127 |
if let Some(runloop) = self.runloops.remove(&path) { |
| 128 |
runloop.cancel(); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
fn remove_all_devices(&mut self) { |
| 133 |
while !self.runloops.is_empty() { |
| 134 |
let path = self.runloops.keys().next().unwrap().clone(); |
| 135 |
self.remove_device(path); |
| 136 |
} |
| 137 |
} |
| 138 |
} |