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
#![allow(missing_docs)]

use crate::class_prelude::*;
use crate::descriptor;
use crate::device::{UsbDevice, UsbDeviceBuilder, UsbVidPid};
use crate::Result;
use core::cmp;

#[cfg(feature = "test-class-high-speed")]
mod sizes {
    pub const BUFFER: usize = 2048;
    pub const CONTROL_ENDPOINT: u8 = 64;
    pub const BULK_ENDPOINT: u16 = 512;
    pub const INTERRUPT_ENDPOINT: u16 = 1024;
}

#[cfg(not(feature = "test-class-high-speed"))]
mod sizes {
    pub const BUFFER: usize = 256;
    pub const CONTROL_ENDPOINT: u8 = 8;
    pub const BULK_ENDPOINT: u16 = 64;
    pub const INTERRUPT_ENDPOINT: u16 = 31;
}

/// Test USB class for testing USB driver implementations. Supports various endpoint types and
/// requests for testing USB peripheral drivers on actual hardware.
pub struct TestClass<'a, B: UsbBus> {
    custom_string: StringIndex,
    interface_string: StringIndex,
    iface: InterfaceNumber,
    ep_bulk_in: EndpointIn<'a, B>,
    ep_bulk_out: EndpointOut<'a, B>,
    ep_interrupt_in: EndpointIn<'a, B>,
    ep_interrupt_out: EndpointOut<'a, B>,
    control_buf: [u8; sizes::BUFFER],
    bulk_buf: [u8; sizes::BUFFER],
    interrupt_buf: [u8; sizes::BUFFER],
    len: usize,
    i: usize,
    bench: bool,
    expect_bulk_in_complete: bool,
    expect_bulk_out: bool,
    expect_interrupt_in_complete: bool,
    expect_interrupt_out: bool,
}

pub const VID: u16 = 0x16c0;
pub const PID: u16 = 0x05dc;
pub const MANUFACTURER: &str = "TestClass Manufacturer";
pub const PRODUCT: &str = "virkkunen.net usb-device TestClass";
pub const SERIAL_NUMBER: &str = "TestClass Serial";
pub const CUSTOM_STRING: &str = "TestClass Custom String";
pub const INTERFACE_STRING: &str = "TestClass Interface";

pub const REQ_STORE_REQUEST: u8 = 1;
pub const REQ_READ_BUFFER: u8 = 2;
pub const REQ_WRITE_BUFFER: u8 = 3;
pub const REQ_SET_BENCH_ENABLED: u8 = 4;
pub const REQ_READ_LONG_DATA: u8 = 5;
pub const REQ_UNKNOWN: u8 = 42;

pub const LONG_DATA: &[u8] = &[0x17; 257];

