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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
use crate::hid_class::descriptor::{
    DescriptorType, HidProtocol, InterfaceProtocol, InterfaceSubClass, USB_CLASS_HID,
};
use crate::hid_class::{BuilderResult, UsbHidBuilderError, UsbPacketSize};
use crate::interface::{InterfaceClass, UsbAllocatable};
use core::cell::RefCell;
use fugit::{ExtU32, MillisDurationU32};
use heapless::Vec;
use log::{error, info, trace, warn};
use option_block::Block32;
use usb_device::bus::{InterfaceNumber, StringIndex, UsbBus, UsbBusAllocator};
use usb_device::class_prelude::{DescriptorWriter, EndpointIn, EndpointOut};
use usb_device::UsbError;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RawInterfaceConfig<'a> {
    pub report_descriptor: &'a [u8],
    pub description: Option<&'a str>,
    pub protocol: InterfaceProtocol,
    pub idle_default: u8,
    pub out_endpoint: Option<EndpointConfig>,
    pub in_endpoint: EndpointConfig,
}

// TODO: make configurable, size depends on number of reports for given interface,
// in most cases Block8 (max 8 reports) would be enough (size 9B vs 36B for Block32)
type ReportIdleArray = Block32<u8>;

pub struct RawInterface<'a, B: UsbBus> {
    id: InterfaceNumber,
    config: RawInterfaceConfig<'a>,
    out_endpoint: Option<EndpointOut<'a, B>>,
    in_endpoint: EndpointIn<'a, B>,
    description_index: Option<StringIndex>,
    protocol: HidProtocol,
    report_idle: ReportIdleArray,
    global_idle: u8,
    control_in_report_buffer: RefCell<Vec<u8, 64>>,
    control_out_report_buffer: RefCell<Vec<u8, 64>>,
}

impl<'a, B: UsbBus + 'a> UsbAllocatable<'a, B> for RawInterfaceConfig<'a> {
    type Allocated = RawInterface<'a, B>;

    fn allocate(self, usb_alloc: &'a UsbBusAllocator<B>) -> Self::Allocated {
        RawInterface {
            config: self,
            id: usb_alloc.interface(),
            in_endpoint: usb_alloc.interrupt(
                self.in_endpoint.max_packet_size as u16,
                self.in_endpoint.poll_interval,
            ),
            out_endpoint: self
                .out_endpoint
                .map(|c| usb_alloc.interrupt(c.max_packet_size as u16, c.poll_interval)),
            description_index: self.description.map(|_| usb_alloc.string()),
            //When initialized, all devices default to report protocol - Hid spec 7.2.6 Set_Protocol Request
            protocol: HidProtocol::Report,
            report_idle: Default::default(),
            global_idle: self.idle_default,
            control_in_report_buffer: RefCell::new(Default::default()),
            control_out_report_buffer: RefCell::new(Default::default()),
        }
    }
}

