summaryrefslogtreecommitdiff
path: root/src/hid.rs
blob: 291216c2a4c557be7f5e5bc865a29caf1fc72345 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use std::convert;
use std::convert::TryFrom as _;

use crate::crc32;
use crate::Error;

pub const REPORT_LEN: usize = 65;
pub const REPORT_PAYLOAD_LEN: usize = REPORT_LEN - 5;
pub const MAX_REQUEST_DATA_LEN: usize = REPORT_PAYLOAD_LEN - 1;
pub const MAX_RESPONSE_DATA_LEN: usize = REPORT_PAYLOAD_LEN - 7;

serde_big_array::big_array! { BigArray; REPORT_PAYLOAD_LEN }

#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
struct Report {
    report_id: u8,
    #[serde(with = "BigArray")]
    payload: [u8; REPORT_PAYLOAD_LEN],
    crc: [u8; 4],
}

static_assertions::assert_eq_size!([u8; REPORT_LEN], Report);

impl Report {
    fn new(report_id: u8, payload: [u8; REPORT_PAYLOAD_LEN]) -> Report {
        let crc = crc32::crc(&payload);
        Report {
            report_id,
            payload,
            crc: crc.to_le_bytes(),
        }
    }

    fn from_bytes(data: [u8; REPORT_LEN]) -> Result<Report, Error> {
        let (report, num_bytes): (Report, _) = ssmarshal::deserialize(&data)?;
        if num_bytes != REPORT_LEN {
            return Err(format!("Only {} of {} report bytes read", num_bytes, REPORT_LEN).into());
        }
        Ok(report)
    }

    fn to_bytes(&self) -> Result<[u8; REPORT_LEN], Error> {
        let mut buf = [0; REPORT_LEN];
        let num_bytes = ssmarshal::serialize(&mut buf, self)?;
        if num_bytes != REPORT_LEN {
            return Err(format!("Invalid report length: {}", num_bytes).into());
        }
        Ok(buf)
    }

    fn crc(&self) -> u32 {
        u32::from_le_bytes(self.crc)
    }

    pub fn check_crc(&self) -> Result<(), Error> {
        let crc = crc32::crc(&self.payload);
        if self.crc() != crc {
            Err(format!(
                "Warning: Report has wrong CRC: got {:x}, expected {:x}",
                self.crc(),
                crc
            )
            .into())
        } else {
            Ok(())
        }
    }
}

impl Default for Report {
    fn default() -> Report {
        Report::new(0, [0; REPORT_PAYLOAD_LEN])
    }
}

#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]
pub struct Request<T> {
    pub command_id: u8,
    pub data: T,
}

static_assertions::assert_eq_size!([u8; REPORT_PAYLOAD_LEN - MAX_REQUEST_DATA_LEN], Request<()>,);

impl<T: serde::Serialize> Request<T> {
    pub fn new(command_id: u8, data: T) -> Request<T> {
        Request { command_id, data }
    }
}

impl<'a, T: serde::Serialize> convert::TryFrom<&'a Request<T>> for Report {
    type Error = Error;

    fn try_from(request: &'a Request<T>) -> Result<Report, Error> {
        let mut payload = [0; REPORT_PAYLOAD_LEN];
        let num_bytes = ssmarshal::serialize(&mut payload, &request)?;
        if num_bytes > REPORT_PAYLOAD_LEN {
            return Err(format!("Invalid request length: {}", num_bytes).into());
        }
        Ok(Report::new(0, payload))
    }
}

#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize)]
#[repr(u8)]
pub enum DeviceStatus {
    Ok,
    Busy,
    Error,
    ReceivedReport,
}

#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize)]
#[repr(u8)]
pub enum CommandStatus {
    Ok,
    WrongCrc,
    WrongSlot,
    SlotNotProgrammed,
    WrongPassword,
    NotAuthorized,
    TimestampWarning,
    NoNameError,
    NotSupported,
    UnknownCommand,
    AesDecFailed,
}

#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize)]
pub struct Response<T> {
    pub device_status: DeviceStatus,
    pub command_id: u8,
    pub last_crc: [u8; 4],
    pub command_status: CommandStatus,
    pub data: T,
}

static_assertions::assert_eq_size!(
    [u8; REPORT_PAYLOAD_LEN - MAX_RESPONSE_DATA_LEN],
    Response<()>,
);

impl<T: serde::de::DeserializeOwned> Response<T> {
    pub fn last_crc(&self) -> u32 {
        u32::from_le_bytes(self.last_crc)
    }
}

impl<'a, T: serde::de::DeserializeOwned> convert::TryFrom<&'a Report> for Response<T> {
    type Error = Error;

    fn try_from(report: &'a Report) -> Result<Response<T>, Error> {
        let (response, _) = ssmarshal::deserialize(&report.payload)?;
        Ok(response)
    }
}

pub trait HidDeviceExt {
    fn send<T: serde::Serialize>(&self, request: &Request<T>) -> Result<u32, Error>;
    fn receive<T: serde::de::DeserializeOwned>(&self) -> Result<Response<T>, Error>;
}

impl HidDeviceExt for hidapi::HidDevice {
    fn send<T: serde::Serialize>(&self, request: &Request<T>) -> Result<u32, Error> {
        let report = Report::try_from(request)?;
        let data = report.to_bytes()?;
        self.send_feature_report(&data)?;
        Ok(report.crc())
    }

    fn receive<T: serde::de::DeserializeOwned>(&self) -> Result<Response<T>, Error> {
        let mut buf = [0; REPORT_LEN];
        let n = self.get_feature_report(&mut buf)?;
        if n != REPORT_LEN {
            return Err(format!("Invalid report length: {}", n).into());
        }
        let report = Report::from_bytes(buf)?;
        if report.report_id != 0 {
            return Err(format!("Invalid report ID: {}", report.report_id).into());
        }
        let response = Response::try_from(&report)?;
        // TODO: is the CRC set for all other status variants?
        if response.device_status != DeviceStatus::Busy {
            report.check_crc()?;
        }
        Ok(response)
    }
}