impl<B: UsbBus> TestClass<'_, B> {
    /// Creates a new TestClass.
    pub fn new(alloc: &UsbBusAllocator<B>) -> TestClass<'_, B> {
        TestClass {
            custom_string: alloc.string(),
            interface_string: alloc.string(),
            iface: alloc.interface(),
            ep_bulk_in: alloc.bulk(sizes::BULK_ENDPOINT),
            ep_bulk_out: alloc.bulk(sizes::BULK_ENDPOINT),
            ep_interrupt_in: alloc.interrupt(sizes::INTERRUPT_ENDPOINT, 1),
            ep_interrupt_out: alloc.interrupt(sizes::INTERRUPT_ENDPOINT, 1),
            control_buf: [0; sizes::BUFFER],
            bulk_buf: [0; sizes::BUFFER],
            interrupt_buf: [0; sizes::BUFFER],
            len: 0,
            i: 0,
            bench: false,
            expect_bulk_in_complete: false,
            expect_bulk_out: false,
            expect_interrupt_in_complete: false,
            expect_interrupt_out: false,
        }
    }

    /// Convenience method to create a UsbDevice that is configured correctly for TestClass.
    pub fn make_device<'a, 'b>(&'a self, usb_bus: &'b UsbBusAllocator<B>) -> UsbDevice<'b, B> {
        self.make_device_builder(usb_bus).build()
    }

    /// Convenience method to create a UsbDeviceBuilder that is configured correctly for TestClass.
    ///
    /// The methods sets
    ///
    /// - manufacturer
    /// - product
    /// - serial number
    /// - max_packet_size_0
    ///
    /// on the returned builder. If you change the manufacturer, product, or serial number fields,
    /// the test host may misbehave.
    pub fn make_device_builder<'a, 'b>(
        &'a self,
        usb_bus: &'b UsbBusAllocator<B>,
    ) -> UsbDeviceBuilder<'b, B> {
        UsbDeviceBuilder::new(usb_bus, UsbVidPid(VID, PID))
            .manufacturer(MANUFACTURER)
            .product(PRODUCT)
            .serial_number(SERIAL_NUMBER)
            .max_packet_size_0(sizes::CONTROL_ENDPOINT)
    }

    /// Must be called after polling the UsbDevice.
    pub fn poll(&mut self) {
        if self.bench {
            match self.ep_bulk_out.read(&mut self.bulk_buf) {
                Ok(_) | Err(UsbError::WouldBlock) => {}
                Err(err) => panic!("bulk bench read {:?}", err),
            };

            match self
                .ep_bulk_in
                .write(&self.bulk_buf[0..self.ep_bulk_in.max_packet_size() as usize])
            {
                Ok(_) | Err(UsbError::WouldBlock) => {}
                Err(err) => panic!("bulk bench write {:?}", err),
            };

            return;
        }

        let temp_i = self.i;
        match self.ep_bulk_out.read(&mut self.bulk_buf[temp_i..]) {
            Ok(count) => {
                if self.expect_bulk_out {
                    self.expect_bulk_out = false;
                } else {
                    panic!("unexpectedly read data from bulk out endpoint");
                }

                self.i += count;

                if count < self.ep_bulk_out.max_packet_size() as usize {
                    self.len = self.i;
                    self.i = 0;

                    self.write_bulk_in(count == 0);
                }
            }
            Err(UsbError::WouldBlock) => {}
            Err(err) => panic!("bulk read {:?}", err),
        };

        match self.ep_interrupt_out.read(&mut self.interrupt_buf) {
            Ok(count) => {
                if self.expect_interrupt_out {
                    self.expect_interrupt_out = false;
                } else {
                    panic!("unexpectedly read data from interrupt out endpoint");
                }

                self.ep_interrupt_in
                    .write(&self.interrupt_buf[0..count])
                    .expect("interrupt write");

                self.expect_interrupt_in_complete = true;
            }
            Err(UsbError::WouldBlock) => {}
            Err(err) => panic!("interrupt read {:?}", err),
        };
    }

    fn write_bulk_in(&mut self, write_empty: bool) {
        let to_write = cmp::min(
            self.len - self.i,
            self.ep_bulk_in.max_packet_size() as usize,
        );

        if to_write == 0 && !write_empty {
            self.len = 0;
            self.i = 0;

            return;
        }

        match self
            .ep_bulk_in
            .write(&self.bulk_buf[self.i..self.i + to_write])
        {
            Ok(count) => {
                assert_eq!(count, to_write);
                self.expect_bulk_in_complete = true;
                self.i += count;
            }
            Err(UsbError::WouldBlock) => {}
            Err(err) => panic!("bulk write {:?}", err),
        };
    }
}