impl<'a, B: UsbBus> InterfaceClass<'a> for RawInterface<'a, B> {
    fn report_descriptor(&self) -> &'_ [u8] {
        self.config.report_descriptor
    }

    fn id(&self) -> InterfaceNumber {
        self.id
    }
    fn write_descriptors(&self, writer: &mut DescriptorWriter) -> usb_device::Result<()> {
        writer.interface_alt(
            self.id,
            usb_device::device::DEFAULT_ALTERNATE_SETTING,
            USB_CLASS_HID,
            InterfaceSubClass::from(self.config.protocol) as u8,
            self.config.protocol as u8,
            self.description_index,
        )?;

        //Hid descriptor
        writer.write(DescriptorType::Hid as u8, &self.hid_descriptor_body())?;

        //Endpoint descriptors
        writer.endpoint(&self.in_endpoint)?;
        if let Some(e) = &self.out_endpoint {
            writer.endpoint(e)?;
        }

        Ok(())
    }
    fn get_string(&self, index: StringIndex, _lang_id: u16) -> Option<&'_ str> {
        self.description_index
            .filter(|&i| i == index)
            .and(self.config.description)
    }
    fn reset(&mut self) {
        self.protocol = HidProtocol::Report;
        self.global_idle = self.config.idle_default;
        self.clear_report_idle();
        self.control_in_report_buffer.borrow_mut().clear();
        self.control_out_report_buffer.borrow_mut().clear();
    }
    fn set_report(&mut self, data: &[u8]) -> usb_device::Result<()> {
        let mut out_buffer = self.control_out_report_buffer.borrow_mut();
        if !out_buffer.is_empty() {
            trace!("Failed to set report, buffer not empty");
            Err(UsbError::WouldBlock)
        } else {
            match out_buffer.extend_from_slice(data) {
                Err(_) => {
                    error!(
                    "Failed to set report, too large for buffer. Report size {:X}, expected <={:X}",
                    data.len(),
                    &out_buffer.capacity()
                );
                    Err(UsbError::BufferOverflow)
                }
                Ok(_) => {
                    trace!("Set report, {:X} bytes", &out_buffer.len());
                    Ok(())
                }
            }
        }
    }

    fn get_report(&mut self, data: &mut [u8]) -> usb_device::Result<usize> {
        let in_buffer = self.control_in_report_buffer.borrow();
        if in_buffer.is_empty() {
            trace!("GetReport would block, empty buffer");
            Err(UsbError::WouldBlock)
        } else if data.len() < in_buffer.len() {
            error!("GetReport failed, buffer too short");
            Err(UsbError::BufferOverflow)
        } else {
            data[..in_buffer.len()].copy_from_slice(&in_buffer[..]);
            Ok(in_buffer.len())
        }
    }

    fn get_report_ack(&mut self) -> usb_device::Result<()> {
        let mut in_buffer = self.control_in_report_buffer.borrow_mut();
        if in_buffer.is_empty() {
            error!("GetReport ACK failed, empty buffer");
            Err(UsbError::WouldBlock)
        } else {
            in_buffer.clear();
            Ok(())
        }
    }

    fn set_idle(&mut self, report_id: u8, value: u8) {
        if report_id == 0 {
            self.global_idle = value;
            //"If the lower byte of value is zero, then the idle rate applies to all
            //input reports generated by the device" - HID spec 7.2.4
            self.clear_report_idle();
            info!("Set global idle to {:X}", value);
        } else if (report_id as u32) < ReportIdleArray::CAPACITY {
            self.report_idle.insert(report_id as usize, value);
            info!("Set report idle for ID{:X} to {:X}", report_id, value);
        } else {
            warn!(
                "Failed to set idle for report id {:X} - max id {:X}",
                report_id,
                ReportIdleArray::CAPACITY - 1
            );
        }
    }
    fn get_idle(&self, report_id: u8) -> u8 {
        if report_id == 0 {
            self.global_idle
        } else {
            self.get_report_idle(report_id).unwrap_or(self.global_idle)
        }
    }
    fn set_protocol(&mut self, protocol: HidProtocol) {
        self.protocol = protocol;
        info!("Set protocol to {:?}", protocol);
    }

    fn get_protocol(&self) -> HidProtocol {
        self.protocol
    }
}

