1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
use std::fmt;
use crate::{devices, hid, features, DeviceInfo, Error, Model};
use crate::devices::RawDevice as _;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u8)]
enum Command {
GetStatus = 0x00,
GetPasswordRetryCount = 0x09,
GetUserPasswordRetryCount = 0x0F,
}
impl From<Command> for u8 {
fn from(command: Command) -> u8 {
command as u8
}
}
pub struct Pro {
hid_device: hidapi::HidDevice,
path: std::ffi::CString,
}
impl Pro {
pub(crate) fn new(hid_device: hidapi::HidDevice, device_info: &DeviceInfo<'_>) -> Self {
Self {
hid_device,
path: device_info.device_info.path().to_owned(),
}
}
}
impl features::Basic for Pro {
fn get_model(&self) -> Model {
Model::Pro
}
fn get_status(&self) -> Result<features::Status, Error> {
self.execute(Command::GetStatus, ())
}
fn get_user_retry_count(&self) -> Result<u8, Error> {
self.execute(Command::GetPasswordRetryCount, ())
}
fn get_admin_retry_count(&self) -> Result<u8, Error> {
self.execute(Command::GetUserPasswordRetryCount, ())
}
}
impl devices::RawDevice for Pro {
fn execute<T, U>(&self, command_id: impl Into<u8>, data: T) -> Result<U, Error>
where
T: serde::Serialize,
U: serde::de::DeserializeOwned,
{
use hid::HidDeviceExt as _;
let command_id = command_id.into();
let crc = self.hid_device.send(&hid::Request::new(command_id, data))?;
let mut response = self.hid_device.receive()?;
let mut retry_count = 0;
while response.device_status == hid::DeviceStatus::Busy && retry_count < 3 {
std::thread::sleep(std::time::Duration::from_millis(100));
response = self.hid_device.receive()?;
retry_count += 1;
}
if response.device_status != hid::DeviceStatus::Ok {
return Err(format!("Got device status {:?}", response.device_status).into());
}
if response.command_id != command_id {
return Err(format!(
"Expected command ID {}, got {}",
command_id, response.command_id
)
.into());
}
if response.command_status != hid::CommandStatus::Ok {
return Err(format!("Got command status {:?}", response.command_status).into());
}
if response.last_crc() != crc {
return Err(format!("Expected last_crc {}, got {}", crc, response.last_crc()).into());
}
Ok(response.data)
}
}
impl fmt::Debug for Pro {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Pro")
.field("path", &self.path)
.finish()
}
}
|