Skip to content

Commit ac594c1

Browse files
committed
Propagating results outward instead of silently ignoring
1 parent ef539cb commit ac594c1

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

src/control_pipe.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,18 @@ impl<B: UsbBus> ControlPipe<'_, B> {
129129
None
130130
}
131131

132-
pub fn handle_out(&mut self) -> Option<Request> {
132+
pub fn handle_out(&mut self) -> Result<Option<Request>> {
133133
match self.state {
134134
ControlState::DataOut(req) => {
135135
let i = self.i;
136136
let count = match self.ep_out.read(&mut self.buf[i..]) {
137137
Ok(count) => count,
138-
Err(UsbError::WouldBlock) => return None,
138+
Err(UsbError::WouldBlock) => return Ok(None),
139139
Err(_) => {
140140
// Failed to read or buffer overflow (overflow is only possible if the host
141141
// sends more data than it indicated in the SETUP request)
142142
self.set_error();
143-
return None;
143+
return Ok(None);
144144
}
145145
};
146146

@@ -154,7 +154,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
154154
if self.i >= self.len {
155155
usb_debug!("Request OUT complete: {:?}", req);
156156
self.state = ControlState::CompleteOut;
157-
return Some(req);
157+
return Ok(Some(req));
158158
}
159159
}
160160
// The host may terminate a DATA stage early by sending a zero-length status packet
@@ -167,7 +167,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
167167
"Control transfer completed. Current state: {:?}",
168168
self.state
169169
);
170-
let _ = self.ep_out.read(&mut []);
170+
self.ep_out.read(&mut [])?;
171171
self.state = ControlState::Idle;
172172
}
173173
_ => {
@@ -176,14 +176,14 @@ impl<B: UsbBus> ControlPipe<'_, B> {
176176
"Discarding EP0 data due to unexpected state. Current state: {:?}",
177177
self.state
178178
);
179-
let _ = self.ep_out.read(&mut []);
179+
self.ep_out.read(&mut [])?;
180180

181181
// Unexpected OUT packet
182182
self.set_error()
183183
}
184184
}
185185

186-
None
186+
Ok(None)
187187
}
188188

189189
pub fn handle_in_complete(&mut self) -> bool {
@@ -262,7 +262,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
262262
}
263263
};
264264

265-
let _ = self.ep_in.write(&[]);
265+
self.ep_in.write(&[])?;
266266
self.state = ControlState::StatusIn;
267267
Ok(())
268268
}

src/device.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
182182
}
183183
_ => {
184184
self.bus.resume();
185+
185186
self.device_state = self
186187
.suspended_device_state
187188
.expect("Unknown state before suspend");
@@ -213,14 +214,24 @@ impl<B: UsbBus> UsbDevice<'_, B> {
213214
let req = if (ep_setup & 1) != 0 {
214215
self.control.handle_setup()
215216
} else if (ep_out & 1) != 0 {
216-
self.control.handle_out()
217+
match self.control.handle_out() {
218+
Ok(req) => req,
219+
Err(_err) => {
220+
// TODO: Propagate error out of `poll()`
221+
usb_debug!("Failed to handle EP0: {_err}");
222+
None
223+
}
224+
}
217225
} else {
218226
None
219227
};
220228

221229
match req {
222230
Some(req) if req.direction == UsbDirection::In => {
223-
self.control_in(classes, req)
231+
if let Err(_err) = self.control_in(classes, req) {
232+
// TODO: Propagate error out of `poll()`
233+
usb_debug!("Failed to handle control request: {_err}");
234+
}
224235
}
225236
Some(req) if req.direction == UsbDirection::Out => {
226237
self.control_out(classes, req)
@@ -310,14 +321,14 @@ impl<B: UsbBus> UsbDevice<'_, B> {
310321
false
311322
}
312323

313-
fn control_in(&mut self, classes: &mut ClassList<'_, B>, req: control::Request) {
324+
fn control_in(&mut self, classes: &mut ClassList<'_, B>, req: control::Request) -> Result<()> {
314325
use crate::control::{Recipient, Request};
315326

316327
for cls in classes.iter_mut() {
317328
cls.control_in(ControlIn::new(&mut self.control, &req));
318329

319330
if !self.control.waiting_for_response() {
320-
return;
331+
return Ok(());
321332
}
322333
}
323334

@@ -334,14 +345,14 @@ impl<B: UsbBus> UsbDevice<'_, B> {
334345
0x0000
335346
};
336347

337-
let _ = xfer.accept_with(&status.to_le_bytes());
348+
xfer.accept_with(&status.to_le_bytes())?;
338349
}
339350

340351
(Recipient::Interface, Request::GET_STATUS) => {
341352
usb_trace!("Processing Interface::GetStatus");
342353
let status: u16 = 0x0000;
343354

344-
let _ = xfer.accept_with(&status.to_le_bytes());
355+
xfer.accept_with(&status.to_le_bytes())?;
345356
}
346357

347358
(Recipient::Endpoint, Request::GET_STATUS) => {
@@ -354,7 +365,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
354365
0x0000
355366
};
356367

357-
let _ = xfer.accept_with(&status.to_le_bytes());
368+
xfer.accept_with(&status.to_le_bytes())?;
358369
}
359370

360371
(Recipient::Device, Request::GET_DESCRIPTOR) => {
@@ -369,39 +380,39 @@ impl<B: UsbBus> UsbDevice<'_, B> {
369380
_ => CONFIGURATION_NONE,
370381
};
371382

372-
let _ = xfer.accept_with(&config.to_le_bytes());
383+
xfer.accept_with(&config.to_le_bytes())?;
373384
}
374385

375386
(Recipient::Interface, Request::GET_INTERFACE) => {
376387
usb_trace!("Processing Interface::GetInterface");
377388
// Reject interface numbers bigger than 255
378389
if req.index > core::u8::MAX.into() {
379-
let _ = xfer.reject();
380-
return;
390+
return xfer.reject();
381391
}
382392

383393
// Ask class implementations, whether they know the alternate setting
384394
// of the interface in question
385395
for cls in classes {
386396
if let Some(setting) = cls.get_alt_setting(InterfaceNumber(req.index as u8))
387397
{
388-
let _ = xfer.accept_with(&setting.to_le_bytes());
389-
return;
398+
return xfer.accept_with(&setting.to_le_bytes());
390399
}
391400
}
392401

393402
// If no class returned an alternate setting, return the default value
394-
let _ = xfer.accept_with(&DEFAULT_ALTERNATE_SETTING.to_le_bytes());
403+
xfer.accept_with(&DEFAULT_ALTERNATE_SETTING.to_le_bytes())?;
395404
}
396405

397-
_ => (),
406+
_ => {}
398407
};
399408
}
400409

401410
if self.control.waiting_for_response() {
402411
usb_debug!("Rejecting control transfer because we were waiting for a response");
403-
let _ = self.control.reject();
412+
self.control.reject()?;
404413
}
414+
415+
Ok(())
405416
}
406417

407418
fn control_out(&mut self, classes: &mut ClassList<'_, B>, req: control::Request) {

0 commit comments

Comments
 (0)