impl<'a, B: UsbBus> RawInterface<'a, B> {
    fn clear_report_idle(&mut self) {
        self.report_idle = Default::default();
    }
    fn get_report_idle(&self, report_id: u8) -> Option<u8> {
        if (report_id as u32) < ReportIdleArray::CAPACITY {
            self.report_idle.get(report_id as usize).copied()
        } else {
            None
        }
    }
    pub fn protocol(&self) -> HidProtocol {
        self.protocol
    }
    pub fn global_idle(&self) -> MillisDurationU32 {
        ((self.global_idle as u32) * 4).millis()
    }
    pub fn report_idle(&self, report_id: u8) -> Option<MillisDurationU32> {
        if report_id == 0 {
            None
        } else {
            self.get_report_idle(report_id)
                .map(|i| ((i as u32) * 4).millis())
        }
    }
    pub fn write_report(&self, data: &[u8]) -> usb_device::Result<usize> {
        //Try to write report to the report buffer for the config endpoint
        let mut in_buffer = self.control_in_report_buffer.borrow_mut();
        let control_result = if in_buffer.is_empty() {
            match in_buffer.extend_from_slice(data) {
                Ok(_) => Ok(data.len()),
                Err(_) => Err(UsbError::BufferOverflow),
            }
        } else {
            Err(UsbError::WouldBlock)
        };

        //Also try to write report to the in endpoint
        let endpoint_result = self.in_endpoint.write(data);

        match (control_result, endpoint_result) {
            //OK if either succeeded
            (_, Ok(n)) => Ok(n),
            (Ok(n), _) => Ok(n),
            //non-WouldBlock errors take preference
            (Err(UsbError::WouldBlock), Err(e)) => Err(e),
            (Err(e), Err(UsbError::WouldBlock)) => Err(e),
            (_, Err(e)) => Err(e),
        }
    }
    pub fn read_report(&self, data: &mut [u8]) -> usb_device::Result<usize> {
        //If there is an out endpoint, try to read from it first
        let ep_result = if let Some(ep) = &self.out_endpoint {
            ep.read(data)
        } else {
            Err(UsbError::WouldBlock)
        };

        match ep_result {
            Err(UsbError::WouldBlock) => {
                //If there wasn't data available from the in endpoint
                //try the config endpoint report buffer
                let mut out_buffer = self.control_out_report_buffer.borrow_mut();
                if out_buffer.is_empty() {
                    Err(UsbError::WouldBlock)
                } else if data.len() < out_buffer.len() {
                    Err(UsbError::BufferOverflow)
                } else {
                    let n = out_buffer.len();
                    data[..n].copy_from_slice(&out_buffer);
                    out_buffer.clear();
                    Ok(n)
                }
            }
            _ => ep_result,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EndpointConfig {
    pub poll_interval: u8,
    pub max_packet_size: UsbPacketSize,
}

#[must_use = "this `UsbHidInterfaceBuilder` must be assigned or consumed by `::build_interface()`"]
#[derive(Clone, Debug)]
pub struct RawInterfaceBuilder<'a> {
    config: RawInterfaceConfig<'a>,
}

impl<'a> RawInterfaceBuilder<'a> {
    pub fn new(report_descriptor: &'a [u8]) -> Self {
        RawInterfaceBuilder {
            config: RawInterfaceConfig {
                report_descriptor,
                description: None,
                protocol: InterfaceProtocol::None,
                idle_default: 0,
                out_endpoint: None,
                in_endpoint: EndpointConfig {
                    max_packet_size: UsbPacketSize::Bytes8,
                    poll_interval: 20,
                },
            },
        }
    }

    pub fn boot_device(mut self, protocol: InterfaceProtocol) -> Self {
        self.config.protocol = protocol;
        self
    }

    pub fn idle_default(mut self, duration: MillisDurationU32) -> BuilderResult<Self> {
        if duration.ticks() == 0 {
            self.config.idle_default = 0;
        } else {
            let scaled_duration = duration.to_millis() / 4;

            if scaled_duration == 0 {
                //round up for 1-3ms
                self.config.idle_default = 1;
            } else {
                self.config.idle_default =
                    u8::try_from(scaled_duration).map_err(|_| UsbHidBuilderError::ValueOverflow)?;
            }
        }
        Ok(self)
    }

    pub fn description(mut self, s: &'static str) -> Self {
        self.config.description = Some(s);
        self
    }

    pub fn with_out_endpoint(
        mut self,
        max_packet_size: UsbPacketSize,
        poll_interval: MillisDurationU32,
    ) -> BuilderResult<Self> {
        self.config.out_endpoint = Some(EndpointConfig {
            max_packet_size,
            poll_interval: u8::try_from(poll_interval.to_millis())
                .map_err(|_| UsbHidBuilderError::ValueOverflow)?,
        });
        Ok(self)
    }

    pub fn without_out_endpoint(mut self) -> Self {
        self.config.out_endpoint = None;
        self
    }

    pub fn in_endpoint(
        mut self,
        max_packet_size: UsbPacketSize,
        poll_interval: MillisDurationU32,
    ) -> BuilderResult<Self> {
        self.config.in_endpoint = EndpointConfig {
            max_packet_size,
            poll_interval: u8::try_from(poll_interval.to_millis())
                .map_err(|_| UsbHidBuilderError::ValueOverflow)?,
        };
        Ok(self)
    }

    pub fn build(self) -> RawInterfaceConfig<'a> {
        self.config
    }
}