impl<B: UsbBus> UsbClass<B> for TestClass<'_, B> {
    fn reset(&mut self) {
        self.len = 0;
        self.i = 0;
        self.bench = false;
        self.expect_bulk_in_complete = false;
        self.expect_bulk_out = false;
        self.expect_interrupt_in_complete = false;
        self.expect_interrupt_out = false;
    }

    fn get_configuration_descriptors(&self, writer: &mut DescriptorWriter) -> Result<()> {
        writer.interface(self.iface, 0xff, 0x00, 0x00)?;
        writer.endpoint(&self.ep_bulk_in)?;
        writer.endpoint(&self.ep_bulk_out)?;
        writer.endpoint(&self.ep_interrupt_in)?;
        writer.endpoint(&self.ep_interrupt_out)?;
        writer.interface_alt(self.iface, 1, 0xff, 0x01, 0x00, Some(self.interface_string))?;

        Ok(())
    }

    fn get_string(&self, index: StringIndex, lang_id: u16) -> Option<&str> {
        if lang_id == descriptor::lang_id::ENGLISH_US {
            if index == self.custom_string {
                return Some(CUSTOM_STRING);
            } else if index == self.interface_string {
                return Some(INTERFACE_STRING);
            }
        }

        None
    }

    fn endpoint_in_complete(&mut self, addr: EndpointAddress) {
        if self.bench {
            return;
        }

        if addr == self.ep_bulk_in.address() {
            if self.expect_bulk_in_complete {
                self.expect_bulk_in_complete = false;

                self.write_bulk_in(false);
            } else {
                panic!("unexpected endpoint_in_complete");
            }
        } else if addr == self.ep_interrupt_in.address() {
            if self.expect_interrupt_in_complete {
                self.expect_interrupt_in_complete = false;
            } else {
                panic!("unexpected endpoint_in_complete");
            }
        }
    }

    fn endpoint_out(&mut self, addr: EndpointAddress) {
        if addr == self.ep_bulk_out.address() {
            self.expect_bulk_out = true;
        } else if addr == self.ep_interrupt_out.address() {
            self.expect_interrupt_out = true;
        }
    }

    fn control_in(&mut self, xfer: ControlIn<B>) {
        let req = *xfer.request();

        if !(req.request_type == control::RequestType::Vendor
            && req.recipient == control::Recipient::Device)
        {
            return;
        }

        match req.request {
            REQ_READ_BUFFER if req.length as usize <= self.control_buf.len() => xfer
                .accept_with(&self.control_buf[0..req.length as usize])
                .expect("control_in REQ_READ_BUFFER failed"),
            REQ_READ_LONG_DATA => xfer
                .accept_with_static(LONG_DATA)
                .expect("control_in REQ_READ_LONG_DATA failed"),
            _ => xfer.reject().expect("control_in reject failed"),
        }
    }

    fn control_out(&mut self, xfer: ControlOut<B>) {
        let req = *xfer.request();

        if !(req.request_type == control::RequestType::Vendor
            && req.recipient == control::Recipient::Device)
        {
            return;
        }

        match req.request {
            REQ_STORE_REQUEST => {
                self.control_buf[0] =
                    (req.direction as u8) | (req.request_type as u8) << 5 | (req.recipient as u8);
                self.control_buf[1] = req.request;
                self.control_buf[2..4].copy_from_slice(&req.value.to_le_bytes());
                self.control_buf[4..6].copy_from_slice(&req.index.to_le_bytes());
                self.control_buf[6..8].copy_from_slice(&req.length.to_le_bytes());

                xfer.accept().expect("control_out REQ_STORE_REQUEST failed");
            }
            REQ_WRITE_BUFFER if xfer.data().len() as usize <= self.control_buf.len() => {
                assert_eq!(
                    xfer.data().len(),
                    req.length as usize,
                    "xfer data len == req.length"
                );

                self.control_buf[0..xfer.data().len()].copy_from_slice(xfer.data());

                xfer.accept().expect("control_out REQ_WRITE_BUFFER failed");
            }
            REQ_SET_BENCH_ENABLED => {
                self.bench = req.value != 0;

                xfer.accept()
                    .expect("control_out REQ_SET_BENCH_ENABLED failed");
            }
            _ => xfer.reject().expect("control_out reject failed"),
        }
    }
}