Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating poll functions to return results #147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Changed
* [breaking] The `usb_device::UsbDevice::poll()` function now returns a `Result` type.

## [0.3.2] - 2024-03-06

### Added
Expand Down
4 changes: 3 additions & 1 deletion src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ pub trait UsbClass<B: UsbBus> {
fn reset(&mut self) {}

/// Called whenever the `UsbDevice` is polled.
fn poll(&mut self) {}
fn poll(&mut self) -> Result<()> {
Ok(())
}

/// Called when a control request is received with direction HostToDevice.
///
Expand Down
26 changes: 14 additions & 12 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ impl<B: UsbBus> UsbDevice<'_, B> {
///
/// Strictly speaking the list of classes is allowed to change between polls if the device has
/// been reset, which is indicated by `state` being equal to [`UsbDeviceState::Default`].
pub fn poll(&mut self, classes: &mut ClassList<'_, B>) -> bool {
pub fn poll(&mut self, classes: &mut ClassList<'_, B>) -> Result<bool> {
let pr = self.bus.poll();

if self.device_state == UsbDeviceState::Suspend {
match pr {
PollResult::Suspend | PollResult::None => {
return false;
return Ok(false);
}
_ => {
self.bus.resume();
Expand Down Expand Up @@ -228,15 +228,17 @@ impl<B: UsbBus> UsbDevice<'_, B> {

match req {
Some(req) if req.direction == UsbDirection::In => {
if let Err(_err) = self.control_in(classes, req) {
if let Err(err) = self.control_in(classes, req) {
// TODO: Propagate error out of `poll()`
usb_debug!("Failed to handle input control request: {:?}", _err);
usb_debug!("Failed to handle input control request: {:?}", err);
return Err(err);
}
}
Some(req) if req.direction == UsbDirection::Out => {
if let Err(_err) = self.control_out(classes, req) {
if let Err(err) = self.control_out(classes, req) {
// TODO: Propagate error out of `poll()`
usb_debug!("Failed to handle output control request: {:?}", _err);
usb_debug!("Failed to handle output control request: {:?}", err);
return Err(err);
}
}

Expand All @@ -248,13 +250,13 @@ impl<B: UsbBus> UsbDevice<'_, B> {
// continue with the next transfer.
let completed = match self.control.handle_in_complete() {
Ok(completed) => completed,
Err(_err) => {
Err(err) => {
// TODO: Propagate this out of `poll()`
usb_debug!(
"Failed to process control-input complete: {:?}",
_err
err
);
false
return Err(err);
}
};

Expand Down Expand Up @@ -317,10 +319,10 @@ impl<B: UsbBus> UsbDevice<'_, B> {
}

for cls in classes.iter_mut() {
cls.poll();
cls.poll()?;
}

return true;
return Ok(true);
}
PollResult::Resume => {}
PollResult::Suspend => {
Expand All @@ -331,7 +333,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
}
}

false
Ok(false)
}

fn control_in(&mut self, classes: &mut ClassList<'_, B>, req: control::Request) -> Result<()> {
Expand Down
Loading