Skip to content

Commit 7308cad

Browse files
committed
Add test for interface descriptors
1 parent e2b45c8 commit 7308cad

File tree

2 files changed

+64
-26
lines changed

2 files changed

+64
-26
lines changed

src/test_class.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mod sizes {
2424
/// requests for testing USB peripheral drivers on actual hardware.
2525
pub struct TestClass<'a, B: UsbBus> {
2626
custom_string: StringIndex,
27+
interface_string: StringIndex,
2728
iface: InterfaceNumber,
2829
ep_bulk_in: EndpointIn<'a, B>,
2930
ep_bulk_out: EndpointOut<'a, B>,
@@ -47,6 +48,7 @@ pub const MANUFACTURER: &'static str = "TestClass Manufacturer";
4748
pub const PRODUCT: &'static str = "virkkunen.net usb-device TestClass";
4849
pub const SERIAL_NUMBER: &'static str = "TestClass Serial";
4950
pub const CUSTOM_STRING: &'static str = "TestClass Custom String";
51+
pub const INTERFACE_STRING: &'static str = "TestClass Interface";
5052

5153
pub const REQ_STORE_REQUEST: u8 = 1;
5254
pub const REQ_READ_BUFFER: u8 = 2;
@@ -63,6 +65,7 @@ impl<B: UsbBus> TestClass<'_, B> {
6365
pub fn new(alloc: &UsbBusAllocator<B>) -> TestClass<'_, B> {
6466
TestClass {
6567
custom_string: alloc.string(),
68+
interface_string: alloc.string(),
6669
iface: alloc.interface(),
6770
ep_bulk_in: alloc.bulk(sizes::BULK_ENDPOINT),
6871
ep_bulk_out: alloc.bulk(sizes::BULK_ENDPOINT),
@@ -197,16 +200,21 @@ impl<B: UsbBus> UsbClass<B> for TestClass<'_, B> {
197200
writer.endpoint(&self.ep_bulk_out)?;
198201
writer.endpoint(&self.ep_interrupt_in)?;
199202
writer.endpoint(&self.ep_interrupt_out)?;
203+
writer.interface_alt(self.iface, 1, 0xff, 0x01, 0x00, Some(self.interface_string))?;
200204

201205
Ok(())
202206
}
203207

204208
fn get_string(&self, index: StringIndex, lang_id: u16) -> Option<&str> {
205-
if index == self.custom_string && lang_id == descriptor::lang_id::ENGLISH_US {
206-
Some(CUSTOM_STRING)
207-
} else {
208-
None
209+
if lang_id == descriptor::lang_id::ENGLISH_US {
210+
if index == self.custom_string {
211+
return Some(CUSTOM_STRING)
212+
} else if index == self.interface_string {
213+
return Some(INTERFACE_STRING);
214+
}
209215
}
216+
217+
None
210218
}
211219

212220
fn endpoint_in_complete(&mut self, addr: EndpointAddress) {

tests/test_class_host/tests.rs

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,6 @@ macro_rules! tests {
3030

3131
tests! {
3232

33-
fn string_descriptors(dev, _out) {
34-
assert_eq!(
35-
dev.read_product_string(dev.en_us, &dev.device_descriptor, TIMEOUT)
36-
.expect("read product string"),
37-
test_class::PRODUCT);
38-
39-
assert_eq!(
40-
dev.read_manufacturer_string(dev.en_us, &dev.device_descriptor, TIMEOUT)
41-
.expect("read manufacturer string"),
42-
test_class::MANUFACTURER);
43-
44-
assert_eq!(
45-
dev.read_serial_number_string(dev.en_us, &dev.device_descriptor, TIMEOUT)
46-
.expect("read serial number string"),
47-
test_class::SERIAL_NUMBER);
48-
49-
assert_eq!(
50-
dev.read_string_descriptor(dev.en_us, 4, TIMEOUT)
51-
.expect("read custom string"),
52-
test_class::CUSTOM_STRING);
53-
}
54-
5533
fn control_request(dev, _out) {
5634
let mut rng = rand::thread_rng();
5735

@@ -133,6 +111,58 @@ fn control_error(dev, _out) {
133111
}
134112
}
135113

114+
fn string_descriptors(dev, _out) {
115+
assert_eq!(
116+
dev.read_product_string(dev.en_us, &dev.device_descriptor, TIMEOUT)
117+
.expect("read product string"),
118+
test_class::PRODUCT);
119+
120+
assert_eq!(
121+
dev.read_manufacturer_string(dev.en_us, &dev.device_descriptor, TIMEOUT)
122+
.expect("read manufacturer string"),
123+
test_class::MANUFACTURER);
124+
125+
assert_eq!(
126+
dev.read_serial_number_string(dev.en_us, &dev.device_descriptor, TIMEOUT)
127+
.expect("read serial number string"),
128+
test_class::SERIAL_NUMBER);
129+
130+
assert_eq!(
131+
dev.read_string_descriptor(dev.en_us, 4, TIMEOUT)
132+
.expect("read custom string"),
133+
test_class::CUSTOM_STRING);
134+
}
135+
136+
fn interface_descriptor(dev, _out) {
137+
let iface = dev.config_descriptor
138+
.interfaces()
139+
.find(|i| i.number() == 0)
140+
.expect("interface not found");
141+
142+
let default_alt_setting = iface.descriptors()
143+
.find(|i| i.setting_number() == 0)
144+
.expect("default alt setting not found");
145+
146+
assert_eq!(default_alt_setting.description_string_index(), None);
147+
assert_eq!(default_alt_setting.class_code(), 0xff);
148+
assert_eq!(default_alt_setting.sub_class_code(), 0x00);
149+
150+
let second_alt_setting = iface.descriptors()
151+
.find(|i| i.setting_number() == 1)
152+
.expect("second alt setting not found");
153+
154+
assert_eq!(second_alt_setting.class_code(), 0xff);
155+
assert_eq!(second_alt_setting.sub_class_code(), 0x01);
156+
157+
let string_index = second_alt_setting.description_string_index()
158+
.expect("second alt setting string is undefined");
159+
160+
assert_eq!(
161+
dev.read_string_descriptor(dev.en_us, string_index, TIMEOUT)
162+
.expect("read interface string"),
163+
test_class::INTERFACE_STRING);
164+
}
165+
136166
fn bulk_loopback(dev, _out) {
137167
for len in &[0, 1, 2, 32, 63, 64, 65, 127, 128, 129] {
138168
let data = random_data(*len);

0 commit comments

Comments
 (0)